Annotate the last_scale_event, scale_lat, nr_samples and scale_grp members of struct child_latency_info with __guarded_by(&lock) such that the Clang context analysis can verify that these members are only accessed with the child_latency_info lock held. Leave scale_cookie unannotated since it is an atomic_t that is accessed without holding the lock. Mark the two intentional lockless reads in check_scale_change() such that the context analysis accepts these. Use context_unsafe(READ_ONCE()) for scale_lat since all writers of that member use WRITE_ONCE() and hence there is no need to disable KCSAN for this read. Use data_race(READ_ONCE()) for nr_samples since that member is updated with plain assignments. Both reads are hints only and their result is allowed to be stale. Signed-off-by: Bart Van Assche --- block/blk-iolatency.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/block/blk-iolatency.c b/block/blk-iolatency.c index 9d1b30e89e76..4c8db4ef925c 100644 --- a/block/blk-iolatency.c +++ b/block/blk-iolatency.c @@ -109,16 +109,16 @@ struct child_latency_info { spinlock_t lock; /* Last time we adjusted the scale of everybody. */ - u64 last_scale_event; + u64 last_scale_event __guarded_by(&lock); /* The latency that we missed. */ - u64 scale_lat; + u64 scale_lat __guarded_by(&lock); /* Total io's from all of our children for the last summation. */ - u64 nr_samples; + u64 nr_samples __guarded_by(&lock); /* The guy who actually changed the latency numbers. */ - struct iolatency_grp *scale_grp; + struct iolatency_grp *scale_grp __guarded_by(&lock); /* Cookie to tell if we need to scale up or down. */ atomic_t scale_cookie; @@ -412,7 +412,11 @@ static void check_scale_change(struct iolatency_grp *iolat) lat_info = &parent->child_lat; cur_cookie = atomic_read(&lat_info->scale_cookie); - scale_lat = READ_ONCE(lat_info->scale_lat); + /* + * Lockless read. All writers use WRITE_ONCE(), see also + * iolatency_check_latencies() and iolatency_clear_scaling(). + */ + scale_lat = context_unsafe(READ_ONCE(lat_info->scale_lat)); if (cur_cookie < our_cookie) direction = -1; @@ -438,7 +442,8 @@ static void check_scale_change(struct iolatency_grp *iolat) * or less of the IO's for the last summation just skip this * scale down event. */ - samples_thresh = lat_info->nr_samples * 5; + /* Lockless hint read; stale values are fine. */ + samples_thresh = data_race(READ_ONCE(lat_info->nr_samples)) * 5; samples_thresh = max(1ULL, div64_u64(samples_thresh, 100)); if (iolat->nr_samples <= samples_thresh) return;