* [PATCH RFC 01/16] treewide: Rename dma_buf_(un)map_attachment() to dma_buf_(un)map_sgt()
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 ` Alistair Popple
2026-12-01 7:06 ` [PATCH RFC 02/16] dma-shared-buf: Add struct dma_shared_buf Alistair Popple
` (15 subsequent siblings)
16 siblings, 0 replies; 21+ messages in thread
From: Alistair Popple @ 2026-12-01 7:06 UTC (permalink / raw)
To: Karol Wachowski, Sumit Semwal, Christian König,
Philipp Stanner, David Airlie, Simona Vetter, Rodrigo Vivi,
Matthew Brost, Thomas Hellström, Jason Gunthorpe,
Leon Romanovsky, Kees Cook, Gustavo A. R. Silva, Alex Williamson,
Kevin Tian, Joerg Roedel (AMD), Will Deacon, Robin Murphy
Cc: dri-devel, linux-media, linaro-mm-sig, intel-xe, linux-rdma,
linux-kernel, linux-hardening, kvm, iommu, Alexey Kardashevskiy,
Mina Almasry, Baolu Lu, chaitanyak, Dongwon Kim, Felix Kuehling,
jhubbard, Karl Mehltretter, Vivek Kasireddy, Xu Yilun, zhipingz,
Alistair Popple, Jason Gunthorpe
From: Jason Gunthorpe <jgg@nvidia.com>
These map and unmap functions only work with SGT importers. Rename them,
and their _unlocked variants, so the scatterlist based interface is
clearly named as such before further mapping types are added.
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
Signed-off-by: Alistair Popple <apopple@nvidia.com>
---
drivers/accel/amdxdna/amdxdna_gem.c | 6 +--
drivers/accel/ivpu/ivpu_gem.c | 6 +--
drivers/accel/qaic/qaic_data.c | 6 +--
drivers/dma-buf/dma-buf.c | 58 +++++++++++-----------
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 4 +-
drivers/gpu/drm/armada/armada_gem.c | 16 +++---
drivers/gpu/drm/drm_prime.c | 6 +--
drivers/gpu/drm/i915/gem/i915_gem_dmabuf.c | 8 +--
drivers/gpu/drm/i915/gem/i915_gem_object.c | 2 +-
.../gpu/drm/i915/gem/selftests/i915_gem_dmabuf.c | 4 +-
drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c | 4 +-
drivers/gpu/drm/tegra/gem.c | 14 +++---
drivers/gpu/drm/virtio/virtgpu_prime.c | 8 +--
drivers/gpu/drm/xe/xe_bo.c | 16 +++---
drivers/iio/industrialio-buffer.c | 6 +--
drivers/infiniband/core/umem_dmabuf.c | 8 +--
drivers/media/common/videobuf2/videobuf2-core.c | 2 +-
.../media/common/videobuf2/videobuf2-dma-contig.c | 8 +--
drivers/media/common/videobuf2/videobuf2-dma-sg.c | 4 +-
.../media/platform/nvidia/tegra-vde/dmabuf-cache.c | 6 +--
drivers/misc/fastrpc.c | 6 +--
drivers/nvme/host/pci-dmabuf.c | 6 +--
drivers/usb/gadget/function/f_fs.c | 4 +-
drivers/xen/gntdev-dmabuf.c | 8 +--
include/linux/dma-buf.h | 27 +++++-----
io_uring/zcrx.c | 6 +--
net/core/devmem.c | 8 +--
27 files changed, 129 insertions(+), 128 deletions(-)
diff --git a/drivers/accel/amdxdna/amdxdna_gem.c b/drivers/accel/amdxdna/amdxdna_gem.c
index f14484bc31ec..adee6e806eed 100644
--- a/drivers/accel/amdxdna/amdxdna_gem.c
+++ b/drivers/accel/amdxdna/amdxdna_gem.c
@@ -701,7 +701,7 @@ static struct dma_buf *amdxdna_gem_prime_export(struct drm_gem_object *gobj, int
static void amdxdna_imported_obj_free(struct amdxdna_gem_obj *abo)
{
- dma_buf_unmap_attachment_unlocked(abo->attach, abo->base.sgt, DMA_BIDIRECTIONAL);
+ dma_buf_unmap_sgt_unlocked(abo->attach, abo->base.sgt, DMA_BIDIRECTIONAL);
dma_buf_detach(abo->dma_buf, abo->attach);
dma_buf_put(abo->dma_buf);
drm_gem_object_release(to_gobj(abo));
@@ -1075,7 +1075,7 @@ amdxdna_gem_prime_import(struct drm_device *dev, struct dma_buf *dma_buf)
goto put_buf;
}
- sgt = dma_buf_map_attachment_unlocked(attach, DMA_BIDIRECTIONAL);
+ sgt = dma_buf_map_sgt_unlocked(attach, DMA_BIDIRECTIONAL);
if (IS_ERR(sgt)) {
ret = PTR_ERR(sgt);
goto fail_detach;
@@ -1096,7 +1096,7 @@ amdxdna_gem_prime_import(struct drm_device *dev, struct dma_buf *dma_buf)
return gobj;
fail_unmap:
- dma_buf_unmap_attachment_unlocked(attach, sgt, DMA_BIDIRECTIONAL);
+ dma_buf_unmap_sgt_unlocked(attach, sgt, DMA_BIDIRECTIONAL);
fail_detach:
dma_buf_detach(dma_buf, attach);
put_buf:
diff --git a/drivers/accel/ivpu/ivpu_gem.c b/drivers/accel/ivpu/ivpu_gem.c
index c521a53f89bb..276f2a04aeda 100644
--- a/drivers/accel/ivpu/ivpu_gem.c
+++ b/drivers/accel/ivpu/ivpu_gem.c
@@ -54,7 +54,7 @@ static struct sg_table *ivpu_bo_map_attachment(struct ivpu_device *vdev, struct
sgt = bo->base.sgt;
if (!sgt) {
- sgt = dma_buf_map_attachment(bo->base.base.import_attach, DMA_BIDIRECTIONAL);
+ sgt = dma_buf_map_sgt(bo->base.base.import_attach, DMA_BIDIRECTIONAL);
if (IS_ERR(sgt))
ivpu_err(vdev, "Failed to map BO in IOMMU: %ld\n", PTR_ERR(sgt));
else
@@ -158,8 +158,8 @@ static void ivpu_bo_unbind_locked(struct ivpu_bo *bo)
if (bo->base.sgt) {
if (drm_gem_is_imported(&bo->base.base)) {
- dma_buf_unmap_attachment(bo->base.base.import_attach,
- bo->base.sgt, DMA_BIDIRECTIONAL);
+ dma_buf_unmap_sgt(bo->base.base.import_attach,
+ bo->base.sgt, DMA_BIDIRECTIONAL);
} else {
dma_unmap_sgtable(vdev->drm.dev, bo->base.sgt, DMA_BIDIRECTIONAL, 0);
sg_free_table(bo->base.sgt);
diff --git a/drivers/accel/qaic/qaic_data.c b/drivers/accel/qaic/qaic_data.c
index be3ceb8c51a0..1f0b31fefc44 100644
--- a/drivers/accel/qaic/qaic_data.c
+++ b/drivers/accel/qaic/qaic_data.c
@@ -869,7 +869,7 @@ struct drm_gem_object *qaic_gem_prime_import(struct drm_device *dev, struct dma_
drm_gem_private_object_init(dev, obj, attach->dmabuf->size);
/*
- * skipping dma_buf_map_attachment() as we do not know the direction
+ * skipping dma_buf_map_sgt() as we do not know the direction
* just yet. Once the direction is known in the subsequent IOCTL to
* attach slicing, we can do it then.
*/
@@ -895,7 +895,7 @@ static int qaic_prepare_import_bo(struct qaic_bo *bo, struct qaic_attach_slice_h
struct sg_table *sgt;
int ret;
- sgt = dma_buf_map_attachment(obj->import_attach, hdr->dir);
+ sgt = dma_buf_map_sgt(obj->import_attach, hdr->dir);
if (IS_ERR(sgt)) {
ret = PTR_ERR(sgt);
return ret;
@@ -936,7 +936,7 @@ static int qaic_prepare_bo(struct qaic_device *qdev, struct qaic_bo *bo,
static void qaic_unprepare_import_bo(struct qaic_bo *bo)
{
- dma_buf_unmap_attachment(bo->base.import_attach, bo->sgt, bo->dir);
+ dma_buf_unmap_sgt(bo->base.import_attach, bo->sgt, bo->dir);
bo->sgt = NULL;
}
diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c
index 0dac2c4fb968..0b8da0c0bc5f 100644
--- a/drivers/dma-buf/dma-buf.c
+++ b/drivers/dma-buf/dma-buf.c
@@ -677,7 +677,7 @@ static struct file *dma_buf_getfile(size_t size, int flags)
*
* 3. Once the buffer is attached to all devices userspace can initiate DMA
* access to the shared buffer. In the kernel this is done by calling
- * dma_buf_map_attachment() and dma_buf_unmap_attachment().
+ * dma_buf_map_sgt() and dma_buf_unmap_sgt().
*
* 4. Once a driver is done with a shared buffer it needs to call
* dma_buf_detach() (after cleaning up any mappings) and then release the
@@ -959,8 +959,8 @@ dma_buf_pin_on_map(struct dma_buf_attachment *attach)
*
* - dma_buf_pin()
* - dma_buf_unpin()
- * - dma_buf_map_attachment()
- * - dma_buf_unmap_attachment()
+ * - dma_buf_map_sgt()
+ * - dma_buf_unmap_sgt()
* - dma_buf_vmap()
* - dma_buf_vunmap()
*
@@ -977,8 +977,8 @@ dma_buf_pin_on_map(struct dma_buf_attachment *attach)
* - dma_buf_mmap()
* - dma_buf_begin_cpu_access()
* - dma_buf_end_cpu_access()
- * - dma_buf_map_attachment_unlocked()
- * - dma_buf_unmap_attachment_unlocked()
+ * - dma_buf_map_sgt_unlocked()
+ * - dma_buf_unmap_sgt_unlocked()
* - dma_buf_vmap_unlocked()
* - dma_buf_vunmap_unlocked()
*
@@ -1169,7 +1169,7 @@ void dma_buf_unpin(struct dma_buf_attachment *attach)
EXPORT_SYMBOL_NS_GPL(dma_buf_unpin, "DMA_BUF");
/**
- * dma_buf_map_attachment - Returns the scatterlist table of the attachment;
+ * dma_buf_map_sgt - Returns the scatterlist table of the attachment;
* mapped into _device_ address space. Is a wrapper for map_dma_buf() of the
* dma_buf_ops.
* @attach: [in] attachment whose scatterlist is to be returned
@@ -1181,7 +1181,7 @@ EXPORT_SYMBOL_NS_GPL(dma_buf_unpin, "DMA_BUF");
* On success, the DMA addresses and lengths in the returned scatterlist are
* PAGE_SIZE aligned.
*
- * A mapping must be unmapped by using dma_buf_unmap_attachment(). Note that
+ * A mapping must be unmapped by using dma_buf_unmap_sgt(). Note that
* the underlying backing storage is pinned for as long as a mapping exists,
* therefore users/importers should not hold onto a mapping for undue amounts of
* time.
@@ -1189,8 +1189,8 @@ EXPORT_SYMBOL_NS_GPL(dma_buf_unpin, "DMA_BUF");
* Important: Dynamic importers must wait for the exclusive fence of the struct
* dma_resv attached to the DMA-BUF first.
*/
-struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
- enum dma_data_direction direction)
+struct sg_table *dma_buf_map_sgt(struct dma_buf_attachment *attach,
+ enum dma_data_direction direction)
{
struct sg_table *sg_table;
signed long ret;
@@ -1261,20 +1261,20 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
return sg_table;
}
-EXPORT_SYMBOL_NS_GPL(dma_buf_map_attachment, "DMA_BUF");
+EXPORT_SYMBOL_NS_GPL(dma_buf_map_sgt, "DMA_BUF");
/**
- * dma_buf_map_attachment_unlocked - Returns the scatterlist table of the attachment;
+ * dma_buf_map_sgt_unlocked - Returns the scatterlist table of the attachment;
* mapped into _device_ address space. Is a wrapper for map_dma_buf() of the
* dma_buf_ops.
* @attach: [in] attachment whose scatterlist is to be returned
* @direction: [in] direction of DMA transfer
*
- * Unlocked variant of dma_buf_map_attachment().
+ * Unlocked variant of dma_buf_map_sgt().
*/
struct sg_table *
-dma_buf_map_attachment_unlocked(struct dma_buf_attachment *attach,
- enum dma_data_direction direction)
+dma_buf_map_sgt_unlocked(struct dma_buf_attachment *attach,
+ enum dma_data_direction direction)
{
struct sg_table *sg_table;
@@ -1284,26 +1284,26 @@ dma_buf_map_attachment_unlocked(struct dma_buf_attachment *attach,
return ERR_PTR(-EINVAL);
dma_resv_lock(attach->dmabuf->resv, NULL);
- sg_table = dma_buf_map_attachment(attach, direction);
+ sg_table = dma_buf_map_sgt(attach, direction);
dma_resv_unlock(attach->dmabuf->resv);
return sg_table;
}
-EXPORT_SYMBOL_NS_GPL(dma_buf_map_attachment_unlocked, "DMA_BUF");
+EXPORT_SYMBOL_NS_GPL(dma_buf_map_sgt_unlocked, "DMA_BUF");
/**
- * dma_buf_unmap_attachment - unmaps and decreases usecount of the buffer;might
+ * dma_buf_unmap_sgt - unmaps and decreases usecount of the buffer;might
* deallocate the scatterlist associated. Is a wrapper for unmap_dma_buf() of
* dma_buf_ops.
* @attach: [in] attachment to unmap buffer from
* @sg_table: [in] scatterlist info of the buffer to unmap
* @direction: [in] direction of DMA transfer
*
- * This unmaps a DMA mapping for @attached obtained by dma_buf_map_attachment().
+ * This unmaps a DMA mapping for @attached obtained by dma_buf_map_sgt().
*/
-void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
- struct sg_table *sg_table,
- enum dma_data_direction direction)
+void dma_buf_unmap_sgt(struct dma_buf_attachment *attach,
+ struct sg_table *sg_table,
+ enum dma_data_direction direction)
{
might_sleep();
@@ -1318,21 +1318,21 @@ void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
if (dma_buf_pin_on_map(attach))
attach->dmabuf->ops->unpin(attach);
}
-EXPORT_SYMBOL_NS_GPL(dma_buf_unmap_attachment, "DMA_BUF");
+EXPORT_SYMBOL_NS_GPL(dma_buf_unmap_sgt, "DMA_BUF");
/**
- * dma_buf_unmap_attachment_unlocked - unmaps and decreases usecount of the buffer;might
+ * dma_buf_unmap_sgt_unlocked - unmaps and decreases usecount of the buffer;might
* deallocate the scatterlist associated. Is a wrapper for unmap_dma_buf() of
* dma_buf_ops.
* @attach: [in] attachment to unmap buffer from
* @sg_table: [in] scatterlist info of the buffer to unmap
* @direction: [in] direction of DMA transfer
*
- * Unlocked variant of dma_buf_unmap_attachment().
+ * Unlocked variant of dma_buf_unmap_sgt().
*/
-void dma_buf_unmap_attachment_unlocked(struct dma_buf_attachment *attach,
- struct sg_table *sg_table,
- enum dma_data_direction direction)
+void dma_buf_unmap_sgt_unlocked(struct dma_buf_attachment *attach,
+ struct sg_table *sg_table,
+ enum dma_data_direction direction)
{
might_sleep();
@@ -1340,10 +1340,10 @@ void dma_buf_unmap_attachment_unlocked(struct dma_buf_attachment *attach,
return;
dma_resv_lock(attach->dmabuf->resv, NULL);
- dma_buf_unmap_attachment(attach, sg_table, direction);
+ dma_buf_unmap_sgt(attach, sg_table, direction);
dma_resv_unlock(attach->dmabuf->resv);
}
-EXPORT_SYMBOL_NS_GPL(dma_buf_unmap_attachment_unlocked, "DMA_BUF");
+EXPORT_SYMBOL_NS_GPL(dma_buf_unmap_sgt_unlocked, "DMA_BUF");
/**
* dma_buf_attach_revocable - check if a DMA-buf importer implements
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
index 326abc9b7f69..f1582970bdb1 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
@@ -962,7 +962,7 @@ static int amdgpu_ttm_backend_bind(struct ttm_device *bdev,
struct sg_table *sgt;
attach = gtt->gobj->import_attach;
- sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
+ sgt = dma_buf_map_sgt(attach, DMA_BIDIRECTIONAL);
if (IS_ERR(sgt))
return PTR_ERR(sgt);
@@ -1083,7 +1083,7 @@ static void amdgpu_ttm_backend_unbind(struct ttm_device *bdev,
struct dma_buf_attachment *attach;
attach = gtt->gobj->import_attach;
- dma_buf_unmap_attachment(attach, ttm->sg, DMA_BIDIRECTIONAL);
+ dma_buf_unmap_sgt(attach, ttm->sg, DMA_BIDIRECTIONAL);
ttm->sg = NULL;
}
diff --git a/drivers/gpu/drm/armada/armada_gem.c b/drivers/gpu/drm/armada/armada_gem.c
index da7335cbe82d..044acdef6351 100644
--- a/drivers/gpu/drm/armada/armada_gem.c
+++ b/drivers/gpu/drm/armada/armada_gem.c
@@ -67,8 +67,8 @@ void armada_gem_free_object(struct drm_gem_object *obj)
if (dobj->obj.import_attach) {
/* We only ever display imported data */
if (dobj->sgt)
- dma_buf_unmap_attachment_unlocked(dobj->obj.import_attach,
- dobj->sgt, DMA_TO_DEVICE);
+ dma_buf_unmap_sgt_unlocked(dobj->obj.import_attach,
+ dobj->sgt, DMA_TO_DEVICE);
drm_prime_gem_destroy(&dobj->obj, NULL);
}
@@ -529,7 +529,7 @@ armada_gem_prime_import(struct drm_device *dev, struct dma_buf *buf)
get_dma_buf(buf);
/*
- * Don't call dma_buf_map_attachment() here - it maps the
+ * Don't call dma_buf_map_sgt() here - it maps the
* scatterlist immediately for DMA, and this is not always
* an appropriate thing to do.
*/
@@ -540,20 +540,20 @@ int armada_gem_map_import(struct armada_gem_object *dobj)
{
int ret;
- dobj->sgt = dma_buf_map_attachment_unlocked(dobj->obj.import_attach,
- DMA_TO_DEVICE);
+ dobj->sgt = dma_buf_map_sgt_unlocked(dobj->obj.import_attach,
+ DMA_TO_DEVICE);
if (IS_ERR(dobj->sgt)) {
ret = PTR_ERR(dobj->sgt);
dobj->sgt = NULL;
- DRM_ERROR("dma_buf_map_attachment() error: %d\n", ret);
+ DRM_ERROR("dma_buf_map_sgt() error: %d\n", ret);
return ret;
}
if (dobj->sgt->nents > 1) {
- DRM_ERROR("dma_buf_map_attachment() returned an (unsupported) scattered list\n");
+ DRM_ERROR("dma_buf_map_sgt() returned an (unsupported) scattered list\n");
return -EINVAL;
}
if (sg_dma_len(dobj->sgt->sgl) < dobj->obj.size) {
- DRM_ERROR("dma_buf_map_attachment() returned a small buffer\n");
+ DRM_ERROR("dma_buf_map_sgt() returned a small buffer\n");
return -EINVAL;
}
dobj->dev_addr = sg_dma_address(dobj->sgt->sgl);
diff --git a/drivers/gpu/drm/drm_prime.c b/drivers/gpu/drm/drm_prime.c
index fe3436d1235d..e27bda3423ed 100644
--- a/drivers/gpu/drm/drm_prime.c
+++ b/drivers/gpu/drm/drm_prime.c
@@ -993,7 +993,7 @@ struct drm_gem_object *drm_gem_prime_import_dev(struct drm_device *dev,
get_dma_buf(dma_buf);
- sgt = dma_buf_map_attachment_unlocked(attach, DMA_BIDIRECTIONAL);
+ sgt = dma_buf_map_sgt_unlocked(attach, DMA_BIDIRECTIONAL);
if (IS_ERR(sgt)) {
ret = PTR_ERR(sgt);
goto fail_detach;
@@ -1011,7 +1011,7 @@ struct drm_gem_object *drm_gem_prime_import_dev(struct drm_device *dev,
return obj;
fail_unmap:
- dma_buf_unmap_attachment_unlocked(attach, sgt, DMA_BIDIRECTIONAL);
+ dma_buf_unmap_sgt_unlocked(attach, sgt, DMA_BIDIRECTIONAL);
fail_detach:
dma_buf_detach(dma_buf, attach);
dma_buf_put(dma_buf);
@@ -1109,7 +1109,7 @@ void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
attach = obj->import_attach;
if (sg)
- dma_buf_unmap_attachment_unlocked(attach, sg, DMA_BIDIRECTIONAL);
+ dma_buf_unmap_sgt_unlocked(attach, sg, DMA_BIDIRECTIONAL);
dma_buf = attach->dmabuf;
dma_buf_detach(attach->dmabuf, attach);
/* remove the reference */
diff --git a/drivers/gpu/drm/i915/gem/i915_gem_dmabuf.c b/drivers/gpu/drm/i915/gem/i915_gem_dmabuf.c
index b43d34c7d641..4ab54d2e616f 100644
--- a/drivers/gpu/drm/i915/gem/i915_gem_dmabuf.c
+++ b/drivers/gpu/drm/i915/gem/i915_gem_dmabuf.c
@@ -240,8 +240,8 @@ static int i915_gem_object_get_pages_dmabuf(struct drm_i915_gem_object *obj)
assert_object_held(obj);
- sgt = dma_buf_map_attachment(obj->base.import_attach,
- DMA_BIDIRECTIONAL);
+ sgt = dma_buf_map_sgt(obj->base.import_attach,
+ DMA_BIDIRECTIONAL);
if (IS_ERR(sgt))
return PTR_ERR(sgt);
@@ -268,8 +268,8 @@ static int i915_gem_object_get_pages_dmabuf(struct drm_i915_gem_object *obj)
static void i915_gem_object_put_pages_dmabuf(struct drm_i915_gem_object *obj,
struct sg_table *sgt)
{
- dma_buf_unmap_attachment(obj->base.import_attach, sgt,
- DMA_BIDIRECTIONAL);
+ dma_buf_unmap_sgt(obj->base.import_attach, sgt,
+ DMA_BIDIRECTIONAL);
}
static const struct drm_i915_gem_object_ops i915_gem_object_dmabuf_ops = {
diff --git a/drivers/gpu/drm/i915/gem/i915_gem_object.c b/drivers/gpu/drm/i915/gem/i915_gem_object.c
index 9e01f8b2079a..1e0aafc3643b 100644
--- a/drivers/gpu/drm/i915/gem/i915_gem_object.c
+++ b/drivers/gpu/drm/i915/gem/i915_gem_object.c
@@ -367,7 +367,7 @@ void __i915_gem_object_pages_fini(struct drm_i915_gem_object *obj)
atomic_set(&obj->mm.pages_pin_count, 0);
/*
- * dma_buf_unmap_attachment() requires reservation to be
+ * dma_buf_unmap_sgt() requires reservation to be
* locked. The imported GEM shouldn't share reservation lock
* and ttm_bo_cleanup_memtype_use() shouldn't be invoked for
* dma-buf, so it's safe to take the lock.
diff --git a/drivers/gpu/drm/i915/gem/selftests/i915_gem_dmabuf.c b/drivers/gpu/drm/i915/gem/selftests/i915_gem_dmabuf.c
index 2fda549dd82d..af9ced05f7a8 100644
--- a/drivers/gpu/drm/i915/gem/selftests/i915_gem_dmabuf.c
+++ b/drivers/gpu/drm/i915/gem/selftests/i915_gem_dmabuf.c
@@ -293,7 +293,7 @@ static int igt_dmabuf_import_same_driver(struct drm_i915_private *i915,
goto out_import;
}
- st = dma_buf_map_attachment_unlocked(import_attach, DMA_BIDIRECTIONAL);
+ st = dma_buf_map_sgt_unlocked(import_attach, DMA_BIDIRECTIONAL);
if (IS_ERR(st)) {
err = PTR_ERR(st);
goto out_detach;
@@ -306,7 +306,7 @@ static int igt_dmabuf_import_same_driver(struct drm_i915_private *i915,
timeout = -ETIME;
}
err = timeout > 0 ? 0 : timeout;
- dma_buf_unmap_attachment_unlocked(import_attach, st, DMA_BIDIRECTIONAL);
+ dma_buf_unmap_sgt_unlocked(import_attach, st, DMA_BIDIRECTIONAL);
out_detach:
dma_buf_detach(dmabuf, import_attach);
out_import:
diff --git a/drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c b/drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c
index 30cf1cdc1aa3..afae0a56da8c 100644
--- a/drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c
+++ b/drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c
@@ -120,7 +120,7 @@ struct drm_gem_object *omap_gem_prime_import(struct drm_device *dev,
get_dma_buf(dma_buf);
- sgt = dma_buf_map_attachment_unlocked(attach, DMA_TO_DEVICE);
+ sgt = dma_buf_map_sgt_unlocked(attach, DMA_TO_DEVICE);
if (IS_ERR(sgt)) {
ret = PTR_ERR(sgt);
goto fail_detach;
@@ -137,7 +137,7 @@ struct drm_gem_object *omap_gem_prime_import(struct drm_device *dev,
return obj;
fail_unmap:
- dma_buf_unmap_attachment_unlocked(attach, sgt, DMA_TO_DEVICE);
+ dma_buf_unmap_sgt_unlocked(attach, sgt, DMA_TO_DEVICE);
fail_detach:
dma_buf_detach(dma_buf, attach);
dma_buf_put(dma_buf);
diff --git a/drivers/gpu/drm/tegra/gem.c b/drivers/gpu/drm/tegra/gem.c
index f76af733ea79..e6aa6dae4ec8 100644
--- a/drivers/gpu/drm/tegra/gem.c
+++ b/drivers/gpu/drm/tegra/gem.c
@@ -86,7 +86,7 @@ static struct host1x_bo_mapping *tegra_bo_pin(struct device *dev, struct host1x_
goto free;
}
- map->sgt = dma_buf_map_attachment_unlocked(map->attach, direction);
+ map->sgt = dma_buf_map_sgt_unlocked(map->attach, direction);
if (IS_ERR(map->sgt)) {
dma_buf_detach(buf, map->attach);
err = PTR_ERR(map->sgt);
@@ -162,8 +162,8 @@ static struct host1x_bo_mapping *tegra_bo_pin(struct device *dev, struct host1x_
static void tegra_bo_unpin(struct host1x_bo_mapping *map)
{
if (map->attach) {
- dma_buf_unmap_attachment_unlocked(map->attach, map->sgt,
- map->direction);
+ dma_buf_unmap_sgt_unlocked(map->attach, map->sgt,
+ map->direction);
dma_buf_detach(map->attach->dmabuf, map->attach);
} else {
dma_unmap_sgtable(map->dev, map->sgt, map->direction, 0);
@@ -479,7 +479,7 @@ static struct tegra_bo *tegra_bo_import(struct drm_device *drm,
goto free;
}
- bo->sgt = dma_buf_map_attachment_unlocked(attach, DMA_TO_DEVICE);
+ bo->sgt = dma_buf_map_sgt_unlocked(attach, DMA_TO_DEVICE);
if (IS_ERR(bo->sgt)) {
err = PTR_ERR(bo->sgt);
goto detach;
@@ -499,7 +499,7 @@ static struct tegra_bo *tegra_bo_import(struct drm_device *drm,
detach:
if (!IS_ERR_OR_NULL(bo->sgt))
- dma_buf_unmap_attachment_unlocked(attach, bo->sgt, DMA_TO_DEVICE);
+ dma_buf_unmap_sgt_unlocked(attach, bo->sgt, DMA_TO_DEVICE);
dma_buf_detach(buf, attach);
dma_buf_put(buf);
@@ -520,8 +520,8 @@ void tegra_bo_free_object(struct drm_gem_object *gem)
tegra_bo_iommu_unmap(tegra, bo);
if (drm_gem_is_imported(gem)) {
- dma_buf_unmap_attachment_unlocked(gem->import_attach, bo->sgt,
- DMA_TO_DEVICE);
+ dma_buf_unmap_sgt_unlocked(gem->import_attach, bo->sgt,
+ DMA_TO_DEVICE);
dma_buf_detach(gem->import_attach->dmabuf, gem->import_attach);
}
}
diff --git a/drivers/gpu/drm/virtio/virtgpu_prime.c b/drivers/gpu/drm/virtio/virtgpu_prime.c
index ebf471044d06..a47783b47dcc 100644
--- a/drivers/gpu/drm/virtio/virtgpu_prime.c
+++ b/drivers/gpu/drm/virtio/virtgpu_prime.c
@@ -161,13 +161,13 @@ int virtgpu_dma_buf_import_sgt(struct virtio_gpu_mem_entry **ents,
if (ret <= 0)
return ret < 0 ? ret : -ETIMEDOUT;
- sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
+ sgt = dma_buf_map_sgt(attach, DMA_BIDIRECTIONAL);
if (IS_ERR(sgt))
return PTR_ERR(sgt);
*ents = kvmalloc_objs(struct virtio_gpu_mem_entry, sgt->nents);
if (!(*ents)) {
- dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
+ dma_buf_unmap_sgt(attach, sgt, DMA_BIDIRECTIONAL);
return -ENOMEM;
}
@@ -192,8 +192,8 @@ static void virtgpu_dma_buf_unmap(struct virtio_gpu_object *bo)
virtio_gpu_detach_object_fenced(bo);
if (bo->sgt)
- dma_buf_unmap_attachment(attach, bo->sgt,
- DMA_BIDIRECTIONAL);
+ dma_buf_unmap_sgt(attach, bo->sgt,
+ DMA_BIDIRECTIONAL);
bo->sgt = NULL;
}
diff --git a/drivers/gpu/drm/xe/xe_bo.c b/drivers/gpu/drm/xe/xe_bo.c
index 12e523c61c1f..14b71d951659 100644
--- a/drivers/gpu/drm/xe/xe_bo.c
+++ b/drivers/gpu/drm/xe/xe_bo.c
@@ -869,7 +869,7 @@ static int xe_bo_move_dmabuf(struct ttm_buffer_object *ttm_bo,
ttm_bo->sg) {
dma_resv_wait_timeout(ttm_bo->base.resv, DMA_RESV_USAGE_BOOKKEEP,
false, MAX_SCHEDULE_TIMEOUT);
- dma_buf_unmap_attachment(attach, ttm_bo->sg, DMA_BIDIRECTIONAL);
+ dma_buf_unmap_sgt(attach, ttm_bo->sg, DMA_BIDIRECTIONAL);
ttm_bo->sg = NULL;
}
@@ -877,11 +877,11 @@ static int xe_bo_move_dmabuf(struct ttm_buffer_object *ttm_bo,
goto out;
if (ttm_bo->sg) {
- dma_buf_unmap_attachment(attach, ttm_bo->sg, DMA_BIDIRECTIONAL);
+ dma_buf_unmap_sgt(attach, ttm_bo->sg, DMA_BIDIRECTIONAL);
ttm_bo->sg = NULL;
}
- sg = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
+ sg = dma_buf_map_sgt(attach, DMA_BIDIRECTIONAL);
if (IS_ERR(sg))
return PTR_ERR(sg);
@@ -1750,9 +1750,9 @@ int xe_bo_dma_unmap_pinned(struct xe_bo *bo)
struct xe_ttm_tt *xe_tt = container_of(tt, typeof(*xe_tt), ttm);
if (ttm_bo->type == ttm_bo_type_sg && ttm_bo->sg) {
- dma_buf_unmap_attachment(ttm_bo->base.import_attach,
- ttm_bo->sg,
- DMA_BIDIRECTIONAL);
+ dma_buf_unmap_sgt(ttm_bo->base.import_attach,
+ ttm_bo->sg,
+ DMA_BIDIRECTIONAL);
ttm_bo->sg = NULL;
xe_tt->sg = NULL;
} else if (xe_tt->sg) {
@@ -1867,8 +1867,8 @@ static void xe_ttm_bo_delete_mem_notify(struct ttm_buffer_object *ttm_bo)
struct xe_ttm_tt *xe_tt = container_of(ttm_bo->ttm,
struct xe_ttm_tt, ttm);
- dma_buf_unmap_attachment(ttm_bo->base.import_attach, ttm_bo->sg,
- DMA_BIDIRECTIONAL);
+ dma_buf_unmap_sgt(ttm_bo->base.import_attach, ttm_bo->sg,
+ DMA_BIDIRECTIONAL);
ttm_bo->sg = NULL;
xe_tt->sg = NULL;
}
diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c
index 4294101810fd..4a1063e4891f 100644
--- a/drivers/iio/industrialio-buffer.c
+++ b/drivers/iio/industrialio-buffer.c
@@ -1593,7 +1593,7 @@ static void iio_buffer_dmabuf_release(struct kref *ref)
struct iio_buffer *buffer = priv->buffer;
struct dma_buf *dmabuf = attach->dmabuf;
- dma_buf_unmap_attachment_unlocked(attach, priv->sgt, priv->dir);
+ dma_buf_unmap_sgt_unlocked(attach, priv->sgt, priv->dir);
buffer->access->detach_dmabuf(buffer, priv->block);
@@ -1733,7 +1733,7 @@ static int iio_buffer_attach_dmabuf(struct iio_dev_buffer_pair *ib,
priv->dir = buffer->direction == IIO_BUFFER_DIRECTION_IN
? DMA_FROM_DEVICE : DMA_TO_DEVICE;
- priv->sgt = dma_buf_map_attachment(attach, priv->dir);
+ priv->sgt = dma_buf_map_sgt(attach, priv->dir);
if (IS_ERR(priv->sgt)) {
err = PTR_ERR(priv->sgt);
dev_err(&indio_dev->dev, "Unable to map attachment: %d\n", err);
@@ -1781,7 +1781,7 @@ static int iio_buffer_attach_dmabuf(struct iio_dev_buffer_pair *ib,
return 0;
err_dmabuf_unmap_attachment:
- dma_buf_unmap_attachment(attach, priv->sgt, priv->dir);
+ dma_buf_unmap_sgt(attach, priv->sgt, priv->dir);
err_resv_unlock:
dma_resv_unlock(dmabuf->resv);
err_dmabuf_detach:
diff --git a/drivers/infiniband/core/umem_dmabuf.c b/drivers/infiniband/core/umem_dmabuf.c
index 39b5564a4c35..f233fdac9a17 100644
--- a/drivers/infiniband/core/umem_dmabuf.c
+++ b/drivers/infiniband/core/umem_dmabuf.c
@@ -29,8 +29,8 @@ int ib_umem_dmabuf_map_pages(struct ib_umem_dmabuf *umem_dmabuf)
if (umem_dmabuf->sgt)
goto wait_fence;
- sgt = dma_buf_map_attachment(umem_dmabuf->attach,
- DMA_BIDIRECTIONAL);
+ sgt = dma_buf_map_sgt(umem_dmabuf->attach,
+ DMA_BIDIRECTIONAL);
if (IS_ERR(sgt))
return PTR_ERR(sgt);
@@ -106,8 +106,8 @@ void ib_umem_dmabuf_unmap_pages(struct ib_umem_dmabuf *umem_dmabuf)
umem_dmabuf->last_sg_trim = 0;
}
- dma_buf_unmap_attachment(umem_dmabuf->attach, umem_dmabuf->sgt,
- DMA_BIDIRECTIONAL);
+ dma_buf_unmap_sgt(umem_dmabuf->attach, umem_dmabuf->sgt,
+ DMA_BIDIRECTIONAL);
umem_dmabuf->sgt = NULL;
}
diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/media/common/videobuf2/videobuf2-core.c
index b0a6084f1757..61a614074222 100644
--- a/drivers/media/common/videobuf2/videobuf2-core.c
+++ b/drivers/media/common/videobuf2/videobuf2-core.c
@@ -1464,7 +1464,7 @@ static int __prepare_dmabuf(struct vb2_buffer *vb)
vb->planes[plane].mem_priv = mem_priv;
/*
- * This pins the buffer(s) with dma_buf_map_attachment()). It's done
+ * This pins the buffer(s) with dma_buf_map_sgt()). It's done
* here instead just before the DMA, while queueing the buffer(s) so
* userspace knows sooner rather than later if the dma-buf map fails.
*/
diff --git a/drivers/media/common/videobuf2/videobuf2-dma-contig.c b/drivers/media/common/videobuf2/videobuf2-dma-contig.c
index 9ce6284cd5f2..284482335c1a 100644
--- a/drivers/media/common/videobuf2/videobuf2-dma-contig.c
+++ b/drivers/media/common/videobuf2/videobuf2-dma-contig.c
@@ -705,7 +705,7 @@ static int vb2_dc_map_dmabuf(void *mem_priv)
}
/* get the associated scatterlist for this buffer */
- sgt = dma_buf_map_attachment_unlocked(buf->db_attach, buf->dma_dir);
+ sgt = dma_buf_map_sgt_unlocked(buf->db_attach, buf->dma_dir);
if (IS_ERR(sgt)) {
pr_err("Error getting dmabuf scatterlist\n");
return -EINVAL;
@@ -716,8 +716,8 @@ static int vb2_dc_map_dmabuf(void *mem_priv)
if (contig_size < buf->size) {
pr_err("contiguous chunk is too small %lu/%lu\n",
contig_size, buf->size);
- dma_buf_unmap_attachment_unlocked(buf->db_attach, sgt,
- buf->dma_dir);
+ dma_buf_unmap_sgt_unlocked(buf->db_attach, sgt,
+ buf->dma_dir);
return -EFAULT;
}
@@ -748,7 +748,7 @@ static void vb2_dc_unmap_dmabuf(void *mem_priv)
dma_buf_vunmap_unlocked(buf->db_attach->dmabuf, &map);
buf->vaddr = NULL;
}
- dma_buf_unmap_attachment_unlocked(buf->db_attach, sgt, buf->dma_dir);
+ dma_buf_unmap_sgt_unlocked(buf->db_attach, sgt, buf->dma_dir);
buf->dma_addr = 0;
buf->dma_sgt = NULL;
diff --git a/drivers/media/common/videobuf2/videobuf2-dma-sg.c b/drivers/media/common/videobuf2/videobuf2-dma-sg.c
index b1d0695cda26..54b07e3840ac 100644
--- a/drivers/media/common/videobuf2/videobuf2-dma-sg.c
+++ b/drivers/media/common/videobuf2/videobuf2-dma-sg.c
@@ -565,7 +565,7 @@ static int vb2_dma_sg_map_dmabuf(void *mem_priv)
}
/* get the associated scatterlist for this buffer */
- sgt = dma_buf_map_attachment_unlocked(buf->db_attach, buf->dma_dir);
+ sgt = dma_buf_map_sgt_unlocked(buf->db_attach, buf->dma_dir);
if (IS_ERR(sgt)) {
pr_err("Error getting dmabuf scatterlist\n");
return -EINVAL;
@@ -597,7 +597,7 @@ static void vb2_dma_sg_unmap_dmabuf(void *mem_priv)
dma_buf_vunmap_unlocked(buf->db_attach->dmabuf, &map);
buf->vaddr = NULL;
}
- dma_buf_unmap_attachment_unlocked(buf->db_attach, sgt, buf->dma_dir);
+ dma_buf_unmap_sgt_unlocked(buf->db_attach, sgt, buf->dma_dir);
buf->dma_sgt = NULL;
}
diff --git a/drivers/media/platform/nvidia/tegra-vde/dmabuf-cache.c b/drivers/media/platform/nvidia/tegra-vde/dmabuf-cache.c
index 7752064be783..d9bac7cd855a 100644
--- a/drivers/media/platform/nvidia/tegra-vde/dmabuf-cache.c
+++ b/drivers/media/platform/nvidia/tegra-vde/dmabuf-cache.c
@@ -38,7 +38,7 @@ static void tegra_vde_release_entry(struct tegra_vde_cache_entry *entry)
if (entry->vde->domain)
tegra_vde_iommu_unmap(entry->vde, entry->iova);
- dma_buf_unmap_attachment_unlocked(entry->a, entry->sgt, entry->dma_dir);
+ dma_buf_unmap_sgt_unlocked(entry->a, entry->sgt, entry->dma_dir);
dma_buf_detach(dmabuf, entry->a);
dma_buf_put(dmabuf);
@@ -102,7 +102,7 @@ int tegra_vde_dmabuf_cache_map(struct tegra_vde *vde,
goto err_unlock;
}
- sgt = dma_buf_map_attachment_unlocked(attachment, dma_dir);
+ sgt = dma_buf_map_sgt_unlocked(attachment, dma_dir);
if (IS_ERR(sgt)) {
dev_err(dev, "Failed to get dmabufs sg_table\n");
err = PTR_ERR(sgt);
@@ -152,7 +152,7 @@ int tegra_vde_dmabuf_cache_map(struct tegra_vde *vde,
err_free:
kfree(entry);
err_unmap:
- dma_buf_unmap_attachment_unlocked(attachment, sgt, dma_dir);
+ dma_buf_unmap_sgt_unlocked(attachment, sgt, dma_dir);
err_detach:
dma_buf_detach(dmabuf, attachment);
err_unlock:
diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c
index cdc2627bdaf2..1a3abc33734c 100644
--- a/drivers/misc/fastrpc.c
+++ b/drivers/misc/fastrpc.c
@@ -401,8 +401,8 @@ static void fastrpc_free_map(struct kref *ref)
return;
}
}
- dma_buf_unmap_attachment_unlocked(map->attach, map->table,
- DMA_BIDIRECTIONAL);
+ dma_buf_unmap_sgt_unlocked(map->attach, map->table,
+ DMA_BIDIRECTIONAL);
dma_buf_detach(map->buf, map->attach);
dma_buf_put(map->buf);
}
@@ -911,7 +911,7 @@ static int fastrpc_map_attach(struct fastrpc_user *fl, int fd,
goto attach_err;
}
- table = dma_buf_map_attachment_unlocked(map->attach, DMA_BIDIRECTIONAL);
+ table = dma_buf_map_sgt_unlocked(map->attach, DMA_BIDIRECTIONAL);
if (IS_ERR(table)) {
err = PTR_ERR(table);
goto map_err;
diff --git a/drivers/nvme/host/pci-dmabuf.c b/drivers/nvme/host/pci-dmabuf.c
index 161e4ffa6919..fe741dc0d54a 100644
--- a/drivers/nvme/host/pci-dmabuf.c
+++ b/drivers/nvme/host/pci-dmabuf.c
@@ -41,7 +41,7 @@ static struct dma_buf_io_map *nvme_dma_buf_io_map(struct dma_buf_io_ctx *ctx)
return ERR_PTR(-ENOMEM);
map->nr_entries = nr_entries;
- sgt = dma_buf_map_attachment(attach, ctx->dir);
+ sgt = dma_buf_map_sgt(attach, ctx->dir);
if (IS_ERR(sgt)) {
ret = PTR_ERR(sgt);
sgt = NULL;
@@ -71,7 +71,7 @@ static struct dma_buf_io_map *nvme_dma_buf_io_map(struct dma_buf_io_ctx *ctx)
return &map->base;
err:
if (sgt)
- dma_buf_unmap_attachment(attach, sgt, ctx->dir);
+ dma_buf_unmap_sgt(attach, sgt, ctx->dir);
kvfree(map);
return ERR_PTR(ret);
}
@@ -84,7 +84,7 @@ static void nvme_dma_buf_io_unmap(struct dma_buf_io_ctx *ctx,
dma_resv_assert_held(ctx->dmabuf->resv);
- dma_buf_unmap_attachment(nvme_ctx->attach, map->sgt, ctx->dir);
+ dma_buf_unmap_sgt(nvme_ctx->attach, map->sgt, ctx->dir);
kvfree(map);
}
diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c
index c4fba4d11570..fb9eaebdec74 100644
--- a/drivers/usb/gadget/function/f_fs.c
+++ b/drivers/usb/gadget/function/f_fs.c
@@ -1377,7 +1377,7 @@ static void ffs_dmabuf_release(struct kref *ref)
struct dma_buf *dmabuf = attach->dmabuf;
pr_vdebug("FFS DMABUF release\n");
- dma_buf_unmap_attachment_unlocked(attach, priv->sgt, priv->dir);
+ dma_buf_unmap_sgt_unlocked(attach, priv->sgt, priv->dir);
dma_buf_detach(attach->dmabuf, attach);
dma_buf_put(dmabuf);
@@ -1577,7 +1577,7 @@ static int ffs_dmabuf_attach(struct file *file, int fd)
if (err)
goto err_free_priv;
- sg_table = dma_buf_map_attachment(attach, dir);
+ sg_table = dma_buf_map_sgt(attach, dir);
dma_resv_unlock(dmabuf->resv);
if (IS_ERR(sg_table)) {
diff --git a/drivers/xen/gntdev-dmabuf.c b/drivers/xen/gntdev-dmabuf.c
index 80fc91dc88e8..037059eacc14 100644
--- a/drivers/xen/gntdev-dmabuf.c
+++ b/drivers/xen/gntdev-dmabuf.c
@@ -587,7 +587,7 @@ dmabuf_imp_to_refs(struct gntdev_dmabuf_priv *priv, struct device *dev,
gntdev_dmabuf->u.imp.attach = attach;
- sgt = dma_buf_map_attachment_unlocked(attach, DMA_BIDIRECTIONAL);
+ sgt = dma_buf_map_sgt_unlocked(attach, DMA_BIDIRECTIONAL);
if (IS_ERR(sgt)) {
ret = ERR_CAST(sgt);
goto fail_detach;
@@ -650,7 +650,7 @@ dmabuf_imp_to_refs(struct gntdev_dmabuf_priv *priv, struct device *dev,
fail_end_access:
dmabuf_imp_end_foreign_access(gntdev_dmabuf->u.imp.refs, count);
fail_unmap:
- dma_buf_unmap_attachment_unlocked(attach, sgt, DMA_BIDIRECTIONAL);
+ dma_buf_unmap_sgt_unlocked(attach, sgt, DMA_BIDIRECTIONAL);
fail_detach:
dma_buf_detach(dma_buf, attach);
fail_free_obj:
@@ -700,8 +700,8 @@ static int dmabuf_imp_release(struct gntdev_dmabuf_priv *priv, u32 fd)
attach = gntdev_dmabuf->u.imp.attach;
if (gntdev_dmabuf->u.imp.sgt)
- dma_buf_unmap_attachment_unlocked(attach, gntdev_dmabuf->u.imp.sgt,
- DMA_BIDIRECTIONAL);
+ dma_buf_unmap_sgt_unlocked(attach, gntdev_dmabuf->u.imp.sgt,
+ DMA_BIDIRECTIONAL);
dma_buf = attach->dmabuf;
dma_buf_detach(attach->dmabuf, attach);
dma_buf_put(dma_buf);
diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h
index d15b2b31d3c9..b138b77212c9 100644
--- a/include/linux/dma-buf.h
+++ b/include/linux/dma-buf.h
@@ -116,7 +116,7 @@ struct dma_buf_ops {
/**
* @map_dma_buf:
*
- * This is called by dma_buf_map_attachment() and is used to map a
+ * This is called by dma_buf_map_sgt() and is used to map a
* shared &dma_buf into device address space, and it is mandatory. It
* can only be called if @attach has been called successfully.
*
@@ -173,7 +173,7 @@ struct dma_buf_ops {
/**
* @unmap_dma_buf:
*
- * This is called by dma_buf_unmap_attachment() and should unmap and
+ * This is called by dma_buf_unmap_sgt() and should unmap and
* release the &sg_table allocated in @map_dma_buf, and it is mandatory.
* For static dma_buf handling this might also unpin the backing
* storage if this is the last mapping of the DMA buffer.
@@ -470,7 +470,7 @@ struct dma_buf_attach_ops {
* @peer2peer: true if the importer can handle peer resources without pages.
* @priv: exporter specific attachment data.
* @importer_ops: importer operations for this attachment, if provided
- * dma_buf_map/unmap_attachment() must be called with the dma_resv lock held.
+ * dma_buf_map/unmap_sgt() must be called with the dma_resv lock held.
* @importer_priv: importer specific attachment data.
*
* This structure holds the attachment information between the dma_buf buffer
@@ -479,8 +479,8 @@ struct dma_buf_attach_ops {
*
* An attachment is created by calling dma_buf_attach(), and released again by
* calling dma_buf_detach(). The DMA mapping itself needed to initiate a
- * transfer is created by dma_buf_map_attachment() and freed again by calling
- * dma_buf_unmap_attachment().
+ * transfer is created by dma_buf_map_sgt() and freed again by calling
+ * dma_buf_unmap_sgt().
*/
struct dma_buf_attachment {
struct dma_buf *dmabuf;
@@ -571,10 +571,11 @@ void dma_buf_fd_install(struct dma_buf *dmabuf, int fd);
struct dma_buf *dma_buf_get(int fd);
void dma_buf_put(struct dma_buf *dmabuf);
-struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *,
- enum dma_data_direction);
-void dma_buf_unmap_attachment(struct dma_buf_attachment *, struct sg_table *,
- enum dma_data_direction);
+struct sg_table *dma_buf_map_sgt(struct dma_buf_attachment *attach,
+ enum dma_data_direction direction);
+void dma_buf_unmap_sgt(struct dma_buf_attachment *attach,
+ struct sg_table *sg_table,
+ enum dma_data_direction direction);
void dma_buf_invalidate_mappings(struct dma_buf *dma_buf);
bool dma_buf_attach_revocable(struct dma_buf_attachment *attach);
int dma_buf_begin_cpu_access(struct dma_buf *dma_buf,
@@ -582,11 +583,11 @@ int dma_buf_begin_cpu_access(struct dma_buf *dma_buf,
int dma_buf_end_cpu_access(struct dma_buf *dma_buf,
enum dma_data_direction dir);
struct sg_table *
-dma_buf_map_attachment_unlocked(struct dma_buf_attachment *attach,
+dma_buf_map_sgt_unlocked(struct dma_buf_attachment *attach,
+ enum dma_data_direction direction);
+void dma_buf_unmap_sgt_unlocked(struct dma_buf_attachment *attach,
+ struct sg_table *sg_table,
enum dma_data_direction direction);
-void dma_buf_unmap_attachment_unlocked(struct dma_buf_attachment *attach,
- struct sg_table *sg_table,
- enum dma_data_direction direction);
int dma_buf_mmap(struct dma_buf *, struct vm_area_struct *,
unsigned long);
diff --git a/io_uring/zcrx.c b/io_uring/zcrx.c
index 21e042b3ccb9..389f34513237 100644
--- a/io_uring/zcrx.c
+++ b/io_uring/zcrx.c
@@ -132,8 +132,8 @@ static void io_unmap_dmabuf(struct io_zcrx_mem *mem)
if (!IS_ENABLED(CONFIG_DMA_SHARED_BUFFER))
return;
if (mem->sgt)
- dma_buf_unmap_attachment_unlocked(mem->attach, mem->sgt,
- DMA_FROM_DEVICE);
+ dma_buf_unmap_sgt_unlocked(mem->attach, mem->sgt,
+ DMA_FROM_DEVICE);
if (mem->attach)
dma_buf_detach(mem->dmabuf, mem->attach);
mem->sgt = NULL;
@@ -182,7 +182,7 @@ static int io_import_dmabuf(struct io_zcrx_ifq *ifq,
goto err;
}
- mem->sgt = dma_buf_map_attachment_unlocked(mem->attach, DMA_FROM_DEVICE);
+ mem->sgt = dma_buf_map_sgt_unlocked(mem->attach, DMA_FROM_DEVICE);
if (IS_ERR(mem->sgt)) {
ret = PTR_ERR(mem->sgt);
mem->sgt = NULL;
diff --git a/net/core/devmem.c b/net/core/devmem.c
index b2bd4373a2ea..a638aa3e806b 100644
--- a/net/core/devmem.c
+++ b/net/core/devmem.c
@@ -75,8 +75,8 @@ void __net_devmem_dmabuf_binding_free(struct work_struct *wq)
kvfree(binding->area.niovs);
dma_resv_lock(binding->dmabuf->resv, NULL);
- dma_buf_unmap_attachment(binding->attachment, binding->sgt,
- binding->direction);
+ dma_buf_unmap_sgt(binding->attachment, binding->sgt,
+ binding->direction);
dma_buf_unpin(binding->attachment);
dma_resv_unlock(binding->dmabuf->resv);
dma_buf_detach(binding->dmabuf, binding->attachment);
@@ -250,7 +250,7 @@ net_devmem_bind_dmabuf(struct net_device *dev, void *vdev,
goto err_unlock;
}
- binding->sgt = dma_buf_map_attachment(binding->attachment, direction);
+ binding->sgt = dma_buf_map_sgt(binding->attachment, direction);
if (IS_ERR(binding->sgt)) {
err = PTR_ERR(binding->sgt);
NL_SET_ERR_MSG(extack, "Failed to map dmabuf attachment");
@@ -356,7 +356,7 @@ net_devmem_bind_dmabuf(struct net_device *dev, void *vdev,
kvfree(binding->tx_vec);
err_unmap:
dma_resv_lock(dmabuf->resv, NULL);
- dma_buf_unmap_attachment(binding->attachment, binding->sgt, direction);
+ dma_buf_unmap_sgt(binding->attachment, binding->sgt, direction);
err_unpin:
dma_buf_unpin(binding->attachment);
err_unlock:
--
2.54.0
^ permalink raw reply related [flat|nested] 21+ messages in thread* [PATCH RFC 02/16] dma-shared-buf: Add struct dma_shared_buf
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 ` Alistair Popple
2026-12-01 7:06 ` [PATCH RFC 03/16] dma-shared-buf: Add dma_shared_buf_iommu_map() Alistair Popple
` (14 subsequent siblings)
16 siblings, 0 replies; 21+ messages in thread
From: Alistair Popple @ 2026-12-01 7:06 UTC (permalink / raw)
To: Karol Wachowski, Sumit Semwal, Christian König,
Philipp Stanner, David Airlie, Simona Vetter, Rodrigo Vivi,
Matthew Brost, Thomas Hellström, Jason Gunthorpe,
Leon Romanovsky, Kees Cook, Gustavo A. R. Silva, Alex Williamson,
Kevin Tian, Joerg Roedel (AMD), Will Deacon, Robin Murphy
Cc: dri-devel, linux-media, linaro-mm-sig, intel-xe, linux-rdma,
linux-kernel, linux-hardening, kvm, iommu, Alexey Kardashevskiy,
Mina Almasry, Baolu Lu, chaitanyak, Dongwon Kim, Felix Kuehling,
jhubbard, Karl Mehltretter, Vivek Kasireddy, Xu Yilun, zhipingz,
Alistair Popple
Currently the sg_table is used by dma-buf exporters to describe the
range of physical addresses to map to the DMA API. This is then passed
directly back to importers.
This is problematic for a number of reasons. It exposes exporter side
information to importers and doesn't have the capability to describe
memory that isn't backed by struct page which is what MMIO such as PCI
BAR needs.
Add the dma-shared-buf as a library to handle this. A new type to
replace the exporter's sg_table, struct dma_shared_buf, is added
to describe a range of physical regions for mapping by the library.
dma_shared_buf_map() can then be used to map this for a particular
device.
The mapping function handles either direct P2P mappings using the PCI
peer-to-peer core if a p2pdma provider is listed, or system memory
otherwise. This allows dma-buf exporters to use common logic to
create mappings instead of open-coding it which is hard to get right,
especially for P2P cases with complex routing and IOMMU requirements.
This is kept in a file of its own as none of it depends on a DMA-buf and
it could be a library elsewhere.
Signed-off-by: Alistair Popple <apopple@nvidia.com>
---
This is supposed to be used as a library for dma-buf exporters/importers
so could be moved. I don't love the name so happy for suggestions or
alternatives.
---
MAINTAINERS | 1 +
drivers/dma-buf/Makefile | 3 +-
drivers/dma-buf/dma-shared-buf.c | 138 +++++++++++++++++++++++++++++++++++++++
include/linux/dma-shared-buf.h | 77 ++++++++++++++++++++++
4 files changed, 218 insertions(+), 1 deletion(-)
diff --git a/MAINTAINERS b/MAINTAINERS
index 84cb98ef4c49..679986e43aca 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -7879,6 +7879,7 @@ F: include/linux/*fence.h
F: include/linux/dma-buf.h
F: include/linux/dma-buf/
F: include/linux/dma-resv.h
+F: include/linux/dma-shared-buf.h
F: rust/helpers/dma-resv.c
F: rust/helpers/dma_fence.c
F: rust/kernel/dma_buf/
diff --git a/drivers/dma-buf/Makefile b/drivers/dma-buf/Makefile
index 523731b0f83e..ea24bd1bd75c 100644
--- a/drivers/dma-buf/Makefile
+++ b/drivers/dma-buf/Makefile
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-y := dma-buf.o dma-fence.o dma-fence-array.o dma-fence-chain.o \
- dma-fence-unwrap.o dma-resv.o dma-buf-mapping.o dma-buf-io.o
+ dma-fence-unwrap.o dma-resv.o dma-buf-mapping.o dma-buf-io.o \
+ dma-shared-buf.o
obj-$(CONFIG_DMABUF_HEAPS) += dma-heap.o
obj-$(CONFIG_DMABUF_HEAPS) += heaps/
obj-$(CONFIG_SYNC_FILE) += sync_file.o
diff --git a/drivers/dma-buf/dma-shared-buf.c b/drivers/dma-buf/dma-shared-buf.c
new file mode 100644
index 000000000000..75ab08828ee8
--- /dev/null
+++ b/drivers/dma-buf/dma-shared-buf.c
@@ -0,0 +1,138 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * struct dma_shared_buf. Memory described by its owner as a list of ranges, to
+ * be mapped for whichever device needs to access it.
+ */
+#include <linux/dma-shared-buf.h>
+#include <linux/overflow.h>
+#include <linux/pci-p2pdma.h>
+#include <linux/slab.h>
+
+/**
+ * dma_shared_buf_map - Map a dma_shared_buf for a device
+ * @dev: The device to map for
+ * @shared_buf: The memory to map
+ * @dir: Direction of the DMA transfer
+ *
+ * A dma_shared_buf with a p2pdma provider goes where the P2P core directs it.
+ * Bus addresses need no mapping at all, otherwise it is mapped through the host
+ * bridge like system memory with DMA_ATTR_MMIO. Either way the ranges are
+ * linked into a single IOVA when @dev has one and mapped one by one otherwise,
+ * so the result holds one DMA range or one per range of @shared_buf.
+ *
+ * Nothing of @shared_buf is referenced once this returns.
+ *
+ * Returns: the DMA addresses, to be released with dma_shared_buf_unmap(),
+ * or a negative error value wrapped into a pointer.
+ */
+struct dma_addr_list *
+dma_shared_buf_map(struct device *dev, const struct dma_shared_buf *shared_buf,
+ enum dma_data_direction dir)
+{
+ struct dma_addr_list *addrs;
+ size_t mapped = 0;
+ size_t i;
+ int ret;
+
+ if (WARN_ON(!shared_buf->nr_ranges || shared_buf->nr_ranges > UINT_MAX))
+ return ERR_PTR(-EINVAL);
+
+ addrs = kvzalloc(struct_size(addrs, ranges, shared_buf->nr_ranges),
+ GFP_KERNEL);
+ if (!addrs)
+ return ERR_PTR(-ENOMEM);
+ addrs->size = shared_buf->size;
+ addrs->nr_ranges = shared_buf->nr_ranges;
+ addrs->attrs = shared_buf->attrs;
+
+ if (shared_buf->provider) {
+ addrs->attrs |= DMA_ATTR_MMIO;
+ switch (pci_p2pdma_map_type(shared_buf->provider, dev)) {
+ case PCI_P2PDMA_MAP_BUS_ADDR:
+ for (i = 0; i < shared_buf->nr_ranges; i++) {
+ const struct phys_vec *vec =
+ &shared_buf->ranges[i];
+
+ addrs->ranges[i].addr =
+ pci_p2pdma_bus_addr_map(shared_buf->provider,
+ vec->paddr);
+ addrs->ranges[i].len = vec->len;
+ }
+ addrs->bus_addr = true;
+ return addrs;
+ case PCI_P2PDMA_MAP_THRU_HOST_BRIDGE:
+ break;
+ default:
+ ret = -EINVAL;
+ goto err_free;
+ }
+ }
+
+ if (dma_iova_try_alloc(dev, &addrs->state, 0, shared_buf->size)) {
+ for (i = 0; i < shared_buf->nr_ranges; i++) {
+ ret = dma_iova_link(dev, &addrs->state,
+ shared_buf->ranges[i].paddr, mapped,
+ shared_buf->ranges[i].len, dir,
+ addrs->attrs);
+ if (ret)
+ goto err_iova;
+ mapped += shared_buf->ranges[i].len;
+ }
+ WARN_ON_ONCE(mapped != shared_buf->size);
+ ret = dma_iova_sync(dev, &addrs->state, 0, mapped);
+ if (ret)
+ goto err_iova;
+ addrs->ranges[0].addr = addrs->state.addr;
+ addrs->ranges[0].len = mapped;
+ addrs->nr_ranges = 1;
+ return addrs;
+ }
+
+ for (i = 0; i < shared_buf->nr_ranges; i++) {
+ dma_addr_t addr = dma_map_phys(dev, shared_buf->ranges[i].paddr,
+ shared_buf->ranges[i].len, dir,
+ addrs->attrs);
+
+ ret = dma_mapping_error(dev, addr);
+ if (ret)
+ goto err_unmap;
+ addrs->ranges[i].addr = addr;
+ addrs->ranges[i].len = shared_buf->ranges[i].len;
+ }
+ return addrs;
+
+err_unmap:
+ while (i--)
+ dma_unmap_phys(dev, addrs->ranges[i].addr, addrs->ranges[i].len,
+ dir, addrs->attrs);
+ goto err_free;
+err_iova:
+ dma_iova_destroy(dev, &addrs->state, mapped, dir, addrs->attrs);
+err_free:
+ kvfree(addrs);
+ return ERR_PTR(ret);
+}
+
+/**
+ * dma_shared_buf_unmap - Unmap and free the result of dma_shared_buf_map()
+ * @dev: The device the dma_shared_buf was mapped for
+ * @addrs: The DMA addresses it was mapped to
+ * @dir: The direction it was mapped with
+ */
+void dma_shared_buf_unmap(struct device *dev, struct dma_addr_list *addrs,
+ enum dma_data_direction dir)
+{
+ unsigned int i;
+
+ if (addrs->bus_addr) {
+ ; /* Bus addresses: nothing was mapped */
+ } else if (dma_use_iova(&addrs->state)) {
+ dma_iova_destroy(dev, &addrs->state, addrs->size, dir,
+ addrs->attrs);
+ } else {
+ for (i = 0; i < addrs->nr_ranges; i++)
+ dma_unmap_phys(dev, addrs->ranges[i].addr,
+ addrs->ranges[i].len, dir, addrs->attrs);
+ }
+ kvfree(addrs);
+}
diff --git a/include/linux/dma-shared-buf.h b/include/linux/dma-shared-buf.h
new file mode 100644
index 000000000000..53f506e0cc85
--- /dev/null
+++ b/include/linux/dma-shared-buf.h
@@ -0,0 +1,77 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * struct dma_shared_buf. Memory described by its owner as a list of ranges, to
+ * be mapped for whichever device needs to access it.
+ */
+#ifndef _LINUX_DMA_SHARED_BUF_H
+#define _LINUX_DMA_SHARED_BUF_H
+
+#include <linux/dma-mapping.h>
+#include <linux/types.h>
+
+struct p2pdma_provider;
+
+/**
+ * struct dma_shared_buf - Memory to be DMA mapped, as described by its owner
+ * @provider: The P2P provider of all of @ranges when they are MMIO, NULL for
+ * system memory
+ * @attrs: DMA attributes to map with, eg. DMA_ATTR_SKIP_CPU_SYNC when the
+ * owner looks after CPU cache synchronisation itself. DMA_ATTR_MMIO
+ * is added when there is a @provider.
+ * @size: Sum of the range lengths
+ * @nr_ranges: Number of entries in @ranges
+ * @ranges: The ranges, in order
+ */
+struct dma_shared_buf {
+ struct p2pdma_provider *provider;
+ unsigned long attrs;
+ size_t size;
+ size_t nr_ranges;
+ struct phys_vec ranges[] __counted_by(nr_ranges);
+};
+
+/**
+ * struct dma_addr_range - A run of addresses in a device's DMA address space
+ * @addr: Start of the run
+ * @len: Length of the run in bytes
+ */
+struct dma_addr_range {
+ dma_addr_t addr;
+ size_t len;
+};
+
+/**
+ * struct dma_addr_list - Memory as a list of ranges of a device's DMA address
+ * space
+ * @size: Sum of the range lengths
+ * @nr_ranges: Number of entries in @ranges
+ * @priv: For the owner of the list, which is whoever called
+ * dma_shared_buf_map() or built the list itself
+ * @state: IOVA state when the list was linked into a single IOVA
+ * @attrs: The DMA attributes the list was mapped with
+ * @bus_addr: True when @ranges are PCI bus addresses, which needed no mapping
+ * @ranges: The DMA addresses covering the memory in order. For a mapped
+ * dma_shared_buf that is one entry per range of it, or a single
+ * entry when it was linked into one IOVA.
+ *
+ * Returned by dma_shared_buf_map() and released by dma_shared_buf_unmap(),
+ * which is all @state, @attrs and @bus_addr are for. Anyone the owner hands
+ * the list to reads @size, @nr_ranges and @ranges and nothing else.
+ */
+struct dma_addr_list {
+ size_t size;
+ unsigned int nr_ranges;
+ void *priv;
+ struct dma_iova_state state;
+ unsigned long attrs;
+ bool bus_addr;
+ struct dma_addr_range ranges[] __counted_by(nr_ranges);
+};
+
+struct dma_addr_list *
+dma_shared_buf_map(struct device *dev, const struct dma_shared_buf *shared_buf,
+ enum dma_data_direction dir);
+void dma_shared_buf_unmap(struct device *dev, struct dma_addr_list *addrs,
+ enum dma_data_direction dir);
+
+#endif /* _LINUX_DMA_SHARED_BUF_H */
--
2.54.0
^ permalink raw reply related [flat|nested] 21+ messages in thread* [PATCH RFC 03/16] dma-shared-buf: Add dma_shared_buf_iommu_map()
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 ` Alistair Popple
2026-12-01 7:06 ` [PATCH RFC 04/16] dma-shared-buf: Add dma_addr_list_to_sgt() Alistair Popple
` (13 subsequent siblings)
16 siblings, 0 replies; 21+ messages in thread
From: Alistair Popple @ 2026-12-01 7:06 UTC (permalink / raw)
To: Karol Wachowski, Sumit Semwal, Christian König,
Philipp Stanner, David Airlie, Simona Vetter, Rodrigo Vivi,
Matthew Brost, Thomas Hellström, Jason Gunthorpe,
Leon Romanovsky, Kees Cook, Gustavo A. R. Silva, Alex Williamson,
Kevin Tian, Joerg Roedel (AMD), Will Deacon, Robin Murphy
Cc: dri-devel, linux-media, linaro-mm-sig, intel-xe, linux-rdma,
linux-kernel, linux-hardening, kvm, iommu, Alexey Kardashevskiy,
Mina Almasry, Baolu Lu, chaitanyak, Dongwon Kim, Felix Kuehling,
jhubbard, Karl Mehltretter, Vivek Kasireddy, Xu Yilun, zhipingz,
Alistair Popple
dma_shared_buf_map() maps a dma_shared_buf for a device through the DMA
API. However a caller such as iommufd manages IOMMU domains itself and
has no device to map for. It needs the memory a dma_shared_buf describes
programmed into a domain at an IOVA of its choosing.
Add dma_shared_buf_iommu_map() to map a window of a dma_shared_buf into
an IOMMU domain. The window is mapped IOMMU_MMIO when the dma_shared_buf
has a p2pdma provider. Nothing is left mapped if it fails. There is no
matching unmap function as nothing is kept for the mapping, so the
caller undoes it with iommu_unmap().
Signed-off-by: Alistair Popple <apopple@nvidia.com>
---
drivers/dma-buf/dma-shared-buf.c | 71 ++++++++++++++++++++++++++++++++++++++++
include/linux/dma-shared-buf.h | 4 +++
2 files changed, 75 insertions(+)
diff --git a/drivers/dma-buf/dma-shared-buf.c b/drivers/dma-buf/dma-shared-buf.c
index 75ab08828ee8..d5d179b771f2 100644
--- a/drivers/dma-buf/dma-shared-buf.c
+++ b/drivers/dma-buf/dma-shared-buf.c
@@ -4,6 +4,7 @@
* be mapped for whichever device needs to access it.
*/
#include <linux/dma-shared-buf.h>
+#include <linux/iommu.h>
#include <linux/overflow.h>
#include <linux/pci-p2pdma.h>
#include <linux/slab.h>
@@ -136,3 +137,73 @@ void dma_shared_buf_unmap(struct device *dev, struct dma_addr_list *addrs,
}
kvfree(addrs);
}
+
+/**
+ * dma_shared_buf_iommu_map - Map a window of a dma_shared_buf into an IOMMU
+ * domain
+ * @shared_buf: The memory to map
+ * @domain: The domain to program
+ * @iova: Where in @domain the window starts
+ * @offset: Where in @shared_buf the window starts
+ * @size: Length of the window
+ * @prot: IOMMU_READ/IOMMU_WRITE/... for the mapping. IOMMU_MMIO is added
+ * and IOMMU_CACHE removed when @shared_buf is MMIO.
+ * @gfp: Allocation flags for the page tables
+ *
+ * For a caller managing an IOMMU domain itself rather than mapping for a
+ * device. Nothing is left mapped on failure and nothing of @shared_buf is
+ * referenced once this returns. The caller undoes the mapping with
+ * iommu_unmap().
+ *
+ * Returns: 0 on success, -EINVAL if the window is outside @shared_buf, or the
+ * error from iommu_map().
+ */
+int dma_shared_buf_iommu_map(const struct dma_shared_buf *shared_buf,
+ struct iommu_domain *domain, unsigned long iova,
+ size_t offset, size_t size, int prot, gfp_t gfp)
+{
+ unsigned long start_iova = iova;
+ const struct phys_vec *vec;
+ size_t i;
+ int ret;
+
+ if (offset >= shared_buf->size || size > shared_buf->size - offset)
+ return -EINVAL;
+
+ if (shared_buf->provider) {
+ prot &= ~IOMMU_CACHE;
+ prot |= IOMMU_MMIO;
+ }
+
+ /* Seek to the range holding @offset, then map up to @size bytes */
+ for (i = 0, vec = shared_buf->ranges; i < shared_buf->nr_ranges;
+ i++, vec++) {
+ size_t len;
+
+ if (offset >= vec->len) {
+ offset -= vec->len;
+ continue;
+ }
+ if (!size)
+ break;
+
+ len = min(size, vec->len - offset);
+ ret = iommu_map(domain, iova, vec->paddr + offset, len, prot,
+ gfp);
+ if (ret)
+ goto err_unmap;
+ iova += len;
+ size -= len;
+ offset = 0;
+ }
+ if (WARN_ON(size)) {
+ ret = -EINVAL;
+ goto err_unmap;
+ }
+ return 0;
+
+err_unmap:
+ if (iova != start_iova)
+ iommu_unmap(domain, start_iova, iova - start_iova);
+ return ret;
+}
diff --git a/include/linux/dma-shared-buf.h b/include/linux/dma-shared-buf.h
index 53f506e0cc85..2a3f6297d23f 100644
--- a/include/linux/dma-shared-buf.h
+++ b/include/linux/dma-shared-buf.h
@@ -9,6 +9,7 @@
#include <linux/dma-mapping.h>
#include <linux/types.h>
+struct iommu_domain;
struct p2pdma_provider;
/**
@@ -73,5 +74,8 @@ dma_shared_buf_map(struct device *dev, const struct dma_shared_buf *shared_buf,
enum dma_data_direction dir);
void dma_shared_buf_unmap(struct device *dev, struct dma_addr_list *addrs,
enum dma_data_direction dir);
+int dma_shared_buf_iommu_map(const struct dma_shared_buf *shared_buf,
+ struct iommu_domain *domain, unsigned long iova,
+ size_t offset, size_t size, int prot, gfp_t gfp);
#endif /* _LINUX_DMA_SHARED_BUF_H */
--
2.54.0
^ permalink raw reply related [flat|nested] 21+ messages in thread* [PATCH RFC 04/16] dma-shared-buf: Add dma_addr_list_to_sgt()
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
` (2 preceding siblings ...)
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 ` Alistair Popple
2026-12-01 7:19 ` sashiko-bot
2026-12-01 7:06 ` [PATCH RFC 05/16] dma-shared-buf: Add dma_shared_buf_from_pages() Alistair Popple
` (12 subsequent siblings)
16 siblings, 1 reply; 21+ messages in thread
From: Alistair Popple @ 2026-12-01 7:06 UTC (permalink / raw)
To: Karol Wachowski, Sumit Semwal, Christian König,
Philipp Stanner, David Airlie, Simona Vetter, Rodrigo Vivi,
Matthew Brost, Thomas Hellström, Jason Gunthorpe,
Leon Romanovsky, Kees Cook, Gustavo A. R. Silva, Alex Williamson,
Kevin Tian, Joerg Roedel (AMD), Will Deacon, Robin Murphy
Cc: dri-devel, linux-media, linaro-mm-sig, intel-xe, linux-rdma,
linux-kernel, linux-hardening, kvm, iommu, Alexey Kardashevskiy,
Mina Almasry, Baolu Lu, chaitanyak, Dongwon Kim, Felix Kuehling,
jhubbard, Karl Mehltretter, Vivek Kasireddy, Xu Yilun, zhipingz,
Alistair Popple
dma_shared_buf_map() returns the DMA addresses a dma_shared_buf was
mapped to as a struct dma_addr_list. However existing DMA-buf importers
expect an sg_table from dma_buf_map_sgt(), so the core has to be able to
present the addresses as one.
Add dma_addr_list_to_sgt() to build an sg_table over a struct
dma_addr_list and dma_addr_list_free_sgt() to free it. The table carries
DMA addresses only. It has no CPU side, so nothing handed the table can
reach a struct page, and orig_nents is zero so that this can be
detected, the same as the tables dma_buf_phys_vec_to_sgt() builds today.
The table is only a view of the mapping. The addresses stay mapped until
dma_shared_buf_unmap() is called, which must not happen while the
table is in use.
Signed-off-by: Alistair Popple <apopple@nvidia.com>
---
drivers/dma-buf/dma-shared-buf.c | 86 ++++++++++++++++++++++++++++++++++++++++
include/linux/dma-shared-buf.h | 4 ++
2 files changed, 90 insertions(+)
diff --git a/drivers/dma-buf/dma-shared-buf.c b/drivers/dma-buf/dma-shared-buf.c
index d5d179b771f2..c346d4c05de8 100644
--- a/drivers/dma-buf/dma-shared-buf.c
+++ b/drivers/dma-buf/dma-shared-buf.c
@@ -4,9 +4,11 @@
* be mapped for whichever device needs to access it.
*/
#include <linux/dma-shared-buf.h>
+#include <linux/align.h>
#include <linux/iommu.h>
#include <linux/overflow.h>
#include <linux/pci-p2pdma.h>
+#include <linux/scatterlist.h>
#include <linux/slab.h>
/**
@@ -207,3 +209,87 @@ int dma_shared_buf_iommu_map(const struct dma_shared_buf *shared_buf,
iommu_unmap(domain, start_iova, iova - start_iova);
return ret;
}
+
+#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;
+ /*
+ * The scatterlist has no CPU list, only the DMA list. Always
+ * set the page related values to NULL to ensure nothing
+ * handed the table can use them. 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;
+}
+
+/**
+ * dma_addr_list_to_sgt - Build an sg_table over the DMA addresses of a mapped
+ * dma_shared_buf
+ * @sgt: The table to build
+ * @addrs: The DMA addresses dma_shared_buf_map() returned
+ *
+ * For a caller which has to present the mapping as an sg_table. The table
+ * carries the DMA side only. The CPU side is left empty so that nothing handed
+ * the table can reach a struct page. The table does not own the mapping:
+ * @addrs must stay mapped while the table is in use and is still released with
+ * dma_shared_buf_unmap().
+ *
+ * Returns: 0 on success, in which case the table is freed with
+ * dma_addr_list_free_sgt(), or a negative error value.
+ */
+int dma_addr_list_to_sgt(struct sg_table *sgt,
+ const struct dma_addr_list *addrs)
+{
+ struct scatterlist *sgl;
+ unsigned int nents = 0;
+ unsigned int i;
+ int ret;
+
+ for (i = 0; i < addrs->nr_ranges; i++) {
+ unsigned int added = DIV_ROUND_UP(addrs->ranges[i].len,
+ MAX_SG_ENT_SZ);
+
+ if (check_add_overflow(nents, added, &nents))
+ return -E2BIG;
+ }
+
+ ret = sg_alloc_table(sgt, nents, GFP_KERNEL | __GFP_ZERO);
+ if (ret)
+ return ret;
+
+ sgl = sgt->sgl;
+ for (i = 0; i < addrs->nr_ranges; i++)
+ sgl = fill_sg_entry(sgl, addrs->ranges[i].len,
+ addrs->ranges[i].addr);
+ /* fill_sg_entry() hands back NULL once the last entry is filled */
+ WARN_ON_ONCE(sgl);
+
+ sgt->orig_nents = 0;
+ return 0;
+}
+
+/**
+ * dma_addr_list_free_sgt - Free a table built by dma_addr_list_to_sgt()
+ * @sgt: The table
+ */
+void dma_addr_list_free_sgt(struct sg_table *sgt)
+{
+ /* sg_free_table() frees nothing for orig_nents == 0, so restore it */
+ sgt->orig_nents = sgt->nents;
+ sg_free_table(sgt);
+}
diff --git a/include/linux/dma-shared-buf.h b/include/linux/dma-shared-buf.h
index 2a3f6297d23f..62e614b50771 100644
--- a/include/linux/dma-shared-buf.h
+++ b/include/linux/dma-shared-buf.h
@@ -11,6 +11,7 @@
struct iommu_domain;
struct p2pdma_provider;
+struct sg_table;
/**
* struct dma_shared_buf - Memory to be DMA mapped, as described by its owner
@@ -77,5 +78,8 @@ void dma_shared_buf_unmap(struct device *dev, struct dma_addr_list *addrs,
int dma_shared_buf_iommu_map(const struct dma_shared_buf *shared_buf,
struct iommu_domain *domain, unsigned long iova,
size_t offset, size_t size, int prot, gfp_t gfp);
+int dma_addr_list_to_sgt(struct sg_table *sgt,
+ const struct dma_addr_list *addrs);
+void dma_addr_list_free_sgt(struct sg_table *sgt);
#endif /* _LINUX_DMA_SHARED_BUF_H */
--
2.54.0
^ permalink raw reply related [flat|nested] 21+ messages in thread* Re: [PATCH RFC 04/16] dma-shared-buf: Add dma_addr_list_to_sgt()
2026-12-01 7:06 ` [PATCH RFC 04/16] dma-shared-buf: Add dma_addr_list_to_sgt() Alistair Popple
@ 2026-12-01 7:19 ` sashiko-bot
0 siblings, 0 replies; 21+ messages in thread
From: sashiko-bot @ 2026-12-01 7:19 UTC (permalink / raw)
To: Alistair Popple; +Cc: dri-devel, intel-xe, linux-rdma, kvm, media-ci
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] dma-shared-buf: integer overflow in dma_addr_list_to_sgt()
nents calculation
--
commit d053fd21323b983e9a0997bdd63898a8c3072b3f
Author: Alistair Popple <apopple@nvidia.com>
dma-shared-buf: Add dma_addr_list_to_sgt()
This commit adds dma_addr_list_to_sgt() and dma_addr_list_free_sgt() to build
and free a DMA-only sg_table over a struct dma_addr_list.
> diff --git a/drivers/dma-buf/dma-shared-buf.c b/drivers/dma-buf/dma-shared-buf.c
> index d5d179b771f22..c346d4c05de8e 100644
> --- a/drivers/dma-buf/dma-shared-buf.c
> +++ b/drivers/dma-buf/dma-shared-buf.c
[ ... ]
> @@ -207,3 +209,87 @@ int dma_shared_buf_iommu_map(const struct dma_shared_buf *shared_buf,
> iommu_unmap(domain, start_iova, iova - start_iova);
> return ret;
> }
> +
> +#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;
> + /*
> + * The scatterlist has no CPU list, only the DMA list. Always
> + * set the page related values to NULL to ensure nothing
> + * handed the table can use them. 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;
> +}
> +
> +/**
> + * dma_addr_list_to_sgt - Build an sg_table over the DMA addresses of a mapped
> + * dma_shared_buf
> + * @sgt: The table to build
> + * @addrs: The DMA addresses dma_shared_buf_map() returned
> + *
> + * For a caller which has to present the mapping as an sg_table. The table
> + * carries the DMA side only. The CPU side is left empty so that nothing handed
> + * the table can reach a struct page. The table does not own the mapping:
> + * @addrs must stay mapped while the table is in use and is still released with
> + * dma_shared_buf_unmap().
> + *
> + * Returns: 0 on success, in which case the table is freed with
> + * dma_addr_list_free_sgt(), or a negative error value.
> + */
> +int dma_addr_list_to_sgt(struct sg_table *sgt,
> + const struct dma_addr_list *addrs)
> +{
> + struct scatterlist *sgl;
> + unsigned int nents = 0;
> + unsigned int i;
> + int ret;
> +
> + for (i = 0; i < addrs->nr_ranges; i++) {
> + unsigned int added = DIV_ROUND_UP(addrs->ranges[i].len,
> + MAX_SG_ENT_SZ);
> +
> + if (check_add_overflow(nents, added, &nents))
> + return -E2BIG;
> + }
[Severity: High]
Can DIV_ROUND_UP(addrs->ranges[i].len, MAX_SG_ENT_SZ) overflow when computing
added?
MAX_SG_ENT_SZ is UINT_MAX - PAGE_SIZE + 1. On 32-bit builds, both size_t and
unsigned long are 32-bit, so the numerator addrs->ranges[i].len +
MAX_SG_ENT_SZ - 1 wraps modulo 2^32 for any range with len > PAGE_SIZE and
evaluates to added == 0. If addrs mixes a single-page range (added == 1) and
a multi-page range (added == 0), sg_alloc_table() allocates too few entries
and fill_sg_entry() advances sgl to NULL via sg_next(sgl), causing a NULL
pointer dereference in sg_set_page().
On 64-bit builds, assigning the quotient to unsigned int added also truncates
the upper 32 bits before check_add_overflow(). Using DIV_ROUND_UP_ULL() into
a u64 before check_add_overflow(nents, added, &nents) avoids both issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261201-dmabuf_shared_buf_v1_xe_rc5-v1-0-b672717c7fc9@nvidia.com?part=4
^ permalink raw reply [flat|nested] 21+ messages in thread
* [PATCH RFC 05/16] dma-shared-buf: Add dma_shared_buf_from_pages()
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
` (3 preceding siblings ...)
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 ` Alistair Popple
2026-12-01 7:06 ` [PATCH RFC 06/16] dma-buf: Allow exporters to hand out a dma_shared_buf Alistair Popple
` (11 subsequent siblings)
16 siblings, 0 replies; 21+ messages in thread
From: Alistair Popple @ 2026-12-01 7:06 UTC (permalink / raw)
To: Karol Wachowski, Sumit Semwal, Christian König,
Philipp Stanner, David Airlie, Simona Vetter, Rodrigo Vivi,
Matthew Brost, Thomas Hellström, Jason Gunthorpe,
Leon Romanovsky, Kees Cook, Gustavo A. R. Silva, Alex Williamson,
Kevin Tian, Joerg Roedel (AMD), Will Deacon, Robin Murphy
Cc: dri-devel, linux-media, linaro-mm-sig, intel-xe, linux-rdma,
linux-kernel, linux-hardening, kvm, iommu, Alexey Kardashevskiy,
Mina Almasry, Baolu Lu, chaitanyak, Dongwon Kim, Felix Kuehling,
jhubbard, Karl Mehltretter, Vivek Kasireddy, Xu Yilun, zhipingz,
Alistair Popple
A struct dma_shared_buf describes the memory to map as a list of ranges.
The DMA-buf exporters handing one out so far export MMIO and already
hold their buffer as a few ranges. However many exporters, most DRM
drivers among them, hold a buffer in system memory as an array of pages
and each would have to open code turning that into a dma_shared_buf.
Add dma_shared_buf_from_pages() to build the dma_shared_buf from an
array of pages. Adjacent pages are merged into one range, the same as
sg_alloc_table_from_pages() does for an sg_table. The dma_shared_buf is
a single allocation which the caller frees with kvfree().
The length of a range is not limited. drm_prime_pages_to_sg() limits a
segment to dma_max_mapping_size() of the exporting device, however the
limit that matters is that of the device the dma_shared_buf is mapped
for, which is not known when it is built. Only dma_shared_buf_map()
could apply it, which it does not do yet.
Signed-off-by: Alistair Popple <apopple@nvidia.com>
---
drivers/dma-buf/dma-shared-buf.c | 53 ++++++++++++++++++++++++++++++++++++++++
include/linux/dma-shared-buf.h | 3 +++
2 files changed, 56 insertions(+)
diff --git a/drivers/dma-buf/dma-shared-buf.c b/drivers/dma-buf/dma-shared-buf.c
index c346d4c05de8..7e198d0b17e8 100644
--- a/drivers/dma-buf/dma-shared-buf.c
+++ b/drivers/dma-buf/dma-shared-buf.c
@@ -6,6 +6,8 @@
#include <linux/dma-shared-buf.h>
#include <linux/align.h>
#include <linux/iommu.h>
+#include <linux/export.h>
+#include <linux/mm.h>
#include <linux/overflow.h>
#include <linux/pci-p2pdma.h>
#include <linux/scatterlist.h>
@@ -293,3 +295,54 @@ void dma_addr_list_free_sgt(struct sg_table *sgt)
sgt->orig_nents = sgt->nents;
sg_free_table(sgt);
}
+
+/**
+ * dma_shared_buf_from_pages - A dma_shared_buf for an array of pages
+ * @pages: The pages, in order
+ * @nr_pages: Number of entries in @pages
+ *
+ * Builds the dma_shared_buf for memory its owner holds as an array of pages.
+ * Adjacent pages are merged into one range and it has no p2pdma provider and no
+ * DMA attributes, which the caller may set afterwards. The pages must stay
+ * where they are for as long as the dma_shared_buf is in use.
+ *
+ * Returns: the dma_shared_buf, which the caller frees with kvfree(), or a
+ * negative error value wrapped into a pointer.
+ */
+struct dma_shared_buf *dma_shared_buf_from_pages(struct page **pages,
+ unsigned long nr_pages)
+{
+ struct dma_shared_buf *shared_buf;
+ struct phys_vec *vec = NULL;
+ size_t nr_ranges = 0;
+ unsigned long i;
+
+ if (!nr_pages)
+ return ERR_PTR(-EINVAL);
+
+ for (i = 0; i < nr_pages; i++)
+ if (!i || page_to_pfn(pages[i]) != page_to_pfn(pages[i - 1]) + 1)
+ nr_ranges++;
+
+ shared_buf = kvmalloc(struct_size(shared_buf, ranges, nr_ranges),
+ GFP_KERNEL);
+ if (!shared_buf)
+ return ERR_PTR(-ENOMEM);
+
+ shared_buf->provider = NULL;
+ shared_buf->attrs = 0;
+ shared_buf->size = (size_t)nr_pages << PAGE_SHIFT;
+ shared_buf->nr_ranges = nr_ranges;
+
+ for (i = 0; i < nr_pages; i++) {
+ if (!i || page_to_pfn(pages[i]) != page_to_pfn(pages[i - 1]) + 1) {
+ vec = vec ? vec + 1 : shared_buf->ranges;
+ vec->paddr = page_to_phys(pages[i]);
+ vec->len = 0;
+ }
+ vec->len += PAGE_SIZE;
+ }
+
+ return shared_buf;
+}
+EXPORT_SYMBOL_NS_GPL(dma_shared_buf_from_pages, "DMA_BUF");
diff --git a/include/linux/dma-shared-buf.h b/include/linux/dma-shared-buf.h
index 62e614b50771..0d5dadc8de5b 100644
--- a/include/linux/dma-shared-buf.h
+++ b/include/linux/dma-shared-buf.h
@@ -11,6 +11,7 @@
struct iommu_domain;
struct p2pdma_provider;
+struct page;
struct sg_table;
/**
@@ -81,5 +82,7 @@ int dma_shared_buf_iommu_map(const struct dma_shared_buf *shared_buf,
int dma_addr_list_to_sgt(struct sg_table *sgt,
const struct dma_addr_list *addrs);
void dma_addr_list_free_sgt(struct sg_table *sgt);
+struct dma_shared_buf *dma_shared_buf_from_pages(struct page **pages,
+ unsigned long nr_pages);
#endif /* _LINUX_DMA_SHARED_BUF_H */
--
2.54.0
^ permalink raw reply related [flat|nested] 21+ messages in thread* [PATCH RFC 06/16] dma-buf: Allow exporters to hand out a dma_shared_buf
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
` (4 preceding siblings ...)
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 ` Alistair Popple
2026-12-01 7:06 ` [PATCH RFC 07/16] dma-buf: Add dma_buf_iommu_map() for IOMMU domain importers Alistair Popple
` (10 subsequent siblings)
16 siblings, 0 replies; 21+ messages in thread
From: Alistair Popple @ 2026-12-01 7:06 UTC (permalink / raw)
To: Karol Wachowski, Sumit Semwal, Christian König,
Philipp Stanner, David Airlie, Simona Vetter, Rodrigo Vivi,
Matthew Brost, Thomas Hellström, Jason Gunthorpe,
Leon Romanovsky, Kees Cook, Gustavo A. R. Silva, Alex Williamson,
Kevin Tian, Joerg Roedel (AMD), Will Deacon, Robin Murphy
Cc: dri-devel, linux-media, linaro-mm-sig, intel-xe, linux-rdma,
linux-kernel, linux-hardening, kvm, iommu, Alexey Kardashevskiy,
Mina Almasry, Baolu Lu, chaitanyak, Dongwon Kim, Felix Kuehling,
jhubbard, Karl Mehltretter, Vivek Kasireddy, Xu Yilun, zhipingz,
Alistair Popple, Jason Gunthorpe
Currently exporters must create an sg_table even if they can't use the
DMA API to create them. For example vfio-pci and uverbs export MMIO
which don't have struct page and call dma_buf_phys_vec_to_sgt() from
their map_dma_buf() to create it for the importing device. iommufd,
which needs to program domains itself, reaches it through a private vfio
interface since no dma-buf API hands out the address of a buffer.
Add a second type of dma_buf_op - {un}map_dma_shared_buf() - which
allows exporters to return a dma_shared_buf describing the memory to
be mapped instead of a sg_table. The dma-shared-buf core can then be
used to map that correctly for the given device and convert it into a
list of dma addresses for importers to program.
This will allow for the eventual removal of dma_buf_{un}map_sgt() and
the associated sg_table handling in dma-buf exporters and importers once
all have been converted to use the new operations.
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.c | 134 ++++++++++++++++++++++++++++++++++++++++++++--
include/linux/dma-buf.h | 30 +++++++++--
2 files changed, 156 insertions(+), 8 deletions(-)
diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c
index 0b8da0c0bc5f..70058509c397 100644
--- a/drivers/dma-buf/dma-buf.c
+++ b/drivers/dma-buf/dma-buf.c
@@ -714,11 +714,17 @@ struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info)
int ret;
if (WARN_ON(!exp_info->priv || !exp_info->ops
- || !exp_info->ops->map_dma_buf
- || !exp_info->ops->unmap_dma_buf
|| !exp_info->ops->release))
return ERR_PTR(-EINVAL);
+ /* Exactly one of the two map and unmap pairs */
+ if (WARN_ON(!exp_info->ops->map_dma_buf != !exp_info->ops->unmap_dma_buf ||
+ !exp_info->ops->map_dma_shared_buf !=
+ !exp_info->ops->unmap_dma_shared_buf ||
+ !exp_info->ops->map_dma_buf ==
+ !exp_info->ops->map_dma_shared_buf))
+ return ERR_PTR(-EINVAL);
+
if (WARN_ON(!exp_info->ops->pin != !exp_info->ops->unpin))
return ERR_PTR(-EINVAL);
@@ -933,6 +939,122 @@ static void dma_buf_unwrap_sg_table(struct sg_table **sg_table)
kfree(copy);
}
+/*
+ * Get a list of DMA addresses to program into importing device hardware from an
+ * exporter that provides a dma_shared_buf.
+ */
+static struct dma_addr_list *
+dma_buf_map_list(struct dma_buf_attachment *attach,
+ enum dma_data_direction dir)
+{
+ struct dma_shared_buf *shared_buf;
+ struct dma_addr_list *addrs;
+
+ dma_resv_assert_held(attach->dmabuf->resv);
+
+ shared_buf = attach->dmabuf->ops->map_dma_shared_buf(attach);
+ if (!shared_buf)
+ return ERR_PTR(-ENOMEM);
+ if (IS_ERR(shared_buf))
+ return ERR_CAST(shared_buf);
+
+ /*
+ * MMIO goes only to importers prepared for memory without struct
+ * pages.
+ */
+ if (shared_buf->provider && !attach->peer2peer) {
+ addrs = ERR_PTR(-EOPNOTSUPP);
+ goto err_unmap_list;
+ }
+
+ addrs = dma_shared_buf_map(attach->dev, shared_buf, dir);
+ if (IS_ERR(addrs))
+ goto err_unmap_list;
+
+ addrs->priv = shared_buf;
+ return addrs;
+
+err_unmap_list:
+ attach->dmabuf->ops->unmap_dma_shared_buf(attach, shared_buf);
+ return addrs;
+}
+
+static void dma_buf_unmap_list(struct dma_buf_attachment *attach,
+ struct dma_addr_list *addrs,
+ enum dma_data_direction dir)
+{
+ struct dma_shared_buf *shared_buf = addrs->priv;
+
+ dma_shared_buf_unmap(attach->dev, addrs, dir);
+ attach->dmabuf->ops->unmap_dma_shared_buf(attach, shared_buf);
+}
+
+/**
+ * struct dma_buf_dma - the sg_table the core builds for an exporter that hands
+ * out a dma_shared_buf
+ * @sgt: A DMA-only scatterlist over the ranges of @addrs, built by
+ * dma_addr_list_to_sgt()
+ * @addrs: The DMA addresses the exporter's dma_shared_buf was mapped to
+ */
+struct dma_buf_dma {
+ struct sg_table sgt;
+ struct dma_addr_list *addrs;
+};
+
+/*
+ * The sg_table of an attachment: made by the exporter's map_dma_buf(), or by
+ * the core from the dma_shared_buf its map_dma_shared_buf() hands out.
+ * Importers see no difference.
+ */
+static struct sg_table *
+dma_buf_map_sg_table(struct dma_buf_attachment *attach,
+ enum dma_data_direction dir)
+{
+ struct dma_buf_dma *dma;
+ int ret;
+
+ if (attach->dmabuf->ops->map_dma_buf)
+ return attach->dmabuf->ops->map_dma_buf(attach, dir);
+
+ dma = kzalloc_obj(*dma);
+ if (!dma)
+ return ERR_PTR(-ENOMEM);
+
+ dma->addrs = dma_buf_map_list(attach, dir);
+ if (IS_ERR(dma->addrs)) {
+ ret = PTR_ERR(dma->addrs);
+ goto err_free;
+ }
+
+ ret = dma_addr_list_to_sgt(&dma->sgt, dma->addrs);
+ if (ret)
+ goto err_unmap;
+ return &dma->sgt;
+
+err_unmap:
+ dma_buf_unmap_list(attach, dma->addrs, dir);
+err_free:
+ kfree(dma);
+ return ERR_PTR(ret);
+}
+
+static void dma_buf_unmap_sg_table(struct dma_buf_attachment *attach,
+ struct sg_table *sg_table,
+ enum dma_data_direction dir)
+{
+ struct dma_buf_dma *dma;
+
+ if (attach->dmabuf->ops->unmap_dma_buf) {
+ attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, dir);
+ return;
+ }
+
+ dma = container_of(sg_table, struct dma_buf_dma, sgt);
+ dma_addr_list_free_sgt(&dma->sgt);
+ dma_buf_unmap_list(attach, dma->addrs, dir);
+ kfree(dma);
+}
+
static inline bool
dma_buf_attachment_is_dynamic(struct dma_buf_attachment *attach)
{
@@ -1001,6 +1123,8 @@ dma_buf_pin_on_map(struct dma_buf_attachment *attach)
* - &dma_buf_ops.unpin()
* - &dma_buf_ops.map_dma_buf()
* - &dma_buf_ops.unmap_dma_buf()
+ * - &dma_buf_ops.map_dma_shared_buf()
+ * - &dma_buf_ops.unmap_dma_shared_buf()
* - &dma_buf_ops.vmap()
* - &dma_buf_ops.vunmap()
*
@@ -1213,7 +1337,7 @@ struct sg_table *dma_buf_map_sgt(struct dma_buf_attachment *attach,
return ERR_PTR(ret);
}
- sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
+ sg_table = dma_buf_map_sg_table(attach, direction);
if (!sg_table)
sg_table = ERR_PTR(-ENOMEM);
if (IS_ERR(sg_table))
@@ -1252,7 +1376,7 @@ struct sg_table *dma_buf_map_sgt(struct dma_buf_attachment *attach,
return sg_table;
error_unmap:
- attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, direction);
+ dma_buf_unmap_sg_table(attach, sg_table, direction);
sg_table = ERR_PTR(ret);
error_unpin:
@@ -1313,7 +1437,7 @@ void dma_buf_unmap_sgt(struct dma_buf_attachment *attach,
dma_resv_assert_held(attach->dmabuf->resv);
dma_buf_unwrap_sg_table(&sg_table);
- attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, direction);
+ dma_buf_unmap_sg_table(attach, sg_table, direction);
if (dma_buf_pin_on_map(attach))
attach->dmabuf->ops->unpin(attach);
diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h
index b138b77212c9..719296a3d47e 100644
--- a/include/linux/dma-buf.h
+++ b/include/linux/dma-buf.h
@@ -19,6 +19,7 @@
#include <linux/scatterlist.h>
#include <linux/list.h>
#include <linux/dma-mapping.h>
+#include <linux/dma-shared-buf.h>
#include <linux/fs.h>
#include <linux/dma-fence.h>
#include <linux/wait.h>
@@ -117,8 +118,10 @@ struct dma_buf_ops {
* @map_dma_buf:
*
* This is called by dma_buf_map_sgt() and is used to map a
- * shared &dma_buf into device address space, and it is mandatory. It
- * can only be called if @attach has been called successfully.
+ * shared &dma_buf into device address space. It can only be called if
+ * @attach has been called successfully. An exporter provides either
+ * this and @unmap_dma_buf or @map_dma_shared_buf and
+ * @unmap_dma_shared_buf, see there.
*
* This call may sleep, e.g. when the backing storage first needs to be
* allocated, or moved to a location suitable for all currently attached
@@ -174,7 +177,7 @@ struct dma_buf_ops {
* @unmap_dma_buf:
*
* This is called by dma_buf_unmap_sgt() and should unmap and
- * release the &sg_table allocated in @map_dma_buf, and it is mandatory.
+ * release the &sg_table allocated in @map_dma_buf, and goes with it.
* For static dma_buf handling this might also unpin the backing
* storage if this is the last mapping of the DMA buffer.
*/
@@ -182,6 +185,27 @@ struct dma_buf_ops {
struct sg_table *,
enum dma_data_direction);
+ /**
+ * @map_dma_shared_buf:
+ *
+ * The alternative to @map_dma_buf for an exporter that provides a
+ * dma_shared_buf instead of a sg_table.
+ *
+ * Returns the dma_shared_buf, or a negative error value wrapped into a
+ * pointer.
+ */
+ struct dma_shared_buf *
+ (*map_dma_shared_buf)(struct dma_buf_attachment *attach);
+
+ /**
+ * @unmap_dma_shared_buf:
+ *
+ * Release a dma_shared_buf returned by @map_dma_shared_buf. The core
+ * holds none of its addresses once this is called.
+ */
+ void (*unmap_dma_shared_buf)(struct dma_buf_attachment *attach,
+ struct dma_shared_buf *shared_buf);
+
/* TODO: Add try_map_dma_buf version, to return immed with -EBUSY
* if the call would block.
*/
--
2.54.0
^ permalink raw reply related [flat|nested] 21+ messages in thread* [PATCH RFC 07/16] dma-buf: Add dma_buf_iommu_map() for IOMMU domain importers
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
` (5 preceding siblings ...)
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 ` 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
` (9 subsequent siblings)
16 siblings, 0 replies; 21+ messages in thread
From: Alistair Popple @ 2026-12-01 7:06 UTC (permalink / raw)
To: Karol Wachowski, Sumit Semwal, Christian König,
Philipp Stanner, David Airlie, Simona Vetter, Rodrigo Vivi,
Matthew Brost, Thomas Hellström, Jason Gunthorpe,
Leon Romanovsky, Kees Cook, Gustavo A. R. Silva, Alex Williamson,
Kevin Tian, Joerg Roedel (AMD), Will Deacon, Robin Murphy
Cc: dri-devel, linux-media, linaro-mm-sig, intel-xe, linux-rdma,
linux-kernel, linux-hardening, kvm, iommu, Alexey Kardashevskiy,
Mina Almasry, Baolu Lu, chaitanyak, Dongwon Kim, Felix Kuehling,
jhubbard, Karl Mehltretter, Vivek Kasireddy, Xu Yilun, zhipingz,
Alistair Popple
iommufd imports DMA-bufs to program their memory into IOMMU domains.
Today it does so through a private interface with vfio-pci that hands
it the address of the BAR, but this doesn't work with other drivers such
as GPU drivers meaning their DMA-bufs cannot be used with iommufd to
orchestrate things such as direct PCIe P2P storage access.
So add an importer for IOMMU domains. The iommufd importer calls
dma_buf_iommu_map() with its domain, an IOVA and a window into the
buffer, and dma_buf_iommu_unmap() to undo it.
The core gets the dma_shared_buf from the exporter and has
dma_shared_buf_iommu_map() program the part inside the window into the
domain, marking it IOMMU_MMIO when the dma_shared_buf has a p2pdma
provider.
Only an exporter implementing the map_dma_shared_buf() ops can be
mapped this way. The functions are exported to iommufd only as there are
no other users of this functionality.
Signed-off-by: Alistair Popple <apopple@nvidia.com>
---
drivers/dma-buf/dma-buf-mapping.c | 124 ++++++++++++++++++++++++++++++++++++++
include/linux/dma-buf-mapping.h | 15 +++++
2 files changed, 139 insertions(+)
diff --git a/drivers/dma-buf/dma-buf-mapping.c b/drivers/dma-buf/dma-buf-mapping.c
index 5167cda7ea21..ec9776b1dff0 100644
--- a/drivers/dma-buf/dma-buf-mapping.c
+++ b/drivers/dma-buf/dma-buf-mapping.c
@@ -5,6 +5,7 @@
*/
#include <linux/dma-buf-mapping.h>
#include <linux/dma-resv.h>
+#include <linux/iommu.h>
#include <linux/overflow.h>
#include <linux/align.h>
@@ -259,3 +260,126 @@ void dma_buf_free_sgt(struct dma_buf_attachment *attach, struct sg_table *sgt,
}
EXPORT_SYMBOL_NS_GPL(dma_buf_free_sgt, "DMA_BUF");
+
+/*
+ * IOMMU domain importers
+ *
+ * An importer that programs an IOMMU domain itself hands over the domain and
+ * an IOVA window, the exporter hands over its dma_shared_buf, and the core,
+ * which sees both, has it programmed into the domain. Neither side sees
+ * the other's data. Only exporters handing out a dma_shared_buf can be mapped
+ * this way, and only iommufd may ask.
+ */
+
+/**
+ * dma_buf_iommu_mappable - Can the buffer be mapped into an IOMMU domain
+ * @dmabuf: the buffer
+ *
+ * Lets an importer fail at import time rather than when a domain is first
+ * filled.
+ *
+ * Returns: true when the exporter hands out a dma_shared_buf, so
+ * dma_buf_iommu_map() can work.
+ */
+bool dma_buf_iommu_mappable(struct dma_buf *dmabuf)
+{
+ return dmabuf->ops->map_dma_shared_buf;
+}
+EXPORT_SYMBOL_FOR_MODULES(dma_buf_iommu_mappable, "iommufd");
+
+/**
+ * dma_buf_iommu_map - Map a window of the buffer into an IOMMU domain
+ * @attach: the attachment
+ * @domain: the domain to program
+ * @iova: where in @domain the window starts
+ * @offset: where in the buffer the window starts
+ * @size: length of the window
+ * @prot: IOMMU_READ/IOMMU_WRITE/... for the mapping. IOMMU_MMIO is added
+ * and IOMMU_CACHE removed when the buffer is MMIO.
+ * @gfp: allocation flags for the page tables
+ *
+ * Called with the dma_resv lock held and the attachment pinned. Asks the
+ * exporter for its dma_shared_buf, has the part of it inside the window
+ * programmed into @domain and releases it again: nothing about the
+ * buffer's addresses is kept between calls. The importer undoes the mapping
+ * with dma_buf_iommu_unmap(), which it must do before the exporter revokes,
+ * i.e. from its invalidate_mappings().
+ *
+ * The buffer's DMA_RESV_USAGE_KERNEL fences are waited for before the domain
+ * is programmed, as the importer has no way to wait for them once the memory
+ * is reachable through @domain.
+ *
+ * Returns: 0 on success, -EOPNOTSUPP if the exporter does not hand out a
+ * dma_shared_buf or the buffer is MMIO and the importer does not allow
+ * peer2peer, -EINVAL if the window is outside the buffer, -ERESTARTSYS if the
+ * fence wait is interrupted by a signal, or the error from the exporter or
+ * iommu_map().
+ */
+int dma_buf_iommu_map(struct dma_buf_attachment *attach,
+ struct iommu_domain *domain, unsigned long iova,
+ size_t offset, size_t size, int prot, gfp_t gfp)
+{
+ const struct dma_buf_ops *ops = attach->dmabuf->ops;
+ struct dma_shared_buf *shared_buf;
+ long wait;
+ int ret;
+
+ dma_resv_assert_held(attach->dmabuf->resv);
+
+ if (!ops->map_dma_shared_buf)
+ return -EOPNOTSUPP;
+
+ shared_buf = ops->map_dma_shared_buf(attach);
+ if (!shared_buf)
+ return -ENOMEM;
+ if (IS_ERR(shared_buf))
+ return PTR_ERR(shared_buf);
+
+ if (shared_buf->provider && !attach->peer2peer) {
+ ret = -EOPNOTSUPP;
+ goto out_unmap_list;
+ }
+
+ /*
+ * The exporter may still be moving the buffer to where it was pinned,
+ * wait until that is finished.
+ */
+ wait = dma_resv_wait_timeout(attach->dmabuf->resv,
+ DMA_RESV_USAGE_KERNEL, true,
+ MAX_SCHEDULE_TIMEOUT);
+ if (wait < 0) {
+ ret = wait;
+ goto out_unmap_list;
+ }
+
+ ret = dma_shared_buf_iommu_map(shared_buf, domain, iova, offset,
+ size, prot, gfp);
+
+out_unmap_list:
+ ops->unmap_dma_shared_buf(attach, shared_buf);
+ return ret;
+}
+EXPORT_SYMBOL_FOR_MODULES(dma_buf_iommu_map, "iommufd");
+
+/**
+ * dma_buf_iommu_unmap - Undo dma_buf_iommu_map() for a window
+ * @attach: the attachment the window was mapped for
+ * @domain: the domain it was mapped into
+ * @iova: where in @domain the window starts
+ * @size: length of the window
+ *
+ * The exporter is not involved and no lock is needed: this only unmaps the
+ * IOVA range. It exists so the importer never operates on a buffer's domain
+ * mappings itself, and must be called before the exporter revokes the
+ * buffer, so from the importer's invalidate_mappings().
+ */
+void dma_buf_iommu_unmap(struct dma_buf_attachment *attach,
+ struct iommu_domain *domain, unsigned long iova,
+ size_t size)
+{
+ size_t unmapped = iommu_unmap(domain, iova, size);
+
+ /* Anything else is a bug here or in the IOMMU driver */
+ WARN_ON(unmapped != size);
+}
+EXPORT_SYMBOL_FOR_MODULES(dma_buf_iommu_unmap, "iommufd");
diff --git a/include/linux/dma-buf-mapping.h b/include/linux/dma-buf-mapping.h
index 09bde3f748e4..04fab6bd5dc7 100644
--- a/include/linux/dma-buf-mapping.h
+++ b/include/linux/dma-buf-mapping.h
@@ -14,4 +14,19 @@ struct sg_table *dma_buf_phys_vec_to_sgt(struct dma_buf_attachment *attach,
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;
+
+/*
+ * Importers programming an IOMMU domain themselves. Restricted to iommufd:
+ * the exporter's dma_shared_buf goes into the domain through the core, so
+ * neither driver sees the other's objects.
+ */
+bool dma_buf_iommu_mappable(struct dma_buf *dmabuf);
+int dma_buf_iommu_map(struct dma_buf_attachment *attach,
+ struct iommu_domain *domain, unsigned long iova,
+ size_t offset, size_t size, int prot, gfp_t gfp);
+void dma_buf_iommu_unmap(struct dma_buf_attachment *attach,
+ struct iommu_domain *domain, unsigned long iova,
+ size_t size);
#endif
--
2.54.0
^ permalink raw reply related [flat|nested] 21+ messages in thread* [PATCH RFC 08/16] dma-buf: Factor out the pin and fence wait around the exporter's map
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
` (6 preceding siblings ...)
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 ` 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
` (8 subsequent siblings)
16 siblings, 0 replies; 21+ messages in thread
From: Alistair Popple @ 2026-12-01 7:06 UTC (permalink / raw)
To: Karol Wachowski, Sumit Semwal, Christian König,
Philipp Stanner, David Airlie, Simona Vetter, Rodrigo Vivi,
Matthew Brost, Thomas Hellström, Jason Gunthorpe,
Leon Romanovsky, Kees Cook, Gustavo A. R. Silva, Alex Williamson,
Kevin Tian, Joerg Roedel (AMD), Will Deacon, Robin Murphy
Cc: dri-devel, linux-media, linaro-mm-sig, intel-xe, linux-rdma,
linux-kernel, linux-hardening, kvm, iommu, Alexey Kardashevskiy,
Mina Almasry, Baolu Lu, chaitanyak, Dongwon Kim, Felix Kuehling,
jhubbard, Karl Mehltretter, Vivek Kasireddy, Xu Yilun, zhipingz,
Alistair Popple
dma_buf_map_sgt() pins the buffer for static importers of exporters with
a pin callback, calls the exporter, and waits for the buffer's fences on
behalf of static importers. A second way of mapping an attachment is
about to need the same steps around a different middle, so move them
into dma_buf_map_pin(), dma_buf_map_wait() and dma_buf_map_unpin(). No
functional change.
Signed-off-by: Alistair Popple <apopple@nvidia.com>
---
drivers/dma-buf/dma-buf.c | 69 +++++++++++++++++++++++++++++------------------
1 file changed, 43 insertions(+), 26 deletions(-)
diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c
index 70058509c397..799be8cad3ca 100644
--- a/drivers/dma-buf/dma-buf.c
+++ b/drivers/dma-buf/dma-buf.c
@@ -1068,6 +1068,40 @@ dma_buf_pin_on_map(struct dma_buf_attachment *attach)
!dma_buf_attachment_is_dynamic(attach);
}
+/* The pin around a static importer's mapping, for exporters that need one */
+static int dma_buf_map_pin(struct dma_buf_attachment *attach)
+{
+ int ret;
+
+ if (!dma_buf_pin_on_map(attach))
+ return 0;
+
+ ret = attach->dmabuf->ops->pin(attach);
+ /*
+ * Catch exporters making buffers inaccessible even when
+ * attachments preventing that exist.
+ */
+ WARN_ON_ONCE(ret == -EBUSY);
+ return ret;
+}
+
+static void dma_buf_map_unpin(struct dma_buf_attachment *attach)
+{
+ if (dma_buf_pin_on_map(attach))
+ attach->dmabuf->ops->unpin(attach);
+}
+
+/* Importers with static attachments don't wait for fences themselves */
+static long dma_buf_map_wait(struct dma_buf_attachment *attach)
+{
+ if (dma_buf_attachment_is_dynamic(attach))
+ return 0;
+
+ return dma_resv_wait_timeout(attach->dmabuf->resv,
+ DMA_RESV_USAGE_KERNEL, true,
+ MAX_SCHEDULE_TIMEOUT);
+}
+
/**
* DOC: locking convention
*
@@ -1326,16 +1360,9 @@ struct sg_table *dma_buf_map_sgt(struct dma_buf_attachment *attach,
dma_resv_assert_held(attach->dmabuf->resv);
- if (dma_buf_pin_on_map(attach)) {
- ret = attach->dmabuf->ops->pin(attach);
- /*
- * Catch exporters making buffers inaccessible even when
- * attachments preventing that exist.
- */
- WARN_ON_ONCE(ret == -EBUSY);
- if (ret)
- return ERR_PTR(ret);
- }
+ ret = dma_buf_map_pin(attach);
+ if (ret)
+ return ERR_PTR(ret);
sg_table = dma_buf_map_sg_table(attach, direction);
if (!sg_table)
@@ -1343,16 +1370,10 @@ struct sg_table *dma_buf_map_sgt(struct dma_buf_attachment *attach,
if (IS_ERR(sg_table))
goto error_unpin;
- /*
- * Importers with static attachments don't wait for fences.
- */
- if (!dma_buf_attachment_is_dynamic(attach)) {
- ret = dma_resv_wait_timeout(attach->dmabuf->resv,
- DMA_RESV_USAGE_KERNEL, true,
- MAX_SCHEDULE_TIMEOUT);
- if (ret < 0)
- goto error_unmap;
- }
+ ret = dma_buf_map_wait(attach);
+ if (ret < 0)
+ goto error_unmap;
+
ret = dma_buf_wrap_sg_table(&sg_table);
if (ret)
goto error_unmap;
@@ -1380,9 +1401,7 @@ struct sg_table *dma_buf_map_sgt(struct dma_buf_attachment *attach,
sg_table = ERR_PTR(ret);
error_unpin:
- if (dma_buf_pin_on_map(attach))
- attach->dmabuf->ops->unpin(attach);
-
+ dma_buf_map_unpin(attach);
return sg_table;
}
EXPORT_SYMBOL_NS_GPL(dma_buf_map_sgt, "DMA_BUF");
@@ -1438,9 +1457,7 @@ void dma_buf_unmap_sgt(struct dma_buf_attachment *attach,
dma_buf_unwrap_sg_table(&sg_table);
dma_buf_unmap_sg_table(attach, sg_table, direction);
-
- if (dma_buf_pin_on_map(attach))
- attach->dmabuf->ops->unpin(attach);
+ dma_buf_map_unpin(attach);
}
EXPORT_SYMBOL_NS_GPL(dma_buf_unmap_sgt, "DMA_BUF");
--
2.54.0
^ permalink raw reply related [flat|nested] 21+ messages in thread* [PATCH RFC 09/16] dma-buf: Add dma_buf_map_dma_list() for DMA address only importers
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
` (7 preceding siblings ...)
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 ` 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
` (7 subsequent siblings)
16 siblings, 0 replies; 21+ messages in thread
From: Alistair Popple @ 2026-12-01 7:06 UTC (permalink / raw)
To: Karol Wachowski, Sumit Semwal, Christian König,
Philipp Stanner, David Airlie, Simona Vetter, Rodrigo Vivi,
Matthew Brost, Thomas Hellström, Jason Gunthorpe,
Leon Romanovsky, Kees Cook, Gustavo A. R. Silva, Alex Williamson,
Kevin Tian, Joerg Roedel (AMD), Will Deacon, Robin Murphy
Cc: dri-devel, linux-media, linaro-mm-sig, intel-xe, linux-rdma,
linux-kernel, linux-hardening, kvm, iommu, Alexey Kardashevskiy,
Mina Almasry, Baolu Lu, chaitanyak, Dongwon Kim, Felix Kuehling,
jhubbard, Karl Mehltretter, Vivek Kasireddy, Xu Yilun, zhipingz,
Alistair Popple
The sg_table is a poor fit for what DMA-buf exists to exchange. Every
importer has to walk a scatterlist to get at the addresses it actually
wants. Replace it with a simple list of DMA address regions.
Add dma_buf_map_dma_list() and dma_buf_unmap_dma_list() as the
alternative to dma_buf_map_sgt(). They return an array of DMA address
ranges, valid in the address space of the attached device, and the
total size. The importer holds the list until unmap. Pin-on-map and the
fence wait for static importers work the same as for dma_buf_map_sgt().
No exporter changes are required for compatibility with importers
using dma_buf_map_dma_list(). A converted exporter already returning a
dma_shared_buf uses that to create a dma_addr_list directly, otherwise
the sg_table is converted to a dma_addr_list.
Signed-off-by: Alistair Popple <apopple@nvidia.com>
---
drivers/dma-buf/dma-buf.c | 191 ++++++++++++++++++++++++++++++++++++++++++++++
include/linux/dma-buf.h | 12 +++
2 files changed, 203 insertions(+)
diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c
index 799be8cad3ca..395903fea45e 100644
--- a/drivers/dma-buf/dma-buf.c
+++ b/drivers/dma-buf/dma-buf.c
@@ -1001,6 +1001,51 @@ struct dma_buf_dma {
struct dma_addr_list *addrs;
};
+/*
+ * The DMA address list of an exporter that maps an sg_table itself: the
+ * table's DMA side copied out, the table kept until unmap. The exporter
+ * cannot tell a list importer from a table one.
+ */
+static struct dma_addr_list *
+dma_buf_sgt_map_dma_list(struct dma_buf_attachment *attach,
+ enum dma_data_direction dir)
+{
+ const struct dma_buf_ops *ops = attach->dmabuf->ops;
+ struct dma_addr_list *addrs;
+ struct scatterlist *sg;
+ struct sg_table *sgt;
+ int i;
+
+ sgt = ops->map_dma_buf(attach, dir);
+ if (!sgt)
+ return ERR_PTR(-ENOMEM);
+ if (IS_ERR(sgt))
+ return ERR_CAST(sgt);
+
+ addrs = kvzalloc(struct_size(addrs, ranges, sgt->nents), GFP_KERNEL);
+ if (!addrs) {
+ ops->unmap_dma_buf(attach, sgt, dir);
+ return ERR_PTR(-ENOMEM);
+ }
+
+ addrs->nr_ranges = sgt->nents;
+ for_each_sgtable_dma_sg(sgt, sg, i) {
+ addrs->ranges[i].addr = sg_dma_address(sg);
+ addrs->ranges[i].len = sg_dma_len(sg);
+ addrs->size += sg_dma_len(sg);
+ }
+ addrs->priv = sgt;
+ return addrs;
+}
+
+static void dma_buf_sgt_unmap_dma_list(struct dma_buf_attachment *attach,
+ struct dma_addr_list *addrs,
+ enum dma_data_direction dir)
+{
+ attach->dmabuf->ops->unmap_dma_buf(attach, addrs->priv, dir);
+ kvfree(addrs);
+}
+
/*
* The sg_table of an attachment: made by the exporter's map_dma_buf(), or by
* the core from the dma_shared_buf its map_dma_shared_buf() hands out.
@@ -1117,6 +1162,8 @@ static long dma_buf_map_wait(struct dma_buf_attachment *attach)
* - dma_buf_unpin()
* - dma_buf_map_sgt()
* - dma_buf_unmap_sgt()
+ * - dma_buf_map_dma_list()
+ * - dma_buf_unmap_dma_list()
* - dma_buf_vmap()
* - dma_buf_vunmap()
*
@@ -1135,6 +1182,8 @@ static long dma_buf_map_wait(struct dma_buf_attachment *attach)
* - dma_buf_end_cpu_access()
* - dma_buf_map_sgt_unlocked()
* - dma_buf_unmap_sgt_unlocked()
+ * - dma_buf_map_dma_list_unlocked()
+ * - dma_buf_unmap_dma_list_unlocked()
* - dma_buf_vmap_unlocked()
* - dma_buf_vunmap_unlocked()
*
@@ -1486,6 +1535,148 @@ void dma_buf_unmap_sgt_unlocked(struct dma_buf_attachment *attach,
}
EXPORT_SYMBOL_NS_GPL(dma_buf_unmap_sgt_unlocked, "DMA_BUF");
+/**
+ * dma_buf_map_dma_list - Returns the buffer's DMA addresses for the attachment's
+ * device, without a scatterlist
+ * @attach: [in] attachment whose DMA addresses are to be returned
+ * @direction: [in] direction of DMA transfer
+ *
+ * The alternative to dma_buf_map_sgt() for importers that want the DMA
+ * addresses and nothing else: an array of ranges, valid in the DMA address
+ * space of the attached device, with no struct pages and no scatterlist behind
+ * them. The importer reads the size and the ranges of the list and nothing
+ * else, and nothing in it is the importer's to free. The same rules apply as
+ * for dma_buf_map_sgt(): the dma_resv lock is held, the backing storage stays
+ * pinned for as long as the mapping exists, and dynamic importers must wait for
+ * the exclusive fence of the buffer's &dma_resv first.
+ *
+ * Returns: the list, valid until dma_buf_unmap_dma_list(), or an ERR_PTR on
+ * error. May return -EINTR if it is interrupted by a signal.
+ */
+const struct dma_addr_list *
+dma_buf_map_dma_list(struct dma_buf_attachment *attach,
+ enum dma_data_direction direction)
+{
+ struct dma_addr_list *list;
+ long ret;
+
+ might_sleep();
+
+ if (WARN_ON(!attach || !attach->dmabuf))
+ return ERR_PTR(-EINVAL);
+
+ dma_resv_assert_held(attach->dmabuf->resv);
+
+ ret = dma_buf_map_pin(attach);
+ if (ret)
+ return ERR_PTR(ret);
+
+ if (attach->dmabuf->ops->map_dma_shared_buf)
+ list = dma_buf_map_list(attach, direction);
+ else
+ list = dma_buf_sgt_map_dma_list(attach, direction);
+ if (IS_ERR(list))
+ goto error_unpin;
+
+ ret = dma_buf_map_wait(attach);
+ if (ret < 0) {
+ if (attach->dmabuf->ops->map_dma_shared_buf)
+ dma_buf_unmap_list(attach, list, direction);
+ else
+ dma_buf_sgt_unmap_dma_list(attach, list, direction);
+ list = ERR_PTR(ret);
+ goto error_unpin;
+ }
+ return list;
+
+error_unpin:
+ dma_buf_map_unpin(attach);
+ return list;
+}
+EXPORT_SYMBOL_NS_GPL(dma_buf_map_dma_list, "DMA_BUF");
+
+/**
+ * dma_buf_map_dma_list_unlocked - Returns the buffer's DMA addresses for the
+ * attachment's device, without a scatterlist
+ * @attach: [in] attachment whose DMA addresses are to be returned
+ * @direction: [in] direction of DMA transfer
+ *
+ * Unlocked variant of dma_buf_map_dma_list().
+ *
+ * Returns: the list, valid until dma_buf_unmap_dma_list_unlocked(), or an
+ * ERR_PTR on error.
+ */
+const struct dma_addr_list *
+dma_buf_map_dma_list_unlocked(struct dma_buf_attachment *attach,
+ enum dma_data_direction direction)
+{
+ const struct dma_addr_list *list;
+
+ might_sleep();
+
+ if (WARN_ON(!attach || !attach->dmabuf))
+ return ERR_PTR(-EINVAL);
+
+ dma_resv_lock(attach->dmabuf->resv, NULL);
+ list = dma_buf_map_dma_list(attach, direction);
+ dma_resv_unlock(attach->dmabuf->resv);
+
+ return list;
+}
+EXPORT_SYMBOL_NS_GPL(dma_buf_map_dma_list_unlocked, "DMA_BUF");
+
+/**
+ * dma_buf_unmap_dma_list - Release a mapping from dma_buf_map_dma_list()
+ * @attach: [in] attachment to unmap buffer from
+ * @list: [in] the list dma_buf_map_dma_list() returned
+ * @direction: [in] direction of DMA transfer
+ */
+void dma_buf_unmap_dma_list(struct dma_buf_attachment *attach,
+ const struct dma_addr_list *list,
+ enum dma_data_direction direction)
+{
+ /* The list is the core's own: only the importer's view is const */
+ struct dma_addr_list *addrs = (struct dma_addr_list *)list;
+
+ might_sleep();
+
+ if (WARN_ON(!attach || !attach->dmabuf || !list))
+ return;
+
+ dma_resv_assert_held(attach->dmabuf->resv);
+
+ if (attach->dmabuf->ops->map_dma_shared_buf)
+ dma_buf_unmap_list(attach, addrs, direction);
+ else
+ dma_buf_sgt_unmap_dma_list(attach, addrs, direction);
+ dma_buf_map_unpin(attach);
+}
+EXPORT_SYMBOL_NS_GPL(dma_buf_unmap_dma_list, "DMA_BUF");
+
+/**
+ * dma_buf_unmap_dma_list_unlocked - Release a mapping from
+ * dma_buf_map_dma_list_unlocked()
+ * @attach: [in] attachment to unmap buffer from
+ * @list: [in] the list dma_buf_map_dma_list_unlocked() returned
+ * @direction: [in] direction of DMA transfer
+ *
+ * Unlocked variant of dma_buf_unmap_dma_list().
+ */
+void dma_buf_unmap_dma_list_unlocked(struct dma_buf_attachment *attach,
+ const struct dma_addr_list *list,
+ enum dma_data_direction direction)
+{
+ might_sleep();
+
+ if (WARN_ON(!attach || !attach->dmabuf || !list))
+ return;
+
+ dma_resv_lock(attach->dmabuf->resv, NULL);
+ dma_buf_unmap_dma_list(attach, list, direction);
+ dma_resv_unlock(attach->dmabuf->resv);
+}
+EXPORT_SYMBOL_NS_GPL(dma_buf_unmap_dma_list_unlocked, "DMA_BUF");
+
/**
* dma_buf_attach_revocable - check if a DMA-buf importer implements
* revoke semantics.
diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h
index 719296a3d47e..c20dae88bf05 100644
--- a/include/linux/dma-buf.h
+++ b/include/linux/dma-buf.h
@@ -612,6 +612,18 @@ dma_buf_map_sgt_unlocked(struct dma_buf_attachment *attach,
void dma_buf_unmap_sgt_unlocked(struct dma_buf_attachment *attach,
struct sg_table *sg_table,
enum dma_data_direction direction);
+const struct dma_addr_list *
+dma_buf_map_dma_list(struct dma_buf_attachment *attach,
+ enum dma_data_direction direction);
+void dma_buf_unmap_dma_list(struct dma_buf_attachment *attach,
+ const struct dma_addr_list *list,
+ enum dma_data_direction direction);
+const struct dma_addr_list *
+dma_buf_map_dma_list_unlocked(struct dma_buf_attachment *attach,
+ enum dma_data_direction direction);
+void dma_buf_unmap_dma_list_unlocked(struct dma_buf_attachment *attach,
+ const struct dma_addr_list *list,
+ enum dma_data_direction direction);
int dma_buf_mmap(struct dma_buf *, struct vm_area_struct *,
unsigned long);
--
2.54.0
^ permalink raw reply related [flat|nested] 21+ messages in thread* [PATCH RFC 10/16] vfio/pci: Export a dma_shared_buf instead of mapping the buffer
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
` (8 preceding siblings ...)
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 ` Alistair Popple
2026-12-01 7:06 ` [PATCH RFC 11/16] RDMA/uverbs: " Alistair Popple
` (6 subsequent siblings)
16 siblings, 0 replies; 21+ messages in thread
From: Alistair Popple @ 2026-12-01 7:06 UTC (permalink / raw)
To: Karol Wachowski, Sumit Semwal, Christian König,
Philipp Stanner, David Airlie, Simona Vetter, Rodrigo Vivi,
Matthew Brost, Thomas Hellström, Jason Gunthorpe,
Leon Romanovsky, Kees Cook, Gustavo A. R. Silva, Alex Williamson,
Kevin Tian, Joerg Roedel (AMD), Will Deacon, Robin Murphy
Cc: dri-devel, linux-media, linaro-mm-sig, intel-xe, linux-rdma,
linux-kernel, linux-hardening, kvm, iommu, Alexey Kardashevskiy,
Mina Almasry, Baolu Lu, chaitanyak, Dongwon Kim, Felix Kuehling,
jhubbard, Karl Mehltretter, Vivek Kasireddy, Xu Yilun, zhipingz,
Alistair Popple, Jason Gunthorpe
Hand out the BAR's ranges through map_dma_shared_buf() and
unmap_dma_shared_buf() and leave the DMA mapping to the core. The SGT
importers of this buffer get their sg_table from the core, which maps
the same provider and ranges this driver passed to
dma_buf_phys_vec_to_sgt(), and are refused while revoked because
map_dma_shared_buf() is; map_dma_shared_buf() holds the kref for the
life of the dma_shared_buf as the SGT map did for the life of the table.
Nothing changes for RDMA, nvme or the network importers. Attach keeps
refusing importers without allow_peer2peer: this memory is always MMIO,
and failing at attach beats failing at map.
The private iommufd interface stays until iommufd is switched over to
dma_buf_iommu_map().
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/vfio/pci/vfio_pci_dmabuf.c | 39 +++++++++++++++++++++-----------------
1 file changed, 22 insertions(+), 17 deletions(-)
diff --git a/drivers/vfio/pci/vfio_pci_dmabuf.c b/drivers/vfio/pci/vfio_pci_dmabuf.c
index c16f460c01d6..667a31efa922 100644
--- a/drivers/vfio/pci/vfio_pci_dmabuf.c
+++ b/drivers/vfio/pci/vfio_pci_dmabuf.c
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: GPL-2.0-only
/* Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES.
*/
-#include <linux/dma-buf-mapping.h>
+#include <linux/dma-buf.h>
#include <linux/pci-p2pdma.h>
#include <linux/dma-resv.h>
@@ -47,37 +47,42 @@ static void vfio_pci_dma_buf_done(struct kref *kref)
complete(&priv->comp);
}
-static struct sg_table *
-vfio_pci_dma_buf_map(struct dma_buf_attachment *attachment,
- enum dma_data_direction dir)
+static struct dma_shared_buf *
+vfio_pci_dma_buf_map_dma_shared_buf(struct dma_buf_attachment *attachment)
{
struct vfio_pci_dma_buf *priv = attachment->dmabuf->priv;
- struct sg_table *ret;
+ struct dma_shared_buf *shared_buf;
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;
+ shared_buf = kvmalloc(struct_size(shared_buf, ranges, priv->nr_ranges),
+ GFP_KERNEL);
+ if (!shared_buf)
+ return ERR_PTR(-ENOMEM);
+
+ shared_buf->provider = priv->provider;
+ shared_buf->attrs = 0;
+ shared_buf->size = priv->size;
+ shared_buf->nr_ranges = priv->nr_ranges;
+ memcpy(shared_buf->ranges, priv->phys_vec,
+ sizeof(shared_buf->ranges[0]) * priv->nr_ranges);
kref_get(&priv->kref);
- return ret;
+ return shared_buf;
}
-static void vfio_pci_dma_buf_unmap(struct dma_buf_attachment *attachment,
- struct sg_table *sgt,
- enum dma_data_direction dir)
+static void
+vfio_pci_dma_buf_unmap_dma_shared_buf(struct dma_buf_attachment *attachment,
+ struct dma_shared_buf *shared_buf)
{
struct vfio_pci_dma_buf *priv = attachment->dmabuf->priv;
dma_resv_assert_held(priv->dmabuf->resv);
- dma_buf_free_sgt(attachment, sgt, dir);
+ kvfree(shared_buf);
kref_put(&priv->kref, vfio_pci_dma_buf_done);
}
@@ -101,8 +106,8 @@ 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,
+ .map_dma_shared_buf = vfio_pci_dma_buf_map_dma_shared_buf,
+ .unmap_dma_shared_buf = vfio_pci_dma_buf_unmap_dma_shared_buf,
.release = vfio_pci_dma_buf_release,
};
--
2.54.0
^ permalink raw reply related [flat|nested] 21+ messages in thread* [PATCH RFC 11/16] RDMA/uverbs: Export a dma_shared_buf instead of mapping the buffer
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
` (9 preceding siblings ...)
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 ` Alistair Popple
2026-12-01 7:06 ` [PATCH RFC 12/16] dma-buf: Remove dma_buf_phys_vec_to_sgt() and dma_buf_free_sgt() Alistair Popple
` (5 subsequent siblings)
16 siblings, 0 replies; 21+ messages in thread
From: Alistair Popple @ 2026-12-01 7:06 UTC (permalink / raw)
To: Karol Wachowski, Sumit Semwal, Christian König,
Philipp Stanner, David Airlie, Simona Vetter, Rodrigo Vivi,
Matthew Brost, Thomas Hellström, Jason Gunthorpe,
Leon Romanovsky, Kees Cook, Gustavo A. R. Silva, Alex Williamson,
Kevin Tian, Joerg Roedel (AMD), Will Deacon, Robin Murphy
Cc: dri-devel, linux-media, linaro-mm-sig, intel-xe, linux-rdma,
linux-kernel, linux-hardening, kvm, iommu, Alexey Kardashevskiy,
Mina Almasry, Baolu Lu, chaitanyak, Dongwon Kim, Felix Kuehling,
jhubbard, Karl Mehltretter, Vivek Kasireddy, Xu Yilun, zhipingz,
Alistair Popple
Like vfio-pci, the uverbs DMA-buf is a single MMIO range with a p2pdma
provider which is mapped for each importing device by calling
dma_buf_phys_vec_to_sgt() from map_dma_buf(). However the DMA-buf core
now does that mapping for an exporter that hands out a dma_shared_buf.
So hand the range out through map_dma_shared_buf() and
unmap_dma_shared_buf() and let the core build the sg_table for SGT
importers. This removes the last caller of dma_buf_phys_vec_to_sgt().
map_dma_shared_buf() refuses while revoked and holds the kref for the
life of the dma_shared_buf, as the SGT map did for the life of the
sg_table, and attach keeps refusing importers without allow_peer2peer,
so the behaviour an importer sees is unchanged.
Signed-off-by: Alistair Popple <apopple@nvidia.com>
---
drivers/infiniband/core/uverbs_std_types_dmabuf.c | 37 ++++++++++++-----------
1 file changed, 20 insertions(+), 17 deletions(-)
diff --git a/drivers/infiniband/core/uverbs_std_types_dmabuf.c b/drivers/infiniband/core/uverbs_std_types_dmabuf.c
index 8d8c997a4883..10d7ae9918ef 100644
--- a/drivers/infiniband/core/uverbs_std_types_dmabuf.c
+++ b/drivers/infiniband/core/uverbs_std_types_dmabuf.c
@@ -3,7 +3,7 @@
* Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved
*/
-#include <linux/dma-buf-mapping.h>
+#include <linux/dma-buf.h>
#include <linux/pci-p2pdma.h>
#include <linux/dma-resv.h>
#include <rdma/uverbs_std_types.h>
@@ -21,36 +21,39 @@ static int uverbs_dmabuf_attach(struct dma_buf *dmabuf,
return 0;
}
-static struct sg_table *
-uverbs_dmabuf_map(struct dma_buf_attachment *attachment,
- enum dma_data_direction dir)
+static struct dma_shared_buf *
+uverbs_dmabuf_map_dma_shared_buf(struct dma_buf_attachment *attachment)
{
struct ib_uverbs_dmabuf_file *priv = attachment->dmabuf->priv;
- struct sg_table *ret;
+ struct dma_shared_buf *shared_buf;
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;
+ shared_buf = kmalloc(struct_size(shared_buf, ranges, 1), GFP_KERNEL);
+ if (!shared_buf)
+ return ERR_PTR(-ENOMEM);
+
+ shared_buf->provider = priv->provider;
+ shared_buf->attrs = 0;
+ shared_buf->size = priv->phys_vec.len;
+ shared_buf->nr_ranges = 1;
+ shared_buf->ranges[0] = priv->phys_vec;
kref_get(&priv->kref);
- return ret;
+ return shared_buf;
}
-static void uverbs_dmabuf_unmap(struct dma_buf_attachment *attachment,
- struct sg_table *sgt,
- enum dma_data_direction dir)
+static void
+uverbs_dmabuf_unmap_dma_shared_buf(struct dma_buf_attachment *attachment,
+ struct dma_shared_buf *shared_buf)
{
struct ib_uverbs_dmabuf_file *priv = attachment->dmabuf->priv;
dma_resv_assert_held(attachment->dmabuf->resv);
- dma_buf_free_sgt(attachment, sgt, dir);
+ kfree(shared_buf);
kref_put(&priv->kref, ib_uverbs_dmabuf_done);
}
@@ -97,8 +100,8 @@ 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,
+ .map_dma_shared_buf = uverbs_dmabuf_map_dma_shared_buf,
+ .unmap_dma_shared_buf = uverbs_dmabuf_unmap_dma_shared_buf,
.pin = uverbs_dmabuf_pin,
.unpin = uverbs_dmabuf_unpin,
.release = uverbs_dmabuf_release,
--
2.54.0
^ permalink raw reply related [flat|nested] 21+ messages in thread* [PATCH RFC 12/16] dma-buf: Remove dma_buf_phys_vec_to_sgt() and dma_buf_free_sgt()
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
` (10 preceding siblings ...)
2026-12-01 7:06 ` [PATCH RFC 11/16] RDMA/uverbs: " Alistair Popple
@ 2026-12-01 7:06 ` Alistair Popple
2026-12-01 7:06 ` [PATCH RFC 13/16] iommufd: Map DMA-bufs through dma_buf_iommu_map() Alistair Popple
` (4 subsequent siblings)
16 siblings, 0 replies; 21+ messages in thread
From: Alistair Popple @ 2026-12-01 7:06 UTC (permalink / raw)
To: Karol Wachowski, Sumit Semwal, Christian König,
Philipp Stanner, David Airlie, Simona Vetter, Rodrigo Vivi,
Matthew Brost, Thomas Hellström, Jason Gunthorpe,
Leon Romanovsky, Kees Cook, Gustavo A. R. Silva, Alex Williamson,
Kevin Tian, Joerg Roedel (AMD), Will Deacon, Robin Murphy
Cc: dri-devel, linux-media, linaro-mm-sig, intel-xe, linux-rdma,
linux-kernel, linux-hardening, kvm, iommu, Alexey Kardashevskiy,
Mina Almasry, Baolu Lu, chaitanyak, Dongwon Kim, Felix Kuehling,
jhubbard, Karl Mehltretter, Vivek Kasireddy, Xu Yilun, zhipingz,
Alistair Popple
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
^ permalink raw reply related [flat|nested] 21+ messages in thread* [PATCH RFC 13/16] iommufd: Map DMA-bufs through dma_buf_iommu_map()
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
` (11 preceding siblings ...)
2026-12-01 7:06 ` [PATCH RFC 12/16] dma-buf: Remove dma_buf_phys_vec_to_sgt() and dma_buf_free_sgt() Alistair Popple
@ 2026-12-01 7:06 ` Alistair Popple
2026-12-01 7:28 ` sashiko-bot
2026-12-01 7:06 ` [PATCH RFC 14/16] vfio/pci: Remove vfio_pci_dma_buf_iommufd_map() Alistair Popple
` (3 subsequent siblings)
16 siblings, 1 reply; 21+ messages in thread
From: Alistair Popple @ 2026-12-01 7:06 UTC (permalink / raw)
To: Karol Wachowski, Sumit Semwal, Christian König,
Philipp Stanner, David Airlie, Simona Vetter, Rodrigo Vivi,
Matthew Brost, Thomas Hellström, Jason Gunthorpe,
Leon Romanovsky, Kees Cook, Gustavo A. R. Silva, Alex Williamson,
Kevin Tian, Joerg Roedel (AMD), Will Deacon, Robin Murphy
Cc: dri-devel, linux-media, linaro-mm-sig, intel-xe, linux-rdma,
linux-kernel, linux-hardening, kvm, iommu, Alexey Kardashevskiy,
Mina Almasry, Baolu Lu, chaitanyak, Dongwon Kim, Felix Kuehling,
jhubbard, Karl Mehltretter, Vivek Kasireddy, Xu Yilun, zhipingz,
Alistair Popple
Switch iommufd to using dma-shared-buf to program the iommu domains.
iommufd is the only importer permitted to use dma_buf_iommu_map(), which
the module restriction on the exports enforces.
Only an exporter handing out a dma_shared_buf can be mapped this way so
IOMMU_IOAS_MAP_FILE refuses any other buffer up front with
dma_buf_iommu_mappable(), the same way the private vfio interface failed
for anything but a vfio DMA-buf. Filling a domain no longer reads
addresses through the pfn reader. Instead the area hands
dma_buf_iommu_map() its domain, IOVA and window into the buffer and the
core has the dma_shared_buf programmed, which also makes exporters with
more than one range work.
Unfilling and revoke go through dma_buf_iommu_unmap(). iommufd
keeps no address at all, only a revoked flag that the exporter's
invalidate_mappings() sets.
Signed-off-by: Alistair Popple <apopple@nvidia.com>
---
drivers/iommu/iommufd/io_pagetable.c | 1 +
drivers/iommu/iommufd/io_pagetable.h | 27 +++-
drivers/iommu/iommufd/iommufd_private.h | 8 --
drivers/iommu/iommufd/pages.c | 219 ++++++++++++++------------------
drivers/iommu/iommufd/selftest.c | 66 +++++-----
5 files changed, 158 insertions(+), 163 deletions(-)
diff --git a/drivers/iommu/iommufd/io_pagetable.c b/drivers/iommu/iommufd/io_pagetable.c
index 5ac736cf5bb5..fc0c49a9ed76 100644
--- a/drivers/iommu/iommufd/io_pagetable.c
+++ b/drivers/iommu/iommufd/io_pagetable.c
@@ -1145,6 +1145,7 @@ static int iopt_fill_domain(struct io_pagetable *iopt,
if (!pages)
continue;
+ guard(iopt_dmabuf_resv)(pages);
guard(mutex)(&pages->mutex);
if (iopt_is_dmabuf(pages)) {
rc = iopt_dmabuf_track_domain(pages, area, domain);
diff --git a/drivers/iommu/iommufd/io_pagetable.h b/drivers/iommu/iommufd/io_pagetable.h
index 27e3e311d395..f8d9523e896d 100644
--- a/drivers/iommu/iommufd/io_pagetable.h
+++ b/drivers/iommu/iommufd/io_pagetable.h
@@ -6,6 +6,7 @@
#define __IO_PAGETABLE_H
#include <linux/dma-buf.h>
+#include <linux/dma-resv.h>
#include <linux/interval_tree.h>
#include <linux/kref.h>
#include <linux/mutex.h>
@@ -202,7 +203,8 @@ struct iopt_pages_dmabuf_track {
struct iopt_pages_dmabuf {
struct dma_buf_attachment *attach;
- struct phys_vec phys;
+ /* Set by the exporter's invalidate_mappings(), never cleared */
+ bool revoked;
/* Always PAGE_SIZE aligned */
unsigned long start;
struct list_head tracker;
@@ -259,10 +261,31 @@ static inline bool iopt_dmabuf_revoked(struct iopt_pages *pages)
{
lockdep_assert_held(&pages->mutex);
if (iopt_is_dmabuf(pages))
- return pages->dmabuf.phys.len == 0;
+ return pages->dmabuf.revoked;
return false;
}
+/*
+ * Filling a domain from a dmabuf calls into the exporter, which needs the
+ * dma_resv lock. It nests outside pages->mutex, the order iopt_map_dmabuf()
+ * establishes, and the exporter's revoke takes the same two locks in the same
+ * order, so a fill never races a revoke.
+ */
+static inline void iopt_dmabuf_resv_lock(struct iopt_pages *pages)
+{
+ if (iopt_is_dmabuf(pages))
+ dma_resv_lock(pages->dmabuf.attach->dmabuf->resv, NULL);
+}
+
+static inline void iopt_dmabuf_resv_unlock(struct iopt_pages *pages)
+{
+ if (iopt_is_dmabuf(pages))
+ dma_resv_unlock(pages->dmabuf.attach->dmabuf->resv);
+}
+
+DEFINE_GUARD(iopt_dmabuf_resv, struct iopt_pages *, iopt_dmabuf_resv_lock(_T),
+ iopt_dmabuf_resv_unlock(_T))
+
struct iopt_pages *iopt_alloc_user_pages(void __user *uptr,
unsigned long length, bool writable);
struct iopt_pages *iopt_alloc_file_pages(struct file *file,
diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h
index eb2e85b27e42..6ffc0c22ccfc 100644
--- a/drivers/iommu/iommufd/iommufd_private.h
+++ b/drivers/iommu/iommufd/iommufd_private.h
@@ -739,8 +739,6 @@ bool iommufd_should_fail(void);
int __init iommufd_test_init(void);
void iommufd_test_exit(void);
bool iommufd_selftest_is_mock_dev(struct device *dev);
-int iommufd_test_dma_buf_iommufd_map(struct dma_buf_attachment *attachment,
- struct phys_vec *phys);
#else
static inline void iommufd_test_syz_conv_iova_id(struct iommufd_ucmd *ucmd,
unsigned int ioas_id,
@@ -762,11 +760,5 @@ static inline bool iommufd_selftest_is_mock_dev(struct device *dev)
{
return false;
}
-static inline int
-iommufd_test_dma_buf_iommufd_map(struct dma_buf_attachment *attachment,
- struct phys_vec *phys)
-{
- return -EOPNOTSUPP;
-}
#endif
#endif
diff --git a/drivers/iommu/iommufd/pages.c b/drivers/iommu/iommufd/pages.c
index fcdbde007a9a..aeac509e574f 100644
--- a/drivers/iommu/iommufd/pages.c
+++ b/drivers/iommu/iommufd/pages.c
@@ -46,6 +46,7 @@
* ULONG_MAX so last_index + 1 cannot overflow.
*/
#include <linux/dma-buf.h>
+#include <linux/dma-buf-mapping.h>
#include <linux/dma-resv.h>
#include <linux/file.h>
#include <linux/highmem.h>
@@ -55,7 +56,6 @@
#include <linux/overflow.h>
#include <linux/slab.h>
#include <linux/sched/mm.h>
-#include <linux/vfio_pci_core.h>
#include "double_span.h"
#include "io_pagetable.h"
@@ -250,6 +250,70 @@ static void iopt_area_unmap_domain_range(struct iopt_area *area,
start_iova + 1);
}
+/* The dmabuf version of iopt_area_unmap_domain_range() */
+static void iopt_dmabuf_unmap_domain_range(struct iopt_area *area,
+ struct iopt_pages *pages,
+ struct iommu_domain *domain,
+ unsigned long start_index,
+ unsigned long last_index)
+{
+ unsigned long start_iova = iopt_area_index_to_iova(area, start_index);
+
+ dma_buf_iommu_unmap(pages->dmabuf.attach, domain, start_iova,
+ iopt_area_index_to_iova_last(area, last_index) -
+ start_iova + 1);
+}
+
+/*
+ * Map the whole area into @domain from the dmabuf. The core does the walk
+ * over the exporter's dma_shared_buf; this only hands it the window of the
+ * buffer the area covers. Requires the dma_resv lock, see
+ * iopt_dmabuf_resv_lock().
+ */
+static int iopt_area_fill_domain_dmabuf(struct iopt_area *area,
+ struct iopt_pages *pages,
+ struct iommu_domain *domain)
+{
+ unsigned long iova = iopt_area_iova(area);
+
+ dma_resv_assert_held(pages->dmabuf.attach->dmabuf->resv);
+ return dma_buf_iommu_map(pages->dmabuf.attach, domain, iova,
+ pages->dmabuf.start +
+ iopt_area_start_byte(area, iova),
+ iopt_area_length(area), area->iommu_prot,
+ GFP_KERNEL_ACCOUNT);
+}
+
+/* Map the whole area into every domain of its iopt, or into none of them */
+static int iopt_area_fill_domains_dmabuf(struct iopt_area *area,
+ struct iopt_pages *pages)
+{
+ struct iommu_domain *domain;
+ unsigned long unmap_index;
+ unsigned long index;
+ int rc;
+
+ if (iopt_dmabuf_revoked(pages))
+ return 0;
+
+ xa_for_each(&area->iopt->domains, index, domain) {
+ rc = iopt_area_fill_domain_dmabuf(area, pages, domain);
+ if (rc)
+ goto err_unmap;
+ }
+ return 0;
+
+err_unmap:
+ xa_for_each(&area->iopt->domains, unmap_index, domain) {
+ if (unmap_index >= index)
+ break;
+ iopt_dmabuf_unmap_domain_range(area, pages, domain,
+ iopt_area_index(area),
+ iopt_area_last_index(area));
+ }
+ return rc;
+}
+
static struct iopt_area *iopt_pages_find_domain_area(struct iopt_pages *pages,
unsigned long index)
{
@@ -261,11 +325,6 @@ static struct iopt_area *iopt_pages_find_domain_area(struct iopt_pages *pages,
return container_of(node, struct iopt_area, pages_node);
}
-enum batch_kind {
- BATCH_CPU_MEMORY = 0,
- BATCH_MMIO,
-};
-
/*
* A simple datastructure to hold a vector of PFNs, optimized for contiguous
* PFNs. This is used as a temporary holding memory for shuttling pfns from one
@@ -279,7 +338,6 @@ struct pfn_batch {
unsigned int array_size;
unsigned int end;
unsigned int total_pfns;
- enum batch_kind kind;
};
enum { MAX_NPFNS = type_max(typeof(((struct pfn_batch *)0)->npfns[0])) };
@@ -289,7 +347,6 @@ static void batch_clear(struct pfn_batch *batch)
batch->end = 0;
batch->pfns[0] = 0;
batch->npfns[0] = 0;
- batch->kind = 0;
}
/*
@@ -359,17 +416,10 @@ static void batch_destroy(struct pfn_batch *batch, void *backup)
}
static bool batch_add_pfn_num(struct pfn_batch *batch, unsigned long pfn,
- u32 nr, enum batch_kind kind)
+ u32 nr)
{
unsigned int end = batch->end;
- if (batch->kind != kind) {
- /* One kind per batch */
- if (batch->end != 0)
- return false;
- batch->kind = kind;
- }
-
if (end && pfn == batch->pfns[end - 1] + batch->npfns[end - 1] &&
nr <= MAX_NPFNS - batch->npfns[end - 1]) {
batch->npfns[end - 1] += nr;
@@ -396,7 +446,7 @@ static void batch_remove_pfn_num(struct pfn_batch *batch, unsigned long nr)
/* true if the pfn was added, false otherwise */
static bool batch_add_pfn(struct pfn_batch *batch, unsigned long pfn)
{
- return batch_add_pfn_num(batch, pfn, 1, BATCH_CPU_MEMORY);
+ return batch_add_pfn_num(batch, pfn, 1);
}
/*
@@ -517,11 +567,6 @@ static int batch_to_domain(struct pfn_batch *batch, struct iommu_domain *domain,
unsigned long iova;
int rc;
- if (batch->kind == BATCH_MMIO) {
- iommu_prot &= ~IOMMU_CACHE;
- iommu_prot |= IOMMU_MMIO;
- }
-
/* The first index might be a partial page */
if (start_index == iopt_area_index(area))
page_offset = area->page_offset;
@@ -675,7 +720,7 @@ static int batch_from_folios(struct pfn_batch *batch, struct folio ***folios_p,
nr = min(nr, npages);
npages -= nr;
- if (!batch_add_pfn_num(batch, pfn, nr, BATCH_CPU_MEMORY))
+ if (!batch_add_pfn_num(batch, pfn, nr))
break;
if (nr > 1) {
rc = folio_add_pins(folio, nr - 1);
@@ -1084,41 +1129,6 @@ static int pfn_reader_user_update_pinned(struct pfn_reader_user *user,
return iopt_pages_update_pinned(pages, npages, inc, user);
}
-struct pfn_reader_dmabuf {
- struct phys_vec phys;
- unsigned long start_offset;
-};
-
-static int pfn_reader_dmabuf_init(struct pfn_reader_dmabuf *dmabuf,
- struct iopt_pages *pages)
-{
- /* Callers must not get here if the dmabuf was already revoked */
- if (WARN_ON(iopt_dmabuf_revoked(pages)))
- return -EINVAL;
-
- dmabuf->phys = pages->dmabuf.phys;
- dmabuf->start_offset = pages->dmabuf.start;
- return 0;
-}
-
-static int pfn_reader_fill_dmabuf(struct pfn_reader_dmabuf *dmabuf,
- struct pfn_batch *batch,
- unsigned long start_index,
- unsigned long last_index)
-{
- unsigned long start = dmabuf->start_offset + start_index * PAGE_SIZE;
-
- /*
- * start/last_index and start are all PAGE_SIZE aligned, the batch is
- * always filled using page size aligned PFNs just like the other types.
- * If the dmabuf has been sliced on a sub page offset then the common
- * batch to domain code will adjust it before mapping to the domain.
- */
- batch_add_pfn_num(batch, PHYS_PFN(dmabuf->phys.paddr + start),
- last_index - start_index + 1, BATCH_MMIO);
- return 0;
-}
-
/*
* PFNs are stored in three places, in order of preference:
* - The iopt_pages xarray. This is only populated if there is a
@@ -1137,10 +1147,7 @@ struct pfn_reader {
unsigned long batch_end_index;
unsigned long last_index;
- union {
- struct pfn_reader_user user;
- struct pfn_reader_dmabuf dmabuf;
- };
+ struct pfn_reader_user user;
};
static int pfn_reader_update_pinned(struct pfn_reader *pfns)
@@ -1185,15 +1192,6 @@ static int pfn_reader_fill_span(struct pfn_reader *pfns)
WARN_ON(span->last_used < start_index))
return -EINVAL;
- /*
- * Always read a dmabuf from its phys, even where a domain already
- * maps it: a domain can only report CPU memory PFNs, losing
- * BATCH_MMIO. last_hole aliases last_used, so this covers any span.
- */
- if (iopt_is_dmabuf(pfns->pages))
- return pfn_reader_fill_dmabuf(&pfns->dmabuf, &pfns->batch,
- start_index, span->last_hole);
-
if (span->is_used == 1) {
batch_from_xarray(&pfns->batch, &pfns->pages->pinned_pfns,
start_index, span->last_used);
@@ -1283,14 +1281,15 @@ static int pfn_reader_init(struct pfn_reader *pfns, struct iopt_pages *pages,
lockdep_assert_held(&pages->mutex);
+ /* dmabufs are mapped by the dma-buf core, not read as PFNs */
+ if (WARN_ON(iopt_is_dmabuf(pages)))
+ return -EINVAL;
+
pfns->pages = pages;
pfns->batch_start_index = start_index;
pfns->batch_end_index = start_index;
pfns->last_index = last_index;
- if (iopt_is_dmabuf(pages))
- pfn_reader_dmabuf_init(&pfns->dmabuf, pages);
- else
- pfn_reader_user_init(&pfns->user, pages);
+ pfn_reader_user_init(&pfns->user, pages);
rc = batch_init(&pfns->batch, last_index - start_index + 1);
if (rc)
return rc;
@@ -1311,12 +1310,8 @@ static int pfn_reader_init(struct pfn_reader *pfns, struct iopt_pages *pages,
static void pfn_reader_release_pins(struct pfn_reader *pfns)
{
struct iopt_pages *pages = pfns->pages;
- struct pfn_reader_user *user;
-
- if (iopt_is_dmabuf(pages))
- return;
+ struct pfn_reader_user *user = &pfns->user;
- user = &pfns->user;
if (user->upages_end > pfns->batch_end_index) {
/* Any pages not transferred to the batch are just unpinned */
@@ -1346,8 +1341,7 @@ static void pfn_reader_destroy(struct pfn_reader *pfns)
struct iopt_pages *pages = pfns->pages;
pfn_reader_release_pins(pfns);
- if (!iopt_is_dmabuf(pfns->pages))
- pfn_reader_user_destroy(&pfns->user, pfns->pages);
+ pfn_reader_user_destroy(&pfns->user, pfns->pages);
batch_destroy(&pfns->batch, NULL);
WARN_ON(pages->last_npinned != pages->npinned);
}
@@ -1455,11 +1449,11 @@ static void iopt_revoke_notify(struct dma_buf_attachment *attach)
list_for_each_entry(track, &pages->dmabuf.tracker, elm) {
struct iopt_area *area = track->area;
- iopt_area_unmap_domain_range(area, track->domain,
- iopt_area_index(area),
- iopt_area_last_index(area));
+ iopt_dmabuf_unmap_domain_range(area, pages, track->domain,
+ iopt_area_index(area),
+ iopt_area_last_index(area));
}
- pages->dmabuf.phys.len = 0;
+ pages->dmabuf.revoked = true;
}
static const struct dma_buf_attach_ops iopt_dmabuf_attach_revoke_ops = {
@@ -1467,38 +1461,16 @@ static const struct dma_buf_attach_ops iopt_dmabuf_attach_revoke_ops = {
.invalidate_mappings = iopt_revoke_notify,
};
-/*
- * iommufd and vfio have a circular dependency. Future work for a phys
- * based private interconnect will remove this.
- */
-static int
-sym_vfio_pci_dma_buf_iommufd_map(struct dma_buf_attachment *attachment,
- struct phys_vec *phys)
-{
- typeof(&vfio_pci_dma_buf_iommufd_map) fn;
- int rc;
-
- rc = iommufd_test_dma_buf_iommufd_map(attachment, phys);
- if (rc != -EOPNOTSUPP)
- return rc;
-
- if (!IS_ENABLED(CONFIG_VFIO_PCI_DMABUF))
- return -EOPNOTSUPP;
-
- fn = symbol_get(vfio_pci_dma_buf_iommufd_map);
- if (!fn)
- return -EOPNOTSUPP;
- rc = fn(attachment, phys);
- symbol_put(vfio_pci_dma_buf_iommufd_map);
- return rc;
-}
-
static int iopt_map_dmabuf(struct iommufd_ctx *ictx, struct iopt_pages *pages,
struct dma_buf *dmabuf)
{
struct dma_buf_attachment *attach;
int rc;
+ /* Fail at import rather than when a domain is first filled */
+ if (!dma_buf_iommu_mappable(dmabuf))
+ return -EOPNOTSUPP;
+
attach = dma_buf_dynamic_attach(dmabuf, iommufd_global_device(),
&iopt_dmabuf_attach_revoke_ops, pages);
if (IS_ERR(attach))
@@ -1518,18 +1490,12 @@ static int iopt_map_dmabuf(struct iommufd_ctx *ictx, struct iopt_pages *pages,
if (rc)
goto err_detach;
- rc = sym_vfio_pci_dma_buf_iommufd_map(attach, &pages->dmabuf.phys);
- if (rc)
- goto err_unpin;
-
dma_resv_unlock(dmabuf->resv);
/* On success iopt_release_pages() will detach and put the dmabuf. */
pages->dmabuf.attach = attach;
return 0;
-err_unpin:
- dma_buf_unpin(attach);
err_detach:
dma_resv_unlock(dmabuf->resv);
dma_buf_detach(dmabuf, attach);
@@ -1764,8 +1730,8 @@ static void __iopt_area_unfill_domain(struct iopt_area *area,
if (iopt_is_dmabuf(pages)) {
if (WARN_ON(iopt_dmabuf_revoked(pages)))
return;
- iopt_area_unmap_domain_range(area, domain, start_index,
- last_index);
+ iopt_dmabuf_unmap_domain_range(area, pages, domain, start_index,
+ last_index);
return;
}
@@ -1870,6 +1836,9 @@ int iopt_area_fill_domain(struct iopt_area *area, struct iommu_domain *domain)
if (iopt_dmabuf_revoked(area->pages))
return 0;
+ if (iopt_is_dmabuf(area->pages))
+ return iopt_area_fill_domain_dmabuf(area, area->pages, domain);
+
rc = pfn_reader_first(&pfns, area->pages, iopt_area_index(area),
iopt_area_last_index(area));
if (rc)
@@ -1928,14 +1897,16 @@ int iopt_area_fill_domains(struct iopt_area *area, struct iopt_pages *pages)
if (xa_empty(&area->iopt->domains))
return 0;
+ iopt_dmabuf_resv_lock(pages);
mutex_lock(&pages->mutex);
if (iopt_is_dmabuf(pages)) {
rc = iopt_dmabuf_track_all_domains(area, pages);
if (rc)
goto out_unlock;
- }
-
- if (!iopt_dmabuf_revoked(pages)) {
+ rc = iopt_area_fill_domains_dmabuf(area, pages);
+ if (rc)
+ goto out_untrack;
+ } else {
rc = pfn_reader_first(&pfns, pages, iopt_area_index(area),
iopt_area_last_index(area));
if (rc)
@@ -1966,6 +1937,7 @@ int iopt_area_fill_domains(struct iopt_area *area, struct iopt_pages *pages)
area->storage_domain = xa_load(&area->iopt->domains, 0);
interval_tree_insert(&area->pages_node, &pages->domains_itree);
mutex_unlock(&pages->mutex);
+ iopt_dmabuf_resv_unlock(pages);
return 0;
out_unmap:
@@ -1999,6 +1971,7 @@ int iopt_area_fill_domains(struct iopt_area *area, struct iopt_pages *pages)
iopt_dmabuf_untrack_all_domains(area, pages);
out_unlock:
mutex_unlock(&pages->mutex);
+ iopt_dmabuf_resv_unlock(pages);
return rc;
}
diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c
index 25387969ab75..f0ba2624f7b3 100644
--- a/drivers/iommu/iommufd/selftest.c
+++ b/drivers/iommu/iommufd/selftest.c
@@ -1959,9 +1959,14 @@ struct iommufd_test_dma_buf {
bool revoked;
};
+/* Like vfio, a revoked buffer refuses new importers at attach */
static int iommufd_test_dma_buf_attach(struct dma_buf *dmabuf,
struct dma_buf_attachment *attachment)
{
+ struct iommufd_test_dma_buf *priv = dmabuf->priv;
+
+ if (priv->revoked)
+ return -ENODEV;
return 0;
}
@@ -1970,19 +1975,6 @@ static void iommufd_test_dma_buf_detach(struct dma_buf *dmabuf,
{
}
-static struct sg_table *
-iommufd_test_dma_buf_map(struct dma_buf_attachment *attachment,
- enum dma_data_direction dir)
-{
- return ERR_PTR(-EOPNOTSUPP);
-}
-
-static void iommufd_test_dma_buf_unmap(struct dma_buf_attachment *attachment,
- struct sg_table *sgt,
- enum dma_data_direction dir)
-{
-}
-
static void iommufd_test_dma_buf_release(struct dma_buf *dmabuf)
{
struct iommufd_test_dma_buf *priv = dmabuf->priv;
@@ -1991,32 +1983,46 @@ static void iommufd_test_dma_buf_release(struct dma_buf *dmabuf)
kfree(priv);
}
-static const struct dma_buf_ops iommufd_test_dmabuf_ops = {
- .attach = iommufd_test_dma_buf_attach,
- .detach = iommufd_test_dma_buf_detach,
- .map_dma_buf = iommufd_test_dma_buf_map,
- .release = iommufd_test_dma_buf_release,
- .unmap_dma_buf = iommufd_test_dma_buf_unmap,
-};
-
-int iommufd_test_dma_buf_iommufd_map(struct dma_buf_attachment *attachment,
- struct phys_vec *phys)
+static struct dma_shared_buf *
+iommufd_test_dma_buf_map_dma_shared_buf(struct dma_buf_attachment *attachment)
{
struct iommufd_test_dma_buf *priv = attachment->dmabuf->priv;
+ struct dma_shared_buf *shared_buf;
dma_resv_assert_held(attachment->dmabuf->resv);
- if (attachment->dmabuf->ops != &iommufd_test_dmabuf_ops)
- return -EOPNOTSUPP;
-
if (priv->revoked)
- return -ENODEV;
+ return ERR_PTR(-ENODEV);
- phys->paddr = virt_to_phys(priv->memory);
- phys->len = priv->length;
- return 0;
+ shared_buf = kvmalloc(struct_size(shared_buf, ranges, 1), GFP_KERNEL);
+ if (!shared_buf)
+ return ERR_PTR(-ENOMEM);
+
+ /* System memory: no provider */
+ shared_buf->provider = NULL;
+ shared_buf->attrs = 0;
+ shared_buf->size = priv->length;
+ shared_buf->nr_ranges = 1;
+ shared_buf->ranges[0].paddr = virt_to_phys(priv->memory);
+ shared_buf->ranges[0].len = priv->length;
+ return shared_buf;
+}
+
+static void
+iommufd_test_dma_buf_unmap_dma_shared_buf(struct dma_buf_attachment *attach,
+ struct dma_shared_buf *shared_buf)
+{
+ kvfree(shared_buf);
}
+static const struct dma_buf_ops iommufd_test_dmabuf_ops = {
+ .attach = iommufd_test_dma_buf_attach,
+ .detach = iommufd_test_dma_buf_detach,
+ .release = iommufd_test_dma_buf_release,
+ .map_dma_shared_buf = iommufd_test_dma_buf_map_dma_shared_buf,
+ .unmap_dma_shared_buf = iommufd_test_dma_buf_unmap_dma_shared_buf,
+};
+
static int iommufd_test_dmabuf_get(struct iommufd_ucmd *ucmd,
unsigned int open_flags,
size_t len)
--
2.54.0
^ permalink raw reply related [flat|nested] 21+ messages in thread* Re: [PATCH RFC 13/16] iommufd: Map DMA-bufs through dma_buf_iommu_map()
2026-12-01 7:06 ` [PATCH RFC 13/16] iommufd: Map DMA-bufs through dma_buf_iommu_map() Alistair Popple
@ 2026-12-01 7:28 ` sashiko-bot
0 siblings, 0 replies; 21+ messages in thread
From: sashiko-bot @ 2026-12-01 7:28 UTC (permalink / raw)
To: Alistair Popple; +Cc: dri-devel, intel-xe, linux-rdma, kvm, media-ci
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] iommufd: missed revoke race during attach in
iopt_map_dmabuf()
- [Low] iommufd: multi-domain unfill paths bypass dma_buf_iommu_unmap()
--
commit e899ecc3d9089496a227965c5bb36a21b049566a
Author: Alistair Popple <apopple@nvidia.com>
iommufd: Map DMA-bufs through dma_buf_iommu_map()
This commit switches iommufd to map and unmap DMA-bufs in IOMMU domains
through dma_buf_iommu_map() and dma_buf_iommu_unmap() instead of reading
physical addresses through the PFN reader.
> diff --git a/drivers/iommu/iommufd/pages.c b/drivers/iommu/iommufd/pages.c
> index fcdbde007a9ab..aeac509e574fe 100644
> --- a/drivers/iommu/iommufd/pages.c
> +++ b/drivers/iommu/iommufd/pages.c
[ ... ]
> @@ -1467,38 +1461,16 @@ static const struct dma_buf_attach_ops iopt_dmabuf_attach_revoke_ops = {
> .invalidate_mappings = iopt_revoke_notify,
> };
>
> -/*
> - * iommufd and vfio have a circular dependency. Future work for a phys
> - * based private interconnect will remove this.
> - */
> -static int
> -sym_vfio_pci_dma_buf_iommufd_map(struct dma_buf_attachment *attachment,
> - struct phys_vec *phys)
> -{
> - typeof(&vfio_pci_dma_buf_iommufd_map) fn;
> - int rc;
> -
> - rc = iommufd_test_dma_buf_iommufd_map(attachment, phys);
> - if (rc != -EOPNOTSUPP)
> - return rc;
> -
> - if (!IS_ENABLED(CONFIG_VFIO_PCI_DMABUF))
> - return -EOPNOTSUPP;
> -
> - fn = symbol_get(vfio_pci_dma_buf_iommufd_map);
> - if (!fn)
> - return -EOPNOTSUPP;
> - rc = fn(attachment, phys);
> - symbol_put(vfio_pci_dma_buf_iommufd_map);
> - return rc;
> -}
> -
> static int iopt_map_dmabuf(struct iommufd_ctx *ictx, struct iopt_pages *pages,
> struct dma_buf *dmabuf)
> {
> struct dma_buf_attachment *attach;
> int rc;
>
> + /* Fail at import rather than when a domain is first filled */
> + if (!dma_buf_iommu_mappable(dmabuf))
> + return -EOPNOTSUPP;
> +
> attach = dma_buf_dynamic_attach(dmabuf, iommufd_global_device(),
> &iopt_dmabuf_attach_revoke_ops, pages);
> if (IS_ERR(attach))
> @@ -1518,18 +1490,12 @@ static int iopt_map_dmabuf(struct iommufd_ctx *ictx, struct iopt_pages *pages,
> if (rc)
> goto err_detach;
>
> - rc = sym_vfio_pci_dma_buf_iommufd_map(attach, &pages->dmabuf.phys);
> - if (rc)
> - goto err_unpin;
> -
> dma_resv_unlock(dmabuf->resv);
>
> /* On success iopt_release_pages() will detach and put the dmabuf. */
> pages->dmabuf.attach = attach;
> return 0;
[Severity: Medium]
Can iopt_map_dmabuf() miss a concurrent revoke and leave
pages->dmabuf.revoked as false on a revoked buffer?
Previously, sym_vfio_pci_dma_buf_iommufd_map() checked priv->revoked under
dmabuf->resv after dma_buf_dynamic_attach() had added attach to
dmabuf->attachments. Now the only check during import is in
dmabuf->ops->attach(), which dma_buf_dynamic_attach() calls before taking
dmabuf->resv and adding attach to dmabuf->attachments, while dma_buf_pin()
is a no-op for exporters without .pin (such as vfio_pci_dmabuf_ops and
iommufd_test_dmabuf_ops):
CPU0 CPU1
iopt_map_dmabuf()
dma_buf_dynamic_attach()
ops->attach()
if (priv->revoked) // false
vfio_pci_dma_buf_move() /
iommufd_test_dmabuf_revoke()
dma_resv_lock(dmabuf->resv, ...)
priv->revoked = true;
dma_buf_invalidate_mappings()
// attach not in attachments yet
dma_resv_unlock(dmabuf->resv)
dma_resv_lock(dmabuf->resv, ...)
list_add(&attach->node,
&dmabuf->attachments)
dma_resv_unlock(dmabuf->resv)
...
// succeeds with pages->dmabuf.revoked == false
If the IOAS has no domains attached yet, iopt_area_fill_domains() returns 0
early when xa_empty(&area->iopt->domains) is true, so IOMMU_IOAS_MAP_FILE
succeeds with pages->dmabuf.revoked left as false. When a domain is later
attached via iopt_table_add_domain() -> iopt_fill_domain(),
iopt_area_fill_domain() sees iopt_dmabuf_revoked() == false and calls
dma_buf_iommu_map(), failing the domain attach with -ENODEV instead of
skipping the revoked DMA-buf (or allowing a later mapping if the exporter
re-enables access after a revoke that should have been permanent).
> @@ -1764,8 +1730,8 @@ static void __iopt_area_unfill_domain(struct iopt_area *area,
> if (iopt_is_dmabuf(pages)) {
> if (WARN_ON(iopt_dmabuf_revoked(pages)))
> return;
> - iopt_area_unmap_domain_range(area, domain, start_index,
> - last_index);
> + iopt_dmabuf_unmap_domain_range(area, pages, domain, start_index,
> + last_index);
> return;
> }
[Severity: Low]
Should the multi-domain unfill paths in iopt_unfill_domain() and
iopt_area_unfill_domains() also unmap DMA-bufs through
iopt_dmabuf_unmap_domain_range() (or iopt_area_unfill_domain()) instead of
iopt_area_unmap_domain() and iopt_area_unmap_domain_range()?
When more than one domain is attached to the IOAS, those two paths still
unmap DMA-buf areas directly via iommu_unmap_nofail() rather than
dma_buf_iommu_unmap():
drivers/iommu/iommufd/io_pagetable.c:iopt_unfill_domain() {
...
if (iopt_is_dmabuf(pages)) {
if (!iopt_dmabuf_revoked(pages))
iopt_area_unmap_domain(area, domain);
iopt_dmabuf_untrack_domain(pages, area, domain);
}
...
}
drivers/iommu/iommufd/pages.c:iopt_area_unfill_domains() {
...
xa_for_each(&iopt->domains, index, domain) {
if (domain == area->storage_domain)
continue;
if (!iopt_dmabuf_revoked(pages))
iopt_area_unmap_domain_range(
area, domain, iopt_area_index(area),
iopt_area_last_index(area));
}
...
}
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261201-dmabuf_shared_buf_v1_xe_rc5-v1-0-b672717c7fc9@nvidia.com?part=13
^ permalink raw reply [flat|nested] 21+ messages in thread
* [PATCH RFC 14/16] vfio/pci: Remove vfio_pci_dma_buf_iommufd_map()
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
` (12 preceding siblings ...)
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 ` 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
` (2 subsequent siblings)
16 siblings, 0 replies; 21+ messages in thread
From: Alistair Popple @ 2026-12-01 7:06 UTC (permalink / raw)
To: Karol Wachowski, Sumit Semwal, Christian König,
Philipp Stanner, David Airlie, Simona Vetter, Rodrigo Vivi,
Matthew Brost, Thomas Hellström, Jason Gunthorpe,
Leon Romanovsky, Kees Cook, Gustavo A. R. Silva, Alex Williamson,
Kevin Tian, Joerg Roedel (AMD), Will Deacon, Robin Murphy
Cc: dri-devel, linux-media, linaro-mm-sig, intel-xe, linux-rdma,
linux-kernel, linux-hardening, kvm, iommu, Alexey Kardashevskiy,
Mina Almasry, Baolu Lu, chaitanyak, Dongwon Kim, Felix Kuehling,
jhubbard, Karl Mehltretter, Vivek Kasireddy, Xu Yilun, zhipingz,
Alistair Popple
iommufd now maps vfio DMA-bufs through dma_buf_iommu_map(), with the
dma-buf core programming the dma_shared_buf this exporter hands out.
The private interface, and its restriction to a single range, are no
longer used.
Signed-off-by: Alistair Popple <apopple@nvidia.com>
---
drivers/vfio/pci/vfio_pci_dmabuf.c | 34 ----------------------------------
include/linux/vfio_pci_core.h | 3 ---
2 files changed, 37 deletions(-)
diff --git a/drivers/vfio/pci/vfio_pci_dmabuf.c b/drivers/vfio/pci/vfio_pci_dmabuf.c
index 667a31efa922..613bf5f5c672 100644
--- a/drivers/vfio/pci/vfio_pci_dmabuf.c
+++ b/drivers/vfio/pci/vfio_pci_dmabuf.c
@@ -111,40 +111,6 @@ static const struct dma_buf_ops vfio_pci_dmabuf_ops = {
.release = vfio_pci_dma_buf_release,
};
-/*
- * This is a temporary "private interconnect" between VFIO DMABUF and iommufd.
- * It allows the two co-operating drivers to exchange the physical address of
- * the BAR. This is to be replaced with a formal DMABUF system for negotiated
- * interconnect types.
- *
- * If this function succeeds the following are true:
- * - There is one physical range and it is pointing to MMIO
- * - When move_notify is called it means revoke, not move, vfio_dma_buf_map
- * will fail if it is currently revoked
- */
-int vfio_pci_dma_buf_iommufd_map(struct dma_buf_attachment *attachment,
- struct phys_vec *phys)
-{
- struct vfio_pci_dma_buf *priv;
-
- dma_resv_assert_held(attachment->dmabuf->resv);
-
- if (attachment->dmabuf->ops != &vfio_pci_dmabuf_ops)
- return -EOPNOTSUPP;
-
- priv = attachment->dmabuf->priv;
- if (priv->revoked)
- return -ENODEV;
-
- /* More than one range to iommufd will require proper DMABUF support */
- if (priv->nr_ranges != 1)
- return -EOPNOTSUPP;
-
- *phys = priv->phys_vec[0];
- return 0;
-}
-EXPORT_SYMBOL_FOR_MODULES(vfio_pci_dma_buf_iommufd_map, "iommufd");
-
int vfio_pci_core_fill_phys_vec(struct phys_vec *phys_vec,
struct vfio_region_dma_range *dma_ranges,
size_t nr_ranges, phys_addr_t start,
diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h
index 2f3fa2787737..f5e9c1dff152 100644
--- a/include/linux/vfio_pci_core.h
+++ b/include/linux/vfio_pci_core.h
@@ -256,9 +256,6 @@ vfio_pci_core_get_iomap(struct vfio_pci_core_device *vdev, unsigned int bar)
return vdev->barmap[bar];
}
-int vfio_pci_dma_buf_iommufd_map(struct dma_buf_attachment *attachment,
- struct phys_vec *phys);
-
int vfio_pci_core_runtime_suspend(struct vfio_pci_core_device *vdev);
int vfio_pci_core_runtime_resume(struct vfio_pci_core_device *vdev);
pci_ers_result_t vfio_pci_core_aer_err_detected(struct vfio_pci_core_device *vdev);
--
2.54.0
^ permalink raw reply related [flat|nested] 21+ messages in thread* [PATCH RFC 15/16] drm/xe/dma-buf: Export a dma_shared_buf instead of mapping the buffer
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
` (13 preceding siblings ...)
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 ` Alistair Popple
2026-12-01 7:26 ` sashiko-bot
2026-12-01 7:06 ` [PATCH RFC 16/16] drm/xe: Import DMA-bufs as a DMA address list Alistair Popple
2026-12-01 7:28 ` [PATCH RFC 00/16] dma-buf: Introduce dma_shared_buf and dma_addr_list to replace sg_table sashiko-bot
16 siblings, 1 reply; 21+ messages in thread
From: Alistair Popple @ 2026-12-01 7:06 UTC (permalink / raw)
To: Karol Wachowski, Sumit Semwal, Christian König,
Philipp Stanner, David Airlie, Simona Vetter, Rodrigo Vivi,
Matthew Brost, Thomas Hellström, Jason Gunthorpe,
Leon Romanovsky, Kees Cook, Gustavo A. R. Silva, Alex Williamson,
Kevin Tian, Joerg Roedel (AMD), Will Deacon, Robin Murphy
Cc: dri-devel, linux-media, linaro-mm-sig, intel-xe, linux-rdma,
linux-kernel, linux-hardening, kvm, iommu, Alexey Kardashevskiy,
Mina Almasry, Baolu Lu, chaitanyak, Dongwon Kim, Felix Kuehling,
jhubbard, Karl Mehltretter, Vivek Kasireddy, Xu Yilun, zhipingz,
Alistair Popple
Xe DMA maps its DMA-bufs for the importing device itself. A buffer in
system memory has its pages put in an sg_table and mapped with
dma_map_sgtable(). A buffer in VRAM has the address of each of its
blocks in the VRAM BAR mapped with dma_map_resource() and put in an
sg_table without pages. However nothing in either path depends on the
driver other than where the buffer currently is, and the VRAM path
duplicates what the dma-shared-buf code now does for exporters of MMIO.
Instead hand out a dma_shared_buf through map_dma_shared_buf() and
unmap_dma_shared_buf(). The dma_shared_buf for a buffer in system memory
is built from its pages by dma_shared_buf_from_pages(). A buffer in VRAM
is a list of its blocks with the p2pdma provider of the VRAM BAR, so
call pcim_p2pdma_init() at probe to have one. The placement is still
decided when the importer maps, and importers without allow_peer2peer
still have the buffer migrated to system memory first, so the
dma_shared_buf always matches what the core will hand that importer.
This removes open-coding of VRAM mapping, instead using the PCI
peer-to-peer core to correctly map it, and allows iommufd to create
mappings for GPU buffers.
Signed-off-by: Alistair Popple <apopple@nvidia.com>
---
drivers/gpu/drm/xe/xe_dma_buf.c | 55 +++++-----------
drivers/gpu/drm/xe/xe_ttm_vram_mgr.c | 117 +++++++++++++----------------------
drivers/gpu/drm/xe/xe_ttm_vram_mgr.h | 13 ++--
drivers/gpu/drm/xe/xe_vram.c | 21 +++++++
drivers/gpu/drm/xe/xe_vram.h | 2 +
5 files changed, 84 insertions(+), 124 deletions(-)
diff --git a/drivers/gpu/drm/xe/xe_dma_buf.c b/drivers/gpu/drm/xe/xe_dma_buf.c
index 97299ca8f831..8727ca456f2f 100644
--- a/drivers/gpu/drm/xe/xe_dma_buf.c
+++ b/drivers/gpu/drm/xe/xe_dma_buf.c
@@ -111,14 +111,14 @@ static void xe_dma_buf_unpin(struct dma_buf_attachment *attach)
xe_bo_unpin_external(bo);
}
-static struct sg_table *xe_dma_buf_map(struct dma_buf_attachment *attach,
- enum dma_data_direction dir)
+static struct dma_shared_buf *
+xe_dma_buf_map_dma_shared_buf(struct dma_buf_attachment *attach)
{
struct dma_buf *dma_buf = attach->dmabuf;
struct drm_gem_object *obj = dma_buf->priv;
struct xe_bo *bo = gem_to_xe_bo(obj);
struct drm_exec *exec = XE_VALIDATION_UNSUPPORTED;
- struct sg_table *sgt;
+ struct dma_shared_buf *shared_buf;
int r = 0;
if (xe_bo_is_purged(bo))
@@ -138,49 +138,24 @@ static struct sg_table *xe_dma_buf_map(struct dma_buf_attachment *attach,
switch (bo->ttm.resource->mem_type) {
case XE_PL_TT:
- sgt = drm_prime_pages_to_sg(obj->dev,
- bo->ttm.ttm->pages,
- obj->size >> PAGE_SHIFT);
- if (IS_ERR(sgt))
- return sgt;
-
- if (dma_map_sgtable(attach->dev, sgt, dir,
- DMA_ATTR_SKIP_CPU_SYNC))
- goto error_free;
- break;
-
+ shared_buf = dma_shared_buf_from_pages(bo->ttm.ttm->pages,
+ obj->size >> PAGE_SHIFT);
+ if (!IS_ERR(shared_buf))
+ shared_buf->attrs = DMA_ATTR_SKIP_CPU_SYNC;
+ return shared_buf;
case XE_PL_VRAM0:
case XE_PL_VRAM1:
- r = xe_ttm_vram_mgr_alloc_sgt(xe_bo_device(bo),
- bo->ttm.resource, 0,
- bo->ttm.base.size, attach->dev,
- dir, &sgt);
- if (r)
- return ERR_PTR(r);
- break;
+ return xe_ttm_vram_mgr_alloc_shared_buf(xe_bo_device(bo),
+ bo->ttm.resource);
default:
return ERR_PTR(-EINVAL);
}
-
- return sgt;
-
-error_free:
- sg_free_table(sgt);
- kfree(sgt);
- return ERR_PTR(-EBUSY);
}
-static void xe_dma_buf_unmap(struct dma_buf_attachment *attach,
- struct sg_table *sgt,
- enum dma_data_direction dir)
+static void xe_dma_buf_unmap_dma_shared_buf(struct dma_buf_attachment *attach,
+ struct dma_shared_buf *shared_buf)
{
- if (sg_page(sgt->sgl)) {
- dma_unmap_sgtable(attach->dev, sgt, dir, 0);
- sg_free_table(sgt);
- kfree(sgt);
- } else {
- xe_ttm_vram_mgr_free_sgt(attach->dev, dir, sgt);
- }
+ kvfree(shared_buf);
}
static int xe_dma_buf_begin_cpu_access(struct dma_buf *dma_buf,
@@ -230,8 +205,8 @@ static const struct dma_buf_ops xe_dmabuf_ops = {
.detach = xe_dma_buf_detach,
.pin = xe_dma_buf_pin,
.unpin = xe_dma_buf_unpin,
- .map_dma_buf = xe_dma_buf_map,
- .unmap_dma_buf = xe_dma_buf_unmap,
+ .map_dma_shared_buf = xe_dma_buf_map_dma_shared_buf,
+ .unmap_dma_shared_buf = xe_dma_buf_unmap_dma_shared_buf,
.release = xe_dma_buf_release,
.begin_cpu_access = xe_dma_buf_begin_cpu_access,
.mmap = drm_gem_dmabuf_mmap,
diff --git a/drivers/gpu/drm/xe/xe_ttm_vram_mgr.c b/drivers/gpu/drm/xe/xe_ttm_vram_mgr.c
index f28550b29f6b..6e41af57ef78 100644
--- a/drivers/gpu/drm/xe/xe_ttm_vram_mgr.c
+++ b/drivers/gpu/drm/xe/xe_ttm_vram_mgr.c
@@ -6,6 +6,7 @@
#include <linux/cgroup_dmem.h>
#include <linux/debugfs.h>
+#include <linux/dma-buf.h>
#include <drm/drm_managed.h>
#include <drm/drm_drv.h>
@@ -27,6 +28,7 @@
#include "xe_res_cursor.h"
#include "xe_ttm_stolen_mgr.h"
#include "xe_ttm_vram_mgr.h"
+#include "xe_vram.h"
#include "xe_vram_types.h"
static inline struct gpu_buddy_block *
@@ -500,98 +502,63 @@ int xe_ttm_vram_mgr_init(struct xe_device *xe, struct xe_vram_region *vram)
PAGE_SIZE);
}
-int xe_ttm_vram_mgr_alloc_sgt(struct xe_device *xe,
- struct ttm_resource *res,
- u64 offset, u64 length,
- struct device *dev,
- enum dma_data_direction dir,
- struct sg_table **sgt)
+/**
+ * xe_ttm_vram_mgr_alloc_shared_buf() - dma_shared_buf of a VRAM resource
+ * @xe: xe device
+ * @res: The VRAM resource, all of which must be CPU visible
+ *
+ * The ranges are the addresses of the resource's blocks in the VRAM BAR, for
+ * a dma-buf exporter to hand out through &dma_buf_ops.map_dma_shared_buf.
+ *
+ * Return: The dma_shared_buf, to be freed with kvfree(), or an ERR_PTR() on
+ * failure.
+ */
+struct dma_shared_buf *
+xe_ttm_vram_mgr_alloc_shared_buf(struct xe_device *xe,
+ struct ttm_resource *res)
{
struct xe_tile *tile = &xe->tiles[res->mem_type - XE_PL_VRAM0];
struct xe_ttm_vram_mgr_resource *vres = to_xe_ttm_vram_mgr_resource(res);
+ struct p2pdma_provider *provider = xe_vram_p2pdma_provider(xe);
+ struct dma_shared_buf *shared_buf;
struct xe_res_cursor cursor;
- struct scatterlist *sg;
- int num_entries = 0;
- int i, r;
+ size_t nr_ranges = 0;
+ size_t i;
if (vres->used_visible_size < res->size)
- return -EOPNOTSUPP;
+ return ERR_PTR(-EOPNOTSUPP);
- *sgt = kmalloc_obj(**sgt);
- if (!*sgt)
- return -ENOMEM;
+ /* Without a provider the core would map the BAR as system memory */
+ if (!provider)
+ return ERR_PTR(-EOPNOTSUPP);
/* Determine the number of GPU_BUDDY blocks to export */
- xe_res_first(res, offset, length, &cursor);
+ xe_res_first(res, 0, res->size, &cursor);
while (cursor.remaining) {
- num_entries++;
- /* Limit maximum size to 2GiB due to SG table limitations. */
- xe_res_next(&cursor, min_t(u64, cursor.size, SZ_2G));
+ nr_ranges++;
+ xe_res_next(&cursor, cursor.size);
}
- r = sg_alloc_table(*sgt, num_entries, GFP_KERNEL);
- if (r)
- goto error_free;
-
- /* Initialize scatterlist nodes of sg_table */
- for_each_sgtable_sg((*sgt), sg, i)
- sg->length = 0;
-
- /*
- * Walk down GPU_BUDDY blocks to populate scatterlist nodes
- * @note: Use iterator api to get first the GPU_BUDDY block
- * and the number of bytes from it. Access the following
- * GPU_BUDDY block(s) if more buffer needs to exported
- */
- xe_res_first(res, offset, length, &cursor);
- for_each_sgtable_sg((*sgt), sg, i) {
- phys_addr_t phys = cursor.start + xe_vram_region_io_start(tile->mem.vram);
- size_t size = min_t(u64, cursor.size, SZ_2G);
- dma_addr_t addr;
-
- addr = dma_map_resource(dev, phys, size, dir,
- DMA_ATTR_SKIP_CPU_SYNC);
- r = dma_mapping_error(dev, addr);
- if (r)
- goto error_unmap;
-
- sg_set_page(sg, NULL, size, 0);
- sg_dma_address(sg) = addr;
- sg_dma_len(sg) = size;
-
- xe_res_next(&cursor, size);
- }
+ shared_buf = kvmalloc(struct_size(shared_buf, ranges, nr_ranges),
+ GFP_KERNEL);
+ if (!shared_buf)
+ return ERR_PTR(-ENOMEM);
- return 0;
+ shared_buf->provider = provider;
+ shared_buf->attrs = 0;
+ shared_buf->size = res->size;
+ shared_buf->nr_ranges = nr_ranges;
-error_unmap:
- for_each_sgtable_sg((*sgt), sg, i) {
- if (!sg->length)
- continue;
+ xe_res_first(res, 0, res->size, &cursor);
+ for (i = 0; i < nr_ranges; i++) {
+ shared_buf->ranges[i].paddr = cursor.start +
+ xe_vram_region_io_start(tile->mem.vram);
+ shared_buf->ranges[i].len = cursor.size;
- dma_unmap_resource(dev, sg->dma_address,
- sg->length, dir,
- DMA_ATTR_SKIP_CPU_SYNC);
+ xe_res_next(&cursor, cursor.size);
}
- sg_free_table(*sgt);
-
-error_free:
- kfree(*sgt);
- return r;
-}
-void xe_ttm_vram_mgr_free_sgt(struct device *dev, enum dma_data_direction dir,
- struct sg_table *sgt)
-{
- struct scatterlist *sg;
- int i;
-
- for_each_sgtable_sg(sgt, sg, i)
- dma_unmap_resource(dev, sg->dma_address,
- sg->length, dir,
- DMA_ATTR_SKIP_CPU_SYNC);
- sg_free_table(sgt);
- kfree(sgt);
+ return shared_buf;
}
u64 xe_ttm_vram_get_cpu_visible_size(struct ttm_resource_manager *man)
diff --git a/drivers/gpu/drm/xe/xe_ttm_vram_mgr.h b/drivers/gpu/drm/xe/xe_ttm_vram_mgr.h
index d77f067d197b..c1613efde23a 100644
--- a/drivers/gpu/drm/xe/xe_ttm_vram_mgr.h
+++ b/drivers/gpu/drm/xe/xe_ttm_vram_mgr.h
@@ -8,7 +8,7 @@
#include "xe_ttm_vram_mgr_types.h"
-enum dma_data_direction;
+struct dma_shared_buf;
struct xe_device;
struct xe_tile;
struct xe_vram_region;
@@ -18,14 +18,9 @@ int __xe_ttm_vram_mgr_init(struct xe_device *xe, struct xe_ttm_vram_mgr *mgr,
u64 default_page_size);
int xe_ttm_vram_mgr_init(struct xe_device *xe, struct xe_vram_region *vram);
void xe_ttm_vram_debugfs_init(struct xe_device *xe, struct dentry *root);
-int xe_ttm_vram_mgr_alloc_sgt(struct xe_device *xe,
- struct ttm_resource *res,
- u64 offset, u64 length,
- struct device *dev,
- enum dma_data_direction dir,
- struct sg_table **sgt);
-void xe_ttm_vram_mgr_free_sgt(struct device *dev, enum dma_data_direction dir,
- struct sg_table *sgt);
+struct dma_shared_buf *
+xe_ttm_vram_mgr_alloc_shared_buf(struct xe_device *xe,
+ struct ttm_resource *res);
u64 xe_ttm_vram_get_avail(struct ttm_resource_manager *man);
u64 xe_ttm_vram_get_cpu_visible_size(struct ttm_resource_manager *man);
diff --git a/drivers/gpu/drm/xe/xe_vram.c b/drivers/gpu/drm/xe/xe_vram.c
index dcedd8cfd731..c0b040582b81 100644
--- a/drivers/gpu/drm/xe/xe_vram.c
+++ b/drivers/gpu/drm/xe/xe_vram.c
@@ -5,6 +5,7 @@
#include <kunit/visibility.h>
#include <linux/pci.h>
+#include <linux/pci-p2pdma.h>
#include <drm/drm_managed.h>
#include <drm/drm_print.h>
@@ -320,6 +321,11 @@ int xe_vram_probe(struct xe_device *xe)
return err;
drm_info(&xe->drm, "VISIBLE VRAM: %pa, %pa\n", &lmem_bar.io_start, &lmem_bar.io_size);
+ /* VRAM is exported to other devices as ranges of the BAR */
+ err = pcim_p2pdma_init(to_pci_dev(xe->drm.dev));
+ if (err && err != -EOPNOTSUPP)
+ return err;
+
remain_io_size = lmem_bar.io_size;
for_each_tile(tile, xe, id) {
@@ -355,6 +361,21 @@ int xe_vram_probe(struct xe_device *xe)
return devm_add_action_or_reset(xe->drm.dev, vram_fini, xe);
}
+/**
+ * xe_vram_p2pdma_provider - Get the p2pdma provider of the VRAM BAR
+ * @xe: xe device
+ *
+ * Return: the provider, or NULL if the device has no VRAM BAR or the BAR
+ * cannot be used for PCI peer-to-peer DMA
+ */
+struct p2pdma_provider *xe_vram_p2pdma_provider(struct xe_device *xe)
+{
+ if (!IS_DGFX(xe))
+ return NULL;
+
+ return pcim_p2pdma_provider(to_pci_dev(xe->drm.dev), LMEM_BAR);
+}
+
/**
* xe_vram_region_io_start - Get the IO start of a VRAM region
* @vram: the VRAM region
diff --git a/drivers/gpu/drm/xe/xe_vram.h b/drivers/gpu/drm/xe/xe_vram.h
index 38425d81f777..3abe6c6e50f9 100644
--- a/drivers/gpu/drm/xe/xe_vram.h
+++ b/drivers/gpu/drm/xe/xe_vram.h
@@ -8,12 +8,14 @@
#include <linux/types.h>
+struct p2pdma_provider;
struct xe_device;
struct xe_vram_region;
struct ttm_resource;
struct xe_vram_region *xe_map_resource_to_region(struct ttm_resource *res);
int xe_vram_probe(struct xe_device *xe);
+struct p2pdma_provider *xe_vram_p2pdma_provider(struct xe_device *xe);
struct xe_vram_region *xe_vram_region_alloc(struct xe_device *xe, u8 id, u32 placement);
--
2.54.0
^ permalink raw reply related [flat|nested] 21+ messages in thread* Re: [PATCH RFC 15/16] drm/xe/dma-buf: Export a dma_shared_buf instead of mapping the buffer
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:26 ` sashiko-bot
0 siblings, 0 replies; 21+ messages in thread
From: sashiko-bot @ 2026-12-01 7:26 UTC (permalink / raw)
To: Alistair Popple; +Cc: dri-devel, intel-xe, linux-rdma, kvm, media-ci
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] drm/xe/dma-buf: xe_dma_buf_attach() clears peer2peer for
iommufd attachments
--
commit afc228f35dfc8ad8b39e640599fbccbde0209c02
Author: Alistair Popple <apopple@nvidia.com>
drm/xe/dma-buf: Export a dma_shared_buf instead of mapping the buffer
This commit replaces xe_dma_buf_map() and xe_dma_buf_unmap() with
map_dma_shared_buf() and unmap_dma_shared_buf() callbacks, initializing
a p2pdma provider for the VRAM BAR so VRAM and system memory buffers are
exported as dma_shared_buf ranges.
> diff --git a/drivers/gpu/drm/xe/xe_dma_buf.c b/drivers/gpu/drm/xe/xe_dma_buf.c
> index 97299ca8f8316..8727ca456f2fb 100644
> --- a/drivers/gpu/drm/xe/xe_dma_buf.c
> +++ b/drivers/gpu/drm/xe/xe_dma_buf.c
> @@ -111,14 +111,14 @@ static void xe_dma_buf_unpin(struct dma_buf_attachment *attach)
> xe_bo_unpin_external(bo);
> }
>
> -static struct sg_table *xe_dma_buf_map(struct dma_buf_attachment *attach,
> - enum dma_data_direction dir)
> +static struct dma_shared_buf *
> +xe_dma_buf_map_dma_shared_buf(struct dma_buf_attachment *attach)
> {
> struct dma_buf *dma_buf = attach->dmabuf;
> struct drm_gem_object *obj = dma_buf->priv;
> struct xe_bo *bo = gem_to_xe_bo(obj);
> struct drm_exec *exec = XE_VALIDATION_UNSUPPORTED;
> - struct sg_table *sgt;
> + struct dma_shared_buf *shared_buf;
> int r = 0;
>
> if (xe_bo_is_purged(bo))
> @@ -138,49 +138,24 @@ static struct sg_table *xe_dma_buf_map(struct dma_buf_attachment *attach,
>
> switch (bo->ttm.resource->mem_type) {
> case XE_PL_TT:
> - sgt = drm_prime_pages_to_sg(obj->dev,
> - bo->ttm.ttm->pages,
> - obj->size >> PAGE_SHIFT);
> - if (IS_ERR(sgt))
> - return sgt;
> -
> - if (dma_map_sgtable(attach->dev, sgt, dir,
> - DMA_ATTR_SKIP_CPU_SYNC))
> - goto error_free;
> - break;
> -
> + shared_buf = dma_shared_buf_from_pages(bo->ttm.ttm->pages,
> + obj->size >> PAGE_SHIFT);
> + if (!IS_ERR(shared_buf))
> + shared_buf->attrs = DMA_ATTR_SKIP_CPU_SYNC;
> + return shared_buf;
> case XE_PL_VRAM0:
> case XE_PL_VRAM1:
> - r = xe_ttm_vram_mgr_alloc_sgt(xe_bo_device(bo),
> - bo->ttm.resource, 0,
> - bo->ttm.base.size, attach->dev,
> - dir, &sgt);
> - if (r)
> - return ERR_PTR(r);
> - break;
> + return xe_ttm_vram_mgr_alloc_shared_buf(xe_bo_device(bo),
> + bo->ttm.resource);
[Severity: High]
Can iommufd ever reach the XE_PL_VRAM0 / XE_PL_VRAM1 path here to map
VRAM buffers?
When iopt_map_dmabuf() attaches to the dma-buf, it passes
iommufd_global_device() (a virtual miscdevice, not a PCI device) as
attach->dev. In xe_dma_buf_attach(), pci_p2pdma_distance() returns -1
because find_parent_pci_dev(attach->dev) is NULL:
drivers/gpu/drm/xe/xe_dma_buf.c:xe_dma_buf_attach() {
...
if (attach->peer2peer &&
pci_p2pdma_distance(to_pci_dev(obj->dev->dev), attach->dev, verbose_p2p_dist) < 0)
attach->peer2peer = false;
if (!attach->peer2peer && !xe_bo_can_migrate(gem_to_xe_bo(obj), XE_PL_TT))
return -EOPNOTSUPP;
...
}
Clearing attach->peer2peer causes VRAM-only buffer objects to fail
iommufd import with -EOPNOTSUPP, while buffers that allow XE_PL_TT are
migrated out of VRAM into system memory and pinned there by
xe_dma_buf_pin().
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261201-dmabuf_shared_buf_v1_xe_rc5-v1-0-b672717c7fc9@nvidia.com?part=15
^ permalink raw reply [flat|nested] 21+ messages in thread
* [PATCH RFC 16/16] drm/xe: Import DMA-bufs as a DMA address list
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
` (14 preceding siblings ...)
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 ` Alistair Popple
2026-12-01 7:28 ` [PATCH RFC 00/16] dma-buf: Introduce dma_shared_buf and dma_addr_list to replace sg_table sashiko-bot
16 siblings, 0 replies; 21+ messages in thread
From: Alistair Popple @ 2026-12-01 7:06 UTC (permalink / raw)
To: Karol Wachowski, Sumit Semwal, Christian König,
Philipp Stanner, David Airlie, Simona Vetter, Rodrigo Vivi,
Matthew Brost, Thomas Hellström, Jason Gunthorpe,
Leon Romanovsky, Kees Cook, Gustavo A. R. Silva, Alex Williamson,
Kevin Tian, Joerg Roedel (AMD), Will Deacon, Robin Murphy
Cc: dri-devel, linux-media, linaro-mm-sig, intel-xe, linux-rdma,
linux-kernel, linux-hardening, kvm, iommu, Alexey Kardashevskiy,
Mina Almasry, Baolu Lu, chaitanyak, Dongwon Kim, Felix Kuehling,
jhubbard, Karl Mehltretter, Vivek Kasireddy, Xu Yilun, zhipingz,
Alistair Popple
Xe maps an imported DMA-buf with dma_buf_map_sgt() and keeps the
sg_table as the backing of the buffer object. However the only thing it
ever reads from the table is the DMA address and length of each entry,
which the resource cursor walks when the buffer is bound to a VM or
mapped into the GGTT.
Map the attachment with dma_buf_map_dma_list() instead, which returns
just the DMA address ranges of the buffer. The list is kept in struct
xe_bo for as long as the buffer is mapped. It takes over from the sg
pointer in the TTM buffer object, which Xe no longer sets, as the record
of whether the attachment is mapped.
Add xe_res_first_bo_dma() to start a walk over either the list of an
imported buffer or the sg_table of a buffer's own pages, and use it
where an imported buffer can turn up: binding to a VM, mapping into the
GGTT and xe_bo_addr(). The other users of xe_bo_sg() only ever see
buffers which Xe allocated itself so they are left alone.
This works with any exporter. The DMA-buf core copies the addresses out
of the sg_table of an exporter which still maps one itself.
Signed-off-by: Alistair Popple <apopple@nvidia.com>
---
Note it wouldn't be too difficult to remove sg_table usage entirely
and instead use a dma_shared_buf. However this change is meant as an
example for how drivers could be switched to the new interface, so I
haven't done that.
---
drivers/gpu/drm/xe/xe_bo.c | 52 +++++++++++--------------
drivers/gpu/drm/xe/xe_bo_types.h | 6 +++
drivers/gpu/drm/xe/xe_ggtt.c | 2 +-
drivers/gpu/drm/xe/xe_pt.c | 4 +-
drivers/gpu/drm/xe/xe_res_cursor.h | 78 ++++++++++++++++++++++++++++++++++++++
5 files changed, 109 insertions(+), 33 deletions(-)
diff --git a/drivers/gpu/drm/xe/xe_bo.c b/drivers/gpu/drm/xe/xe_bo.c
index 14b71d951659..475df4f24966 100644
--- a/drivers/gpu/drm/xe/xe_bo.c
+++ b/drivers/gpu/drm/xe/xe_bo.c
@@ -856,37 +856,35 @@ static int xe_bo_move_dmabuf(struct ttm_buffer_object *ttm_bo,
struct ttm_resource *new_res)
{
struct dma_buf_attachment *attach = ttm_bo->base.import_attach;
- struct xe_ttm_tt *xe_tt = container_of(ttm_bo->ttm, struct xe_ttm_tt,
- ttm);
+ struct xe_bo *bo = ttm_to_xe_bo(ttm_bo);
struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
bool device_unplugged = drm_dev_is_unplugged(&xe->drm);
- struct sg_table *sg;
+ const struct dma_addr_list *list;
xe_assert(xe, attach);
xe_assert(xe, ttm_bo->ttm);
if (device_unplugged && new_res->mem_type == XE_PL_SYSTEM &&
- ttm_bo->sg) {
+ bo->dma_list) {
dma_resv_wait_timeout(ttm_bo->base.resv, DMA_RESV_USAGE_BOOKKEEP,
false, MAX_SCHEDULE_TIMEOUT);
- dma_buf_unmap_sgt(attach, ttm_bo->sg, DMA_BIDIRECTIONAL);
- ttm_bo->sg = NULL;
+ dma_buf_unmap_dma_list(attach, bo->dma_list, DMA_BIDIRECTIONAL);
+ bo->dma_list = NULL;
}
if (new_res->mem_type == XE_PL_SYSTEM)
goto out;
- if (ttm_bo->sg) {
- dma_buf_unmap_sgt(attach, ttm_bo->sg, DMA_BIDIRECTIONAL);
- ttm_bo->sg = NULL;
+ if (bo->dma_list) {
+ dma_buf_unmap_dma_list(attach, bo->dma_list, DMA_BIDIRECTIONAL);
+ bo->dma_list = NULL;
}
- sg = dma_buf_map_sgt(attach, DMA_BIDIRECTIONAL);
- if (IS_ERR(sg))
- return PTR_ERR(sg);
+ list = dma_buf_map_dma_list(attach, DMA_BIDIRECTIONAL);
+ if (IS_ERR(list))
+ return PTR_ERR(list);
- ttm_bo->sg = sg;
- xe_tt->sg = sg;
+ bo->dma_list = list;
out:
ttm_bo_move_null(ttm_bo, new_res);
@@ -1749,12 +1747,11 @@ int xe_bo_dma_unmap_pinned(struct xe_bo *bo)
if (tt) {
struct xe_ttm_tt *xe_tt = container_of(tt, typeof(*xe_tt), ttm);
- if (ttm_bo->type == ttm_bo_type_sg && ttm_bo->sg) {
- dma_buf_unmap_sgt(ttm_bo->base.import_attach,
- ttm_bo->sg,
- DMA_BIDIRECTIONAL);
- ttm_bo->sg = NULL;
- xe_tt->sg = NULL;
+ if (bo->dma_list) {
+ dma_buf_unmap_dma_list(ttm_bo->base.import_attach,
+ bo->dma_list,
+ DMA_BIDIRECTIONAL);
+ bo->dma_list = NULL;
} else if (xe_tt->sg) {
dma_unmap_sgtable(ttm_to_xe_device(ttm_bo->bdev)->drm.dev,
xe_tt->sg,
@@ -1863,14 +1860,10 @@ static void xe_ttm_bo_delete_mem_notify(struct ttm_buffer_object *ttm_bo)
* Object is idle and about to be destroyed. Release the
* dma-buf attachment.
*/
- if (ttm_bo->type == ttm_bo_type_sg && ttm_bo->sg) {
- struct xe_ttm_tt *xe_tt = container_of(ttm_bo->ttm,
- struct xe_ttm_tt, ttm);
-
- dma_buf_unmap_sgt(ttm_bo->base.import_attach, ttm_bo->sg,
- DMA_BIDIRECTIONAL);
- ttm_bo->sg = NULL;
- xe_tt->sg = NULL;
+ if (bo->dma_list) {
+ dma_buf_unmap_dma_list(ttm_bo->base.import_attach, bo->dma_list,
+ DMA_BIDIRECTIONAL);
+ bo->dma_list = NULL;
}
}
@@ -3453,8 +3446,7 @@ dma_addr_t __xe_bo_addr(struct xe_bo *bo, u64 offset, size_t page_size)
if (!xe_bo_is_vram(bo) && !xe_bo_is_stolen(bo)) {
xe_assert(xe, bo->ttm.ttm);
- xe_res_first_sg(xe_bo_sg(bo), page << PAGE_SHIFT,
- page_size, &cur);
+ xe_res_first_bo_dma(bo, page << PAGE_SHIFT, page_size, &cur);
return xe_res_dma(&cur) + offset;
} else {
struct xe_res_cursor cur;
diff --git a/drivers/gpu/drm/xe/xe_bo_types.h b/drivers/gpu/drm/xe/xe_bo_types.h
index 8ec4a01a0092..dd00fae0610e 100644
--- a/drivers/gpu/drm/xe/xe_bo_types.h
+++ b/drivers/gpu/drm/xe/xe_bo_types.h
@@ -17,6 +17,7 @@
#include "xe_device_types.h"
#include "xe_ggtt_types.h"
+struct dma_addr_list;
struct xe_device;
struct xe_mem_pool_node;
struct xe_vm;
@@ -39,6 +40,11 @@ struct xe_bo {
struct xe_bo *parent_obj;
/** @dma_buf: Imported dma-buf ref to keep its resv alive. */
struct dma_buf *dma_buf;
+ /**
+ * @dma_list: DMA addresses of an imported dma-buf whilst it is mapped,
+ * NULL otherwise. Protected by the BO dma_resv.
+ */
+ const struct dma_addr_list *dma_list;
/** @flags: flags for this buffer object */
u32 flags;
/** @vm: VM this BO is attached to, for extobj this will be NULL */
diff --git a/drivers/gpu/drm/xe/xe_ggtt.c b/drivers/gpu/drm/xe/xe_ggtt.c
index b7c55dc8a0bb..21ea116a5f1c 100644
--- a/drivers/gpu/drm/xe/xe_ggtt.c
+++ b/drivers/gpu/drm/xe/xe_ggtt.c
@@ -741,7 +741,7 @@ static void xe_ggtt_map_bo(struct xe_ggtt *ggtt, struct xe_ggtt_node *node,
if (!xe_bo_is_vram(bo) && !xe_bo_is_stolen(bo)) {
xe_assert(xe_bo_device(bo), bo->ttm.ttm);
- for (xe_res_first_sg(xe_bo_sg(bo), 0, xe_bo_size(bo), &cur);
+ for (xe_res_first_bo_dma(bo, 0, xe_bo_size(bo), &cur);
cur.remaining; xe_res_next(&cur, XE_PAGE_SIZE))
ggtt->pt_ops->ggtt_set_pte(ggtt, end - cur.remaining,
pte | xe_res_dma(&cur));
diff --git a/drivers/gpu/drm/xe/xe_pt.c b/drivers/gpu/drm/xe/xe_pt.c
index aeeac0f1ab83..b745de5210df 100644
--- a/drivers/gpu/drm/xe/xe_pt.c
+++ b/drivers/gpu/drm/xe/xe_pt.c
@@ -887,8 +887,8 @@ xe_pt_stage_bind(struct xe_tile *tile, struct xe_vma *vma,
xe_res_first(bo->ttm.resource, xe_vma_bo_offset(vma),
xe_vma_size(vma), &curs);
else
- xe_res_first_sg(xe_bo_sg(bo), xe_vma_bo_offset(vma),
- xe_vma_size(vma), &curs);
+ xe_res_first_bo_dma(bo, xe_vma_bo_offset(vma),
+ xe_vma_size(vma), &curs);
} else if (!range) {
curs.size = xe_vma_size(vma);
}
diff --git a/drivers/gpu/drm/xe/xe_res_cursor.h b/drivers/gpu/drm/xe/xe_res_cursor.h
index c3a037e5f34e..e6541b0c3eb0 100644
--- a/drivers/gpu/drm/xe/xe_res_cursor.h
+++ b/drivers/gpu/drm/xe/xe_res_cursor.h
@@ -24,6 +24,7 @@
#ifndef _XE_RES_CURSOR_H_
#define _XE_RES_CURSOR_H_
+#include <linux/dma-buf.h>
#include <linux/scatterlist.h>
#include <drm/drm_pagemap.h>
@@ -57,6 +58,8 @@ struct xe_res_cursor {
struct scatterlist *sgl;
/** @dma_addr: Current element in a struct drm_pagemap_addr array */
const struct drm_pagemap_addr *dma_addr;
+ /** @dma_range: Current range of a struct dma_addr_list */
+ const struct dma_addr_range *dma_range;
/** @mm: Buddy allocator for VRAM cursor */
struct gpu_buddy *mm;
/**
@@ -93,6 +96,7 @@ static inline void xe_res_first(struct ttm_resource *res,
{
cur->sgl = NULL;
cur->dma_addr = NULL;
+ cur->dma_range = NULL;
if (!res)
goto fallback;
@@ -172,6 +176,21 @@ static inline void __xe_res_sg_next(struct xe_res_cursor *cur)
cur->sgl = sgl;
}
+static inline void __xe_res_dma_list_next(struct xe_res_cursor *cur)
+{
+ const struct dma_addr_range *range = cur->dma_range;
+ u64 start = cur->start;
+
+ while (start >= range->len) {
+ start -= range->len;
+ range++;
+ }
+
+ cur->start = start;
+ cur->size = range->len - start;
+ cur->dma_range = range;
+}
+
/**
* __xe_res_dma_next() - Advance the cursor when end-of-segment is reached
* @cur: The cursor
@@ -222,11 +241,61 @@ static inline void xe_res_first_sg(const struct sg_table *sg,
cur->remaining = size;
cur->size = 0;
cur->dma_addr = NULL;
+ cur->dma_range = NULL;
cur->sgl = sg->sgl;
cur->mem_type = XE_PL_TT;
__xe_res_sg_next(cur);
}
+/**
+ * xe_res_first_dma_list - initialize a xe_res_cursor with a DMA address list
+ *
+ * @list: DMA address list of an imported dma-buf to walk
+ * @start: Start of the range
+ * @size: Size of the range
+ * @cur: cursor object to initialize
+ *
+ * Start walking over the range of allocations between @start and @size.
+ */
+static inline void xe_res_first_dma_list(const struct dma_addr_list *list,
+ u64 start, u64 size,
+ struct xe_res_cursor *cur)
+{
+ XE_WARN_ON(!list);
+ XE_WARN_ON(start + size > list->size);
+ cur->node = NULL;
+ cur->start = start;
+ cur->remaining = size;
+ cur->size = 0;
+ cur->dma_addr = NULL;
+ cur->sgl = NULL;
+ cur->dma_range = list->ranges;
+ cur->mem_type = XE_PL_TT;
+ __xe_res_dma_list_next(cur);
+}
+
+/**
+ * xe_res_first_bo_dma - initialize a xe_res_cursor with the DMA addresses of
+ * a buffer object
+ *
+ * @bo: buffer object to walk, which must not be in VRAM or stolen memory
+ * @start: Start of the range
+ * @size: Size of the range
+ * @cur: cursor object to initialize
+ *
+ * Start walking over the range of allocations between @start and @size. The
+ * addresses are those of the exporter's DMA address list for an imported
+ * dma-buf and those of the buffer object's own pages otherwise.
+ */
+static inline void xe_res_first_bo_dma(struct xe_bo *bo, u64 start, u64 size,
+ struct xe_res_cursor *cur)
+{
+ if (bo->dma_list)
+ xe_res_first_dma_list(bo->dma_list, start, size, cur);
+ else
+ xe_res_first_sg(xe_bo_sg(bo), start, size, cur);
+}
+
/**
* xe_res_first_dma - initialize a xe_res_cursor with dma_addr array
*
@@ -255,6 +324,7 @@ static inline void xe_res_first_dma(const struct drm_pagemap_addr *dma_addr,
cur->dma_addr = dma_addr;
__xe_res_dma_next(cur);
cur->sgl = NULL;
+ cur->dma_range = NULL;
cur->mem_type = XE_PL_TT;
}
@@ -296,6 +366,12 @@ static inline void xe_res_next(struct xe_res_cursor *cur, u64 size)
return;
}
+ if (cur->dma_range) {
+ cur->start += size;
+ __xe_res_dma_list_next(cur);
+ return;
+ }
+
switch (cur->mem_type) {
case XE_PL_STOLEN:
/* Just advance within the contiguous region. */
@@ -339,6 +415,8 @@ static inline u64 xe_res_dma(const struct xe_res_cursor *cur)
return cur->dma_start + cur->start;
else if (cur->sgl)
return sg_dma_address(cur->sgl) + cur->start;
+ else if (cur->dma_range)
+ return cur->dma_range->addr + cur->start;
else
return cur->start;
}
--
2.54.0
^ permalink raw reply related [flat|nested] 21+ messages in thread* Re: [PATCH RFC 00/16] dma-buf: Introduce dma_shared_buf and dma_addr_list to replace sg_table
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
` (15 preceding siblings ...)
2026-12-01 7:06 ` [PATCH RFC 16/16] drm/xe: Import DMA-bufs as a DMA address list Alistair Popple
@ 2026-12-01 7:28 ` sashiko-bot
16 siblings, 0 replies; 21+ messages in thread
From: sashiko-bot @ 2026-12-01 7:28 UTC (permalink / raw)
To: Alistair Popple; +Cc: linux-rdma
Sashiko has finished reviewing this 16-patch series:
- Patches 1-3, 5-12, 14, and 16: no issues found.
- Patch 4 ("dma-shared-buf: Add dma_addr_list_to_sgt()"): 1 potential
issue (1 High)
- Patch 13 ("iommufd: Map DMA-bufs through dma_buf_iommu_map()"): 2
potential issues (1 Medium, 1 Low)
- Patch 15 ("drm/xe/dma-buf: Export a dma_shared_buf instead of m..."):
1 potential issue (1 High)
Detailed reviews have been sent as separate replies to patches 4, 13,
and 15.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261201-dmabuf_shared_buf_v1_xe_rc5-v1-0-b672717c7fc9@nvidia.com
^ permalink raw reply [flat|nested] 21+ messages in thread