Skip to contentSkip to conversation

dm: Preserve the order of REQ_PREFLUSH writes

PATCH Superseded Bart Van Assche 2 participants 6 messages Sep 9, 2025 – Sep 10, 2025 Device mapper

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dm: Preserve the order of REQ_PREFLUSH writes

Splitting a bio into an empty flush bio and a non-flush data bio is only necessary if there are multiple underlying devices. Do not split REQ_PREFLUSH bios if there is only one underlying device. This patch preserves the order of REQ_PREFLUSH writes if there is only one underlying device and if multiple writes are queued. This patch fixes a write error observed with zoned storage devices and with write pipelining enabled.

Cc Mike Snitzer <[email protected]>
Cc Damien Le Moal <[email protected]>
---
 drivers/md/dm.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
drivers/md/dm.c +10 -6
index 66dd5f6ce778..d0791eef21f7 100644
@@ -490,9 +490,13 @@ u64 dm_start_time_ns_from_clone(struct bio *bio)
 }
 EXPORT_SYMBOL_GPL(dm_start_time_ns_from_clone);
 
-static inline bool bio_is_flush_with_data(struct bio *bio)
+static inline bool bio_is_flush_with_data(struct mapped_device *md,
+					  struct bio *bio)
 {
-	return ((bio->bi_opf & REQ_PREFLUSH) && bio->bi_iter.bi_size);
+	guard(rcu)();
+
+	return bio->bi_opf & REQ_PREFLUSH && bio->bi_iter.bi_size &&
+	       ((struct dm_table *)rcu_dereference(md->map))->num_targets > 1;
 }
 
 static inline unsigned int dm_io_sectors(struct dm_io *io, struct bio *bio)
@@ -501,7 +505,7 @@ static inline unsigned int dm_io_sectors(struct dm_io *io, struct bio *bio)
 	 * If REQ_PREFLUSH set, don't account payload, it will be
 	 * submitted (and accounted) after this flush completes.
 	 */
-	if (bio_is_flush_with_data(bio))
+	if (bio_is_flush_with_data(io->md, bio))
 		return 0;
 	if (unlikely(dm_io_flagged(io, DM_IO_WAS_SPLIT)))
 		return io->sectors;
@@ -976,7 +980,7 @@ static void __dm_io_complete(struct dm_io *io, bool first_stage)
 	if (requeued)
 		return;
 
-	if (bio_is_flush_with_data(bio)) {
+	if (bio_is_flush_with_data(md, bio)) {
 		/*
 		 * Preflush done for flush with data, reissue
 		 * without REQ_PREFLUSH.
@@ -1715,7 +1719,7 @@ static void dm_queue_poll_io(struct bio *bio, struct dm_io *io)
 }
 
 /*
- * Select the correct strategy for processing a non-flush bio.
+ * Select the correct strategy for processing a bio.
  */
 static blk_status_t __split_and_process_bio(struct clone_info *ci)
 {
@@ -1996,7 +2000,7 @@ static void dm_split_and_process_bio(struct mapped_device *md,
 	}
 	init_clone_info(&ci, io, map, bio, is_abnormal);
 
-	if (bio->bi_opf & REQ_PREFLUSH) {
+	if (bio->bi_opf & REQ_PREFLUSH && ci.map->num_targets > 1) {
 		__send_empty_flush(&ci);
 		/* dm_io_complete submits any data associated with flush */
 		goto out;
#1 lore raw
Quoted 7 lines · Bart Van Assche

Splitting a bio into an empty flush bio and a non-flush data bio is only necessary if there are multiple underlying devices. Do not split REQ_PREFLUSH bios if there is only one underlying device. This patch preserves the order of REQ_PREFLUSH writes if there is only one underlying device and if multiple writes are queued. This patch fixes a write error observed with zoned storage devices and with write pipelining enabled.

Write pipelining is not upstream. So is this fixing an actual problem with upstream kernels ?

If that is the case and you have a reproducer, please share it (and maybe make it a blktest case).

Quoted 64 lines
Cc Mike Snitzer <[email protected]>
Cc Damien Le Moal <[email protected]>
---
 drivers/md/dm.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
drivers/md/dm.c +10 -6
index 66dd5f6ce778..d0791eef21f7 100644
@@ -490,9 +490,13 @@ u64 dm_start_time_ns_from_clone(struct bio *bio)
 }
 EXPORT_SYMBOL_GPL(dm_start_time_ns_from_clone);
 
-static inline bool bio_is_flush_with_data(struct bio *bio)
+static inline bool bio_is_flush_with_data(struct mapped_device *md,
+					  struct bio *bio)
 {
-	return ((bio->bi_opf & REQ_PREFLUSH) && bio->bi_iter.bi_size);
+	guard(rcu)();
+
+	return bio->bi_opf & REQ_PREFLUSH && bio->bi_iter.bi_size &&
+	       ((struct dm_table *)rcu_dereference(md->map))->num_targets > 1;
 }
 
 static inline unsigned int dm_io_sectors(struct dm_io *io, struct bio *bio)
@@ -501,7 +505,7 @@ static inline unsigned int dm_io_sectors(struct dm_io *io, struct bio *bio)
 	 * If REQ_PREFLUSH set, don't account payload, it will be
 	 * submitted (and accounted) after this flush completes.
 	 */
-	if (bio_is_flush_with_data(bio))
+	if (bio_is_flush_with_data(io->md, bio))
 		return 0;
 	if (unlikely(dm_io_flagged(io, DM_IO_WAS_SPLIT)))
 		return io->sectors;
@@ -976,7 +980,7 @@ static void __dm_io_complete(struct dm_io *io, bool first_stage)
 	if (requeued)
 		return;
 
-	if (bio_is_flush_with_data(bio)) {
+	if (bio_is_flush_with_data(md, bio)) {
 		/*
 		 * Preflush done for flush with data, reissue
 		 * without REQ_PREFLUSH.
@@ -1715,7 +1719,7 @@ static void dm_queue_poll_io(struct bio *bio, struct dm_io *io)
 }
 
 /*
- * Select the correct strategy for processing a non-flush bio.
+ * Select the correct strategy for processing a bio.
  */
 static blk_status_t __split_and_process_bio(struct clone_info *ci)
 {
@@ -1996,7 +2000,7 @@ static void dm_split_and_process_bio(struct mapped_device *md,
 	}
 	init_clone_info(&ci, io, map, bio, is_abnormal);
 
-	if (bio->bi_opf & REQ_PREFLUSH) {
+	if (bio->bi_opf & REQ_PREFLUSH && ci.map->num_targets > 1) {
 		__send_empty_flush(&ci);
 		/* dm_io_complete submits any data associated with flush */
 		goto out;
Signature · 2 lines
Damien Le Moal
Western Digital Research
#2 lore raw
Quoted 10 lines · Damien Le Moal
Quoted 7 lines · Bart Van Assche

Splitting a bio into an empty flush bio and a non-flush data bio is only necessary if there are multiple underlying devices. Do not split REQ_PREFLUSH bios if there is only one underlying device. This patch preserves the order of REQ_PREFLUSH writes if there is only one underlying device and if multiple writes are queued. This patch fixes a write error observed with zoned storage devices and with write pipelining enabled.

Write pipelining is not upstream.

That doesn't matter in this case because this patch also improves performance without write pipelining, even for conventional (non zoned) storage devices.

BTW, I'm still waiting for a review from you for this patch series that was posted two weeks ago: "[PATCH v24 00/18] Improve write performance for zoned UFS devices"
(https://lore.kernel.org/linux-block/[email protected]/)

Thanks,

Bart.

#3 lore raw
Quoted 10 lines · Bart Van Assche
Quoted 9 lines · Damien Le Moal
Quoted 7 lines · Bart Van Assche

Splitting a bio into an empty flush bio and a non-flush data bio is only necessary if there are multiple underlying devices. Do not split REQ_PREFLUSH bios if there is only one underlying device. This patch preserves the order of REQ_PREFLUSH writes if there is only one underlying device and if multiple writes are queued. This patch fixes a write error observed with zoned storage devices and with write pipelining enabled.

Quoted 5 lines
Quoted 1 line

Write pipelining is not upstream.

That doesn't matter in this case because this patch also improves performance without write pipelining, even for conventional (non zoned) storage devices.

Then please rewrite the commit message to clearly state that and add Fixes tags as appropriate. Do not mention things that are not upstream please.

Quoted 4 lines

BTW, I'm still waiting for a review from you for this patch series that was posted two weeks ago: "[PATCH v24 00/18] Improve write performance for zoned UFS devices"
(https://lore.kernel.org/linux-block/[email protected]/)

Yes, I still have this series in my inbox. I have been very busy and have lots of conferences coming up so the review is going to be slow.

Signature · 2 lines
Damien Le Moal
Western Digital Research
#4 lore raw
Quoted 2 lines · Damien Le Moal

Then please rewrite the commit message to clearly state that and add Fixes tags as appropriate.

Hi Damien,

Do we agree that sending REQ_PREFLUSH and data as a single bio instead of as two bios is a performance optimization that neither affects which data gets written nor the behavior in case of a power failure? Do we agree that the Fixes tag should only be used for bug fixes and not for performance optimizations?

Thanks,

Bart.

#5 lore raw
Quoted 5 lines · Bart Van Assche
Quoted 2 lines · Damien Le Moal

Then please rewrite the commit message to clearly state that and add Fixes tags as appropriate.

Quoted 7 lines

Hi Damien,

Do we agree that sending REQ_PREFLUSH and data as a single bio instead of as two bios is a performance optimization that neither affects which data gets written nor the behavior in case of a power failure? Do we agree that the Fixes tag should only be used for bug fixes and not for performance optimizations?

Yes, fixes tag apply to patches that fix a problem. I know that.

Your commit message was mentioning a problem but was not clear since it was mingled with write pipelining, which is not upstream.

For the REQ_PREFLUSH, I did spend a lot of time with the flush machinery to get it to work with zone write plugging (when I was developing that). I probably should have spent more time with DM handling of that request. I do not recall exactly what it does. Would need to reread the code, but I am too busy for that until next week. So I cannot comment about what you are trying to do. Make your commit message clearer to help reviewing please.

Signature · 2 lines
Damien Le Moal
Western Digital Research