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 4/6] sched_ext: scx_qmap: Cache task_ctx in p->scx.bpf_priv
Date: Sun, 06 Dec 2026 05:56:36 +0000	[thread overview]
Message-ID: <sashiko-outbox-252196@kernel.org> (raw)
In-Reply-To: <20261206053728.2026272-5-changwoo@igalia.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] sched_ext: scx_qmap: unverified bpf_priv tag collision in
  lookup_task_ctx()
--

commit 5c944365906adb946e34a37a4704476bce3c8842
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
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..90237a1ce38aa 100644
> --- a/tools/sched_ext/scx_qmap.bpf.c
> +++ b/tools/sched_ext/scx_qmap.bpf.c

[ ... ]

> @@ -256,12 +257,49 @@ 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_core_sched_before() and qmap_dump_task(). So the cached value carries
> + * this sched's id as a tag:
> + *
> + *   task_ctx_key
> + *     63-32  upper 32 bits of the arena's pointers
> + *     31     1, TASK_CTX_TAG_BIT
> + *     30-0   id from scx_bpf_sched_id(), 1 <= id < 2^31
> + *
> + *   p->scx.bpf_priv
> + *     63     1, TASK_CTX_TAG_BIT
> + *     62-32  id
> + *     31-0   lower 32 bits of @p's task_ctx pointer
> + *
> + * The arena never crosses a 4G boundary, so all its pointers share the upper
> + * 32 bits. Ids are never reused, so a value left by another sched, including
> + * one that has since unloaded, never matches. An arena pointer is a user space
> + * address, whose most significant bit is always zero. Bit 31 of the tag is the
> + * most significant bit of p->scx.bpf_priv, and setting it to 1 makes the
> + * cached value an invalid user space address, for foolproofing.
> + *
> + * qmap_enable() sets the value. The first call also sets task_ctx_key, which
> + * then never changes. qmap_exit_task() clears the value before freeing the
> + * task_ctx, as the kernel clears it only after ops.exit_task().
> + */
> +#define TASK_CTX_TAG_BIT	(1U << 31)
> +
> +static u64 task_ctx_key;
> +
>  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 = scx_bpf_task_priv(p);
>  
>  	QMAP_TOUCH_ARENA();
>  
> +	if (key && (u32)(priv >> 32) == (u32)key)
> +		return (task_ctx_t *)((key & ~0xffffffffULL) | (u32)priv);

[Severity: Low]
Can lookup_task_ctx() return a bogus arena pointer when a non-qmap
sub-scheduler writes an arbitrary u64 to p->scx.bpf_priv?

p->scx.bpf_priv has no kernel-enforced tagging format, so a sub-scheduler
can store a value whose upper 32 bits match TASK_CTX_TAG_BIT | id (for
example, 0x80000001 when scx_qmap has id 1).

When qmap_core_sched_before() runs on that task, lookup_task_ctx()
reconstructs an unvalidated offset in scx_qmap's arena instead of returning
NULL.  Dereferencing it then reads unrelated arena memory (corrupting
core-sched ordering) or faults to 0 on an unmapped page.

The comment and commit message also overstate the guarantee, since non-reuse
of scheduler ids only prevents collisions between schedulers using this
tagging layout.

Checking scx_task_sched_id(p) against scx_bpf_sched_id() under the rq lock
(as documented on scx_bpf_task_set_priv()), or validating that the pointer
lies within qa.task_ctxs, would avoid false matches.

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

  reply	other threads:[~2026-12-06  5:56 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
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 [this message]
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-252196@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.