Skip to contentSkip to conversation

ufs: core: Fix error handler host_sem issue

PATCH Superseded [email protected] 2 participants 5 messages Oct 3, 2025 – Oct 8, 2025

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ufs: core: Fix error handler host_sem issue

From: Peter Wang <[email protected]>

Fix the issue where host_sem is not released due to a new return path introduced in commit 'f966e02ae521'. Move the check for pm_op_in_progress before acquiring hba->host_sem to ensure proper semaphore handling. This fix prevents potential deadlocks and ensures that resources are correctly managed during error handling.

Fixes f966e02ae521 ("scsi: ufs: core: Fix runtime suspend error deadlock")
---
 drivers/ufs/core/ufshcd.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
drivers/ufs/core/ufshcd.c +8 -8
index 8185a7791923..9b1207dede64 100644
@@ -6670,6 +6670,14 @@ static void ufshcd_err_handler(struct work_struct *work)
 		 hba->saved_uic_err, hba->force_reset,
 		 ufshcd_is_link_broken(hba) ? "; link is broken" : "");
 
+	ufshcd_rpm_get_noresume(hba);
+	if (hba->pm_op_in_progress) {
+		ufshcd_link_recovery(hba);
+		ufshcd_rpm_put(hba);
+		return;
+	}
+	ufshcd_rpm_put(hba);
+
 	down(&hba->host_sem);
 	spin_lock_irqsave(hba->host->host_lock, flags);
 	if (ufshcd_err_handling_should_stop(hba)) {
@@ -6681,14 +6689,6 @@ static void ufshcd_err_handler(struct work_struct *work)
 	}
 	spin_unlock_irqrestore(hba->host->host_lock, flags);
 
-	ufshcd_rpm_get_noresume(hba);
-	if (hba->pm_op_in_progress) {
-		ufshcd_link_recovery(hba);
-		ufshcd_rpm_put(hba);
-		return;
-	}
-	ufshcd_rpm_put(hba);
-
 	ufshcd_err_handling_prepare(hba);
 
 	spin_lock_irqsave(hba->host->host_lock, flags);
Signature · 1 line
2.45.2
#1 lore raw
Quoted 24 lines · On 10/3/25 3:10 AM, [email protected] wrote:
drivers/ufs/core/ufshcd.c +8 -1
index 8185a7791923..9b1207dede64 100644
@@ -6670,6 +6670,14 @@ static void ufshcd_err_handler(struct work_struct *work)
  		 hba->saved_uic_err, hba->force_reset,
  		 ufshcd_is_link_broken(hba) ? "; link is broken" : "");
 
+	ufshcd_rpm_get_noresume(hba);
+	if (hba->pm_op_in_progress) {
+		ufshcd_link_recovery(hba);
+		ufshcd_rpm_put(hba);
+		return;
+	}
+	ufshcd_rpm_put(hba);
+
  	down(&hba->host_sem);
  	spin_lock_irqsave(hba->host->host_lock, flags);
  	if (ufshcd_err_handling_should_stop(hba)) {
@@ -6681,14 +6689,6 @@ static void ufshcd_err_handler(struct work_struct *work)
  	}
  	spin_unlock_irqrestore(hba->host->host_lock, flags);
 
-	ufshcd_rpm_get_noresume(hba);

drivers/ufs/core/ufshcd.c static void ufshcd_err_handler(struct work_struct *work) · line 6694

Quoted 10 lines
drivers/ufs/core/ufshcd.c +0 -7
-	if (hba->pm_op_in_progress) {
-		ufshcd_link_recovery(hba);
-		ufshcd_rpm_put(hba);
-		return;
-	}
-	ufshcd_rpm_put(hba);
-
  	ufshcd_err_handling_prepare(hba);
 
  	spin_lock_irqsave(hba->host->host_lock, flags);

The purpose of hba->host_sem is to serialize power management operations, error handling and sysfs callbacks. Hence, I think that hba->host_sem should be held around the ufshcd_link_recovery() call and hence that the code block that is moved by this patch should not be moved.

Thanks,

Bart.

#2 lore raw
Quoted 9 lines · Bart Van Assche

The purpose of hba->host_sem is to serialize power management operations, error handling and sysfs callbacks. Hence, I think that hba->host_sem should be held around the ufshcd_link_recovery() call and hence that the code block that is moved by this patch should not be moved.

Thanks,

Bart.

Hi Bart,

No, if we take host_sem before checking pm_op_in_progress, it may cause a deadlock. Consider this case:

ufshcd_wl_suspend
 down(&hba->host_sem);
 __ufshcd_wl_suspend => may trigger ufshcd_err_handler ufshcd_execute_start_stop => stuck here waiting for ufshcd_err_handler to finish

ufshcd_err_handler
 down(&hba->host_sem); => stuck here

Thanks
Peter

#3 lore raw
Quoted 11 lines · Peter Wang (王信友)

No, if we take host_sem before checking pm_op_in_progress, it may cause a deadlock. Consider this case:

ufshcd_wl_suspend
 down(&hba->host_sem);
 __ufshcd_wl_suspend => may trigger ufshcd_err_handler ufshcd_execute_start_stop => stuck here waiting for ufshcd_err_handler to finish

ufshcd_err_handler
 down(&hba->host_sem); => stuck here

Hi Peter,

Please add a comment above the new ufshcd_rpm_get_noresume() call that explains the above. I think it would be good to have the above information in the UFS host controller driver source code.

Thanks,

Bart.

#4 lore raw
Quoted 10 lines · Bart Van Assche

Hi Peter,

Please add a comment above the new ufshcd_rpm_get_noresume() call that
explains the above. I think it would be good to have the above information in the UFS host controller driver source code.

Thanks,

Bart.

Hi Bart,

Okay, I will add the above comment in the next version.

Thanks
Peter