Skip to contentSkip to conversation

hw/block/nvme: oncs and write uncorrectable support

PATCH In review Klaus Jensen 3 participants 11 messages Feb 10, 2021 – Feb 11, 2021

lore
Share & tools
lore
#0 lore raw

cover hw/block/nvme: oncs and write uncorrectable support

From: Klaus Jensen <[email protected]>

First, add support for toggling optional features through the new oncs nvme device parameter.

Secondly, add support for the Write Uncorrectable command.

Gollu Appalanaidu (2):
hw/block/nvme: add oncs device parameter
hw/block/nvme: add write uncorrectable command

 docs/specs/nvme.txt   |   3 +
 hw/block/nvme-ns.h    |   2 +
 hw/block/nvme.h       |   8 ++
 hw/block/nvme-ns.c    |   2 +
 hw/block/nvme.c       | 166 +++++++++++++++++++++++++++++++-----------
 hw/block/trace-events |   1 +
 6 files changed, 140 insertions(+), 42 deletions(-)
Signature · 1 line
2.30.0
#1 lore raw

PATCH 1/2 hw/block/nvme: add oncs device parameter

From: Gollu Appalanaidu <[email protected]>

Add the 'oncs' nvme device parameter to allow optional features to be enabled/disabled explicitly. Since most of these are optional commands, make the CSE log pages dynamic to account for the value of ONCS.

---
 hw/block/nvme.h |   7 ++++
 hw/block/nvme.c | 101 ++++++++++++++++++++++++++++++++----------------
 2 files changed, 74 insertions(+), 34 deletions(-)
hw/block/nvme.h +7 -0
index cb2b5175f1a1..98082b2dfba3 100644
@@ -9,6 +9,7 @@
 
 #define NVME_DEFAULT_ZONE_SIZE   (128 * MiB)
 #define NVME_DEFAULT_MAX_ZA_SIZE (128 * KiB)
+#define NVME_MAX_COMMANDS 0x100
 
 typedef struct NvmeParams {
     char     *serial;
@@ -22,6 +23,7 @@ typedef struct NvmeParams {
     bool     use_intel_id;
     uint32_t zasl_bs;
     bool     legacy_cmb;
+    uint16_t oncs;
 } NvmeParams;
 
 typedef struct NvmeAsyncEvent {
@@ -183,6 +185,11 @@ typedef struct NvmeCtrl {
     NvmeCQueue      admin_cq;
     NvmeIdCtrl      id_ctrl;
     NvmeFeatureVal  features;
+
+    struct {
+        uint32_t nvm[NVME_MAX_COMMANDS];
+        uint32_t zoned[NVME_MAX_COMMANDS];
+    } iocs;
 } NvmeCtrl;
 
 static inline NvmeNamespace *nvme_ns(NvmeCtrl *n, uint32_t nsid)
hw/block/nvme.c +67 -34

192 lines not on this page · raw message

Signature · 1 line
2.30.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] hw/block/nvme: add oncs device parameter

Quoted 8 lines · Klaus Jensen

From: Gollu Appalanaidu <[email protected]>

Add the 'oncs' nvme device parameter to allow optional features to be enabled/disabled explicitly. Since most of these are optional commands, make the CSE log pages dynamic to account for the value of ONCS.

#3 lore raw

PATCH 2/2 hw/block/nvme: add write uncorrectable command

In reply to Klaus Jensen

From: Gollu Appalanaidu <[email protected]>

Add support for marking blocks invalid with the Write Uncorrectable command. Block status is tracked in a (non-persistent) bitmap that is checked on all reads and written to on all writes. This is potentially expensive, so keep Write Uncorrectable disabled by default.

---
 docs/specs/nvme.txt   |  3 ++
 hw/block/nvme-ns.h    |  2 ++
 hw/block/nvme.h       |  1 +
 hw/block/nvme-ns.c    |  2 ++
 hw/block/nvme.c       | 65 +++++++++++++++++++++++++++++++++++++------
 hw/block/trace-events |  1 +
 6 files changed, 66 insertions(+), 8 deletions(-)
docs/specs/nvme.txt +3 -0
index 56d393884e7a..88f9cc278d4c 100644
@@ -19,5 +19,8 @@ Known issues
 
 * The accounting numbers in the SMART/Health are reset across power cycles
 
+* Marking blocks invalid with the Write Uncorrectable is not persisted across
+  power cycles.
+
 * Interrupt Coalescing is not supported and is disabled by default in volation
   of the specification.
hw/block/nvme-ns.h +2 -0
index 7af6884862b5..15fa422ded03 100644
@@ -72,6 +72,8 @@ typedef struct NvmeNamespace {
     struct {
         uint32_t err_rec;
     } features;
+
+    unsigned long *uncorrectable;
 } NvmeNamespace;
 
 static inline uint32_t nvme_nsid(NvmeNamespace *ns)
hw/block/nvme.h +1 -0
index 98082b2dfba3..9b8f85b9cf16 100644
@@ -68,6 +68,7 @@ static inline const char *nvme_io_opc_str(uint8_t opc)
     case NVME_CMD_FLUSH:            return "NVME_NVM_CMD_FLUSH";
     case NVME_CMD_WRITE:            return "NVME_NVM_CMD_WRITE";
     case NVME_CMD_READ:             return "NVME_NVM_CMD_READ";
+    case NVME_CMD_WRITE_UNCOR:      return "NVME_CMD_WRITE_UNCOR";
     case NVME_CMD_COMPARE:          return "NVME_NVM_CMD_COMPARE";
     case NVME_CMD_WRITE_ZEROES:     return "NVME_NVM_CMD_WRITE_ZEROES";
     case NVME_CMD_DSM:              return "NVME_NVM_CMD_DSM";
hw/block/nvme-ns.c +2 -0
index ade46e2f3739..742bbc4b4b62 100644
@@ -72,6 +72,8 @@ static int nvme_ns_init(NvmeNamespace *ns, Error **errp)
     id_ns->mcl = cpu_to_le32(ns->params.mcl);
     id_ns->msrc = ns->params.msrc;
 
+    ns->uncorrectable = bitmap_new(id_ns->nsze);
+
     return 0;
 }
 
hw/block/nvme.c +57 -8

157 lines not on this page · raw message

hw/block/trace-events +1 -0
index 4b5ee04024f4..f30ef220c26a 100644
@@ -128,6 +128,7 @@ pci_nvme_err_invalid_prp2_align(uint64_t prp2) "PRP2 is not page aligned: 0x%"PR
 pci_nvme_err_invalid_opc(uint8_t opc) "invalid opcode 0x%"PRIx8""
 pci_nvme_err_invalid_admin_opc(uint8_t opc) "invalid admin opcode 0x%"PRIx8""
 pci_nvme_err_invalid_lba_range(uint64_t start, uint64_t len, uint64_t limit) "Invalid LBA start=%"PRIu64" len=%"PRIu64" limit=%"PRIu64""
+pci_nvme_err_unrecoverable_read(uint64_t start, uint32_t nlb) "islba 0x%"PRIx64" nlb %"PRIu32""
 pci_nvme_err_invalid_log_page_offset(uint64_t ofs, uint64_t size) "must be <= %"PRIu64", got %"PRIu64""
 pci_nvme_err_cmb_invalid_cba(uint64_t cmbmsc) "cmbmsc 0x%"PRIx64""
 pci_nvme_err_cmb_not_enabled(uint64_t cmbmsc) "cmbmsc 0x%"PRIx64""
Signature · 1 line
2.30.0
Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] hw/block/nvme: add write uncorrectable command

Quoted 106 lines · Klaus Jensen

From: Gollu Appalanaidu <[email protected]>

Add support for marking blocks invalid with the Write Uncorrectable command. Block status is tracked in a (non-persistent) bitmap that is checked on all reads and written to on all writes. This is potentially expensive, so keep Write Uncorrectable disabled by default.

---
 docs/specs/nvme.txt   |  3 ++
 hw/block/nvme-ns.h    |  2 ++
 hw/block/nvme.h       |  1 +
 hw/block/nvme-ns.c    |  2 ++
 hw/block/nvme.c       | 65 +++++++++++++++++++++++++++++++++++++------
 hw/block/trace-events |  1 +
 6 files changed, 66 insertions(+), 8 deletions(-)
docs/specs/nvme.txt +3 -0
index 56d393884e7a..88f9cc278d4c 100644
@@ -19,5 +19,8 @@ Known issues
 
 * The accounting numbers in the SMART/Health are reset across power cycles
 
+* Marking blocks invalid with the Write Uncorrectable is not persisted across
+  power cycles.
+
 * Interrupt Coalescing is not supported and is disabled by default in volation
   of the specification.
hw/block/nvme-ns.h +2 -0
index 7af6884862b5..15fa422ded03 100644
@@ -72,6 +72,8 @@ typedef struct NvmeNamespace {
     struct {
         uint32_t err_rec;
     } features;
+
+    unsigned long *uncorrectable;
 } NvmeNamespace;
 
 static inline uint32_t nvme_nsid(NvmeNamespace *ns)
hw/block/nvme.h +1 -0
index 98082b2dfba3..9b8f85b9cf16 100644
@@ -68,6 +68,7 @@ static inline const char *nvme_io_opc_str(uint8_t opc)
     case NVME_CMD_FLUSH:            return "NVME_NVM_CMD_FLUSH";
     case NVME_CMD_WRITE:            return "NVME_NVM_CMD_WRITE";
     case NVME_CMD_READ:             return "NVME_NVM_CMD_READ";
+    case NVME_CMD_WRITE_UNCOR:      return "NVME_CMD_WRITE_UNCOR";
     case NVME_CMD_COMPARE:          return "NVME_NVM_CMD_COMPARE";
     case NVME_CMD_WRITE_ZEROES:     return "NVME_NVM_CMD_WRITE_ZEROES";
     case NVME_CMD_DSM:              return "NVME_NVM_CMD_DSM";
hw/block/nvme-ns.c +2 -0
index ade46e2f3739..742bbc4b4b62 100644
@@ -72,6 +72,8 @@ static int nvme_ns_init(NvmeNamespace *ns, Error **errp)
     id_ns->mcl = cpu_to_le32(ns->params.mcl);
     id_ns->msrc = ns->params.msrc;
 
+    ns->uncorrectable = bitmap_new(id_ns->nsze);
+
     return 0;
 }
 
hw/block/nvme.c +17 -1
index e5f6666725d7..56048046c193 100644
@@ -1112,6 +1112,20 @@ static uint16_t nvme_check_dulbe(NvmeNamespace *ns, uint64_t slba,
     return NVME_SUCCESS;
 }
 
+static inline uint16_t nvme_check_uncor(NvmeNamespace *ns, uint64_t slba,
+                                        uint32_t nlb)
+{
+    uint64_t elba = nlb + slba;
+
+    if (ns->uncorrectable) {
+        if (find_next_bit(ns->uncorrectable, elba, slba) < elba) {
+            return NVME_UNRECOVERED_READ | NVME_DNR;
+        }
+    }
+
+    return NVME_SUCCESS;
+}
+
 static void nvme_aio_err(NvmeRequest *req, int ret)
 {
     uint16_t status = NVME_SUCCESS;
@@ -1423,14 +1437,24 @@ static void nvme_rw_cb(void *opaque, int ret)
     BlockAcctCookie *acct = &req->acct;
     BlockAcctStats *stats = blk_get_stats(blk);
 
+    bool is_write = nvme_is_write(req);
+
     trace_pci_nvme_rw_cb(nvme_cid(req), blk_name(blk));
 
-    if (ns->params.zoned && nvme_is_write(req)) {
+    if (ns->params.zoned && is_write) {
         nvme_finalize_zoned_write(ns, req);
     }

hw/block/nvme.c static void nvme_rw_cb(void *opaque, int ret) · line 1456

Quoted 10 lines
hw/block/nvme.c +7 -0
 
     if (!ret) {
         block_acct_done(stats, acct);
+
+        if (is_write) {
+            NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd;
+            uint64_t slba = le64_to_cpu(rw->slba);
+            uint32_t nlb = le16_to_cpu(rw->nlb) + 1;
+
+            bitmap_clear(ns->uncorrectable, slba, nlb);

It might be nitpick, 'nlb' would easily represent the value which is defined itself in the spec which is zero-based. Can we have this like:

	uint32_t nlb = le16_to_cpu(rw->nlb);

	bitmap_clear(ns->uncorrectable, slba, nlb + 1);

Otherwise, it looks good to me.

#5 lore raw

Re: [PATCH 2/2] hw/block/nvme: add write uncorrectable command

Quoted 121 lines · Minwoo Im
Quoted 106 lines · Klaus Jensen

From: Gollu Appalanaidu <[email protected]>

Add support for marking blocks invalid with the Write Uncorrectable command. Block status is tracked in a (non-persistent) bitmap that is checked on all reads and written to on all writes. This is potentially expensive, so keep Write Uncorrectable disabled by default.

---
 docs/specs/nvme.txt   |  3 ++
 hw/block/nvme-ns.h    |  2 ++
 hw/block/nvme.h       |  1 +
 hw/block/nvme-ns.c    |  2 ++
 hw/block/nvme.c       | 65 +++++++++++++++++++++++++++++++++++++------
 hw/block/trace-events |  1 +
 6 files changed, 66 insertions(+), 8 deletions(-)
docs/specs/nvme.txt +3 -0
index 56d393884e7a..88f9cc278d4c 100644
@@ -19,5 +19,8 @@ Known issues
 
 * The accounting numbers in the SMART/Health are reset across power cycles
 
+* Marking blocks invalid with the Write Uncorrectable is not persisted across
+  power cycles.
+
 * Interrupt Coalescing is not supported and is disabled by default in volation
   of the specification.
hw/block/nvme-ns.h +2 -0
index 7af6884862b5..15fa422ded03 100644
@@ -72,6 +72,8 @@ typedef struct NvmeNamespace {
     struct {
         uint32_t err_rec;
     } features;
+
+    unsigned long *uncorrectable;
 } NvmeNamespace;
 
 static inline uint32_t nvme_nsid(NvmeNamespace *ns)
hw/block/nvme.h +1 -0
index 98082b2dfba3..9b8f85b9cf16 100644
@@ -68,6 +68,7 @@ static inline const char *nvme_io_opc_str(uint8_t opc)
     case NVME_CMD_FLUSH:            return "NVME_NVM_CMD_FLUSH";
     case NVME_CMD_WRITE:            return "NVME_NVM_CMD_WRITE";
     case NVME_CMD_READ:             return "NVME_NVM_CMD_READ";
+    case NVME_CMD_WRITE_UNCOR:      return "NVME_CMD_WRITE_UNCOR";
     case NVME_CMD_COMPARE:          return "NVME_NVM_CMD_COMPARE";
     case NVME_CMD_WRITE_ZEROES:     return "NVME_NVM_CMD_WRITE_ZEROES";
     case NVME_CMD_DSM:              return "NVME_NVM_CMD_DSM";
hw/block/nvme-ns.c +2 -0
index ade46e2f3739..742bbc4b4b62 100644
@@ -72,6 +72,8 @@ static int nvme_ns_init(NvmeNamespace *ns, Error **errp)
     id_ns->mcl = cpu_to_le32(ns->params.mcl);
     id_ns->msrc = ns->params.msrc;
 
+    ns->uncorrectable = bitmap_new(id_ns->nsze);
+
     return 0;
 }
 
hw/block/nvme.c +17 -1
index e5f6666725d7..56048046c193 100644
@@ -1112,6 +1112,20 @@ static uint16_t nvme_check_dulbe(NvmeNamespace *ns, uint64_t slba,
     return NVME_SUCCESS;
 }
 
+static inline uint16_t nvme_check_uncor(NvmeNamespace *ns, uint64_t slba,
+                                        uint32_t nlb)
+{
+    uint64_t elba = nlb + slba;
+
+    if (ns->uncorrectable) {
+        if (find_next_bit(ns->uncorrectable, elba, slba) < elba) {
+            return NVME_UNRECOVERED_READ | NVME_DNR;
+        }
+    }
+
+    return NVME_SUCCESS;
+}
+
 static void nvme_aio_err(NvmeRequest *req, int ret)
 {
     uint16_t status = NVME_SUCCESS;
@@ -1423,14 +1437,24 @@ static void nvme_rw_cb(void *opaque, int ret)
     BlockAcctCookie *acct = &req->acct;
     BlockAcctStats *stats = blk_get_stats(blk);
 
+    bool is_write = nvme_is_write(req);
+
     trace_pci_nvme_rw_cb(nvme_cid(req), blk_name(blk));
 
-    if (ns->params.zoned && nvme_is_write(req)) {
+    if (ns->params.zoned && is_write) {
         nvme_finalize_zoned_write(ns, req);
     }

hw/block/nvme.c static void nvme_rw_cb(void *opaque, int ret) · line 1456

Quoted 10 lines
hw/block/nvme.c +7 -0
 
     if (!ret) {
         block_acct_done(stats, acct);
+
+        if (is_write) {
+            NvmeRwCmd *rw = (NvmeRwCmd *)&req->cmd;
+            uint64_t slba = le64_to_cpu(rw->slba);
+            uint32_t nlb = le16_to_cpu(rw->nlb) + 1;
+
+            bitmap_clear(ns->uncorrectable, slba, nlb);

It might be nitpick, 'nlb' would easily represent the value which is defined itself in the spec which is zero-based. Can we have this like:

Quoted 4 lines
	uint32_t nlb = le16_to_cpu(rw->nlb);

	bitmap_clear(ns->uncorrectable, slba, nlb + 1);

I do not disagree, but the uint32_t nlb = le16_to_cpu(rw->nlb) + 1; pattern is already used in several places.

Quoted 3 lines

Otherwise, it looks good to me.

Signature · 1 line
One of us - No more doubt, silence or taboo about mental illness.
#6 lore raw

Re: [PATCH 2/2] hw/block/nvme: add write uncorrectable command

Quoted 11 lines
Quoted 7 lines

It might be nitpick, 'nlb' would easily represent the value which is defined itself in the spec which is zero-based. Can we have this like:

	uint32_t nlb = le16_to_cpu(rw->nlb);

	bitmap_clear(ns->uncorrectable, slba, nlb + 1);

I do not disagree, but the uint32_t nlb = le16_to_cpu(rw->nlb) + 1; pattern is already used in several places.

Oh yes, Now I just saw some places. Then, please take my review tag for this patch.

Thanks!

#7 lore raw

Re: [PATCH 2/2] hw/block/nvme: add write uncorrectable command

In reply to Klaus Jensen
Quoted 6 lines · Klaus Jensen

From: Gollu Appalanaidu <[email protected]>

Add support for marking blocks invalid with the Write Uncorrectable command. Block status is tracked in a (non-persistent) bitmap that is checked on all reads and written to on all writes. This is potentially expensive, so keep Write Uncorrectable disabled by default.

I really think attempting to emulate all these things is putting a potentially unnecessary maintenance burden on this device.

The DULBE implementation started off similiar, but I suggested it leverage support out of the backing file, and I feel it ended up better for it.

But unlike punching and checking for holes, there's no filesystem support for Write Uncorrectable in our qemu API, and that's probably because this is kind of a niche feature. Is there a use case with a real qemu guest wanting this?

#8 lore raw

Re: [PATCH 2/2] hw/block/nvme: add write uncorrectable command

Quoted 11 lines · Keith Busch
Quoted 6 lines · Klaus Jensen

From: Gollu Appalanaidu <[email protected]>

Add support for marking blocks invalid with the Write Uncorrectable command. Block status is tracked in a (non-persistent) bitmap that is checked on all reads and written to on all writes. This is potentially expensive, so keep Write Uncorrectable disabled by default.

I really think attempting to emulate all these things is putting a potentially unnecessary maintenance burden on this device.

Even though I myself posted the Telemetry support on behalf of Gollu, I now agree that it would bloat the device with a "useless" feature. We totally accept that and in that initial form there was not a lot of bang for bucks.

This emulated feature comes at a pretty low cost in terms of code and complexity, but I won't argue beyond that. However, it does comes at a performance cost, which is why the intention was to disable it by default.

Quoted 4 lines

The DULBE implementation started off similiar, but I suggested it leverage support out of the backing file, and I feel it ended up better for it.

And thanks for pushing back against that - the solution we ended up with was totally superior, no doubt about that!

Quoted 3 lines

But unlike punching and checking for holes, there's no filesystem support for Write Uncorrectable in our qemu API, and that's probably because this is kind of a niche feature.

True. I don't see any trivial way to support this on a lower level. Even if we persued implementing this in the QEMU block layer I only think it could be supported by "integrated formats" like QCOW2 that could optionally include a persistent bitmap, like the dirty bitmap feature.

Quoted 1 line

Is there a use case with a real qemu guest wanting this?

Like for the extended metadata case (which also does not have a lot of "public" exposure, but definitely have enterprise use), our main motivation here was to ease testing for compliance suites and frameworks like SPDK. I'm honestly have no clue what so ever what a real world use of Write Uncorrectable would be. It's been in the spec since 1.0, so there must have been some reason, Is it just to align with SCSI WRITE LONG? I'm not SCSI expert at all, but from what I can read it looks like that was also intended as a feature for testing read error conditions.

#9 lore raw

Re: [PATCH 2/2] hw/block/nvme: add write uncorrectable command

Quoted 8 lines · Klaus Jensen
Quoted 1 line · Keith Busch

Is there a use case with a real qemu guest wanting this?

Like for the extended metadata case (which also does not have a lot of "public" exposure, but definitely have enterprise use), our main motivation here was to ease testing for compliance suites and frameworks like SPDK.

I'm okay with the metadata patches.

Quoted 5 lines

I'm honestly have no clue what so ever what a real world use of Write Uncorrectable would be. It's been in the spec since 1.0, so there must have been some reason, Is it just to align with SCSI WRITE LONG? I'm not SCSI expert at all, but from what I can read it looks like that was also intended as a feature for testing read error conditions.

I don't think it's for testing purposes.

If you need to send a burst of non-atomic writes (ex: writing a RAID stripe), a power failure can result in an inconsistent state where you don't know at a block level which ones have old data or new data. If you Write Uncorrectable first, you can never read old data, and thus have no "write hole".

Journalling solves this better, and I'm not aware of any real implementation relying on uncorrectable.

#10 lore raw

Re: [PATCH 2/2] hw/block/nvme: add write uncorrectable command

Quoted 20 lines · Keith Busch
Quoted 8 lines · Klaus Jensen
Quoted 1 line · Keith Busch

Is there a use case with a real qemu guest wanting this?

Like for the extended metadata case (which also does not have a lot of "public" exposure, but definitely have enterprise use), our main motivation here was to ease testing for compliance suites and frameworks like SPDK.

I'm okay with the metadata patches.

Quoted 5 lines

I'm honestly have no clue what so ever what a real world use of Write Uncorrectable would be. It's been in the spec since 1.0, so there must have been some reason, Is it just to align with SCSI WRITE LONG? I'm not SCSI expert at all, but from what I can read it looks like that was also intended as a feature for testing read error conditions.

I don't think it's for testing purposes.

Quoted 8 lines

If you need to send a burst of non-atomic writes (ex: writing a RAID stripe), a power failure can result in an inconsistent state where you don't know at a block level which ones have old data or new data. If you Write Uncorrectable first, you can never read old data, and thus have no "write hole".

Journalling solves this better, and I'm not aware of any real implementation relying on uncorrectable.

Right, thanks! I'm aware of the RAID write hole issue, but I did not consider Write Uncorrectable as a possible means to solve it.