Two changes of UFS sysfs
v2 cover Two changes of UFS sysfs
From: Bean Huo <[email protected]>
Changelog:
V1---v2:
1. Add new patch "Let resume callback return -EBUSY after ufshcd_shutdown", Because the ufshcd_*_resume still returns successful result 0 after ufshcd_shutdown(). Even add handling of the return value of pm_runtime_get_sync(), but still there is timeout.
Bean Huo (3):
scsi: ufs: Replace sprintf and snprintf with sysfs_emit
scsi: ufs: Add handling of the return value of pm_runtime_get_sync()
scsi: ufs: Let resume callback return -EBUSY after ufshcd_shutdown
drivers/scsi/ufs/ufs-sysfs.c | 68 +++++++++++++++++++++++++----------- drivers/scsi/ufs/ufshcd.c | 12 ++++--- 2 files changed, 55 insertions(+), 25 deletions(-)
Signature · 1 line
2.17.1
PATCH v2 1/3 scsi: ufs: Replace sprintf and snprintf with sysfs_emit
From: Bean Huo <[email protected]>
sprintf and snprintf may cause output defect in sysfs content, it is better to use new added sysfs_emit function which knows the size of the temporary buffer.
--- drivers/scsi/ufs/ufs-sysfs.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-)
drivers/scsi/ufs/ufs-sysfs.c +15 -15
@@ -67,7 +67,7 @@ static ssize_t rpm_lvl_show(struct device *dev, {struct ufs_hba *hba =dev_get_drvdata (dev); -return sprintf (buf,"%d\n" , hba->rpm_lvl); +return sysfs_emit (buf,"%d\n" , hba->rpm_lvl); } static ssize_t rpm_lvl_store(struct device *dev,
@@ -81,7 +81,7 @@ static ssize_t rpm_target_dev_state_show(struct device *dev, {struct ufs_hba *hba =dev_get_drvdata (dev); -return sprintf (buf,"%s\n" ,ufschd_ufs_dev_pwr_mode_to_string ( +return sysfs_emit (buf,"%s\n" ,ufschd_ufs_dev_pwr_mode_to_string ( ufs_pm_lvl_states[hba->rpm_lvl].dev_state)); }
@@ -90,7 +90,7 @@ static ssize_t rpm_target_link_state_show(struct device *dev, {struct ufs_hba *hba =dev_get_drvdata (dev); -return sprintf (buf,"%s\n" ,ufschd_uic_link_state_to_string ( +return sysfs_emit (buf,"%s\n" ,ufschd_uic_link_state_to_string ( ufs_pm_lvl_states[hba->rpm_lvl].link_state)); }
@@ -99,7 +99,7 @@ static ssize_t spm_lvl_show(struct device *dev, {struct ufs_hba *hba =dev_get_drvdata (dev); -return sprintf (buf,"%d\n" , hba->spm_lvl); +return sysfs_emit (buf,"%d\n" , hba->spm_lvl); } static ssize_t spm_lvl_store(struct device *dev,
@@ -113,7 +113,7 @@ static ssize_t spm_target_dev_state_show(struct device *dev, {struct ufs_hba *hba =dev_get_drvdata (dev); -return sprintf (buf,"%s\n" ,ufschd_ufs_dev_pwr_mode_to_string ( +return sysfs_emit (buf,"%s\n" ,ufschd_ufs_dev_pwr_mode_to_string ( ufs_pm_lvl_states[hba->spm_lvl].dev_state)); }
@@ -122,7 +122,7 @@ static ssize_t spm_target_link_state_show(struct device *dev, {struct ufs_hba *hba =dev_get_drvdata (dev); -return sprintf (buf,"%s\n" ,ufschd_uic_link_state_to_string ( +return sysfs_emit (buf,"%s\n" ,ufschd_uic_link_state_to_string ( ufs_pm_lvl_states[hba->spm_lvl].link_state)); }
@@ -165,7 +165,7 @@ static ssize_t auto_hibern8_show(struct device *dev,ufshcd_release (hba);pm_runtime_put_sync (hba->dev); -return scnprintf (buf,PAGE_SIZE ,"%d\n" ,ufshcd_ahit_to_us (ahit)); +return sysfs_emit (buf,"%d\n" ,ufshcd_ahit_to_us (ahit)); } static ssize_t auto_hibern8_store(struct device *dev,
@@ -233,18 +233,18 @@ static ssize_t ufs_sysfs_read_desc_param(struct ufs_hba *hba,return -EINVAL ;switch (param_size) {case 1 : - ret =sprintf (sysfs_buf,"0x%02X\n" , *desc_buf); + ret =sysfs_emit (sysfs_buf,"0x%02X\n" , *desc_buf);break ;case 2 : - ret =sprintf (sysfs_buf,"0x%04X\n" , + ret =sysfs_emit (sysfs_buf,"0x%04X\n" ,get_unaligned_be16 (desc_buf));break ;case 4 : - ret =sprintf (sysfs_buf,"0x%08X\n" , + ret =sysfs_emit (sysfs_buf,"0x%08X\n" ,get_unaligned_be32 (desc_buf));break ;case 8 : - ret =sprintf (sysfs_buf,"0x%016llX\n" , + ret =sysfs_emit (sysfs_buf,"0x%016llX\n" ,get_unaligned_be64 (desc_buf));break ; }
@@ -609,7 +609,7 @@ static ssize_t _name##_show(struct device *dev, \ SD_ASCII_STD); \if (ret <0 ) \ goto out; \ - ret =snprintf (buf,PAGE_SIZE ,"%s\n" , desc_buf); \ + ret =sysfs_emit (buf,"%s\n" , desc_buf); \ out: \pm_runtime_put_sync (hba->dev); \kfree (desc_buf); \
@@ -659,7 +659,7 @@ static ssize_t _name##_show(struct device *dev, \pm_runtime_put_sync (hba->dev); \if (ret) \return -EINVAL ; \ -return sprintf (buf,"%s\n" , flag ?"true" :"false" ); \ +return sysfs_emit (buf,"%s\n" , flag ?"true" :"false" ); \ } \static DEVICE_ATTR_RO (_name)
@@ -717,7 +717,7 @@ static ssize_t _name##_show(struct device *dev, \pm_runtime_put_sync (hba->dev); \if (ret) \return -EINVAL ; \ -return sprintf (buf,"0x%08X\n" , value); \ +return sysfs_emit (buf,"0x%08X\n" , value); \ } \static DEVICE_ATTR_RO (_name)
@@ -856,7 +856,7 @@ static ssize_t dyn_cap_needed_attribute_show(struct device *dev,pm_runtime_put_sync (hba->dev);if (ret)return -EINVAL ; -return sprintf (buf,"0x%08X\n" , value); +return sysfs_emit (buf,"0x%08X\n" , value); }static DEVICE_ATTR_RO (dyn_cap_needed_attribute);
Signature · 1 line
2.17.1
PATCH v2 2/3 scsi: ufs: Add handling of the return value of pm_runtime_get_sync()
In reply to Bean HuoFrom: Bean Huo <[email protected]>
The race issue may exist between UFS access in UFS sysfs context and UFS shutdown, thus will cause pm_runtime_get_sync() resume failure. Add handling of the return value of pm_runtime_get_sync(). Let it return in case pm_runtime_get_sync() resume failed.
--- drivers/scsi/ufs/ufs-sysfs.c | 38 ++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-)
drivers/scsi/ufs/ufs-sysfs.c +32 -6
@@ -154,12 +154,17 @@ static ssize_t auto_hibern8_show(struct device *dev, struct device_attribute *attr, char *buf) { u32 ahit; +int ret;struct ufs_hba *hba =dev_get_drvdata (dev);if (!ufshcd_is_auto_hibern8_supported (hba))return -EOPNOTSUPP ; -pm_runtime_get_sync (hba->dev); + ret =pm_runtime_get_sync (hba->dev); +if (ret <0 ) { +pm_runtime_put_noidle (hba->dev); +return ret; + }ufshcd_hold (hba, false); ahit =ufshcd_readl (hba,REG_AUTO_HIBERNATE_IDLE_TIMER );ufshcd_release (hba);
@@ -225,7 +230,12 @@ static ssize_t ufs_sysfs_read_desc_param(struct ufs_hba *hba,if (param_size >8 )return -EINVAL ; -pm_runtime_get_sync (hba->dev); + ret =pm_runtime_get_sync (hba->dev); +if (ret <0 ) { +pm_runtime_put_noidle (hba->dev); +return ret; + } + ret =ufshcd_read_desc_param (hba, desc_id, desc_index, param_offset, desc_buf, param_size);pm_runtime_put_sync (hba->dev);
@@ -594,7 +604,11 @@ static ssize_t _name##_show(struct device *dev, \ desc_buf =kzalloc (QUERY_DESC_MAX_SIZE ,GFP_ATOMIC ); \if (!desc_buf) \return -ENOMEM ; \ -pm_runtime_get_sync (hba->dev); \ + ret =pm_runtime_get_sync (hba->dev); \ +if (ret <0 ) { \ +pm_runtime_put_noidle (hba->dev); \ +return ret; \ + } \ ret =ufshcd_query_descriptor_retry (hba, \UPIU_QUERY_OPCODE_READ_DESC ,QUERY_DESC_IDN_DEVICE , \0 ,0 , desc_buf, &desc_len); \
@@ -653,7 +667,11 @@ static ssize_t _name##_show(struct device *dev, \struct ufs_hba *hba =dev_get_drvdata (dev); \ if (ufshcd_is_wb_flags(QUERY_FLAG_IDN##_uname)) \ index =ufshcd_wb_get_query_index (hba); \ -pm_runtime_get_sync (hba->dev); \ + ret =pm_runtime_get_sync (hba->dev); \ +if (ret <0 ) { \ +pm_runtime_put_noidle (hba->dev); \ +return ret; \ + } \ ret =ufshcd_query_flag (hba,UPIU_QUERY_OPCODE_READ_FLAG , \ QUERY_FLAG_IDN##_uname, index, &flag); \pm_runtime_put_sync (hba->dev); \
@@ -711,7 +729,11 @@ static ssize_t _name##_show(struct device *dev, \u8 index =0 ; \ if (ufshcd_is_wb_attrs(QUERY_ATTR_IDN##_uname)) \ index =ufshcd_wb_get_query_index (hba); \ -pm_runtime_get_sync (hba->dev); \ + ret =pm_runtime_get_sync (hba->dev); \ +if (ret <0 ) { \ +pm_runtime_put_noidle (hba->dev); \ +return ret; \ + } \ ret =ufshcd_query_attr (hba,UPIU_QUERY_OPCODE_READ_ATTR , \ QUERY_ATTR_IDN##_uname, index, 0, &value); \pm_runtime_put_sync (hba->dev); \
@@ -850,7 +872,11 @@ static ssize_t dyn_cap_needed_attribute_show(struct device *dev,u8 lun =ufshcd_scsi_to_upiu_lun (sdev->lun);int ret; -pm_runtime_get_sync (hba->dev); + ret =pm_runtime_get_sync (hba->dev); +if (ret <0 ) { +pm_runtime_put_noidle (hba->dev); +return ret; + } ret =ufshcd_query_attr (hba,UPIU_QUERY_OPCODE_READ_ATTR ,QUERY_ATTR_IDN_DYN_CAP_NEEDED , lun,0 , &value);pm_runtime_put_sync (hba->dev);
Signature · 1 line
2.17.1
Re: [PATCH v2 2/3] scsi: ufs: Add handling of the return value of pm_runtime_get_sync()
Quoted 4 lines · Bean Huo
From: Bean Huo <[email protected]>
The race issue may exist between UFS access in UFS sysfs context and UFS shutdown, thus will cause
pm_runtime_get_sync()resume failure.
Are you trying to fix the race condition by adding these checks or just adding these checks in case pm_runtime_get_sync() fails?
Can Guo.
Quoted 103 lines
Add handling of the return value of
pm_runtime_get_sync(). Let it return in casepm_runtime_get_sync()resume failed.Signed-off-by Bean Huo <[email protected]>--- drivers/scsi/ufs/ufs-sysfs.c | 38 ++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-)drivers/scsi/ufs/ufs-sysfs.c +6
-1@@ -154,12 +154,17 @@ static ssize_t auto_hibern8_show(struct device *dev, struct device_attribute *attr, char *buf) {u32 ahit; +int ret;struct ufs_hba *hba =dev_get_drvdata (dev);if (!ufshcd_is_auto_hibern8_supported (hba))return -EOPNOTSUPP ; -pm_runtime_get_sync (hba->dev); + ret =pm_runtime_get_sync (hba->dev); +if (ret <0 ) { +pm_runtime_put_noidle (hba->dev); +return ret; + }ufshcd_hold (hba, false); ahit =ufshcd_readl (hba,REG_AUTO_HIBERNATE_IDLE_TIMER );drivers/scsi/ufs/ufs-sysfs.c +6
-1ufshcd_release (hba);@@ -225,7 +230,12 @@ static ssize_t ufs_sysfs_read_desc_param(struct ufs_hba *hba, if (param_size > 8) return -EINVAL; -pm_runtime_get_sync (hba->dev); + ret =pm_runtime_get_sync (hba->dev); +if (ret <0 ) { +pm_runtime_put_noidle (hba->dev); +return ret; + } + ret =ufshcd_read_desc_param (hba, desc_id, desc_index, param_offset, desc_buf, param_size);drivers/scsi/ufs/ufs-sysfs.c +5
-1pm_runtime_put_sync (hba->dev);@@ -594,7 +604,11 @@ static ssize_t _name##_show(struct device *dev, \ desc_buf =kzalloc (QUERY_DESC_MAX_SIZE ,GFP_ATOMIC ); \if (!desc_buf) \return -ENOMEM ; \ -pm_runtime_get_sync (hba->dev); \ + ret =pm_runtime_get_sync (hba->dev); \ +if (ret <0 ) { \ +pm_runtime_put_noidle (hba->dev); \ +return ret; \ + } \ ret = ufshcd_query_descriptor_retry(hba, \ UPIU_QUERY_OPCODE_READ_DESC, QUERY_DESC_IDN_DEVICE, \drivers/scsi/ufs/ufs-sysfs.c +5
-10, 0, desc_buf, &desc_len); \@@ -653,7 +667,11 @@ static ssize_t _name##_show(struct device *dev, \ struct ufs_hba *hba = dev_get_drvdata(dev); \ if (ufshcd_is_wb_flags(QUERY_FLAG_IDN##_uname)) \ index =ufshcd_wb_get_query_index (hba); \ -pm_runtime_get_sync (hba->dev); \ + ret =pm_runtime_get_sync (hba->dev); \ +if (ret <0 ) { \ +pm_runtime_put_noidle (hba->dev); \ +return ret; \ + } \ ret =ufshcd_query_flag (hba,UPIU_QUERY_OPCODE_READ_FLAG , \ QUERY_FLAG_IDN##_uname, index, &flag); \drivers/scsi/ufs/ufs-sysfs.c +5
-1pm_runtime_put_sync (hba->dev); \@@ -711,7 +729,11 @@ static ssize_t _name##_show(struct device *dev, \ u8 index = 0; \ if (ufshcd_is_wb_attrs(QUERY_ATTR_IDN##_uname)) \ index =ufshcd_wb_get_query_index (hba); \ -pm_runtime_get_sync (hba->dev); \ + ret =pm_runtime_get_sync (hba->dev); \ +if (ret <0 ) { \ +pm_runtime_put_noidle (hba->dev); \ +return ret; \ + } \ ret =ufshcd_query_attr (hba,UPIU_QUERY_OPCODE_READ_ATTR , \ QUERY_ATTR_IDN##_uname, index, 0, &value); \drivers/scsi/ufs/ufs-sysfs.c +5
-1pm_runtime_put_sync (hba->dev); \@@ -850,7 +872,11 @@ static ssize_t dyn_cap_needed_attribute_show(struct device *dev, u8 lun = ufshcd_scsi_to_upiu_lun(sdev->lun);int ret; -pm_runtime_get_sync (hba->dev); + ret =pm_runtime_get_sync (hba->dev); +if (ret <0 ) { +pm_runtime_put_noidle (hba->dev); +return ret; + } ret =ufshcd_query_attr (hba,UPIU_QUERY_OPCODE_READ_ATTR ,QUERY_ATTR_IDN_DYN_CAP_NEEDED , lun,0 , &value);
pm_runtime_put_sync(hba->dev);
Re: [PATCH v2 2/3] scsi: ufs: Add handling of the return value of pm_runtime_get_sync()
Quoted 10 lines · Can Guo
Quoted 4 lines
The race issue may exist between UFS access in UFS sysfs context and
UFS
shutdown, thus will causepm_runtime_get_sync()resume failure.Are you trying to fix the race condition by adding these checks or just
adding these checks in casepm_runtime_get_sync()fails?Can Guo.
Can,
thanks for your review. Sorry, I didn't quite get your point. This patch is just to add checkup in case pm_runtime_get_sync() failed.
what else should be added?
Bean
PATCH v2 3/3 scsi: ufs: Let resume callback return -EBUSY after ufshcd_shutdown
In reply to Bean HuoFrom: Bean Huo <[email protected]>
After ufshcd_shutdown(), both UFS device and UFS LINk are powered off, return '0' will mislead the upper PM layer since the device has not been successfully resumed yet. This will let pm_runtime_get_sync() caller mistakenly believe the device/LINK has been resumed, which leads to request processing timeout that was en-queued later.
To fix this, let ufshcd_system/runtimie_resume() return -EBUSY in case of hba->is_powered == false.
--- drivers/scsi/ufs/ufshcd.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/scsi/ufs/ufshcd.c +8 -4
@@ -8950,14 +8950,16 @@ int ufshcd_system_resume(struct ufs_hba *hba)return -EINVAL ; } -if (!hba->is_powered ||pm_runtime_suspended (hba->dev)) +if (!hba->is_powered ||pm_runtime_suspended (hba->dev)) {/* * Let the runtime resume take care of resuming * if runtime suspended. */ + ret = -EBUSY ; goto out; -else + }else { ret =ufshcd_resume (hba,UFS_SYSTEM_PM ); + } out: trace_ufshcd_system_resume(dev_name(hba->dev), ret, ktime_to_us(ktime_sub(ktime_get(), start)),
@@ -9026,10 +9028,12 @@ int ufshcd_runtime_resume(struct ufs_hba *hba)if (!hba)return -EINVAL ; -if (!hba->is_powered) +if (!hba->is_powered) { + ret = -EBUSY ; goto out; -else + }else { ret =ufshcd_resume (hba,UFS_RUNTIME_PM ); + } out: trace_ufshcd_runtime_resume(dev_name(hba->dev), ret, ktime_to_us(ktime_sub(ktime_get(), start)),
Signature · 1 line
2.17.1
Re: [PATCH v2 3/3] scsi: ufs: Let resume callback return -EBUSY after ufshcd_shutdown
Hi Rafael
Would you please help to review this patch assoociated with PM resume callback implementation?
Original code that ufshcd_system/runtime_resume returns '0' in case resume failed due to the UFS device and UFS LINk are powered off(hba-
Quoted 1 line
is_powered== false).
In the patch 2/3 Add handling of the return value of pm_runtime_get_sync() is to fix race exists between shudown path and device access through sysfs node.
Thanks,
Bean
Quoted 58 lines · Bean Huo
From: Bean Huo <[email protected]>
After
ufshcd_shutdown(), both UFS device and UFS LINk are powered off,
return '0' will mislead the upper PM layer since the device has not been
successfully resumed yet. This will letpm_runtime_get_sync()caller mistakenly believe the device/LINK has been resumed, which leads to request processing timeout that was en-queued later.To fix this, let ufshcd_system/runtimie_resume() return
-EBUSYin case ofhba->is_powered== false.Signed-off-by Bean Huo <[email protected]>--- drivers/scsi/ufs/ufshcd.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)drivers/scsi/ufs/ufshcd.c +8
-4@@ -8950,14 +8950,16 @@ int ufshcd_system_resume(struct ufs_hba *hba)return -EINVAL ; } -if (!hba->is_powered ||pm_runtime_suspended (hba->dev)) +if (!hba->is_powered ||pm_runtime_suspended (hba->dev)) {/* * Let the runtime resume take care of resuming * if runtime suspended. */ + ret = -EBUSY ; goto out; -else + }else { ret =ufshcd_resume (hba,UFS_SYSTEM_PM ); + } out: trace_ufshcd_system_resume(dev_name(hba->dev), ret, ktime_to_us(ktime_sub(ktime_get(), start)),@@ -9026,10 +9028,12 @@ int ufshcd_runtime_resume(struct ufs_hba *hba)if (!hba)return -EINVAL ; -if (!hba->is_powered) +if (!hba->is_powered) { + ret = -EBUSY ; goto out; -else + }else { ret =ufshcd_resume (hba,UFS_RUNTIME_PM ); + } out: trace_ufshcd_runtime_resume(dev_name(hba->dev), ret,
ktime_to_us(ktime_sub(ktime_get(), start)),
Re: [PATCH v2 3/3] scsi: ufs: Let resume callback return -EBUSY after ufshcd_shutdown
In reply to Bean HuoQuoted 10 lines · Bean Huo
From: Bean Huo <[email protected]>
After
ufshcd_shutdown(), both UFS device and UFS LINk are powered off, return '0' will mislead the upper PM layer since the device has not been successfully resumed yet. This will letpm_runtime_get_sync()caller mistakenly believe the device/LINK has been resumed, which leads to request processing timeout that was en-queued later.To fix this, let ufshcd_system/runtimie_resume() return
-EBUSYin case ofhba->is_powered== false.
This change won't work as you expect...
During/after shutdown, for UFS's case only, pm_runtime_get_sync(hba->dev) will most likely return 0, because it is already RUNTIME_ACTIVE, pm_runtime_get_sync() will directly return 0... meaning your change won't even be exercised.
Check Stanley's change -
https://lore.kernel.org/patchwork/patch/1341389/
Can Guo.
Quoted 44 lines
Signed-off-by Bean Huo <[email protected]>--- drivers/scsi/ufs/ufshcd.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)drivers/scsi/ufs/ufshcd.c +8
-4@@ -8950,14 +8950,16 @@ int ufshcd_system_resume(struct ufs_hba *hba)return -EINVAL ; } -if (!hba->is_powered ||pm_runtime_suspended (hba->dev)) +if (!hba->is_powered ||pm_runtime_suspended (hba->dev)) {/* * Let the runtime resume take care of resuming * if runtime suspended. */ + ret = -EBUSY ; goto out; -else + }else { ret =ufshcd_resume (hba,UFS_SYSTEM_PM ); + } out: trace_ufshcd_system_resume(dev_name(hba->dev), ret, ktime_to_us(ktime_sub(ktime_get(), start)),@@ -9026,10 +9028,12 @@ int ufshcd_runtime_resume(struct ufs_hba *hba)if (!hba)return -EINVAL ; -if (!hba->is_powered) +if (!hba->is_powered) { + ret = -EBUSY ; goto out; -else + }else { ret =ufshcd_resume (hba,UFS_RUNTIME_PM ); + } out: trace_ufshcd_runtime_resume(dev_name(hba->dev), ret, ktime_to_us(ktime_sub(ktime_get(), start)),