loop: remove lo_refcount and avoid lo_mutex in ->open / ->release
authorChristoph Hellwig <hch@lst.de>
Wed, 30 Mar 2022 05:29:16 +0000 (07:29 +0200)
committerJens Axboe <axboe@kernel.dk>
Mon, 18 Apr 2022 12:54:09 +0000 (06:54 -0600)
lo_refcount counts how many openers a loop device has, but that count
is already provided by the block layer in the bd_openers field of the
whole-disk block_device.  Remove lo_refcount and allow opens to
succeed even on devices beeing deleted - now that ->free_disk is
implemented we can handle that race gracefull and all I/O on it will
just fail. Similarly there is a small race window now where
loop_control_remove does not synchronize the delete vs the remove
due do bd_openers not being under lo_mutex protection, but we can
handle that just as gracefully.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Jan Kara <jack@suse.cz>
Link: https://lore.kernel.org/r/20220330052917.2566582-15-hch@lst.de
Signed-off-by: Jens Axboe <axboe@kernel.dk>
drivers/block/loop.c
drivers/block/loop.h

index 029398f..204558d 100644 (file)
@@ -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 (disk_openers(lo->lo_disk) > 1) {
                lo->lo_flags |= LO_FLAGS_AUTOCLEAR;
                loop_global_unlock(lo, true);
                return 0;
@@ -1725,33 +1725,15 @@ static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode,
 }
 #endif
 
-static int lo_open(struct block_device *bdev, fmode_t mode)
-{
-       struct loop_device *lo = bdev->bd_disk->private_data;
-       int err;
-
-       err = mutex_lock_killable(&lo->lo_mutex);
-       if (err)
-               return err;
-       if (lo->lo_state == Lo_deleting)
-               err = -ENXIO;
-       else
-               atomic_inc(&lo->lo_refcnt);
-       mutex_unlock(&lo->lo_mutex);
-       return err;
-}
-
 static void lo_release(struct gendisk *disk, fmode_t mode)
 {
        struct loop_device *lo = disk->private_data;
 
-       mutex_lock(&lo->lo_mutex);
-       if (atomic_dec_return(&lo->lo_refcnt))
-               goto out_unlock;
+       if (disk_openers(disk) > 0)
+               return;
 
-       if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) {
-               if (lo->lo_state != Lo_bound)
-                       goto out_unlock;
+       mutex_lock(&lo->lo_mutex);
+       if (lo->lo_state == Lo_bound && (lo->lo_flags & LO_FLAGS_AUTOCLEAR)) {
                lo->lo_state = Lo_rundown;
                mutex_unlock(&lo->lo_mutex);
                /*
@@ -1761,8 +1743,6 @@ static void lo_release(struct gendisk *disk, fmode_t mode)
                __loop_clr_fd(lo, true);
                return;
        }
-
-out_unlock:
        mutex_unlock(&lo->lo_mutex);
 }
 
@@ -1776,7 +1756,6 @@ static void lo_free_disk(struct gendisk *disk)
 
 static const struct block_device_operations lo_fops = {
        .owner =        THIS_MODULE,
-       .open =         lo_open,
        .release =      lo_release,
        .ioctl =        lo_ioctl,
 #ifdef CONFIG_COMPAT
@@ -2030,7 +2009,6 @@ static int loop_add(int i)
         */
        if (!part_shift)
                disk->flags |= GENHD_FL_NO_PART;
-       atomic_set(&lo->lo_refcnt, 0);
        mutex_init(&lo->lo_mutex);
        lo->lo_number           = i;
        spin_lock_init(&lo->lo_lock);
@@ -2120,13 +2098,12 @@ static int loop_control_remove(int idx)
        ret = mutex_lock_killable(&lo->lo_mutex);
        if (ret)
                goto mark_visible;
-       if (lo->lo_state != Lo_unbound ||
-           atomic_read(&lo->lo_refcnt) > 0) {
+       if (lo->lo_state != Lo_unbound || disk_openers(lo->lo_disk) > 0) {
                mutex_unlock(&lo->lo_mutex);
                ret = -EBUSY;
                goto mark_visible;
        }
-       /* Mark this loop device no longer open()-able. */
+       /* Mark this loop device as no more bound, but not quite unbound yet */
        lo->lo_state = Lo_deleting;
        mutex_unlock(&lo->lo_mutex);
 
index 082d4b6..449d562 100644 (file)
@@ -28,7 +28,6 @@ struct loop_func_table;
 
 struct loop_device {
        int             lo_number;
-       atomic_t        lo_refcnt;
        loff_t          lo_offset;
        loff_t          lo_sizelimit;
        int             lo_flags;