public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] sched_ext: Use READ_ONCE() for the read side of dsq->nr update
@ 2026-03-02  9:14 zhidao su
  2026-03-02  9:14 ` [PATCH 2/2] sched_ext: Replace naked scx_root dereferences in kobject callbacks zhidao su
  2026-03-02 17:28 ` [PATCH 1/2] sched_ext: Use READ_ONCE() for the read side of dsq->nr update Tejun Heo
  0 siblings, 2 replies; 3+ messages in thread
From: zhidao su @ 2026-03-02  9:14 UTC (permalink / raw)
  To: sched-ext
  Cc: linux-kernel, tj, void, arighi, changwoo, peterz, mingo,
	zhidao su

From: zhidao su <suzhidao@xiaomi.com>

scx_bpf_dsq_nr_queued() reads dsq->nr via READ_ONCE() without holding
any lock, making dsq->nr a lock-free concurrently accessed variable.
However, dsq_mod_nr(), the sole writer of dsq->nr, only uses
WRITE_ONCE() on the write side without the matching READ_ONCE() on the
read side:

    WRITE_ONCE(dsq->nr, dsq->nr + delta);
                        ^^^^^^^
                        plain read -- KCSAN data race

The KCSAN documentation requires that if one accessor uses READ_ONCE()
or WRITE_ONCE() on a variable to annotate lock-free access, all other
accesses must also use the appropriate accessor. A plain read on the
right-hand side of WRITE_ONCE() leaves the pair incomplete and will
trigger KCSAN warnings.

Fix by using READ_ONCE() for the read side of the update:

    WRITE_ONCE(dsq->nr, READ_ONCE(dsq->nr) + delta);

This is consistent with scx_bpf_dsq_nr_queued() and makes the
concurrent access annotation complete and KCSAN-clean.

Signed-off-by: zhidao su <suzhidao@xiaomi.com>
---
 kernel/sched/ext.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/kernel/sched/ext.c b/kernel/sched/ext.c
index 136b01950a62..718401f4ff2a 100644
--- a/kernel/sched/ext.c
+++ b/kernel/sched/ext.c
@@ -972,8 +972,12 @@ static bool scx_dsq_priq_less(struct rb_node *node_a,
 
 static void dsq_mod_nr(struct scx_dispatch_q *dsq, s32 delta)
 {
-	/* scx_bpf_dsq_nr_queued() reads ->nr without locking, use WRITE_ONCE() */
-	WRITE_ONCE(dsq->nr, dsq->nr + delta);
+	/*
+	 * scx_bpf_dsq_nr_queued() reads ->nr without locking. Use READ_ONCE()
+	 * on the read side and WRITE_ONCE() on the write side to properly
+	 * annotate the concurrent lockless access and avoid KCSAN warnings.
+	 */
+	WRITE_ONCE(dsq->nr, READ_ONCE(dsq->nr) + delta);
 }
 
 static void refill_task_slice_dfl(struct scx_sched *sch, struct task_struct *p)
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 2/2] sched_ext: Replace naked scx_root dereferences in kobject callbacks
  2026-03-02  9:14 [PATCH 1/2] sched_ext: Use READ_ONCE() for the read side of dsq->nr update zhidao su
@ 2026-03-02  9:14 ` zhidao su
  2026-03-02 17:28 ` [PATCH 1/2] sched_ext: Use READ_ONCE() for the read side of dsq->nr update Tejun Heo
  1 sibling, 0 replies; 3+ messages in thread
From: zhidao su @ 2026-03-02  9:14 UTC (permalink / raw)
  To: sched-ext
  Cc: linux-kernel, tj, void, arighi, changwoo, peterz, mingo,
	zhidao su

From: zhidao su <suzhidao@xiaomi.com>

scx_attr_ops_show() and scx_uevent() access scx_root->ops.name directly.
This is problematic for two reasons:

1. The file-level comment explicitly identifies naked scx_root
   dereferences as a temporary measure that needs to be replaced
   with proper per-instance access.

2. scx_attr_events_show(), the neighboring sysfs show function in
   the same group, already uses the correct pattern:

       struct scx_sched *sch = container_of(kobj, struct scx_sched, kobj);

   Having inconsistent access patterns in the same sysfs/uevent
   group is error-prone.

The kobject embedded in struct scx_sched is initialized as:

    kobject_init_and_add(&sch->kobj, &scx_ktype, NULL, "root");

so container_of(kobj, struct scx_sched, kobj) correctly retrieves
the owning scx_sched instance in both callbacks.

Replace the naked scx_root dereferences with container_of()-based
access, consistent with scx_attr_events_show() and in preparation
for proper multi-instance scx_sched support.

Signed-off-by: zhidao su <suzhidao@xiaomi.com>
---
 kernel/sched/ext.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/kernel/sched/ext.c b/kernel/sched/ext.c
index 718401f4ff2a..f25340ac044a 100644
--- a/kernel/sched/ext.c
+++ b/kernel/sched/ext.c
@@ -3631,7 +3631,9 @@ static void scx_kobj_release(struct kobject *kobj)
 static ssize_t scx_attr_ops_show(struct kobject *kobj,
 				 struct kobj_attribute *ka, char *buf)
 {
-	return sysfs_emit(buf, "%s\n", scx_root->ops.name);
+	struct scx_sched *sch = container_of(kobj, struct scx_sched, kobj);
+
+	return sysfs_emit(buf, "%s\n", sch->ops.name);
 }
 SCX_ATTR(ops);
 
@@ -3675,7 +3677,9 @@ static const struct kobj_type scx_ktype = {
 
 static int scx_uevent(const struct kobject *kobj, struct kobj_uevent_env *env)
 {
-	return add_uevent_var(env, "SCXOPS=%s", scx_root->ops.name);
+	const struct scx_sched *sch = container_of(kobj, struct scx_sched, kobj);
+
+	return add_uevent_var(env, "SCXOPS=%s", sch->ops.name);
 }
 
 static const struct kset_uevent_ops scx_uevent_ops = {
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH 1/2] sched_ext: Use READ_ONCE() for the read side of dsq->nr update
  2026-03-02  9:14 [PATCH 1/2] sched_ext: Use READ_ONCE() for the read side of dsq->nr update zhidao su
  2026-03-02  9:14 ` [PATCH 2/2] sched_ext: Replace naked scx_root dereferences in kobject callbacks zhidao su
@ 2026-03-02 17:28 ` Tejun Heo
  1 sibling, 0 replies; 3+ messages in thread
From: Tejun Heo @ 2026-03-02 17:28 UTC (permalink / raw)
  To: zhidao su
  Cc: sched-ext, linux-kernel, void, arighi, changwoo, peterz, mingo,
	zhidao su

> zhidao su (2):
>   sched_ext: Use READ_ONCE() for the read side of dsq->nr update
>   sched_ext: Replace naked scx_root dereferences in kobject callbacks

Applied 1-2 to sched_ext/for-7.0-fixes.

Thanks.

-- 
tejun

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-03-02 17:28 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-02  9:14 [PATCH 1/2] sched_ext: Use READ_ONCE() for the read side of dsq->nr update zhidao su
2026-03-02  9:14 ` [PATCH 2/2] sched_ext: Replace naked scx_root dereferences in kobject callbacks zhidao su
2026-03-02 17:28 ` [PATCH 1/2] sched_ext: Use READ_ONCE() for the read side of dsq->nr update Tejun Heo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox