From patchwork Wed Oct 30 15:12:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leon Romanovsky X-Patchwork-Id: 13856679 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 522CBD5CCB3 for ; Wed, 30 Oct 2024 15:13:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D8CEA8E0002; Wed, 30 Oct 2024 11:13:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CEF788E0001; Wed, 30 Oct 2024 11:13:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B67AE8E0002; Wed, 30 Oct 2024 11:13:42 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 992F78E0001 for ; Wed, 30 Oct 2024 11:13:42 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 4FC071A0C67 for ; Wed, 30 Oct 2024 15:13:42 +0000 (UTC) X-FDA: 82730612580.04.F323C3D Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf27.hostedemail.com (Postfix) with ESMTP id 6653C4000E for ; Wed, 30 Oct 2024 15:13:14 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=LmPDkamq; spf=pass (imf27.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=1730301061; 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=x88jTcq6GLouiEokwKqr1ClmMP8uqV/wSbdaC6q3fFg=; b=XoMpRwbQwKmMIfE+tnP4EppSCNGa34ORYLKkYYfEkvsNadaLIws3MOibt5qd71mgoYzhtE eSeswG5kXkkzZKKHO7c/KMJKDHw7/78ogqGBxLrI9QuMuyGsOQUKgYtxbIA9bQPFIVb3uw jXFoAQapPen/fCUiL9hXFx7plXLPpfU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730301061; a=rsa-sha256; cv=none; b=lJ76zdCEMZBbN4tfMbKT9VirKpiCZYV/5iwVrexwNfk0Mch9D54E5F+/sF9KLO3GgrPgok 2mDC4KLNvIjsfIs7pW2ytpOfAAgTe4MP4B1nvw0fSbXSve+BCUjBCqdfLTQ0h+oZz8eQyB kb8DSZqeoga2Ye5xPY4KxS9kRbKwOOg= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=LmPDkamq; spf=pass (imf27.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 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 4BEECA4255F; Wed, 30 Oct 2024 15:11:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 13913C4CECE; Wed, 30 Oct 2024 15:13:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1730301219; bh=XGQ1NR355bwszo+JrznVV1hbHviA7CQrxv/BERy+y90=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LmPDkamqTEpX+kwqmBANPlqtlaFChfErvXzZhGcch88a2LmIU2YivyLFbxjH/hzDZ UNXh/XXgTFkZfi9o/NrVNl/D2NwKGhCbDbxrukedWRjZezPUNgu3cjHDkBmHwB1ipi ToolLhizyQGeQxxr9bWcbopr2T3qyqnWcemFq8LORVDrrnGTOu7T2CVKojWMqU0aQy +EYFhYN45wrl8n2fh86UD+fmcagGwz4cCmHXzXzOmvR9DGTd+10cF2ZIGLyWyxQLVv N2MVluX7uYAEpW4yfHdfhNE3yxm+LfwU2OgZ2tF67RMlGpA+rFfIfD9Nx5spoyzjvo M+eW1/GKIlmtg== 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 Subject: [PATCH v1 06/17] iommu/dma: Factor out a iommu_dma_map_swiotlb helper Date: Wed, 30 Oct 2024 17:12:52 +0200 Message-ID: <38bd0f07e18f4c74f2ab77f268e9d0424569f69f.1730298502.git.leon@kernel.org> X-Mailer: git-send-email 2.46.2 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: 6653C4000E X-Stat-Signature: ddut3urowfieoxnyziycfxckrxdb59b7 X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1730301194-358554 X-HE-Meta: U2FsdGVkX1+J7NySuwOtqK3kpf9JBZQFWQsxn27rk74phayS3nVQyaibs7oY3xm99gjkZhnb/9VlNoylsn1f1W4o5t1mu6XrIcQohUwL3nAignu6Vfbcrj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xvYTBquXWV1Pt3VVgbduUyecwSaI2TrEWosPVue46n1q2GaKv/pawHbGDbTJU7+zAhHq+VFt6eGNBL3zgXdSCJpm26WVKTeb1XoN 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))