bus: mhi: ep: Update read pointer only after buffer is written
authorSumit Kumar <quic_sumk@quicinc.com>
Wed, 9 Apr 2025 10:47:43 +0000 (16:17 +0530)
committerManivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
Thu, 24 Apr 2025 05:04:51 +0000 (10:34 +0530)
Inside mhi_ep_ring_add_element, the read pointer (rd_offset) is updated
before the buffer is written, potentially causing race conditions where
the host sees an updated read pointer before the buffer is actually
written. Updating rd_offset prematurely can lead to the host accessing
an uninitialized or incomplete element, resulting in data corruption.

Invoke the buffer write before updating rd_offset to ensure the element
is fully written before signaling its availability.

Fixes: bbdcba57a1a2 ("bus: mhi: ep: Add support for ring management")
cc: stable@vger.kernel.org
Co-developed-by: Youssef Samir <quic_yabdulra@quicinc.com>
Signed-off-by: Youssef Samir <quic_yabdulra@quicinc.com>
Signed-off-by: Sumit Kumar <quic_sumk@quicinc.com>
Reviewed-by: Jeff Hugo <jeff.hugo@oss.qualcomm.com>
Reviewed-by: Krishna Chaitanya Chundru <krishna.chundru@oss.qualcomm.com>
Reviewed-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
Link: https://patch.msgid.link/20250409-rp_fix-v1-1-8cf1fa22ed28@quicinc.com
Signed-off-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
drivers/bus/mhi/ep/ring.c

index aeb53b2c34a8cd859393529d0c8860462bc687ed..26357ee68dee984d70ae5bf39f8f09f2cbcafe30 100644 (file)
@@ -131,19 +131,23 @@ int mhi_ep_ring_add_element(struct mhi_ep_ring *ring, struct mhi_ring_element *e
        }
 
        old_offset = ring->rd_offset;
-       mhi_ep_ring_inc_index(ring);
 
        dev_dbg(dev, "Adding an element to ring at offset (%zu)\n", ring->rd_offset);
+       buf_info.host_addr = ring->rbase + (old_offset * sizeof(*el));
+       buf_info.dev_addr = el;
+       buf_info.size = sizeof(*el);
+
+       ret = mhi_cntrl->write_sync(mhi_cntrl, &buf_info);
+       if (ret)
+               return ret;
+
+       mhi_ep_ring_inc_index(ring);
 
        /* Update rp in ring context */
        rp = cpu_to_le64(ring->rd_offset * sizeof(*el) + ring->rbase);
        memcpy_toio((void __iomem *) &ring->ring_ctx->generic.rp, &rp, sizeof(u64));
 
-       buf_info.host_addr = ring->rbase + (old_offset * sizeof(*el));
-       buf_info.dev_addr = el;
-       buf_info.size = sizeof(*el);
-
-       return mhi_cntrl->write_sync(mhi_cntrl, &buf_info);
+       return ret;
 }
 
 void mhi_ep_ring_init(struct mhi_ep_ring *ring, enum mhi_ep_ring_type type, u32 id)