[PATCH v2 2/3] nvme: add sysfs interface for APST table updates
From: Yaxiong Tian
Date: Thu Mar 27 2025 - 21:45:46 EST
From: Yaxiong Tian <tianyaxiong@xxxxxxxxxx>
Currently, the APST (Autonomous Power State Transition) table can only be
updated during module initialization via module parameters or indirectly
by setting QoS latency requirements. This patch adds a direct sysfs
interface to allow dynamic updates to the APST table at runtime.
The new sysfs entry is created at:
/sys/class/nvme/<controller>/apst_update
This provides more flexibility in power management tuning without
requiring module reload or QoS latency changes.
Example usage:
update nvme module parameters.
echo 1 > /sys/class/nvme/nvme0/apst_update
Signed-off-by: Yaxiong Tian <tianyaxiong@xxxxxxxxxx>
---
drivers/nvme/host/core.c | 9 +++++++--
drivers/nvme/host/nvme.h | 2 ++
drivers/nvme/host/sysfs.c | 23 +++++++++++++++++++++++
3 files changed, 32 insertions(+), 2 deletions(-)
diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c
index fb0404fee551..9dea1046b8b4 100644
--- a/drivers/nvme/host/core.c
+++ b/drivers/nvme/host/core.c
@@ -2654,7 +2654,7 @@ static bool nvme_apst_get_transition_time(u64 total_latency,
*
* Users can set ps_max_latency_us to zero to turn off APST.
*/
-static int nvme_configure_apst(struct nvme_ctrl *ctrl)
+int nvme_configure_apst(struct nvme_ctrl *ctrl)
{
struct nvme_feat_auto_pst *table;
unsigned apste = 0;
@@ -2778,8 +2778,11 @@ static void nvme_set_latency_tolerance(struct device *dev, s32 val)
if (ctrl->ps_max_latency_us != latency) {
ctrl->ps_max_latency_us = latency;
- if (nvme_ctrl_state(ctrl) == NVME_CTRL_LIVE)
+ if (nvme_ctrl_state(ctrl) == NVME_CTRL_LIVE) {
+ mutex_lock(&ctrl->apst_lock);
nvme_configure_apst(ctrl);
+ mutex_unlock(&ctrl->apst_lock);
+ }
}
}
@@ -4852,6 +4855,8 @@ int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
ctrl->ka_cmd.common.opcode = nvme_admin_keep_alive;
ctrl->ka_last_check_time = jiffies;
+ mutex_init(&ctrl->apst_lock);
+
BUILD_BUG_ON(NVME_DSM_MAX_RANGES * sizeof(struct nvme_dsm_range) >
PAGE_SIZE);
ctrl->discard_page = alloc_page(GFP_KERNEL);
diff --git a/drivers/nvme/host/nvme.h b/drivers/nvme/host/nvme.h
index 51e078642127..7f8e10f5bf7a 100644
--- a/drivers/nvme/host/nvme.h
+++ b/drivers/nvme/host/nvme.h
@@ -385,6 +385,7 @@ struct nvme_ctrl {
key_serial_t tls_pskid;
/* Power saving configuration */
+ struct mutex apst_lock;
u64 ps_max_latency_us;
bool apst_enabled;
@@ -828,6 +829,7 @@ void nvme_unfreeze(struct nvme_ctrl *ctrl);
void nvme_wait_freeze(struct nvme_ctrl *ctrl);
int nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout);
void nvme_start_freeze(struct nvme_ctrl *ctrl);
+int nvme_configure_apst(struct nvme_ctrl *ctrl);
static inline enum req_op nvme_req_op(struct nvme_command *cmd)
{
diff --git a/drivers/nvme/host/sysfs.c b/drivers/nvme/host/sysfs.c
index 6d31226f7a4f..5003cb294d65 100644
--- a/drivers/nvme/host/sysfs.c
+++ b/drivers/nvme/host/sysfs.c
@@ -684,6 +684,28 @@ static DEVICE_ATTR(dhchap_ctrl_secret, S_IRUGO | S_IWUSR,
nvme_ctrl_dhchap_ctrl_secret_show, nvme_ctrl_dhchap_ctrl_secret_store);
#endif
+static ssize_t apst_update_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t size)
+{
+ int err;
+ bool bool_data = false;
+ struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
+ err = kstrtobool(buf, &bool_data);
+
+ if (err)
+ return err;
+
+ if (bool_data && nvme_ctrl_state(ctrl) == NVME_CTRL_LIVE) {
+ mutex_lock(&ctrl->apst_lock);
+ nvme_configure_apst(ctrl);
+ mutex_unlock(&ctrl->apst_lock);
+ }
+
+ return size;
+}
+static DEVICE_ATTR_WO(apst_update);
+
static struct attribute *nvme_dev_attrs[] = {
&dev_attr_reset_controller.attr,
&dev_attr_rescan_controller.attr,
@@ -712,6 +734,7 @@ static struct attribute *nvme_dev_attrs[] = {
&dev_attr_dhchap_ctrl_secret.attr,
#endif
&dev_attr_adm_passthru_err_log_enabled.attr,
+ &dev_attr_apst_update.attr,
NULL
};
--
2.25.1