]> www.infradead.org Git - users/hch/block.git/commitdiff
make autoclear operation synchronous again part_tbl_mutex
authorTetsuo Handa <penguin-kernel@i-love.sakura.ne.jp>
Sun, 26 Dec 2021 07:06:27 +0000 (16:06 +0900)
committerChristoph Hellwig <hch@lst.de>
Thu, 20 Jan 2022 07:37:59 +0000 (08:37 +0100)
The kernel test robot is reporting that xfstest can fail at

  umount ext2 on xfs
  umount xfs

sequence, for commit 322c4293ecc58110 ("loop: make autoclear operation
asynchronous") broke what commit ("loop: Make explicit loop device
destruction lazy") wanted to achieve.

Although we cannot guarantee that nobody is holding a reference when
"umount xfs" is called, we should try to close a race window opened
by asynchronous autoclear operation.

It turned out that there is no need to call flush_workqueue() from
__loop_clr_fd(), for blk_mq_freeze_queue() blocks until all pending
"struct work_struct" are processed by loop_process_work().

Revert commit 322c4293ecc58110 ("loop: make autoclear operation
asynchronous"), and simply defer calling destroy_workqueue() till
loop_remove().

Since a loop device is likely reused after once created thanks to
ioctl(LOOP_CTL_GET_FREE), being unable to destroy corresponding WQ
when ioctl(LOOP_CLR_FD) is called should be an acceptable waste.

Reported-by: kernel test robot <oliver.sang@intel.com>
Suggested-by: Christoph Hellwig <hch@lst.de>
Tested-by: syzbot <syzbot+643e4ce4b6ad1347d372@syzkaller.appspotmail.com>
Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Signed-off-by: Christoph Hellwig <hch@lst.de>
drivers/block/loop.c
drivers/block/loop.h

index 998d08a18414b394214cc382a24f1fa364ab2130..ff52b9b99f4f39bc0d1abd6bebd5b919e75429d2 100644 (file)
@@ -1004,10 +1004,10 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
            !file->f_op->write_iter)
                lo->lo_flags |= LO_FLAGS_READ_ONLY;
 
-       lo->workqueue = alloc_workqueue("loop%d",
-                                       WQ_UNBOUND | WQ_FREEZABLE,
-                                       0,
-                                       lo->lo_number);
+       if (!lo->workqueue)
+               lo->workqueue = alloc_workqueue("loop%d",
+                                               WQ_UNBOUND | WQ_FREEZABLE,
+                                               0, lo->lo_number);
        if (!lo->workqueue) {
                error = -ENOMEM;
                goto out_unlock;
@@ -1113,7 +1113,6 @@ static void __loop_clr_fd(struct loop_device *lo)
        /* freeze request queue during the transition */
        blk_mq_freeze_queue(lo->lo_queue);
 
-       destroy_workqueue(lo->workqueue);
        spin_lock_irq(&lo->lo_work_lock);
        list_for_each_entry_safe(worker, pos, &lo->idle_worker_list,
                                idle_list) {
@@ -1151,39 +1150,17 @@ static void __loop_clr_fd(struct loop_device *lo)
        if (!part_shift)
                lo->lo_disk->flags |= GENHD_FL_NO_PART;
 
-       fput(filp);
-}
-
-static void loop_rundown_completed(struct loop_device *lo)
-{
        mutex_lock(&lo->lo_mutex);
        lo->lo_state = Lo_unbound;
        mutex_unlock(&lo->lo_mutex);
        module_put(THIS_MODULE);
-}
-
-static void loop_rundown_workfn(struct work_struct *work)
-{
-       struct loop_device *lo = container_of(work, struct loop_device,
-                                             rundown_work);
-       struct block_device *bdev = lo->lo_device;
-       struct gendisk *disk = lo->lo_disk;
 
-       __loop_clr_fd(lo);
-       kobject_put(&bdev->bd_device.kobj);
-       module_put(disk->fops->owner);
-       loop_rundown_completed(lo);
-}
-
-static void loop_schedule_rundown(struct loop_device *lo)
-{
-       struct block_device *bdev = lo->lo_device;
-       struct gendisk *disk = lo->lo_disk;
-
-       __module_get(disk->fops->owner);
-       kobject_get(&bdev->bd_device.kobj);
-       INIT_WORK(&lo->rundown_work, loop_rundown_workfn);
-       queue_work(system_long_wq, &lo->rundown_work);
+       /*
+        * Need not hold lo_mutex to fput backing file. Calling fput holding
+        * lo_mutex triggers a circular lock dependency possibility warning as
+        * fput can take open_mutex which is usually taken before lo_mutex.
+        */
+       fput(filp);
 }
 
 static int loop_clr_fd(struct loop_device *lo)
@@ -1216,7 +1193,6 @@ static int loop_clr_fd(struct loop_device *lo)
        mutex_unlock(&lo->lo_mutex);
 
        __loop_clr_fd(lo);
-       loop_rundown_completed(lo);
        return 0;
 }
 
@@ -1741,7 +1717,7 @@ static void lo_release(struct gendisk *disk, fmode_t mode)
                 * In autoclear mode, stop the loop thread
                 * and remove configuration after last close.
                 */
-               loop_schedule_rundown(lo);
+               __loop_clr_fd(lo);
                return;
        } else if (lo->lo_state == Lo_bound) {
                /*
@@ -2067,6 +2043,8 @@ static void loop_remove(struct loop_device *lo)
        mutex_unlock(&loop_ctl_mutex);
        /* There is no route which can find this loop device. */
        mutex_destroy(&lo->lo_mutex);
+       if (lo->workqueue)
+               destroy_workqueue(lo->workqueue);
        kfree(lo);
 }
 
index 918a7a2dc025933a50dfe1ef8bc420b51639a9c0..082d4b6bfc6a6cbe789fa2c6361ae3a88a8b3e27 100644 (file)
@@ -56,7 +56,6 @@ struct loop_device {
        struct gendisk          *lo_disk;
        struct mutex            lo_mutex;
        bool                    idr_visible;
-       struct work_struct      rundown_work;
 };
 
 struct loop_cmd {