From mboxrd@z Thu Jan 1 00:00:00 1970 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=1796104152; b=L5G1A8MpF/hYV4Q6/tKSohCtbAFYdSQ53vU9ydpM9mk7RXcD6jlyl6jF/tBgNuvW5mgf IzJvV3NBShENGTqXfcCv5qxXVI+9hymhtmUmjmiEffZFsgQRMLC6pVTEVG/TxlA3yzzgq KmOFlkAf8fmiLn1OEUpfhtvRGdQUHj2Qpfaeyi/kBlzxBpoXIxHDOk3D5lVmcKNPKRye6 VFUHe/eatfFB+gcwjet0dkUcgmLimLu2y9r+nLncztwbrL1F+rXmNureffFKn9CmoCKxS Xd7JJNkwSJcwk5dVgIXs4HA/VRsbk52NuyetMVDf2mx4EALzXcUy3dVINEQNlAHDzBg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796104152; 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=mE175rgNtjH8c+I5IDACz6xVoxwlDCx489uDFt7SEqFp0Q1A1/0htou07z0Qrt3BpUVx hWfF+Z7JQ5zJDztW0Pk6dytQMS/ktcn60T0jnprUPd3tGXCDkbGmfEurCB9IeWaATSlPT pn9gMFk6rlhf3hH0Yh3vKuelxLXueJpK8nz2QoMt9AMTf7PsCgWais29ITBsmLwKgAHUC l/7swZrzkBz1UeXJwcEoWFHGSldCxVdNyCFbw4XCip/c2mSKaHmxR1jsyox8RC+CSMpO1 3rvLnlmnoStUjZITuumaOSyH+XxEVDtpYh9XhEKr8iWuW62a3IHuh7bsgulatl7OChg== 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=oubFxhgr; 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 649B7606A31A for ; Tue, 1 Dec 2026 05:49:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 649B7606A31A 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=oubFxhgr 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=1796104152; x=2111464152; b=oubFxhgrNmv/PZ6DkBQY1tFniW68nDEnO/miY+MbXTFSVMF Z2Fo+4wK/5lckKf5qGlZbKME4h7sLZcm3opnvOi/OzrK6bZ7Hj9/qcku703ykJdvm9WUa88g/3LKF PJryu9pkjRDBPI5eOVAVbEVfGWTYDT7GDnPn+Nu4rkAK4ipf1p8l62xelcJrD3anb6zT8AAq+ahWx OrxnHj8WBVgP3O3J++DPZLyM0uTQ7eGUI51PS+V//bM+2Mj+5SMJwmCvAi6poMTDGHrzuSmbPqnKU sbt+4yLiS5RBDi+qHHNqM6c75J+UTVyJxsAPp73ewS88MX1DSyySKAlqQ3wQawNg==; 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: linux-kernel@vger.kernel.org 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