[PATCH] dm init: ensure block device is ready before creating mapped device

jaeyuel.im
2026-02-06T05:05:43Z

Changes in v2:
- Pass autoload parameter for new blkdev_get_no_open()

Changes in v3:
- Exported to a public header for both blkdev_get_no_open() and
blkdev_put_no_open()

Link: https://patchwork.kernel.org/project/dm-devel/patch/20251212000955.171808-1-jaeyuel.im@lge.com/
Signed-off-by: jaeyuel.im <jaeyuel.im@lge.com>
---
drivers/md/dm-init.c | 14 ++++++++++++++
include/linux/blkdev.h | 3 +++
2 files changed, 17 insertions(+)

diff --git a/drivers/md/dm-init.c b/drivers/md/dm-init.c
index b37bbe762500..b3905e094ffc 100644
--- a/drivers/md/dm-init.c
+++ b/drivers/md/dm-init.c
@@ -296,10 +296,24 @@ static int __init dm_init_init(void)
for (i = 0; i < ARRAY_SIZE(waitfor); i++) {
if (waitfor[i]) {
dev_t dev;
+ struct block_device *bdev;

DMINFO("waiting for device %s ...", waitfor[i]);
while (early_lookup_bdev(waitfor[i], &dev))
fsleep(5000);
+
+ /*
+ * early_lookup_bdev() only checks if the device node exists and
+ * returns the dev_t. It does not guarantee that the underlying
+ * block device is fully initialized and ready to be opened. On
+ * some platforms, this can lead to a race condition where
+ * dm_early_create() fails because the device is not yet ready.
+ * Ensure the block device is truly available by attempting to
+ * get it.
+ */
+ while (!(bdev = blkdev_get_no_open(dev, false)))
+ fsleep(5000);
+ blkdev_put_no_open(bdev);
}
}

diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index 72e34acd439c..7f4a05b536ca 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -1873,4 +1873,7 @@ static inline int bio_split_rw_at(struct bio *bio,

#define DEFINE_IO_COMP_BATCH(name) struct io_comp_batch name = { }

+struct block_device *blkdev_get_no_open(dev_t dev, bool autoload);
+void blkdev_put_no_open(struct block_device *bdev);
+
#endif /* _LINUX_BLKDEV_H */
--
2.34.1