From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=90.155.92.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796303132; b=pDhI+suoA80EJLv8yDs5abKHHgVHYApMd9o/jY7jeUU/KV5+jYvsx6y8B+N7r7yuZF3q o420JUodBxHEcgNyKLLXmwypBAHi6adQHJ442jvQYZW+qyWUM4JjmXJwnvcyccyWnw9xJ G7FuEafLInr5i6L/PApYsVGg5ylE1JeEUZmlG4hn0YmUvGvdTJOAPFyDx43OGtlYV3MiI n3lKYDg7KQf8lgnbdjpK/y26Qu+6XpTE0dvYsyskcZy+MjMGG5Z9Wh5zzHCHzu30R24DE XPluiZlasIt/PBkg/7nskbL2nz2gR77dzV0uXLlYnb/Qu/CJ8NuQNHbiW/ovk5b7P3g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796303132; h=DMARC-Filter:DKIM-Signature:Received:Received:Date:From:To:Cc: Subject:Message-ID:References:MIME-Version:Content-Type: Content-Disposition:In-Reply-To; bh=ywJKjnaIIO58R0Imd/BRtqYvonWFjhOFJXLxhmWAPUU=; b=V4Mp+ALgK7i0/1dgE6laRSwEB6ipn46c+IXjcUmK04hMV9+JVpmmRyJceAICR19cZATL sdk9MOxc7qNLB90jrjuaAPsI+u/8cESe+A81ca3mbranUgcnBz3R7mprOUoZQp9pLurB8 Q2z57TqadIpWi1S6GDMHzy9IDHETE4XoG7buYr6dfJCfAqpzepJnAHA7MmCU6B/xmG+PJ R1+3je6RFNRhxFG1pSu19HSJ33+jOOdbTDT48p53x7j9DlCuHR09NHpCcOeMYdqoqH0qx TaHHt3D3y9M2YsJ5MzkBzvOP9Fqff5hKOgRFo/BVVARpzEPdp7fiQgmm+43TThtVeaw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=infradead.org; spf=pass smtp.mailfrom=infradead.org; dkim=pass header.d=infradead.org header.i=@infradead.org header.a=rsa-sha256 header.s=desiato.20200630 header.b=ndK8IsqF; arc=none smtp.remote-ip=90.155.92.199 Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 14102606A332 for ; Thu, 3 Dec 2026 13:05:30 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 14102606A332 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.a=rsa-sha256 header.s=desiato.20200630 header.b=ndK8IsqF DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=ywJKjnaIIO58R0Imd/BRtqYvonWFjhOFJXLxhmWAPUU=; b=ndK8IsqF4+snFwUWwxGpBhFOoY 0X7CAWNA6StotX53ZbuR0XHUYDDyrqVw70lndmvSamxXC5mniQFEnTUGi4MS7hYGZn7iABLW8dFVl bNuA9l2+/9kAHoZpxLfP7MVAwTSnq5Hi0R/oyI0il74m22v8qlvQ4xMvIaIZITLt70tnfpwDrLcp0 JaH1oV2s35/UT9vAkoR4qXdZjVItzt2qhtbJPzS8fDE8sgiH4UQBklVyADGzknwsJKvf0SgPpHEXM bZr/+E6g+HsY+ll2jumkFBeuB3QUvxxEnuxAAj2hX41pVnNJYiS10fYJWN8Dlm4LIFIUs7REI+CCh 203h2N/w==; Received: from 77-249-17-252.cable.dynamic.v4.ziggo.nl ([77.249.17.252] helo=noisy.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.100.1 #2 (Red Hat Linux)) id 1xZ6Uz-0000000DI9b-0c8N; Thu, 03 Dec 2026 13:05:21 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 1000) id 798FB30045A; Thu, 03 Dec 2026 14:05:19 +0100 (CET) Date: Thu, 3 Dec 2026 14:05:19 +0100 From: Peter Zijlstra To: Karl Mehltretter Cc: Sebastian Andrzej Siewior , linux-kernel@vger.kernel.org, syzbot , Thomas Gleixner , Radu Rendec , Ingo Molnar , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Valentin Schneider , K Prateek Nayak Subject: Re: [PATCH v3] sched: Reset cpumask if __migrate_task() failed for migration_cpu_stop() Message-ID: <20261203130519.GC677025@noisy.programming.kicks-ass.net> References: <20261120171658.Cf9qIU-Y@linutronix.de> <20261125172018.GD2668564@noisy.programming.kicks-ass.net> <20261125193735.qrxCdHTx@linutronix.de> <20261126104049.GC49833@noisy.programming.kicks-ass.net> <20261128172430.69629-1-kmehltretter@gmail.com> <20261202144240.GG2668564@noisy.programming.kicks-ass.net> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: 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);