nvme: make SMART / Health Status AENs reach hwmon alarms
Subsystems (2)
v3 cover nvme: make SMART / Health Status AENs reach hwmon alarms
The NVMe hwmon support exposes the temperature alarm as a plain sysfs attribute, so monitoring tools have to poll temp1_alarm to learn about a critical temperature condition. The device does send a SMART / Health Status asynchronous event when a critical warning is raised, but the host never enables it in the Asynchronous Event Configuration, and the completion path silently drops the event.
The temperature warning is subscribed unconditionally when hwmon is present (it has no OAES capability bit), a work item embedded in nvme_ctrl re-reads the SMART log to clear the latched event -- the same shape as the namespace scan reading the changed namespace list -- and the alarm is announced with hwmon_notify_event(), which also feeds the registered thermal zone.
Testing: verified end to end against QEMU's nvme emulation: injecting smart_critical_warning over QMP delivers the AEN, poll(2) on temp1_alarm wakes immediately with EPOLLERR|EPOLLPRI and reads alarm=1; clearing the warning produces no event and no spurious wake; re-injecting the bit wakes again; enabling the spare-block warning through an admin passthrough delivers the AEN without waking the temperature alarm; a controller modeled to reject the critical warning subscription with Invalid Field in Command falls back to the notice-only mask, which a Get Features of the Asynchronous Event Configuration confirms.
Changes since v2:
- exclude transports that do not implement asynchronous event requests from the subscription: apple-nvme reports no OAES support but registers hwmon, which would have queued the async event work and dereferenced a NULL submit_async_event callback (sashiko)
- embed the SMART work in nvme_ctrl and only queue it while the controller is live, so that a work item left over from before a reset does not issue a blocking admin command against a quiesced queue, and the work memory is tied to the controller lifetime (sashiko)
- only notify temp1_alarm when the re-read SMART log has the temperature critical warning set, so non-temperature events enabled through passthrough do not produce false alarms (sashiko)
- fall back to the notice-only Asynchronous Event Configuration when the controller rejects the critical warning subscription with Invalid Field in Command -- the SHCW bits have no capability field to probe, so the subscription itself is the only probe -- and return early when the reduced mask is empty, so the notice events are not lost with the subscription (sashiko)
- drain the SMART work before freeing its data during controller teardown, so that a late asynchronous event completion cannot dereference freed memory (sashiko)
Changes since v1:
- drop the uapi patch defining an OAES bit for the SMART event: OAES bits 7:0 are reserved; the warning is configured through the SMART / Health Critical Warnings field of the Asynchronous Event Configuration feature (sashiko)
- subscribe to the temperature warning unconditionally when hwmon is present, instead of masking against ctrl->oaes (sashiko)
- re-read the SMART log in the deferred work to clear the latched event, like the namespace scan does for the changed namespace list (sashiko)
- call hwmon_notify_event() instead of sysfs_notify(), so the thermal zone registered with HWMON_C_REGISTER_TZ is fed as well (sashiko)
- correct the cover letter: temp1_alarm reflects the temperature warning only, not the spare-block condition
Guixin Liu (1):
nvme-hwmon: notify hwmon alarms on the SMART / Health Status AEN
drivers/nvme/host/core.c | 26 +++++++++++++++++++++++++- drivers/nvme/host/hwmon.c | 32 ++++++++++++++++++++++++++++ drivers/nvme/host/nvme.h | 16 +++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-)
--
2.43.7
PATCH v3 1/1 nvme-hwmon: notify hwmon alarms on the SMART / Health Status AEN
The NVMe hwmon support exposes the temperature alarm as a plain sysfs attribute, so monitoring tools have to poll temp1_alarm to learn about a critical temperature condition. The device does send a SMART / Health Status asynchronous event when a critical warning is raised, but the host never enables it in the Asynchronous Event Configuration, and the completion path silently drops the event.
The temperature warning is configured through the SMART / Health Critical Warnings field of the Asynchronous Event Configuration, which mirrors the critical_warning byte of the SMART log and has no OAES capability bit, so the subscription does not go through ctrl->oaes. A rejection with Invalid Field in Command falls back to the notice-only mask, so the notice events are not lost with it.
On the event, a work item embedded in nvme_ctrl re-reads the SMART log to clear the latched event and notifies temp1_alarm through hwmon_notify_event(), which also feeds the registered thermal zone. The work is only queued while the controller is live and is drained before its data is freed. The nvme-pci transport completes asynchronous events from interrupt context, so the completion path only queues the work.
--- drivers/nvme/host/core.c | 26 +++++++++++++++++++++++++- drivers/nvme/host/hwmon.c | 32 ++++++++++++++++++++++++++++++++ drivers/nvme/host/nvme.h | 16 ++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-)
drivers/nvme/host/core.c +25 -1
@@ -1792,11 +1792,29 @@ static void nvme_enable_aen(struct nvme_ctrl *ctrl)u32 result, supported_aens = ctrl->oaes &NVME_AEN_SUPPORTED ;int status; +if (nvme_hwmon_present (ctrl) && ctrl->ops->submit_async_event) + supported_aens |=NVME_SMART_CRIT_TEMPERATURE ; +if (!supported_aens)return ; status =nvme_set_features (ctrl,NVME_FEAT_ASYNC_EVENT , supported_aens,NULL ,0 , &result); +/* +* The SHCW bits have no capability field, so a controller +* reports the lack of support as Invalid Field in Command. +* Retry with the notice-only mask, so the notice events are +* not lost with the subscription; other errors do not +* indicate an unsupported mask. +*/ +if ((status &NVME_SCT_SC_MASK ) ==NVME_SC_INVALID_FIELD && + (supported_aens &NVME_SMART_CRIT_TEMPERATURE )) { + supported_aens &= ~NVME_SMART_CRIT_TEMPERATURE ; +if (!supported_aens) +return ; + status =nvme_set_features (ctrl,NVME_FEAT_ASYNC_EVENT , + supported_aens,NULL ,0 , &result); + }if (status)dev_warn (ctrl->device,"Failed to configure AEN (cfg %x)\n" , supported_aens);
@@ -5009,8 +5027,14 @@ void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,nvme_handle_aer_persistent_error (ctrl);return ; } - fallthrough; + ctrl->aen_result = result; +break ;case NVME_AER_SMART : + ctrl->aen_result = result; +if (nvme_hwmon_present (ctrl) && +nvme_ctrl_state (ctrl) ==NVME_CTRL_LIVE ) +nvme_hwmon_smart_event (ctrl); +break ;case NVME_AER_CSS :case NVME_AER_VS : ctrl->aen_result = result;
drivers/nvme/host/hwmon.c +32 -0
@@ -223,6 +223,36 @@ static const struct hwmon_chip_info nvme_hwmon_chip_info = { .info = nvme_hwmon_info, }; +static void nvme_hwmon_smart_notify (struct work_struct *work) +{ +struct nvme_ctrl *ctrl = + container_of(work, struct nvme_ctrl, smart_work); +struct nvme_hwmon_data *data; +struct device *hwmon; +bool warn = false; + +if (nvme_ctrl_state (ctrl) !=NVME_CTRL_LIVE ) +return ; + + hwmon = ctrl->hwmon_device; +if (!hwmon) +return ; + data =dev_get_drvdata (hwmon); + +mutex_lock (&data->read_lock); +if (!nvme_hwmon_get_smart_log (data)) + warn = data->log->critical_warning &NVME_SMART_CRIT_TEMPERATURE ; +mutex_unlock (&data->read_lock); + +if (warn) +hwmon_notify_event (hwmon, hwmon_temp, hwmon_temp_alarm,0 ); +} + +void nvme_hwmon_smart_event (struct nvme_ctrl *ctrl) +{ +queue_work (nvme_wq, &ctrl->smart_work); +} +int nvme_hwmon_init (struct nvme_ctrl *ctrl) {struct device *dev = ctrl->device;
@@ -242,6 +272,7 @@ int nvme_hwmon_init(struct nvme_ctrl *ctrl) data->ctrl = ctrl;mutex_init (&data->read_lock); +INIT_WORK (&ctrl->smart_work, nvme_hwmon_smart_notify); err =nvme_hwmon_get_smart_log (data);if (err) {
@@ -273,6 +304,7 @@ void nvme_hwmon_exit(struct nvme_ctrl *ctrl)struct nvme_hwmon_data *data =dev_get_drvdata (ctrl->hwmon_device); +cancel_work_sync (&ctrl->smart_work);hwmon_device_unregister (ctrl->hwmon_device); ctrl->hwmon_device =NULL ;kfree (data->log);
drivers/nvme/host/nvme.h +16 -0
@@ -379,6 +379,7 @@ struct nvme_ctrl {struct device *device;/* char device */ #ifdef CONFIG_NVME_HWMON struct device *hwmon_device; +struct work_struct smart_work;#endif struct cdev cdev;struct work_struct reset_work;
@@ -1276,6 +1277,12 @@ static inline struct nvme_ns *nvme_get_ns_from_dev(struct device *dev)#ifdef CONFIG_NVME_HWMON int nvme_hwmon_init (struct nvme_ctrl *ctrl);void nvme_hwmon_exit (struct nvme_ctrl *ctrl); +void nvme_hwmon_smart_event (struct nvme_ctrl *ctrl); + +static inline bool nvme_hwmon_present (struct nvme_ctrl *ctrl) +{ +return ctrl->hwmon_device !=NULL ; +}#else static inline int nvme_hwmon_init (struct nvme_ctrl *ctrl) {
@@ -1285,6 +1292,15 @@ static inline int nvme_hwmon_init(struct nvme_ctrl *ctrl)static inline void nvme_hwmon_exit (struct nvme_ctrl *ctrl) { } + +static inline void nvme_hwmon_smart_event (struct nvme_ctrl *ctrl) +{ +} + +static inline bool nvme_hwmon_present (struct nvme_ctrl *ctrl) +{ +return false; +}#endif static inline void nvme_start_request (struct request *rq)
Signature · 1 line
2.43.7
Re: [PATCH v3 1/1] nvme-hwmon: notify hwmon alarms on the SMART / Health Status AEN
Quoted 15 lines · Guixin Liu
drivers/nvme/host/hwmon.c +15
-0+static void nvme_hwmon_smart_notify (struct work_struct *work) +{ +struct nvme_ctrl *ctrl = + container_of(work, struct nvme_ctrl, smart_work); +struct nvme_hwmon_data *data; +struct device *hwmon; +bool warn = false; + +if (nvme_ctrl_state (ctrl) !=NVME_CTRL_LIVE ) +return ; + + hwmon = ctrl->hwmon_device; +if (!hwmon) +return ; + data =dev_get_drvdata (hwmon);
drivers/nvme/host/hwmon.c static const struct hwmon_chip_info nvme_hwmon_chip_info = { · line 250
Quoted 10 lines
drivers/nvme/host/hwmon.c +10
-0+ +mutex_lock (&data->read_lock); +if (!nvme_hwmon_get_smart_log (data)) + warn = data->log->critical_warning &NVME_SMART_CRIT_TEMPERATURE ; +mutex_unlock (&data->read_lock); + +if (warn) +hwmon_notify_event (hwmon, hwmon_temp, hwmon_temp_alarm,0 ); +} +
We only generate the notification if the NVME_SMART_CRIT_TEMPERATURE bit is set in the SMART/Health log page. However, consider the following scenario:
the host receives a SMART/Health asynchronous event and schedules smart_work, but by the time the work executes and reads the SMART/Health log page, the temperature has already returned to the normal range.
In that case, the returned log page may no longer have the NVME_SMART_CRIT_TEMPERATURE bit set, causing us to skip the notification even though a temperature-threshold event was reported.
Perhaps we should also examine aer_subtype and, when it indicates a temperature- threshold event, generate the hwmon notification regardless of whether the corresponding temperature bit is currently set in the SMART/Health log page.
Other than this, the rest of the changes look good to me.
Thanks,
--Nilay