Skip to content

Commit 895ee6a

Browse files
committed
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 <arighi@nvidia.com> CC:Tejun Heo <tj@kernel.org> Signed-off-by: Yury Norov [NVIDIA] <yury.norov@gmail.com>
1 parent a256ae2 commit 895ee6a

File tree

2 files changed

+2
-4
lines changed

2 files changed

+2
-4
lines changed

include/linux/nodemask.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ static __always_inline int node_random(const nodemask_t *maskp)
522522

523523
#define for_each_node(node) for_each_node_state(node, N_POSSIBLE)
524524
#define for_each_online_node(node) for_each_node_state(node, N_ONLINE)
525+
#define for_each_node_with_cpus(node) for_each_node_state(node, N_CPU)
525526

526527
/*
527528
* For nodemask scratch area.

include/linux/topology.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include <linux/arch_topology.h>
3131
#include <linux/cpumask.h>
32+
#include <linux/nodemask.h>
3233
#include <linux/bitops.h>
3334
#include <linux/mmzone.h>
3435
#include <linux/smp.h>
@@ -39,10 +40,6 @@
3940
#define nr_cpus_node(node) cpumask_weight(cpumask_of_node(node))
4041
#endif
4142

42-
#define for_each_node_with_cpus(node) \
43-
for_each_online_node(node) \
44-
if (nr_cpus_node(node))
45-
4643
int arch_update_cpu_topology(void);
4744

4845
/* Conform to ACPI 2.0 SLIT distance definitions */

0 commit comments

Comments
 (0)