Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 mm and sched_ext/for-7.5 0/2] Skip nodes without CPUs in the sched_ext idle walk
@ 2026-12-01  9:18 Cheng-Yang Chou
  2026-12-01  9:18 ` [PATCH v2 mm and sched_ext/for-7.5 1/2] mm/fake-numa: map each CPU only to its own node Cheng-Yang Chou
  2026-12-01  9:18 ` [PATCH v2 mm and sched_ext/for-7.5 2/2] sched_ext/idle: Skip nodes without CPUs Cheng-Yang Chou
  0 siblings, 2 replies; 4+ messages in thread
From: Cheng-Yang Chou @ 2026-12-01  9:18 UTC (permalink / raw)
  To: sched-ext, Tejun Heo, David Vernet, Andrea Righi, Changwoo Min,
	linux-mm, Andrew Morton, Mike Rapoport
  Cc: Ching-Chun Huang, Chia-Ping Tsai, Dave Hansen, Andy Lutomirski,
	Peter Zijlstra, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	H . Peter Anvin, x86, Arnd Bergmann, linux-arch, chengyang.chou,
	Cheng-Yang Chou

With numa=fake, NUMA emulation sets a CPU in cpumask_of_node() of every
emulated node on its physical node, while cpu_to_node() returns only
one of them. On a Raspberry Pi 5, whose firmware adds numa=fake=8, all
eight nodes list CPUs 0-3 and have N_CPU set, but cpu_to_node() is 0
for every CPU. sched_ext's per-node idle walk then visits seven nodes
that can't have an idle CPU.

As Tejun suggested [1], patch 1 makes NUMA emulation set a CPU only in
the cpumask of its own node, and patch 2 makes sched_ext walk
node_states[N_CPU] instead of N_ONLINE.

On the rpi5 under hackbench, the per-node ops.select_cpu() time goes
from 484ns to 141ns, against 139ns with the global idle cpumask.

This series is based on sched_ext/for-7.5. Patch 1 also applies cleanly
to memblock/for-next.

[1] https://lore.kernel.org/all/e89bc79ed200cc40699ad62bb3d762b7@kernel.org/

Changes in v2:
- Fix NUMA emulation and walk N_CPU instead of keeping a separate node
  mask in sched_ext. (Tejun)
- Link to v1:
  https://lore.kernel.org/all/20261129163536.15478-1-yphbchou0911@gmail.com/

Cheng-Yang Chou (2):
  mm/fake-numa: map each CPU only to its own node
  sched_ext/idle: Skip nodes without CPUs

 arch/x86/mm/numa.c         |  4 ---
 include/asm-generic/numa.h |  4 ---
 kernel/sched/ext/idle.c    |  4 +--
 mm/arch_numa.c             | 26 ---------------
 mm/numa_emulation.c        | 67 ++------------------------------------
 5 files changed, 5 insertions(+), 100 deletions(-)


base-commit: 95a4df9944cd5d57a92662269114ba99cb972394
-- 
2.56.0



^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 mm and sched_ext/for-7.5 1/2] mm/fake-numa: map each CPU only to its own node
  2026-12-01  9:18 [PATCH v2 mm and sched_ext/for-7.5 0/2] Skip nodes without CPUs in the sched_ext idle walk Cheng-Yang Chou
@ 2026-12-01  9:18 ` Cheng-Yang Chou
  2026-12-01  9:18 ` [PATCH v2 mm and sched_ext/for-7.5 2/2] sched_ext/idle: Skip nodes without CPUs Cheng-Yang Chou
  1 sibling, 0 replies; 4+ messages in thread
From: Cheng-Yang Chou @ 2026-12-01  9:18 UTC (permalink / raw)
  To: sched-ext, Tejun Heo, David Vernet, Andrea Righi, Changwoo Min,
	linux-mm, Andrew Morton, Mike Rapoport
  Cc: Ching-Chun Huang, Chia-Ping Tsai, Dave Hansen, Andy Lutomirski,
	Peter Zijlstra, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	H . Peter Anvin, x86, Arnd Bergmann, linux-arch, chengyang.chou,
	Cheng-Yang Chou

With numa=fake, numa_add_cpu() sets a CPU in cpumask_of_node() of every
emulated node on its physical node, while cpu_to_node() returns only
one of them. On a Raspberry Pi 5, whose firmware adds numa=fake=8, all
eight nodes list CPUs 0-3 and have N_CPU set, but cpu_to_node() is 0
for every CPU.

No real topology puts a CPU in more than one node, and code that walks
N_CPU or cpumask_of_node() expects them to agree with cpu_to_node().
For example, sched_ext tracks idle CPUs by cpu_to_node(), so the seven
other nodes can never have an idle CPU even though N_CPU says they
have CPUs.

Set a CPU only in the cpumask of its own node, the same as without
emulation. The other emulated nodes on the same physical node become
memory-only nodes. Also drop the code this leaves unused.

On that rpi5, node 0 now lists CPUs 0-3, nodes 1-7 list none and has_cpu
is 0, while the interleave policy the rpi5 kernel sets still spreads
memory over all eight nodes.

Suggested-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Cheng-Yang Chou <yphbchou0911@gmail.com>
---
 arch/x86/mm/numa.c         |  4 ---
 include/asm-generic/numa.h |  4 ---
 mm/arch_numa.c             | 26 ---------------
 mm/numa_emulation.c        | 67 ++------------------------------------
 4 files changed, 3 insertions(+), 98 deletions(-)

diff --git a/arch/x86/mm/numa.c b/arch/x86/mm/numa.c
index ced66e68a68e..55d0fe9cd0b2 100644
--- a/arch/x86/mm/numa.c
+++ b/arch/x86/mm/numa.c
@@ -323,7 +323,6 @@ void __init init_cpu_to_node(void)
 
 #ifndef CONFIG_DEBUG_PER_CPU_MAPS
 
-# ifndef CONFIG_NUMA_EMU
 void numa_add_cpu(unsigned int cpu)
 {
 	cpumask_set_cpu(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
@@ -333,7 +332,6 @@ void numa_remove_cpu(unsigned int cpu)
 {
 	cpumask_clear_cpu(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
 }
-# endif	/* !CONFIG_NUMA_EMU */
 
 #else	/* !CONFIG_DEBUG_PER_CPU_MAPS */
 
@@ -393,7 +391,6 @@ void debug_cpumask_set_cpu(unsigned int cpu, int node, bool enable)
 	return;
 }
 
-# ifndef CONFIG_NUMA_EMU
 static void numa_set_cpumask(int cpu, bool enable)
 {
 	debug_cpumask_set_cpu(cpu, early_cpu_to_node(cpu), enable);
@@ -408,7 +405,6 @@ void numa_remove_cpu(unsigned int cpu)
 {
 	numa_set_cpumask(cpu, false);
 }
-# endif	/* !CONFIG_NUMA_EMU */
 
 /*
  * Returns a pointer to the bitmask of CPUs on Node 'node'.
diff --git a/include/asm-generic/numa.h b/include/asm-generic/numa.h
index e063d6487f66..f7397c158860 100644
--- a/include/asm-generic/numa.h
+++ b/include/asm-generic/numa.h
@@ -49,8 +49,4 @@ static inline int early_cpu_to_node(int cpu) { return 0; }
 
 #endif	/* CONFIG_NUMA */
 
-#ifdef CONFIG_NUMA_EMU
-void debug_cpumask_set_cpu(unsigned int cpu, int node, bool enable);
-#endif
-
 #endif	/* __ASM_GENERIC_NUMA_H */
diff --git a/mm/arch_numa.c b/mm/arch_numa.c
index a4b1732274fa..c69d301129d7 100644
--- a/mm/arch_numa.c
+++ b/mm/arch_numa.c
@@ -59,7 +59,6 @@ EXPORT_SYMBOL(cpumask_of_node);
 
 #endif
 
-#ifndef CONFIG_NUMA_EMU
 static void numa_update_cpu(unsigned int cpu, bool remove)
 {
 	int nid = cpu_to_node(cpu);
@@ -82,7 +81,6 @@ void numa_remove_cpu(unsigned int cpu)
 {
 	numa_update_cpu(cpu, true);
 }
-#endif
 
 void numa_clear_node(unsigned int cpu)
 {
@@ -356,28 +354,4 @@ u64 __init numa_emu_dma_end(void)
 {
 	return memblock_start_of_DRAM() + SZ_4G;
 }
-
-void debug_cpumask_set_cpu(unsigned int cpu, int node, bool enable)
-{
-	struct cpumask *mask;
-
-	if (node == NUMA_NO_NODE)
-		return;
-
-	mask = node_to_cpumask_map[node];
-	if (!cpumask_available(mask)) {
-		pr_err("node_to_cpumask_map[%i] NULL\n", node);
-		dump_stack();
-		return;
-	}
-
-	if (enable)
-		cpumask_set_cpu(cpu, mask);
-	else
-		cpumask_clear_cpu(cpu, mask);
-
-	pr_debug("%s cpu %d node %d: mask now %*pbl\n",
-		 enable ? "numa_add_cpu" : "numa_remove_cpu",
-		 cpu, node, cpumask_pr_args(mask));
-}
 #endif /* CONFIG_NUMA_EMU */
diff --git a/mm/numa_emulation.c b/mm/numa_emulation.c
index 55f26b22bb0b..63f3ce5ef839 100644
--- a/mm/numa_emulation.c
+++ b/mm/numa_emulation.c
@@ -366,10 +366,10 @@ static int __init setup_emu2phys_nid(int *dfl_phys_nid)
  *   are mapped to physical nodes and match the actual distances.
  *
  * - emu_nid_to_phys[] reflects how emulated nodes are mapped to physical
- *   nodes.  This is used by numa_add_cpu() and numa_remove_cpu().
+ *   nodes.
  *
- * If emulation is not enabled or fails, emu_nid_to_phys[] is filled with
- * identity mapping and no other modification is made.
+ * If emulation is not enabled or fails, the NUMA configuration is left
+ * unchanged.
  */
 void __init numa_emulation(struct numa_meminfo *numa_meminfo, int numa_dist_cnt)
 {
@@ -547,65 +547,4 @@ void __init numa_emulation(struct numa_meminfo *numa_meminfo, int numa_dist_cnt)
 
 no_emu:
 	numa_nodes_parsed = physnode_mask;
-	/* No emulation.  Build identity emu_nid_to_phys[] for numa_add_cpu() */
-	for (i = 0; i < ARRAY_SIZE(emu_nid_to_phys); i++)
-		emu_nid_to_phys[i] = i;
 }
-
-#ifndef CONFIG_DEBUG_PER_CPU_MAPS
-void numa_add_cpu(unsigned int cpu)
-{
-	int physnid, nid;
-
-	nid = early_cpu_to_node(cpu);
-	BUG_ON(nid == NUMA_NO_NODE || !node_online(nid));
-
-	physnid = emu_nid_to_phys[nid];
-
-	/*
-	 * Map the cpu to each emulated node that is allocated on the physical
-	 * node of the cpu's apic id.
-	 */
-	for_each_online_node(nid)
-		if (emu_nid_to_phys[nid] == physnid)
-			cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
-}
-
-void numa_remove_cpu(unsigned int cpu)
-{
-	int i;
-
-	for_each_online_node(i)
-		cpumask_clear_cpu(cpu, node_to_cpumask_map[i]);
-}
-#else	/* !CONFIG_DEBUG_PER_CPU_MAPS */
-static void numa_set_cpumask(unsigned int cpu, bool enable)
-{
-	int nid, physnid;
-
-	nid = early_cpu_to_node(cpu);
-	if (nid == NUMA_NO_NODE) {
-		/* early_cpu_to_node() already emits a warning and trace */
-		return;
-	}
-
-	physnid = emu_nid_to_phys[nid];
-
-	for_each_online_node(nid) {
-		if (emu_nid_to_phys[nid] != physnid)
-			continue;
-
-		debug_cpumask_set_cpu(cpu, nid, enable);
-	}
-}
-
-void numa_add_cpu(unsigned int cpu)
-{
-	numa_set_cpumask(cpu, true);
-}
-
-void numa_remove_cpu(unsigned int cpu)
-{
-	numa_set_cpumask(cpu, false);
-}
-#endif	/* !CONFIG_DEBUG_PER_CPU_MAPS */

base-commit: 95a4df9944cd5d57a92662269114ba99cb972394
-- 
2.56.0



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v2 mm and sched_ext/for-7.5 2/2] sched_ext/idle: Skip nodes without CPUs
  2026-12-01  9:18 [PATCH v2 mm and sched_ext/for-7.5 0/2] Skip nodes without CPUs in the sched_ext idle walk Cheng-Yang Chou
  2026-12-01  9:18 ` [PATCH v2 mm and sched_ext/for-7.5 1/2] mm/fake-numa: map each CPU only to its own node Cheng-Yang Chou
@ 2026-12-01  9:18 ` Cheng-Yang Chou
  2026-12-01 15:45   ` Tejun Heo
  1 sibling, 1 reply; 4+ messages in thread
From: Cheng-Yang Chou @ 2026-12-01  9:18 UTC (permalink / raw)
  To: sched-ext, Tejun Heo, David Vernet, Andrea Righi, Changwoo Min,
	linux-mm, Andrew Morton, Mike Rapoport
  Cc: Ching-Chun Huang, Chia-Ping Tsai, Dave Hansen, Andy Lutomirski,
	Peter Zijlstra, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	H . Peter Anvin, x86, Arnd Bergmann, linux-arch, chengyang.chou,
	Cheng-Yang Chou

With SCX_OPS_BUILTIN_IDLE_PER_NODE, a CPU is tracked in the idle
cpumask of cpu_to_node(cpu). When the starting node has no idle CPU,
pick_idle_cpu_from_online_nodes() walks the other online nodes in
distance order. Nodes without CPUs, such as memory-only nodes, can't
have an idle one, but the walk still visits them.

Walk only the nodes that have CPUs.

On a Raspberry Pi 5, whose firmware adds numa=fake=8, the previous
patch leaves CPUs only in node 0. Five hackbench -p -g 10 -l 10000
runs under scx_simple, without and with both patches, gave these
means (ops.select_cpu() time per call, run time):

                  global idle cpumask   per-node idle cpumasks
  before          135ns, 7.39s          484ns, 7.49s
  after           139ns, 7.36s          141ns, 7.37s

Suggested-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Cheng-Yang Chou <yphbchou0911@gmail.com>
---
 kernel/sched/ext/idle.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/kernel/sched/ext/idle.c b/kernel/sched/ext/idle.c
index 922fbc3065cc..1e7b26869e8d 100644
--- a/kernel/sched/ext/idle.c
+++ b/kernel/sched/ext/idle.c
@@ -168,10 +168,10 @@ static s32 pick_idle_cpu_from_online_nodes(const struct cpumask *cpus_allowed, i
 	unvisited = this_cpu_ptr(&per_cpu_unvisited);
 
 	/*
-	 * Restrict the search to the online nodes (excluding the current
+	 * Restrict the search to the nodes with CPUs (excluding the current
 	 * node that has been visited already).
 	 */
-	nodes_copy(*unvisited, node_states[N_ONLINE]);
+	nodes_copy(*unvisited, node_states[N_CPU]);
 	node_clear(node, *unvisited);
 
 	/*
-- 
2.56.0



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2 mm and sched_ext/for-7.5 2/2] sched_ext/idle: Skip nodes without CPUs
  2026-12-01  9:18 ` [PATCH v2 mm and sched_ext/for-7.5 2/2] sched_ext/idle: Skip nodes without CPUs Cheng-Yang Chou
@ 2026-12-01 15:45   ` Tejun Heo
  0 siblings, 0 replies; 4+ messages in thread
From: Tejun Heo @ 2026-12-01 15:45 UTC (permalink / raw)
  To: Cheng-Yang Chou
  Cc: sched-ext, David Vernet, Andrea Righi, Changwoo Min, linux-mm,
	Andrew Morton, Mike Rapoport, Ching-Chun Huang, Chia-Ping Tsai,
	Dave Hansen, Andy Lutomirski, Peter Zijlstra, Thomas Gleixner,
	Ingo Molnar, Borislav Petkov, H . Peter Anvin, x86, Arnd Bergmann,
	linux-arch, chengyang.chou

Applied 2 to sched_ext/for-7.5 with the benchmark paragraph dropped from
the description as the numbers depend on patch 1.

Thanks.

--
tejun


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-01 15:45 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01  9:18 [PATCH v2 mm and sched_ext/for-7.5 0/2] Skip nodes without CPUs in the sched_ext idle walk Cheng-Yang Chou
2026-12-01  9:18 ` [PATCH v2 mm and sched_ext/for-7.5 1/2] mm/fake-numa: map each CPU only to its own node Cheng-Yang Chou
2026-12-01  9:18 ` [PATCH v2 mm and sched_ext/for-7.5 2/2] sched_ext/idle: Skip nodes without CPUs Cheng-Yang Chou
2026-12-01 15:45   ` Tejun Heo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox