From: sashiko-bot@kernel.org
To: "Changwoo Min" <changwoo@igalia.com>
Cc: sched-ext@lists.linux.dev
Subject: Re: [PATCH v2 6/8] sched_ext: scx_qmap: Cache task_ctx in p->scx.bpf_priv
Date: Thu, 03 Dec 2026 05:19:31 +0000 [thread overview]
Message-ID: <sashiko-outbox-247185@kernel.org> (raw)
In-Reply-To: <20261203045618.982539-7-changwoo@igalia.com>
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] sched_ext: scx_qmap: stale bpf_priv after qmap_exit_task()
frees task_ctx
- [Low] sched_ext: scx_qmap docs cite qmap_dump_task() bypassing
lookup_task_ctx()
- [Low] sched_ext: scx_qmap: unchecked bpf_priv tag in lookup_task_ctx()
- [Low] sched_ext: unmarked stores to p->scx.bpf_priv race with lockless
READ_ONCE()
--
commit a0014bb43cd73d33b79569621b8c7bf640a6bf3e
Author: Changwoo Min <changwoo@igalia.com>
sched_ext: scx_qmap: Cache task_ctx in p->scx.bpf_priv
This commit caches each task's task_ctx arena pointer in p->scx.bpf_priv
from qmap_enable(), tagged with scx_bpf_sched_id(), so lookup_task_ctx()
can avoid a task storage lookup on the fast path.
> diff --git a/tools/sched_ext/scx_qmap.bpf.c b/tools/sched_ext/scx_qmap.bpf.c
> index 3951f1f6e3c6a..8fa6626d6598f 100644
> --- a/tools/sched_ext/scx_qmap.bpf.c
> +++ b/tools/sched_ext/scx_qmap.bpf.c
[ ... ]
> @@ -256,12 +257,47 @@ static s32 pick_direct_dispatch_cid(struct task_struct *p, s32 prev_cid,
> */
> #define QMAP_TOUCH_ARENA() do { asm volatile("" :: "r"(&arena)); } while (0)
>
> +/*
> + * p->scx.bpf_priv caches @p's task_ctx so that lookup_task_ctx() takes a load
> + * instead of a task storage lookup. The field holds whatever the sched owning
> + * @p stored, and a qmap sched also sees tasks of its sub-scheds, e.g. in
> + * qmap_dump_task(). So the cached value carries this sched's id as a tag:
[Severity: Low]
Does qmap_dump_task() actually use lookup_task_ctx()?
qmap_dump_task() still calls bpf_task_storage_get(&task_ctx_stor, ...)
directly and never reads p->scx.bpf_priv, while qmap_core_sched_before()
is the callback that calls lookup_task_ctx() on tasks that may belong to
sub-schedulers. Either qmap_dump_task() should be switched to
lookup_task_ctx(), or this comment and the commit message should cite
qmap_core_sched_before() instead.
[ ... ]
> static task_ctx_t *lookup_task_ctx(struct task_struct *p)
> {
> struct task_ctx_stor_val *v;
> + u64 key = READ_ONCE(task_ctx_key);
> + u64 priv = READ_ONCE(p->scx.bpf_priv);
[Severity: Low]
Can this lockless READ_ONCE(p->scx.bpf_priv) tear against the unmarked
stores in kernel/sched/ext/ext.c?
lookup_task_ctx() is called without p->pi_lock or task_rq(p) lock from
monitor_timerfn() -> scan_shared_dsq() (under RCU only, after
bpf_iter_scx_dsq_next() drops dsq->lock) and from qmap_dispatch().
Meanwhile, commit "sched_ext: Add p->scx.bpf_priv for O(1) task context
lookup" writes p->scx.bpf_priv with plain assignments in
scx_reset_task_fields() and scx_bpf_task_set_priv().
If store tearing splits the 64-bit write between the upper tag and lower
pointer half, lookup_task_ctx() can pair a matching tag with a zeroed or
stale lower 32 bits and reconstruct a corrupted arena pointer. Using
WRITE_ONCE() in scx_reset_task_fields() and scx_bpf_task_set_priv()
(matching scx_bpf_task_set_lazy_resched()) would pair properly with this
READ_ONCE().
>
> QMAP_TOUCH_ARENA();
>
> + if (key && (u32)(priv >> 32) == (u32)key)
> + return (task_ctx_t *)((key & ~0xffffffffULL) | (u32)priv);
[Severity: Low]
Can a non-qmap sub-scheduler collide with this upper-32-bit tag and make
lookup_task_ctx() fabricate an arbitrary arena pointer?
p->scx.bpf_priv is an opaque u64 with no kernel-enforced tagging format.
Under CONFIG_SCHED_CORE, scx_prio_less() calls qmap_core_sched_before()
on tasks belonging to arbitrary child sub-schedulers accepted by
qmap_sub_attach(). If a sub-scheduler stores a value whose upper 32 bits
match (1U << 31) | id, lookup_task_ctx() returns a bogus arena pointer
instead of NULL, causing qmap_core_sched_before() to read an unmapped or
unrelated arena offset rather than falling back to default ordering.
Checking scx_bpf_task_sched_id() or reading task_ctx_stor directly in
qmap_core_sched_before() would avoid misidentifying sub-scheduler tasks.
Similarly, qmap_enable() calls lookup_task_ctx(p) when initializing
p->scx.bpf_priv instead of reading task_ctx_stor directly, so a colliding
out-of-band scx_bpf_task_set_priv() write from a prior scheduler would be
re-cached rather than overwritten.
[ ... ]
> @@ -1062,6 +1098,27 @@ s32 BPF_STRUCT_OPS_SLEEPABLE(qmap_init_task, struct task_struct *p,
> return 0;
> }
>
> +void BPF_STRUCT_OPS(qmap_enable, struct task_struct *p,
> + struct scx_enable_args *args)
> +{
> + task_ctx_t *taskc = lookup_task_ctx(p);
> + u64 key = READ_ONCE(task_ctx_key);
> + u64 id;
> +
> + if (!taskc)
> + return;
> +
> + if (!key) {
> + id = scx_bpf_sched_id();
> + if (!id || id >= TASK_CTX_TAG_BIT)
> + return;
> + key = ((u64)taskc & ~0xffffffffULL) | TASK_CTX_TAG_BIT | (u32)id;
> + WRITE_ONCE(task_ctx_key, key);
> + }
> +
> + scx_bpf_task_set_priv(p, (u64)(u32)key << 32 | (u32)(u64)taskc);
> +}
[Severity: Medium]
Can lookup_task_ctx() return a freed and recycled task_ctx while p is
leaving the scheduler?
qmap_enable() populates p->scx.bpf_priv, but scx_qmap has no
ops.disable() callback to clear it. In __scx_disable_and_exit_task(),
qmap_exit_task() returns taskc to qa.task_free_head before
scx_reset_task_fields() zeroes p->scx.bpf_priv, and qmap_exit_task()
cannot clear the field itself because scx_bpf_task_set_priv() rejects
tasks that are no longer SCX_TASK_ENABLED:
CPU0 CPU1 CPU2
__scx_disable_and_exit_task(p)
scx_disable_task(p)
qmap_exit_task(p)
v->taskc = NULL
free taskc to slab
qmap_init_task(p2)
pop & reset taskc
monitor_timerfn()
scan_shared_dsq()
lookup_task_ctx(p)
<-- recycled taskc
scx_reset_task_fields(p)
p->scx.bpf_priv = 0
Because scan_shared_dsq() (called from monitor_timerfn() under RCU only,
after bpf_iter_scx_dsq_next() drops dsq->lock) and
update_core_sched_head_seq() (called from qmap_dispatch() without
task_rq(p) lock) do not serialize with __scx_disable_and_exit_task(),
they can read p2's recycled taskc fields and corrupt core-sched head
tracking or rescue/highpri accounting. Clearing p->scx.bpf_priv in an
ops.disable() callback before qmap_exit_task() frees taskc would close
this free-before-invalidate window.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203045618.982539-1-changwoo@igalia.com?part=6
next prev parent reply other threads:[~2026-12-03 5:19 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
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 [this message]
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-247185@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