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=1796535485; b=XIl4UfGiqutZSSPo+YjJF8IDmTSPWmftmqRU3i9a3uNRbtKYOK/0+HcClcbLVyLcx1dq g0URvZsN8LDv/Q0jiHhAJ+KYporKave2ttdQYmKalPEh7jx6IfkeAHySjPhiDDwGAvmD+ vSKHPn77LRznQTKGAmKwLeV3IJkCrQjd+6NOz392/LFjQhMSv0Eouamm/eBWpmhcjZ7Ct ym13wPzF9Ij1Y2z64NMq/8Pp/ujomdx9upQe89/wZjsTMZj9nyD5/HHuWfRvSKTFiNn+8 31gYasjnIaeOaGxPSISyjQujrUNVVpEd7XOIXAfO9fh93zMfdr3PkR/RE+uwjdKikAw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796535485; 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=JI07VIexsrG2vxDXMrNXehLmKS4S6n+eq/LGFHCX0Bs=; b=wPHQ3q8J6G92k+wHtYM/1zGYT7H6xbuTa9DCtGdtviX7tYKqobCWk6Hz1sRR8mvnTxgz HZ0O3EhpG8VeC6v1k+7NWvk7EWvCfrSPzrKset9iftJOL8Ga07SroLmQIPv6gDBZUcZGf My2EAAeG9+QOuKDH5lix0DMmBTexZIHdmTT9bGdjQsaVA7sfkChe2rceAvNdaEF87qrRV Iipsb1SrsM2B/3PUoRkRICNLkeLw4j4c0WPtbdiQF9dzPst3WJtWdBK9Ff7YJJQUmyFNo bhBU0973/9CdQi5jsBDhlTWcdqgC8Sh+oNG4Fale7NSHZbbb2TmF/r6jxUWfjuU45aw== 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="cgy/JDk9"; 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 C76BA606A31F for ; Sun, 6 Dec 2026 05:38:04 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C76BA606A31F 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=cgy/JDk9 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=JI07VIexsrG2vxDXMrNXehLmKS4S6n+eq/LGFHCX0Bs=; t=1796535484; x=2111895484; b=cgy/JDk9JFcy6UcrLKH7zWQpYj4nbauOkIJNoYKl+GYbwNf AGWA5sjS/TnCZ0erXJ60p6tcl49Jdy5/rHk/FXYyExPzgtx2t00kjQL9Daq8y003xWxRIIMobdb2h 5oL3qUjWPJFrleWyxpZu4V71pfhOlnHcMxrdMExlAvEaBv34LAfNgiUJnnYSdHZOdD8fldiFOWKxG qNn/x3OnhbSlQr9EbJt6LqwxOFcXbp6+Vh7p5dkQEyghfDEQ8m4shrIDhKPxBWmk+U0wDYveZRpXe 0G/ncGpzHAjBVTZrt3ljL5JisRNpgzSuR3X0B4qkpiYaZvJIIhP+tK5W0gfSzjTQ==; 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) (envelope-from ) id 1xa4wj-00000002dOO-0oGe; Sun, 06 Dec 2026 06:38:01 +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 v3 5/6] selftests/sched_ext: Test p->scx.bpf_priv across class changes Date: Sun, 6 Dec 2026 14:37:27 +0900 Message-ID: <20261206053728.2026272-6-changwoo@igalia.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261206053728.2026272-1-changwoo@igalia.com> References: <20261206053728.2026272-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 Under a partial-switch scheduler that sets p->scx.bpf_priv from ops.enable(), ops.disable() and ops.exit_task(), move the test task through SCHED_EXT, SCHED_OTHER and SCHED_EXT again, then unload the scheduler. After each step, a syscall program reads the value and sets its own with no lock held. The task stays on the scheduler across the class changes, so the value must survive them and the program's write must succeed. After the unload, the value must be cleared and the write must fail. The task also forks while on SCHED_EXT. The child must start with the value cleared. The child's ops.enable() runs in the parent under the child's rq lock, and its write to the parent must fail. Signed-off-by: Changwoo Min --- tools/testing/selftests/sched_ext/Makefile | 1 + .../selftests/sched_ext/task_bpf_priv.bpf.c | 129 ++++++++++++ .../selftests/sched_ext/task_bpf_priv.c | 195 ++++++++++++++++++ 3 files changed, 325 insertions(+) create mode 100644 tools/testing/selftests/sched_ext/task_bpf_priv.bpf.c create mode 100644 tools/testing/selftests/sched_ext/task_bpf_priv.c diff --git a/tools/testing/selftests/sched_ext/Makefile b/tools/testing/selftests/sched_ext/Makefile index ef59c463cd33..1ab274250403 100644 --- a/tools/testing/selftests/sched_ext/Makefile +++ b/tools/testing/selftests/sched_ext/Makefile @@ -201,6 +201,7 @@ auto-test-targets := \ sched_id \ sub_caps_kfunc \ sub_core_caps \ + task_bpf_priv \ test_example \ total_bw \ unlocked_kfunc \ diff --git a/tools/testing/selftests/sched_ext/task_bpf_priv.bpf.c b/tools/testing/selftests/sched_ext/task_bpf_priv.bpf.c new file mode 100644 index 000000000000..8dec22de5288 --- /dev/null +++ b/tools/testing/selftests/sched_ext/task_bpf_priv.bpf.c @@ -0,0 +1,129 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * A BPF program for testing p->scx.bpf_priv. + * + * Copyright (c) 2026 Meta Platforms, Inc. and affiliates. + * Author: Changwoo Min + */ +#include + +char _license[] SEC("license") = "GPL"; + +UEI_DEFINE(uei); /* Error handling */ + +/* + * The value every op stores. It doesn't carry the task's pid, which user space + * sees in its own PID namespace while p->pid is in the initial one. + */ +#define PRIV_MAGIC 0x5c7da7a05c7da7a0ULL + +/* Results of ops.enable() */ +u64 nr_enable, nr_enable_nonzero, nr_enable_set_fail; + +/* Sets on another task from ops.enable(), which must all fail */ +u64 nr_other_try, nr_other_set; + +/* The test task's tgid, set by set_target() */ +s32 target_tgid; + +/* Whether @p is the test task or its child */ +static bool is_target(struct task_struct *p) +{ + return p->tgid == target_tgid || p->real_parent->tgid == target_tgid; +} + +/* Results of ops.disable() and ops.exit_task() */ +u64 nr_disable, nr_disable_set_fail, nr_exit, nr_exit_zero, nr_exit_set_fail; + +/* The value probe() stores, set by user space */ +u64 probe_val; + +/* Results of the last probe() run, overwritten by every run */ +u64 probe_priv; +bool probe_set_ret; + +void BPF_STRUCT_OPS(task_bpf_priv_enable, struct task_struct *p) +{ + struct task_struct *cur = bpf_get_current_task_btf(); + + if (!is_target(p)) + return; + + __sync_fetch_and_add(&nr_enable, 1); + if (p->scx.bpf_priv) + __sync_fetch_and_add(&nr_enable_nonzero, 1); + if (!scx_bpf_task_set_priv(p, PRIV_MAGIC)) + __sync_fetch_and_add(&nr_enable_set_fail, 1); + + /* + * On the target's fork, @p is the child and @cur the parent. Setting + * @cur must fail, as this op holds @p's rq lock, not @cur's. + */ + if (cur != p && is_target(cur)) { + __sync_fetch_and_add(&nr_other_try, 1); + if (scx_bpf_task_set_priv(cur, PRIV_MAGIC)) + __sync_fetch_and_add(&nr_other_set, 1); + } +} + +void BPF_STRUCT_OPS(task_bpf_priv_disable, struct task_struct *p) +{ + if (!is_target(p)) + return; + + __sync_fetch_and_add(&nr_disable, 1); + if (!scx_bpf_task_set_priv(p, PRIV_MAGIC)) + __sync_fetch_and_add(&nr_disable_set_fail, 1); +} + +void BPF_STRUCT_OPS(task_bpf_priv_exit_task, struct task_struct *p, + struct scx_exit_task_args *args) +{ + if (!is_target(p)) + return; + + /* @p is still on this scheduler, so the value is still there */ + __sync_fetch_and_add(&nr_exit, 1); + if (!p->scx.bpf_priv) + __sync_fetch_and_add(&nr_exit_zero, 1); + if (!scx_bpf_task_set_priv(p, PRIV_MAGIC)) + __sync_fetch_and_add(&nr_exit_set_fail, 1); +} + +void BPF_STRUCT_OPS(task_bpf_priv_exit, struct scx_exit_info *ei) +{ + UEI_RECORD(uei, ei); +} + +/* Record the calling task, the test task, as the target */ +SEC("syscall") +int set_target(void *ctx) +{ + target_tgid = bpf_get_current_task_btf()->tgid; + return 0; +} + +/* + * Read the calling task's value, then set it to probe_val with no lock held. + * Only BPF_PROG_TEST_RUN from the test runs this program, synchronously in the + * test task, so the current task is the task under test. + */ +SEC("syscall") +int probe(void *ctx) +{ + struct task_struct *p = bpf_get_current_task_btf(); + + probe_priv = p->scx.bpf_priv; + probe_set_ret = scx_bpf_task_set_priv(p, probe_val); + return 0; +} + +SEC(".struct_ops.link") +struct sched_ext_ops task_bpf_priv_ops = { + .enable = (void *)task_bpf_priv_enable, + .disable = (void *)task_bpf_priv_disable, + .exit_task = (void *)task_bpf_priv_exit_task, + .exit = (void *)task_bpf_priv_exit, + .flags = SCX_OPS_SWITCH_PARTIAL, + .name = "task_bpf_priv", +}; diff --git a/tools/testing/selftests/sched_ext/task_bpf_priv.c b/tools/testing/selftests/sched_ext/task_bpf_priv.c new file mode 100644 index 000000000000..bfdeab327375 --- /dev/null +++ b/tools/testing/selftests/sched_ext/task_bpf_priv.c @@ -0,0 +1,195 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Test for p->scx.bpf_priv across class changes, fork and unload. + * + * Copyright (c) 2026 Meta Platforms, Inc. and affiliates. + * Author: Changwoo Min + */ +#include +#include +#include +#include +#include +#include "task_bpf_priv.bpf.skel.h" +#include "scx_test.h" + +#define SCHED_EXT 7 +/* Must match task_bpf_priv.bpf.c */ +#define PRIV_MAGIC 0x5c7da7a05c7da7a0ULL +#define PROBE_VAL 0x9a0be9a0be9a0be0ULL + +static int set_policy(int policy) +{ + struct sched_param param = { .sched_priority = 0 }; + + return sched_setscheduler(0, policy, ¶m); +} + +/* Run @prog on this task and return its result */ +static int run_prog(struct bpf_program *prog) +{ + LIBBPF_OPTS(bpf_test_run_opts, topts); + + return bpf_prog_test_run_opts(bpf_program__fd(prog), &topts) ?: + topts.retval; +} + +/* Read this task's value, then set it to PROBE_VAL with no lock held */ +static int probe(struct task_bpf_priv *skel) +{ + skel->bss->probe_val = PROBE_VAL; + return run_prog(skel->progs.probe); +} + +static enum scx_test_status steps(struct task_bpf_priv *skel, + struct bpf_link **link) +{ + int status; + pid_t pid; + + /* + * ops.enable(), ops.disable() and ops.exit_task() store PRIV_MAGIC in + * this task's value. Each probe() reads the value and then replaces it + * with PROBE_VAL. So a read of PRIV_MAGIC shows one of those ops stored + * it since the last probe(), and a read of PROBE_VAL shows none did. + */ + + /* Step 1: Switch this task to SCHED_EXT */ + SCX_FAIL_IF(set_policy(SCHED_EXT), "Failed to switch to SCHED_EXT"); + + /* + * %SCX_OPS_SWITCH_PARTIAL takes only SCHED_EXT tasks. Switching makes + * ops.enable() set the value. probe() must read it back and replace it. + */ + SCX_FAIL_IF(probe(skel), "Failed to run probe()"); + SCX_EQ(skel->bss->probe_priv, PRIV_MAGIC); + SCX_EQ(skel->bss->probe_set_ret, true); + + /* Step 2: Fork a child on SCHED_EXT */ + pid = fork(); + SCX_FAIL_IF(pid < 0, "Failed to fork"); + if (pid == 0) + _exit(0); + SCX_FAIL_IF(waitpid(pid, &status, 0) != pid, + "Failed to wait for the child"); + SCX_FAIL_IF(!WIFEXITED(status) || WEXITSTATUS(status), + "The child failed"); + + /* + * The child must not inherit the value, which ops.enable() checks. The + * child's ops.enable() can't set this task's value either, as it isn't + * called for this task, so probe() must read its own earlier store. + */ + SCX_FAIL_IF(probe(skel), "Failed to run probe()"); + SCX_EQ(skel->bss->probe_priv, PROBE_VAL); + SCX_EQ(skel->bss->probe_set_ret, true); + + /* Step 3: Switch this task to SCHED_OTHER */ + SCX_FAIL_IF(set_policy(SCHED_OTHER), "Failed to switch to SCHED_OTHER"); + + /* + * %SCX_OPS_SWITCH_PARTIAL leaves SCHED_OTHER tasks on the fair class. + * The task stays on the scheduler, so the value ops.disable() set + * survives, and probe() can still set it. + */ + SCX_FAIL_IF(probe(skel), "Failed to run probe()"); + SCX_EQ(skel->bss->probe_priv, PRIV_MAGIC); + SCX_EQ(skel->bss->probe_set_ret, true); + + /* + * Step 4: Switch this task back to SCHED_EXT. ops.enable() finds the + * value probe() set and sets its own. + */ + SCX_FAIL_IF(set_policy(SCHED_EXT), "Failed to switch to SCHED_EXT"); + SCX_FAIL_IF(probe(skel), "Failed to run probe()"); + SCX_EQ(skel->bss->probe_priv, PRIV_MAGIC); + SCX_EQ(skel->bss->probe_set_ret, true); + + /* Step 5: Unload the scheduler */ + SCX_ASSERT_ALIVE(skel, uei); + bpf_link__destroy(*link); + *link = NULL; + + /* + * The scheduler must exit by unregistration, not by an error. The task + * left the scheduler, so the kernel cleared the value after + * ops.exit_task(), and probe() must read 0 and fail to set it. + */ + SCX_EQ(skel->data->uei.kind, EXIT_KIND(SCX_EXIT_UNREG)); + SCX_FAIL_IF(probe(skel), "Failed to run probe()"); + SCX_EQ(skel->bss->probe_priv, 0); + SCX_EQ(skel->bss->probe_set_ret, false); + + /* + * ops.enable() ran twice for this task and once for the child. Only the + * one in step 4 found a value, the one probe() left. + */ + SCX_EQ(skel->bss->nr_enable, 3); + SCX_EQ(skel->bss->nr_enable_nonzero, 1); + SCX_EQ(skel->bss->nr_enable_set_fail, 0); + SCX_EQ(skel->bss->nr_other_try, 1); + SCX_EQ(skel->bss->nr_other_set, 0); + + /* + * ops.disable() ran for this task in steps 3 and 5 and once for the + * child, and ops.exit_task() ran for this task in step 5 and for the + * child. The task was on the scheduler in all of them, so each found + * the value and could set it. + */ + SCX_EQ(skel->bss->nr_disable, 3); + SCX_EQ(skel->bss->nr_disable_set_fail, 0); + SCX_EQ(skel->bss->nr_exit, 2); + SCX_EQ(skel->bss->nr_exit_zero, 0); + SCX_EQ(skel->bss->nr_exit_set_fail, 0); + + return SCX_TEST_PASS; +} + +static enum scx_test_status run(void *ctx) +{ + enum scx_test_status status = SCX_TEST_FAIL; + struct task_bpf_priv *skel; + struct bpf_link *link = NULL; + + skel = task_bpf_priv__open(); + SCX_FAIL_IF(!skel, "Failed to open skel"); + SCX_ENUM_INIT(skel); + if (task_bpf_priv__load(skel)) { + SCX_ERR("Failed to load skel"); + goto out; + } + if (bpf_program__assoc_struct_ops(skel->progs.probe, + skel->maps.task_bpf_priv_ops, NULL)) { + SCX_ERR("Failed to associate probe with task_bpf_priv_ops"); + goto out; + } + + /* Before the attach, so that no op sees target_tgid unset */ + if (run_prog(skel->progs.set_target)) { + SCX_ERR("Failed to set the target"); + goto out; + } + + link = bpf_map__attach_struct_ops(skel->maps.task_bpf_priv_ops); + if (!link) { + SCX_ERR("Failed to attach struct_ops"); + goto out; + } + + status = steps(skel, &link); + +out: + /* Restore the policy even if a step failed */ + set_policy(SCHED_OTHER); + bpf_link__destroy(link); + task_bpf_priv__destroy(skel); + + return status; +} + +struct scx_test task_bpf_priv = { + .name = "task_bpf_priv", + .description = "Verify p->scx.bpf_priv across class changes", + .run = run, +}; +REGISTER_SCX_TEST(&task_bpf_priv) -- 2.56.1