mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [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
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ 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] 5+ 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
  2 siblings, 0 replies; 5+ 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] 5+ 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
  2 siblings, 0 replies; 5+ 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] 5+ 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
  2 siblings, 1 reply; 5+ 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] 5+ 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
  0 siblings, 0 replies; 5+ 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] 5+ messages in thread

end of thread, other threads:[~2026-11-25 19:37 UTC | newest]

Thread overview: 5+ 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

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®