Skip to content

Commit 0da03ca

Browse files
jankaraaxboe
authored andcommitted
loop: Fix deadlock when calling blkdev_reread_part()
Calling blkdev_reread_part() under loop_ctl_mutex causes lockdep to complain about circular lock dependency between bdev->bd_mutex and lo->lo_ctl_mutex. The problem is that on loop device open or close lo_open() and lo_release() get called with bdev->bd_mutex held and they need to acquire loop_ctl_mutex. OTOH when loop_reread_partitions() is called with loop_ctl_mutex held, it will call blkdev_reread_part() which acquires bdev->bd_mutex. See syzbot report for details [1]. Move call to blkdev_reread_part() in __loop_clr_fd() from under loop_ctl_mutex to finish fixing of the lockdep warning and the possible deadlock. [1] https://syzkaller.appspot.com/bug?id=bf154052f0eea4bc7712499e4569505907d1588 Reported-by: syzbot <syzbot+4684a000d5abdade83fac55b1e7d1f935ef1936e@syzkaller.appspotmail.com> Signed-off-by: Jan Kara <jack@suse.cz> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 85b0a54 commit 0da03ca

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

drivers/block/loop.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,12 +1030,14 @@ loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
10301030
return err;
10311031
}
10321032

1033-
static int __loop_clr_fd(struct loop_device *lo)
1033+
static int __loop_clr_fd(struct loop_device *lo, bool release)
10341034
{
10351035
struct file *filp = NULL;
10361036
gfp_t gfp = lo->old_gfp_mask;
10371037
struct block_device *bdev = lo->lo_device;
10381038
int err = 0;
1039+
bool partscan = false;
1040+
int lo_number;
10391041

10401042
mutex_lock(&loop_ctl_mutex);
10411043
if (WARN_ON_ONCE(lo->lo_state != Lo_rundown)) {
@@ -1088,7 +1090,15 @@ static int __loop_clr_fd(struct loop_device *lo)
10881090
module_put(THIS_MODULE);
10891091
blk_mq_unfreeze_queue(lo->lo_queue);
10901092

1091-
if (lo->lo_flags & LO_FLAGS_PARTSCAN && bdev) {
1093+
partscan = lo->lo_flags & LO_FLAGS_PARTSCAN && bdev;
1094+
lo_number = lo->lo_number;
1095+
lo->lo_flags = 0;
1096+
if (!part_shift)
1097+
lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN;
1098+
loop_unprepare_queue(lo);
1099+
out_unlock:
1100+
mutex_unlock(&loop_ctl_mutex);
1101+
if (partscan) {
10921102
/*
10931103
* bd_mutex has been held already in release path, so don't
10941104
* acquire it if this function is called in such case.
@@ -1097,21 +1107,15 @@ static int __loop_clr_fd(struct loop_device *lo)
10971107
* must be at least one and it can only become zero when the
10981108
* current holder is released.
10991109
*/
1100-
if (!atomic_read(&lo->lo_refcnt))
1110+
if (release)
11011111
err = __blkdev_reread_part(bdev);
11021112
else
11031113
err = blkdev_reread_part(bdev);
11041114
pr_warn("%s: partition scan of loop%d failed (rc=%d)\n",
1105-
__func__, lo->lo_number, err);
1115+
__func__, lo_number, err);
11061116
/* Device is gone, no point in returning error */
11071117
err = 0;
11081118
}
1109-
lo->lo_flags = 0;
1110-
if (!part_shift)
1111-
lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN;
1112-
loop_unprepare_queue(lo);
1113-
out_unlock:
1114-
mutex_unlock(&loop_ctl_mutex);
11151119
/*
11161120
* Need not hold loop_ctl_mutex to fput backing file.
11171121
* Calling fput holding loop_ctl_mutex triggers a circular
@@ -1152,7 +1156,7 @@ static int loop_clr_fd(struct loop_device *lo)
11521156
lo->lo_state = Lo_rundown;
11531157
mutex_unlock(&loop_ctl_mutex);
11541158

1155-
return __loop_clr_fd(lo);
1159+
return __loop_clr_fd(lo, false);
11561160
}
11571161

11581162
static int
@@ -1713,7 +1717,7 @@ static void lo_release(struct gendisk *disk, fmode_t mode)
17131717
* In autoclear mode, stop the loop thread
17141718
* and remove configuration after last close.
17151719
*/
1716-
__loop_clr_fd(lo);
1720+
__loop_clr_fd(lo, true);
17171721
return;
17181722
} else if (lo->lo_state == Lo_bound) {
17191723
/*

0 commit comments

Comments
 (0)