From patchwork Wed Nov 6 13:49:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leon Romanovsky X-Patchwork-Id: 13864476 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 64D43D44D54 for ; Wed, 6 Nov 2024 11:50:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ECFA96B008A; Wed, 6 Nov 2024 06:50:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E7CF26B009F; Wed, 6 Nov 2024 06:50:17 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CF7196B00A0; Wed, 6 Nov 2024 06:50:17 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id AFC9B6B008A for ; Wed, 6 Nov 2024 06:50:17 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 5FD0181897 for ; Wed, 6 Nov 2024 11:50:17 +0000 (UTC) X-FDA: 82755500520.19.0D878C2 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf17.hostedemail.com (Postfix) with ESMTP id 5B2974000E for ; Wed, 6 Nov 2024 11:49:49 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=uK9LgiLp; spf=pass (imf17.hostedemail.com: domain of leon@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=leon@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730893630; 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=SpCosTLA/NkmChsWs2RfQujW8bhmH4uNg1XvKHKcTaHvyW2riM0tWQEJkbMJQuri8mDSLt OdQbm7y/lES+K6r/u/7KPrDdKFTehxiu8pxDcXK3M1ST4jKv0Y4JwG0vn5PRpbFI0RxsXi tS4z/K4C+K/gvKE8RBXWGUwQys1tmv4= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=uK9LgiLp; spf=pass (imf17.hostedemail.com: domain of leon@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=leon@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730893630; a=rsa-sha256; cv=none; b=Xjsm3Pr3WDycDcQcR+Oa3m73BjMuJ8K8CBkUchnE1DROw/tUkedIN8x6M7/G09CeZxDIw2 CPVUWHC1Mj4bmaL8TgDSe3/14mhZpqySCtEbs3eH8O6nSK0/CedVRX6ryrxesqG/rFYyny p4z4d0vt5GjiBFp1XkOiaZ30pdXo87w= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 273FCA4370F; Wed, 6 Nov 2024 11:48:20 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F2280C4CED2; Wed, 6 Nov 2024 11:50:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1730893814; bh=6vyLZ9mjLX8vy7gjsSTdc+TDPjgozca4sT5DOBvIDT0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uK9LgiLpdCHR2vE7SHQm5/sc0H6w09CqdJYbGGC1AMXFPGFAwDZ78kvdB8HkZO9pm a6D6PmP3LcljSCuYzGvzdJfc/6zyJznBC91O5FyQyhVeEpLSt5bFpAOb2iUKhhKF43 JKSZfog7hsUGkO3/BSG6AsaCYDrtt2+8dGAZOJoNvvuy74IjG+dhO8qGRWAVe5W5Vk oTQ64BzTGxpiju/bQS/XbzIhg1vzvtKGRMfBTohYAqd+uHC5tI2fd+0906FPVS7hWn S1D9t0ANr1Rfp8E0eDHzJWQwhjU3Lm7qXsZQlMa3QPPGC/rLwedqigCeGnHOv53MQL JOHfiUT4AKMIA== 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 v2 06/17] iommu/dma: Factor out a iommu_dma_map_swiotlb helper Date: Wed, 6 Nov 2024 15:49:34 +0200 Message-ID: <38bd0f07e18f4c74f2ab77f268e9d0424569f69f.1730892663.git.leon@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 5B2974000E X-Stat-Signature: mzp8g8cp7gxkd4up4kkxsdgn66ehat5y X-Rspam-User: X-HE-Tag: 1730893789-405531 X-HE-Meta: U2FsdGVkX1/nGfsyS0ppJvLI+C+q7hvmAvCEP/qf8SB5+l8qL0h24PU2/kYUVv5eUS86bu/fZL92mc/o+Inyw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lU3dnKm0rSCI3kNDOoGMDE7fCaMKqhpBgXwkbS4CUFRiZ/1dlpg8rKmNCOVUTvZbrdO/uzK1YPs8GupGVujw3x6+mtoc5J8TmOnQGlc3ToiePTe68TNOWAOMz/PVJobfZ/YyqYoXkb3aq5zTAhVNFT9Zj+4mLQibIfPJmsLU8VyRGv42igaBGOd69TOCTE4Nf1iHUgoJHpmw94C0ZE+2/hhNa/vlfGFeISbx6A2hRoY= 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))