Skip to content

Commit 08cdc21

Browse files
committed
Merge tag 'for-linus-iommufd' of git://git.kernel.org/pub/scm/linux/kernel/git/jgg/iommufd
Pull iommufd implementation from Jason Gunthorpe: "iommufd is the user API to control the IOMMU subsystem as it relates to managing IO page tables that point at user space memory. It takes over from drivers/vfio/vfio_iommu_type1.c (aka the VFIO container) which is the VFIO specific interface for a similar idea. We see a broad need for extended features, some being highly IOMMU device specific: - Binding iommu_domain's to PASID/SSID - Userspace IO page tables, for ARM, x86 and S390 - Kernel bypassed invalidation of user page tables - Re-use of the KVM page table in the IOMMU - Dirty page tracking in the IOMMU - Runtime Increase/Decrease of IOPTE size - PRI support with faults resolved in userspace Many of these HW features exist to support VM use cases - for instance the combination of PASID, PRI and Userspace IO Page Tables allows an implementation of DMA Shared Virtual Addressing (vSVA) within a guest. Dirty tracking enables VM live migration with SRIOV devices and PASID support allow creating "scalable IOV" devices, among other things. As these features are fundamental to a VM platform they need to be uniformly exposed to all the driver families that do DMA into VMs, which is currently VFIO and VDPA" For more background, see the extended explanations in Jason's pull request: https://lore.kernel.org/lkml/[email protected]/ * tag 'for-linus-iommufd' of git://git.kernel.org/pub/scm/linux/kernel/git/jgg/iommufd: (62 commits) iommufd: Change the order of MSI setup iommufd: Improve a few unclear bits of code iommufd: Fix comment typos vfio: Move vfio group specific code into group.c vfio: Refactor dma APIs for emulated devices vfio: Wrap vfio group module init/clean code into helpers vfio: Refactor vfio_device open and close vfio: Make vfio_device_open() truly device specific vfio: Swap order of vfio_device_container_register() and open_device() vfio: Set device->group in helper function vfio: Create wrappers for group register/unregister vfio: Move the sanity check of the group to vfio_create_group() vfio: Simplify vfio_create_group() iommufd: Allow iommufd to supply /dev/vfio/vfio vfio: Make vfio_container optionally compiled vfio: Move container related MODULE_ALIAS statements into container.c vfio-iommufd: Support iommufd for emulated VFIO devices vfio-iommufd: Support iommufd for physical VFIO devices vfio-iommufd: Allow iommufd to be used in place of a container fd vfio: Use IOMMU_CAP_ENFORCE_CACHE_COHERENCY for vfio_file_enforced_coherent() ...
2 parents aa5ad10 + d6c55c0 commit 08cdc21

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+12804
-1466
lines changed

.clang-format

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,11 @@ ForEachMacros:
441441
- 'inet_lhash2_for_each_icsk'
442442
- 'inet_lhash2_for_each_icsk_continue'
443443
- 'inet_lhash2_for_each_icsk_rcu'
444+
- 'interval_tree_for_each_double_span'
445+
- 'interval_tree_for_each_span'
444446
- 'intlist__for_each_entry'
445447
- 'intlist__for_each_entry_safe'
448+
- 'iopt_for_each_contig_area'
446449
- 'kcore_copy__for_each_phdr'
447450
- 'key_for_each'
448451
- 'key_for_each_safe'

Documentation/userspace-api/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ place where this information is gathered.
2525
ebpf/index
2626
ioctl/index
2727
iommu
28+
iommufd
2829
media/index
2930
netlink/index
3031
sysfs-platform_profile

Documentation/userspace-api/ioctl/ioctl-number.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ Code Seq# Include File Comments
105105
'8' all SNP8023 advanced NIC card
106106
107107
';' 64-7F linux/vfio.h
108+
';' 80-FF linux/iommufd.h
108109
'=' 00-3f uapi/linux/ptp_clock.h <mailto:[email protected]>
109110
'@' 00-0F linux/radeonfb.h conflict!
110111
'@' 00-0F drivers/video/aty/aty128fb.c conflict!
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
.. SPDX-License-Identifier: GPL-2.0+
2+
3+
=======
4+
IOMMUFD
5+
=======
6+
7+
:Author: Jason Gunthorpe
8+
:Author: Kevin Tian
9+
10+
Overview
11+
========
12+
13+
IOMMUFD is the user API to control the IOMMU subsystem as it relates to managing
14+
IO page tables from userspace using file descriptors. It intends to be general
15+
and consumable by any driver that wants to expose DMA to userspace. These
16+
drivers are eventually expected to deprecate any internal IOMMU logic
17+
they may already/historically implement (e.g. vfio_iommu_type1.c).
18+
19+
At minimum iommufd provides universal support of managing I/O address spaces and
20+
I/O page tables for all IOMMUs, with room in the design to add non-generic
21+
features to cater to specific hardware functionality.
22+
23+
In this context the capital letter (IOMMUFD) refers to the subsystem while the
24+
small letter (iommufd) refers to the file descriptors created via /dev/iommu for
25+
use by userspace.
26+
27+
Key Concepts
28+
============
29+
30+
User Visible Objects
31+
--------------------
32+
33+
Following IOMMUFD objects are exposed to userspace:
34+
35+
- IOMMUFD_OBJ_IOAS, representing an I/O address space (IOAS), allowing map/unmap
36+
of user space memory into ranges of I/O Virtual Address (IOVA).
37+
38+
The IOAS is a functional replacement for the VFIO container, and like the VFIO
39+
container it copies an IOVA map to a list of iommu_domains held within it.
40+
41+
- IOMMUFD_OBJ_DEVICE, representing a device that is bound to iommufd by an
42+
external driver.
43+
44+
- IOMMUFD_OBJ_HW_PAGETABLE, representing an actual hardware I/O page table
45+
(i.e. a single struct iommu_domain) managed by the iommu driver.
46+
47+
The IOAS has a list of HW_PAGETABLES that share the same IOVA mapping and
48+
it will synchronize its mapping with each member HW_PAGETABLE.
49+
50+
All user-visible objects are destroyed via the IOMMU_DESTROY uAPI.
51+
52+
The diagram below shows relationship between user-visible objects and kernel
53+
datastructures (external to iommufd), with numbers referred to operations
54+
creating the objects and links::
55+
56+
_________________________________________________________
57+
| iommufd |
58+
| [1] |
59+
| _________________ |
60+
| | | |
61+
| | | |
62+
| | | |
63+
| | | |
64+
| | | |
65+
| | | |
66+
| | | [3] [2] |
67+
| | | ____________ __________ |
68+
| | IOAS |<--| |<------| | |
69+
| | | |HW_PAGETABLE| | DEVICE | |
70+
| | | |____________| |__________| |
71+
| | | | | |
72+
| | | | | |
73+
| | | | | |
74+
| | | | | |
75+
| | | | | |
76+
| |_________________| | | |
77+
| | | | |
78+
|_________|___________________|___________________|_______|
79+
| | |
80+
| _____v______ _______v_____
81+
| PFN storage | | | |
82+
|------------>|iommu_domain| |struct device|
83+
|____________| |_____________|
84+
85+
1. IOMMUFD_OBJ_IOAS is created via the IOMMU_IOAS_ALLOC uAPI. An iommufd can
86+
hold multiple IOAS objects. IOAS is the most generic object and does not
87+
expose interfaces that are specific to single IOMMU drivers. All operations
88+
on the IOAS must operate equally on each of the iommu_domains inside of it.
89+
90+
2. IOMMUFD_OBJ_DEVICE is created when an external driver calls the IOMMUFD kAPI
91+
to bind a device to an iommufd. The driver is expected to implement a set of
92+
ioctls to allow userspace to initiate the binding operation. Successful
93+
completion of this operation establishes the desired DMA ownership over the
94+
device. The driver must also set the driver_managed_dma flag and must not
95+
touch the device until this operation succeeds.
96+
97+
3. IOMMUFD_OBJ_HW_PAGETABLE is created when an external driver calls the IOMMUFD
98+
kAPI to attach a bound device to an IOAS. Similarly the external driver uAPI
99+
allows userspace to initiate the attaching operation. If a compatible
100+
pagetable already exists then it is reused for the attachment. Otherwise a
101+
new pagetable object and iommu_domain is created. Successful completion of
102+
this operation sets up the linkages among IOAS, device and iommu_domain. Once
103+
this completes the device could do DMA.
104+
105+
Every iommu_domain inside the IOAS is also represented to userspace as a
106+
HW_PAGETABLE object.
107+
108+
.. note::
109+
110+
Future IOMMUFD updates will provide an API to create and manipulate the
111+
HW_PAGETABLE directly.
112+
113+
A device can only bind to an iommufd due to DMA ownership claim and attach to at
114+
most one IOAS object (no support of PASID yet).
115+
116+
Kernel Datastructure
117+
--------------------
118+
119+
User visible objects are backed by following datastructures:
120+
121+
- iommufd_ioas for IOMMUFD_OBJ_IOAS.
122+
- iommufd_device for IOMMUFD_OBJ_DEVICE.
123+
- iommufd_hw_pagetable for IOMMUFD_OBJ_HW_PAGETABLE.
124+
125+
Several terminologies when looking at these datastructures:
126+
127+
- Automatic domain - refers to an iommu domain created automatically when
128+
attaching a device to an IOAS object. This is compatible to the semantics of
129+
VFIO type1.
130+
131+
- Manual domain - refers to an iommu domain designated by the user as the
132+
target pagetable to be attached to by a device. Though currently there are
133+
no uAPIs to directly create such domain, the datastructure and algorithms
134+
are ready for handling that use case.
135+
136+
- In-kernel user - refers to something like a VFIO mdev that is using the
137+
IOMMUFD access interface to access the IOAS. This starts by creating an
138+
iommufd_access object that is similar to the domain binding a physical device
139+
would do. The access object will then allow converting IOVA ranges into struct
140+
page * lists, or doing direct read/write to an IOVA.
141+
142+
iommufd_ioas serves as the metadata datastructure to manage how IOVA ranges are
143+
mapped to memory pages, composed of:
144+
145+
- struct io_pagetable holding the IOVA map
146+
- struct iopt_area's representing populated portions of IOVA
147+
- struct iopt_pages representing the storage of PFNs
148+
- struct iommu_domain representing the IO page table in the IOMMU
149+
- struct iopt_pages_access representing in-kernel users of PFNs
150+
- struct xarray pinned_pfns holding a list of pages pinned by in-kernel users
151+
152+
Each iopt_pages represents a logical linear array of full PFNs. The PFNs are
153+
ultimately derived from userspace VAs via an mm_struct. Once they have been
154+
pinned the PFNs are stored in IOPTEs of an iommu_domain or inside the pinned_pfns
155+
xarray if they have been pinned through an iommufd_access.
156+
157+
PFN have to be copied between all combinations of storage locations, depending
158+
on what domains are present and what kinds of in-kernel "software access" users
159+
exist. The mechanism ensures that a page is pinned only once.
160+
161+
An io_pagetable is composed of iopt_areas pointing at iopt_pages, along with a
162+
list of iommu_domains that mirror the IOVA to PFN map.
163+
164+
Multiple io_pagetable-s, through their iopt_area-s, can share a single
165+
iopt_pages which avoids multi-pinning and double accounting of page
166+
consumption.
167+
168+
iommufd_ioas is sharable between subsystems, e.g. VFIO and VDPA, as long as
169+
devices managed by different subsystems are bound to a same iommufd.
170+
171+
IOMMUFD User API
172+
================
173+
174+
.. kernel-doc:: include/uapi/linux/iommufd.h
175+
176+
IOMMUFD Kernel API
177+
==================
178+
179+
The IOMMUFD kAPI is device-centric with group-related tricks managed behind the
180+
scene. This allows the external drivers calling such kAPI to implement a simple
181+
device-centric uAPI for connecting its device to an iommufd, instead of
182+
explicitly imposing the group semantics in its uAPI as VFIO does.
183+
184+
.. kernel-doc:: drivers/iommu/iommufd/device.c
185+
:export:
186+
187+
.. kernel-doc:: drivers/iommu/iommufd/main.c
188+
:export:
189+
190+
VFIO and IOMMUFD
191+
----------------
192+
193+
Connecting a VFIO device to iommufd can be done in two ways.
194+
195+
First is a VFIO compatible way by directly implementing the /dev/vfio/vfio
196+
container IOCTLs by mapping them into io_pagetable operations. Doing so allows
197+
the use of iommufd in legacy VFIO applications by symlinking /dev/vfio/vfio to
198+
/dev/iommufd or extending VFIO to SET_CONTAINER using an iommufd instead of a
199+
container fd.
200+
201+
The second approach directly extends VFIO to support a new set of device-centric
202+
user API based on aforementioned IOMMUFD kernel API. It requires userspace
203+
change but better matches the IOMMUFD API semantics and easier to support new
204+
iommufd features when comparing it to the first approach.
205+
206+
Currently both approaches are still work-in-progress.
207+
208+
There are still a few gaps to be resolved to catch up with VFIO type1, as
209+
documented in iommufd_vfio_check_extension().
210+
211+
Future TODOs
212+
============
213+
214+
Currently IOMMUFD supports only kernel-managed I/O page table, similar to VFIO
215+
type1. New features on the radar include:
216+
217+
- Binding iommu_domain's to PASID/SSID
218+
- Userspace page tables, for ARM, x86 and S390
219+
- Kernel bypass'd invalidation of user page tables
220+
- Re-use of the KVM page table in the IOMMU
221+
- Dirty page tracking in the IOMMU
222+
- Runtime Increase/Decrease of IOPTE size
223+
- PRI support with faults resolved in userspace

MAINTAINERS

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10793,6 +10793,18 @@ F: drivers/iommu/dma-iommu.h
1079310793
F: drivers/iommu/iova.c
1079410794
F: include/linux/iova.h
1079510795

10796+
IOMMUFD
10797+
M: Jason Gunthorpe <[email protected]>
10798+
M: Kevin Tian <[email protected]>
10799+
10800+
S: Maintained
10801+
T: git git://git.kernel.org/pub/scm/linux/kernel/git/jgg/iommufd.git
10802+
F: Documentation/userspace-api/iommufd.rst
10803+
F: drivers/iommu/iommufd/
10804+
F: include/linux/iommufd.h
10805+
F: include/uapi/linux/iommufd.h
10806+
F: tools/testing/selftests/iommu/
10807+
1079610808
IOMMU SUBSYSTEM
1079710809
M: Joerg Roedel <[email protected]>
1079810810
M: Will Deacon <[email protected]>

drivers/dma/idxd/cdev.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include <linux/pci.h>
77
#include <linux/device.h>
88
#include <linux/sched/task.h>
9-
#include <linux/intel-svm.h>
109
#include <linux/io-64-nonatomic-lo-hi.h>
1110
#include <linux/cdev.h>
1211
#include <linux/fs.h>
@@ -100,7 +99,7 @@ static int idxd_cdev_open(struct inode *inode, struct file *filp)
10099
filp->private_data = ctx;
101100

102101
if (device_user_pasid_enabled(idxd)) {
103-
sva = iommu_sva_bind_device(dev, current->mm, NULL);
102+
sva = iommu_sva_bind_device(dev, current->mm);
104103
if (IS_ERR(sva)) {
105104
rc = PTR_ERR(sva);
106105
dev_err(dev, "pasid allocation failed: %d\n", rc);

drivers/dma/idxd/init.c

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include <linux/io-64-nonatomic-lo-hi.h>
1515
#include <linux/device.h>
1616
#include <linux/idr.h>
17-
#include <linux/intel-svm.h>
1817
#include <linux/iommu.h>
1918
#include <uapi/linux/idxd.h>
2019
#include <linux/dmaengine.h>
@@ -502,29 +501,7 @@ static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_d
502501

503502
static int idxd_enable_system_pasid(struct idxd_device *idxd)
504503
{
505-
int flags;
506-
unsigned int pasid;
507-
struct iommu_sva *sva;
508-
509-
flags = SVM_FLAG_SUPERVISOR_MODE;
510-
511-
sva = iommu_sva_bind_device(&idxd->pdev->dev, NULL, &flags);
512-
if (IS_ERR(sva)) {
513-
dev_warn(&idxd->pdev->dev,
514-
"iommu sva bind failed: %ld\n", PTR_ERR(sva));
515-
return PTR_ERR(sva);
516-
}
517-
518-
pasid = iommu_sva_get_pasid(sva);
519-
if (pasid == IOMMU_PASID_INVALID) {
520-
iommu_sva_unbind_device(sva);
521-
return -ENODEV;
522-
}
523-
524-
idxd->sva = sva;
525-
idxd->pasid = pasid;
526-
dev_dbg(&idxd->pdev->dev, "system pasid: %u\n", pasid);
527-
return 0;
504+
return -EOPNOTSUPP;
528505
}
529506

530507
static void idxd_disable_system_pasid(struct idxd_device *idxd)

drivers/gpu/drm/i915/gvt/kvmgt.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -669,9 +669,6 @@ static int intel_vgpu_open_device(struct vfio_device *vfio_dev)
669669

670670
vgpu->attached = true;
671671

672-
kvmgt_protect_table_init(vgpu);
673-
gvt_cache_init(vgpu);
674-
675672
vgpu->track_node.track_write = kvmgt_page_track_write;
676673
vgpu->track_node.track_flush_slot = kvmgt_page_track_flush_slot;
677674
kvm_get_kvm(vgpu->vfio_device.kvm);
@@ -715,6 +712,11 @@ static void intel_vgpu_close_device(struct vfio_device *vfio_dev)
715712
kvmgt_protect_table_destroy(vgpu);
716713
gvt_cache_destroy(vgpu);
717714

715+
WARN_ON(vgpu->nr_cache_entries);
716+
717+
vgpu->gfn_cache = RB_ROOT;
718+
vgpu->dma_addr_cache = RB_ROOT;
719+
718720
intel_vgpu_release_msi_eventfd_ctx(vgpu);
719721

720722
vgpu->attached = false;
@@ -1445,9 +1447,17 @@ static int intel_vgpu_init_dev(struct vfio_device *vfio_dev)
14451447
struct intel_vgpu *vgpu = vfio_dev_to_vgpu(vfio_dev);
14461448
struct intel_vgpu_type *type =
14471449
container_of(mdev->type, struct intel_vgpu_type, type);
1450+
int ret;
14481451

14491452
vgpu->gvt = kdev_to_i915(mdev->type->parent->dev)->gvt;
1450-
return intel_gvt_create_vgpu(vgpu, type->conf);
1453+
ret = intel_gvt_create_vgpu(vgpu, type->conf);
1454+
if (ret)
1455+
return ret;
1456+
1457+
kvmgt_protect_table_init(vgpu);
1458+
gvt_cache_init(vgpu);
1459+
1460+
return 0;
14511461
}
14521462

14531463
static void intel_vgpu_release_dev(struct vfio_device *vfio_dev)
@@ -1468,6 +1478,9 @@ static const struct vfio_device_ops intel_vgpu_dev_ops = {
14681478
.mmap = intel_vgpu_mmap,
14691479
.ioctl = intel_vgpu_ioctl,
14701480
.dma_unmap = intel_vgpu_dma_unmap,
1481+
.bind_iommufd = vfio_iommufd_emulated_bind,
1482+
.unbind_iommufd = vfio_iommufd_emulated_unbind,
1483+
.attach_ioas = vfio_iommufd_emulated_attach_ioas,
14711484
};
14721485

14731486
static int intel_vgpu_probe(struct mdev_device *mdev)

drivers/iommu/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ config MSM_IOMMU
188188

189189
source "drivers/iommu/amd/Kconfig"
190190
source "drivers/iommu/intel/Kconfig"
191+
source "drivers/iommu/iommufd/Kconfig"
191192

192193
config IRQ_REMAP
193194
bool "Support for Interrupt Remapping"

0 commit comments

Comments
 (0)