Netdev List
 help / color / mirror / Atom feed
From: Alistair Popple <apopple@nvidia.com>
To: "Christian König" <christian.koenig@amd.com>,
	"Sumit Semwal" <sumit.semwal@linaro.org>,
	"Thomas Hellström" <thomas.hellstrom@linux.intel.com>,
	"Simona Vetter" <simona.vetter@ffwll.ch>,
	"Matthew Brost" <matthew.brost@intel.com>,
	"Vivek Kasireddy" <vivek.kasireddy@intel.com>,
	"Dongwon Kim" <dongwon.kim@intel.com>,
	"Kevin Tian" <kevin.tian@intel.com>,
	"Xu Yilun" <yilun.xu@linux.intel.com>,
	"Karl Mehltretter" <kmehltretter@gmail.com>,
	"Baolu Lu" <baolu.lu@linux.intel.com>,
	"Felix Kuehling" <felix.kuehling@amd.com>,
	"Alex Deucher" <alexander.deucher@amd.com>,
	linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org,
	intel-xe@lists.freedesktop.org, amd-gfx@lists.freedesktop.org,
	linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org,
	linux-rdma@vger.kernel.org, iommu@lists.linux.dev,
	"Keith Busch" <kbusch@kernel.org>, "Jens Axboe" <axboe@kernel.dk>,
	"Christoph Hellwig" <hch@lst.de>,
	"Sagi Grimberg" <sagi@grimberg.me>,
	linux-nvme@lists.infradead.org,
	"David S. Miller" <davem@davemloft.net>,
	"Eric Dumazet" <edumazet@kernel.org>,
	"Jakub Kicinski" <kuba@kernel.org>,
	"Paolo Abeni" <pabeni@redhat.com>,
	"Simon Horman" <horms@kernel.org>,
	"Mina Almasry" <almasrymina@google.com>,
	netdev@vger.kernel.org, "Alexey Kardashevskiy" <aik@amd.com>,
	zhipingz@meta.com, treding@nvidia.com, jhubbard@nvidia.com,
	chaitanyak@nvidia.com, "Jason Gunthorpe" <jgg@ziepe.ca>,
	"Leon Romanovsky" <leon@kernel.org>
Cc: Alistair Popple <apopple@nvidia.com>
Subject: [PATCH v3 00/24] DMA-buf mapping types
Date: Fri, 20 Nov 2026 16:55:38 +1100	[thread overview]
Message-ID: <20261120-dma_mapping_type_v4-v3-0-c0183a409bdf@nvidia.com> (raw)

This is v3 of the DMA-buf mapping types series [1]. The main difference
from v2 is exporters have been changed to allocate a type-specific
mapping struct and the PCIe P2P changes have been deferred to a
follow-up patch series. The exporter conversion was primarily motivated
by a need to have the importer call the correct unmap functions even
if the selected attachment type has subsequently changed after an
invalidation.

Doing this conversion though will make it easier in future to move away
from providing the sgt data type to importers. A future patch series
will be posted to do that.

The PCIe P2P work was deferred in order to keep this series mostly a
mechanical "no behavioural change" that introduces the types, does the
conversion of exporters and adds the type selection contract/logic.

A follow up adding PCIe P2P as its own type will be posted shortly after
this.

Patch Summary
-------------

Patch 1 renames dma_buf_(un)map_attachment() to dma_buf_(un)map_sgt().

Patches 2-6 introduce the DMA-buf mapping type system, initially
developed by Jason Gunthorpe.

Patches 7-19 convert exporters to allocate and return a
dma_buf_sgt_mapping embedding an sg_table instead of an sg_table
directly. These are mechanical and should not change behaviour.

The mechanical changes for smaller drivers has been split up by class
based on how the sg_table is allocated (eg. per-map, attachment, etc.)
with corner cases getting their own patch. Larger drivers, such as Intel
Xe and amdgpu also get their own patch.

Patch 20 removes the compatibility code the conversion needed and patch
21 adds KUnit tests for the matching rules.

Patches 22-24 add the contract and logic for making and changing
exporter types. Nothing currently exports two types, but a follow up
series building on this with a PCIe P2P type will be posted shortly.

Future Work
-----------

Several series will build on top of this. Current proposals are:

1. A PCIe P2P mapping type series to handle P2P negotiation.
2. An exporter type that allows conversion to iommufd or new DMA address
   list instead of sg_table.
3. Possible exporters/importers for private interconnects such
   as converting existing XGMI code to use this or future UALink
   exporters/importers.

Why mapping types
-----------------

Since its introduction, DMA-buf has only supported using scatterlist for
the exporter and importer to exchange address information.  This is not
sufficient for all use cases as dma_addr_t is a very specific and limited
type that should not be abused for things unrelated to the DMA API.

There are several motivations for addressing this now:
 1) VFIO to IOMMUFD and KVM requires a physical address, not a dma_addr_t
    scatterlist, it cannot be represented in the scatterlist structure
 2) xe vGPU requires the host driver to accept a DMABUF from VFIO of its
    own VF and convert it into an internal VRAM address on the PF
 3) We are starting to look at replacement datastructures for
    scatterlist
 4) Ideas around UALink/etc are suggesting not using the DMA API
 5) Private interconnects, such as XGMI, are forced to work around this using
    driver-specific hacks.

None of these can be achieved using scatterlist. This series introduces a
"mapping type" mechanism allowing dma-buf to negotiate compatibility between
different types and support alternate mapping options. See the description of
patch 2 for more details.

Selecting mapping type
----------------------

Importers and exporters list possible mapping types with the exporter
responsible for selecting the final type at pin/map time under the dma_resv
lock. A matching phase at attachment time eliminates types which statically
cannot be supported for a given exporter/importer.

At pin/map the exporter chooses one of the types matched, taking the other
attachments and the buffer's placement into account. That choice is held by the
core until the exporter explicitly invalidates the attachment. This ensures
unmap/remap cannot cause buffer movement and ping/pong.

The dma-buf core enforces this by calling select_mapping() only on the
first pin/map after attach or after an invalidate. The pin/map type
can therefore only change once the exporter has explicitly invalidated
the mapping and the choice is remembered across unmap/map.

Testing
-------

- KUnit dma-buf-mapping suite passes with new tests to address issue
  found by Karl previously.
- iommufd self-test passes.
- Every patch compile-tested with a config enabling all converted drivers.
- A small test exporting via vfio-pci and importing and pinning via iommufd.

Changes since v2
----------------

Link to v2: https://patch.msgid.link/20261105-dma_mapping_type_v4-v2-0-78b8dc9ae80a@nvidia.com

- PCIe P2P is no longer negotiated in this series. P2P keeps working
  through allow_peer2peer and attach->peer2peer exactly as today, a
  future series will introduce a PCIe P2P type to introduce negotiation.
- Exporters allocate a mapping object with sg_table embedded and their
  unmap takes it back.
- Exporter unmap callbacks take the DMA device from the mapping rather
  than from the attachment so a mapping that outlives an invalidate and a
  new selection is unmapped against the device it was made for.
- dma_buf_map_sgt() and dma_buf_unmap_sgt() are now wrappers around the
  generic dma_buf_mapping_map() and dma_buf_mapping_unmap().
- The single "Convert all the simple exporters" patch is split by class
  of change.
- A pre-existing leak in fsl_asrc_m2m's unmap was fixed as a side-effect.
- Added more KUnit tests to test corner cases identified by Karl in
  previous postings.

Changes since v1 [2]
--------------------

- Rebased onto v7.4-rc1. The rename also covers the new nvme-pci DMA-buf
  importer and devmem's switch to pinning. xe's dynamic-debug knob for the
  p2pdma distance check (3bc497a40178) is dropped, since the check is now
  done by the core at match time, and amdgpu's PREEMPT-domain import
  decision (78cdd96ab497) asks the negotiated match set instead of
  attach->peer2peer.
- The dma_buf_map_attachment()/dma_buf_unmap_attachment() rename is now the
  first patch, squashed into one, and uses the shorter dma_buf_map_sgt() and
  dma_buf_unmap_sgt() names (Christian [3]).
- Dropped dma_buf_mapping_attach() and the dma_buf_sgt_attach() /
  dma_buf_sgt_dynamic_attach() renames. Importers that support more than
  the SGT type list them in a static dma_buf_attach_ops
  ->supported_mapping_types table. The importing device is passed to the
  mapping types through the match arguments rather than stored in each
  entry. dma_buf_attach() and dma_buf_dynamic_attach() are unchanged for
  everyone else (Christian [3]).
- The selected type is an index into the attachment's match set, read
  through dma_buf_attach_map_type(), instead of a copy in attach->map_type.
  finish_match() fills the match set entry directly and select_mapping()
  returns the index.
- A failed pin or map only undoes a selection it made itself, so a map
  failing under a held pin keeps the pinned type and a failed first pin
  leaves the attachment unselected; KUnit tests for both.
- dma_buf_match_mapping() returns -EOPNOTSUPP when the two sides have no
  type in common, keeping -EINVAL for an empty list, which restores the
  error xe's own tests expect (Sashiko).
- New DMA_SGT_EXPORTER_REQUIRES_P2P flavour that requires a P2P capable
  importer without a pci_p2pdma_distance() check at match time, used by
  vfio and uverbs so iommufd's non-PCI device can still attach and so no
  pci_dev is dereferenced outside the dma_resv lock (Sashiko).
- The SGT P2P distance check is quiet, as it was in xe and amdgpu before
  the conversion (Sashiko).
- Fixed a NULL importer_ops dereference in dma_buf_dynamic_attach() and the
  NULL check order in dma_buf_unmap_sgt() (Sashiko).
- Signed-off-by added to every patch (Jason). Patch 4 is now authored by me
  with Jason as co-developer, as little of the original remains.
- Not addressed: the Tegra fault with CONFIG_DMABUF_DEBUG that Karl
  reported [4] is a pre-existing Tegra bug being fixed separately.

Changes since RFC v1 [1]
------------------------

- Dropped the PAL type and its vfio/iommufd users (RFC 21-25).
- Rebased onto v7.3-rc5: move_notify is now invalidate_mappings,
  CONFIG_DMABUF_MOVE_NOTIFY is gone, new amdxdna/panfrost/panthor
  exporters converted, kzalloc_obj().
- Patch 1 records the whole match set in a fixed-size array in the
  attachment; patch 3 initialises the attachment before match_mapping()
  (Karl); patch 15 holds a pci_dev reference instead of asserting the resv
  lock (Baolu).
- New patches 5, 6 and 7: exporter selection at pin/map with core-enforced
  stickiness, match set queries, and dma_buf_mapping_map()/unmap().
- xe and amdgpu conversions written against the final API; amdgpu's XGMI
  override rewrites the recorded match set entry.
- New patch 26 with KUnit tests for the selection contract.

[1] https://lore.kernel.org/all/0-v1-b5cab63049c0+191af-dmabuf_map_type_jgg@nvidia.com/
[2] https://lore.kernel.org/all/20261026053623.202642-1-apopple@nvidia.com/
[3] https://lore.kernel.org/all/3151a820-c583-4968-8440-673a9e390446@amd.com/
[4] https://lore.kernel.org/all/auAIO0y09SXk34CN@gmail.com/

---
Alistair Popple (14):
      dma-buf: Negotiate the mapping type in dma_buf_dynamic_attach()
      dma-buf: Allow single exporter drivers to avoid the match_mapping function
      dma-buf: Convert exporters allocating a table per map to the SGT mapping type
      iommufd/selftest: Convert the test exporter to the SGT mapping type
      dma-buf: Convert exporters keeping a table per attachment to the SGT mapping type
      drm: Convert drm_gem_map_dma_buf() and its users to the SGT mapping type
      udmabuf: Convert to the SGT mapping type
      drm/omapdrm: Convert to the SGT mapping type
      accel/ivpu: Convert the userptr exporter to the SGT mapping type
      dma-buf: Convert dma_buf_phys_vec_to_sgt() and its users to the SGT mapping type
      dma-buf: Remove unused SGT stuff from the common structures
      dma-buf: Let the exporter select mapping type at pin/map
      dma-buf: Add dma_buf_mapping_map() returning a type-tagged mapping and implement it for sgt
      dma-buf: Add kunit tests for mapping type selection

Jason Gunthorpe (10):
      treewide: Rename dma_buf_(un)map_attachment() to dma_buf_(un)map_sgt()
      dma-buf: Introduce DMA-buf mapping types
      dma-buf: Add the SGT DMA mapping type
      dma-buf: Route SGT related actions through the attachment's mapping type
      drm/xe/dma-buf: Use the SGT mapping type
      drm/amdgpu: Use the SGT mapping type
      drm/vmwgfx: Use match_mapping instead of dummy calls
      iio: buffer: convert to use the SGT mapping type
      functionfs: convert to use the SGT mapping type
      dma-buf: Add kunit tests for mapping type

 drivers/accel/amdxdna/amdxdna_cbuf.c               |   30 +-
 drivers/accel/amdxdna/amdxdna_gem.c                |   11 +-
 drivers/accel/amdxdna/amdxdna_ubuf.c               |   34 +-
 drivers/accel/habanalabs/common/memory.c           |   52 +-
 drivers/accel/ivpu/ivpu_gem.c                      |    6 +-
 drivers/accel/ivpu/ivpu_gem_userptr.c              |   29 +-
 drivers/accel/qaic/qaic_data.c                     |    6 +-
 drivers/dma-buf/Makefile                           |    1 +
 drivers/dma-buf/dma-buf-mapping-internal.h         |   39 +
 drivers/dma-buf/dma-buf-mapping.c                  |  307 +++++-
 drivers/dma-buf/dma-buf.c                          |  349 ++++--
 drivers/dma-buf/heaps/cma_heap.c                   |   33 +-
 drivers/dma-buf/heaps/system_heap.c                |   34 +-
 drivers/dma-buf/st-dma-mapping.c                   | 1106 ++++++++++++++++++++
 drivers/dma-buf/udmabuf.c                          |   71 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c        |   85 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c            |   29 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h            |    4 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c       |   30 +-
 drivers/gpu/drm/armada/armada_gem.c                |   52 +-
 drivers/gpu/drm/drm_prime.c                        |   78 +-
 drivers/gpu/drm/i915/gem/i915_gem_dmabuf.c         |   31 +-
 drivers/gpu/drm/i915/gem/i915_gem_object.c         |    2 +-
 .../gpu/drm/i915/gem/selftests/i915_gem_dmabuf.c   |    4 +-
 drivers/gpu/drm/i915/gem/selftests/mock_dmabuf.c   |   30 +-
 drivers/gpu/drm/msm/msm_gem_prime.c                |    7 +-
 drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c          |   51 +-
 drivers/gpu/drm/panfrost/panfrost_gem.c            |   27 +-
 drivers/gpu/drm/panthor/panthor_gem.c              |   25 +-
 drivers/gpu/drm/tegra/gem.c                        |   44 +-
 drivers/gpu/drm/virtio/virtgpu_drv.h               |    9 +-
 drivers/gpu/drm/virtio/virtgpu_prime.c             |   30 +-
 drivers/gpu/drm/virtio/virtgpu_vram.c              |   25 +-
 drivers/gpu/drm/vmwgfx/vmwgfx_prime.c              |   32 +-
 drivers/gpu/drm/xe/xe_bo.c                         |   16 +-
 drivers/gpu/drm/xe/xe_dma_buf.c                    |   62 +-
 drivers/gpu/drm/xe/xe_ttm_vram_mgr.c               |   22 +-
 drivers/gpu/drm/xe/xe_ttm_vram_mgr.h               |    2 +-
 drivers/iio/industrialio-buffer.c                  |   13 +-
 drivers/infiniband/core/umem_dmabuf.c              |    8 +-
 drivers/infiniband/core/uverbs_std_types_dmabuf.c  |   23 +-
 drivers/iommu/iommufd/selftest.c                   |   22 +-
 drivers/media/common/videobuf2/videobuf2-core.c    |    2 +-
 .../media/common/videobuf2/videobuf2-dma-contig.c  |   42 +-
 drivers/media/common/videobuf2/videobuf2-dma-sg.c  |   37 +-
 drivers/media/common/videobuf2/videobuf2-vmalloc.c |   32 +-
 .../media/platform/nvidia/tegra-vde/dmabuf-cache.c |    6 +-
 drivers/misc/fastrpc.c                             |   36 +-
 drivers/nvme/host/pci-dmabuf.c                     |    6 +-
 drivers/tee/tee_heap.c                             |   28 +-
 drivers/usb/gadget/function/f_fs.c                 |    9 +-
 drivers/vfio/pci/vfio_pci_dmabuf.c                 |   24 +-
 drivers/xen/gntdev-dmabuf.c                        |   75 +-
 include/drm/drm_prime.h                            |    8 +-
 include/linux/dma-buf-mapping.h                    |  366 ++++++-
 include/linux/dma-buf.h                            |  212 ++--
 include/trace/events/dma_buf.h                     |    2 +-
 io_uring/zcrx.c                                    |    6 +-
 net/core/devmem.c                                  |    8 +-
 samples/vfio-mdev/mbochs.c                         |   31 +-
 sound/soc/fsl/fsl_asrc_m2m.c                       |   32 +-
 61 files changed, 2972 insertions(+), 861 deletions(-)
---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261105-dma_mapping_type_v4-f71180b977e8

Best regards,
--  
Alistair Popple <apopple@nvidia.com>


             reply	other threads:[~2026-11-20  5:56 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20  5:55 Alistair Popple [this message]
2026-11-20  5:55 ` [PATCH v3 01/24] treewide: Rename dma_buf_(un)map_attachment() to dma_buf_(un)map_sgt() Alistair Popple
2026-11-20  5:55 ` [PATCH v3 02/24] dma-buf: Introduce DMA-buf mapping types Alistair Popple
2026-11-20  5:55 ` [PATCH v3 03/24] dma-buf: Add the SGT DMA mapping type Alistair Popple
2026-11-20  5:55 ` [PATCH v3 04/24] dma-buf: Negotiate the mapping type in dma_buf_dynamic_attach() Alistair Popple
2026-11-20  5:55 ` [PATCH v3 05/24] dma-buf: Route SGT related actions through the attachment's mapping type Alistair Popple
2026-11-20  5:55 ` [PATCH v3 06/24] dma-buf: Allow single exporter drivers to avoid the match_mapping function Alistair Popple
2026-11-20  5:55 ` [PATCH v3 07/24] dma-buf: Convert exporters allocating a table per map to the SGT mapping type Alistair Popple
2026-11-20  5:55 ` [PATCH v3 08/24] iommufd/selftest: Convert the test exporter " Alistair Popple
2026-11-20  5:55 ` [PATCH v3 09/24] dma-buf: Convert exporters keeping a table per attachment " Alistair Popple
2026-11-20  5:55 ` [PATCH v3 10/24] drm: Convert drm_gem_map_dma_buf() and its users " Alistair Popple
2026-11-20  5:55 ` [PATCH v3 11/24] drm/xe/dma-buf: Use " Alistair Popple
2026-11-20  5:55 ` [PATCH v3 12/24] drm/amdgpu: " Alistair Popple
2026-11-20  5:55 ` [PATCH v3 13/24] udmabuf: Convert to " Alistair Popple
2026-11-20  5:55 ` [PATCH v3 14/24] drm/omapdrm: " Alistair Popple
2026-11-20  5:55 ` [PATCH v3 15/24] accel/ivpu: Convert the userptr exporter " Alistair Popple
2026-11-20  5:55 ` [PATCH v3 16/24] drm/vmwgfx: Use match_mapping instead of dummy calls Alistair Popple
2026-11-20  5:55 ` [PATCH v3 17/24] dma-buf: Convert dma_buf_phys_vec_to_sgt() and its users to the SGT mapping type Alistair Popple
2026-11-20  5:55 ` [PATCH v3 18/24] iio: buffer: convert to use " Alistair Popple
2026-11-20  5:55 ` [PATCH v3 19/24] functionfs: " Alistair Popple
2026-11-20  5:55 ` [PATCH v3 20/24] dma-buf: Remove unused SGT stuff from the common structures Alistair Popple
2026-11-20  5:55 ` [PATCH v3 21/24] dma-buf: Add kunit tests for mapping type Alistair Popple
2026-11-20  5:56 ` [PATCH v3 22/24] dma-buf: Let the exporter select mapping type at pin/map Alistair Popple
2026-11-20  5:56 ` [PATCH v3 23/24] dma-buf: Add dma_buf_mapping_map() returning a type-tagged mapping and implement it for sgt Alistair Popple
2026-11-20  5:56 ` [PATCH v3 24/24] dma-buf: Add kunit tests for mapping type selection Alistair Popple

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261120-dma_mapping_type_v4-v3-0-c0183a409bdf@nvidia.com \
    --to=apopple@nvidia.com \
    --cc=aik@amd.com \
    --cc=alexander.deucher@amd.com \
    --cc=almasrymina@google.com \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=axboe@kernel.dk \
    --cc=baolu.lu@linux.intel.com \
    --cc=chaitanyak@nvidia.com \
    --cc=christian.koenig@amd.com \
    --cc=davem@davemloft.net \
    --cc=dongwon.kim@intel.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=edumazet@kernel.org \
    --cc=felix.kuehling@amd.com \
    --cc=hch@lst.de \
    --cc=horms@kernel.org \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=iommu@lists.linux.dev \
    --cc=jgg@ziepe.ca \
    --cc=jhubbard@nvidia.com \
    --cc=kbusch@kernel.org \
    --cc=kevin.tian@intel.com \
    --cc=kmehltretter@gmail.com \
    --cc=kuba@kernel.org \
    --cc=leon@kernel.org \
    --cc=linaro-mm-sig@lists.linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-nvme@lists.infradead.org \
    --cc=linux-rdma@vger.kernel.org \
    --cc=matthew.brost@intel.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=sagi@grimberg.me \
    --cc=simona.vetter@ffwll.ch \
    --cc=sumit.semwal@linaro.org \
    --cc=thomas.hellstrom@linux.intel.com \
    --cc=treding@nvidia.com \
    --cc=vivek.kasireddy@intel.com \
    --cc=yilun.xu@linux.intel.com \
    --cc=zhipingz@meta.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox