nvme: Add sysfs interface for APST configuration management
cover nvme: Add sysfs interface for APST configuration management
From: Yaxiong Tian <[email protected]>
This series enhances NVMe APST (Autonomous Power State Transition) support by:
1. Adding warnings for PST table allocation failures
2. Exposing APST tables via sysfs for runtime inspection
3. Providing per-controller sysfs interface for APST configuration
The changes allow better visibility and control of power management settings through userspace tools while maintaining the existing functionality.
Yaxiong Tian (3):
nvme: Add warning for PST table memory allocation failure in
nvme_configure_apst
nvme: add sysfs interface for APST table updates
nvme: add per-controller sysfs interface for APST configuration
drivers/nvme/host/core.c | 24 ++++++++++------ drivers/nvme/host/nvme.h | 6 ++++ drivers/nvme/host/sysfs.c | 59 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 8 deletions(-)
Signature · 1 line
2.25.1
PATCH 1/3 nvme: Add warning for PST table memory allocation failure in nvme_configure_apst
From: Yaxiong Tian <[email protected]>
Currently the function fails silently on PST table memory allocation failure. Add warning messages to improve error visibility.
--- drivers/nvme/host/core.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/nvme/host/core.c +3 -1
@@ -2678,8 +2678,10 @@ static int nvme_configure_apst(struct nvme_ctrl *ctrl) } table =kzalloc (sizeof (*table),GFP_KERNEL ); -if (!table) +if (!table) { +dev_warn (ctrl->device,"Failed to allocate pst table; not using APST\n" );return 0 ; + }if (!ctrl->apst_enabled || ctrl->ps_max_latency_us ==0 ) {/* Turn off APST. */
Signature · 1 line
2.25.1
PATCH 2/3 nvme: add sysfs interface for APST table updates
In reply to Yaxiong TianFrom: Yaxiong Tian <[email protected]>
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
--- drivers/nvme/host/core.c | 4 +++- drivers/nvme/host/nvme.h | 2 ++ drivers/nvme/host/sysfs.c | 23 +++++++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-)
drivers/nvme/host/core.c +3 -1
@@ -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 ;
@@ -4852,6 +4852,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 );
drivers/nvme/host/nvme.h +2 -0
@@ -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) {
drivers/nvme/host/sysfs.c +23 -0
@@ -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 };
Signature · 1 line
2.25.1
PATCH 3/3 nvme: add per-controller sysfs interface for APST configuration
In reply to Yaxiong TianFrom: Yaxiong Tian <[email protected]>
Currently, APST (Autonomous Power State Transition) parameters are configured as module parameters affecting all controllers uniformly,
This lacks flexibility for heterogeneous systems.
This patch introduces per-controller sysfs attributes under each NVMe controller's sysfs directory:
- apst_primary_timeout_ms
- apst_secondary_timeout_ms
- apst_primary_latency_tol_us
- apst_secondary_latency_tol_us
The attributes allow runtime configuration of:
1. Timeout values for primary/secondary states
2. Latency tolerance requirements
The existing module parameters are retained for backward compatibility but now only serve as default values for new controllers.
--- drivers/nvme/host/core.c | 16 ++++++++++------ drivers/nvme/host/nvme.h | 4 ++++ drivers/nvme/host/sysfs.c | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 6 deletions(-)
drivers/nvme/host/core.c +10 -6
@@ -2608,22 +2608,22 @@ static int nvme_configure_host_options(struct nvme_ctrl *ctrl)* timeout value is returned and the matching tolerance index (1 or 2) is * reported. */ -static bool nvme_apst_get_transition_time (u64 total_latency, +static bool nvme_apst_get_transition_time (struct nvme_ctrl *ctrl,u64 total_latency,u64 *transition_time,unsigned *last_index) { -if (total_latency <= apst_primary_latency_tol_us) { +if (total_latency <= ctrl->apst_primary_latency_tol_us) {if (*last_index ==1 )return false; *last_index =1 ; - *transition_time = apst_primary_timeout_ms; + *transition_time = ctrl->apst_primary_timeout_ms;return true; }if (apst_secondary_timeout_ms && - total_latency <= apst_secondary_latency_tol_us) { + total_latency <= ctrl->apst_secondary_latency_tol_us) {if (*last_index <=2 )return false; *last_index =2 ; - *transition_time = apst_secondary_timeout_ms; + *transition_time = ctrl->apst_secondary_timeout_ms;return true; }return false;
@@ -2728,7 +2728,7 @@ int nvme_configure_apst(struct nvme_ctrl *ctrl)* for higher power states. */ if (apst_primary_timeout_ms && apst_primary_latency_tol_us) { -if (!nvme_apst_get_transition_time (total_latency_us, +if (!nvme_apst_get_transition_time (ctrl, total_latency_us, &transition_ms, &last_lt_index))continue ; }else {
@@ -4853,6 +4853,10 @@ int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev, ctrl->ka_last_check_time = jiffies;mutex_init (&ctrl->apst_lock); + ctrl->apst_primary_timeout_ms = apst_primary_timeout_ms; + ctrl->apst_secondary_timeout_ms = apst_secondary_timeout_ms; + ctrl->apst_primary_latency_tol_us = apst_primary_latency_tol_us; + ctrl->apst_secondary_latency_tol_us = apst_secondary_latency_tol_us;BUILD_BUG_ON (NVME_DSM_MAX_RANGES *sizeof (struct nvme_dsm_range ) >PAGE_SIZE );
drivers/nvme/host/nvme.h +4 -0
@@ -387,6 +387,10 @@ struct nvme_ctrl {/* Power saving configuration */ struct mutex apst_lock;u64 ps_max_latency_us; +u64 apst_primary_timeout_ms; +u64 apst_secondary_timeout_ms; +u64 apst_primary_latency_tol_us; +u64 apst_secondary_latency_tol_us;bool apst_enabled;/* PCIe only: */
drivers/nvme/host/sysfs.c +36 -0
@@ -684,6 +684,37 @@ static DEVICE_ATTR(dhchap_ctrl_secret, S_IRUGO | S_IWUSR, nvme_ctrl_dhchap_ctrl_secret_show, nvme_ctrl_dhchap_ctrl_secret_store);#endif +#define nvme_apst_show_and_store_function (field) \ +static ssize_t field##_show(struct device *dev, \ + struct device_attribute *attr, char *buf) \ +{ \ + struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \ + return sysfs_emit(buf, "%llu\n", ctrl->field); \ +} \ + \ +static ssize_t field##_store(struct device *dev, \ + struct device_attribute *attr, \ + const char *buf, size_t size) \ +{ \ + int err; \ + u64 data = 0; \ + struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \ + err = kstrtou64(buf, 0, &data); \ + if (err < 0) \ + return err; \ + \ + mutex_lock(&ctrl->apst_lock); \ + ctrl->field = data; \ + mutex_unlock(&ctrl->apst_lock); \ + return size; \ +} \ +static DEVICE_ATTR_RW(field); + +nvme_apst_show_and_store_function (apst_primary_timeout_ms); +nvme_apst_show_and_store_function (apst_secondary_timeout_ms); +nvme_apst_show_and_store_function (apst_primary_latency_tol_us); +nvme_apst_show_and_store_function (apst_secondary_latency_tol_us); +static ssize_t apst_update_store (struct device *dev,struct device_attribute *attr,const char *buf,size_t size)
@@ -734,6 +765,11 @@ static struct attribute *nvme_dev_attrs[] = { &dev_attr_dhchap_ctrl_secret.attr, #endif &dev_attr_adm_passthru_err_log_enabled.attr, + + &dev_attr_apst_primary_timeout_ms.attr, + &dev_attr_apst_secondary_timeout_ms.attr, + &dev_attr_apst_primary_latency_tol_us.attr, + &dev_attr_apst_secondary_latency_tol_us.attr, &dev_attr_apst_update.attr,NULL };
Signature · 1 line
2.25.1