Skip to contentSkip to conversation

dm-crypt atomic writes support

PATCH In review John Garry 2 participants 9 messages Nov 5, 2025 Device mapper

lore
Share & tools
lore

John Garry

author
#0 lore raw

cover dm-crypt atomic writes support

This small series enables atomic writes support for dm-crypt.

Apart from setting DM_TARGET_ATOMIC_WRITES flag, we need to make the following changes:
- limit atomic write size to dm-crypt max write size
- BUG_ON() any partial completions for when REQ_ATOMIC is set

John Garry (2):
dm: BUG on REQ_ATOMIC in dm_accept_partial_bio()
dm-crypt: enable DM_TARGET_ATOMIC_WRITES

 drivers/md/dm-crypt.c | 21 +++++++++++++++++++--
 drivers/md/dm.c       |  5 +++--
 2 files changed, 22 insertions(+), 4 deletions(-)
Signature · 1 line
2.43.5
author
#1 lore raw

PATCH 1/2 dm: BUG on REQ_ATOMIC in dm_accept_partial_bio()

Any bio with REQ_ATOMIC flag set should never be split or partially completed, so BUG_ON() on this scenario in dm_accept_partial_bio() (whose intent is to allow partial completions).

---
 drivers/md/dm.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/md/dm.c +3 -2
index f5e5e59b232bf..7bd020ef9db37 100644
@@ -1285,8 +1285,8 @@ 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, 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().
+ * with write BIOs flagged with BIO_EMULATES_ZONE_APPEND), any bio serviced
+ * by __send_duplicate_bios(), and REQ_ATOMIC.
  *
  * 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
@@ -1321,6 +1321,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(bio_sectors > *tio->len_ptr);
 	BUG_ON(n_sectors > bio_sectors);
+	BUG_ON(bio->bi_opf & REQ_ATOMIC);
 
 	if (static_branch_unlikely(&zoned_enabled) &&
 	    unlikely(bdev_is_zoned(bio->bi_bdev))) {
Signature · 1 line
2.43.5
author
#2 lore raw

PATCH 2/2 dm-crypt: enable DM_TARGET_ATOMIC_WRITES

In reply to John Garry

Allow handling of bios with REQ_ATOMIC flag set.

In crypt_io_hints(), the max atomic write sizes needs to be limited according to the max of DM_CRYPT_DEFAULT_MAX_WRITE_SIZE and max_write_size. Otherwise, we may need to split bios in crypt_map() -> dm_accept_partial_bio().

---
 drivers/md/dm-crypt.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
drivers/md/dm-crypt.c +19 -2
index 5ef43231fe77f..c4c97f57bc477 100644
@@ -3515,8 +3515,11 @@ static int crypt_map(struct dm_target *ti, struct bio *bio)
 	 * Check if bio is too large, split as needed.
 	 */
 	max_sectors = get_max_request_sectors(ti, bio);
-	if (unlikely(bio_sectors(bio) > max_sectors))
+	if (unlikely(bio_sectors(bio) > max_sectors)) {
+		if (bio->bi_opf & REQ_ATOMIC)
+			return DM_MAPIO_KILL;
 		dm_accept_partial_bio(bio, max_sectors);
+	}
 
 	/*
 	 * Ensure that bio is a multiple of internal sector encryption size
@@ -3744,6 +3747,8 @@ static int crypt_iterate_devices(struct dm_target *ti,
 static void crypt_io_hints(struct dm_target *ti, struct queue_limits *limits)
 {
 	struct crypt_config *cc = ti->private;
+	unsigned int _max_write_size =
+		min_not_zero(max_write_size, DM_CRYPT_DEFAULT_MAX_WRITE_SIZE);
 
 	limits->logical_block_size =
 		max_t(unsigned int, limits->logical_block_size, cc->sector_size);
@@ -3762,6 +3767,18 @@ static void crypt_io_hints(struct dm_target *ti, struct queue_limits *limits)
 	if (ti->emulate_zone_append)
 		limits->max_hw_sectors = min(limits->max_hw_sectors,
 					     BIO_MAX_VECS << PAGE_SECTORS_SHIFT);
+
+	if (_max_write_size < limits->atomic_write_hw_unit_min) {
+		limits->atomic_write_hw_unit_min = 0;
+		limits->atomic_write_hw_unit_max = 0;
+		limits->atomic_write_hw_max = 0;
+		limits->atomic_write_hw_boundary = 0;
+	} else {
+		limits->atomic_write_hw_unit_max =
+			min(limits->atomic_write_hw_unit_max, _max_write_size);
+		limits->atomic_write_hw_max =
+			min(limits->atomic_write_hw_max, _max_write_size);
+	}
 }
 
 static struct target_type crypt_target = {
@@ -3770,7 +3787,7 @@ static struct target_type crypt_target = {
 	.module = THIS_MODULE,
 	.ctr    = crypt_ctr,
 	.dtr    = crypt_dtr,
-	.features = DM_TARGET_ZONED_HM,
+	.features = DM_TARGET_ZONED_HM | DM_TARGET_ATOMIC_WRITES,
 	.report_zones = crypt_report_zones,
 	.map    = crypt_map,
 	.status = crypt_status,
Signature · 1 line
2.43.5
maintainer
#3 lore raw
In reply to John Garry
Quoted 7 lines · John Garry

This small series enables atomic writes support for dm-crypt.

Apart from setting DM_TARGET_ATOMIC_WRITES flag, we need to make the following changes:
- limit atomic write size to dm-crypt max write size
- BUG_ON() any partial completions for when REQ_ATOMIC is set

Quoted 10 lines

John Garry (2):
dm: BUG on REQ_ATOMIC in dm_accept_partial_bio()
dm-crypt: enable DM_TARGET_ATOMIC_WRITES

 drivers/md/dm-crypt.c | 21 +++++++++++++++++++--
 drivers/md/dm.c       |  5 +++--
 2 files changed, 22 insertions(+), 4 deletions(-)
Signature · 1 line
2.43.5

Hi

Thanks for the patches, I reworked them a bit.

Regarding the first patch - note that the device mapper stack may be arbitrarily reconfigured, so it is possible that REQ_ATOMIC bio may be sent to a target that doesn't support atomic writes - and this could trigger the "BUG_ON(bio->bi_opf & REQ_ATOMIC);".

So, we need to reject atomic bios to targets that don't support them.

Regarding the second patch - max_write_size and DM_CRYPT_DEFAULT_MAX_WRITE_SIZE is a performance hint that may be set arbitrarily by the user (it was added after we discovered that too long bios would not paralellize compression/decompression and it caused performance regression) - so it shouldn't restrict the size of atomic bios. The size of atomic bios should be restricted to "BIO_MAX_VECS << PAGE_SHIFT" - that is the hard limit.

dm-crypt already has logic to not split writes if ti->emulate_zone_append is true - so I joined this logic with the REQ_ATOMIC logic.

I'll send updated patches, please test them.

Mikulas

author
#4 lore raw
Quoted 15 lines · Mikulas Patocka
Quoted 11 lines · John Garry

This small series enables atomic writes support for dm-crypt.

Apart from setting DM_TARGET_ATOMIC_WRITES flag, we need to make the following changes:
- limit atomic write size to dm-crypt max write size
- BUG_ON() any partial completions for when REQ_ATOMIC is set

John Garry (2):
dm: BUG on REQ_ATOMIC in dm_accept_partial_bio()
dm-crypt: enable DM_TARGET_ATOMIC_WRITES

Quoted 7 lines
Quoted 6 lines
  drivers/md/dm-crypt.c | 21 +++++++++++++++++++--
  drivers/md/dm.c       |  5 +++--
  2 files changed, 22 insertions(+), 4 deletions(-)
Signature · 1 line
2.43.5

Hi Mikulas,

Quoted 7 lines

Thanks for the patches, I reworked them a bit.

Regarding the first patch - note that the device mapper stack may be arbitrarily reconfigured, so it is possible that REQ_ATOMIC bio may be sent to a target that doesn't support atomic writes - and this could trigger the "BUG_ON(bio->bi_opf & REQ_ATOMIC);".

When reconfigured, the dm device request queue limits are re-evaluated, right?

Quoted 9 lines

So, we need to reject atomic bios to targets that don't support them.

Regarding the second patch - max_write_size and DM_CRYPT_DEFAULT_MAX_WRITE_SIZE is a performance hint that may be set arbitrarily by the user (it was added after we discovered that too long bios would not paralellize compression/decompression and it caused performance regression) - so it shouldn't restrict the size of atomic bios.

oh, if it's a performance hint, then we can avoid splitting for atomic writes.

Quoted 8 lines

The size of atomic bios should be restricted to "BIO_MAX_VECS << PAGE_SHIFT" - that is the hard limit.

dm-crypt already has logic to not split writes if ti->emulate_zone_append is true - so I joined this logic with the REQ_ATOMIC logic.

I'll send updated patches, please test them.

Thanks,
John

maintainer
#5 lore raw
Quoted 27 lines · John Garry
Quoted 15 lines · Mikulas Patocka
Quoted 11 lines · John Garry

This small series enables atomic writes support for dm-crypt.

Apart from setting DM_TARGET_ATOMIC_WRITES flag, we need to make the following changes:
- limit atomic write size to dm-crypt max write size
- BUG_ON() any partial completions for when REQ_ATOMIC is set

John Garry (2):
dm: BUG on REQ_ATOMIC in dm_accept_partial_bio()
dm-crypt: enable DM_TARGET_ATOMIC_WRITES

Quoted 7 lines
Quoted 6 lines
  drivers/md/dm-crypt.c | 21 +++++++++++++++++++--
  drivers/md/dm.c       |  5 +++--
  2 files changed, 22 insertions(+), 4 deletions(-)
Signature · 1 line
2.43.5

Hi Mikulas,

Quoted 1 line
Quoted 8 lines
Quoted 6 lines

Thanks for the patches, I reworked them a bit.

Regarding the first patch - note that the device mapper stack may be arbitrarily reconfigured, so it is possible that REQ_ATOMIC bio may be sent to a target that doesn't support atomic writes - and this could trigger the "BUG_ON(bio->bi_opf & REQ_ATOMIC);".

When reconfigured, the dm device request queue limits are re-evaluated, right?

It may or may not be re-evaluated. If you stack LVM volume group on the top of another LVM volume group, and if you change something in the lower volume group, the upper group has no way of finding out that the underlying physical volumes (which are logical volumes on the lower group) were reconfigured.

Similarly, if you stack dm-crypt on the top of a logical volume and change the limits of the logical volume, dm-crypt has no way to find you that the underlying logical volume was reconfigured.

There is also time-to-check - vs - time-to-use race condition. Someone may see that a device has non-zero atomic_write_hw_max, construct an atomic write bio, the device could be reconfigured after the bio was contructed and before the bio was dispatched - and the atomic bio would be sent to a dm target that is not expecting it.

Mikulas

author
#6 lore raw
Quoted 10 lines · Mikulas Patocka
Quoted 5 lines
Quoted 4 lines

Regarding the first patch - note that the device mapper stack may be arbitrarily reconfigured, so it is possible that REQ_ATOMIC bio may be sent to a target that doesn't support atomic writes - and this could trigger the "BUG_ON(bio->bi_opf & REQ_ATOMIC);".

When reconfigured, the dm device request queue limits are re-evaluated, right?

It may or may not be re-evaluated. If you stack LVM volume group on the top of another LVM volume group, and if you change something in the lower volume group, the upper group has no way of finding out that the underlying physical volumes (which are logical volumes on the lower group) were reconfigured.

sure

Quoted 4 lines

Similarly, if you stack dm-crypt on the top of a logical volume and change the limits of the logical volume, dm-crypt has no way to find you that the underlying logical volume was reconfigured.

Sure, and I don't know what really can be done about that.

But I am interested in if extent a vg by adding another device - would the limits of a lv be updated? I think that it would.

Quoted 6 lines

There is also time-to-check - vs - time-to-use race condition. Someone may see that a device has non-zero atomic_write_hw_max, construct an atomic write bio, the device could be reconfigured after the bio was contructed and before the bio was dispatched - and the atomic bio would be sent to a dm target that is not expecting it.

Sure, but the good news is that we have checks in the block stack for this, i.e. if dm submits a bio with REQ_ATOMIC to a device which doesn't support atomic writes, then the block stack will reject it. The physical device will also reject it.

Thanks,
John

maintainer
#7 lore raw
Quoted 8 lines · John Garry
Quoted 3 lines

Similarly, if you stack dm-crypt on the top of a logical volume and change the limits of the logical volume, dm-crypt has no way to find you that the underlying logical volume was reconfigured.

Sure, and I don't know what really can be done about that.

But I am interested in if extent a vg by adding another device - would the limits of a lv be updated? I think that it would.

Yes, they would.

Quoted 10 lines
Quoted 5 lines

There is also time-to-check - vs - time-to-use race condition. Someone may see that a device has non-zero atomic_write_hw_max, construct an atomic write bio, the device could be reconfigured after the bio was contructed and before the bio was dispatched - and the atomic bio would be sent to a dm target that is not expecting it.

Sure, but the good news is that we have checks in the block stack for this, i.e. if dm submits a bio with REQ_ATOMIC to a device which doesn't support atomic writes, then the block stack will reject it. The physical device will also reject it.

Yes - but the block layer checks the bio without preventing device mapper table swap.

So, the block layer may find out that a device supports atomic writes, pass the bio through to dm, dm table may be reloaded and the bio would be sent to a target that doesn't expect it.

You must do the check inside the RCU read region to make sure that the dm table can't be swapped underneath.

Mikulas

Quoted 3 lines

Thanks,
John

author
#8 lore raw
Quoted 10 lines · Mikulas Patocka
Quoted 4 lines

Sure, but the good news is that we have checks in the block stack for this, i.e. if dm submits a bio with REQ_ATOMIC to a device which doesn't support atomic writes, then the block stack will reject it. The physical device will also reject it.

Yes - but the block layer checks the bio without preventing device mapper table swap.

So, the block layer may find out that a device supports atomic writes, pass the bio through to dm, dm table may be reloaded and the bio would be sent to a target that doesn't expect it.

ok, I think that I understand... So if this occurs, I think that when the cloned bio is submitted to the target bdev, the block bio submission path may find that the target bdev does not support atomic writes and reject the bio, see REQ_ATOMIC check in submit_bio_noacct().

Quoted 3 lines

You must do the check inside the RCU read region to make sure that the dm table can't be swapped underneath.

Right

Thanks,
John