Skip to content

Commit 6050fa4

Browse files
Tetsuo Handaaxboe
authored andcommitted
loop: don't hold lo_mutex during __loop_clr_fd()
syzbot is reporting circular locking problem at __loop_clr_fd() [1], for commit 87579e9 ("loop: use worker per cgroup instead of kworker") is calling destroy_workqueue() with lo->lo_mutex held. Since all functions where lo->lo_state matters are already checking lo->lo_state with lo->lo_mutex held (in order to avoid racing with e.g. ioctl(LOOP_CTL_REMOVE)), and __loop_clr_fd() can be called from either ioctl(LOOP_CLR_FD) xor close(), lo->lo_state == Lo_rundown is considered as an exclusive lock for __loop_clr_fd(). Therefore, hold lo->lo_mutex inside __loop_clr_fd() only when asserting/updating lo->lo_state. Since ioctl(LOOP_CLR_FD) depends on lo->lo_state == Lo_bound, a valid lo->lo_backing_file must have been assigned by ioctl(LOOP_SET_FD) or ioctl(LOOP_CONFIGURE). Thus, we can remove lo->lo_backing_file test, and convert __loop_clr_fd() into a void function. Link: https://syzkaller.appspot.com/bug?extid=63614029dfb79abd4383 [1] Reported-by: syzbot <syzbot+63614029dfb79abd4383@syzkaller.appspotmail.com> Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> Reviewed-by: Christoph Hellwig <hch@lst.de> Link: https://lore.kernel.org/r/8ebe3b2e-8975-7f26-0620-7144a3b8b8cd@i-love.sakura.ne.jp Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent a30e344 commit 6050fa4

1 file changed

Lines changed: 22 additions & 33 deletions

File tree

‎drivers/block/loop.c‎

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,13 +1082,10 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
10821082
return error;
10831083
}
10841084

1085-
static int __loop_clr_fd(struct loop_device *lo, bool release)
1085+
static void __loop_clr_fd(struct loop_device *lo, bool release)
10861086
{
1087-
struct file *filp = NULL;
1087+
struct file *filp;
10881088
gfp_t gfp = lo->old_gfp_mask;
1089-
int err = 0;
1090-
bool partscan = false;
1091-
int lo_number;
10921089
struct loop_worker *pos, *worker;
10931090

10941091
/*
@@ -1103,17 +1100,14 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
11031100
* became visible.
11041101
*/
11051102

1103+
/*
1104+
* Since this function is called upon "ioctl(LOOP_CLR_FD)" xor "close()
1105+
* after ioctl(LOOP_CLR_FD)", it is a sign of something going wrong if
1106+
* lo->lo_state has changed while waiting for lo->lo_mutex.
1107+
*/
11061108
mutex_lock(&lo->lo_mutex);
1107-
if (WARN_ON_ONCE(lo->lo_state != Lo_rundown)) {
1108-
err = -ENXIO;
1109-
goto out_unlock;
1110-
}
1111-
1112-
filp = lo->lo_backing_file;
1113-
if (filp == NULL) {
1114-
err = -EINVAL;
1115-
goto out_unlock;
1116-
}
1109+
BUG_ON(lo->lo_state != Lo_rundown);
1110+
mutex_unlock(&lo->lo_mutex);
11171111

11181112
if (test_bit(QUEUE_FLAG_WC, &lo->lo_queue->queue_flags))
11191113
blk_queue_write_cache(lo->lo_queue, false, false);
@@ -1134,6 +1128,7 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
11341128
del_timer_sync(&lo->timer);
11351129

11361130
spin_lock_irq(&lo->lo_lock);
1131+
filp = lo->lo_backing_file;
11371132
lo->lo_backing_file = NULL;
11381133
spin_unlock_irq(&lo->lo_lock);
11391134

@@ -1153,12 +1148,11 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
11531148
module_put(THIS_MODULE);
11541149
blk_mq_unfreeze_queue(lo->lo_queue);
11551150

1156-
partscan = lo->lo_flags & LO_FLAGS_PARTSCAN;
1157-
lo_number = lo->lo_number;
11581151
disk_force_media_change(lo->lo_disk, DISK_EVENT_MEDIA_CHANGE);
1159-
out_unlock:
1160-
mutex_unlock(&lo->lo_mutex);
1161-
if (partscan) {
1152+
1153+
if (lo->lo_flags & LO_FLAGS_PARTSCAN) {
1154+
int err;
1155+
11621156
/*
11631157
* open_mutex has been held already in release path, so don't
11641158
* acquire it if this function is called in such case.
@@ -1174,24 +1168,20 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
11741168
mutex_unlock(&lo->lo_disk->open_mutex);
11751169
if (err)
11761170
pr_warn("%s: partition scan of loop%d failed (rc=%d)\n",
1177-
__func__, lo_number, err);
1171+
__func__, lo->lo_number, err);
11781172
/* Device is gone, no point in returning error */
1179-
err = 0;
11801173
}
11811174

11821175
/*
11831176
* lo->lo_state is set to Lo_unbound here after above partscan has
1184-
* finished.
1185-
*
1186-
* There cannot be anybody else entering __loop_clr_fd() as
1187-
* lo->lo_backing_file is already cleared and Lo_rundown state
1188-
* protects us from all the other places trying to change the 'lo'
1189-
* device.
1177+
* finished. There cannot be anybody else entering __loop_clr_fd() as
1178+
* Lo_rundown state protects us from all the other places trying to
1179+
* change the 'lo' device.
11901180
*/
1191-
mutex_lock(&lo->lo_mutex);
11921181
lo->lo_flags = 0;
11931182
if (!part_shift)
11941183
lo->lo_disk->flags |= GENHD_FL_NO_PART;
1184+
mutex_lock(&lo->lo_mutex);
11951185
lo->lo_state = Lo_unbound;
11961186
mutex_unlock(&lo->lo_mutex);
11971187

@@ -1200,9 +1190,7 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
12001190
* lo_mutex triggers a circular lock dependency possibility warning as
12011191
* fput can take open_mutex which is usually taken before lo_mutex.
12021192
*/
1203-
if (filp)
1204-
fput(filp);
1205-
return err;
1193+
fput(filp);
12061194
}
12071195

12081196
static int loop_clr_fd(struct loop_device *lo)
@@ -1234,7 +1222,8 @@ static int loop_clr_fd(struct loop_device *lo)
12341222
lo->lo_state = Lo_rundown;
12351223
mutex_unlock(&lo->lo_mutex);
12361224

1237-
return __loop_clr_fd(lo, false);
1225+
__loop_clr_fd(lo, false);
1226+
return 0;
12381227
}
12391228

12401229
static int

0 commit comments

Comments
 (0)