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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 12C2BC369CB for ; Tue, 29 Apr 2025 06:18:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7FA6F6B0023; Tue, 29 Apr 2025 02:18:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7D0D26B0025; Tue, 29 Apr 2025 02:18:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6998F6B0026; Tue, 29 Apr 2025 02:18:56 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 4A3E56B0023 for ; Tue, 29 Apr 2025 02:18:56 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 1B9EE12106B for ; Tue, 29 Apr 2025 06:18:57 +0000 (UTC) X-FDA: 83386078314.28.62E862D Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by imf18.hostedemail.com (Postfix) with ESMTP id 64B3B1C0004 for ; Tue, 29 Apr 2025 06:18:55 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=kTGc5PsS; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf18.hostedemail.com: domain of leon@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=leon@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1745907535; a=rsa-sha256; cv=none; b=Cb+58y1TX30KO/4dw7AyKATrAS1cC70vf31Xr0Ncr5cUrBA8P5L9x13lTsVwPyeSnl5EXz bLAHilrhxTnz1tp/z3dNS+zeWjxhvAAkIW2Xms+5A4Ur99tRL/p2lDZqO9PjAW9HJEEJqS Kb/87n1i/C6sLPqe5jodSwCSejUL5bI= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=kTGc5PsS; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf18.hostedemail.com: domain of leon@kernel.org designates 172.234.252.31 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=1745907535; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=cXngafouEooNdsj/BODKgjj0K/XsonzLE5Y+hOh1+Fg=; b=Ypz9/AD/hPftiCnSd33oaNo61o8QkmTeayU8XNIRD54SZXigTo+8Z5tQd8JANODo74xmap 7ACTI/L5srBWGYyU5Gl0V1b+TfPbdGmkjn2HyTotL09+FQ9herZWijIo3MoxT6Nb3Vf2Pb NkWJCyZxIhPtYJBmIfPPPA6UFQB8P5o= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sea.source.kernel.org (Postfix) with ESMTP id E408F44395; Tue, 29 Apr 2025 06:18:51 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E7F91C4CEE3; Tue, 29 Apr 2025 06:18:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1745907533; bh=vExQvIRKgBFgSfzxHEyrFALVuzPRUmMmvaWq9RovN60=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=kTGc5PsSm7YoP9EmrE3/tfdAeh2ilrH28OMMhkA1i7TaGCseb+D12f8U4iRskFrE0 RSM+Ykr/eeWEOV3E2nvuETPKv5WgV2r3K5S0fSLkCufCBscRZZxm5uTAwOKKZQ34F/ w5YLetGv0DGVj4FS79jc9/KMtke5/3tgrMHoSQ+QeJcqJgLI/MgFlbbaDFejjar1j7 p+0iatgN6tx2738QkPeIAJSzLkpoG/pCdY9ka1HCzAgLK31eNn58UEn17swH4oZLFI lBdvSt34FC1+vbqL/TFGUu3Gtp8x21gTZM2GnLCqybPJvtISk7Z3RIRONLDJ0zW3JA NWZpkM6/vXKLw== Date: Tue, 29 Apr 2025 09:18:49 +0300 From: Leon Romanovsky To: Baolu Lu 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 , =?iso-8859-1?B?Suly9G1l?= Glisse , 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 Subject: Re: [PATCH v10 06/24] iommu/dma: Factor out a iommu_dma_map_swiotlb helper Message-ID: <20250429061849.GL5848@unreal> References: <8416e94f-171e-4956-b8fe-246ed12a2314@linux.intel.com> <20250429055339.GJ5848@unreal> <9d1abdbc-4b21-47e2-bcaf-6bc8ca365b01@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <9d1abdbc-4b21-47e2-bcaf-6bc8ca365b01@linux.intel.com> X-Rspamd-Queue-Id: 64B3B1C0004 X-Stat-Signature: jbhtam3tpnpuetfj66exqzagbsgrt6kx X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1745907535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: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Tue, Apr 29, 2025 at 01:58:06PM +0800, Baolu Lu wrote: > 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; > <...> > } Such change will cause to extra function call for everyone who doesn't use SWIOTLB (RDMA, HMM e.t.c). In addition, iommu_dma_map_swiotlb() is called through dma_iova_link -> iommu_dma_iova_link_swiotlb -> iommu_dma_iova_bounce_and_link() -> iommu_dma_map_swiotlb() and dma_iova_link() has this "if (dev_use_swiotlb(dev, size, dir) && iova_unaligned(iovad, phys, size))" very early at call stack. So, in dma_iova_link() we will find ourselves with same check twice. Thanks > > Thanks, > baolu >