mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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 3/5] sched_ext: Add p->scx.bpf_data for O(1) task context lookup
Date: Tue, 1 Dec 2026 08:48:08 +0100	[thread overview]
Message-ID: <aw57uMvkiKrLBBBI@gpd4> (raw)
In-Reply-To: <20261201054850.780664-4-changwoo@igalia.com>

Hi Changwoo,

thanks for working at this! Then I can finally remove the ugly
p->scx.dsq_vtime hack. :)

Comments below.

On Tue, Dec 01, 2026 at 02:48:48PM +0900, Changwoo Min wrote:
> BPF schedulers keep their per-task context in task local storage, or in
> an arena allocation reached through it. Each lookup costs a
> bpf_task_storage_get() call.
> 
> Add an opaque u64, p->scx.bpf_data, that the task's scheduler sets with
> scx_bpf_task_set_data(), typically to an arena pointer to its context.
> Any BPF program reads it with a plain load. The kernel never dereferences
> it.
> 
> The value is valid while @p is enabled on the scheduler that set it.
> scx_reset_task_fields() clears it after ops.disable(), which covers @p
> leaving the ext class, moving to another sub-scheduler and exiting.
> ops.enable() runs again whenever @p comes back.
> 
> scx_bpf_task_set_data() follows the rules of scx_bpf_task_set_dsq_vtime().
> It accepts any caller and refuses a task that is not enabled on the calling
> scheduler, except in ops.enable(), where @p is still SCX_TASK_READY. A
> write can race @p leaving and outlive the clear, and synchronizing such
> writes is left to the scheduler, as for dsq_vtime.
> 
> The value means something only to the scheduler that set it. With
> sub-schedulers, a task a scheduler reaches as a waker or from a tracing
> program may belong to another one. A scheduler can tag the value with
> its scx_bpf_sched_id() to recognize its own.
> 
> Signed-off-by: Changwoo Min <changwoo@igalia.com>
> ---
>  include/linux/sched/ext.h                | 12 ++++++
>  kernel/sched/ext/ext.c                   | 54 ++++++++++++++++++++++--
>  tools/sched_ext/include/scx/compat.bpf.h | 24 +++++++++++
>  3 files changed, 86 insertions(+), 4 deletions(-)
> 
> diff --git a/include/linux/sched/ext.h b/include/linux/sched/ext.h
> index f3564581c317..2c2ce144601d 100644
> --- a/include/linux/sched/ext.h
> +++ b/include/linux/sched/ext.h
> @@ -277,6 +277,18 @@ struct sched_ext_entity {
>  	 */
>  	atomic64_t		slice_oob;
>  
> +	/*
> +	 * Opaque value owned by the task's scheduler, typically an arena
> +	 * pointer to its per-task context. The kernel never dereferences it.
> +	 * Set with scx_bpf_task_set_data() while @p is enabled on the
> +	 * scheduler, from ops.enable() on, and cleared when @p leaves the
> +	 * scheduler's control, after ops.disable(). Any BPF program can read
> +	 * it, so a tracing program can reach the context of the current task
> +	 * with a single load. A write from outside @p's own ops can outlive
> +	 * the clear, see scx_bpf_task_set_data().
> +	 */
> +	u64			bpf_data;

Should we rename this p->scx.bpf_priv, with scx_bpf_task_set_priv() and
scx_bpf_task_priv(), to make the scheduler-private ownership clearer?

> +
>  	/*
>  	 * Sub-sched cap rejected reenq context, valid only while
>  	 * %SCX_TASK_REENQ_CAP is set. @reenq_reason_caps is the SCX_CAP_* bits
> diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
> index cdceeffbcc65..bca27ced9d8d 100644
> --- a/kernel/sched/ext/ext.c
> +++ b/kernel/sched/ext/ext.c
> @@ -4749,16 +4749,17 @@ void scx_enable_task(struct scx_sched *sch, struct task_struct *p)
>   * @rq: @p's rq, locked
>   * @p: task leaving its sched's control
>   *
> - * The vtime, the slice and what rides on it, the reenqueue count and the
> - * watchdog wait belong to the sched while it controls @p. The control ends when
> - * @p is taken off the sched or leaves the ext class while staying on the sched.
> - * Reset them so that @p starts clean wherever it goes.
> + * The vtime, the slice and what rides on it, the reenqueue count, the BPF data
> + * and the watchdog wait belong to the sched while it controls @p. The control
> + * ends when @p is taken off the sched or leaves the ext class while staying on
> + * the sched. Reset them so that @p starts clean wherever it goes.
>   */
>  static void scx_reset_task_fields(struct rq *rq, struct task_struct *p)
>  {
>  	lockdep_assert_rq_held(rq);
>  
>  	p->scx.dsq_vtime = 0;
> +	p->scx.bpf_data = 0;
>  	scx_task_slice_ended(rq, p);
>  	scx_set_task_slice(p, 0);
>  	p->scx.reenq_cnt = 0;
> @@ -10886,6 +10887,50 @@ __bpf_kfunc bool scx_bpf_task_set_lazy_resched(struct task_struct *p, bool lazy,
>  	return true;
>  }
>  
> +/**
> + * scx_bpf_task_set_data - Set the BPF data of a task
> + * @p: task of interest
> + * @data: opaque value to store in @p->scx.bpf_data
> + * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs
> + *
> + * Store @data in @p->scx.bpf_data, where any BPF program can read it with a
> + * plain load. The value is valid while @p is enabled on the calling scheduler.
> + * The kernel clears it when @p leaves the scheduler's control, after
> + * ops.disable(), and ops.enable() runs again when @p comes back.
> + *
> + * From @p's own ops, ops.enable() included, @p can't leave the scheduler
> + * concurrently. Elsewhere, e.g. from a timer or a tracing program, the write
> + * can race @p leaving and outlive the clear, as with
> + * scx_bpf_task_set_dsq_vtime(). Synchronizing such writes is the BPF
> + * scheduler's responsibility.
> + *
> + * Return %true if @data was stored, %false if @p is not on the calling
> + * scheduler or is neither enabled on it nor being enabled by ops.enable().
> + */
> +__bpf_kfunc bool scx_bpf_task_set_data(struct task_struct *p, u64 data,
> +				       const struct bpf_prog_aux *aux)
> +{
> +	struct scx_sched *sch;
> +	u32 state;
> +
> +	guard(rcu)();
> +	sch = scx_prog_sched(aux);
> +	if (unlikely(!sch || !scx_task_on_sched(sch, p)))
> +		return false;
> +
> +	/*
> +	 * ops.enable() runs while @p is still %SCX_TASK_READY but already on the
> +	 * ext class. A %SCX_TASK_READY @p on another class is not enabled.
> +	 */
> +	state = scx_get_task_state(p);
> +	if (state != SCX_TASK_ENABLED &&
> +	    (state != SCX_TASK_READY || p->sched_class != &ext_sched_class))
> +		return false;
> +
> +	p->scx.bpf_data = data;
> +	return true;
> +}

An ops.exit_task() callback can use this kfunc to repopulate bpf_data
after the kernel has cleared it. During sub-scheduler handoff, the task
is still READY/ext and associated with the outgoing scheduler, so this
check accepts the write. The incoming scheduler's ops.enable() then
receives the outgoing scheduler's value instead of zero. I reproduced
this without any concurrent writer.

Can we move the ENABLED transition into __scx_enable_task(), immediately
before the enable callback and fold away the wrapper? ops.enable()
can't fail, so this would let the check here become simply:

	if (scx_get_task_state(p) != SCX_TASK_ENABLED)
		return false;

I reproduced this inside virtme-ng and verified that this change fixes
it. Thoughts?

Thanks,
-Andrea

 kernel/sched/ext/ext.c | 24 +++++++-----------------
 1 file changed, 7 insertions(+), 17 deletions(-)

diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
index bca27ced9d8da..7ed1319c12dcf 100644
--- a/kernel/sched/ext/ext.c
+++ b/kernel/sched/ext/ext.c
@@ -4686,7 +4686,7 @@ int __scx_init_task(struct scx_sched *sch, struct task_struct *p,
 	return 0;
 }
 
-static void __scx_enable_task(struct scx_sched *sch, struct task_struct *p)
+void scx_enable_task(struct scx_sched *sch, struct task_struct *p)
 {
 	struct rq *rq = task_rq(p);
 	u32 weight;
@@ -4712,6 +4712,9 @@ static void __scx_enable_task(struct scx_sched *sch, struct task_struct *p)
 	p->scx.weight = sched_weight_to_cgroup(weight);
 	p->scx.lazy_resched = sch->ops.flags & SCX_OPS_LAZY_RESCHED;
 
+	/* Task kfuncs must recognize @p as enabled from ops.enable() on. */
+	scx_set_task_state(p, SCX_TASK_ENABLED);
+
 	if (SCX_HAS_OP(sch, enable)) {
 		if (scx_is_cid_type()) {
 			struct scx_cmask *cmask = scx_fill_cmask_scratch(sch, p->cpus_ptr);
@@ -4738,12 +4741,6 @@ static void __scx_enable_task(struct scx_sched *sch, struct task_struct *p)
 		SCX_CALL_OP_TASK(sch, set_weight, rq, p, p->scx.weight);
 }
 
-void scx_enable_task(struct scx_sched *sch, struct task_struct *p)
-{
-	__scx_enable_task(sch, p);
-	scx_set_task_state(p, SCX_TASK_ENABLED);
-}
-
 /**
  * scx_reset_task_fields - Reset what @p carries from its sched's control
  * @rq: @p's rq, locked
@@ -10904,27 +10901,20 @@ __bpf_kfunc bool scx_bpf_task_set_lazy_resched(struct task_struct *p, bool lazy,
  * scx_bpf_task_set_dsq_vtime(). Synchronizing such writes is the BPF
  * scheduler's responsibility.
  *
- * Return %true if @data was stored, %false if @p is not on the calling
- * scheduler or is neither enabled on it nor being enabled by ops.enable().
+ * Return %true if @data was stored, %false if @p is not enabled on the calling
+ * scheduler.
  */
 __bpf_kfunc bool scx_bpf_task_set_data(struct task_struct *p, u64 data,
 				       const struct bpf_prog_aux *aux)
 {
 	struct scx_sched *sch;
-	u32 state;
 
 	guard(rcu)();
 	sch = scx_prog_sched(aux);
 	if (unlikely(!sch || !scx_task_on_sched(sch, p)))
 		return false;
 
-	/*
-	 * ops.enable() runs while @p is still %SCX_TASK_READY but already on the
-	 * ext class. A %SCX_TASK_READY @p on another class is not enabled.
-	 */
-	state = scx_get_task_state(p);
-	if (state != SCX_TASK_ENABLED &&
-	    (state != SCX_TASK_READY || p->sched_class != &ext_sched_class))
+	if (scx_get_task_state(p) != SCX_TASK_ENABLED)
 		return false;
 
 	p->scx.bpf_data = data;

  reply	other threads:[~2026-12-01  7:48 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01  5:48 [PATCH 0/5] " Changwoo Min
2026-12-01  5:48 ` [PATCH 1/5] sched_ext: Add scx_bpf_sched_id() and scx_bpf_task_sched_id() Changwoo Min
2026-12-01  5:48 ` [PATCH 2/5] selftests/sched_ext: Test " Changwoo Min
2026-12-01  5:48 ` [PATCH 3/5] sched_ext: Add p->scx.bpf_data for O(1) task context lookup Changwoo Min
2026-12-01  7:48   ` Andrea Righi [this message]
2026-12-01 11:04     ` Changwoo Min
2026-12-01  5:48 ` [PATCH 4/5] sched_ext: scx_qmap: Cache task_ctx in p->scx.bpf_data Changwoo Min
2026-12-01  5:48 ` [PATCH 5/5] selftests/sched_ext: Test p->scx.bpf_data across class changes Changwoo Min

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=aw57uMvkiKrLBBBI@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®