From patchwork Sun Nov 10 13:46:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leon Romanovsky X-Patchwork-Id: 13869828 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2B241D6409C for ; Sun, 10 Nov 2024 13:47:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AFB526B0096; Sun, 10 Nov 2024 08:47:46 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A84856B0099; Sun, 10 Nov 2024 08:47:46 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 94C966B009A; Sun, 10 Nov 2024 08:47:46 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 74AD86B0096 for ; Sun, 10 Nov 2024 08:47:46 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 416A71C730C for ; Sun, 10 Nov 2024 13:47:46 +0000 (UTC) X-FDA: 82770312954.29.0E53DA3 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf15.hostedemail.com (Postfix) with ESMTP id 83195A0008 for ; Sun, 10 Nov 2024 13:47:04 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=VLvd3JdR; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf15.hostedemail.com: domain of leon@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=leon@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731246291; a=rsa-sha256; cv=none; b=wUJyIt+wwLbfoHF2xEJ9UaswlRwNkNlSKnxxfIQJiq3P508wnFqZBeySAXUENUfPeoNrah cMQqjyjLgnkg47F5JgXrfCiLdFKIbyQNMF3yqEDm9/p0eLfI8lRmRLVnZi6tIbgJySfXyc 1MvpkARwJQjBdEQYfBxDUzDwzbl9eUY= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=VLvd3JdR; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf15.hostedemail.com: domain of leon@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=leon@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731246291; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=y42j5xQ8uuKkAImqJ1Sbr0phfQsqOqPqPxQXYkNnsPU=; b=ilDdypOuvIWiHUiu5CZAf2A3X/XPWGpVp1Qis6oxNgtFrRIF3X8rph/LOd8ZUGe2wKpqMj TVupYnqCyII9LZovo+BO5RhNBsbHhAPIt5nsTAjBLgxzvDEvJ97d3FsyfnhpgRxAWw/ha7 2XZN/zoaoP5Q5U4FWTNREbkC2NqDXQQ= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 5D8235C4636; Sun, 10 Nov 2024 13:46:59 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CF817C4CECD; Sun, 10 Nov 2024 13:47:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731246463; bh=6vyLZ9mjLX8vy7gjsSTdc+TDPjgozca4sT5DOBvIDT0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VLvd3JdR+kMAvaPiFKVBnSU6eIC3dqJe/OasqqOCUHF/p3FlLxLgcUGOysOByZqCT XEvDSJ1/ufSYAZgkna8+2He3qpJM5ZlkLDRRiSQRtjrL+HuXy+7sk/mkErxt0Rsvoj +PcUGocfI9Rxx+jbA+2R7vxo2ZiqVQEXiO780gkzlhLSjw5SOuHvkuIQfaXDfgNRt+ ZmcDWARnf4qhbsXIjVCvUMe9ksNgTK+Af7DgGEbm4pCLCUzU0mK659soI/gOPEuXNe U+xYnVRMSwOIlCtH8xyBHR2eCdSdM+u4b37HFRhqbYd8888uliQdkebLxAA8t3Uwxt hE4T4qrK5e5NQ== From: Leon Romanovsky To: Jens Axboe , Jason Gunthorpe , Robin Murphy , Joerg Roedel , Will Deacon , Christoph Hellwig , Sagi Grimberg Cc: Keith Busch , Bjorn Helgaas , Logan Gunthorpe , Yishai Hadas , Shameer Kolothum , Kevin Tian , Alex Williamson , Marek Szyprowski , =?utf-8?b?SsOpcsO0bWUgR2xpc3Nl?= , Andrew Morton , Jonathan Corbet , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, linux-rdma@vger.kernel.org, iommu@lists.linux.dev, linux-nvme@lists.infradead.org, linux-pci@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org, Randy Dunlap Subject: [PATCH v3 06/17] iommu/dma: Factor out a iommu_dma_map_swiotlb helper Date: Sun, 10 Nov 2024 15:46:53 +0200 Message-ID: <38bd0f07e18f4c74f2ab77f268e9d0424569f69f.1731244445.git.leon@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: References: MIME-Version: 1.0 X-Stat-Signature: yzxisdixbc43kgi37rbtxp5hz3ue968k X-Rspamd-Queue-Id: 83195A0008 X-Rspamd-Server: rspam08 X-Rspam-User: X-HE-Tag: 1731246424-292744 X-HE-Meta: U2FsdGVkX1/yWG4HK1dzkn9ZlVmEjyM/W/2QMH9Wd9JYJ0+/riTbGpndGm+iAdX1/rVY64i5opk2/i1SSTl/t+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tAquuZWukznwbB+4hCY3GoYsecWM0uQdNToOGx2Kl6tw1/VIst1kOgNefyfB1SwudkqrkNIkpbiQr3TPfqaKq9rEh5MHSPegRerGM/6A4oyxqG2wUEtoLQkeJNk57yxbD/rtesKU3BLHeGzgM0noCGcMnZ0rgEGiLQfUEGQyas/yCh9/l8T3VIc9TNw8ly5UdVJA= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Christoph Hellwig Split the iommu logic from iommu_dma_map_page into a separate helper. This not only keeps the code neatly separated, but will also allow for reuse in another caller. Signed-off-by: Christoph Hellwig Signed-off-by: Leon Romanovsky --- drivers/iommu/dma-iommu.c | 73 ++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 32 deletions(-) diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index 127150f63c95..e1eaad500d27 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -1161,6 +1161,43 @@ void iommu_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sgl, arch_sync_dma_for_device(sg_phys(sg), sg->length, dir); } +static phys_addr_t iommu_dma_map_swiotlb(struct device *dev, phys_addr_t phys, + size_t size, enum dma_data_direction dir, unsigned long attrs) +{ + struct iommu_domain *domain = iommu_get_dma_domain(dev); + struct iova_domain *iovad = &domain->iova_cookie->iovad; + + if (!is_swiotlb_active(dev)) { + dev_warn_once(dev, "DMA bounce buffers are inactive, unable to map unaligned transaction.\n"); + return DMA_MAPPING_ERROR; + } + + trace_swiotlb_bounced(dev, phys, size); + + phys = swiotlb_tbl_map_single(dev, phys, size, iova_mask(iovad), dir, + attrs); + + /* + * Untrusted devices should not see padding areas with random leftover + * kernel data, so zero the pre- and post-padding. + * swiotlb_tbl_map_single() has initialized the bounce buffer proper to + * the contents of the original memory buffer. + */ + if (phys != DMA_MAPPING_ERROR && dev_is_untrusted(dev)) { + size_t start, virt = (size_t)phys_to_virt(phys); + + /* Pre-padding */ + start = iova_align_down(iovad, virt); + memset((void *)start, 0, virt - start); + + /* Post-padding */ + start = virt + size; + memset((void *)start, 0, iova_align(iovad, start) - start); + } + + return phys; +} + dma_addr_t iommu_dma_map_page(struct device *dev, struct page *page, unsigned long offset, size_t size, enum dma_data_direction dir, unsigned long attrs) @@ -1174,42 +1211,14 @@ dma_addr_t iommu_dma_map_page(struct device *dev, struct page *page, dma_addr_t iova, dma_mask = dma_get_mask(dev); /* - * If both the physical buffer start address and size are - * page aligned, we don't need to use a bounce page. + * If both the physical buffer start address and size are page aligned, + * we don't need to use a bounce page. */ if (dev_use_swiotlb(dev, size, dir) && iova_offset(iovad, phys | size)) { - if (!is_swiotlb_active(dev)) { - dev_warn_once(dev, "DMA bounce buffers are inactive, unable to map unaligned transaction.\n"); - return DMA_MAPPING_ERROR; - } - - trace_swiotlb_bounced(dev, phys, size); - - phys = swiotlb_tbl_map_single(dev, phys, size, - iova_mask(iovad), dir, attrs); - + phys = iommu_dma_map_swiotlb(dev, phys, size, dir, attrs); if (phys == DMA_MAPPING_ERROR) - return DMA_MAPPING_ERROR; - - /* - * Untrusted devices should not see padding areas with random - * leftover kernel data, so zero the pre- and post-padding. - * swiotlb_tbl_map_single() has initialized the bounce buffer - * proper to the contents of the original memory buffer. - */ - if (dev_is_untrusted(dev)) { - size_t start, virt = (size_t)phys_to_virt(phys); - - /* Pre-padding */ - start = iova_align_down(iovad, virt); - memset((void *)start, 0, virt - start); - - /* Post-padding */ - start = virt + size; - memset((void *)start, 0, - iova_align(iovad, start) - start); - } + return phys; } if (!coherent && !(attrs & DMA_ATTR_SKIP_CPU_SYNC))