Skip to content

Commit 169dd5c

Browse files
committed
Merge patch series "Connect VFIO to IOMMUFD"
Jason Gunthorpe <[email protected]> says: ================== This series provides an alternative container layer for VFIO implemented using iommufd. This is optional, if CONFIG_IOMMUFD is not set then it will not be compiled in. At this point iommufd can be injected by passing in a iommfd FD to VFIO_GROUP_SET_CONTAINER which will use the VFIO compat layer in iommufd to obtain the compat IOAS and then connect up all the VFIO drivers as appropriate. This is temporary stopping point, a following series will provide a way to directly open a VFIO device FD and directly connect it to IOMMUFD using native ioctls that can expose the IOMMUFD features like hwpt, future vPASID and dynamic attachment. This series, in compat mode, has passed all the qemu tests we have available, including the test suites for the Intel GVT mdev. Aside from the temporary limitation with P2P memory this is belived to be fully compatible with VFIO. This is on github: https://github.com/jgunthorpe/linux/commits/vfio_iommufd It requires the iommufd series: https://lore.kernel.org/r/[email protected] ================== Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jason Gunthorpe <[email protected]>
2 parents 2a54e34 + 01f70cb commit 169dd5c

File tree

18 files changed

+714
-196
lines changed

18 files changed

+714
-196
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,6 +1484,9 @@ static const struct vfio_device_ops intel_vgpu_dev_ops = {
14841484
.mmap = intel_vgpu_mmap,
14851485
.ioctl = intel_vgpu_ioctl,
14861486
.dma_unmap = intel_vgpu_dma_unmap,
1487+
.bind_iommufd = vfio_iommufd_emulated_bind,
1488+
.unbind_iommufd = vfio_iommufd_emulated_unbind,
1489+
.attach_ioas = vfio_iommufd_emulated_attach_ioas,
14871490
};
14881491

14891492
static int intel_vgpu_probe(struct mdev_device *mdev)

drivers/iommu/iommufd/Kconfig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,26 @@ config IOMMUFD
1212
If you don't know what to do here, say N.
1313

1414
if IOMMUFD
15+
config IOMMUFD_VFIO_CONTAINER
16+
bool "IOMMUFD provides the VFIO container /dev/vfio/vfio"
17+
depends on VFIO && !VFIO_CONTAINER
18+
default VFIO && !VFIO_CONTAINER
19+
help
20+
IOMMUFD will provide /dev/vfio/vfio instead of VFIO. This relies on
21+
IOMMUFD providing compatibility emulation to give the same ioctls.
22+
It provides an option to build a kernel with legacy VFIO components
23+
removed.
24+
25+
IOMMUFD VFIO container emulation is known to lack certain features
26+
of the native VFIO container, such as no-IOMMU support, peer-to-peer
27+
DMA mapping, PPC IOMMU support, as well as other potentially
28+
undiscovered gaps. This option is currently intended for the
29+
purpose of testing IOMMUFD with unmodified userspace supporting VFIO
30+
and making use of the Type1 VFIO IOMMU backend. General purpose
31+
enabling of this option is currently discouraged.
32+
33+
Unless testing IOMMUFD, say N here.
34+
1535
config IOMMUFD_TEST
1636
bool "IOMMU Userspace API Test support"
1737
depends on DEBUG_KERNEL

drivers/iommu/iommufd/main.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
#include <uapi/linux/iommufd.h>
1919
#include <linux/iommufd.h>
2020

21+
#include "io_pagetable.h"
2122
#include "iommufd_private.h"
2223
#include "iommufd_test.h"
2324

2425
struct iommufd_object_ops {
2526
void (*destroy)(struct iommufd_object *obj);
2627
};
2728
static const struct iommufd_object_ops iommufd_object_ops[];
29+
static struct miscdevice vfio_misc_dev;
2830

2931
struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx,
3032
size_t size,
@@ -170,6 +172,16 @@ static int iommufd_fops_open(struct inode *inode, struct file *filp)
170172
if (!ictx)
171173
return -ENOMEM;
172174

175+
/*
176+
* For compatibility with VFIO when /dev/vfio/vfio is opened we default
177+
* to the same rlimit accounting as vfio uses.
178+
*/
179+
if (IS_ENABLED(CONFIG_IOMMUFD_VFIO_CONTAINER) &&
180+
filp->private_data == &vfio_misc_dev) {
181+
ictx->account_mode = IOPT_PAGES_ACCOUNT_MM;
182+
pr_info_once("IOMMUFD is providing /dev/vfio/vfio, not VFIO.\n");
183+
}
184+
173185
xa_init_flags(&ictx->objects, XA_FLAGS_ALLOC1 | XA_FLAGS_ACCOUNT);
174186
ictx->file = filp;
175187
filp->private_data = ictx;
@@ -400,25 +412,49 @@ static struct miscdevice iommu_misc_dev = {
400412
.mode = 0660,
401413
};
402414

415+
416+
static struct miscdevice vfio_misc_dev = {
417+
.minor = VFIO_MINOR,
418+
.name = "vfio",
419+
.fops = &iommufd_fops,
420+
.nodename = "vfio/vfio",
421+
.mode = 0666,
422+
};
423+
403424
static int __init iommufd_init(void)
404425
{
405426
int ret;
406427

407428
ret = misc_register(&iommu_misc_dev);
408429
if (ret)
409430
return ret;
431+
432+
if (IS_ENABLED(CONFIG_IOMMUFD_VFIO_CONTAINER)) {
433+
ret = misc_register(&vfio_misc_dev);
434+
if (ret)
435+
goto err_misc;
436+
}
410437
iommufd_test_init();
411438
return 0;
439+
err_misc:
440+
misc_deregister(&iommu_misc_dev);
441+
return ret;
412442
}
413443

414444
static void __exit iommufd_exit(void)
415445
{
416446
iommufd_test_exit();
447+
if (IS_ENABLED(CONFIG_IOMMUFD_VFIO_CONTAINER))
448+
misc_deregister(&vfio_misc_dev);
417449
misc_deregister(&iommu_misc_dev);
418450
}
419451

420452
module_init(iommufd_init);
421453
module_exit(iommufd_exit);
422454

455+
#if IS_ENABLED(CONFIG_IOMMUFD_VFIO_CONTAINER)
456+
MODULE_ALIAS_MISCDEV(VFIO_MINOR);
457+
MODULE_ALIAS("devname:vfio/vfio");
458+
#endif
423459
MODULE_DESCRIPTION("I/O Address Space Management for passthrough devices");
424460
MODULE_LICENSE("GPL");

drivers/s390/cio/vfio_ccw_ops.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,9 @@ static const struct vfio_device_ops vfio_ccw_dev_ops = {
588588
.ioctl = vfio_ccw_mdev_ioctl,
589589
.request = vfio_ccw_mdev_request,
590590
.dma_unmap = vfio_ccw_dma_unmap,
591+
.bind_iommufd = vfio_iommufd_emulated_bind,
592+
.unbind_iommufd = vfio_iommufd_emulated_unbind,
593+
.attach_ioas = vfio_iommufd_emulated_attach_ioas,
591594
};
592595

593596
struct mdev_driver vfio_ccw_mdev_driver = {

drivers/s390/crypto/vfio_ap_ops.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,6 +1805,9 @@ static const struct vfio_device_ops vfio_ap_matrix_dev_ops = {
18051805
.close_device = vfio_ap_mdev_close_device,
18061806
.ioctl = vfio_ap_mdev_ioctl,
18071807
.dma_unmap = vfio_ap_mdev_dma_unmap,
1808+
.bind_iommufd = vfio_iommufd_emulated_bind,
1809+
.unbind_iommufd = vfio_iommufd_emulated_unbind,
1810+
.attach_ioas = vfio_iommufd_emulated_attach_ioas,
18081811
};
18091812

18101813
static struct mdev_driver vfio_ap_matrix_driver = {

drivers/vfio/Kconfig

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,28 @@
22
menuconfig VFIO
33
tristate "VFIO Non-Privileged userspace driver framework"
44
select IOMMU_API
5-
select VFIO_IOMMU_TYPE1 if MMU && (X86 || S390 || ARM || ARM64)
5+
depends on IOMMUFD || !IOMMUFD
66
select INTERVAL_TREE
7+
select VFIO_CONTAINER if IOMMUFD=n
78
help
89
VFIO provides a framework for secure userspace device drivers.
910
See Documentation/driver-api/vfio.rst for more details.
1011

1112
If you don't know what to do here, say N.
1213

1314
if VFIO
15+
config VFIO_CONTAINER
16+
bool "Support for the VFIO container /dev/vfio/vfio"
17+
select VFIO_IOMMU_TYPE1 if MMU && (X86 || S390 || ARM || ARM64)
18+
default y
19+
help
20+
The VFIO container is the classic interface to VFIO for establishing
21+
IOMMU mappings. If N is selected here then IOMMUFD must be used to
22+
manage the mappings.
23+
24+
Unless testing IOMMUFD say Y here.
25+
26+
if VFIO_CONTAINER
1427
config VFIO_IOMMU_TYPE1
1528
tristate
1629
default n
@@ -20,16 +33,6 @@ config VFIO_IOMMU_SPAPR_TCE
2033
depends on SPAPR_TCE_IOMMU
2134
default VFIO
2235

23-
config VFIO_SPAPR_EEH
24-
tristate
25-
depends on EEH && VFIO_IOMMU_SPAPR_TCE
26-
default VFIO
27-
28-
config VFIO_VIRQFD
29-
tristate
30-
select EVENTFD
31-
default n
32-
3336
config VFIO_NOIOMMU
3437
bool "VFIO No-IOMMU support"
3538
help
@@ -43,6 +46,17 @@ config VFIO_NOIOMMU
4346
this mode since there is no IOMMU to provide DMA translation.
4447

4548
If you don't know what to do here, say N.
49+
endif
50+
51+
config VFIO_SPAPR_EEH
52+
tristate
53+
depends on EEH && VFIO_IOMMU_SPAPR_TCE
54+
default VFIO
55+
56+
config VFIO_VIRQFD
57+
tristate
58+
select EVENTFD
59+
default n
4660

4761
source "drivers/vfio/pci/Kconfig"
4862
source "drivers/vfio/platform/Kconfig"

drivers/vfio/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ vfio_virqfd-y := virqfd.o
44
obj-$(CONFIG_VFIO) += vfio.o
55

66
vfio-y += vfio_main.o \
7-
iova_bitmap.o \
8-
container.o
7+
iova_bitmap.o
8+
vfio-$(CONFIG_IOMMUFD) += iommufd.o
9+
vfio-$(CONFIG_VFIO_CONTAINER) += container.o
910

1011
obj-$(CONFIG_VFIO_VIRQFD) += vfio_virqfd.o
1112
obj-$(CONFIG_VFIO_IOMMU_TYPE1) += vfio_iommu_type1.o

0 commit comments

Comments
 (0)