From patchwork Wed Feb 5 14:40:26 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leon Romanovsky X-Patchwork-Id: 13961187 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 7B0A6C02194 for ; Wed, 5 Feb 2025 14:41:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D0D77280005; Wed, 5 Feb 2025 09:41:28 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CBE5C280002; Wed, 5 Feb 2025 09:41:28 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AE872280005; Wed, 5 Feb 2025 09:41:28 -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 8982E280002 for ; Wed, 5 Feb 2025 09:41:28 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 45E69B187A for ; Wed, 5 Feb 2025 14:41:28 +0000 (UTC) X-FDA: 83086154256.11.AE03255 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf10.hostedemail.com (Postfix) with ESMTP id 9E418C0012 for ; Wed, 5 Feb 2025 14:41:26 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=dVIn6T63; spf=pass (imf10.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=1738766486; 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=nbsmQc6BOdogabvWwJMN2WWcAdfn/8sUwSEOcSYrSC8=; b=1vkFW57t6xtIgb8OTbBRsoYsNApHy+CPzAlr6r8ejqQGg0DG+dXrtb+CgMrrOF/k4Ibrnb 5ujuyGFbZMf8uT4GMu7IDANWkWOcl6irlAiUgej02OY4YWeQ1+TquXkJgpUwh6EmhAFq97 baPJ4DY9ua8MuRpfcVF4J4IlPlksjLo= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=dVIn6T63; spf=pass (imf10.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=1738766486; a=rsa-sha256; cv=none; b=bgQUMUnQulY2GXIckaOAb3vdKT0Z6FwkjoFw70PvDJV6vLsNvGkRVBmPV+GFwl7Ubxen1Z YPrpE1yA4xHIZifkM2uG02GWPqSMYlx2HOCv6FsjlNv347eHhMM/eUiB9ReNH6yTqzaDfE uGMU6WwXKpcMoTPMWEIKR48AgX8JX3Y= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 03DC8A436BA; Wed, 5 Feb 2025 14:39:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 15F37C4CED1; Wed, 5 Feb 2025 14:41:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1738766485; bh=6Ai1gt0w10mq1r3SBiKNGGAp0oww0hCWygmK6vVlFiM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dVIn6T63yACvKBXXsPbv0oV/aj5UdNusQV2vQ8C5ZbC6U6GC5Ypa29dBOrgc9ITah U2TAX9SUR/hqSN92vnLtoQb7xOTLMg+PnnbiNUSh0+L1H0TzpXliOozFt7FoQ+G1MM UIA0BPob49QrpErBKVeEv/KcEtv9q31WiWst5vLl7lZnsnHlYix21IoxlSGm03NxAI RV+MGYJfznd5+9hZTN6AsCY4GbLPcCuiiAXkbAOUG+3MxlpKkJ26A0GWAAkMgpX+/0 LaGyTOZIbH4a0RFSKNokQOz1RWmILJtaPg8PLGGFJdjK4rlyJCpFsLw/8dMY56Sdtl ab82/62QBFK3w== 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?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 v7 06/17] iommu/dma: Factor out a iommu_dma_map_swiotlb helper Date: Wed, 5 Feb 2025 16:40:26 +0200 Message-ID: <49bf0300d6c49b784d88db6922c20dfe78f3047d.1738765879.git.leonro@nvidia.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: 9E418C0012 X-Stat-Signature: k6airdwsbupxog155aygh635ei565je1 X-Rspamd-Server: rspam08 X-Rspam-User: X-HE-Tag: 1738766486-47211 X-HE-Meta: U2FsdGVkX19FGitIzqTNsElXZUOdTCy0n8q6iEo+Y9gci4+oDEomXO7l+u3VCIR8gutIQEEpkH+8OyF+6NqIYVijrEajKxLWvPV5OBoBtdWf6pl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LywQn7ZOxmgYXvj0njhTE6BqTnL9IFBIbkz4FQ8Ad6WNemQ== 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))