From: Changwoo Min <changwoo@igalia.com>
To: tj@kernel.org, void@manifault.com, arighi@nvidia.com,
changwoo@igalia.com
Cc: david.dai@linux.dev, Gavin Guo <gavinguo@igalia.com>,
kernel-dev@igalia.com, sched-ext@lists.linux.dev,
linux-kernel@vger.kernel.org
Subject: [PATCH 5/5] selftests/sched_ext: Test p->scx.bpf_data across class changes
Date: Tue, 1 Dec 2026 14:48:50 +0900 [thread overview]
Message-ID: <20261201054850.780664-6-changwoo@igalia.com> (raw)
In-Reply-To: <20261201054850.780664-1-changwoo@igalia.com>
Under a partial-switch scheduler that sets p->scx.bpf_data from
ops.enable(), move the test task through SCHED_EXT, SCHED_FIFO and
SCHED_EXT again, then unload the scheduler. After each step, a syscall
program reads the value and sets it again without the rq lock. The value
must be set while the task is on SCHED_EXT. Otherwise it must be
cleared, and the write must fail.
The task also forks while on SCHED_EXT. ops.enable() must find the value
cleared for every task it enables, the child included.
Signed-off-by: Changwoo Min <changwoo@igalia.com>
---
tools/testing/selftests/sched_ext/Makefile | 1 +
.../selftests/sched_ext/task_bpf_data.bpf.c | 64 +++++++++
.../selftests/sched_ext/task_bpf_data.c | 123 ++++++++++++++++++
3 files changed, 188 insertions(+)
create mode 100644 tools/testing/selftests/sched_ext/task_bpf_data.bpf.c
create mode 100644 tools/testing/selftests/sched_ext/task_bpf_data.c
diff --git a/tools/testing/selftests/sched_ext/Makefile b/tools/testing/selftests/sched_ext/Makefile
index ef59c463cd33..c0a3046a36ab 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_data \
test_example \
total_bw \
unlocked_kfunc \
diff --git a/tools/testing/selftests/sched_ext/task_bpf_data.bpf.c b/tools/testing/selftests/sched_ext/task_bpf_data.bpf.c
new file mode 100644
index 000000000000..f7044f0030ab
--- /dev/null
+++ b/tools/testing/selftests/sched_ext/task_bpf_data.bpf.c
@@ -0,0 +1,64 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * A BPF program for testing p->scx.bpf_data.
+ *
+ * Copyright (c) 2026 Meta Platforms, Inc. and affiliates.
+ * Author: Changwoo Min <changwoo@igalia.com>
+ */
+#include <scx/common.bpf.h>
+
+char _license[] SEC("license") = "GPL";
+
+UEI_DEFINE(uei); /* Error handling */
+
+#define DATA_MAGIC 0x5c7da7a000000000ULL
+
+/* The task to probe, set by user space */
+s32 target_pid;
+
+/* Results of ops.enable() */
+u64 nr_enable, nr_enable_nonzero, nr_enable_set_fail;
+
+/* Results of the last probe() run, overwritten by every run */
+u64 probe_data;
+bool probe_set_ret;
+
+void BPF_STRUCT_OPS(task_bpf_data_enable, struct task_struct *p)
+{
+ /* The value must be cleared whenever a task comes in, forked or not */
+ __sync_fetch_and_add(&nr_enable, 1);
+ if (p->scx.bpf_data)
+ __sync_fetch_and_add(&nr_enable_nonzero, 1);
+
+ if (!scx_bpf_task_set_data(p, DATA_MAGIC | (u32)p->pid))
+ __sync_fetch_and_add(&nr_enable_set_fail, 1);
+}
+
+void BPF_STRUCT_OPS(task_bpf_data_exit, struct scx_exit_info *ei)
+{
+ UEI_RECORD(uei, ei);
+}
+
+/* Read the value, then set it again without the rq lock */
+SEC("syscall")
+int probe(void *ctx)
+{
+ struct task_struct *p;
+
+ p = bpf_task_from_pid(target_pid);
+ if (!p)
+ return -ESRCH;
+
+ probe_data = p->scx.bpf_data;
+ probe_set_ret = scx_bpf_task_set_data(p, DATA_MAGIC | (u32)p->pid);
+ bpf_task_release(p);
+ return 0;
+}
+
+SEC(".struct_ops.link")
+struct sched_ext_ops task_bpf_data_ops = {
+ .enable = (void *)task_bpf_data_enable,
+ .exit = (void *)task_bpf_data_exit,
+ .flags = SCX_OPS_SWITCH_PARTIAL,
+ .name = "task_bpf_data",
+};
diff --git a/tools/testing/selftests/sched_ext/task_bpf_data.c b/tools/testing/selftests/sched_ext/task_bpf_data.c
new file mode 100644
index 000000000000..29c6e803283e
--- /dev/null
+++ b/tools/testing/selftests/sched_ext/task_bpf_data.c
@@ -0,0 +1,123 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Test for p->scx.bpf_data across class changes, fork and unload.
+ *
+ * Copyright (c) 2026 Meta Platforms, Inc. and affiliates.
+ * Author: Changwoo Min <changwoo@igalia.com>
+ */
+#include <bpf/bpf.h>
+#include <sched.h>
+#include <scx/common.h>
+#include <sys/wait.h>
+#include <unistd.h>
+#include "task_bpf_data.bpf.skel.h"
+#include "scx_test.h"
+
+#define SCHED_EXT 7
+/* Must match task_bpf_data.bpf.c */
+#define DATA_MAGIC 0x5c7da7a000000000ULL
+
+static int set_policy(int policy, int prio)
+{
+ struct sched_param param = { .sched_priority = prio };
+
+ return sched_setscheduler(0, policy, ¶m);
+}
+
+/* Run probe() on this task and return its result */
+static int probe(struct task_bpf_data *skel)
+{
+ LIBBPF_OPTS(bpf_test_run_opts, topts);
+
+ return bpf_prog_test_run_opts(bpf_program__fd(skel->progs.probe),
+ &topts) ?: topts.retval;
+}
+
+static enum scx_test_status steps(struct task_bpf_data *skel,
+ struct bpf_link **link)
+{
+ u64 data = DATA_MAGIC | (u32)getpid();
+ int status;
+ pid_t pid;
+
+ skel->bss->target_pid = getpid();
+
+ /* Step 1: ops.enable() sets the value on SCHED_EXT */
+ SCX_EQ(set_policy(SCHED_EXT, 0), 0);
+ SCX_EQ(probe(skel), 0);
+ SCX_EQ(skel->bss->probe_data, data);
+ SCX_EQ(skel->bss->probe_set_ret, true);
+
+ /* Step 2: A child forked on SCHED_EXT doesn't inherit the value */
+ pid = fork();
+ SCX_FAIL_IF(pid < 0, "Failed to fork");
+ if (pid == 0)
+ _exit(0);
+ SCX_EQ(waitpid(pid, &status, 0), pid);
+ SCX_EQ(probe(skel), 0);
+ SCX_EQ(skel->bss->probe_data, data);
+ SCX_EQ(skel->bss->probe_set_ret, true);
+
+ /*
+ * Step 3: Switching to SCHED_FIFO disables the task, and the kernel
+ * clears the value. probe() then reads 0 into probe_data, and its set
+ * fails because the task is no longer enabled.
+ */
+ SCX_EQ(set_policy(SCHED_FIFO, 1), 0);
+ SCX_EQ(probe(skel), 0);
+ SCX_EQ(skel->bss->probe_data, 0);
+ SCX_EQ(skel->bss->probe_set_ret, false);
+
+ /* Step 4: Coming back sets the value again */
+ SCX_EQ(set_policy(SCHED_EXT, 0), 0);
+ SCX_EQ(probe(skel), 0);
+ SCX_EQ(skel->bss->probe_data, data);
+ SCX_EQ(skel->bss->probe_set_ret, true);
+
+ /* Step 5: Unloading the scheduler clears the value */
+ SCX_ASSERT_ALIVE(skel, uei);
+ bpf_link__destroy(*link);
+ *link = NULL;
+ SCX_EQ(skel->data->uei.kind, EXIT_KIND(SCX_EXIT_UNREG));
+ SCX_EQ(probe(skel), 0);
+ SCX_EQ(skel->bss->probe_data, 0);
+ SCX_EQ(skel->bss->probe_set_ret, false);
+
+ /* This task came in twice and the child once, all with no value */
+ SCX_EQ(skel->bss->nr_enable, 3);
+ SCX_EQ(skel->bss->nr_enable_nonzero, 0);
+ SCX_EQ(skel->bss->nr_enable_set_fail, 0);
+
+ return SCX_TEST_PASS;
+}
+
+static enum scx_test_status run(void *ctx)
+{
+ enum scx_test_status status;
+ struct task_bpf_data *skel;
+ struct bpf_link *link;
+
+ skel = task_bpf_data__open();
+ SCX_FAIL_IF(!skel, "Failed to open skel");
+ SCX_ENUM_INIT(skel);
+ SCX_FAIL_IF(task_bpf_data__load(skel), "Failed to load skel");
+
+ link = bpf_map__attach_struct_ops(skel->maps.task_bpf_data_ops);
+ SCX_FAIL_IF(!link, "Failed to attach struct_ops");
+
+ status = steps(skel, &link);
+
+ /* Restore the policy even if a step failed */
+ set_policy(SCHED_OTHER, 0);
+ bpf_link__destroy(link);
+ task_bpf_data__destroy(skel);
+
+ return status;
+}
+
+struct scx_test task_bpf_data = {
+ .name = "task_bpf_data",
+ .description = "Verify p->scx.bpf_data across class changes",
+ .run = run,
+};
+REGISTER_SCX_TEST(&task_bpf_data)
--
2.56.1
prev parent reply other threads:[~2026-12-01 5:49 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 5:48 [PATCH 0/5] sched_ext: Add p->scx.bpf_data for O(1) task context lookup Changwoo Min
2026-12-01 5:48 ` [PATCH 1/5] sched_ext: Add scx_bpf_sched_id() and scx_bpf_task_sched_id() Changwoo Min
2026-12-01 5:48 ` [PATCH 2/5] selftests/sched_ext: Test " Changwoo Min
2026-12-01 5:48 ` [PATCH 3/5] sched_ext: Add p->scx.bpf_data for O(1) task context lookup Changwoo Min
2026-12-01 7:48 ` Andrea Righi
2026-12-01 11:04 ` Changwoo Min
2026-12-01 5:48 ` [PATCH 4/5] sched_ext: scx_qmap: Cache task_ctx in p->scx.bpf_data Changwoo Min
2026-12-01 5:48 ` Changwoo Min [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261201054850.780664-6-changwoo@igalia.com \
--to=changwoo@igalia.com \
--cc=arighi@nvidia.com \
--cc=david.dai@linux.dev \
--cc=gavinguo@igalia.com \
--cc=kernel-dev@igalia.com \
--cc=linux-kernel@vger.kernel.org \
--cc=sched-ext@lists.linux.dev \
--cc=tj@kernel.org \
--cc=void@manifault.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®