From mboxrd@z Thu Jan 1 00:00:00 1970 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2F764C3ABA5 for ; Tue, 29 Apr 2025 18:23:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: Content-Type:In-Reply-To:From:References:Cc:To:Subject:MIME-Version:Date: Message-ID:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=k8MVtkHiFFg4EG4C+fF1ryyzx+RTLfKxBDUbr+C17Go=; b=ECDx4RKxD59KW3XkW4arl1E8ce 5AS2jLJz3pzSQehu8/c8DPsRZslMhK1VhrroWpWtDhesIqMBvphMpjCG2+3NkD3S9pxkYkSDv4NXW obm/0DirAtwQXEY4MerOIOXdi9BIL1kXckZC1+6T3jRkQU3D4wt9414wWAaz+nHsU1n6pGKI//NbN ixOxASlRzCYtLga/R0MOPbbdyJ3lDvIkRaSncYyzu6q29hVXNqXcK+haGWk+chy/fngZ7VYpDsdkN Q2O6yIlgh16eZNii2YpDhwqGRNwUxg6pSXqFhduG47NJbd9CWbnCOYdWFyhjcBUB0OHUZXS4TpmQi kbnql57g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1u9pbj-0000000AWKs-2m7o; Tue, 29 Apr 2025 18:23:03 +0000 Received: from mgamail.intel.com ([192.198.163.7]) by bombadil.infradead.org with esmtps (Exim 4.98.2 #2 (Red Hat Linux)) id 1u9e31-00000008TKf-1tIB for linux-nvme@lists.infradead.org; Tue, 29 Apr 2025 06:02:29 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1745906547; x=1777442547; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=GGOZBkZPXbXcgYWtmEjaWNBSh9vK7IAgAJHtHjY5RGk=; b=cs3CrsLtcywzIDWhehr2+2I0xo1z0iAcY6R3d9NMyFBcaxisWpP/tOyM q2+TRmM0GZu2gcR5WRVsVZK30l2HpZo8rKmvE0QKaMINpSa6cVN97ACd6 Gy2nSrVFPKouz1NWobSG2arMop28f3L/vqjARyA/WFTHa/mrpl3vv2bvU 60Jn31frNVHAlYfGqyycmtiMteyUgDRuq5WkGQGX60hjRAU7GHeYbODR2 dLtlcq+8CCUSWvQ7aZibQdHESYAHcSB50nOG0Tb9Q0QBaaQ/MPR0DOSwC kLgq3KKqP3o88egOFFxVkTaN+f24TCN3fypqlqFiUoARH2PP6FNaAB0+I A==; X-CSE-ConnectionGUID: DiOP2v5hSdS5R6E3yOet6A== X-CSE-MsgGUID: fVFXsmawSi+eiZyegvsPsw== X-IronPort-AV: E=McAfee;i="6700,10204,11417"; a="72891424" X-IronPort-AV: E=Sophos;i="6.15,248,1739865600"; d="scan'208";a="72891424" Received: from orviesa008.jf.intel.com ([10.64.159.148]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Apr 2025 23:02:25 -0700 X-CSE-ConnectionGUID: B6mYu5WYR6y4hHVzuSOAgA== X-CSE-MsgGUID: KHRkdKHCSYO/GfkXnTeDdQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.15,248,1739865600"; d="scan'208";a="134708735" Received: from allen-sbox.sh.intel.com (HELO [10.239.159.30]) ([10.239.159.30]) by orviesa008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Apr 2025 23:02:18 -0700 Message-ID: <9d1abdbc-4b21-47e2-bcaf-6bc8ca365b01@linux.intel.com> Date: Tue, 29 Apr 2025 13:58:06 +0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v10 06/24] iommu/dma: Factor out a iommu_dma_map_swiotlb helper To: Leon Romanovsky Cc: Marek Szyprowski , Jens Axboe , Christoph Hellwig , Keith Busch , 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?B?SsOpcsO0bWUgR2xpc3Nl?= , 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 References: <8416e94f-171e-4956-b8fe-246ed12a2314@linux.intel.com> <20250429055339.GJ5848@unreal> Content-Language: en-US From: Baolu Lu In-Reply-To: <20250429055339.GJ5848@unreal> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250428_230227_509893_422C8190 X-CRM114-Status: GOOD ( 22.75 ) X-Mailman-Approved-At: Tue, 29 Apr 2025 11:22:51 -0700 X-BeenThere: linux-nvme@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Linux-nvme" Errors-To: linux-nvme-bounces+linux-nvme=archiver.kernel.org@lists.infradead.org On 4/29/25 13:53, Leon Romanovsky wrote: > On Tue, Apr 29, 2025 at 12:58:18PM +0800, Baolu Lu wrote: >> On 4/28/25 17:22, Leon Romanovsky wrote: >>> 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 >>> Reviewed-by: Luis Chamberlain >>> Signed-off-by: Leon Romanovsky >> >> Reviewed-by: Lu Baolu >> >> with a nit below ... >> >>> --- >>> 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 d3211a8d755e..d7684024c439 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); >>> } >>> +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 (phys_addr_t)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 != (phys_addr_t)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) >>> @@ -1151,42 +1188,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)) { >> >> ... Is it better to move this check into the helper? Simply no-op if a >> bounce page is not needed: >> >> if (!dev_use_swiotlb(dev, size, dir) || >> !iova_offset(iovad, phys | size)) >> return phys; > > Am I missing something? iommu_dma_map_page() has more code after this > check, so it is not correct to return immediately: > > 1189 dma_addr_t iommu_dma_map_page(struct device *dev, struct page *page, > 1190 unsigned long offset, size_t size, enum dma_data_direction dir, > 1191 unsigned long attrs) > 1192 { > > <...> > > 1201 /* > 1202 * If both the physical buffer start address and size are page aligned, > 1203 * we don't need to use a bounce page. > 1204 */ > 1205 if (dev_use_swiotlb(dev, size, dir) && > 1206 iova_unaligned(iovad, phys, size)) { > 1207 phys = iommu_dma_map_swiotlb(dev, phys, size, dir, attrs); > 1208 if (phys == (phys_addr_t)DMA_MAPPING_ERROR) > 1209 return DMA_MAPPING_ERROR; > 1210 } > 1211 > 1212 if (!coherent && !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) > 1213 arch_sync_dma_for_device(phys, size, dir); > 1214 > 1215 iova = __iommu_dma_map(dev, phys, size, prot, dma_mask); > 1216 if (iova == DMA_MAPPING_ERROR) > 1217 swiotlb_tbl_unmap_single(dev, phys, size, dir, attrs); > 1218 return iova; > 1219 } 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) { <...> /* * 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)) return phys; <...> } Then, 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) { <...> phys = iommu_dma_map_swiotlb(dev, phys, size, dir, attrs); if (phys == (phys_addr_t)DMA_MAPPING_ERROR) return DMA_MAPPING_ERROR; <...> } Thanks, baolu