* [PATCH v2 1/8] sched_ext: Reset task fields after ops.exit_task()
2026-12-03 4:56 [PATCH v2 0/8] sched_ext: Add p->scx.bpf_priv for O(1) task context lookup Changwoo Min
@ 2026-12-03 4:56 ` Changwoo Min
2026-12-03 4:56 ` [PATCH v2 2/8] sched_ext: Mark a task enabled before ops.enable() Changwoo Min
` (6 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Changwoo Min @ 2026-12-03 4:56 UTC (permalink / raw)
To: tj, void, arighi, changwoo
Cc: david.dai, Gavin Guo, kernel-dev, sched-ext, linux-kernel
__scx_disable_and_exit_task() resets the task fields before
ops.exit_task(). ops.exit_task() runs while @p is still on the scheduler,
so scx_bpf_task_set_dsq_vtime() accepts its writes, and they outlive the
reset. When @p moves between a parent scheduler and a sub-scheduler, the
next scheduler's ops.enable() then finds the vtime the previous one left.
Reset after ops.exit_task() instead. ops.disable() and ops.exit_task()
still see the final values, and a cancelled init now gets the reset too.
Fixes: 5771e79e461e ("sched_ext: Reset dsq_vtime and slice when a task leaves SCX")
Reported-by: Andrea Righi <arighi@nvidia.com>
Link: https://lore.kernel.org/r/aw57uMvkiKrLBBBI@gpd4
Signed-off-by: Changwoo Min <changwoo@igalia.com>
---
kernel/sched/ext/ext.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
index a3cf580b0333..eaa68a1e9198 100644
--- a/kernel/sched/ext/ext.c
+++ b/kernel/sched/ext/ext.c
@@ -4814,12 +4814,9 @@ void __scx_disable_and_exit_task(struct scx_sched *sch, struct task_struct *p)
args.cancelled = true;
break;
case SCX_TASK_READY:
- scx_reset_task_fields(task_rq(p), p);
break;
case SCX_TASK_ENABLED:
scx_disable_task(sch, p);
- /* after ops.disable() has seen the final values */
- scx_reset_task_fields(task_rq(p), p);
break;
default:
WARN_ON_ONCE(true);
@@ -4828,6 +4825,12 @@ void __scx_disable_and_exit_task(struct scx_sched *sch, struct task_struct *p)
if (SCX_HAS_OP(sch, exit_task))
SCX_CALL_OP_TASK(sch, exit_task, task_rq(p), p, &args);
+
+ /*
+ * After ops.disable() and ops.exit_task() have seen the final values.
+ * ops.exit_task() can still write them while @p is on @sch.
+ */
+ scx_reset_task_fields(task_rq(p), p);
}
/*
--
2.56.1
^ permalink raw reply [flat|nested] 9+ messages in thread* [PATCH v2 2/8] sched_ext: Mark a task enabled before ops.enable()
2026-12-03 4:56 [PATCH v2 0/8] sched_ext: Add p->scx.bpf_priv for O(1) task context lookup Changwoo Min
2026-12-03 4:56 ` [PATCH v2 1/8] sched_ext: Reset task fields after ops.exit_task() Changwoo Min
@ 2026-12-03 4:56 ` Changwoo Min
2026-12-03 4:56 ` [PATCH v2 3/8] sched_ext: Add scx_bpf_sched_id() and scx_bpf_task_sched_id() Changwoo Min
` (5 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Changwoo Min @ 2026-12-03 4:56 UTC (permalink / raw)
To: tj, void, arighi, changwoo
Cc: david.dai, Gavin Guo, kernel-dev, sched-ext, linux-kernel
scx_enable_task() sets SCX_TASK_ENABLED only after ops.enable() returns.
A task kfunc that accepts only enabled tasks, as scx_bpf_task_set_priv()
will, then refuses calls from ops.enable(). That is where a scheduler
sets up what it keeps for @p while controlling it.
Set the state before ops.enable() instead. ops.enable() can't fail, so @p
can't end up marked enabled without having been enabled. The other
SCX_TASK_ENABLED checks, in set_cpus_allowed_scx() and
reweight_task_scx(), run under the rq lock, which ops.enable() also
holds, so they can't see the earlier transition.
Suggested-by: Andrea Righi <arighi@nvidia.com>
Signed-off-by: Changwoo Min <changwoo@igalia.com>
---
kernel/sched/ext/ext.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
index eaa68a1e9198..7e5f58f1d844 100644
--- a/kernel/sched/ext/ext.c
+++ b/kernel/sched/ext/ext.c
@@ -4740,8 +4740,12 @@ static void __scx_enable_task(struct scx_sched *sch, struct task_struct *p)
void scx_enable_task(struct scx_sched *sch, struct task_struct *p)
{
- __scx_enable_task(sch, p);
+ /*
+ * Task kfuncs must recognize @p as enabled from ops.enable() on. Setting
+ * the state before ops.enable() is safe because it can't fail.
+ */
scx_set_task_state(p, SCX_TASK_ENABLED);
+ __scx_enable_task(sch, p);
}
/**
--
2.56.1
^ permalink raw reply [flat|nested] 9+ messages in thread* [PATCH v2 3/8] sched_ext: Add scx_bpf_sched_id() and scx_bpf_task_sched_id()
2026-12-03 4:56 [PATCH v2 0/8] sched_ext: Add p->scx.bpf_priv for O(1) task context lookup Changwoo Min
2026-12-03 4:56 ` [PATCH v2 1/8] sched_ext: Reset task fields after ops.exit_task() Changwoo Min
2026-12-03 4:56 ` [PATCH v2 2/8] sched_ext: Mark a task enabled before ops.enable() Changwoo Min
@ 2026-12-03 4:56 ` Changwoo Min
2026-12-03 4:56 ` [PATCH v2 4/8] selftests/sched_ext: Test " Changwoo Min
` (4 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Changwoo Min @ 2026-12-03 4:56 UTC (permalink / raw)
To: tj, void, arighi, changwoo
Cc: david.dai, Gavin Guo, kernel-dev, sched-ext, linux-kernel
A BPF scheduler can't tell which scheduler instance it is or which one
owns a task. With sub-schedulers, a task it reaches as a waker, from a
tracing program or by iteration may belong to another scheduler, and a
value that scheduler stored in the task means nothing to it.
Every scx_sched already has an id that starts at 1, increases
monotonically and is never reused. Add scx_bpf_sched_id(), the calling
scheduler's id, and scx_bpf_task_sched_id(), the id of the scheduler
that owns a task, both 0 for none. Any context may call them.
Signed-off-by: Changwoo Min <changwoo@igalia.com>
---
kernel/sched/ext/ext.c | 35 ++++++++++++++++++++++++
tools/sched_ext/include/scx/compat.bpf.h | 22 +++++++++++++++
2 files changed, 57 insertions(+)
diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
index 7e5f58f1d844..2ebc67916757 100644
--- a/kernel/sched/ext/ext.c
+++ b/kernel/sched/ext/ext.c
@@ -11940,6 +11940,39 @@ __bpf_kfunc s32 scx_bpf_task_cpu(const struct task_struct *p)
return task_cpu(p);
}
+/**
+ * scx_bpf_sched_id - Id of the calling scheduler
+ * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs
+ *
+ * Return the calling scheduler's instance id, 0 if there is none. Ids start at
+ * 1, increase monotonically and are never reused.
+ */
+__bpf_kfunc u64 scx_bpf_sched_id(const struct bpf_prog_aux *aux)
+{
+ struct scx_sched *sch;
+
+ guard(rcu)();
+ sch = scx_prog_sched(aux);
+ return sch ? sch->id : 0;
+}
+
+/**
+ * scx_bpf_task_sched_id - Id of the scheduler that owns a task
+ * @p: task of interest
+ *
+ * Return the instance id of the scheduler @p is on, see scx_bpf_sched_id(), 0
+ * if @p is not on any. Without the rq lock of @p, @p may move to another
+ * scheduler right after.
+ */
+__bpf_kfunc u64 scx_bpf_task_sched_id(struct task_struct *p)
+{
+ struct scx_sched *sch;
+
+ guard(rcu)();
+ sch = scx_task_sched_rcu(p);
+ return sch ? sch->id : 0;
+}
+
/**
* scx_bpf_task_cid - cid a task is currently associated with
* @p: task of interest
@@ -12262,6 +12295,8 @@ BTF_ID_FLAGS(func, scx_bpf_get_online_cpumask, KF_ACQUIRE)
BTF_ID_FLAGS(func, scx_bpf_put_cpumask, KF_RELEASE)
BTF_ID_FLAGS(func, scx_bpf_task_running, KF_RCU)
BTF_ID_FLAGS(func, scx_bpf_task_cpu, KF_RCU)
+BTF_ID_FLAGS(func, scx_bpf_sched_id, KF_IMPLICIT_ARGS)
+BTF_ID_FLAGS(func, scx_bpf_task_sched_id, KF_RCU)
BTF_ID_FLAGS(func, scx_bpf_task_cid, KF_RCU)
BTF_ID_FLAGS(func, scx_bpf_locked_rq, KF_IMPLICIT_ARGS | KF_RET_NULL)
BTF_ID_FLAGS(func, scx_bpf_cpu_curr, KF_IMPLICIT_ARGS | KF_RET_NULL | KF_RCU_PROTECTED)
diff --git a/tools/sched_ext/include/scx/compat.bpf.h b/tools/sched_ext/include/scx/compat.bpf.h
index 3520a749f8ce..41882c6497ac 100644
--- a/tools/sched_ext/include/scx/compat.bpf.h
+++ b/tools/sched_ext/include/scx/compat.bpf.h
@@ -487,6 +487,28 @@ static inline void scx_bpf_task_set_lazy_resched(struct task_struct *p, bool laz
scx_bpf_task_set_lazy_resched___new(p, lazy);
}
+/*
+ * v7.x: Add scx_bpf_sched_id() and scx_bpf_task_sched_id(). Ids start at 1,
+ * increase monotonically and are never reused. 0 means no scheduler, including
+ * on kernels without the kfuncs.
+ */
+u64 scx_bpf_sched_id___new(void) __ksym __weak;
+u64 scx_bpf_task_sched_id___new(struct task_struct *p) __ksym __weak;
+
+static inline u64 scx_bpf_sched_id(void)
+{
+ if (bpf_ksym_exists(scx_bpf_sched_id___new))
+ return scx_bpf_sched_id___new();
+ return 0;
+}
+
+static inline u64 scx_bpf_task_sched_id(struct task_struct *p)
+{
+ if (bpf_ksym_exists(scx_bpf_task_sched_id___new))
+ return scx_bpf_task_sched_id___new(p);
+ return 0;
+}
+
/*
* v7.1: New scx_bpf_dsq_reenq() that allows re-enqueues on more DSQs. This
* will eventually deprecate scx_bpf_reenqueue_local().
--
2.56.1
^ permalink raw reply [flat|nested] 9+ messages in thread* [PATCH v2 4/8] selftests/sched_ext: Test scx_bpf_sched_id() and scx_bpf_task_sched_id()
2026-12-03 4:56 [PATCH v2 0/8] sched_ext: Add p->scx.bpf_priv for O(1) task context lookup Changwoo Min
` (2 preceding siblings ...)
2026-12-03 4:56 ` [PATCH v2 3/8] sched_ext: Add scx_bpf_sched_id() and scx_bpf_task_sched_id() Changwoo Min
@ 2026-12-03 4:56 ` Changwoo Min
2026-12-03 4:56 ` [PATCH v2 5/8] sched_ext: Add p->scx.bpf_priv for O(1) task context lookup Changwoo Min
` (3 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Changwoo Min @ 2026-12-03 4:56 UTC (permalink / raw)
To: tj, void, arighi, changwoo
Cc: david.dai, Gavin Guo, kernel-dev, sched-ext, linux-kernel
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 <changwoo@igalia.com>
---
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 <changwoo@igalia.com>
+ */
+#include <scx/common.bpf.h>
+
+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 <changwoo@igalia.com>
+ */
+#include <bpf/bpf.h>
+#include <sched.h>
+#include <scx/common.h>
+#include <unistd.h>
+#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
^ permalink raw reply [flat|nested] 9+ messages in thread* [PATCH v2 5/8] sched_ext: Add p->scx.bpf_priv for O(1) task context lookup
2026-12-03 4:56 [PATCH v2 0/8] sched_ext: Add p->scx.bpf_priv for O(1) task context lookup Changwoo Min
` (3 preceding siblings ...)
2026-12-03 4:56 ` [PATCH v2 4/8] selftests/sched_ext: Test " Changwoo Min
@ 2026-12-03 4:56 ` Changwoo Min
2026-12-03 4:56 ` [PATCH v2 6/8] sched_ext: scx_qmap: Cache task_ctx in p->scx.bpf_priv Changwoo Min
` (2 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Changwoo Min @ 2026-12-03 4:56 UTC (permalink / raw)
To: tj, void, arighi, changwoo
Cc: david.dai, Gavin Guo, kernel-dev, sched-ext, linux-kernel
BPF schedulers keep their per-task context in task local storage, or in
an arena allocation reached through it. Each lookup costs a
bpf_task_storage_get() call.
Add an opaque u64, p->scx.bpf_priv, that the task's scheduler sets with
scx_bpf_task_set_priv(), typically to an arena pointer to its context.
Any BPF program reads it with a plain load. The kernel never dereferences
it.
The value is valid while @p is enabled on the scheduler that set it.
scx_reset_task_fields() clears it when @p leaves the scheduler's control,
after the last op the scheduler runs for @p. That covers @p leaving the
ext class, moving to another sub-scheduler and exiting. ops.enable() runs
again whenever @p comes back.
Like scx_bpf_task_set_dsq_vtime(), scx_bpf_task_set_priv() accepts any
caller and refuses a task that is not on the calling scheduler. Unlike
it, scx_bpf_task_set_priv() also refuses a task that is not enabled. The
value is cleared whenever @p leaves the ext class, and only ops.enable()
runs again when @p comes back. A value set earlier, e.g. from
ops.init_task(), would be lost the first time @p leaves the ext class. A
write from outside @p's own ops can race @p leaving and outlive the
clear. As with dsq_vtime, synchronizing such writes is left to the
scheduler.
The value means something only to the scheduler that set it. With
sub-schedulers, a task a scheduler reaches as a waker or from a tracing
program may belong to another one. A scheduler can tag the value with
its scx_bpf_sched_id() to recognize its own.
Signed-off-by: Changwoo Min <changwoo@igalia.com>
---
include/linux/sched/ext.h | 13 ++++++
kernel/sched/ext/ext.c | 54 ++++++++++++++++++++++--
tools/sched_ext/include/scx/compat.bpf.h | 24 +++++++++++
3 files changed, 87 insertions(+), 4 deletions(-)
diff --git a/include/linux/sched/ext.h b/include/linux/sched/ext.h
index f3564581c317..e63bd789c7d8 100644
--- a/include/linux/sched/ext.h
+++ b/include/linux/sched/ext.h
@@ -277,6 +277,19 @@ struct sched_ext_entity {
*/
atomic64_t slice_oob;
+ /*
+ * Opaque value owned by the task's scheduler, typically an arena
+ * pointer to its per-task context. The kernel never dereferences it.
+ * Set with scx_bpf_task_set_priv() while @p is enabled on the
+ * scheduler, from ops.enable() on, and cleared when @p leaves the
+ * scheduler's control, after the last op the scheduler runs for @p.
+ * Any BPF program can read it, so a tracing program can reach the
+ * context of the current task with a single load. A write from
+ * outside @p's own ops can outlive the clear, see
+ * scx_bpf_task_set_priv().
+ */
+ u64 bpf_priv;
+
/*
* Sub-sched cap rejected reenq context, valid only while
* %SCX_TASK_REENQ_CAP is set. @reenq_reason_caps is the SCX_CAP_* bits
diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
index 2ebc67916757..0877341440ad 100644
--- a/kernel/sched/ext/ext.c
+++ b/kernel/sched/ext/ext.c
@@ -4753,16 +4753,17 @@ void scx_enable_task(struct scx_sched *sch, struct task_struct *p)
* @rq: @p's rq, locked
* @p: task leaving its sched's control
*
- * The vtime, the slice and what rides on it, the reenqueue count and the
- * watchdog wait belong to the sched while it controls @p. The control ends when
- * @p is taken off the sched or leaves the ext class while staying on the sched.
- * Reset them so that @p starts clean wherever it goes.
+ * The vtime, the slice and what rides on it, the reenqueue count, the BPF
+ * private value and the watchdog wait belong to the sched while it controls @p.
+ * The control ends when @p is taken off the sched or leaves the ext class while
+ * staying on the sched. Reset them so that @p starts clean wherever it goes.
*/
static void scx_reset_task_fields(struct rq *rq, struct task_struct *p)
{
lockdep_assert_rq_held(rq);
p->scx.dsq_vtime = 0;
+ p->scx.bpf_priv = 0;
scx_task_slice_ended(rq, p);
scx_set_task_slice(p, 0);
p->scx.reenq_cnt = 0;
@@ -10893,6 +10894,50 @@ __bpf_kfunc bool scx_bpf_task_set_lazy_resched(struct task_struct *p, bool lazy,
return true;
}
+/**
+ * scx_bpf_task_set_priv - Set the BPF scheduler's private value of a task
+ * @p: task of interest
+ * @priv: opaque value to store in @p->scx.bpf_priv
+ * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs
+ *
+ * Store @priv in @p->scx.bpf_priv, where any BPF program can read it with a
+ * plain load. The value is valid while @p is enabled on the calling scheduler.
+ * The kernel clears it when @p leaves the scheduler's control, after the last
+ * op the scheduler runs for @p, and ops.enable() runs again when @p comes back.
+ *
+ * From @p's own ops, ops.enable() included, @p can't leave the scheduler
+ * concurrently. Elsewhere, e.g. from a timer or a tracing program, the write
+ * can race @p leaving and outlive the clear, as with
+ * scx_bpf_task_set_dsq_vtime(). Synchronizing such writes is the BPF
+ * scheduler's responsibility.
+ *
+ * Return %true if @priv was stored, %false if @p is not enabled on the calling
+ * scheduler.
+ */
+__bpf_kfunc bool scx_bpf_task_set_priv(struct task_struct *p, u64 priv,
+ const struct bpf_prog_aux *aux)
+{
+ struct scx_sched *sch;
+
+ guard(rcu)();
+ sch = scx_prog_sched(aux);
+ if (unlikely(!sch || !scx_task_on_sched(sch, p)))
+ return false;
+
+ /*
+ * Accept only %ENABLED. The value is cleared whenever @p leaves the ext
+ * class, and only ops.enable() runs again when @p comes back. A value
+ * set before ops.enable() would be lost the first time @p leaves the ext
+ * class. This refuses ops.init_task(), ops.exit_task() and @p on a
+ * non-ext class.
+ */
+ if (scx_get_task_state(p) != SCX_TASK_ENABLED)
+ return false;
+
+ p->scx.bpf_priv = priv;
+ return true;
+}
+
void scx_kick_cpu(struct scx_sched *sch, s32 cpu, u64 flags)
{
struct scx_sched_pcpu *pcpu;
@@ -12265,6 +12310,7 @@ BTF_KFUNCS_START(scx_kfunc_ids_any)
BTF_ID_FLAGS(func, scx_bpf_task_set_slice, KF_IMPLICIT_ARGS | KF_RCU);
BTF_ID_FLAGS(func, scx_bpf_task_set_dsq_vtime, KF_IMPLICIT_ARGS | KF_RCU);
BTF_ID_FLAGS(func, scx_bpf_task_set_lazy_resched, KF_IMPLICIT_ARGS | KF_RCU);
+BTF_ID_FLAGS(func, scx_bpf_task_set_priv, KF_IMPLICIT_ARGS | KF_RCU);
BTF_ID_FLAGS(func, scx_bpf_kick_cpu, KF_IMPLICIT_ARGS)
BTF_ID_FLAGS(func, scx_bpf_kick_cid, KF_IMPLICIT_ARGS)
BTF_ID_FLAGS(func, scx_bpf_dsq_nr_queued, KF_IMPLICIT_ARGS)
diff --git a/tools/sched_ext/include/scx/compat.bpf.h b/tools/sched_ext/include/scx/compat.bpf.h
index 41882c6497ac..2445f1affbec 100644
--- a/tools/sched_ext/include/scx/compat.bpf.h
+++ b/tools/sched_ext/include/scx/compat.bpf.h
@@ -487,6 +487,30 @@ static inline void scx_bpf_task_set_lazy_resched(struct task_struct *p, bool laz
scx_bpf_task_set_lazy_resched___new(p, lazy);
}
+/* v7.x: Add p->scx.bpf_priv and scx_bpf_task_set_priv(). */
+bool scx_bpf_task_set_priv___new(struct task_struct *p, u64 priv) __ksym __weak;
+
+static inline bool scx_bpf_task_set_priv(struct task_struct *p, u64 priv)
+{
+ if (bpf_ksym_exists(scx_bpf_task_set_priv___new))
+ return scx_bpf_task_set_priv___new(p, priv);
+ return false;
+}
+
+struct sched_ext_entity___bpf_priv {
+ u64 bpf_priv;
+} __attribute__((preserve_access_index));
+
+/* p->scx.bpf_priv, or 0 on kernels without it */
+static __always_inline u64 scx_bpf_task_priv(struct task_struct *p)
+{
+ struct sched_ext_entity___bpf_priv *scx = (void *)&p->scx;
+
+ if (bpf_core_field_exists(scx->bpf_priv))
+ return READ_ONCE(scx->bpf_priv);
+ return 0;
+}
+
/*
* v7.x: Add scx_bpf_sched_id() and scx_bpf_task_sched_id(). Ids start at 1,
* increase monotonically and are never reused. 0 means no scheduler, including
--
2.56.1
^ permalink raw reply [flat|nested] 9+ messages in thread* [PATCH v2 6/8] sched_ext: scx_qmap: Cache task_ctx in p->scx.bpf_priv
2026-12-03 4:56 [PATCH v2 0/8] sched_ext: Add p->scx.bpf_priv for O(1) task context lookup Changwoo Min
` (4 preceding siblings ...)
2026-12-03 4:56 ` [PATCH v2 5/8] sched_ext: Add p->scx.bpf_priv for O(1) task context lookup Changwoo Min
@ 2026-12-03 4:56 ` Changwoo Min
2026-12-03 4:56 ` [PATCH v2 7/8] selftests/sched_ext: Test p->scx.bpf_priv across class changes Changwoo Min
2026-12-03 4:56 ` [PATCH v2 8/8] selftests/sched_ext: Test task field reset across sub-scheduler handoffs Changwoo Min
7 siblings, 0 replies; 9+ messages in thread
From: Changwoo Min @ 2026-12-03 4:56 UTC (permalink / raw)
To: tj, void, arighi, changwoo
Cc: david.dai, Gavin Guo, kernel-dev, sched-ext, linux-kernel
Show how a scheduler uses p->scx.bpf_priv. lookup_task_ctx() runs in most
qmap callbacks and costs a task storage lookup each time. qmap_enable()
now caches the task_ctx arena pointer in the field, and lookup_task_ctx()
takes it with a single load, falling back to task storage otherwise.
The field holds whatever the sched owning the task stored, and a qmap
sched also sees the tasks of its sub-scheds, e.g. when dumping. So the
cached value is tagged with scx_bpf_sched_id(): the upper 32 bits hold
the id with bit 31 set, the lower 32 bits the pointer's. Ids are never
reused, so values of other scheds, including unloaded ones, never match.
The pointer is rebuilt from the upper 32 bits all of the arena's pointers
share.
An arena pointer is a user space address, whose most significant bit is
always zero. Bit 31 of the tag is the most significant bit of the cached
value, and setting it to 1 makes the value an invalid user space address,
for foolproofing.
Signed-off-by: Changwoo Min <changwoo@igalia.com>
---
tools/sched_ext/scx_qmap.bpf.c | 58 ++++++++++++++++++++++++++++++++++
1 file changed, 58 insertions(+)
diff --git a/tools/sched_ext/scx_qmap.bpf.c b/tools/sched_ext/scx_qmap.bpf.c
index 3951f1f6e3c6..8fa6626d6598 100644
--- a/tools/sched_ext/scx_qmap.bpf.c
+++ b/tools/sched_ext/scx_qmap.bpf.c
@@ -9,6 +9,7 @@
* - BPF arena for scheduler state.
* - Core-sched support.
* - Hierarchical sub-scheduling: delegating cpus to child cgroup schedulers.
+ * - Caching the per-task context in p->scx.bpf_priv.
*
* scx_qmap is often used to exercise sched_ext features as they land. It is
* coupled with the kernel it ships with and uses its sched_ext interfaces
@@ -256,12 +257,47 @@ static s32 pick_direct_dispatch_cid(struct task_struct *p, s32 prev_cid,
*/
#define QMAP_TOUCH_ARENA() do { asm volatile("" :: "r"(&arena)); } while (0)
+/*
+ * p->scx.bpf_priv caches @p's task_ctx so that lookup_task_ctx() takes a load
+ * instead of a task storage lookup. The field holds whatever the sched owning
+ * @p stored, and a qmap sched also sees tasks of its sub-scheds, e.g. in
+ * qmap_dump_task(). So the cached value carries this sched's id as a tag:
+ *
+ * task_ctx_key
+ * 63-32 upper 32 bits of the arena's pointers
+ * 31 1, TASK_CTX_TAG_BIT
+ * 30-0 id from scx_bpf_sched_id(), 1 <= id < 2^31
+ *
+ * p->scx.bpf_priv
+ * 63 1, TASK_CTX_TAG_BIT
+ * 62-32 id
+ * 31-0 lower 32 bits of @p's task_ctx pointer
+ *
+ * The arena never crosses a 4G boundary, so all its pointers share the upper
+ * 32 bits. Ids are never reused, so a value left by another sched, including
+ * one that has since unloaded, never matches. An arena pointer is a user space
+ * address, whose most significant bit is always zero. Bit 31 of the tag is the
+ * most significant bit of p->scx.bpf_priv, and setting it to 1 makes the
+ * cached value an invalid user space address, for foolproofing.
+ *
+ * qmap_enable() sets the value. The first call also sets task_ctx_key, which
+ * then never changes. The kernel clears the value when @p leaves this sched.
+ */
+#define TASK_CTX_TAG_BIT (1U << 31)
+
+static u64 task_ctx_key;
+
static task_ctx_t *lookup_task_ctx(struct task_struct *p)
{
struct task_ctx_stor_val *v;
+ u64 key = READ_ONCE(task_ctx_key);
+ u64 priv = READ_ONCE(p->scx.bpf_priv);
QMAP_TOUCH_ARENA();
+ if (key && (u32)(priv >> 32) == (u32)key)
+ return (task_ctx_t *)((key & ~0xffffffffULL) | (u32)priv);
+
v = bpf_task_storage_get(&task_ctx_stor, p, 0, 0);
if (!v || !v->taskc)
return NULL;
@@ -1062,6 +1098,27 @@ s32 BPF_STRUCT_OPS_SLEEPABLE(qmap_init_task, struct task_struct *p,
return 0;
}
+void BPF_STRUCT_OPS(qmap_enable, struct task_struct *p,
+ struct scx_enable_args *args)
+{
+ task_ctx_t *taskc = lookup_task_ctx(p);
+ u64 key = READ_ONCE(task_ctx_key);
+ u64 id;
+
+ if (!taskc)
+ return;
+
+ if (!key) {
+ id = scx_bpf_sched_id();
+ if (!id || id >= TASK_CTX_TAG_BIT)
+ return;
+ key = ((u64)taskc & ~0xffffffffULL) | TASK_CTX_TAG_BIT | (u32)id;
+ WRITE_ONCE(task_ctx_key, key);
+ }
+
+ scx_bpf_task_set_priv(p, (u64)(u32)key << 32 | (u32)(u64)taskc);
+}
+
void BPF_STRUCT_OPS(qmap_exit_task, struct task_struct *p,
struct scx_exit_task_args *args)
{
@@ -2252,6 +2309,7 @@ SCX_OPS_CID_DEFINE(qmap_ops,
.set_cmask = (void *)qmap_set_cmask,
.update_idle = (void *)qmap_update_idle,
.init_task = (void *)qmap_init_task,
+ .enable = (void *)qmap_enable,
.exit_task = (void *)qmap_exit_task,
.dump = (void *)qmap_dump,
.dump_cid = (void *)qmap_dump_cid,
--
2.56.1
^ permalink raw reply [flat|nested] 9+ messages in thread* [PATCH v2 7/8] selftests/sched_ext: Test p->scx.bpf_priv across class changes
2026-12-03 4:56 [PATCH v2 0/8] sched_ext: Add p->scx.bpf_priv for O(1) task context lookup Changwoo Min
` (5 preceding siblings ...)
2026-12-03 4:56 ` [PATCH v2 6/8] sched_ext: scx_qmap: Cache task_ctx in p->scx.bpf_priv Changwoo Min
@ 2026-12-03 4:56 ` Changwoo Min
2026-12-03 4:56 ` [PATCH v2 8/8] selftests/sched_ext: Test task field reset across sub-scheduler handoffs Changwoo Min
7 siblings, 0 replies; 9+ messages in thread
From: Changwoo Min @ 2026-12-03 4:56 UTC (permalink / raw)
To: tj, void, arighi, changwoo
Cc: david.dai, Gavin Guo, kernel-dev, sched-ext, linux-kernel
Under a partial-switch scheduler that sets p->scx.bpf_priv 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_priv.bpf.c | 64 ++++++++
.../selftests/sched_ext/task_bpf_priv.c | 147 ++++++++++++++++++
3 files changed, 212 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..ae57494afbaf
--- /dev/null
+++ b/tools/testing/selftests/sched_ext/task_bpf_priv.bpf.c
@@ -0,0 +1,64 @@
+// 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@igalia.com>
+ */
+#include <scx/common.bpf.h>
+
+char _license[] SEC("license") = "GPL";
+
+UEI_DEFINE(uei); /* Error handling */
+
+#define PRIV_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_priv;
+bool probe_set_ret;
+
+void BPF_STRUCT_OPS(task_bpf_priv_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_priv)
+ __sync_fetch_and_add(&nr_enable_nonzero, 1);
+
+ if (!scx_bpf_task_set_priv(p, PRIV_MAGIC | (u32)p->pid))
+ __sync_fetch_and_add(&nr_enable_set_fail, 1);
+}
+
+void BPF_STRUCT_OPS(task_bpf_priv_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_priv = p->scx.bpf_priv;
+ probe_set_ret = scx_bpf_task_set_priv(p, PRIV_MAGIC | (u32)p->pid);
+ bpf_task_release(p);
+ return 0;
+}
+
+SEC(".struct_ops.link")
+struct sched_ext_ops task_bpf_priv_ops = {
+ .enable = (void *)task_bpf_priv_enable,
+ .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..7500fc5a4992
--- /dev/null
+++ b/tools/testing/selftests/sched_ext/task_bpf_priv.c
@@ -0,0 +1,147 @@
+// 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@igalia.com>
+ */
+#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 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_priv *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_priv *skel,
+ struct bpf_link **link)
+{
+ u64 priv = PRIV_MAGIC | (u32)getpid();
+ int status;
+ pid_t pid;
+
+ skel->bss->target_pid = getpid();
+
+ /* Step 1: Switch this task to SCHED_EXT */
+ SCX_EQ(set_policy(SCHED_EXT, 0), 0);
+
+ /*
+ * %SCX_OPS_SWITCH_PARTIAL takes only SCHED_EXT tasks. Switching makes
+ * ops.enable() set the value. probe() must read it back, and its own
+ * set must succeed because the task is enabled.
+ */
+ SCX_EQ(probe(skel), 0);
+ SCX_EQ(skel->bss->probe_priv, priv);
+ 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_EQ(waitpid(pid, &status, 0), pid);
+
+ /*
+ * The child must not inherit the value, which ops.enable() checks for
+ * every task it enables. The fork must leave this task's value intact.
+ */
+ SCX_EQ(probe(skel), 0);
+ SCX_EQ(skel->bss->probe_priv, priv);
+ SCX_EQ(skel->bss->probe_set_ret, true);
+
+ /* Step 3: Switch this task to SCHED_FIFO */
+ SCX_EQ(set_policy(SCHED_FIFO, 1), 0);
+
+ /*
+ * Leaving the ext class disables the task, and the kernel clears the
+ * value. probe() must read 0, and its set must fail because the task
+ * is no longer enabled.
+ */
+ SCX_EQ(probe(skel), 0);
+ SCX_EQ(skel->bss->probe_priv, 0);
+ SCX_EQ(skel->bss->probe_set_ret, false);
+
+ /* Step 4: Switch this task back to SCHED_EXT */
+ SCX_EQ(set_policy(SCHED_EXT, 0), 0);
+
+ /* ops.enable() runs again and sets the value, as in step 1 */
+ SCX_EQ(probe(skel), 0);
+ SCX_EQ(skel->bss->probe_priv, priv);
+ 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
+ * leaves the ext class, so probe() must read 0 and its set must fail,
+ * as in step 3.
+ */
+ SCX_EQ(skel->data->uei.kind, EXIT_KIND(SCX_EXIT_UNREG));
+ SCX_EQ(probe(skel), 0);
+ 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. Each
+ * run must have found the value cleared and set it successfully.
+ */
+ 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_priv *skel;
+ struct bpf_link *link;
+
+ skel = task_bpf_priv__open();
+ SCX_FAIL_IF(!skel, "Failed to open skel");
+ SCX_ENUM_INIT(skel);
+ SCX_FAIL_IF(task_bpf_priv__load(skel), "Failed to load skel");
+
+ link = bpf_map__attach_struct_ops(skel->maps.task_bpf_priv_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_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
^ permalink raw reply [flat|nested] 9+ messages in thread* [PATCH v2 8/8] selftests/sched_ext: Test task field reset across sub-scheduler handoffs
2026-12-03 4:56 [PATCH v2 0/8] sched_ext: Add p->scx.bpf_priv for O(1) task context lookup Changwoo Min
` (6 preceding siblings ...)
2026-12-03 4:56 ` [PATCH v2 7/8] selftests/sched_ext: Test p->scx.bpf_priv across class changes Changwoo Min
@ 2026-12-03 4:56 ` Changwoo Min
7 siblings, 0 replies; 9+ messages in thread
From: Changwoo Min @ 2026-12-03 4:56 UTC (permalink / raw)
To: tj, void, arighi, changwoo
Cc: david.dai, Gavin Guo, kernel-dev, sched-ext, linux-kernel
A task handed between a root scheduler and a sub-scheduler must not
carry the bpf_priv and dsq_vtime of the scheduler it left, even when that
scheduler writes them from ops.exit_task().
Load one scheduler twice, as the root and as a sub-scheduler on a cgroup.
A task in that cgroup goes to the sub-scheduler when it attaches and
back to the root when it detaches. Both schedulers set bpf_priv from
ops.enable(), and from ops.exit_task() they try to set bpf_priv and set
dsq_vtime. Setting bpf_priv from ops.exit_task() must fail, and every
ops.enable() must find both fields cleared.
The test skips without CONFIG_EXT_SUB_SCHED.
Signed-off-by: Changwoo Min <changwoo@igalia.com>
---
tools/testing/selftests/sched_ext/Makefile | 1 +
.../selftests/sched_ext/sub_handoff.bpf.c | 102 +++++++
.../testing/selftests/sched_ext/sub_handoff.c | 265 ++++++++++++++++++
3 files changed, 368 insertions(+)
create mode 100644 tools/testing/selftests/sched_ext/sub_handoff.bpf.c
create mode 100644 tools/testing/selftests/sched_ext/sub_handoff.c
diff --git a/tools/testing/selftests/sched_ext/Makefile b/tools/testing/selftests/sched_ext/Makefile
index 1ab274250403..1dcaf60c5b33 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 \
+ sub_handoff \
task_bpf_priv \
test_example \
total_bw \
diff --git a/tools/testing/selftests/sched_ext/sub_handoff.bpf.c b/tools/testing/selftests/sched_ext/sub_handoff.bpf.c
new file mode 100644
index 000000000000..d7ce3f4e150c
--- /dev/null
+++ b/tools/testing/selftests/sched_ext/sub_handoff.bpf.c
@@ -0,0 +1,102 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * A cid-form scheduler for the sub_handoff selftest, loaded twice: as the root
+ * and as a sub-scheduler on a cgroup.
+ *
+ * Copyright (c) 2026 Meta Platforms, Inc. and affiliates.
+ * Author: Changwoo Min <changwoo@igalia.com>
+ */
+#include <scx/common.bpf.h>
+
+char _license[] SEC("license") = "GPL";
+
+#define PRIV_MAGIC 0x5c7da7a000000000ULL
+#define VTIME_MAGIC 0x5c7d7173e0000000ULL
+
+struct {
+ __uint(type, BPF_MAP_TYPE_ARENA);
+ __uint(map_flags, BPF_F_MMAPABLE);
+ __uint(max_entries, 1 << 4);
+} arena SEC(".maps");
+
+u64 __arena_global arena_touch;
+
+/* The task handed between the schedulers, set by user space */
+s32 target_pid;
+
+/* ops.enable() calls on the target, and those that saw a value left behind */
+u64 nr_enable, nr_stale_priv, nr_stale_vtime;
+
+/* ops.exit_task() calls on the target in which setting bpf_priv succeeded */
+u64 nr_exit_set;
+
+UEI_DEFINE(uei);
+
+s32 BPF_STRUCT_OPS(sub_handoff_select_cid, struct task_struct *p, s32 prev_cid,
+ u64 wake_flags)
+{
+ return prev_cid;
+}
+
+void BPF_STRUCT_OPS(sub_handoff_enqueue, struct task_struct *p, u64 enq_flags)
+{
+ scx_bpf_dsq_insert(p, SCX_DSQ_LOCAL, SCX_SLICE_DFL, enq_flags);
+}
+
+void BPF_STRUCT_OPS(sub_handoff_enable, struct task_struct *p,
+ struct scx_enable_args *args)
+{
+ if (p->pid != target_pid)
+ return;
+
+ __sync_fetch_and_add(&nr_enable, 1);
+ if (p->scx.bpf_priv)
+ __sync_fetch_and_add(&nr_stale_priv, 1);
+ if (p->scx.dsq_vtime)
+ __sync_fetch_and_add(&nr_stale_vtime, 1);
+
+ scx_bpf_task_set_priv(p, PRIV_MAGIC | (u32)p->pid);
+}
+
+/*
+ * @p is still on this scheduler but no longer enabled. Neither write may reach
+ * the scheduler @p moves to.
+ */
+void BPF_STRUCT_OPS(sub_handoff_exit_task, struct task_struct *p,
+ struct scx_exit_task_args *args)
+{
+ if (p->pid != target_pid)
+ return;
+
+ if (scx_bpf_task_set_priv(p, PRIV_MAGIC | (u32)p->pid))
+ __sync_fetch_and_add(&nr_exit_set, 1);
+ scx_bpf_task_set_dsq_vtime(p, VTIME_MAGIC);
+}
+
+s32 BPF_STRUCT_OPS_SLEEPABLE(sub_handoff_sub_attach,
+ struct scx_sub_attach_args *args)
+{
+ return 0;
+}
+
+s32 BPF_STRUCT_OPS_SLEEPABLE(sub_handoff_init)
+{
+ arena_touch++;
+ return 0;
+}
+
+void BPF_STRUCT_OPS(sub_handoff_exit, struct scx_exit_info *ei)
+{
+ UEI_RECORD(uei, ei);
+}
+
+SCX_OPS_CID_DEFINE(sub_handoff_ops,
+ .select_cid = (void *)sub_handoff_select_cid,
+ .enqueue = (void *)sub_handoff_enqueue,
+ .enable = (void *)sub_handoff_enable,
+ .exit_task = (void *)sub_handoff_exit_task,
+ .sub_attach = (void *)sub_handoff_sub_attach,
+ .init = (void *)sub_handoff_init,
+ .exit = (void *)sub_handoff_exit,
+ .flags = SCX_OPS_SWITCH_PARTIAL,
+ .name = "sub_handoff");
diff --git a/tools/testing/selftests/sched_ext/sub_handoff.c b/tools/testing/selftests/sched_ext/sub_handoff.c
new file mode 100644
index 000000000000..18e20416c911
--- /dev/null
+++ b/tools/testing/selftests/sched_ext/sub_handoff.c
@@ -0,0 +1,265 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Verify that a task handed between a root scheduler and a sub-scheduler
+ * arrives without the p->scx.bpf_priv and dsq_vtime of the scheduler it left,
+ * even when that scheduler writes them from ops.exit_task().
+ *
+ * Copyright (c) 2026 Meta Platforms, Inc. and affiliates.
+ * Author: Changwoo Min <changwoo@igalia.com>
+ */
+#define _GNU_SOURCE
+#include <bpf/bpf.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <sched.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+#include <scx/common.h>
+
+#include "cgroup_util.h"
+#include "scx_test.h"
+#include "sub_handoff.bpf.skel.h"
+
+#ifndef SCHED_EXT
+#define SCHED_EXT 7
+#endif
+
+#define WAIT_TIMEOUT_MS 1000
+
+struct sub_handoff_ctx {
+ struct sub_handoff *root_skel;
+ struct sub_handoff *sub_skel;
+ char sub_cg[PATH_MAX];
+ u64 sub_cgid;
+ bool cg_created;
+};
+
+static u64 get_cgroup_id(const char *path)
+{
+ union {
+ u64 id;
+ unsigned char bytes[8];
+ } id = {};
+ struct file_handle *handle;
+ int mount_id, ret;
+
+ handle = calloc(1, sizeof(*handle) + sizeof(id));
+ if (!handle)
+ return 0;
+ handle->handle_bytes = sizeof(id);
+ ret = name_to_handle_at(AT_FDCWD, path, handle, &mount_id, 0);
+ if (!ret && handle->handle_bytes == sizeof(id))
+ memcpy(id.bytes, handle->f_handle, sizeof(id));
+ free(handle);
+
+ return ret ? 0 : id.id;
+}
+
+static u64 now_ns(void)
+{
+ struct timespec ts;
+
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ return (u64)ts.tv_sec * 1000000000ULL + ts.tv_nsec;
+}
+
+/* Wait until *@ptr reaches @val, return its last value */
+static u64 wait_u64(u64 *ptr, u64 val)
+{
+ u64 deadline = now_ns() + WAIT_TIMEOUT_MS * 1000000ULL;
+ u64 v;
+
+ while ((v = __atomic_load_n(ptr, __ATOMIC_ACQUIRE)) < val &&
+ now_ns() < deadline)
+ usleep(1000);
+ return v;
+}
+
+static void cleanup(void *ctx_ptr)
+{
+ struct sub_handoff_ctx *ctx = ctx_ptr;
+
+ sub_handoff__destroy(ctx->sub_skel);
+ sub_handoff__destroy(ctx->root_skel);
+ if (ctx->cg_created)
+ cg_destroy(ctx->sub_cg);
+ free(ctx);
+}
+
+static struct sub_handoff *open_and_load(u64 sub_cgid)
+{
+ struct sub_handoff *skel;
+
+ skel = sub_handoff__open();
+ if (!skel)
+ return NULL;
+ SCX_ENUM_INIT(skel);
+ skel->struct_ops.sub_handoff_ops->sub_cgroup_id = sub_cgid;
+ if (sub_handoff__load(skel)) {
+ sub_handoff__destroy(skel);
+ return NULL;
+ }
+ return skel;
+}
+
+static enum scx_test_status setup(void **ctx_ptr)
+{
+ struct sub_handoff_ctx *ctx;
+ char root[PATH_MAX];
+
+ if (!__COMPAT_struct_has_field("sched_ext_ops", "sub_cgroup_id")) {
+ printf("SKIP: sub-schedulers not supported\n");
+ return SCX_TEST_SKIP;
+ }
+
+ if (cg_find_unified_root(root, sizeof(root), NULL)) {
+ printf("SKIP: cgroup v2 root not found\n");
+ return SCX_TEST_SKIP;
+ }
+
+ ctx = calloc(1, sizeof(*ctx));
+ SCX_FAIL_IF(!ctx, "Failed to allocate context");
+
+ if (snprintf(ctx->sub_cg, sizeof(ctx->sub_cg), "%s/scx_sub_handoff_%d",
+ root, getpid()) >= (int)sizeof(ctx->sub_cg)) {
+ cleanup(ctx);
+ SCX_FAIL("Sub cgroup path too long");
+ }
+ if (cg_create(ctx->sub_cg)) {
+ cleanup(ctx);
+ SCX_FAIL("Failed to create sub cgroup (%d)", errno);
+ }
+ ctx->cg_created = true;
+
+ ctx->sub_cgid = get_cgroup_id(ctx->sub_cg);
+ if (!ctx->sub_cgid) {
+ cleanup(ctx);
+ SCX_FAIL("Failed to read sub cgroup ID");
+ }
+
+ ctx->root_skel = open_and_load(0);
+ if (!ctx->root_skel) {
+ cleanup(ctx);
+ SCX_FAIL("Failed to load root skel");
+ }
+
+ ctx->sub_skel = open_and_load(ctx->sub_cgid);
+ if (!ctx->sub_skel) {
+ cleanup(ctx);
+ SCX_FAIL("Failed to load sub skel");
+ }
+
+ *ctx_ptr = ctx;
+ return SCX_TEST_PASS;
+}
+
+static enum scx_test_status handoffs(struct sub_handoff_ctx *ctx, pid_t pid,
+ struct bpf_link **root_link,
+ struct bpf_link **sub_link)
+{
+ struct sub_handoff__bss *root = ctx->root_skel->bss;
+ struct sub_handoff__bss *sub = ctx->sub_skel->bss;
+ struct sched_param param = {};
+
+ root->target_pid = pid;
+ sub->target_pid = pid;
+
+ *root_link = bpf_map__attach_struct_ops(ctx->root_skel->maps.sub_handoff_ops);
+ SCX_FAIL_IF(!*root_link, "Failed to attach root scheduler");
+
+ /* Step 1: Move the target to the sub-cgroup */
+ SCX_FAIL_IF(cg_enter(ctx->sub_cg, pid), "Failed to move target into sub cgroup");
+
+ /*
+ * %SCX_OPS_SWITCH_PARTIAL takes only SCHED_EXT tasks. Switching the
+ * target makes the root, the only scheduler yet, enable it and set its
+ * bpf_priv.
+ */
+ SCX_EQ(sched_setscheduler(pid, SCHED_EXT, ¶m), 0);
+ SCX_EQ(wait_u64(&root->nr_enable, 1), 1);
+
+ /* Step 2: Attaching the sub-scheduler hands the target over */
+ *sub_link = bpf_map__attach_struct_ops(ctx->sub_skel->maps.sub_handoff_ops);
+ if (!*sub_link && errno == EOPNOTSUPP) {
+ printf("SKIP: CONFIG_EXT_SUB_SCHED not enabled\n");
+ return SCX_TEST_SKIP;
+ }
+ SCX_FAIL_IF(!*sub_link, "Failed to attach sub scheduler (%d)", errno);
+
+ /*
+ * The root exits the target before the sub-scheduler enables it. The
+ * root's ops.exit_task() tries to set bpf_priv, which must fail because
+ * the target is no longer enabled. It also sets dsq_vtime, which the
+ * kernel must clear afterwards. Both fields start at 0 on a clean
+ * handoff, so the sub-scheduler's ops.enable() must see neither.
+ */
+ SCX_EQ(wait_u64(&sub->nr_enable, 1), 1);
+ SCX_EQ(root->nr_exit_set, 0);
+ SCX_EQ(sub->nr_stale_priv, 0);
+ SCX_EQ(sub->nr_stale_vtime, 0);
+
+ /* Step 3: Detaching the sub-scheduler hands the target back */
+ bpf_link__destroy(*sub_link);
+ *sub_link = NULL;
+
+ /*
+ * The same checks in the other direction, from the sub-scheduler's
+ * ops.exit_task() to the root's second ops.enable(). The root's stale
+ * counters also cover its first ops.enable() in step 1.
+ */
+ SCX_EQ(wait_u64(&root->nr_enable, 2), 2);
+ SCX_EQ(sub->nr_exit_set, 0);
+ SCX_EQ(root->nr_stale_priv, 0);
+ SCX_EQ(root->nr_stale_vtime, 0);
+
+ /* Neither handoff may have made the root exit with an error */
+ SCX_ASSERT_ALIVE(ctx->root_skel, uei);
+ return SCX_TEST_PASS;
+}
+
+static enum scx_test_status run(void *ctx_ptr)
+{
+ struct sub_handoff_ctx *ctx = ctx_ptr;
+ struct bpf_link *root_link = NULL, *sub_link = NULL;
+ enum scx_test_status status;
+ int pfd[2];
+ pid_t pid;
+ char c;
+
+ SCX_FAIL_IF(pipe(pfd), "Failed to create pipe (%d)", errno);
+
+ /* The target sleeps through the handoffs and exits when the pipe closes */
+ pid = fork();
+ if (pid < 0) {
+ close(pfd[0]);
+ close(pfd[1]);
+ SCX_FAIL("Failed to fork target (%d)", errno);
+ }
+ if (pid == 0) {
+ close(pfd[1]);
+ _exit(read(pfd[0], &c, 1) == 0 ? 0 : 1);
+ }
+ close(pfd[0]);
+
+ status = handoffs(ctx, pid, &root_link, &sub_link);
+
+ bpf_link__destroy(sub_link);
+ bpf_link__destroy(root_link);
+ close(pfd[1]);
+ waitpid(pid, NULL, 0);
+ return status;
+}
+
+struct scx_test sub_handoff = {
+ .name = "sub_handoff",
+ .description = "Verify that sub-scheduler handoffs reset the task fields",
+ .setup = setup,
+ .run = run,
+ .cleanup = cleanup,
+};
+REGISTER_SCX_TEST(&sub_handoff)
--
2.56.1
^ permalink raw reply [flat|nested] 9+ messages in thread