Commit 482b4b71325978f4ed8af471fddfaa6325b11f80

Authored by Sebastian Andrzej Siewior
Committed by Greg Kroah-Hartman
1 parent 7a0c5e1279

usb: core: buffer: smallest buffer should start at ARCH_DMA_MINALIGN

commit 5efd2ea8c9f4f12916ffc8ba636792ce052f6911 upstream.

the following error pops up during "testusb -a -t 10"
| musb-hdrc musb-hdrc.1.auto: dma_pool_free buffer-128,	f134e000/be842000 (bad dma)
hcd_buffer_create() creates a few buffers, the smallest has 32 bytes of
size. ARCH_KMALLOC_MINALIGN is set to 64 bytes. This combo results in
hcd_buffer_alloc() returning memory which is 32 bytes aligned and it
might by identified by buffer_offset() as another buffer. This means the
buffer which is on a 32 byte boundary will not get freed, instead it
tries to free another buffer with the error message.

This patch fixes the issue by creating the smallest DMA buffer with the
size of ARCH_KMALLOC_MINALIGN (or 32 in case ARCH_KMALLOC_MINALIGN is
smaller). This might be 32, 64 or even 128 bytes. The next three pools
will have the size 128, 512 and 2048.
In case the smallest pool is 128 bytes then we have only three pools
instead of four (and zero the first entry in the array).
The last pool size is always 2048 bytes which is the assumed PAGE_SIZE /
2 of 4096. I doubt it makes sense to continue using PAGE_SIZE / 2 where
we would end up with 8KiB buffer in case we have 16KiB pages.
Instead I think it makes sense to have a common size(s) and extend them
if there is need to.
There is a BUILD_BUG_ON() now in case someone has a minalign of more than
128 bytes.

Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Acked-by: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Showing 3 changed files with 19 additions and 9 deletions Side-by-side Diff

drivers/usb/core/buffer.c
... ... @@ -22,17 +22,25 @@
22 22 */
23 23  
24 24 /* FIXME tune these based on pool statistics ... */
25   -static const size_t pool_max[HCD_BUFFER_POOLS] = {
26   - /* platforms without dma-friendly caches might need to
27   - * prevent cacheline sharing...
28   - */
29   - 32,
30   - 128,
31   - 512,
32   - PAGE_SIZE / 2
33   - /* bigger --> allocate pages */
  25 +static size_t pool_max[HCD_BUFFER_POOLS] = {
  26 + 32, 128, 512, 2048,
34 27 };
35 28  
  29 +void __init usb_init_pool_max(void)
  30 +{
  31 + /*
  32 + * The pool_max values must never be smaller than
  33 + * ARCH_KMALLOC_MINALIGN.
  34 + */
  35 + if (ARCH_KMALLOC_MINALIGN <= 32)
  36 + ; /* Original value is okay */
  37 + else if (ARCH_KMALLOC_MINALIGN <= 64)
  38 + pool_max[0] = 64;
  39 + else if (ARCH_KMALLOC_MINALIGN <= 128)
  40 + pool_max[0] = 0; /* Don't use this pool */
  41 + else
  42 + BUILD_BUG(); /* We don't allow this */
  43 +}
36 44  
37 45 /* SETUP primitives */
38 46  
drivers/usb/core/usb.c
... ... @@ -1051,6 +1051,7 @@
1051 1051 pr_info("%s: USB support disabled\n", usbcore_name);
1052 1052 return 0;
1053 1053 }
  1054 + usb_init_pool_max();
1054 1055  
1055 1056 retval = usb_debugfs_init();
1056 1057 if (retval)
include/linux/usb/hcd.h
... ... @@ -450,6 +450,7 @@
450 450 #endif /* CONFIG_PCI */
451 451  
452 452 /* pci-ish (pdev null is ok) buffer alloc/mapping support */
  453 +void usb_init_pool_max(void);
453 454 int hcd_buffer_create(struct usb_hcd *hcd);
454 455 void hcd_buffer_destroy(struct usb_hcd *hcd);
455 456