Linux USB
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Hunter Laux" <hunterlaux@gmail.com>
Cc: linux-usb@vger.kernel.org
Subject: Re: [PATCH v2] usb: dwc2: hcd: Fix DMA overrun of short IN buffers
Date: Sat, 05 Dec 2026 09:42:55 +0000	[thread overview]
Message-ID: <sashiko-outbox-251353@kernel.org> (raw)
In-Reply-To: <20261205091423.1543329-1-hunterlaux@gmail.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] usb: dwc2: dwc2_map_urb_for_dma() maps only
  transfer_buffer_length of bounce
- [High] usb: dwc2: dwc2_dma_buffer_len() undersizes split isochronous
  IN DMA buffers
- [Medium] usb: dwc2: full-buffer atomic bounce and slab order doubling
  on IN URBs
--

commit f7f9fce1ced0a74b724b9709a3acd77890be1cad
Author: Hunter Laux <hunterlaux@gmail.com>

usb: dwc2: hcd: Fix DMA overrun of short IN buffers

This commit extends dwc2's DMA bounce buffer path to also bounce aligned
IN URBs whose buffers are not sized to whole max-size packets, moves the
bounce bookkeeping ahead of the data on its own cache line, and copies
back only each isochronous frame's received bytes.

Link: https://lore.kernel.org/linux-usb/20200226210414.28133-2-linux@roeck-us.net/

> diff --git a/drivers/usb/dwc2/hcd.c b/drivers/usb/dwc2/hcd.c
> index 40ab50538334a..045ffdfb9de4b 100644
> --- a/drivers/usb/dwc2/hcd.c
> +++ b/drivers/usb/dwc2/hcd.c
> @@ -2452,69 +2452,108 @@ static int dwc2_alloc_split_dma_aligned_buf(struct dwc2_hsotg *hsotg,

[ ... ]

> +static size_t dwc2_dma_buffer_len(struct urb *urb)
> +{
> +	size_t len = urb->transfer_buffer_length;
> +	unsigned int maxp;
> +	int i;
> +
> +	if (!usb_urb_dir_in(urb))
> +		return len;
> +	maxp = usb_endpoint_maxp(&urb->ep->desc);
> +	if (!maxp)
> +		return len;
> +	if (!usb_pipeisoc(urb->pipe))
> +		return roundup(len, maxp);
> +	for (i = 0; i < urb->number_of_packets; i++)
> +		len = max_t(size_t, len, urb->iso_frame_desc[i].offset +
> +			    roundup(urb->iso_frame_desc[i].length, maxp));
> +	return len;
> +}

[Severity: High]
Does dwc2_dma_buffer_len() undersize the DMA buffer for split isochronous
IN transfers?

For Full-Speed isochronous IN endpoints behind a High-Speed hub,
dwc2_hc_init_xfer() adds qtd->isoc_split_offset (188-byte 4-byte-aligned
chunks, bypassing dwc2_alloc_split_dma_aligned_buf()) to chan->xfer_dma,
while dwc2_hc_start_transfer() always sets chan->xfer_len = chan->max_packet.

On a 512-byte frame with maxp = 512 at offset 0, dwc2_dma_buffer_len()
returns 512 and skips bouncing, yet the third CSPLIT at offset 376 programs
512 bytes (reaching offset 888), letting a short final packet overrun past
byte 512 into adjacent heap memory.

A zero-length split isochronous IN frame hits the same overrun: roundup(0,
maxp) is 0, while dwc2_hc_start_transfer() still programs maxp bytes at
frame_desc->offset.

[ ... ]

> +	/* Aligned and big enough for what the controller writes: use it */
> +	len = dwc2_dma_buffer_len(urb);
> +	if (len == urb->transfer_buffer_length &&
>  	    !((uintptr_t)urb->transfer_buffer & (DWC2_USB_DMA_ALIGN - 1)))
>  		return 0;
>  
>  	/*
> -	 * Allocate a buffer with enough padding for original transfer_buffer
> -	 * pointer. This allocation is guaranteed to be aligned properly for
> -	 * DMA
> +	 * The data starts on a cache line of its own, with the bookkeeping in
> +	 * front of it, in the previous line: nothing the controller writes
> +	 * shares a cache line with it, and the free path finds it without
> +	 * recomputing the length.
>  	 */
> -	kmalloc_size = urb->transfer_buffer_length +
> -		(dma_get_cache_alignment() - 1) +
> -		sizeof(urb->transfer_buffer);
> -
> -	kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
> +	kmalloc_ptr = kmalloc(sizeof(*b) + dma_get_cache_alignment() - 1 + len,
> +			      mem_flags);
>  	if (!kmalloc_ptr)
>  		return -ENOMEM;

[Severity: Medium]
Won't bouncing every aligned non-packet-multiple IN URB regress hot receive
paths and double the slab or page order for power-of-two lengths?

Previously these URBs ran zero-copy.  Drivers such as usbnet rx_submit() on
arm64 (where NET_IP_ALIGN is 0), btusb, and uas (112-byte sense_iu status
URBs) now incur a per-URB GFP_ATOMIC kmalloc(), full-buffer memcpy(), and
kfree() on every receive URB.

Adding sizeof(*b) + dma_get_cache_alignment() - 1 to a power-of-two len
pushes every allocation into the next slab cache or buddy order (512 bytes
to kmalloc-1024, 4096 to 8 KiB).  For large usbfs bulk IN URBs submitted
with GFP_ATOMIC in devio.c, this adds -ENOMEM failures past KMALLOC_MAX_SIZE.

Bouncing only a trailing partial packet via hsotg->unaligned_cache, or
keeping bounce metadata out-of-band, would avoid both regressions.

> +	buf = PTR_ALIGN(kmalloc_ptr + sizeof(*b), dma_get_cache_alignment());
> +	b = buf - sizeof(*b);
> +	b->kmalloc_ptr = kmalloc_ptr;
> +	b->old_xfer_buffer = urb->transfer_buffer;

[Severity: High]
Can dwc2_map_urb_for_dma() leave the rounded-up tail of the bounce buffer
unmapped for DMA?

dwc2_map_urb_for_dma() still delegates to usb_hcd_map_urb_for_dma(), which
maps only urb->transfer_buffer_length bytes via dma_map_single() instead of
the full len programmed into the controller.

Under an IOMMU (__iommu_dma_map()), shifting buf by dma_get_cache_alignment()
can push buf + len past a page boundary.  A page-aligned 3968-byte bulk IN
URB on a 512-byte endpoint (len = 4096) now starts at page0 + 128, mapping
only page 0 so a write past byte 3968 causes an IOMMU fault and AHB error.

Under SWIOTLB (swiotlb_tbl_map_single()), 2 KiB slots are sized from
urb->transfer_buffer_length + offset, so an isochronous IN URB (for example
10 frames of 192 bytes on a 512-byte endpoint, getting 2048 bytes while the
last frame is programmed to byte 2240) can corrupt adjacent SWIOTLB slots.

On non-coherent DMA platforms, dma_map_single() also skips cache
invalidation over the overrun tail past urb->transfer_buffer_length.
Storing len in struct dwc2_dma_bounce and mapping/unmapping the full len
bytes for bounced URBs would fix all three cases.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205091423.1543329-1-hunterlaux@gmail.com?part=1

      reply	other threads:[~2026-12-05  9:42 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05  9:14 [PATCH v2] usb: dwc2: hcd: Fix DMA overrun of short IN buffers Hunter Laux
2026-12-05  9:42 ` sashiko-bot [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-251353@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=hunterlaux@gmail.com \
    --cc=linux-usb@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox