Skip to content

Commit 8b6f173

Browse files
yiliu1765awilliam
authored andcommitted
vfio: Add cdev for vfio_device
This adds cdev support for vfio_device. It allows the user to directly open a vfio device w/o using the legacy container/group interface, as a prerequisite for supporting new iommu features like nested translation and etc. The device fd opened in this manner doesn't have the capability to access the device as the fops open() doesn't open the device until the successful VFIO_DEVICE_BIND_IOMMUFD ioctl which will be added in a later patch. With this patch, devices registered to vfio core would have both the legacy group and the new device interfaces created. - group interface : /dev/vfio/$groupID - device interface: /dev/vfio/devices/vfioX - normal device ("X" is a unique number across vfio devices) For a given device, the user can identify the matching vfioX by searching the vfio-dev folder under the sysfs path of the device. Take PCI device (0000:6a:01.0) as an example, /sys/bus/pci/devices/0000\:6a\:01.0/vfio-dev/vfioX implies the matching vfioX under /dev/vfio/devices/, and vfio-dev/vfioX/dev contains the major:minor number of the matching /dev/vfio/devices/vfioX. The user can get device fd by opening the /dev/vfio/devices/vfioX. The vfio_device cdev logic in this patch: *) __vfio_register_dev() path ends up doing cdev_device_add() for each vfio_device if VFIO_DEVICE_CDEV configured. *) vfio_unregister_group_dev() path does cdev_device_del(); cdev interface does not support noiommu devices, so VFIO only creates the legacy group interface for the physical devices that do not have IOMMU. noiommu users should use the legacy group interface. Reviewed-by: Kevin Tian <[email protected]> Reviewed-by: Jason Gunthorpe <[email protected]> Tested-by: Terrence Xu <[email protected]> Tested-by: Nicolin Chen <[email protected]> Tested-by: Matthew Rosato <[email protected]> Tested-by: Yanting Jiang <[email protected]> Tested-by: Shameer Kolothum <[email protected]> Tested-by: Zhenzhong Duan <[email protected]> Signed-off-by: Yi Liu <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alex Williamson <[email protected]>
1 parent 38c2454 commit 8b6f173

File tree

6 files changed

+151
-4
lines changed

6 files changed

+151
-4
lines changed

drivers/vfio/Kconfig

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

1414
if VFIO
15+
config VFIO_DEVICE_CDEV
16+
bool "Support for the VFIO cdev /dev/vfio/devices/vfioX"
17+
depends on IOMMUFD && !SPAPR_TCE_IOMMU
18+
help
19+
The VFIO device cdev is another way for userspace to get device
20+
access. Userspace gets device fd by opening device cdev under
21+
/dev/vfio/devices/vfioX, and then bind the device fd with an iommufd
22+
to set up secure DMA context for device access. This interface does
23+
not support noiommu.
24+
25+
If you don't know what to do here, say N.
26+
1527
config VFIO_CONTAINER
1628
bool "Support for the VFIO container /dev/vfio/vfio"
1729
select VFIO_IOMMU_TYPE1 if MMU && (X86 || S390 || ARM || ARM64)

drivers/vfio/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ obj-$(CONFIG_VFIO) += vfio.o
44
vfio-y += vfio_main.o \
55
group.o \
66
iova_bitmap.o
7+
vfio-$(CONFIG_VFIO_DEVICE_CDEV) += device_cdev.o
78
vfio-$(CONFIG_IOMMUFD) += iommufd.o
89
vfio-$(CONFIG_VFIO_CONTAINER) += container.o
910
vfio-$(CONFIG_VFIO_VIRQFD) += virqfd.o

drivers/vfio/device_cdev.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Copyright (c) 2023 Intel Corporation.
4+
*/
5+
#include <linux/vfio.h>
6+
7+
#include "vfio.h"
8+
9+
static dev_t device_devt;
10+
11+
void vfio_init_device_cdev(struct vfio_device *device)
12+
{
13+
device->device.devt = MKDEV(MAJOR(device_devt), device->index);
14+
cdev_init(&device->cdev, &vfio_device_fops);
15+
device->cdev.owner = THIS_MODULE;
16+
}
17+
18+
/*
19+
* device access via the fd opened by this function is blocked until
20+
* .open_device() is called successfully during BIND_IOMMUFD.
21+
*/
22+
int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep)
23+
{
24+
struct vfio_device *device = container_of(inode->i_cdev,
25+
struct vfio_device, cdev);
26+
struct vfio_device_file *df;
27+
int ret;
28+
29+
/* Paired with the put in vfio_device_fops_release() */
30+
if (!vfio_device_try_get_registration(device))
31+
return -ENODEV;
32+
33+
df = vfio_allocate_device_file(device);
34+
if (IS_ERR(df)) {
35+
ret = PTR_ERR(df);
36+
goto err_put_registration;
37+
}
38+
39+
filep->private_data = df;
40+
41+
return 0;
42+
43+
err_put_registration:
44+
vfio_device_put_registration(device);
45+
return ret;
46+
}
47+
48+
static char *vfio_device_devnode(const struct device *dev, umode_t *mode)
49+
{
50+
return kasprintf(GFP_KERNEL, "vfio/devices/%s", dev_name(dev));
51+
}
52+
53+
int vfio_cdev_init(struct class *device_class)
54+
{
55+
device_class->devnode = vfio_device_devnode;
56+
return alloc_chrdev_region(&device_devt, 0,
57+
MINORMASK + 1, "vfio-dev");
58+
}
59+
60+
void vfio_cdev_cleanup(void)
61+
{
62+
unregister_chrdev_region(device_devt, MINORMASK + 1);
63+
}

drivers/vfio/vfio.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,60 @@ vfio_iommufd_compat_attach_ioas(struct vfio_device *device,
266266
}
267267
#endif
268268

269+
#if IS_ENABLED(CONFIG_VFIO_DEVICE_CDEV)
270+
void vfio_init_device_cdev(struct vfio_device *device);
271+
272+
static inline int vfio_device_add(struct vfio_device *device)
273+
{
274+
/* cdev does not support noiommu device */
275+
if (vfio_device_is_noiommu(device))
276+
return device_add(&device->device);
277+
vfio_init_device_cdev(device);
278+
return cdev_device_add(&device->cdev, &device->device);
279+
}
280+
281+
static inline void vfio_device_del(struct vfio_device *device)
282+
{
283+
if (vfio_device_is_noiommu(device))
284+
device_del(&device->device);
285+
else
286+
cdev_device_del(&device->cdev, &device->device);
287+
}
288+
289+
int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep);
290+
int vfio_cdev_init(struct class *device_class);
291+
void vfio_cdev_cleanup(void);
292+
#else
293+
static inline void vfio_init_device_cdev(struct vfio_device *device)
294+
{
295+
}
296+
297+
static inline int vfio_device_add(struct vfio_device *device)
298+
{
299+
return device_add(&device->device);
300+
}
301+
302+
static inline void vfio_device_del(struct vfio_device *device)
303+
{
304+
device_del(&device->device);
305+
}
306+
307+
static inline int vfio_device_fops_cdev_open(struct inode *inode,
308+
struct file *filep)
309+
{
310+
return 0;
311+
}
312+
313+
static inline int vfio_cdev_init(struct class *device_class)
314+
{
315+
return 0;
316+
}
317+
318+
static inline void vfio_cdev_cleanup(void)
319+
{
320+
}
321+
#endif /* CONFIG_VFIO_DEVICE_CDEV */
322+
269323
#if IS_ENABLED(CONFIG_VFIO_VIRQFD)
270324
int __init vfio_virqfd_init(void);
271325
void vfio_virqfd_exit(void);

drivers/vfio/vfio_main.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ static int __vfio_register_dev(struct vfio_device *device,
292292
if (ret)
293293
return ret;
294294

295-
ret = device_add(&device->device);
295+
ret = vfio_device_add(device);
296296
if (ret)
297297
goto err_out;
298298

@@ -338,8 +338,11 @@ void vfio_unregister_group_dev(struct vfio_device *device)
338338
*/
339339
vfio_device_group_unregister(device);
340340

341-
/* Balances device_add in register path */
342-
device_del(&device->device);
341+
/*
342+
* Balances vfio_device_add() in register path, also prevents
343+
* new device opened by userspace in the cdev path.
344+
*/
345+
vfio_device_del(device);
343346

344347
vfio_device_put_registration(device);
345348
rc = try_wait_for_completion(&device->comp);
@@ -567,7 +570,8 @@ static int vfio_device_fops_release(struct inode *inode, struct file *filep)
567570
struct vfio_device_file *df = filep->private_data;
568571
struct vfio_device *device = df->device;
569572

570-
vfio_df_group_close(df);
573+
if (df->group)
574+
vfio_df_group_close(df);
571575

572576
vfio_device_put_registration(device);
573577

@@ -1216,6 +1220,7 @@ static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma)
12161220

12171221
const struct file_operations vfio_device_fops = {
12181222
.owner = THIS_MODULE,
1223+
.open = vfio_device_fops_cdev_open,
12191224
.release = vfio_device_fops_release,
12201225
.read = vfio_device_fops_read,
12211226
.write = vfio_device_fops_write,
@@ -1567,9 +1572,16 @@ static int __init vfio_init(void)
15671572
goto err_dev_class;
15681573
}
15691574

1575+
ret = vfio_cdev_init(vfio.device_class);
1576+
if (ret)
1577+
goto err_alloc_dev_chrdev;
1578+
15701579
pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
15711580
return 0;
15721581

1582+
err_alloc_dev_chrdev:
1583+
class_destroy(vfio.device_class);
1584+
vfio.device_class = NULL;
15731585
err_dev_class:
15741586
vfio_virqfd_exit();
15751587
err_virqfd:
@@ -1580,6 +1592,7 @@ static int __init vfio_init(void)
15801592
static void __exit vfio_cleanup(void)
15811593
{
15821594
ida_destroy(&vfio.device_ida);
1595+
vfio_cdev_cleanup();
15831596
class_destroy(vfio.device_class);
15841597
vfio.device_class = NULL;
15851598
vfio_virqfd_exit();

include/linux/vfio.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/mm.h>
1414
#include <linux/workqueue.h>
1515
#include <linux/poll.h>
16+
#include <linux/cdev.h>
1617
#include <uapi/linux/vfio.h>
1718
#include <linux/iova_bitmap.h>
1819

@@ -51,6 +52,9 @@ struct vfio_device {
5152
/* Members below here are private, not for driver use */
5253
unsigned int index;
5354
struct device device; /* device.kref covers object life circle */
55+
#if IS_ENABLED(CONFIG_VFIO_DEVICE_CDEV)
56+
struct cdev cdev;
57+
#endif
5458
refcount_t refcount; /* user count on registered device*/
5559
unsigned int open_count;
5660
struct completion comp;

0 commit comments

Comments
 (0)