2019-02-22 14:08:01

by Dongli Zhang

[permalink] [raw]
Subject: [PATCH v3 0/2] loop: fix two issues introduced by prior commit

This patch set fix two issues introduced by prior commit.


[PATCH v3 1/2] loop: do not print warn message if partition scan is successful
[PATCH v3 1/2] fixes d57f3374ba48 ("loop: Move special partition reread
handling in loop_clr_fd()") to not always print warn message even when
partition scan is successful.


[PATCH v3 2/2] loop: set GENHD_FL_NO_PART_SCAN after blkdev_reread_part()
[PATCH v3 2/2] fixes 0da03cab87e6 ("loop: Fix deadlock when calling
blkdev_reread_part()") to not set GENHD_FL_NO_PART_SCAN before partition
scan when detaching loop device from the file.

Changed since v1:
* move the setting of lo->lo_state to Lo_unbound after partscan has finished as well
(suggested by Jan Kara)

Changed since v2:
* Put the code inline in __loop_clr_fd() but not introduce new function
(suggested by Jan Kara)


Dongli Zhang



2019-02-22 14:08:06

by Dongli Zhang

[permalink] [raw]
Subject: [PATCH v3 1/2] loop: do not print warn message if partition scan is successful

Do not print warn message when the partition scan returns 0.

Fixes: d57f3374ba48 ("loop: Move special partition reread handling in loop_clr_fd()")
Signed-off-by: Dongli Zhang <[email protected]>
Reviewed-by: Jan Kara <[email protected]>
---
drivers/block/loop.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index cf55389..7908673 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -1115,8 +1115,9 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
err = __blkdev_reread_part(bdev);
else
err = blkdev_reread_part(bdev);
- pr_warn("%s: partition scan of loop%d failed (rc=%d)\n",
- __func__, lo_number, err);
+ if (err)
+ pr_warn("%s: partition scan of loop%d failed (rc=%d)\n",
+ __func__, lo_number, err);
/* Device is gone, no point in returning error */
err = 0;
}
--
2.7.4


2019-02-22 14:08:11

by Dongli Zhang

[permalink] [raw]
Subject: [PATCH v3 2/2] loop: set GENHD_FL_NO_PART_SCAN after blkdev_reread_part()

Commit 0da03cab87e6
("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: 0da03cab87e6 ("loop: Fix deadlock when calling blkdev_reread_part()")
Signed-off-by: Dongli Zhang <[email protected]>
Reviewed-by: Jan Kara <[email protected]>
---
Changed since v1:
* move the setting of lo->lo_state to Lo_unbound after partscan has finished as well
(suggested by Jan Kara)

Changed since v2:
* Put the code inline in __loop_clr_fd() but not introduce new function
(suggested by Jan Kara)

drivers/block/loop.c | 21 +++++++++++++++++----
1 file changed, 17 insertions(+), 4 deletions(-)

diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index 7908673..7ec3297 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -1089,16 +1089,12 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
}
mapping_set_gfp_mask(filp->f_mapping, gfp);
- lo->lo_state = Lo_unbound;
/* This is safe: open() is still holding a reference. */
module_put(THIS_MODULE);
blk_mq_unfreeze_queue(lo->lo_queue);

partscan = lo->lo_flags & LO_FLAGS_PARTSCAN && bdev;
lo_number = lo->lo_number;
- lo->lo_flags = 0;
- if (!part_shift)
- lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN;
loop_unprepare_queue(lo);
out_unlock:
mutex_unlock(&loop_ctl_mutex);
@@ -1121,6 +1117,23 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
/* Device is gone, no point in returning error */
err = 0;
}
+
+ /*
+ * lo->lo_state is set to Lo_unbound here after above partscan has
+ * finished.
+ *
+ * There cannot be anybody else entering __loop_clr_fd() as
+ * lo->lo_backing_file is already cleared and Lo_rundown state
+ * protects us from all the other places trying to change the 'lo'
+ * device.
+ */
+ mutex_lock(&loop_ctl_mutex);
+ lo->lo_flags = 0;
+ if (!part_shift)
+ lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN;
+ lo->lo_state = Lo_unbound;
+ mutex_unlock(&loop_ctl_mutex);
+
/*
* Need not hold loop_ctl_mutex to fput backing file.
* Calling fput holding loop_ctl_mutex triggers a circular
--
2.7.4


2019-02-22 22:53:25

by Jens Axboe

[permalink] [raw]
Subject: Re: [PATCH v3 0/2] loop: fix two issues introduced by prior commit

On 2/22/19 7:10 AM, Dongli Zhang wrote:
> This patch set fix two issues introduced by prior commit.
>
>
> [PATCH v3 1/2] loop: do not print warn message if partition scan is successful
> [PATCH v3 1/2] fixes d57f3374ba48 ("loop: Move special partition reread
> handling in loop_clr_fd()") to not always print warn message even when
> partition scan is successful.
>
>
> [PATCH v3 2/2] loop: set GENHD_FL_NO_PART_SCAN after blkdev_reread_part()
> [PATCH v3 2/2] fixes 0da03cab87e6 ("loop: Fix deadlock when calling
> blkdev_reread_part()") to not set GENHD_FL_NO_PART_SCAN before partition
> scan when detaching loop device from the file.
>
> Changed since v1:
> * move the setting of lo->lo_state to Lo_unbound after partscan has finished as well
> (suggested by Jan Kara)
>
> Changed since v2:
> * Put the code inline in __loop_clr_fd() but not introduce new function
> (suggested by Jan Kara)

Applied, thanks.

--
Jens Axboe