Skip to contentSkip to conversation

Fix dm-crypt handling of emulated zone append

PATCH Applied Damien Le Moal 3 participants 13 messages Jun 11, 2025 – Jun 24, 2025 Device mapper

lore
Share & tools
lore
#0 lore raw

cover Fix dm-crypt handling of emulated zone append

Mike, Mikulas,

dm-crypt splitting a regular write used for emulating a zone append operation breaks the writeen sector return for zone append, causing all sorts of issues (e.g. corruptions) with file systems using the zone append operation (btrfs, xfs). The first patch addresses this issue. The second patch adds a check to detect if a DM target attempts to split such BIO.

Damien Le Moal (2):
dm: dm-crypt: Prevent splitting of emulated zone append operations
dm: Check for forbidden splitting of emulated zone append operations

 drivers/md/dm-crypt.c | 12 ++++++++++++
 drivers/md/dm.c       |  6 ++++--
 2 files changed, 16 insertions(+), 2 deletions(-)
Signature · 1 line
2.49.0
#1 lore raw

PATCH 1/2 dm: dm-crypt: Prevent splitting of emulated zone append operations

For a zoned dm-crypt device, the native support for zone append operations (REQ_OP_ZONE_APPEND) is not enabled and instead dm-crypt relies on the emulation done by the block layer using regular write operations. This still requires to properly return the written sector as the BIO sector of the original BIO. However, this can be done correctly only and only if there is a single clone BIO used for processing the original zone append operation issued by the user. If the size of a zone append operation is larger than dm-crypt max_write_size, then the orginal BIO will be split and processed as a chain of regular write operations. Such chaining result in an incorrect written sector being returned to the zone append issuer using the original BIO sector. This in turn results in file system data corruptions using xfs or btrfs.

Fix this by modifying crypt_io_hints() to cap the max_hw_sectors limit for the dm-crypt device to the max_write_size limit. As this limit also caps the device max_zone_append_sectors limit, this forces the caller to issue zone append operations smaller than this limit, thus moving the splitting of large append operations to the caller instead of the incorrect internal splitting.

This change does not have any effect on the size of BIOs received by dm-crypt since the block layer does not automatically splits BIOs for BIO-based devices. So there is no impact on the performance of regular read and write operations.

Fixes f211268ed1f9 ("dm: Use the block layer zone append emulation")
---
 drivers/md/dm-crypt.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/md/dm-crypt.c +12 -0
index 9dfdb63220d7..7b1e88781a82 100644
@@ -3730,6 +3730,18 @@ static void crypt_io_hints(struct dm_target *ti, struct queue_limits *limits)
 		max_t(unsigned int, limits->physical_block_size, cc->sector_size);
 	limits->io_min = max_t(unsigned int, limits->io_min, cc->sector_size);
 	limits->dma_alignment = limits->logical_block_size - 1;
+
+	/*
+	 * For zoned devices, we cannot split write operations used to emulate
+	 * zone append operations. And since all write requests are going to be
+	 * split on get_max_request_size(cc, true) size, apply this limit to the
+	 * maximum hardware I/O size so that we have a cap on the
+	 * max_zone_append_sectors limit when the zone limits are validated by
+	 * the block layer.
+	 */
+	if (ti->emulate_zone_append)
+		limits->max_hw_sectors = min(limits->max_hw_sectors,
+					     get_max_request_size(cc, true));
 }
 
 static struct target_type crypt_target = {
Signature · 1 line
2.49.0
maintainer Applied
#3 lore raw

Re: [PATCH 1/2] dm: dm-crypt: Prevent splitting of emulated zone append operations

In reply to Damien Le Moal

Applied, thanks.

Mikulas

Quoted 58 lines · Damien Le Moal

For a zoned dm-crypt device, the native support for zone append operations (REQ_OP_ZONE_APPEND) is not enabled and instead dm-crypt relies on the emulation done by the block layer using regular write operations. This still requires to properly return the written sector as the BIO sector of the original BIO. However, this can be done correctly only and only if there is a single clone BIO used for processing the original zone append operation issued by the user. If the size of a zone append operation is larger than dm-crypt max_write_size, then the orginal BIO will be split and processed as a chain of regular write operations. Such chaining result in an incorrect written sector being returned to the zone append issuer using the original BIO sector. This in turn results in file system data corruptions using xfs or btrfs.

Fix this by modifying crypt_io_hints() to cap the max_hw_sectors limit for the dm-crypt device to the max_write_size limit. As this limit also caps the device max_zone_append_sectors limit, this forces the caller to issue zone append operations smaller than this limit, thus moving the splitting of large append operations to the caller instead of the incorrect internal splitting.

This change does not have any effect on the size of BIOs received by dm-crypt since the block layer does not automatically splits BIOs for BIO-based devices. So there is no impact on the performance of regular read and write operations.

Fixes f211268ed1f9 ("dm: Use the block layer zone append emulation")
---
 drivers/md/dm-crypt.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/md/dm-crypt.c +12 -0
index 9dfdb63220d7..7b1e88781a82 100644
@@ -3730,6 +3730,18 @@ static void crypt_io_hints(struct dm_target *ti, struct queue_limits *limits)
 		max_t(unsigned int, limits->physical_block_size, cc->sector_size);
 	limits->io_min = max_t(unsigned int, limits->io_min, cc->sector_size);
 	limits->dma_alignment = limits->logical_block_size - 1;
+
+	/*
+	 * For zoned devices, we cannot split write operations used to emulate
+	 * zone append operations. And since all write requests are going to be
+	 * split on get_max_request_size(cc, true) size, apply this limit to the
+	 * maximum hardware I/O size so that we have a cap on the
+	 * max_zone_append_sectors limit when the zone limits are validated by
+	 * the block layer.
+	 */
+	if (ti->emulate_zone_append)
+		limits->max_hw_sectors = min(limits->max_hw_sectors,
+					     get_max_request_size(cc, true));
 }
 
 static struct target_type crypt_target = {
Signature · 1 line
2.49.0
maintainer
#4 lore raw

Re: [PATCH 1/2] dm: dm-crypt: Prevent splitting of emulated zone append operations

Quoted 3 lines · Mikulas Patocka

Applied, thanks.

Mikulas

Applied and reverted :)

I've just realized that this patch won't work because the value get_max_request_size is changeable by the user - it's in the file /sys/module/dm_crypt/parameters/max_write_size. So, if there is some large value when you create the device and later the user sets smaller value, dm-crypt will still split emulated zone append bios.

I suggest to set the limit limits->max_hw_sectors to BIO_MAX_VECS << PAGE_SHIFT (that's the maximum allowable value for dm-crypt) and then modify dm-crypt to not split bios that have the flag BIO_EMULATES_ZONE_APPEND.

Mikulas

Quoted 61 lines · Damien Le Moal
Quoted 59 lines

For a zoned dm-crypt device, the native support for zone append operations (REQ_OP_ZONE_APPEND) is not enabled and instead dm-crypt relies on the emulation done by the block layer using regular write operations. This still requires to properly return the written sector as the BIO sector of the original BIO. However, this can be done correctly only and only if there is a single clone BIO used for processing the original zone append operation issued by the user. If the size of a zone append operation is larger than dm-crypt max_write_size, then the orginal BIO will be split and processed as a chain of regular write operations. Such chaining result in an incorrect written sector being returned to the zone append issuer using the original BIO sector. This in turn results in file system data corruptions using xfs or btrfs.

Fix this by modifying crypt_io_hints() to cap the max_hw_sectors limit for the dm-crypt device to the max_write_size limit. As this limit also caps the device max_zone_append_sectors limit, this forces the caller to issue zone append operations smaller than this limit, thus moving the splitting of large append operations to the caller instead of the incorrect internal splitting.

This change does not have any effect on the size of BIOs received by dm-crypt since the block layer does not automatically splits BIOs for BIO-based devices. So there is no impact on the performance of regular read and write operations.

Fixes f211268ed1f9 ("dm: Use the block layer zone append emulation")
---
 drivers/md/dm-crypt.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/md/dm-crypt.c +12 -0
index 9dfdb63220d7..7b1e88781a82 100644
@@ -3730,6 +3730,18 @@ static void crypt_io_hints(struct dm_target *ti, struct queue_limits *limits)
 		max_t(unsigned int, limits->physical_block_size, cc->sector_size);
 	limits->io_min = max_t(unsigned int, limits->io_min, cc->sector_size);
 	limits->dma_alignment = limits->logical_block_size - 1;
+
+	/*
+	 * For zoned devices, we cannot split write operations used to emulate
+	 * zone append operations. And since all write requests are going to be
+	 * split on get_max_request_size(cc, true) size, apply this limit to the
+	 * maximum hardware I/O size so that we have a cap on the
+	 * max_zone_append_sectors limit when the zone limits are validated by
+	 * the block layer.
+	 */
+	if (ti->emulate_zone_append)
+		limits->max_hw_sectors = min(limits->max_hw_sectors,
+					     get_max_request_size(cc, true));
 }
 
 static struct target_type crypt_target = {
Signature · 1 line
2.49.0
#5 lore raw

Re: [PATCH 1/2] dm: dm-crypt: Prevent splitting of emulated zone append operations

Quoted 8 lines · Mikulas Patocka
Quoted 3 lines · Mikulas Patocka

Applied, thanks.

Mikulas

Quoted 7 lines

Applied and reverted :)

I've just realized that this patch won't work because the value get_max_request_size is changeable by the user - it's in the file /sys/module/dm_crypt/parameters/max_write_size. So, if there is some large value when you create the device and later the user sets smaller value, dm-crypt will still split emulated zone append bios.

Indeed. Good catch.

Quoted 4 lines

I suggest to set the limit limits->max_hw_sectors to BIO_MAX_VECS << PAGE_SHIFT (that's the maximum allowable value for dm-crypt) and then modify dm-crypt to not split bios that have the flag BIO_EMULATES_ZONE_APPEND.

I think we simply need to do a limit update when the user modifies the write limit through sysfs. That will prevent things from going out of sync if the user ever modifies the limit.

Of note is that this patch turns out to be more than just about zone append emulation. We absolutely need it for any write request (for emulating zone append or regular writes) to avoid the accept partial return which would trigger issuing another BIO. In this case, we are at risk of deadlocking with queue freeze on blk_queue_enter() in the submission path. So this patch, and another one forcing splitting of all write BIOs to zoned DM devices is needed to avoid this deadlock.

I will send a v2 with the additional patch and a better commit message for this patch explaining this.

Quoted 65 lines

Mikulas

Quoted 61 lines · Damien Le Moal
Quoted 59 lines

For a zoned dm-crypt device, the native support for zone append operations (REQ_OP_ZONE_APPEND) is not enabled and instead dm-crypt relies on the emulation done by the block layer using regular write operations. This still requires to properly return the written sector as the BIO sector of the original BIO. However, this can be done correctly only and only if there is a single clone BIO used for processing the original zone append operation issued by the user. If the size of a zone append operation is larger than dm-crypt max_write_size, then the orginal BIO will be split and processed as a chain of regular write operations. Such chaining result in an incorrect written sector being returned to the zone append issuer using the original BIO sector. This in turn results in file system data corruptions using xfs or btrfs.

Fix this by modifying crypt_io_hints() to cap the max_hw_sectors limit for the dm-crypt device to the max_write_size limit. As this limit also caps the device max_zone_append_sectors limit, this forces the caller to issue zone append operations smaller than this limit, thus moving the splitting of large append operations to the caller instead of the incorrect internal splitting.

This change does not have any effect on the size of BIOs received by dm-crypt since the block layer does not automatically splits BIOs for BIO-based devices. So there is no impact on the performance of regular read and write operations.

Fixes f211268ed1f9 ("dm: Use the block layer zone append emulation")
---
 drivers/md/dm-crypt.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/md/dm-crypt.c +12 -0
index 9dfdb63220d7..7b1e88781a82 100644
@@ -3730,6 +3730,18 @@ static void crypt_io_hints(struct dm_target *ti, struct queue_limits *limits)
 		max_t(unsigned int, limits->physical_block_size, cc->sector_size);
 	limits->io_min = max_t(unsigned int, limits->io_min, cc->sector_size);
 	limits->dma_alignment = limits->logical_block_size - 1;
+
+	/*
+	 * For zoned devices, we cannot split write operations used to emulate
+	 * zone append operations. And since all write requests are going to be
+	 * split on get_max_request_size(cc, true) size, apply this limit to the
+	 * maximum hardware I/O size so that we have a cap on the
+	 * max_zone_append_sectors limit when the zone limits are validated by
+	 * the block layer.
+	 */
+	if (ti->emulate_zone_append)
+		limits->max_hw_sectors = min(limits->max_hw_sectors,
+					     get_max_request_size(cc, true));
 }
 
 static struct target_type crypt_target = {
Signature · 1 line
2.49.0
Signature · 2 lines
Damien Le Moal
Western Digital Research
maintainer
#6 lore raw

PATCH v2 1/1 dm-crypt: Prevent splitting of emulated zone append operations

Quoted 19 lines · Damien Le Moal
Quoted 8 lines · Mikulas Patocka
Quoted 3 lines · Mikulas Patocka

Applied, thanks.

Mikulas

Quoted 7 lines

Applied and reverted :)

I've just realized that this patch won't work because the value get_max_request_size is changeable by the user - it's in the file /sys/module/dm_crypt/parameters/max_write_size. So, if there is some large value when you create the device and later the user sets smaller value, dm-crypt will still split emulated zone append bios.

Indeed. Good catch.

Quoted 8 lines
Quoted 4 lines

I suggest to set the limit limits->max_hw_sectors to BIO_MAX_VECS << PAGE_SHIFT (that's the maximum allowable value for dm-crypt) and then modify dm-crypt to not split bios that have the flag BIO_EMULATES_ZONE_APPEND.

I think we simply need to do a limit update when the user modifies the write limit through sysfs. That will prevent things from going out of sync if the user ever modifies the limit.

I think that's too complicated.

The sysfs limit was originally introduced to improve performance through concurrent encryption - if dm-crypt splits large bio to several smaller bios, it may improve performance because these smaller bios are encrypted by multiple cores in parallel.

As zoned devices won't see improved performance from limiting write size, we can just ignore the sysfs limit for zone append operations.

Quoted 10 lines

Of note is that this patch turns out to be more than just about zone append emulation. We absolutely need it for any write request (for emulating zone append or regular writes) to avoid the accept partial return which would trigger issuing another BIO. In this case, we are at risk of deadlocking with queue freeze on blk_queue_enter() in the submission path. So this patch, and another one forcing splitting of all write BIOs to zoned DM devices is needed to avoid this deadlock.

I will send a v2 with the additional patch and a better commit message for this patch explaining this.

I've created this patch - please review it and test it.

Mikulas

From: Mikulas Patocka <[email protected]>

For a zoned dm-crypt device, the native support for zone append operations (REQ_OP_ZONE_APPEND) is not enabled and instead dm-crypt relies on the emulation done by the block layer using regular write operations. This still requires to properly return the written sector as the BIO sector of the original BIO. However, this can be done correctly only and only if there is a single clone BIO used for processing the original zone append operation issued by the user. If the size of a zone append operation is larger than dm-crypt max_write_size, then the orginal BIO will be split and processed as a chain of regular write operations. Such chaining result in an incorrect written sector being returned to the zone append issuer using the original BIO sector. This in turn results in file system data corruptions using xfs or btrfs.

Fix this by modifying crypt_io_hints() to cap the max_hw_sectors limit for the dm-crypt device to the BIO_MAX_VECS << PAGE_SHIFT limit. As this limit also caps the device max_zone_append_sectors limit, this forces the caller to issue zone append operations smaller or equal than this limit, thus moving the splitting of large append operations to the caller instead of the incorrect internal splitting.

This change does not have any effect on the size of BIOs received by dm-crypt since the block layer does not automatically splits BIOs for BIO-based devices. So there is no impact on the performance of regular read and write operations.

Fixes f211268ed1f9 ("dm: Use the block layer zone append emulation")
---
 drivers/md/dm-crypt.c |   24 +++++++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)
drivers/md/dm-crypt.c +21 -3
Index: linux-dm/drivers/md/dm-crypt.c
===================================================================
@@ -3496,9 +3496,15 @@ static int crypt_map(struct dm_target *t
 	/*
 	 * Check if bio is too large, split as needed.
 	 */
-	max_sectors = get_max_request_size(cc, bio_data_dir(bio) == WRITE);
-	if (unlikely(bio_sectors(bio) > max_sectors))
-		dm_accept_partial_bio(bio, max_sectors);
+	if (unlikely(bio_flagged(bio, BIO_EMULATES_ZONE_APPEND))) {
+		max_sectors = BIO_MAX_VECS << PAGE_SHIFT >> SECTOR_SHIFT;
+		if (unlikely(bio_sectors(bio) > max_sectors))
+			return DM_MAPIO_KILL;
+	} else {
+		max_sectors = get_max_request_size(cc, bio_data_dir(bio) == WRITE);
+		if (unlikely(bio_sectors(bio) > max_sectors))
+			dm_accept_partial_bio(bio, max_sectors);
+	}
 
 	/*
 	 * Ensure that bio is a multiple of internal sector encryption size
@@ -3733,6 +3739,18 @@ static void crypt_io_hints(struct dm_tar
 		max_t(unsigned int, limits->physical_block_size, cc->sector_size);
 	limits->io_min = max_t(unsigned int, limits->io_min, cc->sector_size);
 	limits->dma_alignment = limits->logical_block_size - 1;
+
+	/*
+	 * For zoned devices, we cannot split write operations used to emulate
+	 * zone append operations. And since all write requests must be split on
+	 * BIO_MAX_VECS << PAGE_SHIFT size, apply this limit to the maximum
+	 * hardware I/O size so that we have a cap on the
+	 * max_zone_append_sectors limit when the zone limits are validated by
+	 * the block layer.
+	 */
+	if (ti->emulate_zone_append)
+		limits->max_hw_sectors = min(limits->max_hw_sectors,
+				BIO_MAX_VECS << PAGE_SHIFT >> SECTOR_SHIFT);
 }
 
 static struct target_type crypt_target = {
#7 lore raw

Re: [PATCH v2] dm-crypt: Prevent splitting of emulated zone append operations

Quoted 104 lines · Mikulas Patocka
Quoted 19 lines · Damien Le Moal
Quoted 8 lines · Mikulas Patocka
Quoted 3 lines · Mikulas Patocka

Applied, thanks.

Mikulas

Quoted 7 lines

Applied and reverted :)

I've just realized that this patch won't work because the value get_max_request_size is changeable by the user - it's in the file /sys/module/dm_crypt/parameters/max_write_size. So, if there is some large value when you create the device and later the user sets smaller value, dm-crypt will still split emulated zone append bios.

Indeed. Good catch.

Quoted 8 lines
Quoted 4 lines

I suggest to set the limit limits->max_hw_sectors to BIO_MAX_VECS << PAGE_SHIFT (that's the maximum allowable value for dm-crypt) and then modify dm-crypt to not split bios that have the flag BIO_EMULATES_ZONE_APPEND.

I think we simply need to do a limit update when the user modifies the write limit through sysfs. That will prevent things from going out of sync if the user ever modifies the limit.

I think that's too complicated.

The sysfs limit was originally introduced to improve performance through concurrent encryption - if dm-crypt splits large bio to several smaller bios, it may improve performance because these smaller bios are encrypted by multiple cores in parallel.

As zoned devices won't see improved performance from limiting write size, we can just ignore the sysfs limit for zone append operations.

Quoted 10 lines

Of note is that this patch turns out to be more than just about zone append emulation. We absolutely need it for any write request (for emulating zone append or regular writes) to avoid the accept partial return which would trigger issuing another BIO. In this case, we are at risk of deadlocking with queue freeze on blk_queue_enter() in the submission path. So this patch, and another one forcing splitting of all write BIOs to zoned DM devices is needed to avoid this deadlock.

I will send a v2 with the additional patch and a better commit message for this patch explaining this.

I've created this patch - please review it and test it.

Mikulas

From: Mikulas Patocka <[email protected]>

For a zoned dm-crypt device, the native support for zone append operations (REQ_OP_ZONE_APPEND) is not enabled and instead dm-crypt relies on the emulation done by the block layer using regular write operations. This still requires to properly return the written sector as the BIO sector of the original BIO. However, this can be done correctly only and only if there is a single clone BIO used for processing the original zone append operation issued by the user. If the size of a zone append operation is larger than dm-crypt max_write_size, then the orginal BIO will be split and processed as a chain of regular write operations. Such chaining result in an incorrect written sector being returned to the zone append issuer using the original BIO sector. This in turn results in file system data corruptions using xfs or btrfs.

Fix this by modifying crypt_io_hints() to cap the max_hw_sectors limit for the dm-crypt device to the BIO_MAX_VECS << PAGE_SHIFT limit. As this limit also caps the device max_zone_append_sectors limit, this forces the caller to issue zone append operations smaller or equal than this limit, thus moving the splitting of large append operations to the caller instead of the incorrect internal splitting.

This change does not have any effect on the size of BIOs received by dm-crypt since the block layer does not automatically splits BIOs for BIO-based devices. So there is no impact on the performance of regular read and write operations.

Fixes f211268ed1f9 ("dm: Use the block layer zone append emulation")
---
 drivers/md/dm-crypt.c |   24 +++++++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)

Index: linux-dm/drivers/md/dm-crypt.c
===================================================================

drivers/md/dm-crypt.c +0 -2
@@ -3496,9 +3496,15 @@ static int crypt_map(struct dm_target *t
 	/*
 	 * Check if bio is too large, split as needed.
 	 */
-	max_sectors = get_max_request_size(cc, bio_data_dir(bio) == WRITE);
-	if (unlikely(bio_sectors(bio) > max_sectors))

drivers/md/dm-crypt.c static int crypt_map(struct dm_target *t · line 3507

Quoted 10 lines
drivers/md/dm-crypt.c +9 -1
-		dm_accept_partial_bio(bio, max_sectors);
+	if (unlikely(bio_flagged(bio, BIO_EMULATES_ZONE_APPEND))) {
+		max_sectors = BIO_MAX_VECS << PAGE_SHIFT >> SECTOR_SHIFT;
+		if (unlikely(bio_sectors(bio) > max_sectors))
+			return DM_MAPIO_KILL;
+	} else {
+		max_sectors = get_max_request_size(cc, bio_data_dir(bio) == WRITE);
+		if (unlikely(bio_sectors(bio) > max_sectors))
+			dm_accept_partial_bio(bio, max_sectors);
+	}

Thanks.

I think we can simplify all this by passing the bio to get_max_request_size() and move the bio_flagged() test there.

I will correct this and send patches tomorrow. This patch is now in fact more than just for zone append. It must also operate on regular write as otherwise we can endup deadlocking with queue freeze operations. There is another patch needed to go with this, to force a call to bio split to limit for any write to a zoned DM device in dm.c, before passing the BIO to the target driver. So all of this needs some more massaging. Sending stuff tomorrow after testing.

Quoted 23 lines
drivers/md/dm-crypt.c +12 -0
 	/*
 	 * Ensure that bio is a multiple of internal sector encryption size
@@ -3733,6 +3739,18 @@ static void crypt_io_hints(struct dm_tar
 		max_t(unsigned int, limits->physical_block_size, cc->sector_size);
 	limits->io_min = max_t(unsigned int, limits->io_min, cc->sector_size);
 	limits->dma_alignment = limits->logical_block_size - 1;
+
+	/*
+	 * For zoned devices, we cannot split write operations used to emulate
+	 * zone append operations. And since all write requests must be split on
+	 * BIO_MAX_VECS << PAGE_SHIFT size, apply this limit to the maximum
+	 * hardware I/O size so that we have a cap on the
+	 * max_zone_append_sectors limit when the zone limits are validated by
+	 * the block layer.
+	 */
+	if (ti->emulate_zone_append)
+		limits->max_hw_sectors = min(limits->max_hw_sectors,
+				BIO_MAX_VECS << PAGE_SHIFT >> SECTOR_SHIFT);
 }
 
 static struct target_type crypt_target = {
Signature · 2 lines
Damien Le Moal
Western Digital Research
#8 lore raw

PATCH 2/2 dm: Check for forbidden splitting of emulated zone append operations

In reply to Damien Le Moal

When a DM target does not support native zone append, the block layer zone write plugging will transform a REQ_OP_ZONE_APPEND operation into a regular REQ_OP_WRITE BIO flagged with BIO_EMULATES_ZONE_APPEND. A DM target must always be able to process such write request without resorting to calling dm_accept_partial_bio() as otherwise, the original BIO cannot indicate to the issuer a correct written sector on completion. Make sure that dm_accept_partial_bio() is never called for an emulated zone append BIO with a BUG_ON() check in dm_accept_partial_bio().

---
 drivers/md/dm.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/md/dm.c +4 -2
index 55579adbeb3f..21920991ab28 100644
@@ -1286,8 +1286,9 @@ static size_t dm_dax_recovery_write(struct dax_device *dax_dev, pgoff_t pgoff,
 /*
  * A target may call dm_accept_partial_bio only from the map routine.  It is
  * allowed for all bio types except REQ_PREFLUSH, REQ_OP_ZONE_* zone management
- * operations, REQ_OP_ZONE_APPEND (zone append writes) and any bio serviced by
- * __send_duplicate_bios().
+ * operations, zone append writes (native with REQ_OP_ZONE_APPEND or emulated
+ * with write BIOs flagged with BIO_EMULATES_ZONE_APPEND) and any bio serviced
+ * by __send_duplicate_bios().
  *
  * dm_accept_partial_bio informs the dm that the target only wants to process
  * additional n_sectors sectors of the bio and the rest of the data should be
@@ -1322,6 +1323,7 @@ void dm_accept_partial_bio(struct bio *bio, unsigned int n_sectors)
 	BUG_ON(dm_tio_flagged(tio, DM_TIO_IS_DUPLICATE_BIO));
 	BUG_ON(op_is_zone_mgmt(bio_op(bio)));
 	BUG_ON(bio_op(bio) == REQ_OP_ZONE_APPEND);
+	BUG_ON(bio_flagged(bio, BIO_EMULATES_ZONE_APPEND));
 	BUG_ON(bio_sectors > *tio->len_ptr);
 	BUG_ON(n_sectors > bio_sectors);
 
Signature · 1 line
2.49.0
#9 lore raw

Re: [PATCH 2/2] dm: Check for forbidden splitting of emulated zone append operations

drivers/md/dm.c void dm_accept_partial_bio(struct bio *bio, unsigned int n_sectors) · line 1328

Quoted 7 lines · Damien Le Moal
drivers/md/dm.c +1 -0
@@ -1322,6 +1323,7 @@ void dm_accept_partial_bio(struct bio *bio, unsigned int n_sectors)
 	BUG_ON(dm_tio_flagged(tio, DM_TIO_IS_DUPLICATE_BIO));
 	BUG_ON(op_is_zone_mgmt(bio_op(bio)));
 	BUG_ON(bio_op(bio) == REQ_OP_ZONE_APPEND);
+	BUG_ON(bio_flagged(bio, BIO_EMULATES_ZONE_APPEND));
 	BUG_ON(bio_sectors > *tio->len_ptr);
 	BUG_ON(n_sectors > bio_sectors);

Shouldn't all these be WARN_ON_ONCE and error return instead?

#10 lore raw

Re: [PATCH 2/2] dm: Check for forbidden splitting of emulated zone append operations

Quoted 10 lines · Christoph Hellwig

drivers/md/dm.c void dm_accept_partial_bio(struct bio *bio, unsigned int n_sectors) · line 1328

Quoted 7 lines · Damien Le Moal
drivers/md/dm.c +1 -0
@@ -1322,6 +1323,7 @@ void dm_accept_partial_bio(struct bio *bio, unsigned int n_sectors)
 	BUG_ON(dm_tio_flagged(tio, DM_TIO_IS_DUPLICATE_BIO));
 	BUG_ON(op_is_zone_mgmt(bio_op(bio)));
 	BUG_ON(bio_op(bio) == REQ_OP_ZONE_APPEND);
+	BUG_ON(bio_flagged(bio, BIO_EMULATES_ZONE_APPEND));
 	BUG_ON(bio_sectors > *tio->len_ptr);
 	BUG_ON(n_sectors > bio_sectors);

Shouldn't all these be WARN_ON_ONCE and error return instead?

Yes, I would prefer that, but that would be a more involved cleanup. I can switch to doing that instead of this simple check.

Signature · 2 lines
Damien Le Moal
Western Digital Research
maintainer
#11 lore raw

Re: [PATCH 2/2] dm: Check for forbidden splitting of emulated zone append operations

Quoted 12 lines · Damien Le Moal
Quoted 10 lines · Christoph Hellwig

drivers/md/dm.c void dm_accept_partial_bio(struct bio *bio, unsigned int n_sectors) · line 1328

Quoted 7 lines · Damien Le Moal
drivers/md/dm.c +1 -0
@@ -1322,6 +1323,7 @@ void dm_accept_partial_bio(struct bio *bio, unsigned int n_sectors)
 	BUG_ON(dm_tio_flagged(tio, DM_TIO_IS_DUPLICATE_BIO));
 	BUG_ON(op_is_zone_mgmt(bio_op(bio)));
 	BUG_ON(bio_op(bio) == REQ_OP_ZONE_APPEND);
+	BUG_ON(bio_flagged(bio, BIO_EMULATES_ZONE_APPEND));
 	BUG_ON(bio_sectors > *tio->len_ptr);
 	BUG_ON(n_sectors > bio_sectors);

Shouldn't all these be WARN_ON_ONCE and error return instead?

Quoted 2 lines

Yes, I would prefer that, but that would be a more involved cleanup. I can switch to doing that instead of this simple check.

dm_accept_partial_bio doesn't return anything. I think that making it return an error and then checking for this "impossible" error in all the callers would just bloat the code.

Mikulas

#12 lore raw
In reply to Damien Le Moal
Quoted 9 lines · Damien Le Moal

Mike, Mikulas,

dm-crypt splitting a regular write used for emulating a zone append operation breaks the writeen sector return for zone append, causing all sorts of issues (e.g. corruptions) with file systems using the zone append operation (btrfs, xfs). The first patch addresses this issue. The second patch adds a check to detect if a DM target attempts to split such BIO.

Quoted 3 lines

Damien Le Moal (2):
dm: dm-crypt: Prevent splitting of emulated zone append operations
dm: Check for forbidden splitting of emulated zone append operations

Ping ?

This is a bug fix. Can we get these queued for 6.16-rc ? Thank you.

Quoted 5 lines
 drivers/md/dm-crypt.c | 12 ++++++++++++
 drivers/md/dm.c       |  6 ++++--
 2 files changed, 16 insertions(+), 2 deletions(-)
Signature · 2 lines
Damien Le Moal
Western Digital Research