support NVMe smart critial warning injection
v3 cover support NVMe smart critial warning injection
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 (4):
block/nvme: introduce bit 5 for critical warning hw/block/nvme: fix overwritten bar.cap
hw/block/nvme: add smart_critical_warning property hw/blocl/nvme: trigger async event during injecting smart warning
hw/block/nvme.c | 86 ++++++++++++++++++++++++++++++++++++++++---- hw/block/nvme.h | 1 + include/block/nvme.h | 1 + 3 files changed, 81 insertions(+), 7 deletions(-)
Signature · 1 line
2.25.1
PATCH v3 1/4 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
@@ -749,6 +749,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, }; enum NvmeLogIdentifier {
Signature · 1 line
2.25.1
PATCH v3 2/4 hw/block/nvme: fix overwritten bar.cap
In reply to zhenwei piAfter PMR initialization, bar.cap should not be clear in function nvme_init_ctrl. Otherwise the PMR cap would be always disabled.
--- hw/block/nvme.c | 1 - 1 file changed, 1 deletion(-)
hw/block/nvme.c +0 -1
@@ -2745,7 +2745,6 @@ static void nvme_init_ctrl(NvmeCtrl *n, PCIDevice *pci_dev) id->psd[0 ].enlat =cpu_to_le32 (0x10 ); id->psd[0 ].exlat =cpu_to_le32 (0x4 ); - n->bar.cap =0 ;NVME_CAP_SET_MQES (n->bar.cap,0x7ff );NVME_CAP_SET_CQR (n->bar.cap,1 );NVME_CAP_SET_TO (n->bar.cap,0xf );
Signature · 1 line
2.25.1
Re: [PATCH v3 2/4] hw/block/nvme: fix overwritten bar.cap
Quoted 24 lines · zhenwei pi
After PMR initialization, bar.cap should not be clear in function
nvme_init_ctrl. Otherwise the PMR cap would be always disabled.Signed-off-by zhenwei pi <[email protected]>--- hw/block/nvme.c | 1 - 1 file changed, 1 deletion(-)hw/block/nvme.c +0
-1@@ -2745,7 +2745,6 @@ static void nvme_init_ctrl(NvmeCtrl *n, PCIDevice *pci_dev) id->psd[0 ].enlat =cpu_to_le32 (0x10 ); id->psd[0 ].exlat =cpu_to_le32 (0x4 ); - n->bar.cap =0 ;NVME_CAP_SET_MQES (n->bar.cap,0x7ff );NVME_CAP_SET_CQR (n->bar.cap,1 );NVME_CAP_SET_TO (n->bar.cap,0xf );Signature · 1 line
2.25.1
Good fix, but looks like you are on master and not on nvme-next[1]? The same fix is already staged.
[1]: http://git.infradead.org/qemu-nvme.git/shortlog/refs/heads/nvme-next
PATCH v3 3/4 hw/block/nvme: add smart_critical_warning property
In reply to zhenwei piThere 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 | 40 ++++++++++++++++++++++++++++++++++++++++ hw/block/nvme.h | 1 + 2 files changed, 41 insertions(+)
hw/block/nvme.c +40 -0
@@ -1214,6 +1214,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 ));
@@ -2826,6 +2827,41 @@ 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 *s =NVME (obj); +uint8_t value = s->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 *s =NVME (obj); +uint8_t value, cap =0 ; +uint64_t pmr_cap =CAP_PMR_MASK ; + +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 (s->bar.cap & (pmr_cap <<CAP_PMR_SHIFT )) { + cap |=NVME_SMART_PMR_UNRELIABLE ; + } + +if ((value & cap) != value) { +error_setg (errp,"unsupported smart critical warning value" ); +return ; + } + + s->smart_critical_warning = value; +} +static const VMStateDescription nvme_vmstate = { .name ="nvme" , .unmigratable =1 ,
@@ -2856,6 +2892,10 @@ static void nvme_instance_init(Object *obj)"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
@@ -139,6 +139,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;
Signature · 1 line
2.25.1
Re: [PATCH v3 3/4] hw/block/nvme: add smart_critical_warning property
Quoted 61 lines · 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, changesmart_critical_warningproperty 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 failedSigned-off-by zhenwei pi <[email protected]>--- hw/block/nvme.c | 40 ++++++++++++++++++++++++++++++++++++++++ hw/block/nvme.h | 1 + 2 files changed, 41 insertions(+)hw/block/nvme.c +17
-0@@ -1214,6 +1214,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 ));@@ -2826,6 +2827,41 @@ 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 *s =NVME (obj); +uint8_t value = s->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 *s =NVME (obj); +uint8_t value, cap =0 ; +uint64_t pmr_cap =CAP_PMR_MASK ;
hw/block/nvme.c static Property nvme_props[] = { · line 2855
Quoted 10 lines
hw/block/nvme.c +10
-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 (s->bar.cap & (pmr_cap <<CAP_PMR_SHIFT )) { + cap |=NVME_SMART_PMR_UNRELIABLE ; + }
Looks like an NVME_CAP_PMRS(cap) macro is missing in include/block/nvme.h. I have added it in another PMR series under review, but you can add it here as well instead of manually doing the shift and check.
Quoted 39 lines
hw/block/nvme.c +13
-0+ +if ((value & cap) != value) { +error_setg (errp,"unsupported smart critical warning value" ); +return ; + } + + s->smart_critical_warning = value; +} +static const VMStateDescription nvme_vmstate = { .name ="nvme" , .unmigratable =1 ,@@ -2856,6 +2892,10 @@ static void nvme_instance_init(Object *obj)"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@@ -139,6 +139,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;Signature · 1 line
2.25.1
Signature · 1 line
One of us - No more doubt, silence or taboo about mental illness.
Re: [PATCH v3 3/4] hw/block/nvme: add smart_critical_warning property
In reply to zhenwei piQuoted 28 lines · 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, changesmart_critical_warningproperty 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 failedSigned-off-by zhenwei pi <[email protected]>--- hw/block/nvme.c | 40 ++++++++++++++++++++++++++++++++++++++++ hw/block/nvme.h | 1 + 2 files changed, 41 insertions(+)
...
Quoted 9 lines
hw/block/nvme.c +9
-0+static void nvme_set_smart_warning (Object *obj,Visitor *v,const char *name, +void *opaque,Error **errp) +{ +NvmeCtrl *s =NVME (obj); +uint8_t value, cap =0 ; +uint64_t pmr_cap =CAP_PMR_MASK ; + +if (!visit_type_uint8 (v, name, &value, errp)) { +return ;
hw/block/nvme.c static Property nvme_props[] = { · line 2858
Quoted 10 lines
hw/block/nvme.c +10
-0+ } + + cap =NVME_SMART_SPARE |NVME_SMART_TEMPERATURE |NVME_SMART_RELIABILITY + |NVME_SMART_MEDIA_READ_ONLY |NVME_SMART_FAILED_VOLATILE_MEDIA ; +if (s->bar.cap & (pmr_cap <<CAP_PMR_SHIFT )) { + cap |=NVME_SMART_PMR_UNRELIABLE ; + } + +if ((value & cap) != value) { +error_setg (errp,"unsupported smart critical warning value" );
More useful:
error_setg(errp,
"unsupported smart critical warning bits: 0x%x",
value & ~cap);Regardless:
Thanks!
Re: [PATCH v3 3/4] hw/block/nvme: add smart_critical_warning property
In reply to zhenwei pihw/block/nvme.c static Property nvme_props[] = { · line 2834
Quoted 4 lines · zhenwei pi
hw/block/nvme.c +4
-0+static void nvme_get_smart_warning (Object *obj,Visitor *v,const char *name, +void *opaque,Error **errp) +{ +NvmeCtrl *s =NVME (obj);
With only one exception, all variables of type 'NvmeCtrl' in this program are called 'n', so let's keep that consistency please. Otherwise, this looks fine.
Quoted 9 lines
hw/block/nvme.c +9
-0+uint8_t value = s->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 *s =NVME (obj);
PATCH v3 4/4 hw/blocl/nvme: trigger async event during injecting smart warning
In reply to zhenwei piDuring smart critical warning injection by setting property from QMP command, also try to trigger asynchronous event.
--- hw/block/nvme.c | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-)
hw/block/nvme.c +40 -7
@@ -847,6 +847,36 @@ static void nvme_enqueue_event(NvmeCtrl *n, uint8_t event_type,nvme_process_aers (n); } +static void nvme_enqueue_smart_event (NvmeCtrl *n,uint8_t event) +{ +uint8_t aer_info; + +if (!(NVME_AEC_SMART (n->features.async_config) & event)) { +return ; + } + +/* Ref SPEC <Asynchronous Event Information – SMART / Health Status> */ +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 : + aer_info =NVME_AER_INFO_SMART_RELIABILITY ; +break ; +case NVME_SMART_PMR_UNRELIABLE : +/* TODO if NVME_SMART_PMR_UNRELIABLE is defined in future */ +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);
@@ -1824,12 +1854,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_enqueue_smart_event (n,NVME_AER_INFO_SMART_TEMP_THRESH ); }break ;
@@ -2841,7 +2868,7 @@ static void nvme_set_smart_warning(Object *obj, Visitor *v, const char *name, void *opaque, Error **errp) { NvmeCtrl *s = NVME(obj); -uint8_t value, cap =0 ; +uint8_t value, cap =0 , event;uint64_t pmr_cap =CAP_PMR_MASK ;if (!visit_type_uint8 (v, name, &value, errp)) {
@@ -2860,6 +2887,12 @@ static void nvme_set_smart_warning(Object *obj, Visitor *v, const char *name, } s->smart_critical_warning = value; + +/* test each bit of uint8_t for smart.critical_warning */ +for (event =0 ; event <8 ; event++) { +if (value & (1 << event)) +nvme_enqueue_smart_event (s,1 << event); + } }static const VMStateDescription nvme_vmstate = {
Signature · 1 line
2.25.1
Re: [PATCH v3 4/4] hw/blocl/nvme: trigger async event during injecting smart warning
Quoted 13 lines · zhenwei pi
During smart critical warning injection by setting property from QMP command, also try to trigger asynchronous event.
Signed-off-by zhenwei pi <[email protected]>--- hw/block/nvme.c | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-)hw/block/nvme.c +0
-0
hw/block/nvme.c static void nvme_enqueue_event(NvmeCtrl *n, uint8_t event_type, · line 850
Quoted 4 lines
hw/block/nvme.c +1
-0@@ -847,6 +847,36 @@ static void nvme_enqueue_event(NvmeCtrl *n, uint8_t event_type,nvme_process_aers (n); } +static void nvme_enqueue_smart_event (NvmeCtrl *n,uint8_t event)
Maybe rename to just nvme_smart_event, since it is conditional if it enqueues anything.
Quoted 12 lines
hw/block/nvme.c +12
-0+{ +uint8_t aer_info; + +if (!(NVME_AEC_SMART (n->features.async_config) & event)) { +return ; + } + +/* Ref SPEC <Asynchronous Event Information ??? SMART / Health Status> */ +switch (event) { +case NVME_SMART_SPARE : + aer_info =NVME_AER_INFO_SMART_SPARE_THRESH ; +break ;
hw/block/nvme.c static void nvme_enqueue_event(NvmeCtrl *n, uint8_t event_type, · line 872
Quoted 10 lines
hw/block/nvme.c +10
-0+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 : + aer_info =NVME_AER_INFO_SMART_RELIABILITY ; +break ; +case NVME_SMART_PMR_UNRELIABLE : +/* TODO if NVME_SMART_PMR_UNRELIABLE is defined in future */
Doesn't NVME_SMART_PMR_UNRELIABLE fall under the NVME_AER_INFO_SMART_RELIABILITY SMART/Health information group? The spec says that the PMR becoming unreliable can cause an AEN, so I think that is the only group that is usable.
Quoted 36 lines
hw/block/nvme.c +11
-7+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);@@ -1824,12 +1854,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_enqueue_smart_event (n,NVME_AER_INFO_SMART_TEMP_THRESH ); }break ;@@ -2841,7 +2868,7 @@ static void nvme_set_smart_warning(Object *obj, Visitor *v, const char *name, void *opaque, Error **errp) { NvmeCtrl *s = NVME(obj); -uint8_t value, cap =0 ; +uint8_t value, cap =0 , event;uint64_t pmr_cap =CAP_PMR_MASK ;if (!visit_type_uint8 (v, name, &value, errp)) {
hw/block/nvme.c static void nvme_set_smart_warning(Object *obj, Visitor *v, const char *name, · line 2895
Quoted 9 lines
hw/block/nvme.c +6
-0@@ -2860,6 +2887,12 @@ static void nvme_set_smart_warning(Object *obj, Visitor *v, const char *name, } s->smart_critical_warning = value; + +/* test each bit of uint8_t for smart.critical_warning */ +for (event =0 ; event <8 ; event++) { +if (value & (1 << event)) +nvme_enqueue_smart_event (s,1 << event); + }
I suggest you add a NVME_SMART_WARN_MAX to the NvmeSmartWarn enum with value '6' and use that instead of the literal '8'.
Quoted 7 lines
hw/block/nvme.c +0
-0}static const VMStateDescription nvme_vmstate = {Signature · 1 line
2.25.1
Signature · 1 line
One of us - No more doubt, silence or taboo about mental illness.
Re: [PATCH v3 4/4] hw/blocl/nvme: trigger async event during injecting smart warning
In reply to zhenwei piQuoted 7 lines · zhenwei pi
During smart critical warning injection by setting property from QMP command, also try to trigger asynchronous event.
Signed-off-by zhenwei pi <[email protected]>--- hw/block/nvme.c | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-)
...
hw/block/nvme.c static void nvme_enqueue_event(NvmeCtrl *n, uint8_t event_type,
Quoted 9 lines
hw/block/nvme.c +9
-0+static void nvme_enqueue_smart_event (NvmeCtrl *n,uint8_t event) +{ +uint8_t aer_info; + +if (!(NVME_AEC_SMART (n->features.async_config) & event)) { +return ; + } + +/* Ref SPEC <Asynchronous Event Information – SMART / Health Status> */
Mojibake UTF-8 encoding problem?
Re: [PATCH v3 4/4] hw/blocl/nvme: trigger async event during injecting smart warning
In reply to zhenwei pihw/block/nvme.c static void nvme_set_smart_warning(Object *obj, Visitor *v, const char *name, · line 2894
Quoted 9 lines · zhenwei pi
hw/block/nvme.c +5
-0@@ -2860,6 +2887,12 @@ static void nvme_set_smart_warning(Object *obj, Visitor *v, const char *name, } s->smart_critical_warning = value; + +/* test each bit of uint8_t for smart.critical_warning */ +for (event =0 ; event <8 ; event++) { +if (value & (1 << event)) +nvme_enqueue_smart_event (s,1 << event);
I think you need to save the events that have already been raised with the host so that you don't send duplicate responses everytime a new event is added to the 'critical_warning'.