Skip to content

Commit 758a58d

Browse files
Dongli Zhangaxboe
authored andcommitted
loop: set GENHD_FL_NO_PART_SCAN after blkdev_reread_part()
Commit 0da03ca ("loop: Fix deadlock when calling blkdev_reread_part()") moves blkdev_reread_part() out of the loop_ctl_mutex. However, GENHD_FL_NO_PART_SCAN is set before __blkdev_reread_part(). As a result, __blkdev_reread_part() will fail the check of GENHD_FL_NO_PART_SCAN and will not rescan the loop device to delete all partitions. Below are steps to reproduce the issue: step1 # dd if=/dev/zero of=tmp.raw bs=1M count=100 step2 # losetup -P /dev/loop0 tmp.raw step3 # parted /dev/loop0 mklabel gpt step4 # parted -a none -s /dev/loop0 mkpart primary 64s 1 step5 # losetup -d /dev/loop0 Step5 will not be able to delete /dev/loop0p1 (introduced by step4) and there is below kernel warning message: [ 464.414043] __loop_clr_fd: partition scan of loop0 failed (rc=-22) This patch sets GENHD_FL_NO_PART_SCAN after blkdev_reread_part(). Fixes: 0da03ca ("loop: Fix deadlock when calling blkdev_reread_part()") Signed-off-by: Dongli Zhang <dongli.zhang@oracle.com> Reviewed-by: Jan Kara <jack@suse.cz> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 40853d6 commit 758a58d

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

drivers/block/loop.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,16 +1089,12 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
10891089
kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
10901090
}
10911091
mapping_set_gfp_mask(filp->f_mapping, gfp);
1092-
lo->lo_state = Lo_unbound;
10931092
/* This is safe: open() is still holding a reference. */
10941093
module_put(THIS_MODULE);
10951094
blk_mq_unfreeze_queue(lo->lo_queue);
10961095

10971096
partscan = lo->lo_flags & LO_FLAGS_PARTSCAN && bdev;
10981097
lo_number = lo->lo_number;
1099-
lo->lo_flags = 0;
1100-
if (!part_shift)
1101-
lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN;
11021098
loop_unprepare_queue(lo);
11031099
out_unlock:
11041100
mutex_unlock(&loop_ctl_mutex);
@@ -1121,6 +1117,23 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
11211117
/* Device is gone, no point in returning error */
11221118
err = 0;
11231119
}
1120+
1121+
/*
1122+
* lo->lo_state is set to Lo_unbound here after above partscan has
1123+
* finished.
1124+
*
1125+
* There cannot be anybody else entering __loop_clr_fd() as
1126+
* lo->lo_backing_file is already cleared and Lo_rundown state
1127+
* protects us from all the other places trying to change the 'lo'
1128+
* device.
1129+
*/
1130+
mutex_lock(&loop_ctl_mutex);
1131+
lo->lo_flags = 0;
1132+
if (!part_shift)
1133+
lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN;
1134+
lo->lo_state = Lo_unbound;
1135+
mutex_unlock(&loop_ctl_mutex);
1136+
11241137
/*
11251138
* Need not hold loop_ctl_mutex to fput backing file.
11261139
* Calling fput holding loop_ctl_mutex triggers a circular

0 commit comments

Comments
 (0)