Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.70 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795606125; b=Ep/7hkPhk3a8xa3gYf2MVlGRJcTQIDA7DKLUB5MsaND/dyTjGmiBzMo/h69bzfmC0ryp RWqiSbUWMSO9ZeZGZ9b7fk1gtnXKl90/JlW+SQctmLQGZQJumHArsr61QA5X0BCQGUjOf dLHIkd/wsfuLNiS7BfK6+jO+0V83hYF4b2eiN8PeRILueN6JjnmDcwVybAnEA3W0SYJZy aE0G0ETsHfRKjTt4EhnPS7ABxRX6hkVLhrweI4ZChIMhLLv0X/wCWYGcoDmYdk25SvT7v uY6DWmDmI7ppAF4gsgQNVxmabNO54GMYrRRXpAP5NDMedepUbTbT+R6vzK4lYa/bduA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795606125; 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=PH9AUh7w6ThZJsKaitS9ec4EBwWzCSLKSPBtEPu/DFo=; b=mbGAKKj/Br/E3BjJ5fMVbxCTwkdBJOdq1cm7MLie3f070Ft5sVdiWjXlp1g2pZNm13sg 8Yv50DFnLrKBfZadQWhlWjixT68NCvFvMdwhkuHeaSNvcTsdKRcMSdQtkS4x7DbY0ikUL yeUmx4DxDsBqpnJFyeHfV5pE/XmTAOU27BiQdfJSv/yTdZO6r0qAB8SdMRSAgPAPTZlXF 2k3a5SKMSj3E7VDacZhu+GTOWv0IDzHnpWUMjAo5LLMpXE8z+j3Ubvb0ByecVlstrQjf4 QyNKUa5akukLYwl4LC5GTRCcsJVtkm2Bj47cYPqWizgK3fxTEQoW+LOYjQwm2DxsEOw== 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=d29oQKxe; arc=none smtp.remote-ip=209.85.128.70 Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) (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 1C651606A32E for ; Wed, 25 Nov 2026 11:28:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 1C651606A32E 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=d29oQKxe Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-4b9fde3044dso40281385e9.0 for ; Wed, 25 Nov 2026 03:28:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795606123; x=1796210923; 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=PH9AUh7w6ThZJsKaitS9ec4EBwWzCSLKSPBtEPu/DFo=; b=d29oQKxeZiG4AX7c7fGcR4iAdfPsMF+MFshlwSb7h2Su1ChS63xp7DKPAZKkQZQbbd Y0JHIKyYgN2YA38Rzm86SzBWCE0n5rVLxdb9enVjJRDJNv1I9MRHjnst7RnUSwvFVfSp 3eyxu1se0WOM6ttl0M8V3FmstpCHx3bj+WSi6qneTEqRAhQ6y3EEvPbaN3grPxIK2im4 PzSIK7ud/KL/7PErySxaUF5QJEfXjJcYbnJkrbziVkXCCAMv1smRdyiKl2lF/Bh7zaaV sMTc+vmo4igmHHhcBLEioP8gsLfKLBikt8zU5GI1+K9+E/TXgR3pLu5tODBx2A10ATUv 8/Lw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795606123; x=1796210923; 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=PH9AUh7w6ThZJsKaitS9ec4EBwWzCSLKSPBtEPu/DFo=; b=Q6bLpBgU/sD02aME3uxgy3KFJxpzJErJjeKzXl5wbeo5RVSVPxY2vmHYbqJf+RWQht nTtin7oxYGE1NN4XOHDv1kJa0IaCa38a9JPPHbDmx0fCTCMwpF+G0mtmtPSap9bDC8QS /iUG44jq+YxyAlxkCKlhjfGVM0vFVMKYE4wOm7n+Ow9rdaI+y8vb9B833U3RjHM9wq7+ e6OC9uzwVhl58hCFTYSBSo48Q7TvpEnAdT73pnWzfFGO3P6I90577UinkzC1gr67SKzD oPpeLPZ6wWUGFGk+HxSm759LXQiXd2Mtn2JwwbHqsGD3xTUKM8hysFMLD9OfvXRBYJv4 ebfA== X-Forwarded-Encrypted: i=1; AL+hErHuS7H86pHmlUOYHh+6/cVBydkQu0Qloz5fK/IPtAnLSRkNqFMbcwCis9YZ5ovKD1ytTwi3eqag3BGBQjY=@vger.kernel.org X-Gm-Message-State: APuU4Z9QIRbLRsuUJDAs2oRh/Qt+0TFeQjtVjFTPvT48QVMXZokwpv5c jqm1bjPG8i9xoy63L9mmHL3vtC0Sj/qUbGVOwO8MJLokCgwkO9qeemgtNC0jzrtw+OwOv0N31Bm 3UQWgUlD4WbxbsQ== X-Received: from wmbz19.prod.google.com ([2002:a05:600c:c093:b0:4a2:8b4:78e1]) (user=jpiecuch job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:6092:b0:4a1:d537:1c8d with SMTP id 5b1f17b1804b1-4bb8892bb67mr329998235e9.19.1795606123419; Wed, 25 Nov 2026 03:28:43 -0800 (PST) Date: Wed, 25 Nov 2026 11:28:38 +0000 In-Reply-To: <20261125112553.1539758-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: <20261125112553.1539758-1-jpiecuch@google.com> X-Mailer: git-send-email 2.56.1.839.g22a8290db9-goog Message-ID: <20261125112553.1539758-5-jpiecuch@google.com> Subject: [PATCH v2 4/4] 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.839.g22a8290db9-goog