public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] topology: make for_each_node_with_cpus() O(N)
@ 2025-05-09 16:20 Yury Norov
  2025-05-09 17:04 ` Andrea Righi
  0 siblings, 1 reply; 3+ messages in thread
From: Yury Norov @ 2025-05-09 16:20 UTC (permalink / raw)
  To: linux-kernel, Rasmus Villemoes, Yicong Yang, Andrea Righi,
	Tejun Heo, Catalin Marinas
  Cc: Yury Norov [NVIDIA]

From: Yury Norov [NVIDIA] <yury.norov@gmail.com>

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).

Signed-off-by: Yury Norov [NVIDIA] <yury.norov@gmail.com>
---
 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 f0ac0633366b..1e2bdda1a0a5 100644
--- a/include/linux/nodemask.h
+++ b/include/linux/nodemask.h
@@ -541,6 +541,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 24e715f0f6d2..ffee6b4a071a 100644
--- a/include/linux/topology.h
+++ b/include/linux/topology.h
@@ -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>
@@ -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.43.0


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

* Re: [PATCH] topology: make for_each_node_with_cpus() O(N)
  2025-05-09 16:20 [PATCH] topology: make for_each_node_with_cpus() O(N) Yury Norov
@ 2025-05-09 17:04 ` Andrea Righi
  2025-05-13 15:38   ` Yury Norov
  0 siblings, 1 reply; 3+ messages in thread
From: Andrea Righi @ 2025-05-09 17:04 UTC (permalink / raw)
  To: Yury Norov
  Cc: linux-kernel, Rasmus Villemoes, Yicong Yang, Tejun Heo,
	Catalin Marinas

Hi Yury,

On Fri, May 09, 2025 at 12:20:08PM -0400, Yury Norov wrote:
> From: Yury Norov [NVIDIA] <yury.norov@gmail.com>
> 
> 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).

Makes sense to me.

Maybe we should mention that previously we were only considering online
nodes with CPUs assigned. Now, we can include also offline nodes with CPUs
assigned (assuming it's possible)?

Semantically speaking, since the name doesn't include "online", it seems
more logical to ignore the state of the node. And if checking the online
state is required, the user can just use node_online() within the loop.

Thanks,
-Andrea

> 
> Signed-off-by: Yury Norov [NVIDIA] <yury.norov@gmail.com>
> ---
>  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 f0ac0633366b..1e2bdda1a0a5 100644
> --- a/include/linux/nodemask.h
> +++ b/include/linux/nodemask.h
> @@ -541,6 +541,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 24e715f0f6d2..ffee6b4a071a 100644
> --- a/include/linux/topology.h
> +++ b/include/linux/topology.h
> @@ -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>
> @@ -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.43.0
> 

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

* Re: [PATCH] topology: make for_each_node_with_cpus() O(N)
  2025-05-09 17:04 ` Andrea Righi
@ 2025-05-13 15:38   ` Yury Norov
  0 siblings, 0 replies; 3+ messages in thread
From: Yury Norov @ 2025-05-13 15:38 UTC (permalink / raw)
  To: Andrea Righi
  Cc: linux-kernel, Rasmus Villemoes, Yicong Yang, Tejun Heo,
	Catalin Marinas

On Fri, May 09, 2025 at 07:04:15PM +0200, Andrea Righi wrote:
> Hi Yury,
> 
> On Fri, May 09, 2025 at 12:20:08PM -0400, Yury Norov wrote:
> > From: Yury Norov [NVIDIA] <yury.norov@gmail.com>
> > 
> > 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).
> 
> Makes sense to me.
> 
> Maybe we should mention that previously we were only considering online
> nodes with CPUs assigned. Now, we can include also offline nodes with CPUs
> assigned (assuming it's possible)?
> 
> Semantically speaking, since the name doesn't include "online", it seems
> more logical to ignore the state of the node. And if checking the online
> state is required, the user can just use node_online() within the loop.

OK. I'll take your comment and move the patch with bitmap-for-next, if
no objections.

Thanks,
Yury

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

end of thread, other threads:[~2025-05-13 15:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-05-09 16:20 [PATCH] topology: make for_each_node_with_cpus() O(N) Yury Norov
2025-05-09 17:04 ` Andrea Righi
2025-05-13 15:38   ` Yury Norov

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