From: Alistair Popple <apopple@nvidia.com>
To: "Christian König" <christian.koenig@amd.com>,
"Sumit Semwal" <sumit.semwal@linaro.org>,
"Thomas Hellström" <thomas.hellstrom@linux.intel.com>,
"Simona Vetter" <simona.vetter@ffwll.ch>,
"Matthew Brost" <matthew.brost@intel.com>,
"Vivek Kasireddy" <vivek.kasireddy@intel.com>,
"Dongwon Kim" <dongwon.kim@intel.com>,
"Kevin Tian" <kevin.tian@intel.com>,
"Xu Yilun" <yilun.xu@linux.intel.com>,
"Karl Mehltretter" <kmehltretter@gmail.com>,
"Baolu Lu" <baolu.lu@linux.intel.com>,
"Felix Kuehling" <felix.kuehling@amd.com>,
"Alex Deucher" <alexander.deucher@amd.com>,
linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org,
intel-xe@lists.freedesktop.org, amd-gfx@lists.freedesktop.org,
linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org,
linux-rdma@vger.kernel.org, iommu@lists.linux.dev,
"Keith Busch" <kbusch@kernel.org>, "Jens Axboe" <axboe@kernel.dk>,
"Christoph Hellwig" <hch@lst.de>,
"Sagi Grimberg" <sagi@grimberg.me>,
linux-nvme@lists.infradead.org,
"David S. Miller" <davem@davemloft.net>,
"Eric Dumazet" <edumazet@kernel.org>,
"Jakub Kicinski" <kuba@kernel.org>,
"Paolo Abeni" <pabeni@redhat.com>,
"Simon Horman" <horms@kernel.org>,
"Mina Almasry" <almasrymina@google.com>,
netdev@vger.kernel.org, "Alexey Kardashevskiy" <aik@amd.com>,
zhipingz@meta.com, treding@nvidia.com, jhubbard@nvidia.com,
chaitanyak@nvidia.com, "Jason Gunthorpe" <jgg@ziepe.ca>,
"Leon Romanovsky" <leon@kernel.org>
Cc: Alistair Popple <apopple@nvidia.com>
Subject: [PATCH v3 17/24] dma-buf: Convert dma_buf_phys_vec_to_sgt() and its users to the SGT mapping type
Date: Fri, 20 Nov 2026 16:55:55 +1100 [thread overview]
Message-ID: <20261120-dma_mapping_type_v4-v3-17-c0183a409bdf@nvidia.com> (raw)
In-Reply-To: <20261120-dma_mapping_type_v4-v3-0-c0183a409bdf@nvidia.com>
Currently dma_buf_phys_vec_to_sgt() allocates and returns an sgt.
Instead convert it to allocate and return a dma_buf_sgt_mapping.
vfio-pci and RDMA/uverbs can then return the mapping directly.
Their map and unmap callbacks can then move from dma_buf_ops into
DMA_BUF_SIMPLE_SGT_EXP_MATCH().
Co-developed-by: Jason Gunthorpe <jgg@nvidia.com>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
Signed-off-by: Alistair Popple <apopple@nvidia.com>
---
drivers/dma-buf/dma-buf-mapping.c | 57 ++++++++++++-----------
drivers/infiniband/core/uverbs_std_types_dmabuf.c | 23 +++++----
drivers/vfio/pci/vfio_pci_dmabuf.c | 24 +++++-----
include/linux/dma-buf-mapping.h | 13 +++---
4 files changed, 61 insertions(+), 56 deletions(-)
diff --git a/drivers/dma-buf/dma-buf-mapping.c b/drivers/dma-buf/dma-buf-mapping.c
index cc3d8d6fe009..d2730be4a9f5 100644
--- a/drivers/dma-buf/dma-buf-mapping.c
+++ b/drivers/dma-buf/dma-buf-mapping.c
@@ -66,12 +66,12 @@ static unsigned int calc_sg_nents(struct dma_iova_state *state,
/**
* struct dma_buf_dma - holds DMA mapping information
- * @sgt: Scatter-gather table
+ * @mapping: The SGT mapping handed to the core, with the 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_buf_sgt_mapping mapping;
struct dma_iova_state *state;
size_t size;
};
@@ -99,12 +99,13 @@ struct dma_buf_dma {
* 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_sgt_mapping *
+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 device *dma_dev = dma_buf_sgt_dma_device(attach);
struct dma_buf_dma *dma;
struct scatterlist *sgl;
size_t mapped_len = 0;
@@ -123,7 +124,7 @@ struct sg_table *dma_buf_phys_vec_to_sgt(struct dma_buf_attachment *attach,
if (!dma)
return ERR_PTR(-ENOMEM);
- switch (pci_p2pdma_map_type(provider, attach->dev)) {
+ switch (pci_p2pdma_map_type(provider, dma_dev)) {
case PCI_P2PDMA_MAP_BUS_ADDR:
/*
* There is no need in IOVA at all for this flow.
@@ -136,7 +137,7 @@ struct sg_table *dma_buf_phys_vec_to_sgt(struct dma_buf_attachment *attach,
goto err_free_dma;
}
- dma_iova_try_alloc(attach->dev, dma->state, 0, size);
+ dma_iova_try_alloc(dma_dev, dma->state, 0, size);
break;
default:
ret = -EINVAL;
@@ -146,18 +147,18 @@ struct sg_table *dma_buf_phys_vec_to_sgt(struct dma_buf_attachment *attach,
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);
+ ret = sg_alloc_table(&dma->mapping.sgt, nents, GFP_KERNEL | __GFP_ZERO);
if (ret)
goto err_free_state;
- sgl = dma->sgt.sgl;
+ sgl = dma->mapping.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,
+ ret = dma_iova_link(dma_dev, dma->state,
phys_vec[i].paddr, 0,
phys_vec[i].len, dir,
DMA_ATTR_MMIO);
@@ -166,10 +167,10 @@ struct sg_table *dma_buf_phys_vec_to_sgt(struct dma_buf_attachment *attach,
mapped_len += phys_vec[i].len;
} else {
- addr = dma_map_phys(attach->dev, phys_vec[i].paddr,
+ addr = dma_map_phys(dma_dev, phys_vec[i].paddr,
phys_vec[i].len, dir,
DMA_ATTR_MMIO);
- ret = dma_mapping_error(attach->dev, addr);
+ ret = dma_mapping_error(dma_dev, addr);
if (ret)
goto err_unmap_dma;
}
@@ -180,7 +181,7 @@ struct sg_table *dma_buf_phys_vec_to_sgt(struct dma_buf_attachment *attach,
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);
+ ret = dma_iova_sync(dma_dev, dma->state, 0, mapped_len);
if (ret)
goto err_unmap_dma;
@@ -193,7 +194,7 @@ struct sg_table *dma_buf_phys_vec_to_sgt(struct dma_buf_attachment *attach,
* No CPU list included — set orig_nents = 0 so others can detect
* this via SG table (use nents only).
*/
- dma->sgt.orig_nents = 0;
+ dma->mapping.sgt.orig_nents = 0;
/*
@@ -201,20 +202,20 @@ struct sg_table *dma_buf_phys_vec_to_sgt(struct dma_buf_attachment *attach,
* and we allocated correct number of entries in sg_alloc_table()
*/
WARN_ON_ONCE(sgl);
- return &dma->sgt;
+ return &dma->mapping;
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_iova_destroy(dma_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),
+ for_each_sgtable_dma_sg(&dma->mapping.sgt, sgl, i)
+ dma_unmap_phys(dma_dev, sg_dma_address(sgl),
sg_dma_len(sgl), dir, DMA_ATTR_MMIO);
}
- sg_free_table(&dma->sgt);
+ sg_free_table(&dma->mapping.sgt);
err_free_state:
kfree(dma->state);
err_free_dma:
@@ -226,16 +227,20 @@ 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
+ * @mapping: [in] the mapping returned by dma_buf_phys_vec_to_sgt()
* @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,
+void dma_buf_free_sgt(struct dma_buf_attachment *attach,
+ struct dma_buf_sgt_mapping *mapping,
enum dma_data_direction dir)
{
- struct dma_buf_dma *dma = container_of(sgt, struct dma_buf_dma, sgt);
+ struct dma_buf_dma *dma =
+ container_of(mapping, struct dma_buf_dma, mapping);
+ struct sg_table *sgt = &mapping->sgt;
+ struct device *dma_dev = dma_buf_sgt_mapping_dma_device(mapping);
int i;
dma_resv_assert_held(attach->dmabuf->resv);
@@ -243,13 +248,13 @@ void dma_buf_free_sgt(struct dma_buf_attachment *attach, struct sg_table *sgt,
if (!dma->state) {
; /* Do nothing */
} else if (dma_use_iova(dma->state)) {
- dma_iova_destroy(attach->dev, dma->state, dma->size, dir,
+ dma_iova_destroy(dma_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),
+ dma_unmap_phys(dma_dev, sg_dma_address(sgl),
sg_dma_len(sgl), dir, DMA_ATTR_MMIO);
}
diff --git a/drivers/infiniband/core/uverbs_std_types_dmabuf.c b/drivers/infiniband/core/uverbs_std_types_dmabuf.c
index 8d8c997a4883..7463877db3d3 100644
--- a/drivers/infiniband/core/uverbs_std_types_dmabuf.c
+++ b/drivers/infiniband/core/uverbs_std_types_dmabuf.c
@@ -21,36 +21,36 @@ static int uverbs_dmabuf_attach(struct dma_buf *dmabuf,
return 0;
}
-static struct sg_table *
+static struct dma_buf_sgt_mapping *
uverbs_dmabuf_map(struct dma_buf_attachment *attachment,
enum dma_data_direction dir)
{
struct ib_uverbs_dmabuf_file *priv = attachment->dmabuf->priv;
- struct sg_table *ret;
+ struct dma_buf_sgt_mapping *mapping;
dma_resv_assert_held(attachment->dmabuf->resv);
if (priv->revoked)
return ERR_PTR(-ENODEV);
- ret = dma_buf_phys_vec_to_sgt(attachment, priv->provider,
- &priv->phys_vec, 1, priv->phys_vec.len,
- dir);
- if (IS_ERR(ret))
- return ret;
+ mapping = dma_buf_phys_vec_to_sgt(attachment, priv->provider,
+ &priv->phys_vec, 1,
+ priv->phys_vec.len, dir);
+ if (IS_ERR(mapping))
+ return ERR_CAST(mapping);
kref_get(&priv->kref);
- return ret;
+ return mapping;
}
static void uverbs_dmabuf_unmap(struct dma_buf_attachment *attachment,
- struct sg_table *sgt,
+ struct dma_buf_sgt_mapping *mapping,
enum dma_data_direction dir)
{
struct ib_uverbs_dmabuf_file *priv = attachment->dmabuf->priv;
dma_resv_assert_held(attachment->dmabuf->resv);
- dma_buf_free_sgt(attachment, sgt, dir);
+ dma_buf_free_sgt(attachment, mapping, dir);
kref_put(&priv->kref, ib_uverbs_dmabuf_done);
}
@@ -97,11 +97,10 @@ static void uverbs_dmabuf_release(struct dma_buf *dmabuf)
static const struct dma_buf_ops uverbs_dmabuf_ops = {
.attach = uverbs_dmabuf_attach,
- .map_dma_buf = uverbs_dmabuf_map,
- .unmap_dma_buf = uverbs_dmabuf_unmap,
.pin = uverbs_dmabuf_pin,
.unpin = uverbs_dmabuf_unpin,
.release = uverbs_dmabuf_release,
+ DMA_BUF_SIMPLE_SGT_EXP_MATCH(uverbs_dmabuf_map, uverbs_dmabuf_unmap),
};
static int UVERBS_HANDLER(UVERBS_METHOD_DMABUF_ALLOC)(
diff --git a/drivers/vfio/pci/vfio_pci_dmabuf.c b/drivers/vfio/pci/vfio_pci_dmabuf.c
index c16f460c01d6..ab3a7217e0a3 100644
--- a/drivers/vfio/pci/vfio_pci_dmabuf.c
+++ b/drivers/vfio/pci/vfio_pci_dmabuf.c
@@ -47,37 +47,37 @@ static void vfio_pci_dma_buf_done(struct kref *kref)
complete(&priv->comp);
}
-static struct sg_table *
+static struct dma_buf_sgt_mapping *
vfio_pci_dma_buf_map(struct dma_buf_attachment *attachment,
enum dma_data_direction dir)
{
struct vfio_pci_dma_buf *priv = attachment->dmabuf->priv;
- struct sg_table *ret;
+ struct dma_buf_sgt_mapping *mapping;
dma_resv_assert_held(priv->dmabuf->resv);
if (priv->revoked)
return ERR_PTR(-ENODEV);
- ret = dma_buf_phys_vec_to_sgt(attachment, priv->provider,
- priv->phys_vec, priv->nr_ranges,
- priv->size, dir);
- if (IS_ERR(ret))
- return ret;
+ mapping = dma_buf_phys_vec_to_sgt(attachment, priv->provider,
+ priv->phys_vec, priv->nr_ranges,
+ priv->size, dir);
+ if (IS_ERR(mapping))
+ return ERR_CAST(mapping);
kref_get(&priv->kref);
- return ret;
+ return mapping;
}
static void vfio_pci_dma_buf_unmap(struct dma_buf_attachment *attachment,
- struct sg_table *sgt,
+ struct dma_buf_sgt_mapping *mapping,
enum dma_data_direction dir)
{
struct vfio_pci_dma_buf *priv = attachment->dmabuf->priv;
dma_resv_assert_held(priv->dmabuf->resv);
- dma_buf_free_sgt(attachment, sgt, dir);
+ dma_buf_free_sgt(attachment, mapping, dir);
kref_put(&priv->kref, vfio_pci_dma_buf_done);
}
@@ -101,9 +101,9 @@ static void vfio_pci_dma_buf_release(struct dma_buf *dmabuf)
static const struct dma_buf_ops vfio_pci_dmabuf_ops = {
.attach = vfio_pci_dma_buf_attach,
- .map_dma_buf = vfio_pci_dma_buf_map,
- .unmap_dma_buf = vfio_pci_dma_buf_unmap,
.release = vfio_pci_dma_buf_release,
+ DMA_BUF_SIMPLE_SGT_EXP_MATCH(vfio_pci_dma_buf_map,
+ vfio_pci_dma_buf_unmap),
};
/*
diff --git a/include/linux/dma-buf-mapping.h b/include/linux/dma-buf-mapping.h
index e442ed0ede71..d73bff5d6853 100644
--- a/include/linux/dma-buf-mapping.h
+++ b/include/linux/dma-buf-mapping.h
@@ -108,12 +108,13 @@ dma_buf_mapping_type_of(const struct dma_buf_mapping *mapping)
return mapping->match->type;
}
-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,
+struct dma_buf_sgt_mapping *
+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 dma_buf_sgt_mapping *mapping,
enum dma_data_direction dir);
int dma_buf_match_mapping(struct dma_buf_match_args *args,
--
2.54.0
next prev parent reply other threads:[~2026-11-20 5:58 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-20 5:55 [PATCH v3 00/24] DMA-buf mapping types Alistair Popple
2026-11-20 5:55 ` [PATCH v3 01/24] treewide: Rename dma_buf_(un)map_attachment() to dma_buf_(un)map_sgt() Alistair Popple
2026-11-20 5:55 ` [PATCH v3 02/24] dma-buf: Introduce DMA-buf mapping types Alistair Popple
2026-11-20 5:55 ` [PATCH v3 03/24] dma-buf: Add the SGT DMA mapping type Alistair Popple
2026-11-20 5:55 ` [PATCH v3 04/24] dma-buf: Negotiate the mapping type in dma_buf_dynamic_attach() Alistair Popple
2026-11-20 5:55 ` [PATCH v3 05/24] dma-buf: Route SGT related actions through the attachment's mapping type Alistair Popple
2026-11-20 5:55 ` [PATCH v3 06/24] dma-buf: Allow single exporter drivers to avoid the match_mapping function Alistair Popple
2026-11-20 5:55 ` [PATCH v3 07/24] dma-buf: Convert exporters allocating a table per map to the SGT mapping type Alistair Popple
2026-11-20 5:55 ` [PATCH v3 08/24] iommufd/selftest: Convert the test exporter " Alistair Popple
2026-11-20 5:55 ` [PATCH v3 09/24] dma-buf: Convert exporters keeping a table per attachment " Alistair Popple
2026-11-20 5:55 ` [PATCH v3 10/24] drm: Convert drm_gem_map_dma_buf() and its users " Alistair Popple
2026-11-20 5:55 ` [PATCH v3 11/24] drm/xe/dma-buf: Use " Alistair Popple
2026-11-20 5:55 ` [PATCH v3 12/24] drm/amdgpu: " Alistair Popple
2026-11-20 5:55 ` [PATCH v3 13/24] udmabuf: Convert to " Alistair Popple
2026-11-20 5:55 ` [PATCH v3 14/24] drm/omapdrm: " Alistair Popple
2026-11-20 5:55 ` [PATCH v3 15/24] accel/ivpu: Convert the userptr exporter " Alistair Popple
2026-11-20 5:55 ` [PATCH v3 16/24] drm/vmwgfx: Use match_mapping instead of dummy calls Alistair Popple
2026-11-20 5:55 ` Alistair Popple [this message]
2026-11-20 5:55 ` [PATCH v3 18/24] iio: buffer: convert to use the SGT mapping type Alistair Popple
2026-11-20 5:55 ` [PATCH v3 19/24] functionfs: " 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=20261120-dma_mapping_type_v4-v3-17-c0183a409bdf@nvidia.com \
--to=apopple@nvidia.com \
--cc=aik@amd.com \
--cc=alexander.deucher@amd.com \
--cc=almasrymina@google.com \
--cc=amd-gfx@lists.freedesktop.org \
--cc=axboe@kernel.dk \
--cc=baolu.lu@linux.intel.com \
--cc=chaitanyak@nvidia.com \
--cc=christian.koenig@amd.com \
--cc=davem@davemloft.net \
--cc=dongwon.kim@intel.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=edumazet@kernel.org \
--cc=felix.kuehling@amd.com \
--cc=hch@lst.de \
--cc=horms@kernel.org \
--cc=intel-xe@lists.freedesktop.org \
--cc=iommu@lists.linux.dev \
--cc=jgg@ziepe.ca \
--cc=jhubbard@nvidia.com \
--cc=kbusch@kernel.org \
--cc=kevin.tian@intel.com \
--cc=kmehltretter@gmail.com \
--cc=kuba@kernel.org \
--cc=leon@kernel.org \
--cc=linaro-mm-sig@lists.linaro.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=linux-nvme@lists.infradead.org \
--cc=linux-rdma@vger.kernel.org \
--cc=matthew.brost@intel.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=sagi@grimberg.me \
--cc=simona.vetter@ffwll.ch \
--cc=sumit.semwal@linaro.org \
--cc=thomas.hellstrom@linux.intel.com \
--cc=treding@nvidia.com \
--cc=vivek.kasireddy@intel.com \
--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