[PATCH v3 5/6] selftests/sched_ext: Test p->scx.bpf_priv across class changes

From: Changwoo Min

Date: Sun Dec 06 2026 - 00:38:08 EST


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 <changwoo@xxxxxxxxxx>
---
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 <changwoo@xxxxxxxxxx>
+ */
+#include <scx/common.bpf.h>
+
+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 <changwoo@xxxxxxxxxx>
+ */
+#include <bpf/bpf.h>
+#include <sched.h>
+#include <scx/common.h>
+#include <sys/wait.h>
+#include <unistd.h>
+#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, &param);
+}
+
+/* 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