block: remove redundant GD_NEED_PART_SCAN in add_disk_final()
Subsystems (2)
PATCH 1/1 block: remove redundant GD_NEED_PART_SCAN in add_disk_final()
add_disk_final() sets GD_NEED_PART_SCAN before calling bdev_add(), then calls disk_scan_partitions() which sets the flag itself. The early set is redundant and introduces a race.
Between bdev_add() and disk_scan_partitions(), concurrent openers (multipathd, blkid, LVM) see the flag in blkdev_get_whole() and trigger bdev_disk_changed(). When disk_scan_partitions() then runs, it calls bdev_disk_changed() again, dropping the partitions the concurrent opener already created before re-adding them, which can result in transient partition disappearances.
The race is observable by inserting an msleep() between bdev_add() and disk_scan_partitions() while running concurrent open() calls during device bind. Without artificial delay, it manifests under scheduling pressure during boot on systems with aggressive device scanners (multipathd, systemd-udevd).
Therefore, do not set GD_NEED_PART_SCAN in add_disk_final(). Other GD_NEED_PART_SCAN consumers (blkdev_get_whole(), sd_need_revalidate()) should not be affected as the flag is set internally by disk_scan_partitions().
The retry-on-next-open intention from commit e5cfefa97bcc ("block: fix scan partition for exclusively open device again") should also not be affected as the early return paths in disk_scan_partitions() should be unreachable at device registration time (bd_holder is NULL and open_partitions is zero).
--- block/genhd.c | 4 ---- 1 file changed, 4 deletions(-)
block/genhd.c +0 -4
@@ -407,10 +407,6 @@ static void add_disk_final(struct gendisk *disk)struct device *ddev =disk_to_dev (disk);if (!(disk->flags &GENHD_FL_HIDDEN )) { -/* Make sure the first partition scan will be proceed */ -if (get_capacity (disk) &&disk_has_partscan (disk)) -set_bit (GD_NEED_PART_SCAN , &disk->state); -bdev_add (disk->part0, ddev->devt);if (get_capacity (disk))disk_scan_partitions (disk,BLK_OPEN_READ );
--
2.47.3
Looks good:
Hi Jens,
Have you had a chance to look at this patch?
Thanks,
Connor
Quoted 4 lines · Connor Williamson
add_disk_final()setsGD_NEED_PART_SCANbefore callingbdev_add(), then callsdisk_scan_partitions()which sets the flag itself. The early set is redundant and introduces a race.
Quoted 8 lines
Between
bdev_add()anddisk_scan_partitions(), concurrent openers (multipathd, blkid, LVM) see the flag inblkdev_get_whole()and triggerbdev_disk_changed(). Whendisk_scan_partitions()then runs, it callsbdev_disk_changed()again, dropping the partitions the concurrent opener already created before re-adding them, which can result in transient partition disappearances.[...]
Applied, thanks!
[1/1] block: remove redundant GD_NEED_PART_SCAN in add_disk_final()
commit: 181bb9c9eae4f69fe510a62a42c2932d0314a800
Best regards,
Signature · 1 line
Jens Axboe