[PATCH 34/60] mm/mempolicy: don't rebind policies onto private nodes
From: Gregory Price via B4 Relay
Date: Fri Nov 20 2026 - 13:18:27 EST
From: "Gregory Price (Meta)" <gourry@xxxxxxxxxx>
A cpuset can allow private nodes, and rebinding a policy to a new cpuset
mask can move its nodes onto them:
- a static policy reapplies the raw user mask.
- relative and remapped policies follow node positions in the cpuset
mask.
Ordinary policies cannot use private nodes.
Rebind policies over the cpuset's nodes other than private ones, and
record the remap base the same way, so adding or removing a private node
moves no policy.
Cpusets keep common memory in every effective mask, so a mask with no
node left is a bug. Warn, and keep the whole mask rather than leave the
policy empty.
On systems without a private node, rebinding is unchanged.
Signed-off-by: Gregory Price (Meta) <gourry@xxxxxxxxxx>
---
mm/mempolicy.c | 42 ++++++++++++++++++++++++++++++------------
1 file changed, 30 insertions(+), 12 deletions(-)
diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index e208018499d6..81ccc481cea1 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -418,6 +418,18 @@ static int mpol_new_preferred(struct mempolicy *pol, const nodemask_t *nodes)
return 0;
}
+/*
+ * Policies are rebound over the cpuset's nodes other than private ones. A node
+ * removed since the remap base was recorded keeps its position.
+ */
+static void mpol_rebindable_mems(nodemask_t *dst, const nodemask_t *nodes)
+{
+ nodes_andnot(*dst, *nodes, node_states[N_MEMORY_PRIVATE]);
+ /* Cpusets keep common memory in every effective mask. */
+ if (WARN_ON_ONCE(nodes_empty(*dst)))
+ *dst = *nodes;
+}
+
/*
* mpol_set_nodemask is called after mpol_new() to set up the nodemask, if
* any, for the new policy. mpol_new() has already validated the nodes
@@ -453,7 +465,8 @@ static int mpol_set_nodemask(struct mempolicy *pol,
if (mpol_store_user_nodemask(pol))
pol->w.user_nodemask = *nodes;
else
- pol->w.cpuset_mems_allowed = cpuset_current_mems_allowed;
+ mpol_rebindable_mems(&pol->w.cpuset_mems_allowed,
+ &cpuset_current_mems_allowed);
ret = mpol_ops[pol->mode].create(pol, &nsc->mask2);
return ret;
@@ -528,20 +541,22 @@ static void mpol_rebind_default(struct mempolicy *pol, const nodemask_t *nodes)
static void mpol_rebind_nodemask(struct mempolicy *pol, const nodemask_t *nodes)
{
- nodemask_t tmp;
+ nodemask_t tmp, allowed;
+
+ mpol_rebindable_mems(&allowed, nodes);
- if (pol->flags & MPOL_F_STATIC_NODES)
- nodes_and(tmp, pol->w.user_nodemask, *nodes);
- else if (pol->flags & MPOL_F_RELATIVE_NODES)
- mpol_relative_nodemask(&tmp, &pol->w.user_nodemask, nodes);
- else {
+ if (pol->flags & MPOL_F_STATIC_NODES) {
+ nodes_and(tmp, pol->w.user_nodemask, allowed);
+ } else if (pol->flags & MPOL_F_RELATIVE_NODES) {
+ mpol_relative_nodemask(&tmp, &pol->w.user_nodemask, &allowed);
+ } else {
nodes_remap(tmp, pol->nodes, pol->w.cpuset_mems_allowed,
- *nodes);
- pol->w.cpuset_mems_allowed = *nodes;
+ allowed);
+ pol->w.cpuset_mems_allowed = allowed;
}
if (nodes_empty(tmp))
- tmp = *nodes;
+ tmp = allowed;
pol->nodes = tmp;
}
@@ -549,7 +564,7 @@ static void mpol_rebind_nodemask(struct mempolicy *pol, const nodemask_t *nodes)
static void mpol_rebind_preferred(struct mempolicy *pol,
const nodemask_t *nodes)
{
- pol->w.cpuset_mems_allowed = *nodes;
+ mpol_rebindable_mems(&pol->w.cpuset_mems_allowed, nodes);
}
/*
@@ -561,10 +576,13 @@ static void mpol_rebind_preferred(struct mempolicy *pol,
*/
static void mpol_rebind_policy(struct mempolicy *pol, const nodemask_t *newmask)
{
+ nodemask_t mems;
+
if (!pol || pol->mode == MPOL_LOCAL)
return;
+ mpol_rebindable_mems(&mems, newmask);
if (!mpol_store_user_nodemask(pol) &&
- nodes_equal(pol->w.cpuset_mems_allowed, *newmask))
+ nodes_equal(pol->w.cpuset_mems_allowed, mems))
return;
mpol_ops[pol->mode].rebind(pol, newmask);
--
2.56.1