From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4265D56C658; Wed, 9 Sep 2026 14:37:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788964673; cv=none; b=SeMtRs+QFdW7VD6C+KSWL6GjGQRGkTqPlA1zkAlUEsujbcOM/bzRVPAQU3X4YAslTrW38lE8rFaYV8Zq1pj+XSbZnDwLC28MQSpexq9HFZAhVzNT27/eA6DrO3fcFFVw9P5waWP8BUTlNHQGayQfciTr0wJeJL+5fPJx3YLFMf4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788964673; c=relaxed/simple; bh=thSVJfH4+qTIaW9v/dWySus0+yYeJsvyCS4ukDcMkOU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BfWGxzG1GlyAKQOrE5vGdnKiGci6u8PTftbMwM0cnVDxhywrwYq2fiYpY0JZ2qOVkO1tRdhrErAp18F4GKetY0/eXtV9mfrWTcgijZMLI7YG3+lmmsmF8g16mPIFrqHy14O4j6is0ES4lyyTrBLPxim4O7tpDdrJHDZi7PeyeL0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=xp/be74n; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="xp/be74n" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8EDEF1F00A3A; Wed, 9 Sep 2026 14:37:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1788964672; bh=4YM8MobuiWy9oxraNceHinpTxSePbO/7smPedhPzB08=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=xp/be74nDHGbXqmORDW/zHVOOhc73Ih23OOmvEnhBUJMc95Fz7ibRsfMEI7xarj3k DDq34g3sq+GVvO+44YLwf+HZSFim4TjZ4gEPZsfdcnPKtcZQK7xKkbvn8mk7nsLxTO ZwDs2gci70tyJLoaiSh9LB3lBcZ4HoY5H+4SyL14= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Tejun Heo , "Peter Zijlstra (Intel)" , Sasha Levin Subject: [PATCH 6.18 503/583] sched/core: Make core-sched flips wait for in-flight selections Date: Wed, 9 Sep 2026 15:43:08 +0200 Message-ID: <20260909134255.190285986@linuxfoundation.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260909134237.773280130@linuxfoundation.org> References: <20260909134237.773280130@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Tejun Heo [ 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. Fixes: 539f65125d20 ("sched: Add core wide task selection and scheduling") Cc: stable@vger.kernel.org # v5.14+ Signed-off-by: Tejun Heo Acked-by: Peter Zijlstra (Intel) Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- kernel/sched/core.c | 22 ++++++++++++++++++++++ kernel/sched/sched.h | 1 + 2 files changed, 23 insertions(+) --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -434,6 +434,17 @@ static void __sched_core_flip(bool enabl 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_ 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_ } 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); @@ -6467,6 +6481,13 @@ static void sched_core_cpu_deactivate(un 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 * have a cookie. @@ -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 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -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 */