[PATCH AUTOSEL 5.12 62/63] block: fix a race between del_gendisk and BLKRRPART

From: Sasha Levin
Date: Mon May 24 2021 - 10:52:01 EST


From: Gulam Mohamed <gulam.mohamed@xxxxxxxxxx>

[ Upstream commit bc6a385132601c29a6da1dbf8148c0d3c9ad36dc ]

When BLKRRPART is called concurrently with del_gendisk, the partitions
rescan can create a stale partition that will never be be cleaned up.

Fix this by checking the the disk is up before rescanning partitions
while under bd_mutex.

Signed-off-by: Gulam Mohamed <gulam.mohamed@xxxxxxxxxx>
[hch: split from a larger patch]
Signed-off-by: Christoph Hellwig <hch@xxxxxx>
Reviewed-by: Ming Lei <ming.lei@xxxxxxxxxx>
Link: https://lore.kernel.org/r/20210514131842.1600568-3-hch@xxxxxx
Signed-off-by: Jens Axboe <axboe@xxxxxxxxx>
Signed-off-by: Sasha Levin <sashal@xxxxxxxxxx>
---
fs/block_dev.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/fs/block_dev.c b/fs/block_dev.c
index a5a6a7930e5e..389609bc5aa3 100644
--- a/fs/block_dev.c
+++ b/fs/block_dev.c
@@ -1244,6 +1244,9 @@ int bdev_disk_changed(struct block_device *bdev, bool invalidate)

lockdep_assert_held(&bdev->bd_mutex);

+ if (!(disk->flags & GENHD_FL_UP))
+ return -ENXIO;
+
rescan:
ret = blk_drop_partitions(bdev);
if (ret)
--
2.30.2