FAILED: patch "[PATCH] sched/core: Make core-sched flips wait for in-flight" failed to apply to 6.18-stable tree
Subsystems (2)
[email protected]
maintainerThe patch below does not apply to the 6.18-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.18.y
git checkout FETCH_HEAD
git cherry-pick -x f3629c63a4af3e491381780bc6c123cb498c4c40
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026090310-surround-endnote-4269@gregkh' --subject-prefix 'PATCH 6.18.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From f3629c63a4af3e491381780bc6c123cb498c4c40 Mon Sep 17 00:00:00 2001
From: Tejun Heo <[email protected]>
Date: Fri, 7 Aug 2026 11:02:17 -1000
Subject: [PATCH] sched/core: Make core-sched flips wait for in-flight
selections
Core scheduling's pick_next_task() operates on all sibling rqs under one acquisition of the shared core-wide lock. A ->pick_task() that releases the rq lock leaves every sibling __lock momentarily free, letting __sched_core_flip(false) complete mid-selection and rebind rq_lockp() under it. The selection resumes on the split locks, touching sibling state it no longer protects, and __schedule() finally releases a lock that was never taken while leaking the one that was.
Count in-flight core-wide selections in the leader's rq->core_pick_in_flight and make __sched_core_flip() wait for the count to drain. The count only changes under the shared lock, which the flip holds while sampling, so no other ordering is needed. The wait can repeat while selections overlap, but the flip backs off between samples and flips are rare cookie-lifetime events.
sched_core_cpu_deactivate() moves the count to the new leader - a stale copy left behind would bias it forever if that CPU later returns as its own leader.
kernel/sched/core.c +22 -0
@@ -443,6 +443,17 @@ static void __sched_core_flip(bool enabled)sched_core_lock (cpu, &flags); +/* +* A core-wide selection may have the shared rq lock temporarily +* released by a lock-dropping ->pick_task(). Flipping would +* rebind rq_lockp() under it. Wait it out. +*/ +while (cpu_rq (cpu)->core->core_pick_in_flight) { +sched_core_unlock (cpu, &flags); +cpu_relax (); +sched_core_lock (cpu, &flags); + } +for_each_cpu (t, smt_mask)cpu_rq (t)->core_enabled = enabled;
@@ -6236,6 +6247,8 @@ pick_next_task(struct rq *rq, struct rq_flags *rf)return __pick_next_task (rq, rf); } + rq->core->core_pick_in_flight++; +/* * If there were no {en,de}queues since we picked (IOW, the task * pointers are all still valid), and we haven't scheduled the last
@@ -6450,6 +6463,7 @@ pick_next_task(struct rq *rq, struct rq_flags *rf) } out_set_next: + rq->core->core_pick_in_flight--;put_prev_set_next_task (rq, rq->donor, next);if (rq->core->core_forceidle_count && next == rq->idle)queue_core_balance (rq);
@@ -6644,6 +6658,13 @@ static void sched_core_cpu_deactivate(unsigned int cpu) core_rq->core_forceidle_seq = rq->core_forceidle_seq; core_rq->core_forceidle_occupation = rq->core_forceidle_occupation; +/* +* A stale leftover would bias the count forever if this CPU later +* returns as its own leader. Move, don't copy. +*/ + core_rq->core_pick_in_flight = rq->core_pick_in_flight; + rq->core_pick_in_flight =0 ; +/* * Accounting edge for forced idle is handled in pick_next_task(). * Don't need another one here, since the hotplug thread shouldn't
@@ -9060,6 +9081,7 @@ void __init sched_init(void) rq->core_forceidle_count =0 ; rq->core_forceidle_occupation =0 ; rq->core_forceidle_start =0 ; + rq->core_pick_in_flight =0 ; rq->core_cookie =0UL ;#endif
kernel/sched/sched.h +1 -0
@@ -1358,6 +1358,7 @@ struct rq {unsigned int core_forceidle_seq;unsigned int core_forceidle_occupation;u64 core_forceidle_start; +unsigned int core_pick_in_flight;#endif /* CONFIG_SCHED_CORE */ /* Scratch cpumask to be temporarily used under rq_lock */
Sasha Levin
maintainerPATCH 1/3 sched: Add assert_balance_callbacks_empty helper
From: John Stultz <[email protected]>
[ Upstream commit f9530b3183358bbf945f7c20d4a6e2048061ec50 ]
With proxy-exec utilizing pick-again logic, we can end up having balance callbacks set by the preivous pick_next_task() call left on the list.
So pull the warning out into a helper function, and make sure we check it when we pick again.
Stable-dep-of: f3629c63a4af ("sched/core: Make core-sched flips wait for in-flight selections")
--- kernel/sched/core.c | 1 + kernel/sched/sched.h | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-)
kernel/sched/core.c +1 -0
@@ -6880,6 +6880,7 @@ static void __sched notrace __schedule(int sched_mode) } pick_again: +assert_balance_callbacks_empty (rq); next =pick_next_task (rq, rq->donor, &rf);rq_set_donor (rq, next);if (unlikely (task_is_blocked (next))) {
kernel/sched/sched.h +8 -1
@@ -1787,6 +1787,13 @@ static inline void scx_rq_clock_update(struct rq *rq, u64 clock) {}static inline void scx_rq_clock_invalidate (struct rq *rq) {}#endif /* !CONFIG_SCHED_CLASS_EXT */ +static inline void assert_balance_callbacks_empty (struct rq *rq) +{ +WARN_ON_ONCE (IS_ENABLED (CONFIG_PROVE_LOCKING ) && + rq->balance_callback && + rq->balance_callback != &balance_push_callback); +} +/* * Lockdep annotation that avoids accidental unlocks; it's like a * sticky/continuous lockdep_assert_held().
@@ -1803,7 +1810,7 @@ static inline void rq_pin_lock(struct rq *rq, struct rq_flags *rf) rq->clock_update_flags &= (RQCF_REQ_SKIP |RQCF_ACT_SKIP ); rf->clock_update_flags =0 ; -WARN_ON_ONCE (rq->balance_callback && rq->balance_callback != &balance_push_callback); +assert_balance_callbacks_empty (rq); }static inline void rq_unpin_lock (struct rq *rq,struct rq_flags *rf)
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/3 sched: Rework prev_balance() to avoid stale prev references
From: John Stultz <[email protected]>
[ Upstream commit 7a3a6bfbd62a2ba3e0ef1e92d6b71abb66890825 ]
Historically, the prev value from __schedule() was the rq->curr. This prev value is passed down through numerous functions, and used in the class scheduler implementations. The fact that prev was on_cpu until the end of __schedule(), meant it was stable across the rq lock drops that the class->balance() implementations often do.
However, with proxy-exec, the prev passed to functions called by __schedule() is rq->donor, which may not be the same as rq->curr and may not be on_cpu, this makes the prev value potentially unstable across rq lock drops.
A recently found issue with proxy-exec, is when we begin doing return migration from try_to_wake_up(), its possible we may be waking up the rq->donor. When we do this, we proxy_resched_idle() to put_prev_set_next() setting the rq->donor to rq->idle, allowing the rq->donor to be return migrated and allowed to run.
This however runs into trouble, as on another cpu we might be in the middle of calling __schedule(). Conceptually the rq lock is held for the majority of the time, but in calling prev_balance() its possible the class->balance() handler call may briefly drop the rq lock. This opens a window for try_to_wake_up() to wake and return migrate the rq->donor before the class logic reacquires the rq lock.
Unfortunately prev_balance() pass in a prev argument, to which we pass rq->donor. However this prev value can now become stale and incorrect across a rq lock drop.
So, to correct this, rework the prev_balance() call so that it does not take a "prev" argument.
Backport adaptation for 6.18:
Keep the existing sched_class::pick_next_task callbacks, pick_task interfaces, SCX balancing setup and proxy-execution flow. The later upstream scheduler refactors and lock annotations are not prerequisites for this dependency.
Convert all six balance callbacks, including the fair and SCX callbacks still present here, to the new signature. Read rq->donor at the existing selection and balance call sites, and refresh prev after new-idle balancing in pick_next_task_fair() because that path still drops the rq lock inside the stable pick_next_task implementation.
This preserves the existing functions and supplies the selection context needed for f3629c63a4af (sched/core: Make core-sched flips wait for in-flight selections) to apply without changes.
Stable-dep-of: f3629c63a4af ("sched/core: Make core-sched flips wait for in-flight selections")
--- kernel/sched/core.c | 37 ++++++++++++++++++------------------- kernel/sched/deadline.c | 8 +++++++- kernel/sched/ext.c | 5 ++--- kernel/sched/fair.c | 4 +++- kernel/sched/idle.c | 2 +- kernel/sched/rt.c | 8 +++++++- kernel/sched/sched.h | 2 +- kernel/sched/stop_task.c | 2 +- 8 files changed, 40 insertions(+), 28 deletions(-)
kernel/sched/core.c +18 -19
@@ -5923,10 +5923,9 @@ static inline void schedule_debug(struct task_struct *prev, bool preempt)schedstat_inc (this_rq ()->sched_count); } -static void prev_balance (struct rq *rq,struct task_struct *prev, -struct rq_flags *rf) +static void prev_balance (struct rq *rq,struct rq_flags *rf) { -const struct sched_class *start_class = prev->sched_class; +const struct sched_class *start_class = rq->donor->sched_class;const struct sched_class *class;#ifdef CONFIG_SCHED_CLASS_EXT
@@ -5951,7 +5950,7 @@ static void prev_balance(struct rq *rq, struct task_struct *prev,* a runnable task of @class priority or higher. */ for_active_class_range(class, start_class, &idle_sched_class) { -if (class->balance && class->balance (rq, prev, rf)) +if (class->balance && class->balance (rq, rf))break ; } }
@@ -5960,7 +5959,7 @@ static void prev_balance(struct rq *rq, struct task_struct *prev,* Pick up the highest-prio task: */ static inline struct task_struct * -__pick_next_task (struct rq *rq,struct task_struct *prev,struct rq_flags *rf) +__pick_next_task (struct rq *rq,struct rq_flags *rf) {const struct sched_class *class;struct task_struct *p;
@@ -5976,34 +5975,34 @@ __pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)* higher scheduling class, because otherwise those lose the * opportunity to pull in more work from other CPUs. */ -if (likely (!sched_class_above (prev->sched_class, &fair_sched_class) && +if (likely (!sched_class_above (rq->donor->sched_class, &fair_sched_class) && rq->nr_running == rq->cfs.h_nr_queued)) { - p =pick_next_task_fair (rq, prev, rf); + p =pick_next_task_fair (rq, rq->donor, rf);if (unlikely (p ==RETRY_TASK )) goto restart;/* Assume the next prioritized class is idle_sched_class */ if (!p) { p =pick_task_idle (rq); -put_prev_set_next_task (rq, prev, p); +put_prev_set_next_task (rq, rq->donor, p); }return p; } restart: -prev_balance (rq, prev, rf); +prev_balance (rq, rf);for_each_active_class (class) {if (class->pick_next_task) { - p = class->pick_next_task (rq, prev); + p = class->pick_next_task (rq, rq->donor);if (p)return p; }else { p = class->pick_task (rq);if (p) { -put_prev_set_next_task (rq, prev, p); +put_prev_set_next_task (rq, rq->donor, p);return p; } }
@@ -6052,7 +6051,7 @@ extern void task_vruntime_update(struct rq *rq, struct task_struct *p, bool in_fstatic void queue_core_balance (struct rq *rq);static struct task_struct * -pick_next_task (struct rq *rq,struct task_struct *prev,struct rq_flags *rf) +pick_next_task (struct rq *rq,struct rq_flags *rf) {struct task_struct *next, *p, *max =NULL ;const struct cpumask *smt_mask;
@@ -6064,7 +6063,7 @@ pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)bool need_sync;if (!sched_core_enabled (rq)) -return __pick_next_task (rq, prev, rf); +return __pick_next_task (rq, rf); cpu =cpu_of (rq);
@@ -6077,7 +6076,7 @@ pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)*/ rq->core_pick =NULL ; rq->core_dl_server =NULL ; -return __pick_next_task (rq, prev, rf); +return __pick_next_task (rq, rf); }/*
@@ -6101,7 +6100,7 @@ pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf) goto out_set_next; } -prev_balance (rq, prev, rf); +prev_balance (rq, rf); smt_mask =cpu_smt_mask (cpu); need_sync = !!rq->core->core_cookie;
@@ -6274,7 +6273,7 @@ pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf) } out_set_next: -put_prev_set_next_task (rq, prev, next); +put_prev_set_next_task (rq, rq->donor, next);if (rq->core->core_forceidle_count && next == rq->idle)queue_core_balance (rq);
@@ -6496,9 +6495,9 @@ static inline void sched_core_cpu_deactivate(unsigned int cpu) {}static inline void sched_core_cpu_dying (unsigned int cpu) {}static struct task_struct * -pick_next_task (struct rq *rq,struct task_struct *prev,struct rq_flags *rf) +pick_next_task (struct rq *rq,struct rq_flags *rf) { -return __pick_next_task (rq, prev, rf); +return __pick_next_task (rq, rf); }#endif /* !CONFIG_SCHED_CORE */
@@ -6881,7 +6880,7 @@ static void __sched notrace __schedule(int sched_mode) pick_again:assert_balance_callbacks_empty (rq); - next =pick_next_task (rq, rq->donor, &rf); + next =pick_next_task (rq, &rf);rq_set_donor (rq, next);if (unlikely (task_is_blocked (next))) { next =find_proxy_task (rq, next, &rf);
kernel/sched/deadline.c +7 -1
@@ -2474,8 +2474,14 @@ static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p)resched_curr (rq); } -static int balance_dl (struct rq *rq,struct task_struct *p,struct rq_flags *rf) +static int balance_dl (struct rq *rq,struct rq_flags *rf) { +/* +* Note, rq->donor may change during rq lock drops, +* so don't re-use p across lock drops +*/ +struct task_struct *p = rq->donor; +if (!on_dl_rq (&p->dl) &&need_pull_dl_task (rq, p)) {/* * This is OK, because current is on_cpu, which avoids it being
kernel/sched/ext.c +2 -3
@@ -2200,14 +2200,13 @@ static int balance_one(struct rq *rq, struct task_struct *prev)return true; } -static int balance_scx (struct rq *rq,struct task_struct *prev, -struct rq_flags *rf) +static int balance_scx (struct rq *rq,struct rq_flags *rf) {int ret;rq_unpin_lock (rq, rf); - ret =balance_one (rq, prev); + ret =balance_one (rq, rq->donor);#ifdef CONFIG_SCHED_SMT /*
kernel/sched/fair.c +3 -1
@@ -8879,7 +8879,7 @@ static void set_cpus_allowed_fair(struct task_struct *p, struct affinity_context }static int -balance_fair (struct rq *rq,struct task_struct *prev,struct rq_flags *rf) +balance_fair (struct rq *rq,struct rq_flags *rf) {if (sched_fair_runnable (rq))return 1 ;
@@ -9200,6 +9200,8 @@ pick_next_task_fair(struct rq *rq, struct task_struct *prev, struct rq_flags *rf idle:if (rf) { new_tasks =sched_balance_newidle (rq, rf); +/* The donor may have changed while the rq lock was dropped. */ + prev = rq->donor;/* * Because sched_balance_newidle() releases (and re-acquires)
kernel/sched/idle.c +1 -1
@@ -460,7 +460,7 @@ select_task_rq_idle(struct task_struct *p, int cpu, int flags) }static int -balance_idle (struct rq *rq,struct task_struct *prev,struct rq_flags *rf) +balance_idle (struct rq *rq,struct rq_flags *rf) {return WARN_ON_ONCE (1 ); }
kernel/sched/rt.c +7 -1
@@ -1591,8 +1591,14 @@ static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p)resched_curr (rq); } -static int balance_rt (struct rq *rq,struct task_struct *p,struct rq_flags *rf) +static int balance_rt (struct rq *rq,struct rq_flags *rf) { +/* +* Note, rq->donor may change during rq lock drops, +* so don't re-use p across lock drops +*/ +struct task_struct *p = rq->donor; +if (!on_rt_rq (&p->rt) &&need_pull_rt_task (rq, p)) {/* * This is OK, because current is on_cpu, which avoids it being
kernel/sched/sched.h +1 -1
@@ -2422,7 +2422,7 @@ struct sched_class {void (*wakeup_preempt)(struct rq *rq,struct task_struct *p,int flags); -int (*balance)(struct rq *rq,struct task_struct *prev,struct rq_flags *rf); +int (*balance)(struct rq *rq,struct rq_flags *rf);struct task_struct *(*pick_task)(struct rq *rq);/* * Optional! When implemented pick_next_task() should be equivalent to:
kernel/sched/stop_task.c +1 -1
@@ -16,7 +16,7 @@ select_task_rq_stop(struct task_struct *p, int cpu, int flags) }static int -balance_stop (struct rq *rq,struct task_struct *prev,struct rq_flags *rf) +balance_stop (struct rq *rq,struct rq_flags *rf) {return sched_stop_runnable (rq); }
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 3/3 sched/core: Make core-sched flips wait for in-flight selections
In reply to Sasha LevinFrom: Tejun Heo <[email protected]>
[ Upstream commit f3629c63a4af3e491381780bc6c123cb498c4c40 ]
Core scheduling's pick_next_task() operates on all sibling rqs under one acquisition of the shared core-wide lock. A ->pick_task() that releases the rq lock leaves every sibling __lock momentarily free, letting __sched_core_flip(false) complete mid-selection and rebind rq_lockp() under it. The selection resumes on the split locks, touching sibling state it no longer protects, and __schedule() finally releases a lock that was never taken while leaking the one that was.
Count in-flight core-wide selections in the leader's rq->core_pick_in_flight and make __sched_core_flip() wait for the count to drain. The count only changes under the shared lock, which the flip holds while sampling, so no other ordering is needed. The wait can repeat while selections overlap, but the flip backs off between samples and flips are rare cookie-lifetime events.
sched_core_cpu_deactivate() moves the count to the new leader - a stale copy left behind would bias it forever if that CPU later returns as its own leader.
--- kernel/sched/core.c | 22 ++++++++++++++++++++++ kernel/sched/sched.h | 1 + 2 files changed, 23 insertions(+)
kernel/sched/core.c +22 -0
@@ -434,6 +434,17 @@ static void __sched_core_flip(bool enabled)sched_core_lock (cpu, &flags); +/* +* A core-wide selection may have the shared rq lock temporarily +* released by a lock-dropping ->pick_task(). Flipping would +* rebind rq_lockp() under it. Wait it out. +*/ +while (cpu_rq (cpu)->core->core_pick_in_flight) { +sched_core_unlock (cpu, &flags); +cpu_relax (); +sched_core_lock (cpu, &flags); + } +for_each_cpu (t, smt_mask)cpu_rq (t)->core_enabled = enabled;
@@ -6079,6 +6090,8 @@ pick_next_task(struct rq *rq, struct rq_flags *rf)return __pick_next_task (rq, rf); } + rq->core->core_pick_in_flight++; +/* * If there were no {en,de}queues since we picked (IOW, the task * pointers are all still valid), and we haven't scheduled the last
@@ -6273,6 +6286,7 @@ pick_next_task(struct rq *rq, struct rq_flags *rf) } out_set_next: + rq->core->core_pick_in_flight--;put_prev_set_next_task (rq, rq->donor, next);if (rq->core->core_forceidle_count && next == rq->idle)queue_core_balance (rq);
@@ -6466,6 +6480,13 @@ static void sched_core_cpu_deactivate(unsigned int cpu) core_rq->core_forceidle_seq = rq->core_forceidle_seq; core_rq->core_forceidle_occupation = rq->core_forceidle_occupation; +/* +* A stale leftover would bias the count forever if this CPU later +* returns as its own leader. Move, don't copy. +*/ + core_rq->core_pick_in_flight = rq->core_pick_in_flight; + rq->core_pick_in_flight =0 ; +/* * Accounting edge for forced idle is handled in pick_next_task(). * Don't need another one here, since the hotplug thread shouldn't
@@ -8774,6 +8795,7 @@ void __init sched_init(void) rq->core_forceidle_count =0 ; rq->core_forceidle_occupation =0 ; rq->core_forceidle_start =0 ; + rq->core_pick_in_flight =0 ; rq->core_cookie =0UL ;#endif
kernel/sched/sched.h +1 -0
@@ -1310,6 +1310,7 @@ struct rq {unsigned int core_forceidle_seq;unsigned int core_forceidle_occupation;u64 core_forceidle_start; +unsigned int core_pick_in_flight;#endif /* CONFIG_SCHED_CORE */ /* Scratch cpumask to be temporarily used under rq_lock */
Signature · 1 line
2.53.0