From patchwork Thu Sep 28 21:06:03 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: 146337 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp3649326vqu; Thu, 28 Sep 2023 15:53:44 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGe9gph3xaXV9BrG0c+FQeG+EmNKV+SgZyUAJublxcFlAVsg3IcB9sahJCTJsgLjDlokIHP X-Received: by 2002:a05:6358:9042:b0:13a:cb52:4832 with SMTP id f2-20020a056358904200b0013acb524832mr2753964rwf.23.1695941624541; Thu, 28 Sep 2023 15:53:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695941624; cv=none; d=google.com; s=arc-20160816; b=0TLnaXp2Kpz3ZoSlSQ7Y9oQvYa5enPiPDu14gmOOLlA5nDZ2IkVjsYs7XUenBFEvCw 9TE4fG+3OwJZ687aIN8/PMp05/yNU7fCFShc3JuASCJHBxVe6pl+JxekW6OTwmJ1dQV5 r1P5BFGy7AGdAu/plFlYgda3a5f5ZO4y3K+sxUnCUbxMHf29nkn9tK/6L5mriQD6QwTZ wypOzVtizJxLwzlmmblCRm1qW1tHc2+oVJSHPhNiJxht5uHbkffZHrzP+w4Qv06Zlapv KxjsOFOhWWmcelO1TlAg2wyT+qbd95I8Htg8xlU5oRm2YhwJWlhre5UpXAZUe6z7iaz2 DIhA== 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=UfruAuA8DgNcFVk/QvRvkqibYxayu4NlAZ+uw9X4q9o=; fh=kwxXSZ0wpsns+zwHjWYl55h09qQE8UVpuJ6fPBCHhTU=; b=FAO6dREv6lSynnGF3z+qpN139zpYuZ7MRFMVDxPmLTU5YBUmd4qnbwWpZsEdK8Z797 hQN7qAKJGHA1f31DRlD8yEhjRzI8o7dqraxLP6EGSNm3aUMCH+j8eEC8ft11p3/j658N Ph27tBggORzxWWabonFA7HnUyuyCitLRRhfcPoV4VSA44f0mJ90m9+0vC1uxmJgFALhB mCFjozCrxa+wDueNbWCcv1HXe1T/qVJSo3Khk+IBEObI7gAAffje5r5qAxr51bcX9fcy xnGLyd4E6inszTV9In4GODpreLyVYd1+p/bgAxaLsTiLiheirm3V5JYdK41hijDVAAvq D9EA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rere.qmqm.pl header.s=1 header.b=LTQ+BWtA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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. [23.128.96.31]) by mx.google.com with ESMTPS id p30-20020a63951e000000b00565e01815f2si20359919pgd.732.2023.09.28.15.53.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Sep 2023 15:53:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@rere.qmqm.pl header.s=1 header.b=LTQ+BWtA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 E4A238026973; Thu, 28 Sep 2023 14:06:51 -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 S232466AbjI1VGX (ORCPT + 22 others); Thu, 28 Sep 2023 17:06:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54866 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232355AbjI1VGO (ORCPT ); Thu, 28 Sep 2023 17:06:14 -0400 Received: from rere.qmqm.pl (rere.qmqm.pl [91.227.64.183]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F3D2A19D; Thu, 28 Sep 2023 14:06:06 -0700 (PDT) Received: from remote.user (localhost [127.0.0.1]) by rere.qmqm.pl (Postfix) with ESMTPSA id 4RxQxm0PkWzHj; Thu, 28 Sep 2023 23:06:04 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=rere.qmqm.pl; s=1; t=1695935164; bh=iXyH63ousHu8sDotYqQaWkewO39D2pY6N9s4ZikScZY=; h=Date:In-Reply-To:References:Subject:From:To:Cc:From; b=LTQ+BWtAXtgJOH/HsOsrXjCtsI3rHVZ9aUiOC8noTbkFWAiTB971JiNiczGhQ8RR7 LM3GxX6sKMdcJ8o6SPppg/ruXPnMYSZAs0C5yrXFMS7/M8QEcKVNfIG+5WXxrUkiFj wsOej967Ghqi2IR3eQJ4MconH4WoF5jdb75q8oUA+Z8VYz/Xr7AjIN8BlCTCVoPouN AXHBL8fSDcP7HBGYAXJ/ALag3j/m6jnTZs4yBes7hLIYoW+NEXbRi0WtCyf6AuCBUj KNEFvX14H+2LiCgQgb6Pie6o763uKTRQOO6TvstzYljdzWX7fL7i0e/EMVDhGqkD+b uSnmvmREy3rlA== X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at mail Date: Thu, 28 Sep 2023 23:06:03 +0200 Message-Id: In-Reply-To: References: Subject: [PATCH v2 2/3] usb: chipidea: Simplify Tegra DMA alignment code MIME-Version: 1.0 From: =?utf-8?b?TWljaGHFgiBNaXJvc8WCYXc=?= To: Peter Geis , Dmitry Osipenko , Greg Kroah-Hartman , Peter Chen , Thierry Reding Cc: 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]); Thu, 28 Sep 2023 14:06:51 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778323685191146539 X-GMAIL-MSGID: 1778323685191146539 The USB host on Tegra3 works with 32-bit 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 expected alignment. Since kmalloc() guarantees at least 8-byte alignment already, the alignment-extending is removed. Fixes: fc53d5279094 ("usb: chipidea: tegra: Support host mode") Signed-off-by: Michał Mirosław Acked-by: Thierry Reding Acked-by: Peter Chen --- 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