From nobody Tue Dec 16 21:57:57 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 27A032798EC; Wed, 23 Apr 2025 08:14:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745396068; cv=none; b=RHCkwbUJFastWcgOGdc/t4m+VwPQ1PukiBkQ/gRnSLOqMB/LTmSmCwhlO4FfxNRLdDCNp9ZudFv07KrocWY7uekgJarWwTpln/GiUabOxQcb8vIt+SjI0lozgu7vHQdWQKqgf6Uegytnru4vbN2HeUm3P3ogypxSEe2gCkmBgLg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745396068; c=relaxed/simple; bh=Mv7I8YYM3HEUPYjUz1FgbEGJOnhUwi/LbJXqgGU0l4Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ojwnjYKPa5OQKNc/aMUJiOm9joBkWGXGXNAegDXhXGBO3P6G3++fO5TGHmt28MqbhYSPF8oQLZzXL0G/H2rJq4Q1cEcJOtRu7SAC62lYo+nur0jNFo3VWswgJSZbXRbqIjlutX8rbSaPHs4vaYODh+a+pEp9ZL1xJ/28TLgidAM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=UqxMbDG6; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="UqxMbDG6" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9F490C4CEEE; Wed, 23 Apr 2025 08:14:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1745396067; bh=Mv7I8YYM3HEUPYjUz1FgbEGJOnhUwi/LbJXqgGU0l4Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UqxMbDG6CorgX3nyZL77s9bhuP8XjcgeQ+WRiz/N0aOj+40AUapikG50IXBQyXsZS Big9dbqCUvgGx1hFSGmXyVbCXpCmBz9UIptUnGuH8gL3IWp0xbD+ozIWslYx1px4u+ GwUP0i6V95reXEuFHshDbHAtF0KlVU5Zuw76lOGptXyQDVWsrH4xYhQcHZzRGI0W9N fF1EV2AO1f5O/5/tmT2EtEZlAg6D3rm4CfkJcUlQA48XpV8yIJHbc8sbiZwXowUwkZ gnOljvF67VNvqk9YmLwf2CxqiJbBWxpkKVZikgiM0vR98ZS9d9YDmQOG6WSh5xuYkb Os9QEgMm185rg== From: Leon Romanovsky To: Marek Szyprowski , Jens Axboe , Christoph Hellwig , Keith Busch Cc: Jake Edge , Jonathan Corbet , Jason Gunthorpe , Zhu Yanjun , Robin Murphy , Joerg Roedel , Will Deacon , Sagi Grimberg , Bjorn Helgaas , Logan Gunthorpe , Yishai Hadas , Shameer Kolothum , Kevin Tian , Alex Williamson , =?UTF-8?q?J=C3=A9r=C3=B4me=20Glisse?= , Andrew Morton , 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, Niklas Schnelle , Chuck Lever , Luis Chamberlain , Matthew Wilcox , Dan Williams , Kanchan Joshi , Chaitanya Kulkarni , Leon Romanovsky Subject: [PATCH v9 06/24] iommu/dma: Factor out a iommu_dma_map_swiotlb helper Date: Wed, 23 Apr 2025 11:12:57 +0300 Message-ID: <9ef79ed6c24d12cfea7e6f491da48ae170a5f3f3.1745394536.git.leon@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 Tested-by: Jens Axboe Signed-off-by: Leon Romanovsky Reviewed-by: Luis Chamberlain --- 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 6ca9305a26cc..d2c298083e0a 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -1138,6 +1138,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); } =20 +static phys_addr_t iommu_dma_map_swiotlb(struct device *dev, phys_addr_t p= hys, + size_t size, enum dma_data_direction dir, unsigned long attrs) +{ + struct iommu_domain *domain =3D iommu_get_dma_domain(dev); + struct iova_domain *iovad =3D &domain->iova_cookie->iovad; + + if (!is_swiotlb_active(dev)) { + dev_warn_once(dev, "DMA bounce buffers are inactive, unable to map unali= gned transaction.\n"); + return (phys_addr_t)DMA_MAPPING_ERROR; + } + + trace_swiotlb_bounced(dev, phys, size); + + phys =3D 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 !=3D (phys_addr_t)DMA_MAPPING_ERROR && dev_is_untrusted(dev)) { + size_t start, virt =3D (size_t)phys_to_virt(phys); + + /* Pre-padding */ + start =3D iova_align_down(iovad, virt); + memset((void *)start, 0, virt - start); + + /* Post-padding */ + start =3D 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) @@ -1151,42 +1188,14 @@ dma_addr_t iommu_dma_map_page(struct device *dev, s= truct page *page, dma_addr_t iova, dma_mask =3D dma_get_mask(dev); =20 /* - * 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 unal= igned transaction.\n"); - return DMA_MAPPING_ERROR; - } - - trace_swiotlb_bounced(dev, phys, size); - - phys =3D swiotlb_tbl_map_single(dev, phys, size, - iova_mask(iovad), dir, attrs); - - if (phys =3D=3D DMA_MAPPING_ERROR) + phys =3D iommu_dma_map_swiotlb(dev, phys, size, dir, attrs); + if (phys =3D=3D (phys_addr_t)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 =3D (size_t)phys_to_virt(phys); - - /* Pre-padding */ - start =3D iova_align_down(iovad, virt); - memset((void *)start, 0, virt - start); - - /* Post-padding */ - start =3D virt + size; - memset((void *)start, 0, - iova_align(iovad, start) - start); - } } =20 if (!coherent && !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) --=20 2.49.0