Linux-NVME Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Guixin Liu <kanie@linux.alibaba.com>
To: Keith Busch <kbusch@kernel.org>, Jens Axboe <axboe@kernel.dk>,
	Christoph Hellwig <hch@lst.de>, Sagi Grimberg <sagi@grimberg.me>,
	Nilay Shroff <nilay@linux.ibm.com>,
	Daniel Wagner <dwagner@suse.de>, Hannes Reinecke <hare@suse.de>
Cc: linux-nvme@lists.infradead.org
Subject: [PATCH v3 1/1] nvme-hwmon: notify hwmon alarms on the SMART / Health Status AEN
Date: Mon, 23 Nov 2026 19:23:42 +0800	[thread overview]
Message-ID: <20261123112342.2761131-2-kanie@linux.alibaba.com> (raw)
In-Reply-To: <20261123112342.2761131-1-kanie@linux.alibaba.com>

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.

Signed-off-by: Guixin Liu <kanie@linux.alibaba.com>
---
 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(-)

diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c
index cd0df9b038063..d5e4cc107f36c 100644
--- a/drivers/nvme/host/core.c
+++ b/drivers/nvme/host/core.c
@@ -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;
diff --git a/drivers/nvme/host/hwmon.c b/drivers/nvme/host/hwmon.c
index 406203d8b9adb..07c26c78596e0 100644
--- a/drivers/nvme/host/hwmon.c
+++ b/drivers/nvme/host/hwmon.c
@@ -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);
diff --git a/drivers/nvme/host/nvme.h b/drivers/nvme/host/nvme.h
index 2543e52d6358a..f22197347f099 100644
--- a/drivers/nvme/host/nvme.h
+++ b/drivers/nvme/host/nvme.h
@@ -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)
-- 
2.43.7



  reply	other threads:[~2026-11-23 11:24 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 11:23 [PATCH v3 0/1] nvme: make SMART / Health Status AENs reach hwmon alarms Guixin Liu
2026-11-23 11:23 ` Guixin Liu [this message]
2026-11-24  8:08   ` [PATCH v3 1/1] nvme-hwmon: notify hwmon alarms on the SMART / Health Status AEN Nilay Shroff
2026-11-24 11:23     ` Guixin Liu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261123112342.2761131-2-kanie@linux.alibaba.com \
    --to=kanie@linux.alibaba.com \
    --cc=axboe@kernel.dk \
    --cc=dwagner@suse.de \
    --cc=hare@suse.de \
    --cc=hch@lst.de \
    --cc=kbusch@kernel.org \
    --cc=linux-nvme@lists.infradead.org \
    --cc=nilay@linux.ibm.com \
    --cc=sagi@grimberg.me \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox