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 C74A5C369D5 for ; Sun, 27 Apr 2025 08:13:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 20B1F6B0005; Sun, 27 Apr 2025 04:13:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 191BF6B0006; Sun, 27 Apr 2025 04:13:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 00B586B0007; Sun, 27 Apr 2025 04:13:20 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id C97646B0005 for ; Sun, 27 Apr 2025 04:13:20 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 0F4C4C140F for ; Sun, 27 Apr 2025 08:13:22 +0000 (UTC) X-FDA: 83379109044.08.4F5E7DA Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf14.hostedemail.com (Postfix) with ESMTP id 5FFA7100008 for ; Sun, 27 Apr 2025 08:13:20 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="l/Og4bFj"; spf=pass (imf14.hostedemail.com: domain of leon@kernel.org designates 139.178.84.217 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=1745741600; 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=yuTEcEqbJ5b/BSlup/EjJyouGcoJebwoTOVTOXZZSCk=; b=KumlUgmbOT7P6UkrLWv+CYH01XptcADjgzcjsnXOowD2FWb5w3+YPJAPeqEGFHFB82ogF2 5in/6rwQJIuDIGpDs+G8j5MKQi44gvH7Abpc9FA/ekUVdtJnJEmumemKEfruR7Q7guBJnv HnF9PAWnH8JDnNNVbqL466bKb/sKTeU= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="l/Og4bFj"; spf=pass (imf14.hostedemail.com: domain of leon@kernel.org designates 139.178.84.217 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=1745741600; a=rsa-sha256; cv=none; b=cKC2KumqHJed2A9mBjI9LOUBsJkF6bM3tHyZyBlqA8JHbqBRbCO0U5sWEPa99E8cyFD+7q I+2jlxJh0MZ0OnO6NVVDri2ZBSxbuhm1A8aqxBNwMzipVP6XPFv4EysA8knZ6mjWZvOF7f wFoCJcw7YOz81UtEIMuuKJLNxRlyvcg= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id DC8C05C5BAD; Sun, 27 Apr 2025 08:11:01 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 103DBC4CEE3; Sun, 27 Apr 2025 08:13:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1745741598; bh=n1LP0hWDAYYPPGoxCmhHUpx37KloYPiSW/LsrkJ+pog=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=l/Og4bFjNyyLOyqiobd0OX+NlvlhiWMjlqCuR7bjQ3koqjqNsiHAbxhyy+n6EFWpx F0s02toBkpYSqLGr5zllGOwnm/YR+cBtijfhM7TVWEWRlqZZD7tdVQ2f1cr6H3j+vY /V4qD4omORMMjdDmZK0stm+5Mw4PJdjUhKlKsl6UBn8mYclrE4YOqm0/14yFLod31h ZCy3hMjY93C/oDHv0y2/amWuCxtPm2rAUMkBEU2/j5WJZJ36ZRGs00HmVVhJdNAAU3 OAmjF5VOgM3oeArUzKcU+xI7P+9v0yugvVf2x9c6hhu7fKaqUTKAgYRmAQgjnW9EMm 4KL9ASZVaAifQ== Date: Sun, 27 Apr 2025 11:13:12 +0300 From: Leon Romanovsky To: Luis Chamberlain 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 , Matthew Wilcox , Dan Williams , Kanchan Joshi , Chaitanya Kulkarni Subject: Re: [PATCH v9 07/24] dma-mapping: Implement link/unlink ranges API Message-ID: <20250427081312.GE5848@unreal> References: <2d6ca43ef8d26177d7674b9e3bdf0fe62b55a7ed.1745394536.git.leon@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Stat-Signature: d73gpcjshmt51yg4jkoonh9dwakkp88m X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 5FFA7100008 X-Rspam-User: X-HE-Tag: 1745741600-789466 X-HE-Meta: U2FsdGVkX18UA8cwrRQc6n75d4wTZS52iJbM2DphKsgfkBRt7ulcw90snaVChqmPeilN1GNzrkQl3oQNe9I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v5vhbak1wjSpAkzZPlObRFagPR6gAVCgC3y3cs6T4ZzhTZtSktPwllLWePp1urCDSIvuw/tnASU4oAgDir6xH7SLlAd0g7CwD7qUOiui1oiVZ4oo5S0rotui1VxxGMzKlGC+SjJMyqiV00rk= 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: On Sat, Apr 26, 2025 at 03:46:30PM -0700, Luis Chamberlain wrote: > On Wed, Apr 23, 2025 at 11:12:58AM +0300, Leon Romanovsky wrote: > > From: Leon Romanovsky > > > > Introduce new DMA APIs to perform DMA linkage of buffers > > in layers higher than DMA. > > > > In proposed API, the callers will perform the following steps. > > In map path: > > if (dma_can_use_iova(...)) > > dma_iova_alloc() > > for (page in range) > > dma_iova_link_next(...) > > dma_iova_sync(...) > > else > > /* Fallback to legacy map pages */ > > for (all pages) > > dma_map_page(...) > > > > In unmap path: > > if (dma_can_use_iova(...)) > > dma_iova_destroy() > > else > > for (all pages) > > dma_unmap_page(...) > > > > Reviewed-by: Christoph Hellwig > > Tested-by: Jens Axboe > > Signed-off-by: Leon Romanovsky > > --- > > drivers/iommu/dma-iommu.c | 261 ++++++++++++++++++++++++++++++++++++ > > include/linux/dma-mapping.h | 32 +++++ > > 2 files changed, 293 insertions(+) > > > > diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c > > index d2c298083e0a..2e014db5a244 100644 > > --- a/drivers/iommu/dma-iommu.c > > +++ b/drivers/iommu/dma-iommu.c > > @@ -1818,6 +1818,267 @@ void dma_iova_free(struct device *dev, struct dma_iova_state *state) > > } > > EXPORT_SYMBOL_GPL(dma_iova_free); > > > > +static int __dma_iova_link(struct device *dev, dma_addr_t addr, > > + phys_addr_t phys, size_t size, enum dma_data_direction dir, > > + unsigned long attrs) > > +{ > > + bool coherent = dev_is_dma_coherent(dev); > > + > > + if (!coherent && !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) > > + arch_sync_dma_for_device(phys, size, dir); > > So arch_sync_dma_for_device() is a no-op on some architectures, notably x86. > So since you're doing this work and given the above pattern is common on > the non iova case, we could save ourselves 2 branches checks on x86 on > __dma_iova_link() and also generalize savings for the non-iova case as > well. For the non-iova case we have two use cases, one with the attrs on > initial mapping, and one without on subsequent sync ops. For the iova > case the attr is always consistently used. I want to believe that compiler will discards these "if (!coherent && !(attrs & DMA_ATTR_SKIP_CPU_SYNC)))" branch if case is empty. > > So we could just have something like this: > > #ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE > static inline void arch_sync_dma_device(struct device *dev, > phys_addr_t paddr, size_t size, > enum dma_data_direction dir) > { > if (!dev_is_dma_coherent(dev)) > arch_sync_dma_for_device(paddr, size, dir); > } > > static inline void arch_sync_dma_device_attrs(struct device *dev, > phys_addr_t paddr, size_t size, > enum dma_data_direction dir, > unsigned long attrs) > { > if (!dev_is_dma_coherent(dev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) > arch_sync_dma_for_device(paddr, size, dir); > } > #else > static inline void arch_sync_dma_device(struct device *dev, > phys_addr_t paddr, size_t size, > enum dma_data_direction dir) > { > } > > static inline void arch_sync_dma_device_attrs(struct device *dev, > phys_addr_t paddr, size_t size, > enum dma_data_direction dir, > unsigned long attrs) > { > } > #endif The problem is that dev_is_dma_coherent() and DMA_ATTR_SKIP_CPU_SYNC checks are scattered over all dma-iommu.c file with different combinations. While we can do new static functions for small number of use cases, it will be half-solution. > > > +/** > > + * dma_iova_link - Link a range of IOVA space > > + * @dev: DMA device > > + * @state: IOVA state > > + * @phys: physical address to link > > + * @offset: offset into the IOVA state to map into > > + * @size: size of the buffer > > + * @dir: DMA direction > > + * @attrs: attributes of mapping properties > > + * > > + * Link a range of IOVA space for the given IOVA state without IOTLB sync. > > + * This function is used to link multiple physical addresses in contiguous > > + * IOVA space without performing costly IOTLB sync. > > + * > > + * The caller is responsible to call to dma_iova_sync() to sync IOTLB at > > + * the end of linkage. > > + */ > > +int dma_iova_link(struct device *dev, struct dma_iova_state *state, > > + phys_addr_t phys, size_t offset, size_t size, > > + enum dma_data_direction dir, unsigned long attrs) > > +{ > > + struct iommu_domain *domain = iommu_get_dma_domain(dev); > > + struct iommu_dma_cookie *cookie = domain->iova_cookie; > > + struct iova_domain *iovad = &cookie->iovad; > > + size_t iova_start_pad = iova_offset(iovad, phys); > > + > > + if (WARN_ON_ONCE(iova_start_pad && offset > 0)) > > + return -EIO; > > + > > + if (dev_use_swiotlb(dev, size, dir) && iova_offset(iovad, phys | size)) > > There is already a similar check for the non-iova case for this on > iommu_dma_map_page() and a nice comment about what why this checked, > this seems to be just screaming for a helper: > > /* > * Checks if a physical buffer has unaligned boundaries with respect to > * the IOMMU granule. Returns non-zero if either the start or end > * address is not aligned to the granule boundary. > */ > static inline size_t iova_unaligned(struct iova_domain *iovad, > phys_addr_t phys, > size_t size) > { > return iova_offset(iovad, phys | size); > } I added this function, thanks. > Other than that, looks good. > > Reviewed-by: Luis Chamberlain > > Luis