From 895ee6a22e3195b7c1fee140c842bdeedb89ed33 Mon Sep 17 00:00:00 2001 From: "Yury Norov [NVIDIA]" Date: Fri, 9 May 2025 12:20:08 -0400 Subject: [PATCH] topology: make for_each_node_with_cpus() O(N) 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 CC:Tejun Heo Signed-off-by: Yury Norov [NVIDIA] --- include/linux/nodemask.h | 1 + include/linux/topology.h | 5 +---- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/include/linux/nodemask.h b/include/linux/nodemask.h index 0aa6b63aa747d..f08ae71585faa 100644 --- a/include/linux/nodemask.h +++ b/include/linux/nodemask.h @@ -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. diff --git a/include/linux/topology.h b/include/linux/topology.h index 24e715f0f6d23..ffee6b4a071ae 100644 --- a/include/linux/topology.h +++ b/include/linux/topology.h @@ -29,6 +29,7 @@ #include #include +#include #include #include #include @@ -39,10 +40,6 @@ #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 */ -- 2.47.2