On 2022/03/16 20:22, Jan Kara wrote: >> @@ -1244,7 +1244,7 @@ static int loop_clr_fd(struct loop_device *lo) >> * <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d >> * command to fail with EBUSY. >> */ >> - if (atomic_read(&lo->lo_refcnt) > 1) { >> + if (lo->lo_disk->part0->bd_openers > 1) { > > But bd_openers can be read safely only under disk->open_mutex. So for this > to be safe against compiler playing nasty tricks with optimizations, we > need to either make bd_openers atomic_t or use READ_ONCE / WRITE_ONCE when > accessing it. Use of READ_ONCE() / WRITE_ONCE() are not for avoiding races but for making sure that memory access happens only once. It is data_race() which is needed for tolerating and annotating races. For example, data_race(lo->lo_state) is needed when accessing lo->lo_state without lo->lo_mutex held. Use of atomic_t for lo->lo_disk->part0->bd_openers does not help, for currently lo->lo_mutex is held in order to avoid races. That is, it is disk->open_mutex which loop_clr_fd() needs to hold when accessing lo->lo_disk->part0->bd_openers.