From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.5 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6EC8BC432BE for ; Fri, 13 Aug 2021 07:42:55 +0000 (UTC) Received: from smtp4.osuosl.org (smtp4.osuosl.org [140.211.166.137]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 2FAB560F14 for ; Fri, 13 Aug 2021 07:42:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 2FAB560F14 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.linux-foundation.org Received: from localhost (localhost [127.0.0.1]) by smtp4.osuosl.org (Postfix) with ESMTP id 100AD405A1; Fri, 13 Aug 2021 07:42:55 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp4.osuosl.org ([127.0.0.1]) by localhost (smtp4.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id EqJyt_jbhHbV; Fri, 13 Aug 2021 07:42:51 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp4.osuosl.org (Postfix) with ESMTPS id 9F97240580; Fri, 13 Aug 2021 07:42:50 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 7F2F9C001A; Fri, 13 Aug 2021 07:42:50 +0000 (UTC) Received: from smtp1.osuosl.org (smtp1.osuosl.org [140.211.166.138]) by lists.linuxfoundation.org (Postfix) with ESMTP id 56765C000E for ; Fri, 13 Aug 2021 07:42:49 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id 4491080E7C for ; Fri, 13 Aug 2021 07:42:49 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Authentication-Results: smtp1.osuosl.org (amavisd-new); dkim=pass (1024-bit key) header.d=chromium.org Received: from smtp1.osuosl.org ([127.0.0.1]) by localhost (smtp1.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id PBa4ZHJpumdD for ; Fri, 13 Aug 2021 07:42:45 +0000 (UTC) X-Greylist: whitelisted by SQLgrey-1.8.0 Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) by smtp1.osuosl.org (Postfix) with ESMTPS id 5983480E87 for ; Fri, 13 Aug 2021 07:42:45 +0000 (UTC) Received: by mail-pj1-x102d.google.com with SMTP id om1-20020a17090b3a8100b0017941c44ce4so1176251pjb.3 for ; Fri, 13 Aug 2021 00:42:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=rIU9X3CBq/5AqsD6O3iRvIx9yS+jn8jnQi951pLbz+Y=; b=CpWrapi04Qe3TlO+IfIPfGbmUXcMyRbAR+4u4GqI7kXEjimMNk3JQqXp2z6exXyUIi KCqsWNYbOmOUbo+1d+i18P4ga6EthnyczN+5BLJwq4D+z+qlagCgb37zV4aKLNguBtos tSaziCRjCyNfEZ62Ii7PVbcLp/euQgUbxe9bo= 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=rIU9X3CBq/5AqsD6O3iRvIx9yS+jn8jnQi951pLbz+Y=; b=XCqDQAgw12jmEySziUD6AolCj9ihx6LLCfKsO3b7zhdBxzDtBUGlT7gizfcdvHE813 N9RfEyJrseZ2/UcZX8W6+AAi5xmzNHlDXfgsP8iuX1zgVmERsqOnXziiU2J9TF8kd/Ac 5EOd1Dd71yjAc7njGTBmq8C/I/N8HmE7BDFNR1VAZSZPp4/MPp61KZrxhHb1gf3hLX3R CklekmHRV1A9QejxuFUfGCh2FJukgNfn6mNVejC0ms3qbI1GHpNA/YbT8REUF/6n7DCH ipkJdJPwgMUx9AuzC2A7bB3W82qcQNRS7WHkkA2Aj3fOKsT9FqfjoElJhzuwtbP2k+NB kQ6g== X-Gm-Message-State: AOAM533xpgxp5SyVh9oJCYXAjLL6PJYTC5zEPxTNi9SDXHSVq414sUoW R+lMhC5Qpwo22TcPWAfSMMA4tg== X-Google-Smtp-Source: ABdhPJwxO/YyLMkdVX0p9Wg82OhRawlZrko1pQV28/p0kuQAJn+b1Qt9U1a17S7lCdRmtYeH2UGRAA== X-Received: by 2002:a17:90a:9411:: with SMTP id r17mr1308830pjo.49.1628840564914; Fri, 13 Aug 2021 00:42:44 -0700 (PDT) Received: from localhost ([2401:fa00:8f:203:cbc8:5998:527e:5f43]) by smtp.gmail.com with UTF8SMTPSA id g5sm1164326pfm.209.2021.08.13.00.42.42 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 13 Aug 2021 00:42:44 -0700 (PDT) From: David Stevens X-Google-Original-From: David Stevens To: Robin Murphy , Christoph Hellwig Subject: [PATCH v4 3/6] dma-iommu: skip extra sync during unmap w/swiotlb Date: Fri, 13 Aug 2021 16:38:36 +0900 Message-Id: <20210813073839.1562438-4-stevensd@google.com> X-Mailer: git-send-email 2.33.0.rc1.237.g0d66db33f3-goog In-Reply-To: <20210813073839.1562438-1-stevensd@google.com> References: <20210813073839.1562438-1-stevensd@google.com> MIME-Version: 1.0 Cc: linux-kernel@vger.kernel.org, Tom Murphy , iommu@lists.linux-foundation.org, David Stevens , Will Deacon X-BeenThere: iommu@lists.linux-foundation.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: Development issues for Linux IOMMU support List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: iommu-bounces@lists.linux-foundation.org Sender: "iommu" From: David Stevens Calling the iommu_dma_sync_*_for_cpu functions during unmap can cause two copies out of the swiotlb buffer. Fold __iommu_dma_unmap_swiotlb into iommu_dma_unmap_page, and directly call arch_sync_dma_for_cpu instead of iommu_dma_sync_single_for_cpu to avoid this double sync. With this refactor, calling iommu_dma_sync_sg_for_cpu for untrusted devices in iommu_dma_unmap_sg is also no longer necessary, so move that invocation later in the function. Signed-off-by: David Stevens --- drivers/iommu/dma-iommu.c | 42 +++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index e9119ff93535..f7da4934f7e6 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -493,23 +493,6 @@ static void __iommu_dma_unmap(struct device *dev, dma_addr_t dma_addr, iommu_dma_free_iova(cookie, dma_addr, size, iotlb_gather.freelist); } -static void __iommu_dma_unmap_swiotlb(struct device *dev, dma_addr_t dma_addr, - size_t size, enum dma_data_direction dir, - unsigned long attrs) -{ - struct iommu_domain *domain = iommu_get_dma_domain(dev); - phys_addr_t phys; - - phys = iommu_iova_to_phys(domain, dma_addr); - if (WARN_ON(!phys)) - return; - - __iommu_dma_unmap(dev, dma_addr, size); - - if (unlikely(is_swiotlb_buffer(phys))) - swiotlb_tbl_unmap_single(dev, phys, size, dir, attrs); -} - static dma_addr_t __iommu_dma_map(struct device *dev, phys_addr_t phys, size_t size, int prot, u64 dma_mask) { @@ -845,9 +828,20 @@ static dma_addr_t iommu_dma_map_page(struct device *dev, struct page *page, static void iommu_dma_unmap_page(struct device *dev, dma_addr_t dma_handle, size_t size, enum dma_data_direction dir, unsigned long attrs) { - if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC)) - iommu_dma_sync_single_for_cpu(dev, dma_handle, size, dir); - __iommu_dma_unmap_swiotlb(dev, dma_handle, size, dir, attrs); + struct iommu_domain *domain = iommu_get_dma_domain(dev); + phys_addr_t phys; + + phys = iommu_iova_to_phys(domain, dma_handle); + if (WARN_ON(!phys)) + return; + + if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) && !dev_is_dma_coherent(dev)) + arch_sync_dma_for_cpu(phys, size, dir); + + __iommu_dma_unmap(dev, dma_handle, size); + + if (unlikely(is_swiotlb_buffer(phys))) + swiotlb_tbl_unmap_single(dev, phys, size, dir, attrs); } /* @@ -932,7 +926,7 @@ static void iommu_dma_unmap_sg_swiotlb(struct device *dev, struct scatterlist *s int i; for_each_sg(sg, s, nents, i) - __iommu_dma_unmap_swiotlb(dev, sg_dma_address(s), + iommu_dma_unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs); } @@ -1053,14 +1047,14 @@ static void iommu_dma_unmap_sg(struct device *dev, struct scatterlist *sg, struct scatterlist *tmp; int i; - if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC)) - iommu_dma_sync_sg_for_cpu(dev, sg, nents, dir); - if (dev_is_untrusted(dev)) { iommu_dma_unmap_sg_swiotlb(dev, sg, nents, dir, attrs); return; } + if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC)) + iommu_dma_sync_sg_for_cpu(dev, sg, nents, dir); + /* * The scatterlist segments are mapped into a single * contiguous IOVA allocation, so this is incredibly easy. -- 2.33.0.rc1.237.g0d66db33f3-goog _______________________________________________ iommu mailing list iommu@lists.linux-foundation.org https://lists.linuxfoundation.org/mailman/listinfo/iommu