LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH] usb: dwc2: Fix DMA cache alignment issues
@ 2019-02-18  6:37 Martin Schiller
  2019-05-29 18:30 ` Doug Anderson
  0 siblings, 1 reply; 3+ messages in thread
From: Martin Schiller @ 2019-02-18  6:37 UTC (permalink / raw)
  To: hminas, gregkh, felipe.balbi, a.seppala, dianders
  Cc: linux-usb, linux-kernel, Martin Schiller

Insert a padding between data and the stored_xfer_buffer pointer to
ensure they are not on the same cache line.

Otherwise, the stored_xfer_buffer gets corrupted for IN URBs on
non-cache-coherent systems. (In my case: Lantiq xRX200 MIPS)

Fixes: 3bc04e28a030 ("usb: dwc2: host: Get aligned DMA in a more supported way")
Fixes: 56406e017a88 ("usb: dwc2: Fix DMA alignment to start at allocated boundary")
Signed-off-by: Martin Schiller <ms@dev.tdt.de>
---
 drivers/usb/dwc2/hcd.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/drivers/usb/dwc2/hcd.c b/drivers/usb/dwc2/hcd.c
index dd82fa516f3f..f3035dd4db25 100644
--- a/drivers/usb/dwc2/hcd.c
+++ b/drivers/usb/dwc2/hcd.c
@@ -2664,8 +2664,10 @@ static void dwc2_free_dma_aligned_buffer(struct urb *urb)
 		return;
 
 	/* Restore urb->transfer_buffer from the end of the allocated area */
-	memcpy(&stored_xfer_buffer, urb->transfer_buffer +
-	       urb->transfer_buffer_length, sizeof(urb->transfer_buffer));
+	memcpy(&stored_xfer_buffer,
+	       PTR_ALIGN(urb->transfer_buffer + urb->transfer_buffer_length,
+			 dma_get_cache_alignment()),
+	       sizeof(urb->transfer_buffer));
 
 	if (usb_urb_dir_in(urb)) {
 		if (usb_pipeisoc(urb->pipe))
@@ -2697,6 +2699,7 @@ static int dwc2_alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags)
 	 * DMA
 	 */
 	kmalloc_size = urb->transfer_buffer_length +
+		(dma_get_cache_alignment() - 1) +
 		sizeof(urb->transfer_buffer);
 
 	kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
@@ -2707,7 +2710,8 @@ static int dwc2_alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags)
 	 * Position value of original urb->transfer_buffer pointer to the end
 	 * of allocation for later referencing
 	 */
-	memcpy(kmalloc_ptr + urb->transfer_buffer_length,
+	memcpy(PTR_ALIGN(kmalloc_ptr + urb->transfer_buffer_length,
+			 dma_get_cache_alignment()),
 	       &urb->transfer_buffer, sizeof(urb->transfer_buffer));
 
 	if (usb_urb_dir_out(urb))
-- 
2.11.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] usb: dwc2: Fix DMA cache alignment issues
  2019-02-18  6:37 [PATCH] usb: dwc2: Fix DMA cache alignment issues Martin Schiller
@ 2019-05-29 18:30 ` Doug Anderson
  2019-05-30 10:29   ` Minas Harutyunyan
  0 siblings, 1 reply; 3+ messages in thread
From: Doug Anderson @ 2019-05-29 18:30 UTC (permalink / raw)
  To: Martin Schiller, Felipe Balbi
  Cc: Minas Harutyunyan, Greg Kroah-Hartman, Antti Seppälä,
	linux-usb, LKML, Tomasz Figa

Hi,

On Sun, Feb 17, 2019 at 10:37 PM Martin Schiller <ms@dev.tdt.de> wrote:
>
> Insert a padding between data and the stored_xfer_buffer pointer to
> ensure they are not on the same cache line.
>
> Otherwise, the stored_xfer_buffer gets corrupted for IN URBs on
> non-cache-coherent systems. (In my case: Lantiq xRX200 MIPS)
>
> Fixes: 3bc04e28a030 ("usb: dwc2: host: Get aligned DMA in a more supported way")
> Fixes: 56406e017a88 ("usb: dwc2: Fix DMA alignment to start at allocated boundary")
> Signed-off-by: Martin Schiller <ms@dev.tdt.de>
> ---
>  drivers/usb/dwc2/hcd.c | 10 +++++++---
>  1 file changed, 7 insertions(+), 3 deletions(-)

This patch has been in the back of my mind for a while bug I never got
around to it.  Today I was debugging memory corruption problems when
using a webcam on dwc2 on rk3288-veyron-jerry.  This patch appears to
solve my problems nicely.  Thanks!

Tested-by: Douglas Anderson <dianders@chromium.org>
Reviewed-by: Douglas Anderson <dianders@chromium.org>
Cc: <stable@vger.kernel.org>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] usb: dwc2: Fix DMA cache alignment issues
  2019-05-29 18:30 ` Doug Anderson
@ 2019-05-30 10:29   ` Minas Harutyunyan
  0 siblings, 0 replies; 3+ messages in thread
From: Minas Harutyunyan @ 2019-05-30 10:29 UTC (permalink / raw)
  To: Doug Anderson, Martin Schiller, Felipe Balbi
  Cc: Greg Kroah-Hartman, Antti Seppälä,
	linux-usb, LKML, Tomasz Figa

On 5/29/2019 10:31 PM, Doug Anderson wrote:
> Hi,
> 
> On Sun, Feb 17, 2019 at 10:37 PM Martin Schiller <ms@dev.tdt.de> wrote:
>>
>> Insert a padding between data and the stored_xfer_buffer pointer to
>> ensure they are not on the same cache line.
>>
>> Otherwise, the stored_xfer_buffer gets corrupted for IN URBs on
>> non-cache-coherent systems. (In my case: Lantiq xRX200 MIPS)
>>
>> Fixes: 3bc04e28a030 ("usb: dwc2: host: Get aligned DMA in a more supported way")
>> Fixes: 56406e017a88 ("usb: dwc2: Fix DMA alignment to start at allocated boundary")
>> Signed-off-by: Martin Schiller <ms@dev.tdt.de>
>> ---
>>   drivers/usb/dwc2/hcd.c | 10 +++++++---
>>   1 file changed, 7 insertions(+), 3 deletions(-)
> 
> This patch has been in the back of my mind for a while bug I never got
> around to it.  Today I was debugging memory corruption problems when
> using a webcam on dwc2 on rk3288-veyron-jerry.  This patch appears to
> solve my problems nicely.  Thanks!
> 
> Tested-by: Douglas Anderson <dianders@chromium.org>
> Reviewed-by: Douglas Anderson <dianders@chromium.org>
> Cc: <stable@vger.kernel.org>
> 
Acked-by: Minas Harutyunyan <hminas@synopsys.com>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-05-30 10:30 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-02-18  6:37 [PATCH] usb: dwc2: Fix DMA cache alignment issues Martin Schiller
2019-05-29 18:30 ` Doug Anderson
2019-05-30 10:29   ` Minas Harutyunyan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).