The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] nodemask: use nr_node_ids instead of MAX_NUMNODES in __nodemask_pr_numnodes()
@ 2026-07-09  7:17 lirongqing
  2026-07-09 23:49 ` Andrew Morton
  0 siblings, 1 reply; 3+ messages in thread
From: lirongqing @ 2026-07-09  7:17 UTC (permalink / raw)
  To: Yury Norov, Rasmus Villemoes, linux-kernel
  Cc: Li RongQing, Andrew Morton, linux-mm

From: Li RongQing <lirongqing@baidu.com>

__nodemask_pr_numnodes() returns MAX_NUMNODES, a compile-time constant,
as the width for '%*pb[l]' nodemask printing. On systems with few NUMA
nodes this produces excessive zero-padding, e.g. with MAX_NUMNODES=1024
but only 2 nodes:

  Mems_allowed: 00000000,00000000,...,00000003  (32 groups)

Use nr_node_ids (the runtime highest-node-id + 1) instead, matching the
behavior of cpumask_pr_args() which uses nr_cpu_ids:

  Mems_allowed: 00000003

Move the nr_node_ids declaration earlier in the file (guarded by
__nodemask_pr_numnodes(), and remove the now-duplicate declarations.

Signed-off-by: Li RongQing <lirongqing@baidu.com>
Cc: Yury Norov <yury.norov@gmail.com>
Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: linux-mm@kvack.org
---
 include/linux/nodemask.h | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/include/linux/nodemask.h b/include/linux/nodemask.h
index b842aa5..35269ea 100644
--- a/include/linux/nodemask.h
+++ b/include/linux/nodemask.h
@@ -95,6 +95,12 @@
 
 extern nodemask_t _unused_nodemask_arg_;
 
+#if MAX_NUMNODES > 1
+extern unsigned int nr_node_ids;
+#else
+#define nr_node_ids		1U
+#endif
+
 /**
  * nodemask_pr_args - printf args to output a nodemask
  * @maskp: nodemask to be printed
@@ -105,7 +111,7 @@ extern nodemask_t _unused_nodemask_arg_;
 				__nodemask_pr_bits(maskp)
 static __always_inline unsigned int __nodemask_pr_numnodes(const nodemask_t *m)
 {
-	return m ? MAX_NUMNODES : 0;
+	return m ? nr_node_ids : 0;
 }
 static __always_inline const unsigned long *__nodemask_pr_bits(const nodemask_t *m)
 {
@@ -438,7 +444,6 @@ static __always_inline unsigned int next_memory_node(int nid)
 	return next_node(nid, node_states[N_MEMORY]);
 }
 
-extern unsigned int nr_node_ids;
 extern unsigned int nr_online_nodes;
 
 static __always_inline void node_set_online(int nid)
@@ -480,7 +485,6 @@ static __always_inline int num_node_state(enum node_states state)
 #define first_memory_node	0
 #define next_online_node(nid)	(MAX_NUMNODES)
 #define next_memory_node(nid)	(MAX_NUMNODES)
-#define nr_node_ids		1U
 #define nr_online_nodes		1U
 
 #define node_set_online(node)	   node_set_state((node), N_ONLINE)
-- 
2.9.4


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

* Re: [PATCH] nodemask: use nr_node_ids instead of MAX_NUMNODES in __nodemask_pr_numnodes()
  2026-07-09  7:17 [PATCH] nodemask: use nr_node_ids instead of MAX_NUMNODES in __nodemask_pr_numnodes() lirongqing
@ 2026-07-09 23:49 ` Andrew Morton
  2026-07-10  0:44   ` 答复: [????] " Li,Rongqing
  0 siblings, 1 reply; 3+ messages in thread
From: Andrew Morton @ 2026-07-09 23:49 UTC (permalink / raw)
  To: lirongqing; +Cc: Yury Norov, Rasmus Villemoes, linux-kernel, linux-mm

On Thu, 9 Jul 2026 15:17:39 +0800 lirongqing <lirongqing@baidu.com> wrote:

> From: Li RongQing <lirongqing@baidu.com>
> 
> __nodemask_pr_numnodes() returns MAX_NUMNODES, a compile-time constant,
> as the width for '%*pb[l]' nodemask printing. On systems with few NUMA
> nodes this produces excessive zero-padding, e.g. with MAX_NUMNODES=1024
> but only 2 nodes:
> 
>   Mems_allowed: 00000000,00000000,...,00000003  (32 groups)

Please let's completely describe the effects of this. 
nodemask_pr_args() is used in several places so I believe that a number
of userspace-facing strings will now appear differently?  Let's spell
out which userspace interfaces are affected, and how.

> Use nr_node_ids (the runtime highest-node-id + 1) instead, matching the
> behavior of cpumask_pr_args() which uses nr_cpu_ids:
> 
>   Mems_allowed: 00000003
> 
> Move the nr_node_ids declaration earlier in the file (guarded by
> __nodemask_pr_numnodes(), and remove the now-duplicate declarations.
> 
> ...
>
> --- a/include/linux/nodemask.h
> +++ b/include/linux/nodemask.h
> @@ -95,6 +95,12 @@
>  
>  extern nodemask_t _unused_nodemask_arg_;
>  
> +#if MAX_NUMNODES > 1
> +extern unsigned int nr_node_ids;
> +#else
> +#define nr_node_ids		1U
> +#endif
> +
>  /**
>   * nodemask_pr_args - printf args to output a nodemask
>   * @maskp: nodemask to be printed
> @@ -105,7 +111,7 @@ extern nodemask_t _unused_nodemask_arg_;
>  				__nodemask_pr_bits(maskp)
>  static __always_inline unsigned int __nodemask_pr_numnodes(const nodemask_t *m)
>  {
> -	return m ? MAX_NUMNODES : 0;
> +	return m ? nr_node_ids : 0;
>  }
>  static __always_inline const unsigned long *__nodemask_pr_bits(const nodemask_t *m)
>  {
> @@ -438,7 +444,6 @@ static __always_inline unsigned int next_memory_node(int nid)
>  	return next_node(nid, node_states[N_MEMORY]);
>  }
>  
> -extern unsigned int nr_node_ids;
>  extern unsigned int nr_online_nodes;

Let's relocate nr_online_nodes also?  To keep them together.

>  static __always_inline void node_set_online(int nid)
> @@ -480,7 +485,6 @@ static __always_inline int num_node_state(enum node_states state)
>  #define first_memory_node	0
>  #define next_online_node(nid)	(MAX_NUMNODES)
>  #define next_memory_node(nid)	(MAX_NUMNODES)
> -#define nr_node_ids		1U
>  #define nr_online_nodes		1U
>  
>  #define node_set_online(node)	   node_set_state((node), N_ONLINE)
> -- 
> 2.9.4

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

* 答复: [????] Re: [PATCH] nodemask: use nr_node_ids instead of MAX_NUMNODES in __nodemask_pr_numnodes()
  2026-07-09 23:49 ` Andrew Morton
@ 2026-07-10  0:44   ` Li,Rongqing
  0 siblings, 0 replies; 3+ messages in thread
From: Li,Rongqing @ 2026-07-10  0:44 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Yury Norov, Rasmus Villemoes, linux-kernel@vger.kernel.org,
	linux-mm@kvack.org

> On Thu, 9 Jul 2026 15:17:39 +0800 lirongqing <lirongqing@baidu.com>
> wrote:
> 
> > From: Li RongQing <lirongqing@baidu.com>
> >
> > __nodemask_pr_numnodes() returns MAX_NUMNODES, a compile-time
> > constant, as the width for '%*pb[l]' nodemask printing. On systems
> > with few NUMA nodes this produces excessive zero-padding, e.g. with
> > MAX_NUMNODES=1024 but only 2 nodes:
> >
> >   Mems_allowed: 00000000,00000000,...,00000003  (32 groups)
> 
> Please let's completely describe the effects of this.
> nodemask_pr_args() is used in several places so I believe that a number of
> userspace-facing strings will now appear differently?  Let's spell out which
> userspace interfaces are affected, and how.
> 

This affects every nodemask_pr_args() user.  Userspace-visible output
includes node state sysfs files under /sys/devices/system/node/, NVMe
multipath numa_nodes, memory tier nodelist, cpuset mems/effective_mems
files, /proc/<pid>/status Mems_allowed*, mempolicy strings such as those
shown in /proc/<pid>/numa_maps, and slub debugfs "nodes=" output.  Kernel
log messages that print nodemasks, such as cpuset warnings, allocation
failure warnings and OOM diagnostics, are affected as well.

The semantic value is unchanged.  Only the printed width changes from
MAX_NUMNODES bits to nr_node_ids bits, removing trailing zero words on
systems built with a large MAX_NUMNODES but booted with a smaller
possible-node range.  List outputs (%*pbl) remain unchanged for common
contiguous node ranges such as "0-1"; bitmap outputs (%*pb), such as
/proc/<pid>/status Mems_allowed, become shorter.


> > Use nr_node_ids (the runtime highest-node-id + 1) instead, matching
> > the behavior of cpumask_pr_args() which uses nr_cpu_ids:
> >
> >   Mems_allowed: 00000003
> >
> > Move the nr_node_ids declaration earlier in the file (guarded by
> > __nodemask_pr_numnodes(), and remove the now-duplicate declarations.
> >
> > ...
> >
> > --- a/include/linux/nodemask.h
> > +++ b/include/linux/nodemask.h
> > @@ -95,6 +95,12 @@
> >
> >  extern nodemask_t _unused_nodemask_arg_;
> >
> > +#if MAX_NUMNODES > 1
> > +extern unsigned int nr_node_ids;
> > +#else
> > +#define nr_node_ids		1U
> > +#endif
> > +
> >  /**
> >   * nodemask_pr_args - printf args to output a nodemask
> >   * @maskp: nodemask to be printed
> > @@ -105,7 +111,7 @@ extern nodemask_t _unused_nodemask_arg_;
> >  				__nodemask_pr_bits(maskp)
> >  static __always_inline unsigned int __nodemask_pr_numnodes(const
> > nodemask_t *m)  {
> > -	return m ? MAX_NUMNODES : 0;
> > +	return m ? nr_node_ids : 0;
> >  }
> >  static __always_inline const unsigned long *__nodemask_pr_bits(const
> > nodemask_t *m)  { @@ -438,7 +444,6 @@ static __always_inline
> unsigned
> > int next_memory_node(int nid)
> >  	return next_node(nid, node_states[N_MEMORY]);  }
> >
> > -extern unsigned int nr_node_ids;
> >  extern unsigned int nr_online_nodes;
> 
> Let's relocate nr_online_nodes also?  To keep them together.


I will relocate it in V2

Thanks

[Li,Rongqing] 



> 
> >  static __always_inline void node_set_online(int nid) @@ -480,7 +485,6
> > @@ static __always_inline int num_node_state(enum node_states state)
> >  #define first_memory_node	0
> >  #define next_online_node(nid)	(MAX_NUMNODES)
> >  #define next_memory_node(nid)	(MAX_NUMNODES)
> > -#define nr_node_ids		1U
> >  #define nr_online_nodes		1U
> >
> >  #define node_set_online(node)	   node_set_state((node), N_ONLINE)
> > --
> > 2.9.4


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

end of thread, other threads:[~2026-07-10  0:44 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-09  7:17 [PATCH] nodemask: use nr_node_ids instead of MAX_NUMNODES in __nodemask_pr_numnodes() lirongqing
2026-07-09 23:49 ` Andrew Morton
2026-07-10  0:44   ` 答复: [????] " Li,Rongqing

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