From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796463776; b=HGxonQjcdFtS/BqBzSeMaaGtHTEd8QkWKTOsOYMdVad1UaYnUW91zWGXDcUKv7Om+YPR 4EGvOZAEF9DUbUPyw2FCctqzhZKoc4YZ1ssVnAZ6XVhX+O/74DWZuWlJMa1qVPgGDg2bi crFPHDOUCyMvOK/sGkaBLXDKDzsuLIH/pYCi6/ktAKltkKFvuN+dMIXywNTVIqrrQAmm7 P8o4q3go0IbPG43UAwLbzmNsTwBxhfILVkS12tEbFQFXB6Gg4hcbBvcHodsqJQjvADqk0 hMmXx0V6INU0xLj7tTmChpn2A/73TS7BRbPwpryJvzjFXsgU9qQu5uYWTKOdwpAJn+g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796463776; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=V2MDAvQWrijKMjtuxM7tXOldmDFjDU9UsjOImQK18r8=; b=H75V/wMIj5du5+0Jzy3OnbAS5xxxc0kWaV+OzXkS+4vWW8B4CYSLfuifUBOKo0U5gKXg Mev1KM8a1qmV03IfK42QQiO82aIs2c66eIF6k0xniF0LWb2cAiyBeucdAnzwhiWVoSSYK nQFpZ9edtKQpPJej6DfFePVfDcrqv94QjmU0nrL+gCuKJ7z/xS82uAJtVIOy4cPiHj11I hdfvMKRb3trJ8qJEU0StM4r9v/k2tdRoXlWi70kiZnrwrFvgATL8s6nbTGqoSKhahjK0Y Ys/QOR/6qdCIze3fRKAbgKaEOTH5sQeW3JfD0u0oikHhSz1PM4FbmfxKwz4sEqvv+Zg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=iwkast5E; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A9233606A322 for ; Sat, 5 Dec 2026 09:42:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=iwkast5E Received: by smtp.kernel.org (Postfix) with ESMTPSA id 536951F0092B; Sat, 5 Dec 2026 09:42:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796463776; bh=V2MDAvQWrijKMjtuxM7tXOldmDFjDU9UsjOImQK18r8=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=iwkast5EKG/pBgxUSId1mCnitd+7SLWzvftvXjiTt869MR4JvcolC0LukS+4wAPnJ Mk+H0hcTQtA/FSwusLLhnm98jKHYvDxt20MZxsKhFA7OO9Il5jorIQxUDuPwCPxsG3 THuwN+NTX7y9oPd+U1s7f9JX/7o7PX60N6RebJvK0cpuXxY+sHyV1iG1R8RWa18n2P qh8uAuAj/wkKoy8pkKzmzTLEDHSF9j5VSpKM3LMCvOWdQM16035vcfTzw/+HOq34Mj KYYoRUeYGtDSuhr5RHvEN/GgvMaJ4GGhJ3hLQmKXzRSI9WCfvOZzSyhGfxaZcu4uzt LUaLAVwAOHRWA== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v2] usb: dwc2: hcd: Fix DMA overrun of short IN buffers Reply-To: sashiko-reviews@lists.linux.dev To: "Hunter Laux" Cc: linux-usb@vger.kernel.org In-Reply-To: <20261205091423.1543329-1-hunterlaux@gmail.com> References: <20261205091423.1543329-1-hunterlaux@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Sat, 05 Dec 2026 09:42:55 +0000 X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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 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(stru= ct dwc2_hsotg *hsotg, [ ... ] > +static size_t dwc2_dma_buffer_len(struct urb *urb) > +{ > + size_t len =3D urb->transfer_buffer_length; > + unsigned int maxp; > + int i; > + > + if (!usb_urb_dir_in(urb)) > + return len; > + maxp =3D usb_endpoint_maxp(&urb->ep->desc); > + if (!maxp) > + return len; > + if (!usb_pipeisoc(urb->pipe)) > + return roundup(len, maxp); > + for (i =3D 0; i < urb->number_of_packets; i++) > + len =3D 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 =3D chan->max_pac= ket. On a 512-byte frame with maxp =3D 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 =3D dwc2_dma_buffer_len(urb); > + if (len =3D=3D urb->transfer_buffer_length && > !((uintptr_t)urb->transfer_buffer & (DWC2_USB_DMA_ALIGN - 1))) > return 0; > =20 > /* > - * 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 =3D urb->transfer_buffer_length + > - (dma_get_cache_alignment() - 1) + > - sizeof(urb->transfer_buffer); > - > - kmalloc_ptr =3D kmalloc(kmalloc_size, mem_flags); > + kmalloc_ptr =3D kmalloc(sizeof(*b) + dma_get_cache_alignment() - 1 + le= n, > + 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_SIZ= E. Bouncing only a trailing partial packet via hsotg->unaligned_cache, or keeping bounce metadata out-of-band, would avoid both regressions. > + buf =3D PTR_ALIGN(kmalloc_ptr + sizeof(*b), dma_get_cache_alignment()); > + b =3D buf - sizeof(*b); > + b->kmalloc_ptr =3D kmalloc_ptr; > + b->old_xfer_buffer =3D 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 =3D 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. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261205091423.1543= 329-1-hunterlaux@gmail.com?part=3D1