scsi: ufs: core: Let the sq_lock protect sq_tail_slot access
authorCan Guo <quic_cang@quicinc.com>
Mon, 18 Dec 2023 15:32:17 +0000 (07:32 -0800)
committerMartin K. Petersen <martin.petersen@oracle.com>
Tue, 19 Dec 2023 02:09:54 +0000 (21:09 -0500)
When accessing sq_tail_slot without protection from sq_lock, a race
condition can cause multiple SQEs to be copied to duplicate SQE slots. This
can lead to multiple stability issues. Fix this by moving the *dest
initialization in ufshcd_send_command() back under protection from the
sq_lock.

Fixes: 3c85f087faec ("scsi: ufs: mcq: Use pointer arithmetic in ufshcd_send_command()")
Signed-off-by: Can Guo <quic_cang@quicinc.com>
Link: https://lore.kernel.org/r/1702913550-20631-1-git-send-email-quic_cang@quicinc.com
Reviewed-by: Bart Van Assche <bvanassche@acm.org>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
drivers/ufs/core/ufshcd.c

index bce0d2a9a7f3a55497572bd3e51e9cf4fc6a0b4e..16d76325039a5d7db5c59458d11fc248a915632c 100644 (file)
@@ -2274,9 +2274,10 @@ void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag,
        if (is_mcq_enabled(hba)) {
                int utrd_size = sizeof(struct utp_transfer_req_desc);
                struct utp_transfer_req_desc *src = lrbp->utr_descriptor_ptr;
-               struct utp_transfer_req_desc *dest = hwq->sqe_base_addr + hwq->sq_tail_slot;
+               struct utp_transfer_req_desc *dest;
 
                spin_lock(&hwq->sq_lock);
+               dest = hwq->sqe_base_addr + hwq->sq_tail_slot;
                memcpy(dest, src, utrd_size);
                ufshcd_inc_sq_tail(hwq);
                spin_unlock(&hwq->sq_lock);