Skip to contentSkip to conversation

Block integrity with flexible-offset PI

PATCH Applied Kanchan Joshi 6 participants 16 messages Feb 1, 2024 – Oct 2, 2024 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): CGME20240201130828epcas5p10bd98bcb6b8e9444603e347c2a910c44@epcas5p1.samsung.com

#1 lore raw

v2 cover Block integrity with flexible-offset PI

The block integrity subsystem can only work with PI placed in the first bytes of the metadata buffer.

The series makes block-integrity support the flexible placement of PI. And changes NVMe driver to make use of the new capability.

This helps to
(i) enable the more common case for NVMe (PI in last bytes is the norm) (ii) reduce nop profile users (tried by Jens recently [1]).

/* For NS 4K+16b, 8b PI, last bytes */
Before:

# cat /sys/block/nvme0n1/integrity/format
nop

After:

# cat /sys/block/nvme0n1/integrity/format
T10-DIF-TYPE1-CRC

Changes since v1:
- Reworded commit messsage in patch 2 and 3 (hch)
- Variable initialization order change (hch)
- Collect reviewed-by

[1] https://lore.kernel.org/linux-block/[email protected]/

Kanchan Joshi (3):
block: refactor guard helpers
block: support PI at non-zero offset within metadata
nvme: allow integrity when PI is not in first bytes

 block/bio-integrity.c         |  1 +
 block/blk-integrity.c         |  1 +
 block/t10-pi.c                | 72 +++++++++++++++++++++++------------
 drivers/nvme/host/core.c      |  8 +++-
 drivers/nvme/host/nvme.h      |  1 +
 include/linux/blk-integrity.h |  1 +
 include/linux/blkdev.h        |  1 +
 7 files changed, 60 insertions(+), 25 deletions(-)
Signature · 1 line
2.25.1
#2 lore raw

PATCH v2 1/3 block: refactor guard helpers

Allow computation using the existing guard value. This is a prep patch.

---
 block/t10-pi.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
block/t10-pi.c +10 -10
index 914d8cddd43a..251a7b188963 100644
@@ -12,14 +12,14 @@
 #include <net/checksum.h>
 #include <asm/unaligned.h>
 
-typedef __be16 (csum_fn) (void *, unsigned int);
+typedef __be16 (csum_fn) (__be16, void *, unsigned int);
 
-static __be16 t10_pi_crc_fn(void *data, unsigned int len)
+static __be16 t10_pi_crc_fn(__be16 crc, void *data, unsigned int len)
 {
-	return cpu_to_be16(crc_t10dif(data, len));
+	return cpu_to_be16(crc_t10dif_update(be16_to_cpu(crc), data, len));
 }
 
-static __be16 t10_pi_ip_fn(void *data, unsigned int len)
+static __be16 t10_pi_ip_fn(__be16 csum, void *data, unsigned int len)
 {
 	return (__force __be16)ip_compute_csum(data, len);
 }
@@ -37,7 +37,7 @@ static blk_status_t t10_pi_generate(struct blk_integrity_iter *iter,
 	for (i = 0 ; i < iter->data_size ; i += iter->interval) {
 		struct t10_pi_tuple *pi = iter->prot_buf;
 
-		pi->guard_tag = fn(iter->data_buf, iter->interval);
+		pi->guard_tag = fn(0, iter->data_buf, iter->interval);
 		pi->app_tag = 0;
 
 		if (type == T10_PI_TYPE1_PROTECTION)
@@ -83,7 +83,7 @@ static blk_status_t t10_pi_verify(struct blk_integrity_iter *iter,
 				goto next;
 		}
 
-		csum = fn(iter->data_buf, iter->interval);
+		csum = fn(0, iter->data_buf, iter->interval);
 
 		if (pi->guard_tag != csum) {
 			pr_err("%s: guard tag error at sector %llu " \
@@ -280,9 +280,9 @@ const struct blk_integrity_profile t10_pi_type3_ip = {
 };
 EXPORT_SYMBOL(t10_pi_type3_ip);
 
-static __be64 ext_pi_crc64(void *data, unsigned int len)
+static __be64 ext_pi_crc64(u64 crc, void *data, unsigned int len)
 {
-	return cpu_to_be64(crc64_rocksoft(data, len));
+	return cpu_to_be64(crc64_rocksoft_update(crc, data, len));
 }
 
 static blk_status_t ext_pi_crc64_generate(struct blk_integrity_iter *iter,
@@ -293,7 +293,7 @@ static blk_status_t ext_pi_crc64_generate(struct blk_integrity_iter *iter,
 	for (i = 0 ; i < iter->data_size ; i += iter->interval) {
 		struct crc64_pi_tuple *pi = iter->prot_buf;
 
-		pi->guard_tag = ext_pi_crc64(iter->data_buf, iter->interval);
+		pi->guard_tag = ext_pi_crc64(0, iter->data_buf, iter->interval);
 		pi->app_tag = 0;
 
 		if (type == T10_PI_TYPE1_PROTECTION)
@@ -343,7 +343,7 @@ static blk_status_t ext_pi_crc64_verify(struct blk_integrity_iter *iter,
 				goto next;
 		}
 
-		csum = ext_pi_crc64(iter->data_buf, iter->interval);
+		csum = ext_pi_crc64(0, iter->data_buf, iter->interval);
 		if (pi->guard_tag != csum) {
 			pr_err("%s: guard tag error at sector %llu " \
 			       "(rcvd %016llx, want %016llx)\n",
Signature · 1 line
2.25.1
#3 lore raw

PATCH v2 2/3 block: support PI at non-zero offset within metadata

In reply to Kanchan Joshi

Block layer integrity processing assumes that protection information (PI) is placed in the first bytes of each metadata block.

Remove this limitation and include the metadata before the PI in the calculation of the guard tag.

---
 block/bio-integrity.c         |  1 +
 block/blk-integrity.c         |  1 +
 block/t10-pi.c                | 52 +++++++++++++++++++++++++----------
 include/linux/blk-integrity.h |  1 +
 include/linux/blkdev.h        |  1 +
 5 files changed, 42 insertions(+), 14 deletions(-)
block/bio-integrity.c +1 -0
index c9a16fba58b9..2e3e8e04961e 100644
@@ -395,6 +395,7 @@ static blk_status_t bio_integrity_process(struct bio *bio,
 	iter.tuple_size = bi->tuple_size;
 	iter.seed = proc_iter->bi_sector;
 	iter.prot_buf = bvec_virt(bip->bip_vec);
+	iter.pi_offset = bi->pi_offset;
 
 	__bio_for_each_segment(bv, bio, bviter, *proc_iter) {
 		void *kaddr = bvec_kmap_local(&bv);
block/blk-integrity.c +1 -0
index d4e9b4556d14..ccbeb6dfa87a 100644
@@ -370,6 +370,7 @@ void blk_integrity_register(struct gendisk *disk, struct blk_integrity *template
 	bi->profile = template->profile ? template->profile : &nop_profile;
 	bi->tuple_size = template->tuple_size;
 	bi->tag_size = template->tag_size;
+	bi->pi_offset = template->pi_offset;
 
 	blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES, disk->queue);
 
block/t10-pi.c +38 -14

158 lines not on this page · raw message

include/linux/blk-integrity.h +1 -0
index 378b2459efe2..e253e7bd0d17 100644
@@ -20,6 +20,7 @@ struct blk_integrity_iter {
 	unsigned int		data_size;
 	unsigned short		interval;
 	unsigned char		tuple_size;
+	unsigned char		pi_offset;
 	const char		*disk_name;
 };
 
include/linux/blkdev.h +1 -0
index d7cac3de65b3..bb4d811fee46 100644
@@ -108,6 +108,7 @@ struct blk_integrity {
 	const struct blk_integrity_profile	*profile;
 	unsigned char				flags;
 	unsigned char				tuple_size;
+	unsigned char				pi_offset;
 	unsigned char				interval_exp;
 	unsigned char				tag_size;
 };
Signature · 1 line
2.25.1
maintainer
#4 lore raw

Re: [PATCH v2 2/3] block: support PI at non-zero offset within metadata

Quoted 5 lines · Kanchan Joshi

Block layer integrity processing assumes that protection information (PI) is placed in the first bytes of each metadata block.

Remove this limitation and include the metadata before the PI in the calculation of the guard tag.

Very late reply, but I am just now discovering the consequences of this patch.

We have drives with this format, 64b metadata with PI at the end. With previous kernels, we had written data to these drives. Those kernel versions disabled the GUARD generation, so the metadata was written without it, and everything was fine.

Now we upgrade to 6.9+, and this kernel enables the GUARD check. All the data previously written to this drive is unreadable because the GUARD is invalid.

Not sure exactly what to do about this, but it is a broken kernel upgrade path, so wanted to throw that information out there.

#5 lore raw

Re: [PATCH v2 2/3] block: support PI at non-zero offset within metadata

Quoted 15 lines · Keith Busch
Quoted 5 lines · Kanchan Joshi

Block layer integrity processing assumes that protection information (PI) is placed in the first bytes of each metadata block.

Remove this limitation and include the metadata before the PI in the calculation of the guard tag.

Very late reply, but I am just now discovering the consequences of this patch.

We have drives with this format, 64b metadata with PI at the end. With previous kernels, we had written data to these drives. Those kernel versions disabled the GUARD generation, so the metadata was written without it, and everything was fine.

Quoted 7 lines

Now we upgrade to 6.9+, and this kernel enables the GUARD check. All the data previously written to this drive is unreadable because the GUARD is invalid.

Not sure exactly what to do about this, but it is a broken kernel upgrade path, so wanted to throw that information out there.

Ah, writing to the disk without any guard, but after that reading with the guard!

I wish if there was a way to format the NVMe only to reset its pi type (from non-zero to 0).
But there are kernel knobs too. Hope you are able to get to the same state (as nop profile) by clearing write_generate and read_verify:
echo 0 > /sys/block/nvme0n1/integrity/read_verify

maintainer
#6 lore raw

Re: [PATCH v2 2/3] block: support PI at non-zero offset within metadata

Quoted 3 lines · Kanchan Joshi

But there are kernel knobs too. Hope you are able to get to the same state (as nop profile) by clearing write_generate and read_verify:
echo 0 > /sys/block/nvme0n1/integrity/read_verify

It's not the kernel's verify causing the failure; it's the end device. For nvme, it'll return status 0x282, End-to-end Guard Check Error, so the kernel doesn't have a chance to check the data. We'd need to turn off the command's NVME_RW_PRINFO_PRCHK_GUARD flag, but there's currently no knob to toggle that.

#7 lore raw

Re: [PATCH v2 2/3] block: support PI at non-zero offset within metadata

Quoted 10 lines · Keith Busch
Quoted 3 lines · Kanchan Joshi

But there are kernel knobs too. Hope you are able to get to the same state (as nop profile) by clearing write_generate and read_verify:
echo 0 > /sys/block/nvme0n1/integrity/read_verify

It's not the kernel's verify causing the failure; it's the end device. For nvme, it'll return status 0x282, End-to-end Guard Check Error, so the kernel doesn't have a chance to check the data. We'd need to turn off the command's NVME_RW_PRINFO_PRCHK_GUARD flag, but there's currently no knob to toggle that.

Indeed.
I spent a good deal of time on this today. I was thinking to connect block read_verify/write_generate knobs to influence things at nvme level (those PRCHK flags). But that will not be enough. Because with those knobs block-layer will not attach meta-buffer, which is still needed.

The data was written under the condition when nvme driver set the pi_type to 0 (even though at device level it was non-zero) during integrity registration.

Thinking whether it will make sense to have a knob at the block-layer level to do something like that i.e., override the set integrity-profile with nop.

#8 lore raw

Re: [PATCH v2 2/3] block: support PI at non-zero offset within metadata

Kanchan,

Quoted 5 lines

I spent a good deal of time on this today. I was thinking to connect block read_verify/write_generate knobs to influence things at nvme level (those PRCHK flags). But that will not be enough. Because with those knobs block-layer will not attach meta-buffer, which is still needed.

Quoted 7 lines

The data was written under the condition when nvme driver set the pi_type to 0 (even though at device level it was non-zero) during integrity registration.

Thinking whether it will make sense to have a knob at the block-layer level to do something like that i.e., override the set integrity-profile with nop.

SCSI went to great lengths to ensure that invalid protection information would never be written during normal operation, regardless of whether the host sent PI or not. And thus the only time one would anticipate a PI error was if the data had actually been corrupted.

Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering
#9 lore raw

Re: [PATCH v2 2/3] block: support PI at non-zero offset within metadata

Quoted 12 lines · Martin K. Petersen

Kanchan,

Quoted 9 lines

I spent a good deal of time on this today. I was thinking to connect block read_verify/write_generate knobs to influence things at nvme level (those PRCHK flags). But that will not be enough. Because with those knobs block-layer will not attach meta-buffer, which is still needed.

The data was written under the condition when nvme driver set the pi_type to 0 (even though at device level it was non-zero) during integrity registration.

Quoted 9 lines
Quoted 3 lines

Thinking whether it will make sense to have a knob at the block-layer level to do something like that i.e., override the set integrity-profile with nop.

SCSI went to great lengths to ensure that invalid protection information would never be written during normal operation, regardless of whether the host sent PI or not. And thus the only time one would anticipate a PI error was if the data had actually been corrupted.

Is this something we should work on bringin to the NVMe TWG?

Javier

maintainer
#10 lore raw

Re: [PATCH v2 2/3] block: support PI at non-zero offset within metadata

Quoted 17 lines · Javier González
Quoted 16 lines · Martin K. Petersen

Kanchan,

Quoted 12 lines

I spent a good deal of time on this today. I was thinking to connect block read_verify/write_generate knobs to influence things at nvme level (those PRCHK flags). But that will not be enough. Because with those knobs block-layer will not attach meta-buffer, which is still needed.

The data was written under the condition when nvme driver set the pi_type to 0 (even though at device level it was non-zero) during integrity registration.

Thinking whether it will make sense to have a knob at the block-layer level to do something like that i.e., override the set integrity-profile with nop.

Quoted 7 lines
Quoted 5 lines

SCSI went to great lengths to ensure that invalid protection information would never be written during normal operation, regardless of whether the host sent PI or not. And thus the only time one would anticipate a PI error was if the data had actually been corrupted.

Is this something we should work on bringin to the NVMe TWG?

Maybe. It looks like they did the spec this way one purpose with the ability to toggle guard tags per IO.

Just some more background on this because it may sound odd to use a data protection namespace format that the kernel didn't support:

In this use case, writes to the device primarily come from the passthrough interface, which could always use the guard tags for end-to-end protection. The kernel block IO was the only path that had the limitation.

Besides the passthrough interface, though, the setup uses kernel block layer to write the partition tables. Upgrading from 6.8 -> 6.9 won't be able to read the partition table on these devices. I'm still not sure the best way to handle this, though.

#11 lore raw

Re: [PATCH v2 2/3] block: support PI at non-zero offset within metadata

Quoted 37 lines · Keith Busch
Quoted 17 lines · Javier González
Quoted 16 lines · Martin K. Petersen

Kanchan,

Quoted 12 lines

I spent a good deal of time on this today. I was thinking to connect block read_verify/write_generate knobs to influence things at nvme level (those PRCHK flags). But that will not be enough. Because with those knobs block-layer will not attach meta-buffer, which is still needed.

The data was written under the condition when nvme driver set the pi_type to 0 (even though at device level it was non-zero) during integrity registration.

Thinking whether it will make sense to have a knob at the block-layer level to do something like that i.e., override the set integrity-profile with nop.

Quoted 7 lines
Quoted 5 lines

SCSI went to great lengths to ensure that invalid protection information would never be written during normal operation, regardless of whether the host sent PI or not. And thus the only time one would anticipate a PI error was if the data had actually been corrupted.

Is this something we should work on bringin to the NVMe TWG?

Maybe. It looks like they did the spec this way one purpose with the ability to toggle guard tags per IO.

Just some more background on this because it may sound odd to use a data protection namespace format that the kernel didn't support:

In this use case, writes to the device primarily come from the passthrough interface, which could always use the guard tags for end-to-end protection. The kernel block IO was the only path that had the limitation.

Quoted 4 lines

Besides the passthrough interface, though, the setup uses kernel block layer to write the partition tables. Upgrading from 6.8 -> 6.9 won't be able to read the partition table on these devices. I'm still not sure the best way to handle this, though.

Mmmm. Need to think more about this.

Seems this is by design in NVMe. Will try to come back to Mike and Judy to understand the background for this at the time...

#12 lore raw

Re: [PATCH v2 2/3] block: support PI at non-zero offset within metadata

In reply to Keith Busch

Keith,

Quoted 4 lines

Besides the passthrough interface, though, the setup uses kernel block layer to write the partition tables. Upgrading from 6.8 -> 6.9 won't be able to read the partition table on these devices. I'm still not sure the best way to handle this, though.

That's why, despite Type 2 supporting offset ref tags, the kernel always uses the LBA. This is to ensure that any application which doesn't have prior knowledge of the contents of each block's PI can perform a read without failing. Including the kernel itself when reading the partition table. But it's not just a kernel problem, the BIOS also needs to be able to read arbitrary blocks, at least at the beginning and end of the device.

Many of the features of the original DIF spec were squarely aimed at use inside storage arrays where there was a single entity, the array firmware, performing all reads and writes.

With DIX, since the interface needed to be used by array firmware and general purpose operating systems alike, we needed to add the all required features to the interface to accommodate every use case. And NVMe inherited this. But NVMe did not really capture which things are suitable only in the "single application" scenario.

I'm not sure there's a good fix. Other than as a passthrough user, the burden is on you to ensure that things can be read by somebody else. Could be backup applications, virus scanners, system firmware, etc.

Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering
maintainer
#13 lore raw

Re: [PATCH v2 2/3] block: support PI at non-zero offset within metadata

Quoted 3 lines · Martin K. Petersen

I'm not sure there's a good fix. Other than as a passthrough user, the burden is on you to ensure that things can be read by somebody else. Could be backup applications, virus scanners, system firmware, etc.

Maybe we should disable device side guard checks on read commands. NVMe spec allows us to do that. This way the host would always be able to read data, and the host can optionally validate the guard on its side if it wants to.

#14 lore raw

PATCH v2 3/3 nvme: allow integrity when PI is not in first bytes

In reply to Kanchan Joshi

NVM command set 1.0 (or later) mandates PI to be in the last bytes of metadata. But this was not supported in the block-layer, and driver registered a nop profile.

Since block-integrity can now handle flexible PI offset, change the driver to support this configuration.

---
 drivers/nvme/host/core.c | 8 +++++++-
 drivers/nvme/host/nvme.h | 1 +
 2 files changed, 8 insertions(+), 1 deletion(-)
drivers/nvme/host/core.c +7 -1
index 85ab0fcf9e88..4945d6259a13 100644
@@ -1726,6 +1726,7 @@ static void nvme_init_integrity(struct gendisk *disk,
 	}
 
 	integrity.tuple_size = head->ms;
+	integrity.pi_offset = head->pi_offset;
 	blk_integrity_register(disk, &integrity);
 	blk_queue_max_integrity_segments(disk->queue, max_integrity_segments);
 }
@@ -1835,11 +1836,16 @@ static int nvme_init_ms(struct nvme_ctrl *ctrl, struct nvme_ns_head *head,
 free_data:
 	kfree(nvm);
 set_pi:
-	if (head->pi_size && (first || head->ms == head->pi_size))
+	if (head->pi_size && head->ms >= head->pi_size)
 		head->pi_type = id->dps & NVME_NS_DPS_PI_MASK;
 	else
 		head->pi_type = 0;
 
+	if (first)
+		head->pi_offset = 0;
+	else
+		head->pi_offset = head->ms - head->pi_size;
+
 	return ret;
 }
 
drivers/nvme/host/nvme.h +1 -0
index 030c80818240..281657320c3a 100644
@@ -462,6 +462,7 @@ struct nvme_ns_head {
 	u16			ms;
 	u16			pi_size;
 	u8			pi_type;
+	u8			pi_offset;
 	u8			guard_type;
 	u16			sgs;
 	u32			sws;
Signature · 1 line
2.25.1
#15 lore raw
In reply to Kanchan Joshi
Quoted 6 lines · Kanchan Joshi

The block integrity subsystem can only work with PI placed in the first bytes of the metadata buffer.

The series makes block-integrity support the flexible placement of PI. And changes NVMe driver to make use of the new capability.

Hi Jens,

This has got the necessary reviews. Can this be picked up? Or do you see anything missing?

maintainer Applied
#16 lore raw
In reply to Kanchan Joshi
Quoted 11 lines · Kanchan Joshi

The block integrity subsystem can only work with PI placed in the first bytes of the metadata buffer.

The series makes block-integrity support the flexible placement of PI. And changes NVMe driver to make use of the new capability.

This helps to
(i) enable the more common case for NVMe (PI in last bytes is the norm) (ii) reduce nop profile users (tried by Jens recently [1]).

[...]

Applied, thanks!

[1/3] block: refactor guard helpers

      commit: 6b5c132a3f0d3b7c024ae98f0ace07c04d32cf73
[2/3] block: support PI at non-zero offset within metadata
      commit: 60d21aac52e26531affdadb7543fe5b93f58b450
[3/3] nvme: allow integrity when PI is not in first bytes
      commit: 921e81db524d17db683cc29aed7ff02f06ea3f96

Best regards,

Signature · 1 line
Jens Axboe