ufs: core: Fix error handler host_sem issue
Subsystems (3)
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.
--- drivers/ufs/core/ufshcd.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
drivers/ufs/core/ufshcd.c +8 -8
@@ -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
Bart Van Assche
reviewerQuoted 24 lines · On 10/3/25 3:10 AM, [email protected] wrote:
drivers/ufs/core/ufshcd.c +8
-1@@ -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.
Quoted 9 lines · Bart Van Assche
The purpose of
hba->host_semis to serialize power management operations, error handling and sysfs callbacks. Hence, I think thathba->host_semshould be held around theufshcd_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
Bart Van Assche
reviewerQuoted 11 lines · Peter Wang (王信友)
No, if we take
host_sembefore checkingpm_op_in_progress, it may cause a deadlock. Consider this case:
ufshcd_wl_suspend
down(&hba->host_sem);
__ufshcd_wl_suspend => may triggerufshcd_err_handlerufshcd_execute_start_stop=> stuck here waiting forufshcd_err_handlerto 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.
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