From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796379883; b=qyOCvsGzLm3iQ19ZgWdqAP2SLB+sRxMsTlIxIrQARJILmQJd03eYLtvb+wjP1eX9rxfp 0M6p8kiKd6aQFBZzf2VehVGAQ3Vquv1v2/LeTT2DgF4yJWS6K50z0F1s0esvHxyNEjAMQ CzS7DfAkRgLY4AGRUpKVDiX5AJOAtsEojYQKOwBmxCM6xmi9bDDiusZd+oMAkBSZEjCCw HyHJEcqaytacH/0aHkS46KJPnAAI/klDK+57FXgEloukWOvoy905gSB5bylbKL8Ouigtm M2a0Y/NWjfgJu3KRtv10U/fSFqXaRpMM6EwcDWVPI6IJCt7dpk1g7qtfSh/zncckoNQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796379883; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=aDiH9VsJHNf5ECwipDBW++pcevmus20A8tHAFej0H/Y=; b=aIwwUfpEMEwUJ6Xq6a7/WBUAYL1flJYIJuo46pYkDVN+gjtY/d6/KlZeIoYHi29cAgqm rfwh7nCZ1PLNhhovajK7DyYeSIlZ5NHwEsPEOkZGu8tvFnCHkebZrXnpDZ+QM7zFWUuTA xPwbB+2eKUlZTRjSK8ohxzccUhg3OD4gaDLLLpePmalOpbfHG11xBrOWXd11RbUy/WJkg JDKjw0TNfIFdfL6yR8oQAdjK+PbmCzEsdZcoGzpAawl4XN/c+pMVTcGU6sk5MlI01JKKd iC2wYdy0X7/R2ssslaRgAEnEpKv2EkG8WgX7+bX5FwI1x9tE2JBHG+QTkjlSv/lUJiA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=i9revckZ; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1418B606A322; Fri, 4 Dec 2026 10:24:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=i9revckZ Received: by smtp.kernel.org (Postfix) with ESMTPSA id 82E431F000C2; Fri, 4 Dec 2026 10:24:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796379882; bh=aDiH9VsJHNf5ECwipDBW++pcevmus20A8tHAFej0H/Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=i9revckZA9PNhug6pD0bL37LkiYEhB09J9zwdqEVwSSd6WZ8R8xAMoxB6mR7dJPHd 8mLfAye3AOgERnZNLxEdJ/F3vU/6eIHwO8sQThpG0ONUnZaQCkh5MEJI0CTGgmWW7r VFa+WXCNeP75M/h1zJqa1gcpoV0TvxK93Ah8D06Y= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Tejun Heo , Andrea Righi , Sasha Levin Subject: [PATCH 7.3 026/742] sched_ext: Fix ops.enqueue/dispatch() calls before ops.cpu_online() Date: Fri, 4 Dec 2026 18:58:10 +0900 Message-ID: <20261204100635.750026140@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100635.034361255@linuxfoundation.org> References: <20261204100635.034361255@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 7.3-stable review patch. If anyone has any objections, please let me know. ------------------ From: Tejun Heo [ Upstream commit 2227a34ff7bfcf6112931039e3554f4b70b65ab9 ] ops.cpu_online() documents that the cpu doesn't call ops.enqueue() or ops.dispatch() before that op. Both are gated on SCX_RQ_ONLINE, which follows the core's rq online state through the ext class's rq_online and rq_offline hooks. On a runtime cpu up, sched_cpu_activate() runs: 1. set_cpu_active() marks the cpu active. 2. cpuset_cpu_active() rebuilds the sched domains synchronously, and rq_attach_root() puts the rq online as the cpu is active now. The rq_online hook sets SCX_RQ_ONLINE. 3. scx_rq_activate() calls the root's ops.cpu_online(). Between 2 and 3, ops.enqueue() can be called for tasks woken or migrated onto the cpu, and ops.dispatch() can run on it. The hooks can't call the ops. They run under the rq lock with IRQs off while the ops can be sleepable BPF programs, and handle_hotplug() needs the rebuilt sched domains for its idle topology update. Track SCX_RQ_ONLINE in handle_hotplug() alone. Set it right after the root's ops.cpu_online(). Clear it and move the cpu's tasks to its local DSQ when the cpu goes down. Sched domain rebuilds, which take a live cpu's rq offline and online again, no longer touch the flag, so a remote dispatch can't see it cleared by a rebuild and bounce to the global DSQ. Between 2 and 3, tasks that land on the cpu now go to its local DSQ without ops.enqueue(), and with sub-schedulers a task whose scheduler lacks caps on the cpu is admitted anyway and counted in SCX_EV_SUB_FORCED_ADMIT. Both already happen between 1 and 2. Fixes: 60c27fb59f6c ("sched_ext: Implement sched_ext_ops.cpu_online/offline()") Cc: stable@vger.kernel.org # v6.12+ Signed-off-by: Tejun Heo Reviewed-by: Andrea Righi [ sashal: Moved the SCX_RQ_ONLINE set/clear into handle_hotplug() and placed the ecaps reseed after it, dropping context lines for helpers and a flag this tree lacks and using the older comment wording. ] Signed-off-by: Sasha Levin --- kernel/sched/ext/ext.c | 65 ++++++++++++++++++++++---------------- kernel/sched/ext/inlines.h | 2 +- kernel/sched/ext/sub.c | 9 +++--- kernel/sched/sched.h | 8 +++-- 4 files changed, 48 insertions(+), 36 deletions(-) diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c index 6db6ca912bb37..fbdf03214564d 100644 --- a/kernel/sched/ext/ext.c +++ b/kernel/sched/ext/ext.c @@ -1997,11 +1997,11 @@ static void direct_dispatch(struct scx_sched *sch, struct task_struct *p, bool scx_rq_online(struct rq *rq) { /* - * Test both cpu_active() and %SCX_RQ_ONLINE. %SCX_RQ_ONLINE indicates - * the online state as seen from the BPF scheduler. cpu_active() test - * guarantees that, if this function returns %true, %SCX_RQ_ONLINE will - * stay set until the current scheduling operation is complete even if - * we aren't locking @rq. + * Test both cpu_active() and %SCX_RQ_ONLINE. %SCX_RQ_ONLINE is set + * after the root's ops.cpu_online() and cleared when the cpu goes down. + * cpu_active() test guarantees that, if this function returns %true, + * %SCX_RQ_ONLINE will stay set until the current scheduling operation + * is complete even if we aren't locking @rq. */ return likely((rq->scx.flags & SCX_RQ_ONLINE) && cpu_active(cpu_of(rq))); } @@ -3825,6 +3825,13 @@ static void handle_hotplug(struct rq *rq, bool online) s32 cpu = cpu_of(rq); s32 cpu_or_cid = cpu; + if (!online) { + guard(rq_lock_irqsave)(rq); + update_rq_clock(rq); + rq->scx.flags &= ~SCX_RQ_ONLINE; + drain_offline_rq(rq); + } + atomic_long_inc(&scx_hotplug_seq); /* @@ -3832,15 +3839,19 @@ static void handle_hotplug(struct rq *rq, bool online) * stable here. Note that we can't depend on scx_enabled() test as the * hotplug ops need to be enabled before __scx_enabled is set. */ - if (unlikely(!sch)) + if (unlikely(!sch)) { + /* no root to notify, set %SCX_RQ_ONLINE right away */ + if (online) { + guard(rq_lock_irqsave)(rq); + rq->scx.flags |= SCX_RQ_ONLINE; + } return; + } if (scx_enabled()) scx_idle_update_selcpu_topology(&sch->ops); - if (online) - scx_online_ecaps(rq); - else + if (!online) scx_offline_ecaps(rq); /* @@ -3880,6 +3891,23 @@ static void handle_hotplug(struct rq *rq, bool online) SCX_ECODE_ACT_RESTART | SCX_ECODE_RSN_HOTPLUG, "cpu %d going %s, exiting scheduler", cpu, online ? "online" : "offline"); + + if (online) { + /* + * The cpu must not call ops.enqueue() or ops.dispatch() before + * the root's ops.cpu_online(). The core's rq online state can't + * gate them, as the sched domain rebuild in + * sched_cpu_activate() can put @rq online before this function + * runs. Set %SCX_RQ_ONLINE here. + * + * scx_online_ecaps() queues a reseed whose ecaps ops run in + * dispatch context on the cpu and can insert into its local + * DSQ. Set the flag first. + */ + scoped_guard (rq_lock_irqsave, rq) + rq->scx.flags |= SCX_RQ_ONLINE; + scx_online_ecaps(rq); + } } void scx_rq_activate(struct rq *rq) @@ -3892,22 +3920,6 @@ void scx_rq_deactivate(struct rq *rq) handle_hotplug(rq, false); } -static void rq_online_scx(struct rq *rq) -{ - rq->scx.flags |= SCX_RQ_ONLINE; -} - -static void rq_offline_scx(struct rq *rq) -{ - rq->scx.flags &= ~SCX_RQ_ONLINE; - - /* sched domain rebuilds call rq_offline with the CPU staying alive */ - if (cpu_active(cpu_of(rq))) - return; - - drain_offline_rq(rq); -} - static bool check_rq_for_timeouts(struct rq *rq) { struct scx_sched *sch; @@ -5287,9 +5299,6 @@ DEFINE_SCHED_CLASS(ext) = { .task_woken = task_woken_scx, .set_cpus_allowed = set_cpus_allowed_scx, - .rq_online = rq_online_scx, - .rq_offline = rq_offline_scx, - .task_tick = task_tick_scx, .switching_to = switching_to_scx, diff --git a/kernel/sched/ext/inlines.h b/kernel/sched/ext/inlines.h index 5dce394e08134..8de281a5c9c6e 100644 --- a/kernel/sched/ext/inlines.h +++ b/kernel/sched/ext/inlines.h @@ -104,7 +104,7 @@ scx_dispatch_sched(struct scx_sched *sch, struct rq *rq, /* * scx_rq_online() can't be used. Its cpu_active() test goes false * before CPU hotplug waits for an RCU grace period, and - * rq_offline_scx() moves this CPU's tasks to the local DSQ only after + * handle_hotplug() moves this CPU's tasks to the local DSQ only after * the wait. The grace period can depend on those tasks running. */ if (unlikely(!SCX_HAS_OP(sch, dispatch)) || !(rq->scx.flags & SCX_RQ_ONLINE)) diff --git a/kernel/sched/ext/sub.c b/kernel/sched/ext/sub.c index 4dc3bb12aca3a..c06017429890c 100644 --- a/kernel/sched/ext/sub.c +++ b/kernel/sched/ext/sub.c @@ -883,10 +883,11 @@ struct scx_dispatch_q *scx_resolve_local_dsq(struct scx_sched *sch, struct rq *r return &rq->scx.local_dsq; /* - * The task must run on this CPU regardless of caps: the rq is draining - * offline (BPF scheduler bypassed), the task is migration-disabled, or a - * migration is pending. Admit despite the missing caps and count it. - * Refuse preemptions. + * The task must run on this CPU regardless of caps: ops.enqueue() is + * skipped while the CPU is going down or before the root's + * ops.cpu_online(), the task is migration-disabled, or a migration is + * pending. Admit despite the missing caps and count it. Refuse + * preemptions. */ if (unlikely(!scx_rq_online(rq) || is_migration_disabled(p) || p->migration_pending)) { diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index 61b913032c640..80e72b01b718c 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -781,9 +781,11 @@ struct cfs_rq { /* scx_rq->flags, protected by the rq lock */ enum scx_rq_flags { /* - * A hotplugged CPU starts scheduling before rq_online_scx(). Track - * ops.cpu_on/offline() state so that ops.enqueue/dispatch() are called - * only while the BPF scheduler considers the CPU to be online. + * Set right after the root's ops.cpu_online() when a cpu comes up and + * cleared when the cpu goes down, see handle_hotplug(). A hotplugged + * cpu starts scheduling before %SCX_RQ_ONLINE is set. ops.enqueue() and + * ops.dispatch() are called for the cpu only while the flag is set, see + * scx_rq_online() and scx_dispatch_sched(). */ SCX_RQ_ONLINE = 1 << 0, SCX_RQ_CAN_STOP_TICK = 1 << 1, -- 2.53.0