From nobody Mon Nov 25 05:48:07 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CD8CD21312B; Wed, 30 Oct 2024 15:13:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730301217; cv=none; b=sBnfLKboamDi+UNL4O/LfRo0mK6alrV5Td0HF0T7ZzFyvPfzS6bFmsuOeGrNPhj0jI3gF7o8dNdKTGIVuJSVFgHd3gm8+H77sGeYpbXgXXA8qgg41AH0n0GyQ/NUxDmft6OkVzo+rws1a509ny+bjB5wkTHopPW2OV9dAV+CmG0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730301217; c=relaxed/simple; bh=wHgEMwPQysWFRNninL/6ReSJNwvAIakyr6CEHJOLARw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=I+N3FR8Cjbjxh60TMrzmmOLxVX+bXuTPZ7Qfd0xLOqnav0haiJyEJWMyb0zRCuVM/7Q3C0xh/6wnxZoTofOhSEEX2Tl4nm7FA7WM1bVzDfkNXhKgId/t7SOlJNnfzkOTdWjxJ1EvzRWem0mapb+Cg86JEe3JK/0n/DwnQmwIKN4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=SbwHDIv5; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="SbwHDIv5" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C2123C4CED2; Wed, 30 Oct 2024 15:13:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1730301216; bh=wHgEMwPQysWFRNninL/6ReSJNwvAIakyr6CEHJOLARw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SbwHDIv5BcBoTUVjjDQDtfvTuNMIDzcN5nDrlN1kiCBopZXLtJxiGHs8GmFNSny6i xM06GCTdJcEBYA0nsJrS5joJzq8XbFneIawU8UeLMrR9Przpd6vT94nB4ReBhUjG47 vFCiJgZi0F4NXugQ8xGts9u9XVa472CAeaLbQoKs0FtDWG2/its9c7DJ0KdsN2YYfI AGaSV2WzluQ4Awrsbu4697lpMRqZlfJvrPxoSk1xFj6foQmOhvnkzvbjB1WYp73wUP Ju0sDenS3aETSRn//mw80yUhcsCkQ35k//5CF6T/F2j6xOrFddNsTzfs4Glb9qf50Z zJeVW46+yVnuw== From: Leon Romanovsky To: Jens Axboe , Jason Gunthorpe , Robin Murphy , Joerg Roedel , Will Deacon , Christoph Hellwig , Sagi Grimberg Cc: Leon Romanovsky , Keith Busch , Bjorn Helgaas , Logan Gunthorpe , Yishai Hadas , Shameer Kolothum , Kevin Tian , Alex Williamson , Marek Szyprowski , =?UTF-8?q?J=C3=A9r=C3=B4me=20Glisse?= , 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 Subject: [PATCH v1 05/17] dma: Provide an interface to allow allocate IOVA Date: Wed, 30 Oct 2024 17:12:51 +0200 Message-ID: X-Mailer: git-send-email 2.46.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Leon Romanovsky The existing .map_page() callback provides both allocating of IOVA and linking DMA pages. That combination works great for most of the callers who use it in control paths, but is less effective in fast paths where there may be multiple calls to map_page(). These advanced callers already manage their data in some sort of database and can perform IOVA allocation in advance, leaving range linkage operation to be in fast path. Provide an interface to allocate/deallocate IOVA and next patch link/unlink DMA ranges to that specific IOVA. The API is exported from dma-iommu as it is the only implementation supported, the namespace is clearly different from iommu_* functions which are not allowed to be used. This code layout allows us to save function call per API call used in datapath as well as a lot of boilerplate code. Signed-off-by: Leon Romanovsky --- drivers/iommu/dma-iommu.c | 79 +++++++++++++++++++++++++++++++++++++ include/linux/dma-mapping.h | 15 +++++++ 2 files changed, 94 insertions(+) diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index 853247c42f7d..127150f63c95 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -1746,6 +1746,85 @@ size_t iommu_dma_max_mapping_size(struct device *dev) return SIZE_MAX; } =20 +static bool iommu_dma_iova_alloc(struct device *dev, + struct dma_iova_state *state, phys_addr_t phys, size_t size) +{ + struct iommu_domain *domain =3D iommu_get_dma_domain(dev); + struct iommu_dma_cookie *cookie =3D domain->iova_cookie; + struct iova_domain *iovad =3D &cookie->iovad; + size_t iova_off =3D iova_offset(iovad, phys); + dma_addr_t addr; + + if (WARN_ON_ONCE(!size)) + return false; + if (WARN_ON_ONCE(size & DMA_IOVA_USE_SWIOTLB)) + return false; + + addr =3D iommu_dma_alloc_iova(domain, + iova_align(iovad, size + iova_off), + dma_get_mask(dev), dev); + if (!addr) + return false; + + state->addr =3D addr + iova_off; + state->__size =3D size; + return true; +} + +/** + * dma_iova_try_alloc - Try to allocate an IOVA space + * @dev: Device to allocate the IOVA space for + * @state: IOVA state + * @phys: physical address + * @size: IOVA size + * + * Check if @dev supports the IOVA-based DMA API, and if yes allocate IOVA= space + * for the given base address and size. + * + * Note: @phys is only used to calculate the IOVA alignment. Callers that = always + * do PAGE_SIZE aligned transfers can safely pass 0 here. + * + * Returns %true if the IOVA-based DMA API can be used and IOVA space has = been + * allocated, or %false if the regular DMA API should be used. + */ +bool dma_iova_try_alloc(struct device *dev, struct dma_iova_state *state, + phys_addr_t phys, size_t size) +{ + memset(state, 0, sizeof(*state)); + if (!use_dma_iommu(dev)) + return false; + if (static_branch_unlikely(&iommu_deferred_attach_enabled) && + iommu_deferred_attach(dev, iommu_get_domain_for_dev(dev))) + return false; + return iommu_dma_iova_alloc(dev, state, phys, size); +} +EXPORT_SYMBOL_GPL(dma_iova_try_alloc); + +/** + * dma_iova_free - Free an IOVA space + * @dev: Device to free the IOVA space for + * @state: IOVA state + * + * Undoes a successful dma_try_iova_alloc(). + * + * Note that all dma_iova_link() calls need to be undone first. For calle= rs + * that never call dma_iova_unlink(), dma_iova_destroy() can be used inste= ad + * which unlinks all ranges and frees the IOVA space in a single efficient + * operation. + */ +void dma_iova_free(struct device *dev, struct dma_iova_state *state) +{ + struct iommu_domain *domain =3D iommu_get_dma_domain(dev); + struct iommu_dma_cookie *cookie =3D domain->iova_cookie; + struct iova_domain *iovad =3D &cookie->iovad; + size_t iova_start_pad =3D iova_offset(iovad, state->addr); + size_t size =3D dma_iova_size(state); + + iommu_dma_free_iova(cookie, state->addr - iova_start_pad, + iova_align(iovad, size + iova_start_pad), NULL); +} +EXPORT_SYMBOL_GPL(dma_iova_free); + void iommu_setup_dma_ops(struct device *dev) { struct iommu_domain *domain =3D iommu_get_domain_for_dev(dev); diff --git a/include/linux/dma-mapping.h b/include/linux/dma-mapping.h index 6075e0708deb..817f11bce7bc 100644 --- a/include/linux/dma-mapping.h +++ b/include/linux/dma-mapping.h @@ -11,6 +11,7 @@ #include #include #include +#include =20 /** * List of possible attributes associated with a DMA mapping. The semantics @@ -77,6 +78,7 @@ #define DMA_BIT_MASK(n) (((n) =3D=3D 64) ? ~0ULL : ((1ULL<<(n))-1)) =20 struct dma_iova_state { + dma_addr_t addr; size_t __size; }; =20 @@ -307,11 +309,24 @@ static inline bool dma_use_iova(struct dma_iova_state= *state) { return state->__size !=3D 0; } + +bool dma_iova_try_alloc(struct device *dev, struct dma_iova_state *state, + phys_addr_t phys, size_t size); +void dma_iova_free(struct device *dev, struct dma_iova_state *state); #else /* CONFIG_IOMMU_DMA */ static inline bool dma_use_iova(struct dma_iova_state *state) { return false; } +static inline bool dma_iova_try_alloc(struct device *dev, + struct dma_iova_state *state, phys_addr_t phys, size_t size) +{ + return false; +} +static inline void dma_iova_free(struct device *dev, + struct dma_iova_state *state) +{ +} #endif /* CONFIG_IOMMU_DMA */ =20 #if defined(CONFIG_HAS_DMA) && defined(CONFIG_DMA_NEED_SYNC) --=20 2.46.2