All of lore.kernel.org
 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 v3 1/6] sched_ext: Add scx_bpf_sched_id() and scx_task_sched_id()
Date: Sun, 06 Dec 2026 06:03:43 +0000	[thread overview]
Message-ID: <sashiko-outbox-252202@kernel.org> (raw)
In-Reply-To: <20261206053728.2026272-2-changwoo@igalia.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] sched_ext: TOCTOU race between flags and sched loads in
  scx_task_sched_id()
--

commit e70589ddaf2c9dcb29ade05fb3583c3b7983b075
Author: Changwoo Min <changwoo@igalia.com>

sched_ext: Add scx_bpf_sched_id() and scx_task_sched_id()

This commit adds the scx_bpf_sched_id() kfunc to return the calling
scheduler's instance id and the scx_task_sched_id() BPF helper to return
the id of the scheduler owning a task.  It also updates
scx_set_task_state() to publish task state bits with a single
WRITE_ONCE().

> diff --git a/tools/sched_ext/include/scx/compat.bpf.h b/tools/sched_ext/include/scx/compat.bpf.h
> index 3520a749f8ce6..4d2298bd7ac55 100644
> --- a/tools/sched_ext/include/scx/compat.bpf.h
> +++ b/tools/sched_ext/include/scx/compat.bpf.h
> @@ -487,6 +487,67 @@ static inline void scx_bpf_task_set_lazy_resched(struct task_struct *p, bool laz

[ ... ]

> +static __always_inline u64 scx_task_sched_id(const struct task_struct *p)
> +{
> +	struct sched_ext_entity___task_id *scx = (void *)&p->scx;
> +	struct scx_sched___task_id *sch;
> +	u32 state;
> +	u64 id;
> +
> +	if (!bpf_ksym_exists(scx_bpf_sched_id___new))
> +		return 0;
> +
> +	/* A sched owns @p only after ops.init_task() */
> +	state = READ_ONCE(scx->flags) &
> +		bpf_core_enum_value(enum scx_ent_flags, SCX_TASK_STATE_MASK);
> +	if (state == bpf_core_enum_value(enum scx_ent_flags, SCX_TASK_NONE) ||
> +	    state == bpf_core_enum_value(enum scx_ent_flags, SCX_TASK_INIT_BEGIN) ||
> +	    state == bpf_core_enum_value(enum scx_ent_flags, SCX_TASK_DEAD))
> +		return 0;
> +
> +	/*
> +	 * Without CONFIG_EXT_SUB_SCHED, which @cgrp comes with, the kernel
> +	 * doesn't set p->scx.sched, and the root is the only scheduler.
> +	 */
> +	if (!bpf_core_field_exists(struct scx_sched___task_id, cgrp))
> +		return scx_bpf_sched_id();
> +
> +	/* Sleepable programs aren't in an RCU read section */
> +	bpf_rcu_read_lock();
> +	sch = scx->sched;
> +	id = sch ? sch->id : 0;
> +	bpf_rcu_read_unlock();
> +	return id;
> +}

[Severity: Low]
Can scx_task_sched_id() return a scheduler id for a task whose
ops.init_task() never ran on that scheduler?

Without CONFIG_EXT_SUB_SCHED, scx_task_sched_id() reads scx->flags and
calls scx_bpf_sched_id() outside RCU.  A sleepable BPF caller can read
an active state under root scheduler S1, get preempted across S1
teardown and S2 startup, and return S2's id before S2 initializes p:

CPU0 (sleepable BPF)              CPU1
scx_task_sched_id(p)
  READ_ONCE(scx->flags)
  /* sees SCX_TASK_ENABLED */
                                  scx_disable_workfn() /* S1 */
                                    scx_set_task_state(p, SCX_TASK_NONE)
                                    synchronize_rcu()
                                  scx_enable() /* S2 */
                                    rcu_assign_pointer(scx_root, S2)
  scx_bpf_sched_id()  <-- returns S2->id before S2 ops.init_task(p)

Because CPU0 is not inside bpf_rcu_read_lock(), synchronize_rcu() in
scx_disable_workfn() does not wait for it.  Wrapping the state check
and scx_bpf_sched_id() in bpf_rcu_read_lock() prevents this straddle.

With CONFIG_EXT_SUB_SCHED, an unlocked caller has a similar race with
scx_punt_task(), which sets p to SCX_TASK_NONE and then assigns
p->scx.sched without running ops.init_task(), letting scx_task_sched_id()
pair the pre-punt active state with the new scx->sched pointer.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261206053728.2026272-1-changwoo@igalia.com?part=1

  reply	other threads:[~2026-12-06  6:03 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-06  5:37 [PATCH v3 0/6] sched_ext: Add p->scx.bpf_priv for O(1) task context lookup 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  6:03   ` sashiko-bot [this message]
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  6:00   ` sashiko-bot
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:56   ` sashiko-bot
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:59   ` sashiko-bot
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  6:06   ` 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-252202@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.