* [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-04 6:10 ` Andrea Righi
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, 1 reply; 18+ 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 related [flat|nested] 18+ messages in thread* Re: [PATCH v2 1/8] sched_ext: Reset task fields after ops.exit_task()
2026-12-03 4:56 ` [PATCH v2 1/8] sched_ext: Reset task fields after ops.exit_task() Changwoo Min
@ 2026-12-04 6:10 ` Andrea Righi
0 siblings, 0 replies; 18+ messages in thread
From: Andrea Righi @ 2026-12-04 6:10 UTC (permalink / raw)
To: Changwoo Min
Cc: tj, void, david.dai, Gavin Guo, kernel-dev, sched-ext,
linux-kernel
On Thu, Dec 03, 2026 at 01:56:11PM +0900, Changwoo Min wrote:
> __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>
Reviewed-by: Andrea Righi <arighi@nvidia.com>
Thanks,
-Andrea
> ---
> 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] 18+ 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-04 6:10 ` Andrea Righi
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, 1 reply; 18+ 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 related [flat|nested] 18+ messages in thread* Re: [PATCH v2 2/8] sched_ext: Mark a task enabled before ops.enable()
2026-12-03 4:56 ` [PATCH v2 2/8] sched_ext: Mark a task enabled before ops.enable() Changwoo Min
@ 2026-12-04 6:10 ` Andrea Righi
0 siblings, 0 replies; 18+ messages in thread
From: Andrea Righi @ 2026-12-04 6:10 UTC (permalink / raw)
To: Changwoo Min
Cc: tj, void, david.dai, Gavin Guo, kernel-dev, sched-ext,
linux-kernel
On Thu, Dec 03, 2026 at 01:56:12PM +0900, Changwoo Min wrote:
> 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>
Reviewed-by: Andrea Righi <arighi@nvidia.com>
Thanks,
-Andrea
> ---
> 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] 18+ 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 5:14 ` sashiko-bot
2026-12-04 6:44 ` Andrea Righi
2026-12-03 4:56 ` [PATCH v2 4/8] selftests/sched_ext: Test " Changwoo Min
` (4 subsequent siblings)
7 siblings, 2 replies; 18+ 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 related [flat|nested] 18+ messages in thread* Re: [PATCH v2 3/8] sched_ext: Add scx_bpf_sched_id() and scx_bpf_task_sched_id()
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 5:14 ` sashiko-bot
2026-12-04 6:44 ` Andrea Righi
1 sibling, 0 replies; 18+ messages in thread
From: sashiko-bot @ 2026-12-03 5:14 UTC (permalink / raw)
To: Changwoo Min; +Cc: sched-ext
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] sched_ext: scx_bpf_task_sched_id() ignores task when
CONFIG_EXT_SUB_SCHED=n
- [Low] sched_ext: scx_bpf_task_sched_id() omits const on task_struct
pointer
--
commit d4e2b84786b2aa7b0554a97e35d420c7934a3861
Author: Changwoo Min <changwoo@igalia.com>
sched_ext: Add scx_bpf_sched_id() and scx_bpf_task_sched_id()
This commit adds the scx_bpf_sched_id() and scx_bpf_task_sched_id() kfuncs
and their compat wrappers to let BPF programs query a scheduler's instance
id and the id of the scheduler owning a given task.
> diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
> index 7e5f58f1d8447..2ebc67916757a 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_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)
[Severity: Low]
Should the task_struct pointer parameter here (and in the ksym declaration
and wrapper in tools/sched_ext/include/scx/compat.bpf.h) be const?
scx_bpf_task_sched_id() only passes p to scx_task_sched_rcu(), and its
adjacent read-only task kfuncs (scx_bpf_task_running(), scx_bpf_task_cpu(),
scx_bpf_task_cid()) all take const struct task_struct *p. Omitting const
forces BPF callers with a const pointer to cast it away.
> +{
> + struct scx_sched *sch;
> +
> + guard(rcu)();
> + sch = scx_task_sched_rcu(p);
> + return sch ? sch->id : 0;
> +}
[Severity: Medium]
Does scx_bpf_task_sched_id() return 0 for unowned tasks when
CONFIG_EXT_SUB_SCHED is disabled?
When CONFIG_CGROUPS=n, CONFIG_EXT_SUB_SCHED is disabled and
scx_task_sched_rcu(p) in kernel/sched/ext/internal.h ignores p and returns
rcu_dereference_all(scx_root) directly. That makes scx_bpf_task_sched_id()
return scx_root->id for tasks not on any scheduler, such as idle tasks.
In scx_root_disable(), scx_root also stays non-NULL after
WRITE_ONCE(sch->dead, true) until RCU_INIT_POINTER(scx_root, NULL), so
scx_bpf_task_sched_id() keeps returning the dead scheduler's id even after
scx_bpf_sched_id() returns 0.
As a result, BPF callers checking scx_bpf_task_sched_id(p) will misclassify
unowned tasks. Checking scx_get_task_state(p) and !READ_ONCE(sch->dead) on
the !CONFIG_EXT_SUB_SCHED path would preserve the documented contract.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203045618.982539-1-changwoo@igalia.com?part=3
^ permalink raw reply [flat|nested] 18+ messages in thread* Re: [PATCH v2 3/8] sched_ext: Add scx_bpf_sched_id() and scx_bpf_task_sched_id()
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 5:14 ` sashiko-bot
@ 2026-12-04 6:44 ` Andrea Righi
1 sibling, 0 replies; 18+ messages in thread
From: Andrea Righi @ 2026-12-04 6:44 UTC (permalink / raw)
To: Changwoo Min
Cc: tj, void, david.dai, Gavin Guo, kernel-dev, sched-ext,
linux-kernel
Hi Changwoo,
On Thu, Dec 03, 2026 at 01:56:13PM +0900, Changwoo Min wrote:
> 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;
> +}
Instead of introducing this as a kfunc, can we provide a compat.bpf.h
helper like this?
struct scx_sched___id {
u64 id;
} __attribute__((preserve_access_index));
struct sched_ext_entity___sched {
struct scx_sched___id *sched;
} __attribute__((preserve_access_index));
static __always_inline u64 scx_task_sched_id(struct task_struct *p)
{
struct sched_ext_entity___sched *scx = (void *)&p->scx;
struct scx_sched___id *sch;
if (!bpf_core_field_exists(scx->sched) ||
!bpf_core_field_exists(struct scx_sched___id, id))
return 0;
sch = READ_ONCE(scx->sched);
return sch ? READ_ONCE(sch->id) : 0;
}
Thanks,
-Andrea
> +
> /**
> * 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] 18+ 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 5:14 ` sashiko-bot
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, 1 reply; 18+ 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 related [flat|nested] 18+ messages in thread* Re: [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 4/8] selftests/sched_ext: Test " Changwoo Min
@ 2026-12-03 5:14 ` sashiko-bot
0 siblings, 0 replies; 18+ messages in thread
From: sashiko-bot @ 2026-12-03 5:14 UTC (permalink / raw)
To: Changwoo Min; +Cc: sched-ext
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Low] selftests/sched_ext: leak BPF skeleton and FDs on error in
load_and_check()
- [Low] selftests/sched_ext: unassociated probe() bypasses ops->priv in
scx_prog_sched()
- [Low] selftests/sched_ext: SCX_EQ() evaluates load_and_check() twice
on failure
- [Low] selftests/sched_ext: sched_id passes getpid() to
bpf_task_from_pid()
--
commit 9708b30d3e6e6f4f027faf04578e789bbcd68ef1
Author: Changwoo Min <changwoo@igalia.com>
selftests/sched_ext: Test scx_bpf_sched_id() and scx_bpf_task_sched_id()
This commit adds the sched_id sched_ext selftest to verify
scx_bpf_sched_id() and scx_bpf_task_sched_id() across scheduler load,
unload, and reload.
> 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 0000000000000..8828c3ecec3e4
> --- /dev/null
> +++ b/tools/testing/selftests/sched_ext/sched_id.bpf.c
> @@ -0,0 +1,47 @@
[ ... ]
> +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;
> +}
[Severity: Low]
Can bpf_task_from_pid(target_pid) fail or inspect the wrong task when the
test runs in a non-root PID namespace?
load_and_check() stores getpid() in target_pid, which is relative to the
caller's PID namespace, while bpf_task_from_pid() resolves PIDs in
init_pid_ns. Since probe() runs in the calling task's context via
bpf_prog_test_run_opts(), using bpf_get_current_task_btf() avoids the
namespace mismatch.
> 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 0000000000000..a926062e13c76
> --- /dev/null
> +++ b/tools/testing/selftests/sched_ext/sched_id.c
> @@ -0,0 +1,103 @@
[ ... ]
> +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");
[Severity: Low]
Does calling sched_id__load() without associating skel->progs.probe with
skel->maps.sched_id_ops bypass the primary scx_prog_sched() path?
Without bpf_program__assoc_struct_ops(), bpf_prog_get_assoc_struct_ops()
returns NULL in scx_prog_sched(), so scx_bpf_sched_id() only passes via
the legacy v6.19 scx_root fallback instead of testing ops->priv.
Associating skel->progs.probe with skel->maps.sched_id_ops before loading
would exercise the primary lookup directly.
> + 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");
[Severity: Low]
Do the SCX_FAIL_IF() checks on sched_id__load() and
bpf_map__attach_struct_ops() leak skel?
SCX_FAIL_IF() returns SCX_TEST_FAIL immediately and bypasses the out
cleanup label, skipping sched_id__destroy(skel). Because runner.c runs
all selftests in one process, the skeleton allocations and loaded BPF
file descriptors stay leaked for subsequent tests. Initializing link to
NULL and jumping to cleanup avoids this.
[ ... ]
> +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);
[Severity: Low]
Will SCX_EQ() invoke load_and_check() twice when it fails?
SCX_EQ(_x, _y) expands _x both in the comparison and in the error format
arguments, so a failing load_and_check() runs a second full
load/attach/unload cycle, duplicates error output, and reports the second
call's return value instead of the first. Storing the return value in a
local variable before passing it to SCX_EQ() avoids the double evaluation.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203045618.982539-1-changwoo@igalia.com?part=4
^ permalink raw reply [flat|nested] 18+ 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 5:20 ` sashiko-bot
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, 1 reply; 18+ 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 related [flat|nested] 18+ messages in thread* Re: [PATCH v2 5/8] sched_ext: Add p->scx.bpf_priv for O(1) task context lookup
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 5:20 ` sashiko-bot
0 siblings, 0 replies; 18+ messages in thread
From: sashiko-bot @ 2026-12-03 5:20 UTC (permalink / raw)
To: Changwoo Min; +Cc: sched-ext
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] sched_ext: missing WRITE_ONCE() and release ordering on
p->scx.bpf_priv
- [Medium] sched_ext: lockless unmarked read of p->scx.flags in
scx_bpf_task_set_priv()
- [Medium] sched_ext: switched_from_scx() clears bpf_priv before
ops.exit_task()
--
commit 1bbafecc216dad15995f74ded039783962b3186c
Author: Changwoo Min <changwoo@igalia.com>
sched_ext: Add p->scx.bpf_priv for O(1) task context lookup
This commit adds an opaque u64 field p->scx.bpf_priv and the
scx_bpf_task_set_priv() kfunc so BPF schedulers can store and read
per-task context pointers without a task local storage lookup.
> diff --git a/include/linux/sched/ext.h b/include/linux/sched/ext.h
> index f3564581c317c..e63bd789c7d89 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
[Severity: Medium]
Is p->scx.bpf_priv actually cleared after the last op the scheduler runs for
@p?
On root scheduler teardown, scx_root_disable() switches each task out of
ext_sched_class via sched_change_begin() -> switched_from_scx() before
calling scx_disable_and_exit_task(). switched_from_scx() runs
scx_disable_task() and then immediately calls scx_reset_task_fields(),
clearing p->scx.bpf_priv to 0 before ops.exit_task() runs. The same happens
when a task leaves ext_sched_class via sched_setscheduler() and later exits.
By contrast, sched_ext_dead() and sub-scheduler handoffs keep p->sched_class
unchanged, so ops.exit_task() sees the non-zero value before
__scx_disable_and_exit_task() resets it. A scheduler relying on this
comment (and the matching kernel-doc on scx_bpf_task_set_priv()) to read
p->scx.bpf_priv in ops.exit_task() for cleanup will see 0 on root unload and
class switches. Should the documentation scope validity to ops.disable(),
or should the reset timing be aligned with ops.exit_task()?
> diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
> index 2ebc67916757a..0877341440ad6 100644
> --- a/kernel/sched/ext/ext.c
> +++ b/kernel/sched/ext/ext.c
[ ... ]
> @@ -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;
[Severity: Medium]
Is calling scx_get_task_state(p) without the rq lock safe against concurrent
updates to p->scx.flags?
scx_bpf_task_set_priv() is registered in scx_kfunc_ids_any and holds only
guard(rcu)(), while p->scx.flags is protected by the rq lock and modified
with unmarked bitwise operations across scheduling paths. In particular,
scx_set_task_state() updates the state bits in two separate plain stores:
kernel/sched/ext/ext.c:scx_set_task_state() {
...
p->scx.flags &= ~SCX_TASK_STATE_MASK;
p->scx.flags |= state;
}
An unlocked caller such as a BPF timer or syscall program invoking
scx_bpf_task_set_priv() concurrently with scheduling activity on another CPU
triggers a KCSAN data race on p->scx.flags and can transiently observe
SCX_TASK_NONE between those two stores.
> +
> + p->scx.bpf_priv = priv;
> + return true;
> +}
[Severity: Medium]
Should the write to p->scx.bpf_priv use smp_store_release() here and
WRITE_ONCE() in scx_reset_task_fields()?
Lockless BPF readers load p->scx.bpf_priv via READ_ONCE(scx->bpf_priv) in
scx_bpf_task_priv(). When a scheduler initializes a per-task arena context
and publishes its pointer here, a plain store lacks release ordering on
weakly-ordered architectures such as arm64, so a reader on another CPU can
observe the pointer before the context initialization stores are visible.
The unmarked 64-bit stores here and in scx_reset_task_fields() also race
with lockless readers and concurrent unlocked scx_bpf_task_set_priv()
callers, allowing compiler store tearing and KCSAN data-race warnings
(compare WRITE_ONCE() in scx_bpf_task_set_lazy_resched() right above).
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203045618.982539-1-changwoo@igalia.com?part=5
^ permalink raw reply [flat|nested] 18+ 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 5:19 ` sashiko-bot
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, 1 reply; 18+ 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 related [flat|nested] 18+ messages in thread* Re: [PATCH v2 6/8] sched_ext: scx_qmap: Cache task_ctx in p->scx.bpf_priv
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 5:19 ` sashiko-bot
0 siblings, 0 replies; 18+ messages in thread
From: sashiko-bot @ 2026-12-03 5:19 UTC (permalink / raw)
To: Changwoo Min; +Cc: sched-ext
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] sched_ext: scx_qmap: stale bpf_priv after qmap_exit_task()
frees task_ctx
- [Low] sched_ext: scx_qmap docs cite qmap_dump_task() bypassing
lookup_task_ctx()
- [Low] sched_ext: scx_qmap: unchecked bpf_priv tag in lookup_task_ctx()
- [Low] sched_ext: unmarked stores to p->scx.bpf_priv race with lockless
READ_ONCE()
--
commit a0014bb43cd73d33b79569621b8c7bf640a6bf3e
Author: Changwoo Min <changwoo@igalia.com>
sched_ext: scx_qmap: Cache task_ctx in p->scx.bpf_priv
This commit caches each task's task_ctx arena pointer in p->scx.bpf_priv
from qmap_enable(), tagged with scx_bpf_sched_id(), so lookup_task_ctx()
can avoid a task storage lookup on the fast path.
> diff --git a/tools/sched_ext/scx_qmap.bpf.c b/tools/sched_ext/scx_qmap.bpf.c
> index 3951f1f6e3c6a..8fa6626d6598f 100644
> --- a/tools/sched_ext/scx_qmap.bpf.c
> +++ b/tools/sched_ext/scx_qmap.bpf.c
[ ... ]
> @@ -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:
[Severity: Low]
Does qmap_dump_task() actually use lookup_task_ctx()?
qmap_dump_task() still calls bpf_task_storage_get(&task_ctx_stor, ...)
directly and never reads p->scx.bpf_priv, while qmap_core_sched_before()
is the callback that calls lookup_task_ctx() on tasks that may belong to
sub-schedulers. Either qmap_dump_task() should be switched to
lookup_task_ctx(), or this comment and the commit message should cite
qmap_core_sched_before() instead.
[ ... ]
> 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);
[Severity: Low]
Can this lockless READ_ONCE(p->scx.bpf_priv) tear against the unmarked
stores in kernel/sched/ext/ext.c?
lookup_task_ctx() is called without p->pi_lock or task_rq(p) lock from
monitor_timerfn() -> scan_shared_dsq() (under RCU only, after
bpf_iter_scx_dsq_next() drops dsq->lock) and from qmap_dispatch().
Meanwhile, commit "sched_ext: Add p->scx.bpf_priv for O(1) task context
lookup" writes p->scx.bpf_priv with plain assignments in
scx_reset_task_fields() and scx_bpf_task_set_priv().
If store tearing splits the 64-bit write between the upper tag and lower
pointer half, lookup_task_ctx() can pair a matching tag with a zeroed or
stale lower 32 bits and reconstruct a corrupted arena pointer. Using
WRITE_ONCE() in scx_reset_task_fields() and scx_bpf_task_set_priv()
(matching scx_bpf_task_set_lazy_resched()) would pair properly with this
READ_ONCE().
>
> QMAP_TOUCH_ARENA();
>
> + if (key && (u32)(priv >> 32) == (u32)key)
> + return (task_ctx_t *)((key & ~0xffffffffULL) | (u32)priv);
[Severity: Low]
Can a non-qmap sub-scheduler collide with this upper-32-bit tag and make
lookup_task_ctx() fabricate an arbitrary arena pointer?
p->scx.bpf_priv is an opaque u64 with no kernel-enforced tagging format.
Under CONFIG_SCHED_CORE, scx_prio_less() calls qmap_core_sched_before()
on tasks belonging to arbitrary child sub-schedulers accepted by
qmap_sub_attach(). If a sub-scheduler stores a value whose upper 32 bits
match (1U << 31) | id, lookup_task_ctx() returns a bogus arena pointer
instead of NULL, causing qmap_core_sched_before() to read an unmapped or
unrelated arena offset rather than falling back to default ordering.
Checking scx_bpf_task_sched_id() or reading task_ctx_stor directly in
qmap_core_sched_before() would avoid misidentifying sub-scheduler tasks.
Similarly, qmap_enable() calls lookup_task_ctx(p) when initializing
p->scx.bpf_priv instead of reading task_ctx_stor directly, so a colliding
out-of-band scx_bpf_task_set_priv() write from a prior scheduler would be
re-cached rather than overwritten.
[ ... ]
> @@ -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);
> +}
[Severity: Medium]
Can lookup_task_ctx() return a freed and recycled task_ctx while p is
leaving the scheduler?
qmap_enable() populates p->scx.bpf_priv, but scx_qmap has no
ops.disable() callback to clear it. In __scx_disable_and_exit_task(),
qmap_exit_task() returns taskc to qa.task_free_head before
scx_reset_task_fields() zeroes p->scx.bpf_priv, and qmap_exit_task()
cannot clear the field itself because scx_bpf_task_set_priv() rejects
tasks that are no longer SCX_TASK_ENABLED:
CPU0 CPU1 CPU2
__scx_disable_and_exit_task(p)
scx_disable_task(p)
qmap_exit_task(p)
v->taskc = NULL
free taskc to slab
qmap_init_task(p2)
pop & reset taskc
monitor_timerfn()
scan_shared_dsq()
lookup_task_ctx(p)
<-- recycled taskc
scx_reset_task_fields(p)
p->scx.bpf_priv = 0
Because scan_shared_dsq() (called from monitor_timerfn() under RCU only,
after bpf_iter_scx_dsq_next() drops dsq->lock) and
update_core_sched_head_seq() (called from qmap_dispatch() without
task_rq(p) lock) do not serialize with __scx_disable_and_exit_task(),
they can read p2's recycled taskc fields and corrupt core-sched head
tracking or rescue/highpri accounting. Clearing p->scx.bpf_priv in an
ops.disable() callback before qmap_exit_task() frees taskc would close
this free-before-invalidate window.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203045618.982539-1-changwoo@igalia.com?part=6
^ permalink raw reply [flat|nested] 18+ 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 5:26 ` sashiko-bot
2026-12-03 4:56 ` [PATCH v2 8/8] selftests/sched_ext: Test task field reset across sub-scheduler handoffs Changwoo Min
7 siblings, 1 reply; 18+ 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 related [flat|nested] 18+ messages in thread* Re: [PATCH v2 7/8] selftests/sched_ext: Test p->scx.bpf_priv across class changes
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 5:26 ` sashiko-bot
0 siblings, 0 replies; 18+ messages in thread
From: sashiko-bot @ 2026-12-03 5:26 UTC (permalink / raw)
To: Changwoo Min; +Cc: sched-ext
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Low] selftests/sched_ext: leak BPF skeleton on load or attach failure
in run()
- [Low] selftests/sched_ext: SCHED_FIFO fails with EPERM under
RT_GROUP_SCHED
- [Low] selftests/sched_ext: SCX_EQ() double-evaluates side-effecting
calls
--
commit 2808488ab5be9ddd62f306578e0441c81a3df090
Author: Changwoo Min <changwoo@igalia.com>
selftests/sched_ext: Test p->scx.bpf_priv across class changes
This commit adds the task_bpf_priv sched_ext selftest to verify that
p->scx.bpf_priv is set on ops.enable(), cleared on class changes and
scheduler unload, and not inherited across fork.
> 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 0000000000000..7500fc5a4992f
> --- /dev/null
> +++ b/tools/testing/selftests/sched_ext/task_bpf_priv.c
> @@ -0,0 +1,147 @@
[ ... ]
> + /* Step 1: Switch this task to SCHED_EXT */
> + SCX_EQ(set_policy(SCHED_EXT, 0), 0);
[Severity: Low]
Can passing set_policy(), probe(), and waitpid() directly to SCX_EQ()
execute them twice on failure?
SCX_EQ() in scx_test.h expands both arguments a second time when
formatting the error message. If waitpid() fails with EINTR, the second
call can reap the child and print equal values in the failure log.
Storing the return value in a local variable avoids double evaluation.
[ ... ]
> + /* Step 3: Switch this task to SCHED_FIFO */
> + SCX_EQ(set_policy(SCHED_FIFO, 1), 0);
[Severity: Low]
Will switching to SCHED_FIFO fail with EPERM under CONFIG_RT_GROUP_SCHED?
In a non-root cgroup v2 group (such as a systemd slice or container),
__sched_setscheduler() rejects SCHED_FIFO with EPERM even for root when
rt_runtime is 0, causing a false test failure.
Because task_bpf_priv_ops uses SCX_OPS_SWITCH_PARTIAL, switching to
SCHED_OTHER exercises the same switched_from_scx() disable and reset
path without requiring RT group bandwidth.
[ ... ]
> +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;
> +}
[Severity: Low]
Does run() leak skel if task_bpf_priv__load() or
bpf_map__attach_struct_ops() fails?
SCX_FAIL_IF() returns SCX_TEST_FAIL directly, skipping
task_bpf_priv__destroy(skel). Since task_bpf_priv has no cleanup
callback and the runner executes all tests in one process, the skeleton
memory and loaded BPF file descriptors remain leaked for the rest of the
run.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203045618.982539-1-changwoo@igalia.com?part=7
^ permalink raw reply [flat|nested] 18+ 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
2026-12-03 5:32 ` sashiko-bot
7 siblings, 1 reply; 18+ 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 related [flat|nested] 18+ messages in thread* Re: [PATCH v2 8/8] selftests/sched_ext: Test task field reset across sub-scheduler handoffs
2026-12-03 4:56 ` [PATCH v2 8/8] selftests/sched_ext: Test task field reset across sub-scheduler handoffs Changwoo Min
@ 2026-12-03 5:32 ` sashiko-bot
0 siblings, 0 replies; 18+ messages in thread
From: sashiko-bot @ 2026-12-03 5:32 UTC (permalink / raw)
To: Changwoo Min; +Cc: sched-ext
Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Medium] selftests/sched_ext: unsynchronized child sleep triggers
SCX_EXIT_ERROR_REENQ
- [Medium] selftests/sched_ext: unchecked kfunc returns allow vacuous
sub_handoff passes
- [Medium] selftests/sched_ext: handoffs() never checks sub_skel UEI
liveness or exit
- [Low] selftests/sched_ext: sub_handoff.c duplicates get_cgroup_id()
and now_ns()
- [Low] selftests/sched_ext: sub_handoff_enable() increments nr_enable
too early
--
commit 606e70f4ac2fdd8ea9df9beb5122bf3ee7426a80
Author: Changwoo Min <changwoo@igalia.com>
selftests/sched_ext: Test task field reset across sub-scheduler handoffs
This commit adds the sub_handoff sched_ext selftest to verify that
p->scx.bpf_priv and p->scx.dsq_vtime are cleared when a task moves
between a root scheduler and a sub-scheduler, and that ops.exit_task()
cannot set bpf_priv.
> 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 0000000000000..d7ce3f4e150c6
> --- /dev/null
> +++ b/tools/testing/selftests/sched_ext/sub_handoff.bpf.c
[ ... ]
> +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);
> +}
[Severity: Low]
Should nr_enable be incremented at the end of sub_handoff_enable()
instead of before the stale-field checks?
wait_u64() polls nr_enable with __ATOMIC_ACQUIRE as the completion gate
before handoffs() asserts on nr_stale_priv and nr_stale_vtime. If a
concurrent observer such as a BPF link iterator holds a transient
reference to sub_link during bpf_link__destroy(), bpf_scx_unreg() runs
asynchronously when that reference is dropped:
CPU0 CPU1
handoffs()
bpf_link__destroy(*sub_link)
(deferred by extra link ref)
wait_u64(&root->nr_enable, 2)
bpf_link_put_deferred()
...
sub_handoff_enable()
nr_enable++ (now 2)
reads nr_stale_priv == 0 <-- stale check has not run yet
reads nr_stale_vtime == 0
if (p->scx.bpf_priv)
nr_stale_priv++
Userspace can then observe zero stale counts even when a regression
leaves bpf_priv or dsq_vtime uncleared. Moving the nr_enable increment
to the end of sub_handoff_enable() fixes the release-acquire ordering.
> +
> +/*
> + * @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);
> +}
[Severity: Medium]
Can the assertions in handoffs() pass vacuously if the setup writes fail
or ops.exit_task() never runs?
Both scx_bpf_task_set_priv() and scx_bpf_task_set_dsq_vtime() return a
bool, yet sub_handoff_enable() ignores the return of
scx_bpf_task_set_priv() and sub_handoff_exit_task() ignores the return
of scx_bpf_task_set_dsq_vtime().
sub_handoff_exit_task() also does not count invocations on target_pid.
If a regression causes either write to fail or skips ops.exit_task() on
handoff, the fields remain 0 and the zero-count assertions in handoffs()
still pass.
Tracking write failures and ops.exit_task() counts and asserting them in
handoffs() would ensure non-zero values were written before verifying
they were cleared.
> 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 0000000000000..18e20416c9110
> --- /dev/null
> +++ b/tools/testing/selftests/sched_ext/sub_handoff.c
[ ... ]
> +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;
> +}
[Severity: Low]
This isn't a bug, but get_cgroup_id() duplicates get_cgroup_id() in
sub_core_caps.c and cgroup_id() in cgroup_nr_cpus.c, and now_ns()
duplicates now_ns() in sub_core_caps.c. Could these helpers be moved to
the shared util.c and util.h instead?
[ ... ]
> + /* 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;
> +}
[Severity: Medium]
Should handoffs() also verify that sub_skel stayed alive until step 3
and exited cleanly with SCX_EXIT_UNREG?
If sub_skel aborts with a runtime error during step 2, the kernel
disables it and moves target_pid back to root_skel. root->nr_enable
still reaches 2 and all current assertions pass despite the error exit.
Checking SCX_ASSERT_ALIVE(ctx->sub_skel, uei) before
bpf_link__destroy(*sub_link) and EXIT_KIND(SCX_EXIT_UNREG) afterward
would catch unexpected sub_skel exits.
[ ... ]
> + /* 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);
[Severity: Medium]
Can the child still be runnable when step 2 attaches sub_link and
trigger SCX_EXIT_ERROR_REENQ on sub_skel?
run() enters handoffs() immediately after fork() without waiting for the
child to block in read(). Because sub_handoff_sub_attach() grants no
CPU capabilities to sub_skel and sub_handoff_enqueue() unconditionally
inserts into SCX_DSQ_LOCAL without SCX_ENQ_RESCUE, a runnable child on
sub_skel cannot be placed on a local DSQ:
CPU0 CPU1
run()
fork()
handoffs()
attach sub_link (child still runnable, not in read())
scx_sub_enable_workfn()
scx_bypass(sch, false)
sub_handoff_enqueue()
scx_bpf_dsq_insert(..., SCX_DSQ_LOCAL, ...)
__scx_resolve_local_dsq() -> reject_dsq
scx_reenq_reject() (repeats 256x) <-- SCX_EXIT_ERROR_REENQ
After SCX_REENQ_MAX_REPEAT rejections, scx_do_enqueue_task() aborts
sub_skel with SCX_EXIT_ERROR_REENQ and moves the target back to
root_skel before step 3 detaches it. Waiting for the child to sleep
before calling handoffs() (as in sub_core_caps.c) avoids this race.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203045618.982539-1-changwoo@igalia.com?part=8
^ permalink raw reply [flat|nested] 18+ messages in thread