loop: Move loop_reread_partitions() out of loop_ctl_mutex
authorJan Kara <jack@suse.cz>
Thu, 8 Nov 2018 13:01:13 +0000 (14:01 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Tue, 22 Jan 2019 20:40:37 +0000 (21:40 +0100)
commit 85b0a54a82e4fbceeb1aebb7cb6909edd1a24668 upstream.

Calling loop_reread_partitions() 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 all calls of loop_rescan_partitions() out of loop_ctl_mutex to
avoid lockdep warning and fix deadlock possibility.

[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>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/block/loop.c

index db73fb5..0d54c3e 100644 (file)
@@ -680,6 +680,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
 {
        struct file     *file, *old_file;
        int             error;
+       bool            partscan;
 
        error = mutex_lock_killable_nested(&loop_ctl_mutex, 1);
        if (error)
@@ -721,9 +722,10 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
        blk_mq_unfreeze_queue(lo->lo_queue);
 
        fput(old_file);
-       if (lo->lo_flags & LO_FLAGS_PARTSCAN)
-               loop_reread_partitions(lo, bdev);
+       partscan = lo->lo_flags & LO_FLAGS_PARTSCAN;
        mutex_unlock(&loop_ctl_mutex);
+       if (partscan)
+               loop_reread_partitions(lo, bdev);
        return 0;
 
 out_putf:
@@ -904,6 +906,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
        int             lo_flags = 0;
        int             error;
        loff_t          size;
+       bool            partscan;
 
        /* This is safe, since we have a reference from open(). */
        __module_get(THIS_MODULE);
@@ -970,14 +973,15 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
        lo->lo_state = Lo_bound;
        if (part_shift)
                lo->lo_flags |= LO_FLAGS_PARTSCAN;
-       if (lo->lo_flags & LO_FLAGS_PARTSCAN)
-               loop_reread_partitions(lo, bdev);
+       partscan = lo->lo_flags & LO_FLAGS_PARTSCAN;
 
        /* Grab the block_device to prevent its destruction after we
         * put /dev/loopXX inode. Later in __loop_clr_fd() we bdput(bdev).
         */
        bdgrab(bdev);
        mutex_unlock(&loop_ctl_mutex);
+       if (partscan)
+               loop_reread_partitions(lo, bdev);
        return 0;
 
 out_unlock:
@@ -1158,6 +1162,8 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
        int err;
        struct loop_func_table *xfer;
        kuid_t uid = current_uid();
+       struct block_device *bdev;
+       bool partscan = false;
 
        err = mutex_lock_killable_nested(&loop_ctl_mutex, 1);
        if (err)
@@ -1246,10 +1252,13 @@ out_unfreeze:
             !(lo->lo_flags & LO_FLAGS_PARTSCAN)) {
                lo->lo_flags |= LO_FLAGS_PARTSCAN;
                lo->lo_disk->flags &= ~GENHD_FL_NO_PART_SCAN;
-               loop_reread_partitions(lo, lo->lo_device);
+               bdev = lo->lo_device;
+               partscan = true;
        }
 out_unlock:
        mutex_unlock(&loop_ctl_mutex);
+       if (partscan)
+               loop_reread_partitions(lo, bdev);
 
        return err;
 }