* [PATCH 1/5] sched_ext: Add scx_bpf_sched_id() and scx_bpf_task_sched_id()
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 ` Changwoo Min
2026-12-01 5:48 ` [PATCH 2/5] selftests/sched_ext: Test " Changwoo Min
` (3 subsequent siblings)
4 siblings, 0 replies; 8+ messages in thread
From: Changwoo Min @ 2026-12-01 5:48 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 a3cf580b0333..cdceeffbcc65 100644
--- a/kernel/sched/ext/ext.c
+++ b/kernel/sched/ext/ext.c
@@ -11933,6 +11933,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
@@ -12255,6 +12288,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] 8+ messages in thread* [PATCH 2/5] selftests/sched_ext: Test scx_bpf_sched_id() and scx_bpf_task_sched_id()
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 ` 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
` (2 subsequent siblings)
4 siblings, 0 replies; 8+ messages in thread
From: Changwoo Min @ 2026-12-01 5:48 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] 8+ messages in thread* [PATCH 3/5] sched_ext: Add p->scx.bpf_data for O(1) task context lookup
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 ` Changwoo Min
2026-12-01 7:48 ` Andrea Righi
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 ` [PATCH 5/5] selftests/sched_ext: Test p->scx.bpf_data across class changes Changwoo Min
4 siblings, 1 reply; 8+ messages in thread
From: Changwoo Min @ 2026-12-01 5:48 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_data, that the task's scheduler sets with
scx_bpf_task_set_data(), 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 after ops.disable(), which covers @p
leaving the ext class, moving to another sub-scheduler and exiting.
ops.enable() runs again whenever @p comes back.
scx_bpf_task_set_data() follows the rules of scx_bpf_task_set_dsq_vtime().
It accepts any caller and refuses a task that is not enabled on the calling
scheduler, except in ops.enable(), where @p is still SCX_TASK_READY. A
write can race @p leaving and outlive the clear, and synchronizing such
writes is left to the scheduler, as for dsq_vtime.
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 | 12 ++++++
kernel/sched/ext/ext.c | 54 ++++++++++++++++++++++--
tools/sched_ext/include/scx/compat.bpf.h | 24 +++++++++++
3 files changed, 86 insertions(+), 4 deletions(-)
diff --git a/include/linux/sched/ext.h b/include/linux/sched/ext.h
index f3564581c317..2c2ce144601d 100644
--- a/include/linux/sched/ext.h
+++ b/include/linux/sched/ext.h
@@ -277,6 +277,18 @@ 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_data() while @p is enabled on the
+ * scheduler, from ops.enable() on, and cleared when @p leaves the
+ * scheduler's control, after ops.disable(). 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_data().
+ */
+ u64 bpf_data;
+
/*
* 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 cdceeffbcc65..bca27ced9d8d 100644
--- a/kernel/sched/ext/ext.c
+++ b/kernel/sched/ext/ext.c
@@ -4749,16 +4749,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 data
+ * 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_data = 0;
scx_task_slice_ended(rq, p);
scx_set_task_slice(p, 0);
p->scx.reenq_cnt = 0;
@@ -10886,6 +10887,50 @@ __bpf_kfunc bool scx_bpf_task_set_lazy_resched(struct task_struct *p, bool lazy,
return true;
}
+/**
+ * scx_bpf_task_set_data - Set the BPF data of a task
+ * @p: task of interest
+ * @data: opaque value to store in @p->scx.bpf_data
+ * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs
+ *
+ * Store @data in @p->scx.bpf_data, 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
+ * ops.disable(), 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 @data was stored, %false if @p is not on the calling
+ * scheduler or is neither enabled on it nor being enabled by ops.enable().
+ */
+__bpf_kfunc bool scx_bpf_task_set_data(struct task_struct *p, u64 data,
+ const struct bpf_prog_aux *aux)
+{
+ struct scx_sched *sch;
+ u32 state;
+
+ guard(rcu)();
+ sch = scx_prog_sched(aux);
+ if (unlikely(!sch || !scx_task_on_sched(sch, p)))
+ return false;
+
+ /*
+ * ops.enable() runs while @p is still %SCX_TASK_READY but already on the
+ * ext class. A %SCX_TASK_READY @p on another class is not enabled.
+ */
+ state = scx_get_task_state(p);
+ if (state != SCX_TASK_ENABLED &&
+ (state != SCX_TASK_READY || p->sched_class != &ext_sched_class))
+ return false;
+
+ p->scx.bpf_data = data;
+ return true;
+}
+
void scx_kick_cpu(struct scx_sched *sch, s32 cpu, u64 flags)
{
struct scx_sched_pcpu *pcpu;
@@ -12258,6 +12303,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_data, 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..a67f0e18a783 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_data and scx_bpf_task_set_data(). */
+bool scx_bpf_task_set_data___new(struct task_struct *p, u64 data) __ksym __weak;
+
+static inline bool scx_bpf_task_set_data(struct task_struct *p, u64 data)
+{
+ if (bpf_ksym_exists(scx_bpf_task_set_data___new))
+ return scx_bpf_task_set_data___new(p, data);
+ return false;
+}
+
+struct sched_ext_entity___bpf_data {
+ u64 bpf_data;
+} __attribute__((preserve_access_index));
+
+/* p->scx.bpf_data, or 0 on kernels without it */
+static __always_inline u64 scx_bpf_task_data(struct task_struct *p)
+{
+ struct sched_ext_entity___bpf_data *scx = (void *)&p->scx;
+
+ if (bpf_core_field_exists(scx->bpf_data))
+ return READ_ONCE(scx->bpf_data);
+ 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] 8+ messages in thread* Re: [PATCH 3/5] sched_ext: Add p->scx.bpf_data for O(1) task context lookup
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
0 siblings, 1 reply; 8+ messages in thread
From: Andrea Righi @ 2026-12-01 7:48 UTC (permalink / raw)
To: Changwoo Min
Cc: tj, void, david.dai, Gavin Guo, kernel-dev, sched-ext, linux-kernel
Hi Changwoo,
thanks for working at this! Then I can finally remove the ugly
p->scx.dsq_vtime hack. :)
Comments below.
On Tue, Dec 01, 2026 at 02:48:48PM +0900, Changwoo Min wrote:
> 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_data, that the task's scheduler sets with
> scx_bpf_task_set_data(), 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 after ops.disable(), which covers @p
> leaving the ext class, moving to another sub-scheduler and exiting.
> ops.enable() runs again whenever @p comes back.
>
> scx_bpf_task_set_data() follows the rules of scx_bpf_task_set_dsq_vtime().
> It accepts any caller and refuses a task that is not enabled on the calling
> scheduler, except in ops.enable(), where @p is still SCX_TASK_READY. A
> write can race @p leaving and outlive the clear, and synchronizing such
> writes is left to the scheduler, as for dsq_vtime.
>
> 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 | 12 ++++++
> kernel/sched/ext/ext.c | 54 ++++++++++++++++++++++--
> tools/sched_ext/include/scx/compat.bpf.h | 24 +++++++++++
> 3 files changed, 86 insertions(+), 4 deletions(-)
>
> diff --git a/include/linux/sched/ext.h b/include/linux/sched/ext.h
> index f3564581c317..2c2ce144601d 100644
> --- a/include/linux/sched/ext.h
> +++ b/include/linux/sched/ext.h
> @@ -277,6 +277,18 @@ 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_data() while @p is enabled on the
> + * scheduler, from ops.enable() on, and cleared when @p leaves the
> + * scheduler's control, after ops.disable(). 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_data().
> + */
> + u64 bpf_data;
Should we rename this p->scx.bpf_priv, with scx_bpf_task_set_priv() and
scx_bpf_task_priv(), to make the scheduler-private ownership clearer?
> +
> /*
> * 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 cdceeffbcc65..bca27ced9d8d 100644
> --- a/kernel/sched/ext/ext.c
> +++ b/kernel/sched/ext/ext.c
> @@ -4749,16 +4749,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 data
> + * 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_data = 0;
> scx_task_slice_ended(rq, p);
> scx_set_task_slice(p, 0);
> p->scx.reenq_cnt = 0;
> @@ -10886,6 +10887,50 @@ __bpf_kfunc bool scx_bpf_task_set_lazy_resched(struct task_struct *p, bool lazy,
> return true;
> }
>
> +/**
> + * scx_bpf_task_set_data - Set the BPF data of a task
> + * @p: task of interest
> + * @data: opaque value to store in @p->scx.bpf_data
> + * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs
> + *
> + * Store @data in @p->scx.bpf_data, 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
> + * ops.disable(), 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 @data was stored, %false if @p is not on the calling
> + * scheduler or is neither enabled on it nor being enabled by ops.enable().
> + */
> +__bpf_kfunc bool scx_bpf_task_set_data(struct task_struct *p, u64 data,
> + const struct bpf_prog_aux *aux)
> +{
> + struct scx_sched *sch;
> + u32 state;
> +
> + guard(rcu)();
> + sch = scx_prog_sched(aux);
> + if (unlikely(!sch || !scx_task_on_sched(sch, p)))
> + return false;
> +
> + /*
> + * ops.enable() runs while @p is still %SCX_TASK_READY but already on the
> + * ext class. A %SCX_TASK_READY @p on another class is not enabled.
> + */
> + state = scx_get_task_state(p);
> + if (state != SCX_TASK_ENABLED &&
> + (state != SCX_TASK_READY || p->sched_class != &ext_sched_class))
> + return false;
> +
> + p->scx.bpf_data = data;
> + return true;
> +}
An ops.exit_task() callback can use this kfunc to repopulate bpf_data
after the kernel has cleared it. During sub-scheduler handoff, the task
is still READY/ext and associated with the outgoing scheduler, so this
check accepts the write. The incoming scheduler's ops.enable() then
receives the outgoing scheduler's value instead of zero. I reproduced
this without any concurrent writer.
Can we move the ENABLED transition into __scx_enable_task(), immediately
before the enable callback and fold away the wrapper? ops.enable()
can't fail, so this would let the check here become simply:
if (scx_get_task_state(p) != SCX_TASK_ENABLED)
return false;
I reproduced this inside virtme-ng and verified that this change fixes
it. Thoughts?
Thanks,
-Andrea
kernel/sched/ext/ext.c | 24 +++++++-----------------
1 file changed, 7 insertions(+), 17 deletions(-)
diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
index bca27ced9d8da..7ed1319c12dcf 100644
--- a/kernel/sched/ext/ext.c
+++ b/kernel/sched/ext/ext.c
@@ -4686,7 +4686,7 @@ int __scx_init_task(struct scx_sched *sch, struct task_struct *p,
return 0;
}
-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)
{
struct rq *rq = task_rq(p);
u32 weight;
@@ -4712,6 +4712,9 @@ static void __scx_enable_task(struct scx_sched *sch, struct task_struct *p)
p->scx.weight = sched_weight_to_cgroup(weight);
p->scx.lazy_resched = sch->ops.flags & SCX_OPS_LAZY_RESCHED;
+ /* Task kfuncs must recognize @p as enabled from ops.enable() on. */
+ scx_set_task_state(p, SCX_TASK_ENABLED);
+
if (SCX_HAS_OP(sch, enable)) {
if (scx_is_cid_type()) {
struct scx_cmask *cmask = scx_fill_cmask_scratch(sch, p->cpus_ptr);
@@ -4738,12 +4741,6 @@ static void __scx_enable_task(struct scx_sched *sch, struct task_struct *p)
SCX_CALL_OP_TASK(sch, set_weight, rq, p, p->scx.weight);
}
-void scx_enable_task(struct scx_sched *sch, struct task_struct *p)
-{
- __scx_enable_task(sch, p);
- scx_set_task_state(p, SCX_TASK_ENABLED);
-}
-
/**
* scx_reset_task_fields - Reset what @p carries from its sched's control
* @rq: @p's rq, locked
@@ -10904,27 +10901,20 @@ __bpf_kfunc bool scx_bpf_task_set_lazy_resched(struct task_struct *p, bool lazy,
* scx_bpf_task_set_dsq_vtime(). Synchronizing such writes is the BPF
* scheduler's responsibility.
*
- * Return %true if @data was stored, %false if @p is not on the calling
- * scheduler or is neither enabled on it nor being enabled by ops.enable().
+ * Return %true if @data was stored, %false if @p is not enabled on the calling
+ * scheduler.
*/
__bpf_kfunc bool scx_bpf_task_set_data(struct task_struct *p, u64 data,
const struct bpf_prog_aux *aux)
{
struct scx_sched *sch;
- u32 state;
guard(rcu)();
sch = scx_prog_sched(aux);
if (unlikely(!sch || !scx_task_on_sched(sch, p)))
return false;
- /*
- * ops.enable() runs while @p is still %SCX_TASK_READY but already on the
- * ext class. A %SCX_TASK_READY @p on another class is not enabled.
- */
- state = scx_get_task_state(p);
- if (state != SCX_TASK_ENABLED &&
- (state != SCX_TASK_READY || p->sched_class != &ext_sched_class))
+ if (scx_get_task_state(p) != SCX_TASK_ENABLED)
return false;
p->scx.bpf_data = data;
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH 3/5] sched_ext: Add p->scx.bpf_data for O(1) task context lookup
2026-12-01 7:48 ` Andrea Righi
@ 2026-12-01 11:04 ` Changwoo Min
0 siblings, 0 replies; 8+ messages in thread
From: Changwoo Min @ 2026-12-01 11:04 UTC (permalink / raw)
To: Andrea Righi
Cc: tj, void, david.dai, Gavin Guo, kernel-dev, sched-ext, linux-kernel
Thanks Andrea for the review!
On 12/1/26 4:48 PM, Andrea Righi wrote:
> Hi Changwoo,
>
> thanks for working at this! Then I can finally remove the ugly
> p->scx.dsq_vtime hack. :)
>
> Comments below.
>
> On Tue, Dec 01, 2026 at 02:48:48PM +0900, Changwoo Min wrote:
>> 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_data, that the task's scheduler sets with
>> scx_bpf_task_set_data(), 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 after ops.disable(), which covers @p
>> leaving the ext class, moving to another sub-scheduler and exiting.
>> ops.enable() runs again whenever @p comes back.
>>
>> scx_bpf_task_set_data() follows the rules of scx_bpf_task_set_dsq_vtime().
>> It accepts any caller and refuses a task that is not enabled on the calling
>> scheduler, except in ops.enable(), where @p is still SCX_TASK_READY. A
>> write can race @p leaving and outlive the clear, and synchronizing such
>> writes is left to the scheduler, as for dsq_vtime.
>>
>> 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 | 12 ++++++
>> kernel/sched/ext/ext.c | 54 ++++++++++++++++++++++--
>> tools/sched_ext/include/scx/compat.bpf.h | 24 +++++++++++
>> 3 files changed, 86 insertions(+), 4 deletions(-)
>>
>> diff --git a/include/linux/sched/ext.h b/include/linux/sched/ext.h
>> index f3564581c317..2c2ce144601d 100644
>> --- a/include/linux/sched/ext.h
>> +++ b/include/linux/sched/ext.h
>> @@ -277,6 +277,18 @@ 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_data() while @p is enabled on the
>> + * scheduler, from ops.enable() on, and cleared when @p leaves the
>> + * scheduler's control, after ops.disable(). 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_data().
>> + */
>> + u64 bpf_data;
>
> Should we rename this p->scx.bpf_priv, with scx_bpf_task_set_priv() and
> scx_bpf_task_priv(), to make the scheduler-private ownership clearer?
bpf_priv more clearly expresses the intent. I will rename it bpf_priv,
scx_bpf_task_set_priv() and scx_bpf_task_priv() as you suggested.
>
>> +
>> /*
>> * 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 cdceeffbcc65..bca27ced9d8d 100644
>> --- a/kernel/sched/ext/ext.c
>> +++ b/kernel/sched/ext/ext.c
>> @@ -4749,16 +4749,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 data
>> + * 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_data = 0;
>> scx_task_slice_ended(rq, p);
>> scx_set_task_slice(p, 0);
>> p->scx.reenq_cnt = 0;
>> @@ -10886,6 +10887,50 @@ __bpf_kfunc bool scx_bpf_task_set_lazy_resched(struct task_struct *p, bool lazy,
>> return true;
>> }
>>
>> +/**
>> + * scx_bpf_task_set_data - Set the BPF data of a task
>> + * @p: task of interest
>> + * @data: opaque value to store in @p->scx.bpf_data
>> + * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs
>> + *
>> + * Store @data in @p->scx.bpf_data, 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
>> + * ops.disable(), 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 @data was stored, %false if @p is not on the calling
>> + * scheduler or is neither enabled on it nor being enabled by ops.enable().
>> + */
>> +__bpf_kfunc bool scx_bpf_task_set_data(struct task_struct *p, u64 data,
>> + const struct bpf_prog_aux *aux)
>> +{
>> + struct scx_sched *sch;
>> + u32 state;
>> +
>> + guard(rcu)();
>> + sch = scx_prog_sched(aux);
>> + if (unlikely(!sch || !scx_task_on_sched(sch, p)))
>> + return false;
>> +
>> + /*
>> + * ops.enable() runs while @p is still %SCX_TASK_READY but already on the
>> + * ext class. A %SCX_TASK_READY @p on another class is not enabled.
>> + */
>> + state = scx_get_task_state(p);
>> + if (state != SCX_TASK_ENABLED &&
>> + (state != SCX_TASK_READY || p->sched_class != &ext_sched_class))
>> + return false;
>> +
>> + p->scx.bpf_data = data;
>> + return true;
>> +}
>
> An ops.exit_task() callback can use this kfunc to repopulate bpf_data
> after the kernel has cleared it. During sub-scheduler handoff, the task
> is still READY/ext and associated with the outgoing scheduler, so this
> check accepts the write. The incoming scheduler's ops.enable() then
> receives the outgoing scheduler's value instead of zero. I reproduced
> this without any concurrent writer.
You are right. I didn't consider this path. I think we should call
scx_reset_task_fields() *after* ops.exit_task() to prevent leaking
bpf_priv (and dsq_vtime and slice) to the other scx scheduler.
>
> Can we move the ENABLED transition into __scx_enable_task(), immediately
> before the enable callback and fold away the wrapper? ops.enable()
> can't fail, so this would let the check here become simply:
>
> if (scx_get_task_state(p) != SCX_TASK_ENABLED)
> return false;
>
> I reproduced this inside virtme-ng and verified that this change fixes
> it. Thoughts?
I agree. This is cleaner. I will change it as you suggested.
Thanks!
Changwoo Min
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH 4/5] sched_ext: scx_qmap: Cache task_ctx in p->scx.bpf_data
2026-12-01 5:48 [PATCH 0/5] sched_ext: Add p->scx.bpf_data for O(1) task context lookup Changwoo Min
` (2 preceding siblings ...)
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 5:48 ` Changwoo Min
2026-12-01 5:48 ` [PATCH 5/5] selftests/sched_ext: Test p->scx.bpf_data across class changes Changwoo Min
4 siblings, 0 replies; 8+ messages in thread
From: Changwoo Min @ 2026-12-01 5:48 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_data. 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..079082711f69 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_data.
*
* 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_data 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_data
+ * 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_data, 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 data = READ_ONCE(p->scx.bpf_data);
QMAP_TOUCH_ARENA();
+ if (key && (u32)(data >> 32) == (u32)key)
+ return (task_ctx_t *)((key & ~0xffffffffULL) | (u32)data);
+
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_data(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] 8+ messages in thread* [PATCH 5/5] selftests/sched_ext: Test p->scx.bpf_data across class changes
2026-12-01 5:48 [PATCH 0/5] sched_ext: Add p->scx.bpf_data for O(1) task context lookup Changwoo Min
` (3 preceding siblings ...)
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
4 siblings, 0 replies; 8+ messages in thread
From: Changwoo Min @ 2026-12-01 5:48 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_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
^ permalink raw reply [flat|nested] 8+ messages in thread