kho: fix bootmem sizing
Subsystems (3)
Mike Rapoport (Microsoft)
maintainerv2 cover kho: fix bootmem sizing
There were several reports about issues with KHO bootmem sizing.
The bootmem areas are sized from overlapping views of the memory reserved in memblock, so on NUMA systems memory allocated on a node is accounted twice and with CONFIG_NUMA=n the lowmem and the global areas are accounted once again in the area of node 0. Besides, the lowmem and the global areas are reserved even when they are empty.
The first patch makes kexec walk the bootmem areas in address order. Otherwise kexec on arm64 may fail if a small area at the top of the memory is the first in the list, and with smaller bootmem areas this becomes more likely.
The second patch skips empty bootmem areas and the third one makes the lowmem, per-node and global areas cover separate parts of the reserved memory.
---
Changes in v2:
- add a patch that walks bootmem areas in address order
- update kho_bootmem_cnt directly in kho_reserve_bootmem_area() (Pratyush)
- don't reserve the lowmem area if ARCH_LOW_ADDRESS_LIMIT covers all
memory (Pratyush)
- extend the comment above bootmem_size_update() (Pratyush)
- Link to v1: https://patch.msgid.link/[email protected]
---
Mike Rapoport (Microsoft) (3):
kho: walk bootmem areas in address order
kho: skip zero-sized lowmem and global bootmem areas
kho: fix bootmem sizing
kernel/liveupdate/kexec_handover.c | 212 ++++++++++++++++++++++--------------- 1 file changed, 128 insertions(+), 84 deletions(-)
---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261103-kho-bootmem-sizing-5d6aed47a243
--
Sincerely yours,
Mike.
Mike Rapoport (Microsoft)
maintainerPATCH v2 1/3 kho: walk bootmem areas in address order
kexec_add_buffer() users may depend on the order in which memory holes are searched. For instance, arm64 places the kernel bottom-up and then requires the DTB to be above the kernel.
kho_walk_bootmem() walks the bootmem areas in the order they were reserved, so if the kernel lands in a small area at the top of the memory there is no room left for the DTB and kexec fails. For example, on an arm64 VM with 2G of RAM and kho_bootmem=8M,64M,64M, the lowmem area is reserved first at the top of the memory, the kernel image takes most of it and kexec fails with
kexec_file(Image): Could not find any suitable kernel location!
Sort the bootmem areas by address and walk them in ascending order, or in descending order for top-down searches, like kexec_walk_resources() does.
--- kernel/liveupdate/kexec_handover.c | 57 ++++++++++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 11 deletions(-)
kernel/liveupdate/kexec_handover.c +46 -11
@@ -26,6 +26,7 @@#include <linux/list.h> #include <linux/memblock.h> #include <linux/page-isolation.h> +#include <linux/sort.h> #include <linux/unaligned.h> #include <linux/vmalloc.h>
@@ -820,6 +821,14 @@ bool kho_bootmem_overlap(phys_addr_t phys, size_t size)return false; } +static int __init kho_bootmem_cmp (const void *_a,const void *_b) +{ +const struct kho_bootmem *a = _a; +const struct kho_bootmem *b = _b; + +return cmp_int (a->addr, b->addr); +} +/** * kho_reserve_bootmem - Reserve a contiguous chunk of memory for kexec *
@@ -904,6 +913,9 @@ static void __init kho_reserve_bootmem(void)*/ kho_bootmem_cnt = i; +sort (kho_bootmem, kho_bootmem_cnt,sizeof (*kho_bootmem), + kho_bootmem_cmp,NULL ); +return ; err_free_bootmem_areas:
@@ -2081,20 +2093,40 @@ int kho_fill_kimage(struct kimage *image)return 0 ; } -static int kho_walk_bootmem (struct kexec_buf *kbuf, -int (*func)(struct resource *,void *)) +static int kho_bootmem_walk_one (struct kexec_buf *kbuf, +const struct kho_bootmem *area, +int (*func)(struct resource *,void *)) +{ +struct resource res = { + .start = area->addr, + .end = area->addr + area->size -1 , + }; + +/* Try to fit the kbuf into our KHO bootmem region */ +return func (&res, kbuf); +} + +static int kho_bootmem_walk_top_down (struct kexec_buf *kbuf, +int (*func)(struct resource *,void *)) {int ret =0 ; -int i; -for (i =0 ; i < kho_bootmem_cnt; i++) { -struct resource res = { - .start = kho_bootmem[i].addr, - .end = kho_bootmem[i].addr + kho_bootmem[i].size -1 , - }; +for (int i = kho_bootmem_cnt -1 ; i >=0 ; i--) { + ret =kho_bootmem_walk_one (kbuf, &kho_bootmem[i], func); +if (ret) +break ; + } + +return ret; +} -/* Try to fit the kimage into our KHO bootmem region */ - ret =func (&res, kbuf); +static int kho_bootmem_walk_bottom_up (struct kexec_buf *kbuf, +int (*func)(struct resource *,void *)) +{ +int ret =0 ; + +for (int i =0 ; i < kho_bootmem_cnt; i++) { + ret =kho_bootmem_walk_one (kbuf, &kho_bootmem[i], func);if (ret)break ; }
@@ -2110,7 +2142,10 @@ int kho_locate_mem_hole(struct kexec_buf *kbuf,if (!kho_enable || kbuf->image->type ==KEXEC_TYPE_CRASH )return 1 ; - ret =kho_walk_bootmem (kbuf, func); +if (kbuf->top_down) + ret =kho_bootmem_walk_top_down (kbuf, func); +else + ret =kho_bootmem_walk_bottom_up (kbuf, func);return ret ==1 ?0 : -EADDRNOTAVAIL ; }
Signature · 1 line
2.53.0
Re: [PATCH v2 1/3] kho: walk bootmem areas in address order
Quoted 4 lines · Mike Rapoport (Microsoft)
kexec_add_buffer()users may depend on the order in which memory holes are searched. For instance, arm64 places the kernel bottom-up and then requires the DTB to be above the kernel.
Quoted 8 lines
kho_walk_bootmem()walks the bootmem areas in the order they were reserved, so if the kernel lands in a small area at the top of the memory there is no room left for the DTB and kexec fails. For example, on an arm64 VM with 2G of RAM and kho_bootmem=8M,64M,64M, the lowmem area is reserved first at the top of the memory, the kernel image takes most of it and kexec fails with[...]
Signature · 1 line
Pasha
Re: [PATCH v2 1/3] kho: walk bootmem areas in address order
In reply to Mike Rapoport (Microsoft)Quoted 11 lines · Mike Rapoport (Microsoft)
kexec_add_buffer()users may depend on the order in which memory holes are searched. For instance, arm64 places the kernel bottom-up and then requires the DTB to be above the kernel.
kho_walk_bootmem()walks the bootmem areas in the order they were reserved, so if the kernel lands in a small area at the top of the memory there is no room left for the DTB and kexec fails. For example, on an arm64 VM with 2G of RAM and kho_bootmem=8M,64M,64M, the lowmem area is reserved first at the top of the memory, the kernel image takes most of it and kexec fails with
Quoted 7 lines
kexec_file(Image): Could not find any suitable kernel location!Sort the bootmem areas by address and walk them in ascending order, or in descending order for top-down searches, like
kexec_walk_resources()does.Fixes 3bdecc3c93f9 ("kexec: add KHO support to kexec file loads")Signed-off-by Mike Rapoport (Microsoft) <[email protected]>
Signature · 2 lines
cheers, jose a. p-a
Re: [PATCH v2 1/3] kho: walk bootmem areas in address order
In reply to Mike Rapoport (Microsoft)Quoted 11 lines · Mike Rapoport (Microsoft)
kexec_add_buffer()users may depend on the order in which memory holes are searched. For instance, arm64 places the kernel bottom-up and then requires the DTB to be above the kernel.
kho_walk_bootmem()walks the bootmem areas in the order they were reserved, so if the kernel lands in a small area at the top of the memory there is no room left for the DTB and kexec fails. For example, on an arm64 VM with 2G of RAM and kho_bootmem=8M,64M,64M, the lowmem area is reserved first at the top of the memory, the kernel image takes most of it and kexec fails with
Quoted 7 lines
kexec_file(Image): Could not find any suitable kernel location!Sort the bootmem areas by address and walk them in ascending order, or in descending order for top-down searches, like
kexec_walk_resources()does.Fixes 3bdecc3c93f9 ("kexec: add KHO support to kexec file loads")Signed-off-by Mike Rapoport (Microsoft) <[email protected]>
[...]
Signature · 2 lines
Regards, Pratyush Yadav
Re: [PATCH v2 1/3] kho: walk bootmem areas in address order
In reply to Mike Rapoport (Microsoft)Hi Mike,
Quoted 50 lines · Mike Rapoport (Microsoft)
kexec_add_buffer()users may depend on the order in which memory holes are searched. For instance, arm64 places the kernel bottom-up and then requires the DTB to be above the kernel.
kho_walk_bootmem()walks the bootmem areas in the order they were reserved, so if the kernel lands in a small area at the top of the memory there is no room left for the DTB and kexec fails. For example, on an arm64 VM with 2G of RAM and kho_bootmem=8M,64M,64M, the lowmem area is reserved first at the top of the memory, the kernel image takes most of it and kexec fails with
kexec_file(Image): Could not find any suitable kernel location!Sort the bootmem areas by address and walk them in ascending order, or in descending order for top-down searches, like
kexec_walk_resources()does.Fixes 3bdecc3c93f9 ("kexec: add KHO support to kexec file loads")Signed-off-by Mike Rapoport (Microsoft) <[email protected]>--- kernel/liveupdate/kexec_handover.c | 57 ++++++++++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 11 deletions(-)kernel/liveupdate/kexec_handover.c +9
-0@@ -26,6 +26,7 @@#include <linux/list.h> #include <linux/memblock.h> #include <linux/page-isolation.h> +#include <linux/sort.h> #include <linux/unaligned.h> #include <linux/vmalloc.h> @@ -820,6 +821,14 @@ bool kho_bootmem_overlap(phys_addr_t phys, size_t size)return false; } +static int __init kho_bootmem_cmp (const void *_a,const void *_b) +{ +const struct kho_bootmem *a = _a; +const struct kho_bootmem *b = _b; + +return cmp_int (a->addr, b->addr); +} +/** * kho_reserve_bootmem - Reserve a contiguous chunk of memory for kexec *
kernel/liveupdate/kexec_handover.c static void __init kho_reserve_bootmem(void) · line 917
Quoted 5 lines
kernel/liveupdate/kexec_handover.c +2
-0@@ -904,6 +913,9 @@ static void __init kho_reserve_bootmem(void)*/ kho_bootmem_cnt = i; +sort (kho_bootmem, kho_bootmem_cnt,sizeof (*kho_bootmem), + kho_bootmem_cmp,NULL );
This only covers the case where this kernel allocated the areas itself. On a KHO boot the array comes from the previous kernel. A kernel with this series that is handed over from one without it therefore keeps walking the areas in reservation order for every following kexec_file_load, and the arm64 failure you describe comes back until the next cold boot and vise versa because the ABI string is still "kho-v4".
So, sorting once in kho_memory_init_early after kho_bootmem is mapped would make the invariant independent of the producer. When the previous kernel does have the sort this is a no-op, but the consumer cannot tell which kernel recorded the array, because the ABI string is the same.
Quoted 25 lines
kernel/liveupdate/kexec_handover.c +16
-2+return ; err_free_bootmem_areas:@@ -2081,20 +2093,40 @@ int kho_fill_kimage(struct kimage *image)return 0 ; } -static int kho_walk_bootmem (struct kexec_buf *kbuf, -int (*func)(struct resource *,void *)) +static int kho_bootmem_walk_one (struct kexec_buf *kbuf, +const struct kho_bootmem *area, +int (*func)(struct resource *,void *)) +{ +struct resource res = { + .start = area->addr, + .end = area->addr + area->size -1 , + }; + +/* Try to fit the kbuf into our KHO bootmem region */ +return func (&res, kbuf); +} + +static int kho_bootmem_walk_top_down (struct kexec_buf *kbuf, +int (*func)(struct resource *,void *))
kernel/liveupdate/kexec_handover.c int kho_fill_kimage(struct kimage *image) · line 2114
Quoted 10 lines
kernel/liveupdate/kexec_handover.c +1
-6{int ret =0 ; -int i; -for (i =0 ; i < kho_bootmem_cnt; i++) { -struct resource res = { - .start = kho_bootmem[i].addr, - .end = kho_bootmem[i].addr + kho_bootmem[i].size -1 , - }; +for (int i = kho_bootmem_cnt -1 ; i >=0 ; i--) {
kho_bootmem_cnt is unsigned, but the reverse loops in kho_reserve_bootmem() and kho_bootmem_walk_top_down() start an int counter at kho_bootmem_cnt - 1, which for a zero count is UINT_MAX converted to int. That conversion is implementation-defined; with the current compilers it yields -1 and the loop body is skipped.
How about using this one.
for (unsigned int i = kho_bootmem_cnt; i-- > 0;)
Quoted 33 lines
kernel/liveupdate/kexec_handover.c +18
-3+ ret =kho_bootmem_walk_one (kbuf, &kho_bootmem[i], func); +if (ret) +break ; + } + +return ret; +} -/* Try to fit the kimage into our KHO bootmem region */ - ret =func (&res, kbuf); +static int kho_bootmem_walk_bottom_up (struct kexec_buf *kbuf, +int (*func)(struct resource *,void *)) +{ +int ret =0 ; + +for (int i =0 ; i < kho_bootmem_cnt; i++) { + ret =kho_bootmem_walk_one (kbuf, &kho_bootmem[i], func);if (ret)break ; }@@ -2110,7 +2142,10 @@ int kho_locate_mem_hole(struct kexec_buf *kbuf,if (!kho_enable || kbuf->image->type ==KEXEC_TYPE_CRASH )return 1 ; - ret =kho_walk_bootmem (kbuf, func); +if (kbuf->top_down) + ret =kho_bootmem_walk_top_down (kbuf, func); +else + ret =kho_bootmem_walk_bottom_up (kbuf, func);return ret ==1 ?0 : -EADDRNOTAVAIL ; }
Signature · 2 lines
Regards, Mukesh Pilaniya
Mike Rapoport
maintainerRe: [PATCH v2 1/3] kho: walk bootmem areas in address order
Quoted 56 lines · Mukesh Pilaniya
Hi Mike,
Quoted 53 lines · Mike Rapoport (Microsoft)
kexec_add_buffer()users may depend on the order in which memory holes are searched. For instance, arm64 places the kernel bottom-up and then requires the DTB to be above the kernel.
kho_walk_bootmem()walks the bootmem areas in the order they were reserved, so if the kernel lands in a small area at the top of the memory there is no room left for the DTB and kexec fails. For example, on an arm64 VM with 2G of RAM and kho_bootmem=8M,64M,64M, the lowmem area is reserved first at the top of the memory, the kernel image takes most of it and kexec fails with
kexec_file(Image): Could not find any suitable kernel location!Sort the bootmem areas by address and walk them in ascending order, or in descending order for top-down searches, like
kexec_walk_resources()does.Fixes 3bdecc3c93f9 ("kexec: add KHO support to kexec file loads")Signed-off-by Mike Rapoport (Microsoft) <[email protected]>--- kernel/liveupdate/kexec_handover.c | 57 ++++++++++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 11 deletions(-)kernel/liveupdate/kexec_handover.c +9
-0@@ -26,6 +26,7 @@#include <linux/list.h> #include <linux/memblock.h> #include <linux/page-isolation.h> +#include <linux/sort.h> #include <linux/unaligned.h> #include <linux/vmalloc.h> @@ -820,6 +821,14 @@ bool kho_bootmem_overlap(phys_addr_t phys, size_t size)return false; } +static int __init kho_bootmem_cmp (const void *_a,const void *_b) +{ +const struct kho_bootmem *a = _a; +const struct kho_bootmem *b = _b; + +return cmp_int (a->addr, b->addr); +} +/** * kho_reserve_bootmem - Reserve a contiguous chunk of memory for kexec * @@ -904,6 +913,9 @@ static void __init kho_reserve_bootmem(void)*/ kho_bootmem_cnt = i;
Quoted 9 lines
kernel/liveupdate/kexec_handover.c static void __init kho_reserve_bootmem(void) · line 917
Quoted 2 lines
kernel/liveupdate/kexec_handover.c +2
-0+sort (kho_bootmem, kho_bootmem_cnt,sizeof (*kho_bootmem), + kho_bootmem_cmp,NULL );This only covers the case where this kernel allocated the areas itself. On a KHO boot the array comes from the previous kernel. A kernel with this series that is handed over from one without it therefore keeps walking the areas in reservation order for every following
kexec_file_load, and the arm64 failure you describe comes back until the next cold boot and vise versa because the ABI string is still "kho-v4".
If we were able to kexec from a kernel with no reservation ordering, the issue is not there, isn't it?
And after a cycle of KHO boots to newer kernels we anyway can get to the point when bootmem is not large enough.
Quoted 6 lines
Quoted 6 lines
kernel/liveupdate/kexec_handover.c +2
-1+static int kho_bootmem_walk_top_down (struct kexec_buf *kbuf, +int (*func)(struct resource *,void *)) {int ret =0 ; -int i;
Quoted 12 lines
kernel/liveupdate/kexec_handover.c int kho_fill_kimage(struct kimage *image) · line 2114
Quoted 6 lines
kernel/liveupdate/kexec_handover.c +1
-5-for (i =0 ; i < kho_bootmem_cnt; i++) { -struct resource res = { - .start = kho_bootmem[i].addr, - .end = kho_bootmem[i].addr + kho_bootmem[i].size -1 , - }; +for (int i = kho_bootmem_cnt -1 ; i >=0 ; i--) {
kho_bootmem_cntis unsigned, but the reverse loops inkho_reserve_bootmem()andkho_bootmem_walk_top_down()start an int counter atkho_bootmem_cnt- 1, which for a zero count isUINT_MAXconverted to int. That conversion is implementation-defined; with the current compilers it yields -1 and the loop body is skipped.
We never get here with kho_bootmem_cnt == 0.
Quoted 4 lines
How about using this one.
for (unsigned int i =kho_bootmem_cnt; i-- > 0;)Reviewed-by Mukesh Pilaniya <[email protected]>
Thanks!
Quoted 4 lines
Signature · 2 lines
Regards, Mukesh Pilaniya
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH v2 1/3] kho: walk bootmem areas in address order
Hi Mike,
Thanks for the quick check. I apologies in advance if I am missing something obvious.
Quoted 67 lines · Mike Rapoport
Quoted 56 lines · Mukesh Pilaniya
Hi Mike,
Quoted 53 lines · Mike Rapoport (Microsoft)
kexec_add_buffer()users may depend on the order in which memory holes are searched. For instance, arm64 places the kernel bottom-up and then requires the DTB to be above the kernel.
kho_walk_bootmem()walks the bootmem areas in the order they were reserved, so if the kernel lands in a small area at the top of the memory there is no room left for the DTB and kexec fails. For example, on an arm64 VM with 2G of RAM and kho_bootmem=8M,64M,64M, the lowmem area is reserved first at the top of the memory, the kernel image takes most of it and kexec fails with
kexec_file(Image): Could not find any suitable kernel location!Sort the bootmem areas by address and walk them in ascending order, or in descending order for top-down searches, like
kexec_walk_resources()does.Fixes 3bdecc3c93f9 ("kexec: add KHO support to kexec file loads")Signed-off-by Mike Rapoport (Microsoft) <[email protected]>--- kernel/liveupdate/kexec_handover.c | 57 ++++++++++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 11 deletions(-)kernel/liveupdate/kexec_handover.c +9
-0@@ -26,6 +26,7 @@#include <linux/list.h> #include <linux/memblock.h> #include <linux/page-isolation.h> +#include <linux/sort.h> #include <linux/unaligned.h> #include <linux/vmalloc.h> @@ -820,6 +821,14 @@ bool kho_bootmem_overlap(phys_addr_t phys, size_t size)return false; } +static int __init kho_bootmem_cmp (const void *_a,const void *_b) +{ +const struct kho_bootmem *a = _a; +const struct kho_bootmem *b = _b; + +return cmp_int (a->addr, b->addr); +} +/** * kho_reserve_bootmem - Reserve a contiguous chunk of memory for kexec * @@ -904,6 +913,9 @@ static void __init kho_reserve_bootmem(void)*/ kho_bootmem_cnt = i;Quoted 9 lines
kernel/liveupdate/kexec_handover.c static void __init kho_reserve_bootmem(void) · line 917
Quoted 2 lines
kernel/liveupdate/kexec_handover.c +2
-0+sort (kho_bootmem, kho_bootmem_cnt,sizeof (*kho_bootmem), + kho_bootmem_cmp,NULL );This only covers the case where this kernel allocated the areas itself. On a KHO boot the array comes from the previous kernel. A kernel with this series that is handed over from one without it therefore keeps walking the areas in reservation order for every following
kexec_file_load, and the arm64 failure you describe comes back until the next cold boot and vise versa because the ABI string is still "kho-v4".
Quoted 2 lines
If we were able to kexec from a kernel with no reservation ordering, the issue is not there, isn't it?
IMHO that holds for the image that was just handed over, but maybe not for the next one. The producer's last kexec_file_load did place that exact Image, DTB and command line through the areas in that order, so I agree that reloading the same image from the consumer will find the same spot. The order is still arbitrary though, and whether it works seems to depend on the size of the image being loaded.
To give you the case I was looking at: on a QEMU arm64 guest with 4K pages, 2 GiB of RAM and kho_bootmem=44M,64M,64M, the 44 MiB lowmem area is reserved first, at the top of RAM (0xb8800000). The Image is about 38 MiB, and the DTB has to go above it on a 2 MiB boundary, which lands exactly on 0xbb400000, the end of that area. The load then fails with "kexec_file_load failed: Cannot assign requested address", and the next area is never tried, because the walk stops at the first area that cannot take the buffer. With the areas walked in address order the same image is placed in a lower, larger area and boots. So the first area decides, and a few MiB of growth in the Image or a larger DTB is enough to flip the result -- which is the part that makes me a bit nervous about the first kexec after a handover.
It also does not settle down the chain, as far as I can tell. A kernel booted by KHO never reserves, so it never sorts: B inherits A's order, C inherits B's order, and only a cold boot sorts. That makes the same binary behave differently depending on how it was started. Sorting in kho_mem_retrieve as well would cover it.
Quoted 3 lines
And after a cycle of KHO boots to newer kernels we anyway can get to the point when bootmem is not large enough.
That is the same argument, and it is probably why I am not fully convinced that the previous successful kexec covers the ordering case either. The inherited state was adequate for the load that produced the running kernel, not necessarily for the load it is about to perform.
Quoted 20 lines
Quoted 7 lines
Quoted 7 lines
kernel/liveupdate/kexec_handover.c +2
-1+static int kho_bootmem_walk_top_down (struct kexec_buf *kbuf, +int (*func)(struct resource *,void *)) {int ret =0 ; -int i;Quoted 12 lines
kernel/liveupdate/kexec_handover.c int kho_fill_kimage(struct kimage *image) · line 2114
Quoted 6 lines
kernel/liveupdate/kexec_handover.c +1
-5-for (i =0 ; i < kho_bootmem_cnt; i++) { -struct resource res = { - .start = kho_bootmem[i].addr, - .end = kho_bootmem[i].addr + kho_bootmem[i].size -1 , - }; +for (int i = kho_bootmem_cnt -1 ; i >=0 ; i--) {
kho_bootmem_cntis unsigned, but the reverse loops inkho_reserve_bootmem()andkho_bootmem_walk_top_down()start an int counter atkho_bootmem_cnt- 1, which for a zero count isUINT_MAXconverted to int. That conversion is implementation-defined; with the current compilers it yields -1 and the loop body is skipped.
Quoted 1 line
We never get here with
kho_bootmem_cnt== 0.
Unless I am misreading it, we do, via kho_bootmem=0%. The parser accepts it and stores the scale without a zero check, then bootmem_size_update() takes the explicit-size branch and assigns the zero-initialised bootmem_size_pernode to every node. kho_reserve_bootmem_area() returns early for a zero size without recording the area, so kho_bootmem_cnt stays 0 and kho_enable is left alone and only the error path clears it.
kho_locate_mem_hole() is guarded by kho_enable only, so with cnt == 0 both walks return 0 and the caller turns that into -EADDRNOTAVAIL: KHO is enabled with no regions and every kexec_file_load fails with "Cannot assign requested address".
FWIW this became possible with 214f9649 commit, before it the zero-sized areas were recorded, so cnt was never 0. The zero-scale patch you posted in the v4 series [v3 1/5]thread closes this one.
Quoted 13 lines
Quoted 5 lines
How about using this one.
for (unsigned int i =kho_bootmem_cnt; i-- > 0;)Reviewed-by Mukesh Pilaniya <[email protected]>Thanks!
Quoted 4 lines
Signature · 2 lines
Regards, Mukesh Pilaniya
Signature · 2 lines
Regards, Mukesh Pilaniya
Mike Rapoport
maintainerRe: [PATCH v2 1/3] kho: walk bootmem areas in address order
Quoted 82 lines · Mukesh Pilaniya
Hi Mike,
Thanks for the quick check. I apologies in advance if I am missing something obvious.
Quoted 67 lines · Mike Rapoport
Quoted 56 lines · Mukesh Pilaniya
Hi Mike,
Quoted 53 lines · Mike Rapoport (Microsoft)
kexec_add_buffer()users may depend on the order in which memory holes are searched. For instance, arm64 places the kernel bottom-up and then requires the DTB to be above the kernel.
kho_walk_bootmem()walks the bootmem areas in the order they were reserved, so if the kernel lands in a small area at the top of the memory there is no room left for the DTB and kexec fails. For example, on an arm64 VM with 2G of RAM and kho_bootmem=8M,64M,64M, the lowmem area is reserved first at the top of the memory, the kernel image takes most of it and kexec fails with
kexec_file(Image): Could not find any suitable kernel location!Sort the bootmem areas by address and walk them in ascending order, or in descending order for top-down searches, like
kexec_walk_resources()does.Fixes 3bdecc3c93f9 ("kexec: add KHO support to kexec file loads")Signed-off-by Mike Rapoport (Microsoft) <[email protected]>--- kernel/liveupdate/kexec_handover.c | 57 ++++++++++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 11 deletions(-)kernel/liveupdate/kexec_handover.c +9
-0@@ -26,6 +26,7 @@#include <linux/list.h> #include <linux/memblock.h> #include <linux/page-isolation.h> +#include <linux/sort.h> #include <linux/unaligned.h> #include <linux/vmalloc.h> @@ -820,6 +821,14 @@ bool kho_bootmem_overlap(phys_addr_t phys, size_t size)return false; } +static int __init kho_bootmem_cmp (const void *_a,const void *_b) +{ +const struct kho_bootmem *a = _a; +const struct kho_bootmem *b = _b; + +return cmp_int (a->addr, b->addr); +} +/** * kho_reserve_bootmem - Reserve a contiguous chunk of memory for kexec * @@ -904,6 +913,9 @@ static void __init kho_reserve_bootmem(void)*/ kho_bootmem_cnt = i;Quoted 9 lines
kernel/liveupdate/kexec_handover.c static void __init kho_reserve_bootmem(void) · line 917
Quoted 2 lines
kernel/liveupdate/kexec_handover.c +2
-0+sort (kho_bootmem, kho_bootmem_cnt,sizeof (*kho_bootmem), + kho_bootmem_cmp,NULL );This only covers the case where this kernel allocated the areas itself. On a KHO boot the array comes from the previous kernel. A kernel with this series that is handed over from one without it therefore keeps walking the areas in reservation order for every following
kexec_file_load, and the arm64 failure you describe comes back until the next cold boot and vise versa because the ABI string is still "kho-v4".Quoted 2 lines
If we were able to kexec from a kernel with no reservation ordering, the issue is not there, isn't it?
IMHO that holds for the image that was just handed over, but maybe not for the next one. The producer's last
kexec_file_loaddid place that exact Image, DTB and command line through the areas in that order, so I agree that reloading the same image from the consumer will find the same spot. The order is still arbitrary though, and whether it works seems to depend on the size of the image being loaded.
Quoted 12 lines
To give you the case I was looking at: on a QEMU arm64 guest with 4K pages, 2 GiB of RAM and kho_bootmem=44M,64M,64M, the 44 MiB lowmem area is reserved first, at the top of RAM (
0xb8800000). The Image is about 38 MiB, and the DTB has to go above it on a 2 MiB boundary, which lands exactly on0xbb400000, the end of that area. The load then fails with "kexec_file_loadfailed: Cannot assign requested address", and the next area is never tried, because the walk stops at the first area that cannot take the buffer. With the areas walked in address order the same image is placed in a lower, larger area and boots. So the first area decides, and a few MiB of growth in the Image or a larger DTB is enough to flip the result -- which is the part that makes me a bit nervous about the first kexec after a handover.
There's always a risk that KHO will run out of bootmem, even presuming we fixed all our bugs :)
In your example it happens sooner then it should because there's still free memory that kexec won't use, but it's still the same - we've run out of bootmem.
I prefer to keep the sorting in the cold boot going forward to save a few cycles at kexec time, so I'd rather not add sorting to the kexec'ed boot.
So the kernel from 7.5 will have it right and if there's a case when we use an older stable kernel for the cold boot, and we see that we *really* need sorting after kexec there we can just add such sort as a fix for stable.
BTW, in your case it's enough to switch lowmem and global sizes to gain a few megs for future image growth, isn't it?
Quoted 68 lines
It also does not settle down the chain, as far as I can tell. A kernel booted by KHO never reserves, so it never sorts: B inherits A's order, C inherits B's order, and only a cold boot sorts. That makes the same binary behave differently depending on how it was started. Sorting in
kho_mem_retrieveas well would cover it.Quoted 3 lines
And after a cycle of KHO boots to newer kernels we anyway can get to the point when bootmem is not large enough.
That is the same argument, and it is probably why I am not fully convinced that the previous successful kexec covers the ordering case either. The inherited state was adequate for the load that produced the running kernel, not necessarily for the load it is about to perform.
Quoted 20 lines
Quoted 7 lines
Quoted 7 lines
kernel/liveupdate/kexec_handover.c +2
-1+static int kho_bootmem_walk_top_down (struct kexec_buf *kbuf, +int (*func)(struct resource *,void *)) {int ret =0 ; -int i;Quoted 12 lines
kernel/liveupdate/kexec_handover.c int kho_fill_kimage(struct kimage *image) · line 2114
Quoted 6 lines
kernel/liveupdate/kexec_handover.c +1
-5-for (i =0 ; i < kho_bootmem_cnt; i++) { -struct resource res = { - .start = kho_bootmem[i].addr, - .end = kho_bootmem[i].addr + kho_bootmem[i].size -1 , - }; +for (int i = kho_bootmem_cnt -1 ; i >=0 ; i--) {
kho_bootmem_cntis unsigned, but the reverse loops inkho_reserve_bootmem()andkho_bootmem_walk_top_down()start an int counter atkho_bootmem_cnt- 1, which for a zero count isUINT_MAXconverted to int. That conversion is implementation-defined; with the current compilers it yields -1 and the loop body is skipped.Quoted 1 line
We never get here with
kho_bootmem_cnt== 0.Unless I am misreading it, we do, via kho_bootmem=0%. The parser accepts it and stores the scale without a zero check, then
bootmem_size_update()takes the explicit-size branch and assigns the zero-initialisedbootmem_size_pernodeto every node.kho_reserve_bootmem_area()returns early for a zero size without recording the area, sokho_bootmem_cntstays 0 andkho_enableis left alone and only the error path clears it.
kho_locate_mem_hole()is guarded bykho_enableonly, so with cnt == 0 both walks return 0 and the caller turns that into-EADDRNOTAVAIL: KHO is enabled with no regions and everykexec_file_loadfails with "Cannot assign requested address".FWIW this became possible with
214f9649commit, before it the zero-sized areas were recorded, so cnt was never 0. The zero-scale patch you posted in the v4 series [v3 1/5]thread closes this one.Quoted 13 lines
Quoted 5 lines
How about using this one.
for (unsigned int i =kho_bootmem_cnt; i-- > 0;)Reviewed-by Mukesh Pilaniya <[email protected]>Thanks!
Quoted 4 lines
Signature · 2 lines
Regards, Mukesh PilaniyaSignature · 2 lines
Regards, Mukesh Pilaniya
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH v2 1/3] kho: walk bootmem areas in address order
Hi Mike,
Quoted 99 lines · Mike Rapoport
Quoted 82 lines · Mukesh Pilaniya
Hi Mike,
Thanks for the quick check. I apologies in advance if I am missing something obvious.
Quoted 67 lines · Mike Rapoport
Quoted 56 lines · Mukesh Pilaniya
Hi Mike,
Quoted 53 lines · Mike Rapoport (Microsoft)
kexec_add_buffer()users may depend on the order in which memory holes are searched. For instance, arm64 places the kernel bottom-up and then requires the DTB to be above the kernel.
kho_walk_bootmem()walks the bootmem areas in the order they were reserved, so if the kernel lands in a small area at the top of the memory there is no room left for the DTB and kexec fails. For example, on an arm64 VM with 2G of RAM and kho_bootmem=8M,64M,64M, the lowmem area is reserved first at the top of the memory, the kernel image takes most of it and kexec fails with
kexec_file(Image): Could not find any suitable kernel location!Sort the bootmem areas by address and walk them in ascending order, or in descending order for top-down searches, like
kexec_walk_resources()does.Fixes 3bdecc3c93f9 ("kexec: add KHO support to kexec file loads")Signed-off-by Mike Rapoport (Microsoft) <[email protected]>--- kernel/liveupdate/kexec_handover.c | 57 ++++++++++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 11 deletions(-)kernel/liveupdate/kexec_handover.c +9
-0@@ -26,6 +26,7 @@#include <linux/list.h> #include <linux/memblock.h> #include <linux/page-isolation.h> +#include <linux/sort.h> #include <linux/unaligned.h> #include <linux/vmalloc.h> @@ -820,6 +821,14 @@ bool kho_bootmem_overlap(phys_addr_t phys, size_t size)return false; } +static int __init kho_bootmem_cmp (const void *_a,const void *_b) +{ +const struct kho_bootmem *a = _a; +const struct kho_bootmem *b = _b; + +return cmp_int (a->addr, b->addr); +} +/** * kho_reserve_bootmem - Reserve a contiguous chunk of memory for kexec * @@ -904,6 +913,9 @@ static void __init kho_reserve_bootmem(void)*/ kho_bootmem_cnt = i;Quoted 9 lines
kernel/liveupdate/kexec_handover.c static void __init kho_reserve_bootmem(void) · line 917
Quoted 2 lines
kernel/liveupdate/kexec_handover.c +2
-0+sort (kho_bootmem, kho_bootmem_cnt,sizeof (*kho_bootmem), + kho_bootmem_cmp,NULL );This only covers the case where this kernel allocated the areas itself. On a KHO boot the array comes from the previous kernel. A kernel with this series that is handed over from one without it therefore keeps walking the areas in reservation order for every following
kexec_file_load, and the arm64 failure you describe comes back until the next cold boot and vise versa because the ABI string is still "kho-v4".Quoted 2 lines
If we were able to kexec from a kernel with no reservation ordering, the issue is not there, isn't it?
IMHO that holds for the image that was just handed over, but maybe not for the next one. The producer's last
kexec_file_loaddid place that exact Image, DTB and command line through the areas in that order, so I agree that reloading the same image from the consumer will find the same spot. The order is still arbitrary though, and whether it works seems to depend on the size of the image being loaded.Quoted 12 lines
To give you the case I was looking at: on a QEMU arm64 guest with 4K pages, 2 GiB of RAM and kho_bootmem=44M,64M,64M, the 44 MiB lowmem area is reserved first, at the top of RAM (
0xb8800000). The Image is about 38 MiB, and the DTB has to go above it on a 2 MiB boundary, which lands exactly on0xbb400000, the end of that area. The load then fails with "kexec_file_loadfailed: Cannot assign requested address", and the next area is never tried, because the walk stops at the first area that cannot take the buffer. With the areas walked in address order the same image is placed in a lower, larger area and boots. So the first area decides, and a few MiB of growth in the Image or a larger DTB is enough to flip the result -- which is the part that makes me a bit nervous about the first kexec after a handover.There's always a risk that KHO will run out of bootmem, even presuming we fixed all our bugs :)
Quoted 6 lines
In your example it happens sooner then it should because there's still free memory that kexec won't use, but it's still the same - we've run out of bootmem.
I prefer to keep the sorting in the cold boot going forward to save a few cycles at kexec time, so I'd rather not add sorting to the kexec'ed boot.
Fair enough, and I'll drop the sorting-in-retrieve suggestion.
Quoted 4 lines
So the kernel from 7.5 will have it right and if there's a case when we use an older stable kernel for the cold boot, and we see that we *really* need sorting after kexec there we can just add such sort as a fix for stable.
That works for me. The change is small, so the backport should be mechanical.
Quoted 73 lines
BTW, in your case it's enough to switch lowmem and global sizes to gain a few megs for future image growth, isn't it?
Quoted 68 lines
It also does not settle down the chain, as far as I can tell. A kernel booted by KHO never reserves, so it never sorts: B inherits A's order, C inherits B's order, and only a cold boot sorts. That makes the same binary behave differently depending on how it was started. Sorting in
kho_mem_retrieveas well would cover it.Quoted 3 lines
And after a cycle of KHO boots to newer kernels we anyway can get to the point when bootmem is not large enough.
That is the same argument, and it is probably why I am not fully convinced that the previous successful kexec covers the ordering case either. The inherited state was adequate for the load that produced the running kernel, not necessarily for the load it is about to perform.
Quoted 20 lines
Quoted 7 lines
Quoted 7 lines
kernel/liveupdate/kexec_handover.c +2
-1+static int kho_bootmem_walk_top_down (struct kexec_buf *kbuf, +int (*func)(struct resource *,void *)) {int ret =0 ; -int i;Quoted 12 lines
kernel/liveupdate/kexec_handover.c int kho_fill_kimage(struct kimage *image) · line 2114
Quoted 6 lines
kernel/liveupdate/kexec_handover.c +1
-5-for (i =0 ; i < kho_bootmem_cnt; i++) { -struct resource res = { - .start = kho_bootmem[i].addr, - .end = kho_bootmem[i].addr + kho_bootmem[i].size -1 , - }; +for (int i = kho_bootmem_cnt -1 ; i >=0 ; i--) {
kho_bootmem_cntis unsigned, but the reverse loops inkho_reserve_bootmem()andkho_bootmem_walk_top_down()start an int counter atkho_bootmem_cnt- 1, which for a zero count isUINT_MAXconverted to int. That conversion is implementation-defined; with the current compilers it yields -1 and the loop body is skipped.Quoted 1 line
We never get here with
kho_bootmem_cnt== 0.Unless I am misreading it, we do, via kho_bootmem=0%. The parser accepts it and stores the scale without a zero check, then
bootmem_size_update()takes the explicit-size branch and assigns the zero-initialisedbootmem_size_pernodeto every node.kho_reserve_bootmem_area()returns early for a zero size without recording the area, sokho_bootmem_cntstays 0 andkho_enableis left alone and only the error path clears it.
kho_locate_mem_hole()is guarded bykho_enableonly, so with cnt == 0 both walks return 0 and the caller turns that into-EADDRNOTAVAIL: KHO is enabled with no regions and everykexec_file_loadfails with "Cannot assign requested address".FWIW this became possible with
214f9649commit, before it the zero-sized areas were recorded, so cnt was never 0. The zero-scale patch you posted in the v4 series [v3 1/5]thread closes this one.Quoted 13 lines
Quoted 5 lines
How about using this one.
for (unsigned int i =kho_bootmem_cnt; i-- > 0;)Reviewed-by Mukesh Pilaniya <[email protected]>Thanks!
Quoted 4 lines
Signature · 2 lines
Regards, Mukesh PilaniyaSignature · 2 lines
Regards, Mukesh Pilaniya
Signature · 2 lines
Regards, Mukesh Pilaniya
Mike Rapoport (Microsoft)
maintainerPATCH v2 2/3 kho: skip zero-sized lowmem and global bootmem areas
In reply to Mike Rapoport (Microsoft)The lowmem and the global bootmem areas can have zero size, for example when none or all of the memory is below ARCH_LOW_ADDRESS_LIMIT, or when either of those areas is explicitly set to zero with kho_bootmem=.
For a zero-sized allocation memblock returns an address without reserving anything, and that empty area is recorded in kho_bootmem. If there is no memory below ARCH_LOW_ADDRESS_LIMIT, allocation of the empty lowmem area fails and KHO gets disabled.
Extract a helper that checks for zero size and allocates memory from memblock, and use it to skip empty areas the same way as empty per-node areas are skipped.
--- kernel/liveupdate/kexec_handover.c | 66 +++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 36 deletions(-)
kernel/liveupdate/kexec_handover.c +30 -36
@@ -829,6 +829,26 @@ static int __init kho_bootmem_cmp(const void *_a, const void *_b)return cmp_int (a->addr, b->addr); } +static int __init kho_reserve_bootmem_area (phys_addr_t size,phys_addr_t end, +int nid) +{ +phys_addr_t addr; + +if (!size) +return 0 ; + + addr =memblock_alloc_range_nid (size,BOOTMEM_ALIGNMENT_BYTES ,0 , end, + nid, true); +if (!addr) +return -ENOMEM ; + + kho_bootmem[kho_bootmem_cnt].addr = addr; + kho_bootmem[kho_bootmem_cnt].size = size; + kho_bootmem_cnt++; + +return 0 ; +} +/** * kho_reserve_bootmem - Reserve a contiguous chunk of memory for kexec *
@@ -840,8 +860,8 @@ static int __init kho_bootmem_cmp(const void *_a, const void *_b)*/ static void __init kho_reserve_bootmem (void ) { -int nid, i =0 , max_nr_bootmem; -phys_addr_t addr, size; +int nid, max_nr_bootmem; +phys_addr_t size;if (!kho_enable)return ;
@@ -861,66 +881,40 @@ static void __init kho_reserve_bootmem(void)* reserve bootmem area in low memory for lowmem allocations in the * next kernel */ - size = bootmem_size_lowmem; - addr =memblock_phys_alloc_range (size,BOOTMEM_ALIGNMENT_BYTES ,0 , -ARCH_LOW_ADDRESS_LIMIT ); -if (!addr) { +if (kho_reserve_bootmem_area (bootmem_size_lowmem,ARCH_LOW_ADDRESS_LIMIT , +NUMA_NO_NODE )) {pr_err ("Failed to reserve lowmem bootmem\n" ); goto err_free_bootmem_desc; } - kho_bootmem[i].addr = addr; - kho_bootmem[i].size = size; - i++; -/* reserve large contiguous area for allocations without nid */ - size = bootmem_size_global; - addr =memblock_phys_alloc (size,BOOTMEM_ALIGNMENT_BYTES ); -if (!addr) { +if (kho_reserve_bootmem_area (bootmem_size_global, +MEMBLOCK_ALLOC_ACCESSIBLE ,NUMA_NO_NODE )) {pr_err ("Failed to reserve global bootmem\n" ); goto err_free_bootmem_areas; } - kho_bootmem[i].addr = addr; - kho_bootmem[i].size = size; - i++; -/* * Loop over nodes that have both memory and are online. Skip * memoryless nodes, as we can not allocate bootmem areas there. */ for_each_node_state (nid,N_MEMORY ) { - size =bootmem_size_node (nid); -if (!size) -continue ; - - addr =memblock_alloc_range_nid (size,BOOTMEM_ALIGNMENT_BYTES , -0 ,MEMBLOCK_ALLOC_ACCESSIBLE , - nid, true); -if (!addr) { +if (kho_reserve_bootmem_area (bootmem_size_node (nid), +MEMBLOCK_ALLOC_ACCESSIBLE , nid)) {pr_err ("Failed to reserve nid %d bootmem\n" , nid); goto err_free_bootmem_areas; } - - kho_bootmem[i].addr = addr; - kho_bootmem[i].size = size; - i++; } -/* -* Nodes that needed no bootmem were skipped, so fewer areas than -* initially estimated may have been reserved. -*/ - kho_bootmem_cnt = i; -sort (kho_bootmem, kho_bootmem_cnt,sizeof (*kho_bootmem), kho_bootmem_cmp,NULL );return ; err_free_bootmem_areas: -for (i--; i >=0 ; i--) +for (int i = kho_bootmem_cnt -1 ; i >=0 ; i--)memblock_phys_free (kho_bootmem[i].addr, kho_bootmem[i].size); + kho_bootmem_cnt =0 ; err_free_bootmem_desc:memblock_free (kho_bootmem, max_nr_bootmem *sizeof (*kho_bootmem)); err_disable_kho:
Signature · 1 line
2.53.0
Re: [PATCH v2 2/3] kho: skip zero-sized lowmem and global bootmem areas
Quoted 10 lines · Mike Rapoport (Microsoft)
The lowmem and the global bootmem areas can have zero size, for example when none or all of the memory is below
ARCH_LOW_ADDRESS_LIMIT, or when either of those areas is explicitly set to zero with kho_bootmem=.For a zero-sized allocation memblock returns an address without reserving anything, and that empty area is recorded in
kho_bootmem. If there is no memory belowARCH_LOW_ADDRESS_LIMIT, allocation of the empty lowmem area fails and KHO gets disabled.[...]
Signature · 1 line
Pasha
Re: [PATCH v2 2/3] kho: skip zero-sized lowmem and global bootmem areas
In reply to Mike Rapoport (Microsoft)Quoted 9 lines · Mike Rapoport (Microsoft)
The lowmem and the global bootmem areas can have zero size, for example when none or all of the memory is below
ARCH_LOW_ADDRESS_LIMIT, or when either of those areas is explicitly set to zero with kho_bootmem=.For a zero-sized allocation memblock returns an address without reserving anything, and that empty area is recorded in
kho_bootmem. If there is no memory belowARCH_LOW_ADDRESS_LIMIT, allocation of the empty lowmem area fails and KHO gets disabled.
Quoted 8 lines
Extract a helper that checks for zero size and allocates memory from memblock, and use it to skip empty areas the same way as empty per-node areas are skipped.
Fixes 3dc92c311498 ("kexec: add Kexec HandOver (KHO) generation helpers")Reported-by Chen Pei <[email protected]>Signed-off-by Mike Rapoport (Microsoft) <[email protected]>
Signature · 2 lines
cheers, jose a. p-a
Re: [PATCH v2 2/3] kho: skip zero-sized lowmem and global bootmem areas
In reply to Mike Rapoport (Microsoft)Quoted 9 lines · Mike Rapoport (Microsoft)
The lowmem and the global bootmem areas can have zero size, for example when none or all of the memory is below
ARCH_LOW_ADDRESS_LIMIT, or when either of those areas is explicitly set to zero with kho_bootmem=.For a zero-sized allocation memblock returns an address without reserving anything, and that empty area is recorded in
kho_bootmem. If there is no memory belowARCH_LOW_ADDRESS_LIMIT, allocation of the empty lowmem area fails and KHO gets disabled.
Quoted 8 lines
Extract a helper that checks for zero size and allocates memory from memblock, and use it to skip empty areas the same way as empty per-node areas are skipped.
Fixes 3dc92c311498 ("kexec: add Kexec HandOver (KHO) generation helpers")Reported-by Chen Pei <[email protected]>Signed-off-by Mike Rapoport (Microsoft) <[email protected]>
[...]
Signature · 2 lines
Regards, Pratyush Yadav
Re: [PATCH v2 2/3] kho: skip zero-sized lowmem and global bootmem areas
In reply to Mike Rapoport (Microsoft)Hi Mike,
Quoted 29 lines · Mike Rapoport (Microsoft)
The lowmem and the global bootmem areas can have zero size, for example when none or all of the memory is below
ARCH_LOW_ADDRESS_LIMIT, or when either of those areas is explicitly set to zero with kho_bootmem=.For a zero-sized allocation memblock returns an address without reserving anything, and that empty area is recorded in
kho_bootmem. If there is no memory belowARCH_LOW_ADDRESS_LIMIT, allocation of the empty lowmem area fails and KHO gets disabled.Extract a helper that checks for zero size and allocates memory from memblock, and use it to skip empty areas the same way as empty per-node areas are skipped.
Fixes 3dc92c311498 ("kexec: add Kexec HandOver (KHO) generation helpers")Reported-by Chen Pei <[email protected]>Signed-off-by Mike Rapoport (Microsoft) <[email protected]>--- kernel/liveupdate/kexec_handover.c | 66 +++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 36 deletions(-)kernel/liveupdate/kexec_handover.c +0
-0@@ -829,6 +829,26 @@ static int __init kho_bootmem_cmp(const void *_a, const void *_b)return cmp_int (a->addr, b->addr); }
kernel/liveupdate/kexec_handover.c static int __init kho_bootmem_cmp(const void *_a, const void *_b) · line 841
Quoted 10 lines
kernel/liveupdate/kexec_handover.c +10
-0+static int __init kho_reserve_bootmem_area (phys_addr_t size,phys_addr_t end, +int nid) +{ +phys_addr_t addr; + +if (!size) +return 0 ; + + addr =memblock_alloc_range_nid (size,BOOTMEM_ALIGNMENT_BYTES ,0 , end, + nid, true);
The low memory and global areas are reserved with NUMA_NO_NODE, but kho_reserve_bootmem_area passes exact_nid = true for them as well. memblock only uses the flag for a valid node So Pass numa_valid_node(nid) instant of true.
Quoted 84 lines
kernel/liveupdate/kexec_handover.c +18
-33+if (!addr) +return -ENOMEM ; + + kho_bootmem[kho_bootmem_cnt].addr = addr; + kho_bootmem[kho_bootmem_cnt].size = size; + kho_bootmem_cnt++; + +return 0 ; +} +/** * kho_reserve_bootmem - Reserve a contiguous chunk of memory for kexec * @@ -840,8 +860,8 @@ static int __init kho_bootmem_cmp(const void *_a, const void *_b)*/ static void __init kho_reserve_bootmem (void ) { -int nid, i =0 , max_nr_bootmem; -phys_addr_t addr, size; +int nid, max_nr_bootmem; +phys_addr_t size;if (!kho_enable)return ;@@ -861,66 +881,40 @@ static void __init kho_reserve_bootmem(void)* reserve bootmem area in low memory for lowmem allocations in the * next kernel */ - size = bootmem_size_lowmem; - addr =memblock_phys_alloc_range (size,BOOTMEM_ALIGNMENT_BYTES ,0 , -ARCH_LOW_ADDRESS_LIMIT ); -if (!addr) { +if (kho_reserve_bootmem_area (bootmem_size_lowmem,ARCH_LOW_ADDRESS_LIMIT , +NUMA_NO_NODE )) {pr_err ("Failed to reserve lowmem bootmem\n" ); goto err_free_bootmem_desc; } - kho_bootmem[i].addr = addr; - kho_bootmem[i].size = size; - i++; -/* reserve large contiguous area for allocations without nid */ - size = bootmem_size_global; - addr =memblock_phys_alloc (size,BOOTMEM_ALIGNMENT_BYTES ); -if (!addr) { +if (kho_reserve_bootmem_area (bootmem_size_global, +MEMBLOCK_ALLOC_ACCESSIBLE ,NUMA_NO_NODE )) {pr_err ("Failed to reserve global bootmem\n" ); goto err_free_bootmem_areas; } - kho_bootmem[i].addr = addr; - kho_bootmem[i].size = size; - i++; -/* * Loop over nodes that have both memory and are online. Skip * memoryless nodes, as we can not allocate bootmem areas there. */ for_each_node_state (nid,N_MEMORY ) { - size =bootmem_size_node (nid); -if (!size) -continue ; - - addr =memblock_alloc_range_nid (size,BOOTMEM_ALIGNMENT_BYTES , -0 ,MEMBLOCK_ALLOC_ACCESSIBLE , - nid, true); -if (!addr) { +if (kho_reserve_bootmem_area (bootmem_size_node (nid), +MEMBLOCK_ALLOC_ACCESSIBLE , nid)) {pr_err ("Failed to reserve nid %d bootmem\n" , nid); goto err_free_bootmem_areas; } - - kho_bootmem[i].addr = addr; - kho_bootmem[i].size = size; - i++; } -/* -* Nodes that needed no bootmem were skipped, so fewer areas than -* initially estimated may have been reserved. -*/
kernel/liveupdate/kexec_handover.c static void __init kho_reserve_bootmem(void) · line 915
Quoted 10 lines
kernel/liveupdate/kexec_handover.c +1
-3- kho_bootmem_cnt = i; -sort (kho_bootmem, kho_bootmem_cnt,sizeof (*kho_bootmem), kho_bootmem_cmp,NULL );return ; err_free_bootmem_areas: -for (i--; i >=0 ; i--) +for (int i = kho_bootmem_cnt -1 ; i >=0 ; i--)
Check the First patch comment on this.
Quoted 6 lines
kernel/liveupdate/kexec_handover.c +1
-0memblock_phys_free (kho_bootmem[i].addr, kho_bootmem[i].size); + kho_bootmem_cnt =0 ; err_free_bootmem_desc:memblock_free (kho_bootmem, max_nr_bootmem *sizeof (*kho_bootmem)); err_disable_kho:
Signature · 2 lines
Regards, Mukesh Pilaniya
Mike Rapoport (Microsoft)
maintainerPATCH v2 3/3 kho: fix bootmem sizing
In reply to Mike Rapoport (Microsoft)Bootmem areas are sized from overlapping views of the memory reserved in memblock:
* the global area is sized from all reserved memory, including memory allocated on specific nodes, so on NUMA systems everything allocated on a node is accounted in both the global area and the area of that node.
* per-node sizes are calculated after the lowmem and the global areas are reserved. With CONFIG_NUMA=n all memblock regions belong to node 0, so the lowmem and the global areas are accounted in the size of node 0 area and scaled once again.
As a result, bootmem is about twice bigger than it should be on NUMA systems and about four times bigger with CONFIG_NUMA=n. On small systems reservation of the bootmem may fail and then KHO gets disabled.
Make the areas cover separate parts of the reserved memory: the lowmem area covers allocations below ARCH_LOW_ADDRESS_LIMIT, per-node areas cover allocations above it on each node and the global area covers allocations that are not bound to any node. Calculate all sizes before reserving any of the areas.
With CONFIG_NUMA=n the global area becomes empty and it is not reserved.
If ARCH_LOW_ADDRESS_LIMIT covers all memory, as may happen on arm64, any area can serve lowmem allocations, so don't reserve the lowmem area.
Bootmem sizes in MiB in x86 QEMU VMs, before -> after:
CONFIG_NUMA=y CONFIG_NUMA=n
1G, 1 node 176 -> 92 320 -> 80 4G, 1 node 508 -> 332 1264 -> 316 16G, 2 nodes 1268 -> 704 2944 -> 736 32G, 4 nodes 2296 -> 1224 5196 -> 1300 64G, 4 nodes 4344 -> 2248 9664 -> 2420
--- kernel/liveupdate/kexec_handover.c | 93 ++++++++++++++++++++++---------------- 1 file changed, 54 insertions(+), 39 deletions(-)
kernel/liveupdate/kexec_handover.c +54 -39
@@ -685,6 +685,7 @@ static unsigned int bootmem_scale __initdata = 200;static phys_addr_t bootmem_size_global__initdata ;static phys_addr_t bootmem_size_pernode__initdata ;static phys_addr_t bootmem_size_lowmem__initdata ; +static phys_addr_t bootmem_size_node[MAX_NUMNODES ]__initdata ;static int __init kho_parse_bootmem_size (char *p) {
@@ -754,29 +755,58 @@ static int __init kho_parse_bootmem_size(char *p) }early_param ("kho_bootmem" , kho_parse_bootmem_size); +static phys_addr_t __init reserved_mem_size (phys_addr_t limit,int nid) +{ +return memblock_reserved_kern_size (limit, nid) - +memblock_reserved_hugetlb_size (limit, nid); +} + +static phys_addr_t __init bootmem_scale_size (phys_addr_t size) +{ +return size * bootmem_scale /100 ; +} + +/* +* The lowmem area covers allocations below ARCH_LOW_ADDRESS_LIMIT, per-node +* areas cover allocations above it on each node and the global area covers +* allocations that are not bound to any node. +* +* The reserved memory on a node and the total reserved memory include +* lowmem, so it is subtracted from both. +* +* If ARCH_LOW_ADDRESS_LIMIT covers all memory, any area can serve lowmem +* allocations and there is no lowmem area. +* +* All sizes must be calculated before any area is reserved, otherwise the +* areas reserved first will be accounted in the sizes of the others. +*/ static void __init bootmem_size_update (void ) { -/* -* If fixed sizes are not provided via command line, calculate them now. -* Remove HugeTLB allocations from it because they never get allocated -* from bootmem. -*/ +int nid; +if (bootmem_scale) { -phys_addr_t size; - - size =memblock_reserved_kern_size (ARCH_LOW_ADDRESS_LIMIT , -NUMA_NO_NODE ); - size -=memblock_reserved_hugetlb_size (ARCH_LOW_ADDRESS_LIMIT , -NUMA_NO_NODE ); - size = size * bootmem_scale /100 ; - bootmem_size_lowmem = size; - - size =memblock_reserved_kern_size (MEMBLOCK_ALLOC_ANYWHERE , -NUMA_NO_NODE ); - size -=memblock_reserved_hugetlb_size (MEMBLOCK_ALLOC_ANYWHERE , -NUMA_NO_NODE ); - size = size * bootmem_scale /100 - bootmem_size_lowmem; - bootmem_size_global = size; +phys_addr_t low_limit =ARCH_LOW_ADDRESS_LIMIT ; +phys_addr_t lowmem, global, size; + +if (low_limit >=memblock_end_of_DRAM () -1 ) + low_limit =0 ; + + lowmem =reserved_mem_size (low_limit,NUMA_NO_NODE ); + global =reserved_mem_size (MEMBLOCK_ALLOC_ANYWHERE ,NUMA_NO_NODE ); + global -= lowmem; + +for_each_node_state (nid,N_MEMORY ) { + size =reserved_mem_size (MEMBLOCK_ALLOC_ANYWHERE , nid) - +reserved_mem_size (low_limit, nid); + bootmem_size_node[nid] =bootmem_scale_size (size); + global -= size; + } + + bootmem_size_lowmem =bootmem_scale_size (lowmem); + bootmem_size_global =bootmem_scale_size (global); + }else { +for_each_node_state (nid,N_MEMORY ) + bootmem_size_node[nid] = bootmem_size_pernode; }/*
@@ -785,24 +815,9 @@ static void __init bootmem_size_update(void)*/ bootmem_size_lowmem =round_up (bootmem_size_lowmem,BOOTMEM_ALIGNMENT_BYTES ); bootmem_size_global =round_up (bootmem_size_global,BOOTMEM_ALIGNMENT_BYTES ); -} - -static phys_addr_t __init bootmem_size_node (int nid) -{ -phys_addr_t size; - -if (bootmem_scale) { - size =memblock_reserved_kern_size (MEMBLOCK_ALLOC_ANYWHERE , - nid); -/* Do not count HugeTLB pages. */ - size -=memblock_reserved_hugetlb_size (MEMBLOCK_ALLOC_ANYWHERE , - nid); - size = size * bootmem_scale /100 ; - }else { - size = bootmem_size_pernode; - } - -return round_up (size,BOOTMEM_ALIGNMENT_BYTES ); +for_each_node_state (nid,N_MEMORY ) + bootmem_size_node[nid] =round_up (bootmem_size_node[nid], +BOOTMEM_ALIGNMENT_BYTES ); }bool kho_bootmem_overlap (phys_addr_t phys,size_t size)
@@ -899,7 +914,7 @@ static void __init kho_reserve_bootmem(void)* memoryless nodes, as we can not allocate bootmem areas there. */ for_each_node_state (nid,N_MEMORY ) { -if (kho_reserve_bootmem_area (bootmem_size_node (nid), +if (kho_reserve_bootmem_area (bootmem_size_node[nid],MEMBLOCK_ALLOC_ACCESSIBLE , nid)) {pr_err ("Failed to reserve nid %d bootmem\n" , nid); goto err_free_bootmem_areas;
Signature · 1 line
2.53.0
Quoted 150 lines
Bootmem areas are sized from overlapping views of the memory reserved in memblock:
* the global area is sized from all reserved memory, including memory allocated on specific nodes, so on NUMA systems everything allocated on a node is accounted in both the global area and the area of that node.
* per-node sizes are calculated after the lowmem and the global areas are reserved. WithCONFIG_NUMA=nall memblock regions belong to node 0, so the lowmem and the global areas are accounted in the size of node 0 area and scaled once again.As a result, bootmem is about twice bigger than it should be on NUMA systems and about four times bigger with
CONFIG_NUMA=n. On small systems reservation of the bootmem may fail and then KHO gets disabled.Make the areas cover separate parts of the reserved memory: the lowmem area covers allocations below
ARCH_LOW_ADDRESS_LIMIT, per-node areas cover allocations above it on each node and the global area covers allocations that are not bound to any node. Calculate all sizes before reserving any of the areas.With
CONFIG_NUMA=nthe global area becomes empty and it is not reserved.If
ARCH_LOW_ADDRESS_LIMITcovers all memory, as may happen on arm64, any area can serve lowmem allocations, so don't reserve the lowmem area.Bootmem sizes in MiB in x86 QEMU VMs, before -> after:
CONFIG_NUMA=yCONFIG_NUMA=n1G, 1 node 176 -> 92 320 -> 80 4G, 1 node 508 -> 332 1264 -> 316 16G, 2 nodes 1268 -> 704 2944 -> 736 32G, 4 nodes 2296 -> 1224 5196 -> 1300 64G, 4 nodes 4344 -> 2248 9664 -> 2420Fixes 3dc92c311498 ("kexec: add Kexec HandOver (KHO) generation helpers")Reported-by Kexin Liu <[email protected]>Reported-by George Guo <[email protected]>Reported-by Sourabh Jain <[email protected]>Signed-off-by Mike Rapoport (Microsoft) <[email protected]>kernel/liveupdate/kexec_handover.c +50
-31@@ -685,6 +685,7 @@ static unsigned int bootmem_scale __initdata = 200;static phys_addr_t bootmem_size_global__initdata ;static phys_addr_t bootmem_size_pernode__initdata ;static phys_addr_t bootmem_size_lowmem__initdata ; +static phys_addr_t bootmem_size_node[MAX_NUMNODES ]__initdata ;static int __init kho_parse_bootmem_size (char *p) {@@ -754,29 +755,58 @@ static int __init kho_parse_bootmem_size(char *p) }early_param ("kho_bootmem" , kho_parse_bootmem_size); +static phys_addr_t __init reserved_mem_size (phys_addr_t limit,int nid) +{ +return memblock_reserved_kern_size (limit, nid) - +memblock_reserved_hugetlb_size (limit, nid); +} + +static phys_addr_t __init bootmem_scale_size (phys_addr_t size) +{ +return size * bootmem_scale /100 ; +} + +/* +* The lowmem area covers allocations below ARCH_LOW_ADDRESS_LIMIT, per-node +* areas cover allocations above it on each node and the global area covers +* allocations that are not bound to any node. +* +* The reserved memory on a node and the total reserved memory include +* lowmem, so it is subtracted from both. +* +* If ARCH_LOW_ADDRESS_LIMIT covers all memory, any area can serve lowmem +* allocations and there is no lowmem area. +* +* All sizes must be calculated before any area is reserved, otherwise the +* areas reserved first will be accounted in the sizes of the others. +*/ static void __init bootmem_size_update (void ) { -/* -* If fixed sizes are not provided via command line, calculate them now. -* Remove HugeTLB allocations from it because they never get allocated -* from bootmem. -*/ +int nid; +if (bootmem_scale) { -phys_addr_t size; - - size =memblock_reserved_kern_size (ARCH_LOW_ADDRESS_LIMIT , -NUMA_NO_NODE ); - size -=memblock_reserved_hugetlb_size (ARCH_LOW_ADDRESS_LIMIT , -NUMA_NO_NODE ); - size = size * bootmem_scale /100 ; - bootmem_size_lowmem = size; - - size =memblock_reserved_kern_size (MEMBLOCK_ALLOC_ANYWHERE , -NUMA_NO_NODE ); - size -=memblock_reserved_hugetlb_size (MEMBLOCK_ALLOC_ANYWHERE , -NUMA_NO_NODE ); - size = size * bootmem_scale /100 - bootmem_size_lowmem; - bootmem_size_global = size; +phys_addr_t low_limit =ARCH_LOW_ADDRESS_LIMIT ; +phys_addr_t lowmem, global, size; + +if (low_limit >=memblock_end_of_DRAM () -1 ) + low_limit =0 ; + + lowmem =reserved_mem_size (low_limit,NUMA_NO_NODE ); + global =reserved_mem_size (MEMBLOCK_ALLOC_ANYWHERE ,NUMA_NO_NODE ); + global -= lowmem; + +for_each_node_state (nid,N_MEMORY ) { + size =reserved_mem_size (MEMBLOCK_ALLOC_ANYWHERE , nid) - +reserved_mem_size (low_limit, nid); + bootmem_size_node[nid] =bootmem_scale_size (size); + global -= size; + } + + bootmem_size_lowmem =bootmem_scale_size (lowmem); + bootmem_size_global =bootmem_scale_size (global); + }else { +for_each_node_state (nid,N_MEMORY ) + bootmem_size_node[nid] = bootmem_size_pernode; }/* @@ -785,24 +815,9 @@ static void __init bootmem_size_update(void)*/ bootmem_size_lowmem =round_up (bootmem_size_lowmem,BOOTMEM_ALIGNMENT_BYTES ); bootmem_size_global =round_up (bootmem_size_global,BOOTMEM_ALIGNMENT_BYTES ); -} - -static phys_addr_t __init bootmem_size_node (int nid) -{ -phys_addr_t size; - -if (bootmem_scale) { - size =memblock_reserved_kern_size (MEMBLOCK_ALLOC_ANYWHERE , - nid); -/* Do not count HugeTLB pages. */ - size -= memblock_reserved_hugetlb_size(MEMBLOCK_ALLOC_ANYWHERE,
kernel/liveupdate/kexec_handover.c static void __init bootmem_size_update(void) · line 820
Quoted 10 lines
kernel/liveupdate/kexec_handover.c +3
-7- nid); - size = size * bootmem_scale /100 ; - }else { - size = bootmem_size_pernode; - } - -return round_up (size,BOOTMEM_ALIGNMENT_BYTES ); +for_each_node_state (nid,N_MEMORY ) + bootmem_size_node[nid] =round_up (bootmem_size_node[nid], +BOOTMEM_ALIGNMENT_BYTES );
Nit, but I prefer to avoid multi-line indented statements without braces. Just put a braces around the above.
Signature · 1 line
Pasha
Mike Rapoport
maintainerQuoted 8 lines · Pasha Tatashin
kernel/liveupdate/kexec_handover.c static void __init bootmem_size_update(void) · line 820
Quoted 5 lines
kernel/liveupdate/kexec_handover.c +3
-2- -return round_up (size,BOOTMEM_ALIGNMENT_BYTES ); +for_each_node_state (nid,N_MEMORY ) + bootmem_size_node[nid] =round_up (bootmem_size_node[nid], +BOOTMEM_ALIGNMENT_BYTES );Nit, but I prefer to avoid multi-line indented statements without braces. Just put a braces around the above.
Yep, I'll fix it up while applying.
Quoted 5 lines
Reviewed-by Pasha Tatashin <[email protected]>Signature · 1 line
Pasha
Signature · 2 lines
Sincerely yours, Mike.
Quoted 36 lines · Mike Rapoport (Microsoft)
Bootmem areas are sized from overlapping views of the memory reserved in memblock:
* the global area is sized from all reserved memory, including memory allocated on specific nodes, so on NUMA systems everything allocated on a node is accounted in both the global area and the area of that node.
* per-node sizes are calculated after the lowmem and the global areas are reserved. WithCONFIG_NUMA=nall memblock regions belong to node 0, so the lowmem and the global areas are accounted in the size of node 0 area and scaled once again.As a result, bootmem is about twice bigger than it should be on NUMA systems and about four times bigger with
CONFIG_NUMA=n. On small systems reservation of the bootmem may fail and then KHO gets disabled.Make the areas cover separate parts of the reserved memory: the lowmem area covers allocations below
ARCH_LOW_ADDRESS_LIMIT, per-node areas cover allocations above it on each node and the global area covers allocations that are not bound to any node. Calculate all sizes before reserving any of the areas.With
CONFIG_NUMA=nthe global area becomes empty and it is not reserved.If
ARCH_LOW_ADDRESS_LIMITcovers all memory, as may happen on arm64, any area can serve lowmem allocations, so don't reserve the lowmem area.Bootmem sizes in MiB in x86 QEMU VMs, before -> after:
CONFIG_NUMA=yCONFIG_NUMA=n1G, 1 node 176 -> 92 320 -> 80 4G, 1 node 508 -> 332 1264 -> 316 16G, 2 nodes 1268 -> 704 2944 -> 736 32G, 4 nodes 2296 -> 1224 5196 -> 1300 64G, 4 nodes 4344 -> 2248 9664 -> 2420
Quoted 7 lines
Fixes 3dc92c311498 ("kexec: add Kexec HandOver (KHO) generation helpers")Reported-by Kexin Liu <[email protected]>Reported-by George Guo <[email protected]>Reported-by Sourabh Jain <[email protected]>Signed-off-by Mike Rapoport (Microsoft) <[email protected]>
Signature · 2 lines
cheers, jose a. p-a
Quoted 36 lines · Mike Rapoport (Microsoft)
Bootmem areas are sized from overlapping views of the memory reserved in memblock:
* the global area is sized from all reserved memory, including memory allocated on specific nodes, so on NUMA systems everything allocated on a node is accounted in both the global area and the area of that node.
* per-node sizes are calculated after the lowmem and the global areas are reserved. WithCONFIG_NUMA=nall memblock regions belong to node 0, so the lowmem and the global areas are accounted in the size of node 0 area and scaled once again.As a result, bootmem is about twice bigger than it should be on NUMA systems and about four times bigger with
CONFIG_NUMA=n. On small systems reservation of the bootmem may fail and then KHO gets disabled.Make the areas cover separate parts of the reserved memory: the lowmem area covers allocations below
ARCH_LOW_ADDRESS_LIMIT, per-node areas cover allocations above it on each node and the global area covers allocations that are not bound to any node. Calculate all sizes before reserving any of the areas.With
CONFIG_NUMA=nthe global area becomes empty and it is not reserved.If
ARCH_LOW_ADDRESS_LIMITcovers all memory, as may happen on arm64, any area can serve lowmem allocations, so don't reserve the lowmem area.Bootmem sizes in MiB in x86 QEMU VMs, before -> after:
CONFIG_NUMA=yCONFIG_NUMA=n1G, 1 node 176 -> 92 320 -> 80 4G, 1 node 508 -> 332 1264 -> 316 16G, 2 nodes 1268 -> 704 2944 -> 736 32G, 4 nodes 2296 -> 1224 5196 -> 1300 64G, 4 nodes 4344 -> 2248 9664 -> 2420
Quoted 7 lines
Fixes 3dc92c311498 ("kexec: add Kexec HandOver (KHO) generation helpers")Reported-by Kexin Liu <[email protected]>Reported-by George Guo <[email protected]>Reported-by Sourabh Jain <[email protected]>Signed-off-by Mike Rapoport (Microsoft) <[email protected]>
[...]
Signature · 2 lines
Regards, Pratyush Yadav
Hi Mike,
Quoted 22 lines · Mike Rapoport (Microsoft)
Bootmem areas are sized from overlapping views of the memory reserved in memblock:
* the global area is sized from all reserved memory, including memory allocated on specific nodes, so on NUMA systems everything allocated on a node is accounted in both the global area and the area of that node.
* per-node sizes are calculated after the lowmem and the global areas are reserved. WithCONFIG_NUMA=nall memblock regions belong to node 0, so the lowmem and the global areas are accounted in the size of node 0 area and scaled once again.As a result, bootmem is about twice bigger than it should be on NUMA systems and about four times bigger with
CONFIG_NUMA=n. On small systems reservation of the bootmem may fail and then KHO gets disabled.Make the areas cover separate parts of the reserved memory: the lowmem area covers allocations below
ARCH_LOW_ADDRESS_LIMIT, per-node areas cover allocations above it on each node and the global area covers allocations that are not bound to any node. Calculate all sizes before reserving any of the areas.
Quoted 4 lines
With
CONFIG_NUMA=nthe global area becomes empty and it is not reserved.If
ARCH_LOW_ADDRESS_LIMITcovers all memory, as may happen on arm64, any area can serve lowmem allocations, so don't reserve the lowmem area.
The documentation still describes the old layout says __there is always a boot memory region per node plus one for node-less allocations__, which stops being true as soon as the global or the lowmem area is empty, and the lowmem area is not mentioned at all. So please update the documentation as well.
Quoted 150 lines
Bootmem sizes in MiB in x86 QEMU VMs, before -> after:
CONFIG_NUMA=yCONFIG_NUMA=n1G, 1 node 176 -> 92 320 -> 80 4G, 1 node 508 -> 332 1264 -> 316 16G, 2 nodes 1268 -> 704 2944 -> 736 32G, 4 nodes 2296 -> 1224 5196 -> 1300 64G, 4 nodes 4344 -> 2248 9664 -> 2420Fixes 3dc92c311498 ("kexec: add Kexec HandOver (KHO) generation helpers")Reported-by Kexin Liu <[email protected]>Reported-by George Guo <[email protected]>Reported-by Sourabh Jain <[email protected]>Signed-off-by Mike Rapoport (Microsoft) <[email protected]>--- kernel/liveupdate/kexec_handover.c | 93 ++++++++++++++++++++++---------------- 1 file changed, 54 insertions(+), 39 deletions(-)kernel/liveupdate/kexec_handover.c +54
-39@@ -685,6 +685,7 @@ static unsigned int bootmem_scale __initdata = 200;static phys_addr_t bootmem_size_global__initdata ;static phys_addr_t bootmem_size_pernode__initdata ;static phys_addr_t bootmem_size_lowmem__initdata ; +static phys_addr_t bootmem_size_node[MAX_NUMNODES ]__initdata ;static int __init kho_parse_bootmem_size (char *p) {@@ -754,29 +755,58 @@ static int __init kho_parse_bootmem_size(char *p) }early_param ("kho_bootmem" , kho_parse_bootmem_size); +static phys_addr_t __init reserved_mem_size (phys_addr_t limit,int nid) +{ +return memblock_reserved_kern_size (limit, nid) - +memblock_reserved_hugetlb_size (limit, nid); +} + +static phys_addr_t __init bootmem_scale_size (phys_addr_t size) +{ +return size * bootmem_scale /100 ; +} + +/* +* The lowmem area covers allocations below ARCH_LOW_ADDRESS_LIMIT, per-node +* areas cover allocations above it on each node and the global area covers +* allocations that are not bound to any node. +* +* The reserved memory on a node and the total reserved memory include +* lowmem, so it is subtracted from both. +* +* If ARCH_LOW_ADDRESS_LIMIT covers all memory, any area can serve lowmem +* allocations and there is no lowmem area. +* +* All sizes must be calculated before any area is reserved, otherwise the +* areas reserved first will be accounted in the sizes of the others. +*/ static void __init bootmem_size_update (void ) { -/* -* If fixed sizes are not provided via command line, calculate them now. -* Remove HugeTLB allocations from it because they never get allocated -* from bootmem. -*/ +int nid; +if (bootmem_scale) { -phys_addr_t size; - - size =memblock_reserved_kern_size (ARCH_LOW_ADDRESS_LIMIT , -NUMA_NO_NODE ); - size -=memblock_reserved_hugetlb_size (ARCH_LOW_ADDRESS_LIMIT , -NUMA_NO_NODE ); - size = size * bootmem_scale /100 ; - bootmem_size_lowmem = size; - - size =memblock_reserved_kern_size (MEMBLOCK_ALLOC_ANYWHERE , -NUMA_NO_NODE ); - size -=memblock_reserved_hugetlb_size (MEMBLOCK_ALLOC_ANYWHERE , -NUMA_NO_NODE ); - size = size * bootmem_scale /100 - bootmem_size_lowmem; - bootmem_size_global = size; +phys_addr_t low_limit =ARCH_LOW_ADDRESS_LIMIT ; +phys_addr_t lowmem, global, size; + +if (low_limit >=memblock_end_of_DRAM () -1 ) + low_limit =0 ; + + lowmem =reserved_mem_size (low_limit,NUMA_NO_NODE ); + global =reserved_mem_size (MEMBLOCK_ALLOC_ANYWHERE ,NUMA_NO_NODE ); + global -= lowmem; + +for_each_node_state (nid,N_MEMORY ) { + size =reserved_mem_size (MEMBLOCK_ALLOC_ANYWHERE , nid) - +reserved_mem_size (low_limit, nid); + bootmem_size_node[nid] =bootmem_scale_size (size); + global -= size; + } + + bootmem_size_lowmem =bootmem_scale_size (lowmem); + bootmem_size_global =bootmem_scale_size (global); + }else { +for_each_node_state (nid,N_MEMORY ) + bootmem_size_node[nid] = bootmem_size_pernode; }/* @@ -785,24 +815,9 @@ static void __init bootmem_size_update(void)*/ bootmem_size_lowmem =round_up (bootmem_size_lowmem,BOOTMEM_ALIGNMENT_BYTES ); bootmem_size_global =round_up (bootmem_size_global,BOOTMEM_ALIGNMENT_BYTES ); -} - -static phys_addr_t __init bootmem_size_node (int nid) -{ -phys_addr_t size; - -if (bootmem_scale) { - size =memblock_reserved_kern_size (MEMBLOCK_ALLOC_ANYWHERE , - nid); -/* Do not count HugeTLB pages. */ - size -=memblock_reserved_hugetlb_size (MEMBLOCK_ALLOC_ANYWHERE , - nid); - size = size * bootmem_scale /100 ; - }else { - size = bootmem_size_pernode; - } - -return round_up (size,BOOTMEM_ALIGNMENT_BYTES ); +for_each_node_state (nid,N_MEMORY ) + bootmem_size_node[nid] =round_up (bootmem_size_node[nid], +BOOTMEM_ALIGNMENT_BYTES ); }bool kho_bootmem_overlap (phys_addr_t phys,size_t size)@@ -899,7 +914,7 @@ static void __init kho_reserve_bootmem(void)* memoryless nodes, as we can not allocate bootmem areas there. */ for_each_node_state (nid,N_MEMORY ) { -if (kho_reserve_bootmem_area (bootmem_size_node (nid), +if (kho_reserve_bootmem_area (bootmem_size_node[nid],MEMBLOCK_ALLOC_ACCESSIBLE , nid)) {pr_err ("Failed to reserve nid %d bootmem\n" , nid); goto err_free_bootmem_areas;
Signature · 2 lines
Regards, Mukesh Pilaniya