dm-crypt: enable DM_TARGET_ATOMIC_WRITES
Mikulas Patocka
maintainerPATCH 2/2 dm-crypt: enable DM_TARGET_ATOMIC_WRITES
Allow handling of bios with REQ_ATOMIC flag set.
Don't split these bios and fail them if they overrun the hard limit "BIO_MAX_VECS << PAGE_SHIFT".
This commit joins the logic that avoids splitting emulated zone append bios with atomic write bios.
--- drivers/md/dm-crypt.c | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-)
drivers/md/dm-crypt.c +24 -15
@@ -254,22 +254,15 @@ static unsigned int max_write_size = 0;module_param (max_write_size, uint,0644 );MODULE_PARM_DESC (max_write_size,"Maximum size of a write request" ); -static unsigned get_max_request_sectors (struct dm_target *ti,struct bio *bio) +static unsigned get_max_request_sectors (struct dm_target *ti,struct bio *bio,bool no_split) {struct crypt_config *cc = ti->private;unsigned val, sector_align;bool wrt =op_is_write (bio_op (bio)); -if (wrt) { -/* -* For zoned devices, splitting write operations creates the -* risk of deadlocking queue freeze operations with zone write -* plugging BIO work when the reminder of a split BIO is -* issued. So always allow the entire BIO to proceed. -*/ -if (ti->emulate_zone_append) -return bio_sectors (bio); - +if (no_split) { + val =-1 ; + }else if (wrt) { val =min_not_zero (READ_ONCE (max_write_size),DM_CRYPT_DEFAULT_MAX_WRITE_SIZE ); }else {
@@ -3496,6 +3489,7 @@ static int crypt_map(struct dm_target *tstruct dm_crypt_io *io;struct crypt_config *cc = ti->private;unsigned max_sectors; +bool no_split;/* * If bio is REQ_PREFLUSH or REQ_OP_DISCARD, just bypass crypt queues.
@@ -3513,10 +3507,20 @@ static int crypt_map(struct dm_target *t/* * Check if bio is too large, split as needed. -*/ - max_sectors =get_max_request_sectors (ti, bio); -if (unlikely (bio_sectors (bio) > max_sectors)) +* +* For zoned devices, splitting write operations creates the +* risk of deadlocking queue freeze operations with zone write +* plugging BIO work when the reminder of a split BIO is +* issued. So always allow the entire BIO to proceed. +*/ + no_split = (ti->emulate_zone_append &&op_is_write (bio_op (bio))) || + (bio->bi_opf &REQ_ATOMIC ); + max_sectors =get_max_request_sectors (ti, bio, no_split); +if (unlikely (bio_sectors (bio) > max_sectors)) { +if (unlikely (no_split)) +return DM_MAPIO_KILL ;dm_accept_partial_bio (bio, max_sectors); + }/* * Ensure that bio is a multiple of internal sector encryption size
@@ -3762,6 +3766,11 @@ static void crypt_io_hints(struct dm_tarif (ti->emulate_zone_append) limits->max_hw_sectors =min (limits->max_hw_sectors,BIO_MAX_VECS <<PAGE_SECTORS_SHIFT ); + + limits->atomic_write_hw_unit_max =min (limits->atomic_write_hw_unit_max, +BIO_MAX_VECS <<PAGE_SHIFT ); + limits->atomic_write_hw_max =min (limits->atomic_write_hw_max, +BIO_MAX_VECS <<PAGE_SHIFT ); }static struct target_type crypt_target = {
@@ -3770,7 +3779,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,
Benjamin Marzinski
maintainerQuoted 79 lines · Mikulas Patocka
Allow handling of bios with
REQ_ATOMICflag set.Don't split these bios and fail them if they overrun the hard limit "
BIO_MAX_VECS<<PAGE_SHIFT".This commit joins the logic that avoids splitting emulated zone append bios with atomic write bios.
Signed-off-by John Garry <[email protected]>Signed-off-by Mikulas Patocka <[email protected]>--- drivers/md/dm-crypt.c | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-)Index:
linux-2.6/drivers/md/dm-crypt.c
===================================================================drivers/md/dm-crypt.c +18
-14@@ -254,22 +254,15 @@ static unsigned int max_write_size = 0;module_param (max_write_size, uint,0644 );MODULE_PARM_DESC (max_write_size,"Maximum size of a write request" ); -static unsigned get_max_request_sectors (struct dm_target *ti,struct bio *bio) +static unsigned get_max_request_sectors (struct dm_target *ti,struct bio *bio,bool no_split) {struct crypt_config *cc = ti->private;unsigned val, sector_align;bool wrt =op_is_write (bio_op (bio)); -if (wrt) { -/* -* For zoned devices, splitting write operations creates the -* risk of deadlocking queue freeze operations with zone write -* plugging BIO work when the reminder of a split BIO is -* issued. So always allow the entire BIO to proceed. -*/ -if (ti->emulate_zone_append) -return bio_sectors (bio); - +if (no_split) { + val =-1 ; + }else if (wrt) { val =min_not_zero (READ_ONCE (max_write_size),DM_CRYPT_DEFAULT_MAX_WRITE_SIZE ); }else {@@ -3496,6 +3489,7 @@ static int crypt_map(struct dm_target *tstruct dm_crypt_io *io;struct crypt_config *cc = ti->private;unsigned max_sectors; +bool no_split;/* * If bio is REQ_PREFLUSH or REQ_OP_DISCARD, just bypass crypt queues. @@ -3513,10 +3507,20 @@ static int crypt_map(struct dm_target *t/* * Check if bio is too large, split as needed. -*/ - max_sectors =get_max_request_sectors (ti, bio); -if (unlikely (bio_sectors (bio) > max_sectors)) +* +* For zoned devices, splitting write operations creates the +* risk of deadlocking queue freeze operations with zone write +* plugging BIO work when the reminder of a split BIO is +* issued. So always allow the entire BIO to proceed. +*/ + no_split = (ti->emulate_zone_append &&op_is_write (bio_op (bio))) || + (bio->bi_opf &REQ_ATOMIC ); + max_sectors =get_max_request_sectors (ti, bio, no_split); +if (unlikely (bio_sectors (bio) > max_sectors)) { +if (unlikely (no_split)) +return DM_MAPIO_KILL ;dm_accept_partial_bio (bio, max_sectors); + }/* * Ensure that bio is a multiple of internal sector encryption size
drivers/md/dm-crypt.c static void crypt_io_hints(struct dm_tar · line 3774
Quoted 9 lines
drivers/md/dm-crypt.c +5
-0@@ -3762,6 +3766,11 @@ static void crypt_io_hints(struct dm_tarif (ti->emulate_zone_append) limits->max_hw_sectors =min (limits->max_hw_sectors,BIO_MAX_VECS <<PAGE_SECTORS_SHIFT ); + + limits->atomic_write_hw_unit_max =min (limits->atomic_write_hw_unit_max, +BIO_MAX_VECS <<PAGE_SHIFT ); + limits->atomic_write_hw_max =min (limits->atomic_write_hw_max, +BIO_MAX_VECS <<PAGE_SHIFT ); }
Do we need to cap these limits, instead of just accepting the underlying device limits? Neither of them are really used for IO. atomic_write_unit_max, which is used for IO, will already get a capped value from atomic_write_hw_unit_max in blk_atomic_writes_update_limits(). And capping atomic_write_hw_max seems wrong, since atomic_write_hw_max == UINT_MAX is used blk_validate_atomic_write_limits() to indicate that atomic writes were never set up, because no underlying device supported them. I don't think these caps will actually break things, but in my mind they make some already confusing limits even more confusing.
Or am I missing some reason why this is needed?
-Ben
Quoted 12 lines
drivers/md/dm-crypt.c +1
-1static struct target_type crypt_target = {@@ -3770,7 +3779,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,
Quoted 16 lines · Benjamin Marzinski
Quoted 5 lines
}
/*
* Ensure that bio is a multiple of internal sector encryption sizedrivers/md/dm-crypt.c static void crypt_io_hints(struct dm_tar · line 3774
Quoted 9 lines
drivers/md/dm-crypt.c +5
-0@@ -3762,6 +3766,11 @@ static void crypt_io_hints(struct dm_tarif (ti->emulate_zone_append) limits->max_hw_sectors =min (limits->max_hw_sectors,BIO_MAX_VECS <<PAGE_SECTORS_SHIFT ); + + limits->atomic_write_hw_unit_max =min (limits->atomic_write_hw_unit_max, +BIO_MAX_VECS <<PAGE_SHIFT ); + limits->atomic_write_hw_max =min (limits->atomic_write_hw_max, +BIO_MAX_VECS <<PAGE_SHIFT ); }Do we need to cap these limits, instead of just accepting the underlying device limits?
I was going to mention that I don't think that this is required.
Quoted 4 lines
Neither of them are really used for IO.
atomic_write_unit_max, which is used for IO, will already get a capped value fromatomic_write_hw_unit_maxinblk_atomic_writes_update_limits().
Yes, we limit the software atomic writes limits by what the block stack can actually handle for never requiring to split a bio with REQ_ATOMIC flag set. The HW limits are just what the HW can support.
Quoted 4 lines
And capping
atomic_write_hw_maxseems
wrong, sinceatomic_write_hw_max==UINT_MAXis usedblk_validate_atomic_write_limits()to indicate that atomic writes were never set up, because no underlying device supported them.
That UINT_MAX is used as a flag to indicate that we have not started to stack limits for bottom devices yet, and so we just report 0 for the atomic limits (when set). I actually don't think that this check is strictly required in blk_validate_atomic_write_limits(), as we don't try to validate those the limits before stacking the bottom devices. I did hit it previously - I need to check on that.
Quoted 5 lines
I don't think
these caps will actually break things, but in my mind they make some already confusing limits even more confusing.Or am I missing some reason why this is needed?
Thanks,
John
Mikulas Patocka
maintainerQuoted 5 lines · Benjamin Marzinski
Quoted 5 lines
drivers/md/dm-crypt.c +1
-0@@ -3762,6 +3766,11 @@ static void crypt_io_hints(struct dm_tarif (ti->emulate_zone_append) limits->max_hw_sectors =min (limits->max_hw_sectors,BIO_MAX_VECS <<PAGE_SECTORS_SHIFT ); +
Quoted 8 lines
drivers/md/dm-crypt.c static void crypt_io_hints(struct dm_tar · line 3774
Quoted 5 lines
drivers/md/dm-crypt.c +4
-0+ limits->atomic_write_hw_unit_max =min (limits->atomic_write_hw_unit_max, +BIO_MAX_VECS <<PAGE_SHIFT ); + limits->atomic_write_hw_max =min (limits->atomic_write_hw_max, +BIO_MAX_VECS <<PAGE_SHIFT ); }Do we need to cap these limits, instead of just accepting the underlying device limits?
BIO_MAX_VECS << PAGE_SHIFT is the hard limit for the size of any write (atomic or non-atomic) in dm-crypt. dm-crypt cannot support larger writes by design.
If non-atomic write is larger than BIO_MAX_VECS << PAGE_SHIFT, dm-crypt splits it using dm_accept_partial_bio. But atomic writes cannot be split, so we need to inform the upper layer to not sent larger bios.
Quoted 10 lines
Neither of them are really used for IO.
atomic_write_unit_max, which is used for IO, will already get a capped value fromatomic_write_hw_unit_maxinblk_atomic_writes_update_limits(). And cappingatomic_write_hw_maxseems wrong, sinceatomic_write_hw_max==UINT_MAXis usedblk_validate_atomic_write_limits()to indicate that atomic writes were never set up, because no underlying device supported them. I don't think these caps will actually break things, but in my mind they make some already confusing limits even more confusing.
Quoted 3 lines
Or am I missing some reason why this is needed?
-Ben
How do you think we should modify the limits, to make sure that atomic write size is capped at BIO_MAX_VECS << PAGE_SHIFT?
Mikulas
Quoted 10 lines · Mikulas Patocka
Quoted 10 lines
Neither of them are really used for IO.
atomic_write_unit_max, which is used for IO, will already get a capped value fromatomic_write_hw_unit_maxinblk_atomic_writes_update_limits(). And cappingatomic_write_hw_maxseems wrong, sinceatomic_write_hw_max==UINT_MAXis usedblk_validate_atomic_write_limits()to indicate that atomic writes were never set up, because no underlying device supported them. I don't think these caps will actually break things, but in my mind they make some already confusing limits even more confusing.
Quoted 5 lines
Quoted 3 lines
Or am I missing some reason why this is needed?
-Ben
How do you think we should modify the limits, to make sure that atomic write size is capped at
BIO_MAX_VECS<<PAGE_SHIFT?
The atomic write size is limited elsewhere, specifically blk_atomic_writes_update_limits() - the limit there is quite similar (to what is added in this updated patch). So I don't think that we need to do anything special in dm-crypt, as that block limit is already tighter.
I suppose that this code can be kept with a comment (saying that it is unnecessary, but safer to keep). Or we can just drop the code and add a comment (saying that we don't need to limit here, as it is done elsewhere).
Thanks,
John
Mikulas Patocka
maintainerQuoted 22 lines · John Garry
Quoted 10 lines · Mikulas Patocka
Quoted 10 lines
Neither of them are really used for IO.
atomic_write_unit_max, which is used for IO, will already get a capped value fromatomic_write_hw_unit_maxinblk_atomic_writes_update_limits(). And cappingatomic_write_hw_maxseems wrong, sinceatomic_write_hw_max==UINT_MAXis usedblk_validate_atomic_write_limits()to indicate that atomic writes were never set up, because no underlying device supported them. I don't think these caps will actually break things, but in my mind they make some already confusing limits even more confusing.Quoted 5 lines
Quoted 3 lines
Or am I missing some reason why this is needed?
-Ben
How do you think we should modify the limits, to make sure that atomic write size is capped at
BIO_MAX_VECS<<PAGE_SHIFT?The atomic write size is limited elsewhere, specifically
blk_atomic_writes_update_limits()- the limit there is quite similar (to what is added in this updated patch). So I don't think that we need to do anything special in dm-crypt, as that block limit is already tighter.
Quoted 6 lines
I suppose that this code can be kept with a comment (saying that it is unnecessary, but safer to keep). Or we can just drop the code and add a comment (saying that we don't need to limit here, as it is done elsewhere).
Thanks,
John
What if someone creates a hypothetical device that supports atomic writes larger than BIO_MAX_VECS << PAGE_SHIFT? I think, we should limit it.
Mikulas
Quoted 5 lines · Mikulas Patocka
Quoted 5 lines
The atomic write size is limited elsewhere, specifically
blk_atomic_writes_update_limits()- the limit there is quite similar (to what is added in this updated patch). So I don't think that we need to do anything special in dm-crypt, as that block limit is already tighter.
Quoted 8 lines
Quoted 6 lines
I suppose that this code can be kept with a comment (saying that it is unnecessary, but safer to keep). Or we can just drop the code and add a comment (saying that we don't need to limit here, as it is done elsewhere).
Thanks,
JohnWhat if someone creates a hypothetical device that supports atomic writes larger than
BIO_MAX_VECS<<PAGE_SHIFT? I think, we should limit it.
Yeah, it's entirely possible. And this is why we have separate HW and SW atomic write limits. The HW limits are just what the HW can support, and has no theoretical limit (apart from what we can fit in the request_queue limits values). But the SW limits are a combination of the HW limits and what the block stack can support (when guaranteeing to never split a bio). The SW upper limit is about ~500KB for 4K PAGE_SIZE - it can be less if the bdev has a limited segment count max. The SW limits are what we report to userspace in terms of the maximum atomic write size; in addition, it is the SW limits which the block stack checks for when submitting a bio.
So you could cap the HW limits to BIO_MAX_VECS << PAGE_SHIFT, but it would not have any effect in practice.
Thanks,
John
Quoted 11 lines · Mikulas Patocka
Allow handling of bios with
REQ_ATOMICflag set.Don't split these bios and fail them if they overrun the hard limit "
BIO_MAX_VECS<<PAGE_SHIFT".This commit joins the logic that avoids splitting emulated zone append bios with atomic write bios.
Signed-off-by John Garry <[email protected]>Signed-off-by Mikulas Patocka <[email protected]>
fwiw, I still don't think that we require the change in crypt_io_hints() [not shown], but it should not make a difference.
Thanks!
On 10/11/2025 16:02, John Garry wrote:
Hi Mikulas,
Just a friendly reminder on this series. Let me know if you require anything else.
Cheers
Quoted 19 lines · Mikulas Patocka
Quoted 11 lines
Allow handling of bios with
REQ_ATOMICflag set.Don't split these bios and fail them if they overrun the hard limit "
BIO_MAX_VECS<<PAGE_SHIFT".This commit joins the logic that avoids splitting emulated zone append bios with atomic write bios.
Signed-off-by John Garry <[email protected]>Signed-off-by Mikulas Patocka <[email protected]>Tested-by John Garry <[email protected]>fwiw, I still don't think that we require the change in
crypt_io_hints()[not shown], but it should not make a difference.Thanks!
Mikulas Patocka
maintainerQuoted 8 lines · John Garry
On 10/11/2025 16:02, John Garry wrote:
Hi Mikulas,
Just a friendly reminder on this series. Let me know if you require anything else.
Cheers
Hi
In crypt_io_hints - should I set "limits->atomic_write_hw_max", "limits->atomic_write_hw_unit_max" or both?
I think that we haven't settled about that when we talked about it.
Mikulas
Quoted 21 lines
Quoted 20 lines · Mikulas Patocka
Quoted 11 lines
Allow handling of bios with
REQ_ATOMICflag set.Don't split these bios and fail them if they overrun the hard limit "
BIO_MAX_VECS<<PAGE_SHIFT".This commit joins the logic that avoids splitting emulated zone append bios with atomic write bios.
Signed-off-by John Garry <[email protected]>Signed-off-by Mikulas Patocka <[email protected]>Tested-by John Garry <[email protected]>fwiw, I still don't think that we require the change in
crypt_io_hints()[not shown], but it should not make a difference.Thanks!
Quoted 4 lines · Mikulas Patocka
In
crypt_io_hints- should I set "limits->atomic_write_hw_max", "limits->atomic_write_hw_unit_max" or both?I think that we haven't settled about that when we talked about it.
If you are going to set one then set both of them.
I already tested this, so it's fine.
Thanks,
John
Mikulas Patocka
maintainerQuoted 4 lines · John Garry
Quoted 3 lines · Mikulas Patocka
In
crypt_io_hints- should I set "limits->atomic_write_hw_max", "limits->atomic_write_hw_unit_max" or both?
Quoted 8 lines
Quoted 1 line
I think that we haven't settled about that when we talked about it.
If you are going to set one then set both of them.
I already tested this, so it's fine.
Thanks,
John
Hi
I committed those two patches - you can have look at linux-dm.git, branch for-next.
Mikulas