mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Karl Mehltretter <kmehltretter@gmail.com>
Cc: Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
	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: Thu, 3 Dec 2026 14:05:19 +0100	[thread overview]
Message-ID: <20261203130519.GC677025@noisy.programming.kicks-ass.net> (raw)
In-Reply-To: <axDIghJxSZK_eoV2@gmail.com>

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);

  reply	other threads:[~2026-12-03 13:05 UTC|newest]

Thread overview: 15+ 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
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 [this message]
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

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=20261203130519.GC677025@noisy.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=bigeasy@linutronix.de \
    --cc=bsegall@google.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=juri.lelli@redhat.com \
    --cc=kmehltretter@gmail.com \
    --cc=kprateek.nayak@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --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®