Linux MIPS Architecture development
 help / color / mirror / Atom feed
* [PATCH 00/11] nodemask: switch to runtime nr_node_ids
@ 2026-11-27 23:01 Yury Norov
  2026-11-27 23:01 ` [PATCH 01/11] nodemask: add 'small' and 'large' compile-time sizes Yury Norov
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: Yury Norov @ 2026-11-27 23:01 UTC (permalink / raw)
  To: linux-kernel, Andrew Morton, linux-mm, Rasmus Villemoes,
	David Hildenbrand, Gregory Price, Zi Yan, Joshua Hahn, Rakie Kim,
	Ying Huang, Muchun Song, Oscar Salvador, Johannes Weiner,
	Kairui Song, Thomas Bogendoerfer, Rafael J. Wysocki, Len Brown,
	Jens Axboe, Peter Zijlstra, Mel Gorman, Steven Rostedt,
	Masami Hiramatsu, Mathieu Desnoyers, linux-mips, linux-acpi,
	linux-block, linux-trace-kernel
  Cc: Yury Norov, Liam R. Howlett, Axel Rasmussen, Baolin Wang,
	Baoquan He, Barry Song, Linus Torvalds, Lorenzo Stoakes,
	Michal Hocko, Mike Rapoport, Qi Zheng, Shakeel Butt,
	Suren Baghdasaryan, Vlastimil Babka, Wei Xu, Yuanchu Xie,
	Yury Norov

Nodemasks have a compile-time capacity of MAX_NUMNODES, and
nr_node_ids describes the runtime node ID usage.

Modern distros, like my Ubuntu, default MAX_NUMNODES to 1024, while the
actual number of nodes is typically smaller. All nodemask functions
currently refer to the MAX_NUMNODES, which adds a substantial overhead
on every traversing operation.

This series switches the kernel to runtime length of nodemasks, which
also saves up to 8K of .text for minimal config, and accelerates basic
nodemask operations ~4x if MAX_NUMNODES == 1024 and nr_node_ids == 2.

Yury Norov (11):
  nodemask: add 'small' and 'large' compile-time sizes
  nodemask: pass nodemask length to node search functions
  mm/mempolicy: reject migrate_pages source IDs beyond nr_node_ids
  mm/mempolicy: preserve full-width userspace nodemasks
  mm: switch to runtime nodemask length
  MIPS: IP27: switch nmi_dump to runtime nodemask length
  ACPI: NUMA: switch acpi_map_pxm_to_node to runtime nodemask length
  mtip32xx: switch next_online_node() to runtime nodemask length
  nodemask: add nodes_copy_and_extend()
  sched/numa: bound cpuset trace masks by nr_node_ids
  nodemask: add cpumask-style operation size bounds

 arch/mips/sgi-ip27/ip27-nmi.c     |   2 +-
 drivers/acpi/numa/srat.c          |   2 +-
 drivers/block/mtip32xx/mtip32xx.c |   2 +-
 include/linux/nodemask.h          | 122 +++++++++++++++++++-----------
 include/trace/events/sched.h      |   7 +-
 mm/hugetlb_internal.h             |   2 +-
 mm/mempolicy.c                    |  55 +++++++++-----
 mm/mmzone.c                       |   2 +-
 mm/vmscan.c                       |   2 +-
 9 files changed, 123 insertions(+), 73 deletions(-)

-- 
2.53.0


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

* [PATCH 01/11] nodemask: add 'small' and 'large' compile-time sizes
  2026-11-27 23:01 [PATCH 00/11] nodemask: switch to runtime nr_node_ids Yury Norov
@ 2026-11-27 23:01 ` Yury Norov
  2026-11-27 23:01 ` [PATCH 02/11] nodemask: pass nodemask length to node search functions Yury Norov
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Yury Norov @ 2026-11-27 23:01 UTC (permalink / raw)
  To: linux-kernel, Andrew Morton, linux-mm, Rasmus Villemoes,
	David Hildenbrand, Gregory Price, Zi Yan, Joshua Hahn, Rakie Kim,
	Ying Huang, Muchun Song, Oscar Salvador, Johannes Weiner,
	Kairui Song, Thomas Bogendoerfer, Rafael J. Wysocki, Len Brown,
	Jens Axboe, Peter Zijlstra, Mel Gorman, Steven Rostedt,
	Masami Hiramatsu, Mathieu Desnoyers, linux-mips, linux-acpi,
	linux-block, linux-trace-kernel
  Cc: Yury Norov, Liam R. Howlett, Axel Rasmussen, Baolin Wang,
	Baoquan He, Barry Song, Linus Torvalds, Lorenzo Stoakes,
	Michal Hocko, Mike Rapoport, Qi Zheng, Shakeel Butt,
	Suren Baghdasaryan, Vlastimil Babka, Wei Xu, Yuanchu Xie,
	Yury Norov

Introduce 'small' and 'large' compile-time sizes for nodemask operations,
similarly to how it works for cpumasks. Small_nodemask_bits is used by
single nodemask operations and searches, and large_nodemask_bits is used
by clearing and copying. The nr_node_ids is used where the exact runtime
length is needed.

Signed-off-by: Yury Norov <ynorov@nvidia.com>
---
 include/linux/nodemask.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/include/linux/nodemask.h b/include/linux/nodemask.h
index 607373bd83ee..37fd5ac957c0 100644
--- a/include/linux/nodemask.h
+++ b/include/linux/nodemask.h
@@ -103,6 +103,22 @@ extern unsigned int nr_online_nodes;
 #define nr_online_nodes		1U
 #endif
 
+/*
+ * Compile-time bounds for optimized nodemask operations. See comments
+ * for small_cpumask_bits and large_cpumask_bits in linux/cpumask.h for
+ * detailed description.
+ */
+#if MAX_NUMNODES <= BITS_PER_LONG
+#define small_nodemask_bits ((unsigned int)MAX_NUMNODES)
+#define large_nodemask_bits ((unsigned int)MAX_NUMNODES)
+#elif MAX_NUMNODES <= 4 * BITS_PER_LONG
+#define small_nodemask_bits nr_node_ids
+#define large_nodemask_bits ((unsigned int)MAX_NUMNODES)
+#else
+#define small_nodemask_bits nr_node_ids
+#define large_nodemask_bits nr_node_ids
+#endif
+
 /**
  * nodemask_pr_args - printf args to output a nodemask
  * @maskp: nodemask to be printed
-- 
2.53.0


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

* [PATCH 02/11] nodemask: pass nodemask length to node search functions
  2026-11-27 23:01 [PATCH 00/11] nodemask: switch to runtime nr_node_ids Yury Norov
  2026-11-27 23:01 ` [PATCH 01/11] nodemask: add 'small' and 'large' compile-time sizes Yury Norov
@ 2026-11-27 23:01 ` Yury Norov
  2026-11-27 23:01 ` [PATCH 03/11] mm/mempolicy: reject migrate_pages source IDs beyond nr_node_ids Yury Norov
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Yury Norov @ 2026-11-27 23:01 UTC (permalink / raw)
  To: linux-kernel, Andrew Morton, linux-mm, Rasmus Villemoes,
	David Hildenbrand, Gregory Price, Zi Yan, Joshua Hahn, Rakie Kim,
	Ying Huang, Muchun Song, Oscar Salvador, Johannes Weiner,
	Kairui Song, Thomas Bogendoerfer, Rafael J. Wysocki, Len Brown,
	Jens Axboe, Peter Zijlstra, Mel Gorman, Steven Rostedt,
	Masami Hiramatsu, Mathieu Desnoyers, linux-mips, linux-acpi,
	linux-block, linux-trace-kernel
  Cc: Yury Norov, Liam R. Howlett, Axel Rasmussen, Baolin Wang,
	Baoquan He, Barry Song, Linus Torvalds, Lorenzo Stoakes,
	Michal Hocko, Mike Rapoport, Qi Zheng, Shakeel Butt,
	Suren Baghdasaryan, Vlastimil Babka, Wei Xu, Yuanchu Xie,
	Yury Norov

Contrary to other underscored nodemask functions, search API doesn't
take the length of nodemask. This series switches the kernel to runtime
nr_node_ids, and where the MAX_NUMNODES is required, the caller would
need to pass it explicitly.

Add the length argument to __first_node(), __next_node() and
__next_node_in().

Signed-off-by: Yury Norov <ynorov@nvidia.com>
---
 include/linux/nodemask.h | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)

diff --git a/include/linux/nodemask.h b/include/linux/nodemask.h
index 37fd5ac957c0..a17aa5c2e562 100644
--- a/include/linux/nodemask.h
+++ b/include/linux/nodemask.h
@@ -271,29 +271,32 @@ static __always_inline int __nodes_weight(const nodemask_t *srcp, unsigned int n
 /* FIXME: better would be to fix all architectures to never return
           > MAX_NUMNODES, then the silly min()s could be dropped. */
 
-#define first_node(src) __first_node(&(src))
-static __always_inline unsigned int __first_node(const nodemask_t *srcp)
+#define first_node(src) __first_node(&(src), MAX_NUMNODES)
+static __always_inline unsigned int __first_node(const nodemask_t *srcp,
+						unsigned int nbits)
 {
-	return min(MAX_NUMNODES, find_first_bit(srcp->bits, MAX_NUMNODES));
+	return min(nbits, find_first_bit(srcp->bits, nbits));
 }
 
-#define next_node(n, src) __next_node((n), &(src))
-static __always_inline unsigned int __next_node(int n, const nodemask_t *srcp)
+#define next_node(n, src) __next_node((n), &(src), MAX_NUMNODES)
+static __always_inline unsigned int __next_node(int n, const nodemask_t *srcp,
+					       unsigned int nbits)
 {
-	return min(MAX_NUMNODES, find_next_bit(srcp->bits, MAX_NUMNODES, n+1));
+	return min(nbits, find_next_bit(srcp->bits, nbits, n + 1));
 }
 
 /*
  * Find the next present node in src, starting after node n, wrapping around to
  * the first node in src if needed.  Returns MAX_NUMNODES if src is empty.
  */
-#define next_node_in(n, src) __next_node_in((n), &(src))
-static __always_inline unsigned int __next_node_in(int node, const nodemask_t *srcp)
+#define next_node_in(n, src) __next_node_in((n), &(src), MAX_NUMNODES)
+static __always_inline unsigned int __next_node_in(int node, const nodemask_t *srcp,
+						  unsigned int nbits)
 {
-	unsigned int ret = __next_node(node, srcp);
+	unsigned int ret = __next_node(node, srcp, nbits);
 
-	if (ret == MAX_NUMNODES)
-		ret = __first_node(srcp);
+	if (ret == nbits)
+		ret = __first_node(srcp, nbits);
 	return ret;
 }
 
-- 
2.53.0


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

* [PATCH 03/11] mm/mempolicy: reject migrate_pages source IDs beyond nr_node_ids
  2026-11-27 23:01 [PATCH 00/11] nodemask: switch to runtime nr_node_ids Yury Norov
  2026-11-27 23:01 ` [PATCH 01/11] nodemask: add 'small' and 'large' compile-time sizes Yury Norov
  2026-11-27 23:01 ` [PATCH 02/11] nodemask: pass nodemask length to node search functions Yury Norov
@ 2026-11-27 23:01 ` Yury Norov
  2026-11-27 23:01 ` [PATCH 04/11] mm/mempolicy: preserve full-width userspace nodemasks Yury Norov
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Yury Norov @ 2026-11-27 23:01 UTC (permalink / raw)
  To: linux-kernel, Andrew Morton, linux-mm, Rasmus Villemoes,
	David Hildenbrand, Gregory Price, Zi Yan, Joshua Hahn, Rakie Kim,
	Ying Huang, Muchun Song, Oscar Salvador, Johannes Weiner,
	Kairui Song, Thomas Bogendoerfer, Rafael J. Wysocki, Len Brown,
	Jens Axboe, Peter Zijlstra, Mel Gorman, Steven Rostedt,
	Masami Hiramatsu, Mathieu Desnoyers, linux-mips, linux-acpi,
	linux-block, linux-trace-kernel
  Cc: Yury Norov, Liam R. Howlett, Axel Rasmussen, Baolin Wang,
	Baoquan He, Barry Song, Linus Torvalds, Lorenzo Stoakes,
	Michal Hocko, Mike Rapoport, Qi Zheng, Shakeel Butt,
	Suren Baghdasaryan, Vlastimil Babka, Wei Xu, Yuanchu Xie,
	Yury Norov

The runtime nodemask conversion limits source iteration and weight
calculations in do_migrate_pages() to the runtime node range. Reject
userspace source masks containing node IDs at or above nr_node_ids
instead of silently ignoring those bits during migration selection.

Check the full MAX_NUMNODES-wide mask after copying old_nodes and return
-EINVAL if any source bit lies outside the runtime range. Keep the
existing destination permission checks and the do_migrate_pages() API.

Signed-off-by: Yury Norov <ynorov@nvidia.com>
---
 mm/mempolicy.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index ffe4e45cc97d..67e258c62527 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -1905,6 +1905,12 @@ static int kernel_migrate_pages(pid_t pid, unsigned long maxnode,
 	if (err)
 		goto out;
 
+	/* Source node IDs must fit within the runtime nodemask. */
+	if (__next_node(nr_node_ids - 1, old, MAX_NUMNODES) < MAX_NUMNODES) {
+		err = -EINVAL;
+		goto out;
+	}
+
 	err = get_nodes(new, new_nodes, maxnode);
 	if (err)
 		goto out;
-- 
2.53.0


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

* [PATCH 04/11] mm/mempolicy: preserve full-width userspace nodemasks
  2026-11-27 23:01 [PATCH 00/11] nodemask: switch to runtime nr_node_ids Yury Norov
                   ` (2 preceding siblings ...)
  2026-11-27 23:01 ` [PATCH 03/11] mm/mempolicy: reject migrate_pages source IDs beyond nr_node_ids Yury Norov
@ 2026-11-27 23:01 ` Yury Norov
  2026-11-27 23:01 ` [PATCH 05/11] mm: switch to runtime nodemask length Yury Norov
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Yury Norov @ 2026-11-27 23:01 UTC (permalink / raw)
  To: linux-kernel, Andrew Morton, linux-mm, Rasmus Villemoes,
	David Hildenbrand, Gregory Price, Zi Yan, Joshua Hahn, Rakie Kim,
	Ying Huang, Muchun Song, Oscar Salvador, Johannes Weiner,
	Kairui Song, Thomas Bogendoerfer, Rafael J. Wysocki, Len Brown,
	Jens Axboe, Peter Zijlstra, Mel Gorman, Steven Rostedt,
	Masami Hiramatsu, Mathieu Desnoyers, linux-mips, linux-acpi,
	linux-block, linux-trace-kernel
  Cc: Yury Norov, Liam R. Howlett, Axel Rasmussen, Baolin Wang,
	Baoquan He, Barry Song, Linus Torvalds, Lorenzo Stoakes,
	Michal Hocko, Mike Rapoport, Qi Zheng, Shakeel Butt,
	Suren Baghdasaryan, Vlastimil Babka, Wei Xu, Yuanchu Xie,
	Yury Norov

This series switches nodemask API to runtime nr_node_ids.

Userspace mempolicy masks can contain static node IDs or relative
indices above nr_node_ids. They must retain their full MAX_NUMNODES
width when the nodemask helpers switch to runtime bounds.

For example, with MAX_NUMNODES=1024, nr_node_ids=2 and allowed
memory nodes {0,1},

       set_mempolicy(MPOL_BIND | MPOL_F_RELATIVE_NODES, ...)

can specify a mask containing only bit 3. This is a relative
index: folding it modulo the two allowed nodes maps it to node 1.

A runtime-bounded emptiness check would reject this valid mask,
and a runtime-bounded fold would discard its only set bit.

Use underscored nodemask API with an explicit MAX_NUMNODES bound where
needed.

Signed-off-by: Yury Norov <ynorov@nvidia.com>
---
 mm/mempolicy.c | 29 +++++++++++++++++++----------
 1 file changed, 19 insertions(+), 10 deletions(-)

diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index 67e258c62527..ed8b20b27753 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -396,7 +396,10 @@ static void mpol_relative_nodemask(nodemask_t *ret, const nodemask_t *orig,
 				   const nodemask_t *rel)
 {
 	nodemask_t tmp;
-	nodes_fold(tmp, *orig, nodes_weight(*rel));
+
+	/* Relative indices can exceed the runtime node ID limit. */
+	__nodes_fold(&tmp, orig, nodes_weight(*rel), MAX_NUMNODES);
+
 	nodes_onto(*ret, tmp, *rel);
 }
 
@@ -462,14 +465,16 @@ static int mpol_set_nodemask(struct mempolicy *pol,
 /*
  * This function just creates a new policy, does some check and simple
  * initialization. You must invoke mpol_set_nodemask() to set nodes.
+ * Input masks can include static IDs or relative indices above nr_node_ids.
  */
 static struct mempolicy *mpol_new(unsigned short mode, unsigned short flags,
 				  nodemask_t *nodes)
 {
 	struct mempolicy *policy;
+	bool empty = !nodes || __nodes_empty(nodes, MAX_NUMNODES);
 
 	if (mode == MPOL_DEFAULT) {
-		if (nodes && !nodes_empty(*nodes))
+		if (!empty)
 			return ERR_PTR(-EINVAL);
 		return NULL;
 	}
@@ -481,7 +486,7 @@ static struct mempolicy *mpol_new(unsigned short mode, unsigned short flags,
 	 * All other modes require a valid pointer to a non-empty nodemask.
 	 */
 	if (mode == MPOL_PREFERRED) {
-		if (nodes_empty(*nodes)) {
+		if (empty) {
 			if (((flags & MPOL_F_STATIC_NODES) ||
 			     (flags & MPOL_F_RELATIVE_NODES)))
 				return ERR_PTR(-EINVAL);
@@ -489,11 +494,11 @@ static struct mempolicy *mpol_new(unsigned short mode, unsigned short flags,
 			mode = MPOL_LOCAL;
 		}
 	} else if (mode == MPOL_LOCAL) {
-		if (!nodes_empty(*nodes) ||
+		if (!empty ||
 		    (flags & MPOL_F_STATIC_NODES) ||
 		    (flags & MPOL_F_RELATIVE_NODES))
 			return ERR_PTR(-EINVAL);
-	} else if (nodes_empty(*nodes))
+	} else if (empty)
 		return ERR_PTR(-EINVAL);
 
 	policy = kmem_cache_alloc(policy_cache, GFP_KERNEL);
@@ -1685,7 +1690,8 @@ static int get_nodes(nodemask_t *nodes, const unsigned long __user *nmask,
 		     unsigned long maxnode)
 {
 	--maxnode;
-	nodes_clear(*nodes);
+	/* Userspace masks retain their full MAX_NUMNODES capacity. */
+	__nodes_clear(nodes, MAX_NUMNODES);
 	if (maxnode == 0 || !nmask)
 		return 0;
 	if (maxnode > PAGE_SIZE*BITS_PER_BYTE)
@@ -1940,7 +1946,9 @@ static int kernel_migrate_pages(pid_t pid, unsigned long maxnode,
 
 	task_nodes = cpuset_mems_allowed(task);
 	/* Is the user allowed to access the target nodes? */
-	if (!nodes_subset(*new, task_nodes) && !capable(CAP_SYS_NICE)) {
+	if ((!nodes_subset(*new, task_nodes) ||
+	     __next_node(nr_node_ids - 1, new, MAX_NUMNODES) < MAX_NUMNODES) &&
+	    !capable(CAP_SYS_NICE)) {
 		err = -EPERM;
 		goto out_put;
 	}
@@ -2902,7 +2910,8 @@ bool __mpol_equal(struct mempolicy *a, struct mempolicy *b)
 	if (a->home_node != b->home_node)
 		return false;
 	if (mpol_store_user_nodemask(a))
-		if (!nodes_equal(a->w.user_nodemask, b->w.user_nodemask))
+		if (!__nodes_equal(&a->w.user_nodemask, &b->w.user_nodemask,
+				   MAX_NUMNODES))
 			return false;
 
 	switch (a->mode) {
@@ -3381,7 +3390,7 @@ static inline void __init check_numabalancing_enable(void)
 
 void __init numa_policy_init(void)
 {
-	nodemask_t interleave_nodes;
+	nodemask_t interleave_nodes = NODE_MASK_NONE;
 	unsigned long largest = 0;
 	int nid, prefer = 0;
 
@@ -3467,7 +3476,7 @@ int mpol_parse_str(char *str, struct mempolicy **mpol)
 {
 	struct mempolicy *new = NULL;
 	unsigned short mode_flags;
-	nodemask_t nodes;
+	nodemask_t nodes = NODE_MASK_NONE;
 	char *nodelist = strchr(str, ':');
 	char *flags = strchr(str, '=');
 	int err = 1, mode;
-- 
2.53.0


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

* [PATCH 05/11] mm: switch to runtime nodemask length
  2026-11-27 23:01 [PATCH 00/11] nodemask: switch to runtime nr_node_ids Yury Norov
                   ` (3 preceding siblings ...)
  2026-11-27 23:01 ` [PATCH 04/11] mm/mempolicy: preserve full-width userspace nodemasks Yury Norov
@ 2026-11-27 23:01 ` Yury Norov
  2026-11-27 23:01 ` [PATCH 06/11] MIPS: IP27: switch nmi_dump " Yury Norov
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Yury Norov @ 2026-11-27 23:01 UTC (permalink / raw)
  To: linux-kernel, Andrew Morton, linux-mm, Rasmus Villemoes,
	David Hildenbrand, Gregory Price, Zi Yan, Joshua Hahn, Rakie Kim,
	Ying Huang, Muchun Song, Oscar Salvador, Johannes Weiner,
	Kairui Song, Thomas Bogendoerfer, Rafael J. Wysocki, Len Brown,
	Jens Axboe, Peter Zijlstra, Mel Gorman, Steven Rostedt,
	Masami Hiramatsu, Mathieu Desnoyers, linux-mips, linux-acpi,
	linux-block, linux-trace-kernel
  Cc: Yury Norov, Liam R. Howlett, Axel Rasmussen, Baolin Wang,
	Baoquan He, Barry Song, Linus Torvalds, Lorenzo Stoakes,
	Michal Hocko, Mike Rapoport, Qi Zheng, Shakeel Butt,
	Suren Baghdasaryan, Vlastimil Babka, Wei Xu, Yuanchu Xie,
	Yury Norov

In preparation for switching nodemask API to runtime nr_node_ids,
update the boundary checking conditions.

Signed-off-by: Yury Norov <ynorov@nvidia.com>
---
 mm/hugetlb_internal.h |  2 +-
 mm/mempolicy.c        | 20 ++++++++++----------
 mm/mmzone.c           |  2 +-
 mm/vmscan.c           |  2 +-
 4 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/mm/hugetlb_internal.h b/mm/hugetlb_internal.h
index 1d2f870deccf..bcdff7485c8d 100644
--- a/mm/hugetlb_internal.h
+++ b/mm/hugetlb_internal.h
@@ -31,7 +31,7 @@ static inline bool hstate_is_gigantic_no_runtime(struct hstate *h)
 static inline int next_node_allowed(int nid, nodemask_t *nodes_allowed)
 {
 	nid = next_node_in(nid, *nodes_allowed);
-	VM_BUG_ON(nid >= MAX_NUMNODES);
+	VM_BUG_ON(nid >= nr_node_ids);
 
 	return nid;
 }
diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index ed8b20b27753..d64d3e70a82e 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -1123,7 +1123,7 @@ static long do_set_mempolicy(unsigned short mode, unsigned short flags,
 	current->mempolicy = new;
 	if (new && (new->mode == MPOL_INTERLEAVE ||
 		    new->mode == MPOL_WEIGHTED_INTERLEAVE)) {
-		current->il_prev = MAX_NUMNODES-1;
+		current->il_prev = nr_node_ids - 1;
 		current->il_weight = 0;
 	}
 	task_unlock(current);
@@ -2141,7 +2141,7 @@ static unsigned int weighted_interleave_nodes(struct mempolicy *policy)
 		node = next_node_in(node, policy->nodes);
 		if (read_mems_allowed_retry(cpuset_mems_cookie))
 			goto retry;
-		if (node == MAX_NUMNODES)
+		if (node >= nr_node_ids)
 			return node;
 		current->il_prev = node;
 		current->il_weight = get_il_weight(node);
@@ -2162,7 +2162,7 @@ static unsigned int interleave_nodes(struct mempolicy *policy)
 		nid = next_node_in(current->il_prev, policy->nodes);
 	} while (read_mems_allowed_retry(cpuset_mems_cookie));
 
-	if (nid < MAX_NUMNODES)
+	if (nid < nr_node_ids)
 		current->il_prev = nid;
 	return nid;
 }
@@ -2261,7 +2261,7 @@ static unsigned int weighted_interleave_nid(struct mempolicy *pol, pgoff_t ilx)
 	 * Clamp this loop to a single pass (nnodes) to keep the walk
 	 * bounded by node count.
 	 */
-	while (target && nnodes-- && nid < MAX_NUMNODES) {
+	while (target && nnodes-- && nid < nr_node_ids) {
 		/* detect system default usage */
 		weight = table ? table[nid] : 1;
 		if (target < weight)
@@ -2272,7 +2272,7 @@ static unsigned int weighted_interleave_nid(struct mempolicy *pol, pgoff_t ilx)
 	rcu_read_unlock();
 
 	/* the mask emptied under the walk */
-	if (nid >= MAX_NUMNODES)
+	if (nid >= nr_node_ids)
 		return numa_node_id();
 	return nid;
 }
@@ -2295,11 +2295,11 @@ static unsigned int interleave_nid(struct mempolicy *pol, pgoff_t ilx)
 	nid = first_node(pol->nodes);
 
 	/* A concurrent cpuset rebind may cause us to see an empty nodemask */
-	for (i = 0; i < target && nid < MAX_NUMNODES; i++)
+	for (i = 0; i < target && nid < nr_node_ids; i++)
 		nid = next_node_in(nid, pol->nodes);
 
 	/* the mask emptied under the walk */
-	if (nid >= MAX_NUMNODES)
+	if (nid >= nr_node_ids)
 		return numa_node_id();
 	return nid;
 }
@@ -2697,7 +2697,7 @@ static unsigned long alloc_pages_bulk_weighted_interleave(gfp_t gfp,
 	unsigned int weight_total = 0;
 	unsigned long rem_pages = nr_pages;
 	int nnodes, node;
-	int resume_node = MAX_NUMNODES - 1;
+	int resume_node = nr_node_ids - 1;
 	u8 resume_weight = 0;
 	int prev_node;
 	int i;
@@ -2759,12 +2759,12 @@ static unsigned long alloc_pages_bulk_weighted_interleave(gfp_t gfp,
 	rounds = rem_pages / weight_total;
 	delta = rem_pages % weight_total;
 	resume_node = next_node_in(prev_node, pol->nodes);
-	if (resume_node >= MAX_NUMNODES)
+	if (resume_node >= nr_node_ids)
 		goto out;
 	resume_weight = table ? table[resume_node] : 1;
 	for (i = 0; i < nnodes; i++) {
 		node = next_node_in(prev_node, pol->nodes);
-		if (node >= MAX_NUMNODES)
+		if (node >= nr_node_ids)
 			break;
 		weight = table ? table[node] : 1;
 		node_pages = weight * rounds;
diff --git a/mm/mmzone.c b/mm/mmzone.c
index 65880396f104..2c96341ad7e0 100644
--- a/mm/mmzone.c
+++ b/mm/mmzone.c
@@ -19,7 +19,7 @@ struct pglist_data *next_online_pgdat(struct pglist_data *pgdat)
 {
 	int nid = next_online_node(pgdat->node_id);
 
-	if (nid == MAX_NUMNODES)
+	if (nid >= nr_node_ids)
 		return NULL;
 	return NODE_DATA(nid);
 }
diff --git a/mm/vmscan.c b/mm/vmscan.c
index 6a1af9b92349..7d78181946b7 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -5751,7 +5751,7 @@ static void *lru_gen_seq_next(struct seq_file *m, void *v, loff_t *pos)
 	++*pos;
 
 	nid = next_memory_node(nid);
-	if (nid == MAX_NUMNODES) {
+	if (nid >= nr_node_ids) {
 		memcg = mem_cgroup_iter(NULL, memcg, NULL);
 		if (!memcg)
 			return NULL;
-- 
2.53.0


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

* [PATCH 06/11] MIPS: IP27: switch nmi_dump to runtime nodemask length
  2026-11-27 23:01 [PATCH 00/11] nodemask: switch to runtime nr_node_ids Yury Norov
                   ` (4 preceding siblings ...)
  2026-11-27 23:01 ` [PATCH 05/11] mm: switch to runtime nodemask length Yury Norov
@ 2026-11-27 23:01 ` Yury Norov
  2026-11-27 23:01 ` [PATCH 07/11] ACPI: NUMA: switch acpi_map_pxm_to_node " Yury Norov
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Yury Norov @ 2026-11-27 23:01 UTC (permalink / raw)
  To: linux-kernel, Andrew Morton, linux-mm, Rasmus Villemoes,
	David Hildenbrand, Gregory Price, Zi Yan, Joshua Hahn, Rakie Kim,
	Ying Huang, Muchun Song, Oscar Salvador, Johannes Weiner,
	Kairui Song, Thomas Bogendoerfer, Rafael J. Wysocki, Len Brown,
	Jens Axboe, Peter Zijlstra, Mel Gorman, Steven Rostedt,
	Masami Hiramatsu, Mathieu Desnoyers, linux-mips, linux-acpi,
	linux-block, linux-trace-kernel
  Cc: Yury Norov, Liam R. Howlett, Axel Rasmussen, Baolin Wang,
	Baoquan He, Barry Song, Linus Torvalds, Lorenzo Stoakes,
	Michal Hocko, Mike Rapoport, Qi Zheng, Shakeel Butt,
	Suren Baghdasaryan, Vlastimil Babka, Wei Xu, Yuanchu Xie,
	Yury Norov

In preparation for switching nodemask API to runtime nr_node_ids,
update the boundary checking condition.

Signed-off-by: Yury Norov <ynorov@nvidia.com>
---
 arch/mips/sgi-ip27/ip27-nmi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/mips/sgi-ip27/ip27-nmi.c b/arch/mips/sgi-ip27/ip27-nmi.c
index c502982621e6..38b9a89bcca2 100644
--- a/arch/mips/sgi-ip27/ip27-nmi.c
+++ b/arch/mips/sgi-ip27/ip27-nmi.c
@@ -207,7 +207,7 @@ static void nmi_dump(void)
 		for_each_online_node(node)
 			if (NODEPDA(node)->dump_count == 0)
 				break;
-		if (node == MAX_NUMNODES)
+		if (node >= nr_node_ids)
 			break;
 		if (i == 1000) {
 			for_each_online_node(node)
-- 
2.53.0


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

* [PATCH 07/11] ACPI: NUMA: switch acpi_map_pxm_to_node to runtime nodemask length
  2026-11-27 23:01 [PATCH 00/11] nodemask: switch to runtime nr_node_ids Yury Norov
                   ` (5 preceding siblings ...)
  2026-11-27 23:01 ` [PATCH 06/11] MIPS: IP27: switch nmi_dump " Yury Norov
@ 2026-11-27 23:01 ` Yury Norov
  2026-11-27 23:01 ` [PATCH 08/11] mtip32xx: switch next_online_node() " Yury Norov
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Yury Norov @ 2026-11-27 23:01 UTC (permalink / raw)
  To: linux-kernel, Andrew Morton, linux-mm, Rasmus Villemoes,
	David Hildenbrand, Gregory Price, Zi Yan, Joshua Hahn, Rakie Kim,
	Ying Huang, Muchun Song, Oscar Salvador, Johannes Weiner,
	Kairui Song, Thomas Bogendoerfer, Rafael J. Wysocki, Len Brown,
	Jens Axboe, Peter Zijlstra, Mel Gorman, Steven Rostedt,
	Masami Hiramatsu, Mathieu Desnoyers, linux-mips, linux-acpi,
	linux-block, linux-trace-kernel
  Cc: Yury Norov, Liam R. Howlett, Axel Rasmussen, Baolin Wang,
	Baoquan He, Barry Song, Linus Torvalds, Lorenzo Stoakes,
	Michal Hocko, Mike Rapoport, Qi Zheng, Shakeel Butt,
	Suren Baghdasaryan, Vlastimil Babka, Wei Xu, Yuanchu Xie,
	Yury Norov

In preparation for switching nodemask API to runtime nr_node_ids,
update the boundary checking condition.

Signed-off-by: Yury Norov <ynorov@nvidia.com>
---
 drivers/acpi/numa/srat.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/acpi/numa/srat.c b/drivers/acpi/numa/srat.c
index f5616830f5a9..d7acb2e973a3 100644
--- a/drivers/acpi/numa/srat.c
+++ b/drivers/acpi/numa/srat.c
@@ -74,7 +74,7 @@ int acpi_map_pxm_to_node(int pxm)
 
 	if (node == NUMA_NO_NODE) {
 		node = first_unset_node(nodes_found_map);
-		if (node >= MAX_NUMNODES)
+		if (node >= nr_node_ids)
 			return NUMA_NO_NODE;
 		__acpi_map_pxm_to_node(pxm, node);
 		node_set(node, nodes_found_map);
-- 
2.53.0


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

* [PATCH 08/11] mtip32xx: switch next_online_node() to runtime nodemask length
  2026-11-27 23:01 [PATCH 00/11] nodemask: switch to runtime nr_node_ids Yury Norov
                   ` (6 preceding siblings ...)
  2026-11-27 23:01 ` [PATCH 07/11] ACPI: NUMA: switch acpi_map_pxm_to_node " Yury Norov
@ 2026-11-27 23:01 ` Yury Norov
  2026-11-27 23:01 ` [PATCH 09/11] nodemask: add nodes_copy_and_extend() Yury Norov
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Yury Norov @ 2026-11-27 23:01 UTC (permalink / raw)
  To: linux-kernel, Andrew Morton, linux-mm, Rasmus Villemoes,
	David Hildenbrand, Gregory Price, Zi Yan, Joshua Hahn, Rakie Kim,
	Ying Huang, Muchun Song, Oscar Salvador, Johannes Weiner,
	Kairui Song, Thomas Bogendoerfer, Rafael J. Wysocki, Len Brown,
	Jens Axboe, Peter Zijlstra, Mel Gorman, Steven Rostedt,
	Masami Hiramatsu, Mathieu Desnoyers, linux-mips, linux-acpi,
	linux-block, linux-trace-kernel
  Cc: Yury Norov, Liam R. Howlett, Axel Rasmussen, Baolin Wang,
	Baoquan He, Barry Song, Linus Torvalds, Lorenzo Stoakes,
	Michal Hocko, Mike Rapoport, Qi Zheng, Shakeel Butt,
	Suren Baghdasaryan, Vlastimil Babka, Wei Xu, Yuanchu Xie,
	Yury Norov

In preparation for switching nodemask API to runtime nr_node_ids,
update the boundary checking condition.

Signed-off-by: Yury Norov <ynorov@nvidia.com>
---
 drivers/block/mtip32xx/mtip32xx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/block/mtip32xx/mtip32xx.c b/drivers/block/mtip32xx/mtip32xx.c
index 113bdb868c46..8b8b4211e41a 100644
--- a/drivers/block/mtip32xx/mtip32xx.c
+++ b/drivers/block/mtip32xx/mtip32xx.c
@@ -3630,7 +3630,7 @@ static inline int mtip_get_next_rr_node(void)
 	}
 
 	next_node = next_online_node(next_node);
-	if (next_node == MAX_NUMNODES)
+	if (next_node >= nr_node_ids)
 		next_node = first_online_node;
 	return next_node;
 }
-- 
2.53.0


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

* [PATCH 09/11] nodemask: add nodes_copy_and_extend()
  2026-11-27 23:01 [PATCH 00/11] nodemask: switch to runtime nr_node_ids Yury Norov
                   ` (7 preceding siblings ...)
  2026-11-27 23:01 ` [PATCH 08/11] mtip32xx: switch next_online_node() " Yury Norov
@ 2026-11-27 23:01 ` Yury Norov
  2026-11-27 23:01 ` [PATCH 10/11] sched/numa: bound cpuset trace masks by nr_node_ids Yury Norov
  2026-11-27 23:01 ` [PATCH 11/11] nodemask: add cpumask-style operation size bounds Yury Norov
  10 siblings, 0 replies; 12+ messages in thread
From: Yury Norov @ 2026-11-27 23:01 UTC (permalink / raw)
  To: linux-kernel, Andrew Morton, linux-mm, Rasmus Villemoes,
	David Hildenbrand, Gregory Price, Zi Yan, Joshua Hahn, Rakie Kim,
	Ying Huang, Muchun Song, Oscar Salvador, Johannes Weiner,
	Kairui Song, Thomas Bogendoerfer, Rafael J. Wysocki, Len Brown,
	Jens Axboe, Peter Zijlstra, Mel Gorman, Steven Rostedt,
	Masami Hiramatsu, Mathieu Desnoyers, linux-mips, linux-acpi,
	linux-block, linux-trace-kernel
  Cc: Yury Norov, Liam R. Howlett, Axel Rasmussen, Baolin Wang,
	Baoquan He, Barry Song, Linus Torvalds, Lorenzo Stoakes,
	Michal Hocko, Mike Rapoport, Qi Zheng, Shakeel Butt,
	Suren Baghdasaryan, Vlastimil Babka, Wei Xu, Yuanchu Xie,
	Yury Norov

Add nodes_copy_and_extend() to copy the nr_node_ids range and clear all
remaining bits in the destination's MAX_NUMNODES-sized storage.

Signed-off-by: Yury Norov <ynorov@nvidia.com>
---
 include/linux/nodemask.h | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/include/linux/nodemask.h b/include/linux/nodemask.h
index a17aa5c2e562..4ef6ca642054 100644
--- a/include/linux/nodemask.h
+++ b/include/linux/nodemask.h
@@ -30,6 +30,7 @@
  * void nodes_or(dst, src1, src2)	dst = src1 | src2  [union]
  * void nodes_xor(dst, src1, src2)	dst = src1 ^ src2
  * bool nodes_andnot(dst, src1, src2)	dst = src1 & ~src2
+ * void nodes_copy_and_extend(dst, src)	Copy runtime nodes, clear remaining bits
  * void nodes_complement(dst, src)	dst = ~src
  *
  * bool nodes_equal(mask1, mask2)	Does mask1 == mask2?
@@ -218,6 +219,16 @@ static __always_inline void __nodes_copy(nodemask_t *dstp,
 	bitmap_copy(dstp->bits, srcp->bits, nbits);
 }
 
+/* Copy runtime nodes and clear the rest of the destination's storage. */
+#define nodes_copy_and_extend(dst, src) \
+		__nodes_copy_and_extend(&(dst), &(src), nr_node_ids, MAX_NUMNODES)
+static __always_inline void __nodes_copy_and_extend(nodemask_t *dstp,
+					const nodemask_t *srcp,
+					unsigned int nbits, unsigned int size)
+{
+	bitmap_copy_and_extend(dstp->bits, srcp->bits, nbits, size);
+}
+
 #define nodes_complement(dst, src) \
 			__nodes_complement(&(dst), &(src), MAX_NUMNODES)
 static __always_inline void __nodes_complement(nodemask_t *dstp,
-- 
2.53.0


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

* [PATCH 10/11] sched/numa: bound cpuset trace masks by nr_node_ids
  2026-11-27 23:01 [PATCH 00/11] nodemask: switch to runtime nr_node_ids Yury Norov
                   ` (8 preceding siblings ...)
  2026-11-27 23:01 ` [PATCH 09/11] nodemask: add nodes_copy_and_extend() Yury Norov
@ 2026-11-27 23:01 ` Yury Norov
  2026-11-27 23:01 ` [PATCH 11/11] nodemask: add cpumask-style operation size bounds Yury Norov
  10 siblings, 0 replies; 12+ messages in thread
From: Yury Norov @ 2026-11-27 23:01 UTC (permalink / raw)
  To: linux-kernel, Andrew Morton, linux-mm, Rasmus Villemoes,
	David Hildenbrand, Gregory Price, Zi Yan, Joshua Hahn, Rakie Kim,
	Ying Huang, Muchun Song, Oscar Salvador, Johannes Weiner,
	Kairui Song, Thomas Bogendoerfer, Rafael J. Wysocki, Len Brown,
	Jens Axboe, Peter Zijlstra, Mel Gorman, Steven Rostedt,
	Masami Hiramatsu, Mathieu Desnoyers, linux-mips, linux-acpi,
	linux-block, linux-trace-kernel
  Cc: Yury Norov, Liam R. Howlett, Axel Rasmussen, Baolin Wang,
	Baoquan He, Barry Song, Linus Torvalds, Lorenzo Stoakes,
	Michal Hocko, Mike Rapoport, Qi Zheng, Shakeel Butt,
	Suren Baghdasaryan, Vlastimil Babka, Wei Xu, Yuanchu Xie,
	Yury Norov

With switching nodemask API to runtime nr_node_ids, bits beyond this
boundary till MAX_NUMNODES may be in undefined state. Those bits may
show up in the raw trace record.

Keep the fixed-size trace record and use nodes_copy_and_extend() to copy
the runtime range and clear every remaining bit, including padding in
the last copied word.

Print only the runtime range.

Signed-off-by: Yury Norov <ynorov@nvidia.com>
---
 include/trace/events/sched.h | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/include/trace/events/sched.h b/include/trace/events/sched.h
index 535860581f15..38a2a10cba42 100644
--- a/include/trace/events/sched.h
+++ b/include/trace/events/sched.h
@@ -793,8 +793,9 @@ TRACE_EVENT(sched_skip_cpuset_numa,
 		__entry->ngid		 = task_numa_group_id(tsk);
 		BUILD_BUG_ON(sizeof(nodemask_t) != \
 			     BITS_TO_LONGS(MAX_NUMNODES) * sizeof(long));
-		memcpy(__entry->mem_allowed, mem_allowed_ptr->bits,
-		       sizeof(__entry->mem_allowed));
+		/* Clear unused bits in the fixed-size trace field. */
+		nodes_copy_and_extend(*(nodemask_t *)__entry->mem_allowed,
+				      *mem_allowed_ptr);
 	),
 
 	TP_printk("comm=%s pid=%d tgid=%d ngid=%d mem_nodes_allowed=%*pbl",
@@ -802,7 +803,7 @@ TRACE_EVENT(sched_skip_cpuset_numa,
 		  __entry->pid,
 		  __entry->tgid,
 		  __entry->ngid,
-		  MAX_NUMNODES, __entry->mem_allowed)
+		  nr_node_ids, __entry->mem_allowed)
 );
 #endif /* CONFIG_NUMA_BALANCING */
 
-- 
2.53.0


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

* [PATCH 11/11] nodemask: add cpumask-style operation size bounds
  2026-11-27 23:01 [PATCH 00/11] nodemask: switch to runtime nr_node_ids Yury Norov
                   ` (9 preceding siblings ...)
  2026-11-27 23:01 ` [PATCH 10/11] sched/numa: bound cpuset trace masks by nr_node_ids Yury Norov
@ 2026-11-27 23:01 ` Yury Norov
  10 siblings, 0 replies; 12+ messages in thread
From: Yury Norov @ 2026-11-27 23:01 UTC (permalink / raw)
  To: linux-kernel, Andrew Morton, linux-mm, Rasmus Villemoes,
	David Hildenbrand, Gregory Price, Zi Yan, Joshua Hahn, Rakie Kim,
	Ying Huang, Muchun Song, Oscar Salvador, Johannes Weiner,
	Kairui Song, Thomas Bogendoerfer, Rafael J. Wysocki, Len Brown,
	Jens Axboe, Peter Zijlstra, Mel Gorman, Steven Rostedt,
	Masami Hiramatsu, Mathieu Desnoyers, linux-mips, linux-acpi,
	linux-block, linux-trace-kernel
  Cc: Yury Norov, Liam R. Howlett, Axel Rasmussen, Baolin Wang,
	Baoquan He, Barry Song, Linus Torvalds, Lorenzo Stoakes,
	Michal Hocko, Mike Rapoport, Qi Zheng, Shakeel Butt,
	Suren Baghdasaryan, Vlastimil Babka, Wei Xu, Yuanchu Xie,
	Yury Norov

Nodemasks have a compile-time capacity of MAX_NUMNODES, and
nr_node_ids describes the runtime node ID usage.

Modern distros like Ubuntu default MAX_NUMNODES to 1024, while the
actual number of nodes is typically smaller. All nodemask operations
currently refer to the MAX_NUMNODES, which adds a substantial overhead
on every traversing operation.

Simple switch to the runtime nr_node_ids might hurt users configuring
their MAX_NUMNODES to reasonable small values because the runtime value
breaks compile-time optimizations in the underlying bitmap API.

Thus, switch to using small_nodemask_bits, large_nodemask_bits, or
the precise nr_node_ids, as appropriate.

Bloat-o-meter reports 1-8KB decrease of .text, depending on NR_CPUS
and MAX_NUMNODES. With nr_node_ids == 2 and MAX_NUMNODES == 1024, basic
operations like nodes_copy() are ~4x faster

Signed-off-by: Yury Norov <ynorov@nvidia.com>
---
 include/linux/nodemask.h | 80 +++++++++++++++++++++-------------------
 1 file changed, 42 insertions(+), 38 deletions(-)

diff --git a/include/linux/nodemask.h b/include/linux/nodemask.h
index 4ef6ca642054..67ad972b8942 100644
--- a/include/linux/nodemask.h
+++ b/include/linux/nodemask.h
@@ -40,12 +40,12 @@
  * bool nodes_full(mask)		Is mask full (all bits sets)?
  * int nodes_weight(mask)		Hamming weight - number of set bits
  *
- * unsigned int first_node(mask)	Number lowest set bit, or MAX_NUMNODES
- * unsigned int next_node(node, mask)	Next node past 'node', or MAX_NUMNODES
+ * unsigned int first_node(mask)	Number lowest set bit, or >= nr_node_ids
+ * unsigned int next_node(node, mask)	Next node past 'node', or >= nr_node_ids
  * unsigned int next_node_in(node, mask) Next node past 'node', or wrap to first,
- *					or MAX_NUMNODES
+ *					or >= nr_node_ids
  * unsigned int first_unset_node(mask)	First node not set in mask, or
- *					MAX_NUMNODES
+ *					>= nr_node_ids
  *
  * nodemask_t nodemask_of_node(node)	Return nodemask with bit 'node' set
  * NODE_MASK_ALL			Initializer - all bits set
@@ -90,7 +90,6 @@
 
 #include <linux/threads.h>
 #include <linux/bitmap.h>
-#include <linux/minmax.h>
 #include <linux/nodemask_types.h>
 #include <linux/random.h>
 
@@ -158,13 +157,17 @@ static __always_inline void __node_clear(int node, volatile nodemask_t *dstp)
 	clear_bit(node, dstp->bits);
 }
 
-#define nodes_setall(dst) __nodes_setall(&(dst), MAX_NUMNODES)
+#define nodes_setall(dst) __nodes_setall(&(dst), nr_node_ids)
 static __always_inline void __nodes_setall(nodemask_t *dstp, unsigned int nbits)
 {
+	if (small_const_nbits(small_nodemask_bits)) {
+		dstp->bits[0] = BITMAP_LAST_WORD_MASK(nbits);
+		return;
+	}
 	bitmap_fill(dstp->bits, nbits);
 }
 
-#define nodes_clear(dst) __nodes_clear(&(dst), MAX_NUMNODES)
+#define nodes_clear(dst) __nodes_clear(&(dst), large_nodemask_bits)
 static __always_inline void __nodes_clear(nodemask_t *dstp, unsigned int nbits)
 {
 	bitmap_zero(dstp->bits, nbits);
@@ -181,7 +184,7 @@ static __always_inline bool __node_test_and_set(int node, nodemask_t *addr)
 }
 
 #define nodes_and(dst, src1, src2) \
-			__nodes_and(&(dst), &(src1), &(src2), MAX_NUMNODES)
+			__nodes_and(&(dst), &(src1), &(src2), small_nodemask_bits)
 static __always_inline bool __nodes_and(nodemask_t *dstp, const nodemask_t *src1p,
 					const nodemask_t *src2p, unsigned int nbits)
 {
@@ -189,7 +192,7 @@ static __always_inline bool __nodes_and(nodemask_t *dstp, const nodemask_t *src1
 }
 
 #define nodes_or(dst, src1, src2) \
-			__nodes_or(&(dst), &(src1), &(src2), MAX_NUMNODES)
+			__nodes_or(&(dst), &(src1), &(src2), small_nodemask_bits)
 static __always_inline void __nodes_or(nodemask_t *dstp, const nodemask_t *src1p,
 					const nodemask_t *src2p, unsigned int nbits)
 {
@@ -197,7 +200,7 @@ static __always_inline void __nodes_or(nodemask_t *dstp, const nodemask_t *src1p
 }
 
 #define nodes_xor(dst, src1, src2) \
-			__nodes_xor(&(dst), &(src1), &(src2), MAX_NUMNODES)
+			__nodes_xor(&(dst), &(src1), &(src2), small_nodemask_bits)
 static __always_inline void __nodes_xor(nodemask_t *dstp, const nodemask_t *src1p,
 					const nodemask_t *src2p, unsigned int nbits)
 {
@@ -205,14 +208,14 @@ static __always_inline void __nodes_xor(nodemask_t *dstp, const nodemask_t *src1
 }
 
 #define nodes_andnot(dst, src1, src2) \
-			__nodes_andnot(&(dst), &(src1), &(src2), MAX_NUMNODES)
+			__nodes_andnot(&(dst), &(src1), &(src2), small_nodemask_bits)
 static __always_inline bool __nodes_andnot(nodemask_t *dstp, const nodemask_t *src1p,
 					const nodemask_t *src2p, unsigned int nbits)
 {
 	return bitmap_andnot(dstp->bits, src1p->bits, src2p->bits, nbits);
 }
 
-#define nodes_copy(dst, src) __nodes_copy(&(dst), &(src), MAX_NUMNODES)
+#define nodes_copy(dst, src) __nodes_copy(&(dst), &(src), large_nodemask_bits)
 static __always_inline void __nodes_copy(nodemask_t *dstp,
 					const nodemask_t *srcp, unsigned int nbits)
 {
@@ -230,15 +233,19 @@ static __always_inline void __nodes_copy_and_extend(nodemask_t *dstp,
 }
 
 #define nodes_complement(dst, src) \
-			__nodes_complement(&(dst), &(src), MAX_NUMNODES)
+			__nodes_complement(&(dst), &(src), nr_node_ids)
 static __always_inline void __nodes_complement(nodemask_t *dstp,
 					const nodemask_t *srcp, unsigned int nbits)
 {
+	if (small_const_nbits(small_nodemask_bits)) {
+		dstp->bits[0] = ~srcp->bits[0] & BITMAP_LAST_WORD_MASK(nbits);
+		return;
+	}
 	bitmap_complement(dstp->bits, srcp->bits, nbits);
 }
 
 #define nodes_equal(src1, src2) \
-			__nodes_equal(&(src1), &(src2), MAX_NUMNODES)
+			__nodes_equal(&(src1), &(src2), small_nodemask_bits)
 static __always_inline bool __nodes_equal(const nodemask_t *src1p,
 					const nodemask_t *src2p, unsigned int nbits)
 {
@@ -246,7 +253,7 @@ static __always_inline bool __nodes_equal(const nodemask_t *src1p,
 }
 
 #define nodes_intersects(src1, src2) \
-			__nodes_intersects(&(src1), &(src2), MAX_NUMNODES)
+			__nodes_intersects(&(src1), &(src2), small_nodemask_bits)
 static __always_inline bool __nodes_intersects(const nodemask_t *src1p,
 					const nodemask_t *src2p, unsigned int nbits)
 {
@@ -254,53 +261,50 @@ static __always_inline bool __nodes_intersects(const nodemask_t *src1p,
 }
 
 #define nodes_subset(src1, src2) \
-			__nodes_subset(&(src1), &(src2), MAX_NUMNODES)
+			__nodes_subset(&(src1), &(src2), small_nodemask_bits)
 static __always_inline bool __nodes_subset(const nodemask_t *src1p,
 					const nodemask_t *src2p, unsigned int nbits)
 {
 	return bitmap_subset(src1p->bits, src2p->bits, nbits);
 }
 
-#define nodes_empty(src) __nodes_empty(&(src), MAX_NUMNODES)
+#define nodes_empty(src) __nodes_empty(&(src), small_nodemask_bits)
 static __always_inline bool __nodes_empty(const nodemask_t *srcp, unsigned int nbits)
 {
 	return bitmap_empty(srcp->bits, nbits);
 }
 
-#define nodes_full(nodemask) __nodes_full(&(nodemask), MAX_NUMNODES)
+#define nodes_full(nodemask) __nodes_full(&(nodemask), nr_node_ids)
 static __always_inline bool __nodes_full(const nodemask_t *srcp, unsigned int nbits)
 {
 	return bitmap_full(srcp->bits, nbits);
 }
 
-#define nodes_weight(nodemask) __nodes_weight(&(nodemask), MAX_NUMNODES)
+#define nodes_weight(nodemask) __nodes_weight(&(nodemask), small_nodemask_bits)
 static __always_inline int __nodes_weight(const nodemask_t *srcp, unsigned int nbits)
 {
 	return bitmap_weight(srcp->bits, nbits);
 }
 
-/* FIXME: better would be to fix all architectures to never return
-          > MAX_NUMNODES, then the silly min()s could be dropped. */
-
-#define first_node(src) __first_node(&(src), MAX_NUMNODES)
+#define first_node(src) __first_node(&(src), small_nodemask_bits)
 static __always_inline unsigned int __first_node(const nodemask_t *srcp,
 						unsigned int nbits)
 {
-	return min(nbits, find_first_bit(srcp->bits, nbits));
+	return find_first_bit(srcp->bits, nbits);
 }
 
-#define next_node(n, src) __next_node((n), &(src), MAX_NUMNODES)
+#define next_node(n, src) __next_node((n), &(src), small_nodemask_bits)
 static __always_inline unsigned int __next_node(int n, const nodemask_t *srcp,
 					       unsigned int nbits)
 {
-	return min(nbits, find_next_bit(srcp->bits, nbits, n + 1));
+	return find_next_bit(srcp->bits, nbits, n + 1);
 }
 
 /*
  * Find the next present node in src, starting after node n, wrapping around to
- * the first node in src if needed.  Returns MAX_NUMNODES if src is empty.
+ * the first node in src if needed.  Returns >= nr_node_ids if src is empty.
  */
-#define next_node_in(n, src) __next_node_in((n), &(src), MAX_NUMNODES)
+#define next_node_in(n, src) __next_node_in((n), &(src), small_nodemask_bits)
 static __always_inline unsigned int __next_node_in(int node, const nodemask_t *srcp,
 						  unsigned int nbits)
 {
@@ -331,7 +335,7 @@ static __always_inline void init_nodemask_of_node(nodemask_t *mask, int node)
 #define first_unset_node(mask) __first_unset_node(&(mask))
 static __always_inline unsigned int __first_unset_node(const nodemask_t *maskp)
 {
-	return min(MAX_NUMNODES, find_first_zero_bit(maskp->bits, MAX_NUMNODES));
+	return find_first_zero_bit(maskp->bits, small_nodemask_bits);
 }
 
 #define NODE_MASK_LAST_WORD BITMAP_LAST_WORD_MASK(MAX_NUMNODES)
@@ -361,21 +365,21 @@ static __always_inline unsigned int __first_unset_node(const nodemask_t *maskp)
 #define nodes_addr(src) ((src).bits)
 
 #define nodemask_parse_user(ubuf, ulen, dst) \
-		__nodemask_parse_user((ubuf), (ulen), &(dst), MAX_NUMNODES)
+		__nodemask_parse_user((ubuf), (ulen), &(dst), nr_node_ids)
 static __always_inline int __nodemask_parse_user(const char __user *buf, int len,
 					nodemask_t *dstp, int nbits)
 {
 	return bitmap_parse_user(buf, len, dstp->bits, nbits);
 }
 
-#define nodelist_parse(buf, dst) __nodelist_parse((buf), &(dst), MAX_NUMNODES)
+#define nodelist_parse(buf, dst) __nodelist_parse((buf), &(dst), nr_node_ids)
 static __always_inline int __nodelist_parse(const char *buf, nodemask_t *dstp, int nbits)
 {
 	return bitmap_parselist(buf, dstp->bits, nbits);
 }
 
 #define node_remap(oldbit, old, new) \
-		__node_remap((oldbit), &(old), &(new), MAX_NUMNODES)
+		__node_remap((oldbit), &(old), &(new), nr_node_ids)
 static __always_inline int __node_remap(int oldbit,
 		const nodemask_t *oldp, const nodemask_t *newp, int nbits)
 {
@@ -383,7 +387,7 @@ static __always_inline int __node_remap(int oldbit,
 }
 
 #define nodes_remap(dst, src, old, new) \
-		__nodes_remap(&(dst), &(src), &(old), &(new), MAX_NUMNODES)
+		__nodes_remap(&(dst), &(src), &(old), &(new), nr_node_ids)
 static __always_inline void __nodes_remap(nodemask_t *dstp, const nodemask_t *srcp,
 		const nodemask_t *oldp, const nodemask_t *newp, int nbits)
 {
@@ -391,7 +395,7 @@ static __always_inline void __nodes_remap(nodemask_t *dstp, const nodemask_t *sr
 }
 
 #define nodes_onto(dst, orig, relmap) \
-		__nodes_onto(&(dst), &(orig), &(relmap), MAX_NUMNODES)
+		__nodes_onto(&(dst), &(orig), &(relmap), nr_node_ids)
 static __always_inline void __nodes_onto(nodemask_t *dstp, const nodemask_t *origp,
 		const nodemask_t *relmapp, int nbits)
 {
@@ -399,7 +403,7 @@ static __always_inline void __nodes_onto(nodemask_t *dstp, const nodemask_t *ori
 }
 
 #define nodes_fold(dst, orig, sz) \
-		__nodes_fold(&(dst), &(orig), sz, MAX_NUMNODES)
+		__nodes_fold(&(dst), &(orig), sz, nr_node_ids)
 static __always_inline void __nodes_fold(nodemask_t *dstp, const nodemask_t *origp,
 		int sz, int nbits)
 {
@@ -409,7 +413,7 @@ static __always_inline void __nodes_fold(nodemask_t *dstp, const nodemask_t *ori
 #if MAX_NUMNODES > 1
 #define for_each_node_mask(node, mask)				    \
 	for ((node) = first_node(mask);				    \
-	     (node) < MAX_NUMNODES;				    \
+	     (node) < small_nodemask_bits;			    \
 	     (node) = next_node((node), (mask)))
 #else /* MAX_NUMNODES == 1 */
 #define for_each_node_mask(node, mask)                                  \
@@ -524,9 +528,9 @@ static __always_inline int num_node_state(enum node_states state)
 static __always_inline int node_random(const nodemask_t *maskp)
 {
 #if defined(CONFIG_NUMA) && (MAX_NUMNODES > 1)
-	int node = find_random_bit(maskp->bits, MAX_NUMNODES);
+	int node = find_random_bit(maskp->bits, nr_node_ids);
 
-	return node < MAX_NUMNODES ? node : NUMA_NO_NODE;
+	return node < nr_node_ids ? node : NUMA_NO_NODE;
 #else
 	return 0;
 #endif
-- 
2.53.0


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

end of thread, other threads:[~2026-11-27 23:02 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 23:01 [PATCH 00/11] nodemask: switch to runtime nr_node_ids Yury Norov
2026-11-27 23:01 ` [PATCH 01/11] nodemask: add 'small' and 'large' compile-time sizes Yury Norov
2026-11-27 23:01 ` [PATCH 02/11] nodemask: pass nodemask length to node search functions Yury Norov
2026-11-27 23:01 ` [PATCH 03/11] mm/mempolicy: reject migrate_pages source IDs beyond nr_node_ids Yury Norov
2026-11-27 23:01 ` [PATCH 04/11] mm/mempolicy: preserve full-width userspace nodemasks Yury Norov
2026-11-27 23:01 ` [PATCH 05/11] mm: switch to runtime nodemask length Yury Norov
2026-11-27 23:01 ` [PATCH 06/11] MIPS: IP27: switch nmi_dump " Yury Norov
2026-11-27 23:01 ` [PATCH 07/11] ACPI: NUMA: switch acpi_map_pxm_to_node " Yury Norov
2026-11-27 23:01 ` [PATCH 08/11] mtip32xx: switch next_online_node() " Yury Norov
2026-11-27 23:01 ` [PATCH 09/11] nodemask: add nodes_copy_and_extend() Yury Norov
2026-11-27 23:01 ` [PATCH 10/11] sched/numa: bound cpuset trace masks by nr_node_ids Yury Norov
2026-11-27 23:01 ` [PATCH 11/11] nodemask: add cpumask-style operation size bounds Yury Norov

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