From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.72 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796146600; b=Q0Y1eEQdrvi+tWp+bMjH0AoFGZxSoMSoBYuUoxWujjozYPABxyqEhyw2H4DDb3Q3YMhu xoU2nHfXXAG+QkCRshF22R13K9923MYuEm9Z2MIrCj6Ya4c6cQZ3iry/bvl9Rl1KPJgbn 03AFxgDBPMQFOLsiiiHnBUSBPLAeTcmkkfxNnkB4txr/JWdatCgZxQiPhrubV+AknM1Zo D2XYQ6Bc6Ht+rvOzdq8NxkpZtichDw8f6pCIIKmUa1nlvZsQ444davTrhwlcLkZTPNjfZ odPdwTJ+5FDMaAbkovhPqIqU+0yCWTYOajy+lAsjZTlA2V9Yyh4DHAoW19/ARYiQWNQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796146600; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Received:Date:Mime-Version:X-Mailer:Message-ID: Subject:From:To:Cc:Content-Type; bh=TSBQ1MuOVb2h488K0rJ/p2dg4eBuxrU4PPYeolRyLh0=; b=Y93D19hgcNy472JirMQTCSyBk98lshYSaaTB7chviHwSwC1oBiuMLJ+xUPYGYw7EObuU Lxdnr0/Pz47QtPKQMfrDlE1m2goQuRSAWO311mK6uWmlTKmySz1LPKGRjKkhMoGs5d5wW yLBdpZYpAmhFLHoxW1IgXB+8uxhvSShnhTudiBPgERCYY4uDZymehzu571EGn+uhIi1XS 6nGayn89Vlji9ChlL8O2/Tef7J4BmwS/EkRHX/N6ZpbLqtx/bXFoz1LlX7Uvf9RptzHo0 2g0cjNv9IilKlmM7+mHOeNun2JUunjWv6zlwJ9ST4F0rKUejJeHGGv0+SLQXSY4u33A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--jpiecuch.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=k7LYGPoV; arc=none smtp.remote-ip=209.85.128.72 Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) (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 88889606A325 for ; Tue, 1 Dec 2026 17:36:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 88889606A325 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jpiecuch.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=k7LYGPoV Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-4bbb2a62535so31046605e9.2 for ; Tue, 01 Dec 2026 09:36:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796146599; x=1796751399; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:mime-version:date:from :to:cc:subject:date:message-id:reply-to:content-type; bh=TSBQ1MuOVb2h488K0rJ/p2dg4eBuxrU4PPYeolRyLh0=; b=k7LYGPoV6z1CgWDJZgz09WBsOWmEH6KNp7d2sOEvv48GzSN7pM4Cmv+EztKr3RZ+rY 3dP64bsEDt1KW2MT78clKWBos4P45Zw7GrOCPhoBScFv15iAQUWBnNwSz1jbXh6gl2sM fERZRiaKM1IPJovcXm4alC8KfNB6ZXRR8++XkckYWrdQbQAsjVK3W0JnwO6hdY6qxcD9 4MIdj5WKEyq5B0lDpjQcfHbR6MYzdRYmu3mPpV7v/gnDk88qTR4oi3RUOky1qVLtx/mb E3PFkfnLbLhmGS2yEFIErMjdTOkjgj1Mx7AZcEIUJHOFyOhGJlKf/JiPIR+q6DCs4Wvb DEXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796146599; x=1796751399; h=content-type:cc:to:from:subject:message-id:mime-version:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=TSBQ1MuOVb2h488K0rJ/p2dg4eBuxrU4PPYeolRyLh0=; b=IifT6qC+PmF1/d9YBf/We6p4Y41y7nx2Dd8+5WxfDQ2aFfv2cY3OpXNuj8TUNxwa9X cB8nyewA7vI2NTTCyfP64CE8XsaK0S91kQm4Y6/6c5+DRAdO6njich82w//St3L7zEF1 XL9NfqIlpvKwz7Ty+U6/dLXES4RXA57G4Ir98P1sYOceI5wKT5NxaZEWYHtGeao+AQqZ sWuQQTvGrhGSfP6h6jKnNwLOBFXeAvlTNnLc5woJ7QnKICWc7masOpPArsv66Gj34DCq 7wd5RSM4Anwt4HARX8utOjp97Pxv+bhu+lb7yKlG+LTRSzf/91jmyQCnn1VIhg88XgYN xEYw== X-Gm-Message-State: APuU4Z9mRn7qnud44mXtr0ZtlmgaQnubDM7m/+7X7oQngj6qMV+fP1vC C2zMIu3nqZck+KzYDYl8cJhEPP4MOCV0TpdPpy2KA6Lh/A4SePtxX8bR7ClxAqKK3WOdyTAFt1U 8CyW+hMSDN/GeOGA6gD6hYu8hDOfkcsex4C+Yfo6s5ABtGUJd54LvhxMdMLIAZQvTwAmdLuqfiK HQ/pQXgtxfOli5+FRJMkbWU4J8WrlRAnyWvTs1D2zZTQ+BSZY= X-Received: from wmbje17.prod.google.com ([2002:a05:600c:1f91:b0:4bb:5e69:8177]) (user=jpiecuch job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:1d09:b0:4bb:73d5:ab33 with SMTP id 5b1f17b1804b1-4bb88816acemr828627995e9.22.1796146598585; Tue, 01 Dec 2026 09:36:38 -0800 (PST) Date: Tue, 1 Dec 2026 17:36:37 +0000 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 X-Mailer: git-send-email 2.56.1.911.g883d8bcb20-goog Message-ID: <20261201173537.1103264-1-jpiecuch@google.com> Subject: [PATCH 6.18.y] sched_ext: Fix this_rq() assumptions in dispatch kfuncs From: Kuba Piecuch To: stable@vger.kernel.org Cc: Tejun Heo , Greg Kroah-Hartman , Sasha Levin , sched-ext@lists.linux.dev, linux-kernel@vger.kernel.org, Kuba Piecuch Content-Type: text/plain; charset="UTF-8" From: Tejun Heo [ Upstream commit 3dd52416e44a70bc993adb96d2e0d71b9ea21359 ] Under core scheduling, dispatch runs from within the core-wide pick and can target a sibling rq, so ops.dispatch() may execute on a CPU different from the dispatched rq's. Several kfunc paths assumed the two always coincide: - scx_dsq_move() decided whether an rq lock is held by testing this_rq()'s rq flags and lock-danced accordingly. A dispatch for a sibling took the unlocked-context branch and acquired the source rq lock on top of the already held dispatched rq lock which could deadlock. - scx_bpf_sub_dispatch() dispatched this_rq() with its stashed sub_dispatch_prev, which is NULL when dispatching for a sibling. - finish_dispatch(), scx_bpf_dsq_reenq() and scx_bpf_dsq_nr_queued() resolved SCX_DSQ_LOCAL to this CPU's local DSQ rather than the dispatched rq's. The latter two are callable from other rq-locked operations too, where SCX_DSQ_LOCAL now likewise resolves to the op's rq. This changes behavior also without core scheduling, e.g. for ops.enqueue() running a remote wakeup on the waking CPU, and is intended: which CPU happens to execute an operation is incidental, the op's rq is what it is operating on, and the resolution now matches the insert side where SCX_DSQ_LOCAL dispatches land on the task's rq. Use the rq tracked by scx_locked_rq(), which is set to the dispatched rq around ops invocations and NULL in unlocked contexts. Fixes: 4c95380701f5 ("sched/ext: Fold balance_scx() into pick_task_scx()") Cc: stable@vger.kernel.org # v6.19+ Signed-off-by: Tejun Heo [ jpiecuch: The bug affects 6.18.y because balance_scx() runs balance_one() for SMT sibling rqs under core scheduling since commit 7b0888b7cc19 ("sched_ext: Implement core-sched support"). Adapted path to kernel/sched/ext.c, replaced switch_rq_lock() with raw_spin_rq_unlock()/raw_spin_rq_lock(), kept scx_breather(), and dropped the scx_bpf_sub_dispatch() and scx_bpf_dsq_reenq() hunks which do not exist in 6.18.y. ] Signed-off-by: Kuba Piecuch --- Tested on v6.18.66 (CONFIG_SCHED_CORE=y, CONFIG_PROVE_LOCKING=y, CONFIG_DEBUG_ATOMIC_SLEEP=y, SMT-2 4-CPU VM): - Custom SMT-2 PR_SCHED_CORE BPF reproducer exercising sibling ops.dispatch(): - scx_bpf_dsq_insert(SCX_DSQ_LOCAL): fails 5/5 without (wrong CPU), passes 5/5 with (0 wrong CPU). - scx_bpf_dsq_nr_queued(SCX_DSQ_LOCAL): fails 5/5 without (mismatches), passes 5/5 with (0 mismatches). - scx_bpf_dsq_move(..., SCX_DSQ_LOCAL): fails 3/3 without (lockdep possible recursive locking detected + deadlock), passes 5/5 with (0 wrong CPU, 0 lockdep splats). - tools/testing/selftests/sched_ext/runner: 23/23 pass (after locally working around the unrelated select_cpu_vtime.bpf.c build break on v6.18.66), with no kernel warnings or lockdep splats. This backport was prepared with the help of an AI coding assistant. I have reviewed the patch and verified the tests run by the tool. kernel/sched/ext.c | 47 ++++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/kernel/sched/ext.c b/kernel/sched/ext.c index b3a6d6070940..8a378a50c344 100644 --- a/kernel/sched/ext.c +++ b/kernel/sched/ext.c @@ -2062,7 +2062,7 @@ static void finish_dispatch(struct scx_sched *sch, struct rq *rq, BUG_ON(!(p->scx.flags & SCX_TASK_QUEUED)); - dsq = find_dsq_for_dispatch(sch, this_rq(), dsq_id, p); + dsq = find_dsq_for_dispatch(sch, rq, dsq_id, p); if (dsq->id == SCX_DSQ_LOCAL) dispatch_to_local_dsq(sch, rq, dsq, p, enq_flags); @@ -5762,9 +5762,8 @@ static bool scx_dsq_move(struct bpf_iter_scx_dsq_kern *kit, { struct scx_sched *sch = scx_root; struct scx_dispatch_q *src_dsq = kit->dsq, *dst_dsq; - struct rq *this_rq, *src_rq, *locked_rq; + struct rq *p_rq, *src_rq, *locked_rq; bool dispatched = false; - bool in_balance; unsigned long flags; /* @@ -5780,19 +5779,23 @@ static bool scx_dsq_move(struct bpf_iter_scx_dsq_kern *kit, return false; /* - * Can be called from either ops.dispatch() locking this_rq() or any - * context where no rq lock is held. If latter, lock @p's task_rq which - * we'll likely need anyway. + * Can be called from either ops.dispatch() holding the dispatched rq's + * lock or any context where no rq lock is held. If latter, lock @p's + * task_rq which we'll likely need anyway. */ src_rq = task_rq(p); local_irq_save(flags); - this_rq = this_rq(); - in_balance = this_rq->scx.flags & SCX_RQ_IN_BALANCE; - if (in_balance) { - if (this_rq != src_rq) { - raw_spin_rq_unlock(this_rq); + /* + * Under core scheduling, dispatch can run for a sibling rq, so the + * locked rq is not necessarily this CPU's. + */ + locked_rq = scx_locked_rq(); + + if (locked_rq) { + if (locked_rq != src_rq) { + raw_spin_rq_unlock(locked_rq); raw_spin_rq_lock(src_rq); } } else { @@ -5806,7 +5809,7 @@ static bool scx_dsq_move(struct bpf_iter_scx_dsq_kern *kit, */ scx_breather(src_rq); - locked_rq = src_rq; + p_rq = src_rq; raw_spin_lock(&src_dsq->lock); /* @@ -5822,7 +5825,7 @@ static bool scx_dsq_move(struct bpf_iter_scx_dsq_kern *kit, } /* @p is still on $src_dsq and stable, determine the destination */ - dst_dsq = find_dsq_for_dispatch(sch, this_rq, dsq_id, p); + dst_dsq = find_dsq_for_dispatch(sch, locked_rq ?: this_rq(), dsq_id, p); /* * Apply vtime and slice updates before moving so that the new time is @@ -5835,16 +5838,16 @@ static bool scx_dsq_move(struct bpf_iter_scx_dsq_kern *kit, p->scx.slice = kit->slice; /* execute move */ - locked_rq = move_task_between_dsqs(sch, p, enq_flags, src_dsq, dst_dsq); + p_rq = move_task_between_dsqs(sch, p, enq_flags, src_dsq, dst_dsq); dispatched = true; out: - if (in_balance) { - if (this_rq != locked_rq) { - raw_spin_rq_unlock(locked_rq); - raw_spin_rq_lock(this_rq); + if (locked_rq) { + if (locked_rq != p_rq) { + raw_spin_rq_unlock(p_rq); + raw_spin_rq_lock(locked_rq); } } else { - raw_spin_rq_unlock_irqrestore(locked_rq, flags); + raw_spin_rq_unlock_irqrestore(p_rq, flags); } kit->cursor.flags &= ~(__SCX_DSQ_ITER_HAS_SLICE | @@ -6276,6 +6279,10 @@ __bpf_kfunc void scx_bpf_kick_cpu(s32 cpu, u64 flags) * * Return the number of tasks in the DSQ matching @dsq_id. If not found, * -%ENOENT is returned. + * + * %SCX_DSQ_LOCAL resolves to the local DSQ of the rq the current scheduler + * operation is locked to - e.g. the rq being dispatched for in ops.dispatch() - + * or the calling CPU's when no rq is locked. */ __bpf_kfunc s32 scx_bpf_dsq_nr_queued(u64 dsq_id) { @@ -6292,7 +6299,7 @@ __bpf_kfunc s32 scx_bpf_dsq_nr_queued(u64 dsq_id) } if (dsq_id == SCX_DSQ_LOCAL) { - ret = READ_ONCE(this_rq()->scx.local_dsq.nr); + ret = READ_ONCE((scx_locked_rq() ?: this_rq())->scx.local_dsq.nr); goto out; } else if ((dsq_id & SCX_DSQ_LOCAL_ON) == SCX_DSQ_LOCAL_ON) { s32 cpu = dsq_id & SCX_DSQ_LOCAL_CPU_MASK; base-commit: 2c7bb1d0212c23234a9f85123eb7a36eecc76776 -- 2.56.1.839.g22a8290db9-goog