18 #include <linux/device.h>
24 #include <linux/list.h>
25 #include <linux/module.h>
27 #include <linux/sched.h>
28 #include <linux/slab.h>
29 #include <linux/string.h>
31 #include <linux/vfio.h>
32 #include <linux/wait.h>
34 #define DRIVER_VERSION "0.3"
36 #define DRIVER_DESC "VFIO - User Level meta-driver"
41 struct mutex iommu_drivers_lock;
44 struct mutex group_lock;
45 struct cdev group_cdev;
105 if (tmp->
ops == ops) {
112 list_add(&driver->
vfio_next, &vfio.iommu_drivers_list);
126 if (driver->
ops == ops) {
161 static void vfio_free_group_minor(
int minor)
168 static void vfio_group_get(
struct vfio_group *group);
178 kref_get(&container->
kref);
181 static void vfio_container_release(
struct kref *
kref)
191 kref_put(&container->
kref, vfio_container_release);
207 kref_init(&group->
kref);
213 group->
nb.notifier_call = vfio_iommu_group_notifier;
230 minor = vfio_alloc_group_minor(group);
234 return ERR_PTR(minor);
241 vfio_free_group_minor(minor);
251 vfio_free_group_minor(minor);
257 group->
minor = minor;
260 list_add(&group->
vfio_next, &vfio.group_list);
268 static void vfio_group_release(
struct kref *
kref)
276 vfio_free_group_minor(group->
minor);
289 static void vfio_group_put(
struct vfio_group *group)
291 kref_put_mutex(&group->
kref, vfio_group_release, &vfio.group_lock);
295 static void vfio_group_get(
struct vfio_group *group)
297 kref_get(&group->
kref);
310 if (group == target) {
311 vfio_group_get(group);
322 struct vfio_group *vfio_group_get_from_iommu(
struct iommu_group *iommu_group)
329 vfio_group_get(group);
339 static struct vfio_group *vfio_group_get_from_minor(
int minor)
344 group =
idr_find(&vfio.group_idr, minor);
349 vfio_group_get(group);
367 device = kzalloc(
sizeof(*device),
GFP_KERNEL);
371 kref_init(&device->
kref);
384 vfio_group_get(group);
393 static void vfio_device_release(
struct kref *kref)
411 static void vfio_device_put(
struct vfio_device *device)
414 kref_put_mutex(&device->
kref, vfio_device_release, &group->
device_lock);
415 vfio_group_put(group);
418 static void vfio_device_get(
struct vfio_device *device)
420 vfio_group_get(device->
group);
421 kref_get(&device->
kref);
431 if (device->
dev == dev) {
432 vfio_device_get(device);
446 static const char *
const vfio_driver_whitelist[] = {
"pci-stub" };
448 static bool vfio_whitelisted_driver(
struct device_driver *drv)
452 for (i = 0; i <
ARRAY_SIZE(vfio_driver_whitelist); i++) {
453 if (!
strcmp(drv->
name, vfio_driver_whitelist[i]))
465 static int vfio_dev_viable(
struct device *dev,
void *
data)
470 if (!dev->
driver || vfio_whitelisted_driver(dev->
driver))
473 device = vfio_group_get_device(group, dev);
475 vfio_device_put(device);
485 static int vfio_group_nb_add_dev(
struct vfio_group *group,
struct device *dev)
490 device = vfio_group_get_device(group, dev);
492 vfio_device_put(device);
501 WARN(
"Device %s added to live group %d!\n", dev_name(dev),
507 static int vfio_group_nb_del_dev(
struct vfio_group *group,
struct device *dev)
517 device = vfio_group_get_device(group, dev);
521 WARN(
"Device %s removed from live group %d!\n", dev_name(dev),
524 vfio_device_put(device);
528 static int vfio_group_nb_verify(
struct vfio_group *group,
struct device *dev)
534 return vfio_dev_viable(dev, group);
538 unsigned long action,
void *data)
541 struct device *dev =
data;
549 group = vfio_group_try_get(group);
554 case IOMMU_GROUP_NOTIFY_ADD_DEVICE:
555 vfio_group_nb_add_dev(group, dev);
557 case IOMMU_GROUP_NOTIFY_DEL_DEVICE:
558 vfio_group_nb_del_dev(group, dev);
560 case IOMMU_GROUP_NOTIFY_BIND_DRIVER:
561 pr_debug(
"%s: Device %s, group %d binding to driver\n",
562 __func__, dev_name(dev),
565 case IOMMU_GROUP_NOTIFY_BOUND_DRIVER:
566 pr_debug(
"%s: Device %s, group %d bound to driver %s\n",
567 __func__, dev_name(dev),
569 BUG_ON(vfio_group_nb_verify(group, dev));
571 case IOMMU_GROUP_NOTIFY_UNBIND_DRIVER:
572 pr_debug(
"%s: Device %s, group %d unbinding from driver %s\n",
573 __func__, dev_name(dev),
576 case IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER:
577 pr_debug(
"%s: Device %s, group %d unbound from driver\n",
578 __func__, dev_name(dev),
590 vfio_group_put(group);
600 struct iommu_group *iommu_group;
608 group = vfio_group_get_from_iommu(iommu_group);
610 group = vfio_create_group(iommu_group);
613 return PTR_ERR(group);
617 device = vfio_group_get_device(group, dev);
619 WARN(1,
"Device %s already exists on group %d\n",
621 vfio_device_put(device);
622 vfio_group_put(group);
627 device = vfio_group_create_device(group, dev, ops, device_data);
628 if (IS_ERR(device)) {
629 vfio_group_put(group);
631 return PTR_ERR(device);
639 vfio_group_put(group);
646 static bool vfio_dev_present(
struct device *dev)
648 struct iommu_group *iommu_group;
656 group = vfio_group_get_from_iommu(iommu_group);
662 device = vfio_group_get_device(group, dev);
664 vfio_group_put(group);
669 vfio_device_put(device);
670 vfio_group_put(group);
682 struct iommu_group *iommu_group = group->
iommu_group;
685 vfio_device_put(device);
688 wait_event(vfio.release_q, !vfio_dev_present(dev));
699 static long vfio_ioctl_check_extension(
struct vfio_container *container,
718 if (!try_module_get(driver->
ops->owner))
724 module_put(driver->
ops->owner);
738 static int __vfio_container_attach_groups(
struct vfio_container *container,
762 static long vfio_ioctl_set_iommu(
struct vfio_container *container,
787 if (!try_module_get(driver->
ops->owner))
798 module_put(driver->
ops->owner);
805 data = driver->
ops->open(arg);
808 module_put(driver->
ops->owner);
809 goto skip_drivers_unlock;
812 ret = __vfio_container_attach_groups(container, driver, data);
817 driver->
ops->release(data);
818 module_put(driver->
ops->owner);
821 goto skip_drivers_unlock;
831 static long vfio_fops_unl_ioctl(
struct file *filep,
832 unsigned int cmd,
unsigned long arg)
850 ret = vfio_ioctl_check_extension(container, arg);
853 ret = vfio_ioctl_set_iommu(container, arg);
857 ret = driver->
ops->ioctl(data, cmd, arg);
864 static long vfio_fops_compat_ioctl(
struct file *filep,
865 unsigned int cmd,
unsigned long arg)
867 arg = (
unsigned long)compat_ptr(arg);
868 return vfio_fops_unl_ioctl(filep, cmd, arg);
872 static int vfio_fops_open(
struct inode *
inode,
struct file *filep)
876 container = kzalloc(
sizeof(*container),
GFP_KERNEL);
882 kref_init(&container->
kref);
889 static int vfio_fops_release(
struct inode *inode,
struct file *filep)
895 vfio_container_put(container);
904 static ssize_t vfio_fops_read(
struct file *filep,
char __user *
buf,
905 size_t count, loff_t *ppos)
913 return driver->
ops->read(container->
iommu_data, buf, count, ppos);
916 static ssize_t vfio_fops_write(
struct file *filep,
const char __user *buf,
917 size_t count, loff_t *ppos)
925 return driver->
ops->write(container->
iommu_data, buf, count, ppos);
941 .open = vfio_fops_open,
942 .release = vfio_fops_release,
943 .read = vfio_fops_read,
944 .write = vfio_fops_write,
945 .unlocked_ioctl = vfio_fops_unl_ioctl,
947 .compat_ioctl = vfio_fops_compat_ioctl,
949 .mmap = vfio_fops_mmap,
955 static void __vfio_group_unset_container(
struct vfio_group *group)
971 if (driver && list_empty(&container->
group_list)) {
973 module_put(driver->
ops->owner);
980 vfio_container_put(container);
989 static int vfio_group_unset_container(
struct vfio_group *group)
998 __vfio_group_unset_container(group);
1009 static void vfio_group_try_dissolve_container(
struct vfio_group *group)
1012 __vfio_group_unset_container(group);
1015 static int vfio_group_set_container(
struct vfio_group *group,
int container_fd)
1025 f = fdget(container_fd);
1030 if (
f.file->f_op != &vfio_fops) {
1035 container =
f.file->private_data;
1052 vfio_container_get(container);
1061 static bool vfio_group_viable(
struct vfio_group *group)
1064 group, vfio_dev_viable) == 0);
1069 static int vfio_group_get_device_fd(
struct vfio_group *group,
char *buf)
1076 !group->
container->iommu_driver || !vfio_group_viable(group))
1099 if (IS_ERR(filep)) {
1101 ret = PTR_ERR(filep);
1113 vfio_device_get(device);
1124 static long vfio_group_fops_unl_ioctl(
struct file *filep,
1125 unsigned int cmd,
unsigned long arg)
1134 unsigned long minsz;
1141 if (
status.argsz < minsz)
1146 if (vfio_group_viable(group))
1162 if (
get_user(fd, (
int __user *)arg))
1168 ret = vfio_group_set_container(group, fd);
1172 ret = vfio_group_unset_container(group);
1180 return PTR_ERR(buf);
1182 ret = vfio_group_get_device_fd(group, buf);
1191 #ifdef CONFIG_COMPAT
1192 static long vfio_group_fops_compat_ioctl(
struct file *filep,
1193 unsigned int cmd,
unsigned long arg)
1195 arg = (
unsigned long)compat_ptr(arg);
1196 return vfio_group_fops_unl_ioctl(filep, cmd, arg);
1200 static int vfio_group_fops_open(
struct inode *inode,
struct file *filep)
1204 group = vfio_group_get_from_minor(iminor(inode));
1209 vfio_group_put(group);
1218 static int vfio_group_fops_release(
struct inode *inode,
struct file *filep)
1224 vfio_group_try_dissolve_container(group);
1226 vfio_group_put(group);
1233 .unlocked_ioctl = vfio_group_fops_unl_ioctl,
1234 #ifdef CONFIG_COMPAT
1235 .compat_ioctl = vfio_group_fops_compat_ioctl,
1237 .open = vfio_group_fops_open,
1238 .release = vfio_group_fops_release,
1244 static int vfio_device_fops_release(
struct inode *inode,
struct file *filep)
1250 vfio_group_try_dissolve_container(device->
group);
1252 vfio_device_put(device);
1257 static long vfio_device_fops_unl_ioctl(
struct file *filep,
1258 unsigned int cmd,
unsigned long arg)
1268 static ssize_t vfio_device_fops_read(
struct file *filep,
char __user *buf,
1269 size_t count, loff_t *ppos)
1279 static ssize_t vfio_device_fops_write(
struct file *filep,
1280 const char __user *buf,
1281 size_t count, loff_t *ppos)
1288 return device->
ops->write(device->
device_data, buf, count, ppos);
1301 #ifdef CONFIG_COMPAT
1302 static long vfio_device_fops_compat_ioctl(
struct file *filep,
1303 unsigned int cmd,
unsigned long arg)
1305 arg = (
unsigned long)compat_ptr(arg);
1306 return vfio_device_fops_unl_ioctl(filep, cmd, arg);
1312 .release = vfio_device_fops_release,
1313 .read = vfio_device_fops_read,
1314 .write = vfio_device_fops_write,
1315 .unlocked_ioctl = vfio_device_fops_unl_ioctl,
1316 #ifdef CONFIG_COMPAT
1317 .compat_ioctl = vfio_device_fops_compat_ioctl,
1319 .mmap = vfio_device_fops_mmap,
1325 static char *vfio_devnode(
struct device *dev,
umode_t *
mode)
1330 static int __init vfio_init(
void)
1337 INIT_LIST_HEAD(&vfio.group_list);
1338 INIT_LIST_HEAD(&vfio.iommu_drivers_list);
1342 if (IS_ERR(vfio.class)) {
1343 ret = PTR_ERR(vfio.class);
1347 vfio.class->devnode = vfio_devnode;
1351 goto err_base_chrdev;
1354 ret =
cdev_add(&vfio.cdev, vfio.devt, 1);
1359 if (IS_ERR(vfio.dev)) {
1360 ret = PTR_ERR(vfio.dev);
1365 cdev_init(&vfio.group_cdev, &vfio_group_fops);
1369 goto err_groups_cdev;
1378 request_module_nowait(
"vfio_iommu_type1");
1395 static void __exit vfio_cleanup(
void)
1397 WARN_ON(!list_empty(&vfio.group_list));