From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796275172; b=SZ4jzfAL+rsHRwkhU3D+dmDCCdqKFh4iMgnVf60oNBPcew7/Lt824e7Wn2MfEHbJSWKS 28fMOeOm8CECd+3/fgIAym23OMTbfodBMVhR6EjkoNSK5ndgymphpPetSl6Z+WtZDz1y8 EMrVirDFYDlzH/MTGSDwdiftbz0tOxEmrkn9GzOKLTD0ptQcKNEHUNxu6sF5wllOeKZKO /fKo2IHwzQbmgqXsVt8QguSrJaap4NxTbt+hUJdJLmIkFKr2q5FMjyw82Hlgx7nMAVdhD swPaswEaDQkVovzJ9iIxHoqCN+IDapX5KPOZHsnXv8aawIQAkq35IH3UnXt9tLM60Kw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796275172; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=LYgQ4IpBxXpL/VQ9n+rjPiEtLHHlD7UkxaROf61Jbxw=; b=I1mTqPYftHPUdpyUQnVmaH8iQTR90uUBUXv8Uzr7abETUQ33xaGNsP/B/vvL3g0u3uv9 ez7DzXvkO00pvf9HrIX6Et1nlRNW/BTmsHe1/Dmw+DYBzLf3KShcg8AAanEYza9dnD9sj rawtraVmc6uRn1jmMSJvUwJOzGkHsgVbu0MAaJSQxcZTvTX2QnDairlqWw0PTr0cPub2H QJ6vymi8c+soDkyerixQ6+CFAGB8y8YL267u5pv+iL6LQCSbRrf93o3DMxON2uC1YyWXQ IjD3AO0FIAKZmduk86vH28kD5PaSvfZQJ9oos6pEXWw065bKs3phkj9tsm9roUCWQjA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="fy4lCT/H"; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D71F7606A322 for ; Thu, 3 Dec 2026 05:19:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=fy4lCT/H Received: by smtp.kernel.org (Postfix) with ESMTPSA id 654E41F00893; Thu, 3 Dec 2026 05:19:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796275172; bh=LYgQ4IpBxXpL/VQ9n+rjPiEtLHHlD7UkxaROf61Jbxw=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=fy4lCT/Hc2woD+WQPOY1g4OJaGCYEyLEAuDph/m3ZPqK1ytHO8McI/muUxN5pcbjM M57lJO6rynnpa68CQovtkLd1RGgmr+Rd5kDax0agdWUnvks8Tey1MpquTxk7mTkY4f qwJpFJ8viCQVOWUFub9ZVM1dRxH2z6jVSbuMMwF82IhEC+sdpIykFPDKua0TwOMV59 j/hsrR0/OCj2M2fW9MMUJN0rDtLH06tEbdrQ/kbKW+97dso7WZlcxuBWfQJyARysJb nwYzu3j2U/y+1hDuGNY18FM+ztmepMY/TsdO6QEimw6MiGHUu7mVRvYfzSjZpgoIod 7B+ov7t/oQedA== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v2 6/8] sched_ext: scx_qmap: Cache task_ctx in p->scx.bpf_priv Reply-To: sashiko-reviews@lists.linux.dev To: "Changwoo Min" Cc: sched-ext@lists.linux.dev In-Reply-To: <20261203045618.982539-7-changwoo@igalia.com> References: <20261203045618.982539-1-changwoo@igalia.com> <20261203045618.982539-7-changwoo@igalia.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Thu, 03 Dec 2026 05:19:31 +0000 X-Mailing-List: sched-ext@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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 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.bp= f.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_str= uct *p, s32 prev_cid, > */ > #define QMAP_TOUCH_ARENA() do { asm volatile("" :: "r"(&arena)); } while= (0) > =20 > +/* > + * 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 ta= g: [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 =3D READ_ONCE(task_ctx_key); > + u64 priv =3D 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(). > =20 > QMAP_TOUCH_ARENA(); > =20 > + if (key && (u32)(priv >> 32) =3D=3D (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, struc= t task_struct *p, > return 0; > } > =20 > +void BPF_STRUCT_OPS(qmap_enable, struct task_struct *p, > + struct scx_enable_args *args) > +{ > + task_ctx_t *taskc =3D lookup_task_ctx(p); > + u64 key =3D READ_ONCE(task_ctx_key); > + u64 id; > + > + if (!taskc) > + return; > + > + if (!key) { > + id =3D scx_bpf_sched_id(); > + if (!id || id >=3D TASK_CTX_TAG_BIT) > + return; > + key =3D ((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 =3D NULL free taskc to slab qmap_init_task(p2) pop & reset taskc monitor_timerfn() scan_shared_dsq() lookup_task_ctx(p) <-- recycled tas= kc scx_reset_task_fields(p) p->scx.bpf_priv =3D 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. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261203045618.9825= 39-1-changwoo@igalia.com?part=3D6