From patchwork Sat Sep 23 19:41:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?TWljaGHFgiBNaXJvc8WCYXc=?= X-Patchwork-Id: 144052 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp542274vqu; Sun, 24 Sep 2023 00:30:43 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFvWAWtuRCNfmqGK2VSHP5yO0YJEL7h4xxCRranPeYdOpTfoCkENF+/ZH+ggfvYZ/h3htMT X-Received: by 2002:a25:cc08:0:b0:d80:11df:65f7 with SMTP id l8-20020a25cc08000000b00d8011df65f7mr3708379ybf.7.1695540643725; Sun, 24 Sep 2023 00:30:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695540643; cv=none; d=google.com; s=arc-20160816; b=tBLgCcONc/kPf3715JIHiz/qbpZdUuJ94cVYkYV8D6jtOoIr8MaMVIAyhf7bx3RlqA kCDnTSj+X34yRHKlogeLZ+a10MhVFCR87lAiIPuee0TmEYuX7b8+ANtWhSzYhAGTOQ2f 3zsxGomVY4vTt5Oyq9H+nU5ZzcJZFb9q75DKVGu++w6+HfhAkVvxL46WD8qeVMLFV5av Dw03QwBxoBPJ8UqojSPC1QtMvYE8gnr1i6kBMmiXxhiwpAWEoj/eMvh5Zm7JtvEdcoEZ Ieq2qOmD1s0qng3tdEWpoiqTKNUCx8zrRVQyq2UyvHD0qp3xtBv2A0XavDBNC6hSuu1E O6vA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:content-transfer-encoding :mime-version:subject:references:in-reply-to:message-id:date :dkim-signature; bh=2ku4OdoPI4KG3FBjcmClRNams/TdR/4bFrDRispQ5WQ=; fh=CLAIVDlyLZHMC1J3K8WwgWRwY48dgwyGqMcM4Ihmcvo=; b=z+kgdNoQXJWippE+oNxMaEZXrGVo8jX9IICK0FzrDMEqu0XHSf2E2aFKvFNRYkdLAZ /P/sZB/Hwll5Wl6LIRZbThRi19pLtEzaUELn5qcPCaYviLP2dBTtGUal4qYzARRGu8Ft eHMtAo5Hy7ZdDE/dAwOrC7mVD5dUCyXeo8ECRobePeS/f9vWJ1bpDKm0OX9yx/bWnyOf PlFzgFn8top4YI+IAxt3Ouc5evdc2LyS+mZ9Cjal/qyB9HvoOzCtoRYZ8ZFDE8J6h4Us GzVIaq4ci6afg6KJ/C+BKP0ZH401nUmMhMwTO8j5Fs0enw9GiJENeh9tNhmyyUd1lTCi RnGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rere.qmqm.pl header.s=1 header.b=MJuNiuzH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=rere.qmqm.pl Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id l3-20020a654c43000000b00573fc592e9dsi7343591pgr.848.2023.09.24.00.30.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 Sep 2023 00:30:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@rere.qmqm.pl header.s=1 header.b=MJuNiuzH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=rere.qmqm.pl Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 76019833AB09; Sat, 23 Sep 2023 12:42:24 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229665AbjIWTmN (ORCPT + 30 others); Sat, 23 Sep 2023 15:42:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60578 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229476AbjIWTmI (ORCPT ); Sat, 23 Sep 2023 15:42:08 -0400 Received: from rere.qmqm.pl (rere.qmqm.pl [91.227.64.183]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 77E46196 for ; Sat, 23 Sep 2023 12:41:59 -0700 (PDT) Received: from remote.user (localhost [127.0.0.1]) by rere.qmqm.pl (Postfix) with ESMTPSA id 4RtKK032ZRz8x; Sat, 23 Sep 2023 21:41:56 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=rere.qmqm.pl; s=1; t=1695498116; bh=gJMERvxnLVOKYH/aIEsRWMYnuo69cEYR+2/lalyD1xE=; h=Date:In-Reply-To:References:Subject:From:To:Cc:From; b=MJuNiuzH8doJjg97Ydt1b6Jds+XcAFNp5S5+Al2adhMcTFVkB9yZrs8cokY211KmR 5GzIKsM8OJw0oJDHgRtdPQoaqheKXNxpQSF++hdh6rNNBTPXYdKQb1rzIC66Kjlv4+ foEaT5YOo10U6dP55bHhPwHJga5fu2aatPNgH4u/8AnaAaRu8k4e2x/m1nw/n4b8F7 QpaiphDrn0J16aMzFyzzNNPt42xsPBS7FyjYmXiZ4r+Z4FLbxW2ZCzu3XNPeA/9QTv eejy122ovLyxIB1rRVnh5mfsdgb8w0y9DeWndZ39V6UJzFtLNhI3ESnPjDtnai97GK ZH3Bf8hMA7gWg== X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.8 at mail Date: Sat, 23 Sep 2023 21:41:56 +0200 Message-Id: In-Reply-To: References: Subject: [PATCH 2/3] usb: chipidea: Simplify Tegra DMA alignment code MIME-Version: 1.0 From: =?utf-8?b?TWljaGHFgiBNaXJvc8WCYXc=?= To: Peter Chen , Greg Kroah-Hartman , Peter Geis , Dmitry Osipenko , Thierry Reding , Jonathan Hunter Cc: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-tegra@vger.kernel.org, linux-usb@vger.kernel.org X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Sat, 23 Sep 2023 12:42:24 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777903225720939305 X-GMAIL-MSGID: 1777903225720939305 The USB host on Tegra3 works with DWORD alignment. Previous code tried to align the buffer, but it did align the wrapper struct instead, so the buffer was at a constant offset of 8 bytes (two pointers) from assumed alignment. Since kmalloc() guarantees at least 8-byte alignment, the alignment-extending is removed. Fixes: fc53d5279094 ("usb: chipidea: tegra: Support host mode") Signed-off-by: Michał Mirosław --- drivers/usb/chipidea/host.c | 45 +++++++++++++++---------------------- 1 file changed, 18 insertions(+), 27 deletions(-) diff --git a/drivers/usb/chipidea/host.c b/drivers/usb/chipidea/host.c index abddd39d1ff1..0cce19208370 100644 --- a/drivers/usb/chipidea/host.c +++ b/drivers/usb/chipidea/host.c @@ -30,8 +30,7 @@ struct ehci_ci_priv { }; struct ci_hdrc_dma_aligned_buffer { - void *kmalloc_ptr; - void *old_xfer_buffer; + void *original_buffer; u8 data[]; }; @@ -380,60 +379,52 @@ static int ci_ehci_bus_suspend(struct usb_hcd *hcd) return 0; } -static void ci_hdrc_free_dma_aligned_buffer(struct urb *urb) +static void ci_hdrc_free_dma_aligned_buffer(struct urb *urb, bool copy_back) { struct ci_hdrc_dma_aligned_buffer *temp; - size_t length; if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER)) return; + urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER; temp = container_of(urb->transfer_buffer, struct ci_hdrc_dma_aligned_buffer, data); + urb->transfer_buffer = temp->original_buffer; + + if (copy_back && usb_urb_dir_in(urb)) { + size_t length; - if (usb_urb_dir_in(urb)) { if (usb_pipeisoc(urb->pipe)) length = urb->transfer_buffer_length; else length = urb->actual_length; - memcpy(temp->old_xfer_buffer, temp->data, length); + memcpy(temp->original_buffer, temp->data, length); } - urb->transfer_buffer = temp->old_xfer_buffer; - kfree(temp->kmalloc_ptr); - urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER; + kfree(temp); } static int ci_hdrc_alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags) { - struct ci_hdrc_dma_aligned_buffer *temp, *kmalloc_ptr; - const unsigned int ci_hdrc_usb_dma_align = 32; - size_t kmalloc_size; + struct ci_hdrc_dma_aligned_buffer *temp; if (urb->num_sgs || urb->sg || urb->transfer_buffer_length == 0) return 0; - if (!((uintptr_t)urb->transfer_buffer & (ci_hdrc_usb_dma_align - 1)) && !(urb->transfer_buffer_length & 3)) + if (IS_ALIGNED((uintptr_t)urb->transfer_buffer, 4) + && IS_ALIGNED(urb->transfer_buffer_length, 4)) return 0; - /* Allocate a buffer with enough padding for alignment */ - kmalloc_size = ALIGN(urb->transfer_buffer_length, 4) + - sizeof(struct ci_hdrc_dma_aligned_buffer) + - ci_hdrc_usb_dma_align - 1; - - kmalloc_ptr = kmalloc(kmalloc_size, mem_flags); - if (!kmalloc_ptr) + temp = kmalloc(sizeof(*temp) + ALIGN(urb->transfer_buffer_length, 4), mem_flags); + if (!temp) return -ENOMEM; - /* Position our struct dma_aligned_buffer such that data is aligned */ - temp = PTR_ALIGN(kmalloc_ptr + 1, ci_hdrc_usb_dma_align) - 1; - temp->kmalloc_ptr = kmalloc_ptr; - temp->old_xfer_buffer = urb->transfer_buffer; if (usb_urb_dir_out(urb)) memcpy(temp->data, urb->transfer_buffer, urb->transfer_buffer_length); + + temp->original_buffer = urb->transfer_buffer; urb->transfer_buffer = temp->data; - urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER; return 0; @@ -450,7 +441,7 @@ static int ci_hdrc_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb, ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags); if (ret) - ci_hdrc_free_dma_aligned_buffer(urb); + ci_hdrc_free_dma_aligned_buffer(urb, false); return ret; } @@ -458,7 +449,7 @@ static int ci_hdrc_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb, static void ci_hdrc_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb) { usb_hcd_unmap_urb_for_dma(hcd, urb); - ci_hdrc_free_dma_aligned_buffer(urb); + ci_hdrc_free_dma_aligned_buffer(urb, true); } #ifdef CONFIG_PM_SLEEP