From: Ayubjon Qosimov 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 --- 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 #include #include +#include #include #include #include @@ -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