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=1796379380; b=HiPNboWZeXRie+nct90bfVInXGdXd1UjNAwXtf5bv9TAgNB3H6uCZXp6pLdzxN9P0WB3 1Juhby+64m7w+UnQDv/5qgLeknoB3+onhDFAFwxkwwfHKLoI7sqfB1n0+pKcZQY1xYAWJ LvGNu047vmN02lfK100Iesp1k0T2QTyUxH81PIqMPsJBbAlEnqmLBY63hQmJMpwXeCm7W u3SVe5ZC70V0S5NKUbjzFaOt8ARCanYbEN+AjXXl+bncFu4SMbzYfxfeFi2xAfyDNFP7J LWpNYSNMx4cWwx3ZXDl93J9SxE8ukemM8tnGfQge+zGbEjUgzYYVqN9lMzgwINiiHkQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796379380; 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=9IEjYukMxqJns6VwtLJKgvBAFOE1tyJaDANzKcD6Rcc=; b=puzG/FafSFaBoPhBmCoplRM+Dq0vBfn9lh4nVsdktaF2BS1iyxELeQdhWh1pH0ZOmOqR Bp5oBlM1pl/2YsWgHho5v2vFQgnl3+jwu7rdyD377TR9Q/ubowkus0aCXTJpNeyK7IDL0 NjIP81m5vMP+6flKAupMYI45b6Bbel/BcKkwDIFViRx6Z0LQ5SJ6sKoLkOMNT/dNa8zcg c3cexJ95xPNqqWmPmeAVV5WfyvjleVO36Ql4zxGlvaw+KDw4juHzrxGXf+IsXtoxivhVT HfwBY4US2SSEnfZShpThcV/bfsx9UVVsTKq6uskb4WEoBpJbT14XbiTXbTBRlGymUNw== 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=gLzLUcAX; 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 3E854606A335; Fri, 4 Dec 2026 10:16:20 +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=gLzLUcAX Received: by smtp.kernel.org (Postfix) with ESMTPSA id A90641F000C2; Fri, 4 Dec 2026 10:16:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796379380; bh=9IEjYukMxqJns6VwtLJKgvBAFOE1tyJaDANzKcD6Rcc=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=gLzLUcAXlzXi4UgSVaOWIcKj5DMPZ5qeJGve+Ml86t2cPQwJYtzb49wQNMlNzQZ7S RaWzKGy1WK9GogJ/6GiN3G5SGyul1a1V4ADzHWk78GzTR9w3ASMmcGaEaKPcAfFaRC jOKGKsCVeneHb3M5h8j90d1CeOXtOLlDphVOSD5s= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Sashiko , Kuba Piecuch , Andrea Righi , Tejun Heo , Sasha Levin Subject: [PATCH 6.18 035/703] sched_ext: Read exit_kind before flushing in scx_flush_disable_work() Date: Fri, 4 Dec 2026 18:58:54 +0900 Message-ID: <20261204100628.535389518@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100627.522266145@linuxfoundation.org> References: <20261204100627.522266145@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 6.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Kuba Piecuch [ Upstream commit 3133107d98057595c0af39ff907c852134926912 ] scx_disable_workfn() sets exit_kind to SCX_EXIT_DONE before it tears the scheduler down. scx_flush_disable_work() reads exit_kind after syncing disable_irq_work and flushing disable_work, so it can observe DONE from a disable_work that was queued after its flush and return while the teardown is still running: CPU A CPU B ----- ----- scx_vexit() scx_claim_exit() -> true bpf_struct_ops_map_link_detach() bpf_scx_unreg() scx_disable() scx_claim_exit() -> false scx_flush_disable_work() irq_work_sync() // not queued kthread_flush_work() // not queued irq_work_queue() scx_disable_irq_workfn() kthread_queue_work() scx_disable_workfn() exit_kind = SCX_EXIT_DONE scx_root_disable() atomic_read() -> SCX_EXIT_DONE bpf_map_put() Once bpf_scx_unreg() returns, its caller drops the struct_ops map reference, and the struct_ops trampolines can be freed while scx_root_disable() is still running and can still call into the BPF scheduler, e.g. ops.exit(). Read exit_kind before syncing and flushing, with acquire ordering paired with the atomic_try_cmpxchg() in scx_disable_workfn(). DONE is only set from disable_work, so once it is observed, the following kthread_flush_work() sees the queued work and waits for the teardown to finish. This patch was created with the help of an AI coding assistant. Fixes: d99f7a32f09d ("sched_ext: Fix scx_flush_disable_work() UAF race") Cc: stable@vger.kernel.org # v7.1+ Reported-by: Sashiko Closes: https://lore.kernel.org/all/sashiko-outbox-195908@kernel.org/ Signed-off-by: Kuba Piecuch Reviewed-by: Andrea Righi Signed-off-by: Tejun Heo [ jpiecuch: applied kernel/sched/ext/ext.c hunk to kernel/sched/ext.c; kept error_irq_work instead of disable_irq_work. ] Signed-off-by: Kuba Piecuch Signed-off-by: Sasha Levin --- kernel/sched/ext.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/kernel/sched/ext.c b/kernel/sched/ext.c index b3a6d60709403..64a68d698b823 100644 --- a/kernel/sched/ext.c +++ b/kernel/sched/ext.c @@ -4247,10 +4247,17 @@ static void scx_flush_disable_work(struct scx_sched *sch) { int kind; + /* + * scx_disable_workfn() sets SCX_EXIT_DONE before tearing down. Read + * exit_kind with acquire ordering (paired with atomic_try_cmpxchg() in + * scx_disable_workfn()) before syncing and flushing so that, once DONE + * is seen, the following flush observes the queued work and waits for + * the teardown to finish. + */ do { + kind = atomic_read_acquire(&sch->exit_kind); irq_work_sync(&sch->error_irq_work); kthread_flush_work(&sch->disable_work); - kind = atomic_read(&sch->exit_kind); } while (kind != SCX_EXIT_NONE && kind != SCX_EXIT_DONE); } -- 2.53.0