Skip to contentSkip to conversation

nvme: fix PI insert on write

PATCH Superseded Christoph Hellwig 3 participants 4 messages Aug 20, 2025 – Aug 21, 2025 NVMe

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Christoph Hellwig

author maintainer
#0 lore raw

PATCH 1/1 nvme: fix PI insert on write

I recently ran into an issue where the PI generated using the block layer integrity code differs from that from a kernel using the PRACT fallback when the block layer integrity code is disabled, and I tracked this down to us using PRACT incorrectly.

The NVM Command Set Specification (section 5.33 in 1.2, similar in older versions) specifies the PRACT insert behavior as:

Inserted protection information consists of the computed CRC for the protection information format (refer to section 5.3.1) in the Guard field, the LBAT field value in the Application Tag field, the LBST field value in the Storage Tag field, if defined, and the computed reference tag in the Logical Block Reference Tag.

Where the computed reference tag is defined as following for type 1 and type 2 using the text below that is duplicated in the respective bullet points:

the value of the computed reference tag for the first logical block of the command is the value contained in the Initial Logical Block Reference Tag (ILBRT) or Expected Initial Logical Block Reference Tag (EILBRT) field in the command, and the computed reference tag is incremented for each subsequent logical block.

So we need to set ILBRT field, but we currently don't. Interestingly this works fine on my older type 1 formatted SSD, but Qemu trips up on this. We already set ILBRT for Write Same since commit aeb7bb061be5 ("nvme: set the PRACT bit when using Write Zeroes with T10 PI").

---
 drivers/nvme/host/core.c | 1 +
 1 file changed, 1 insertion(+)
drivers/nvme/host/core.c +1 -0
index 9707a0e8c431..3bd6ffd1f809 100644
@@ -1039,6 +1039,7 @@ static inline blk_status_t nvme_setup_rw(struct nvme_ns *ns,
 			if (WARN_ON_ONCE(!nvme_ns_has_pi(ns->head)))
 				return BLK_STS_NOTSUPP;
 			control |= NVME_RW_PRINFO_PRACT;
+			nvme_set_ref_tag(ns, cmnd, req);
 		}
 
 		if (bio_integrity_flagged(req->bio, BIP_CHECK_GUARD))
Signature · 1 line
2.47.2
maintainer
#1 lore raw

drivers/nvme/host/core.c static inline blk_status_t nvme_setup_rw(struct nvme_ns *ns, · line 1043

Quoted 10 lines · Christoph Hellwig
drivers/nvme/host/core.c +1 -0
index 9707a0e8c431..3bd6ffd1f809 100644
@@ -1039,6 +1039,7 @@ static inline blk_status_t nvme_setup_rw(struct nvme_ns *ns,
 			if (WARN_ON_ONCE(!nvme_ns_has_pi(ns->head)))
 				return BLK_STS_NOTSUPP;
 			control |= NVME_RW_PRINFO_PRACT;
+			nvme_set_ref_tag(ns, cmnd, req);
 		}

Do we need to check that it's type 1 or 2 before setting this?

#2 lore raw
Quoted 7 lines · Keith Busch

drivers/nvme/host/core.c static inline blk_status_t nvme_setup_rw(struct nvme_ns *ns, · line 1043

Quoted 6 lines
drivers/nvme/host/core.c +1 -0
@@ -1039,6 +1039,7 @@ static inline blk_status_t nvme_setup_rw(struct nvme_ns *ns,
  			if (WARN_ON_ONCE(!nvme_ns_has_pi(ns->head)))
  				return BLK_STS_NOTSUPP;
  			control |= NVME_RW_PRINFO_PRACT;
+			nvme_set_ref_tag(ns, cmnd, req);
  		}

Do we need to check that it's type 1 or 2 before setting this?

Seems so.
And if the check moves inside nvme_set_ref_tag(), it will keep current/future call-sites simple.

author maintainer
#3 lore raw
Quoted 9 lines · Kanchan Joshi
Quoted 7 lines · Keith Busch

drivers/nvme/host/core.c static inline blk_status_t nvme_setup_rw(struct nvme_ns *ns, · line 1043

Quoted 6 lines
drivers/nvme/host/core.c +1 -0
@@ -1039,6 +1039,7 @@ static inline blk_status_t nvme_setup_rw(struct nvme_ns *ns,
  			if (WARN_ON_ONCE(!nvme_ns_has_pi(ns->head)))
  				return BLK_STS_NOTSUPP;
  			control |= NVME_RW_PRINFO_PRACT;
+			nvme_set_ref_tag(ns, cmnd, req);
  		}

Do we need to check that it's type 1 or 2 before setting this?

Quoted 3 lines

Seems so.
And if the check moves inside nvme_set_ref_tag(), it will keep current/future call-sites simple.

Yes, that was my thought as well when reading Keith' reply.