Skip to contentSkip to conversation

core: ufs: fix racing issue between force complete and isr

PATCH Applied [email protected] 4 participants 4 messages Oct 24, 2023 – Oct 25, 2023 1 message not in the archive

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

PATCH 1/1 core: ufs: fix racing issue between force complete and isr

From: Alice Chao <[email protected]>

While error handler force complete command (Thread A) and completion irq raising (Thread B) of the same command, it may cause race condition.

Below is racing step (from 1 to 6):
ufshcd_mcq_compl_pending_transfer (Thread A)
1 if (cmd && !test_bit(SCMD_STATE_COMPLETE, &cmd->state)) {
5 spin_lock_irqsave(&hwq->cq_lock, flags); // wait lock release

		set_host_byte(cmd, DID_REQUEUE);
6		ufshcd_release_scsi_cmd(hba, lrbp);	// access null pointer
		scsi_done(cmd);
		spin_unlock_irqrestore(&hwq->cq_lock, flags);
	}

	ufshcd_mcq_poll_cqe_lock (Thread B)

2 spin_lock_irqsave(&hwq->cq_lock, flags);
ufshcd_mcq_poll_cqe_nolock()
ufshcd_compl_one_cqe()
3 ufshcd_release_scsi_cmd() // lrbp->cmd = NULL;
4 spin_unlock_irqrestore(&hwq->cq_lock, flags);

---
 drivers/ufs/core/ufshcd.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/ufs/core/ufshcd.c +2 -2
index 8382e8cfa414..ef6bd146a767 100644
@@ -5518,13 +5518,13 @@ static void ufshcd_mcq_compl_pending_transfer(struct ufs_hba *hba,
 			 * For those cmds of which the cqes are not present
 			 * in the cq, complete them explicitly.
 			 */
+			spin_lock_irqsave(&hwq->cq_lock, flags);
 			if (cmd && !test_bit(SCMD_STATE_COMPLETE, &cmd->state)) {
-				spin_lock_irqsave(&hwq->cq_lock, flags);
 				set_host_byte(cmd, DID_REQUEUE);
 				ufshcd_release_scsi_cmd(hba, lrbp);
 				scsi_done(cmd);
-				spin_unlock_irqrestore(&hwq->cq_lock, flags);
 			}
+			spin_unlock_irqrestore(&hwq->cq_lock, flags);
 		} else {
 			ufshcd_mcq_poll_cqe_lock(hba, hwq);
 		}
Signature · 1 line
2.18.0
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · On 10/24/23 01:43, [email protected] wrote:
drivers/ufs/core/ufshcd.c +1 -0
index 8382e8cfa414..ef6bd146a767 100644
@@ -5518,13 +5518,13 @@ static void ufshcd_mcq_compl_pending_transfer(struct ufs_hba *hba,
  			 * For those cmds of which the cqes are not present
  			 * in the cq, complete them explicitly.
  			 */
+			spin_lock_irqsave(&hwq->cq_lock, flags);
  			if (cmd && !test_bit(SCMD_STATE_COMPLETE, &cmd->state)) {

drivers/ufs/core/ufshcd.c static void ufshcd_mcq_compl_pending_transfer(struct ufs_hba *hba, · line 5530

Quoted 10 lines
drivers/ufs/core/ufshcd.c +1 -2
-				spin_lock_irqsave(&hwq->cq_lock, flags);
  				set_host_byte(cmd, DID_REQUEUE);
  				ufshcd_release_scsi_cmd(hba, lrbp);
  				scsi_done(cmd);
-				spin_unlock_irqrestore(&hwq->cq_lock, flags);
  			}
+			spin_unlock_irqrestore(&hwq->cq_lock, flags);
  		} else {
  			ufshcd_mcq_poll_cqe_lock(hba, hwq);
  		}
#2 lore raw
In reply to [email protected]

Alice,

Quoted 3 lines

While error handler force complete command (Thread A) and completion irq raising (Thread B) of the same command, it may cause race condition.

Applied to 6.7/scsi-staging, thanks!

Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering

A message that is not in the archive (sent off-list or not yet received): [email protected]