scsi: ufshcd: Fix race between clk scaling and ungate work
authorVenkat Gopalakrishnan <venkatg@codeaurora.org>
Tue, 18 Oct 2016 00:10:53 +0000 (17:10 -0700)
committerMartin K. Petersen <martin.petersen@oracle.com>
Tue, 8 Nov 2016 22:29:58 +0000 (17:29 -0500)
The ungate work turns on the clock before it exits hibern8, if the link
was put in hibern8 during clock gating work.  There occurs a race
condition when clock scaling work calls ufshcd_hold() to make sure low
power states cannot be entered, but that returns by checking only
whether the clocks are on.  This causes the clock scaling work to issue
UIC commands when the link is in hibern8 causing failures. Make sure we
exit hibern8 state before returning from ufshcd_hold().

Callstacks for race condition:

 ufshcd_scale_gear
 ufshcd_devfreq_scale
 ufshcd_devfreq_target
 update_devfreq
 devfreq_monitor
 process_one_work
 worker_thread
 kthread
 ret_from_fork

 ufshcd_uic_hibern8_exit
 ufshcd_ungate_work
 process_one_work
 worker_thread
 kthread
 ret_from_fork

Signed-off-by: Venkat Gopalakrishnan <venkatg@codeaurora.org>
Signed-off-by: Subhash Jadavani <subhashj@codeaurora.org>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
drivers/scsi/ufs/ufshcd.c

index d2930fb8b44690cb482231bd3348a79ddc6628da..5ad4480afd011ac42eb5e50b5ddbe8f8204f31f5 100644 (file)
@@ -675,6 +675,21 @@ int ufshcd_hold(struct ufs_hba *hba, bool async)
 start:
        switch (hba->clk_gating.state) {
        case CLKS_ON:
+               /*
+                * Wait for the ungate work to complete if in progress.
+                * Though the clocks may be in ON state, the link could
+                * still be in hibner8 state if hibern8 is allowed
+                * during clock gating.
+                * Make sure we exit hibern8 state also in addition to
+                * clocks being ON.
+                */
+               if (ufshcd_can_hibern8_during_gating(hba) &&
+                   ufshcd_is_link_hibern8(hba)) {
+                       spin_unlock_irqrestore(hba->host->host_lock, flags);
+                       flush_work(&hba->clk_gating.ungate_work);
+                       spin_lock_irqsave(hba->host->host_lock, flags);
+                       goto start;
+               }
                break;
        case REQ_CLKS_OFF:
                if (cancel_delayed_work(&hba->clk_gating.gate_work)) {