From patchwork Fri Jul 6 06:35:37 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?QW50dGkgU2VwcMOkbMOk?= X-Patchwork-Id: 940285 X-Patchwork-Delegate: hauke@hauke-m.de Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=lists.openwrt.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="nwNp9d0L"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="imAvCjy9"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 41MQ3y1fGrz9s4Z for ; Fri, 6 Jul 2018 16:36:30 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Subject:MIME-Version:Message-Id:Date:To :From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=waShBT6ykLwZJtcoMdcshZUGRhb/5A1JuZkOV4CwVyw=; b=nwNp9d0LDXZe7N h2PWAAPloClNC1Nc1dpqy7xbxKEkS+blQDr+bEH6EL2lCvxNNaM4eHXJdjXGYdbq/mlmZ8iZu5Go4 jSzKcMXJE5QgeFlnU1LV0QxH+8b9togcouZJY3OnjbPr1M2doRXWDMBpLa7CLagGw+orf+ydp5zu+ /6FIVoyDTDAUw5f6SKX5HWNYfZX6pZqd70nUOagj7ys2/PQxIeGda8rwicW16j3ib8K2A3qAtQszy HUjT3daJuh+h9OCSZuUjvYVKjT/aJKAFtS3C/6Jg07LbyGPyIPXizBHe7DqOTydD0To46ewjI1dju HQMCQdjMJb6AnHlGnnzA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1fbKLv-0003tt-SY; Fri, 06 Jul 2018 06:36:23 +0000 Received: from mail-lj1-x244.google.com ([2a00:1450:4864:20::244]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fbKLs-0003t7-60 for openwrt-devel@lists.openwrt.org; Fri, 06 Jul 2018 06:36:21 +0000 Received: by mail-lj1-x244.google.com with SMTP id r13-v6so8334743ljg.10 for ; Thu, 05 Jul 2018 23:36:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=rTmEuFxUHBN4aAYMtphHeutMUBU9oHYsAipmSg6Hygo=; b=imAvCjy9GPKR2TeS8nEnUWV2NTi/RoUq4n9z8c3qP3PLelClga7KL7MTyHx6Jbstaq 0vU7p+Ry4itBIr1NDqx4YscE34HDx3Erw+uApTnIdQCVLRxrxdgOwGVMbrb0KtaX7PJw MEsBn/Efnl/RHCnx74PDAeupQVYKHo3sToP2q20WKnQ7Tw/+f9MCG8w64XnqPv/dHKOZ MJYVjz9NFeAyC9VoDPRwyESy2cFraJvPqtHLtfcUk9IiF7hRKQLrWv9gHXO8iCp4z4oo tJ8Q02iCvmMw0KRH0o7hiKDunoz4oxzVMGtto1V7+56cxhHBRe35aZOY+B1ahbuIwIRM 95Rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=rTmEuFxUHBN4aAYMtphHeutMUBU9oHYsAipmSg6Hygo=; b=Mt3qmbyc7Vu1R1yilBa6TUOpTPQD3/LHGk8LA9LEIJ31C71Sm8+SoH+2p9QjpFvYq4 XAkcOiLuzQwkfzSJ69tEUhKkJ9BowlZ+yd5vuOBpRkI8tMwjVScT2c/4Qkeiez2S2ovN agVm19I2XstlaBjGygBoYQxOAilqOdjtO7znstDoYJ7+Kbxmf+8mZgpyczz1BtZKT7n6 pbwLgDKdh7xWD0LsBC32iMYIrMPR5PXmwprjbj8DCiokG9y4UsMA5t/nxWP8IKskSA4D hPGzBPXXf/3iMf3UCXKG2xPEOwVuYqPXLMlTo4YkYIHX+NQhV6LiGxvo4yFvXA6ylP9/ i1xA== X-Gm-Message-State: APt69E1kWI+z/dllVazWCCxfiapFMUkHgAqW/GEapudwvKX1iSuijew8 BbrPElBGthUf7lLM20Xkyqw= X-Google-Smtp-Source: AAOMgpfrQxABV2amh1golv2Tn5oXaBZI7CcucIlxijobWHJAMiZ9jr2kkIF9900928X0PtXPMsT1vw== X-Received: by 2002:a2e:40c:: with SMTP id 12-v6mr5895878lje.146.1530858967718; Thu, 05 Jul 2018 23:36:07 -0700 (PDT) Received: from griffin.lan (dtyqvkyyyyyyyyyyyyydt-3.rev.dnainternet.fi. [2001:14ba:801b:1400::7]) by smtp.gmail.com with ESMTPSA id t9-v6sm1170807ljt.21.2018.07.05.23.36.06 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 05 Jul 2018 23:36:06 -0700 (PDT) From: =?utf-8?b?QW50dGkgU2VwcMOkbMOk?= To: John Crispin , Mathias Kresin , Hauke Mehrtens Date: Fri, 6 Jul 2018 09:35:37 +0300 Message-Id: <20180706063537.1813-1-a.seppala@gmail.com> X-Mailer: git-send-email 2.13.6 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180705_233620_248060_9BA76C6C X-CRM114-Status: GOOD ( 21.84 ) X-Spam-Score: -0.1 (/) X-Spam-Report: SpamAssassin version 3.4.1 on bombadil.infradead.org summary: Content analysis details: (-0.1 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/, no trust [2a00:1450:4864:20:0:0:0:244 listed in] [list.dnswl.org] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (a.seppala[at]gmail.com) -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid Subject: [OpenWrt-Devel] [PATCH] kernel: usb: dwc2 DMA alignment fixes X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?utf-8?b?QW50dGkgU2VwcMOkbMOk?= , openwrt-devel@lists.openwrt.org Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org Add two patches submitted for upstream review that significantly improve the dwc2 driver on openwrt from kernel stability and performance perspectives. Fixes: FS#1367 Signed-off-by: Antti Seppälä --- Notes: Given 170-usb-dwc2-Fix-DMA-alignment-to-start-at-allocated-boun.patch fixes an upstream regression back-porting the patch to openwrt-18.06 branch and kernels therein might also be a good idea ...-DMA-alignment-to-start-at-allocated-boun.patch | 131 +++++++++++++++++++++ ...Fix-inefficient-copy-of-unaligned-buffers.patch | 55 +++++++++ 2 files changed, 186 insertions(+) create mode 100644 target/linux/generic/pending-4.14/170-usb-dwc2-Fix-DMA-alignment-to-start-at-allocated-boun.patch create mode 100644 target/linux/generic/pending-4.14/171-usb-dwc2-Fix-inefficient-copy-of-unaligned-buffers.patch diff --git a/target/linux/generic/pending-4.14/170-usb-dwc2-Fix-DMA-alignment-to-start-at-allocated-boun.patch b/target/linux/generic/pending-4.14/170-usb-dwc2-Fix-DMA-alignment-to-start-at-allocated-boun.patch new file mode 100644 index 0000000000..481e6c7ecc --- /dev/null +++ b/target/linux/generic/pending-4.14/170-usb-dwc2-Fix-DMA-alignment-to-start-at-allocated-boun.patch @@ -0,0 +1,131 @@ +From 028c9191bdf88f120f65626920a6a679170fcc3e Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Antti=20Sepp=C3=A4l=C3=A4?= +Date: Thu, 5 Jul 2018 11:37:03 +0300 +Subject: [PATCH 1/2] usb: dwc2: Fix DMA alignment to start at allocated + boundary +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit + +The commit 3bc04e28a030 ("usb: dwc2: host: Get aligned DMA in a more +supported way") introduced a common way to align DMA allocations. +The code in the commit aligns the struct dma_aligned_buffer but the +actual DMA address pointed by data[0] gets aligned to an offset from +the allocated boundary by the kmalloc_ptr and the old_xfer_buffer +pointers. + +This is against the recommendation in Documentation/DMA-API.txt which +states: + + Therefore, it is recommended that driver writers who don't take + special care to determine the cache line size at run time only map + virtual regions that begin and end on page boundaries (which are + guaranteed also to be cache line boundaries). + +The effect of this is that architectures with non-coherent DMA caches +may run into memory corruption or kernel crashes with Unhandled +kernel unaligned accesses exceptions. + +Fix the alignment by positioning the DMA area in front of the allocation +and use memory at the end of the area for storing the orginal +transfer_buffer pointer. This may have the added benefit of increased +performance as the DMA area is now fully aligned on all architectures. + +Tested with Lantiq xRX200 (MIPS) and RPi Model B Rev 2 (ARM). + +Fixes: 3bc04e28a030 ("usb: dwc2: host: Get aligned DMA in a more +supported way") + +Signed-off-by: Antti Seppälä +--- + drivers/usb/dwc2/hcd.c | 44 +++++++++++++++++++++++--------------------- + 1 file changed, 23 insertions(+), 21 deletions(-) + +diff --git a/drivers/usb/dwc2/hcd.c b/drivers/usb/dwc2/hcd.c +index edaf0b6af4f0..056eb7323be7 100644 +--- a/drivers/usb/dwc2/hcd.c ++++ b/drivers/usb/dwc2/hcd.c +@@ -2628,34 +2628,29 @@ static void dwc2_hc_init_xfer(struct dwc2_hsotg *hsotg, + + #define DWC2_USB_DMA_ALIGN 4 + +-struct dma_aligned_buffer { +- void *kmalloc_ptr; +- void *old_xfer_buffer; +- u8 data[0]; +-}; +- + static void dwc2_free_dma_aligned_buffer(struct urb *urb) + { +- struct dma_aligned_buffer *temp; ++ void *stored_xfer_buffer; + + if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER)) + return; + +- temp = container_of(urb->transfer_buffer, +- struct dma_aligned_buffer, data); ++ /* 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)); + + if (usb_urb_dir_in(urb)) +- memcpy(temp->old_xfer_buffer, temp->data, ++ memcpy(stored_xfer_buffer, urb->transfer_buffer, + urb->transfer_buffer_length); +- urb->transfer_buffer = temp->old_xfer_buffer; +- kfree(temp->kmalloc_ptr); ++ kfree(urb->transfer_buffer); ++ urb->transfer_buffer = stored_xfer_buffer; + + urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER; + } + + static int dwc2_alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags) + { +- struct dma_aligned_buffer *temp, *kmalloc_ptr; ++ void *kmalloc_ptr; + size_t kmalloc_size; + + if (urb->num_sgs || urb->sg || +@@ -2663,22 +2658,29 @@ static int dwc2_alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags) + !((uintptr_t)urb->transfer_buffer & (DWC2_USB_DMA_ALIGN - 1))) + return 0; + +- /* Allocate a buffer with enough padding for alignment */ ++ /* ++ * Allocate a buffer with enough padding for original transfer_buffer ++ * pointer. This allocation is guaranteed to be aligned properly for ++ * DMA ++ */ + kmalloc_size = urb->transfer_buffer_length + +- sizeof(struct dma_aligned_buffer) + DWC2_USB_DMA_ALIGN - 1; ++ sizeof(urb->transfer_buffer); + + kmalloc_ptr = kmalloc(kmalloc_size, mem_flags); + if (!kmalloc_ptr) + return -ENOMEM; + +- /* Position our struct dma_aligned_buffer such that data is aligned */ +- temp = PTR_ALIGN(kmalloc_ptr + 1, DWC2_USB_DMA_ALIGN) - 1; +- temp->kmalloc_ptr = kmalloc_ptr; +- temp->old_xfer_buffer = urb->transfer_buffer; ++ /* ++ * Position value of original urb->transfer_buffer pointer to the end ++ * of allocation for later referencing ++ */ ++ memcpy(kmalloc_ptr + urb->transfer_buffer_length, ++ &urb->transfer_buffer, sizeof(urb->transfer_buffer)); ++ + if (usb_urb_dir_out(urb)) +- memcpy(temp->data, urb->transfer_buffer, ++ memcpy(kmalloc_ptr, urb->transfer_buffer, + urb->transfer_buffer_length); +- urb->transfer_buffer = temp->data; ++ urb->transfer_buffer = kmalloc_ptr; + + urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER; + +-- +2.13.6 + diff --git a/target/linux/generic/pending-4.14/171-usb-dwc2-Fix-inefficient-copy-of-unaligned-buffers.patch b/target/linux/generic/pending-4.14/171-usb-dwc2-Fix-inefficient-copy-of-unaligned-buffers.patch new file mode 100644 index 0000000000..728fb6e13b --- /dev/null +++ b/target/linux/generic/pending-4.14/171-usb-dwc2-Fix-inefficient-copy-of-unaligned-buffers.patch @@ -0,0 +1,55 @@ +From 81da1738eee68f1961e03bdeb2d60cf0eb4dd713 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Antti=20Sepp=C3=A4l=C3=A4?= +Date: Thu, 5 Jul 2018 12:06:18 +0300 +Subject: [PATCH 2/2] usb: dwc2: Fix inefficient copy of unaligned buffers +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit + +Make sure only to copy any actual data rather than the whole buffer, +when releasing the temporary buffer used for unaligned non-isochronous +transfers. + +Taken directly from commit 0efd937e27d5e ("USB: ehci-tegra: fix inefficient +copy of unaligned buffers") + +Tested with Lantiq xRX200 (MIPS) and RPi Model B Rev 2 (ARM) + +Signed-off-by: Antti Seppälä +--- + drivers/usb/dwc2/hcd.c | 12 +++++++++--- + 1 file changed, 9 insertions(+), 3 deletions(-) + +diff --git a/drivers/usb/dwc2/hcd.c b/drivers/usb/dwc2/hcd.c +index 056eb7323be7..bbaae5451727 100644 +--- a/drivers/usb/dwc2/hcd.c ++++ b/drivers/usb/dwc2/hcd.c +@@ -2631,6 +2631,7 @@ static void dwc2_hc_init_xfer(struct dwc2_hsotg *hsotg, + static void dwc2_free_dma_aligned_buffer(struct urb *urb) + { + void *stored_xfer_buffer; ++ size_t length; + + if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER)) + return; +@@ -2639,9 +2640,14 @@ static void dwc2_free_dma_aligned_buffer(struct urb *urb) + memcpy(&stored_xfer_buffer, urb->transfer_buffer + + urb->transfer_buffer_length, sizeof(urb->transfer_buffer)); + +- if (usb_urb_dir_in(urb)) +- memcpy(stored_xfer_buffer, urb->transfer_buffer, +- urb->transfer_buffer_length); ++ if (usb_urb_dir_in(urb)) { ++ if (usb_pipeisoc(urb->pipe)) ++ length = urb->transfer_buffer_length; ++ else ++ length = urb->actual_length; ++ ++ memcpy(stored_xfer_buffer, urb->transfer_buffer, length); ++ } + kfree(urb->transfer_buffer); + urb->transfer_buffer = stored_xfer_buffer; + +-- +2.13.6 +