Skip to content

RFC Provide a new two step DMA API mapping API

Leon Romanovsky kvm linux-block linux-iommu linux-mm linux-nvme linux-pci linux-rdma lkml 2 versions – New

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v2 RFC Latest21 patches 0 repliesNew
  2. v1 RFC18 patches 32 repliesSuperseded

Changes in v2 from the cover letter · 20 lines

* Remove attr field from dma_iova_attrs
* Added attr parameter to every new API call except alloc_iova
* Embed use_iova boolean into struct dma_iova_state
* Embed struct dma_memory_type into struct dma_iova_state
* Changed function signatures and added new function to initialize state
* Combined dma_set_memory_type() and dma_can_use_iova() to one function, they were used together anyway
* Made dma_start_range/dma_end_range to be NOP for non-iommu case
* Restructured the code to avoid inclusion of linux/pci.h in global header
* Based on DMA static calls series
* Changed iommu/dma functions to get parameters instead of dma_iova_state
* Removed iommu domain pointer from dma_iova_state
* dma_link_range now returns DMA address
* Rewrote NVMe patch
v1: https://lore.kernel.org/all/[email protected]
* Rewrote cover letter
* Changed to API as proposed
https://lore.kernel.org/linux-rdma/[email protected]/
* Removed IB DMA wrappers and use DMA API directly
v0: https://lore.kernel.org/all/[email protected]
-------------------------------------------------------------------------

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/21iommu/dma: Provide an interface to allow preallocate IOVAdrivers/iommu/dma-iommu.c include/linux/iommu-dma.h+56 -120 replies
2/21iommu/dma: Implement link/unlink ranges callbacksdrivers/iommu/dma-iommu.c include/linux/iommu-dma.h+111 -00 replies
3/21iommu/dma: Add check if IOVA can be useddrivers/iommu/dma-iommu.c drivers/pci/p2pdma.c include/linux/dma-map-ops.h +1 more+37 -20 replies
4/21dma-mapping: initialize IOVA state structinclude/linux/dma-mapping.h+14 -00 replies
5/21dma-mapping: provide an interface to allocate IOVAinclude/linux/dma-mapping.h kernel/dma/mapping.c+53 -00 replies
6/21dma-mapping: set and query DMA IOVA stateinclude/linux/dma-mapping.h kernel/dma/mapping.c+50 -00 replies
7/21dma-mapping: implement link range APIinclude/linux/dma-mapping.h kernel/dma/mapping.c+86 -00 replies
8/21mm/hmm: let users to tag specific PFN with DMA mapped bitinclude/linux/hmm.h mm/hmm.c+25 -130 replies
9/21dma-mapping: provide callbacks to link/unlink HMM PFNs to specific IOVAinclude/linux/dma-mapping.h kernel/dma/mapping.c+117 -00 replies
10/21RDMA/umem: Preallocate and cache IOVA for UMEM ODPdrivers/infiniband/core/umem_odp.c include/rdma/ib_umem_odp.h+13 -10 replies
11/21RDMA/umem: Store ODP access mask information in PFNdrivers/infiniband/core/umem_odp.c drivers/infiniband/hw/mlx5/mlx5_ib.h drivers/infiniband/hw/mlx5/odp.c +1 more+59 -910 replies
12/21RDMA/core: Separate DMA mapping to caching IOVA and page linkagedrivers/infiniband/core/umem_odp.c drivers/infiniband/hw/mlx5/odp.c include/rdma/ib_umem_odp.h +1 more+14 -690 replies
13/21RDMA/umem: Prevent UMEM ODP creation with SWIOTLBdrivers/infiniband/core/umem_odp.c drivers/iommu/dma-iommu.c+40 -390 replies
14/21vfio/mlx5: Explicitly use number of pages instead of allocated lengthdrivers/vfio/pci/mlx5/cmd.c drivers/vfio/pci/mlx5/cmd.h drivers/vfio/pci/mlx5/main.c+57 -410 replies
15/21vfio/mlx5: Rewrite create mkey flow to allow better code reusedrivers/vfio/pci/mlx5/cmd.c drivers/vfio/pci/mlx5/cmd.h+90 -700 replies
16/21vfio/mlx5: Explicitly store page listdrivers/vfio/pci/mlx5/cmd.c drivers/vfio/pci/mlx5/cmd.h+13 -170 replies
17/21vfio/mlx5: Convert vfio to use DMA link APIdrivers/vfio/pci/mlx5/cmd.c drivers/vfio/pci/mlx5/cmd.h drivers/vfio/pci/mlx5/main.c+112 -1400 replies
18/21nvme-pci: remove optimizations for single DMA entrydrivers/nvme/host/pci.c+0 -690 replies
19/21nvme-pci: precalculate number of DMA entries for each commanddrivers/nvme/host/pci.c+20 -00 replies
20/21nvme-pci: use new dma APIdrivers/nvme/host/pci.c+215 -1390 replies
21/21nvme-pci: don't allow mapping of bvecs with offsetdrivers/nvme/host/pci.c+3 -00 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v2 yet.

Read the v2 thread

Cover letter [RFC v2 00/21] Provide a new two step DMA API mapping API

Instead of waiting and waiting when I finally fix NVMe patch which doesn't work correctly in DMA direct mode (it causes to failures in module reload), I decided to send the series as is to at least get a feedback about API and the overall direction.

Thanks

-------------------------------------------------------------------------
Changelog:
v2:
* Remove attr field from dma_iova_attrs
* Added attr parameter to every new API call except alloc_iova
* Embed use_iova boolean into struct dma_iova_state
* Embed struct dma_memory_type into struct dma_iova_state
* Changed function signatures and added new function to initialize state
* Combined dma_set_memory_type() and dma_can_use_iova() to one function, they were used together anyway
* Made dma_start_range/dma_end_range to be NOP for non-iommu case
* Restructured the code to avoid inclusion of linux/pci.h in global header
* Based on DMA static calls series
* Changed iommu/dma functions to get parameters instead of dma_iova_state
* Removed iommu domain pointer from dma_iova_state
* dma_link_range now returns DMA address
* Rewrote NVMe patch
v1: https://lore.kernel.org/all/[email protected]
* Rewrote cover letter
* Changed to API as proposed
https://lore.kernel.org/linux-rdma/[email protected]/
* Removed IB DMA wrappers and use DMA API directly
v0: https://lore.kernel.org/all/[email protected]
-------------------------------------------------------------------------

Currently the only efficient way to map a complex memory description through the DMA API is by using the scatterlist APIs. The SG APIs are unique in that they efficiently combine the two fundamental operations of sizing and allocating a large IOVA window from the IOMMU and processing all the per-address swiotlb/flushing/p2p/map details.

This uniqueness has been a long standing pain point as the scatterlist API is mandatory, but expensive to use. It prevents any kind of optimization or feature improvement (such as avoiding struct page for P2P) due to the impossibility of improving the scatterlist.

Several approaches have been explored to expand the DMA API with additional scatterlist-like structures (BIO[1], rlist[2]), instead split up the DMAAPI to allow callers to bring their own data structure.

The API is split up into parts:
- dma_alloc_iova() / dma_free_iova()
To do any pre-allocation required. This is done based on the caller supplying some details about how much IOMMU address space it would need in worst case.
- dma_link_range() / dma_unlink_range()
Perform the actual mapping into the pre-allocated IOVA. This is very similar to dma_map_page().

A driver will extent its mapping size using its own data structure, such as BIO, to request the required IOVA. Then it will iterate directly over it's data structure to DMA map each range. The result can then be stored directly into the HW specific DMA list. No intermediate scatterlist is required.

In this series, examples of three users are converted to the new API to show the benefits. Each user has a unique flow:
1. RDMA ODP is an example of "SVA mirroring" using HMM that needs to dynamically map/unmap large numbers of single pages. This becomes significantly faster in the IOMMU case as the map/unmap is now just a page table walk, the IOVA allocation is pre-computed once. Significant amounts of memory are saved as there is no longer a need to store the dma_addr_t of each page.
2. VFIO PCI live migration code is building a very large "page list" for the device. Instead of allocating a scatter list entry per allocated page it can just allocate an array of 'struct page *', saving a large amount of memory.
3. NVMe PCI demonstrates how a BIO can be converted to a HW scatter list without having to allocate then populate an intermediate SG table.

This step is first along a path to provide alternatives to scatterlist and solve some of the abuses and design mistakes, for instance in DMABUF's P2P support.

The ODP and VFIO versions are complete and fully tested, they can be the users of the new API to merge it. The NVMe requires more work.

[1] https://lore.kernel.org/all/169772852492.5232.17148564580779995849.stgit@klimt.1015granger.net/
[2] https://lore.kernel.org/all/[email protected]/

Thanks

Leon Romanovsky (21):
iommu/dma: Provide an interface to allow preallocate IOVA iommu/dma: Implement link/unlink ranges callbacks iommu/dma: Add check if IOVA can be used
dma-mapping: initialize IOVA state struct
dma-mapping: provide an interface to allocate IOVA
dma-mapping: set and query DMA IOVA state
dma-mapping: implement link range API
mm/hmm: let users to tag specific PFN with DMA mapped bit
dma-mapping: provide callbacks to link/unlink HMM PFNs to specific
IOVA
RDMA/umem: Preallocate and cache IOVA for UMEM ODP RDMA/umem: Store ODP access mask information in PFN RDMA/core: Separate DMA mapping to caching IOVA and page linkage RDMA/umem: Prevent UMEM ODP creation with SWIOTLB vfio/mlx5: Explicitly use number of pages instead of allocated length vfio/mlx5: Rewrite create mkey flow to allow better code reuse vfio/mlx5: Explicitly store page list
vfio/mlx5: Convert vfio to use DMA link API
nvme-pci: remove optimizations for single DMA entry
nvme-pci: precalculate number of DMA entries for each command
nvme-pci: use new dma API
nvme-pci: don't allow mapping of bvecs with offset

 drivers/infiniband/core/umem_odp.c   | 216 +++++---------
 drivers/infiniband/hw/mlx5/mlx5_ib.h |   1 +
 drivers/infiniband/hw/mlx5/odp.c     |  44 +--
 drivers/iommu/dma-iommu.c            | 165 ++++++++++-
 drivers/nvme/host/pci.c              | 428 ++++++++++++++-------------
 drivers/pci/p2pdma.c                 |   4 +-
 drivers/vfio/pci/mlx5/cmd.c          | 312 ++++++++++---------
 drivers/vfio/pci/mlx5/cmd.h          |  23 +-
 drivers/vfio/pci/mlx5/main.c         |  89 +++---
 include/linux/dma-map-ops.h          |   7 +
 include/linux/dma-mapping.h          |  85 ++++++
 include/linux/hmm.h                  |   4 +
 include/linux/iommu-dma.h            |  43 +++
 include/rdma/ib_umem_odp.h           |  23 +-
 kernel/dma/mapping.c                 | 232 +++++++++++++++
 mm/hmm.c                             |  34 ++-
 16 files changed, 1096 insertions(+), 614 deletions(-)
Signature · 1 line
2.46.0