Sched_ext development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Changwoo Min" <changwoo@igalia.com>
Cc: sched-ext@lists.linux.dev
Subject: Re: [PATCH v2 5/8] sched_ext: Add p->scx.bpf_priv for O(1) task context lookup
Date: Thu, 03 Dec 2026 05:20:24 +0000	[thread overview]
Message-ID: <sashiko-outbox-247188@kernel.org> (raw)
In-Reply-To: <20261203045618.982539-6-changwoo@igalia.com>

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

  reply	other threads:[~2026-12-03  5:20 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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-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
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
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
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
2026-12-03  5:20   ` sashiko-bot [this message]
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
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
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

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=sashiko-outbox-247188@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=changwoo@igalia.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=sched-ext@lists.linux.dev \
    /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