ufs: sysfs: fix current_power_mode read without SSU
Subsystems (2)
PATCH v3 1/1 ufs: sysfs: fix current_power_mode read without SSU
current_power_mode was generated by UFS_ATTRIBUTE(), which calls ufshcd_rpm_get_sync() before issuing the query. That resumes the UFS device WLUN, and ufshcd_wl_runtime_resume() sends START STOP UNIT to bring the device back to Active. As a result bCurrentPowerMode always read back as 0x11 (Active) no matter what state the device was actually
in: the act of reading the attribute woke the device, so Sleep could never be observed through sysfs.
Per the UFS spec, bCurrentPowerMode is the one attribute the device must answer in any power mode, so there is no need to wake it. Give current_power_mode its own show function that deliberately does not resume the WLUN. It only resumes the host controller then reads the attribute over whatever link state the WLUN suspend left behind:
- link active: query directly.
- link in Hibern8: exit Hibern8, query, then re-enter Hibern8 so the link is left exactly as rpm_lvl requested.
- device powered down or link off (rpm_lvl 4/5/6): fail, the device cannot answer without a full resume.
Tested on hardware: with the device runtime-suspended (wb_on=0, so the device actually reaches Sleep instead of being held Active for a WB flush), reading current_power_mode used to return 0x11; after this change it returns 0x22 (Sleep), matching the device's real state.
---
Change-Logs in V3:
- Synchonize the race between WLUN suspend/resume and other contexts that change the link power state.
- Restore UFS link if exiting Hibern8 for the query, then re-enter Hibern8 so the link is left exactly as rpm_lvl requested.
- Correct the commit author name and mail
--- drivers/ufs/core/ufs-sysfs.c | 74 +++++++++++++++++++++++++++++++++- drivers/ufs/core/ufshcd-priv.h | 11 +++++ drivers/ufs/core/ufshcd.c | 12 +++--- include/ufs/ufshcd.h | 3 ++ 4 files changed, 94 insertions(+), 6 deletions(-)
drivers/ufs/core/ufs-sysfs.c +73 -1
@@ -1786,7 +1786,79 @@ out: \static DEVICE_ATTR_RO (_name)UFS_ATTRIBUTE (boot_lun_enabled, _BOOT_LU_EN); -UFS_ATTRIBUTE (current_power_mode, _POWER_MODE); + +/* +* Per UFS spec, bCurrentPowerMode is the only attribute the device must +* respond to in any power mode. Read it without waking the device to +* Active. +*/ +static ssize_t current_power_mode_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct ufs_hba *hba =dev_get_drvdata (dev); +bool exited_h8 = false; +u32 value; +int ret =0 ; + +down (&hba->host_sem); +if (!ufshcd_is_user_access_allowed (hba)) { + ret = -EBUSY ; + goto out_unlock; + } + +pm_runtime_get_sync (hba->dev); + +ufshcd_hold (hba); + scoped_guard(ufshcd_wl_pm, hba) { +if (ufshcd_is_link_off (hba) ||ufshcd_is_ufs_dev_poweroff (hba)) { +/* +* Device is powered down (rpm_lvl 4/5) or link is off +* (rpm_lvl 5/6): nothing to query until a full resume. +*/ + ret = -EINVAL ; +break ; + } + +if (ufshcd_is_link_hibern8 (hba)) { + ret =ufshcd_uic_hibern8_exit (hba); +if (ret) { +dev_err (hba->dev,"%s: hibern8 exit failed %d\n" , + __func__, ret); +break ; + } +ufshcd_set_link_active (hba); + exited_h8 = true; + } + + ret =ufshcd_query_attr (hba,UPIU_QUERY_OPCODE_READ_ATTR , +QUERY_ATTR_IDN_POWER_MODE ,0 ,0 , &value); + +if (exited_h8) { +int h8_ret =ufshcd_uic_hibern8_enter (hba); + +if (h8_ret) { +dev_err (hba->dev,"%s: hibern8 enter failed %d\n" , + __func__, h8_ret); + ret = h8_ret; + }else { +ufshcd_set_link_hibern8 (hba); + } + } + } +ufshcd_release (hba); +pm_runtime_put_sync (hba->dev); + +if (!ret) + ret =sysfs_emit (buf,"0x%08X\n" , value); +else + ret = -EINVAL ; + +out_unlock: +up (&hba->host_sem); +return ret; +} +static DEVICE_ATTR_RO (current_power_mode); +UFS_ATTRIBUTE (active_icc_level, _ACTIVE_ICC_LVL);UFS_ATTRIBUTE (ooo_data_enabled, _OOO_DATA_EN);UFS_ATTRIBUTE (bkops_status, _BKOPS_STATUS);
drivers/ufs/core/ufshcd-priv.h +11 -0
@@ -3,6 +3,7 @@#ifndef _UFSHCD_PRIV_H_#define _UFSHCD_PRIV_H_ +#include <linux/cleanup.h> #include <linux/pm_runtime.h> #include <ufs/ufshcd.h>
@@ -15,6 +16,16 @@ static inline bool ufshcd_is_user_access_allowed(struct ufs_hba *hba)void ufshcd_schedule_eh_work (struct ufs_hba *hba); +DEFINE_LOCK_GUARD_1(ufshcd_wl_pm, struct ufs_hba, + ({ +mutex_lock (&_T->lock->wl_pm_mutex); + _T->lock->pm_op_in_progress = true; + }), + ({ + _T->lock->pm_op_in_progress = false; +mutex_unlock (&_T->lock->wl_pm_mutex); + })) +static inline bool ufshcd_keep_autobkops_enabled_except_suspend (struct ufs_hba *hba) {
drivers/ufs/core/ufshcd.c +7 -5
@@ -10294,7 +10294,8 @@ static int __ufshcd_wl_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)enum ufs_dev_pwr_mode req_dev_pwr_mode;enum uic_link_state req_link_state; - hba->pm_op_in_progress = true; +guard (ufshcd_wl_pm)(hba); +if (pm_op !=UFS_SHUTDOWN_PM ) { pm_lvl = pm_op ==UFS_RUNTIME_PM ? hba->rpm_lvl : hba->spm_lvl;
@@ -10467,7 +10468,6 @@ static int __ufshcd_wl_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op) hba->clk_gating.is_suspended = false;ufshcd_release (hba); } - hba->pm_op_in_progress = false;return ret; }
@@ -10475,9 +10475,11 @@ static int __ufshcd_wl_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)static int __ufshcd_wl_resume (struct ufs_hba *hba,enum ufs_pm_op pm_op) {int ret; -enum uic_link_state old_link_state = hba->uic_link_state; +enum uic_link_state old_link_state; + +guard (ufshcd_wl_pm)(hba); - hba->pm_op_in_progress = true; + old_link_state = hba->uic_link_state;/* * Call vendor specific resume callback. As these callbacks may access
@@ -10568,7 +10570,6 @@ static int __ufshcd_wl_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)ufshcd_update_evt_hist (hba,UFS_EVT_WL_RES_ERR , (u32 )ret); hba->clk_gating.is_suspended = false;ufshcd_release (hba); - hba->pm_op_in_progress = false;return ret; }
@@ -11256,6 +11257,7 @@ int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)INIT_WORK (&hba->eeh_work, ufshcd_exception_event_handler);sema_init (&hba->host_sem,1 ); +mutex_init (&hba->wl_pm_mutex);/* Initialize UIC command mutex */ mutex_init (&hba->uic_cmd_mutex);
include/ufs/ufshcd.h +3 -0
@@ -1010,6 +1010,8 @@ enum ufshcd_mcq_opr {* @is_powered: flag to check if HBA is powered * @shutting_down: flag to check if shutdown has been invoked * @host_sem: semaphore used to serialize concurrent contexts +* @wl_pm_mutex: serializes WLUN suspend/resume against other contexts that +* change the link power state while the WLUN may be suspended * @eh_wq: Workqueue that eh_work works on * @eh_work: Worker to handle UFS errors that require s/w attention * @eeh_work: Worker to handle exception events
@@ -1176,6 +1178,7 @@ struct ufs_hba {bool is_powered;bool shutting_down;struct semaphore host_sem; +struct mutex wl_pm_mutex;/* Work Queues */ struct workqueue_struct *eh_wq;
Signature · 1 line
2.34.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] ufs: sysfs: missing check for pm_runtime_get_sync() failure - [High] ufs: core: sysfs read breaks…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] ufs: sysfs: missing check for pm_runtime_get_sync() failure
- [High] ufs: core: sysfs read breaks SCSI error recovery by setting pm_op_in_progress
- [High] ufs: core: ufshcd_hold() during WLUN suspend clobbers clk_gating.is_suspended
- [Medium] ufs: cleanup: mixing goto and scoped guards violates subsystem rules --
commit d0c0842fc07bdb2ba7fa8889f82851b6387be9b5
Author: Yiwei Lin <[email protected]>
Subject: ufs: sysfs: fix current_power_mode read without SSU
This patch modifies the current_power_mode sysfs attribute to read the value without fully resuming the WLUN. It uses Hibern8 exit/enter instead of waking the device to Active mode.
Quoted 4 lines
drivers/ufs/core/ufs-sysfs.c +0
-0
[ ... ]
Quoted 16 lines
drivers/ufs/core/ufs-sysfs.c +11
-1@@ -1786,7 +1786,79 @@ out: \static DEVICE_ATTR_RO (_name)UFS_ATTRIBUTE (boot_lun_enabled, _BOOT_LU_EN); -UFS_ATTRIBUTE (current_power_mode, _POWER_MODE); + +/* +* Per UFS spec, bCurrentPowerMode is the only attribute the device must +* respond to in any power mode. Read it without waking the device to +* Active. +*/ +static ssize_t current_power_mode_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct ufs_hba *hba =dev_get_drvdata (dev); +bool exited_h8 = false;
drivers/ufs/core/ufs-sysfs.c out: \ · line 1809
Quoted 10 lines
drivers/ufs/core/ufs-sysfs.c +10
-0+u32 value; +int ret =0 ; + +down (&hba->host_sem); +if (!ufshcd_is_user_access_allowed (hba)) { + ret = -EBUSY ; + goto out_unlock; + } + +pm_runtime_get_sync (hba->dev);
[Severity: High]
Does this code need to check the return value of pm_runtime_get_sync()?
If it fails, the controller power domain remains off, but the function ignores the error, proceeds to call ufshcd_hold(), and directly accesses hardware registers via ufshcd_uic_hibern8_exit() and ufshcd_query_attr(), which could cause a system crash (synchronous external abort) due to accessing unpowered hardware registers.
drivers/ufs/core/ufs-sysfs.c out: \ · line 1811
Quoted 2 lines
drivers/ufs/core/ufs-sysfs.c +2
-0+ +ufshcd_hold (hba);
[Severity: High]
Can calling ufshcd_hold() without waking the WLUN clobber the clock gating state?
If clocks are off, this queues ufshcd_ungate_work(). In ufshcd_ungate_work(), the code unconditionally overwrites hba->clk_gating.is_suspended = false at the end of its Hibern8 exit block.
Because the WLUN is actually still suspended, this state corruption causes the subsequent ufshcd_release(hba) to schedule ufshcd_gate_work(), which will then attempt to issue Hibern8 commands to a link that may be in DeepSleep (powered off), resulting in unrecoverable failures.
drivers/ufs/core/ufs-sysfs.c out: \ · line 1813
Quoted 2 lines
drivers/ufs/core/ufs-sysfs.c +2
-0+ scoped_guard(ufshcd_wl_pm, hba) { +if (ufshcd_is_link_off (hba) ||ufshcd_is_ufs_dev_poweroff (hba)) {
[Severity: Medium]
Does mixing scoped_guard() with the manual goto out_unlock cleanup below violate the cleanup subsystem architectural rules?
The rules state that code should not mix goto-based error handling with guard()/scoped_guard() declarations in the same function to avoid confusing ownership semantics.
[Severity: High]
Can holding this scoped_guard() break SCSI error recovery by setting pm_op_in_progress?
The DEFINE_LOCK_GUARD_1(ufshcd_wl_pm) macro sets hba->pm_op_in_progress = true. If a concurrent background I/O times out while this sysfs read holds the guard, ufshcd_eh_timed_out() observes the flag, bypasses standard aborts/resets to avoid deadlocking with PM, and directly completes the command. This leaves the hardware permanently hung since no actual PM recovery operation is taking place.
Quoted 7 lines
drivers/ufs/core/ufs-sysfs.c +7
-0+/* +* Device is powered down (rpm_lvl 4/5) or link is off +* (rpm_lvl 5/6): nothing to query until a full resume. +*/ + ret = -EINVAL ; +break ; + }
[ ... ]
Quoted 25 lines
drivers/ufs/core/ufs-sysfs.c +25
-0+if (exited_h8) { +int h8_ret =ufshcd_uic_hibern8_enter (hba); + +if (h8_ret) { +dev_err (hba->dev,"%s: hibern8 enter failed %d\n" , + __func__, h8_ret); + ret = h8_ret; + }else { +ufshcd_set_link_hibern8 (hba); + } + } + } +ufshcd_release (hba); +pm_runtime_put_sync (hba->dev); + +if (!ret) + ret =sysfs_emit (buf,"0x%08X\n" , value); +else + ret = -EINVAL ; + +out_unlock: +up (&hba->host_sem); +return ret; +} +static DEVICE_ATTR_RO (current_power_mode);
[ ... ]
drivers/ufs/core/ufshcd.c static int __ufshcd_wl_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op) · line 10324
Quoted 12 lines
drivers/ufs/core/ufshcd.c +2
-1@@ -10319,7 +10319,8 @@ static int __ufshcd_wl_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)enum ufs_dev_pwr_mode req_dev_pwr_mode;enum uic_link_state req_link_state; - hba->pm_op_in_progress = true; +guard (ufshcd_wl_pm)(hba); +if (pm_op !=UFS_SHUTDOWN_PM ) {
[Severity: Medium]
Does the use of guard() here mix with the extensive goto state transitions present later in this function (e.g., goto vops_suspend)?
The cleanup.md guidelines explicitly prohibit mixing goto-based error handling with guard() declarations in the same function.
Quoted 2 lines
drivers/ufs/core/ufshcd.c +0
-0pm_lvl = pm_op ==UFS_RUNTIME_PM ? hba->rpm_lvl : hba->spm_lvl;
[ ... ]
drivers/ufs/core/ufshcd.c static int __ufshcd_wl_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op) · line 10507
Quoted 11 lines
drivers/ufs/core/ufshcd.c +4
-2@@ -10500,9 +10500,11 @@ static int __ufshcd_wl_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)static int __ufshcd_wl_resume (struct ufs_hba *hba,enum ufs_pm_op pm_op) {int ret; -enum uic_link_state old_link_state = hba->uic_link_state; +enum uic_link_state old_link_state; + +guard (ufshcd_wl_pm)(hba); - hba->pm_op_in_progress = true; + old_link_state = hba->uic_link_state;
[Severity: Medium]
Does this mix guard() with goto out cleanup jumps in this function?
The rules state that a function should convert all resources to scope-based cleanup or none of them.
Quoted 3 lines
drivers/ufs/core/ufshcd.c +0
-0/* * Call vendor specific resume callback. As these callbacks may access
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1