From patchwork Tue Dec 17 13:00:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leon Romanovsky X-Patchwork-Id: 13911734 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 49CE0E7717F for ; Tue, 17 Dec 2024 13:01:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CFAA66B00C3; Tue, 17 Dec 2024 08:01:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CAACD6B00C4; Tue, 17 Dec 2024 08:01:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B4D616B00C5; Tue, 17 Dec 2024 08:01:24 -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 87B0F6B00C3 for ; Tue, 17 Dec 2024 08:01:24 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 03DAB1A06F2 for ; Tue, 17 Dec 2024 13:01:23 +0000 (UTC) X-FDA: 82904461122.23.DD7FB1C Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf16.hostedemail.com (Postfix) with ESMTP id 02727180037 for ; Tue, 17 Dec 2024 13:00:50 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="RFwhKA/z"; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf16.hostedemail.com: domain of leon@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=leon@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734440453; a=rsa-sha256; cv=none; b=P+mUryRpQLHVJ/e+asnWZGwS/7ExC6lKqwFVE1jhCERzRcGs1UpbKU6+aHFewYg47mZ1O9 lyyzJCcEOZOsWt2JmJMeKte8a4Atygnbmy8zhsu8F/zNSh+aznnBBhrEgkgh0/FwLfwZXN PnkYI+6y/LKrP1TNxTMjSISHnj31hyU= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="RFwhKA/z"; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf16.hostedemail.com: domain of leon@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=leon@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1734440453; 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=Iy0gp93DodbSNSHicJXf8vjZWTU+QO9fJqZUilF8j+A=; b=zI6KyA+qnk9YZ6RjHvub5GO4/e+/7AVjVDq8m/HiZZJmPQR78HhqjKnrswvKADiuJ0XwSc e2uF5V6mzQVBZqiCsZN6H7HzAlQsgk5y6l38+e8uVh6O3IoOEDwruncpjy9VfSsW+nS2Hf g0GaVlWqwL7hm1+sa09uk/LY9OZv/SA= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 171795C0741; Tue, 17 Dec 2024 13:00:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F0224C4CED3; Tue, 17 Dec 2024 13:01:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734440480; bh=d/zWaTbRWl2SeZvtdCC9mFOn7TRDMhrcIPAPxHQpIr4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RFwhKA/zJWrblb4tpgarpe8NCYl7Kl2IT/4LiFu4JEiCX69VRrPbAZkQAGaOJEJUG 5fkoMi3XhIgW1V/eBjC2CzrDZRR9pd4IGf/8iULSNVoGyZUk5C77Hy3XkWv4AzJe87 2B/s9yHsmSXseje9qkDjFCbbmpqKc211dWgcz+iNybtlScOcz31SGjfAWmfy1E0t40 HXFObjwTar9/gFpvUpIoYpBsTSr0MSqYqFUdxfZyVTd0gnZkOaD0gwlnkMF6G5rmhk cAQnHnoGyVL1zogPve1mYPzmZSR28x+EXnYsqaCcz4YDsseXmIRuy0SMaKi0w4hDzK bl050WlzPuL5g== 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 v5 06/17] iommu/dma: Factor out a iommu_dma_map_swiotlb helper Date: Tue, 17 Dec 2024 15:00:24 +0200 Message-ID: X-Mailer: git-send-email 2.47.0 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 02727180037 X-Stat-Signature: u8grysqb3dentop5ngouqd49ws1tphtw X-Rspam-User: X-HE-Tag: 1734440450-818320 X-HE-Meta: U2FsdGVkX1+wnlChHaBXcYP8HTVn3Pf/8i7em3ITYJbZDxnqSjUx/EJRk8z0zgXhbihq6mMOLOD5gktslTVzqm1rSjs/JJaQHixxZWx2+1DU7CMTdV/yq3FRzVL6mSCkjGczuy3yeuG3+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iqPjBXTrSzvT/iqertvkYeSGNbZAF6Pj5djX171p7mCqIvGbVu8je2QlCnQ+hsYQ+Lsj 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 5906b47a300c..d473ea4329ab 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))