From patchwork Fri Jan 17 10:03:37 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leon Romanovsky X-Patchwork-Id: 13943120 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 009A3C02183 for ; Fri, 17 Jan 2025 10:04:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6D2F6280008; Fri, 17 Jan 2025 05:04:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 682CD280001; Fri, 17 Jan 2025 05:04:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4D570280008; Fri, 17 Jan 2025 05:04:21 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 2A539280001 for ; Fri, 17 Jan 2025 05:04:21 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id D7EB1C07DD for ; Fri, 17 Jan 2025 10:04:20 +0000 (UTC) X-FDA: 83016508680.27.C456AD9 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf25.hostedemail.com (Postfix) with ESMTP id 35136A0018 for ; Fri, 17 Jan 2025 10:04:19 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Mc8CLwKz; spf=pass (imf25.hostedemail.com: domain of leon@kernel.org designates 139.178.84.217 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=1737108259; 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=Avq+ozoMMu+jQmoTfVCX5VDb19iU3ByTSlyhXaFVsV0=; b=y2SrUv09OkjtT02As0UF9SgKaQ5sVZ28Ei55nuDkp0ifIEY5GaXdtUIrL0pRLhFQBGRBzC dexptdcCaZSdXRdw/G9A6gyF+YzBSgjGhGWBA1J31xn3HenRdYk/StyHWq+4DyVGDDmeFH Nq9A+p3DSB4cHqdljDU2DweikoARUug= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737108259; a=rsa-sha256; cv=none; b=GMySzluPYtfQnA8ayViXaxSAuYwKL/lWJmTQSNfGAeuBxiCjBT1B5bV4Vw4MdMX459jpSr 6zgE6QTCe21sQ7FicMpp9pAi1FEuXqWjfbqRs6EEPB/Pgla8Lr/LFS92zMHDbZYtLY9apf o0BO9JQ2uSBuQIuAQAMWC33Dk1tYIgc= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Mc8CLwKz; spf=pass (imf25.hostedemail.com: domain of leon@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=leon@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id AA0D65C56AB; Fri, 17 Jan 2025 10:03:37 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1AC38C4CEDD; Fri, 17 Jan 2025 10:04:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1737108257; bh=rum8Whg042xtqAAmYnLBImAToSE6OI+VPKJj0K+Pohc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Mc8CLwKz7mcdjOCnAG6ibIY8fcYKhCcOq2PmhjxuMXrgUUBs6Vg+fAM3jon/hdaY3 El7Bqjx4eTk7ePsxyy1gVt7SPj8RPnH0fKGXTOTj8ZDrW9ypTPHtDuOn8XKBqedSfg YuIvn3AO+94EpLsXUhTmaiVjImD7VM2FgNl4K8edzDIkwhQsihPCDrlvDqPMD5XUA9 +hsYCA86uB9WKSypdcSPK0LPXU9jv7CUgSfvLM5JVEelfKbMZr+RbNQJG7BNhfMpv+ Ub3w7UF+6N3O2/R0+ZoAV4boOKGwAZ7IFEVW3ZF/mb7I4QgKF+L0vD4K6LabcgFGC8 4jwmdTjQfSEMw== From: Leon Romanovsky To: Christoph Hellwig , Jason Gunthorpe , Robin Murphy Cc: Jens Axboe , Joerg Roedel , Will Deacon , Sagi Grimberg , "Keith Busch" , "Bjorn Helgaas" , "Logan Gunthorpe" , "Yishai Hadas" , "Shameer Kolothum" , "Kevin Tian" , "Alex Williamson" , "Marek Szyprowski" , =?utf-8?b?SsOpcsO0bWUgR2xp?= =?utf-8?b?c3Nl?= , "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 v6 06/17] iommu/dma: Factor out a iommu_dma_map_swiotlb helper Date: Fri, 17 Jan 2025 12:03:37 +0200 Message-ID: X-Mailer: git-send-email 2.47.1 In-Reply-To: References: MIME-Version: 1.0 X-Stat-Signature: wnhio7fogm5dzwsmn8z1s5radphxwn3b X-Rspam-User: X-Rspamd-Queue-Id: 35136A0018 X-Rspamd-Server: rspam03 X-HE-Tag: 1737108259-129409 X-HE-Meta: U2FsdGVkX18hJ/HF1DELfmmMc++ft0tbZ7RCChbJWwlyp5y73KdxRj71wF4U7GL6gyrsvaRXmeSgm9Jiyl9FEGj6QtFDY6ykN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slvVM6hwl0xVP45a4Fldv4M6aMu6O4w4tHprkUneMOyKKXAvo3fQmHL1oUX06r09Gq4wH5yDiDw4WbNBif3WUMovPTyPW3BMH7rqkC 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 309d278b1d86..80cc2c51ac99 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))