* [PATCH 0/5] Mirror reserved memblock regions in linear time
@ 2026-12-04 15:29 Ayubjon Qosimov via B4 Relay
2026-12-04 15:29 ` [PATCH 1/5] resource: add reserve_memblock_reserved_resources() Ayubjon Qosimov via B4 Relay
` (4 more replies)
0 siblings, 5 replies; 7+ messages in thread
From: Ayubjon Qosimov via B4 Relay @ 2026-12-04 15:29 UTC (permalink / raw)
To: Andrew Morton
Cc: Mike Rapoport, linux-mm, Catalin Marinas, Will Deacon,
Mark Rutland, linux-arm-kernel, Paul Walmsley, linux-riscv,
Huacai Chen, loongarch, nh-open-source, linux-kernel,
Ayubjon Qosimov
arm64, riscv and LoongArch each mirror memblock.reserved into
/proc/iomem at arch_initcall time with reserve_region_with_split(), once
per reserved range. Every insertion walks the parent's child list from
its head, so n ranges cost O(n^2). x86 is not affected: it reports the
e820 map in /proc/iomem, not memblock.reserved.
This matters once there are many reserved ranges, as in the kernel
booted by a kexec with Kexec HandOver (KHO). KHO reserves every
preserved folio separately, and memory preserved through the Live Update
Orchestrator, such as memfd/shmem folios or vmalloc pages, is scattered,
so the ranges do not merge. A host that preserves guest memory through
LUO ends up with about 3200 reserved ranges after each kexec, and this
initcall then takes 12 ms, which adds directly to the time the guests
are paused during a live update.
Patch 1 adds reserve_memblock_reserved_resources(), one helper for the
three architectures. It holds resource_lock once for the whole array of
memory resources and walks memblock.reserved for each of them with a
hint, so each insertion resumes at the child inserted or hit last: n
ranges cost O(n) insertions instead of O(n^2). The children it skips all
end below the new range, so the tree is identical. Patch 2 adds KUnit
cases that build the same tree with and without the hint, and compare
the helper with the old loop on a memblock table of their own. Patches
3-5 switch the architectures over. /proc/iomem is unchanged; dropping or
merging the KHO ranges there would be a visible change and can be
discussed separately.
The series is based on mm-new as of 2026-12-01. Patches 3-5 depend on
patch 1, so I'd suggest it goes through Andrew's tree (mm-nonmm), with
acks from the arch maintainers.
Measured in a 96-vCPU KVM guest on a Graviton4 host, standing in for a
large host, in the kernel booted by a KHO kexec. To get disjoint
reserved ranges, lib/test_kho.c was changed locally to preserve order-0
pages at fixed addresses with a free page between each (the stock test
preserves large folios that merge into a few ranges); that change is not
part of the series. The time is the gap between the initcall_debug
"calling" and "returned" lines for reserve_memblock_reserved_regions(),
min-max over the boots:
ranges before after boots
3223 11.9-12.3 ms 0.17-0.18 ms 5
/proc/iomem with and without the series is unchanged except for ranges
that also move between two boots of the same kernel.
Testing: the KUnit cases pass under UML and under arm64 QEMU with 4K,
16K and 64K pages. arm64 and x86_64 defconfig kernels build with no W=1
warnings in the touched files. riscv and LoongArch defconfig kernels
boot in QEMU with an unchanged /proc/iomem apart from the kernel image
size; those boots have few reserved ranges, so they only show that
nothing regressed.
Signed-off-by: Ayubjon Qosimov <ayubjon@amazon.de>
---
Ayubjon Qosimov (5):
resource: add reserve_memblock_reserved_resources()
resource, kunit: add test cases for reserve_memblock_reserved_resources()
arm64: Use reserve_memblock_reserved_resources()
riscv: use reserve_memblock_reserved_resources()
LoongArch: Use reserve_memblock_reserved_resources()
arch/arm64/kernel/setup.c | 24 +---
arch/loongarch/kernel/setup.c | 24 +---
arch/riscv/kernel/setup.c | 24 +---
include/linux/ioport.h | 2 +
kernel/resource.c | 96 +++++++++++++++-
kernel/resource_internal.h | 18 +++
kernel/resource_kunit.c | 254 ++++++++++++++++++++++++++++++++++++++++++
7 files changed, 370 insertions(+), 72 deletions(-)
---
base-commit: 1a7610248a3802e38124ce66b07eef12277be9a0
change-id: 20261204-b4-iomem-ev5-2e4483a4d290
Best regards,
--
Ayubjon Qosimov <ayubjon@amazon.de>
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 1/5] resource: add reserve_memblock_reserved_resources()
2026-12-04 15:29 [PATCH 0/5] Mirror reserved memblock regions in linear time Ayubjon Qosimov via B4 Relay
@ 2026-12-04 15:29 ` Ayubjon Qosimov via B4 Relay
2026-12-04 15:29 ` [PATCH 2/5] resource, kunit: add test cases for reserve_memblock_reserved_resources() Ayubjon Qosimov via B4 Relay
` (3 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: Ayubjon Qosimov via B4 Relay @ 2026-12-04 15:29 UTC (permalink / raw)
To: Andrew Morton
Cc: Mike Rapoport, linux-mm, Catalin Marinas, Will Deacon,
Mark Rutland, linux-arm-kernel, Paul Walmsley, linux-riscv,
Huacai Chen, loongarch, nh-open-source, linux-kernel,
Ayubjon Qosimov
From: Ayubjon Qosimov <ayubjon@amazon.de>
arm64, riscv and LoongArch each have a reserve_memblock_reserved_regions()
initcall that mirrors memblock.reserved into /proc/iomem: under every
standard memory resource, it adds a child for each reserved range that
overlaps it, with reserve_region_with_split(). The three copies are the
same apart from the name of the children, "reserved" on arm64 and
"Reserved" on the other two. Each insertion walks the parent's child
list from its head, so n reserved ranges cost O(n^2).
Add one helper for the three architectures. It takes resource_lock once
for the whole array of memory resources and, for each of them, walks
memblock.reserved with a hint, so that each insertion resumes at the
child inserted or hit last instead of at the head of the list, and stops
at the first range past the end of the resource. The insertions then
cost O(n) for n ranges. The array may be in any order; riscv builds it
in descending address order. The helper lives in kernel/resource.c
rather than mm/memblock.c because it needs resource_lock and the
internal insertion code.
Holding resource_lock across the whole walk, instead of once per range
as reserve_region_with_split() does, adds no new constraint:
__reserve_region_with_split() runs under that lock in either case and
already allocates with GFP_ATOMIC, nothing in the walk sleeps, and the
helper runs once at arch_initcall, where the whole walk takes about
0.2 ms for 3200 ranges.
__request_resource() becomes a wrapper around __request_resource_hint(),
which can start the walk at a given child: children are sorted and do
not overlap, so the children it skips all end below the new range, and
the tree is unchanged. __reserve_region_with_split() passes the hint
through; existing callers pass NULL.
The architecture loops also skipped a memory resource unless
memblock_is_region_reserved() reported an overlap. For resources whose
start and end + 1 are page aligned, as on all three architectures, a
reserved range overlaps one exactly when its page-rounded copy does, so
the helper does without that check. It skips resources that are inverted
or whose size wraps to zero, such as the [0, ~0] resource arm64 builds
when less than a page of memory remains at physical address 0.
Signed-off-by: Ayubjon Qosimov <ayubjon@amazon.de>
---
include/linux/ioport.h | 2 ++
kernel/resource.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++---
2 files changed, 89 insertions(+), 5 deletions(-)
diff --git a/include/linux/ioport.h b/include/linux/ioport.h
index f7930b3dfd0a..377f2f7808e3 100644
--- a/include/linux/ioport.h
+++ b/include/linux/ioport.h
@@ -248,6 +248,8 @@ void release_child_resources(struct resource *new);
extern void reserve_region_with_split(struct resource *root,
resource_size_t start, resource_size_t end,
const char *name);
+void reserve_memblock_reserved_resources(struct resource *res, unsigned int nr,
+ const char *name);
extern struct resource *insert_resource_conflict(struct resource *parent, struct resource *new);
extern int insert_resource(struct resource *parent, struct resource *new);
extern void insert_resource_expand_to_fit(struct resource *root, struct resource *new);
diff --git a/kernel/resource.c b/kernel/resource.c
index 0c315c917fbb..2b274f30fbde 100644
--- a/kernel/resource.c
+++ b/kernel/resource.c
@@ -13,6 +13,7 @@
#include <linux/export.h>
#include <linux/errno.h>
#include <linux/ioport.h>
+#include <linux/memblock.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
@@ -185,8 +186,18 @@ static struct resource *alloc_resource(gfp_t flags)
return kzalloc_obj(struct resource, flags);
}
-/* Return the conflict entry if you can't request it */
-static struct resource * __request_resource(struct resource *root, struct resource *new)
+/*
+ * Return the conflict entry if you can't request it.
+ *
+ * @hint, if not NULL, points to NULL or to a child of @root. If that child
+ * starts at or below @new, the walk starts there: every child before it ends
+ * below @new. *@hint is then set to @new, or to a conflict that starts at or
+ * below @new, so ascending insertions visit each child once. The caller
+ * holds resource_lock.
+ */
+static struct resource *__request_resource_hint(struct resource *root,
+ struct resource *new,
+ struct resource **hint)
{
resource_size_t start = new->start;
resource_size_t end = new->end;
@@ -199,21 +210,36 @@ static struct resource * __request_resource(struct resource *root, struct resour
if (end > root->end)
return root;
p = &root->child;
+ if (hint && *hint && (*hint)->start <= start) {
+ if ((*hint)->end >= start)
+ return *hint;
+ p = &(*hint)->sibling;
+ }
for (;;) {
tmp = *p;
if (!tmp || tmp->start > end) {
new->sibling = tmp;
*p = new;
new->parent = root;
+ if (hint)
+ *hint = new;
return NULL;
}
p = &tmp->sibling;
if (tmp->end < start)
continue;
+ if (hint && tmp->start <= start)
+ *hint = tmp;
return tmp;
}
}
+static struct resource *__request_resource(struct resource *root,
+ struct resource *new)
+{
+ return __request_resource_hint(root, new, NULL);
+}
+
static int __release_resource(struct resource *old, bool release_child)
{
struct resource *tmp, **p, *chd;
@@ -1142,9 +1168,11 @@ int adjust_resource(struct resource *res, resource_size_t start,
}
EXPORT_SYMBOL(adjust_resource);
+/* Called with resource_lock held; @hint is passed to __request_resource_hint(). */
static void __init
__reserve_region_with_split(struct resource *root, resource_size_t start,
- resource_size_t end, const char *name)
+ resource_size_t end, const char *name,
+ struct resource **hint)
{
struct resource *parent = root;
struct resource *conflict;
@@ -1163,7 +1191,7 @@ __reserve_region_with_split(struct resource *root, resource_size_t start,
while (1) {
- conflict = __request_resource(parent, res);
+ conflict = __request_resource_hint(parent, res, hint);
if (!conflict) {
if (!next_res)
break;
@@ -1228,10 +1256,64 @@ reserve_region_with_split(struct resource *root, resource_size_t start,
dump_stack();
}
if (!abort)
- __reserve_region_with_split(root, start, end, name);
+ __reserve_region_with_split(root, start, end, name, NULL);
+ write_unlock(&resource_lock);
+}
+
+/* reserve_memblock_reserved_resources() on the ranges of @type. */
+static void __init __reserve_memblock_resources(struct memblock_type *type,
+ struct resource *res,
+ unsigned int nr,
+ const char *name)
+{
+ phys_addr_t r_start, r_end;
+ unsigned int k;
+ u64 i;
+
+ write_lock(&resource_lock);
+ for (k = 0; k < nr; k++) {
+ struct resource *mem = &res[k], *hint = NULL;
+
+ if (mem->start > mem->end || !resource_size(mem))
+ continue;
+
+ __for_each_mem_range(i, type, NULL, NUMA_NO_NODE, MEMBLOCK_NONE,
+ &r_start, &r_end, NULL) {
+ resource_size_t start, end;
+
+ start = max(PFN_PHYS(PFN_DOWN(r_start)), mem->start);
+ end = min(PFN_PHYS(PFN_UP(r_end)) - 1, mem->end);
+ if (start > mem->end)
+ break;
+ if (end < mem->start)
+ continue;
+ __reserve_region_with_split(mem, start, end, name, &hint);
+ }
+ }
write_unlock(&resource_lock);
}
+/**
+ * reserve_memblock_reserved_resources - show reserved memory in /proc/iomem
+ * @res: memory resources whose start and end + 1 are page aligned, in any
+ * order
+ * @nr: number of entries in @res
+ * @name: name of the resources to add
+ *
+ * Under each resource in @res, add a busy child named @name for each range
+ * of memblock.reserved that overlaps it, rounded out to whole pages, as
+ * reserve_region_with_split() does. Resources that are inverted or whose
+ * size wraps to zero are skipped. Call it from an initcall at boot; it
+ * takes resource_lock. Without CONFIG_ARCH_KEEP_MEMBLOCK, memblock.reserved
+ * may already be freed by then.
+ */
+void __init reserve_memblock_reserved_resources(struct resource *res,
+ unsigned int nr,
+ const char *name)
+{
+ __reserve_memblock_resources(&memblock.reserved, res, nr, name);
+}
+
/**
* resource_alignment - calculate resource's alignment
* @res: resource pointer
--
2.47.3
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH 2/5] resource, kunit: add test cases for reserve_memblock_reserved_resources()
2026-12-04 15:29 [PATCH 0/5] Mirror reserved memblock regions in linear time Ayubjon Qosimov via B4 Relay
2026-12-04 15:29 ` [PATCH 1/5] resource: add reserve_memblock_reserved_resources() Ayubjon Qosimov via B4 Relay
@ 2026-12-04 15:29 ` Ayubjon Qosimov via B4 Relay
2026-12-05 15:30 ` sashiko-bot
2026-12-04 15:29 ` [PATCH 3/5] arm64: Use reserve_memblock_reserved_resources() Ayubjon Qosimov via B4 Relay
` (2 subsequent siblings)
4 siblings, 1 reply; 7+ messages in thread
From: Ayubjon Qosimov via B4 Relay @ 2026-12-04 15:29 UTC (permalink / raw)
To: Andrew Morton
Cc: Mike Rapoport, linux-mm, Catalin Marinas, Will Deacon,
Mark Rutland, linux-arm-kernel, Paul Walmsley, linux-riscv,
Huacai Chen, loongarch, nh-open-source, linux-kernel,
Ayubjon Qosimov
From: Ayubjon Qosimov <ayubjon@amazon.de>
The hint that reserve_memblock_reserved_resources() uses must leave the
resource tree exactly as reserve_region_with_split() builds it; it only
changes where each walk starts.
Build the same tree under two identical roots, once with
reserve_region_with_split() and once through
__reserve_region_with_split() with a hint, for ascending ranges, ranges
that overlap pre-existing children, ranges that overlap the hint (as page
rounding produces) and unsorted ranges, and check after each range that
the hint is a child of the root starting no higher than the range. A
second case checks that a range a child already covers moves the hint
onto that child. A third runs the helper's walk on a memblock table of
its own, against the loop the architectures used: memory resources out
of order, a range spanning two of them, a resource no range reaches,
overlapping resources, a pre-existing child, and inverted and wrapping
entries that must be skipped.
For this, __reserve_region_with_split() and the walk,
__reserve_memblock_resources(), are made visible to KUnit through
kernel/resource_internal.h. The functions are __init, so the cases run
in an init-section suite, and only when the test is built in.
Signed-off-by: Ayubjon Qosimov <ayubjon@amazon.de>
---
kernel/resource.c | 12 ++-
kernel/resource_internal.h | 18 ++++
kernel/resource_kunit.c | 254 +++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 279 insertions(+), 5 deletions(-)
diff --git a/kernel/resource.c b/kernel/resource.c
index 2b274f30fbde..247c4b56563d 100644
--- a/kernel/resource.c
+++ b/kernel/resource.c
@@ -31,6 +31,9 @@
#include <linux/string.h>
#include <linux/vmalloc.h>
#include <asm/io.h>
+#include <kunit/visibility.h>
+
+#include "resource_internal.h"
struct resource ioport_resource = {
@@ -1169,7 +1172,7 @@ int adjust_resource(struct resource *res, resource_size_t start,
EXPORT_SYMBOL(adjust_resource);
/* Called with resource_lock held; @hint is passed to __request_resource_hint(). */
-static void __init
+VISIBLE_IF_KUNIT void __init
__reserve_region_with_split(struct resource *root, resource_size_t start,
resource_size_t end, const char *name,
struct resource **hint)
@@ -1261,10 +1264,9 @@ reserve_region_with_split(struct resource *root, resource_size_t start,
}
/* reserve_memblock_reserved_resources() on the ranges of @type. */
-static void __init __reserve_memblock_resources(struct memblock_type *type,
- struct resource *res,
- unsigned int nr,
- const char *name)
+VISIBLE_IF_KUNIT void __init
+__reserve_memblock_resources(struct memblock_type *type, struct resource *res,
+ unsigned int nr, const char *name)
{
phys_addr_t r_start, r_end;
unsigned int k;
diff --git a/kernel/resource_internal.h b/kernel/resource_internal.h
new file mode 100644
index 000000000000..81a872b48385
--- /dev/null
+++ b/kernel/resource_internal.h
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _KERNEL_RESOURCE_INTERNAL_H
+#define _KERNEL_RESOURCE_INTERNAL_H
+
+#include <linux/ioport.h>
+
+#if IS_ENABLED(CONFIG_KUNIT)
+/* Exposed for kernel/resource_kunit.c */
+void __reserve_region_with_split(struct resource *root, resource_size_t start,
+ resource_size_t end, const char *name,
+ struct resource **hint);
+struct memblock_type;
+void __reserve_memblock_resources(struct memblock_type *type,
+ struct resource *res, unsigned int nr,
+ const char *name);
+#endif
+
+#endif /* _KERNEL_RESOURCE_INTERNAL_H */
diff --git a/kernel/resource_kunit.c b/kernel/resource_kunit.c
index 9eeb2b1a85c0..75d7a92a23bb 100644
--- a/kernel/resource_kunit.c
+++ b/kernel/resource_kunit.c
@@ -9,6 +9,11 @@
#include <linux/string.h>
#include <linux/sizes.h>
#include <linux/mm.h>
+#include <linux/slab.h>
+#include <linux/memblock.h>
+#include <linux/pfn.h>
+
+#include "resource_internal.h"
#define R0_START 0x0000
#define R0_END 0xffff
@@ -305,5 +310,254 @@ static struct kunit_suite resource_test_suite = {
};
kunit_test_suite(resource_test_suite);
+#if IS_BUILTIN(CONFIG_RESOURCE_KUNIT_TEST)
+/*
+ * The functions below are __init, so these cases run in an init-section suite
+ * and only when the test is built in. Each case builds the same tree twice,
+ * once the way the architectures used to and once with the hint, and checks
+ * that the two match.
+ */
+struct hint_range {
+ resource_size_t start, end;
+};
+
+/* Frees every child below @root; they are all kmalloc'ed. */
+static void __init hint_free_tree(struct resource *root)
+{
+ struct resource *p = root->child, *next;
+
+ root->child = NULL;
+ for (; p; p = next) {
+ next = p->sibling;
+ hint_free_tree(p);
+ kfree(p);
+ }
+}
+
+struct hint_roots {
+ struct resource *res;
+ unsigned int nr;
+};
+
+/* Runs when the case ends, also after a failed assertion. */
+static void __init hint_cleanup(void *data)
+{
+ struct hint_roots *roots = data;
+ unsigned int k;
+
+ for (k = 0; k < roots->nr; k++)
+ hint_free_tree(&roots->res[k]);
+}
+
+static struct resource * __init hint_alloc_roots(struct kunit *test,
+ const struct hint_range *mem,
+ unsigned int nr)
+{
+ struct hint_roots *roots;
+ unsigned int k;
+
+ roots = kunit_kzalloc(test, sizeof(*roots), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, roots);
+ roots->res = kunit_kcalloc(test, nr, sizeof(*roots->res), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, roots->res);
+ roots->nr = nr;
+ for (k = 0; k < nr; k++) {
+ resource_size_t size = mem[k].end - mem[k].start + 1;
+
+ roots->res[k] = DEFINE_RES_MEM_NAMED(mem[k].start, size, "mem");
+ }
+ resource_test_add_action_or_abort(test, hint_cleanup, roots);
+ return roots->res;
+}
+
+/* Request a kmalloc'ed [start, end] named "pre" under @a and under @b. */
+static void __init hint_add_pre(struct kunit *test, struct resource *a,
+ struct resource *b, resource_size_t start,
+ resource_size_t end)
+{
+ struct resource *root[] = { a, b }, *r;
+ int i, ret;
+
+ for (i = 0; i < 2; i++) {
+ r = kzalloc_obj(*r);
+ KUNIT_ASSERT_NOT_NULL(test, r);
+ *r = DEFINE_RES_MEM_NAMED(start, end - start + 1, "pre");
+ ret = request_resource(root[i], r);
+ if (ret)
+ kfree(r);
+ KUNIT_ASSERT_EQ(test, 0, ret);
+ }
+}
+
+static void __init hint_expect_same(struct kunit *test, struct resource *a,
+ struct resource *b)
+{
+ struct resource *pa, *pb;
+
+ for (pa = a->child, pb = b->child; pa && pb;
+ pa = pa->sibling, pb = pb->sibling) {
+ KUNIT_EXPECT_EQ(test, pa->start, pb->start);
+ KUNIT_EXPECT_EQ(test, pa->end, pb->end);
+ KUNIT_EXPECT_STREQ(test, pa->name, pb->name);
+ KUNIT_EXPECT_PTR_EQ(test, pb->parent, b);
+ hint_expect_same(test, pa, pb);
+ }
+ KUNIT_EXPECT_NULL(test, pa);
+ KUNIT_EXPECT_NULL(test, pb);
+}
+
+struct hint_case {
+ const char *desc;
+ struct hint_range pre[2];
+ unsigned int npre;
+ struct hint_range r[5];
+ unsigned int n;
+ bool tail;
+};
+
+static const struct hint_case hint_cases[] = {
+ { "ascending", {}, 0,
+ { { 0x0000, 0x0fff }, { 0x2000, 0x2fff }, { 0x3000, 0x3fff },
+ { 0x8000, 0x80ff }, { 0x9000, 0xffff } }, 5, true },
+ { "pre-existing children", { { 0x1800, 0x27ff }, { 0x6000, 0x6fff } }, 2,
+ { { 0x1000, 0x1fff }, { 0x2000, 0x3fff }, { 0x5000, 0x7fff },
+ { 0x6100, 0x61ff }, { 0x8000, 0x8fff } }, 5, false },
+ { "overlapping the hint", {}, 0,
+ { { 0x1000, 0x1fff }, { 0x1000, 0x1fff }, { 0x1000, 0x2fff },
+ { 0x2000, 0x3fff }, { 0x4000, 0x4fff } }, 5, false },
+ { "unsorted", {}, 0,
+ { { 0x9000, 0x9fff }, { 0x5000, 0x5fff }, { 0x7000, 0x7fff },
+ { 0x1000, 0x1fff }, { 0x0000, 0x0fff } }, 5, false },
+};
+
+static void hint_case_desc(const struct hint_case *c, char *desc)
+{
+ strscpy(desc, c->desc, KUNIT_PARAM_DESC_SIZE);
+}
+
+KUNIT_ARRAY_PARAM(hint, hint_cases, hint_case_desc);
+
+static void __init resource_test_hint(struct kunit *test)
+{
+ static const struct hint_range root = { 0, SZ_64K - 1 };
+ const struct hint_case *c = test->param_value;
+ struct resource *a, *b, *hint = NULL;
+ unsigned int i;
+
+ a = hint_alloc_roots(test, &root, 1);
+ b = hint_alloc_roots(test, &root, 1);
+ for (i = 0; i < c->npre; i++)
+ hint_add_pre(test, a, b, c->pre[i].start, c->pre[i].end);
+
+ for (i = 0; i < c->n; i++) {
+ reserve_region_with_split(a, c->r[i].start, c->r[i].end, "r");
+ /* @b is not in the resource tree, so resource_lock is not needed. */
+ __reserve_region_with_split(b, c->r[i].start, c->r[i].end, "r",
+ &hint);
+
+ /* The hint is a child of @b starting no higher than r[i].end. */
+ KUNIT_ASSERT_NOT_NULL(test, hint);
+ KUNIT_EXPECT_PTR_EQ(test, hint->parent, b);
+ KUNIT_EXPECT_LE(test, hint->start, c->r[i].end);
+ if (c->tail)
+ KUNIT_EXPECT_NULL(test, hint->sibling);
+ }
+ hint_expect_same(test, a, b);
+}
+
+static void __init resource_test_hint_absorbed(struct kunit *test)
+{
+ /* A range that a child covers moves the hint onto that child. */
+ static const struct hint_range root = { 0, SZ_64K - 1 };
+ struct resource *a, *b, *pre, *hint = NULL;
+
+ a = hint_alloc_roots(test, &root, 1);
+ b = hint_alloc_roots(test, &root, 1);
+ hint_add_pre(test, a, b, 0x6000, 0x6fff);
+ pre = b->child;
+
+ __reserve_region_with_split(b, 0x5000, 0x5fff, "r", &hint);
+ KUNIT_ASSERT_NOT_NULL(test, hint);
+ KUNIT_EXPECT_PTR_EQ(test, hint->sibling, pre);
+ __reserve_region_with_split(b, 0x6100, 0x61ff, "r", &hint);
+ KUNIT_EXPECT_PTR_EQ(test, hint, pre);
+ __reserve_region_with_split(b, 0x6200, 0x62ff, "r", &hint);
+ KUNIT_EXPECT_PTR_EQ(test, hint, pre);
+}
+
+static void __init resource_test_memblock_walk(struct kunit *test)
+{
+ /*
+ * Memory resources out of order: a range spans two of them, no range
+ * reaches one, one overlaps another, one has a child already, and an
+ * inverted and a wrapping one must be skipped.
+ */
+ static struct memblock_region regions[] __initdata = {
+ { .base = 1 * PAGE_SIZE, .size = PAGE_SIZE },
+ { .base = 3 * PAGE_SIZE, .size = 2 * PAGE_SIZE },
+ { .base = 7 * PAGE_SIZE + PAGE_SIZE / 2, .size = PAGE_SIZE / 16 },
+ { .base = 10 * PAGE_SIZE, .size = PAGE_SIZE / 16 },
+ { .base = 12 * PAGE_SIZE, .size = 4 * PAGE_SIZE },
+ };
+ static struct memblock_type type __initdata = {
+ .regions = regions,
+ .cnt = ARRAY_SIZE(regions),
+ .max = ARRAY_SIZE(regions),
+ .name = "test",
+ };
+#define P(n) ((resource_size_t)(n) * PAGE_SIZE)
+ static const struct hint_range mem[] __initconst = {
+ { P(14), P(17) - 1 }, { P(9), P(10) - 1 }, { P(4), P(8) - 1 },
+ { P(11), P(14) - 1 }, { P(0), P(4) - 1 }, { P(2), P(6) - 1 },
+ { P(32), P(32) - 1 }, { 0, ~(resource_size_t)0 },
+ };
+#undef P
+ const unsigned int nr = ARRAY_SIZE(mem);
+ phys_addr_t r_start, r_end;
+ struct resource *a, *b;
+ unsigned int k;
+ u64 i;
+
+ a = hint_alloc_roots(test, mem, nr);
+ b = hint_alloc_roots(test, mem, nr);
+ hint_add_pre(test, &a[2], &b[2], 4 * PAGE_SIZE + PAGE_SIZE / 4,
+ 4 * PAGE_SIZE + PAGE_SIZE / 2 - 1);
+
+ /* The loop the helper replaces, on the six valid resources. */
+ for (k = 0; k < nr - 2; k++) {
+ __for_each_mem_range(i, &type, NULL, NUMA_NO_NODE, MEMBLOCK_NONE,
+ &r_start, &r_end, NULL) {
+ resource_size_t start, end;
+
+ start = max(PFN_PHYS(PFN_DOWN(r_start)), a[k].start);
+ end = min(PFN_PHYS(PFN_UP(r_end)) - 1, a[k].end);
+ if (start > a[k].end || end < a[k].start)
+ continue;
+ reserve_region_with_split(&a[k], start, end, "r");
+ }
+ }
+ __reserve_memblock_resources(&type, b, nr, "r");
+
+ for (k = 0; k < nr; k++)
+ hint_expect_same(test, &a[k], &b[k]);
+ KUNIT_EXPECT_NOT_NULL(test, b[0].child);
+ KUNIT_EXPECT_NULL(test, b[1].child);
+ KUNIT_EXPECT_NOT_NULL(test, b[3].child);
+}
+
+static struct kunit_case __refdata resource_init_test_cases[] = {
+ KUNIT_CASE_PARAM(resource_test_hint, hint_gen_params),
+ KUNIT_CASE(resource_test_hint_absorbed),
+ KUNIT_CASE(resource_test_memblock_walk),
+ {}
+};
+
+static struct kunit_suite resource_init_test_suite = {
+ .name = "resource_init",
+ .test_cases = resource_init_test_cases,
+};
+kunit_test_init_section_suite(resource_init_test_suite);
+#endif /* IS_BUILTIN(CONFIG_RESOURCE_KUNIT_TEST) */
+
MODULE_DESCRIPTION("I/O Port & Memory Resource manager unit tests");
MODULE_LICENSE("GPL");
--
2.47.3
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH 3/5] arm64: Use reserve_memblock_reserved_resources()
2026-12-04 15:29 [PATCH 0/5] Mirror reserved memblock regions in linear time Ayubjon Qosimov via B4 Relay
2026-12-04 15:29 ` [PATCH 1/5] resource: add reserve_memblock_reserved_resources() Ayubjon Qosimov via B4 Relay
2026-12-04 15:29 ` [PATCH 2/5] resource, kunit: add test cases for reserve_memblock_reserved_resources() Ayubjon Qosimov via B4 Relay
@ 2026-12-04 15:29 ` Ayubjon Qosimov via B4 Relay
2026-12-04 15:29 ` [PATCH 4/5] riscv: use reserve_memblock_reserved_resources() Ayubjon Qosimov via B4 Relay
2026-12-04 15:29 ` [PATCH 5/5] LoongArch: Use reserve_memblock_reserved_resources() Ayubjon Qosimov via B4 Relay
4 siblings, 0 replies; 7+ messages in thread
From: Ayubjon Qosimov via B4 Relay @ 2026-12-04 15:29 UTC (permalink / raw)
To: Andrew Morton
Cc: Mike Rapoport, linux-mm, Catalin Marinas, Will Deacon,
Mark Rutland, linux-arm-kernel, Paul Walmsley, linux-riscv,
Huacai Chen, loongarch, nh-open-source, linux-kernel,
Ayubjon Qosimov
From: Ayubjon Qosimov <ayubjon@amazon.de>
reserve_memblock_reserved_regions() adds a "reserved" child under each
standard memory resource for every memblock.reserved range it overlaps,
and each insertion walks the parent's child list from its head, so n
ranges cost O(n^2). After a kexec with Kexec HandOver (KHO) that
preserves many separate pages, for example memfd or vmalloc memory
preserved through the Live Update Orchestrator, there are thousands of
such ranges, and on a host that is live updated this way the initcall
adds directly to the time the guests are paused.
Use reserve_memblock_reserved_resources(), which does the same under one
hold of resource_lock and with a hint per memory resource, so the
insertions cost O(n). The resulting resource tree is unchanged.
Measured in a 96-vCPU KVM guest on a Graviton4 host, standing in for a
large host, in the kernel booted by a KHO kexec, with lib/test_kho.c
changed locally to preserve order-0 pages with a free page between each
(the stock test preserves large folios that merge into a few ranges).
The time is the gap between the initcall_debug "calling" and "returned"
lines for reserve_memblock_reserved_regions(), min-max over the boots:
ranges before after boots
3223 11.9-12.3 ms 0.17-0.18 ms 5
/proc/iomem is unchanged except for ranges that also move between two
boots of the same kernel.
Signed-off-by: Ayubjon Qosimov <ayubjon@amazon.de>
---
arch/arm64/kernel/setup.c | 24 ++----------------------
1 file changed, 2 insertions(+), 22 deletions(-)
diff --git a/arch/arm64/kernel/setup.c b/arch/arm64/kernel/setup.c
index a707deba6ae7..af16e68ab0f9 100644
--- a/arch/arm64/kernel/setup.c
+++ b/arch/arm64/kernel/setup.c
@@ -245,28 +245,8 @@ static void __init request_standard_resources(void)
static int __init reserve_memblock_reserved_regions(void)
{
- u64 i, j;
-
- for (i = 0; i < num_standard_resources; ++i) {
- struct resource *mem = &standard_resources[i];
- phys_addr_t r_start, r_end, mem_size = resource_size(mem);
-
- if (!memblock_is_region_reserved(mem->start, mem_size))
- continue;
-
- for_each_reserved_mem_range(j, &r_start, &r_end) {
- resource_size_t start, end;
-
- start = max(PFN_PHYS(PFN_DOWN(r_start)), mem->start);
- end = min(PFN_PHYS(PFN_UP(r_end)) - 1, mem->end);
-
- if (start > mem->end || end < mem->start)
- continue;
-
- reserve_region_with_split(mem, start, end, "reserved");
- }
- }
-
+ reserve_memblock_reserved_resources(standard_resources,
+ num_standard_resources, "reserved");
return 0;
}
arch_initcall(reserve_memblock_reserved_regions);
--
2.47.3
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH 4/5] riscv: use reserve_memblock_reserved_resources()
2026-12-04 15:29 [PATCH 0/5] Mirror reserved memblock regions in linear time Ayubjon Qosimov via B4 Relay
` (2 preceding siblings ...)
2026-12-04 15:29 ` [PATCH 3/5] arm64: Use reserve_memblock_reserved_resources() Ayubjon Qosimov via B4 Relay
@ 2026-12-04 15:29 ` Ayubjon Qosimov via B4 Relay
2026-12-04 15:29 ` [PATCH 5/5] LoongArch: Use reserve_memblock_reserved_resources() Ayubjon Qosimov via B4 Relay
4 siblings, 0 replies; 7+ messages in thread
From: Ayubjon Qosimov via B4 Relay @ 2026-12-04 15:29 UTC (permalink / raw)
To: Andrew Morton
Cc: Mike Rapoport, linux-mm, Catalin Marinas, Will Deacon,
Mark Rutland, linux-arm-kernel, Paul Walmsley, linux-riscv,
Huacai Chen, loongarch, nh-open-source, linux-kernel,
Ayubjon Qosimov
From: Ayubjon Qosimov <ayubjon@amazon.de>
reserve_memblock_reserved_regions() is a copy of what
reserve_memblock_reserved_resources() now does, with "Reserved" as the
name of the children. Use the helper instead. riscv does not support
Kexec HandOver, so this only removes a duplicate loop: /proc/iomem is
unchanged.
riscv selects ARCH_KEEP_MEMBLOCK only with ACPI or KEXEC. Without it,
this initcall already ran after memblock_discard(); that does not change
here.
Signed-off-by: Ayubjon Qosimov <ayubjon@amazon.de>
---
arch/riscv/kernel/setup.c | 24 ++----------------------
1 file changed, 2 insertions(+), 22 deletions(-)
diff --git a/arch/riscv/kernel/setup.c b/arch/riscv/kernel/setup.c
index d79f99b86033..10e585759864 100644
--- a/arch/riscv/kernel/setup.c
+++ b/arch/riscv/kernel/setup.c
@@ -226,28 +226,8 @@ static void __init init_resources(void)
static int __init reserve_memblock_reserved_regions(void)
{
- u64 i, j;
-
- for (i = 0; i < num_standard_resources; i++) {
- struct resource *mem = &standard_resources[i];
- phys_addr_t r_start, r_end, mem_size = resource_size(mem);
-
- if (!memblock_is_region_reserved(mem->start, mem_size))
- continue;
-
- for_each_reserved_mem_range(j, &r_start, &r_end) {
- resource_size_t start, end;
-
- start = max(PFN_PHYS(PFN_DOWN(r_start)), mem->start);
- end = min(PFN_PHYS(PFN_UP(r_end)) - 1, mem->end);
-
- if (start > mem->end || end < mem->start)
- continue;
-
- reserve_region_with_split(mem, start, end, "Reserved");
- }
- }
-
+ reserve_memblock_reserved_resources(standard_resources,
+ num_standard_resources, "Reserved");
return 0;
}
arch_initcall(reserve_memblock_reserved_regions);
--
2.47.3
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH 5/5] LoongArch: Use reserve_memblock_reserved_resources()
2026-12-04 15:29 [PATCH 0/5] Mirror reserved memblock regions in linear time Ayubjon Qosimov via B4 Relay
` (3 preceding siblings ...)
2026-12-04 15:29 ` [PATCH 4/5] riscv: use reserve_memblock_reserved_resources() Ayubjon Qosimov via B4 Relay
@ 2026-12-04 15:29 ` Ayubjon Qosimov via B4 Relay
4 siblings, 0 replies; 7+ messages in thread
From: Ayubjon Qosimov via B4 Relay @ 2026-12-04 15:29 UTC (permalink / raw)
To: Andrew Morton
Cc: Mike Rapoport, linux-mm, Catalin Marinas, Will Deacon,
Mark Rutland, linux-arm-kernel, Paul Walmsley, linux-riscv,
Huacai Chen, loongarch, nh-open-source, linux-kernel,
Ayubjon Qosimov
From: Ayubjon Qosimov <ayubjon@amazon.de>
reserve_memblock_reserved_regions() is a copy of what
reserve_memblock_reserved_resources() now does, with "Reserved" as the
name of the children. Use the helper instead. LoongArch does not support
Kexec HandOver, so this only removes a duplicate loop: /proc/iomem is
unchanged.
Signed-off-by: Ayubjon Qosimov <ayubjon@amazon.de>
---
arch/loongarch/kernel/setup.c | 24 ++----------------------
1 file changed, 2 insertions(+), 22 deletions(-)
diff --git a/arch/loongarch/kernel/setup.c b/arch/loongarch/kernel/setup.c
index 6fa4a22a58fd..6e80384dfb0b 100644
--- a/arch/loongarch/kernel/setup.c
+++ b/arch/loongarch/kernel/setup.c
@@ -542,28 +542,8 @@ arch_initcall(arch_reserve_pio_range);
static int __init reserve_memblock_reserved_regions(void)
{
- u64 i, j;
-
- for (i = 0; i < num_standard_resources; ++i) {
- struct resource *mem = &standard_resources[i];
- phys_addr_t r_start, r_end, mem_size = resource_size(mem);
-
- if (!memblock_is_region_reserved(mem->start, mem_size))
- continue;
-
- for_each_reserved_mem_range(j, &r_start, &r_end) {
- resource_size_t start, end;
-
- start = max(PFN_PHYS(PFN_DOWN(r_start)), mem->start);
- end = min(PFN_PHYS(PFN_UP(r_end)) - 1, mem->end);
-
- if (start > mem->end || end < mem->start)
- continue;
-
- reserve_region_with_split(mem, start, end, "Reserved");
- }
- }
-
+ reserve_memblock_reserved_resources(standard_resources,
+ num_standard_resources, "Reserved");
return 0;
}
arch_initcall(reserve_memblock_reserved_regions);
--
2.47.3
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH 2/5] resource, kunit: add test cases for reserve_memblock_reserved_resources()
2026-12-04 15:29 ` [PATCH 2/5] resource, kunit: add test cases for reserve_memblock_reserved_resources() Ayubjon Qosimov via B4 Relay
@ 2026-12-05 15:30 ` sashiko-bot
0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-12-05 15:30 UTC (permalink / raw)
To: Ayubjon Qosimov, Andrew Morton
Cc: Mike Rapoport, linux-mm, Catalin Marinas, Will Deacon,
Mark Rutland, Paul Walmsley, Huacai Chen, nh-open-source
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] resource, kunit: unused reference tree a in
resource_test_hint_absorbed()
--
commit 1fb74f6da0b7f065e68520a5d0823883c99a3f51
Author: Ayubjon Qosimov <ayubjon@amazon.de>
resource, kunit: add test cases for reserve_memblock_reserved_resources()
This commit exposes __reserve_region_with_split() and
__reserve_memblock_resources() to KUnit and adds an init-section KUnit
test suite in kernel/resource_kunit.c to verify hint-based resource
reservation against reserve_region_with_split().
> diff --git a/kernel/resource_kunit.c b/kernel/resource_kunit.c
> index 9eeb2b1a85c01..75d7a92a23bb5 100644
> --- a/kernel/resource_kunit.c
> +++ b/kernel/resource_kunit.c
[ ... ]
> +static void __init resource_test_hint_absorbed(struct kunit *test)
> +{
> + /* A range that a child covers moves the hint onto that child. */
> + static const struct hint_range root = { 0, SZ_64K - 1 };
> + struct resource *a, *b, *pre, *hint = NULL;
> +
> + a = hint_alloc_roots(test, &root, 1);
> + b = hint_alloc_roots(test, &root, 1);
> + hint_add_pre(test, a, b, 0x6000, 0x6fff);
> + pre = b->child;
> +
> + __reserve_region_with_split(b, 0x5000, 0x5fff, "r", &hint);
> + KUNIT_ASSERT_NOT_NULL(test, hint);
> + KUNIT_EXPECT_PTR_EQ(test, hint->sibling, pre);
> + __reserve_region_with_split(b, 0x6100, 0x61ff, "r", &hint);
> + KUNIT_EXPECT_PTR_EQ(test, hint, pre);
> + __reserve_region_with_split(b, 0x6200, 0x62ff, "r", &hint);
> + KUNIT_EXPECT_PTR_EQ(test, hint, pre);
> +}
[Severity: Low]
Is root a intentionally unused after hint_add_pre()?
resource_test_hint_absorbed() allocates and populates a alongside b,
consistent with the suite comment that each case builds both trees and
checks that they match, but never reserves on a or calls
hint_expect_same(test, a, b).
Mirroring the three reservations on a with reserve_region_with_split()
and calling hint_expect_same(test, a, b) would match the other cases
and the suite comment.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204-b4-iomem-ev5-v1-0-ccf26a176cf6@amazon.de?part=2
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-12-05 15:30 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 15:29 [PATCH 0/5] Mirror reserved memblock regions in linear time Ayubjon Qosimov via B4 Relay
2026-12-04 15:29 ` [PATCH 1/5] resource: add reserve_memblock_reserved_resources() Ayubjon Qosimov via B4 Relay
2026-12-04 15:29 ` [PATCH 2/5] resource, kunit: add test cases for reserve_memblock_reserved_resources() Ayubjon Qosimov via B4 Relay
2026-12-05 15:30 ` sashiko-bot
2026-12-04 15:29 ` [PATCH 3/5] arm64: Use reserve_memblock_reserved_resources() Ayubjon Qosimov via B4 Relay
2026-12-04 15:29 ` [PATCH 4/5] riscv: use reserve_memblock_reserved_resources() Ayubjon Qosimov via B4 Relay
2026-12-04 15:29 ` [PATCH 5/5] LoongArch: Use reserve_memblock_reserved_resources() Ayubjon Qosimov via B4 Relay
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox