170-usb-dwc2-Fix-DMA-alignment-to-start-at-allocated-boun.patch 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. From 028c9191bdf88f120f65626920a6a679170fcc3e Mon Sep 17 00:00:00 2001
  2. From: =?UTF-8?q?Antti=20Sepp=C3=A4l=C3=A4?= <[email protected]>
  3. Date: Thu, 5 Jul 2018 11:37:03 +0300
  4. Subject: [PATCH 1/2] usb: dwc2: Fix DMA alignment to start at allocated
  5. boundary
  6. MIME-Version: 1.0
  7. Content-Type: text/plain; charset=UTF-8
  8. Content-Transfer-Encoding: 8bit
  9. The commit 3bc04e28a030 ("usb: dwc2: host: Get aligned DMA in a more
  10. supported way") introduced a common way to align DMA allocations.
  11. The code in the commit aligns the struct dma_aligned_buffer but the
  12. actual DMA address pointed by data[0] gets aligned to an offset from
  13. the allocated boundary by the kmalloc_ptr and the old_xfer_buffer
  14. pointers.
  15. This is against the recommendation in Documentation/DMA-API.txt which
  16. states:
  17. Therefore, it is recommended that driver writers who don't take
  18. special care to determine the cache line size at run time only map
  19. virtual regions that begin and end on page boundaries (which are
  20. guaranteed also to be cache line boundaries).
  21. The effect of this is that architectures with non-coherent DMA caches
  22. may run into memory corruption or kernel crashes with Unhandled
  23. kernel unaligned accesses exceptions.
  24. Fix the alignment by positioning the DMA area in front of the allocation
  25. and use memory at the end of the area for storing the orginal
  26. transfer_buffer pointer. This may have the added benefit of increased
  27. performance as the DMA area is now fully aligned on all architectures.
  28. Tested with Lantiq xRX200 (MIPS) and RPi Model B Rev 2 (ARM).
  29. Fixes: 3bc04e28a030 ("usb: dwc2: host: Get aligned DMA in a more
  30. supported way")
  31. Signed-off-by: Antti Seppälä <[email protected]>
  32. ---
  33. drivers/usb/dwc2/hcd.c | 44 +++++++++++++++++++++++---------------------
  34. 1 file changed, 23 insertions(+), 21 deletions(-)
  35. --- a/drivers/usb/dwc2/hcd.c
  36. +++ b/drivers/usb/dwc2/hcd.c
  37. @@ -2628,34 +2628,29 @@ static void dwc2_hc_init_xfer(struct dwc
  38. #define DWC2_USB_DMA_ALIGN 4
  39. -struct dma_aligned_buffer {
  40. - void *kmalloc_ptr;
  41. - void *old_xfer_buffer;
  42. - u8 data[0];
  43. -};
  44. -
  45. static void dwc2_free_dma_aligned_buffer(struct urb *urb)
  46. {
  47. - struct dma_aligned_buffer *temp;
  48. + void *stored_xfer_buffer;
  49. if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
  50. return;
  51. - temp = container_of(urb->transfer_buffer,
  52. - struct dma_aligned_buffer, data);
  53. + /* Restore urb->transfer_buffer from the end of the allocated area */
  54. + memcpy(&stored_xfer_buffer, urb->transfer_buffer +
  55. + urb->transfer_buffer_length, sizeof(urb->transfer_buffer));
  56. if (usb_urb_dir_in(urb))
  57. - memcpy(temp->old_xfer_buffer, temp->data,
  58. + memcpy(stored_xfer_buffer, urb->transfer_buffer,
  59. urb->transfer_buffer_length);
  60. - urb->transfer_buffer = temp->old_xfer_buffer;
  61. - kfree(temp->kmalloc_ptr);
  62. + kfree(urb->transfer_buffer);
  63. + urb->transfer_buffer = stored_xfer_buffer;
  64. urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
  65. }
  66. static int dwc2_alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags)
  67. {
  68. - struct dma_aligned_buffer *temp, *kmalloc_ptr;
  69. + void *kmalloc_ptr;
  70. size_t kmalloc_size;
  71. if (urb->num_sgs || urb->sg ||
  72. @@ -2663,22 +2658,29 @@ static int dwc2_alloc_dma_aligned_buffer
  73. !((uintptr_t)urb->transfer_buffer & (DWC2_USB_DMA_ALIGN - 1)))
  74. return 0;
  75. - /* Allocate a buffer with enough padding for alignment */
  76. + /*
  77. + * Allocate a buffer with enough padding for original transfer_buffer
  78. + * pointer. This allocation is guaranteed to be aligned properly for
  79. + * DMA
  80. + */
  81. kmalloc_size = urb->transfer_buffer_length +
  82. - sizeof(struct dma_aligned_buffer) + DWC2_USB_DMA_ALIGN - 1;
  83. + sizeof(urb->transfer_buffer);
  84. kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
  85. if (!kmalloc_ptr)
  86. return -ENOMEM;
  87. - /* Position our struct dma_aligned_buffer such that data is aligned */
  88. - temp = PTR_ALIGN(kmalloc_ptr + 1, DWC2_USB_DMA_ALIGN) - 1;
  89. - temp->kmalloc_ptr = kmalloc_ptr;
  90. - temp->old_xfer_buffer = urb->transfer_buffer;
  91. + /*
  92. + * Position value of original urb->transfer_buffer pointer to the end
  93. + * of allocation for later referencing
  94. + */
  95. + memcpy(kmalloc_ptr + urb->transfer_buffer_length,
  96. + &urb->transfer_buffer, sizeof(urb->transfer_buffer));
  97. +
  98. if (usb_urb_dir_out(urb))
  99. - memcpy(temp->data, urb->transfer_buffer,
  100. + memcpy(kmalloc_ptr, urb->transfer_buffer,
  101. urb->transfer_buffer_length);
  102. - urb->transfer_buffer = temp->data;
  103. + urb->transfer_buffer = kmalloc_ptr;
  104. urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;