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

From: Jose A. Perez de Azpillaga

Date: Wed Oct 21 2026 - 17:11:17 EST


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.

Signed-off-by: Jose A. Perez de Azpillaga <azpijr@xxxxxxxxx>
---
mm/memblock.c | 70 ++++++++++++++++++++++++++-------------------------
1 file changed, 36 insertions(+), 34 deletions(-)

diff --git a/mm/memblock.c b/mm/memblock.c
index a450092a80fb..f43659688681 100644
--- a/mm/memblock.c
+++ b/mm/memblock.c
@@ -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;
--
2.55.0