Skip to contentSkip to conversation

Fix nvme copy command with pi metadata

PATCH In review Dmitry Tikhov 2 participants 7 messages Apr 20, 2022 – Apr 21, 2022

lore
Share & tools
lore
#0 lore raw

cover Fix nvme copy command with pi metadata

Current implementation of copy command, for namespace with end-to-end data protection enabled, always returns data integrity field check errors.
For example, issuing with nvme-cli:

    nvme copy --sdlba=25 --blocks=2,1,3 --slbs=1,37,50 --prinfow=5
    --prinfor=5 --ref-tag=25 --expected-ref-tags=1,37,50 /dev/nvme0n1

Always returns End-to-end Reference Tag Check Error.
To reproduce you may need to use upstream version of nvme-cli since there was a bug which prevented passing prinfow to a command, fixed in 2cf9825 commit.

This patch set attempts to fix copy command for data protection enabled namespaces.

Dmitry Tikhov (2):
hw/nvme: refactor check of disabled dif
hw/nvme: fix copy cmd for pi enabled namespaces

 hw/nvme/ctrl.c       |   5 ++
 hw/nvme/dif.c        | 186 +++++++++++++++++++++++++++++++++----------
 hw/nvme/dif.h        |   3 +
 hw/nvme/trace-events |   4 +-
 4 files changed, 155 insertions(+), 43 deletions(-)
Signature · 1 line
2.35.1
#1 lore raw

PATCH 1/2 hw/nvme: refactor check of disabled dif

Move to a separate function code determining whether protection checking in end-to-end data protection enabled namespace should be done. Currently this code is used only in nvme_dif_prchk_crc16 and nvme_dif_prchk_crc64 functions.

---
 hw/nvme/dif.c        | 121 ++++++++++++++++++++++++++++---------------
 hw/nvme/dif.h        |   1 +
 hw/nvme/trace-events |   4 +-
 3 files changed, 83 insertions(+), 43 deletions(-)
hw/nvme/dif.c +80 -41
index 63c44c86ab..0f65687396 100644
@@ -60,6 +60,75 @@ static uint64_t crc64_nvme(uint64_t crc, const unsigned char *buffer,
     return crc ^ (uint64_t)~0;
 }
 
+static bool nvme_dif_is_disabled_crc16(NvmeNamespace *ns, NvmeDifTuple *dif)
+{
+    switch (NVME_ID_NS_DPS_TYPE(ns->id_ns.dps)) {
+    case NVME_ID_NS_DPS_TYPE_3:
+        if (be32_to_cpu(dif->g16.reftag) != 0xffffffff) {
+            break;
+        }
+
+        /* fallthrough */
+    case NVME_ID_NS_DPS_TYPE_1:
+    case NVME_ID_NS_DPS_TYPE_2:
+        if (be16_to_cpu(dif->g16.apptag) != 0xffff) {
+            break;
+        }
+
+        trace_pci_nvme_dif_is_disabled_crc16(be16_to_cpu(dif->g16.apptag),
+                                             be32_to_cpu(dif->g16.reftag));
+
+        return true;
+    }
+
+    return false;
+}
+
+static bool nvme_dif_is_disabled_crc64(NvmeNamespace *ns, NvmeDifTuple *dif)
+{
+    uint64_t r = 0;
+
+    r |= (uint64_t)dif->g64.sr[0] << 40;
+    r |= (uint64_t)dif->g64.sr[1] << 32;
+    r |= (uint64_t)dif->g64.sr[2] << 24;
+    r |= (uint64_t)dif->g64.sr[3] << 16;
+    r |= (uint64_t)dif->g64.sr[4] << 8;
+    r |= (uint64_t)dif->g64.sr[5];
+
+    switch (NVME_ID_NS_DPS_TYPE(ns->id_ns.dps)) {
+    case NVME_ID_NS_DPS_TYPE_3:
+        if (r != 0xffffffffffff) {
+            break;
+        }
+
+        /* fallthrough */
+    case NVME_ID_NS_DPS_TYPE_1:
+    case NVME_ID_NS_DPS_TYPE_2:
+        if (be16_to_cpu(dif->g64.apptag) != 0xffff) {
+            break;
+        }
+
+        trace_pci_nvme_dif_is_disabled_crc64(be16_to_cpu(dif->g16.apptag),
+                                             r);
+
+        return true;
+    }
+
+    return false;
+}
+
+bool nvme_dif_is_disabled(NvmeNamespace *ns, NvmeDifTuple *dif)
+{
+    switch (ns->pif) {
+    case NVME_PI_GUARD_16:
+        return nvme_dif_is_disabled_crc16(ns, dif);
+    case NVME_PI_GUARD_64:
+        return nvme_dif_is_disabled_crc64(ns, dif);
+    default:
+        abort();
+    }
+}
+
 static void nvme_dif_pract_generate_dif_crc16(NvmeNamespace *ns, uint8_t *buf,
                                               size_t len, uint8_t *mbuf,
                                               size_t mlen, uint16_t apptag,
@@ -155,22 +224,7 @@ static uint16_t nvme_dif_prchk_crc16(NvmeNamespace *ns, NvmeDifTuple *dif,
                                      uint8_t prinfo, uint16_t apptag,
                                      uint16_t appmask, uint64_t reftag)
 {
-    switch (NVME_ID_NS_DPS_TYPE(ns->id_ns.dps)) {
-    case NVME_ID_NS_DPS_TYPE_3:
-        if (be32_to_cpu(dif->g16.reftag) != 0xffffffff) {
-            break;
-        }
-
-        /* fallthrough */
-    case NVME_ID_NS_DPS_TYPE_1:
-    case NVME_ID_NS_DPS_TYPE_2:
-        if (be16_to_cpu(dif->g16.apptag) != 0xffff) {
-            break;
-        }
-
-        trace_pci_nvme_dif_prchk_disabled_crc16(be16_to_cpu(dif->g16.apptag),
-                                                be32_to_cpu(dif->g16.reftag));
-
+    if (nvme_dif_is_disabled_crc16(ns, dif)) {
         return NVME_SUCCESS;
     }
 
@@ -214,31 +268,7 @@ static uint16_t nvme_dif_prchk_crc64(NvmeNamespace *ns, NvmeDifTuple *dif,
                                      uint8_t prinfo, uint16_t apptag,
                                      uint16_t appmask, uint64_t reftag)
 {
-    uint64_t r = 0;
-
-    r |= (uint64_t)dif->g64.sr[0] << 40;
-    r |= (uint64_t)dif->g64.sr[1] << 32;
-    r |= (uint64_t)dif->g64.sr[2] << 24;
-    r |= (uint64_t)dif->g64.sr[3] << 16;
-    r |= (uint64_t)dif->g64.sr[4] << 8;
-    r |= (uint64_t)dif->g64.sr[5];
-
-    switch (NVME_ID_NS_DPS_TYPE(ns->id_ns.dps)) {
-    case NVME_ID_NS_DPS_TYPE_3:
-        if (r != 0xffffffffffff) {
-            break;
-        }
-
-        /* fallthrough */
-    case NVME_ID_NS_DPS_TYPE_1:
-    case NVME_ID_NS_DPS_TYPE_2:
-        if (be16_to_cpu(dif->g64.apptag) != 0xffff) {
-            break;
-        }
-
-        trace_pci_nvme_dif_prchk_disabled_crc64(be16_to_cpu(dif->g16.apptag),
-                                                r);
-
+    if (nvme_dif_is_disabled_crc64(ns, dif)) {
         return NVME_SUCCESS;
     }
 
@@ -266,6 +296,15 @@ static uint16_t nvme_dif_prchk_crc64(NvmeNamespace *ns, NvmeDifTuple *dif,
     }
 
     if (prinfo & NVME_PRINFO_PRCHK_REF) {
+        uint64_t r = 0;
+
+        r |= (uint64_t)dif->g64.sr[0] << 40;
+        r |= (uint64_t)dif->g64.sr[1] << 32;
+        r |= (uint64_t)dif->g64.sr[2] << 24;
+        r |= (uint64_t)dif->g64.sr[3] << 16;
+        r |= (uint64_t)dif->g64.sr[4] << 8;
+        r |= (uint64_t)dif->g64.sr[5];
+
         trace_pci_nvme_dif_prchk_reftag_crc64(r, reftag);
 
         if (r != reftag) {
hw/nvme/dif.h +1 -0
index f12e312250..fe1e5828d7 100644
@@ -186,6 +186,7 @@ uint16_t nvme_dif_check(NvmeNamespace *ns, uint8_t *buf, size_t len,
                         uint8_t *mbuf, size_t mlen, uint8_t prinfo,
                         uint64_t slba, uint16_t apptag,
                         uint16_t appmask, uint64_t *reftag);
+bool nvme_dif_is_disabled(NvmeNamespace *ns, NvmeDifTuple *dif);
 uint16_t nvme_dif_rw(NvmeCtrl *n, NvmeRequest *req);
 
 #endif /* HW_NVME_DIF_H */
hw/nvme/trace-events +2 -2
index ff1b458969..c4e75b1f5d 100644
@@ -23,8 +23,8 @@ pci_nvme_dif_rw_check_cb(uint16_t cid, uint8_t prinfo, uint16_t apptag, uint16_t
 pci_nvme_dif_pract_generate_dif_crc16(size_t len, size_t lba_size, size_t chksum_len, uint16_t apptag, uint32_t reftag) "len %zu lba_size %zu chksum_len %zu apptag 0x%"PRIx16" reftag 0x%"PRIx32""
 pci_nvme_dif_pract_generate_dif_crc64(size_t len, size_t lba_size, size_t chksum_len, uint16_t apptag, uint64_t reftag) "len %zu lba_size %zu chksum_len %zu apptag 0x%"PRIx16" reftag 0x%"PRIx64""
 pci_nvme_dif_check(uint8_t prinfo, uint16_t chksum_len) "prinfo 0x%"PRIx8" chksum_len %"PRIu16""
-pci_nvme_dif_prchk_disabled_crc16(uint16_t apptag, uint32_t reftag) "apptag 0x%"PRIx16" reftag 0x%"PRIx32""
-pci_nvme_dif_prchk_disabled_crc64(uint16_t apptag, uint64_t reftag) "apptag 0x%"PRIx16" reftag 0x%"PRIx64""
+pci_nvme_dif_is_disabled_crc16(uint16_t apptag, uint32_t reftag) "apptag 0x%"PRIx16" reftag 0x%"PRIx32""
+pci_nvme_dif_is_disabled_crc64(uint16_t apptag, uint64_t reftag) "apptag 0x%"PRIx16" reftag 0x%"PRIx64""
 pci_nvme_dif_prchk_guard_crc16(uint16_t guard, uint16_t crc) "guard 0x%"PRIx16" crc 0x%"PRIx16""
 pci_nvme_dif_prchk_guard_crc64(uint64_t guard, uint64_t crc) "guard 0x%"PRIx64" crc 0x%"PRIx64""
 pci_nvme_dif_prchk_apptag(uint16_t apptag, uint16_t elbat, uint16_t elbatm) "apptag 0x%"PRIx16" elbat 0x%"PRIx16" elbatm 0x%"PRIx16""
Signature · 1 line
2.35.1
#2 lore raw

PATCH 2/2 hw/nvme: fix copy cmd for pi enabled namespaces

In reply to Dmitry Tikhov

Current implementation have two problems:
First in the read part of copy command. Because there is no metadata mangling before nvme_dif_check invocation, reftag error is thrown for blocks of namespace that have not been previously written to. Second in the write part. Reftag in the protection information section of the source metadata should not be copied as is to the destination. Source range start lba and destination range start lba could differ so recalculation of reftag is always needed.

---
 hw/nvme/ctrl.c |  5 ++++
 hw/nvme/dif.c  | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/nvme/dif.h  |  2 ++
 3 files changed, 72 insertions(+)
hw/nvme/ctrl.c +5 -0
index 74540a03d5..cb493f4506 100644
@@ -2787,6 +2787,10 @@ static void nvme_copy_in_completed_cb(void *opaque, int ret)
         size_t mlen = nvme_m2b(ns, nlb);
         uint8_t *mbounce = iocb->bounce + nvme_l2b(ns, nlb);
 
+        status = nvme_dif_mangle_mdata(ns, mbounce, mlen, reftag);
+        if (status) {
+            goto invalid;
+        }
         status = nvme_dif_check(ns, iocb->bounce, len, mbounce, mlen, prinfor,
                                 slba, apptag, appmask, &reftag);
         if (status) {
@@ -2805,6 +2809,7 @@ static void nvme_copy_in_completed_cb(void *opaque, int ret)
             nvme_dif_pract_generate_dif(ns, iocb->bounce, len, mbounce, mlen,
                                         apptag, &iocb->reftag);
         } else {
+            nvme_dif_restore_reftag(ns, mbounce, mlen, iocb->reftag);
             status = nvme_dif_check(ns, iocb->bounce, len, mbounce, mlen,
                                     prinfow, iocb->slba, apptag, appmask,
                                     &iocb->reftag);
hw/nvme/dif.c +65 -0

71 lines not on this page · raw message

hw/nvme/dif.h +2 -0
index fe1e5828d7..3203837658 100644
@@ -186,6 +186,8 @@ uint16_t nvme_dif_check(NvmeNamespace *ns, uint8_t *buf, size_t len,
                         uint8_t *mbuf, size_t mlen, uint8_t prinfo,
                         uint64_t slba, uint16_t apptag,
                         uint16_t appmask, uint64_t *reftag);
+void nvme_dif_restore_reftag(NvmeNamespace *ns, uint8_t *mbuf,
+                             size_t mlen, uint64_t reftag);
 bool nvme_dif_is_disabled(NvmeNamespace *ns, NvmeDifTuple *dif);
 uint16_t nvme_dif_rw(NvmeCtrl *n, NvmeRequest *req);
 
Signature · 1 line
2.35.1
#3 lore raw

Re: [PATCH 2/2] hw/nvme: fix copy cmd for pi enabled namespaces

Quoted 4 lines · Dmitry Tikhov

Current implementation have two problems:
First in the read part of copy command. Because there is no metadata mangling before nvme_dif_check invocation, reftag error is thrown for blocks of namespace that have not been previously written to.

Yes, this is definitely a bug and the fix is good, thanks!

Quoted 2 lines

Second in the write part. Reftag in the protection information section of the source metadata should not be copied as is to the destination.

Hmm, says who?

Quoted 3 lines

Source range start lba and destination range start lba could differ so recalculation of reftag is always needed.

If PRACT is 0, we really should not touch the protection information. My interpretation of the Copy command is that it is simply just screwed if used with PRACT 0 and Type 1. PRACT bit is specifically to allow the controller to generate appropriate PI in this case.

On the other hand, I can totally see your interpretation as valid as well. Let me ask some spec people about this, and I will get back to you.

#4 lore raw

Re: [PATCH 2/2] hw/nvme: fix copy cmd for pi enabled namespaces

Quoted 17 lines · Klaus Jensen
Quoted 4 lines · Dmitry Tikhov

Current implementation have two problems:
First in the read part of copy command. Because there is no metadata mangling before nvme_dif_check invocation, reftag error is thrown for blocks of namespace that have not been previously written to.

Yes, this is definitely a bug and the fix is good, thanks!

Quoted 2 lines

Second in the write part. Reftag in the protection information section of the source metadata should not be copied as is to the destination.

Hmm, says who?

Quoted 3 lines

Source range start lba and destination range start lba could differ so recalculation of reftag is always needed.

Quoted 8 lines

If PRACT is 0, we really should not touch the protection information. My interpretation of the Copy command is that it is simply just screwed if used with PRACT 0 and Type 1. PRACT bit is specifically to allow the controller to generate appropriate PI in this case.

On the other hand, I can totally see your interpretation as valid as well. Let me ask some spec people about this, and I will get back to you.

Discussed this with the TP authors and, no, reftag should not be re-computed for PRACT 0, regardless of the PI type.

#5 lore raw

Re: [PATCH 2/2] hw/nvme: fix copy cmd for pi enabled namespaces

Quoted 2 lines · Klaus Jensen

Discussed this with the TP authors and, no, reftag should not be re-computed for PRACT 0, regardless of the PI type.

Ok, should i resend patch with only adding nvme_dif_mangle_mdata in the read part?

#6 lore raw

Re: [PATCH 2/2] hw/nvme: fix copy cmd for pi enabled namespaces

Quoted 5 lines · Dmitry Tikhov
Quoted 2 lines · Klaus Jensen

Discussed this with the TP authors and, no, reftag should not be re-computed for PRACT 0, regardless of the PI type.

Ok, should i resend patch with only adding nvme_dif_mangle_mdata in the read part?

Yes, that is still a bug :)