]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
topology: make for_each_node_with_cpus() O(N)
authorYury Norov [NVIDIA] <yury.norov@gmail.com>
Fri, 9 May 2025 16:20:08 +0000 (12:20 -0400)
committerYury Norov <yury.norov@gmail.com>
Tue, 13 May 2025 15:40:04 +0000 (11:40 -0400)
for_each_node_with_cpus() calls nr_cpus_node() at every iteration, which
makes it O(N^2). Kernel tracks such nodes with N_CPU record in node_states
array. Switching to it makes for_each_node_with_cpus() O(N).

Andrea:

Now we can include also offline nodes with CPUs assigned (assuming it's
possible). If checking the online state is required, the user must use
node_online() within the loop.

CC: Andrea Righi <arighi@nvidia.com>
CC:Tejun Heo <tj@kernel.org>
Signed-off-by: Yury Norov [NVIDIA] <yury.norov@gmail.com>
include/linux/nodemask.h
include/linux/topology.h

index 0aa6b63aa747dc58655311b5a53a41ad6a26c0d3..f08ae71585faab7014eed78111ee49caaa7e74ea 100644 (file)
@@ -522,6 +522,7 @@ static __always_inline int node_random(const nodemask_t *maskp)
 
 #define for_each_node(node)       for_each_node_state(node, N_POSSIBLE)
 #define for_each_online_node(node) for_each_node_state(node, N_ONLINE)
+#define for_each_node_with_cpus(node)  for_each_node_state(node, N_CPU)
 
 /*
  * For nodemask scratch area.
index 24e715f0f6d23a98cc173ed80607b6ce129fa038..ffee6b4a071ae55c98f392a9f8167db812cbc1ca 100644 (file)
@@ -29,6 +29,7 @@
 
 #include <linux/arch_topology.h>
 #include <linux/cpumask.h>
+#include <linux/nodemask.h>
 #include <linux/bitops.h>
 #include <linux/mmzone.h>
 #include <linux/smp.h>
 #define nr_cpus_node(node) cpumask_weight(cpumask_of_node(node))
 #endif
 
-#define for_each_node_with_cpus(node)                  \
-       for_each_online_node(node)                      \
-               if (nr_cpus_node(node))
-
 int arch_update_cpu_topology(void);
 
 /* Conform to ACPI 2.0 SLIT distance definitions */