From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.71 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796146799; b=g33CiAMQ0G3fJK2I+8AfFsfxz5nM3AbZu1za7NlkWz3bBACP8AajyJ+sjzTLCyP1aLCR BwPKvHi4AFl2eYqXFkEgXqKrEiEMdxCThgYagNkqCJWSXbQVMHK/rXPQivVD4yLyli9vn E/d8uSJapHN9Q2A0S8YPDcFpGY+dDlj88s0RhWyqodQzjpt+qHgbqOcKBfGbGvI0zw19Z hxq1nFm65iCONp6vNTE1SXejN3g92YF68ZX9SY1ZVUd0uqbrYtRXmSY7ijDcizlzTKR5F ZNqqs1AvIHGG/T6h+EhYDJUVy+ECB5hWOCvGVPEINtZ90LPnMo20V7TE83TDrRuBjWg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796146799; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:Mime-Version: X-Mailer:Message-ID:Subject:From:To:Cc:Content-Type; bh=LfrV7GZ4IWWLJ1AjGCbHXKVi1w7skTuxI+s3LOCzlGU=; b=Z2zfkb7K1K0fo+o23/CdRekuLrwbq2bJt35kRuvMwTV398SK0VCk3BbIo8FE0NN4JZeq 3vvjKWCv4IZEgbrTCEv+4/d/rIJaeaqU3rZIBH9fMmu0ORuEDXFBEpNBoERli+YM5TpYw DXpIWq9cjyrPb/Krj/nhgaKFEBbgxycGhpU51+x1sS6u+djYjjU6A5LZkN1KPrYZ1Ajtj jucjQcyIt71FH4MZA/gFcLrYBNyMYFOfZQhDD5+D4jT1j7lkL+o6q4eAknXkqZgq3XPqj gca+XgMcJQU6ACTYh3KTei/jRziUeZfB8BEjMOlkSIsZJj3cs4fP3o+czDSsqiI0kKw== 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=IAdH8zTu; arc=none smtp.remote-ip=209.85.128.71 Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) (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 E9A74606A322 for ; Tue, 1 Dec 2026 17:39:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E9A74606A322 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=IAdH8zTu Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-4bb69d84053so93951065e9.0 for ; Tue, 01 Dec 2026 09:39:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796146797; x=1796751597; 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=LfrV7GZ4IWWLJ1AjGCbHXKVi1w7skTuxI+s3LOCzlGU=; b=IAdH8zTu+UUzgcsl7QlgEh3PJUmXLiMJ1a8KnuNE8jLfZ0ewDIGNc21B+BynrKui78 ztXAS97isethd/8RgrxxNfJavpTbSUFZ8MLMMozWMn4QQeXx9RCU46JoLkxAneFcXBNl rZhLCWM0+JNKuVGN0NPRdENwcFyQf6vBdicRb+fZbJc713m3EKDQDnL+0OVBL0iDTEOh sTy33IoHIuH+wHuxy7q8IktUsgLGCf2BthRoXubaJazkitsyaAnEg1fVu9iXluUhMO/G P8IXJw+UAAzt5WiRd/jxacbXNXA2SimzU1ezWL9lWBJ1qTy1mYbyoFKU6IpmmxGsuruH HmCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796146797; x=1796751597; 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=LfrV7GZ4IWWLJ1AjGCbHXKVi1w7skTuxI+s3LOCzlGU=; b=GsGhRy1uCBnkAqVbsY2lptGaM32kUJusDPqotDHaOZ5lrtHjMl5oJ9O20hwB0kMRWp YnjRkw5x76+wsDjK2wwkocuKZUzxMvqBvgpcAW/gw9/c3aazT+OXx854eex95xDYI/zw BFX70LnVayKZg7u57fd72MVzPrkU6QmzXvNhKIakz/ghy1xe53tJUEk+NelT4MmMnLQD Hf5eeqPBp59wW0HlrCjoyRndQeTJb17aFP7Sb6WtjcXaup4MHo/bsxHN1HI24idDNmMP ejtFOqzwtCw1ArwIbv1tFSc5CuWxmVPHKAFOp5QzeNb87VcPgq30248HgV5r+PMEoaqy nBFw== X-Forwarded-Encrypted: i=1; AL+hErF91zz8CoLqOz2qvYLKGtJ3N7xyIVvtep7hG7gcC54psC78jjQeEwpnDNTuXj5Nkcn2/8Hdp96+aSW6WXw=@vger.kernel.org X-Gm-Message-State: APuU4Z92qEaBRaTnjpp9F3USY3wG6L5nW4hYA/I+3nvPB7x7HI3U+mOk KejeZqR3CRsHRImGsFJFMWJLv1aWDUrwasFAUo/z2MLrPIwepoBTwSRhXl0QSQbaRv/MMhwBQSA YqbSC96xdZBxfHA== X-Received: from wmsp23.prod.google.com ([2002:a05:600c:1d97:b0:4a1:811f:6de2]) (user=jpiecuch job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:8888:b0:4bb:aa4f:c022 with SMTP id 5b1f17b1804b1-4bbaa4fc136mr316584055e9.2.1796146797173; Tue, 01 Dec 2026 09:39:57 -0800 (PST) Date: Tue, 1 Dec 2026 17:39:56 +0000 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 X-Mailer: git-send-email 2.56.1.911.g883d8bcb20-goog Message-ID: <20261201173846.2308513-1-jpiecuch@google.com> Subject: [PATCH 6.12.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.12.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 and scx_dispatch_from_dsq(), replaced switch_rq_lock() with raw_spin_rq_unlock()/raw_spin_rq_lock(), and dropped the scx_bpf_sub_dispatch() and scx_bpf_dsq_reenq() hunks which do not exist in 6.12.y. 6.12.y lacks scx_locked_rq(), so resolve the dispatched rq from scx_dsp_ctx->rq when SCX_KF_DISPATCH is set (balance_one() is its sole caller and always initializes dspc->rq); non-dispatch rq-locked ops such as remote-wakeup ops.enqueue() keep using this_rq() in scx_bpf_dsq_nr_queued(SCX_DSQ_LOCAL). ] Signed-off-by: Kuba Piecuch --- Tested on v6.12.120 (CONFIG_SCHED_CORE=y, CONFIG_SCHED_DEBUG=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_dispatch(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_dispatch_from_dsq(..., 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: 21/21 pass, 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 | 50 +++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/kernel/sched/ext.c b/kernel/sched/ext.c index 7a4b23e48cc6..5206c43f3f1e 100644 --- a/kernel/sched/ext.c +++ b/kernel/sched/ext.c @@ -2754,7 +2754,7 @@ static void finish_dispatch(struct rq *rq, struct task_struct *p, BUG_ON(!(p->scx.flags & SCX_TASK_QUEUED)); - dsq = find_dsq_for_dispatch(this_rq(), dsq_id, p); + dsq = find_dsq_for_dispatch(rq, dsq_id, p); if (dsq->id == SCX_DSQ_LOCAL) dispatch_to_local_dsq(rq, dsq, p, enq_flags); @@ -6415,9 +6415,8 @@ static bool scx_dispatch_from_dsq(struct bpf_iter_scx_dsq_kern *kit, u64 enq_flags) { 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; /* @@ -6432,26 +6431,31 @@ static bool scx_dispatch_from_dsq(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 = (current->scx.kf_mask & SCX_KF_DISPATCH) ? + this_cpu_read(scx_dsp_ctx->rq) : NULL; + + if (locked_rq) { + if (locked_rq != src_rq) { + raw_spin_rq_unlock(locked_rq); raw_spin_rq_lock(src_rq); } } else { raw_spin_rq_lock(src_rq); } - locked_rq = src_rq; + p_rq = src_rq; raw_spin_lock(&src_dsq->lock); /* @@ -6467,7 +6471,7 @@ static bool scx_dispatch_from_dsq(struct bpf_iter_scx_dsq_kern *kit, } /* @p is still on $src_dsq and stable, determine the destination */ - dst_dsq = find_dsq_for_dispatch(this_rq, dsq_id, p); + dst_dsq = find_dsq_for_dispatch(locked_rq ?: this_rq(), dsq_id, p); /* * Apply vtime and slice updates before moving so that the new time is @@ -6480,16 +6484,16 @@ static bool scx_dispatch_from_dsq(struct bpf_iter_scx_dsq_kern *kit, p->scx.slice = kit->slice; /* execute move */ - locked_rq = move_task_between_dsqs(p, enq_flags, src_dsq, dst_dsq); + p_rq = move_task_between_dsqs(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 | @@ -6860,6 +6864,9 @@ __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 being dispatched for in + * ops.dispatch(), or the calling CPU's otherwise. */ __bpf_kfunc s32 scx_bpf_dsq_nr_queued(u64 dsq_id) { @@ -6869,7 +6876,10 @@ __bpf_kfunc s32 scx_bpf_dsq_nr_queued(u64 dsq_id) preempt_disable(); if (dsq_id == SCX_DSQ_LOCAL) { - ret = READ_ONCE(this_rq()->scx.local_dsq.nr); + struct rq *rq = (current->scx.kf_mask & SCX_KF_DISPATCH) ? + this_cpu_read(scx_dsp_ctx->rq) : this_rq(); + + ret = READ_ONCE(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: fa3ff5a9ce6f5ada4510bda0d8b891018a0be4b7 -- 2.56.1.839.g22a8290db9-goog