From nobody Tue Sep 9 09:11:47 2025 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 239C915A86B; Tue, 2 Jul 2024 09:10:25 +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=1719911425; cv=none; b=WYvBTiaueRLOUD9Ck8pUi9LYv0Wtd4WkiaSM9T0bwqo/065LOt6obQeLVudN5hQ722jnKUkpmjPkA5SUIRNQ9e3uSn+tqo/xc85Kti2wlgHKEWj+xoUTkX3HcFarI0jPE7ifgPvqlRoIvzS552HtZVq7h3t1Y9yx7Eag+heaNCw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719911425; c=relaxed/simple; bh=3LQD0/3Cm7R9Az4oi5cuZkbR6iiFWyUtiH8FYV5qU6g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tAaXXdzxLkD+hs8DdUXJ/yJMmnis8lKrLvniIq4tPDnqjYajeSwpNqsCHOWJdxj9Kh6U8EWCCgNeeBk1E6yB56AYZ//lsLfw1KnDHjZ4wZoatZuZqHDM8NwdeGBSqfMzevcMKdSvB/H8sQLvKXjdL4ueRp7NFphBHEA5YUpfkU0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=jzgab3Uf; 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="jzgab3Uf" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3695BC116B1; Tue, 2 Jul 2024 09:10:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1719911425; bh=3LQD0/3Cm7R9Az4oi5cuZkbR6iiFWyUtiH8FYV5qU6g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jzgab3UfsAsewcAa24eobcDOjpYZ/7PPMau2CiYGMxN+FfxGb00wDLL3BI2ELXmGU moRg+6YKgbtiDf2JVUe5eQJT8XKaWX5rrUt19Vkg6yDcQ3EOq1SxCxg+IKYC8PD3l0 HS940fsiVLIaK9q8PMwxocD9VU27iN2gNzMGrNBw8q8UcVws7pN76iWryjdgzqisHm VXjMg0SkcQEtPyECwWKViptiiD9d+jmj3OBTAB4yD01wnJBA03aMuR0LwlA7S+3cjd 2MBCI6RclneZK65+IxUismvWUQZUYYw63kyUErE4LhTZye947A31418PnSxjVrmxDt Tsc8PYJOJc7iA== From: Leon Romanovsky To: Jens Axboe , Jason Gunthorpe , Robin Murphy , Joerg Roedel , Will Deacon , Keith Busch , Christoph Hellwig , "Zeng, Oak" , Chaitanya Kulkarni Cc: Leon Romanovsky , Sagi Grimberg , 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 , linux-block@vger.kernel.org, linux-kernel@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: [RFC PATCH v1 07/18] iommu/dma: Provide an interface to allow preallocate IOVA Date: Tue, 2 Jul 2024 12:09:37 +0300 Message-ID: X-Mailer: git-send-email 2.45.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 Separate IOVA allocation to dedicated callback so it will allow cache of IOVA and reuse it in fast paths for devices which support ODP (on-demand-paging) mechanism. Signed-off-by: Leon Romanovsky --- drivers/iommu/dma-iommu.c | 50 +++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index 89e34503e0bb..0b5ca6961940 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -357,7 +357,7 @@ int iommu_dma_init_fq(struct iommu_domain *domain) atomic_set(&cookie->fq_timer_on, 0); /* * Prevent incomplete fq state being observable. Pairs with path from - * __iommu_dma_unmap() through iommu_dma_free_iova() to queue_iova() + * __iommu_dma_unmap() through __iommu_dma_free_iova() to queue_iova() */ smp_wmb(); WRITE_ONCE(cookie->fq_domain, domain); @@ -745,7 +745,7 @@ static int dma_info_to_prot(enum dma_data_direction dir= , bool coherent, } } =20 -static dma_addr_t iommu_dma_alloc_iova(struct iommu_domain *domain, +static dma_addr_t __iommu_dma_alloc_iova(struct iommu_domain *domain, size_t size, u64 dma_limit, struct device *dev) { struct iommu_dma_cookie *cookie =3D domain->iova_cookie; @@ -791,7 +791,7 @@ static dma_addr_t iommu_dma_alloc_iova(struct iommu_dom= ain *domain, return (dma_addr_t)iova << shift; } =20 -static void iommu_dma_free_iova(struct iommu_dma_cookie *cookie, +static void __iommu_dma_free_iova(struct iommu_dma_cookie *cookie, dma_addr_t iova, size_t size, struct iommu_iotlb_gather *gather) { struct iova_domain *iovad =3D &cookie->iovad; @@ -828,7 +828,7 @@ static void __iommu_dma_unmap(struct device *dev, dma_a= ddr_t dma_addr, =20 if (!iotlb_gather.queued) iommu_iotlb_sync(domain, &iotlb_gather); - iommu_dma_free_iova(cookie, dma_addr, size, &iotlb_gather); + __iommu_dma_free_iova(cookie, dma_addr, size, &iotlb_gather); } =20 static dma_addr_t __iommu_dma_map(struct device *dev, phys_addr_t phys, @@ -851,12 +851,12 @@ static dma_addr_t __iommu_dma_map(struct device *dev,= phys_addr_t phys, =20 size =3D iova_align(iovad, size + iova_off); =20 - iova =3D iommu_dma_alloc_iova(domain, size, dma_mask, dev); + iova =3D __iommu_dma_alloc_iova(domain, size, dma_mask, dev); if (!iova) return DMA_MAPPING_ERROR; =20 if (iommu_map(domain, iova, phys - iova_off, size, prot, GFP_ATOMIC)) { - iommu_dma_free_iova(cookie, iova, size, NULL); + __iommu_dma_free_iova(cookie, iova, size, NULL); return DMA_MAPPING_ERROR; } return iova + iova_off; @@ -960,7 +960,7 @@ static struct page **__iommu_dma_alloc_noncontiguous(st= ruct device *dev, return NULL; =20 size =3D iova_align(iovad, size); - iova =3D iommu_dma_alloc_iova(domain, size, dev->coherent_dma_mask, dev); + iova =3D __iommu_dma_alloc_iova(domain, size, dev->coherent_dma_mask, dev= ); if (!iova) goto out_free_pages; =20 @@ -994,7 +994,7 @@ static struct page **__iommu_dma_alloc_noncontiguous(st= ruct device *dev, out_free_sg: sg_free_table(sgt); out_free_iova: - iommu_dma_free_iova(cookie, iova, size, NULL); + __iommu_dma_free_iova(cookie, iova, size, NULL); out_free_pages: __iommu_dma_free_pages(pages, count); return NULL; @@ -1429,7 +1429,7 @@ static int iommu_dma_map_sg(struct device *dev, struc= t scatterlist *sg, if (!iova_len) return __finalise_sg(dev, sg, nents, 0); =20 - iova =3D iommu_dma_alloc_iova(domain, iova_len, dma_get_mask(dev), dev); + iova =3D __iommu_dma_alloc_iova(domain, iova_len, dma_get_mask(dev), dev); if (!iova) { ret =3D -ENOMEM; goto out_restore_sg; @@ -1446,7 +1446,7 @@ static int iommu_dma_map_sg(struct device *dev, struc= t scatterlist *sg, return __finalise_sg(dev, sg, nents, iova); =20 out_free_iova: - iommu_dma_free_iova(cookie, iova, iova_len, NULL); + __iommu_dma_free_iova(cookie, iova, iova_len, NULL); out_restore_sg: __invalidate_sg(sg, nents); out: @@ -1707,6 +1707,30 @@ static size_t iommu_dma_max_mapping_size(struct devi= ce *dev) return SIZE_MAX; } =20 +static dma_addr_t iommu_dma_alloc_iova(struct device *dev, 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; + dma_addr_t dma_mask =3D dma_get_mask(dev); + + size =3D iova_align(iovad, size); + return __iommu_dma_alloc_iova(domain, size, dma_mask, dev); +} + +static void iommu_dma_free_iova(struct device *dev, dma_addr_t iova, + 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; + struct iommu_iotlb_gather iotlb_gather; + + size =3D iova_align(iovad, size); + iommu_iotlb_gather_init(&iotlb_gather); + __iommu_dma_free_iova(cookie, iova, size, &iotlb_gather); +} + static const struct dma_map_ops iommu_dma_ops =3D { .flags =3D DMA_F_PCI_P2PDMA_SUPPORTED | DMA_F_CAN_SKIP_SYNC, @@ -1731,6 +1755,8 @@ static const struct dma_map_ops iommu_dma_ops =3D { .get_merge_boundary =3D iommu_dma_get_merge_boundary, .opt_mapping_size =3D iommu_dma_opt_mapping_size, .max_mapping_size =3D iommu_dma_max_mapping_size, + .alloc_iova =3D iommu_dma_alloc_iova, + .free_iova =3D iommu_dma_free_iova, }; =20 void iommu_setup_dma_ops(struct device *dev) @@ -1773,7 +1799,7 @@ static struct iommu_dma_msi_page *iommu_dma_get_msi_p= age(struct device *dev, if (!msi_page) return NULL; =20 - iova =3D iommu_dma_alloc_iova(domain, size, dma_get_mask(dev), dev); + iova =3D __iommu_dma_alloc_iova(domain, size, dma_get_mask(dev), dev); if (!iova) goto out_free_page; =20 @@ -1787,7 +1813,7 @@ static struct iommu_dma_msi_page *iommu_dma_get_msi_p= age(struct device *dev, return msi_page; =20 out_free_iova: - iommu_dma_free_iova(cookie, iova, size, NULL); + __iommu_dma_free_iova(cookie, iova, size, NULL); out_free_page: kfree(msi_page); return NULL; --=20 2.45.2