Re: [PATCH] sched/core: Skip empty source rqs in try_steal_cookie()
From: Peter Zijlstra
Date: Fri Nov 27 2026 - 04:49:10 EST
On Thu, Nov 26, 2026 at 06:24:26PM +0000, Kuba Piecuch wrote:
> sched_core_balance() walks each CPU of each sched domain of a forced-idle
> CPU via steal_cookie_task() -> try_steal_cookie() until a matching-cookie
> task is stolen or need_resched() is set.
>
> try_steal_cookie() disables IRQs and acquires double_rq_lock(dst, src)
> before inspecting @src, even when src->core_tree is empty and no task can
> be stolen from @src. On large machines, a forced-idle balance pass incurs
> many IRQ-off double_rq_lock() rounds and remote rq lock contention for
> nothing.
>
> Return early if src->core_tree is empty via a lockless data_race() peek
> before disabling IRQs and locking both runqueues. Missing a task enqueued
> just after the peek is equivalent to missing one enqueued right after
> releasing the locks; all remaining checks stay under double_rq_lock().
>
> Signed-off-by: Kuba Piecuch <jpiecuch@xxxxxxxxxx>
> ---
> This patch was written with the help of an AI coding assistant. I have
> reviewed the patch and verified the tests run by the tool.
*sigh*, so we need AI to write effectively single line patches these
days :-(
Anyway, the whole thing seems sane enough. Thanks!
>
> kernel/sched/core.c | 7 +++++++
> 1 file changed, 7 insertions(+)
>
> diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> index ea89471d4743..dfff7756465f 100644
> --- a/kernel/sched/core.c
> +++ b/kernel/sched/core.c
> @@ -6591,6 +6591,13 @@ static bool try_steal_cookie(int this, int that)
> unsigned long cookie;
> bool success = false;
>
> + /*
> + * Lockless hint: a missed concurrent enqueue is like arriving after
> + * unlock; a concurrent dequeue is caught under double_rq_lock().
> + */
> + if (data_race(RB_EMPTY_ROOT(&src->core_tree)))
> + return false;
> +
> guard(irq)();
> guard(double_rq_lock)(dst, src);
>
>
> base-commit: c47af85667065c6dd5b79564203c2d29948ef60e
> --
> 2.56.1.839.g22a8290db9-goog
>