kexec_file: Load kernel at top of system RAM if required
Subsystems (5)
cover kexec_file: Load kernel at top of system RAM if required
Justification:
==============Kexec_load interface has been doing top down searching and loading kernel/initrd/purgtory etc to prepare for kexec reboot. In that way, the benefits are that it avoids to consume and fragment limited low memory which satisfy DMA buffer allocation and big chunk of continuous memory during system init; and avoids to stir with BIOS/FW reserved or occupied areas, or corner case handling/work around/quirk occupied areas when doing system init. By the way, the top-down searching and loading of kexec-ed kernel is done in user space utility code.
For kexec_file loading, even if kexec_buf.top_down is 'true', it's simply ignored. It calls walk_system_ram_res() directly to go through all resources of System RAM bottom up, to find an available memory region, then call locate_mem_hole_callback() to allocate memory in that found memory region from top to down. This is not expected and inconsistent with kexec_load.
Implementation
===============
In patch 1, introduce a new function walk_system_ram_res_rev() which is a variant of walk_system_ram_res(), it walks through a list of all the resources of System RAM in reversed order, i.e., from higher to lower.
In patch 2, check if kexec_buf.top_down is 'true' in kexec_walk_resources(), if yes, call walk_system_ram_res_rev() to find memory region of system RAM from top to down to load kernel/initrd etc.
Background information:
=======================
And I ever tried this in the past in a different way, please see below link. In the post, I tried to adjust struct sibling linking code, replace the the singly linked list with list_head so that walk_system_ram_res_rev() can be implemented in a much easier way. Finally I failed.
https://lore.kernel.org/all/[email protected]/
This time, I picked up the patch from AKASHI Takahiro's old post and made some change to take as the current patch 1:
https://lists.infradead.org/pipermail/linux-arm-kernel/2017-September/531456.html
Testing:
========
Only tried on x86_64
Baoquan He (2):
resource: add walk_system_ram_res_rev()
kexec_file: Load kernel at top of system RAM if required
include/linux/ioport.h | 3 +++ kernel/kexec_file.c | 2 ++ kernel/resource.c | 61 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+)
Signature · 1 line
2.41.0
PATCH 1/2 resource: add walk_system_ram_res_rev()
This function, being a variant of walk_system_ram_res() introduced in commit 8c86e70acead ("resource: provide new functions to walk through resources"), walks through a list of all the resources of System RAM in reversed order, i.e., from higher to lower.
It will be used in kexec_file code to load kernel, initrd etc when preparing kexec reboot.
--- include/linux/ioport.h | 3 +++ kernel/resource.c | 61 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+)
include/linux/ioport.h +3 -0
@@ -331,6 +331,9 @@ extern int walk_system_ram_res(u64 start, u64 end, void *arg, int (*func)(struct resource *, void *));extern int +walk_system_ram_res_rev (u64 start,u64 end,void *arg, +int (*func)(struct resource *,void *)); +extern int walk_iomem_res_desc (unsigned long desc,unsigned long flags,u64 start,u64 end,void *arg,int (*func)(struct resource *,void *));
kernel/resource.c +61 -0
73 lines not on this page · raw message
Signature · 1 line
2.41.0
Andrew Morton
maintainerRe: [PATCH 1/2] resource: add walk_system_ram_res_rev()
Quoted 31 lines · On Tue, 14 Nov 2023 17:16:57 +0800 Baoquan He <[email protected]> wrote:
This function, being a variant of
walk_system_ram_res()introduced in commit8c86e70acead("resource: provide new functions to walk through resources"), walks through a list of all the resources of System RAM in reversed order, i.e., from higher to lower.It will be used in
kexec_filecode to load kernel, initrd etc when preparing kexec reboot....
kernel/resource.c +21
-0+/* +* This function, being a variant of walk_system_ram_res(), calls the @func +* callback against all memory ranges of type System RAM which are marked as +* IORESOURCE_SYSTEM_RAM and IORESOUCE_BUSY in reversed order, i.e., from +* higher to lower. +*/ +int walk_system_ram_res_rev (u64 start,u64 end,void *arg, +int (*func)(struct resource *,void *)) +{ +struct resource res, *rams; +int rams_size =16 , i; +unsigned long flags; +int ret =-1 ; + +/* create a list */ + rams =kvcalloc (rams_size,sizeof (struct resource ),GFP_KERNEL ); +if (!rams) +return ret; + + flags =IORESOURCE_SYSTEM_RAM |IORESOURCE_BUSY ; + i =0 ;
kernel/resource.c int walk_system_ram_res(u64 start, u64 end, void *arg, · line 464
Quoted 10 lines
kernel/resource.c +10
-0+while ((start < end) && + (!find_next_iomem_res (start, end, flags,IORES_DESC_NONE , &res))) { +if (i >= rams_size) { +/* re-alloc */ +struct resource *rams_new; +int rams_new_size; + + rams_new_size = rams_size +16 ; + rams_new =kvcalloc (rams_new_size,sizeof (struct resource ), +GFP_KERNEL );
kvrealloc()?
Quoted 33 lines
kernel/resource.c +28
-0+if (!rams_new) + goto out; + +memcpy (rams_new, rams, +sizeof (struct resource ) * rams_size); +kvfree (rams); + rams = rams_new; + rams_size = rams_new_size; + } + + rams[i].start = res.start; + rams[i++].end = res.end; + + start = res.end +1 ; + } + +/* go reverse */ +for (i--; i >=0 ; i--) { + ret = (*func)(&rams[i], arg); +if (ret) +break ; + } + +out: +kvfree (rams); +return ret; +} +/* * This function calls the @func callback against all memory ranges, which * are ranges marked as IORESOURCE_MEM and IORESOUCE_BUSY. Signature · 1 line
2.41.0
Re: [PATCH 1/2] resource: add walk_system_ram_res_rev()
Quoted 37 lines · Andrew Morton
Quoted 35 lines · On Tue, 14 Nov 2023 17:16:57 +0800 Baoquan He <[email protected]> wrote:
This function, being a variant of
walk_system_ram_res()introduced in commit8c86e70acead("resource: provide new functions to walk through resources"), walks through a list of all the resources of System RAM in reversed order, i.e., from higher to lower.It will be used in
kexec_filecode to load kernel, initrd etc when preparing kexec reboot....
kernel/resource.c +25
-0+/* +* This function, being a variant of walk_system_ram_res(), calls the @func +* callback against all memory ranges of type System RAM which are marked as +* IORESOURCE_SYSTEM_RAM and IORESOUCE_BUSY in reversed order, i.e., from +* higher to lower. +*/ +int walk_system_ram_res_rev (u64 start,u64 end,void *arg, +int (*func)(struct resource *,void *)) +{ +struct resource res, *rams; +int rams_size =16 , i; +unsigned long flags; +int ret =-1 ; + +/* create a list */ + rams =kvcalloc (rams_size,sizeof (struct resource ),GFP_KERNEL ); +if (!rams) +return ret; + + flags =IORESOURCE_SYSTEM_RAM |IORESOURCE_BUSY ; + i =0 ; +while ((start < end) && + (!find_next_iomem_res (start, end, flags,IORES_DESC_NONE , &res))) { +if (i >= rams_size) { +/* re-alloc */
Quoted 8 lines
kernel/resource.c int walk_system_ram_res(u64 start, u64 end, void *arg, · line 464
Quoted 6 lines
kernel/resource.c +6
-0+struct resource *rams_new; +int rams_new_size; + + rams_new_size = rams_size +16 ; + rams_new =kvcalloc (rams_new_size,sizeof (struct resource ), +GFP_KERNEL );
kvrealloc()?
Exactly. Will udpate. Thanks for the great suggestion.
Quoted 35 lines
Quoted 34 lines
kernel/resource.c +28
-0+if (!rams_new) + goto out; + +memcpy (rams_new, rams, +sizeof (struct resource ) * rams_size); +kvfree (rams); + rams = rams_new; + rams_size = rams_new_size; + } + + rams[i].start = res.start; + rams[i++].end = res.end; + + start = res.end +1 ; + } + +/* go reverse */ +for (i--; i >=0 ; i--) { + ret = (*func)(&rams[i], arg); +if (ret) +break ; + } + +out: +kvfree (rams); +return ret; +} +/* * This function calls the @func callback against all memory ranges, which * are ranges marked as IORESOURCE_MEM and IORESOUCE_BUSY. Signature · 1 line
2.41.0
PATCH v2 1/2 resource: add walk_system_ram_res_rev()
In reply to Baoquan HeThis function, being a variant of walk_system_ram_res() introduced in commit 8c86e70acead ("resource: provide new functions to walk through resources"), walks through a list of all the resources of System RAM in reversed order, i.e., from higher to lower.
It will be used in kexec_file code to load kernel, initrd etc when preparing kexec reboot.
---v1->v2:
- Use kvrealloc() to reallocate memory instead of kvcalloc(), this simplifies code. Suggested by Andrew.
include/linux/ioport.h | 3 +++ kernel/resource.c | 57 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+)
include/linux/ioport.h +3 -0
@@ -331,6 +331,9 @@ extern int walk_system_ram_res(u64 start, u64 end, void *arg, int (*func)(struct resource *, void *));extern int +walk_system_ram_res_rev (u64 start,u64 end,void *arg, +int (*func)(struct resource *,void *)); +extern int walk_iomem_res_desc (unsigned long desc,unsigned long flags,u64 start,u64 end,void *arg,int (*func)(struct resource *,void *));
kernel/resource.c +57 -0
69 lines not on this page · raw message
Signature · 1 line
2.41.0
Re: [PATCH v2 1/2] resource: add walk_system_ram_res_rev()
Hi Andrew,
Quoted 10 lines · Baoquan He
This function, being a variant of
walk_system_ram_res()introduced in commit8c86e70acead("resource: provide new functions to walk through resources"), walks through a list of all the resources of System RAM in reversed order, i.e., from higher to lower.It will be used in
kexec_filecode to load kernel, initrd etc when preparing kexec reboot.Signed-off-by AKASHI Takahiro <[email protected]>Signed-off-by Baoquan He <[email protected]>
Quoted 8 lines
---
v1->v2:
- Usekvrealloc()to reallocate memory instead ofkvcalloc(), this simplifies code. Suggested by Andrew.include/linux/ioport.h | 3 +++ kernel/resource.c | 57 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+)
Gentle ping.
Could you pick this patchset into next tree so that it can be run on testing robots?
Thanks
Baoquan
Quoted 93 lines
include/linux/ioport.h +3
-0@@ -331,6 +331,9 @@ extern int walk_system_ram_res(u64 start, u64 end, void *arg, int (*func)(struct resource *, void *));extern int +walk_system_ram_res_rev (u64 start,u64 end,void *arg, +int (*func)(struct resource *,void *)); +extern int walk_iomem_res_desc (unsigned long desc,unsigned long flags,u64 start,u64 end,void *arg,int (*func)(struct resource *,void *));kernel/resource.c +57
-0@@ -27,6 +27,8 @@#include <linux/mount.h> #include <linux/resource_ext.h> #include <uapi/linux/magic.h> +#include <linux/string.h> +#include <linux/vmalloc.h> #include <asm/io.h> @@ -429,6 +431,61 @@ int walk_system_ram_res(u64 start, u64 end, void *arg, func); } +/* +* This function, being a variant of walk_system_ram_res(), calls the @func +* callback against all memory ranges of type System RAM which are marked as +* IORESOURCE_SYSTEM_RAM and IORESOUCE_BUSY in reversed order, i.e., from +* higher to lower. +*/ +int walk_system_ram_res_rev (u64 start,u64 end,void *arg, +int (*func)(struct resource *,void *)) +{ +struct resource res, *rams; +int rams_size =16 , i; +unsigned long flags; +int ret =-1 ; + +/* create a list */ + rams =kvcalloc (rams_size,sizeof (struct resource ),GFP_KERNEL ); +if (!rams) +return ret; + + flags =IORESOURCE_SYSTEM_RAM |IORESOURCE_BUSY ; + i =0 ; +while ((start < end) && + (!find_next_iomem_res (start, end, flags,IORES_DESC_NONE , &res))) { +if (i >= rams_size) { +/* re-alloc */ +struct resource *rams_new; + + rams_new =kvrealloc (rams, rams_size *sizeof (struct resource ), + (rams_size +16 ) *sizeof (struct resource ), +GFP_KERNEL ); +if (!rams_new) + goto out; + + rams = rams_new; + rams_size +=16 ; + } + + rams[i].start = res.start; + rams[i++].end = res.end; + + start = res.end +1 ; + } + +/* go reverse */ +for (i--; i >=0 ; i--) { + ret = (*func)(&rams[i], arg); +if (ret) +break ; + } + +out: +kvfree (rams); +return ret; +} +/* * This function calls the @func callback against all memory ranges, which * are ranges marked as IORESOURCE_MEM and IORESOUCE_BUSY. Signature · 1 line
2.41.0
Re: [PATCH v2 1/2] resource: add walk_system_ram_res_rev()
In reply to Baoquan HeThis patch was applied to riscv/linux.git (fixes) by Andrew Morton <[email protected]>:
Hello:
This patch was applied to riscv/linux.git (fixes)
by Andrew Morton <[email protected]>:
Quoted 9 lines · On Wed, 15 Nov 2023 21:00:27 +0800 you wrote:
This function, being a variant of
walk_system_ram_res()introduced in commit8c86e70acead("resource: provide new functions to walk through resources"), walks through a list of all the resources of System RAM in reversed order, i.e., from higher to lower.It will be used in
kexec_filecode to load kernel, initrd etc when preparing kexec reboot.[...]
Here is the summary with links:
- [v2,1/2] resource: add walk_system_ram_res_rev()
https://git.kernel.org/riscv/c/7acf164b259d
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html
PATCH 2/2 kexec_file: Load kernel at top of system RAM if required
In reply to Baoquan HeKexec_load interface has been doing top down searching and loading kernel/initrd/purgtory etc to prepare for kexec reboot. In that way, the benefits are that it avoids to consume and fragment limited low memory which satisfy DMA buffer allocation and big chunk of continuous memory during system init; and avoids to stir with BIOS/FW reserved or occupied areas, or corner case handling/work around/quirk occupied areas when doing system init. By the way, the top-down searching and loading of kexec-ed kernel is done in user space utility code.
For kexec_file loading, even if kexec_buf.top_down is 'true', it's simply ignored. It calls walk_system_ram_res() directly to go through all resources of System RAM bottom up, to find an available memory region, then call locate_mem_hole_callback() to allocate memory in that found memory region from top to down. This is not expected and inconsistent with kexec_load.
Here check if kexec_buf.top_down is 'true' in kexec_walk_resources(), if yes, call the newly added walk_system_ram_res_rev() to find memory region of system RAM from top to down to load kernel/initrd etc.
--- kernel/kexec_file.c | 2 ++ 1 file changed, 2 insertions(+)
kernel/kexec_file.c +2 -0
@@ -592,6 +592,8 @@ static int kexec_walk_resources(struct kexec_buf *kbuf,IORESOURCE_SYSTEM_RAM |IORESOURCE_BUSY , crashk_res.start, crashk_res.end, kbuf, func); + else if (kbuf->top_down) + return walk_system_ram_res_rev(0, ULONG_MAX, kbuf, func); else return walk_system_ram_res(0, ULONG_MAX, kbuf, func); }
Signature · 7 lines
2.41.0 _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec
This series was applied to riscv/linux.git (fixes) by Andrew Morton <[email protected]>:
Hello:
This series was applied to riscv/linux.git (fixes) by Andrew Morton <[email protected]>:
Quoted 11 lines · On Tue, 14 Nov 2023 17:16:56 +0800 you wrote:
Justification:
==============Kexec_loadinterface has been doing top down searching and loadingkernel/initrd/purgtoryetc to prepare for kexec reboot. In that way, the benefits are that it avoids to consume and fragment limited low memory which satisfy DMA buffer allocation and big chunk of continuous memory during system init; and avoids to stir with BIOS/FW reserved or occupied areas, or corner case handling/work around/quirk occupied areas when doing system init. By the way, the top-down searching and loading of kexec-ed kernel is done in user space utility code.
Quoted 1 line
[...]
Here is the summary with links:
- [1/2] resource: add walk_system_ram_res_rev()
(no matching commit)
- [2/2] kexec_file: Load kernel at top of system RAM if required
https://git.kernel.org/riscv/c/b3ba234171cd
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html