Skip to content

Commit 01f70cb

Browse files
committed
iommufd: Allow iommufd to supply /dev/vfio/vfio
If the VFIO container is compiled out, give a kconfig option for iommufd to provide the miscdev node with the same name and permissions as vfio uses. The compatibility node supports the same ioctls as VFIO and automatically enables the VFIO compatible pinned page accounting mode. Link: https://lore.kernel.org/r/[email protected] Reviewed-by: Kevin Tian <[email protected]> Reviewed-by: Yi Liu <[email protected]> Reviewed-by: Alex Williamson <[email protected]> Tested-by: Alex Williamson <[email protected]> Tested-by: Nicolin Chen <[email protected]> Tested-by: Yi Liu <[email protected]> Tested-by: Lixiao Yang <[email protected]> Tested-by: Matthew Rosato <[email protected]> Tested-by: Yu He <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]>
1 parent e5a9ec7 commit 01f70cb

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

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");

0 commit comments

Comments
 (0)