Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=213.97.179.56 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796104151; b=Hny7M8vW6pJqSVd3UUAAA/1My71OuxQdkSWwb24P2Mt0HXOAdfTgAgqOJq6QrIyxbhI0 dTam2vXZ0SGgw+MyrMaeDmP+tBTSPnQCM8eMysFPxPJhft8AwO8g8UFxcCgsKLumhj4aG Zx3KINw/kIK8lV6HJredabVH8p/fq0pjKfqR67VnosRjM/zqVjjOAgBH+Yq9Tl1NBg2Ve GG+FEn6V8dCaV/JwTlJa9HHZNTyFiAaDMunblrnxc0slSNg97CaXwdRnHcfcmZ8rkHFRi xMrnzBu4R3isMBTZsfL+JL4uR+fJT4hWNl6apzbtXEuA6ZHKBWEsHBW1ayBGefyONCg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796104151; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=Nk2AhsmC4vjZpnJ2Yd+yWnjUCUu0oRNrm3P9EIadjdk=; b=Sn/HaXWKIP38lfTURCMP+GlclOcKC3BZZ2bOLSc1gG7Z5uQNCvi3uQ4gOzewWSkTHLEM ar4tV/3HvZ8nM8ej/PuwTN75lu0iG7geLKT18rYBgoF7sqKt5/8yAoBLNtVIU9ZBD1Zan RcL8QQqn8AIj4ua47tRK4H1BEZSkdQ/Cia9CdI9tKeJkFDYcx805ZjF6GcUtgBcGnPVCy F2vNoIhadZsZRl6868/ZFila48GvKQU2K4CDDhrMAYYims04uWmKSGe/MU7ee9ix+F5ZP IEVKxGHkSTusMUiZYiZBgCbZVbNrp9DQaGoXKkei+AzhVyG0Zq/J/uGJKjrDhrcJKVw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=igalia.com; spf=pass smtp.mailfrom=igalia.com; dkim=pass header.d=igalia.com header.i=@igalia.com header.a=rsa-sha256 header.s=20170329 header.b=Cqm7Sh1P; arc=none smtp.remote-ip=213.97.179.56 Received: from fanzine2.igalia.com (fanzine2.igalia.com [213.97.179.56]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (prime256v1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B715F606A31A for ; Tue, 1 Dec 2026 05:49:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B715F606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=igalia.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=igalia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=igalia.com header.i=@igalia.com header.a=rsa-sha256 header.s=20170329 header.b=Cqm7Sh1P DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:MIME-Version:Message-ID:Date:Subject: Cc:To:From:From:Reply-To; bh=Nk2AhsmC4vjZpnJ2Yd+yWnjUCUu0oRNrm3P9EIadjdk=; t=1796104150; x=2111464150; b=Cqm7Sh1PkPBEYwXhFgAgrf9OBUa6o0Pwa1Q2BKLoZe/biE5 RDU/qBIFJ7J1UjqkjZPahPN6Wc5saJjYCNlGG/Y/P54dGwRPW1GoFrKCdZEaeduVzBrVcjPnPRdX5 fW1Vk7fE1spr8QKKNT9l1l6XJZxOEdZ7pxRt/6aN36HzbgQM4FMl5+QnmkpDPf8oVB2wM1ycJ18Aq u0pGrpGqBe1mqWMiulN8RfOsLziNpfgNjWFr+f6rEOO8ZMqW6TYA4zOMJEQGBpYrADOiXoJ2U5iaN Ey7j5cfE40YMsqIbUQ6VIM6ZtJxsSlc33FrelIl3rtJJe8/hI02gSYbBT+H8F9rw==; Received: from [58.29.145.179] (helo=localhost) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1xYGjj-00000000Ufv-2J7E; Tue, 01 Dec 2026 06:49:08 +0100 From: Changwoo Min To: tj@kernel.org, void@manifault.com, arighi@nvidia.com, changwoo@igalia.com Cc: david.dai@linux.dev, Gavin Guo , kernel-dev@igalia.com, sched-ext@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH 2/5] selftests/sched_ext: Test scx_bpf_sched_id() and scx_bpf_task_sched_id() Date: Tue, 1 Dec 2026 14:48:47 +0900 Message-ID: <20261201054850.780664-3-changwoo@igalia.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261201054850.780664-1-changwoo@igalia.com> References: <20261201054850.780664-1-changwoo@igalia.com> X-Mailing-List: sched-ext@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Load a partial-switch scheduler and move the test task onto it. The task's owner id must equal the scheduler's id and be nonzero, and both must read 0 once the scheduler is unloaded. A second instance must get a larger id. Signed-off-by: Changwoo Min --- tools/testing/selftests/sched_ext/Makefile | 1 + .../selftests/sched_ext/sched_id.bpf.c | 47 ++++++++ tools/testing/selftests/sched_ext/sched_id.c | 103 ++++++++++++++++++ 3 files changed, 151 insertions(+) create mode 100644 tools/testing/selftests/sched_ext/sched_id.bpf.c create mode 100644 tools/testing/selftests/sched_ext/sched_id.c diff --git a/tools/testing/selftests/sched_ext/Makefile b/tools/testing/selftests/sched_ext/Makefile index fe63dbd53f56..ef59c463cd33 100644 --- a/tools/testing/selftests/sched_ext/Makefile +++ b/tools/testing/selftests/sched_ext/Makefile @@ -198,6 +198,7 @@ auto-test-targets := \ select_cpu_dispatch_dbl_dsp \ select_cpu_vtime \ rt_stall \ + sched_id \ sub_caps_kfunc \ sub_core_caps \ test_example \ diff --git a/tools/testing/selftests/sched_ext/sched_id.bpf.c b/tools/testing/selftests/sched_ext/sched_id.bpf.c new file mode 100644 index 000000000000..8828c3ecec3e --- /dev/null +++ b/tools/testing/selftests/sched_ext/sched_id.bpf.c @@ -0,0 +1,47 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * A BPF program for testing scx_bpf_sched_id() and scx_bpf_task_sched_id(). + * + * Copyright (c) 2026 Meta Platforms, Inc. and affiliates. + * Author: Changwoo Min + */ +#include + +char _license[] SEC("license") = "GPL"; + +UEI_DEFINE(uei); /* Error handling */ + +/* The task to probe, set by user space */ +s32 target_pid; + +/* Results of the last probe() run, overwritten by every run */ +u64 probe_sched_id; +u64 probe_task_sched_id; + +void BPF_STRUCT_OPS(sched_id_exit, struct scx_exit_info *ei) +{ + UEI_RECORD(uei, ei); +} + +/* Read this scheduler's id and the id of the target task's scheduler */ +SEC("syscall") +int probe(void *ctx) +{ + struct task_struct *p; + + p = bpf_task_from_pid(target_pid); + if (!p) + return -ESRCH; + + probe_sched_id = scx_bpf_sched_id(); + probe_task_sched_id = scx_bpf_task_sched_id(p); + bpf_task_release(p); + return 0; +} + +SEC(".struct_ops.link") +struct sched_ext_ops sched_id_ops = { + .exit = (void *)sched_id_exit, + .flags = SCX_OPS_SWITCH_PARTIAL, + .name = "sched_id", +}; diff --git a/tools/testing/selftests/sched_ext/sched_id.c b/tools/testing/selftests/sched_ext/sched_id.c new file mode 100644 index 000000000000..a926062e13c7 --- /dev/null +++ b/tools/testing/selftests/sched_ext/sched_id.c @@ -0,0 +1,103 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Test for scx_bpf_sched_id() and scx_bpf_task_sched_id(). + * + * Copyright (c) 2026 Meta Platforms, Inc. and affiliates. + * Author: Changwoo Min + */ +#include +#include +#include +#include +#include "sched_id.bpf.skel.h" +#include "scx_test.h" + +#define SCHED_EXT 7 + +static int set_policy(int policy) +{ + struct sched_param param = {}; + + return sched_setscheduler(0, policy, ¶m); +} + +/* Run probe() on this task and return its result */ +static int probe(struct sched_id *skel) +{ + LIBBPF_OPTS(bpf_test_run_opts, topts); + + return bpf_prog_test_run_opts(bpf_program__fd(skel->progs.probe), + &topts) ?: topts.retval; +} + +/* + * Load a scheduler, move this task onto it and return the scheduler's id, after + * checking that the task reports the same owner and that both read 0 once the + * scheduler is unloaded. + */ +static enum scx_test_status load_and_check(u64 *id) +{ + enum scx_test_status status = SCX_TEST_FAIL; + struct sched_id *skel; + struct bpf_link *link; + + skel = sched_id__open(); + SCX_FAIL_IF(!skel, "Failed to open skel"); + SCX_ENUM_INIT(skel); + SCX_FAIL_IF(sched_id__load(skel), "Failed to load skel"); + skel->bss->target_pid = getpid(); + + link = bpf_map__attach_struct_ops(skel->maps.sched_id_ops); + SCX_FAIL_IF(!link, "Failed to attach struct_ops"); + + /* Step 1: The task on SCHED_EXT reports the scheduler as its owner */ + if (set_policy(SCHED_EXT) || probe(skel)) { + SCX_ERR("Failed to switch to SCHED_EXT and probe"); + goto out; + } + *id = skel->bss->probe_sched_id; + if (!*id || skel->bss->probe_task_sched_id != *id) { + SCX_ERR("sched_id=%llu task_sched_id=%llu", + (unsigned long long)*id, + (unsigned long long)skel->bss->probe_task_sched_id); + goto out; + } + + /* Step 2: Both read 0 after the scheduler is unloaded */ + bpf_link__destroy(link); + link = NULL; + if (probe(skel) || skel->bss->probe_sched_id || + skel->bss->probe_task_sched_id) { + SCX_ERR("After unload: sched_id=%llu task_sched_id=%llu", + (unsigned long long)skel->bss->probe_sched_id, + (unsigned long long)skel->bss->probe_task_sched_id); + goto out; + } + + status = SCX_TEST_PASS; +out: + set_policy(SCHED_OTHER); + bpf_link__destroy(link); + sched_id__destroy(skel); + return status; +} + +static enum scx_test_status run(void *ctx) +{ + u64 id1 = 0, id2 = 0; + + SCX_EQ(load_and_check(&id1), SCX_TEST_PASS); + + /* Step 3: A new instance gets a larger id */ + SCX_EQ(load_and_check(&id2), SCX_TEST_PASS); + SCX_GT(id2, id1); + + return SCX_TEST_PASS; +} + +struct scx_test sched_id = { + .name = "sched_id", + .description = "Verify scx_bpf_sched_id() and scx_bpf_task_sched_id()", + .run = run, +}; +REGISTER_SCX_TEST(&sched_id) -- 2.56.1