From: Alistair Popple <apopple@nvidia.com>
To: "Karol Wachowski" <karol.wachowski@linux.intel.com>,
"Sumit Semwal" <sumit.semwal@linaro.org>,
"Christian König" <christian.koenig@amd.com>,
"Philipp Stanner" <phasta@kernel.org>,
"David Airlie" <airlied@gmail.com>,
"Simona Vetter" <simona@ffwll.ch>,
"Rodrigo Vivi" <rodrigo.vivi@intel.com>,
"Matthew Brost" <matthew.brost@intel.com>,
"Thomas Hellström" <thomas.hellstrom@linux.intel.com>,
"Jason Gunthorpe" <jgg@ziepe.ca>,
"Leon Romanovsky" <leon@kernel.org>,
"Kees Cook" <kees@kernel.org>,
"Gustavo A. R. Silva" <gustavoars@kernel.org>,
"Alex Williamson" <alex@shazbot.org>,
"Kevin Tian" <kevin.tian@intel.com>,
"Joerg Roedel (AMD)" <joro@8bytes.org>,
"Will Deacon" <will@kernel.org>,
"Robin Murphy" <robin.murphy@arm.com>
Cc: dri-devel@lists.freedesktop.org, linux-media@vger.kernel.org,
linaro-mm-sig@lists.linaro.org, intel-xe@lists.freedesktop.org,
linux-rdma@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-hardening@vger.kernel.org, kvm@vger.kernel.org,
iommu@lists.linux.dev, Alexey Kardashevskiy <aik@amd.com>,
Mina Almasry <almasrymina@google.com>,
Baolu Lu <baolu.lu@linux.intel.com>,
chaitanyak@nvidia.com, Dongwon Kim <dongwon.kim@intel.com>,
Felix Kuehling <felix.kuehling@amd.com>,
jhubbard@nvidia.com, Karl Mehltretter <kmehltretter@gmail.com>,
Vivek Kasireddy <vivek.kasireddy@intel.com>,
Xu Yilun <yilun.xu@linux.intel.com>,
zhipingz@meta.com, Alistair Popple <apopple@nvidia.com>
Subject: [PATCH RFC 12/16] dma-buf: Remove dma_buf_phys_vec_to_sgt() and dma_buf_free_sgt()
Date: Tue, 01 Dec 2026 18:06:28 +1100 [thread overview]
Message-ID: <20261201-dmabuf_shared_buf_v1_xe_rc5-v1-12-b672717c7fc9@nvidia.com> (raw)
In-Reply-To: <20261201-dmabuf_shared_buf_v1_xe_rc5-v1-0-b672717c7fc9@nvidia.com>
vfio-pci and uverbs were the only callers of dma_buf_phys_vec_to_sgt()
and dma_buf_free_sgt(). Now that both hand out a dma_shared_buf
through map_dma_shared_buf() and leave the DMA mapping to the core,
neither function is used so remove them.
Signed-off-by: Alistair Popple <apopple@nvidia.com>
---
drivers/dma-buf/dma-buf-mapping.c | 254 --------------------------------------
include/linux/dma-buf-mapping.h | 8 --
2 files changed, 262 deletions(-)
diff --git a/drivers/dma-buf/dma-buf-mapping.c b/drivers/dma-buf/dma-buf-mapping.c
index ec9776b1dff0..fae2f435e67d 100644
--- a/drivers/dma-buf/dma-buf-mapping.c
+++ b/drivers/dma-buf/dma-buf-mapping.c
@@ -6,260 +6,6 @@
#include <linux/dma-buf-mapping.h>
#include <linux/dma-resv.h>
#include <linux/iommu.h>
-#include <linux/overflow.h>
-#include <linux/align.h>
-
-#define MAX_SG_ENT_SZ ALIGN_DOWN(UINT_MAX, PAGE_SIZE)
-
-static struct scatterlist *fill_sg_entry(struct scatterlist *sgl, size_t length,
- dma_addr_t addr)
-{
- size_t len;
-
- while (length) {
- len = min(length, MAX_SG_ENT_SZ);
- length -= len;
- /*
- * DMABUF abuses scatterlist to create a scatterlist
- * that does not have any CPU list, only the DMA list.
- * Always set the page related values to NULL to ensure
- * importers can't use it. The phys_addr based DMA API
- * does not require the CPU list for mapping or unmapping.
- */
- sg_set_page(sgl, NULL, 0, 0);
- sg_dma_address(sgl) = addr;
- sg_dma_len(sgl) = len;
- addr += len;
- /* Unconditionally advance. On last segment, this becomes NULL */
- sgl = sg_next(sgl);
- }
-
- return sgl;
-}
-
-static unsigned int calc_sg_nents(struct dma_iova_state *state,
- struct phys_vec *phys_vec, size_t nr_ranges,
- size_t size)
-{
- unsigned int nents = 0;
- size_t i;
-
- if (!state || !dma_use_iova(state)) {
- for (i = 0; i < nr_ranges; i++) {
- unsigned int added = DIV_ROUND_UP(phys_vec[i].len, MAX_SG_ENT_SZ);
-
- if (check_add_overflow(nents, added, &nents))
- return 0;
- }
- } else {
- /*
- * In IOVA case, there is only one SG entry which spans
- * for whole IOVA address space, but we need to make sure
- * that it fits sg->length, maybe we need more.
- */
- nents = DIV_ROUND_UP(size, MAX_SG_ENT_SZ);
- }
-
- return nents;
-}
-
-/**
- * struct dma_buf_dma - holds DMA mapping information
- * @sgt: Scatter-gather table
- * @state: DMA IOVA state relevant in IOMMU-based DMA
- * @size: Total size of DMA transfer
- */
-struct dma_buf_dma {
- struct sg_table sgt;
- struct dma_iova_state *state;
- size_t size;
-};
-
-/**
- * dma_buf_phys_vec_to_sgt - Returns the scatterlist table of the attachment
- * from arrays of physical vectors. This funciton is intended for MMIO memory
- * only.
- * @attach: [in] attachment whose scatterlist is to be returned
- * @provider: [in] p2pdma provider
- * @phys_vec: [in] array of physical vectors
- * @nr_ranges: [in] number of entries in phys_vec array
- * @size: [in] total size of phys_vec
- * @dir: [in] direction of DMA transfer
- *
- * Returns sg_table containing the scatterlist to be returned; returns ERR_PTR
- * on error. May return -EINTR if it is interrupted by a signal.
- *
- * On success, the DMA addresses and lengths in the returned scatterlist are
- * PAGE_SIZE aligned.
- *
- * A mapping must be unmapped by using dma_buf_free_sgt().
- *
- * NOTE: This function is intended for exporters. If direct traffic routing is
- * mandatory exporter should call routing pci_p2pdma_map_type() before calling
- * this function.
- */
-struct sg_table *dma_buf_phys_vec_to_sgt(struct dma_buf_attachment *attach,
- struct p2pdma_provider *provider,
- struct phys_vec *phys_vec,
- size_t nr_ranges, size_t size,
- enum dma_data_direction dir)
-{
- struct dma_buf_dma *dma;
- struct scatterlist *sgl;
- size_t mapped_len = 0;
- unsigned int nents;
- dma_addr_t addr;
- size_t i;
- int ret;
-
- if (WARN_ON(!attach || !attach->dmabuf || !provider))
- /* This function is supposed to work on MMIO memory only */
- return ERR_PTR(-EINVAL);
-
- dma_resv_assert_held(attach->dmabuf->resv);
-
- dma = kzalloc_obj(*dma);
- if (!dma)
- return ERR_PTR(-ENOMEM);
-
- switch (pci_p2pdma_map_type(provider, attach->dev)) {
- case PCI_P2PDMA_MAP_BUS_ADDR:
- /*
- * There is no need in IOVA at all for this flow.
- */
- break;
- case PCI_P2PDMA_MAP_THRU_HOST_BRIDGE:
- dma->state = kzalloc_obj(*dma->state);
- if (!dma->state) {
- ret = -ENOMEM;
- goto err_free_dma;
- }
-
- dma_iova_try_alloc(attach->dev, dma->state, 0, size);
- break;
- default:
- ret = -EINVAL;
- goto err_free_dma;
- }
-
- nents = calc_sg_nents(dma->state, phys_vec, nr_ranges, size);
-
- /* sg_alloc_table will cleanly fail and return -EINVAL if nents == 0 */
- ret = sg_alloc_table(&dma->sgt, nents, GFP_KERNEL | __GFP_ZERO);
- if (ret)
- goto err_free_state;
-
- sgl = dma->sgt.sgl;
-
- for (i = 0; i < nr_ranges; i++) {
- if (!dma->state) {
- addr = pci_p2pdma_bus_addr_map(provider,
- phys_vec[i].paddr);
- } else if (dma_use_iova(dma->state)) {
- ret = dma_iova_link(attach->dev, dma->state,
- phys_vec[i].paddr, mapped_len,
- phys_vec[i].len, dir,
- DMA_ATTR_MMIO);
- if (ret)
- goto err_unmap_dma;
-
- mapped_len += phys_vec[i].len;
- } else {
- addr = dma_map_phys(attach->dev, phys_vec[i].paddr,
- phys_vec[i].len, dir,
- DMA_ATTR_MMIO);
- ret = dma_mapping_error(attach->dev, addr);
- if (ret)
- goto err_unmap_dma;
- }
-
- if (!dma->state || !dma_use_iova(dma->state))
- sgl = fill_sg_entry(sgl, phys_vec[i].len, addr);
- }
-
- if (dma->state && dma_use_iova(dma->state)) {
- WARN_ON_ONCE(mapped_len != size);
- ret = dma_iova_sync(attach->dev, dma->state, 0, mapped_len);
- if (ret)
- goto err_unmap_dma;
-
- sgl = fill_sg_entry(sgl, mapped_len, dma->state->addr);
- }
-
- dma->size = size;
-
- /*
- * No CPU list included — set orig_nents = 0 so others can detect
- * this via SG table (use nents only).
- */
- dma->sgt.orig_nents = 0;
-
-
- /*
- * SGL must be NULL to indicate that SGL is the last one
- * and we allocated correct number of entries in sg_alloc_table()
- */
- WARN_ON_ONCE(sgl);
- return &dma->sgt;
-
-err_unmap_dma:
- if (!i || !dma->state) {
- ; /* Do nothing */
- } else if (dma_use_iova(dma->state)) {
- dma_iova_destroy(attach->dev, dma->state, mapped_len, dir,
- DMA_ATTR_MMIO);
- } else {
- for_each_sgtable_dma_sg(&dma->sgt, sgl, i)
- dma_unmap_phys(attach->dev, sg_dma_address(sgl),
- sg_dma_len(sgl), dir, DMA_ATTR_MMIO);
- }
- sg_free_table(&dma->sgt);
-err_free_state:
- kfree(dma->state);
-err_free_dma:
- kfree(dma);
- return ERR_PTR(ret);
-}
-EXPORT_SYMBOL_NS_GPL(dma_buf_phys_vec_to_sgt, "DMA_BUF");
-
-/**
- * dma_buf_free_sgt- unmaps the buffer
- * @attach: [in] attachment to unmap buffer from
- * @sgt: [in] scatterlist info of the buffer to unmap
- * @dir: [in] direction of DMA transfer
- *
- * This unmaps a DMA mapping for @attached obtained
- * by dma_buf_phys_vec_to_sgt().
- */
-void dma_buf_free_sgt(struct dma_buf_attachment *attach, struct sg_table *sgt,
- enum dma_data_direction dir)
-{
- struct dma_buf_dma *dma = container_of(sgt, struct dma_buf_dma, sgt);
- int i;
-
- dma_resv_assert_held(attach->dmabuf->resv);
-
- if (!dma->state) {
- ; /* Do nothing */
- } else if (dma_use_iova(dma->state)) {
- dma_iova_destroy(attach->dev, dma->state, dma->size, dir,
- DMA_ATTR_MMIO);
- } else {
- struct scatterlist *sgl;
-
- for_each_sgtable_dma_sg(sgt, sgl, i)
- dma_unmap_phys(attach->dev, sg_dma_address(sgl),
- sg_dma_len(sgl), dir, DMA_ATTR_MMIO);
- }
-
- /* Restore the allocation count cleared for the DMA-only table. */
- sgt->orig_nents = sgt->nents;
- sg_free_table(sgt);
- kfree(dma->state);
- kfree(dma);
-
-}
-EXPORT_SYMBOL_NS_GPL(dma_buf_free_sgt, "DMA_BUF");
/*
* IOMMU domain importers
diff --git a/include/linux/dma-buf-mapping.h b/include/linux/dma-buf-mapping.h
index 04fab6bd5dc7..e73a90c67fa4 100644
--- a/include/linux/dma-buf-mapping.h
+++ b/include/linux/dma-buf-mapping.h
@@ -7,14 +7,6 @@
#define __DMA_BUF_MAPPING_H__
#include <linux/dma-buf.h>
-struct sg_table *dma_buf_phys_vec_to_sgt(struct dma_buf_attachment *attach,
- struct p2pdma_provider *provider,
- struct phys_vec *phys_vec,
- size_t nr_ranges, size_t size,
- enum dma_data_direction dir);
-void dma_buf_free_sgt(struct dma_buf_attachment *attach, struct sg_table *sgt,
- enum dma_data_direction dir);
-
struct iommu_domain;
/*
--
2.54.0
next prev parent reply other threads:[~2026-12-01 7:08 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 7:06 [PATCH RFC 00/16] dma-buf: Introduce dma_shared_buf and dma_addr_list to replace sg_table Alistair Popple
2026-12-01 7:06 ` [PATCH RFC 01/16] treewide: Rename dma_buf_(un)map_attachment() to dma_buf_(un)map_sgt() Alistair Popple
2026-12-01 7:06 ` [PATCH RFC 02/16] dma-shared-buf: Add struct dma_shared_buf Alistair Popple
2026-12-01 7:06 ` [PATCH RFC 03/16] dma-shared-buf: Add dma_shared_buf_iommu_map() Alistair Popple
2026-12-01 7:06 ` [PATCH RFC 04/16] dma-shared-buf: Add dma_addr_list_to_sgt() Alistair Popple
2026-12-01 7:06 ` [PATCH RFC 05/16] dma-shared-buf: Add dma_shared_buf_from_pages() Alistair Popple
2026-12-01 7:06 ` [PATCH RFC 06/16] dma-buf: Allow exporters to hand out a dma_shared_buf Alistair Popple
2026-12-01 7:06 ` [PATCH RFC 07/16] dma-buf: Add dma_buf_iommu_map() for IOMMU domain importers Alistair Popple
2026-12-01 7:06 ` [PATCH RFC 08/16] dma-buf: Factor out the pin and fence wait around the exporter's map Alistair Popple
2026-12-01 7:06 ` [PATCH RFC 09/16] dma-buf: Add dma_buf_map_dma_list() for DMA address only importers Alistair Popple
2026-12-01 7:06 ` [PATCH RFC 10/16] vfio/pci: Export a dma_shared_buf instead of mapping the buffer Alistair Popple
2026-12-01 7:06 ` [PATCH RFC 11/16] RDMA/uverbs: " Alistair Popple
2026-12-01 7:06 ` Alistair Popple [this message]
2026-12-01 7:06 ` [PATCH RFC 13/16] iommufd: Map DMA-bufs through dma_buf_iommu_map() Alistair Popple
2026-12-01 7:06 ` [PATCH RFC 14/16] vfio/pci: Remove vfio_pci_dma_buf_iommufd_map() Alistair Popple
2026-12-01 7:06 ` [PATCH RFC 15/16] drm/xe/dma-buf: Export a dma_shared_buf instead of mapping the buffer Alistair Popple
2026-12-01 7:06 ` [PATCH RFC 16/16] drm/xe: Import DMA-bufs as a DMA address list Alistair Popple
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261201-dmabuf_shared_buf_v1_xe_rc5-v1-12-b672717c7fc9@nvidia.com \
--to=apopple@nvidia.com \
--cc=aik@amd.com \
--cc=airlied@gmail.com \
--cc=alex@shazbot.org \
--cc=almasrymina@google.com \
--cc=baolu.lu@linux.intel.com \
--cc=chaitanyak@nvidia.com \
--cc=christian.koenig@amd.com \
--cc=dongwon.kim@intel.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=felix.kuehling@amd.com \
--cc=gustavoars@kernel.org \
--cc=intel-xe@lists.freedesktop.org \
--cc=iommu@lists.linux.dev \
--cc=jgg@ziepe.ca \
--cc=jhubbard@nvidia.com \
--cc=joro@8bytes.org \
--cc=karol.wachowski@linux.intel.com \
--cc=kees@kernel.org \
--cc=kevin.tian@intel.com \
--cc=kmehltretter@gmail.com \
--cc=kvm@vger.kernel.org \
--cc=leon@kernel.org \
--cc=linaro-mm-sig@lists.linaro.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=linux-rdma@vger.kernel.org \
--cc=matthew.brost@intel.com \
--cc=phasta@kernel.org \
--cc=robin.murphy@arm.com \
--cc=rodrigo.vivi@intel.com \
--cc=simona@ffwll.ch \
--cc=sumit.semwal@linaro.org \
--cc=thomas.hellstrom@linux.intel.com \
--cc=vivek.kasireddy@intel.com \
--cc=will@kernel.org \
--cc=yilun.xu@linux.intel.com \
--cc=zhipingz@meta.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®