sched_ext: Use READ_ONCE() for plain reads of deferred reenq flags
Subsystems (2)
PATCH 1/1 sched_ext: Use READ_ONCE() for plain reads of deferred reenq flags
commit a90449b12682 ("sched_ext: Optimize schedule_dsq_reenq() with lockless fast path") introduced lockless reads of drl->flags and dru->flags via READ_ONCE() in the fast path of schedule_dsq_reenq(). However, four read-side accesses in the locked slow path and consumer functions use plain reads without the matching READ_ONCE():
/* schedule_dsq_reenq() - local DSQ path */ WRITE_ONCE(drl->flags, drl->flags | reenq_flags); ^^^^^^^^^^ plain read -- KCSAN data race /* schedule_dsq_reenq() - user DSQ path */ WRITE_ONCE(dru->flags, dru->flags | reenq_flags); ^^^^^^^^^^ plain read -- KCSAN data race /* process_deferred_reenq_locals() */ reenq_flags = drl->flags; ^^^^^^^^^^ plain read -- KCSAN data race /* process_deferred_reenq_users() */ reenq_flags = dru->flags; ^^^^^^^^^^ plain read -- KCSAN data race
The KCSAN documentation requires that if one accessor uses READ_ONCE() or WRITE_ONCE() to annotate lock-free access, all other concurrent accesses must also use the appropriate accessor. Plain reads alongside READ_ONCE()/WRITE_ONCE() leave the annotation incomplete and trigger KCSAN warnings.
Fix all four plain reads with READ_ONCE() to complete the concurrent access annotation and make the code KCSAN-clean, consistent with the lockless fast path READ_ONCE(drl->flags) already in schedule_dsq_reenq().
--- kernel/sched/ext.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/sched/ext.c +4 -4
@@ -1187,7 +1187,7 @@ static void schedule_dsq_reenq(struct scx_sched *sch, struct scx_dispatch_q *dsqif (list_empty (&drl->node))list_move_tail (&drl->node, &rq->scx.deferred_reenq_locals); -WRITE_ONCE (drl->flags, drl->flags | reenq_flags); +WRITE_ONCE (drl->flags,READ_ONCE (drl->flags) | reenq_flags); }schedule_deferred (rq);
@@ -1209,7 +1209,7 @@ static void schedule_dsq_reenq(struct scx_sched *sch, struct scx_dispatch_q *dsqif (list_empty (&dru->node))list_move_tail (&dru->node, &rq->scx.deferred_reenq_users); -WRITE_ONCE (dru->flags, dru->flags | reenq_flags); +WRITE_ONCE (dru->flags,READ_ONCE (dru->flags) | reenq_flags); }schedule_deferred (rq);
@@ -3817,7 +3817,7 @@ static void process_deferred_reenq_locals(struct rq *rq) sch_pcpu = container_of(drl, struct scx_sched_pcpu, deferred_reenq_local); sch = sch_pcpu->sch; - reenq_flags = drl->flags; + reenq_flags =READ_ONCE (drl->flags);WRITE_ONCE (drl->flags,0 );list_del_init (&drl->node); }
@@ -3921,7 +3921,7 @@ static void process_deferred_reenq_users(struct rq *rq) dsq_pcpu = container_of(dru, struct scx_dsq_pcpu, deferred_reenq_user); dsq = dsq_pcpu->dsq; - reenq_flags = dru->flags; + reenq_flags =READ_ONCE (dru->flags);WRITE_ONCE (dru->flags,0 );list_del_init (&dru->node); }
Signature · 1 line
2.48.1
Tejun Heo
reviewerQuoted 10 lines · Cheng-Yang Chou
commit
a90449b12682("sched_ext: Optimizeschedule_dsq_reenq()with lockless fast path") introduced lockless reads ofdrl->flagsanddru->flagsviaREAD_ONCE()in the fast path ofschedule_dsq_reenq(). However, four read-side accesses in the locked slow path and consumer functions use plain reads without the matchingREAD_ONCE():/* schedule_dsq_reenq() - local DSQ path */ WRITE_ONCE(drl->flags, drl->flags | reenq_flags); ^^^^^^^^^^ plain read -- KCSAN data race
Does KCSAN actually trigger on it? The writes are protected by rq->scx.deferred_reenq_lock and reads are protected together with it. These shouldn't constitute racy reads.
Thanks.
Signature · 1 line
tejun
Quoted 12 lines · Tejun Heo
Quoted 10 lines · Cheng-Yang Chou
commit
a90449b12682("sched_ext: Optimizeschedule_dsq_reenq()with lockless fast path") introduced lockless reads ofdrl->flagsanddru->flagsviaREAD_ONCE()in the fast path ofschedule_dsq_reenq(). However, four read-side accesses in the locked slow path and consumer functions use plain reads without the matchingREAD_ONCE():/* schedule_dsq_reenq() - local DSQ path */ WRITE_ONCE(drl->flags, drl->flags | reenq_flags); ^^^^^^^^^^ plain read -- KCSAN data race
Quoted 8 lines
Does KCSAN actually trigger on it? The writes are protected by
rq->scx.deferred_reenq_lockand reads are protected together with it. These shouldn't constitute racy reads.Thanks.
Signature · 1 line
tejun
Thanks for the clarification.
This was a preemptive fix from code reading, not based on an actual triggered report. I'll hold off for now and ensure I provide KCSAN logs and verification for such cases in the future.
Signature · 2 lines
Thanks, Cheng-Yang
Tejun Heo
reviewerQuoted 3 lines · Cheng-Yang Chou
This was a preemptive fix from code reading, not based on an actual triggered report. I'll hold off for now and ensure I provide KCSAN logs and verification for such cases in the future.
Yes, please stop doing this. Verify before sending patches. This just increases workload and potentially introduces unnecessary and confusing changes into the code base. This doesn't help. This harms.
Thanks.
Signature · 1 line
tejun