mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
To: Peter Zijlstra <peterz@infradead.org>
Cc: linux-kernel@vger.kernel.org,
	syzbot <syzbot+6835d5c11145e4f77057@syzkaller.appspotmail.com>,
	Thomas Gleixner <tglx@kernel.org>, Radu Rendec <radu@rendec.net>,
	Ingo Molnar <mingo@redhat.com>,
	Juri Lelli <juri.lelli@redhat.com>,
	Vincent Guittot <vincent.guittot@linaro.org>,
	Dietmar Eggemann <dietmar.eggemann@arm.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	Ben Segall <bsegall@google.com>, Mel Gorman <mgorman@suse.de>,
	Valentin Schneider <vschneid@redhat.com>,
	K Prateek Nayak <kprateek.nayak@amd.com>
Subject: Re: [PATCH v3] sched: Reset cpumask if __migrate_task() failed for migration_cpu_stop()
Date: Wed, 25 Nov 2026 20:37:35 +0100	[thread overview]
Message-ID: <20261125193735.qrxCdHTx@linutronix.de> (raw)
In-Reply-To: <20261125172018.GD2668564@noisy.programming.kicks-ass.net>

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

      reply	other threads:[~2026-11-25 19:37 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 17:16 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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261125193735.qrxCdHTx@linutronix.de \
    --to=bigeasy@linutronix.de \
    --cc=bsegall@google.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=juri.lelli@redhat.com \
    --cc=kprateek.nayak@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=radu@rendec.net \
    --cc=rostedt@goodmis.org \
    --cc=syzbot+6835d5c11145e4f77057@syzkaller.appspotmail.com \
    --cc=tglx@kernel.org \
    --cc=vincent.guittot@linaro.org \
    --cc=vschneid@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®