blk-integrity: support arbitrary buffer alignment
PATCH v2 1/1 blk-integrity: support arbitrary buffer alignment
From: Keith Busch <[email protected]>
A bio segment might have partial block data with the rest continuing into the next segments.
At the same time, the protection information may also be split in multiple segments. The most likely way that may happen is if two requests merge, or if we're directly using the io_uring user metadata. Further, it may be possible to have unalign the protection fields from the user space buffer, or if there are odd additional opaque bytes in front or in back of the protection information metadata region.
Change up the iteration to allow spanning multiple segments. This patch is mostly a re-write of the protection information handling to allow any arbitrary alignments, so it's probably easier to review the end result rather than the diff.
Note, this strives to be a very general solution that should work in scenarios that I think unlikely to ever encounter in real life.
This was tested using recently proposed io_uring metadata test case here:
https://lore.kernel.org/io-uring/[email protected]/
The test purposefully contructs metadata with offsets that have the data integrity field straddle pages. As longs as they're not physically contiguous, that will split the field across multiple segments and test those conditions, which will either get a copy buffer if the device doesn't support multiple integrity segments, or get a temporary data integrity field copy during the reftag remapping.
---v1->v2:
Fixed up the new "union" type and added a comment to explain what its for.
Fixed up sparse warnings
I think I fixed the ip checksum type based on the existing implementation, but don't know as I never encountered a device that supports that.
Fixed up remapping for partial completions.
Various common code cleanups.
block/blk-settings.c | 10 - block/t10-pi.c | 853 +++++++++++++++++++++++++------------------ 2 files changed, 499 insertions(+), 364 deletions(-)
block/blk-settings.c +0 -10
@@ -197,16 +197,6 @@ static int blk_validate_integrity_limits(struct queue_limits *lim)if (!bi->interval_exp) bi->interval_exp =ilog2 (lim->logical_block_size); -/* -* The PI generation / validation helpers do not expect intervals to -* straddle multiple bio_vecs. Enforce alignment so that those are -* never generated, and that each buffer is aligned as expected. -*/ -if (bi->csum_type) { - lim->dma_alignment =max (lim->dma_alignment, - (1U << bi->interval_exp) -1 ); - } -/* * The block layer automatically adds integrity data for bios that don't * already have it. Limit the I/O size so that a single maximum size
block/t10-pi.c +499 -354
956 lines not on this page · raw message
Signature · 1 line
2.47.3
Quoted 11 lines · Keith Busch
This was tested using recently proposed
io_uringmetadata test case here:https://lore.kernel.org/io-uring/[email protected]/
The test purposefully contructs metadata with offsets that have the data integrity field straddle pages. As longs as they're not physically contiguous, that will split the field across multiple segments and test those conditions, which will either get a copy buffer if the device doesn't support multiple integrity segments, or get a temporary data integrity field copy during the reftag remapping.
Any chance we could get this test or something like it into blktests?
That way it would get regularly run as part of block layer validation.
The changes looks sensible to be, and pass very basic sanity testing using my PI setup. I have few cleanups we should get into (attached).
1: pass the union type down instead of casting to the t10/crc tuples to improve type safety
2: fix W=1 warnings due to not quite kerneldoc comments 3: cleanup the copy wrappers that are only used once each 4: just always use the unaligned handlers. I guess this might be a bit contentious, but at least for x86 it actually generates better code. Alternatively we could require dword (4 byte) alignment for PI, and only the guard tag in the crc64 format would require any unaligned handling at all.
From 0160966a9f74c9d60f3a8092a56ad5b429ae8bcb Mon Sep 17 00:00:00 2001
From: Christoph Hellwig <[email protected]>
Date: Fri, 7 Nov 2025 07:27:18 -0500
Subject: pass union pi_tuple down
Provide some extra type safety.
--- block/t10-pi.c | 70 ++++++++++++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 36 deletions(-)
block/t10-pi.c +34 -36
@@ -102,7 +102,7 @@ static void __blk_integrity_copy_from_tuple(struct bio_integrity_payload *bip, }static void blk_integrity_copy_from_tuple (struct blk_integrity_iter *iter, -void *tuple) +union pi_tuple *tuple) {__blk_integrity_copy_from_tuple (iter->bip, &iter->prot_iter, tuple, iter->bi->pi_tuple_size);
@@ -132,17 +132,16 @@ static void __blk_integrity_copy_to_tuple(struct bio_integrity_payload *bip, }static void blk_integrity_copy_to_tuple (struct blk_integrity_iter *iter, -void *tuple) +union pi_tuple *tuple) {__blk_integrity_copy_to_tuple (iter->bip, &iter->prot_iter, tuple, iter->bi->pi_tuple_size); } -static void blk_set_ext_pi (void *prot_buf,struct blk_integrity_iter *iter) +static void blk_set_ext_pi (struct crc64_pi_tuple *pi, +struct blk_integrity_iter *iter) { -struct crc64_pi_tuple *pi = prot_buf; - -if (unlikely ((unsigned long )prot_buf & (sizeof (*pi) -1 ))) { +if (unlikely ((unsigned long )pi & (sizeof (*pi) -1 ))) {put_unaligned_be64 (iter->crc, &pi->guard_tag);put_unaligned_be16 (0 , &pi->app_tag);put_unaligned_be48 (iter->seed, &pi->ref_tag);
@@ -153,11 +152,10 @@ static void blk_set_ext_pi(void *prot_buf, struct blk_integrity_iter *iter) } } -static void blk_set_t10_pi (void *prot_buf,struct blk_integrity_iter *iter) +static void blk_set_t10_pi (struct t10_pi_tuple *pi, +struct blk_integrity_iter *iter) { -struct t10_pi_tuple *pi = prot_buf; - -if (unlikely ((unsigned long )prot_buf & (sizeof (*pi) -1 ))) { +if (unlikely ((unsigned long )pi & (sizeof (*pi) -1 ))) {put_unaligned_be16 (iter->crc, &pi->guard_tag);put_unaligned_be16 (0 , &pi->app_tag);put_unaligned_be32 (iter->seed, &pi->ref_tag);
@@ -168,12 +166,12 @@ static void blk_set_t10_pi(void *prot_buf, struct blk_integrity_iter *iter) } } -static void blk_set_ip_pi (void *prot_buf,struct blk_integrity_iter *iter) +static void blk_set_ip_pi (struct t10_pi_tuple *pi, +struct blk_integrity_iter *iter) {__be16 csum = (__force __be16 )~(lower_16_bits (iter->crc)); -struct t10_pi_tuple *pi = prot_buf; -if (unlikely ((unsigned long )prot_buf & (sizeof (*pi) -1 ))) { +if (unlikely ((unsigned long )pi & (sizeof (*pi) -1 ))) {__put_unaligned_t (__be16, csum, &pi->guard_tag);put_unaligned_be16 (0 , &pi->app_tag);put_unaligned_be32 (iter->seed, &pi->ref_tag);
@@ -271,29 +269,29 @@ static blk_status_t t10_pi_verify(struct blk_integrity_iter *iter, }static blk_status_t blk_integrity_verify (struct blk_integrity_iter *iter, -void *tuple) +union pi_tuple *tuple) {switch (iter->bi->csum_type) {case BLK_INTEGRITY_CSUM_CRC64 : -return ext_pi_crc64_verify (iter, tuple); +return ext_pi_crc64_verify (iter, &tuple->crc64_pi);case BLK_INTEGRITY_CSUM_CRC :case BLK_INTEGRITY_CSUM_IP : -return t10_pi_verify (iter, tuple); +return t10_pi_verify (iter, &tuple->t10_pi);default :return BLK_STS_OK ; } }static void blk_integrity_set (struct blk_integrity_iter *iter, -void *tuple) +union pi_tuple *tuple) {switch (iter->bi->csum_type) {case BLK_INTEGRITY_CSUM_CRC64 : -return blk_set_ext_pi (tuple, iter); +return blk_set_ext_pi (&tuple->crc64_pi, iter);case BLK_INTEGRITY_CSUM_CRC : -return blk_set_t10_pi (tuple, iter); +return blk_set_t10_pi (&tuple->t10_pi, iter);case BLK_INTEGRITY_CSUM_IP : -return blk_set_ip_pi (tuple, iter); +return blk_set_ip_pi (&tuple->t10_pi, iter);default :WARN_ON_ONCE (1 );return ;
@@ -467,18 +465,18 @@ static void blk_tuple_remap_end(union pi_tuple *tuple, void *ptuple,bvec_iter_advance (bip->bip_vec, iter, len); } -static void blk_set_ext_unmap_ref (void *prot_buf,u64 virt,u64 ref_tag) +static void blk_set_ext_unmap_ref (struct crc64_pi_tuple *pi,u64 virt, +u64 ref_tag) { -struct crc64_pi_tuple *pi = prot_buf;u64 ref =get_unaligned_be48 (&pi->ref_tag);if (ref ==lower_48_bits (virt) && ref != virt)put_unaligned_be48 (virt, pi->ref_tag); } -static void blk_set_t10_unmap_ref (void *prot_buf,u32 virt,u32 ref_tag) +static void blk_set_t10_unmap_ref (struct t10_pi_tuple *pi,u32 virt, +u32 ref_tag) { -struct t10_pi_tuple *pi = prot_buf;u32 ref;if (unlikely ((unsigned long )pi & (sizeof (*pi) -1 )))
@@ -495,16 +493,16 @@ static void blk_set_t10_unmap_ref(void *prot_buf, u32 virt, u32 ref_tag) pi->ref_tag =cpu_to_be32 (virt); } -static void blk_reftag_remap_complete (struct blk_integrity *bi,void *tuple, -u64 virt,u64 ref) +static void blk_reftag_remap_complete (struct blk_integrity *bi, +union pi_tuple *tuple,u64 virt,u64 ref) {switch (bi->csum_type) {case BLK_INTEGRITY_CSUM_CRC64 : -blk_set_ext_unmap_ref (tuple, virt, ref); +blk_set_ext_unmap_ref (&tuple->crc64_pi, virt, ref);break ;case BLK_INTEGRITY_CSUM_CRC :case BLK_INTEGRITY_CSUM_IP : -blk_set_t10_unmap_ref (tuple, virt, ref); +blk_set_t10_unmap_ref (&tuple->t10_pi, virt, ref);break ;default :WARN_ON_ONCE (1 );
@@ -512,18 +510,17 @@ static void blk_reftag_remap_complete(struct blk_integrity *bi, void *tuple, } } -static void blk_set_ext_map_ref (void *prot_buf,u64 virt,u64 ref_tag) +static void blk_set_ext_map_ref (struct crc64_pi_tuple *pi,u64 virt, +u64 ref_tag) { -struct crc64_pi_tuple *pi = prot_buf;u64 ref =get_unaligned_be48 (&pi->ref_tag);if (ref ==lower_48_bits (virt) && ref != ref_tag)put_unaligned_be48 (ref_tag, pi->ref_tag); } -static void blk_set_t10_map_ref (void *prot_buf,u32 virt,u32 ref_tag) +static void blk_set_t10_map_ref (struct t10_pi_tuple *pi,u32 virt,u32 ref_tag) { -struct t10_pi_tuple *pi = prot_buf;u32 ref;if (unlikely ((unsigned long )pi & (sizeof (*pi) -1 )))
@@ -540,16 +537,17 @@ static void blk_set_t10_map_ref(void *prot_buf, u32 virt, u32 ref_tag) pi->ref_tag =cpu_to_be32 (ref_tag); } -static void blk_reftag_remap_prepare (struct blk_integrity *bi,void *tuple, +static void blk_reftag_remap_prepare (struct blk_integrity *bi, +union pi_tuple *tuple,u64 virt,u64 ref) {switch (bi->csum_type) {case BLK_INTEGRITY_CSUM_CRC64 : -blk_set_ext_map_ref (tuple, virt, ref); +blk_set_ext_map_ref (&tuple->crc64_pi, virt, ref);break ;case BLK_INTEGRITY_CSUM_CRC :case BLK_INTEGRITY_CSUM_IP : -blk_set_t10_map_ref (tuple, virt, ref); +blk_set_t10_map_ref (&tuple->t10_pi, virt, ref);break ;default :WARN_ON_ONCE (1 );
@@ -564,7 +562,7 @@ static void __blk_reftag_remap(struct bio *bio, struct blk_integrity *bi,struct bvec_iter iter = bip->bip_iter;u64 virt =bip_get_seed (bip);union pi_tuple tuple; -void *ptuple; +union pi_tuple *ptuple;if (prep && bip->bip_flags &BIP_MAPPED_INTEGRITY ) { *ref += bio->bi_iter.bi_size >> bi->interval_exp;
Signature · 1 line
2.47.3
From 8e15595cf69757fd82623caaf7937cf49349c56d Mon Sep 17 00:00:00 2001
From: Christoph Hellwig <[email protected]>
Date: Fri, 7 Nov 2025 08:06:02 -0500
Subject: un-kerneldoc
Otherwise make W=1 complains. For the copy helpers I dropped the comments entirely as they don't seem to provide a value over just the function names.
--- block/t10-pi.c | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-)
block/t10-pi.c +6 -13
@@ -57,9 +57,9 @@ static void blk_crc(struct blk_integrity_iter *iter, void *data, } } -/** -* blk_integrity_crc_offset - update the crc for formats that have metadata -* padding in front of the data integrity field +/* +* Update the crc for formats that have metadata padding in front of the data +* integrity field */ static void blk_integrity_crc_offset (struct blk_integrity_iter *iter) {
@@ -78,9 +78,6 @@ static void blk_integrity_crc_offset(struct blk_integrity_iter *iter) } } -/** -* __blk_integrity_copy_from_tuple() - copy from @tuple to @iter -*/ static void __blk_integrity_copy_from_tuple (struct bio_integrity_payload *bip,struct bvec_iter *iter,void *tuple,unsigned int tuple_size)
@@ -108,9 +105,6 @@ static void blk_integrity_copy_from_tuple(struct blk_integrity_iter *iter, tuple, iter->bi->pi_tuple_size); } -/** -* __blk_integrity_copy_to_tuple() - copy to &tuple from @iter -*/ static void __blk_integrity_copy_to_tuple (struct bio_integrity_payload *bip,struct bvec_iter *iter,void *tuple,unsigned int tuple_size)
@@ -405,10 +399,9 @@ void blk_integrity_verify_iter(struct bio *bio, struct bvec_iter *saved_iter) } } -/** -* blk_pi_advance_offset - advance @iter past the protection offset -* -* For protection formats that contain front padding on the metadata region. +/* +* Advance @iter past the protection offset for protection formats that +* contain front padding on the metadata region. */ static void blk_pi_advance_offset(struct blk_integrity *bi, struct bio_integrity_payload *bip,
Signature · 1 line
2.47.3
From 6b14968fcbcc6e0a5c9c13b02f467d04a12d8bb6 Mon Sep 17 00:00:00 2001
From: Christoph Hellwig <[email protected]>
Date: Fri, 7 Nov 2025 08:08:39 -0500
Subject: simplify copy/to helpers
No real need for the wrappers that have a single caller. Also make the trivial comments non-kerneldoc as they don't document all paramters anyway.
--- block/t10-pi.c | 44 +++++++++++++++++--------------------------- 1 file changed, 17 insertions(+), 27 deletions(-)
block/t10-pi.c +17 -27
87 lines not on this page · raw message
Signature · 1 line
2.47.3
From c73943c50ce29d8c453e9339a84258ea16340266 Mon Sep 17 00:00:00 2001
From: Christoph Hellwig <[email protected]>
Date: Fri, 7 Nov 2025 07:42:38 -0500
Subject: unaligned
The unaligned handling isn't any more expensive on "sane" architectures. In fact on x86 it compiles down to the same code as the aligned version, just using slightly different register allocation.
--- block/t10-pi.c | 97 +++++++++++--------------------------------------- 1 file changed, 21 insertions(+), 76 deletions(-)
block/t10-pi.c +21 -76
142 lines not on this page · raw message
Signature · 1 line
2.47.3
Quoted 9 lines · Christoph Hellwig
Quoted 4 lines · Keith Busch
This was tested using recently proposed
io_uringmetadata test case here:Any chance we could get this test or something like it into blktests?
That way it would get regularly run as part of block layer validation.
I'll give it a shot. The only problem is that liburing currently doesn't define the SQE fields or pi attributes for this feature, so we'd need to have blktests conditionally redefine things depending on which liburing version the system is using.
Quoted 2 lines
The changes looks sensible to be, and pass very basic sanity testing using my PI setup. I have few cleanups we should get into (attached).
Thanks! These all look good to me. If it's okay with you, I'll fold these in and tag you for Co-developed-by.
Quoted 2 lines · Keith Busch
Thanks! These all look good to me. If it's okay with you, I'll fold these in and tag you for Co-developed-by.
Please fold them yes. I don't think I need a Co-developed-by or any attribution for these trivial cleanups.