Skip to contentSkip to conversation

memblock: keep reserve_mem regions out of KHO bootmem

RFC PATCH In review Jose A. Perez de Azpillaga 3 participants 10 messages Oct 21 – Nov 4

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover memblock: keep reserve_mem regions out of KHO bootmem

On a KHO boot, a reserve_mem= region that can't be revived (new name, size or alignment) can be allocated from KHO bootmem, which the next kernel boots out of. With CONFIG_KEXEC_HANDOVER_DEBUG preserving it fails; without it, the next kernel's memmap ends up on top of it.

Patch 1 factors the bootmem check out of memblock_alloc_hugetlb(), and patch 2 uses it for reserve_mem. That also drops the TODO in reserve_mem(), which asks for the allocation to be outside KHO_NOPRSRV, though it's bootmem it has to stay out of.

Questions:

- If kho_discover_noprsrv() fails, only bootmem is NOPRSRV, and reserve_mem() now fails with -ENOMEM instead of using it. Is that the right thing to do?

- Should reserve_mem skip mirrored memory like HugeTLB does, rather than keep the fallback?

No Cc: stable, since patch 2 relies on 14773e2aa4c5 ("kho: extend scratch") for room outside bootmem.

Based on next-20261021, as it needs the KHO bootmem rename from liveupdate-7.4 and touches code changed in memblock for-next.

Tested on x86_64 in QEMU.

Jose A. Perez de Azpillaga (2):
memblock: factor out the KHO bootmem check from
memblock_alloc_hugetlb()
memblock: don't allocate reserve_mem regions from KHO bootmem

 mm/memblock.c | 100 ++++++++++++++++++++++++++++++++------------------
 1 file changed, 64 insertions(+), 36 deletions(-)

base-commit: 9a482184d910775cf648863148944945b74e2815 --
2.55.0

#1 lore raw

RFC PATCH 1/2 memblock: factor out the KHO bootmem check from memblock_alloc_hugetlb()

reserve_mem regions are preserved with KHO too, and need the same search that keeps HugeTLB pages out of KHO bootmem. Move it into a helper.

No functional change intended.

---
 mm/memblock.c | 70 ++++++++++++++++++++++++++-------------------------
 1 file changed, 36 insertions(+), 34 deletions(-)
mm/memblock.c +36 -34
index a450092a80fb..f43659688681 100644
@@ -1744,6 +1744,31 @@ void * __init memblock_alloc_try_nid_raw(
 	return memblock_alloc_internal(size, align, min_addr, max_addr, nid);
 }
 
+/*
+ * Memory that can be preserved with KHO can't be in KHO bootmem, since the
+ * next kernel boots out of it. Bootmem areas are normally not very large, so
+ * this shouldn't take too many retries.
+ */
+static phys_addr_t __init memblock_find_outside_kho_bootmem(phys_addr_t size,
+					phys_addr_t align, int nid,
+					enum memblock_flags flags)
+{
+	phys_addr_t addr, start = 0, end = MEMBLOCK_ALLOC_ACCESSIBLE;
+
+	while ((addr = memblock_find_in_range_node(size, align, start, end,
+						   nid, flags))) {
+		if (!kho_bootmem_overlap(addr, size))
+			break;
+
+		if (memblock_bottom_up())
+			start = addr + size;
+		else
+			end = addr;
+	}
+
+	return addr;
+}
+
 /**
  * memblock_alloc_hugetlb - allocate boot memory for HugeTLB pages
  * @size:      size of the memory to be allocated in bytes
@@ -1761,52 +1786,29 @@ void * __init memblock_alloc_try_nid_raw(
 void * __init memblock_alloc_hugetlb(phys_addr_t size, int nid, bool exact_nid)
 {
 	enum memblock_flags flags = choose_memblock_flags();
-	phys_addr_t addr, start = 0, end = MEMBLOCK_ALLOC_ACCESSIBLE;
+	phys_addr_t addr;
 
 	memblock_dbg("%s: %llu bytes, nid=%d, exact_nid=%d %pS\n", __func__,
 		     (u64)size, nid, exact_nid, (void *)_RET_IP_);
 
 	/* Don't waste mirrored memory on HugeTLB pages. */
 	flags &= ~MEMBLOCK_MIRROR;
-retry:
-	/* HugeTLB pages are always aligned by their size. */
-	addr = memblock_find_in_range_node(size, size, start, end, nid, flags);
-	if (addr)
-		goto found;
+
+	/*
+	 * HugeTLB pages are always aligned by their size, and can be preserved
+	 * with KHO.
+	 */
+	addr = memblock_find_outside_kho_bootmem(size, size, nid, flags);
 
 	/* Try all nodes if allowed. */
-	if (numa_valid_node(nid) && !exact_nid) {
+	if (!addr && numa_valid_node(nid) && !exact_nid) {
 		nid = NUMA_NO_NODE;
-		/*
-		 * If a previous candidate overlapped with KHO bootmem, it would
-		 * update start or end. Now that the search is opening to all
-		 * nodes, reset them.
-		 */
-		start = 0;
-		end = MEMBLOCK_ALLOC_ACCESSIBLE;
-
-		goto retry;
+		addr = memblock_find_outside_kho_bootmem(size, size, nid, flags);
 	}
 
 	/* Found nothing... :-( */
-	return NULL;
-
-found:
-	/*
-	 * HugeTLB pages can be preserved with KHO and no preserved memory can
-	 * be in bootmem. So retry if found address overlaps with bootmem.
-	 *
-	 * Bootmem areas are normally not very large, so this shouldn't take too
-	 * many retries.
-	 */
-	if (kho_bootmem_overlap(addr, size)) {
-		if (memblock_bottom_up())
-			start = addr + size;
-		else
-			end = addr;
-
-		goto retry;
-	}
+	if (!addr)
+		return NULL;
 
 	if (__memblock_reserve(addr, size, nid, MEMBLOCK_RSRV_KERN | MEMBLOCK_RSRV_HUGETLB))
 		return NULL;
Signature · 1 line
2.55.0
#2 lore raw

Re: [RFC PATCH 1/2] memblock: factor out the KHO bootmem check from memblock_alloc_hugetlb()

Sashiko has reviewed this patch and found no issues.
Quoted 6 lines

reserve_mem regions are preserved with KHO too, and need the same search that keeps HugeTLB pages out of KHO bootmem. Move it into a helper.

No functional change intended.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

RFC PATCH 2/2 memblock: don't allocate reserve_mem regions from KHO bootmem

In reply to Jose A. Perez de Azpillaga

On a KHO boot, a reserve_mem region that can't be revived is allocated from KHO_NOPRSRV memory, which includes the bootmem the next kernel boots out of. With CONFIG_KEXEC_HANDOVER_DEBUG, preserving the region then fails:

  WARNING: kernel/liveupdate/kexec_handover.c:1245 at kho_preserve_pages+0x168/0x180
  failed to prepare memblock FDT for KHO: -22

Without it, the next kernel puts its memmap on top of the region and the ramoops records in it are lost.

Use the search that keeps HugeTLB pages out of bootmem, and keep the mirrored memory fallback of memblock_phys_alloc().

Fixes f99230780211 ("memblock: add KHO support for reserve_mem")
---
 mm/memblock.c | 30 ++++++++++++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)
mm/memblock.c +28 -2
index f43659688681..0fa9d6529285 100644
@@ -2761,6 +2761,33 @@ static bool __init reserve_mem_kho_revive(const char *name, phys_addr_t size,
 }
 #endif /* CONFIG_KEXEC_HANDOVER */
 
+/*
+ * The region gets preserved with KHO, so it can't be in KHO bootmem. On a KHO
+ * boot only KHO_NOPRSRV memory is used at this point, and that is more than
+ * just bootmem.
+ */
+static phys_addr_t __init reserve_mem_alloc(phys_addr_t size, phys_addr_t align)
+{
+	enum memblock_flags flags = choose_memblock_flags();
+	phys_addr_t start;
+
+	start = memblock_find_outside_kho_bootmem(size, align, NUMA_NO_NODE,
+						  flags);
+	if (!start && (flags & MEMBLOCK_MIRROR)) {
+		pr_warn("Could not allocate %pap bytes of mirrored memory\n",
+			&size);
+		start = memblock_find_outside_kho_bootmem(size, align,
+							  NUMA_NO_NODE,
+							  flags & ~MEMBLOCK_MIRROR);
+	}
+
+	if (!start || memblock_reserve_kern(start, size))
+		return 0;
+
+	memblock_prep_allocation(start, size, true);
+	return start;
+}
+
 /*
  * Parse reserve_mem=nn:align:name
  */
@@ -2829,8 +2856,7 @@ static int __init reserve_mem(char *p)
 	if (reserve_mem_kho_revive(name, size, align))
 		return 1;
 
-	/* TODO: Allocation must be outside of KHO_NOPRSRV region */
-	start = memblock_phys_alloc(size, align);
+	start = reserve_mem_alloc(size, align);
 	if (!start) {
 		pr_err("reserve_mem: memblock allocation failed\n");
 		return -ENOMEM;
Signature · 1 line
2.55.0
#4 lore raw

Re: [RFC PATCH 2/2] memblock: don't allocate reserve_mem regions from KHO bootmem

Sashiko has reviewed this patch and found no issues.
Quoted 8 lines

On a KHO boot, a reserve_mem region that can't be revived is allocated from KHO_NOPRSRV memory, which includes the bootmem the next kernel boots out of. With CONFIG_KEXEC_HANDOVER_DEBUG, preserving the region then fails:

  WARNING: kernel/liveupdate/kexec_handover.c:1245 at kho_preserve_pages+0x168/0x180
  failed to prepare memblock FDT for KHO: -22
Quoted 8 lines

Without it, the next kernel puts its memmap on top of the region and the ramoops records in it are lost.

Use the search that keeps HugeTLB pages out of bootmem, and keep the mirrored memory fallback of memblock_phys_alloc().

Fixes f99230780211 ("memblock: add KHO support for reserve_mem")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
maintainer
#5 lore raw
In reply to Jose A. Perez de Azpillaga
Quoted 4 lines · Jose A. Perez de Azpillaga

On a KHO boot, a reserve_mem= region that can't be revived (new name, size or alignment) can be allocated from KHO bootmem, which the next kernel boots out of. With CONFIG_KEXEC_HANDOVER_DEBUG preserving it fails; without it, the next kernel's memmap ends up on top of it.

What's your use-case? Why are you adding new reserve_mem regions after cold boot? I'd rather avoid making purely theoretical fixes.

[...]

Signature · 2 lines
Regards,
Pratyush Yadav
#6 lore raw
Quoted 8 lines · Pratyush Yadav
Quoted 4 lines · Jose A. Perez de Azpillaga

On a KHO boot, a reserve_mem= region that can't be revived (new name, size or alignment) can be allocated from KHO bootmem, which the next kernel boots out of. With CONFIG_KEXEC_HANDOVER_DEBUG preserving it fails; without it, the next kernel's memmap ends up on top of it.

What's your use-case? Why are you adding new reserve_mem regions after cold boot? I'd rather avoid making purely theoretical fixes.

I don't have one, I found it through the TODO. a different reserve_mem= in the cmdline passed to kexec is enough to hit it.

if it isn't meant to work across a live update, would you rather have reserve_mem() fail on KHO boot when it can't revive the region?

Signature · 2 lines
cheers,
jose a. p-a
#7 lore raw
Quoted 7 lines · Jose A. Perez de Azpillaga
Quoted 6 lines · Pratyush Yadav
Quoted 4 lines · Jose A. Perez de Azpillaga

On a KHO boot, a reserve_mem= region that can't be revived (new name, size or alignment) can be allocated from KHO bootmem, which the next kernel boots out of. With CONFIG_KEXEC_HANDOVER_DEBUG preserving it fails; without it, the next kernel's memmap ends up on top of it.

Quoted 9 lines
Quoted 2 lines

What's your use-case? Why are you adding new reserve_mem regions after cold boot? I'd rather avoid making purely theoretical fixes.

I don't have one, I found it through the TODO. a different reserve_mem= in the cmdline passed to kexec is enough to hit it.

if it isn't meant to work across a live update, would you rather have reserve_mem() fail on KHO boot when it can't revive the region?

ping, I can send a v2 either way.

Signature · 2 lines
cheers,
jose a. p-a
maintainer
#8 lore raw
Quoted 8 lines · Jose A. Perez de Azpillaga
Quoted 7 lines · Jose A. Perez de Azpillaga
Quoted 6 lines · Pratyush Yadav
Quoted 4 lines · Jose A. Perez de Azpillaga

On a KHO boot, a reserve_mem= region that can't be revived (new name, size or alignment) can be allocated from KHO bootmem, which the next kernel boots out of. With CONFIG_KEXEC_HANDOVER_DEBUG preserving it fails; without it, the next kernel's memmap ends up on top of it.

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

What's your use-case? Why are you adding new reserve_mem regions after cold boot? I'd rather avoid making purely theoretical fixes.

I don't have one, I found it through the TODO. a different reserve_mem= in the cmdline passed to kexec is enough to hit it.

If there was a use case, I wouldn't mind fixing it. But I'd rather not add code we don't have a use case for.

Quoted 3 lines
Quoted 3 lines

if it isn't meant to work across a live update, would you rather have reserve_mem() fail on KHO boot when it can't revive the region?

This sounds good I think.

Quoted 3 lines
Quoted 1 line

ping, I can send a v2 either way.

Signature · 2 lines
Regards,
Pratyush Yadav
#9 lore raw
Quoted 16 lines · Pratyush Yadav
Quoted 8 lines · Jose A. Perez de Azpillaga
Quoted 7 lines · Jose A. Perez de Azpillaga
Quoted 6 lines · Pratyush Yadav
Quoted 4 lines · Jose A. Perez de Azpillaga

On a KHO boot, a reserve_mem= region that can't be revived (new name, size or alignment) can be allocated from KHO bootmem, which the next kernel boots out of. With CONFIG_KEXEC_HANDOVER_DEBUG preserving it fails; without it, the next kernel's memmap ends up on top of it.

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

What's your use-case? Why are you adding new reserve_mem regions after cold boot? I'd rather avoid making purely theoretical fixes.

I don't have one, I found it through the TODO. a different reserve_mem= in the cmdline passed to kexec is enough to hit it.

Quoted 9 lines

If there was a use case, I wouldn't mind fixing it. But I'd rather not add code we don't have a use case for.

Quoted 3 lines
Quoted 3 lines

if it isn't meant to work across a live update, would you rather have reserve_mem() fail on KHO boot when it can't revive the region?

This sounds good I think.

okay, I'll drop 1/2 and send that as v2 :)

Signature · 2 lines
cheers,
jose a. p-a