From: Kuba Piecuch <jpiecuch@google.com>
To: Tejun Heo <tj@kernel.org>, David Vernet <void@manifault.com>,
Andrea Righi <arighi@nvidia.com>,
Changwoo Min <changwoo@igalia.com>
Cc: Kuba Piecuch <jpiecuch@google.com>,
fangqiurong <fangqiurong@kylinos.cn>,
sched-ext@lists.linux.dev, linux-kernel@vger.kernel.org
Subject: [PATCH 3/3] selftests/sched_ext: Test that ops.dequeue() runs with the task's rq locked
Date: Mon, 23 Nov 2026 17:10:34 +0000 [thread overview]
Message-ID: <20261123170846.3165648-4-jpiecuch@google.com> (raw)
In-Reply-To: <20261123170846.3165648-1-jpiecuch@google.com>
Check in dequeue_remote's ops.dequeue() that scx_bpf_locked_rq() is the
task's rq, and add a scenario that inserts tasks into SCX_DSQ_GLOBAL from
ops.dispatch(), mostly from CPUs other than the task's.
dequeue_remote fails without "sched_ext: Fix ops_state and ops.dequeue()
locking races in dispatch".
Signed-off-by: Kuba Piecuch <jpiecuch@google.com>
---
.../selftests/sched_ext/dequeue_remote.bpf.c | 34 +++++++++++++++--
.../selftests/sched_ext/dequeue_remote.c | 37 +++++++++++++++----
2 files changed, 61 insertions(+), 10 deletions(-)
diff --git a/tools/testing/selftests/sched_ext/dequeue_remote.bpf.c b/tools/testing/selftests/sched_ext/dequeue_remote.bpf.c
index 983590c6d498..8996bcc96e1d 100644
--- a/tools/testing/selftests/sched_ext/dequeue_remote.bpf.c
+++ b/tools/testing/selftests/sched_ext/dequeue_remote.bpf.c
@@ -8,7 +8,11 @@
* the dispatching CPU's local DSQ, so most moves cross CPUs. With
* @test_use_move_to_local, tasks are queued on a user DSQ and consumed with
* scx_bpf_dsq_move_to_local(). Otherwise, they are queued in a BPF queue and
- * dispatched with SCX_DSQ_LOCAL_ON.
+ * dispatched with SCX_DSQ_LOCAL_ON or, with @test_use_global, inserted into
+ * SCX_DSQ_GLOBAL, often from a CPU other than the task's.
+ *
+ * ops.dequeue() also verifies that it's always called with the task's rq
+ * locked.
*
* Copyright (c) 2026 Google LLC.
*/
@@ -53,10 +57,11 @@ struct task_struct___core_sched {
} __attribute__((preserve_access_index));
bool test_use_move_to_local;
+bool test_use_global;
u64 enqueue_cnt, dequeue_cnt, dispatch_dequeue_cnt, change_dequeue_cnt;
u64 remote_dispatch_cnt, remote_running_cnt, missed_dequeue_cnt;
-u64 core_sched_exec_dequeue_cnt;
+u64 core_sched_exec_dequeue_cnt, remote_global_cnt, wrong_rq_dequeue_cnt;
static struct task_ctx *lookup_task_ctx(struct task_struct *p)
{
@@ -121,9 +126,22 @@ void BPF_STRUCT_OPS(dequeue_remote_enqueue, struct task_struct *p, u64 enq_flags
void BPF_STRUCT_OPS(dequeue_remote_dequeue, struct task_struct *p, u64 deq_flags)
{
struct task_ctx *tctx;
+ struct rq *locked_rq;
__sync_fetch_and_add(&dequeue_cnt, 1);
+ /*
+ * ops.dequeue() must always run with @p's rq locked, even when @p is
+ * inserted into SCX_DSQ_GLOBAL by ops.dispatch() on another CPU.
+ */
+ locked_rq = scx_bpf_locked_rq();
+ if (!locked_rq || locked_rq->cpu != scx_bpf_task_cpu(p)) {
+ __sync_fetch_and_add(&wrong_rq_dequeue_cnt, 1);
+ scx_bpf_error("%d (%s): dequeue without task rq lock (locked=%d cpu=%d)",
+ p->pid, p->comm, locked_rq ? locked_rq->cpu : -1,
+ scx_bpf_task_cpu(p));
+ }
+
tctx = lookup_task_ctx(p);
if (!tctx)
return;
@@ -187,7 +205,17 @@ void BPF_STRUCT_OPS(dequeue_remote_dispatch, s32 cpu, struct task_struct *prev)
continue;
}
- if (bpf_cpumask_test_cpu(cpu, p->cpus_ptr)) {
+ if (test_use_global) {
+ /*
+ * @p goes on the global DSQ of its own node, which this
+ * CPU may not consume. Kick @p's CPU in case it's idle.
+ */
+ if (scx_bpf_task_cpu(p) != cpu) {
+ __sync_fetch_and_add(&remote_global_cnt, 1);
+ scx_bpf_kick_cpu(scx_bpf_task_cpu(p), SCX_KICK_IDLE);
+ }
+ scx_bpf_dsq_insert(p, SCX_DSQ_GLOBAL, SCX_SLICE_DFL, 0);
+ } else if (bpf_cpumask_test_cpu(cpu, p->cpus_ptr)) {
if (scx_bpf_task_cpu(p) != cpu)
__sync_fetch_and_add(&remote_dispatch_cnt, 1);
scx_bpf_dsq_insert(p, SCX_DSQ_LOCAL_ON | cpu,
diff --git a/tools/testing/selftests/sched_ext/dequeue_remote.c b/tools/testing/selftests/sched_ext/dequeue_remote.c
index af344581d5f1..aab6dfb2d64f 100644
--- a/tools/testing/selftests/sched_ext/dequeue_remote.c
+++ b/tools/testing/selftests/sched_ext/dequeue_remote.c
@@ -1,7 +1,9 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Verify that ops.dequeue() is called for tasks leaving BPF custody through
- * an SCX-internal cross-CPU migration (move_remote_task_to_local_dsq()).
+ * an SCX-internal cross-CPU migration (move_remote_task_to_local_dsq()) and
+ * that it always runs with the task's rq locked, including when the task is
+ * inserted into SCX_DSQ_GLOBAL from another CPU.
*
* Copyright (c) 2026 Google LLC.
*/
@@ -54,7 +56,7 @@ static void worker_fn(int id)
}
static enum scx_test_status run_scenario(struct dequeue_remote *skel,
- bool use_move_to_local,
+ bool use_move_to_local, bool use_global,
const char *name)
{
enum scx_test_status ret = SCX_TEST_PASS;
@@ -69,6 +71,7 @@ static enum scx_test_status run_scenario(struct dequeue_remote *skel,
nr_workers = MAX_WORKERS;
skel->bss->test_use_move_to_local = use_move_to_local;
+ skel->bss->test_use_global = use_global;
skel->bss->enqueue_cnt = 0;
skel->bss->dequeue_cnt = 0;
skel->bss->dispatch_dequeue_cnt = 0;
@@ -77,6 +80,8 @@ static enum scx_test_status run_scenario(struct dequeue_remote *skel,
skel->bss->remote_running_cnt = 0;
skel->bss->missed_dequeue_cnt = 0;
skel->bss->core_sched_exec_dequeue_cnt = 0;
+ skel->bss->remote_global_cnt = 0;
+ skel->bss->wrong_rq_dequeue_cnt = 0;
memset(&skel->data->uei, 0, sizeof(skel->data->uei));
link = bpf_map__attach_struct_ops(skel->maps.dequeue_remote_ops);
@@ -126,7 +131,10 @@ static enum scx_test_status run_scenario(struct dequeue_remote *skel,
(unsigned long)skel->bss->dequeue_cnt,
(unsigned long)skel->bss->dispatch_dequeue_cnt,
(unsigned long)skel->bss->change_dequeue_cnt);
- if (!use_move_to_local)
+ if (use_global)
+ printf(" remote SCX_DSQ_GLOBAL dispatches: %lu\n",
+ (unsigned long)skel->bss->remote_global_cnt);
+ else if (!use_move_to_local)
printf(" remote SCX_DSQ_LOCAL_ON dispatches: %lu\n",
(unsigned long)skel->bss->remote_dispatch_cnt);
printf(" ran on a CPU other than the enqueue CPU: %lu\n",
@@ -135,15 +143,25 @@ static enum scx_test_status run_scenario(struct dequeue_remote *skel,
(unsigned long)skel->bss->missed_dequeue_cnt);
printf(" late SCX_DEQ_CORE_SCHED_EXEC dequeues: %lu\n",
(unsigned long)skel->bss->core_sched_exec_dequeue_cnt);
+ printf(" dequeues without the task's rq locked: %lu\n",
+ (unsigned long)skel->bss->wrong_rq_dequeue_cnt);
if (skel->data->uei.kind != EXIT_KIND(SCX_EXIT_UNREG))
SCX_ERR("Scheduler exited with kind=%lld: %s",
(long long)skel->data->uei.kind, skel->data->uei.msg);
SCX_EQ(skel->data->uei.kind, EXIT_KIND(SCX_EXIT_UNREG));
- /* the test is meaningless if no task was moved across CPUs */
- SCX_GT(skel->bss->remote_running_cnt, 0);
+ /*
+ * The test is meaningless if no task was moved across CPUs. Global DSQs
+ * are per node, so the global scenario may not migrate tasks at all and
+ * needs remote global dispatches instead.
+ */
+ if (use_global)
+ SCX_GT(skel->bss->remote_global_cnt, 0);
+ else
+ SCX_GT(skel->bss->remote_running_cnt, 0);
SCX_EQ(skel->bss->enqueue_cnt, skel->bss->dequeue_cnt);
+ SCX_EQ(skel->bss->wrong_rq_dequeue_cnt, 0);
return SCX_TEST_PASS;
}
@@ -174,16 +192,21 @@ static enum scx_test_status run(void *ctx)
struct dequeue_remote *skel = ctx;
enum scx_test_status status;
- status = run_scenario(skel, false,
+ status = run_scenario(skel, false, false,
"BPF queue -> SCX_DSQ_LOCAL_ON | cpu");
if (status != SCX_TEST_PASS)
return status;
- status = run_scenario(skel, true,
+ status = run_scenario(skel, true, false,
"user DSQ -> scx_bpf_dsq_move_to_local()");
if (status != SCX_TEST_PASS)
return status;
+ status = run_scenario(skel, false, true,
+ "BPF queue -> SCX_DSQ_GLOBAL");
+ if (status != SCX_TEST_PASS)
+ return status;
+
return SCX_TEST_PASS;
}
--
2.56.1.750.gf70dc682cf-goog
prev parent reply other threads:[~2026-11-23 17:10 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-23 17:10 [PATCHSET sched_ext/for-7.4-fixes] sched_ext: Fix ops_state and ops.dequeue() locking in dispatch Kuba Piecuch
2026-11-23 17:10 ` [PATCH 1/3] sched_ext: Fix ops_state and ops.dequeue() locking races " Kuba Piecuch
2026-11-25 0:18 ` Tejun Heo
2026-11-25 11:07 ` Kuba Piecuch
2026-11-23 17:10 ` [PATCH 2/3] sched_ext: Remove scx_wait_dispatching() Kuba Piecuch
2026-11-23 17:10 ` Kuba Piecuch [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261123170846.3165648-4-jpiecuch@google.com \
--to=jpiecuch@google.com \
--cc=arighi@nvidia.com \
--cc=changwoo@igalia.com \
--cc=fangqiurong@kylinos.cn \
--cc=linux-kernel@vger.kernel.org \
--cc=sched-ext@lists.linux.dev \
--cc=tj@kernel.org \
--cc=void@manifault.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.