From: Yu Kuai yukuai3@huawei.com
mainline inclusion from mainline-v6.3-rc1 commit e5cfefa97bccf956ea0bb6464c1f6c84fd7a8d9f category: bugfix bugzilla: https://gitee.com/openeuler/kernel/issues/I6MRB5 CVE: NA
Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?i...
--------------------------------
As explained in commit 36369f46e917 ("block: Do not reread partition table on exclusively open device"), reread partition on the device that is exclusively opened by someone else is problematic.
This patch will make sure partition scan will only be proceed if current thread open the device exclusively, or the device is not opened exclusively, and in the later case, other scanners and exclusive openers will be blocked temporarily until partition scan is done.
Fixes: 10c70d95c0f2 ("block: remove the bd_openers checks in blk_drop_partitions") Cc: stable@vger.kernel.org Suggested-by: Jan Kara jack@suse.cz Signed-off-by: Yu Kuai yukuai3@huawei.com Reviewed-by: Christoph Hellwig hch@lst.de Link: https://lore.kernel.org/r/20230217022200.3092987-3-yukuai1@huaweicloud.com Signed-off-by: Jens Axboe axboe@kernel.dk
Conflicts: block/genhd.c block/ioctl.c Signed-off-by: Yu Kuai yukuai3@huawei.com Reviewed-by: Hou Tao houtao1@huawei.com Signed-off-by: Yongqiang Liu liuyongqiang13@huawei.com --- block/blk.h | 4 ++++ block/genhd.c | 36 ++++++++++++++++++++++++++++++++++-- block/ioctl.c | 2 +- fs/block_dev.c | 4 ++-- 4 files changed, 41 insertions(+), 5 deletions(-)
diff --git a/block/blk.h b/block/blk.h index 9269bb6b14f8..965e9c507654 100644 --- a/block/blk.h +++ b/block/blk.h @@ -214,6 +214,10 @@ unsigned int blk_plug_queued_count(struct request_queue *q); void blk_account_io_start(struct request *req, bool new_io); void blk_account_io_completion(struct request *req, unsigned int bytes); void blk_account_io_done(struct request *req, u64 now); +int bd_prepare_to_claim(struct block_device *bdev, + struct block_device *whole, void *holder); +void bd_abort_claiming(struct block_device *bdev, struct block_device *whole, + void *holder);
/* * EH timer and IO completion will both attempt to 'grab' the request, make diff --git a/block/genhd.c b/block/genhd.c index 1f981753d4e7..bf095fb5c41a 100644 --- a/block/genhd.c +++ b/block/genhd.c @@ -651,12 +651,33 @@ int disk_scan_partitions(struct gendisk *disk, fmode_t mode) if (!bdev) return -ENOMEM;
- bdev->bd_invalidated = 1; + /* + * If the device is opened exclusively by current thread already, it's + * safe to scan partitons, otherwise, use bd_prepare_to_claim() to + * synchronize with other exclusive openers and other partition + * scanners. + */ + if (!(mode & FMODE_EXCL)) { + ret = bd_prepare_to_claim(bdev, bdev, disk_scan_partitions); + if (ret) { + bdput(bdev); + return ret; + }
- ret = blkdev_get(bdev, mode, NULL); + /* Ping the bdev until bd_abort_claiming() */ + bdgrab(bdev); + } + + bdev->bd_invalidated = 1; + ret = blkdev_get(bdev, mode & ~FMODE_EXCL, NULL); if (!ret) blkdev_put(bdev, mode);
+ if (!(mode & FMODE_EXCL)) { + bd_abort_claiming(bdev, bdev, disk_scan_partitions); + bdput(bdev); + } + return ret; }
@@ -694,6 +715,7 @@ static void disk_init_partition(struct gendisk *disk) static void __device_add_disk(struct device *parent, struct gendisk *disk, bool register_queue) { + struct block_device *bdev = NULL; dev_t devt; int retval;
@@ -746,12 +768,22 @@ static void __device_add_disk(struct device *parent, struct gendisk *disk, disk_add_events(disk); blk_integrity_add(disk);
+ /* Make sure the first partition scan will be proceed */ + if (get_capacity(disk) && disk_part_scan_enabled(disk)) { + bdev = bdget_disk(disk, 0); + if (bdev) + bdev->bd_invalidated = 1; + } + /* * Set the flag at last, so that block devcie can't be opened * before it's registration is done. */ disk->flags |= GENHD_FL_UP; disk_init_partition(disk); + + if (bdev) + bdput(bdev); }
void device_add_disk(struct device *parent, struct gendisk *disk) diff --git a/block/ioctl.c b/block/ioctl.c index ddc6d340e876..911887eefc29 100644 --- a/block/ioctl.c +++ b/block/ioctl.c @@ -603,7 +603,7 @@ int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd, return -EACCES; if (bdev != bdev->bd_contains) return -EINVAL; - return disk_scan_partitions(bdev->bd_disk, mode & ~FMODE_EXCL); + return disk_scan_partitions(bdev->bd_disk, mode); case BLKGETSIZE: size = i_size_read(bdev->bd_inode); if ((size >> 9) > ~0UL) diff --git a/fs/block_dev.c b/fs/block_dev.c index b4bb16d79d78..7fa66b5bf886 100644 --- a/fs/block_dev.c +++ b/fs/block_dev.c @@ -1072,8 +1072,8 @@ static bool bd_may_claim(struct block_device *bdev, struct block_device *whole, * RETURNS: * 0 if @bdev can be claimed, -EBUSY otherwise. */ -static int bd_prepare_to_claim(struct block_device *bdev, - struct block_device *whole, void *holder) +int bd_prepare_to_claim(struct block_device *bdev, + struct block_device *whole, void *holder) { retry: spin_lock(&bdev_lock);