* Re: [PATCH] bus: mhi: ep: Update read pointer only after buffer is written
2025-04-09 10:47 [PATCH] bus: mhi: ep: Update read pointer only after buffer is written Sumit Kumar
@ 2025-04-09 11:12 ` Krishna Chaitanya Chundru
2025-04-09 14:41 ` Jeff Hugo
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Krishna Chaitanya Chundru @ 2025-04-09 11:12 UTC (permalink / raw)
To: Sumit Kumar, Manivannan Sadhasivam, Alex Elder, Greg Kroah-Hartman
Cc: mhi, linux-arm-msm, linux-kernel, quic_akhvin, quic_skananth,
quic_vbadigan, stable, Youssef Samir
On 4/9/2025 4:17 PM, Sumit Kumar wrote:
> 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: Krishna Chaitanya Chundru <krishna.chundru@oss.qualcomm.com>
- Krishna Chaitanya.
> ---
> ---
> drivers/bus/mhi/ep/ring.c | 16 ++++++++++------
> 1 file changed, 10 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/bus/mhi/ep/ring.c b/drivers/bus/mhi/ep/ring.c
> index aeb53b2c34a8cd859393529d0c8860462bc687ed..26357ee68dee984d70ae5bf39f8f09f2cbcafe30 100644
> --- a/drivers/bus/mhi/ep/ring.c
> +++ b/drivers/bus/mhi/ep/ring.c
> @@ -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)
>
> ---
> base-commit: 1e26c5e28ca5821a824e90dd359556f5e9e7b89f
> change-id: 20250328-rp_fix-d7ebc18bc3be
>
> Best regards,
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH] bus: mhi: ep: Update read pointer only after buffer is written
2025-04-09 10:47 [PATCH] bus: mhi: ep: Update read pointer only after buffer is written Sumit Kumar
2025-04-09 11:12 ` Krishna Chaitanya Chundru
@ 2025-04-09 14:41 ` Jeff Hugo
2025-04-15 7:24 ` Manivannan Sadhasivam
2025-04-15 7:26 ` Manivannan Sadhasivam
3 siblings, 0 replies; 5+ messages in thread
From: Jeff Hugo @ 2025-04-09 14:41 UTC (permalink / raw)
To: Sumit Kumar, Manivannan Sadhasivam, Alex Elder, Greg Kroah-Hartman
Cc: mhi, linux-arm-msm, linux-kernel, quic_krichai, quic_akhvin,
quic_skananth, quic_vbadigan, stable, Youssef Samir
On 4/9/2025 4:47 AM, Sumit Kumar wrote:
> 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>
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH] bus: mhi: ep: Update read pointer only after buffer is written
2025-04-09 10:47 [PATCH] bus: mhi: ep: Update read pointer only after buffer is written Sumit Kumar
2025-04-09 11:12 ` Krishna Chaitanya Chundru
2025-04-09 14:41 ` Jeff Hugo
@ 2025-04-15 7:24 ` Manivannan Sadhasivam
2025-04-15 7:26 ` Manivannan Sadhasivam
3 siblings, 0 replies; 5+ messages in thread
From: Manivannan Sadhasivam @ 2025-04-15 7:24 UTC (permalink / raw)
To: Sumit Kumar
Cc: Alex Elder, Greg Kroah-Hartman, mhi, linux-arm-msm, linux-kernel,
quic_krichai, quic_akhvin, quic_skananth, quic_vbadigan, stable,
Youssef Samir
On Wed, Apr 09, 2025 at 04:17:43PM +0530, Sumit Kumar wrote:
> 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.
>
Hmm. I was under assumption that the host wouldn't access the rd_offset before
raising the interrupt.
But anyway, the fix looks legitimate irrespective of that.
> 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: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
- Mani
> ---
> ---
> drivers/bus/mhi/ep/ring.c | 16 ++++++++++------
> 1 file changed, 10 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/bus/mhi/ep/ring.c b/drivers/bus/mhi/ep/ring.c
> index aeb53b2c34a8cd859393529d0c8860462bc687ed..26357ee68dee984d70ae5bf39f8f09f2cbcafe30 100644
> --- a/drivers/bus/mhi/ep/ring.c
> +++ b/drivers/bus/mhi/ep/ring.c
> @@ -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)
>
> ---
> base-commit: 1e26c5e28ca5821a824e90dd359556f5e9e7b89f
> change-id: 20250328-rp_fix-d7ebc18bc3be
>
> Best regards,
> --
> Sumit Kumar <quic_sumk@quicinc.com>
>
--
மணிவண்ணன் சதாசிவம்
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH] bus: mhi: ep: Update read pointer only after buffer is written
2025-04-09 10:47 [PATCH] bus: mhi: ep: Update read pointer only after buffer is written Sumit Kumar
` (2 preceding siblings ...)
2025-04-15 7:24 ` Manivannan Sadhasivam
@ 2025-04-15 7:26 ` Manivannan Sadhasivam
3 siblings, 0 replies; 5+ messages in thread
From: Manivannan Sadhasivam @ 2025-04-15 7:26 UTC (permalink / raw)
To: Sumit Kumar
Cc: Alex Elder, Greg Kroah-Hartman, mhi, linux-arm-msm, linux-kernel,
quic_krichai, quic_akhvin, quic_skananth, quic_vbadigan, stable,
Youssef Samir
On Wed, Apr 09, 2025 at 04:17:43PM +0530, Sumit Kumar wrote:
> 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>
Applied to mhi-fixes!
- Mani
> ---
> ---
> drivers/bus/mhi/ep/ring.c | 16 ++++++++++------
> 1 file changed, 10 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/bus/mhi/ep/ring.c b/drivers/bus/mhi/ep/ring.c
> index aeb53b2c34a8cd859393529d0c8860462bc687ed..26357ee68dee984d70ae5bf39f8f09f2cbcafe30 100644
> --- a/drivers/bus/mhi/ep/ring.c
> +++ b/drivers/bus/mhi/ep/ring.c
> @@ -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)
>
> ---
> base-commit: 1e26c5e28ca5821a824e90dd359556f5e9e7b89f
> change-id: 20250328-rp_fix-d7ebc18bc3be
>
> Best regards,
> --
> Sumit Kumar <quic_sumk@quicinc.com>
>
--
மணிவண்ணன் சதாசிவம்
^ permalink raw reply [flat|nested] 5+ messages in thread