public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] dm init: ensure block device is ready before creating mapped device
@ 2025-12-11  7:34 jaeyuel.im
  2025-12-11  8:19 ` Dongsheng Yang
  0 siblings, 1 reply; 4+ messages in thread
From: jaeyuel.im @ 2025-12-11  7:34 UTC (permalink / raw)
  To: Alasdair Kergon, Mike Snitzer, Mikulas Patocka
  Cc: dm-devel, linux-kernel, jaeyuel.im

From: "jaeyuel.im" <jaeyuel.im@lge.com>

The current implementation of dm_init_init() uses early_lookup_bdev() to
wait for the device node to appear. However, early_lookup_bdev() only
verifies that the device node exists and returns the dev_t. It does not
guarantee that the underlying block device structure is fully initialized
and ready for I/O operations or to be opened.

On certain platforms (e.g., embedded systems with specific storage
drivers), this can lead to a race condition where dm_early_create()
attempts to open the device immediately after early_lookup_bdev() returns,
but fails because the device is not yet fully ready. This results in boot
failures as the mapped device cannot be created.

This patch adds an additional check using blkdev_get_no_open() after
early_lookup_bdev() returns. This ensures that the struct block_device is
actually available and the device is ready to be opened, effectively
preventing the race condition.

Signed-off-by: jaeyuel.im <jaeyuel.im@lge.com>
---
 drivers/md/dm-init.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/drivers/md/dm-init.c b/drivers/md/dm-init.c
index ff9420f06483..76b84ca39906 100644
--- a/drivers/md/dm-init.c
+++ b/drivers/md/dm-init.c
@@ -299,10 +299,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)))
+				fsleep(5000);
+			blkdev_put_no_open(bdev);
 		}
 	}
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] dm init: ensure block device is ready before creating mapped device
  2025-12-11  7:34 [PATCH] dm init: ensure block device is ready before creating mapped device jaeyuel.im
@ 2025-12-11  8:19 ` Dongsheng Yang
  0 siblings, 0 replies; 4+ messages in thread
From: Dongsheng Yang @ 2025-12-11  8:19 UTC (permalink / raw)
  To: jaeyuel.im, Alasdair Kergon, Mike Snitzer, Mikulas Patocka
  Cc: dm-devel, linux-kernel


在 12/11/2025 3:34 PM, jaeyuel.im@lge.com 写道:
> From: "jaeyuel.im" <jaeyuel.im@lge.com>
>
> The current implementation of dm_init_init() uses early_lookup_bdev() to
> wait for the device node to appear. However, early_lookup_bdev() only
> verifies that the device node exists and returns the dev_t. It does not
> guarantee that the underlying block device structure is fully initialized
> and ready for I/O operations or to be opened.
>
> On certain platforms (e.g., embedded systems with specific storage
> drivers), this can lead to a race condition where dm_early_create()
> attempts to open the device immediately after early_lookup_bdev() returns,
> but fails because the device is not yet fully ready. This results in boot
> failures as the mapped device cannot be created.
>
> This patch adds an additional check using blkdev_get_no_open() after
> early_lookup_bdev() returns. This ensures that the struct block_device is
> actually available and the device is ready to be opened, effectively
> preventing the race condition.
>
> Signed-off-by: jaeyuel.im <jaeyuel.im@lge.com>
> ---
>   drivers/md/dm-init.c | 14 ++++++++++++++
>   1 file changed, 14 insertions(+)
>
> diff --git a/drivers/md/dm-init.c b/drivers/md/dm-init.c
> index ff9420f06483..76b84ca39906 100644
> --- a/drivers/md/dm-init.c
> +++ b/drivers/md/dm-init.c
> @@ -299,10 +299,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)))

You need to pass autoload parameter for new blkdev_get_no_open()

"struct block_device *blkdev_get_no_open(dev_t dev, bool autoload)"


Thanx

> +				fsleep(5000);
> +			blkdev_put_no_open(bdev);
>   		}
>   	}
>   

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH] dm init: ensure block device is ready before creating mapped device
  2025-12-12  0:09 [PATCH v2] " jaeyuel.im
@ 2026-02-06  5:05 ` jaeyuel.im
  2026-02-08  6:21   ` Benjamin Marzinski
  0 siblings, 1 reply; 4+ messages in thread
From: jaeyuel.im @ 2026-02-06  5:05 UTC (permalink / raw)
  To: Alasdair Kergon, Mike Snitzer, Mikulas Patocka
  Cc: dm-devel, linux-kernel, jaeyuel.im

From: "jaeyuel.im" <jaeyuel.im@lge.com>

The current implementation of dm_init_init() uses early_lookup_bdev() to
wait for the device node to appear. However, early_lookup_bdev() only
verifies that the device node exists and returns the dev_t. It does not
guarantee that the underlying block device structure is fully initialized
and ready for I/O operations or to be opened.

On certain platforms (e.g., embedded systems with specific storage
drivers), this can lead to a race condition where dm_early_create()
attempts to open the device immediately after early_lookup_bdev() returns,
but fails because the device is not yet fully ready. This results in boot
failures as the mapped device cannot be created.

This patch adds an additional check using blkdev_get_no_open() after
early_lookup_bdev() returns. This ensures that the struct block_device is
actually available and the device is ready to be opened, effectively
preventing the race condition.

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


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] dm init: ensure block device is ready before creating mapped device
  2026-02-06  5:05 ` [PATCH] " jaeyuel.im
@ 2026-02-08  6:21   ` Benjamin Marzinski
  0 siblings, 0 replies; 4+ messages in thread
From: Benjamin Marzinski @ 2026-02-08  6:21 UTC (permalink / raw)
  To: jaeyuel.im
  Cc: Alasdair Kergon, Mike Snitzer, Mikulas Patocka, dm-devel,
	linux-kernel, Jens Axboe, linux-block

On Fri, Feb 06, 2026 at 05:05:26AM +0000, jaeyuel.im@lge.com wrote:
> From: "jaeyuel.im" <jaeyuel.im@lge.com>
> 
> The current implementation of dm_init_init() uses early_lookup_bdev() to
> wait for the device node to appear. However, early_lookup_bdev() only
> verifies that the device node exists and returns the dev_t. It does not
> guarantee that the underlying block device structure is fully initialized
> and ready for I/O operations or to be opened.
> 
> On certain platforms (e.g., embedded systems with specific storage
> drivers), this can lead to a race condition where dm_early_create()
> attempts to open the device immediately after early_lookup_bdev() returns,
> but fails because the device is not yet fully ready. This results in boot
> failures as the mapped device cannot be created.
> 
> This patch adds an additional check using blkdev_get_no_open() after
> early_lookup_bdev() returns. This ensures that the struct block_device is
> actually available and the device is ready to be opened, effectively
> preventing the race condition.
> 
> 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>
> ---

You should put the description of your version changes here, after the
triple dash, so that they aren't part of the actual commit message. 

>  drivers/md/dm-init.c   | 14 ++++++++++++++
>  include/linux/blkdev.h |  3 +++
>  2 files changed, 17 insertions(+)
> 
> [snip] 
> 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 = { }

If you're adding declarations linux/blkdev.h, you should remove them
from block/blk.h, so the functions aren't declared twice.

Also, IMHO it seems more natural for these declarions to placed near the
related bdev_file_open_by_dev() and bdev_file_open_by_path() ones
earlier in this file.

Finally I Added Jens and linux-block to the CCs, since the patch
includes changes to the block files. FYI, scripts/get_maintainer.pl can
help you send your patches the right people and lists.

-Ben
  
> +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
> 


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-02-08  6:21 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-11  7:34 [PATCH] dm init: ensure block device is ready before creating mapped device jaeyuel.im
2025-12-11  8:19 ` Dongsheng Yang
  -- strict thread matches above, loose matches on Subject: below --
2025-12-12  0:09 [PATCH v2] " jaeyuel.im
2026-02-06  5:05 ` [PATCH] " jaeyuel.im
2026-02-08  6:21   ` Benjamin Marzinski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox