[PATCH v5 2/2] scsi: ufs: core: Do not recover the link early in the error handler during system PM

From: HOYOUNG SEO

Date: Thu Nov 05 2026 - 03:07:03 EST


ufshcd_err_handler() recovers the link without taking hba->host_sem
whenever hba->pm_op_in_progress is set. That early path was added by
commit f966e02ae521 ("scsi: ufs: core: Fix runtime suspend error
deadlock") because the regular error handling would wait for a runtime
PM operation that cannot complete while the error handler is waiting.

pm_op_in_progress is also set during system suspend and system resume,
where the early path is not needed and runs concurrently with the PM
path itself. For example, if a LINERESET is reported and the hibern8
exit fails in __ufshcd_wl_resume() during system resume, both
ufshcd_err_handler() and __ufshcd_wl_resume() call
ufshcd_link_recovery(), and the host and the device may be reset at the
same time.

The system PM callbacks hold host_sem from ufshcd_wl_suspend() until
ufshcd_wl_resume() returns, so the regular error handling can simply
wait for them. Skip the early link recovery while a system PM operation
is in progress. Runtime PM, shutdown and poweroff do not hold host_sem
and keep the early link recovery.

Since pm_op_in_progress does not tell which PM operation is running, add
hba->system_pm_in_progress and set it in __ufshcd_wl_suspend() and
__ufshcd_wl_resume().

Fixes: e23ef4f22db3 ("scsi: ufs: core: Fix error handler host_sem issue")
Signed-off-by: HOYOUNG SEO <hy50.seo@xxxxxxxxxxx>
---
drivers/ufs/core/ufshcd.c | 10 +++++++++-
include/ufs/ufshcd.h | 2 ++
2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c
index 3b5580ac390c..761ffc8f0a32 100644
--- a/drivers/ufs/core/ufshcd.c
+++ b/drivers/ufs/core/ufshcd.c
@@ -7091,9 +7091,13 @@ static void ufshcd_err_handler(struct work_struct *work)
* runtime resume. This avoids potential deadlocks that could
* happen if we tried to resume the device while a PM operation
* is already in progress.
+ *
+ * Do not do this for system PM. The system PM callbacks hold
+ * host_sem, so the regular path below runs after them instead
+ * of resetting the link concurrently.
*/
ufshcd_rpm_get_noresume(hba);
- if (hba->pm_op_in_progress) {
+ if (hba->pm_op_in_progress && !hba->system_pm_in_progress) {
ufshcd_link_recovery(hba);
ufshcd_rpm_put(hba);
return;
@@ -10393,6 +10397,7 @@ static int __ufshcd_wl_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
enum uic_link_state req_link_state;

hba->pm_op_in_progress = true;
+ hba->system_pm_in_progress = (pm_op == UFS_SYSTEM_PM);
if (pm_op != UFS_SHUTDOWN_PM) {
pm_lvl = pm_op == UFS_RUNTIME_PM ?
hba->rpm_lvl : hba->spm_lvl;
@@ -10565,6 +10570,7 @@ static int __ufshcd_wl_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
hba->clk_gating.is_suspended = false;
ufshcd_release(hba);
}
+ hba->system_pm_in_progress = false;
hba->pm_op_in_progress = false;
return ret;
}
@@ -10576,6 +10582,7 @@ static int __ufshcd_wl_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
enum uic_link_state old_link_state = hba->uic_link_state;

hba->pm_op_in_progress = true;
+ hba->system_pm_in_progress = (pm_op == UFS_SYSTEM_PM);

/*
* Call vendor specific resume callback. As these callbacks may access
@@ -10666,6 +10673,7 @@ static int __ufshcd_wl_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
ufshcd_update_evt_hist(hba, UFS_EVT_WL_RES_ERR, (u32)ret);
hba->clk_gating.is_suspended = false;
ufshcd_release(hba);
+ hba->system_pm_in_progress = false;
hba->pm_op_in_progress = false;
return ret;
}
diff --git a/include/ufs/ufshcd.h b/include/ufs/ufshcd.h
index be62165b785e..0d28853e4107 100644
--- a/include/ufs/ufshcd.h
+++ b/include/ufs/ufshcd.h
@@ -974,6 +974,7 @@ enum ufshcd_mcq_opr {
* @spm_lvl: desired UFS power management level during system PM.
* @pm_lvl_min: minimum supported power management level.
* @pm_op_in_progress: whether or not a PM operation is in progress.
+ * @system_pm_in_progress: whether or not a system PM operation is in progress.
* @ahit: value of Auto-Hibernate Idle Timer register.
* @outstanding_tasks: Bits representing outstanding task requests
* @outstanding_lock: Protects @outstanding_reqs.
@@ -1129,6 +1130,7 @@ struct ufs_hba {
enum ufs_pm_level spm_lvl;
enum ufs_pm_level pm_lvl_min;
int pm_op_in_progress;
+ bool system_pm_in_progress;

/* Auto-Hibernate Idle Timer register value */
u32 ahit;
--
2.34.1