From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.48 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796439882; b=05TkPwY9YAslrY02bRhv2QA2I47b9W6L/FSzCQieGULEP+bcx6AkiN/ABaxm9Zwk4WIA U4YMiUU9/yH7Q+Es9cdWYuVM9REu3V0v7TEoZrt0ndQSaey8DjAh2WVA9084k+93Y8OSg zWDLhlD3kpd11TIrLbMDnXO1hYrbHwzSgaIBzxWrfSwKWB8HndQv2Paepjx1SlOvuo7Av 8JpKx9LS3arHQhREJzijmJGxLHcsI+Et80dnVXJjBL3RpeEpzZMwRhsTHjwru1Y+UuUjm dcujfy5qUMpLZq16URmaKTKXR0+O66Niiga1h4qRyud+q32D3/vZI+afO2irOm/S85w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796439882; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=oLBJrdNg5EcjCLQMtu/6QeutXQwapkg1rPQfDdLbqh4=; b=cfFWWrplj9vC32Jnu5qKaVTQGprpRFcMeCqqsDa9Z20NhbhqBerPAoK6na6QsdrHoVuN HRoh165WNo9qPJliHAjn1938lDWGty8hP9KRyKkB5H5ZWTXdFZr7fPX9HeFpLEjGFA+ge wqd3/4AjrX6m4VH7ep6kdYE6ZezKu/FdGXKEPF1hpLu/BWQ7DUyDnnZsZjhJKZsY6Lq0Y qKbsXboOlzHVZqUufyJGV/c4j5HJMgUxrzgALytlW+7noD+Ow9+qfWAyNw5m8537Qsae/ tHX6pGqmyUFQrt9fIfex4iQehCmCr3Y5/6Ead8sBDFLLQ07TIxiIRlmO3lb4Np+kKMg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=VYATLsMs; arc=none smtp.remote-ip=74.125.82.48 Received: from mail-dl1-f48.google.com (mail-dl1-f48.google.com [74.125.82.48]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 33133606A32F for ; Sat, 5 Dec 2026 03:04:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 33133606A32F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=VYATLsMs Received: by mail-dl1-f48.google.com with SMTP id a92af1059eb24-1b5d5f3d5b8so1690895c88.0 for ; Fri, 04 Dec 2026 19:04:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796439881; x=1797044681; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=oLBJrdNg5EcjCLQMtu/6QeutXQwapkg1rPQfDdLbqh4=; b=VYATLsMsTZBG6zggbCycyIN7c7poKpKeSZVueB/OmxyTIaNfCuqpvPtuz0bEPUkBGt G2fWUJl0rJE61JZuLbrvbiAH1I1OFJlRdSXwVhSeJ6BmdVSRcEIWrPCnxHZF6clPkydu SS8lmTdFdaOWVcKlLi1HyLkZaBpm5R4r9yDS4sjWGw7c6coaS/Tez010TzKAS3RF1ZA4 HLKo4BVQ/VxUecVBwoLKnnCCt3cQVvlRLcG2gx156ue+unxD3KH5Xg9k6cO4o4+gh2y6 xsOaI3QQAD+E1gTJ+Tj9tQ0/gwxq5dfr4TNJPdVixwPl4ZKGKEh5ty3s0tElbirgTY6Z ymFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796439881; x=1797044681; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=oLBJrdNg5EcjCLQMtu/6QeutXQwapkg1rPQfDdLbqh4=; b=VddvqoD5ZxijOmExD5+ut6SMkbAXM1Q6PvRkruoyD0wZafclVasWvR4F3i7ojE9uyg aDXYnR8TUcmPOx1n3IxW7/S5eGskdJ16MY6W6r8M2e1tVBnEEMz5XICU8GpfemrayHVU Hg3s9TJ9GUyELNhm9MwxQAT3GeBPY9505pdSspZjorszR1UV6N2Dq7aB0tfcwf6H98VL zoXooOEiVTPgzQjKuexoAi06UiGRKMFwIcEQMZhtywBJe6NPfOlEIcTZn4NGWBOTJAeW 5dpVPxgKEDbmcmU87bx1ZRosIA8i8ntdtsC8+4/nzJ/NuUguWd9JmqHkoBDPAJNqwg0v og3g== X-Forwarded-Encrypted: i=1; AL+hErHb8mSMs93YGavKB6HaJFMnXeBi3oHwF2fI1eWmwfT+RkwHFv0MtyHoPcgMSWPaVk8zYfnPUc1U71AAeS8=@vger.kernel.org X-Gm-Message-State: APuU4Z9rFbECa9hc2JiTQFF7oKwQAXfQwIhDZG/MSYE1kD91pb+v7VEz GtwROOBUjIuBt3rP6t87lERcjRSRXU6/rinK9zmX/Mt3jOmTmN9oTS2F X-Gm-Gg: AZFdTZe7kBN+l91gZbCWjcXR5fcxdGms5MxZh2eTfHwOo1qOotYLaEIe+LZX7dQ4ARe V4Q23WQYmggarSFhKx/5i1XFR7LsR6BctBe62LbpWkHUgaxNDvPGQkwNMCPJ0Qwvfc0NMtFbAGn Qc24HhPDppWRpNQ25NU8yZb6ZuNACaDgfTbck8IJiLFCe1n12QM0KCdwMvR7jxU0Xi6kOvwXOHI K03d99wJaOzzrv+SasQF68TfM1ZrFb75vAvZRffn1vqcv7LkWW2wTyRotxXcOm//9uC02w4G1Gi 9gPZZ+1mCFYFxZk7Qb46/+3LYDR4asVok3ekDc/hLIbq4CQc9Z7miMzQ+oAIAYa6t4O+/fSdLBo ohI/A1Die7M18KAFZnOglY7GtA9Gvnd3+68KtbYRMUCsV/yqM46Ub5TdXJOhNnuIoHP+EkuXefj iNypUP2Zi/JC9AjOfd4clRQKQOOQmFTFlBuKl6qL0VB56hBhAH0zoyoT8HbMbqJptoCu5R5wf3J Zo4N1GG+uXKyP2A0eUU2lsVQ1p2zUO4XRAgkEN95SPYysj9cAo/bT58jcJuCVVRsot1BFGtITvI bK5PbpoxmRRYh9bDJ/iJY6BxoN14LztIgR/9Layn68tHJEtsq/SipOQ7L7XduqbpPw== X-Received: by 2002:a05:701b:418a:10b0:1b2:4da6:bccd with SMTP id a92af1059eb24-1b5d7269733mr3646233c88.34.1796439881135; Fri, 04 Dec 2026 19:04:41 -0800 (PST) Received: from 6610b1a2ef46 ([203.116.140.24]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-1b5d644171bsm11495765c88.7.2026.12.04.19.04.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 19:04:40 -0800 (PST) From: Hang Zhengyang To: Tejun Heo , David Vernet , Andrea Righi , Changwoo Min , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Valentin Schneider , K Prateek Nayak Cc: Cheng-Yang Chou , Barret Rhoden , Josh Don , Hao Luo , sched-ext@lists.linux.dev, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: [PATCH sched_ext/for-7.4-fixes v2 1/2] sched_ext: Don't enable or disable a task being forked on class changes Date: Sat, 5 Dec 2026 03:04:26 +0000 Message-ID: <20261205030427.933980-2-hang.zhengyang1010@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261205030427.933980-1-hang.zhengyang1010@gmail.com> References: <20261205030427.933980-1-hang.zhengyang1010@gmail.com> 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-Transfer-Encoding: 8bit A new task is visible by PID before scx_post_fork() runs and stays in SCX_TASK_INIT until then. If another task moves it to or off the ext class with sched_setscheduler() in that window, switching_to_scx() enables it or switched_from_scx() disables it, and scx_post_fork() then makes its own transition. ops.enable() runs twice, or ops.disable() runs for a task that was never enabled, and the state check warns: [ 8.241338] sched_ext: Invalid task state transition 0x4000000 -> 0x8000000 for scxfk[266] Leave such a task to scx_post_fork() in both callbacks, and make scx_post_fork() set READY and check the class under the rq lock that the callbacks hold. Fixes: f0e1a0643a59 ("sched_ext: Implement BPF extensible scheduler class") Cc: stable@vger.kernel.org # v6.12+ Signed-off-by: Hang Zhengyang --- kernel/sched/ext/ext.c | 48 +++++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c index 3bfeb35b002b..4750b73c2e59 100644 --- a/kernel/sched/ext/ext.c +++ b/kernel/sched/ext/ext.c @@ -4963,21 +4963,23 @@ int scx_fork(struct task_struct *p, struct kernel_clone_args *kargs) void scx_post_fork(struct task_struct *p) { if (scx_init_task_enabled) { - scx_set_task_state(p, SCX_TASK_READY); + struct rq_flags rf; + struct rq *rq; /* - * Enable the task immediately if it's running on sched_ext. - * Otherwise, it'll be enabled in switching_to_scx() if and - * when it's ever configured to run with a SCHED_EXT policy. + * Enable @p now if it is on the ext class. Otherwise, + * switching_to_scx() enables it if it ever switches over. + * + * A class switch before this point leaves enabling @p to us. + * Set %READY and test the class under the rq lock it holds, so + * that it can't run between the two and enable @p twice or not + * at all. */ - if (p->sched_class == &ext_sched_class) { - struct rq_flags rf; - struct rq *rq; - - rq = task_rq_lock(p, &rf); + rq = task_rq_lock(p, &rf); + scx_set_task_state(p, SCX_TASK_READY); + if (p->sched_class == &ext_sched_class) scx_enable_task(scx_task_sched(p), p); - task_rq_unlock(rq, p, &rf); - } + task_rq_unlock(rq, p, &rf); } scoped_guard(raw_spinlock_irq, &scx_tasks_lock) { @@ -5114,7 +5116,15 @@ static void switching_to_scx(struct rq *rq, struct task_struct *p) if (task_dead_and_done(p)) return; - scx_enable_task(sch, p); + /* + * A task being forked is %INIT until scx_post_fork(), which enables it + * if it is on the ext class by then. Enabling it here as well would + * call ops.enable() twice. Root enable and re-homing take other tasks + * through %INIT without dropping the rq lock, so no other task can be + * %INIT here. + */ + if (scx_get_task_state(p) != SCX_TASK_INIT) + scx_enable_task(sch, p); /* * set_cpus_allowed_scx() is not called while @p is associated with a @@ -5127,6 +5137,8 @@ static void switching_to_scx(struct rq *rq, struct task_struct *p) static void switched_from_scx(struct rq *rq, struct task_struct *p) { + u32 state; + /* * A class change of the running task: sched_change_begin() put @p with * no successor, which leaves rq->scx.sched set. @@ -5144,10 +5156,16 @@ static void switched_from_scx(struct rq *rq, struct task_struct *p) * scx_disable_task() would WARN on the non-%ENABLED state and trigger a * NONE -> READY validation failure. * - * Either way, reset @p after ops.disable() has seen the final values. A - * %NONE @p can still be a rescuee under the parent. + * %INIT means @p is being forked and hasn't been enabled. + * scx_disable_task() would WARN and call ops.disable() without a + * matching ops.enable(). scx_post_fork() will find @p off the ext class + * and leave it %READY. + * + * In all cases, reset @p after ops.disable() has seen the final values. + * A %NONE @p can still be a rescuee under the parent. */ - if (scx_get_task_state(p) != SCX_TASK_NONE) + state = scx_get_task_state(p); + if (state != SCX_TASK_NONE && state != SCX_TASK_INIT) scx_disable_task(scx_task_sched(p), p); scx_reset_task_fields(rq, p); } -- 2.53.0