Skip to contentSkip to conversation

ufs: core: Fix an error handler crash

PATCH Applied Bart Van Assche 4 participants 6 messages Dec 4, 2025 – Dec 9, 2025

Subsystems (2)
lore
Share & tools
lore

Bart Van Assche

author reviewer
#0 lore raw

PATCH 1/1 ufs: core: Fix an error handler crash

The UFS error handler may be activated before SCSI scanning has started and hence before hba->ufs_device_wlun has been set. Check the hba->ufs_device_wlun pointer before using it.

Cc Peter Wang <[email protected]>
Cc Nitin Rawat <[email protected]>
Fixes e23ef4f22db3 ("scsi: ufs: core: Fix error handler host_sem issue")
Fixes f966e02ae521 ("scsi: ufs: core: Fix runtime suspend error deadlock")
---
 drivers/ufs/core/ufshcd.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)
drivers/ufs/core/ufshcd.c +14 -11
index b834b9635062..80c0b49f30b0 100644
@@ -6698,19 +6698,22 @@ static void ufshcd_err_handler(struct work_struct *work)
 		 hba->saved_uic_err, hba->force_reset,
 		 ufshcd_is_link_broken(hba) ? "; link is broken" : "");
 
-	/*
-	 * Use ufshcd_rpm_get_noresume() here to safely perform link recovery
-	 * even if an error occurs during runtime suspend or runtime resume.
-	 * This avoids potential deadlocks that could happen if we tried to
-	 * resume the device while a PM operation is already in progress.
-	 */
-	ufshcd_rpm_get_noresume(hba);
-	if (hba->pm_op_in_progress) {
-		ufshcd_link_recovery(hba);
+	if (hba->ufs_device_wlun) {
+		/*
+		 * Use ufshcd_rpm_get_noresume() here to safely perform link
+		 * recovery even if an error occurs during runtime suspend or
+		 * runtime resume. This avoids potential deadlocks that could
+		 * happen if we tried to resume the device while a PM operation
+		 * is already in progress.
+		 */
+		ufshcd_rpm_get_noresume(hba);
+		if (hba->pm_op_in_progress) {
+			ufshcd_link_recovery(hba);
+			ufshcd_rpm_put(hba);
+			return;
+		}
 		ufshcd_rpm_put(hba);
-		return;
 	}
-	ufshcd_rpm_put(hba);
 
 	down(&hba->host_sem);
 	spin_lock_irqsave(hba->host->host_lock, flags);
#1 lore raw
Quoted 41 lines · Bart Van Assche

The UFS error handler may be activated before SCSI scanning has started and hence before hba->ufs_device_wlun has been set. Check the hba->ufs_device_wlun pointer before using it.

Cc Peter Wang <[email protected]>
Cc Nitin Rawat <[email protected]>
Fixes e23ef4f22db3 ("scsi: ufs: core: Fix error handler host_sem issue")
Fixes f966e02ae521 ("scsi: ufs: core: Fix runtime suspend error deadlock")
---
  drivers/ufs/core/ufshcd.c | 25 ++++++++++++++-----------
  1 file changed, 14 insertions(+), 11 deletions(-)
drivers/ufs/core/ufshcd.c +11 -9
index b834b9635062..80c0b49f30b0 100644
@@ -6698,19 +6698,22 @@ static void ufshcd_err_handler(struct work_struct *work)
  		 hba->saved_uic_err, hba->force_reset,
  		 ufshcd_is_link_broken(hba) ? "; link is broken" : "");
 
-	/*
-	 * Use ufshcd_rpm_get_noresume() here to safely perform link recovery
-	 * even if an error occurs during runtime suspend or runtime resume.
-	 * This avoids potential deadlocks that could happen if we tried to
-	 * resume the device while a PM operation is already in progress.
-	 */
-	ufshcd_rpm_get_noresume(hba);
-	if (hba->pm_op_in_progress) {
-		ufshcd_link_recovery(hba);
+	if (hba->ufs_device_wlun) {
+		/*
+		 * Use ufshcd_rpm_get_noresume() here to safely perform link
+		 * recovery even if an error occurs during runtime suspend or
+		 * runtime resume. This avoids potential deadlocks that could
+		 * happen if we tried to resume the device while a PM operation
+		 * is already in progress.
+		 */
+		ufshcd_rpm_get_noresume(hba);
+		if (hba->pm_op_in_progress) {
+			ufshcd_link_recovery(hba);

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

Quoted 10 lines
drivers/ufs/core/ufshcd.c +3 -2
+			ufshcd_rpm_put(hba);
+			return;
+		}
  		ufshcd_rpm_put(hba);
-		return;
  	}
-	ufshcd_rpm_put(hba);
 
  	down(&hba->host_sem);
  	spin_lock_irqsave(hba->host->host_lock, flags);

Hi Bart,

It seems you missed sending the below patch. Both patches are required to address the issue (hang and clock scaling errors), except for the UIC error, which still needs to be root-caused

Quoted 24 lines
drivers/ufs/core/ufshcd.c +4 -5
index 1b3fe1d8655e..fd0b6b620b53 100644
@@ -1455,15 +1455,14 @@ static int ufshcd_clock_scaling_prepare(struct
 ufs_hba *hba, u64 timeout_us)
  static void ufshcd_clock_scaling_unprepare(struct ufs_hba *hba, int err)
  {
        up_write(&hba->clk_scaling_lock);
-
+       mutex_unlock(&hba->wb_mutex);
+       blk_mq_unquiesce_tagset(&hba->host->tag_set);
+       mutex_unlock(&hba->host->scan_mutex);
+
        /* Enable Write Booster if current gear requires it else disable it */
        if (ufshcd_enable_wb_if_scaling_up(hba) && !err)
                ufshcd_wb_toggle(hba, hba->pwr_info.gear_rx >=
 hba->clk_scaling.wb_gear);
 
-       mutex_unlock(&hba->wb_mutex);
-
-       blk_mq_unquiesce_tagset(&hba->host->tag_set);
-       mutex_unlock(&hba->host->scan_mutex);
        ufshcd_release(hba);
Quoted 1 line
  }

Thanks,
Nitin

author reviewer
#2 lore raw
Quoted 1 line · Nitin Rawat

It seems you missed sending the below patch. Both patches are required to address the issue (hang and clock scaling errors), except for the UIC error, which still needs to be root-caused

My shell history tells me that I tried to send the fix for the clock scaling hang to the linux-scsi mailing list yesterday. Apparently the patch didn't reach the linux-scsi mailing list. Maybe this was caused by the hotel Wi-Fi. Anyway, thanks for having pointed this out. I have resent the fix for the clock scaling hang.

Bart.

Reviewed-by on v1 1/1
#3 lore raw
In reply to Bart Van Assche
Quoted 5 lines · Bart Van Assche

The UFS error handler may be activated before SCSI scanning has started and
hence before hba->ufs_device_wlun has been set. Check the hba->ufs_device_wlun pointer before using it.

Quoted 7 lines
Cc Peter Wang <[email protected]>
Cc Nitin Rawat <[email protected]>
Fixes e23ef4f22db3 ("scsi: ufs: core: Fix error handler host_sem

issue")

Fixes f966e02ae521 ("scsi: ufs: core: Fix runtime suspend error

deadlock")

Thanks for fix this bug.

Reviewed-by Tested-by on v1 1/1
#4 lore raw
In reply to Bart Van Assche
Quoted 9 lines · Bart Van Assche

The UFS error handler may be activated before SCSI scanning has started and hence before hba->ufs_device_wlun has been set. Check the hba->ufs_device_wlun pointer before using it.

Cc Peter Wang <[email protected]>
Cc Nitin Rawat <[email protected]>
Fixes e23ef4f22db3 ("scsi: ufs: core: Fix error handler host_sem issue")
Fixes f966e02ae521 ("scsi: ufs: core: Fix runtime suspend error deadlock")
#5 lore raw
In reply to Bart Van Assche

Bart,

Quoted 3 lines

The UFS error handler may be activated before SCSI scanning has started and hence before hba->ufs_device_wlun has been set. Check the hba->ufs_device_wlun pointer before using it.

Applied to 6.19/scsi-staging, thanks!

Signature · 1 line
Martin K. Petersen