Skip to content

Commit 54891a9

Browse files
Yongpeng Yangaxboe
authored andcommitted
loop: use READ_ONCE() to read lo->lo_state without locking
When lo->lo_mutex is not held, direct access may read stale data. This patch uses READ_ONCE() to read lo->lo_state and data_race() to silence code checkers, and changes all assignments to use WRITE_ONCE(). Reviewed-by: Damien Le Moal <dlemoal@kernel.org> Signed-off-by: Yongpeng Yang <yangyongpeng@xiaomi.com> Reviewed-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 9869d3a commit 54891a9

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

drivers/block/loop.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,7 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
10821082
/* Order wrt reading lo_state in loop_validate_file(). */
10831083
wmb();
10841084

1085-
lo->lo_state = Lo_bound;
1085+
WRITE_ONCE(lo->lo_state, Lo_bound);
10861086
if (part_shift)
10871087
lo->lo_flags |= LO_FLAGS_PARTSCAN;
10881088
partscan = lo->lo_flags & LO_FLAGS_PARTSCAN;
@@ -1179,7 +1179,7 @@ static void __loop_clr_fd(struct loop_device *lo)
11791179
if (!part_shift)
11801180
set_bit(GD_SUPPRESS_PART_SCAN, &lo->lo_disk->state);
11811181
mutex_lock(&lo->lo_mutex);
1182-
lo->lo_state = Lo_unbound;
1182+
WRITE_ONCE(lo->lo_state, Lo_unbound);
11831183
mutex_unlock(&lo->lo_mutex);
11841184

11851185
/*
@@ -1218,7 +1218,7 @@ static int loop_clr_fd(struct loop_device *lo)
12181218

12191219
lo->lo_flags |= LO_FLAGS_AUTOCLEAR;
12201220
if (disk_openers(lo->lo_disk) == 1)
1221-
lo->lo_state = Lo_rundown;
1221+
WRITE_ONCE(lo->lo_state, Lo_rundown);
12221222
loop_global_unlock(lo, true);
12231223

12241224
return 0;
@@ -1743,7 +1743,7 @@ static void lo_release(struct gendisk *disk)
17431743

17441744
mutex_lock(&lo->lo_mutex);
17451745
if (lo->lo_state == Lo_bound && (lo->lo_flags & LO_FLAGS_AUTOCLEAR))
1746-
lo->lo_state = Lo_rundown;
1746+
WRITE_ONCE(lo->lo_state, Lo_rundown);
17471747

17481748
need_clear = (lo->lo_state == Lo_rundown);
17491749
mutex_unlock(&lo->lo_mutex);
@@ -1858,7 +1858,7 @@ static blk_status_t loop_queue_rq(struct blk_mq_hw_ctx *hctx,
18581858

18591859
blk_mq_start_request(rq);
18601860

1861-
if (lo->lo_state != Lo_bound)
1861+
if (data_race(READ_ONCE(lo->lo_state)) != Lo_bound)
18621862
return BLK_STS_IOERR;
18631863

18641864
switch (req_op(rq)) {
@@ -2016,7 +2016,7 @@ static int loop_add(int i)
20162016
lo->worker_tree = RB_ROOT;
20172017
INIT_LIST_HEAD(&lo->idle_worker_list);
20182018
timer_setup(&lo->timer, loop_free_idle_workers_timer, TIMER_DEFERRABLE);
2019-
lo->lo_state = Lo_unbound;
2019+
WRITE_ONCE(lo->lo_state, Lo_unbound);
20202020

20212021
err = mutex_lock_killable(&loop_ctl_mutex);
20222022
if (err)
@@ -2174,7 +2174,7 @@ static int loop_control_remove(int idx)
21742174
goto mark_visible;
21752175
}
21762176
/* Mark this loop device as no more bound, but not quite unbound yet */
2177-
lo->lo_state = Lo_deleting;
2177+
WRITE_ONCE(lo->lo_state, Lo_deleting);
21782178
mutex_unlock(&lo->lo_mutex);
21792179

21802180
loop_remove(lo);
@@ -2197,8 +2197,12 @@ static int loop_control_get_free(int idx)
21972197
if (ret)
21982198
return ret;
21992199
idr_for_each_entry(&loop_index_idr, lo, id) {
2200-
/* Hitting a race results in creating a new loop device which is harmless. */
2201-
if (lo->idr_visible && data_race(lo->lo_state) == Lo_unbound)
2200+
/*
2201+
* Hitting a race results in creating a new loop device
2202+
* which is harmless.
2203+
*/
2204+
if (lo->idr_visible &&
2205+
data_race(READ_ONCE(lo->lo_state)) == Lo_unbound)
22022206
goto found;
22032207
}
22042208
mutex_unlock(&loop_ctl_mutex);

0 commit comments

Comments
 (0)