Skip to contentSkip to conversation

mm: don't allow empty relative nodemask in mpol_relative_nodemask()

PATCH In review Yury Norov 9 participants 41 messages May 28 – Jun 29

Subsystems (5)
lore
Share & tools
lore

Yury Norov

author
#0 lore raw

PATCH 1/1 mm: don't allow empty relative nodemask in mpol_relative_nodemask()

Reassigning nodes relative an empty user-provided nodemask is useless, and triggers divide-by-zero in the function.

---
 mm/mempolicy.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
mm/mempolicy.c +6 -1
index 4e4421b22b59..cd961fa1eb33 100644
@@ -370,8 +370,13 @@ static inline int mpol_store_user_nodemask(const struct mempolicy *pol)
 static void mpol_relative_nodemask(nodemask_t *ret, const nodemask_t *orig,
 				   const nodemask_t *rel)
 {
+	unsigned int w = nodes_weight(*rel);
 	nodemask_t tmp;
-	nodes_fold(tmp, *orig, nodes_weight(*rel));
+
+	if (w == 0)
+		return -EINVAL;
+
+	nodes_fold(tmp, *orig, w);
 	nodes_onto(*ret, tmp, *rel);
 }
 
Signature · 1 line
2.51.0
maintainer
#1 lore raw
Quoted 19 lines · Yury Norov

Reassigning nodes relative an empty user-provided nodemask is useless, and triggers divide-by-zero in the function.

---
  mm/mempolicy.c | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
mm/mempolicy.c +1 -0
index 4e4421b22b59..cd961fa1eb33 100644
@@ -370,8 +370,13 @@ static inline int mpol_store_user_nodemask(const struct mempolicy *pol)
  static void mpol_relative_nodemask(nodemask_t *ret, const nodemask_t *orig,
  				   const nodemask_t *rel)
  {
+	unsigned int w = nodes_weight(*rel);

mm/mempolicy.c static inline int mpol_store_user_nodemask(const struct mempolicy *pol) · line 382

Quoted 10 lines
mm/mempolicy.c +5 -1
  	nodemask_t tmp;
-	nodes_fold(tmp, *orig, nodes_weight(*rel));
+
+	if (w == 0)
+		return -EINVAL;
+
+	nodes_fold(tmp, *orig, w);
  	nodes_onto(*ret, tmp, *rel);
  }
 

mpol_relative_nodemask() is a void function, so this code should fail compilation. Right?

Cheers,
Longman

author
#2 lore raw
Quoted 24 lines · Waiman Long
Quoted 23 lines · Yury Norov

Reassigning nodes relative an empty user-provided nodemask is useless, and triggers divide-by-zero in the function.

---
  mm/mempolicy.c | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
mm/mempolicy.c +3 -1
index 4e4421b22b59..cd961fa1eb33 100644
@@ -370,8 +370,13 @@ static inline int mpol_store_user_nodemask(const struct mempolicy *pol)
  static void mpol_relative_nodemask(nodemask_t *ret, const nodemask_t *orig,
  				   const nodemask_t *rel)
  {
+	unsigned int w = nodes_weight(*rel);
  	nodemask_t tmp;
-	nodes_fold(tmp, *orig, nodes_weight(*rel));
+
+	if (w == 0)
Quoted 8 lines

mm/mempolicy.c static inline int mpol_store_user_nodemask(const struct mempolicy *pol) · line 381

Quoted 5 lines
mm/mempolicy.c +3 -0
+		return -EINVAL;
+
+	nodes_fold(tmp, *orig, w);
  	nodes_onto(*ret, tmp, *rel);
  }

mpol_relative_nodemask() is a void function, so this code should fail compilation. Right?

Apologize, submitted the wrong file. Will resend shortly.

#3 lore raw
In reply to Yury Norov
Quoted 1 line · Yury Norov

static void mpol_relative_nodemask(nodemask_t *ret, const nodemask_t *orig,

          ^^^^

mm/mempolicy.c static inline int mpol_store_user_nodemask(const struct mempolicy *pol) · line 377

Quoted 8 lines
mm/mempolicy.c +4 -1
 				   const nodemask_t *rel)
 {
+	unsigned int w = nodes_weight(*rel);
 	nodemask_t tmp;
-	nodes_fold(tmp, *orig, nodes_weight(*rel));
+
+	if (w == 0)
+		return -EINVAL;

... this doesn't even compile.

maintainer
#4 lore raw
In reply to Yury Norov
Quoted 5 lines · On Thu, 28 May 2026 15:03:37 -0400 Yury Norov <[email protected]> wrote:

Reassigning nodes relative an empty user-provided nodemask is useless, and triggers divide-by-zero in the function.

Thanks both.

It looks like this is very old code, so we'll be wanting a cc:stable in this.

Quoted 6 lines
mm/mempolicy.c +0 -0
@@ -370,8 +370,13 @@ static inline int mpol_store_user_nodemask(const struct mempolicy *pol)
 static void mpol_relative_nodemask(nodemask_t *ret, const nodemask_t *orig,
 				   const nodemask_t *rel)
 {

mm/mempolicy.c static inline int mpol_store_user_nodemask(const struct mempolicy *pol) · line 381

Quoted 10 lines
mm/mempolicy.c +6 -1
+	unsigned int w = nodes_weight(*rel);
 	nodemask_t tmp;
-	nodes_fold(tmp, *orig, nodes_weight(*rel));
+
+	if (w == 0)
+		return -EINVAL;
+
+	nodes_fold(tmp, *orig, w);
 	nodes_onto(*ret, tmp, *rel);
 }

I suspect we should address this at the mpol level - it should never have got that far. Hopefully the mempolicy maintainers can have a think.

reviewer
#5 lore raw
Quoted 25 lines · On Thu, 28 May 2026 12:41:33 -0700 Andrew Morton <[email protected]>…
Quoted 5 lines · On Thu, 28 May 2026 15:03:37 -0400 Yury Norov <[email protected]> wrote:

Reassigning nodes relative an empty user-provided nodemask is useless, and triggers divide-by-zero in the function.

Thanks both.

It looks like this is very old code, so we'll be wanting a cc:stable in this.

Quoted 12 lines
mm/mempolicy.c +4 -1
@@ -370,8 +370,13 @@ static inline int mpol_store_user_nodemask(const struct mempolicy *pol)
 static void mpol_relative_nodemask(nodemask_t *ret, const nodemask_t *orig,
 				   const nodemask_t *rel)
 {
+	unsigned int w = nodes_weight(*rel);
 	nodemask_t tmp;
-	nodes_fold(tmp, *orig, nodes_weight(*rel));
+
+	if (w == 0)
+		return -EINVAL;
Quoted 8 lines

mm/mempolicy.c static inline int mpol_store_user_nodemask(const struct mempolicy *pol) · line 381

Quoted 4 lines
mm/mempolicy.c +2 -0
+
+	nodes_fold(tmp, *orig, w);
 	nodes_onto(*ret, tmp, *rel);
 }

I suspect we should address this at the mpol level - it should never have got that far. Hopefully the mempolicy maintainers can have a think.

Hello Andrew, hello Yury,

I agree with Andrew here.
mpol_relative_nodemask is called from two places, the first being mpol_rebind_nodemask which is the calling function seen in the bug report as well.

The other place is mpol_set_nodemask, which has a helpful comment that notes:
"mpol_set_nodemask is called after mpol_new() [...snip...] mpol_new() has already validated the nodes parameter with respect to the policy mode and flags".

So it seems like we are missing the big if-else if-else if block from mpol_new in other places that should in fact have it, like mpol_rebind_nodemask.

The approach proposed here of just checking whether the node weight is 0 won't work for a few cases, namely for MPOL_DEFAULT and MPOL_PREFERRED where empty nodemasks are actually allowed. So what should really be done here is to do the full policy-nodemask checking section in mpol_new and call that from mpol_set_nodemask as well.

Thank you for taking a shot at fixing the bug report, please let me know what you think! Have a great day : -)
Joshua

author
#6 lore raw
Quoted 51 lines · Joshua Hahn
Quoted 25 lines · On Thu, 28 May 2026 12:41:33 -0700 Andrew Morton <[email protected]>…
Quoted 5 lines · On Thu, 28 May 2026 15:03:37 -0400 Yury Norov <[email protected]> wrote:

Reassigning nodes relative an empty user-provided nodemask is useless, and triggers divide-by-zero in the function.

Thanks both.

It looks like this is very old code, so we'll be wanting a cc:stable in this.

Quoted 12 lines
mm/mempolicy.c +4 -1
@@ -370,8 +370,13 @@ static inline int mpol_store_user_nodemask(const struct mempolicy *pol)
 static void mpol_relative_nodemask(nodemask_t *ret, const nodemask_t *orig,
 				   const nodemask_t *rel)
 {
+	unsigned int w = nodes_weight(*rel);
 	nodemask_t tmp;
-	nodes_fold(tmp, *orig, nodes_weight(*rel));
+
+	if (w == 0)
+		return -EINVAL;
Quoted 8 lines

mm/mempolicy.c static inline int mpol_store_user_nodemask(const struct mempolicy *pol) · line 381

Quoted 4 lines
mm/mempolicy.c +2 -0
+
+	nodes_fold(tmp, *orig, w);
 	nodes_onto(*ret, tmp, *rel);
 }

I suspect we should address this at the mpol level - it should never have got that far. Hopefully the mempolicy maintainers can have a think.

Hello Andrew, hello Yury,

I agree with Andrew here.
mpol_relative_nodemask is called from two places, the first being mpol_rebind_nodemask which is the calling function seen in the bug report as well.

The other place is mpol_set_nodemask, which has a helpful comment that notes:
"mpol_set_nodemask is called after mpol_new() [...snip...] mpol_new() has already validated the nodes parameter with respect to the policy mode and flags".

So it seems like we are missing the big if-else if-else if block from mpol_new in other places that should in fact have it, like mpol_rebind_nodemask.

Quoted 8 lines

The approach proposed here of just checking whether the node weight is 0 won't work for a few cases, namely for MPOL_DEFAULT and MPOL_PREFERRED where empty nodemasks are actually allowed. So what should really be done here is to do the full policy-nodemask checking section in mpol_new and call that from mpol_set_nodemask as well.

Thank you for taking a shot at fixing the bug report, please let me know what you think! Have a great day : -)

Hi Joshua.

Indeed, quick and dirty shot.

The problem is that nodes_fold() can't work with the sz == 0. In other words, folding to a 0-bit bitmap is an error. We don't check that on bitmaps level because it's an internal helper, and it's a caller's responsibility to validate the parameters.

nodes_onto(), or more specifically bitmap_onto(), is a different story. In case of empty relmap, the function actually clears all the bits in dst and returns.

I see 2 options to move this forward.

1. Simply disallow empty relmap in mpol_relative_nodemask(). There's no valid cases for it, AFAIK, so the nodes_fold() limitation looks reasonable. We can consider it as a new policy.

We've got 2 users for mpol_relative_nodemask(). In mpol_set_nodemask() we can simply propagate the error; and in mpol_rebind_nodemask() we can throw a warning and do nothing.

2. Follow the spirit of the nodes_onto(), and in case of empty relmask, clean the ret mask and bail out

I'm in a favor for the 1st option, because empty relmask looks buggy anyways.

Quoted 3 lines

The approach proposed here of just checking whether the node weight is 0 won't work for a few cases, namely for MPOL_DEFAULT and MPOL_PREFERRED where empty nodemasks are actually allowed.

Not sure I understand this. The mpol_relative_nodemask() is called only if MPOL_F_RELATIVE_NODES is set. In mpol_rebind_nodemask(), if both MPOL_F_STATIC_NODES and MPOL_F_RELATIVE_NODES are set, the former wins. How would the RELATIVE mode mess with the others?

The mpol_new() code seemingly tries to disable empty nodes in case of MPOL_DEFAILT and MPOL_PREFERRED + MPOL_F_RELATIVE_NODES, but obviously it doesn't work very well in the rebind case.

Anyways, I'm not really deep in mempolicy domain, so please educate me if I miss something.

Thanks,
Yury

reviewer
#7 lore raw
Quoted 65 lines · On Fri, 29 May 2026 13:47:12 -0400 Yury Norov <[email protected]> wrote:
Quoted 51 lines · Joshua Hahn
Quoted 25 lines · On Thu, 28 May 2026 12:41:33 -0700 Andrew Morton <[email protected]>…
Quoted 5 lines · On Thu, 28 May 2026 15:03:37 -0400 Yury Norov <[email protected]> wrote:

Reassigning nodes relative an empty user-provided nodemask is useless, and triggers divide-by-zero in the function.

Thanks both.

It looks like this is very old code, so we'll be wanting a cc:stable in this.

Quoted 12 lines
mm/mempolicy.c +4 -1
@@ -370,8 +370,13 @@ static inline int mpol_store_user_nodemask(const struct mempolicy *pol)
 static void mpol_relative_nodemask(nodemask_t *ret, const nodemask_t *orig,
 				   const nodemask_t *rel)
 {
+	unsigned int w = nodes_weight(*rel);
 	nodemask_t tmp;
-	nodes_fold(tmp, *orig, nodes_weight(*rel));
+
+	if (w == 0)
+		return -EINVAL;
Quoted 8 lines

mm/mempolicy.c static inline int mpol_store_user_nodemask(const struct mempolicy *pol) · line 381

Quoted 4 lines
mm/mempolicy.c +2 -0
+
+	nodes_fold(tmp, *orig, w);
 	nodes_onto(*ret, tmp, *rel);
 }

I suspect we should address this at the mpol level - it should never have got that far. Hopefully the mempolicy maintainers can have a think.

Hello Andrew, hello Yury,

I agree with Andrew here.
mpol_relative_nodemask is called from two places, the first being mpol_rebind_nodemask which is the calling function seen in the bug report as well.

The other place is mpol_set_nodemask, which has a helpful comment that notes:
"mpol_set_nodemask is called after mpol_new() [...snip...] mpol_new() has already validated the nodes parameter with respect to the policy mode and flags".

So it seems like we are missing the big if-else if-else if block from mpol_new in other places that should in fact have it, like mpol_rebind_nodemask.

Quoted 8 lines

The approach proposed here of just checking whether the node weight is 0 won't work for a few cases, namely for MPOL_DEFAULT and MPOL_PREFERRED where empty nodemasks are actually allowed. So what should really be done here is to do the full policy-nodemask checking section in mpol_new and call that from mpol_set_nodemask as well.

Thank you for taking a shot at fixing the bug report, please let me know what you think! Have a great day : -)

Hi Joshua.

Indeed, quick and dirty shot.

Quoted 8 lines

The problem is that nodes_fold() can't work with the sz == 0. In other words, folding to a 0-bit bitmap is an error. We don't check that on bitmaps level because it's an internal helper, and it's a caller's responsibility to validate the parameters.

nodes_onto(), or more specifically bitmap_onto(), is a different story. In case of empty relmap, the function actually clears all the bits in dst and returns.

I see, thank you for helping me understand. Yeah, we probably don't want an empty nodemask here regardless of policy, as long as MPOL_F_RELATIVE_NODES is set.

Quoted 9 lines

I see 2 options to move this forward.

1. Simply disallow empty relmap in mpol_relative_nodemask(). There's no valid cases for it, AFAIK, so the nodes_fold() limitation looks reasonable. We can consider it as a new policy.

We've got 2 users for mpol_relative_nodemask(). In mpol_set_nodemask() we can simply propagate the error; and in mpol_rebind_nodemask() we can throw a warning and do nothing.

I think we should never be able to reach mpol_set_nodemask with an empty nodemask if MPOL_F_RELATIVE_NODES is set. Not sure if we need to be extra defensive here.

For mpol_rebind_nodemask I think we should actually do some more checks, I think we should do it in mpol_rebind_policy since it gives us an opportunity to catch other sources of failure too, like calling mpol_rebind_preferred with an empty nodemask as well (which shouldn't be allowed for MPOL_F_{ RELATIVE, STATIC}_NODES) as far as I can tell from the checks in mpol_new.

Setting empty nodemask for mpol_rebind_preferred won't throw a div0 error like for mpol_rebind_nodemask but we can at least throw a warning like you suggested.

Does that make sense? This is your fix and if you would prefer to address only the div0 case, that makes sense too, since the empty nodemask for preferred is more of a semantic incorrectness and will not cause panics. Entirely up to you! : -)

Quoted 6 lines

2. Follow the spirit of the nodes_onto(), and in case of empty relmask, clean the ret mask and bail out

I'm in a favor for the 1st option, because empty relmask looks buggy anyways.

Quoted 8 lines
Quoted 3 lines

The approach proposed here of just checking whether the node weight is 0 won't work for a few cases, namely for MPOL_DEFAULT and MPOL_PREFERRED where empty nodemasks are actually allowed.

Not sure I understand this. The mpol_relative_nodemask() is called only if MPOL_F_RELATIVE_NODES is set. In mpol_rebind_nodemask(), if both MPOL_F_STATIC_NODES and MPOL_F_RELATIVE_NODES are set, the former wins. How would the RELATIVE mode mess with the others?

Yes, you're right, the case that MPOL_DEFAULT and MPOL_PREFERRED allows empty nodemasks is precisely when !STATIC && !RELATIVE :p this is my bad for missing that case completely.

Quoted 2 lines

Anyways, I'm not really deep in mempolicy domain, so please educate me if I miss something.

Thank you, I have also learned a lot looking into this to think about what the best solution is!
Joshua

#8 lore raw
In reply to Yury Norov
Quoted 8 lines
Quoted 3 lines

Thank you for taking a shot at fixing the bug report, please let me know what you think! Have a great day : -)

Hi Joshua.

Indeed, quick and dirty shot.

Quoted 8 lines

The problem is that nodes_fold() can't work with the sz == 0. In other words, folding to a 0-bit bitmap is an error. We don't check that on bitmaps level because it's an internal helper, and it's a caller's responsibility to validate the parameters.

nodes_onto(), or more specifically bitmap_onto(), is a different story. In case of empty relmap, the function actually clears all the bits in dst and returns.

It's very weird that mpol_new_nodemask() (->create() callback) disallows empty nodemasks, but mpol_rebind_nodemask() (->rebind() callback) would allow empty nodemasks.

I guess mpol_set_nodemask() could trigger it after doing the

	nodes_and(nsc->mask1, cpuset_current_mems_allowed,
		  node_states[N_MEMORY]);

And ending with an empty &nsc->mask1.

The later "mpol_ops[pol->mode].create(pol, &nsc->mask2);" would reject it, but the division by zero could still happen.

Quoted 10 lines

I see 2 options to move this forward.

1. Simply disallow empty relmap in mpol_relative_nodemask(). There's no valid cases for it, AFAIK, so the nodes_fold() limitation looks reasonable. We can consider it as a new policy.

We've got 2 users for mpol_relative_nodemask(). In mpol_set_nodemask() we can simply propagate the error; and in mpol_rebind_nodemask() we can throw a warning and do nothing.

Throwing a warning is really bad. We'd still end up with an empty nodemask?

Signature · 3 lines
Cheers,

David
maintainer
#9 lore raw
Quoted 13 lines · David Hildenbrand (Arm)
Quoted 13 lines
Quoted 3 lines

Thank you for taking a shot at fixing the bug report, please let me know what you think! Have a great day : -)

Hi Joshua.

Indeed, quick and dirty shot.

The problem is that nodes_fold() can't work with the sz == 0. In other words, folding to a 0-bit bitmap is an error. We don't check that on bitmaps level because it's an internal helper, and it's a caller's responsibility to validate the parameters.

Quoted 8 lines
Quoted 3 lines

nodes_onto(), or more specifically bitmap_onto(), is a different story. In case of empty relmap, the function actually clears all the bits in dst and returns.

It's very weird that mpol_new_nodemask() (->create() callback) disallows empty nodemasks, but mpol_rebind_nodemask() (->rebind() callback) would allow empty nodemasks.

Was this actually observed?

mpol_rebind_nodemask() happens when cgroup.cpuset changes, and cgroup.cpuset cannot be empty.

cpuset only changes with sysfs twiddles or offlining. In either case, cpuset *guarantees* that cpuset.mems will never be empty.

So... is this an observed bug or just a statically discovered "bug" that can't actually be reached?

~Gregory

#10 lore raw
Quoted 22 lines · Gregory Price
Quoted 10 lines · David Hildenbrand (Arm)
Quoted 10 lines

Hi Joshua.

Indeed, quick and dirty shot.

The problem is that nodes_fold() can't work with the sz == 0. In other words, folding to a 0-bit bitmap is an error. We don't check that on bitmaps level because it's an internal helper, and it's a caller's responsibility to validate the parameters.

Quoted 8 lines
Quoted 3 lines

nodes_onto(), or more specifically bitmap_onto(), is a different story. In case of empty relmap, the function actually clears all the bits in dst and returns.

It's very weird that mpol_new_nodemask() (->create() callback) disallows empty nodemasks, but mpol_rebind_nodemask() (->rebind() callback) would allow empty nodemasks.

Was this actually observed?

Quoted 8 lines

mpol_rebind_nodemask() happens when cgroup.cpuset changes, and cgroup.cpuset cannot be empty.

cpuset only changes with sysfs twiddles or offlining. In either case, cpuset *guarantees* that cpuset.mems will never be empty.

So... is this an observed bug or just a statically discovered "bug" that can't actually be reached?

According to the report [1] syzkaller can trigger it. There is no reproducer, though.

[1]
https://lore.kernel.org/all/CA+0ovCgxbZkXa+OU8w3s84R3KNPNxxRfmsNR-udh+afQBbGNmw@mail.gmail.com/

Signature · 3 lines
Cheers,

David
maintainer
#11 lore raw
Quoted 7 lines · David Hildenbrand (Arm)

According to the report [1] syzkaller can trigger it. There is no reproducer, though.

[1]
https://lore.kernel.org/all/CA+0ovCgxbZkXa+OU8w3s84R3KNPNxxRfmsNR-udh+afQBbGNmw@mail.gmail.com/

The actual implication of this report is that there is a bug in cpuset, not mempolicy.

  mpol_rebind_mm+0x3ab/0x680 mm/mempolicy.c:569
      ^^^ should never receive a 0-node nodemask ^^^
  ...snip...
  cpuset_update_tasks_nodemask+0x22e/0x340 kernel/cgroup/cpuset.c:2777
      ^^^ calls guarantee_online_mems ^^^
  ...snip...
  hotplug_update_tasks kernel/cgroup/cpuset.c:3882 [inline]
  cpuset_hotplug_update_tasks kernel/cgroup/cpuset.c:3985 [inline]

Relevant code:

void cpuset_update_tasks_nodemask(struct cpuset *cs) {
... snip ...
guarantee_online_mems(cs, &newmems); <<< critical call
... snip ...

        while ((task = css_task_iter_next(&it))) {
... snip ...
                mpol_rebind_mm(mm, &cs->mems_allowed);

Seems like maybe mpol_rebind_mm should be called with newmems, not cs->mems_allowed, though cs->mems_allowed should never be allowed to be empty, because that makes no sense.

Just eyeballing it, I can't say whether calling with newmems is the right thing, or if mems_allowed should not be allowed to be empty, would have to dig in a little further.

~Gregory

#12 lore raw

Confirmed, with a standalone reproducer (attached); it panics linus/master at e8c2f9fdadee. cs->mems_allowed can legitimately be empty
on v2 -- a freshly created cpuset child that never had cpuset.mems written keeps mems_allowed empty (never initialized) while effective_mems is inherited non-empty in cpuset_css_online(), and v2 allows attaching tasks to it (the empty-mems guard in cpuset_can_attach_check() is gated on !is_in_v2_mode()). So the non-empty guarantee holds for effective_mems, not for the configured cs->mems_allowed; forbidding empty cpuset.mems would break v2's inherit-from-parent semantics.

The reproducer enables +cpuset, mkdirs a child without writing cpuset.mems, moves a task in, mbind()s a VMA with
MPOL_BIND | MPOL_F_RELATIVE_NODES, and offlines a CPU; the hotplug walk then calls mpol_rebind_mm(mm, &cs->mems_allowed) with the empty mask and folds modulo nodes_weight(*rel) == 0 (console logs attached).

The newmems instinct looks right: it's the effective, online mask the task is actually allowed to use, guarantee_online_mems() keeps it non-empty, and it matches cpuset_attach(), which already rebinds against cs->effective_mems. The fix this implies:

- mpol_rebind_mm(mm, &cs->mems_allowed);
+ mpol_rebind_mm(mm, &newmems);

I built the current base (e8c2f9fdadee) with and without this one-liner:
the unpatched kernel panics on the first cpu1 offline, while the patched kernel runs the reproducer's 8 offline/online cycles cleanly, with no divide error.

This regressed in ae1c802382f7 ("cpuset: apply cs->effective_{cpus,mems}", v3.17), which moved cpuset_attach() to the effective mask but left this rebind on cs->mems_allowed.

Happy to send this as a proper patch (Fixes: ae1c802382f7, Cc: stable, reproducer) if you agree the cpuset side is right, or to test a mempolicy-side fix if not.

Thanks,

Farhad Alemi
PhD Student
SEFCOM Lab @ ASU

Quoted 42 lines · Gregory Price
Quoted 7 lines · David Hildenbrand (Arm)

According to the report [1] syzkaller can trigger it. There is no reproducer, though.

[1]
https://lore.kernel.org/all/CA+0ovCgxbZkXa+OU8w3s84R3KNPNxxRfmsNR-udh+afQBbGNmw@mail.gmail.com/

The actual implication of this report is that there is a bug in cpuset, not mempolicy.

  mpol_rebind_mm+0x3ab/0x680 mm/mempolicy.c:569
      ^^^ should never receive a 0-node nodemask ^^^
  ...snip...
  cpuset_update_tasks_nodemask+0x22e/0x340 kernel/cgroup/cpuset.c:2777
      ^^^ calls guarantee_online_mems ^^^
  ...snip...
  hotplug_update_tasks kernel/cgroup/cpuset.c:3882 [inline]
  cpuset_hotplug_update_tasks kernel/cgroup/cpuset.c:3985 [inline]

Relevant code:

void cpuset_update_tasks_nodemask(struct cpuset *cs) {
... snip ...
guarantee_online_mems(cs, &newmems); <<< critical call
... snip ...

        while ((task = css_task_iter_next(&it))) {
... snip ...
                mpol_rebind_mm(mm, &cs->mems_allowed);

Seems like maybe mpol_rebind_mm should be called with newmems, not cs->mems_allowed, though cs->mems_allowed should never be allowed to be empty, because that makes no sense.

Just eyeballing it, I can't say whether calling with newmems is the right thing, or if mems_allowed should not be allowed to be empty, would have to dig in a little further.

~Gregory

#13 lore raw
Quoted 29 lines · Farhad Alemi

Confirmed, with a standalone reproducer (attached); it panics linus/master at e8c2f9fdadee. cs->mems_allowed can legitimately be empty
on v2 -- a freshly created cpuset child that never had cpuset.mems written keeps mems_allowed empty (never initialized) while effective_mems is inherited non-empty in cpuset_css_online(), and v2 allows attaching tasks to it (the empty-mems guard in cpuset_can_attach_check() is gated on !is_in_v2_mode()). So the non-empty guarantee holds for effective_mems, not for the configured cs->mems_allowed; forbidding empty cpuset.mems would break v2's inherit-from-parent semantics.

The reproducer enables +cpuset, mkdirs a child without writing cpuset.mems, moves a task in, mbind()s a VMA with
MPOL_BIND | MPOL_F_RELATIVE_NODES, and offlines a CPU; the hotplug walk then calls mpol_rebind_mm(mm, &cs->mems_allowed) with the empty mask and folds modulo nodes_weight(*rel) == 0 (console logs attached).

The newmems instinct looks right: it's the effective, online mask the task is actually allowed to use, guarantee_online_mems() keeps it non-empty, and it matches cpuset_attach(), which already rebinds against cs->effective_mems. The fix this implies:

- mpol_rebind_mm(mm, &cs->mems_allowed);
+ mpol_rebind_mm(mm, &newmems);

I built the current base (e8c2f9fdadee) with and without this one-liner:
the unpatched kernel panics on the first cpu1 offline, while the patched kernel runs the reproducer's 8 offline/online cycles cleanly, with no divide error.

Quoted 7 lines

This regressed in ae1c802382f7 ("cpuset: apply cs->effective_{cpus,mems}", v3.17), which moved cpuset_attach() to the effective mask but left this rebind on cs->mems_allowed.

Happy to send this as a proper patch (Fixes: ae1c802382f7, Cc: stable, reproducer) if you agree the cpuset side is right, or to test a mempolicy-side fix if not.

Yes, please send a patch, including a high-level explanation of what you analyzed above!

Signature · 3 lines
Cheers,

David
#14 lore raw

PATCH 1/1 cgroup/cpuset: rebind mm mempolicy to effective_mems, not mems_allowed

cpuset_update_tasks_nodemask() rebinds a task's own mempolicy to the cpuset's effective, online mems (newmems, from guarantee_online_mems()), but rebinds that task's VMA mempolicies to the *configured* mask instead:

	cpuset_change_task_nodemask(task, &newmems);
	...
	mpol_rebind_mm(mm, &cs->mems_allowed);

On the default (v2) hierarchy a cpuset that has never had cpuset.mems written keeps mems_allowed empty while effective_mems is inherited non-empty from the parent, and tasks may be attached to it (the empty-mems attach check is v1-only). A subsequent rebind -- e.g. from a CPU hotplug event walking the cpuset -- then calls mpol_rebind_mm() with an empty mask. For a VMA policy created with MPOL_F_RELATIVE_NODES this reaches mpol_relative_nodemask() ->
nodes_fold(..., nodes_weight(cs->mems_allowed) == 0) -> bitmap_fold(), whose set_bit(oldbit % sz, dst) divides by zero:

  Oops: divide error: 0000 [#1] SMP KASAN NOPTI
  RIP: 0010:bitmap_fold+0x5e/0xb0
   mpol_rebind_nodemask
   mpol_rebind_mm
   cpuset_update_tasks_nodemask
   cpuset_handle_hotplug
   sched_cpu_deactivate
   cpuhp_thread_fun

cs->mems_allowed is the only nodemask in this function that is not the effective set: the task-policy rebind, the page-migration target and cs->old_mems_allowed all use newmems. The sibling cpuset_attach() path already rebinds VMA policies against the effective mems (cpuset_attach_nodemask_to = cs->effective_mems) and explicitly notes that mems_allowed can be empty under hotplug. Rebind the VMA policies to newmems too: it is guaranteed non-empty by guarantee_online_mems(), which fixes the divide-by-zero, and it makes the VMA policies consistent with the task policy and with the nodes the task is actually allowed to use.

Fixes ae1c802382f7 ("cpuset: apply cs->effective_{cpus,mems}")
---
 kernel/cgroup/cpuset.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/cgroup/cpuset.c +1 -1
@@ -2649,7 +2649,7 @@ void cpuset_update_tasks_nodemask(struct cpuset *cs)
 
 		migrate = is_memory_migrate(cs);
 
-		mpol_rebind_mm(mm, &cs->mems_allowed);
+		mpol_rebind_mm(mm, &newmems);
 		if (migrate)
 			cpuset_migrate_mm(mm, &cs->old_mems_allowed, &newmems);
 		else
Signature · 1 line
2.43.0
maintainer
#15 lore raw

Re: [PATCH] cgroup/cpuset: rebind mm mempolicy to effective_mems, not mems_allowed

Quoted 3 lines · On Tue, 9 Jun 2026 19:57:41 -0400 Farhad Alemi <[email protected]>…

cpuset_update_tasks_nodemask() rebinds a task's own mempolicy to the cpuset's effective, online mems (newmems, from guarantee_online_mems()), but rebinds that task's VMA mempolicies to the *configured* mask instead:

Hard to understand. Was "rebinds" supposed to be "is supposed to rebind"?

Quoted 23 lines
	cpuset_change_task_nodemask(task, &newmems);
	...
	mpol_rebind_mm(mm, &cs->mems_allowed);

On the default (v2) hierarchy a cpuset that has never had cpuset.mems written keeps mems_allowed empty while effective_mems is inherited non-empty from the parent, and tasks may be attached to it (the empty-mems attach check is v1-only). A subsequent rebind -- e.g. from a CPU hotplug event walking the cpuset -- then calls mpol_rebind_mm() with an empty mask. For a VMA policy created with MPOL_F_RELATIVE_NODES this reaches mpol_relative_nodemask() ->
nodes_fold(..., nodes_weight(cs->mems_allowed) == 0) -> bitmap_fold(), whose set_bit(oldbit % sz, dst) divides by zero:

  Oops: divide error: 0000 [#1] SMP KASAN NOPTI
  RIP: 0010:bitmap_fold+0x5e/0xb0
   mpol_rebind_nodemask
   mpol_rebind_mm
   cpuset_update_tasks_nodemask
   cpuset_handle_hotplug
   sched_cpu_deactivate
   cpuhp_thread_fun
Quoted 9 lines

cs->mems_allowed is the only nodemask in this function that is not the effective set: the task-policy rebind, the page-migration target and cs->old_mems_allowed all use newmems. The sibling cpuset_attach() path already rebinds VMA policies against the effective mems (cpuset_attach_nodemask_to = cs->effective_mems) and explicitly notes that mems_allowed can be empty under hotplug. Rebind the VMA policies to newmems too: it is guaranteed non-empty by guarantee_online_mems(), which fixes the divide-by-zero, and it makes the VMA policies consistent with the task policy and with the nodes the task is actually allowed to use.

How is this bug triggered?

maintainer
#16 lore raw

Re: [PATCH] cgroup/cpuset: rebind mm mempolicy to effective_mems, not mems_allowed

In reply to Farhad Alemi
Quoted 27 lines · Farhad Alemi

cpuset_update_tasks_nodemask() rebinds a task's own mempolicy to the cpuset's effective, online mems (newmems, from guarantee_online_mems()), but rebinds that task's VMA mempolicies to the *configured* mask instead:

	cpuset_change_task_nodemask(task, &newmems);
	...
	mpol_rebind_mm(mm, &cs->mems_allowed);

On the default (v2) hierarchy a cpuset that has never had cpuset.mems written keeps mems_allowed empty while effective_mems is inherited non-empty from the parent, and tasks may be attached to it (the empty-mems attach check is v1-only). A subsequent rebind -- e.g. from a CPU hotplug event walking the cpuset -- then calls mpol_rebind_mm() with an empty mask. For a VMA policy created with MPOL_F_RELATIVE_NODES this reaches mpol_relative_nodemask() ->
nodes_fold(..., nodes_weight(cs->mems_allowed) == 0) -> bitmap_fold(), whose set_bit(oldbit % sz, dst) divides by zero:

  Oops: divide error: 0000 [#1] SMP KASAN NOPTI
  RIP: 0010:bitmap_fold+0x5e/0xb0
   mpol_rebind_nodemask
   mpol_rebind_mm
   cpuset_update_tasks_nodemask
   cpuset_handle_hotplug
   sched_cpu_deactivate
   cpuhp_thread_fun
Quoted 10 lines

cs->mems_allowed is the only nodemask in this function that is not the effective set: the task-policy rebind, the page-migration target and cs->old_mems_allowed all use newmems. The sibling cpuset_attach() path already rebinds VMA policies against the effective mems (cpuset_attach_nodemask_to = cs->effective_mems) and explicitly notes that mems_allowed can be empty under hotplug. Rebind the VMA policies to newmems too: it is guaranteed non-empty by guarantee_online_mems(), which fixes the divide-by-zero, and it makes the VMA policies consistent with the task policy and with the nodes the task is actually allowed to use.

I think you can make this a bit more concise:

Creating a child cpuset where cpuset.mems is never set leads to a div/0 when a VMA mempolicy with MPOL_F_RELATIVE_NODES rebinds in response to a CPU hotplug event.

Reproduction steps:
1) Create a cgroup w/ cpuset controls (do not set cpuset.mems)
2) Move the task into the child cpuset
3) Create a VMA mempolicy for that task with MPOL_F_RELATIVE_NODES
4) unplug and hotplug a cpu
echo 0 > /sys/devices/system/cpu/cpu1/oneline
echo 1 > /sys/devices/system/cpu/cpu1/oneline
5) mempolicy rebind does a div/0 in mpol_relative_nodemask on the call to __nodes_fold()

The cpuset code passes (cs->mems_allowed) which is not guaranteed to have nodes to the rebind routine. Use mems_effective - the value returned by guarantee_online_mems() - instead, which is guaranteed to have a non-empty nodemask..

Maybe add a link to your reproducer and the original [BUG]

Does this need a Closes tag?

Quoted 22 lines
Fixes ae1c802382f7 ("cpuset: apply cs->effective_{cpus,mems}")
---
 kernel/cgroup/cpuset.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/cgroup/cpuset.c +1 -1
@@ -2649,7 +2649,7 @@ void cpuset_update_tasks_nodemask(struct cpuset *cs)
 
 		migrate = is_memory_migrate(cs);
 
-		mpol_rebind_mm(mm, &cs->mems_allowed);
+		mpol_rebind_mm(mm, &newmems);
 		if (migrate)
 			cpuset_migrate_mm(mm, &cs->old_mems_allowed, &newmems);
 		else
Signature · 1 line
2.43.0
maintainer
#17 lore raw

Re: [PATCH] cgroup/cpuset: rebind mm mempolicy to effective_mems, not mems_allowed

In reply to Farhad Alemi
Quoted 49 lines · Farhad Alemi

cpuset_update_tasks_nodemask() rebinds a task's own mempolicy to the cpuset's effective, online mems (newmems, from guarantee_online_mems()), but rebinds that task's VMA mempolicies to the *configured* mask instead:

	cpuset_change_task_nodemask(task, &newmems);
	...
	mpol_rebind_mm(mm, &cs->mems_allowed);

On the default (v2) hierarchy a cpuset that has never had cpuset.mems written keeps mems_allowed empty while effective_mems is inherited non-empty from the parent, and tasks may be attached to it (the empty-mems attach check is v1-only). A subsequent rebind -- e.g. from a CPU hotplug event walking the cpuset -- then calls mpol_rebind_mm() with an empty mask. For a VMA policy created with MPOL_F_RELATIVE_NODES this reaches mpol_relative_nodemask() ->
nodes_fold(..., nodes_weight(cs->mems_allowed) == 0) -> bitmap_fold(), whose set_bit(oldbit % sz, dst) divides by zero:

   Oops: divide error: 0000 [#1] SMP KASAN NOPTI
   RIP: 0010:bitmap_fold+0x5e/0xb0
    mpol_rebind_nodemask
    mpol_rebind_mm
    cpuset_update_tasks_nodemask
    cpuset_handle_hotplug
    sched_cpu_deactivate
    cpuhp_thread_fun

cs->mems_allowed is the only nodemask in this function that is not the effective set: the task-policy rebind, the page-migration target and cs->old_mems_allowed all use newmems. The sibling cpuset_attach() path already rebinds VMA policies against the effective mems (cpuset_attach_nodemask_to = cs->effective_mems) and explicitly notes that mems_allowed can be empty under hotplug. Rebind the VMA policies to newmems too: it is guaranteed non-empty by guarantee_online_mems(), which fixes the divide-by-zero, and it makes the VMA policies consistent with the task policy and with the nodes the task is actually allowed to use.

Fixes ae1c802382f7 ("cpuset: apply cs->effective_{cpus,mems}")
---
  kernel/cgroup/cpuset.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

kernel/cgroup/cpuset.c void cpuset_update_tasks_nodemask(struct cpuset *cs) · line 2655

Quoted 8 lines
kernel/cgroup/cpuset.c +1 -1
@@ -2649,7 +2649,7 @@ void cpuset_update_tasks_nodemask(struct cpuset *cs)
 
  		migrate = is_memory_migrate(cs);
 
-		mpol_rebind_mm(mm, &cs->mems_allowed);
+		mpol_rebind_mm(mm, &newmems);
  		if (migrate)
  			cpuset_migrate_mm(mm, &cs->old_mems_allowed, &newmems);
  		else

Could you change it to &cs->effecitve_mems instead? For v2, effective_mems will never be empty.

In fact, this is part of the following patch

https://lore.kernel.org/lkml/[email protected]/

Given that this bug can crash the kernel, it should be separated out as a separate patch.

Cheers,
Longman

#18 lore raw

PATCH v2 1/1 cgroup/cpuset: rebind mm mempolicy to effective_mems, not mems_allowed

In reply to Farhad Alemi

Creating a child cpuset where cpuset.mems is never set leads to a div/0 when a VMA mempolicy with MPOL_F_RELATIVE_NODES rebinds in response to a CPU hotplug event.

Reproduction steps:
1) Create a cgroup w/ cpuset controls (do not set cpuset.mems)
2) Move the task into the child cpuset
3) Create a VMA mempolicy for that task with MPOL_F_RELATIVE_NODES
4) unplug and hotplug a cpu
echo 0 > /sys/devices/system/cpu/cpu1/online
echo 1 > /sys/devices/system/cpu/cpu1/online
5) mempolicy rebind does a div/0 in mpol_relative_nodemask on the call to __nodes_fold()

The cpuset code passes (cs->mems_allowed) which is not guaranteed to have nodes to the rebind routine. Use cs->effective_mems instead, which is guaranteed to have a non-empty nodemask.

---
v2: rebind to cs->effective_mems instead of newmems (Waiman Long);
condense the changelog.

 kernel/cgroup/cpuset.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/cgroup/cpuset.c +1 -1
@@ -2649,7 +2649,7 @@ void cpuset_update_tasks_nodemask(struct cpuset *cs)
 
 		migrate = is_memory_migrate(cs);
 
-		mpol_rebind_mm(mm, &cs->mems_allowed);
+		mpol_rebind_mm(mm, &cs->effective_mems);
 		if (migrate)
 			cpuset_migrate_mm(mm, &cs->old_mems_allowed, &newmems);
 		else
Signature · 1 line
2.43.0
#19 lore raw

Re: [PATCH v2] cgroup/cpuset: rebind mm mempolicy to effective_mems, not mems_allowed

On 6/14/26 15:25, Farhad Alemi wrote:

Hi, thanks for your patch!

For the future, please don't submit new revisions as reply to previous submissions.

Quoted 14 lines

Creating a child cpuset where cpuset.mems is never set leads to a div/0 when a VMA mempolicy with MPOL_F_RELATIVE_NODES rebinds in response to a CPU hotplug event.

Reproduction steps:
1) Create a cgroup w/ cpuset controls (do not set cpuset.mems)
2) Move the task into the child cpuset
3) Create a VMA mempolicy for that task with MPOL_F_RELATIVE_NODES
4) unplug and hotplug a cpu
echo 0 > /sys/devices/system/cpu/cpu1/online
echo 1 > /sys/devices/system/cpu/cpu1/online
5) mempolicy rebind does a div/0 in mpol_relative_nodemask on the call to __nodes_fold()

Quoted 3 lines

The cpuset code passes (cs->mems_allowed) which is not guaranteed to have nodes to the rebind routine. Use cs->effective_mems instead, which is guaranteed to have a non-empty nodemask.

Probably worth mentioning here that this makes the linked reproducer happy.

Quoted 2 lines

This should be a

Closes:
https://lore.kernel.org/linux-mm/CA+0ovCgxbZkXa+OU8w3s84R3KNPNxxRfmsNR-udh+afQBbGNmw@mail.gmail.com/

Quoted 17 lines

---
v2: rebind to cs->effective_mems instead of newmems (Waiman Long);
condense the changelog.

 kernel/cgroup/cpuset.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

kernel/cgroup/cpuset.c void cpuset_update_tasks_nodemask(struct cpuset *cs) · line 2652

Quoted 5 lines
kernel/cgroup/cpuset.c +1 -1
@@ -2649,7 +2649,7 @@ void cpuset_update_tasks_nodemask(struct cpuset *cs)
 
 		migrate = is_memory_migrate(cs);
 
-		mpol_rebind_mm(mm, &cs->mems_allowed);
+		mpol_rebind_mm(mm, &cs->effective_mems);

God this is confusing.

So, we obtain newmems from guarantee_online_mems(), which guarantees that newmems is non-empty.

In cpuset_change_task_nodemask(), we set tsk->mems_allowed to newmems, and call mpol_rebind_task(tsk, newmems).

So at least tsk->mems_allowed should be non-empty.

Then we call mpol_rebind_mm(mm, &cs->mems_allowed);

Naturally I wonder: Why are we not using "task->mems_allowed" (maybe cs vs. tsk was the original bug?), which is effectively just newmems?

guarantee_online_mems() computes newmems as "cs->effective_mems & node_states[N_MEMORY]", but walks up to the parent if it would be empty.

Signature · 3 lines
Cheers,

David
maintainer
#20 lore raw

Re: [PATCH v2] cgroup/cpuset: rebind mm mempolicy to effective_mems, not mems_allowed

Quoted 7 lines · David Hildenbrand (Arm)
Quoted 6 lines · Farhad Alemi
kernel/cgroup/cpuset.c +0 -0
@@ -2649,7 +2649,7 @@ void cpuset_update_tasks_nodemask(struct cpuset *cs)
 
Quoted 7 lines

kernel/cgroup/cpuset.c void cpuset_update_tasks_nodemask(struct cpuset *cs) · line 2652

Quoted 4 lines
kernel/cgroup/cpuset.c +1 -1
 		migrate = is_memory_migrate(cs);
 
-		mpol_rebind_mm(mm, &cs->mems_allowed);
+		mpol_rebind_mm(mm, &cs->effective_mems);

God this is confusing.

All interactions between mempolicy and cpuset are horrible and confusing. Much like Lorenzo's anon_vma work, I have to keep notes on how this whole thing doesn't just spew SIGBUS constantly.

The short answer is: mempolicy is advisory and cpuset is strictly followed - in a dispute cpuset wins... except for file backed memory, then everyon loses and nothing is consistent.

Quoted 3 lines

Naturally I wonder: Why are we not using "task->mems_allowed" (maybe cs vs. tsk was the original bug?), which is effectively just newmems?

Short answer: task->mems_allowed is protected by the task lock and we don't hold the task lock for a foreign task (not-current) over mm operations.

Long answer: Reasons and "Stop looking at the spaghetti, it's going to break"

~Gregory

#21 lore raw

Re: [PATCH v2] cgroup/cpuset: rebind mm mempolicy to effective_mems, not mems_allowed

Quoted 24 lines · Gregory Price
Quoted 7 lines · David Hildenbrand (Arm)
Quoted 6 lines · Farhad Alemi
kernel/cgroup/cpuset.c +0 -0
@@ -2649,7 +2649,7 @@ void cpuset_update_tasks_nodemask(struct cpuset *cs)
 
Quoted 7 lines

kernel/cgroup/cpuset.c void cpuset_update_tasks_nodemask(struct cpuset *cs) · line 2652

Quoted 4 lines
kernel/cgroup/cpuset.c +1 -1
 		migrate = is_memory_migrate(cs);
 
-		mpol_rebind_mm(mm, &cs->mems_allowed);
+		mpol_rebind_mm(mm, &cs->effective_mems);

God this is confusing.

All interactions between mempolicy and cpuset are horrible and confusing. Much like Lorenzo's anon_vma work, I have to keep notes on how this whole thing doesn't just spew SIGBUS constantly.

The short answer is: mempolicy is advisory and cpuset is strictly followed - in a dispute cpuset wins... except for file backed memory, then everyon loses and nothing is consistent.

Quoted 7 lines
Quoted 3 lines

Naturally I wonder: Why are we not using "task->mems_allowed" (maybe cs vs. tsk was the original bug?), which is effectively just newmems?

Short answer: task->mems_allowed is protected by the task lock and we don't hold the task lock for a foreign task (not-current) over mm operations.

Well, we can just use newmems, which cannot change? Again, that is based on cs->effective_mems but is guaranteed to return something non-empty.

AI was not able to convince me (neither was I able to convince AI) that there is not some obscure cgroup v1 scenario where the current fix would also be wrong.

With newmems it's clear that it is guaranteed to not be empty.

Signature · 3 lines
Cheers,

David
maintainer
#22 lore raw

Re: [PATCH v2] cgroup/cpuset: rebind mm mempolicy to effective_mems, not mems_allowed

Quoted 1 line · David Hildenbrand (Arm)

With newmems it's clear that it is guaranteed to not be empty.

I hadn't noticed he switched the patch from newmems -> effective_mems.

This needs to be changed back to newmems, otherwise we're depending on a derivative value set somewhere else in the code being correct instead of using what we *know* is correct *at the moment we need to use it*.

So yes, go back to using newmems.

~Gregory

maintainer Acked-by
#23 lore raw

Re: [PATCH v2] cgroup/cpuset: rebind mm mempolicy to effective_mems, not mems_allowed

Quoted 10 lines · Gregory Price
Quoted 1 line · David Hildenbrand (Arm)

With newmems it's clear that it is guaranteed to not be empty.

I hadn't noticed he switched the patch from newmems -> effective_mems.

This needs to be changed back to newmems, otherwise we're depending on a derivative value set somewhere else in the code being correct instead of using what we *know* is correct *at the moment we need to use it*.

So yes, go back to using newmems.

Right, that's what v1 did looking at this now. Waiman requested the change, but I don't think we want that.

So for v1:

Signature · 3 lines
Cheers,

David
maintainer
#24 lore raw

Re: [PATCH v2] cgroup/cpuset: rebind mm mempolicy to effective_mems, not mems_allowed

In reply to Gregory Price
Quoted 13 lines · Gregory Price
Quoted 6 lines · David Hildenbrand (Arm)
Quoted 5 lines · Farhad Alemi
kernel/cgroup/cpuset.c +0 -0
@@ -2649,7 +2649,7 @@ void cpuset_update_tasks_nodemask(struct cpuset *cs)
 
Quoted 6 lines

kernel/cgroup/cpuset.c void cpuset_update_tasks_nodemask(struct cpuset *cs) · line 2652

Quoted 4 lines
kernel/cgroup/cpuset.c +1 -1
  		migrate = is_memory_migrate(cs);
 
-		mpol_rebind_mm(mm, &cs->mems_allowed);
+		mpol_rebind_mm(mm, &cs->effective_mems);

God this is confusing.

Quoted 7 lines

All interactions between mempolicy and cpuset are horrible and confusing. Much like Lorenzo's anon_vma work, I have to keep notes on how this whole thing doesn't just spew SIGBUS constantly.

The short answer is: mempolicy is advisory and cpuset is strictly followed - in a dispute cpuset wins... except for file backed memory, then everyon loses and nothing is consistent.

That is what I believe why mpol_rebind_mm() a bit differently from the others and it is historically done this way a long time ago before cgroup v2.

For cgroup v1, mems_allowed can't be empty or you can't put any task into the cpuset. Also effective_mems is the same as mems_allowed. cgroup v2 is quite different in how it handles memory nodes and CPUs. Users can isn't forced to set mems_allowed and cpus_allowed as effective_mems and effective_cpus will inherit parent version if mems_allowed and cpus_allowed are not set. IOW, effective_mems will never be empty. Yes, it is a bug with the introduction of cpuset v2 that we should have replaced mems_allowed by effective_mems at that time. With v2, effective_mems should contain only online nodes. The only exception is during the short transition period when a memory node hotunplug operation is in progress when a write to cpuset.mems is happening at the same time. With v1, it is theoretically possible that none of the nodes in mems_allowed is online.

The reason why I am suggesting to use cs->effective_mems to keep the old cgroup v1 behavior. If the consensus is to use the output of guarantee_online_mems() for mpol_rebind_mm(), I will not be against that but it will be a slight change in user-visible behavior.

Cheers, Longman

Quoted 12 lines
Quoted 3 lines

Naturally I wonder: Why are we not using "task->mems_allowed" (maybe cs vs. tsk was the original bug?), which is effectively just newmems?

Short answer: task->mems_allowed is protected by the task lock and we don't hold the task lock for a foreign task (not-current) over mm operations.

Long answer: Reasons and "Stop looking at the spaghetti, it's going to break"

~Gregory

maintainer
#25 lore raw

Re: [PATCH v2] cgroup/cpuset: rebind mm mempolicy to effective_mems, not mems_allowed

Quoted 24 lines · Waiman Long
Quoted 14 lines · Gregory Price
Quoted 7 lines · David Hildenbrand (Arm)
Quoted 6 lines · Farhad Alemi
kernel/cgroup/cpuset.c +0 -0
@@ -2649,7 +2649,7 @@ void cpuset_update_tasks_nodemask(struct 
 cpuset *cs)
 
Quoted 6 lines

kernel/cgroup/cpuset.c void cpuset_update_tasks_nodemask(struct cpuset *cs) · line 2652

Quoted 4 lines
kernel/cgroup/cpuset.c +1 -1
           migrate = is_memory_migrate(cs);
 
-        mpol_rebind_mm(mm, &cs->mems_allowed);
+        mpol_rebind_mm(mm, &cs->effective_mems);

God this is confusing.

Quoted 7 lines

All interactions between mempolicy and cpuset are horrible and confusing.  Much like Lorenzo's anon_vma work, I have to keep notes on how this whole thing doesn't just spew SIGBUS constantly.

The short answer is: mempolicy is advisory and cpuset is strictly followed - in a dispute cpuset wins... except for file backed memory, then everyon loses and nothing is consistent.

Quoted 5 lines

That is what I believe why mpol_rebind_mm() a bit differently from the others and it is historically done this way a long time ago before cgroup v2.

For cgroup v1, mems_allowed can't be empty or you can't put any task into the cpuset. Also effective_mems is the same as mems_allowed. cgroup v2 is quite different in how it handles memory nodes and CPUs. Users can isn't forced to set mems_allowed and cpus_allowed as effective_mems and effective_cpus will inherit parent version if mems_allowed and cpus_allowed are not set. IOW, effective_mems will never be empty. Yes, it is a bug with the introduction of cpuset v2 that we should have replaced mems_allowed by effective_mems at that time. With v2, effective_mems should contain only online nodes. The only exception is during the short transition period when a memory node hotunplug operation is in progress when a write to cpuset.mems is happening at the same time. With v1, it is theoretically possible that none of the nodes in mems_allowed is online.

The reason why I am suggesting to use cs->effective_mems to keep the old cgroup v1 behavior. If the consensus is to use the output of guarantee_online_mems() for mpol_rebind_mm(), I will not be against that but it will be a slight change in user-visible behavior.

BTW, I still prefer the v2 patch. If it is decided we should use the guarantee_online_mems() value instead, it will have to be a separate patch with changes in the relevant documentation like Documentation/admin-guide/cgroup-v1/cpuset.rst.

Cheers,
Longman

#26 lore raw

Re: [PATCH v2] cgroup/cpuset: rebind mm mempolicy to effective_mems, not mems_allowed

Quoted 32 lines · Waiman Long
Quoted 26 lines · Waiman Long
Quoted 7 lines · Gregory Price

All interactions between mempolicy and cpuset are horrible and confusing.  Much like Lorenzo's anon_vma work, I have to keep notes on how this whole thing doesn't just spew SIGBUS constantly.

The short answer is: mempolicy is advisory and cpuset is strictly followed - in a dispute cpuset wins... except for file backed memory, then everyon loses and nothing is consistent.

That is what I believe why mpol_rebind_mm() a bit differently from the others and it is historically done this way a long time ago before cgroup v2.

For cgroup v1, mems_allowed can't be empty or you can't put any task into the cpuset. Also effective_mems is the same as mems_allowed. cgroup v2 is quite different in how it handles memory nodes and CPUs. Users can isn't forced to set mems_allowed and cpus_allowed as effective_mems and effective_cpus will inherit parent version if mems_allowed and cpus_allowed are not set. IOW, effective_mems will never be empty. Yes, it is a bug with the introduction of cpuset v2 that we should have replaced mems_allowed by effective_mems at that time. With v2, effective_mems should contain only online nodes. The only exception is during the short transition period when a memory node hotunplug operation is in progress when a write to cpuset.mems is happening at the same time. With v1, it is theoretically possible that none of the nodes in mems_allowed is online.

Quoted 4 lines

The reason why I am suggesting to use cs->effective_mems to keep the old cgroup v1 behavior. If the consensus is to use the output of guarantee_online_mems() for mpol_rebind_mm(), I will not be against that but it will be a slight change in user-visible behavior.

Quoted 4 lines

BTW, I still prefer the v2 patch. If it is decided we should use the guarantee_online_mems() value instead, it will have to be a separate patch with changes in the relevant documentation like Documentation/admin-guide/cgroup-v1/ cpuset.rst.

newmems is "obviously" correct, so I really don't see why we should add something that needs half a page of text to explain why it is fine -- if newmems just does the trick?

Please enlighten me.

Signature · 3 lines
Cheers,

David
maintainer
#27 lore raw

Re: [PATCH v2] cgroup/cpuset: rebind mm mempolicy to effective_mems, not mems_allowed

Quoted 8 lines · David Hildenbrand (Arm)
Quoted 7 lines · Waiman Long
Quoted 6 lines · Waiman Long

The reason why I am suggesting to use cs->effective_mems to keep the old cgroup v1 behavior. If the consensus is to use the output of guarantee_online_mems() for mpol_rebind_mm(), I will not be against that but it will be a slight change in user-visible behavior.

I'm not grok'ing what is user-visible here.

The two values should effectively be equivalent because we're using this value to constrain mpol's during a hotplug event.

If the values differed, you would be saying there's a race condition that could affect correctness of the rebind (which can't happen, because this whole thing is done under the hotplug lock btw).

Can you help me understand?

~Gregory

maintainer
#28 lore raw

Re: [PATCH v2] cgroup/cpuset: rebind mm mempolicy to effective_mems, not mems_allowed

Quoted 10 lines · Gregory Price
Quoted 7 lines · David Hildenbrand (Arm)
Quoted 6 lines · Waiman Long
Quoted 5 lines · Waiman Long

The reason why I am suggesting to use cs->effective_mems to keep the old cgroup v1 behavior. If the consensus is to use the output of guarantee_online_mems() for mpol_rebind_mm(), I will not be against that but it will be a slight change in user-visible behavior.

I'm not grok'ing what is user-visible here.

Quoted 8 lines

The two values should effectively be equivalent because we're using this value to constrain mpol's during a hotplug event.

If the values differed, you would be saying there's a race condition that could affect correctness of the rebind (which can't happen, because this whole thing is done under the hotplug lock btw).

Can you help me understand?

The effective_mems and guarantee_online_mems() should be more or less equivalent for v2, but not for v1. In my response to David, I gave an example of how the behavior will differ.

Cheers,
Longman

maintainer
#29 lore raw

Re: [PATCH v2] cgroup/cpuset: rebind mm mempolicy to effective_mems, not mems_allowed

In reply to David Hildenbrand (Arm)
Quoted 34 lines · David Hildenbrand (Arm)
Quoted 25 lines · Waiman Long
Quoted 24 lines · Waiman Long
Quoted 7 lines · Gregory Price

All interactions between mempolicy and cpuset are horrible and confusing.  Much like Lorenzo's anon_vma work, I have to keep notes on how this whole thing doesn't just spew SIGBUS constantly.

The short answer is: mempolicy is advisory and cpuset is strictly followed - in a dispute cpuset wins... except for file backed memory, then everyon loses and nothing is consistent.

That is what I believe why mpol_rebind_mm() a bit differently from the others and it is historically done this way a long time ago before cgroup v2.

For cgroup v1, mems_allowed can't be empty or you can't put any task into the cpuset. Also effective_mems is the same as mems_allowed. cgroup v2 is quite different in how it handles memory nodes and CPUs. Users can isn't forced to set mems_allowed and cpus_allowed as effective_mems and effective_cpus will inherit parent version if mems_allowed and cpus_allowed are not set. IOW, effective_mems will never be empty. Yes, it is a bug with the introduction of cpuset v2 that we should have replaced mems_allowed by effective_mems at that time. With v2, effective_mems should contain only online nodes. The only exception is during the short transition period when a memory node hotunplug operation is in progress when a write to cpuset.mems is happening at the same time. With v1, it is theoretically possible that none of the nodes in mems_allowed is online.

Quoted 8 lines
Quoted 4 lines

The reason why I am suggesting to use cs->effective_mems to keep the old cgroup v1 behavior. If the consensus is to use the output of guarantee_online_mems() for mpol_rebind_mm(), I will not be against that but it will be a slight change in user-visible behavior.

BTW, I still prefer the v2 patch. If it is decided we should use the guarantee_online_mems() value instead, it will have to be a separate patch with changes in the relevant documentation like Documentation/admin-guide/cgroup-v1/ cpuset.rst.

Quoted 5 lines

newmems is "obviously" correct, so I really don't see why we should add something that needs half a page of text to explain why it is fine -- if newmems just does the trick?

Please enlighten me.

Yes, taking newmems is a reasonable choice and there are pros and cons with each options. My focus is more on not changing how v1 cpuset behaves as it is well defined in the v1 cpusets.rst file:

    Requests by a task, using the sched_setaffinity(2) system call to include CPUs in its CPU affinity mask, and using the mbind(2) and set_mempolicy(2) system calls to include Memory Nodes in its memory policy, are both filtered through that task's cpuset, filtering out any CPUs or Memory Nodes not in that cpuset.  The scheduler will not schedule a task on a CPU that is not allowed in its cpus_allowed vector, and the kernel page allocator will not allocate a page on a node that is not allowed in the requesting task's mems_allowed vector.

v2, OTOH, is more vague as to what setting cpuset.mems will mean and we generally follow what v1 is doing, but we have more leeway of what we can do.

Using newmems will make the above text not totally correct. At least the offline memory nodes will be filtered out which will not be utilized by the task when the offline node becomes online. That is why I am saying that we will have to correct the documentation if we want to make this change.

Cheers,
Longman

#30 lore raw

Re: [PATCH v2] cgroup/cpuset: rebind mm mempolicy to effective_mems, not mems_allowed

Quoted 25 lines · Waiman Long
Quoted 10 lines · David Hildenbrand (Arm)
Quoted 4 lines · Waiman Long

BTW, I still prefer the v2 patch. If it is decided we should use the guarantee_online_mems() value instead, it will have to be a separate patch with changes in the relevant documentation like Documentation/admin-guide/cgroup-v1/ cpuset.rst.

newmems is "obviously" correct, so I really don't see why we should add something that needs half a page of text to explain why it is fine -- if newmems just does the trick?

Please enlighten me.

Yes, taking newmems is a reasonable choice and there are pros and cons with each options. My focus is more on not changing how v1 cpuset behaves as it is well defined in the v1 cpusets.rst file:

    Requests by a task, using the sched_setaffinity(2) system call to include CPUs in its CPU affinity mask, and using the mbind(2) and set_mempolicy(2) system calls to include Memory Nodes in its memory policy, are both filtered through that task's cpuset, filtering out any CPUs or Memory Nodes not in that cpuset.  The scheduler will not schedule a task on a CPU that is not allowed in its cpus_allowed vector, and the kernel page allocator will not allocate a page on a node that is not allowed in the requesting task's mems_allowed vector.

Quoted 7 lines

v2, OTOH, is more vague as to what setting cpuset.mems will mean and we generally follow what v1 is doing, but we have more leeway of what we can do.

Using newmems will make the above text not totally correct. At least the offline memory nodes will be filtered out which will not be utilized by the task when the offline node becomes online. That is why I am saying that we will have to correct the documentation if we want to make this change.

So IIUC:

kernel/cgroup/cpuset.c +7 -1
index 1335e437098e..cdfc615f35a5 100644
@@ -2645,7 +2645,13 @@ void cpuset_update_tasks_nodemask(struct cpuset *cs)
 
                migrate = is_memory_migrate(cs);
 
-               mpol_rebind_mm(mm, &cs->mems_allowed);
+               /*
+                * For v1 we can have empty effective_mems, but we cannot
+                * attach any tasks (see cpuset_can_attach_check()). For v2,
+                * it's guaranteed to not be empty.
+                */
+               VM_WARN_ON_ONCE(nodes_empty(cs->effective_mems));
+               mpol_rebind_mm(mm, &cs->effective_mems);
                if (migrate)
                        cpuset_migrate_mm(mm, &cs->old_mems_allowed, &newmems);
                else
Signature · 3 lines
Cheers,

David
maintainer
#31 lore raw

Re: [PATCH v2] cgroup/cpuset: rebind mm mempolicy to effective_mems, not mems_allowed

Quoted 43 lines · David Hildenbrand (Arm)
Quoted 24 lines · Waiman Long
Quoted 10 lines · David Hildenbrand (Arm)
Quoted 4 lines · Waiman Long

BTW, I still prefer the v2 patch. If it is decided we should use the guarantee_online_mems() value instead, it will have to be a separate patch with changes in the relevant documentation like Documentation/admin-guide/cgroup-v1/ cpuset.rst.

newmems is "obviously" correct, so I really don't see why we should add something that needs half a page of text to explain why it is fine -- if newmems just does the trick?

Please enlighten me.

Yes, taking newmems is a reasonable choice and there are pros and cons with each options. My focus is more on not changing how v1 cpuset behaves as it is well defined in the v1 cpusets.rst file:

    Requests by a task, using the sched_setaffinity(2) system call to include CPUs in its CPU affinity mask, and using the mbind(2) and set_mempolicy(2) system calls to include Memory Nodes in its memory policy, are both filtered through that task's cpuset, filtering out any CPUs or Memory Nodes not in that cpuset.  The scheduler will not schedule a task on a CPU that is not allowed in its cpus_allowed vector, and the kernel page allocator will not allocate a page on a node that is not allowed in the requesting task's mems_allowed vector.

Quoted 7 lines

v2, OTOH, is more vague as to what setting cpuset.mems will mean and we generally follow what v1 is doing, but we have more leeway of what we can do.

Using newmems will make the above text not totally correct. At least the offline memory nodes will be filtered out which will not be utilized by the task when the offline node becomes online. That is why I am saying that we will have to correct the documentation if we want to make this change.

So IIUC:

kernel/cgroup/cpuset.c +0 -1
index 1335e437098e..cdfc615f35a5 100644
@@ -2645,7 +2645,13 @@ void cpuset_update_tasks_nodemask(struct cpuset *cs)
 
                 migrate = is_memory_migrate(cs);
 
-               mpol_rebind_mm(mm, &cs->mems_allowed);

kernel/cgroup/cpuset.c void cpuset_update_tasks_nodemask(struct cpuset *cs) · line 2657

Quoted 10 lines
kernel/cgroup/cpuset.c +7 -0
+               /*
+                * For v1 we can have empty effective_mems, but we cannot
+                * attach any tasks (see cpuset_can_attach_check()). For v2,
+                * it's guaranteed to not be empty.
+                */
+               VM_WARN_ON_ONCE(nodes_empty(cs->effective_mems));
+               mpol_rebind_mm(mm, &cs->effective_mems);
                 if (migrate)
                         cpuset_migrate_mm(mm, &cs->old_mems_allowed, &newmems);
                 else

That is true, but I don't think we need a VM_WARN_ON_ONCE() here.

Cheers,
Longman

Quoted 1 line
#32 lore raw

Re: [PATCH v2] cgroup/cpuset: rebind mm mempolicy to effective_mems, not mems_allowed

Quoted 35 lines · Waiman Long
Quoted 34 lines · David Hildenbrand (Arm)
Quoted 13 lines · Waiman Long

Yes, taking newmems is a reasonable choice and there are pros and cons with each options. My focus is more on not changing how v1 cpuset behaves as it is well defined in the v1 cpusets.rst file:

     Requests by a task, using the sched_setaffinity(2) system call to include CPUs in its CPU affinity mask, and using the mbind(2) and set_mempolicy(2) system calls to include Memory Nodes in its memory policy, are both filtered through that task's cpuset, filtering out any CPUs or Memory Nodes not in that cpuset.  The scheduler will not schedule a task on a CPU that is not allowed in its cpus_allowed vector, and the kernel page allocator will not allocate a page on a node that is not allowed in the requesting task's mems_allowed vector.

Quoted 7 lines

v2, OTOH, is more vague as to what setting cpuset.mems will mean and we generally follow what v1 is doing, but we have more leeway of what we can do.

Using newmems will make the above text not totally correct. At least the offline memory nodes will be filtered out which will not be utilized by the task when the offline node becomes online. That is why I am saying that we will have to correct the documentation if we want to make this change.

So IIUC:

kernel/cgroup/cpuset.c +4 -0
index 1335e437098e..cdfc615f35a5 100644
@@ -2645,7 +2645,13 @@ void cpuset_update_tasks_nodemask(struct cpuset *cs)
                    migrate = is_memory_migrate(cs);
   -               mpol_rebind_mm(mm, &cs->mems_allowed);
+               /*
+                * For v1 we can have empty effective_mems, but we cannot
+                * attach any tasks (see cpuset_can_attach_check()). For v2,
+                * it's guaranteed to not be empty.
Quoted 8 lines
Quoted 6 lines
kernel/cgroup/cpuset.c +3 -0
+                */
+               VM_WARN_ON_ONCE(nodes_empty(cs->effective_mems));
+               mpol_rebind_mm(mm, &cs->effective_mems);
                  if (migrate)
                          cpuset_migrate_mm(mm, &cs->old_mems_allowed, &newmems);

                 else

That is true, but I don't think we need a VM_WARN_ON_ONCE() here.

I'd prefer if we catch such stuff in the future more easily than running into late divide-by-zero. Maybe we should check in mpol_rebind_mm() instead.

Signature · 3 lines
Cheers,

David
maintainer Acked-by on v2 1/1
#33 lore raw

Re: [PATCH v2] cgroup/cpuset: rebind mm mempolicy to effective_mems, not mems_allowed

In reply to Farhad Alemi
Quoted 36 lines · Farhad Alemi

Creating a child cpuset where cpuset.mems is never set leads to a div/0 when a VMA mempolicy with MPOL_F_RELATIVE_NODES rebinds in response to a CPU hotplug event.

Reproduction steps:
1) Create a cgroup w/ cpuset controls (do not set cpuset.mems)
2) Move the task into the child cpuset
3) Create a VMA mempolicy for that task with MPOL_F_RELATIVE_NODES
4) unplug and hotplug a cpu
echo 0 > /sys/devices/system/cpu/cpu1/online
echo 1 > /sys/devices/system/cpu/cpu1/online
5) mempolicy rebind does a div/0 in mpol_relative_nodemask on the call to __nodes_fold()

The cpuset code passes (cs->mems_allowed) which is not guaranteed to have nodes to the rebind routine. Use cs->effective_mems instead, which is guaranteed to have a non-empty nodemask.

---
v2: rebind to cs->effective_mems instead of newmems (Waiman Long);
condense the changelog.

  kernel/cgroup/cpuset.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

kernel/cgroup/cpuset.c void cpuset_update_tasks_nodemask(struct cpuset *cs) · line 2655

Quoted 8 lines
kernel/cgroup/cpuset.c +1 -1
@@ -2649,7 +2649,7 @@ void cpuset_update_tasks_nodemask(struct cpuset *cs)
 
  		migrate = is_memory_migrate(cs);
 
-		mpol_rebind_mm(mm, &cs->mems_allowed);
+		mpol_rebind_mm(mm, &cs->effective_mems);
  		if (migrate)
  			cpuset_migrate_mm(mm, &cs->old_mems_allowed, &newmems);
  		else
maintainer Acked-by Waiman Long on v2 1/1
#34 lore raw

Re: [PATCH v2] cgroup/cpuset: rebind mm mempolicy to effective_mems, not mems_allowed

In reply to Farhad Alemi
Quoted 4 lines · On Sun, 14 Jun 2026 06:25:55 -0700 Farhad Alemi <[email protected]>…

Creating a child cpuset where cpuset.mems is never set leads to a div/0 when a VMA mempolicy with MPOL_F_RELATIVE_NODES rebinds in response to a CPU hotplug event.

Quoted 9 lines

Reproduction steps:
1) Create a cgroup w/ cpuset controls (do not set cpuset.mems)
2) Move the task into the child cpuset
3) Create a VMA mempolicy for that task with MPOL_F_RELATIVE_NODES
4) unplug and hotplug a cpu
echo 0 > /sys/devices/system/cpu/cpu1/online
echo 1 > /sys/devices/system/cpu/cpu1/online
5) mempolicy rebind does a div/0 in mpol_relative_nodemask on the call to __nodes_fold()

Oops.

Quoted 3 lines

The cpuset code passes (cs->mems_allowed) which is not guaranteed to have nodes to the rebind routine. Use cs->effective_mems instead, which is guaranteed to have a non-empty nodemask.

Well gee, what happened with this patch.

I apologize for misfiling a cc:stable bugfix into my post-rc1 backlog pile, but I got there in the end.

I guess this is an MM patch, even though it's against kernel/cgroup/cpuset.c.

Nobody cc'ed Tejun. Fixed.

David acked v1 but is being coy about the v2 patch?

Sashiko AI review suggests that there's a similar bug in sys_set_mempolicy():

https://sashiko.dev/#/patchset/CA+0ovCgfHJHv5d1mzapWWvF-LhjppzDX8NPPLvCPZxPKg8RiYw@mail.gmail.com

Anyway, I'll queue the v2 patch as an mm.git hotfix, but not with a lot of confidence at this time. Can people please refocus on this and help recommend a way forward?

From: Farhad Alemi <[email protected]>
Subject: cgroup/cpuset: rebind mm mempolicy to effective_mems, not mems_allowed
Date: Sun, 14 Jun 2026 06:25:55 -0700

Creating a child cpuset where cpuset.mems is never set leads to a div/0 when a VMA mempolicy with MPOL_F_RELATIVE_NODES rebinds in response to a CPU hotplug event.

Reproduction steps:
1) Create a cgroup w/ cpuset controls (do not set cpuset.mems)
2) Move the task into the child cpuset
3) Create a VMA mempolicy for that task with MPOL_F_RELATIVE_NODES
4) unplug and hotplug a cpu
echo 0 > /sys/devices/system/cpu/cpu1/online
echo 1 > /sys/devices/system/cpu/cpu1/online
5) mempolicy rebind does a div/0 in mpol_relative_nodemask on the call to __nodes_fold()

The cpuset code passes (cs->mems_allowed) which is not guaranteed to have nodes to the rebind routine. Use cs->effective_mems instead, which is guaranteed to have a non-empty nodemask.

---

 kernel/cgroup/cpuset.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/cgroup/cpuset.c renamed from kernel/cgroup/cpuset.c~cgroup-cpuset-rebind-mm-mempolicy-to-effective_mems-not-mems_allowed +1 -1
@@ -2653,7 +2653,7 @@ void cpuset_update_tasks_nodemask(struct
 
 		migrate = is_memory_migrate(cs);
 
-		mpol_rebind_mm(mm, &cs->mems_allowed);
+		mpol_rebind_mm(mm, &cs->effective_mems);
 		if (migrate)
 			cpuset_migrate_mm(mm, &cs->old_mems_allowed, &newmems);
 		else

_

#35 lore raw

Re: [PATCH v2] cgroup/cpuset: rebind mm mempolicy to effective_mems, not mems_allowed

Quoted 27 lines · Andrew Morton
Quoted 4 lines · On Sun, 14 Jun 2026 06:25:55 -0700 Farhad Alemi <[email protected]>…

Creating a child cpuset where cpuset.mems is never set leads to a div/0 when a VMA mempolicy with MPOL_F_RELATIVE_NODES rebinds in response to a CPU hotplug event.

Quoted 9 lines

Reproduction steps:
1) Create a cgroup w/ cpuset controls (do not set cpuset.mems)
2) Move the task into the child cpuset
3) Create a VMA mempolicy for that task with MPOL_F_RELATIVE_NODES
4) unplug and hotplug a cpu
echo 0 > /sys/devices/system/cpu/cpu1/online
echo 1 > /sys/devices/system/cpu/cpu1/online
5) mempolicy rebind does a div/0 in mpol_relative_nodemask on the call to __nodes_fold()

Oops.

Quoted 3 lines

The cpuset code passes (cs->mems_allowed) which is not guaranteed to have nodes to the rebind routine. Use cs->effective_mems instead, which is guaranteed to have a non-empty nodemask.

Well gee, what happened with this patch.

I apologize for misfiling a cc:stable bugfix into my post-rc1 backlog pile, but I got there in the end.

Quoted 6 lines

I guess this is an MM patch, even though it's against kernel/cgroup/cpuset.c.

Nobody cc'ed Tejun. Fixed.

David acked v1 but is being coy about the v2 patch?

Yes, after the discussion, I think we should add comment similar to what I proposed.

Signature · 3 lines
Cheers,

David
#36 lore raw
In reply to Yury Norov
kernel test robot noticed the following build errors:

Hi Yury,

kernel test robot noticed the following build errors:

[auto build test ERROR on akpm-mm/mm-everything]

url:    https://github.com/intel-lab-lkp/linux/commits/Yury-Norov/mm-don-t-allow-empty-relative-nodemask-in-mpol_relative_nodemask/20260529-030835
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20260528190337.878027-1-ynorov%40nvidia.com
patch subject: [PATCH] mm: don't allow empty relative nodemask in mpol_relative_nodemask()
config: x86_64-buildonly-randconfig-003-20260529 (https://download.01.org/0day-ci/archive/20260529/[email protected]/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260529/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

mm/mempolicy.c: In function 'mpol_relative_nodemask':

>> mm/mempolicy.c:377:24: error: 'return' with a value, in function returning void [-Wreturn-mismatch]
     377 |                 return -EINVAL;
         |                        ^

mm/mempolicy.c:370:13: note: declared here
370 | static void mpol_relative_nodemask(nodemask_t *ret, const nodemask_t *orig,
| ^~~~~~~~~~~~~~~~~~~~~~

Kconfig warnings: (for reference only)
WARNING: unmet direct dependencies detected for MFD_STMFX Depends on [n]: HAS_IOMEM [=y] && I2C [=y] && OF [=n] Selected by [y]:
- PINCTRL_STMFX [=y] && PINCTRL [=y] && I2C [=y] && HAS_IOMEM [=y]

vim +/return +377 mm/mempolicy.c

369
370 static void mpol_relative_nodemask(nodemask_t *ret, const nodemask_t *orig,
371 const nodemask_t *rel)
372 {
373 unsigned int w = nodes_weight(*rel);
374 nodemask_t tmp;
375
376 if (w == 0)

 > 377			return -EINVAL;

378
379 nodes_fold(tmp, *orig, w);
380 nodes_onto(*ret, tmp, *rel);
381 }
382

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

#37 lore raw
In reply to Yury Norov
kernel test robot noticed the following build warnings:

Hi Yury,

kernel test robot noticed the following build warnings:

[auto build test WARNING on akpm-mm/mm-everything]

url:    https://github.com/intel-lab-lkp/linux/commits/Yury-Norov/mm-don-t-allow-empty-relative-nodemask-in-mpol_relative_nodemask/20260529-030835
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20260528190337.878027-1-ynorov%40nvidia.com
patch subject: [PATCH] mm: don't allow empty relative nodemask in mpol_relative_nodemask()
config: x86_64-kexec (https://download.01.org/0day-ci/archive/20260529/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260529/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> mm/mempolicy.c:377:3: warning: void function 'mpol_relative_nodemask' should not return a value [-Wreturn-mismatch]
     377 |                 return -EINVAL;
         |                 ^      ~~~~~~~

1 warning generated.

vim +/mpol_relative_nodemask +377 mm/mempolicy.c

369
370 static void mpol_relative_nodemask(nodemask_t *ret, const nodemask_t *orig,
371 const nodemask_t *rel)
372 {
373 unsigned int w = nodes_weight(*rel);
374 nodemask_t tmp;
375
376 if (w == 0)

 > 377			return -EINVAL;

378
379 nodes_fold(tmp, *orig, w);
380 nodes_onto(*ret, tmp, *rel);
381 }
382

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

#38 lore raw
In reply to Yury Norov
kernel test robot noticed the following build warnings:

Hi Yury,

kernel test robot noticed the following build warnings:

[auto build test WARNING on akpm-mm/mm-everything]

url:    https://github.com/intel-lab-lkp/linux/commits/Yury-Norov/mm-don-t-allow-empty-relative-nodemask-in-mpol_relative_nodemask/20260529-030835
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20260528190337.878027-1-ynorov%40nvidia.com
patch subject: [PATCH] mm: don't allow empty relative nodemask in mpol_relative_nodemask()
config: x86_64-kexec (https://download.01.org/0day-ci/archive/20260529/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260529/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> mm/mempolicy.c:377:3: warning: void function 'mpol_relative_nodemask' should not return a value [-Wreturn-mismatch]
     377 |                 return -EINVAL;
         |                 ^      ~~~~~~~

1 warning generated.

vim +/mpol_relative_nodemask +377 mm/mempolicy.c

369
370 static void mpol_relative_nodemask(nodemask_t *ret, const nodemask_t *orig,
371 const nodemask_t *rel)
372 {
373 unsigned int w = nodes_weight(*rel);
374 nodemask_t tmp;
375
376 if (w == 0)

 > 377			return -EINVAL;

378
379 nodes_fold(tmp, *orig, w);
380 nodes_onto(*ret, tmp, *rel);
381 }
382

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

#39 lore raw
In reply to Yury Norov
kernel test robot noticed the following build warnings:

Hi Yury,

kernel test robot noticed the following build warnings:

[auto build test WARNING on akpm-mm/mm-everything]

url:    https://github.com/intel-lab-lkp/linux/commits/Yury-Norov/mm-don-t-allow-empty-relative-nodemask-in-mpol_relative_nodemask/20260529-030835
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20260528190337.878027-1-ynorov%40nvidia.com
patch subject: [PATCH] mm: don't allow empty relative nodemask in mpol_relative_nodemask()
config: sparc64-randconfig-002-20260529 (https://download.01.org/0day-ci/archive/20260529/[email protected]/config)
compiler: sparc64-linux-gcc (GCC) 8.5.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260529/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

mm/mempolicy.c: In function 'mpol_relative_nodemask':

>> mm/mempolicy.c:377:10: warning: 'return' with a value, in function returning void
      return -EINVAL;
             ^

mm/mempolicy.c:370:13: note: declared here
static void mpol_relative_nodemask(nodemask_t *ret, const nodemask_t *orig,
^~~~~~~~~~~~~~~~~~~~~~

vim +/return +377 mm/mempolicy.c

369
370 static void mpol_relative_nodemask(nodemask_t *ret, const nodemask_t *orig,
371 const nodemask_t *rel)
372 {
373 unsigned int w = nodes_weight(*rel);
374 nodemask_t tmp;
375
376 if (w == 0)

 > 377			return -EINVAL;

378
379 nodes_fold(tmp, *orig, w);
380 nodes_onto(*ret, tmp, *rel);
381 }
382

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

#40 lore raw
In reply to Yury Norov
Quoted 5 lines · Yury Norov

Reassigning nodes relative an empty user-provided nodemask is useless, and triggers divide-by-zero in the function.

Likely this should be a

Closes:

And be accompanied by a Fixes: and Cc stable.

mm/mempolicy.c static inline int mpol_store_user_nodemask(const struct mempolicy *pol) · line 372

Quoted 13 lines
---
 mm/mempolicy.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
mm/mempolicy.c +0 -0
index 4e4421b22b59..cd961fa1eb33 100644
@@ -370,8 +370,13 @@ static inline int mpol_store_user_nodemask(const struct mempolicy *pol)
 static void mpol_relative_nodemask(nodemask_t *ret, const nodemask_t *orig,
 				   const nodemask_t *rel)
 {

Continuing the discussion of the context in the other thread :)

Signature · 3 lines
Cheers,

David