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 1/6] sched_ext: Add scx_bpf_sched_id() and scx_task_sched_id()
Date: Sun, 6 Dec 2026 11:54:00 +0100 [thread overview]
Message-ID: <axU-yNX826E2RNzX@gpd4> (raw)
In-Reply-To: <20261206053728.2026272-2-changwoo@igalia.com>
On Sun, Dec 06, 2026 at 02:37:23PM +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 the scx_bpf_sched_id() kfunc,
> the calling scheduler's id, 0 for none. Only the kernel knows which
> scheduler a program belongs to, so it has to be a kfunc.
>
> Add scx_task_sched_id() to compat.bpf.h, the id of the scheduler that
> owns a task, 0 for none. It reads p->scx.sched with CO-RE, but only in
> INIT, READY or ENABLED, as a scheduler owns a task only after
> ops.init_task(). Without CONFIG_EXT_SUB_SCHED, the kernel doesn't set
> p->scx.sched, so the helper returns scx_bpf_sched_id(), the root's id.
>
> scx_set_task_state() now publishes the state bits in one store, as the
> helper reads them without a lock.
>
> Suggested-by: Andrea Righi <arighi@nvidia.com>
> Signed-off-by: Changwoo Min <changwoo@igalia.com>
> ---
...
> +/*
> + * Id of the scheduler @p is on, see scx_bpf_sched_id(), 0 if @p is not on any.
> + * Unless @p's rq lock or pi_lock is held, @p may move to another scheduler
> + * right after.
> + */
> +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;
> +}
About the sashiko report on this one, I think it's mostly irrelevant in
practice. The doc already says the result is only stable with @p's rq
lock or pi_lock held, and lockless users are supposed to tag their
values with scx_bpf_sched_id() anyway.
That said, the !CONFIG_EXT_SUB_SCHED case is easy to close: if we move
bpf_rcu_read_lock() up to cover the state check as well, the
synchronize_rcu() in the root disable path guarantees that a reader that
saw an active state can't see the next root.
Something like this:
bpf_rcu_read_lock();
state = READ_ONCE(scx->flags) & ...;
if (state == NONE || state == INIT_BEGIN || state == DEAD) {
id = 0;
} else if (!bpf_core_field_exists(struct scx_sched___task_id, cgrp)) {
id = scx_bpf_sched_id();
} else {
sch = scx->sched;
id = sch ? sch->id : 0;
}
bpf_rcu_read_unlock();
return id;
The scx_punt_task() case can't be fixed this way (no grace period
there), but it's harmless, only the punt target can be misled and it's
already bypassed and dying.
Thanks,
-Andrea
next prev parent reply other threads:[~2026-12-06 10:54 UTC|newest]
Thread overview: 10+ 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 10:54 ` Andrea Righi [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 8:15 ` Andrea Righi
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=axU-yNX826E2RNzX@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®