Skip to contentSkip to conversation

support NVMe smart critial warning injection

PATCH Applied zhenwei pi 3 participants 10 messages Jan 15, 2021 – Jan 20, 2021

lore
Share & tools
lore

zhenwei pi

author
#0 lore raw

v4 cover support NVMe smart critial warning injection

v3 -> v4:
- Drop "Fix overwritten bar.cap". (Already fixed)

- Avoid to enqueue the duplicate event.

- Several minor changes for coding style & function/variable name.

v2 -> v3:
- Introduce "Persistent Memory Region has become read-only or unreliable"

- Fix overwritten bar.cap

- Check smart critical warning value from QOM.

- Trigger asynchronous event during smart warning injection.

v1 -> v2:
- Suggested by Philippe & Klaus, set/get smart_critical_warning by QMP.

v1:
- Add smart_critical_warning for nvme device which can be set by QEMU command line to emulate hardware error.

Zhenwei Pi (3):
block/nvme: introduce bit 5 for critical warning hw/block/nvme: add smart_critical_warning property hw/blocl/nvme: trigger async event during injecting smart warning

 hw/block/nvme.c      | 91 +++++++++++++++++++++++++++++++++++++++-----
 hw/block/nvme.h      |  1 +
 include/block/nvme.h |  3 ++
 3 files changed, 86 insertions(+), 9 deletions(-)
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v4 1/3 block/nvme: introduce bit 5 for critical warning

According to NVMe spec 1.4 section
<SMART / Health Information (Log Identifier 02h)>, introduce bit 5 for "Persistent Memory Region has become read-only or unreliable".

---
 include/block/nvme.h | 1 +
 1 file changed, 1 insertion(+)
include/block/nvme.h +1 -0
index 9494246f1f..0baf4b27cd 100644
@@ -789,6 +789,7 @@ enum NvmeSmartWarn {
     NVME_SMART_RELIABILITY            = 1 << 2,
     NVME_SMART_MEDIA_READ_ONLY        = 1 << 3,
     NVME_SMART_FAILED_VOLATILE_MEDIA  = 1 << 4,
+    NVME_SMART_PMR_UNRELIABLE         = 1 << 5,
 };
 
 typedef struct NvmeEffectsLog {
Signature · 1 line
2.25.1
Reviewed-by on v4 1/3
#2 lore raw

Re: [PATCH v4 1/3] block/nvme: introduce bit 5 for critical warning

Quoted 1 line · zhenwei pi

According to NVMe spec 1.4 section

"According to NVMe spec 1.4 section 5.14.1.2"

Quoted 7 lines

<SMART / Health Information (Log Identifier 02h)>, introduce bit 5 for "Persistent Memory Region has become read-only or unreliable".

---
 include/block/nvme.h | 1 +
 1 file changed, 1 insertion(+)
author
#3 lore raw

PATCH v4 2/3 hw/block/nvme: add smart_critical_warning property

In reply to zhenwei pi

There is a very low probability that hitting physical NVMe disk hardware critical warning case, it's hard to write & test a monitor agent service.

For debugging purposes, add a new 'smart_critical_warning' property to emulate this situation.

The orignal version of this change is implemented by adding a fixed property which could be initialized by QEMU command line. Suggested by Philippe & Klaus, rework like current version.

Test with this patch:
1, change smart_critical_warning property for a running VM:
#virsh qemu-monitor-command nvme-upstream '{ "execute": "qom-set",
"arguments": { "path": "/machine/peripheral-anon/device[0]", "property": "smart_critical_warning", "value":16 } }'
2, run smartctl in guest
#smartctl -H -l error /dev/nvme0n1

=== START OF SMART DATA SECTION ===
SMART overall-health self-assessment test result: FAILED!
- volatile memory backup device has failed

---
 hw/block/nvme.c      | 45 +++++++++++++++++++++++++++++++++++++++++---
 hw/block/nvme.h      |  1 +
 include/block/nvme.h |  1 +
 3 files changed, 44 insertions(+), 3 deletions(-)
hw/block/nvme.c +42 -3
index 0854ee3072..2d71ebeb30 100644
@@ -2425,6 +2425,7 @@ static uint16_t nvme_smart_info(NvmeCtrl *n, uint8_t rae, uint32_t buf_len,
     }
 
     trans_len = MIN(sizeof(smart) - off, buf_len);
+    smart.critical_warning = n->smart_critical_warning;
 
     smart.data_units_read[0] = cpu_to_le64(DIV_ROUND_UP(stats.units_read,
                                                         1000));
@@ -4346,6 +4347,40 @@ static Property nvme_props[] = {
     DEFINE_PROP_END_OF_LIST(),
 };
 
+static void nvme_get_smart_warning(Object *obj, Visitor *v, const char *name,
+                                   void *opaque, Error **errp)
+{
+    NvmeCtrl *n = NVME(obj);
+    uint8_t value = n->smart_critical_warning;
+
+    visit_type_uint8(v, name, &value, errp);
+}
+
+static void nvme_set_smart_warning(Object *obj, Visitor *v, const char *name,
+                                   void *opaque, Error **errp)
+{
+    NvmeCtrl *n = NVME(obj);
+    uint8_t value, cap = 0;
+
+    if (!visit_type_uint8(v, name, &value, errp)) {
+        return;
+    }
+
+    cap = NVME_SMART_SPARE | NVME_SMART_TEMPERATURE | NVME_SMART_RELIABILITY
+          | NVME_SMART_MEDIA_READ_ONLY | NVME_SMART_FAILED_VOLATILE_MEDIA;
+    if (NVME_CAP_PMR(n->bar.cap)) {
+        cap |= NVME_SMART_PMR_UNRELIABLE;
+    }
+
+    if ((value & cap) != value) {
+        error_setg(errp, "unsupported smart critical warning bits: 0x%x",
+                   value & ~cap);
+        return;
+    }
+
+    n->smart_critical_warning = value;
+}
+
 static const VMStateDescription nvme_vmstate = {
     .name = "nvme",
     .unmigratable = 1,
@@ -4369,13 +4404,17 @@ static void nvme_class_init(ObjectClass *oc, void *data)
 
 static void nvme_instance_init(Object *obj)
 {
-    NvmeCtrl *s = NVME(obj);
+    NvmeCtrl *n = NVME(obj);
 
-    if (s->namespace.blkconf.blk) {
-        device_add_bootindex_property(obj, &s->namespace.blkconf.bootindex,
+    if (n->namespace.blkconf.blk) {
+        device_add_bootindex_property(obj, &n->namespace.blkconf.bootindex,
                                       "bootindex", "/namespace@1,0",
                                       DEVICE(obj));
     }
+
+    object_property_add(obj, "smart_critical_warning", "uint8",
+                        nvme_get_smart_warning,
+                        nvme_set_smart_warning, NULL, NULL);
 }
 
 static const TypeInfo nvme_info = {
hw/block/nvme.h +1 -0
index b7fbcca39d..bd5a2b416f 100644
@@ -145,6 +145,7 @@ typedef struct NvmeCtrl {
     uint64_t    timestamp_set_qemu_clock_ms;    /* QEMU clock time */
     uint64_t    starttime_ms;
     uint16_t    temperature;
+    uint8_t     smart_critical_warning;
 
     HostMemoryBackend *pmrdev;
 
include/block/nvme.h +1 -0
index 0baf4b27cd..520bc2e4cf 100644
@@ -60,6 +60,7 @@ enum NvmeCapMask {
 #define NVME_CAP_CSS(cap)   (((cap) >> CAP_CSS_SHIFT)    & CAP_CSS_MASK)
 #define NVME_CAP_MPSMIN(cap)(((cap) >> CAP_MPSMIN_SHIFT) & CAP_MPSMIN_MASK)
 #define NVME_CAP_MPSMAX(cap)(((cap) >> CAP_MPSMAX_SHIFT) & CAP_MPSMAX_MASK)
+#define NVME_CAP_PMR(cap)   (((cap) >> CAP_PMR_SHIFT)    & CAP_PMR_MASK)
 
 #define NVME_CAP_SET_MQES(cap, val)   (cap |= (uint64_t)(val & CAP_MQES_MASK)  \
                                                            << CAP_MQES_SHIFT)
Signature · 1 line
2.25.1
author
#4 lore raw

PATCH v4 3/3 hw/blocl/nvme: trigger async event during injecting smart warning

In reply to zhenwei pi

During smart critical warning injection by setting property from QMP command, also try to trigger asynchronous event.

Suggested by Keith, if a event has already been raised, there is no need to enqueue the duplicate event any more.

---
 hw/block/nvme.c      | 48 +++++++++++++++++++++++++++++++++++++-------
 include/block/nvme.h |  1 +
 2 files changed, 42 insertions(+), 7 deletions(-)
hw/block/nvme.c +41 -7
index 2d71ebeb30..f72a30b499 100644
@@ -980,6 +980,35 @@ static void nvme_enqueue_event(NvmeCtrl *n, uint8_t event_type,
     nvme_process_aers(n);
 }
 
+static void nvme_smart_event(NvmeCtrl *n, uint8_t event)
+{
+    uint8_t aer_info;
+
+    /* Ref SPEC <Asynchronous Event Information 0x2013 SMART / Health Status> */
+    if (!(NVME_AEC_SMART(n->features.async_config) & event)) {
+        return;
+    }
+
+    switch (event) {
+    case NVME_SMART_SPARE:
+        aer_info = NVME_AER_INFO_SMART_SPARE_THRESH;
+        break;
+    case NVME_SMART_TEMPERATURE:
+        aer_info = NVME_AER_INFO_SMART_TEMP_THRESH;
+        break;
+    case NVME_SMART_RELIABILITY:
+    case NVME_SMART_MEDIA_READ_ONLY:
+    case NVME_SMART_FAILED_VOLATILE_MEDIA:
+    case NVME_SMART_PMR_UNRELIABLE:
+        aer_info = NVME_AER_INFO_SMART_RELIABILITY;
+        break;
+    default:
+        return;
+    }
+
+    nvme_enqueue_event(n, NVME_AER_TYPE_SMART, aer_info, NVME_LOG_SMART_INFO);
+}
+
 static void nvme_clear_events(NvmeCtrl *n, uint8_t event_type)
 {
     n->aer_mask &= ~(1 << event_type);
@@ -3238,12 +3267,9 @@ static uint16_t nvme_set_feature(NvmeCtrl *n, NvmeRequest *req)
             return NVME_INVALID_FIELD | NVME_DNR;
         }
 
-        if (((n->temperature >= n->features.temp_thresh_hi) ||
-             (n->temperature <= n->features.temp_thresh_low)) &&
-            NVME_AEC_SMART(n->features.async_config) & NVME_SMART_TEMPERATURE) {
-            nvme_enqueue_event(n, NVME_AER_TYPE_SMART,
-                               NVME_AER_INFO_SMART_TEMP_THRESH,
-                               NVME_LOG_SMART_INFO);
+        if ((n->temperature >= n->features.temp_thresh_hi) ||
+            (n->temperature <= n->features.temp_thresh_low)) {
+            nvme_smart_event(n, NVME_AER_INFO_SMART_TEMP_THRESH);
         }
 
         break;
@@ -4360,7 +4386,7 @@ static void nvme_set_smart_warning(Object *obj, Visitor *v, const char *name,
                                    void *opaque, Error **errp)
 {
     NvmeCtrl *n = NVME(obj);
-    uint8_t value, cap = 0;
+    uint8_t value, old_value, cap = 0, index, event;
 
     if (!visit_type_uint8(v, name, &value, errp)) {
         return;
@@ -4378,7 +4404,15 @@ static void nvme_set_smart_warning(Object *obj, Visitor *v, const char *name,
         return;
     }
 
+    old_value = n->smart_critical_warning;
     n->smart_critical_warning = value;
+
+    /* only inject new bits of smart critical warning */
+    for (index = 0; index < NVME_SMART_WARN_MAX; index++) {
+        event = 1 << index;
+        if (value & ~old_value & event)
+            nvme_smart_event(n, event);
+    }
 }
 
 static const VMStateDescription nvme_vmstate = {
include/block/nvme.h +1 -0
index 520bc2e4cf..0adccaf5b4 100644
@@ -784,6 +784,7 @@ typedef struct QEMU_PACKED NvmeSmartLog {
     uint8_t     reserved2[320];
 } NvmeSmartLog;
 
+#define NVME_SMART_WARN_MAX     6
 enum NvmeSmartWarn {
     NVME_SMART_SPARE                  = 1 << 0,
     NVME_SMART_TEMPERATURE            = 1 << 1,
Signature · 1 line
2.25.1
#5 lore raw

Re: [PATCH v4 3/3] hw/blocl/nvme: trigger async event during injecting smart warning

Quoted 11 lines · zhenwei pi

During smart critical warning injection by setting property from QMP command, also try to trigger asynchronous event.

Suggested by Keith, if a event has already been raised, there is no need to enqueue the duplicate event any more.

---
 hw/block/nvme.c      | 48 +++++++++++++++++++++++++++++++++++++-------
 include/block/nvme.h |  1 +
 2 files changed, 42 insertions(+), 7 deletions(-)

Typo "blocl" in subject ;) No need to repost.

Tested-by Reviewed-by on the v4 series
#6 lore raw
In reply to zhenwei pi
Quoted 29 lines · zhenwei pi

v3 -> v4:
- Drop "Fix overwritten bar.cap". (Already fixed)

- Avoid to enqueue the duplicate event.

- Several minor changes for coding style & function/variable name.

v2 -> v3:
- Introduce "Persistent Memory Region has become read-only or unreliable"

- Fix overwritten bar.cap

- Check smart critical warning value from QOM.

- Trigger asynchronous event during smart warning injection.

v1 -> v2:
- Suggested by Philippe & Klaus, set/get smart_critical_warning by QMP.

v1:
- Add smart_critical_warning for nvme device which can be set by QEMU command line to emulate hardware error.

Zhenwei Pi (3):
block/nvme: introduce bit 5 for critical warning hw/block/nvme: add smart_critical_warning property hw/blocl/nvme: trigger async event during injecting smart warning

Quoted 5 lines
 hw/block/nvme.c      | 91 +++++++++++++++++++++++++++++++++++++++-----
 hw/block/nvme.h      |  1 +
 include/block/nvme.h |  3 ++
 3 files changed, 86 insertions(+), 9 deletions(-)

This looks pretty good to me.

I think maybe we want to handle the duplicate event stuff more generally from the AER/AEN code, but this does the job.

author
#7 lore raw
Quoted 36 lines · Klaus Jensen
Quoted 29 lines · zhenwei pi

v3 -> v4:
- Drop "Fix overwritten bar.cap". (Already fixed)

- Avoid to enqueue the duplicate event.

- Several minor changes for coding style & function/variable name.

v2 -> v3:
- Introduce "Persistent Memory Region has become read-only or unreliable"

- Fix overwritten bar.cap

- Check smart critical warning value from QOM.

- Trigger asynchronous event during smart warning injection.

v1 -> v2:
- Suggested by Philippe & Klaus, set/get smart_critical_warning by QMP.

v1:
- Add smart_critical_warning for nvme device which can be set by QEMU command line to emulate hardware error.

Zhenwei Pi (3):
block/nvme: introduce bit 5 for critical warning hw/block/nvme: add smart_critical_warning property hw/blocl/nvme: trigger async event during injecting smart warning

Quoted 5 lines
  hw/block/nvme.c      | 91 +++++++++++++++++++++++++++++++++++++++-----
  hw/block/nvme.h      |  1 +
  include/block/nvme.h |  3 ++
  3 files changed, 86 insertions(+), 9 deletions(-)
Quoted 8 lines

This looks pretty good to me.

I think maybe we want to handle the duplicate event stuff more generally from the AER/AEN code, but this does the job.

What's the next step I should take? Should I push a new version to implement this purpose? From my understanding, before inserting a new event to aer_queue, I can parse all the pending aer to find the same event.

nvme_enqueue_event()

{
    ...

    QTAILQ_FOREACH_SAFE(event, &n->aer_queue, entry, next) {
        if ((event->result.event_type == event_type)
            && (event->result.event_info == event_info)
            && (event->result.log_page == log_page))
            return;
    }

    QTAILQ_INSERT_TAIL(&n->aer_queue, event, entry); 



    n->aer_queued++;
    ...
}
Signature · 1 line
zhenwei pi
#8 lore raw
Quoted 61 lines · zhenwei pi
Quoted 36 lines · Klaus Jensen
Quoted 29 lines · zhenwei pi

v3 -> v4:
- Drop "Fix overwritten bar.cap". (Already fixed)

- Avoid to enqueue the duplicate event.

- Several minor changes for coding style & function/variable name.

v2 -> v3:
- Introduce "Persistent Memory Region has become read-only or unreliable"

- Fix overwritten bar.cap

- Check smart critical warning value from QOM.

- Trigger asynchronous event during smart warning injection.

v1 -> v2:
- Suggested by Philippe & Klaus, set/get smart_critical_warning by QMP.

v1:
- Add smart_critical_warning for nvme device which can be set by QEMU command line to emulate hardware error.

Zhenwei Pi (3):
block/nvme: introduce bit 5 for critical warning hw/block/nvme: add smart_critical_warning property hw/blocl/nvme: trigger async event during injecting smart warning

Quoted 5 lines
  hw/block/nvme.c      | 91 +++++++++++++++++++++++++++++++++++++++-----
  hw/block/nvme.h      |  1 +
  include/block/nvme.h |  3 ++
  3 files changed, 86 insertions(+), 9 deletions(-)
Quoted 8 lines

This looks pretty good to me.

I think maybe we want to handle the duplicate event stuff more generally from the AER/AEN code, but this does the job.

What's the next step I should take? Should I push a new version to implement this purpose? From my understanding, before inserting a new event to aer_queue, I can parse all the pending aer to find the same event.

nvme_enqueue_event()

{
    ...

    QTAILQ_FOREACH_SAFE(event, &n->aer_queue, entry, next) {
        if ((event->result.event_type == event_type)
            && (event->result.event_info == event_info)
            && (event->result.log_page == log_page))
            return;
    }
Quoted 8 lines
    QTAILQ_INSERT_TAIL(&n->aer_queue, event, entry);



    n->aer_queued++;
    ...
}

No, I'll pick up your series as is, I'll pick it up for nvme-next later today if noone complains! :)

Applied
#9 lore raw
In reply to zhenwei pi
Quoted 29 lines · zhenwei pi

v3 -> v4:
- Drop "Fix overwritten bar.cap". (Already fixed)

- Avoid to enqueue the duplicate event.

- Several minor changes for coding style & function/variable name.

v2 -> v3:
- Introduce "Persistent Memory Region has become read-only or unreliable"

- Fix overwritten bar.cap

- Check smart critical warning value from QOM.

- Trigger asynchronous event during smart warning injection.

v1 -> v2:
- Suggested by Philippe & Klaus, set/get smart_critical_warning by QMP.

v1:
- Add smart_critical_warning for nvme device which can be set by QEMU command line to emulate hardware error.

Zhenwei Pi (3):
block/nvme: introduce bit 5 for critical warning hw/block/nvme: add smart_critical_warning property hw/blocl/nvme: trigger async event during injecting smart warning

Quoted 5 lines
 hw/block/nvme.c      | 91 +++++++++++++++++++++++++++++++++++++++-----
 hw/block/nvme.h      |  1 +
 include/block/nvme.h |  3 ++
 3 files changed, 86 insertions(+), 9 deletions(-)

Thanks! Applied to nvme-next.