Synchronize user layer access with system PM ops and error handling
v3 cover Synchronize user layer access with system PM ops and error handling
This series contains two changes and it is based on 5.11/scsi-queue
The 1st change is just a minor fix.
The 2nd change is to synchronize user layer access through UFS sysfs nodes, so that system PM ops (suspend, resume and shutdown), error handling and async probe won't be disturbed by user layer access. The protection is only added to some sysfs nodes, not all of them.
Change since v2:
- Updated the 1st change, added a global boolean flag to tell if system suspend is invoked when hba is NULL, it is used during resume in case of hba becomes not NULL.
Change since v1:
- Slightly updated the 2nd change, added a dedicated inline func to check hba->shutting_down in ufshcd.h. This inline func can be updated to add more rules for sysfs passage in future.
Can Guo (2):
scsi: ufs: Fix a possible NULL pointer issue
scsi: ufs: Protect PM ops and err_handler from user access through
sysfs
drivers/scsi/ufs/ufs-sysfs.c | 104 ++++++++++++++++++++++++++++++++++++------- drivers/scsi/ufs/ufshcd.c | 49 ++++++++++++-------- drivers/scsi/ufs/ufshcd.h | 10 ++++- 3 files changed, 128 insertions(+), 35 deletions(-)
Signature · 1 line
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project.
PATCH v3 1/2 scsi: ufs: Fix a possible NULL pointer issue
During system resume/suspend, hba could be NULL. In this case, do not touch eh_sem.
drivers/scsi/ufs/ufshcd.c +14 -3
@@ -94,6 +94,8 @@ 16, 4, buf, __len, false); \ }while (0 ) +static bool early_suspend; +int ufshcd_dump_regs (struct ufs_hba *hba,size_t offset,size_t len,const char *prefix) {
@@ -8896,8 +8898,14 @@ int ufshcd_system_suspend(struct ufs_hba *hba)int ret =0 ;ktime_t start =ktime_get (); +if (!hba) { + early_suspend = true; +return 0 ; + } +down (&hba->eh_sem); -if (!hba || !hba->is_powered) + +if (!hba->is_powered)return 0 ; if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) ==
@@ -8945,9 +8953,12 @@ int ufshcd_system_resume(struct ufs_hba *hba)int ret =0 ;ktime_t start =ktime_get (); -if (!hba) { -up (&hba->eh_sem); +if (!hba)return -EINVAL ; + +if (unlikely (early_suspend)) { + early_suspend = false; +down (&hba->eh_sem); }if (!hba->is_powered ||pm_runtime_suspended (hba->dev))
Signature · 1 line
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project.
Re: [PATCH 1/2] scsi: ufs: Fix a possible NULL pointer issue
Hi Can,
Quoted 39 lines · Can Guo
During system resume/suspend, hba could be NULL. In this case, do not touch
eh_sem.Fixes 88a92d6ae4fe ("scsi: ufs: Serialize eh_work with system PM events and async scan")Signed-off-by Can Guo <[email protected]>drivers/scsi/ufs/ufshcd.c +9
-1@@ -94,6 +94,8 @@ 16, 4, buf, __len, false); \ }while (0 ) +static bool early_suspend; +int ufshcd_dump_regs (struct ufs_hba *hba,size_t offset,size_t len,const char *prefix) {@@ -8896,8 +8898,14 @@ int ufshcd_system_suspend(struct ufs_hba *hba)int ret =0 ;ktime_t start =ktime_get (); +if (!hba) { + early_suspend = true; +return 0 ; + } +down (&hba->eh_sem); -if (!hba || !hba->is_powered) + +if (!hba->is_powered)return 0 ; if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) ==@@ -8945,9 +8953,12 @@ int ufshcd_system_resume(struct ufs_hba *hba)int ret =0 ;ktime_t start =ktime_get ();
drivers/scsi/ufs/ufshcd.c int ufshcd_system_resume(struct ufs_hba *hba) · line 8962
Quoted 10 lines
drivers/scsi/ufs/ufshcd.c +5
-2-if (!hba) { -up (&hba->eh_sem); +if (!hba)return -EINVAL ; + +if (unlikely (early_suspend)) { + early_suspend = false; +down (&hba->eh_sem); }
I guess early_suspend here is to handle the case that hba is null during ufshcd_system_suspend() but !null during ufshcd_system_resume(). If yes, would it be possible? If no, may I know what is the purpose?
Thanks a lot.
Stanley Chu
Quoted 2 lines
drivers/scsi/ufs/ufshcd.c +0
-0if (!hba->is_powered ||pm_runtime_suspended (hba->dev))
Re: [PATCH 1/2] scsi: ufs: Fix a possible NULL pointer issue
Quoted 43 lines · Stanley Chu
Hi Can,
Quoted 40 lines · Can Guo
During system resume/suspend, hba could be NULL. In this case, do not touch
eh_sem.Fixes 88a92d6ae4fe ("scsi: ufs: Serialize eh_work with system PM events and async scan")Signed-off-by Can Guo <[email protected]>drivers/scsi/ufs/ufshcd.c +9
-1@@ -94,6 +94,8 @@ 16, 4, buf, __len, false); \ }while (0 ) +static bool early_suspend; +int ufshcd_dump_regs (struct ufs_hba *hba,size_t offset,size_t len,const char *prefix) {@@ -8896,8 +8898,14 @@ int ufshcd_system_suspend(struct ufs_hba *hba)int ret =0 ;ktime_t start =ktime_get (); +if (!hba) { + early_suspend = true; +return 0 ; + } +down (&hba->eh_sem); -if (!hba || !hba->is_powered) + +if (!hba->is_powered)return 0 ; if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) ==@@ -8945,9 +8953,12 @@ int ufshcd_system_resume(struct ufs_hba *hba)int ret =0 ;ktime_t start =ktime_get ();
Quoted 14 lines
drivers/scsi/ufs/ufshcd.c int ufshcd_system_resume(struct ufs_hba *hba) · line 8962
Quoted 9 lines
drivers/scsi/ufs/ufshcd.c +5
-2-if (!hba) { -up (&hba->eh_sem); +if (!hba)return -EINVAL ; + +if (unlikely (early_suspend)) { + early_suspend = false; +down (&hba->eh_sem); }I guess
early_suspendhere is to handle the case that hba is null duringufshcd_system_suspend()but !null duringufshcd_system_resume(). If yes, would it be possible? If no, may I know what is the purpose?
Yes, you are right. I think it is possible. platform_set_drvdata() is called in ufshcd_pltfrm_init(). Say suspend happens before platform_set_drvdata() is called, but resume comes back after platform_set_drvdata() is called. What do you think?
Thanks,
Can Guo.
Quoted 5 lines
Thanks a lot.
Stanley ChuQuoted 2 lines
drivers/scsi/ufs/ufshcd.c +0
-0if (!hba->is_powered ||pm_runtime_suspended (hba->dev))
Stanley Chu
Acked-byRe: [PATCH 1/2] scsi: ufs: Fix a possible NULL pointer issue
Hi Can,
Quoted 63 lines · Can Guo
Quoted 45 lines · Stanley Chu
Hi Can,
Quoted 42 lines · Can Guo
During system resume/suspend, hba could be NULL. In this case, do not touch
eh_sem.Fixes 88a92d6ae4fe ("scsi: ufs: Serialize eh_work with system PMevents and async scan")
Signed-off-by Can Guo <[email protected]>drivers/scsi/ufs/ufshcd.c +9
-1@@ -94,6 +94,8 @@ 16, 4, buf, __len, false); \ }while (0 ) +static bool early_suspend; +int ufshcd_dump_regs (struct ufs_hba *hba,size_t offset,size_t len,const char *prefix) {@@ -8896,8 +8898,14 @@ int ufshcd_system_suspend(struct ufs_hba *hba)int ret =0 ;ktime_t start =ktime_get (); +if (!hba) { + early_suspend = true; +return 0 ; + } +down (&hba->eh_sem); -if (!hba || !hba->is_powered) + +if (!hba->is_powered)return 0 ; if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) ==@@ -8945,9 +8953,12 @@ int ufshcd_system_resume(struct ufs_hba *hba)int ret =0 ;ktime_t start =ktime_get ();Quoted 16 lines
drivers/scsi/ufs/ufshcd.c int ufshcd_system_resume(struct ufs_hba *hba) · line 8962
Quoted 9 lines
drivers/scsi/ufs/ufshcd.c +5
-2-if (!hba) { -up (&hba->eh_sem); +if (!hba)return -EINVAL ; + +if (unlikely (early_suspend)) { + early_suspend = false; +down (&hba->eh_sem); }I guess
early_suspendhere is to handle the case that hba is null duringufshcd_system_suspend()but !null duringufshcd_system_resume(). If yes,
would it be possible? If no, may I know what is the purpose?
Quoted 4 lines
Yes, you are right. I think it is possible.
platform_set_drvdata()is called inufshcd_pltfrm_init(). Say suspend happens beforeplatform_set_drvdata()is called, but resume comes back afterplatform_set_drvdata()is called. What do you think?
Thanks for remind. After looking into system suspend flow, kernel thread may continue running even after UFS suspend callback is executed by suspend flow.
Feel free to add
PATCH v3 2/2 scsi: ufs: Protect PM ops and err_handler from user access through sysfs
In reply to Can GuoUser layer may access sysfs nodes when system PM ops or error handling is running, which can cause various problems. Rename eh_sem to host_sem and use it to protect PM ops and error handling from user layer intervene.
drivers/scsi/ufs/ufs-sysfs.c +89 -15
@@ -154,18 +154,29 @@ 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 ; +down (&hba->host_sem); +if (!ufshcd_is_sysfs_allowed (hba)) { + ret = -EBUSY ; + goto out; + } +pm_runtime_get_sync (hba->dev);ufshcd_hold (hba, false); ahit =ufshcd_readl (hba,REG_AUTO_HIBERNATE_IDLE_TIMER );ufshcd_release (hba);pm_runtime_put_sync (hba->dev); -return scnprintf (buf,PAGE_SIZE ,"%d\n" ,ufshcd_ahit_to_us (ahit)); + ret =scnprintf (buf,PAGE_SIZE ,"%d\n" ,ufshcd_ahit_to_us (ahit)); + +out: +up (&hba->host_sem); +return ret; } static ssize_t auto_hibern8_store(struct device *dev,
@@ -174,6 +185,7 @@ static ssize_t auto_hibern8_store(struct device *dev, {struct ufs_hba *hba =dev_get_drvdata (dev);unsigned int timer; +int ret =0 ;if (!ufshcd_is_auto_hibern8_supported (hba))return -EOPNOTSUPP ;
@@ -184,9 +196,17 @@ static ssize_t auto_hibern8_store(struct device *dev,if (timer >UFSHCI_AHIBERN8_MAX )return -EINVAL ; +down (&hba->host_sem); +if (!ufshcd_is_sysfs_allowed (hba)) { + ret = -EBUSY ; + goto out; + } +ufshcd_auto_hibern8_update (hba,ufshcd_us_to_ahit (timer)); -return count; +out: +up (&hba->host_sem); +return ret ? ret : count; }static DEVICE_ATTR_RW (rpm_lvl);
@@ -225,12 +245,21 @@ static ssize_t ufs_sysfs_read_desc_param(struct ufs_hba *hba,if (param_size >8 )return -EINVAL ; +down (&hba->host_sem); +if (!ufshcd_is_sysfs_allowed (hba)) { + ret = -EBUSY ; + goto out; + } +pm_runtime_get_sync (hba->dev); ret =ufshcd_read_desc_param (hba, desc_id, desc_index, param_offset, desc_buf, param_size);pm_runtime_put_sync (hba->dev); -if (ret) -return -EINVAL ; +if (ret) { + ret = -EINVAL ; + goto out; + } +switch (param_size) {case 1 : ret =sprintf (sysfs_buf,"0x%02X\n" , *desc_buf);
@@ -249,6 +278,8 @@ static ssize_t ufs_sysfs_read_desc_param(struct ufs_hba *hba,break ; } +out: +up (&hba->host_sem);return ret; }
@@ -591,9 +622,16 @@ static ssize_t _name##_show(struct device *dev, \int desc_len =QUERY_DESC_MAX_SIZE ; \u8 *desc_buf; \ \ +down (&hba->host_sem); \ +if (!ufshcd_is_sysfs_allowed (hba)) { \ +up (&hba->host_sem); \ +return -EBUSY ; \ + } \ desc_buf =kzalloc (QUERY_DESC_MAX_SIZE ,GFP_ATOMIC ); \ -if (!desc_buf) \ -return -ENOMEM ; \ +if (!desc_buf) { \ +up (&hba->host_sem); \ +return -ENOMEM ; \ + } \pm_runtime_get_sync (hba->dev); \ ret = ufshcd_query_descriptor_retry(hba, \ UPIU_QUERY_OPCODE_READ_DESC, QUERY_DESC_IDN_DEVICE, \
@@ -613,6 +651,7 @@ static ssize_t _name##_show(struct device *dev, \ out: \pm_runtime_put_sync (hba->dev); \kfree (desc_buf); \ +up (&hba->host_sem); \return ret; \ } \static DEVICE_ATTR_RO (_name)
@@ -651,15 +690,26 @@ static ssize_t _name##_show(struct device *dev, \u8 index =0 ; \int ret; \struct ufs_hba *hba =dev_get_drvdata (dev); \ + \ +down (&hba->host_sem); \ +if (!ufshcd_is_sysfs_allowed (hba)) { \ +up (&hba->host_sem); \ +return -EBUSY ; \ + } \ if (ufshcd_is_wb_flags(QUERY_FLAG_IDN##_uname)) \ index =ufshcd_wb_get_query_index (hba); \pm_runtime_get_sync (hba->dev); \ ret =ufshcd_query_flag (hba,UPIU_QUERY_OPCODE_READ_FLAG , \ QUERY_FLAG_IDN##_uname, index, &flag); \pm_runtime_put_sync (hba->dev); \ -if (ret) \ -return -EINVAL ; \ -return sprintf (buf,"%s\n" , flag ?"true" :"false" ); \ +if (ret) { \ + ret = -EINVAL ; \ + goto out; \ + } \ + ret =sprintf (buf,"%s\n" , flag ?"true" :"false" ); \ +out: \ +up (&hba->host_sem); \ +return ret; \ } \static DEVICE_ATTR_RO (_name)
@@ -709,15 +759,26 @@ static ssize_t _name##_show(struct device *dev, \u32 value; \int ret; \u8 index =0 ; \ + \ +down (&hba->host_sem); \ +if (!ufshcd_is_sysfs_allowed (hba)) { \ +up (&hba->host_sem); \ +return -EBUSY ; \ + } \ if (ufshcd_is_wb_attrs(QUERY_ATTR_IDN##_uname)) \ index =ufshcd_wb_get_query_index (hba); \pm_runtime_get_sync (hba->dev); \ ret =ufshcd_query_attr (hba,UPIU_QUERY_OPCODE_READ_ATTR , \ QUERY_ATTR_IDN##_uname, index, 0, &value); \pm_runtime_put_sync (hba->dev); \ -if (ret) \ -return -EINVAL ; \ -return sprintf (buf,"0x%08X\n" , value); \ +if (ret) { \ + ret = -EINVAL ; \ + goto out; \ + } \ + ret =sprintf (buf,"0x%08X\n" , value); \ +out: \ +up (&hba->host_sem); \ +return ret; \ } \static DEVICE_ATTR_RO (_name)
@@ -850,13 +911,26 @@ static ssize_t dyn_cap_needed_attribute_show(struct device *dev,u8 lun =ufshcd_scsi_to_upiu_lun (sdev->lun);int ret; +down (&hba->host_sem); +if (!ufshcd_is_sysfs_allowed (hba)) { + ret = -EBUSY ; + goto out; + } +pm_runtime_get_sync (hba->dev); 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); -if (ret) -return -EINVAL ; -return sprintf (buf,"0x%08X\n" , value); +if (ret) { + ret = -EINVAL ; + goto out; + } + + ret =sprintf (buf,"0x%08X\n" , value); + +out: +up (&hba->host_sem); +return ret; }static DEVICE_ATTR_RO (dyn_cap_needed_attribute);
drivers/scsi/ufs/ufshcd.c +26 -16
132 lines not on this page · raw message
drivers/scsi/ufs/ufshcd.h +9 -1
@@ -655,6 +655,8 @@ struct ufs_hba_variant_params {* @intr_mask: Interrupt Mask Bits * @ee_ctrl_mask: Exception event control mask * @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 * @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
@@ -751,7 +753,8 @@ struct ufs_hba {u32 intr_mask;u16 ee_ctrl_mask;bool is_powered; -struct semaphore eh_sem; +bool shutting_down; +struct semaphore host_sem;/* Work Queues */ struct workqueue_struct *eh_wq;
@@ -875,6 +878,11 @@ static inline bool ufshcd_is_wb_allowed(struct ufs_hba *hba)return hba->caps &UFSHCD_CAP_WB_EN ; } +static inline bool ufshcd_is_sysfs_allowed (struct ufs_hba *hba) +{ +return !hba->shutting_down; +} +#define ufshcd_writel (hba, val, reg) \ writel((val), (hba)->mmio_base + (reg))#define ufshcd_readl (hba, reg) \
Signature · 1 line
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project.
Re: [PATCH 2/2] scsi: ufs: Protect PM ops and err_handler from user access through sysfs
Quoted 1 line · Can Guo
+ * @shutting_down: flag to check if shutdown has been invoked
I am not much sure if this flag is need, since once PM going in shutdown path, what will be returnded by pm_runtime_get_sync()?
If pm_runtime_get_sync() will fail, just check its return.
Hi Rafael
would you please help us confirm this?
thanks,
Bean
Quoted 11 lines
+ * @host_sem: semaphore used to serialize concurrent contexts
* @eh_wq: Workqueue thateh_workworks on
* @eh_work: Worker to handle UFS errors that require s/w attention
* @eeh_work: Worker to handle exception eventsdrivers/scsi/ufs/ufshcd.h +2
-1@@ -751,7 +753,8 @@ struct ufs_hba {u32 intr_mask;u16 ee_ctrl_mask;bool is_powered; -struct semaphore eh_sem; +bool shutting_down; +struct semaphore host_sem;
Re: [PATCH 2/2] scsi: ufs: Protect PM ops and err_handler from user access through sysfs
Quoted 8 lines · Bean Huo
Quoted 1 line · Can Guo
+ * @shutting_down: flag to check if shutdown has been invoked
I am not much sure if this flag is need, since once PM going in shutdown path, what will be returnded by
pm_runtime_get_sync()?If
pm_runtime_get_sync()will fail, just check its return.
That depends. 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 you cannot count on it.
Check Stanley's change -
https://lore.kernel.org/patchwork/patch/1341389/
Can Guo.
Quoted 18 lines
Hi Rafael
would you please help us confirm this?thanks,
BeanQuoted 11 lines
+ * @host_sem: semaphore used to serialize concurrent contexts
* @eh_wq: Workqueue thateh_workworks on
* @eh_work: Worker to handle UFS errors that require s/w attention
* @eeh_work: Worker to handle exception eventsdrivers/scsi/ufs/ufshcd.h +2
-1@@ -751,7 +753,8 @@ struct ufs_hba {u32 intr_mask;u16 ee_ctrl_mask;bool is_powered; -struct semaphore eh_sem; +bool shutting_down; +struct semaphore host_sem;
Re: [PATCH 2/2] scsi: ufs: Protect PM ops and err_handler from user access through sysfs
Quoted 15 lines · Can Guo
Quoted 8 lines · Bean Huo
Quoted 1 line · Can Guo
+ * @shutting_down: flag to check if shutdown has been invoked
I am not much sure if this flag is need, since once PM going in shutdown path, what will be returnded by
pm_runtime_get_sync()?If
pm_runtime_get_sync()will fail, just check its return.That depends. During/after shutdown, for UFS's case only,
pm_runtime_get_sync(hba->dev)will most likely return 0, because it is alreadyRUNTIME_ACTIVE,pm_runtime_get_sync()will directly return 0... meaning you cannot count on it.
Quoted 4 lines
Check Stanley's change -
https://lore.kernel.org/patchwork/patch/1341389/Can Guo.
Can,
Thanks for pointing out that.
Based on my understanding, that patch is redundent. maybe I misundestood Linux shutdown sequence.
I checked the shutdown flow:
1. Set the "system_state" variable
2. Disable usermod to ensure that no user from userspace can start a request
3. device_shutdown()
So, userspace thread can not start a request to trigger runtime resume(pm_runtime_get_sync) after step 2.
also, no need to add that flag to checkup if shutdwon is running, maybe it is better to check variable system_state:
if (system_state == SYSTEM_POWER_OFF || system_state == SYSTEM_HALT || system_state == SYSTEM_RESTART)
//shutdown start
I still hope Rafael or someone else can confirm that if pm_runtime_get_sync() will really return ok in shutdown flow.
thanks,
Bean
Quoted 6 lines
Quoted 6 lines
Hi Rafael
would you please help us confirm this?thanks,
Bean
Re: [PATCH 2/2] scsi: ufs: Protect PM ops and err_handler from user access through sysfs
Hi Bean,
Quoted 19 lines · Bean Huo
Quoted 18 lines · Can Guo
Quoted 8 lines · Bean Huo
Quoted 1 line · Can Guo
+ * @shutting_down: flag to check if shutdown has been invoked
I am not much sure if this flag is need, since once PM going in shutdown path, what will be returnded by
pm_runtime_get_sync()?If
pm_runtime_get_sync()will fail, just check its return.That depends. During/after shutdown, for UFS's case only,
pm_runtime_get_sync(hba->dev)will most likely return 0, because it is alreadyRUNTIME_ACTIVE,pm_runtime_get_sync()will directly return 0... meaning you cannot count on it.Check Stanley's change -
https://lore.kernel.org/patchwork/patch/1341389/
Quoted 8 lines
Quoted 1 line
Can Guo.
Can,
Thanks for pointing out that.
Based on my understanding, that patch is redundent. maybe I misundestood Linux shutdown sequence.
Sorry, do you mean Stanley's change is redundant?
Quoted 6 lines
I checked the shutdown flow:
1. Set the "
system_state" variable
2. Disable usermod to ensure that no user from userspace can start a request
I hope it is like what you interpreted, but step #2 only stops UMH(#265) but not all user space activities. Whereas, UMH is for kernel space calling user space.
264 system_state = state;
265 usermodehelper_disable();
266 device_shutdown();
Thanks,
Can Guo.
Quoted 25 lines
3.
device_shutdown()
So, userspace thread can not start a request to trigger runtimeresume(pm_runtime_get_sync)after step 2.also, no need to add that flag to checkup if shutdwon is running, maybe it is better to check variable
system_state:if (
system_state==SYSTEM_POWER_OFF||system_state==SYSTEM_HALT||system_state==SYSTEM_RESTART)//shutdown startI still hope Rafael or someone else can confirm that if
pm_runtime_get_sync()will really return ok in shutdown flow.thanks,
BeanQuoted 6 lines
Quoted 6 lines
Hi Rafael
would you please help us confirm this?thanks,
Bean
Re: [PATCH 2/2] scsi: ufs: Protect PM ops and err_handler from user access through sysfs
Quoted 25 lines · Can Guo
Hi Bean,
Quoted 22 lines · Bean Huo
Quoted 20 lines · Can Guo
Quoted 9 lines · Bean Huo
Quoted 2 lines · Can Guo
+ * @shutting_down: flag to check if shutdown has been invoked
I am not much sure if this flag is need, since once PM going in shutdown path, what will be returnded by
pm_runtime_get_sync()?If
pm_runtime_get_sync()will fail, just check its return.That depends. During/after shutdown, for UFS's case only,
pm_runtime_get_sync(hba->dev)will most likely return 0, because it is alreadyRUNTIME_ACTIVE,pm_runtime_get_sync()will directly return 0... meaning you cannot count on it.Check Stanley's change -
https://lore.kernel.org/patchwork/patch/1341389/Can Guo.
Quoted 8 lines
Quoted 6 lines
Can,
Thanks for pointing out that.
Based on my understanding, that patch is redundent. maybe I misundestood Linux shutdown sequence.
Sorry, do you mean Stanley's change is redundant?
yes.
Quoted 9 lines
Quoted 8 lines
I checked the shutdown flow:
1. Set the "
system_state" variable
2. Disable usermod to ensure that no user from userspace can start a
request
Quoted 5 lines
I hope it is like what you interpreted, but step #2 only stops UMH(#265)
but not all user space activities. Whereas, UMH is for kernel space calling
user space.
Can,
I did further study and homework on the Linux shutdown in the last few days. Yes, you are right, usermodehelper_disable() is to prevent executing the process from the kernel space.
But I didn't reproduce this "maybe" race issue while shutdown. no matter how I torment my system, once Linux shutdown/halt/reboot starts, nobody can access the sysfs node. I create 10 processes in the user space and constantly access UFS sysfs node, also, fio is running in the background for the normal data read/write. there is a shutdown thread that will randomly trigger shutdown/halt/reboot. but no race issue appears.
I don't know if this is a hypothetical issue(the race between shutdown flow and sysfs node access), it may not really exist in the Linux envriroment. everytime, the shutdonw flow will be:
e10_sync_handler()->e10_svc()->do_e10_svc()->__do_sys_reboot()-
Quoted 2 lines
kernel_poweroff/kernel_halt()->device_shutdown()->platform_shutdown()- ufshcd_platform_shutdown()->ufshcd_shutdown().
I think before going into the kernel shutdown, the userspace cannot issue new requests anymore. otherwise, this would be a big issue.
pm_runtime_get_sync() will return 0 or failure while shutdown? the answer is not important now, maybe as you said, it is always 0. But in my testing, it didn't get there the system has been shutdown. Which means once shutdonw starts, sysfs node access path cannot reach pm_runtime_get_sync(). (note, I don't know if sysfs node access thread has been disabled or not)
Responsibly say, I didn't reproduce this issue on my system (ubuntu), maybe you are using Android. I am not an expert on this topic, if you have the best idea on how to reproduce this issue. please please let me try. appreciate it!!!!!
Thanks,
Bean
Quoted 7 lines
264 system_state = state; 265 usermodehelper_disable(); 266 device_shutdown();Thanks,
Can Guo.
Re: [PATCH 2/2] scsi: ufs: Protect PM ops and err_handler from user access through sysfs
Hi Bean,
Quoted 86 lines · Bean Huo
Quoted 25 lines · Can Guo
Hi Bean,
Quoted 22 lines · Bean Huo
Quoted 20 lines · Can Guo
Quoted 9 lines · Bean Huo
Quoted 2 lines · Can Guo
+ * @shutting_down: flag to check if shutdown has been invoked
I am not much sure if this flag is need, since once PM going in shutdown path, what will be returnded by
pm_runtime_get_sync()?If
pm_runtime_get_sync()will fail, just check its return.That depends. During/after shutdown, for UFS's case only,
pm_runtime_get_sync(hba->dev)will most likely return 0, because it is alreadyRUNTIME_ACTIVE,pm_runtime_get_sync()will directly return 0... meaning you cannot count on it.Check Stanley's change -
https://lore.kernel.org/patchwork/patch/1341389/Can Guo.
Quoted 8 lines
Quoted 6 lines
Can,
Thanks for pointing out that.
Based on my understanding, that patch is redundent. maybe I misundestood Linux shutdown sequence.
Sorry, do you mean Stanley's change is redundant?
yes.
Quoted 9 lines
Quoted 8 lines
I checked the shutdown flow:
1. Set the "
system_state" variable
2. Disable usermod to ensure that no user from userspace can start a
requestQuoted 5 lines
I hope it is like what you interpreted, but step #2 only stops UMH(#265)
but not all user space activities. Whereas, UMH is for kernel space calling
user space.Can,
I did further study and homework on the Linux shutdown in the last few days. Yes, you are right,
usermodehelper_disable()is to prevent executing the process from the kernel space.But I didn't reproduce this "maybe" race issue while shutdown. no matter how I torment my system, once Linux shutdown/halt/reboot starts, nobody can access the sysfs node. I create 10 processes in the user space and constantly access UFS sysfs node, also, fio is running in the background for the normal data read/write. there is a shutdown thread that will randomly trigger shutdown/halt/reboot. but no race issue appears.
I don't know if this is a hypothetical issue(the race between shutdown flow and sysfs node access), it may not really exist in the Linux envriroment. everytime, the shutdonw flow will be:
e10_sync_handler()->e10_svc()->do_e10_svc()->__do_sys_reboot()-
Quoted 2 lines
kernel_poweroff/kernel_halt()->device_shutdown()->platform_shutdown()- ufshcd_platform_shutdown()->ufshcd_shutdown().
I think before going into the kernel shutdown, the userspace cannot issue new requests anymore. otherwise, this would be a big issue.
pm_runtime_get_sync()will return 0 or failure while shutdown? the answer is not important now, maybe as you said, it is always 0. But in my testing, it didn't get there the system has been shutdown. Which means once shutdonw starts, sysfs node access path cannot reachpm_runtime_get_sync(). (note, I don't know if sysfs node access thread has been disabled or not)
Quoted 5 lines
Responsibly say, I didn't reproduce this issue on my system (ubuntu), maybe you are using Android. I am not an expert on this topic, if you have the best idea on how to reproduce this issue. please please let me try. appreciate it!!!!!
One of the racing in my platforms happens due to I/O access triggered from kernel space, not user space.
Thanks,
Stanley Chu
Quoted 13 lines
Thanks,
BeanQuoted 7 lines
264 system_state = state; 265 usermodehelper_disable(); 266 device_shutdown();Thanks,
Can Guo.
Re: [PATCH 2/2] scsi: ufs: Protect PM ops and err_handler from user access through sysfs
In reply to Bean HuoQuoted 28 lines · Bean Huo
Quoted 27 lines · Can Guo
Hi Bean,
Quoted 24 lines · Bean Huo
Quoted 16 lines · Can Guo
Quoted 9 lines · Bean Huo
Quoted 2 lines · Can Guo
+ * @shutting_down: flag to check if shutdown has been invoked
I am not much sure if this flag is need, since once PM going in shutdown path, what will be returnded by
pm_runtime_get_sync()?If
pm_runtime_get_sync()will fail, just check its return.That depends. During/after shutdown, for UFS's case only,
pm_runtime_get_sync(hba->dev)will most likely return 0, because it is alreadyRUNTIME_ACTIVE,pm_runtime_get_sync()will directly return 0... meaning you cannot count on it.Quoted 4 lines
Check Stanley's change -
https://lore.kernel.org/patchwork/patch/1341389/Can Guo.
Can,
Quoted 9 lines
Quoted 6 lines
Quoted 4 lines
Thanks for pointing out that.
Based on my understanding, that patch is redundent. maybe I misundestood Linux shutdown sequence.
Sorry, do you mean Stanley's change is redundant?
yes.
No, it is definitely needed. As Stanley replied you in another thread, it is not protecting I/Os from user layer, but from other subsystems during shutdown.
Quoted 49 lines
Quoted 9 lines
Quoted 8 lines
I checked the shutdown flow:
1. Set the "
system_state" variable
2. Disable usermod to ensure that no user from userspace can start a
requestQuoted 5 lines
I hope it is like what you interpreted, but step #2 only stops UMH(#265)
but not all user space activities. Whereas, UMH is for kernel space calling
user space.Can,
I did further study and homework on the Linux shutdown in the last few days. Yes, you are right,
usermodehelper_disable()is to prevent executing the process from the kernel space.But I didn't reproduce this "maybe" race issue while shutdown. no matter how I torment my system, once Linux shutdown/halt/reboot starts, nobody can access the sysfs node. I create 10 processes in the user space and constantly access UFS sysfs node, also, fio is running in the background for the normal data read/write. there is a shutdown thread that will randomly trigger shutdown/halt/reboot. but no race issue appears.
I don't know if this is a hypothetical issue(the race between shutdown flow and sysfs node access), it may not really exist in the Linux envriroment. everytime, the shutdonw flow will be:
e10_sync_handler()->e10_svc()->do_e10_svc()->__do_sys_reboot()-
Quoted 2 lines
kernel_poweroff/kernel_halt()->device_shutdown()->platform_shutdown()- ufshcd_platform_shutdown()->ufshcd_shutdown().
I think before going into the kernel shutdown, the userspace cannot issue new requests anymore. otherwise, this would be a big issue.
pm_runtime_get_sync()will return 0 or failure while shutdown? the answer is not important now, maybe as you said, it is always 0. But in my testing, it didn't get there the system has been shutdown. Which means once shutdonw starts, sysfs node access path cannot reachpm_runtime_get_sync(). (note, I don't know if sysfs node access thread has been disabled or not)
Quoted 5 lines
Responsibly say, I didn't reproduce this issue on my system (ubuntu), maybe you are using Android. I am not an expert on this topic, if you have the best idea on how to reproduce this issue. please please let me try. appreciate it!!!!!
When you do a reboot/shutdown/poweroff, how your system behaves highly depends on how the reboot cmd is implemented in C code under /sbin/.
On Ubuntu, reboot looks like:
$ reboot --help reboot [OPTIONS...] [ARG]
Reboot the system.
--help Show this help
--halt Halt the machine
-p --poweroff Switch off the machine
--reboot Reboot the machine
-f --force Force immediate halt/power-off/reboot -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record
-d --no-wtmp Don't write wtmp record
--no-wall Don't send wall message before halt/power-off/reboot
On a pure Linux with a initrd RAM FS built from busybox, reboot looks like:
# reboot --help BusyBox v1.30.1 (2019-05-24 12:53:36 IST) multi-call binary.
Usage: reboot [-d DELAY] [-n] [-f]
Reboot the system
-d SEC Delay interval
-n Do not sync
-f Force (don't go through init)For example, when you work on a pure Linux with a filesystem built from busybox, when you hit reboot cmd, halt_main() will be called. And based on the reboot options passed to reboot cmd, halt_main() behaves differently.
A plain reboot cmd does things like sync filesystem, send SIGKILL to all processes (except for init), remount all filesytem as read-only and so on before invoking linux kernel reboot syscall. In this case, we are safe.
However, if you do a "reboot -f", halt_main() directly invokes reboot(). And with "reboot -f", I can easily reproduce the race condition we are talking about here - it is not based on imagination.
Find the patch I used for replication in the attachment, fix conflicts if any. After boot up, the cmd lines I used are
# while true; do cat /sys/devices/platform/soc@0/*ufshc*/rpm_lvl; done & # reboot -f
Can Guo.
Quoted 12 lines
Thanks,
BeanQuoted 7 lines
264 system_state = state; 265 usermodehelper_disable(); 266 device_shutdown();Thanks,
Can Guo.
From 639358dd6e2f7e5e9f7e66b828bf043fa3c7bbf2 Mon Sep 17 00:00:00 2001
From: Can Guo <[email protected]>
Date: Sat, 9 Jan 2021 12:37:40 +0800
Subject: [PATCH] scsi: ufs: Reproduce race condition btw sysfs access and
shutdown
Reproduce race condition btw sysfs access and shutdown
Usage:
drivers/scsi/ufs/ufs-sysfs.c +2 -0
@@ -59,6 +59,8 @@ static ssize_t rpm_lvl_show(struct device *dev, {struct ufs_hba *hba =dev_get_drvdata (dev); +BUG_ON (hba->shutting_down); +return sprintf (buf,"%d\n" , hba->rpm_lvl); }
drivers/scsi/ufs/ufshcd.c +6 -0
@@ -8277,6 +8277,11 @@ int ufshcd_shutdown(struct ufs_hba *hba) {int ret =0 ; + hba->shutting_down = true; +pr_err ("[DEBUG]%s: UFS SHUTDOWN START\n" , __func__); + +usleep_range (10000000 ,11000000 ); +if (!hba->is_powered) goto out;
@@ -8294,6 +8299,7 @@ int ufshcd_shutdown(struct ufs_hba *hba)if (ret)dev_err (hba->dev,"%s failed, err %d\n" , __func__, ret);/* allow force shutdown even in case of errors */ +pr_err ("[DEBUG]%s: UFS SHUTDOWN END\n" , __func__);return 0 ; }EXPORT_SYMBOL (ufshcd_shutdown);
drivers/scsi/ufs/ufshcd.h +1 -0
@@ -677,6 +677,7 @@ struct ufs_hba {u16 ee_ctrl_mask;u16 hba_enable_delay_us;bool is_powered; +bool shutting_down;/* Work Queues */ struct work_struct eh_work;
Signature · 1 line
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project.
Re: [PATCH 2/2] scsi: ufs: Protect PM ops and err_handler from user access through sysfs
Quoted 142 lines · Can Guo
Quoted 28 lines · Bean Huo
Quoted 27 lines · Can Guo
Hi Bean,
Quoted 24 lines · Bean Huo
Quoted 16 lines · Can Guo
Quoted 9 lines · Bean Huo
Quoted 2 lines · Can Guo
+ * @shutting_down: flag to check if shutdown has been invoked
I am not much sure if this flag is need, since once PM going in shutdown path, what will be returnded by
pm_runtime_get_sync()?If
pm_runtime_get_sync()will fail, just check its return.That depends. During/after shutdown, for UFS's case only,
pm_runtime_get_sync(hba->dev)will most likely return 0, because it is alreadyRUNTIME_ACTIVE,pm_runtime_get_sync()will directly return 0... meaning you cannot count on it.Quoted 4 lines
Check Stanley's change -
https://lore.kernel.org/patchwork/patch/1341389/Can Guo.
Can,
Quoted 9 lines
Quoted 6 lines
Quoted 4 lines
Thanks for pointing out that.
Based on my understanding, that patch is redundent. maybe I misundestood Linux shutdown sequence.
Sorry, do you mean Stanley's change is redundant?
yes.
No, it is definitely needed. As Stanley replied you in another thread, it is not protecting I/Os from user layer, but from other subsystems during shutdown.
Quoted 49 lines
Quoted 9 lines
Quoted 8 lines
I checked the shutdown flow:
1. Set the "
system_state" variable
2. Disable usermod to ensure that no user from userspace can start a
requestQuoted 5 lines
I hope it is like what you interpreted, but step #2 only stops UMH(#265)
but not all user space activities. Whereas, UMH is for kernel space calling
user space.Can,
I did further study and homework on the Linux shutdown in the last few days. Yes, you are right,
usermodehelper_disable()is to prevent executing the process from the kernel space.But I didn't reproduce this "maybe" race issue while shutdown. no matter how I torment my system, once Linux shutdown/halt/reboot starts, nobody can access the sysfs node. I create 10 processes in the user space and constantly access UFS sysfs node, also, fio is running in the background for the normal data read/write. there is a shutdown thread that will randomly trigger shutdown/halt/reboot. but no race issue appears.
I don't know if this is a hypothetical issue(the race between shutdown flow and sysfs node access), it may not really exist in the Linux envriroment. everytime, the shutdonw flow will be:
e10_sync_handler()->e10_svc()->do_e10_svc()->__do_sys_reboot()-
Quoted 2 lines
kernel_poweroff/kernel_halt()->device_shutdown()->platform_shutdown()- ufshcd_platform_shutdown()->ufshcd_shutdown().
I think before going into the kernel shutdown, the userspace cannot issue new requests anymore. otherwise, this would be a big issue.
pm_runtime_get_sync()will return 0 or failure while shutdown? the answer is not important now, maybe as you said, it is always 0. But in my testing, it didn't get there the system has been shutdown. Which means once shutdonw starts, sysfs node access path cannot reachpm_runtime_get_sync(). (note, I don't know if sysfs node access thread has been disabled or not)Quoted 5 lines
Responsibly say, I didn't reproduce this issue on my system (ubuntu), maybe you are using Android. I am not an expert on this topic, if you have the best idea on how to reproduce this issue. please please let me try. appreciate it!!!!!
When you do a reboot/shutdown/poweroff, how your system behaves highly depends on how the reboot cmd is implemented in C code under
/sbin/.On Ubuntu, reboot looks like:
$ reboot --help reboot [OPTIONS...] [ARG]Reboot the system.
--help Show this help --halt Halt the machine -p --poweroff Switch off the machine --reboot Reboot the machine -f --force Force immediate halt/power-off/reboot-w --wtmp-only Don't halt/power-off/reboot, just write wtmp record
-d --no-wtmp Don't write wtmp record
--no-wall Don't send wall message before halt/power-off/rebootOn a pure Linux with a initrd RAM FS built from busybox, reboot looks like:
# reboot --help BusyBox v1.30.1 (2019-05-24 12:53:36 IST) multi-call binary.Usage: reboot [-d DELAY] [-n] [-f]
Reboot the system
-d SEC Delay interval -n Do not sync -f Force (don't go through init)For example, when you work on a pure Linux with a filesystem built from busybox, when you hit reboot cmd,
halt_main()will be called. And based on the reboot options passed to reboot cmd,halt_main()behaves differently.A plain reboot cmd does things like sync filesystem, send SIGKILL to all processes (except for init), remount all filesytem as read-only and so on before invoking linux kernel reboot syscall. In this case, we are safe.
However, if you do a "reboot -f",
halt_main()directly invokesreboot(). And with "reboot -f", I can easily reproduce the race condition we are talking about here - it is not based on imagination.
Quoted 7 lines
Find the patch I used for replication in the attachment, fix conflicts if any. After boot up, the cmd lines I used are
# while true; do cat /sys/devices/platform/soc@0/*ufshc*/rpm_lvl; done & # reboot -fCan Guo.
Oops... forgot the logs:
#
# while true; do cat /sys/devices/platform/soc@0/*ufshc*/rpm_lvl; done & 3 3 3 3 .... # reboot -f 3 3 3 .... [ 17.959206] sd 0:0:0:5: [sdf] Synchronizing SCSI cache 3 [ 17.964833] sd 0:0:0:4: [sde] Synchronizing SCSI cache [ 17.970224] sd 0:0:0:3: [sdd] Synchronizing SCSI cache [ 17.975574] sd 0:0:0:2: [sdc] Synchronizing SCSI cache 3 [ 17.981034] sd 0:0:0:1: [sdb] Synchronizing SCSI cache [ 17.986493] sd 0:0:0:0: [sda] Synchronizing SCSI cache 3 [ 17.991870] [DEBUG]ufshcd_shutdown: UFS SHUTDOWN START [ 17.998902] ------------[ cut here ]------------ [ 18.003648] kernel BUG at drivers/scsi/ufs/ufs-sysfs.c:62! [ 18.009286] Internal error: Oops - BUG: 0 [#1] PREEMPT SMP [ 18.034249] pstate: 40c00005 (nZcv daif +PAN +UAO) [ 18.039185] pc : rpm_lvl_show+0x38/0x40 [ 18.043137] lr : dev_attr_show+0x1c/0x58 [ 18.132552] Call trace: [ 18.135076] rpm_lvl_show+0x38/0x40 [ 18.138672] sysfs_kf_seq_show+0xa8/0x140 [ 18.142802] kernfs_seq_show+0x28/0x30 [ 18.146665] seq_read+0x1d8/0x4b0 [ 18.150072] kernfs_fop_read+0x12c/0x1f0 [ 18.154109] do_iter_read+0x184/0x1c0 [ 18.157882] vfs_readv+0x68/0xb0 ....
Quoted 13 lines
Quoted 13 lines
Thanks,
BeanQuoted 7 lines
264 system_state = state; 265 usermodehelper_disable(); 266 device_shutdown();Thanks,
Can Guo.
Re: [PATCH 2/2] scsi: ufs: Protect PM ops and err_handler from user access through sysfs
Quoted 225 lines · Can Guo
Quoted 185 lines · Can Guo
Quoted 31 lines · Bean Huo
Quoted 30 lines · Can Guo
Hi Bean,
Quoted 27 lines · Bean Huo
Quoted 19 lines · Can Guo
Quoted 4 lines · Bean Huo
Quoted 2 lines · Can Guo
+ * @shutting_down: flag to check if shutdown has been invoked
Quoted 8 lines
I am not much sure if this flag is need, since once PM going in
shutdown path, what will be returnded bypm_runtime_get_sync()?If
pm_runtime_get_sync()will fail, just check its return.That depends. During/after shutdown, for UFS's case only,
pm_runtime_get_sync(hba->dev)will most likely return 0, because it is alreadyRUNTIME_ACTIVE,pm_runtime_get_sync()will directly return 0... meaning you cannot count on it.Quoted 4 lines
Check Stanley's change -
https://lore.kernel.org/patchwork/patch/1341389/Can Guo.
Can,
Quoted 9 lines
Quoted 6 lines
Quoted 4 lines
Thanks for pointing out that.
Based on my understanding, that patch is redundent. maybe I misundestood Linux shutdown sequence.
Sorry, do you mean Stanley's change is redundant?
yes.
No, it is definitely needed. As Stanley replied you in another thread, it is not protecting I/Os from user layer, but from other subsystems during shutdown.
Quoted 65 lines
Quoted 10 lines
Quoted 9 lines
I checked the shutdown flow:
1. Set the "
system_state" variable
2. Disable usermod to ensure that no user from userspace can start
a
requestQuoted 6 lines
I hope it is like what you interpreted, but step #2 only stops UMH(#265)
but not all user space activities. Whereas, UMH is for kernel space
calling
user space.Can,
I did further study and homework on the Linux shutdown in the last few
days. Yes, you are right,usermodehelper_disable()is to prevent executing the process from the kernel space.But I didn't reproduce this "maybe" race issue while shutdown. no matter how I torment my system, once Linux shutdown/halt/reboot starts,
nobody can access the sysfs node. I create 10 processes in the user
space and constantly access UFS sysfs node, also, fio is running in
the
background for the normal data read/write. there is a shutdown thread
that will randomly trigger shutdown/halt/reboot. but no race issue
appears.I don't know if this is a hypothetical issue(the race between shutdown
flow and sysfs node access), it may not really exist in the Linux envriroment. everytime, the shutdonw flow will be:e10_sync_handler()->e10_svc()->do_e10_svc()->__do_sys_reboot()-
Quoted 3 lines
kernel_poweroff/kernel_halt()->device_shutdown()-
Quoted 1 line
platform_shutdown()-
ufshcd_platform_shutdown()->ufshcd_shutdown().
I think before going into the kernel shutdown, the userspace cannot
issue new requests anymore. otherwise, this would be a big issue.
pm_runtime_get_sync()will return 0 or failure while shutdown? the
answer is not important now, maybe as you said, it is always 0. But in
my testing, it didn't get there the system has been shutdown. Which
means once shutdonw starts, sysfs node access path cannot reachpm_runtime_get_sync(). (note, I don't know if sysfs node access thread
has been disabled or not)Quoted 9 lines
Responsibly say, I didn't reproduce this issue on my system (ubuntu),
maybe you are using Android. I am not an expert on this topic, if you
have the best idea on how to reproduce this issue. please please let
me
try. appreciate it!!!!!When you do a reboot/shutdown/poweroff, how your system behaves highly
depends on how the reboot cmd is implemented in C code under/sbin/.On Ubuntu, reboot looks like:
$ reboot --help reboot [OPTIONS...] [ARG]Reboot the system.
--help Show this help --halt Halt the machine -p --poweroff Switch off the machine --reboot Reboot the machine -f --force Force immediate halt/power-off/reboot-w --wtmp-only Don't halt/power-off/reboot, just write wtmp
record
-d --no-wtmp Don't write wtmp record
--no-wall Don't send wall message before halt/power-
off/rebootOn a pure Linux with a initrd RAM FS built from busybox, reboot looks
like:# reboot --help BusyBox v1.30.1 (2019-05-24 12:53:36 IST) multi-call binary.Usage: reboot [-d DELAY] [-n] [-f]
Reboot the system
-d SEC Delay interval -n Do not sync -f Force (don't go through init)For example, when you work on a pure Linux with a filesystem built from
busybox, when you hit reboot cmd,halt_main()will be called. And based
on the reboot options passed to reboot cmd,halt_main()behaves differently.A plain reboot cmd does things like sync filesystem, send SIGKILL to
all
processes (except for init), remount all filesytem as read-only and so
on
before invoking linux kernel reboot syscall. In this case, we are safe.However, if you do a "reboot -f",
halt_main()directly invokesreboot().
And with "reboot -f", I can easily reproduce the race condition we are
talking about here - it is not based on imagination.Find the patch I used for replication in the attachment, fix conflicts
if any. After boot up, the cmd lines I used areQuoted 6 lines
# while true; do cat /sys/devices/platform/soc@0/*ufshc*/rpm_lvl; done & # reboot -fCan Guo.
Oops... forgot the logs:
#
# while true; do cat /sys/devices/platform/soc@0/*ufshc*/rpm_lvl; done & 3 3 3 3 .... # reboot -f 3 3 3 .... [ 17.959206] sd 0:0:0:5: [sdf] Synchronizing SCSI cache 3 [ 17.964833] sd 0:0:0:4: [sde] Synchronizing SCSI cache [ 17.970224] sd 0:0:0:3: [sdd] Synchronizing SCSI cache [ 17.975574] sd 0:0:0:2: [sdc] Synchronizing SCSI cache 3 [ 17.981034] sd 0:0:0:1: [sdb] Synchronizing SCSI cache [ 17.986493] sd 0:0:0:0: [sda] Synchronizing SCSI cache 3 [ 17.991870] [DEBUG]ufshcd_shutdown: UFS SHUTDOWN START [ 17.998902] ------------[ cut here ]------------ [ 18.003648] kernel BUG at drivers/scsi/ufs/ufs-sysfs.c:62! [ 18.009286] Internal error: Oops - BUG: 0 [#1] PREEMPT SMP [ 18.034249] pstate: 40c00005 (nZcv daif +PAN +UAO) [ 18.039185] pc : rpm_lvl_show+0x38/0x40 [ 18.043137] lr : dev_attr_show+0x1c/0x58 [ 18.132552] Call trace:
Quoted 8 lines
[ 18.135076] rpm_lvl_show+0x38/0x40 [ 18.138672] sysfs_kf_seq_show+0xa8/0x140 [ 18.142802] kernfs_seq_show+0x28/0x30 [ 18.146665] seq_read+0x1d8/0x4b0 [ 18.150072] kernfs_fop_read+0x12c/0x1f0 [ 18.154109] do_iter_read+0x184/0x1c0 [ 18.157882] vfs_readv+0x68/0xb0 ....
Hi Can
Please forgive me for being verbose.
The above BUG log is from your BUG_ON() called, not becuase of the race betwen shutdown flow and sysfs node access(if I misunderstood, correct me). But it tells that the user can still access UFS by sysfs node in the "reboot -f" flow since it skips the actual shutdown process, " --force" is not a safe way anyway.
If accessing sysfs nodes, which triggers a UFS UPIU request to read/write UFS device descriptors during shutdown flow, there is only one issue that sysfs node access failure since UFS device and LINK has been shutdown. Strictly speaking, the failure comes after ufshcd_set_dev_pwr_mode().
__ufshcd_query_descriptor: opcode 0x01 for idn 0 failed, index 0,
err = -11
Since the shutdown is oneway process, this failure is not big issue. If you meant to avoid this failure for unsafe shutdown, I agree with you, But for the race issue, I don't know.
Bean
Re: [PATCH 2/2] scsi: ufs: Protect PM ops and err_handler from user access through sysfs
Quoted 235 lines · Bean Huo
Quoted 225 lines · Can Guo
Quoted 185 lines · Can Guo
Quoted 31 lines · Bean Huo
Quoted 30 lines · Can Guo
Hi Bean,
Quoted 27 lines · Bean Huo
Quoted 19 lines · Can Guo
Quoted 4 lines · Bean Huo
Quoted 2 lines · Can Guo
+ * @shutting_down: flag to check if shutdown has been invoked
Quoted 8 lines
I am not much sure if this flag is need, since once PM going in
shutdown path, what will be returnded bypm_runtime_get_sync()?If
pm_runtime_get_sync()will fail, just check its return.That depends. During/after shutdown, for UFS's case only,
pm_runtime_get_sync(hba->dev)will most likely return 0, because it is alreadyRUNTIME_ACTIVE,pm_runtime_get_sync()will directly return 0... meaning you cannot count on it.Quoted 4 lines
Check Stanley's change -
https://lore.kernel.org/patchwork/patch/1341389/Can Guo.
Can,
Quoted 9 lines
Quoted 6 lines
Quoted 4 lines
Thanks for pointing out that.
Based on my understanding, that patch is redundent. maybe I misundestood Linux shutdown sequence.
Sorry, do you mean Stanley's change is redundant?
yes.
No, it is definitely needed. As Stanley replied you in another thread, it is not protecting I/Os from user layer, but from other subsystems during shutdown.
Quoted 65 lines
Quoted 10 lines
Quoted 9 lines
I checked the shutdown flow:
1. Set the "
system_state" variable
2. Disable usermod to ensure that no user from userspace can start
a
requestQuoted 6 lines
I hope it is like what you interpreted, but step #2 only stops UMH(#265)
but not all user space activities. Whereas, UMH is for kernel space
calling
user space.Can,
I did further study and homework on the Linux shutdown in the last few
days. Yes, you are right,usermodehelper_disable()is to prevent executing the process from the kernel space.But I didn't reproduce this "maybe" race issue while shutdown. no matter how I torment my system, once Linux shutdown/halt/reboot starts,
nobody can access the sysfs node. I create 10 processes in the user
space and constantly access UFS sysfs node, also, fio is running in
the
background for the normal data read/write. there is a shutdown thread
that will randomly trigger shutdown/halt/reboot. but no race issue
appears.I don't know if this is a hypothetical issue(the race between shutdown
flow and sysfs node access), it may not really exist in the Linux envriroment. everytime, the shutdonw flow will be:e10_sync_handler()->e10_svc()->do_e10_svc()->__do_sys_reboot()-
Quoted 3 lines
kernel_poweroff/kernel_halt()->device_shutdown()-
Quoted 1 line
platform_shutdown()-
ufshcd_platform_shutdown()->ufshcd_shutdown().
I think before going into the kernel shutdown, the userspace cannot
issue new requests anymore. otherwise, this would be a big issue.
pm_runtime_get_sync()will return 0 or failure while shutdown? the
answer is not important now, maybe as you said, it is always 0. But in
my testing, it didn't get there the system has been shutdown. Which
means once shutdonw starts, sysfs node access path cannot reachpm_runtime_get_sync(). (note, I don't know if sysfs node access thread
has been disabled or not)Quoted 9 lines
Responsibly say, I didn't reproduce this issue on my system (ubuntu),
maybe you are using Android. I am not an expert on this topic, if you
have the best idea on how to reproduce this issue. please please let
me
try. appreciate it!!!!!When you do a reboot/shutdown/poweroff, how your system behaves highly
depends on how the reboot cmd is implemented in C code under/sbin/.On Ubuntu, reboot looks like:
$ reboot --help reboot [OPTIONS...] [ARG]Reboot the system.
--help Show this help --halt Halt the machine -p --poweroff Switch off the machine --reboot Reboot the machine -f --force Force immediate halt/power-off/reboot-w --wtmp-only Don't halt/power-off/reboot, just write wtmp
record
-d --no-wtmp Don't write wtmp record
--no-wall Don't send wall message before halt/power-
off/rebootOn a pure Linux with a initrd RAM FS built from busybox, reboot looks
like:# reboot --help BusyBox v1.30.1 (2019-05-24 12:53:36 IST) multi-call binary.Usage: reboot [-d DELAY] [-n] [-f]
Reboot the system
-d SEC Delay interval -n Do not sync -f Force (don't go through init)For example, when you work on a pure Linux with a filesystem built from
busybox, when you hit reboot cmd,halt_main()will be called. And based
on the reboot options passed to reboot cmd,halt_main()behaves differently.A plain reboot cmd does things like sync filesystem, send SIGKILL to
all
processes (except for init), remount all filesytem as read-only and so
on
before invoking linux kernel reboot syscall. In this case, we are safe.However, if you do a "reboot -f",
halt_main()directly invokesreboot().
And with "reboot -f", I can easily reproduce the race condition we are
talking about here - it is not based on imagination.Find the patch I used for replication in the attachment, fix conflicts
if any. After boot up, the cmd lines I used areQuoted 6 lines
# while true; do cat /sys/devices/platform/soc@0/*ufshc*/rpm_lvl; done & # reboot -fCan Guo.
Oops... forgot the logs:
#
# while true; do cat /sys/devices/platform/soc@0/*ufshc*/rpm_lvl; done & 3 3 3 3 .... # reboot -f 3 3 3 .... [ 17.959206] sd 0:0:0:5: [sdf] Synchronizing SCSI cache 3 [ 17.964833] sd 0:0:0:4: [sde] Synchronizing SCSI cache [ 17.970224] sd 0:0:0:3: [sdd] Synchronizing SCSI cache [ 17.975574] sd 0:0:0:2: [sdc] Synchronizing SCSI cache 3 [ 17.981034] sd 0:0:0:1: [sdb] Synchronizing SCSI cache [ 17.986493] sd 0:0:0:0: [sda] Synchronizing SCSI cache 3 [ 17.991870] [DEBUG]ufshcd_shutdown: UFS SHUTDOWN START [ 17.998902] ------------[ cut here ]------------ [ 18.003648] kernel BUG at drivers/scsi/ufs/ufs-sysfs.c:62! [ 18.009286] Internal error: Oops - BUG: 0 [#1] PREEMPT SMP [ 18.034249] pstate: 40c00005 (nZcv daif +PAN +UAO) [ 18.039185] pc : rpm_lvl_show+0x38/0x40 [ 18.043137] lr : dev_attr_show+0x1c/0x58 [ 18.132552] Call trace:Quoted 8 lines
[ 18.135076] rpm_lvl_show+0x38/0x40 [ 18.138672] sysfs_kf_seq_show+0xa8/0x140 [ 18.142802] kernfs_seq_show+0x28/0x30 [ 18.146665] seq_read+0x1d8/0x4b0 [ 18.150072] kernfs_fop_read+0x12c/0x1f0 [ 18.154109] do_iter_read+0x184/0x1c0 [ 18.157882] vfs_readv+0x68/0xb0 ....
Quoted 9 lines
Hi Can
Please forgive me for being verbose.The above BUG log is from your
BUG_ON()called, not becuase of the race betwen shutdown flow and sysfs nodeaccess(if I misunderstood, correct me). But it tells that the user can still access UFS by sysfs node in the "reboot -f" flow since it skips the actual shutdown process, " --force" is not a safe way anyway.
Hi Bean,
Yes, you misunderstood it. I am just giving you an example since rpm_lvl is the first one shows up in the code. I am showing you that it can happen to any other UFS sysfs nodes. When the BUG_ON I added is triggered, it means that we run into race condition - sysfs node is trying to turn on the host to do something while ufshcd_shutdown() is trying to shut down.
Quotes from Wikipedia:
"A race condition arises in software when a computer program, to operate properly, depends on the sequence or timing of the program's processes or threads. Critical race conditions cause invalid execution and software bugs."
If you are saying race condition is not the right word being used in this patch, please advise a better one.
Quoted 9 lines
If accessing sysfs nodes, which triggers a UFS UPIU request to read/write UFS device descriptors during shutdown flow, there is only one issue that sysfs node access failure since UFS device and LINK has been shutdown. Strictly speaking, the failure comes after
ufshcd_set_dev_pwr_mode().__ufshcd_query_descriptor: opcode 0x01 for idn 0 failed, index 0,err = -11
You misunderstood it again. You are expecting a simple query cmd error. But what really matters are NoC issues[1] and OCP[2]. And while/after UFS shutting down, either of them may happen.
[1] When a un-clocked register access issue happens, we call it a NoC issue, meaning you are tring to access a register when clocks are disabled. This leads to system CRASH.
[2] OCP is over current protection. While UFS shutting down, you may have put UFS regulators to LPM. After that, if you are still trying to talk to UFS, OCP can happen on VCCQ/VCCQ2. This leads to system CRASH too.
Quoted 5 lines
Since the shutdown is oneway process, this failure is not big issue. If you meant to avoid this failure for unsafe shutdown, I agree with you, But for the race issue, I don't know.
Easy for you to say. System crash is a big issue to any SoC vendors I belive.
Can Guo.
Quoted 1 line
Bean
Re: [PATCH 2/2] scsi: ufs: Protect PM ops and err_handler from user access through sysfs
Quoted 20 lines · Can Guo
Quoted 9 lines
If accessing sysfs nodes, which triggers a UFS UPIU request to read/write UFS device descriptors during shutdown flow, there is only
one issue that sysfs node access failure since UFS device and LINK has
been shutdown. Strictly speaking, the failure comes afterufshcd_set_dev_pwr_mode().Quoted 3 lines
__ufshcd_query_descriptor: opcode 0x01 for idn 0 failed, index0,
err = -11You misunderstood it again. You are expecting a simple query cmd error.
But what really matters are NoC issues[1] and OCP[2]. And while/after
UFS
shutting down, either of them may happen.
Quoted 7 lines
[1] When a un-clocked register access issue happens, we call it a NoC
issue,
meaning you are tring to access a register when clocks are disabled. This
leads to system CRASH.
OK, let it simple, share this kind of crash log becuase of access sysfs node in the shutdown flow.
Quoted 6 lines
[2] OCP is over current protection. While UFS shutting down, you may have put UFS regulators to LPM. After that, if you are still trying to
talk to UFS, OCP can happen on VCCQ/VCCQ2. This leads to system CRASH
too.
the same as above, share the crash log.
Quoted 9 lines
Quoted 8 lines
Since the shutdown is oneway process, this failure is not big issue. If
you meant to avoid this failure for unsafe shutdown, I agree with you,
But for the race issue, I don't know.
Quoted 4 lines
Easy for you to say. System crash is a big issue to any SoC vendors I
belive.
indeed, crash is serious issue, share the log.
Thanks,
Bean
Quoted 1 line
Can Guo.
Re: [PATCH 2/2] scsi: ufs: Protect PM ops and err_handler from user access through sysfs
Quoted 33 lines · Bean Huo
Quoted 20 lines · Can Guo
Quoted 9 lines
If accessing sysfs nodes, which triggers a UFS UPIU request to read/write UFS device descriptors during shutdown flow, there is only
one issue that sysfs node access failure since UFS device and LINK has
been shutdown. Strictly speaking, the failure comes afterufshcd_set_dev_pwr_mode().Quoted 3 lines
__ufshcd_query_descriptor: opcode 0x01 for idn 0 failed, index0,
err = -11You misunderstood it again. You are expecting a simple query cmd error.
But what really matters are NoC issues[1] and OCP[2]. And while/after
UFS
shutting down, either of them may happen.Quoted 7 lines
[1] When a un-clocked register access issue happens, we call it a NoC
issue,
meaning you are tring to access a register when clocks are disabled. This
leads to system CRASH.OK, let it simple, share this kind of crash log becuase of access sysfs node in the shutdown flow.
Quoted 9 lines
Quoted 6 lines
[2] OCP is over current protection. While UFS shutting down, you may have put UFS regulators to LPM. After that, if you are still trying to
talk to UFS, OCP can happen on VCCQ/VCCQ2. This leads to system CRASH
too.the same as above, share the crash log.
If you have hand-on experiences on NoC and/or OCP issues, you won't ask for the crash log. The tricky parts about critial NoC and OCP issues is that they don't print much logs (maybe no logs at all) in uart, which is why they are hard to debug and why I add another flag to help debug.
Take OCP as an example, when OCP happens on VCCQ/VCCQ2, PMIC will do a hard reset and put the system to crash dump mode (this is a general design in our mutual customers, but it may vary platform by platform).
And if you have a crash dump tool to collect the dump, after the dump is parsed, the best part which you can count on is the last callstacks and related flags, variables in hba. The callstack is pretty much same with the one I shared with my debug patch applied during the weekend.
Stanley can correct me if I am wrong.
Thanks,
Can Guo.
Quoted 22 lines
Quoted 9 lines
Quoted 8 lines
Since the shutdown is oneway process, this failure is not big issue. If
you meant to avoid this failure for unsafe shutdown, I agree with you,
But for the race issue, I don't know.Quoted 4 lines
Easy for you to say. System crash is a big issue to any SoC vendors I
belive.indeed, crash is serious issue, share the log.
Thanks,
BeanQuoted 1 line
Can Guo.
Re: [PATCH 2/2] scsi: ufs: Protect PM ops and err_handler from user access through sysfs
Quoted 20 lines · Can Guo
Quoted 20 lines
Quoted 5 lines
meaning you are tring to access a register when clocks are disabled.
This
leads to system CRASH.OK, let it simple, share this kind of crash log becuase of access sysfs
node in the shutdown flow.Quoted 8 lines
[2] OCP is over current protection. While UFS shutting down, you may
have put UFS regulators to LPM. After that, if you are still trying
to
talk to UFS, OCP can happen on VCCQ/VCCQ2. This leads to system CRASH
too.
Quoted 7 lines
Quoted 2 lines
the same as above, share the crash log.
If you have hand-on experiences on NoC and/or OCP issues, you won't ask
for the crash log. The tricky parts about critial NoC and OCP issues is
OK, interesting. would you tell me which register access node in ufs- sysfs.c can trigger this crash? let me verify your statement.
Bean
Quoted 1 line
Re: [PATCH 2/2] scsi: ufs: Protect PM ops and err_handler from user access through sysfs
Quoted 24 lines · Bean Huo
Quoted 23 lines · Can Guo
Quoted 22 lines
Quoted 5 lines
meaning you are tring to access a register when clocks are disabled.
This
leads to system CRASH.OK, let it simple, share this kind of crash log becuase of access sysfs
node in the shutdown flow.Quoted 8 lines
[2] OCP is over current protection. While UFS shutting down, you may
have put UFS regulators to LPM. After that, if you are still trying
to
talk to UFS, OCP can happen on VCCQ/VCCQ2. This leads to system CRASH
too.the same as above, share the crash log.
Quoted 9 lines
Quoted 4 lines
If you have hand-on experiences on NoC and/or OCP issues, you won't ask
for the crash log. The tricky parts about critial NoC and OCP issues isOK, interesting. would you tell me which register access node in ufs-
sysfs.ccan trigger this crash? let me verify your statement.
I believe I have explained enough to prove we need this change.
If you are really interested in NoC and OCP, feel free to ping me on teams, I will show you how to trigger one and what is it like on my setup.
Can Guo.
Quoted 3 lines
Bean
Quoted 1 line
Re: [PATCH 2/2] scsi: ufs: Protect PM ops and err_handler from user access through sysfs
Quoted 22 lines · Can Guo
Quoted 16 lines
Quoted 9 lines
Quoted 8 lines
Quoted 5 lines
to
talk to UFS, OCP can happen on VCCQ/VCCQ2. This leads to system
CRASH
too.the same as above, share the crash log.
Quoted 6 lines
If you have hand-on experiences on NoC and/or OCP issues, you won't
ask
for the crash log. The tricky parts about critial NoC and OCP issues
isQuoted 5 lines
OK, interesting. would you tell me which register access node in ufs-
sysfs.ccan trigger this crash? let me verify your statement.
Quoted 7 lines
I believe I have explained enough to prove we need this change.
If you are really interested in NoC and OCP, feel free to ping me on teams, I will show you how to trigger one and what is it like on my setup.
Can Guo.
Ok, I no meant to stop or slow down your patch merging, meant to avoid redundant instruction cycles.
Avri and Stanley have been convinced. let it go at that and merge.
Thanks,
Bean
Re: [PATCH 2/2] scsi: ufs: Protect PM ops and err_handler from user access through sysfs
In reply to Bean HuoQuoted 235 lines · Bean Huo
Quoted 225 lines · Can Guo
Quoted 185 lines · Can Guo
Quoted 31 lines · Bean Huo
Quoted 30 lines · Can Guo
Hi Bean,
Quoted 27 lines · Bean Huo
Quoted 19 lines · Can Guo
Quoted 4 lines · Bean Huo
Quoted 2 lines · Can Guo
+ * @shutting_down: flag to check if shutdown has been invoked
Quoted 8 lines
I am not much sure if this flag is need, since once PM going in
shutdown path, what will be returnded bypm_runtime_get_sync()?If
pm_runtime_get_sync()will fail, just check its return.That depends. During/after shutdown, for UFS's case only,
pm_runtime_get_sync(hba->dev)will most likely return 0, because it is alreadyRUNTIME_ACTIVE,pm_runtime_get_sync()will directly return 0... meaning you cannot count on it.Quoted 4 lines
Check Stanley's change -
https://lore.kernel.org/patchwork/patch/1341389/Can Guo.
Can,
Quoted 9 lines
Quoted 6 lines
Quoted 4 lines
Thanks for pointing out that.
Based on my understanding, that patch is redundent. maybe I misundestood Linux shutdown sequence.
Sorry, do you mean Stanley's change is redundant?
yes.
No, it is definitely needed. As Stanley replied you in another thread, it is not protecting I/Os from user layer, but from other subsystems during shutdown.
Quoted 65 lines
Quoted 10 lines
Quoted 9 lines
I checked the shutdown flow:
1. Set the "
system_state" variable
2. Disable usermod to ensure that no user from userspace can start
a
requestQuoted 6 lines
I hope it is like what you interpreted, but step #2 only stops UMH(#265)
but not all user space activities. Whereas, UMH is for kernel space
calling
user space.Can,
I did further study and homework on the Linux shutdown in the last few
days. Yes, you are right,usermodehelper_disable()is to prevent executing the process from the kernel space.But I didn't reproduce this "maybe" race issue while shutdown. no matter how I torment my system, once Linux shutdown/halt/reboot starts,
nobody can access the sysfs node. I create 10 processes in the user
space and constantly access UFS sysfs node, also, fio is running in
the
background for the normal data read/write. there is a shutdown thread
that will randomly trigger shutdown/halt/reboot. but no race issue
appears.I don't know if this is a hypothetical issue(the race between shutdown
flow and sysfs node access), it may not really exist in the Linux envriroment. everytime, the shutdonw flow will be:e10_sync_handler()->e10_svc()->do_e10_svc()->__do_sys_reboot()-
Quoted 3 lines
kernel_poweroff/kernel_halt()->device_shutdown()-
Quoted 1 line
platform_shutdown()-
ufshcd_platform_shutdown()->ufshcd_shutdown().
I think before going into the kernel shutdown, the userspace cannot
issue new requests anymore. otherwise, this would be a big issue.
pm_runtime_get_sync()will return 0 or failure while shutdown? the
answer is not important now, maybe as you said, it is always 0. But in
my testing, it didn't get there the system has been shutdown. Which
means once shutdonw starts, sysfs node access path cannot reachpm_runtime_get_sync(). (note, I don't know if sysfs node access thread
has been disabled or not)Quoted 9 lines
Responsibly say, I didn't reproduce this issue on my system (ubuntu),
maybe you are using Android. I am not an expert on this topic, if you
have the best idea on how to reproduce this issue. please please let
me
try. appreciate it!!!!!When you do a reboot/shutdown/poweroff, how your system behaves highly
depends on how the reboot cmd is implemented in C code under/sbin/.On Ubuntu, reboot looks like:
$ reboot --help reboot [OPTIONS...] [ARG]Reboot the system.
--help Show this help --halt Halt the machine -p --poweroff Switch off the machine --reboot Reboot the machine -f --force Force immediate halt/power-off/reboot-w --wtmp-only Don't halt/power-off/reboot, just write wtmp
record
-d --no-wtmp Don't write wtmp record
--no-wall Don't send wall message before halt/power-
off/rebootOn a pure Linux with a initrd RAM FS built from busybox, reboot looks
like:# reboot --help BusyBox v1.30.1 (2019-05-24 12:53:36 IST) multi-call binary.Usage: reboot [-d DELAY] [-n] [-f]
Reboot the system
-d SEC Delay interval -n Do not sync -f Force (don't go through init)For example, when you work on a pure Linux with a filesystem built from
busybox, when you hit reboot cmd,halt_main()will be called. And based
on the reboot options passed to reboot cmd,halt_main()behaves differently.A plain reboot cmd does things like sync filesystem, send SIGKILL to
all
processes (except for init), remount all filesytem as read-only and so
on
before invoking linux kernel reboot syscall. In this case, we are safe.However, if you do a "reboot -f",
halt_main()directly invokesreboot().
And with "reboot -f", I can easily reproduce the race condition we are
talking about here - it is not based on imagination.Find the patch I used for replication in the attachment, fix conflicts
if any. After boot up, the cmd lines I used areQuoted 6 lines
# while true; do cat /sys/devices/platform/soc@0/*ufshc*/rpm_lvl; done & # reboot -fCan Guo.
Oops... forgot the logs:
#
# while true; do cat /sys/devices/platform/soc@0/*ufshc*/rpm_lvl; done & 3 3 3 3 .... # reboot -f 3 3 3 .... [ 17.959206] sd 0:0:0:5: [sdf] Synchronizing SCSI cache 3 [ 17.964833] sd 0:0:0:4: [sde] Synchronizing SCSI cache [ 17.970224] sd 0:0:0:3: [sdd] Synchronizing SCSI cache [ 17.975574] sd 0:0:0:2: [sdc] Synchronizing SCSI cache 3 [ 17.981034] sd 0:0:0:1: [sdb] Synchronizing SCSI cache [ 17.986493] sd 0:0:0:0: [sda] Synchronizing SCSI cache 3 [ 17.991870] [DEBUG]ufshcd_shutdown: UFS SHUTDOWN START [ 17.998902] ------------[ cut here ]------------ [ 18.003648] kernel BUG at drivers/scsi/ufs/ufs-sysfs.c:62! [ 18.009286] Internal error: Oops - BUG: 0 [#1] PREEMPT SMP [ 18.034249] pstate: 40c00005 (nZcv daif +PAN +UAO) [ 18.039185] pc : rpm_lvl_show+0x38/0x40 [ 18.043137] lr : dev_attr_show+0x1c/0x58 [ 18.132552] Call trace:Quoted 8 lines
[ 18.135076] rpm_lvl_show+0x38/0x40 [ 18.138672] sysfs_kf_seq_show+0xa8/0x140 [ 18.142802] kernfs_seq_show+0x28/0x30 [ 18.146665] seq_read+0x1d8/0x4b0 [ 18.150072] kernfs_fop_read+0x12c/0x1f0 [ 18.154109] do_iter_read+0x184/0x1c0 [ 18.157882] vfs_readv+0x68/0xb0 ....
Quoted 8 lines
Hi Can
Please forgive me for being verbose.The above BUG log is from your
BUG_ON()called, not becuase of the race betwen shutdown flow and sysfs nodeaccess(if I misunderstood, correct me). But it tells that the user can still access UFS by sysfs node in the "reboot -f" flow since it skips the actual shutdown process, " --force" is not a safe way anyway.
What is an "actual shutdown process", is there a standard? I believe it is free for users to decide what to do before invoking kernel reboot() syscall. "reboot -f" simply invokes kernel reboot() syscall, safe or not I don't know, but UFS shold not be the one nailed to the pillar. Do you agree?
Can Guo.
Quoted 16 lines
If accessing sysfs nodes, which triggers a UFS UPIU request to read/write UFS device descriptors during shutdown flow, there is only one issue that sysfs node access failure since UFS device and LINK has been shutdown. Strictly speaking, the failure comes after
ufshcd_set_dev_pwr_mode().__ufshcd_query_descriptor: opcode 0x01 for idn 0 failed, index 0,err = -11
Since the shutdown is oneway process, this failure is not big issue. If you meant to avoid this failure for unsafe shutdown, I agree with you, But for the race issue, I don't know.
Bean
Re: [PATCH 2/2] scsi: ufs: Protect PM ops and err_handler from user access through sysfs
In reply to Can GuoQuoted 16 lines · Can Guo
+ * @shutting_down: flag to check if shutdown has been invoked
+ * @host_sem: semaphore used to serialize concurrent contexts
* @eh_wq: Workqueue thateh_workworks on
* @eh_work: Worker to handle UFS errors that require s/w attention
* @eeh_work: Worker to handle exception eventsdrivers/scsi/ufs/ufshcd.h +2
-1@@ -751,7 +753,8 @@ struct ufs_hba {u32 intr_mask;u16 ee_ctrl_mask;bool is_powered; -struct semaphore eh_sem; +bool shutting_down; +struct semaphore host_sem;/* Work Queues */ struct workqueue_struct *eh_wq;
drivers/scsi/ufs/ufshcd.h static inline bool ufshcd_is_wb_allowed(struct · line 886
Quoted 9 lines
drivers/scsi/ufs/ufshcd.h +5
-0@@ -875,6 +878,11 @@ static inline bool ufshcd_is_wb_allowed(struct ufs_hba *hba)return hba->caps &UFSHCD_CAP_WB_EN ; } +static inline bool ufshcd_is_sysfs_allowed (struct ufs_hba *hba) +{ +return !hba->shutting_down; +} +
Can,
Instead adding new shutting_down flag, can we use availible variable system_state?
Thanks,
Bean
Re: [PATCH 2/2] scsi: ufs: Protect PM ops and err_handler from user access through sysfs
Quoted 30 lines · Bean Huo
Quoted 16 lines · Can Guo
+ * @shutting_down: flag to check if shutdown has been invoked
+ * @host_sem: semaphore used to serialize concurrent contexts
* @eh_wq: Workqueue thateh_workworks on
* @eh_work: Worker to handle UFS errors that require s/w attention
* @eeh_work: Worker to handle exception eventsdrivers/scsi/ufs/ufshcd.h +2
-1@@ -751,7 +753,8 @@ struct ufs_hba {u32 intr_mask;u16 ee_ctrl_mask;bool is_powered; -struct semaphore eh_sem; +bool shutting_down; +struct semaphore host_sem;/* Work Queues */ struct workqueue_struct *eh_wq;drivers/scsi/ufs/ufshcd.h static inline bool ufshcd_is_wb_allowed(struct · line 886
Quoted 9 lines
drivers/scsi/ufs/ufshcd.h +5
-0@@ -875,6 +878,11 @@ static inline bool ufshcd_is_wb_allowed(struct ufs_hba *hba)return hba->caps &UFSHCD_CAP_WB_EN ; } +static inline bool ufshcd_is_sysfs_allowed (struct ufs_hba *hba) +{ +return !hba->shutting_down; +} +Can,
Quoted 5 lines
Instead adding new
shutting_downflag, can we use availible variablesystem_state?Thanks,
Bean
Hi Bean,
I prefer the flag shutting_down, it tells us whether ufshcd_shutdown() has been invoked or not. It comes handy when debug some system crash issues caused by UFS during reboot/shutdown tests. system_state is too wide in this case.
Thanks,
Can Guo.
Re: [PATCH 2/2] scsi: ufs: Protect PM ops and err_handler from user access through sysfs
Quoted 16 lines · Can Guo
Quoted 9 lines
drivers/scsi/ufs/ufshcd.h static inline bool ufshcd_is_wb_allowed(struct ufs_hba *hba) · line 885
Quoted 5 lines
drivers/scsi/ufs/ufshcd.h +5
-0+static inline bool ufshcd_is_sysfs_allowed (struct ufs_hba *hba) +{ +return !hba->shutting_down; +} +Can,
Quoted 6 lines
Instead adding new
shutting_downflag, can we use availible variablesystem_state?Thanks,
Bean
Quoted 9 lines
Hi Bean,
I prefer the flag
shutting_down, it tells us whetherufshcd_shutdown()
has been invoked or not. It comes handy when debug some system crash issues caused by UFS during reboot/shutdown tests.system_stateis too
wide in this case.
It is only a suggestion, and others LLD use system_state, you prefer adding new flags.
Bean
Quoted 2 lines
Thanks,
Can Guo.
Avri Altman
Acked-byRE: [PATCH 2/2] scsi: ufs: Protect PM ops and err_handler from user access through sysfs
In reply to Bean HuoQuoted 32 lines · Can Guo
Quoted 16 lines
+ * @shutting_down: flag to check if shutdown has been invoked
+ * @host_sem: semaphore used to serialize concurrent contexts
* @eh_wq: Workqueue thateh_workworks on
* @eh_work: Worker to handle UFS errors that require s/w attention
* @eeh_work: Worker to handle exception eventsdrivers/scsi/ufs/ufshcd.h +2
-1@@ -751,7 +753,8 @@ struct ufs_hba {u32 intr_mask;u16 ee_ctrl_mask;bool is_powered; -struct semaphore eh_sem; +bool shutting_down; +struct semaphore host_sem;/* Work Queues */ struct workqueue_struct *eh_wq;drivers/scsi/ufs/ufshcd.h static inline bool ufshcd_is_wb_allowed(struct · line 886
Quoted 9 lines
drivers/scsi/ufs/ufshcd.h +5
-0@@ -875,6 +878,11 @@ static inline bool ufshcd_is_wb_allowed(struct ufs_hba *hba)return hba->caps &UFSHCD_CAP_WB_EN ; } +static inline bool ufshcd_is_sysfs_allowed (struct ufs_hba *hba) +{ +return !hba->shutting_down; +} +Can,
Quoted 3 lines
Instead adding new
shutting_downflag, can we use availible variablesystem_state?
Like Can, I am too, don't think that using system state here, e.g. UFS_SHUTDOWN_PM suffices. The use of the new flag, jointly with the semaphore, provides a tighter control.
Re: [PATCH 2/2] scsi: ufs: Protect PM ops and err_handler from user access through sysfs
In reply to Can GuoQuoted 6 lines · Can Guo
User layer may access sysfs nodes when system PM ops or error handling is running, which can cause various problems. Rename
eh_semtohost_semand use it to protect PM ops and error handling from user layer intervene.Signed-off-by Can Guo <[email protected]>
Looks good to me.
Feel free to add
Can,
Quoted 1 line
This series contains two changes and it is based on 5.11/scsi-queue
Please rebase against 5.12/scsi-queue.
Thanks!
Signature · 1 line
Martin K. Petersen Oracle Linux Engineering
Quoted 7 lines · Martin K. Petersen
Can,
Quoted 1 line
This series contains two changes and it is based on 5.11/scsi-queue
Please rebase against 5.12/scsi-queue.
Thanks!
Sure. Thanks Martin.