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=1796275224; b=04/4D+VZiYeU6d+ZLI+pz/vtlJXIQlhPSod0BPKdk4GiSeuqAoxcP39EkP4ZvCn8VD9y yY5fvVQSGnmvnt+Qy1Rtm6zEKpYt6YsQLb9cqrIhOg6UXnTsJFfwL0VJdFD8FK7nOvwHG pR2QQnp8w/PxXTfRlonB+44ysNFZt5/AZiakOt0Z/jLZ/HtGBKncw4u+RLl160vcSotbN imfkFBanQmFul52VkUeFPko0jYmP3ljL8CKtPxZk3CPgQV8NlXHA2bHxmnW+s+czu4laB LljDN0pH8SjihufsiE+U3bDFJIKU8nuaM6jxSD3mvzMynYmL4aNJKHodyiCeY7jW0DQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796275224; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=iarLM5TqJqKcZzOVn9CONHR74V4QQH1qxmQ0eNTMV1U=; b=1YBtvYh8Ie19Th8aB55SFD6llgTDbJLgH3f4g8X0cFyFMg/Ea8/wptqMhGDmOdUUnCqC ebQS5mXIO7s+xEd2Ff/PbhhCl854JZlJawcSUT9FiF+YRUrBYMEvndwsl07f879xP7c/j 2xkbumZgEdWlDxE7YpDDWBvmES2bSHk+K/IkjdEixAn2/XDj0LPk/So/2IIGMg66K34Ig LX1jzO77j1Dv27+DBxPGFcf1QcHzn5vlci2rbaN9xQbD/hRczC9fgt5b/sS339ghAFSya aXrSYa86zr+jBGMLLniimBZahFFZiGDvbeuekwmD6u7XiQa08w88akOvol8+uCwRdvA== 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=OwXUpRsE; 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 D5A90606A338 for ; Thu, 3 Dec 2026 05:20:24 +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=OwXUpRsE Received: by smtp.kernel.org (Postfix) with ESMTPSA id 56FC21F00893; Thu, 3 Dec 2026 05:20:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796275224; bh=iarLM5TqJqKcZzOVn9CONHR74V4QQH1qxmQ0eNTMV1U=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=OwXUpRsEBDxHmZSeefgQNqvsKF6OvqQL1VnjpQKwo/IvHfXzOfq0/pIVGQ6S3cQQz Cm5UhwpONlNxC3fByM+3935jKeHZP3024HjhHZtOOqufBRvz6sEytqu9KDxEzuxP18 UlRxZZvnwVM0LAQLhG5iOcGTGoPSwcmyc3+R+yErYVSqROlD0eGxpCLjct1ug2+8yw uwjB0zu94oE1ndmnE6baMd+OzFed6cljrCXzoV7rGdnx1CI+xytbuyZclj+3zC0p4W Fe7Tvp+lziFcXUc4yvLqNZ6DYSdocw2Y3Toxv5cV/KWgcX9O3lpNX2r9SseABUKQMB 0LHCe4eWWOpGg== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v2 5/8] sched_ext: Add p->scx.bpf_priv for O(1) task context lookup Reply-To: sashiko-reviews@lists.linux.dev To: "Changwoo Min" Cc: sched-ext@lists.linux.dev In-Reply-To: <20261203045618.982539-6-changwoo@igalia.com> References: <20261203045618.982539-1-changwoo@igalia.com> <20261203045618.982539-6-changwoo@igalia.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Thu, 03 Dec 2026 05:20:24 +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 3 potential issues to consider: - [Medium] sched_ext: missing WRITE_ONCE() and release ordering on p->scx.bpf_priv - [Medium] sched_ext: lockless unmarked read of p->scx.flags in scx_bpf_task_set_priv() - [Medium] sched_ext: switched_from_scx() clears bpf_priv before ops.exit_task() -- commit 1bbafecc216dad15995f74ded039783962b3186c Author: Changwoo Min sched_ext: Add p->scx.bpf_priv for O(1) task context lookup This commit adds an opaque u64 field p->scx.bpf_priv and the scx_bpf_task_set_priv() kfunc so BPF schedulers can store and read per-task context pointers without a task local storage lookup. > diff --git a/include/linux/sched/ext.h b/include/linux/sched/ext.h > index f3564581c317c..e63bd789c7d89 100644 > --- a/include/linux/sched/ext.h > +++ b/include/linux/sched/ext.h > @@ -277,6 +277,19 @@ struct sched_ext_entity { > */ > atomic64_t slice_oob; > =20 > + /* > + * Opaque value owned by the task's scheduler, typically an arena > + * pointer to its per-task context. The kernel never dereferences it. > + * Set with scx_bpf_task_set_priv() while @p is enabled on the > + * scheduler, from ops.enable() on, and cleared when @p leaves the > + * scheduler's control, after the last op the scheduler runs for @p. > + * Any BPF program can read it, so a tracing program can reach the > + * context of the current task with a single load. A write from > + * outside @p's own ops can outlive the clear, see > + * scx_bpf_task_set_priv(). > + */ > + u64 bpf_priv; > + > /* > * Sub-sched cap rejected reenq context, valid only while > * %SCX_TASK_REENQ_CAP is set. @reenq_reason_caps is the SCX_CAP_* bits [Severity: Medium] Is p->scx.bpf_priv actually cleared after the last op the scheduler runs for @p? On root scheduler teardown, scx_root_disable() switches each task out of ext_sched_class via sched_change_begin() -> switched_from_scx() before calling scx_disable_and_exit_task(). switched_from_scx() runs scx_disable_task() and then immediately calls scx_reset_task_fields(), clearing p->scx.bpf_priv to 0 before ops.exit_task() runs. The same happens when a task leaves ext_sched_class via sched_setscheduler() and later exits. By contrast, sched_ext_dead() and sub-scheduler handoffs keep p->sched_class unchanged, so ops.exit_task() sees the non-zero value before __scx_disable_and_exit_task() resets it. A scheduler relying on this comment (and the matching kernel-doc on scx_bpf_task_set_priv()) to read p->scx.bpf_priv in ops.exit_task() for cleanup will see 0 on root unload and class switches. Should the documentation scope validity to ops.disable(), or should the reset timing be aligned with ops.exit_task()? > diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c > index 2ebc67916757a..0877341440ad6 100644 > --- a/kernel/sched/ext/ext.c > +++ b/kernel/sched/ext/ext.c [ ... ] > @@ -10893,6 +10894,50 @@ __bpf_kfunc bool scx_bpf_task_set_lazy_resched(s= truct task_struct *p, bool lazy, > return true; > } > =20 > +/** > + * scx_bpf_task_set_priv - Set the BPF scheduler's private value of a ta= sk > + * @p: task of interest > + * @priv: opaque value to store in @p->scx.bpf_priv > + * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF pr= ogs > + * > + * Store @priv in @p->scx.bpf_priv, where any BPF program can read it wi= th a > + * plain load. The value is valid while @p is enabled on the calling sch= eduler. > + * The kernel clears it when @p leaves the scheduler's control, after th= e last > + * op the scheduler runs for @p, and ops.enable() runs again when @p com= es back. > + * > + * From @p's own ops, ops.enable() included, @p can't leave the scheduler > + * concurrently. Elsewhere, e.g. from a timer or a tracing program, the = write > + * can race @p leaving and outlive the clear, as with > + * scx_bpf_task_set_dsq_vtime(). Synchronizing such writes is the BPF > + * scheduler's responsibility. > + * > + * Return %true if @priv was stored, %false if @p is not enabled on the = calling > + * scheduler. > + */ > +__bpf_kfunc bool scx_bpf_task_set_priv(struct task_struct *p, u64 priv, > + const struct bpf_prog_aux *aux) > +{ > + struct scx_sched *sch; > + > + guard(rcu)(); > + sch =3D scx_prog_sched(aux); > + if (unlikely(!sch || !scx_task_on_sched(sch, p))) > + return false; > + > + /* > + * Accept only %ENABLED. The value is cleared whenever @p leaves the ext > + * class, and only ops.enable() runs again when @p comes back. A value > + * set before ops.enable() would be lost the first time @p leaves the e= xt > + * class. This refuses ops.init_task(), ops.exit_task() and @p on a > + * non-ext class. > + */ > + if (scx_get_task_state(p) !=3D SCX_TASK_ENABLED) > + return false; [Severity: Medium] Is calling scx_get_task_state(p) without the rq lock safe against concurrent updates to p->scx.flags? scx_bpf_task_set_priv() is registered in scx_kfunc_ids_any and holds only guard(rcu)(), while p->scx.flags is protected by the rq lock and modified with unmarked bitwise operations across scheduling paths. In particular, scx_set_task_state() updates the state bits in two separate plain stores: kernel/sched/ext/ext.c:scx_set_task_state() { ... p->scx.flags &=3D ~SCX_TASK_STATE_MASK; p->scx.flags |=3D state; } An unlocked caller such as a BPF timer or syscall program invoking scx_bpf_task_set_priv() concurrently with scheduling activity on another CPU triggers a KCSAN data race on p->scx.flags and can transiently observe SCX_TASK_NONE between those two stores. > + > + p->scx.bpf_priv =3D priv; > + return true; > +} [Severity: Medium] Should the write to p->scx.bpf_priv use smp_store_release() here and WRITE_ONCE() in scx_reset_task_fields()? Lockless BPF readers load p->scx.bpf_priv via READ_ONCE(scx->bpf_priv) in scx_bpf_task_priv(). When a scheduler initializes a per-task arena context and publishes its pointer here, a plain store lacks release ordering on weakly-ordered architectures such as arm64, so a reader on another CPU can observe the pointer before the context initialization stores are visible. The unmarked 64-bit stores here and in scx_reset_task_fields() also race with lockless readers and concurrent unlocked scx_bpf_task_set_priv() callers, allowing compiler store tearing and KCSAN data-race warnings (compare WRITE_ONCE() in scx_bpf_task_set_lazy_resched() right above). --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261203045618.9825= 39-1-changwoo@igalia.com?part=3D5