From: Andrea Righi <arighi@nvidia.com>
To: Changwoo Min <changwoo@igalia.com>
Cc: tj@kernel.org, void@manifault.com, david.dai@linux.dev,
Gavin Guo <gavinguo@igalia.com>,
kernel-dev@igalia.com, sched-ext@lists.linux.dev,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3 3/6] sched_ext: Add p->scx.bpf_priv for O(1) task context lookup
Date: Sun, 6 Dec 2026 09:15:40 +0100 [thread overview]
Message-ID: <axUZhLr1U3X_tB5H@gpd4> (raw)
In-Reply-To: <20261206053728.2026272-4-changwoo@igalia.com>
Hi Changwoo,
On Sun, Dec 06, 2026 at 02:37:25PM +0900, Changwoo Min wrote:
> BPF schedulers keep their per-task context in task local storage, or in
> an arena allocation reached through it, and pay a bpf_task_storage_get()
> call per lookup.
>
> Add p->scx.bpf_priv, an opaque u64 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 value belongs to the scheduler that owns @p, from after
> ops.init_task() until ops.exit_task(). The kernel clears it after
> ops.exit_task(), so it never reaches the next scheduler. It isn't reset
> with the other task fields when @p leaves the ext class, as the
> scheduler still owns @p then.
>
> scx_bpf_task_set_priv() checks the ownership under @p's locks, so a
> write can't race @p's move to another scheduler. Like
> scx_bpf_cpuperf_set(), it works from an op called for @p, which holds
> one of those locks, or with no lock held, where it takes them. It fails
> from any other op, and tracing programs, which may run under any lock,
> can't call it. For lockless readers, the store is ordered after the
> stores that set up what the value points to.
>
> With sub-schedulers, a task that a scheduler reaches as a waker, from a
> tracing program or in ops.init_task() may belong to another one. A
> reader holding the task's locks compares scx_task_sched_id() with its
> scx_bpf_sched_id(). A lockless reader tags the value with its id
> instead, since only the owner stores and the kernel clears the value
> before the task moves.
>
> Signed-off-by: Changwoo Min <changwoo@igalia.com>
> ---
> include/linux/sched/ext.h | 11 +++
> kernel/sched/ext/ext.c | 89 ++++++++++++++++++++++++
> tools/sched_ext/include/scx/compat.bpf.h | 24 +++++++
> 3 files changed, 124 insertions(+)
>
> diff --git a/include/linux/sched/ext.h b/include/linux/sched/ext.h
> index f3564581c317..a1793ab5f0c3 100644
> --- a/include/linux/sched/ext.h
> +++ b/include/linux/sched/ext.h
> @@ -277,6 +277,17 @@ 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 on the scheduler,
> + * from after ops.init_task() until ops.exit_task(), and cleared
> + * after ops.exit_task(). Any BPF program can read it, so a tracing
> + * program can reach the context of the current task with a single
> + * load.
> + */
> + u64 bpf_priv;
As reported by sashiko, on 32-bit architectures a u64 store/load isn't
single-copy atomic, so a lockless reader could see a torn value. Arena
pointers don't exist on 32-bit anyway (arena is 64BIT && MMU only), so
maybe we can simply restrict bpf_priv to get/set 32-bit values there and
add a check like this in scx_bpf_task_set_priv():
if (!IS_ENABLED(CONFIG_64BIT) && unlikely(upper_32_bits(priv))) {
scx_error(sch, "bpf_priv 0x%llx exceeds 32 bits", priv);
return false;
}
Thoughts?
Thanks,
-Andrea
> +
> /*
> * 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 343d82f328ef..4f575a19df76 100644
> --- a/kernel/sched/ext/ext.c
> +++ b/kernel/sched/ext/ext.c
> @@ -4833,6 +4833,12 @@ void __scx_disable_and_exit_task(struct scx_sched *sch, struct task_struct *p)
> * ops.exit_task() can still write them while @p is on @sch.
> */
> scx_reset_task_fields(task_rq(p), p);
> +
> + /*
> + * Not in scx_reset_task_fields(), which also runs when @p leaves the
> + * ext class. The value stays until @p leaves @sch.
> + */
> + WRITE_ONCE(p->scx.bpf_priv, 0);
> }
>
> /*
> @@ -10912,6 +10918,84 @@ __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 calling scheduler can set it while @p is on it, from after
> + * ops.init_task() until ops.exit_task(), whatever @p's class. The kernel
> + * clears it after ops.exit_task().
> + *
> + * @p may be on another scheduler, e.g. as a waker, in a tracing program or
> + * in ops.init_task(), and the value is then that scheduler's. So check the
> + * owner before using it. With @p's rq lock or pi_lock held, compare
> + * scx_task_sched_id() of @p, in compat.bpf.h, with scx_bpf_sched_id().
> + * Without them, @p may move in between, so tag the value with
> + * scx_bpf_sched_id() and check the tag.
> + *
> + * Can be called from an op called for @p, or with no rq lock held, e.g. from
> + * an unlocked op, a BPF timer or a syscall program, in which case @p's locks
> + * are taken. Fails from any other op, such as one called for another task.
> + * Tracing programs can't call it.
> + *
> + * Return %true if @priv was stored, %false if @p is not on the calling
> + * scheduler or the caller is an op not called for @p.
> + */
> +__bpf_kfunc bool scx_bpf_task_set_priv(struct task_struct *p, u64 priv,
> + const struct bpf_prog_aux *aux)
> +{
> + struct scx_sched *sch;
> + struct rq_flags rf;
> + struct rq *rq = NULL;
> + bool locked, ret = false;
> + u32 state;
> +
> + guard(rcu)();
> + sch = scx_prog_sched(aux);
> + if (unlikely(!sch || !scx_kf_allowed_ctx(sch)))
> + return false;
> +
> + /* Fast path, rechecked under @p's locks below */
> + if (!scx_task_on_sched(sch, p))
> + return false;
> +
> + /*
> + * @p's owner and state change only under both @p's pi_lock and rq lock.
> + * An op called for @p holds one of them, the rq lock or, for
> + * ops.select_cpu(), the pi_lock. Any other lock doesn't pin @p, and
> + * taking @p's locks under it can deadlock, so restrict the call to @p's
> + * own op when a lock is held.
> + */
> + locked = p == current->scx.kf_tasks[0] || p == current->scx.kf_tasks[1];
> + if (!locked && (scx_locked_rq() || current->scx.kf_tasks[0]))
> + return false;
> +
> + /* If no lock is held, pin @p by taking its locks */
> + if (!locked)
> + rq = task_rq_lock(p, &rf);
> +
> + /* The sched owns @p from after ops.init_task() until ops.exit_task() */
> + state = scx_get_task_state(p);
> + if (scx_task_on_sched(sch, p) &&
> + (state == SCX_TASK_INIT || state == SCX_TASK_READY ||
> + state == SCX_TASK_ENABLED)) {
> + /*
> + * Lockless readers may dereference @priv. Order the stores that
> + * set up what @priv points to before the store that publishes it.
> + */
> + smp_wmb();
> + WRITE_ONCE(p->scx.bpf_priv, priv);
> + ret = true;
> + }
> +
> + if (!locked)
> + task_rq_unlock(rq, p, &rf);
> + return ret;
> +}
> +
> void scx_kick_cpu(struct scx_sched *sch, s32 cpu, u64 flags)
> {
> struct scx_sched_pcpu *pcpu;
> @@ -12267,6 +12351,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)
> @@ -12330,6 +12415,9 @@ static const struct btf_kfunc_id_set scx_kfunc_set_any = {
> * when scx_locked_rq() returns NULL. Taking it while the context already holds
> * an rq lock, the same one or another cpu's, can deadlock.
> *
> + * scx_bpf_task_set_priv() takes the task's pi_lock and rq lock outside an op
> + * called for the task, with the same deadlock.
> + *
> * scx_bpf_destroy_dsq() takes a user DSQ lock. So do
> * bpf_iter_scx_dsq_next() and bpf_iter_scx_dsq_destroy(), the latter only
> * after a successful next() has linked the cursor. A tracing program can
> @@ -12347,6 +12435,7 @@ static const struct btf_kfunc_id_set scx_kfunc_set_any = {
> BTF_KFUNCS_START(scx_kfunc_ids_no_tracing)
> BTF_ID_FLAGS(func, scx_bpf_cpuperf_set, KF_IMPLICIT_ARGS)
> BTF_ID_FLAGS(func, scx_bpf_cidperf_set, KF_IMPLICIT_ARGS)
> +BTF_ID_FLAGS(func, scx_bpf_task_set_priv, KF_IMPLICIT_ARGS | KF_RCU)
> BTF_ID_FLAGS(func, scx_bpf_sub_grant, KF_IMPLICIT_ARGS)
> BTF_ID_FLAGS(func, scx_bpf_sub_revoke, KF_IMPLICIT_ARGS)
> BTF_ID_FLAGS(func, scx_bpf_destroy_dsq, KF_IMPLICIT_ARGS)
> diff --git a/tools/sched_ext/include/scx/compat.bpf.h b/tools/sched_ext/include/scx/compat.bpf.h
> index 4d2298bd7ac5..e8cb5f35eed0 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.5: 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.5: Add scx_bpf_sched_id(). Ids start at 1, increase monotonically and are
> * never reused. 0 means no scheduler, including on kernels without the kfunc.
> --
> 2.56.1
>
next prev parent reply other threads:[~2026-12-06 8:15 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-06 5:37 [PATCH v3 0/6] " Changwoo Min
2026-12-06 5:37 ` [PATCH v3 1/6] sched_ext: Add scx_bpf_sched_id() and scx_task_sched_id() Changwoo Min
2026-12-06 10:54 ` Andrea Righi
2026-12-06 5:37 ` [PATCH v3 2/6] selftests/sched_ext: Test " Changwoo Min
2026-12-06 5:37 ` [PATCH v3 3/6] sched_ext: Add p->scx.bpf_priv for O(1) task context lookup Changwoo Min
2026-12-06 8:15 ` Andrea Righi [this message]
2026-12-06 5:37 ` [PATCH v3 4/6] sched_ext: scx_qmap: Cache task_ctx in p->scx.bpf_priv Changwoo Min
2026-12-06 5:37 ` [PATCH v3 5/6] selftests/sched_ext: Test p->scx.bpf_priv across class changes Changwoo Min
2026-12-06 5:37 ` [PATCH v3 6/6] selftests/sched_ext: Test task field reset across sub-scheduler handoffs Changwoo Min
2026-12-06 10:57 ` [PATCH v3 0/6] sched_ext: Add p->scx.bpf_priv for O(1) task context lookup Andrea Righi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=axUZhLr1U3X_tB5H@gpd4 \
--to=arighi@nvidia.com \
--cc=changwoo@igalia.com \
--cc=david.dai@linux.dev \
--cc=gavinguo@igalia.com \
--cc=kernel-dev@igalia.com \
--cc=linux-kernel@vger.kernel.org \
--cc=sched-ext@lists.linux.dev \
--cc=tj@kernel.org \
--cc=void@manifault.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®