Skip to contentSkip to conversation

scsi: ufs: fix tm cmd timeout/ISR racing issue

PATCH In review [email protected] 2 participants 4 messages Nov 11, 2021 – Nov 16, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scsi: ufs: fix tm cmd timeout/ISR racing issue

From: Peter Wang <[email protected]>

When tmc 100 ms timeout and recevied tmc complete ISR concurrently, Bug happen because complete NULL poiner and KE.
Fix this racing issue by check NULL and use host_lock protect.

---
 drivers/scsi/ufs/ufshcd.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/scsi/ufs/ufshcd.c +5 -1
index 5c6a58a666d2..6821ceb6783e 100644
@@ -6442,7 +6442,8 @@ static irqreturn_t ufshcd_tmc_handler(struct ufs_hba *hba)
 		struct request *req = hba->tmf_rqs[tag];
 		struct completion *c = req->end_io_data;
 
-		complete(c);
+		if (c)
+			complete(c);
 		ret = IRQ_HANDLED;
 	}
 	spin_unlock_irqrestore(hba->host->host_lock, flags);
@@ -6597,7 +6598,10 @@ static int __ufshcd_issue_tm_cmd(struct ufs_hba *hba,
 		 * Make sure that ufshcd_compl_tm() does not trigger a
 		 * use-after-free.
 		 */
+		spin_lock_irqsave(hba->host->host_lock, flags);
 		req->end_io_data = NULL;
+		spin_unlock_irqrestore(hba->host->host_lock, flags);
+
 		ufshcd_add_tm_upiu_trace(hba, task_tag, UFS_TM_ERR);
 		dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
 				__func__, tm_function);
Signature · 1 line
2.18.0
#1 lore raw
Quoted 26 lines · On 11/11/21 1:49 AM, [email protected] wrote:

From: Peter Wang <[email protected]>

When tmc 100 ms timeout and recevied tmc complete ISR concurrently, Bug happen because complete NULL poiner and KE.
Fix this racing issue by check NULL and use host_lock protect.

---
  drivers/scsi/ufs/ufshcd.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
drivers/scsi/ufs/ufshcd.c +2 -1
index 5c6a58a666d2..6821ceb6783e 100644
@@ -6442,7 +6442,8 @@ static irqreturn_t ufshcd_tmc_handler(struct ufs_hba *hba)
  		struct request *req = hba->tmf_rqs[tag];
  		struct completion *c = req->end_io_data;
 
-		complete(c);
+		if (c)
+			complete(c);
  		ret = IRQ_HANDLED;
  	}
  	spin_unlock_irqrestore(hba->host->host_lock, flags);

drivers/scsi/ufs/ufshcd.c static int __ufshcd_issue_tm_cmd(struct ufs_hba *hba, · line 6607

Quoted 10 lines
drivers/scsi/ufs/ufshcd.c +3 -0
@@ -6597,7 +6598,10 @@ static int __ufshcd_issue_tm_cmd(struct ufs_hba *hba,
  		 * Make sure that ufshcd_compl_tm() does not trigger a
  		 * use-after-free.
  		 */
+		spin_lock_irqsave(hba->host->host_lock, flags);
  		req->end_io_data = NULL;
+		spin_unlock_irqrestore(hba->host->host_lock, flags);
+
  		ufshcd_add_tm_upiu_trace(hba, task_tag, UFS_TM_ERR);
  		dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
  				__func__, tm_function);

Isn't this already addressed by Adrian Hunter's patches? See also
https://lore.kernel.org/linux-scsi/[email protected]/

Thanks,

Bart.

_______________________________________________
Linux-mediatek mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-mediatek

author
#2 lore raw
Quoted 42 lines · Bart Van Assche
Quoted 36 lines · On 11/11/21 1:49 AM, [email protected] wrote:

From: Peter Wang <[email protected]>

When tmc 100 ms timeout and recevied tmc complete ISR concurrently, Bug happen because complete NULL poiner and KE.
Fix this racing issue by check NULL and use host_lock protect.

---
  drivers/scsi/ufs/ufshcd.c | 6 +++++-

  1 file changed, 5 insertions(+), 1 deletion(-)
drivers/scsi/ufs/ufshcd.c +2 -0
index 5c6a58a666d2..6821ceb6783e 100644
@@ -6442,7 +6442,8 @@ static irqreturn_t ufshcd_tmc_handler(struct 
 ufs_hba *hba)
           struct request *req = hba->tmf_rqs[tag];
           struct completion *c = req->end_io_data;
   -        complete(c);
+        if (c)
+            complete(c);
           ret = IRQ_HANDLED;
       }

      spin_unlock_irqrestore(hba->host->host_lock, flags);

drivers/scsi/ufs/ufshcd.c +3 -0
@@ -6597,7 +6598,10 @@ static int __ufshcd_issue_tm_cmd(struct 
 ufs_hba *hba,
            * Make sure that ufshcd_compl_tm() does not trigger a
            * use-after-free.
            */
+        spin_lock_irqsave(hba->host->host_lock, flags);
           req->end_io_data = NULL;
+        spin_unlock_irqrestore(hba->host->host_lock, flags);
+
           ufshcd_add_tm_upiu_trace(hba, task_tag, UFS_TM_ERR);
           dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
Quoted 1 line

                  __func__, tm_function);

Isn't this already addressed by Adrian Hunter's patches? See also
https://urldefense.com/v3/__https://lore.kernel.org/linux-scsi/[email protected]/__;!!CTRNKA9wMg0ARbw!zttcrXBZgCk261BxtN67hFHTMRzOwcDr1IVH8znRw4I0POKCxUijARo7H3btU8SfRQ$

Quoted 4 lines

Thanks,

Bart.

Hi Bart,

Yes, I will drop this patch.

By the way, we observe that 100ms TMC timeout value may not enough for some device, maybe we need enlarge this value?

Thanks

Peter

#3 lore raw
Quoted 2 lines · Peter Wang

By the way, we observe that 100ms TMC timeout value may not enough for some device, maybe we need enlarge this value?

Is that the TM_CMD_TIMEOUT constant? It surprises me that 100 ms is not enough. Will increasing that constant have a negative impact on the error handler in case it hits a task management timeout?

Thanks,

Bart.