From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.72 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795453840; b=P5hnb00qP+cLTXAmR8TXhsYY58CCYHvtdI4yyjLgda4WW8Ar/mvkTV4BOfunqyW8P1je z2/Scink/GyVgJTi9lKq6vZvjkVTCV0Wuqj3yG5XZYuQ8JWvww7iph7eEkpRkvuZz5heX TiCiQpah7Y5TJPFsJjbD9cswEH1N56PXb2hcQtHjs8bynR0RH0C/4UpITAK/ExEvFHA3g hNjCmGFVc2VAee1vGAHVa0xAKGwgt73LE3K4Jt/6o2UgTJG72ZPnNx7nyasZiUC1Z5eSI ucEsQtQLguD5+iq1TYRl3nWsjl24FjA0WYwekjv/ziTsK9Fm3Hneqvw4oZdp2EGaNbA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795453840; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=t25R0OQppt5JZE3XIImm1+OnaJwjOmNRYl7971qMLRY=; b=k+PS30j3aNk+thUznBoY4nsYygyGtr0jl05rAVmAyP4QPnoKp0CfSRFGvScdhNCCjagv APcB9Jh8M0gFqDW/PGrSrkiYSt5HEGL+tSXKSpVqIbwzYnPYjI5cd/KobBTzlgELNSd7E o1X3lpmZW7A2GW6J5/Gb/9qmBM6mmYUY3QKzdpF0cwDqhqi6B/tVcrldw0By05nZre7Gc 6nnl7vaXbNTQEFLxdwMaMDrCZoq3k/Hhi5rFnKx5Onu4BMd3b4cUeYbBICmUWPtNueGHx vhGggsUalD3QBYnAlTVvlTWyDdiqAh6JUf4i5/e4qHbG78zIB2SJQiPGvkLYu8Sn7ZQ== 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=cR7QYVSm; arc=none smtp.remote-ip=209.85.221.72 Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.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 44FC5606A691 for ; Mon, 23 Nov 2026 17:10:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 44FC5606A691 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=cR7QYVSm Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-48e0389d6f9so2135461f8f.1 for ; Mon, 23 Nov 2026 09:10:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795453838; x=1796058638; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=t25R0OQppt5JZE3XIImm1+OnaJwjOmNRYl7971qMLRY=; b=cR7QYVSmMD9f1IHgsJFbaiVkhPax007Dt9WOGLEilYtcjrUZv8ZjxJW3Civv6fNeo5 PXvJBdDLlFg3NSTKAdT1NPWHzVWFgqWaWc4klg5w2fz5hphIC9D0JI49BPCn7ofRAuYq ZDWxoKOJFHdWO8nFwWSoKcJY+FP/eJUlpiMEUtVDHT6tvwKkB2YSKtOMK6rgMZEC3EAt BprLFWAASgR+wkKUa2hb3CQ/PUvcsLPv0KW90msap9wzdOYjyCTppMvwTj1+hoUs6OQj d3eJD1FOhvzjQ0bYNAJGPKYZbE8eDFV8NJeBEbNN1WbTjpAA73aS1ZWOUzS8J7mVf9tU xG2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795453838; x=1796058638; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=t25R0OQppt5JZE3XIImm1+OnaJwjOmNRYl7971qMLRY=; b=jwTdo5BPzlQ8669LquP2gGYVL+PvIhVvOmcNe3rIrdThM9SYCKtF4RbpukLGuHe0ak 1Kh0aZN58q1CuR9aFz2LNBeUBpVGmkMJR2QRl4hAlwFjzQRhe8Ll57bmKqVPYAxJ1bk+ GGaG9nyErS2lp0l4valKDS4R73fRtAcUyMm14/r8bvlNNIfM0CcwqT5HYFSrgUfZIK9J gCrFGIDdYnKTHwGaD2twUuJfXgtJ8ftuh2hPGtSG4BcI0Gd44Fv5pyANDrDjDFZTpiz/ T5lPIMj9t8T4QGSbw9JGboPTBhqWXpg8fY6j8Kaixif1a57NfT9AOUctaKLu7WvDjveD DQkg== X-Forwarded-Encrypted: i=1; AL+hErGqciWR0ythPPIdM648wrg9omhDWfOktMOh1pxnAEbTJbG0AmGHV5CeH8pEvxAqM6II3RS/nN1ylqH4A5Q=@vger.kernel.org X-Gm-Message-State: APuU4Z9bpRYrg6wrzHSzouG+rXMhVgDmno3afkF7eghdtgqizVvnqa67 0FA6xkTYefFbBmQ+yriXJPtaE4cOZ+UjuzX1ovCFdExdErivFwxE1ZdV47cy33grkJHzWLPZbpL f9F2Gnf6elaZbWQ== X-Received: from wmbh22.prod.google.com ([2002:a05:600c:a116:b0:4a2:677:24c]) (user=jpiecuch job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600d:6510:10b0:4a1:811c:8078 with SMTP id 5b1f17b1804b1-4bb8881de57mr168409645e9.26.1795453838453; Mon, 23 Nov 2026 09:10:38 -0800 (PST) Date: Mon, 23 Nov 2026 17:10:34 +0000 In-Reply-To: <20261123170846.3165648-1-jpiecuch@google.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 References: <20261123170846.3165648-1-jpiecuch@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261123170846.3165648-4-jpiecuch@google.com> Subject: [PATCH 3/3] selftests/sched_ext: Test that ops.dequeue() runs with the task's rq locked From: Kuba Piecuch To: Tejun Heo , David Vernet , Andrea Righi , Changwoo Min Cc: Kuba Piecuch , fangqiurong , sched-ext@lists.linux.dev, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" 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 --- .../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