Skip to contentSkip to conversation

treewide, numa_memblks: remove redundant work during NUMA init

PATCH Applied Sang-Heon Jeon 4 participants 15 messages Jul 3 – Jul 7

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

v2 cover treewide, numa_memblks: remove redundant work during NUMA init

Every existing numa_add_memblk() caller passes a valid node id and separately marks that node in numa_nodes_parsed with node_set(). In addition, numa_nodemask_from_meminfo() recomputes the same "nodes that own memory" set from numa_meminfo, which numa_nodes_parsed already contains.

This redundancy implicitly depends on the callers' node_set(). So, before removing the redundancy, make numa_add_memblk() set the node in numa_nodes_parsed explicitly. Then remove the per-caller node_set() and numa_nodemask_from_meminfo().

Also, since the generic numa_register_meminfo() already sets node_possible_map to numa_nodes_parsed, remove the duplicate assignment in arch_numa's numa_register_nodes().

Patch 1 adds the node_set() to numa_add_memblk() itself, so every memblk's node is set in numa_nodes_parsed on add.

Patches 2-6 depend on patch 1 and remove the redundant per-caller node_set() from all callers.

Patch 7 removes both numa_nodemask_from_meminfo() call sites and the unused function itself.

Patch 8 removes the duplicate node_possible_map assignment in arch_numa.

Patch 9 is a minor cleanup, using the existing numa_add_reserved_memblk() wrapper in numa_cleanup_meminfo().

No functional change.

---
Changes from v1 [1]
- remove warning in numa_add_memblk_to() when start == end
- add Acked-by tag
- rebased onto latest mm-new

[1] https://lore.kernel.org/all/[email protected]/
---

Sang-Heon Jeon (9):
mm: numa_memblks: set numa_nodes_parsed in numa_add_memblk()
ACPI: NUMA: remove redundant numa_nodes_parsed node_set() of/numa: remove redundant numa_nodes_parsed node_set() x86/numa: remove redundant numa_nodes_parsed node_set()
arch_numa: remove redundant numa_nodes_parsed node_set()
LoongArch: remove redundant numa_nodes_parsed node_set()
mm: numa_memblks: remove redundant numa_nodemask_from_meminfo()
arch_numa: remove redundant node_possible_map assignment
mm: numa_memblks: use numa_add_reserved_memblk() in
numa_cleanup_meminfo()

 arch/loongarch/kernel/numa.c |  1 -
 arch/x86/mm/amdtopology.c    |  1 -
 arch/x86/mm/numa.c           |  1 -
 drivers/acpi/numa/srat.c     |  2 --
 drivers/base/arch_numa.c     |  4 ----
 drivers/of/of_numa.c         |  5 +----
 mm/numa_memblks.c            | 39 +++++++++++++++++-------------------
 7 files changed, 19 insertions(+), 34 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/9 mm: numa_memblks: set numa_nodes_parsed in numa_add_memblk()

Every existing numa_add_memblk() caller separately marks the new node in numa_nodes_parsed with node_set(). Set the node in numa_add_memblk() itself on a successful add, so this no longer depends on each caller.

numa_add_memblk_to() now returns -EINVAL for an out-of-range node id, so a zero return implies @nid was valid. No caller passes an invalid one, so existing callers are unaffected.

The per-caller node_set() calls are removed in later patches.

No functional change.

---
 mm/numa_memblks.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
mm/numa_memblks.c +17 -3
index 3c3c4eac3514..9815192549c3 100644
@@ -135,13 +135,20 @@ EXPORT_SYMBOL(__node_distance);
 static int __init numa_add_memblk_to(int nid, u64 start, u64 end,
 				     struct numa_meminfo *mi)
 {
+	/* whine about and ignore invalid nid */
+	if (nid < 0 || nid >= MAX_NUMNODES) {
+		pr_warn("Warning: invalid memblk node id %d [mem %#010Lx-%#010Lx]\n",
+			nid, start, end - 1);
+		return -EINVAL;
+	}
+
 	/* ignore zero length blks */
 	if (start == end)
 		return 0;
 
 	/* whine about and ignore invalid blks */
-	if (start > end || nid < 0 || nid >= MAX_NUMNODES) {
-		pr_warn("Warning: invalid memblk node %d [mem %#010Lx-%#010Lx]\n",
+	if (start > end) {
+		pr_warn("Warning: invalid memblk node size %d [mem %#010Lx-%#010Lx]\n",
 			nid, start, end - 1);
 		return 0;
 	}
@@ -193,13 +200,20 @@ static void __init numa_move_tail_memblk(struct numa_meminfo *dst, int idx,
  * @end: End address of the new memblk
  *
  * Add a new memblk to the default numa_meminfo.
+ * On success @nid is also set in numa_nodes_parsed.
  *
  * RETURNS:
  * 0 on success, -errno on failure.
  */
 int __init numa_add_memblk(int nid, u64 start, u64 end)
 {
-	return numa_add_memblk_to(nid, start, end, &numa_meminfo);
+	int ret;
+
+	ret = numa_add_memblk_to(nid, start, end, &numa_meminfo);
+	if (!ret)
+		node_set(nid, numa_nodes_parsed);
+
+	return ret;
 }
 
 /**
Signature · 1 line
2.43.0
#2 lore raw

PATCH v2 2/9 ACPI: NUMA: remove redundant numa_nodes_parsed node_set()

In reply to Sang-Heon Jeon

numa_add_memblk() now sets the node in numa_nodes_parsed itself, so the caller's own node_set() is redundant. Remove it.

No functional change.

---
 drivers/acpi/numa/srat.c | 2 --
 1 file changed, 2 deletions(-)
drivers/acpi/numa/srat.c +0 -2
index 62d4a8df0b8c..5c407dc6401e 100644
@@ -399,8 +399,6 @@ acpi_parse_memory_affinity(union acpi_subtable_headers *header,
 		goto out_err_bad_srat;
 	}
 
-	node_set(node, numa_nodes_parsed);
-
 	pr_info("SRAT: Node %u PXM %u [mem %#010Lx-%#010Lx]%s%s\n",
 		node, pxm,
 		(unsigned long long) start, (unsigned long long) end - 1,
Signature · 1 line
2.43.0
maintainer Applied
#3 lore raw

Re: [PATCH v2 2/9] ACPI: NUMA: remove redundant numa_nodes_parsed node_set()

Quoted 16 lines · Sang-Heon Jeon

numa_add_memblk() now sets the node in numa_nodes_parsed itself, so the caller's own node_set() is redundant. Remove it.

No functional change.

---
 drivers/acpi/numa/srat.c | 2 --
 1 file changed, 2 deletions(-)
drivers/acpi/numa/srat.c +0 -0
index 62d4a8df0b8c..5c407dc6401e 100644

drivers/acpi/numa/srat.c acpi_parse_memory_affinity(union acpi_subtable_headers *header,

Quoted 9 lines
drivers/acpi/numa/srat.c +0 -3
@@ -399,8 +399,6 @@ acpi_parse_memory_affinity(union acpi_subtable_headers *header,
                goto out_err_bad_srat;
        }
 
-       node_set(node, numa_nodes_parsed);
-
        pr_info("SRAT: Node %u PXM %u [mem %#010Lx-%#010Lx]%s%s\n",
                node, pxm,
                (unsigned long long) start, (unsigned long long) end - 1,
--

Applied as 7.3 material, thanks!

maintainer
#4 lore raw

Re: [PATCH v2 2/9] ACPI: NUMA: remove redundant numa_nodes_parsed node_set()

Hi Rafael,

Quoted 20 lines · Rafael J. Wysocki (Intel)
Quoted 19 lines · Sang-Heon Jeon

numa_add_memblk() now sets the node in numa_nodes_parsed itself, so the caller's own node_set() is redundant. Remove it.

No functional change.

---
 drivers/acpi/numa/srat.c | 2 --
 1 file changed, 2 deletions(-)
drivers/acpi/numa/srat.c +0 -0
index 62d4a8df0b8c..5c407dc6401e 100644
@@ -399,8 +399,6 @@ acpi_parse_memory_affinity(union acpi_subtable_headers *header,
                goto out_err_bad_srat;
        }
 
Quoted 8 lines

drivers/acpi/numa/srat.c acpi_parse_memory_affinity(union acpi_subtable_headers *header,

Quoted 6 lines
drivers/acpi/numa/srat.c +0 -3
-       node_set(node, numa_nodes_parsed);
-
        pr_info("SRAT: Node %u PXM %u [mem %#010Lx-%#010Lx]%s%s\n",
                node, pxm,
                (unsigned long long) start, (unsigned long long) end - 1,
--

Applied as 7.3 material, thanks!

This depends on patch 1 in the series, so I think it's better to keep the series together and merge via the memblock tree so I took it there a few days ago:

https://lore.kernel.org/all/178306815065.2173096.9480193260074524906.b4-ty@b4

Would you like me to add your tag for this patch?

Signature · 2 lines
Sincerely yours,
Mike.
#5 lore raw

PATCH v2 3/9 of/numa: remove redundant numa_nodes_parsed node_set()

In reply to Sang-Heon Jeon

numa_add_memblk() now sets the node in numa_nodes_parsed itself, so the caller's own node_set() is redundant. Remove it.

No functional change.

---
 drivers/of/of_numa.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/of/of_numa.c +1 -4
index cd2dc8e825c9..230d5f628c1b 100644
@@ -59,11 +59,8 @@ static int __init of_numa_parse_memory_nodes(void)
 			r = -EINVAL;
 		}
 
-		for (i = 0; !r && !of_address_to_resource(np, i, &rsrc); i++) {
+		for (i = 0; !r && !of_address_to_resource(np, i, &rsrc); i++)
 			r = numa_add_memblk(nid, rsrc.start, rsrc.end + 1);
-			if (!r)
-				node_set(nid, numa_nodes_parsed);
-		}
 
 		if (!i || r) {
 			of_node_put(np);
Signature · 1 line
2.43.0
#6 lore raw

Re: [PATCH v2 3/9] of/numa: remove redundant numa_nodes_parsed node_set()

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] The function's final return status is order-dependent, improperly shadowing earlier successes if the last evaluated memory node lacks a 'numa-node-id' property. --

commit a42c5d6d6a0d76bbe5386c6e6e6307393d31c628
Author: Sang-Heon Jeon <[email protected]>

of/numa: remove redundant numa_nodes_parsed node_set()

This commit simplifies of_numa_parse_memory_nodes() by removing a redundant node_set() call. The operation is now handled internally by numa_add_memblk().

Quoted 5 lines
drivers/of/of_numa.c +0 -0
index cd2dc8e825c92..230d5f628c1b4 100644

drivers/of/of_numa.c static int __init of_numa_parse_memory_nodes(void) · line 66

Quoted 9 lines
drivers/of/of_numa.c +1 -4
@@ -59,11 +59,8 @@ static int __init of_numa_parse_memory_nodes(void)
 			r = -EINVAL;
 		}
 
-		for (i = 0; !r && !of_address_to_resource(np, i, &rsrc); i++) {
+		for (i = 0; !r && !of_address_to_resource(np, i, &rsrc); i++)
 			r = numa_add_memblk(nid, rsrc.start, rsrc.end + 1);
-			if (!r)
-				node_set(nid, numa_nodes_parsed);
-		}

[Severity: High]
This is a pre-existing issue, but does this function accidentally return an error if the last memory node evaluated lacks a NUMA node ID?

Looking at of_numa_parse_memory_nodes(), if the device tree contains multiple memory nodes and the final iterated node does not have a "numa-node-id" property, the loop handles it like this:

    for_each_node_by_type(np, "memory") {
        r = of_property_read_u32(np, "numa-node-id", &nid);
        if (r == -EINVAL)
            continue;
        ...

This clobbers the r variable with -EINVAL and continues to the end of the loop. If this is the last iteration, the loop terminates and the function returns -EINVAL, discarding prior successful parses.

Could this lead to NUMA initialization failing entirely on valid system configurations and falling back to a single node?

Quoted 3 lines
drivers/of/of_numa.c +0 -0
 		if (!i || r) {
 			of_node_put(np);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#7 lore raw

Re: [PATCH v2 3/9] of/numa: remove redundant numa_nodes_parsed node_set()

Quoted 45 lines · On Fri, Jul 3, 2026 at 1:27 PM <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] The function's final return status is order-dependent, improperly shadowing earlier successes if the last evaluated memory node lacks a 'numa-node-id' property. --

commit a42c5d6d6a0d76bbe5386c6e6e6307393d31c628
Author: Sang-Heon Jeon <[email protected]>

of/numa: remove redundant numa_nodes_parsed node_set()

This commit simplifies of_numa_parse_memory_nodes() by removing a redundant node_set() call. The operation is now handled internally by numa_add_memblk().

Quoted 5 lines
drivers/of/of_numa.c +0 -0
index cd2dc8e825c92..230d5f628c1b4 100644

drivers/of/of_numa.c static int __init of_numa_parse_memory_nodes(void) · line 66

Quoted 9 lines
drivers/of/of_numa.c +1 -4
@@ -59,11 +59,8 @@ static int __init of_numa_parse_memory_nodes(void)
                      r = -EINVAL;
              }
 
-             for (i = 0; !r && !of_address_to_resource(np, i, &rsrc); i++) {
+             for (i = 0; !r && !of_address_to_resource(np, i, &rsrc); i++)
                      r = numa_add_memblk(nid, rsrc.start, rsrc.end + 1);
-                     if (!r)
-                             node_set(nid, numa_nodes_parsed);
-             }

[Severity: High]
This is a pre-existing issue, but does this function accidentally return an error if the last memory node evaluated lacks a NUMA node ID?

Looking at of_numa_parse_memory_nodes(), if the device tree contains multiple memory nodes and the final iterated node does not have a "numa-node-id" property, the loop handles it like this:

    for_each_node_by_type(np, "memory") {
        r = of_property_read_u32(np, "numa-node-id", &nid);
        if (r == -EINVAL)
            continue;
        ...
Quoted 10 lines

This clobbers the r variable with -EINVAL and continues to the end of the loop. If this is the last iteration, the loop terminates and the function returns -EINVAL, discarding prior successful parses.

Could this lead to NUMA initialization failing entirely on valid system configurations and falling back to a single node?

Quoted 3 lines
drivers/of/of_numa.c +0 -0
              if (!i || r) {
                      of_node_put(np);

Already being discussed in a separate patch, See [1].

[1] https://lore.kernel.org/all/[email protected]/

Quoted 2 lines

--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3

Best Regards,
Sang-Heon Jeon

#8 lore raw

PATCH v2 4/9 x86/numa: remove redundant numa_nodes_parsed node_set()

In reply to Sang-Heon Jeon

numa_add_memblk() now sets the node in numa_nodes_parsed itself, so the caller's own node_set() is redundant. Remove it.

No functional change.

---
 arch/x86/mm/amdtopology.c | 1 -
 arch/x86/mm/numa.c        | 1 -
 2 files changed, 2 deletions(-)
arch/x86/mm/amdtopology.c +0 -1
index f980b0eb0105..1cb581bbf2b6 100644
@@ -150,7 +150,6 @@ int __init amd_numa_init(void)
 
 		prevbase = base;
 		numa_add_memblk(nodeid, base, limit);
-		node_set(nodeid, numa_nodes_parsed);
 	}
 
 	if (nodes_empty(numa_nodes_parsed))
arch/x86/mm/numa.c +0 -1
index 99d0a9332c14..ced66e68a68e 100644
@@ -216,7 +216,6 @@ static int __init dummy_numa_init(void)
 	printk(KERN_INFO "Faking a node at [mem %#018Lx-%#018Lx]\n",
 	       0LLU, PFN_PHYS(max_pfn) - 1);
 
-	node_set(0, numa_nodes_parsed);
 	node_set(0, numa_phys_nodes_parsed);
 	numa_add_memblk(0, 0, PFN_PHYS(max_pfn));
 
Signature · 1 line
2.43.0
#9 lore raw

PATCH v2 5/9 arch_numa: remove redundant numa_nodes_parsed node_set()

In reply to Sang-Heon Jeon

numa_add_memblk() now sets the node in numa_nodes_parsed itself, so the caller's own node_set() is redundant. Remove it.

No functional change.

---
 drivers/base/arch_numa.c | 1 -
 1 file changed, 1 deletion(-)
drivers/base/arch_numa.c +0 -1
index 442ea239bba7..c7f63c4cf367 100644
@@ -279,7 +279,6 @@ static int __init dummy_numa_init(void)
 		pr_err("NUMA init failed\n");
 		return ret;
 	}
-	node_set(0, numa_nodes_parsed);
 
 	numa_off = true;
 	return 0;
Signature · 1 line
2.43.0
#10 lore raw

PATCH v2 6/9 LoongArch: remove redundant numa_nodes_parsed node_set()

In reply to Sang-Heon Jeon

numa_add_memblk() now sets the node in numa_nodes_parsed itself, so the caller's own node_set() is redundant. Remove it.

No functional change.

---
 arch/loongarch/kernel/numa.c | 1 -
 1 file changed, 1 deletion(-)
arch/loongarch/kernel/numa.c +0 -1
index 8b89898e20df..c96c53623715 100644
@@ -216,7 +216,6 @@ static int __init fake_numa_init(void)
 	phys_addr_t start = memblock_start_of_DRAM();
 	phys_addr_t end = memblock_end_of_DRAM() - 1;
 
-	node_set(0, numa_nodes_parsed);
 	pr_info("Faking a node at [mem %pap-%pap]\n", &start, &end);
 
 	return numa_add_memblk(0, start, end + 1);
Signature · 1 line
2.43.0
#11 lore raw

PATCH v2 7/9 mm: numa_memblks: remove redundant numa_nodemask_from_meminfo()

In reply to Sang-Heon Jeon

numa_add_memblk() now sets each added node in numa_nodes_parsed, so numa_nodes_parsed already contains every node that owns memory. The nodes numa_nodemask_from_meminfo() adds from numa_meminfo are already set, so the calls in numa_alloc_distance() and numa_register_meminfo() are redundant.

So remove both call sites and the unused function itself.

No functional change.

---
 mm/numa_memblks.c | 16 ----------------
 1 file changed, 16 deletions(-)
mm/numa_memblks.c +0 -16
index 9815192549c3..48428060e93f 100644
@@ -17,20 +17,6 @@ nodemask_t numa_nodes_parsed __initdata;
 static struct numa_meminfo numa_meminfo __initdata_or_meminfo;
 static struct numa_meminfo numa_reserved_meminfo __initdata_or_meminfo;
 
-/*
- * Set nodes, which have memory in @mi, in *@nodemask.
- */
-static void __init numa_nodemask_from_meminfo(nodemask_t *nodemask,
-					      const struct numa_meminfo *mi)
-{
-	int i;
-
-	for (i = 0; i < ARRAY_SIZE(mi->blk); i++)
-		if (mi->blk[i].start != mi->blk[i].end &&
-		    mi->blk[i].nid != NUMA_NO_NODE)
-			node_set(mi->blk[i].nid, *nodemask);
-}
-
 /**
  * numa_reset_distance - Reset NUMA distance table
  *
@@ -56,7 +42,6 @@ static int __init numa_alloc_distance(void)
 
 	/* size the new table and allocate it */
 	nodes_parsed = numa_nodes_parsed;
-	numa_nodemask_from_meminfo(&nodes_parsed, &numa_meminfo);
 
 	for_each_node_mask(i, nodes_parsed)
 		cnt = i;
@@ -415,7 +400,6 @@ static int __init numa_register_meminfo(struct numa_meminfo *mi)
 
 	/* Account for nodes with cpus and no memory */
 	node_possible_map = numa_nodes_parsed;
-	numa_nodemask_from_meminfo(&node_possible_map, mi);
 	if (WARN_ON(nodes_empty(node_possible_map)))
 		return -EINVAL;
 
Signature · 1 line
2.43.0
#12 lore raw

PATCH v2 8/9 arch_numa: remove redundant node_possible_map assignment

In reply to Sang-Heon Jeon

numa_register_meminfo() sets node_possible_map to numa_nodes_parsed. The later assignment in numa_register_nodes() is therefore redundant, so remove it.

No functional change.

---
 drivers/base/arch_numa.c | 3 ---
 1 file changed, 3 deletions(-)
drivers/base/arch_numa.c +0 -3
index c7f63c4cf367..6476227b772c 100644
@@ -221,9 +221,6 @@ static int __init numa_register_nodes(void)
 		node_set_online(nid);
 	}
 
-	/* Setup online nodes to actual nodes*/
-	node_possible_map = numa_nodes_parsed;
-
 	return 0;
 }
 
Signature · 1 line
2.43.0
#13 lore raw

PATCH v2 9/9 mm: numa_memblks: use numa_add_reserved_memblk() in numa_cleanup_meminfo()

In reply to Sang-Heon Jeon

numa_cleanup_meminfo() calls the internal numa_add_memblk_to() to add a block to numa_reserved_meminfo, even though numa_add_reserved_memblk() wraps exactly that.

Use the wrapper instead, so numa_add_memblk_to() is reached only through its two wrappers.

No functional change.

---
 mm/numa_memblks.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
mm/numa_memblks.c +1 -2
index 48428060e93f..426d17ff6859 100644
@@ -255,8 +255,7 @@ int __init numa_cleanup_meminfo(struct numa_meminfo *mi)
 
 		/* preserve info for non-RAM areas above 'max_pfn': */
 		if (bi->end > high) {
-			numa_add_memblk_to(bi->nid, high, bi->end,
-					   &numa_reserved_meminfo);
+			numa_add_reserved_memblk(bi->nid, high, bi->end);
 			bi->end = high;
 		}
 
Signature · 1 line
2.43.0
maintainer Applied
#14 lore raw
In reply to Sang-Heon Jeon
Quoted 11 lines · Sang-Heon Jeon

Every existing numa_add_memblk() caller passes a valid node id and separately marks that node in numa_nodes_parsed with node_set(). In addition, numa_nodemask_from_meminfo() recomputes the same "nodes that own memory" set from numa_meminfo, which numa_nodes_parsed already contains.

This redundancy implicitly depends on the callers' node_set(). So, before removing the redundancy, make numa_add_memblk() set the node in numa_nodes_parsed explicitly. Then remove the per-caller node_set() and numa_nodemask_from_meminfo().

[...]

Applied to numa_memblks-redundant-work branch of memblock.git tree, thanks!

[1/9] mm: numa_memblks: set numa_nodes_parsed in numa_add_memblk()

      commit: abdbd8329281f40afd381346410d6d43604af82c
[2/9] ACPI: NUMA: remove redundant numa_nodes_parsed node_set()
      commit: 7cbdade40fb8f440c13ccd7a02d104bf32285187
[3/9] of/numa: remove redundant numa_nodes_parsed node_set()
      commit: 3b1e5d902dfa832e4b175cb1f5a000236d45ceb5
[4/9] x86/numa: remove redundant numa_nodes_parsed node_set()
      commit: 63fa742bae02f0d2ffe95ff540a51837815abc5c
[5/9] arch_numa: remove redundant numa_nodes_parsed node_set()
      commit: 8b9cecbdc78c5a6cfaaf3b00ce7ebe05cf5417e7
[6/9] LoongArch: remove redundant numa_nodes_parsed node_set()
      commit: 3aeac07c5b1c3399487f4b38182f8cfbc3dbbd53
[7/9] mm: numa_memblks: remove redundant numa_nodemask_from_meminfo()
      commit: a9bafc1832d2db97813069823821ed333b8ecda6
[8/9] arch_numa: remove redundant node_possible_map assignment
      commit: f5a77a50a14dffb659ee8f550c824f8280e37fce
[9/9] mm: numa_memblks: use numa_add_reserved_memblk() in numa_cleanup_meminfo()
      commit: e55424c84afd48aa2f0f761ae0c006128ef541cf

tree: https://git.kernel.org/pub/scm/linux/kernel/git/rppt/memblock
branch: numa_memblks-redundant-work

--
Sincerely yours,
Mike.