* [PATCH v3] sched: Reset cpumask if __migrate_task() failed for migration_cpu_stop()
@ 2026-11-20 17:16 Sebastian Andrzej Siewior
2026-11-21 13:20 ` Karl Mehltretter
` (3 more replies)
0 siblings, 4 replies; 15+ messages in thread
From: Sebastian Andrzej Siewior @ 2026-11-20 17:16 UTC (permalink / raw)
To: linux-kernel
Cc: Peter Zijlstra, syzbot, Thomas Gleixner, Radu Rendec,
Ingo Molnar, Juri Lelli, Vincent Guittot, Dietmar Eggemann,
Steven Rostedt, Ben Segall, Mel Gorman, Valentin Schneider,
K Prateek Nayak
syzbot reported a race of assigning a CPU affinity during CPU hotplug
operation.
After the affinity of an IRQ has been changed, the IRQ-core sets
IRQTF_AFFINITY and wakes the relevant interrupt threads which need to
adjust their affinity mask. The thread will then invoke
set_cpus_allowed_ptr() to update the mask. Based on the new mask a CPU
is chosen on which the thread should run.
It is verified that this CPU is online however there is no guarantee
that this CPU remains online while affine_move_task() is moving _this_
task. Since the current task requests the migration the stopper/
migration_cpu_stop() is involved at which point the task pauses for a
while. If the CPU goes offline (or is no longer cpu_online_mask) then
__migrate_task() (due to is_cpu_allowed() reject) will return the rq of
the CPU on which the task is currently running (which does not match
it's task_struct::cpus_mask).
The aftermath:
The IRQ-thread's CPU and task's cpus_mask do not match. A
migrate_disable()-> schedule() will change task_struct::cpus_ptr
ensuring that the following migrate_enable() will update the task to the
requested affinity mask. At this point, affine_move_task() expects
task_struct::migration_pending set but it is NULL because noone
requested an affinity change while the task was in migrate-disable
section.
There are a few spots where set_cpus_allowed_ptr() does not use
cpus_read_lock(). In the IRQ thread case, there will be an update
following if the CPU goes offline so it is not really needed to block
CPU hotplug during the update.
If __migrate_task() fails, set the CPU mask of the task to runqueue it
is currently on. Use do_set_cpus_allowed() for this so it also invokes
class' set_cpus_allowed callback.
Remove the comment about __migrate_task() failing.
Reported-by: syzbot+6835d5c11145e4f77057@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/all/6a9919ac.94649fcc.25487e.0005.GAE@google.com/
Fixes: af449901b84c9 ("sched: Add migrate_disable()")
Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
---
This survives my testcase without any warnings.
v2…v3: https://lore.kernel.org/all/20261012103946.aOSAmrRe@linutronix.de/
- Drop RFC and with it the irq_thread_check_affinity() since the
problem is not limited to it.
- Instead of manually setting the CPU mask bit, use
do_set_cpus_allowed() which should handle the DL bits correctly.
v1…v2: https://lore.kernel.org/all/20260907085825.f-CZ1Q5y@linutronix.de/
- Repost with collected notes.
kernel/sched/core.c | 15 +++++++--------
1 file changed, 7 insertions(+), 8 deletions(-)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 5ed660f099a55..c95d16ba9fe36 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -2710,17 +2710,16 @@ static int migration_cpu_stop(void *data)
if (task_on_rq_queued(p)) {
update_rq_clock(rq);
rq = __migrate_task(rq, &rf, p, arg->dest_cpu);
+ if (rq != cpu_rq(arg->dest_cpu)) {
+ struct affinity_context ac = {
+ .new_mask = cpumask_of(rq->cpu),
+ };
+
+ do_set_cpus_allowed(p, &ac);
+ }
} else {
p->wake_cpu = arg->dest_cpu;
}
-
- /*
- * XXX __migrate_task() can fail, at which point we might end
- * up running on a dodgy CPU, AFAICT this can only happen
- * during CPU hotplug, at which point we'll get pushed out
- * anyway, so it's probably not a big deal.
- */
-
} else if (pending) {
/*
* This happens when we get migrated between migrate_enable()'s
--
2.55.0
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v3] sched: Reset cpumask if __migrate_task() failed for migration_cpu_stop()
2026-11-20 17:16 [PATCH v3] sched: Reset cpumask if __migrate_task() failed for migration_cpu_stop() Sebastian Andrzej Siewior
@ 2026-11-21 13:20 ` Karl Mehltretter
2026-11-21 14:39 ` Karl Mehltretter
` (2 subsequent siblings)
3 siblings, 0 replies; 15+ messages in thread
From: Karl Mehltretter @ 2026-11-21 13:20 UTC (permalink / raw)
To: Sebastian Andrzej Siewior
Cc: Karl Mehltretter, linux-kernel, Peter Zijlstra, syzbot,
Thomas Gleixner, Radu Rendec, Ingo Molnar, Juri Lelli,
Vincent Guittot, Dietmar Eggemann, Steven Rostedt, Ben Segall,
Mel Gorman, Valentin Schneider, K Prateek Nayak
On Fri, 20 Nov 2026 18:16:58 +0100, Sebastian Andrzej Siewior wrote:
> if (task_on_rq_queued(p)) {
> update_rq_clock(rq);
> rq = __migrate_task(rq, &rf, p, arg->dest_cpu);
> + if (rq != cpu_rq(arg->dest_cpu)) {
> + struct affinity_context ac = {
> + .new_mask = cpumask_of(rq->cpu),
> + };
> +
> + do_set_cpus_allowed(p, &ac);
> + }
do_set_cpus_allowed() updates the rq clock a second time here, through
sched_change_begin() without DEQUEUE_NOCLOCK. With WARN_DOUBLE_CLOCK
enabled:
WARNING: kernel/sched/core.c:881 at update_rq_clock+0x4c7/0x560, CPU#0: migration/0/22
Stopper: migration_cpu_stop+0x0/0xbe0 <- __set_cpus_allowed_ptr_locked+0xd1e/0x19c0
sched_change_begin+0x14d/0x660
migration_cpu_stop+0x5f6/0xbe0
cpu_stopper_thread+0x25e/0x3f0
Seen on v7.4-rc3 with this patch (RT and non-RT) with sched_setaffinity() loops
while another loop writes off and on to /sys/devices/system/cpu/smt/control.
Thanks,
Karl
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v3] sched: Reset cpumask if __migrate_task() failed for migration_cpu_stop()
2026-11-20 17:16 [PATCH v3] sched: Reset cpumask if __migrate_task() failed for migration_cpu_stop() Sebastian Andrzej Siewior
2026-11-21 13:20 ` Karl Mehltretter
@ 2026-11-21 14:39 ` Karl Mehltretter
2026-11-25 17:20 ` Peter Zijlstra
2026-12-03 11:25 ` Zhen Wang
3 siblings, 0 replies; 15+ messages in thread
From: Karl Mehltretter @ 2026-11-21 14:39 UTC (permalink / raw)
To: Sebastian Andrzej Siewior
Cc: Karl Mehltretter, linux-kernel, Peter Zijlstra, syzbot,
Thomas Gleixner, Radu Rendec, Ingo Molnar, Juri Lelli,
Vincent Guittot, Dietmar Eggemann, Steven Rostedt, Ben Segall,
Mel Gorman, Valentin Schneider, K Prateek Nayak
On Fri, 20 Nov 2026 18:16:58 +0100, Sebastian Andrzej Siewior wrote:
> syzbot reported a race of assigning a CPU affinity during CPU hotplug
> operation.
I tested the patch on PREEMPT_RT with syzbot's config, changing IRQ
affinities while SMT was switched off and on. The warning is gone with
the patch. Without it, every boot warned, and one also hit a BUG:
kernel BUG at kernel/sched/core.c:3642!
try_to_wake_up+0x85f/0x1320
irq_do_set_affinity+0x42c/0x5d0
RIP: 0010:select_fallback_rq+0x1f6/0x200
Kernel panic - not syncing: Fatal exception in interrupt
> There are a few spots where set_cpus_allowed_ptr() does not use
> cpus_read_lock(). In the IRQ thread case, there will be an update
> following if the CPU goes offline so it is not really needed to block
> CPU hotplug during the update.
sched_setaffinity() is one of those spots, but a user task gets no
later update.
With the patch, a test that changed the affinity of user threads in the
same setup read back a CPU outside the requested mask in about 1 of 5000
calls, on RT and non-RT. Without the patch it never did.
Thanks,
Karl
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v3] sched: Reset cpumask if __migrate_task() failed for migration_cpu_stop()
2026-11-20 17:16 [PATCH v3] sched: Reset cpumask if __migrate_task() failed for migration_cpu_stop() Sebastian Andrzej Siewior
2026-11-21 13:20 ` Karl Mehltretter
2026-11-21 14:39 ` Karl Mehltretter
@ 2026-11-25 17:20 ` Peter Zijlstra
2026-11-25 19:37 ` Sebastian Andrzej Siewior
2026-12-03 11:25 ` Zhen Wang
3 siblings, 1 reply; 15+ messages in thread
From: Peter Zijlstra @ 2026-11-25 17:20 UTC (permalink / raw)
To: Sebastian Andrzej Siewior
Cc: linux-kernel, syzbot, Thomas Gleixner, Radu Rendec, Ingo Molnar,
Juri Lelli, Vincent Guittot, Dietmar Eggemann, Steven Rostedt,
Ben Segall, Mel Gorman, Valentin Schneider, K Prateek Nayak
On Fri, Nov 20, 2026 at 06:16:58PM +0100, Sebastian Andrzej Siewior wrote:
> syzbot reported a race of assigning a CPU affinity during CPU hotplug
> operation.
> After the affinity of an IRQ has been changed, the IRQ-core sets
> IRQTF_AFFINITY and wakes the relevant interrupt threads which need to
> adjust their affinity mask. The thread will then invoke
> set_cpus_allowed_ptr() to update the mask. Based on the new mask a CPU
> is chosen on which the thread should run.
This is irq_thread_check_affinity(), right?
So the problem begins here, by selecting a CPU that isn't guarded to
remain online until after the operation completes.
> It is verified that this CPU is online however there is no guarantee
> that this CPU remains online while affine_move_task() is moving _this_
> task. Since the current task requests the migration the stopper/
> migration_cpu_stop() is involved at which point the task pauses for a
> while. If the CPU goes offline (or is no longer cpu_online_mask) then
> __migrate_task() (due to is_cpu_allowed() reject) will return the rq of
> the CPU on which the task is currently running (which does not match
> it's task_struct::cpus_mask).
Right.
> The aftermath:
> The IRQ-thread's CPU and task's cpus_mask do not match. A
> migrate_disable()-> schedule() will change task_struct::cpus_ptr
> ensuring that the following migrate_enable() will update the task to the
> requested affinity mask.
OK, so is this all afterwards, or interleaved? That is, are we talking
about:
A)
CPU-A CPU-B CPU-C
migrate_disable()
schedule()
migrate_disable_switch();
<hotplug unplug A> set_cpus_allowed()
migrate_enable()
or B)
CPU-A CPU-B CPU-C
<hotplug unplug A> set_cpus_allowed()
migrate_disable()
schedule()
migrate_disable_switch();
migrate_enable()
?
> At this point, affine_move_task() expects
> task_struct::migration_pending set but it is NULL because noone
> requested an affinity change while the task was in migrate-disable
> section.
We're talking about:
migrate_enable()
___migrate_able()
__set_cpus_allowed_ptr()
__set_cpus_allowed_ptr_locked()
affine_move_task();
Right? Where the target is a kthread while running on a CPU that isn't
inside p->cpus_mask (because p->cpus_mask intersected with online_mask
is empty).
So, affine_move_task() will fail the first branch; we're not inside
cpus_mask. And we'll fail the second branch, because SCA_MIGRATE_ENABLE,
and hit:
pending = p->migration_pending;
WARN_ON_ONCE(!pending);
OK.
> There are a few spots where set_cpus_allowed_ptr() does not use
> cpus_read_lock(). In the IRQ thread case, there will be an update
> following if the CPU goes offline so it is not really needed to block
> CPU hotplug during the update.
>
> If __migrate_task() fails, set the CPU mask of the task to runqueue it
> is currently on. Use do_set_cpus_allowed() for this so it also invokes
> class' set_cpus_allowed callback.
> Remove the comment about __migrate_task() failing.
>
> Reported-by: syzbot+6835d5c11145e4f77057@syzkaller.appspotmail.com
> Closes: https://lore.kernel.org/all/6a9919ac.94649fcc.25487e.0005.GAE@google.com/
> Fixes: af449901b84c9 ("sched: Add migrate_disable()")
> Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
> ---
>
> This survives my testcase without any warnings.
>
> v2…v3: https://lore.kernel.org/all/20261012103946.aOSAmrRe@linutronix.de/
> - Drop RFC and with it the irq_thread_check_affinity() since the
> problem is not limited to it.
> - Instead of manually setting the CPU mask bit, use
> do_set_cpus_allowed() which should handle the DL bits correctly.
>
> v1…v2: https://lore.kernel.org/all/20260907085825.f-CZ1Q5y@linutronix.de/
> - Repost with collected notes.
>
> kernel/sched/core.c | 15 +++++++--------
> 1 file changed, 7 insertions(+), 8 deletions(-)
>
> diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> index 5ed660f099a55..c95d16ba9fe36 100644
> --- a/kernel/sched/core.c
> +++ b/kernel/sched/core.c
> @@ -2710,17 +2710,16 @@ static int migration_cpu_stop(void *data)
> if (task_on_rq_queued(p)) {
> update_rq_clock(rq);
> rq = __migrate_task(rq, &rf, p, arg->dest_cpu);
> + if (rq != cpu_rq(arg->dest_cpu)) {
> + struct affinity_context ac = {
> + .new_mask = cpumask_of(rq->cpu),
> + };
> +
> + do_set_cpus_allowed(p, &ac);
> + }
> } else {
> p->wake_cpu = arg->dest_cpu;
> }
> -
> - /*
> - * XXX __migrate_task() can fail, at which point we might end
> - * up running on a dodgy CPU, AFAICT this can only happen
> - * during CPU hotplug, at which point we'll get pushed out
> - * anyway, so it's probably not a big deal.
> - */
> -
> } else if (pending) {
> /*
> * This happens when we get migrated between migrate_enable()'s
So yes, this cures things, but I'm not sure silently updating the mask
is nice. People could be surprised the cpumask isn't what they set --
then again, perhaps such people should disable hotplug when changing
affinity?
Let me ponder this while making dinner :-)
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v3] sched: Reset cpumask if __migrate_task() failed for migration_cpu_stop()
2026-11-25 17:20 ` Peter Zijlstra
@ 2026-11-25 19:37 ` Sebastian Andrzej Siewior
2026-11-26 10:40 ` Peter Zijlstra
0 siblings, 1 reply; 15+ messages in thread
From: Sebastian Andrzej Siewior @ 2026-11-25 19:37 UTC (permalink / raw)
To: Peter Zijlstra
Cc: linux-kernel, syzbot, Thomas Gleixner, Radu Rendec, Ingo Molnar,
Juri Lelli, Vincent Guittot, Dietmar Eggemann, Steven Rostedt,
Ben Segall, Mel Gorman, Valentin Schneider, K Prateek Nayak
On 2026-11-25 18:20:18 [+0100], Peter Zijlstra wrote:
> On Fri, Nov 20, 2026 at 06:16:58PM +0100, Sebastian Andrzej Siewior wrote:
> > syzbot reported a race of assigning a CPU affinity during CPU hotplug
> > operation.
> > After the affinity of an IRQ has been changed, the IRQ-core sets
> > IRQTF_AFFINITY and wakes the relevant interrupt threads which need to
> > adjust their affinity mask. The thread will then invoke
> > set_cpus_allowed_ptr() to update the mask. Based on the new mask a CPU
> > is chosen on which the thread should run.
>
> This is irq_thread_check_affinity(), right?
This is what the bot used to trigger this, correct.
> So the problem begins here, by selecting a CPU that isn't guarded to
> remain online until after the operation completes.
Right, we don't enforce this so everyone without a CPU-HP lock held has
a problem here. From irq_thread_check_affinity() perspective, it is fine
because once the CPU goes down, there will be another update aligning
the thread with the CPU that has been picked to serve the interrupt.
> > The aftermath:
> > The IRQ-thread's CPU and task's cpus_mask do not match. A
> > migrate_disable()-> schedule() will change task_struct::cpus_ptr
> > ensuring that the following migrate_enable() will update the task to the
> > requested affinity mask.
>
> OK, so is this all afterwards, or interleaved? That is, are we talking
> about:
>
> A)
> CPU-A CPU-B CPU-C
>
> migrate_disable()
> schedule()
> migrate_disable_switch();
> <hotplug unplug A> set_cpus_allowed()
>
> migrate_enable()
>
> or B)
>
> CPU-A CPU-B CPU-C
>
> <hotplug unplug A> set_cpus_allowed()
>
> migrate_disable()
> schedule()
> migrate_disable_switch();
>
> migrate_enable()
>
> ?
It is B) and CPU-A = CPU-C in this case.
CPU-A CPU-B
irq_thread()
irq_thread_check_affinity()
irq_thread_check_affinity
set_cpus_allowed_ptr(B)
<hotplug unplug>
<returns with butchered mask>
irq_forced_thread_fn()
migrate_disable();
schedule();
migrate_disable_switch();
migrate_enable();
> > At this point, affine_move_task() expects
> > task_struct::migration_pending set but it is NULL because noone
> > requested an affinity change while the task was in migrate-disable
> > section.
>
> We're talking about:
>
> migrate_enable()
> ___migrate_able()
> __set_cpus_allowed_ptr()
> __set_cpus_allowed_ptr_locked()
> affine_move_task();
>
> Right? Where the target is a kthread while running on a CPU that isn't
> inside p->cpus_mask (because p->cpus_mask intersected with online_mask
> is empty).
Almost. The requested CPU wasn't part of online_mask and it got rejected
earlier in is_cpu_allowed(). But now in __set_cpus_allowed_ptr_locked()
has this at the end
| dest_cpu = cpumask_any_and_distribute(cpu_valid_mask, ctx->new_mask);
| if (dest_cpu >= nr_cpu_ids) {
| ret = -EINVAL;
| goto out;
| }
and we don't take this exit. So the CPU is back.
> So, affine_move_task() will fail the first branch; we're not inside
> cpus_mask. And we'll fail the second branch, because SCA_MIGRATE_ENABLE,
> and hit:
>
> pending = p->migration_pending;
> WARN_ON_ONCE(!pending);
>
> OK.
Yes.
> > --- a/kernel/sched/core.c
> > +++ b/kernel/sched/core.c
> > @@ -2710,17 +2710,16 @@ static int migration_cpu_stop(void *data)
> > if (task_on_rq_queued(p)) {
> > update_rq_clock(rq);
> > rq = __migrate_task(rq, &rf, p, arg->dest_cpu);
> > + if (rq != cpu_rq(arg->dest_cpu)) {
> > + struct affinity_context ac = {
> > + .new_mask = cpumask_of(rq->cpu),
> > + };
> > +
> > + do_set_cpus_allowed(p, &ac);
> > + }
> > } else {
> > p->wake_cpu = arg->dest_cpu;
> > }
> > -
> > - /*
> > - * XXX __migrate_task() can fail, at which point we might end
> > - * up running on a dodgy CPU, AFAICT this can only happen
> > - * during CPU hotplug, at which point we'll get pushed out
> > - * anyway, so it's probably not a big deal.
> > - */
> > -
> > } else if (pending) {
> > /*
> > * This happens when we get migrated between migrate_enable()'s
>
> So yes, this cures things, but I'm not sure silently updating the mask
> is nice. People could be surprised the cpumask isn't what they set --
> then again, perhaps such people should disable hotplug when changing
> affinity?
Yes, exactly. Use the HP-lock if it is needed. As I said earlier, it is
like select_fallback_rq() but without the printk.
> Let me ponder this while making dinner :-)
Have a good one.
Sebastian
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v3] sched: Reset cpumask if __migrate_task() failed for migration_cpu_stop()
2026-11-25 19:37 ` Sebastian Andrzej Siewior
@ 2026-11-26 10:40 ` Peter Zijlstra
2026-11-28 17:24 ` Karl Mehltretter
2026-12-02 14:12 ` Sebastian Andrzej Siewior
0 siblings, 2 replies; 15+ messages in thread
From: Peter Zijlstra @ 2026-11-26 10:40 UTC (permalink / raw)
To: Sebastian Andrzej Siewior
Cc: linux-kernel, syzbot, Thomas Gleixner, Radu Rendec, Ingo Molnar,
Juri Lelli, Vincent Guittot, Dietmar Eggemann, Steven Rostedt,
Ben Segall, Mel Gorman, Valentin Schneider, K Prateek Nayak
On Wed, Nov 25, 2026 at 08:37:35PM +0100, Sebastian Andrzej Siewior wrote:
> On 2026-11-25 18:20:18 [+0100], Peter Zijlstra wrote:
> > On Fri, Nov 20, 2026 at 06:16:58PM +0100, Sebastian Andrzej Siewior wrote:
> > > syzbot reported a race of assigning a CPU affinity during CPU hotplug
> > > operation.
> > > After the affinity of an IRQ has been changed, the IRQ-core sets
> > > IRQTF_AFFINITY and wakes the relevant interrupt threads which need to
> > > adjust their affinity mask. The thread will then invoke
> > > set_cpus_allowed_ptr() to update the mask. Based on the new mask a CPU
> > > is chosen on which the thread should run.
> >
> > This is irq_thread_check_affinity(), right?
>
> This is what the bot used to trigger this, correct.
>
> > So the problem begins here, by selecting a CPU that isn't guarded to
> > remain online until after the operation completes.
>
> Right, we don't enforce this so everyone without a CPU-HP lock held has
> a problem here. From irq_thread_check_affinity() perspective, it is fine
> because once the CPU goes down, there will be another update aligning
> the thread with the CPU that has been picked to serve the interrupt.
Right, that is true in this case. But will that always be true for
people that 'forget' hotplug.
> It is B) and CPU-A = CPU-C in this case.
> CPU-A CPU-B
> irq_thread()
> irq_thread_check_affinity()
> irq_thread_check_affinity
> set_cpus_allowed_ptr(B)
> <hotplug unplug>
> <returns with butchered mask>
> irq_forced_thread_fn()
> migrate_disable();
> schedule();
> migrate_disable_switch();
>
> migrate_enable();
>
> > > At this point, affine_move_task() expects
> > > task_struct::migration_pending set but it is NULL because noone
> > > requested an affinity change while the task was in migrate-disable
> > > section.
> >
> > We're talking about:
> >
> > migrate_enable()
> > ___migrate_able()
> > __set_cpus_allowed_ptr()
> > __set_cpus_allowed_ptr_locked()
> > affine_move_task();
> >
> > Right? Where the target is a kthread while running on a CPU that isn't
> > inside p->cpus_mask (because p->cpus_mask intersected with online_mask
> > is empty).
>
> Almost. The requested CPU wasn't part of online_mask and it got rejected
> earlier in is_cpu_allowed(). But now in __set_cpus_allowed_ptr_locked()
> has this at the end
>
> | dest_cpu = cpumask_any_and_distribute(cpu_valid_mask, ctx->new_mask);
> | if (dest_cpu >= nr_cpu_ids) {
> | ret = -EINVAL;
> | goto out;
> | }
>
> and we don't take this exit. So the CPU is back.
Ooh, cute. So not only was the CPU unplugged, it was then quickly
brought back online again. Fun though; if we did hit this condition then
migrate_enable() would have silently failed. Also not a good thing.
We should probably have ___migrate_enable() warn on this.
> > So, affine_move_task() will fail the first branch; we're not inside
> > cpus_mask. And we'll fail the second branch, because SCA_MIGRATE_ENABLE,
> > and hit:
> >
> > pending = p->migration_pending;
> > WARN_ON_ONCE(!pending);
> >
> > OK.
>
> Yes.
>
> > > --- a/kernel/sched/core.c
> > > +++ b/kernel/sched/core.c
> > > @@ -2710,17 +2710,16 @@ static int migration_cpu_stop(void *data)
> > > if (task_on_rq_queued(p)) {
> > > update_rq_clock(rq);
> > > rq = __migrate_task(rq, &rf, p, arg->dest_cpu);
> > > + if (rq != cpu_rq(arg->dest_cpu)) {
> > > + struct affinity_context ac = {
> > > + .new_mask = cpumask_of(rq->cpu),
> > > + };
> > > +
> > > + do_set_cpus_allowed(p, &ac);
> > > + }
> > > } else {
> > > p->wake_cpu = arg->dest_cpu;
> > > }
> > > -
> > > - /*
> > > - * XXX __migrate_task() can fail, at which point we might end
> > > - * up running on a dodgy CPU, AFAICT this can only happen
> > > - * during CPU hotplug, at which point we'll get pushed out
> > > - * anyway, so it's probably not a big deal.
> > > - */
> > > -
> > > } else if (pending) {
> > > /*
> > > * This happens when we get migrated between migrate_enable()'s
> >
> > So yes, this cures things, but I'm not sure silently updating the mask
> > is nice. People could be surprised the cpumask isn't what they set --
> > then again, perhaps such people should disable hotplug when changing
> > affinity?
>
> Yes, exactly. Use the HP-lock if it is needed. As I said earlier, it is
> like select_fallback_rq() but without the printk.
Right -- so the printk is gated with p->mm, which kthreads will mostly
fail. So even if the timing was different and we'd have hit
select_fallback_rq() we'd not have seen a printk().
However, the 'fixup' is different, select_fallback_rq() will set the
widest possible mask, while this will set only the one CPU.
Then there also are __balance_push_cpu_stop() and
sched_non_preferred_cpu_push_stop() that use __migrate_task(). AFAICT
both of those should also be able to hit this task, both of them run it
through select_fallback_rq().
Does something like so make sense? That routes them all through
select_fallback_rq().
---
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 5ed660f099a5..6ec27dc5e0c2 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -2627,27 +2627,7 @@ struct set_affinity_pending {
struct migration_arg arg;
};
-/*
- * Move (not current) task off this CPU, onto the destination CPU. We're doing
- * this because either it can't run here any more (set_cpus_allowed()
- * away from this CPU, or CPU going down), or because we're
- * attempting to rebalance this task on exec (sched_exec).
- *
- * So we race with normal scheduler movements, but that's OK, as long
- * as the task is no longer on this CPU.
- */
-static struct rq *__migrate_task(struct rq *rq, struct rq_flags *rf,
- struct task_struct *p, int dest_cpu)
- __must_hold(__rq_lockp(rq))
-{
- /* Affinity changed (again). */
- if (!is_cpu_allowed(p, dest_cpu))
- return rq;
-
- rq = move_queued_task(rq, rf, p, dest_cpu);
-
- return rq;
-}
+static int select_fallback_rq(int cpu, struct task_struct *p);
/*
* migration_cpu_stop - this will be executed by a high-prio stopper thread
@@ -2670,7 +2650,7 @@ static int migration_cpu_stop(void *data)
local_irq_save(rf.flags);
/*
* We need to explicitly wake pending tasks before running
- * __migrate_task() such that we will not miss enforcing cpus_ptr
+ * move_queued_task() such that we will not miss enforcing cpus_ptr
* during wakeups, see set_cpus_allowed_ptr()'s TASK_WAKING test.
*/
flush_smp_call_function_queue();
@@ -2708,19 +2688,16 @@ static int migration_cpu_stop(void *data)
}
if (task_on_rq_queued(p)) {
+ int cpu = arg->dest_cpu;
+
update_rq_clock(rq);
- rq = __migrate_task(rq, &rf, p, arg->dest_cpu);
+ if (!is_cpu_allowed(p, cpu))
+ cpu = select_fallback_rq(rq->cpu, p);
+
+ rq = move_queued_task(rq, &rf, p, cpu);
} else {
p->wake_cpu = arg->dest_cpu;
}
-
- /*
- * XXX __migrate_task() can fail, at which point we might end
- * up running on a dodgy CPU, AFAICT this can only happen
- * during CPU hotplug, at which point we'll get pushed out
- * anyway, so it's probably not a big deal.
- */
-
} else if (pending) {
/*
* This happens when we get migrated between migrate_enable()'s
@@ -8415,7 +8392,7 @@ static int __balance_push_cpu_stop(void *arg)
rq_lock(rq, &rf);
update_rq_clock(rq);
if (task_rq(p) == rq && task_on_rq_queued(p))
- rq = __migrate_task(rq, &rf, p, cpu);
+ rq = move_queued_task(rq, &rf, p, cpu);
rq_unlock(rq, &rf);
}
@@ -11315,7 +11292,7 @@ static int sched_non_preferred_cpu_push_stop(void *arg)
update_rq_clock(rq);
if (task_rq(p) == rq && task_on_rq_queued(p)) {
- struct rq *dest_rq = __migrate_task(rq, &rf, p, cpu);
+ struct rq *dest_rq = move_queued_task(rq, &rf, p, cpu);
if (rq != dest_rq)
schedstat_inc(p->stats.nr_migrations_cpu_non_preferred);
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v3] sched: Reset cpumask if __migrate_task() failed for migration_cpu_stop()
2026-11-26 10:40 ` Peter Zijlstra
@ 2026-11-28 17:24 ` Karl Mehltretter
2026-12-02 14:26 ` Sebastian Andrzej Siewior
2026-12-02 14:42 ` Peter Zijlstra
2026-12-02 14:12 ` Sebastian Andrzej Siewior
1 sibling, 2 replies; 15+ messages in thread
From: Karl Mehltretter @ 2026-11-28 17:24 UTC (permalink / raw)
To: Peter Zijlstra, Sebastian Andrzej Siewior
Cc: Karl Mehltretter, linux-kernel, syzbot, Thomas Gleixner,
Radu Rendec, Ingo Molnar, Juri Lelli, Vincent Guittot,
Dietmar Eggemann, Steven Rostedt, Ben Segall, Mel Gorman,
Valentin Schneider, K Prateek Nayak
On Thu, 26 Nov 2026 11:40:49 +0100, Peter Zijlstra wrote:
> if (task_on_rq_queued(p)) {
> + int cpu = arg->dest_cpu;
> +
> update_rq_clock(rq);
> - rq = __migrate_task(rq, &rf, p, arg->dest_cpu);
> + if (!is_cpu_allowed(p, cpu))
> + cpu = select_fallback_rq(rq->cpu, p);
> +
> + rq = move_queued_task(rq, &rf, p, cpu);
This deadlocks.
select_fallback_rq() can end up in set_cpus_allowed_force(), which takes
rq->lock again.
That's similar issue as af13e5e437dc ("sched: Fix the do_set_cpus_allowed()
locking fix").
With your diff on tip/sched/core, IRQ/user affinity changes racing with CPU
hotplug hang on both RT and !RT (arm64 KVM):
WARNING: possible recursive locking detected
migration/0/21 is trying to acquire lock:
ffff00003fd7e620 (&rq->__lock){-...}-{2:2}, at: ___task_rq_lock+0x68/0x140
but task is already holding lock:
ffff00003fd7e620 (&rq->__lock){-...}-{2:2}, at: migration_cpu_stop+0xac/0x370
___task_rq_lock+0x68/0x140
set_cpus_allowed_force+0x54/0x100
select_fallback_rq+0xf4/0x180
migration_cpu_stop+0x35c/0x370
Dropping rq->lock around select_fallback_rq() and rechecking
task_rq(p) == rq and task_on_rq_queued(p) before updating the clock
gets the test through on both RT and !RT.
> if (task_rq(p) == rq && task_on_rq_queued(p))
> - rq = __migrate_task(rq, &rf, p, cpu);
> + rq = move_queued_task(rq, &rf, p, cpu);
[...]
> - struct rq *dest_rq = __migrate_task(rq, &rf, p, cpu);
> + struct rq *dest_rq = move_queued_task(rq, &rf, p, cpu);
Don't you still need the is_cpu_allowed() check here? Both push stoppers
do select_fallback_rq() before taking rq->lock.
Thanks,
Karl
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v3] sched: Reset cpumask if __migrate_task() failed for migration_cpu_stop()
2026-11-26 10:40 ` Peter Zijlstra
2026-11-28 17:24 ` Karl Mehltretter
@ 2026-12-02 14:12 ` Sebastian Andrzej Siewior
1 sibling, 0 replies; 15+ messages in thread
From: Sebastian Andrzej Siewior @ 2026-12-02 14:12 UTC (permalink / raw)
To: Peter Zijlstra
Cc: linux-kernel, syzbot, Thomas Gleixner, Radu Rendec, Ingo Molnar,
Juri Lelli, Vincent Guittot, Dietmar Eggemann, Steven Rostedt,
Ben Segall, Mel Gorman, Valentin Schneider, K Prateek Nayak
On 2026-11-26 11:40:49 [+0100], Peter Zijlstra wrote:
> > > So the problem begins here, by selecting a CPU that isn't guarded to
> > > remain online until after the operation completes.
> >
> > Right, we don't enforce this so everyone without a CPU-HP lock held has
> > a problem here. From irq_thread_check_affinity() perspective, it is fine
> > because once the CPU goes down, there will be another update aligning
> > the thread with the CPU that has been picked to serve the interrupt.
>
> Right, that is true in this case. But will that always be true for
> people that 'forget' hotplug.
We could have one interface for tglx and one for everyone else with a
warning with the HP lock is missing. I added a warning there and the
first one was kthread_affine_node() with a warning why it is okay if we
miss the CPU…
…
> Right -- so the printk is gated with p->mm, which kthreads will mostly
> fail. So even if the timing was different and we'd have hit
> select_fallback_rq() we'd not have seen a printk().
>
> However, the 'fixup' is different, select_fallback_rq() will set the
> widest possible mask, while this will set only the one CPU.
Yes, the current CPU we were on anyway.
> Then there also are __balance_push_cpu_stop() and
> sched_non_preferred_cpu_push_stop() that use __migrate_task(). AFAICT
> both of those should also be able to hit this task, both of them run it
> through select_fallback_rq().
>
> Does something like so make sense? That routes them all through
> select_fallback_rq().
The box freezes. migration_cpu_stop() takes the rq and then
select_fallback_rq() does the same:
| ============================================
| WARNING: possible recursive locking detected
| 7.4.0-rc5+ #16 Not tainted
| --------------------------------------------
| migration/0/21 is trying to acquire lock:
| ffff88817bc2e220 (&rq->__lock){-...}-{2:2}, at: ___task_rq_lock+0x4d/0x100
|
| but task is already holding lock:
| ffff88817bc2e220 (&rq->__lock){-...}-{2:2}, at: migration_cpu_stop+0x72/0x360
|
| other info that might help us debug this:
| Possible unsafe locking scenario:
|
| CPU0
| ----
| lock(&rq->__lock);
| lock(&rq->__lock);
|
| *** DEADLOCK ***
|
| May be due to missing lock nesting notation
|
| locks held by migration/0/21: 2, on CPU#0:
| #0: ffff888102b55f80 (&p->pi_lock){-...}-{2:2}, at: migration_cpu_stop+0x5c/0x360
| #1: ffff88817bc2e220 (&rq->__lock){-...}-{2:2}, at: migration_cpu_stop+0x72/0x360
|
| stack backtrace:
| CPU: 0 UID: 0 PID: 21 Comm: migration/0 Not tainted 7.4.0-rc5+ #16 PREEMPT_{RT,(lazy)}
| Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 2026.11+ds-1 11/26/2026
| Stopper: migration_cpu_stop+0x0/0x360 <- affine_move_task+0x459/0x620
| Call Trace:
| Call Trace:
| <TASK>
| dump_stack_lvl+0x5b/0x80
| print_deadlock_bug.cold+0xc0/0xe2
| __lock_acquire+0x1515/0x27e0
| lock_acquire+0xc2/0x2b0
| _raw_spin_lock_nested+0x2a/0x40
| ___task_rq_lock+0x4d/0x100
| set_cpus_allowed_force+0x45/0xd0
| select_fallback_rq+0x128/0x140
| migration_cpu_stop+0x34c/0x360
| cpu_stopper_thread+0x9d/0x140
| smpboot_thread_fn+0xf5/0x240
| kthread+0xe4/0x120
| ret_from_fork+0x2ba/0x390
| ret_from_fork_asm+0x11/0x20
| </TASK>
Sebastian
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v3] sched: Reset cpumask if __migrate_task() failed for migration_cpu_stop()
2026-11-28 17:24 ` Karl Mehltretter
@ 2026-12-02 14:26 ` Sebastian Andrzej Siewior
2026-12-02 14:42 ` Peter Zijlstra
1 sibling, 0 replies; 15+ messages in thread
From: Sebastian Andrzej Siewior @ 2026-12-02 14:26 UTC (permalink / raw)
To: Karl Mehltretter
Cc: Peter Zijlstra, linux-kernel, syzbot, Thomas Gleixner,
Radu Rendec, Ingo Molnar, Juri Lelli, Vincent Guittot,
Dietmar Eggemann, Steven Rostedt, Ben Segall, Mel Gorman,
Valentin Schneider, K Prateek Nayak
On 2026-11-28 18:24:30 [+0100], Karl Mehltretter wrote:
> On Thu, 26 Nov 2026 11:40:49 +0100, Peter Zijlstra wrote:
> > if (task_on_rq_queued(p)) {
> > + int cpu = arg->dest_cpu;
> > +
> > update_rq_clock(rq);
> > - rq = __migrate_task(rq, &rf, p, arg->dest_cpu);
> > + if (!is_cpu_allowed(p, cpu))
> > + cpu = select_fallback_rq(rq->cpu, p);
> > +
> > + rq = move_queued_task(rq, &rf, p, cpu);
>
> This deadlocks.
Ach you noticed this, too. Was a bit late…
Sebastian
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v3] sched: Reset cpumask if __migrate_task() failed for migration_cpu_stop()
2026-11-28 17:24 ` Karl Mehltretter
2026-12-02 14:26 ` Sebastian Andrzej Siewior
@ 2026-12-02 14:42 ` Peter Zijlstra
2026-12-03 1:50 ` Karl Mehltretter
1 sibling, 1 reply; 15+ messages in thread
From: Peter Zijlstra @ 2026-12-02 14:42 UTC (permalink / raw)
To: Karl Mehltretter
Cc: Sebastian Andrzej Siewior, linux-kernel, syzbot, Thomas Gleixner,
Radu Rendec, Ingo Molnar, Juri Lelli, Vincent Guittot,
Dietmar Eggemann, Steven Rostedt, Ben Segall, Mel Gorman,
Valentin Schneider, K Prateek Nayak
On Sat, Nov 28, 2026 at 06:24:30PM +0100, Karl Mehltretter wrote:
> On Thu, 26 Nov 2026 11:40:49 +0100, Peter Zijlstra wrote:
> > if (task_on_rq_queued(p)) {
> > + int cpu = arg->dest_cpu;
> > +
> > update_rq_clock(rq);
> > - rq = __migrate_task(rq, &rf, p, arg->dest_cpu);
> > + if (!is_cpu_allowed(p, cpu))
> > + cpu = select_fallback_rq(rq->cpu, p);
> > +
> > + rq = move_queued_task(rq, &rf, p, cpu);
>
> This deadlocks.
>
> select_fallback_rq() can end up in set_cpus_allowed_force(), which takes
> rq->lock again.
>
> That's similar issue as af13e5e437dc ("sched: Fix the do_set_cpus_allowed()
> locking fix").
>
> With your diff on tip/sched/core, IRQ/user affinity changes racing with CPU
> hotplug hang on both RT and !RT (arm64 KVM):
>
> WARNING: possible recursive locking detected
> migration/0/21 is trying to acquire lock:
> ffff00003fd7e620 (&rq->__lock){-...}-{2:2}, at: ___task_rq_lock+0x68/0x140
> but task is already holding lock:
> ffff00003fd7e620 (&rq->__lock){-...}-{2:2}, at: migration_cpu_stop+0xac/0x370
> ___task_rq_lock+0x68/0x140
> set_cpus_allowed_force+0x54/0x100
> select_fallback_rq+0xf4/0x180
> migration_cpu_stop+0x35c/0x370
>
> Dropping rq->lock around select_fallback_rq() and rechecking
> task_rq(p) == rq and task_on_rq_queued(p) before updating the clock
> gets the test through on both RT and !RT.
Durr. Dropping rq lock here makes my head hurt. Perhaps I'll try and
make __select_fallback_rq() or somesuch. I'll have a poke.
> > if (task_rq(p) == rq && task_on_rq_queued(p))
> > - rq = __migrate_task(rq, &rf, p, cpu);
> > + rq = move_queued_task(rq, &rf, p, cpu);
> [...]
> > - struct rq *dest_rq = __migrate_task(rq, &rf, p, cpu);
> > + struct rq *dest_rq = move_queued_task(rq, &rf, p, cpu);
>
> Don't you still need the is_cpu_allowed() check here? Both push stoppers
> do select_fallback_rq() before taking rq->lock.
All hold p->pi_lock, and hence IRQs and preemption are disabled, the
affinity mask it locked and hotplug will wait for an RCU grace period
due to deactivate_cpu or somesuch.
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v3] sched: Reset cpumask if __migrate_task() failed for migration_cpu_stop()
2026-12-02 14:42 ` Peter Zijlstra
@ 2026-12-03 1:50 ` Karl Mehltretter
2026-12-03 13:05 ` Peter Zijlstra
0 siblings, 1 reply; 15+ messages in thread
From: Karl Mehltretter @ 2026-12-03 1:50 UTC (permalink / raw)
To: Peter Zijlstra
Cc: Sebastian Andrzej Siewior, linux-kernel, syzbot, Thomas Gleixner,
Radu Rendec, Ingo Molnar, Juri Lelli, Vincent Guittot,
Dietmar Eggemann, Steven Rostedt, Ben Segall, Mel Gorman,
Valentin Schneider, K Prateek Nayak
On Wed, Dec 02, 2026 at 03:42:40PM +0100, Peter Zijlstra wrote:
> > > if (task_rq(p) == rq && task_on_rq_queued(p))
> > > - rq = __migrate_task(rq, &rf, p, cpu);
> > > + rq = move_queued_task(rq, &rf, p, cpu);
> > [...]
> > > - struct rq *dest_rq = __migrate_task(rq, &rf, p, cpu);
> > > + struct rq *dest_rq = move_queued_task(rq, &rf, p, cpu);
> >
> > Don't you still need the is_cpu_allowed() check here? Both push stoppers
> > do select_fallback_rq() before taking rq->lock.
>
> All hold p->pi_lock, and hence IRQs and preemption are disabled, the
> affinity mask it locked and hotplug will wait for an RCU grace period
> due to deactivate_cpu or somesuch.
>
yes, that makes sense for affinity and hotplug.
I'm still wondering about migrate_disable().
Unlike try_to_wake_up(), the push stoppers call select_fallback_rq()
before taking rq->lock. If p is running on another CPU with migration
disabled at that point, is_cpu_allowed() only checks cpu_online().
Could p then re-enable migration and move back to rq before
rq_lock() in sched_non_preferred_cpu_push_stop()?
Thanks,
Karl
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v3] sched: Reset cpumask if __migrate_task() failed for migration_cpu_stop()
2026-11-20 17:16 [PATCH v3] sched: Reset cpumask if __migrate_task() failed for migration_cpu_stop() Sebastian Andrzej Siewior
` (2 preceding siblings ...)
2026-11-25 17:20 ` Peter Zijlstra
@ 2026-12-03 11:25 ` Zhen Wang
2026-12-03 12:05 ` Sebastian Andrzej Siewior
3 siblings, 1 reply; 15+ messages in thread
From: Zhen Wang @ 2026-12-03 11:25 UTC (permalink / raw)
To: mingo, peterz, bigeasy
Cc: juri.lelli, vincent.guittot, dietmar.eggemann, rostedt, bsegall,
mgorman, vschneid, kprateek.nayak, bristot, tglx, radu,
syzbot+6835d5c11145e4f77057, linux-kernel, Xuewen.Yan,
Hao_hao.Wang, Ke.Wang
On Thu, Nov 26, 2026 at 11:40:49AM +0100, Peter Zijlstra wrote:
> Fun though; if we did hit this condition then
> migrate_enable() would have silently failed. Also not a good thing.
>
> We should probably have ___migrate_enable() warn on this.
We hit exactly this condition in testing. The difference from the
syzbot trace: the CPU stays offline, so the task's cpus_mask contains
no online CPU at all, and we *do* take the -EINVAL exit even with the
migration_disabled() mitigation switching to cpu_online_mask.
Two extra consequences we see, beyond the silently ignored affinity:
- __migrate_enable() only calls ___migrate_enable() when
p->cpus_ptr != &p->cpus_mask. With a stale cpus_ptr that branch is
taken on every enable: each later migrate_enable() re-enters
__set_cpus_allowed_ptr(), re-takes task_rq_lock() and fails again.
A healthy task returns early without taking any lock.
- That breaks migrate_enable() in constrained contexts. We see it
from a BPF program attached to trace_sched_switch that calls
migrate_enable(): the call takes task_rq_lock() on the runqueue
__schedule() already holds and deadlocks. With an intact cpus_ptr
the enable takes no lock, so the same program is fine.
-- >8 --
From: Zhen Wang <zhen.wang1@unisoc.com>
Date: Wed, 2 Dec 2026 16:47:03 +0800
Subject: [RFC PATCH] sched/core: Fix cpus_ptr leak leading to spinlock
recursion on CPU hotplug
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
When a task changes its affinity to a CPU that is dying,
__set_cpus_allowed_ptr can succeed in updating p->cpus_mask, but
affine_move_task / migration_cpu_stop may fail to migrate the task,
leaving it running on an out-of-affinity CPU.
If the task then enters migrate_disable(), migrate_disable_switch()
sets p->cpus_ptr to the per-CPU cpumask of the current CPU. When
migrate_enable() subsequently calls __set_cpus_allowed_ptr(),
cpumask_any_and_distribute() on p->cpus_mask returns an empty
result because the original target CPU is already offline, causing
__set_cpus_allowed_ptr_locked() to bail out without restoring
p->cpus_ptr back to &p->cpus_mask.
The stale cpus_ptr then causes any later migrate_enable() call
— including those invoked from BPF programs attached to
trace_sched_switch — to enter __set_cpus_allowed_ptr() and
attempt task_rq_lock() on a runqueue already locked by
__schedule(), triggering spinlock recursion.
The comment in __set_cpus_allowed_ptr_locked() already warns
about this:
"Specifically, migration_disabled() tasks must not fail the
cpumask_any_and_distribute() pick below, esp. so on
SCA_MIGRATE_ENABLE, otherwise we'll not call
set_cpus_allowed_common() and actually reset p->cpus_ptr."
However the existing mitigation (using cpu_online_mask for
migration_disabled tasks) is insufficient when the cpumask
contains only offline CPUs.
Fix this by restoring p->cpus_ptr to ctx->new_mask even when
a valid destination CPU cannot be found, when called under
SCA_MIGRATE_ENABLE. This is done under rq->lock and p->pi_lock,
satisfying the locking requirement for cpus_ptr updates.
Fixes: af449901b84c9 ("sched: Add migrate_disable()")
Signed-off-by: Zhen Wang <zhen.wang1@unisoc.com>
---
kernel/sched/core.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 8f5e15af3c4e..222dad4b73e3 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -3245,6 +3245,8 @@ static int __set_cpus_allowed_ptr_locked(struct task_struct *p,
*/
dest_cpu = cpumask_any_and_distribute(cpu_valid_mask, ctx->new_mask);
if (dest_cpu >= nr_cpu_ids) {
+ if (ctx->flags & SCA_MIGRATE_ENABLE)
+ p->cpus_ptr = ctx->new_mask;
ret = -EINVAL;
goto out;
}
--
2.34.1
________________________________
This email (including its attachments) is intended only for the person or entity to which it is addressed and may contain information that is privileged, confidential or otherwise protected from disclosure. Unauthorized use, dissemination, distribution or copying of this email or the information herein or taking any action in reliance on the contents of this email or the information herein, by anyone other than the intended recipient, or an employee or agent responsible for delivering the message to the intended recipient, is strictly prohibited. If you are not the intended recipient, please do not read, copy, use or disclose any part of this e-mail to others. Please notify the sender immediately and permanently delete this e-mail and any attachments if you received it in error. Internet communications cannot be guaranteed to be timely, secure, error-free or virus-free. The sender does not accept liability for any errors or omissions.
本邮件及其附件具有保密性质,受法律保护不得泄露,仅发送给本邮件所指特定收件人。严禁非经授权使用、宣传、发布或复制本邮件或其内容。若非该特定收件人,请勿阅读、复制、 使用或披露本邮件的任何内容。若误收本邮件,请从系统中永久性删除本邮件及所有附件,并以回复邮件的方式即刻告知发件人。无法保证互联网通信及时、安全、无误或防毒。发件人对任何错漏均不承担责任。
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v3] sched: Reset cpumask if __migrate_task() failed for migration_cpu_stop()
2026-12-03 11:25 ` Zhen Wang
@ 2026-12-03 12:05 ` Sebastian Andrzej Siewior
0 siblings, 0 replies; 15+ messages in thread
From: Sebastian Andrzej Siewior @ 2026-12-03 12:05 UTC (permalink / raw)
To: Zhen Wang
Cc: mingo, peterz, juri.lelli, vincent.guittot, dietmar.eggemann,
rostedt, bsegall, mgorman, vschneid, kprateek.nayak, bristot,
tglx, radu, syzbot+6835d5c11145e4f77057, linux-kernel,
Xuewen.Yan, Hao_hao.Wang, Ke.Wang
On 2026-12-03 19:25:14 [+0800], Zhen Wang wrote:
> On Thu, Nov 26, 2026 at 11:40:49AM +0100, Peter Zijlstra wrote:
> > Fun though; if we did hit this condition then
> > migrate_enable() would have silently failed. Also not a good thing.
> >
> > We should probably have ___migrate_enable() warn on this.
>
> We hit exactly this condition in testing. The difference from the
> syzbot trace: the CPU stays offline, so the task's cpus_mask contains
> no online CPU at all, and we *do* take the -EINVAL exit even with the
> migration_disabled() mitigation switching to cpu_online_mask.
>
> Two extra consequences we see, beyond the silently ignored affinity:
>
> - __migrate_enable() only calls ___migrate_enable() when
> p->cpus_ptr != &p->cpus_mask. With a stale cpus_ptr that branch is
> taken on every enable: each later migrate_enable() re-enters
> __set_cpus_allowed_ptr(), re-takes task_rq_lock() and fails again.
> A healthy task returns early without taking any lock.
Yeah.
>
> - That breaks migrate_enable() in constrained contexts. We see it
what is a constrained context?
> from a BPF program attached to trace_sched_switch that calls
> migrate_enable(): the call takes task_rq_lock() on the runqueue
> __schedule() already holds and deadlocks. With an intact cpus_ptr
> the enable takes no lock, so the same program is fine.
So the migrate_enable() didn't succeed in restoring the mask and the
balanced migrate_enable() with the scheduler does it again. And the BPF
code does migrate_en/dis so…
…
> --- a/kernel/sched/core.c
> +++ b/kernel/sched/core.c
> @@ -3245,6 +3245,8 @@ static int __set_cpus_allowed_ptr_locked(struct task_struct *p,
> */
> dest_cpu = cpumask_any_and_distribute(cpu_valid_mask, ctx->new_mask);
> if (dest_cpu >= nr_cpu_ids) {
> + if (ctx->flags & SCA_MIGRATE_ENABLE)
> + p->cpus_ptr = ctx->new_mask;
> ret = -EINVAL;
> goto out;
> }
This does not make it right, it just avoids the problem on enabled. The
mask still does not match the CPU it runs on. do_set_cpus_allowed() was
not yet invoked yet so that is nice.
Do you see the printk where select_fallback_rq() fixes the mask?
Sebastian
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v3] sched: Reset cpumask if __migrate_task() failed for migration_cpu_stop()
2026-12-03 1:50 ` Karl Mehltretter
@ 2026-12-03 13:05 ` Peter Zijlstra
2026-12-03 14:19 ` Sebastian Andrzej Siewior
0 siblings, 1 reply; 15+ messages in thread
From: Peter Zijlstra @ 2026-12-03 13:05 UTC (permalink / raw)
To: Karl Mehltretter
Cc: Sebastian Andrzej Siewior, linux-kernel, syzbot, Thomas Gleixner,
Radu Rendec, Ingo Molnar, Juri Lelli, Vincent Guittot,
Dietmar Eggemann, Steven Rostedt, Ben Segall, Mel Gorman,
Valentin Schneider, K Prateek Nayak
On Thu, Dec 03, 2026 at 02:50:42AM +0100, Karl Mehltretter wrote:
> On Wed, Dec 02, 2026 at 03:42:40PM +0100, Peter Zijlstra wrote:
> > > > if (task_rq(p) == rq && task_on_rq_queued(p))
> > > > - rq = __migrate_task(rq, &rf, p, cpu);
> > > > + rq = move_queued_task(rq, &rf, p, cpu);
> > > [...]
> > > > - struct rq *dest_rq = __migrate_task(rq, &rf, p, cpu);
> > > > + struct rq *dest_rq = move_queued_task(rq, &rf, p, cpu);
> > >
> > > Don't you still need the is_cpu_allowed() check here? Both push stoppers
> > > do select_fallback_rq() before taking rq->lock.
> >
> > All hold p->pi_lock, and hence IRQs and preemption are disabled, the
> > affinity mask it locked and hotplug will wait for an RCU grace period
> > due to deactivate_cpu or somesuch.
> >
>
> yes, that makes sense for affinity and hotplug.
>
> I'm still wondering about migrate_disable().
>
> Unlike try_to_wake_up(), the push stoppers call select_fallback_rq()
> before taking rq->lock. If p is running on another CPU with migration
> disabled at that point, is_cpu_allowed() only checks cpu_online().
>
> Could p then re-enable migration and move back to rq before
> rq_lock() in sched_non_preferred_cpu_push_stop()?
I am not quite seeing the race; everything is serialized by p->pi_lock,
no?
Draw me a picture?
The below patch boots without burning the machine down.
---
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -1933,6 +1933,7 @@ extern int dl_bw_alloc(int cpu, u64 dl_b
extern void dl_bw_free(int cpu, u64 dl_bw);
/* set_cpus_allowed_force() - consider using set_cpus_allowed_ptr() instead */
+extern void __set_cpus_allowed_force(struct task_struct *p, const struct cpumask *new_mask);
extern void set_cpus_allowed_force(struct task_struct *p, const struct cpumask *new_mask);
/**
--- a/kernel/cgroup/cpuset.c
+++ b/kernel/cgroup/cpuset.c
@@ -4305,7 +4305,7 @@ bool cpuset_cpus_allowed_fallback(struct
rcu_read_lock();
cs_mask = task_cs(tsk)->cpus_allowed;
if (is_in_v2_mode() && cpumask_subset(cs_mask, possible_mask)) {
- set_cpus_allowed_force(tsk, cs_mask);
+ __set_cpus_allowed_force(tsk, cs_mask);
changed = true;
}
rcu_read_unlock();
--- a/kernel/kthread.c
+++ b/kernel/kthread.c
@@ -570,8 +570,7 @@ static void __kthread_bind_mask(struct t
return;
}
- scoped_guard (raw_spinlock_irqsave, &p->pi_lock)
- set_cpus_allowed_force(p, mask);
+ set_cpus_allowed_force(p, mask);
/* It's safe because the task is inactive. */
p->flags |= PF_NO_SETAFFINITY;
@@ -864,8 +863,7 @@ int kthread_affine_preferred(struct task
list_add_tail(&kthread->affinity_node, &kthread_affinity_list);
kthread_fetch_affinity(kthread, affinity);
- scoped_guard (raw_spinlock_irqsave, &p->pi_lock)
- set_cpus_allowed_force(p, affinity);
+ set_cpus_allowed_force(p, affinity);
mutex_unlock(&kthread_affinity_lock);
out:
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -2627,27 +2627,7 @@ struct set_affinity_pending {
struct migration_arg arg;
};
-/*
- * Move (not current) task off this CPU, onto the destination CPU. We're doing
- * this because either it can't run here any more (set_cpus_allowed()
- * away from this CPU, or CPU going down), or because we're
- * attempting to rebalance this task on exec (sched_exec).
- *
- * So we race with normal scheduler movements, but that's OK, as long
- * as the task is no longer on this CPU.
- */
-static struct rq *__migrate_task(struct rq *rq, struct rq_flags *rf,
- struct task_struct *p, int dest_cpu)
- __must_hold(__rq_lockp(rq))
-{
- /* Affinity changed (again). */
- if (!is_cpu_allowed(p, dest_cpu))
- return rq;
-
- rq = move_queued_task(rq, rf, p, dest_cpu);
-
- return rq;
-}
+static int __select_fallback_rq(int cpu, struct task_struct *p);
/*
* migration_cpu_stop - this will be executed by a high-prio stopper thread
@@ -2670,7 +2650,7 @@ static int migration_cpu_stop(void *data
local_irq_save(rf.flags);
/*
* We need to explicitly wake pending tasks before running
- * __migrate_task() such that we will not miss enforcing cpus_ptr
+ * move_queued_task() such that we will not miss enforcing cpus_ptr
* during wakeups, see set_cpus_allowed_ptr()'s TASK_WAKING test.
*/
flush_smp_call_function_queue();
@@ -2708,19 +2688,16 @@ static int migration_cpu_stop(void *data
}
if (task_on_rq_queued(p)) {
+ int cpu = arg->dest_cpu;
+
+ if (!is_cpu_allowed(p, cpu))
+ cpu = __select_fallback_rq(rq->cpu, p);
+
update_rq_clock(rq);
- rq = __migrate_task(rq, &rf, p, arg->dest_cpu);
+ rq = move_queued_task(rq, &rf, p, cpu);
} else {
p->wake_cpu = arg->dest_cpu;
}
-
- /*
- * XXX __migrate_task() can fail, at which point we might end
- * up running on a dodgy CPU, AFAICT this can only happen
- * during CPU hotplug, at which point we'll get pushed out
- * anyway, so it's probably not a big deal.
- */
-
} else if (pending) {
/*
* This happens when we get migrated between migrate_enable()'s
@@ -2846,7 +2823,7 @@ do_set_cpus_allowed(struct task_struct *
* Used for kthread_bind() and select_fallback_rq(), in both cases the user
* affinity (if any) should be destroyed too.
*/
-void set_cpus_allowed_force(struct task_struct *p, const struct cpumask *new_mask)
+void __set_cpus_allowed_force(struct task_struct *p, const struct cpumask *new_mask)
{
struct affinity_context ac = {
.new_mask = new_mask,
@@ -2858,8 +2835,7 @@ void set_cpus_allowed_force(struct task_
struct rcu_head rcu;
};
- scoped_guard (__task_rq_lock, p)
- do_set_cpus_allowed(p, &ac);
+ do_set_cpus_allowed(p, &ac);
/*
* Because this is called with p->pi_lock held, it is not possible
@@ -2869,6 +2845,12 @@ void set_cpus_allowed_force(struct task_
kfree_rcu((union cpumask_rcuhead *)ac.user_mask, rcu);
}
+void set_cpus_allowed_force(struct task_struct *p, const struct cpumask *new_mask)
+{
+ guard(task_rq_lock)(p);
+ __set_cpus_allowed_force(p, new_mask);
+}
+
int dup_user_cpus_ptr(struct task_struct *dst, struct task_struct *src,
int node)
{
@@ -3595,7 +3577,7 @@ EXPORT_SYMBOL_GPL(kick_process);
* select_task_rq() below may allow selection of !active CPUs in order
* to satisfy the above rules.
*/
-static int select_fallback_rq(int cpu, struct task_struct *p)
+static int __select_fallback_rq(int cpu, struct task_struct *p)
{
int nid = cpu_to_node(cpu);
const struct cpumask *nodemask = NULL;
@@ -3635,7 +3617,7 @@ static int select_fallback_rq(int cpu, s
}
fallthrough;
case possible:
- set_cpus_allowed_force(p, task_cpu_fallback_mask(p));
+ __set_cpus_allowed_force(p, task_cpu_fallback_mask(p));
state = fail;
break;
case fail:
@@ -3660,6 +3642,12 @@ static int select_fallback_rq(int cpu, s
return dest_cpu;
}
+static int select_fallback_rq(int cpu, struct task_struct *p)
+{
+ guard(__task_rq_lock)(p);
+ return __select_fallback_rq(cpu, p);
+}
+
/*
* The caller (fork, wakeup) owns p->pi_lock, ->cpus_ptr is stable.
*/
@@ -8415,7 +8403,7 @@ static int __balance_push_cpu_stop(void
rq_lock(rq, &rf);
update_rq_clock(rq);
if (task_rq(p) == rq && task_on_rq_queued(p))
- rq = __migrate_task(rq, &rf, p, cpu);
+ rq = move_queued_task(rq, &rf, p, cpu);
rq_unlock(rq, &rf);
}
@@ -11326,7 +11314,7 @@ static int sched_non_preferred_cpu_push_
update_rq_clock(rq);
if (task_rq(p) == rq && task_on_rq_queued(p)) {
- struct rq *dest_rq = __migrate_task(rq, &rf, p, cpu);
+ struct rq *dest_rq = move_queued_task(rq, &rf, p, cpu);
if (rq != dest_rq)
schedstat_inc(p->stats.nr_migrations_cpu_non_preferred);
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v3] sched: Reset cpumask if __migrate_task() failed for migration_cpu_stop()
2026-12-03 13:05 ` Peter Zijlstra
@ 2026-12-03 14:19 ` Sebastian Andrzej Siewior
0 siblings, 0 replies; 15+ messages in thread
From: Sebastian Andrzej Siewior @ 2026-12-03 14:19 UTC (permalink / raw)
To: Peter Zijlstra
Cc: Karl Mehltretter, linux-kernel, syzbot, Thomas Gleixner,
Radu Rendec, Ingo Molnar, Juri Lelli, Vincent Guittot,
Dietmar Eggemann, Steven Rostedt, Ben Segall, Mel Gorman,
Valentin Schneider, K Prateek Nayak
On 2026-12-03 14:05:19 [+0100], Peter Zijlstra wrote:
> The below patch boots without burning the machine down.
It seems to work, I don't see any fallout.
…
> @@ -11326,7 +11314,7 @@ static int sched_non_preferred_cpu_push_
> update_rq_clock(rq);
>
> if (task_rq(p) == rq && task_on_rq_queued(p)) {
There is now a
!is_migration_disabled(p))
here in tree as of rc5
> - struct rq *dest_rq = __migrate_task(rq, &rf, p, cpu);
> + struct rq *dest_rq = move_queued_task(rq, &rf, p, cpu);
>
> if (rq != dest_rq)
> schedstat_inc(p->stats.nr_migrations_cpu_non_preferred);
Sebastian
^ permalink raw reply [flat|nested] 15+ messages in thread
end of thread, other threads:[~2026-12-03 14:19 UTC | newest]
Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-20 17:16 [PATCH v3] sched: Reset cpumask if __migrate_task() failed for migration_cpu_stop() Sebastian Andrzej Siewior
2026-11-21 13:20 ` Karl Mehltretter
2026-11-21 14:39 ` Karl Mehltretter
2026-11-25 17:20 ` Peter Zijlstra
2026-11-25 19:37 ` Sebastian Andrzej Siewior
2026-11-26 10:40 ` Peter Zijlstra
2026-11-28 17:24 ` Karl Mehltretter
2026-12-02 14:26 ` Sebastian Andrzej Siewior
2026-12-02 14:42 ` Peter Zijlstra
2026-12-03 1:50 ` Karl Mehltretter
2026-12-03 13:05 ` Peter Zijlstra
2026-12-03 14:19 ` Sebastian Andrzej Siewior
2026-12-02 14:12 ` Sebastian Andrzej Siewior
2026-12-03 11:25 ` Zhen Wang
2026-12-03 12:05 ` Sebastian Andrzej Siewior
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®