Skip to contentSkip to conversation

blk-integrity: support arbitrary buffer alignment

PATCH Superseded Keith Busch 4 participants 11 messages Nov 13, 2025

Subsystems (2)

v4 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 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. The generate/verify, however, only ever accessed the first bip_vec.

Further, it may be possible to 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.

Martin reports many SCSI controllers are not able to handle interval data composed of multiple segments when PI is used, so this patch introduces a new integrity limit that a low level driver can set to notify that it is capable of handling that, default to false. The nvme driver is the first one to enable it in this patch. Everyone else will force DMA alignment to the logical block size to ensure interval data is always aligned within a single segment.

Cc "Martin K. Petersen" <[email protected]>

---
v3->v4:

Fixed spelling mistake for "ESCAPE"

Continue to force memory alignment to the logical block size when guard tags are used unless the low level driver says it's unnecessary; update changelog according.

 block/blk-settings.c     |   6 +-
 block/t10-pi.c           | 791 +++++++++++++++++++++------------------
 drivers/nvme/host/core.c |   1 +
 include/linux/blkdev.h   |   1 +
 4 files changed, 436 insertions(+), 363 deletions(-)
block/blk-settings.c +3 -3
index 78dfef1176231..d1b3f71de2ec4 100644
@@ -198,11 +198,11 @@ static int blk_validate_integrity_limits(struct queue_limits *lim)
 		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
+	 * Some IO controllers can not handle data intervals straddling
+	 * multiple bio_vecs.  For those, enforce alignment so that those are
 	 * never generated, and that each buffer is aligned as expected.
 	 */
-	if (bi->csum_type) {
+	if (!bi->split_interval_capable && bi->csum_type) {
 		lim->dma_alignment = max(lim->dma_alignment,
 					(1U << bi->interval_exp) - 1);
 	}
block/t10-pi.c +431 -360

889 lines not on this page · raw message

drivers/nvme/host/core.c +1 -0
index c0fe50fb7b08c..9136295c419fb 100644
@@ -1874,6 +1874,7 @@ static bool nvme_init_integrity(struct nvme_ns_head *head,
 		break;
 	}
 
+	bi->split_interval_capable = true;
 	bi->metadata_size = head->ms;
 	if (bi->csum_type) {
 		bi->pi_tuple_size = head->pi_size;
include/linux/blkdev.h +1 -0
index f0ab02e0a673c..df031abe507ac 100644
@@ -122,6 +122,7 @@ struct blk_integrity {
 	unsigned char				interval_exp;
 	unsigned char				tag_size;
 	unsigned char				pi_tuple_size;
+	bool					split_interval_capable;
 };
 
 typedef unsigned int __bitwise blk_mode_t;
Signature · 1 line
2.47.3
#1 lore raw

block/t10-pi.c line 140

Quoted 9 lines · Keith Busch
block/t10-pi.c +3 -5
+static void blk_set_ip_pi(struct t10_pi_tuple *pi,
+			  struct blk_integrity_iter *iter)
 {
-	u8 offset = bi->pi_offset;
-	unsigned int i;
-
-	for (i = 0 ; i < iter->data_size ; i += iter->interval) {
-		struct crc64_pi_tuple *pi = iter->prot_buf + offset;
+	__be16 csum = (__force __be16)~(lower_16_bits(iter->crc));

This just throws away half of the checksum instead of properly combining the two halves. How is this being tested?

block/t10-pi.c line 142

Quoted 1 line
block/t10-pi.c +1 -0
+	__put_unaligned_t(__be16, csum, &pi->guard_tag);

This can just use put_unaligned().

- Eric

maintainer
#2 lore raw
Quoted 13 lines · Eric Biggers

block/t10-pi.c line 140

Quoted 9 lines · Keith Busch
block/t10-pi.c +3 -5
+static void blk_set_ip_pi(struct t10_pi_tuple *pi,
+			  struct blk_integrity_iter *iter)
 {
-	u8 offset = bi->pi_offset;
-	unsigned int i;
-
-	for (i = 0 ; i < iter->data_size ; i += iter->interval) {
-		struct crc64_pi_tuple *pi = iter->prot_buf + offset;
+	__be16 csum = (__force __be16)~(lower_16_bits(iter->crc));

This just throws away half of the checksum instead of properly combining the two halves. How is this being tested?

Yeah, this is the only guard type I've never seen a device subscribe to, so not particularly easily tested on my side. I just forced the code path down here anyway and checked if the result matches the result from the existing code calling "ip_compute_csum()". Maybe I can just continue using that as I suspect devices using that can't handle split data intervals that I'm trying to enable.

#3 lore raw
Quoted 15 lines · Keith Busch
Quoted 13 lines · Eric Biggers

block/t10-pi.c line 140

Quoted 9 lines · Keith Busch
block/t10-pi.c +3 -5
+static void blk_set_ip_pi(struct t10_pi_tuple *pi,
+			  struct blk_integrity_iter *iter)
 {
-	u8 offset = bi->pi_offset;
-	unsigned int i;
-
-	for (i = 0 ; i < iter->data_size ; i += iter->interval) {
-		struct crc64_pi_tuple *pi = iter->prot_buf + offset;
+	__be16 csum = (__force __be16)~(lower_16_bits(iter->crc));

This just throws away half of the checksum instead of properly combining the two halves. How is this being tested?

Quoted 6 lines

Yeah, this is the only guard type I've never seen a device subscribe to, so not particularly easily tested on my side. I just forced the code path down here anyway and checked if the result matches the result from the existing code calling "ip_compute_csum()". Maybe I can just continue using that as I suspect devices using that can't handle split data intervals that I'm trying to enable.

Wouldn't csum_fold() combine the halves correctly?

Anyway, it needs to be tested.

- Eric

maintainer
#4 lore raw
Quoted 16 lines · Eric Biggers
Quoted 15 lines · Keith Busch
Quoted 13 lines · Eric Biggers

block/t10-pi.c line 140

Quoted 9 lines · Keith Busch
block/t10-pi.c +3 -5
+static void blk_set_ip_pi(struct t10_pi_tuple *pi,
+			  struct blk_integrity_iter *iter)
 {
-	u8 offset = bi->pi_offset;
-	unsigned int i;
-
-	for (i = 0 ; i < iter->data_size ; i += iter->interval) {
-		struct crc64_pi_tuple *pi = iter->prot_buf + offset;
+	__be16 csum = (__force __be16)~(lower_16_bits(iter->crc));

This just throws away half of the checksum instead of properly combining the two halves. How is this being tested?

Quoted 8 lines
Quoted 6 lines

Yeah, this is the only guard type I've never seen a device subscribe to, so not particularly easily tested on my side. I just forced the code path down here anyway and checked if the result matches the result from the existing code calling "ip_compute_csum()". Maybe I can just continue using that as I suspect devices using that can't handle split data intervals that I'm trying to enable.

Wouldn't csum_fold() combine the halves correctly?

It doesn't look like that would be correct if we assume the existing code is correct. The current result from ip_compute_csum() just downcasts the result without folding, just like I'm doing here. My new code is produces the same result as the existing code, so worst case scenario, this isn't introducing a regression.

Quoted 1 line

Anyway, it needs to be tested.

Like on real hardware? I'm a bit at a loss as to how, I've never seen anything subscribe to this format, not even in emulation. The only thing I can readily do to test this is run random data through the old code, print the result, then run the same data through the new code and see if they're the same. That test is successful. Not good enough?

#5 lore raw

Keith,

Quoted 2 lines

Like on real hardware? I'm a bit at a loss as to how, I've never seen anything subscribe to this format, not even in emulation.

# modinfo scsi_debug | grep guard
parm:           guard:protection checksum: 0=crc, 1=ip (def=0) (uint)
Signature · 1 line
Martin K. Petersen
maintainer
#6 lore raw
Quoted 8 lines · Martin K. Petersen

Keith,

Quoted 2 lines

Like on real hardware? I'm a bit at a loss as to how, I've never seen anything subscribe to this format, not even in emulation.

# modinfo scsi_debug | grep guard
parm:           guard:protection checksum: 0=crc, 1=ip (def=0) (uint)

Awesome, thanks for the pointer. I'll give this a try.

#7 lore raw
In reply to Keith Busch
Quoted 32 lines · Keith Busch
Quoted 16 lines · Eric Biggers
Quoted 15 lines · Keith Busch
Quoted 13 lines · Eric Biggers

block/t10-pi.c line 140

Quoted 9 lines · Keith Busch
block/t10-pi.c +3 -5
+static void blk_set_ip_pi(struct t10_pi_tuple *pi,
+			  struct blk_integrity_iter *iter)
 {
-	u8 offset = bi->pi_offset;
-	unsigned int i;
-
-	for (i = 0 ; i < iter->data_size ; i += iter->interval) {
-		struct crc64_pi_tuple *pi = iter->prot_buf + offset;
+	__be16 csum = (__force __be16)~(lower_16_bits(iter->crc));

This just throws away half of the checksum instead of properly combining the two halves. How is this being tested?

Quoted 8 lines
Quoted 6 lines

Yeah, this is the only guard type I've never seen a device subscribe to, so not particularly easily tested on my side. I just forced the code path down here anyway and checked if the result matches the result from the existing code calling "ip_compute_csum()". Maybe I can just continue using that as I suspect devices using that can't handle split data intervals that I'm trying to enable.

Wouldn't csum_fold() combine the halves correctly?

It doesn't look like that would be correct if we assume the existing code is correct. The current result from ip_compute_csum() just downcasts the result without folding, just like I'm doing here. My new code is produces the same result as the existing code, so worst case scenario, this isn't introducing a regression.

Quoted 7 lines
Quoted 1 line

Anyway, it needs to be tested.

Like on real hardware? I'm a bit at a loss as to how, I've never seen anything subscribe to this format, not even in emulation. The only thing I can readily do to test this is run random data through the old code, print the result, then run the same data through the new code and see if they're the same. That test is successful. Not good enough?

ip_compute_csum() returns a folded 16-bit checksum, whereas csum_partial() returns an unfolded 32-bit checksum. I don't see how the checksums can be the same as before. Hence my concern about whether this was tested. It could be with hardware, an emulator, or a unit test.

- Eric

maintainer
#8 lore raw
Quoted 9 lines · Eric Biggers
Quoted 5 lines · Keith Busch

Like on real hardware? I'm a bit at a loss as to how, I've never seen anything subscribe to this format, not even in emulation. The only thing I can readily do to test this is run random data through the old code, print the result, then run the same data through the new code and see if they're the same. That test is successful. Not good enough?

ip_compute_csum() returns a folded 16-bit checksum, whereas csum_partial() returns an unfolded 32-bit checksum.

Sorry, I must be missing something. do_csum() returns an unfolded 32-bit result, and it is just getting down cast to a 16-bit result. Where does the folding happen?

__sum16 ip_compute_csum(const void *buff, int len)

{
        return (__force __sum16)~do_csum(buff, len);
}

In any case, I find that running any random data through it at block interval lengths (4k or 512b) is always producing results that fit in 16 bits anyway, so maybe that's why it appears to be working?

Quoted 4 lines

I don't see how the
checksums can be the same as before. Hence my concern about whether this was tested. It could be with hardware, an emulator, or a unit test.

Martin pointed to a good in-kernel debug module that can do it. I'll set a test up with that.

#9 lore raw
Quoted 8 lines · Keith Busch
Quoted 7 lines · Eric Biggers
Quoted 5 lines · Keith Busch

Like on real hardware? I'm a bit at a loss as to how, I've never seen anything subscribe to this format, not even in emulation. The only thing I can readily do to test this is run random data through the old code, print the result, then run the same data through the new code and see if they're the same. That test is successful. Not good enough?

Quoted 5 lines
Quoted 2 lines

ip_compute_csum() returns a folded 16-bit checksum, whereas csum_partial() returns an unfolded 32-bit checksum.

Sorry, I must be missing something. do_csum() returns an unfolded 32-bit result

Nope. It returns a folded 16-bit result.

Quoted 3 lines

In any case, I find that running any random data through it at block interval lengths (4k or 512b) is always producing results that fit in 16 bits anyway, so maybe that's why it appears to be working?

Your kernel build may be using the generic csum_partial(), which folds the checksum more frequently than the x86 one (and maybe some of the other arch-optimized implementations too). Try the x86 one.

- Eric

maintainer
#10 lore raw
In reply to Keith Busch
Quoted 8 lines · Keith Busch
Quoted 7 lines · Eric Biggers
Quoted 5 lines · Keith Busch

Like on real hardware? I'm a bit at a loss as to how, I've never seen anything subscribe to this format, not even in emulation. The only thing I can readily do to test this is run random data through the old code, print the result, then run the same data through the new code and see if they're the same. That test is successful. Not good enough?

Quoted 6 lines
Quoted 2 lines

ip_compute_csum() returns a folded 16-bit checksum, whereas csum_partial() returns an unfolded 32-bit checksum.

Sorry, I must be missing something. do_csum() returns an unfolded 32-bit result, and it is just getting down cast to a 16-bit result. Where does the folding happen?

My apologies, please disregard that. I see where I am mistaken. It happened to be working based on how the data was lining up in my tests.