[PATCH v2 2/2] mm/mempolicy: stop copying the nodemask in the interleave paths

From: Gregory Price

Date: Thu Sep 17 2026 - 20:23:37 EST


The interleave node selectors copy pol->nodes onto the stack so the mask
cannot change while they walk it. nodemask_t is 128 bytes at
MAX_NUMNODES=1024, and two of the three run per folio fault.

The copy only buys consistency between the node count and the walk.
Drop the consistency and just bounds check the walk instead.

The nodelist access is racy by design, but safe as long as we handle
the scenario where a cpuset rebind causes a torn nodemask read to
perceive the nodemask as empty (weight_total == 0).

If an empty nodelist or weight is perceived, fall back to numa_node_id(),
which is what the functions already did when the copy came back empty

Otherwise, iterating the nodelist during the actual allocation loop
is perfectly safe - a concurrent rebind may simply cause a skew in
in the distribution of memory (or fail and fall back the same as any
other error condition).

weighted_interleave_nid() counts the nodes as we sum the weights. We use
that node count to limit the maximum skew a single node can host.

interleave_nid() walks with next_node_in() rather than next_node(), so a
mask that shrank mid-walk wraps to a node still in the policy.

alloc_pages_bulk_weighted_interleave() derives per-node counts from a
weight total summed over the mask, so a changing mask can make them exceed
the request. Clamp each chunk to the space left in page_array.

A cpuset cookie will not work here: two of these take VMA policies, which
mpol_rebind_mm() rebinds under mmap_write_lock(), not mems_allowed_seq.

Cost is distribution accuracy during a rebind - but the copy never
corrected this anyway, it was just a safety mechanism to prevent div/0
and overrunning the alloc request buffer.

Remove read_once_policy_nodemask(), now unused.

-fstack-usage at MAX_NUMNODES=1024:

weighted_interleave_nid 184 -> 56
interleave_nid 168 -> 32
alloc_pages_bulk_mempolicy_noprof 360 -> 136

Assisted-by: LLM
Signed-off-by: Gregory Price (Meta) <gourry@xxxxxxxxxx>
Acked-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
Reviewed-by: Rakie Kim <rakie.kim@xxxxxx>
---
mm/mempolicy.c | 93 ++++++++++++++++++++++++++++++--------------------
1 file changed, 56 insertions(+), 37 deletions(-)

diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index 2643915dc966..fd97fb0289bc 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -2197,34 +2197,15 @@ unsigned int mempolicy_slab_node(void)
}
}

-static unsigned int read_once_policy_nodemask(struct mempolicy *pol,
- nodemask_t *mask)
-{
- /*
- * barrier stabilizes the nodemask locally so that it can be iterated
- * over safely without concern for changes. Allocators validate node
- * selection does not violate mems_allowed, so this is safe.
- */
- barrier();
- memcpy(mask, &pol->nodes, sizeof(nodemask_t));
- barrier();
- return nodes_weight(*mask);
-}
-
static unsigned int weighted_interleave_nid(struct mempolicy *pol, pgoff_t ilx)
{
struct weighted_interleave_state *state;
- nodemask_t nodemask;
- unsigned int target, nr_nodes;
+ unsigned int target, nnodes = 0;
u8 *table = NULL;
unsigned int weight_total = 0;
u8 weight;
int nid = 0;

- nr_nodes = read_once_policy_nodemask(pol, &nodemask);
- if (!nr_nodes)
- return numa_node_id();
-
rcu_read_lock();

state = rcu_dereference(wi_state);
@@ -2232,22 +2213,45 @@ static unsigned int weighted_interleave_nid(struct mempolicy *pol, pgoff_t ilx)
if (state)
table = state->iw_table;

- /* calculate the total weight */
- for_each_node_mask(nid, nodemask)
+ /* calculate the total weight and the node count */
+ for_each_node_mask(nid, pol->nodes) {
weight_total += table ? table[nid] : 1;
+ nnodes++;
+ }
+
+ /* the mask is empty */
+ if (!weight_total) {
+ rcu_read_unlock();
+ return numa_node_id();
+ }

/* Calculate the node offset based on totals */
target = ilx % weight_total;
- nid = first_node(nodemask);
- while (target) {
+ nid = first_node(pol->nodes);
+
+ /*
+ * The target was calculated in a separate loop, and a concurrent
+ * rebind can change the contents of pol->nodes as we calculate.
+ * Access is safe, in the worst case we suddenly perceive an empty
+ * nodemask and return numa_node_id() below - otherwise we may
+ * simply cause a skew in allocations.
+ *
+ * Clamp this loop to a single pass (nnodes) to keep the walk
+ * bounded by node count.
+ */
+ while (target && nnodes-- && nid < MAX_NUMNODES) {
/* detect system default usage */
weight = table ? table[nid] : 1;
if (target < weight)
break;
target -= weight;
- nid = next_node_in(nid, nodemask);
+ nid = next_node_in(nid, pol->nodes);
}
rcu_read_unlock();
+
+ /* the mask emptied under the walk */
+ if (nid >= MAX_NUMNODES)
+ return numa_node_id();
return nid;
}

@@ -2258,18 +2262,23 @@ static unsigned int weighted_interleave_nid(struct mempolicy *pol, pgoff_t ilx)
*/
static unsigned int interleave_nid(struct mempolicy *pol, pgoff_t ilx)
{
- nodemask_t nodemask;
unsigned int target, nnodes;
int i;
int nid;

- nnodes = read_once_policy_nodemask(pol, &nodemask);
+ nnodes = nodes_weight(pol->nodes);
if (!nnodes)
return numa_node_id();
target = ilx % nnodes;
- nid = first_node(nodemask);
- for (i = 0; i < target; i++)
- nid = next_node(nid, nodemask);
+ 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++)
+ nid = next_node_in(nid, pol->nodes);
+
+ /* the mask emptied under the walk */
+ if (nid >= MAX_NUMNODES)
+ return numa_node_id();
return nid;
}

@@ -2665,7 +2674,6 @@ static unsigned long alloc_pages_bulk_weighted_interleave(gfp_t gfp,
u8 *table, weight;
unsigned int weight_total = 0;
unsigned long rem_pages = nr_pages;
- nodemask_t nodes;
int nnodes, node;
int resume_node = MAX_NUMNODES - 1;
u8 resume_weight = 0;
@@ -2675,10 +2683,10 @@ static unsigned long alloc_pages_bulk_weighted_interleave(gfp_t gfp,
if (!nr_pages)
return 0;

- /* read the nodes onto the stack, retry if done during rebind */
+ /* count the nodes, retry if a rebind happened during the read */
do {
cpuset_mems_cookie = read_mems_allowed_begin();
- nnodes = read_once_policy_nodemask(pol, &nodes);
+ nnodes = nodes_weight(pol->nodes);
} while (read_mems_allowed_retry(cpuset_mems_cookie));

/* if the nodemask has become invalid, we cannot do anything */
@@ -2688,7 +2696,7 @@ static unsigned long alloc_pages_bulk_weighted_interleave(gfp_t gfp,
/* Continue allocating from most recent node and adjust the nr_pages */
node = me->il_prev;
weight = me->il_weight;
- if (weight && node_isset(node, nodes)) {
+ if (weight && node_isset(node, pol->nodes)) {
node_pages = min(rem_pages, weight);
nr_allocated = __alloc_pages_bulk(gfp, node, NULL, node_pages,
page_array);
@@ -2712,9 +2720,13 @@ static unsigned long alloc_pages_bulk_weighted_interleave(gfp_t gfp,
table = state ? state->iw_table : NULL;

/* calculate total, detect system default usage */
- for_each_node_mask(node, nodes)
+ for_each_node_mask(node, pol->nodes)
weight_total += table ? table[node] : 1;

+ /* the mask emptied since it was counted */
+ if (!weight_total)
+ goto out;
+
/*
* Calculate rounds/partial rounds to minimize __alloc_pages_bulk calls.
* Track which node weighted interleave should resume from.
@@ -2724,10 +2736,14 @@ 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, nodes);
+ resume_node = next_node_in(prev_node, pol->nodes);
+ if (resume_node >= MAX_NUMNODES)
+ goto out;
resume_weight = table ? table[resume_node] : 1;
for (i = 0; i < nnodes; i++) {
- node = next_node_in(prev_node, nodes);
+ node = next_node_in(prev_node, pol->nodes);
+ if (node >= MAX_NUMNODES)
+ break;
weight = table ? table[node] : 1;
node_pages = weight * rounds;
/* If a delta exists, add this node's portion of the delta */
@@ -2744,6 +2760,8 @@ static unsigned long alloc_pages_bulk_weighted_interleave(gfp_t gfp,
/* node_pages can be 0 if an allocation fails and rounds == 0 */
if (!node_pages)
break;
+ /* a rebind can invalidate the counts: never overrun page_array */
+ node_pages = min(node_pages, nr_pages - total_allocated);
nr_allocated = __alloc_pages_bulk(gfp, node, NULL, node_pages,
page_array);
page_array += nr_allocated;
@@ -2754,6 +2772,7 @@ static unsigned long alloc_pages_bulk_weighted_interleave(gfp_t gfp,
}
me->il_prev = resume_node;
me->il_weight = resume_weight;
+out:
srcu_read_unlock_fast(&wi_srcu, scp);
return total_allocated;
}
--
2.55.0