mwifiex: add support for sleep cookie in PCIe
[linux-2.6/btrfs-unstable.git] / drivers / vfio / vfio.c
blob21271d8df02374aaae378c552ff7ad10b89e6e84
1 /*
2 * VFIO core
4 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
5 * Author: Alex Williamson <alex.williamson@redhat.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * Derived from original vfio:
12 * Copyright 2010 Cisco Systems, Inc. All rights reserved.
13 * Author: Tom Lyon, pugs@cisco.com
16 #include <linux/cdev.h>
17 #include <linux/compat.h>
18 #include <linux/device.h>
19 #include <linux/file.h>
20 #include <linux/anon_inodes.h>
21 #include <linux/fs.h>
22 #include <linux/idr.h>
23 #include <linux/iommu.h>
24 #include <linux/list.h>
25 #include <linux/miscdevice.h>
26 #include <linux/module.h>
27 #include <linux/mutex.h>
28 #include <linux/rwsem.h>
29 #include <linux/sched.h>
30 #include <linux/slab.h>
31 #include <linux/stat.h>
32 #include <linux/string.h>
33 #include <linux/uaccess.h>
34 #include <linux/vfio.h>
35 #include <linux/wait.h>
37 #define DRIVER_VERSION "0.3"
38 #define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>"
39 #define DRIVER_DESC "VFIO - User Level meta-driver"
41 static struct vfio {
42 struct class *class;
43 struct list_head iommu_drivers_list;
44 struct mutex iommu_drivers_lock;
45 struct list_head group_list;
46 struct idr group_idr;
47 struct mutex group_lock;
48 struct cdev group_cdev;
49 dev_t group_devt;
50 wait_queue_head_t release_q;
51 } vfio;
53 struct vfio_iommu_driver {
54 const struct vfio_iommu_driver_ops *ops;
55 struct list_head vfio_next;
58 struct vfio_container {
59 struct kref kref;
60 struct list_head group_list;
61 struct rw_semaphore group_lock;
62 struct vfio_iommu_driver *iommu_driver;
63 void *iommu_data;
66 struct vfio_group {
67 struct kref kref;
68 int minor;
69 atomic_t container_users;
70 struct iommu_group *iommu_group;
71 struct vfio_container *container;
72 struct list_head device_list;
73 struct mutex device_lock;
74 struct device *dev;
75 struct notifier_block nb;
76 struct list_head vfio_next;
77 struct list_head container_next;
78 atomic_t opened;
81 struct vfio_device {
82 struct kref kref;
83 struct device *dev;
84 const struct vfio_device_ops *ops;
85 struct vfio_group *group;
86 struct list_head group_next;
87 void *device_data;
90 /**
91 * IOMMU driver registration
93 int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops)
95 struct vfio_iommu_driver *driver, *tmp;
97 driver = kzalloc(sizeof(*driver), GFP_KERNEL);
98 if (!driver)
99 return -ENOMEM;
101 driver->ops = ops;
103 mutex_lock(&vfio.iommu_drivers_lock);
105 /* Check for duplicates */
106 list_for_each_entry(tmp, &vfio.iommu_drivers_list, vfio_next) {
107 if (tmp->ops == ops) {
108 mutex_unlock(&vfio.iommu_drivers_lock);
109 kfree(driver);
110 return -EINVAL;
114 list_add(&driver->vfio_next, &vfio.iommu_drivers_list);
116 mutex_unlock(&vfio.iommu_drivers_lock);
118 return 0;
120 EXPORT_SYMBOL_GPL(vfio_register_iommu_driver);
122 void vfio_unregister_iommu_driver(const struct vfio_iommu_driver_ops *ops)
124 struct vfio_iommu_driver *driver;
126 mutex_lock(&vfio.iommu_drivers_lock);
127 list_for_each_entry(driver, &vfio.iommu_drivers_list, vfio_next) {
128 if (driver->ops == ops) {
129 list_del(&driver->vfio_next);
130 mutex_unlock(&vfio.iommu_drivers_lock);
131 kfree(driver);
132 return;
135 mutex_unlock(&vfio.iommu_drivers_lock);
137 EXPORT_SYMBOL_GPL(vfio_unregister_iommu_driver);
140 * Group minor allocation/free - both called with vfio.group_lock held
142 static int vfio_alloc_group_minor(struct vfio_group *group)
144 return idr_alloc(&vfio.group_idr, group, 0, MINORMASK + 1, GFP_KERNEL);
147 static void vfio_free_group_minor(int minor)
149 idr_remove(&vfio.group_idr, minor);
152 static int vfio_iommu_group_notifier(struct notifier_block *nb,
153 unsigned long action, void *data);
154 static void vfio_group_get(struct vfio_group *group);
157 * Container objects - containers are created when /dev/vfio/vfio is
158 * opened, but their lifecycle extends until the last user is done, so
159 * it's freed via kref. Must support container/group/device being
160 * closed in any order.
162 static void vfio_container_get(struct vfio_container *container)
164 kref_get(&container->kref);
167 static void vfio_container_release(struct kref *kref)
169 struct vfio_container *container;
170 container = container_of(kref, struct vfio_container, kref);
172 kfree(container);
175 static void vfio_container_put(struct vfio_container *container)
177 kref_put(&container->kref, vfio_container_release);
180 static void vfio_group_unlock_and_free(struct vfio_group *group)
182 mutex_unlock(&vfio.group_lock);
184 * Unregister outside of lock. A spurious callback is harmless now
185 * that the group is no longer in vfio.group_list.
187 iommu_group_unregister_notifier(group->iommu_group, &group->nb);
188 kfree(group);
192 * Group objects - create, release, get, put, search
194 static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group)
196 struct vfio_group *group, *tmp;
197 struct device *dev;
198 int ret, minor;
200 group = kzalloc(sizeof(*group), GFP_KERNEL);
201 if (!group)
202 return ERR_PTR(-ENOMEM);
204 kref_init(&group->kref);
205 INIT_LIST_HEAD(&group->device_list);
206 mutex_init(&group->device_lock);
207 atomic_set(&group->container_users, 0);
208 atomic_set(&group->opened, 0);
209 group->iommu_group = iommu_group;
211 group->nb.notifier_call = vfio_iommu_group_notifier;
214 * blocking notifiers acquire a rwsem around registering and hold
215 * it around callback. Therefore, need to register outside of
216 * vfio.group_lock to avoid A-B/B-A contention. Our callback won't
217 * do anything unless it can find the group in vfio.group_list, so
218 * no harm in registering early.
220 ret = iommu_group_register_notifier(iommu_group, &group->nb);
221 if (ret) {
222 kfree(group);
223 return ERR_PTR(ret);
226 mutex_lock(&vfio.group_lock);
228 minor = vfio_alloc_group_minor(group);
229 if (minor < 0) {
230 vfio_group_unlock_and_free(group);
231 return ERR_PTR(minor);
234 /* Did we race creating this group? */
235 list_for_each_entry(tmp, &vfio.group_list, vfio_next) {
236 if (tmp->iommu_group == iommu_group) {
237 vfio_group_get(tmp);
238 vfio_free_group_minor(minor);
239 vfio_group_unlock_and_free(group);
240 return tmp;
244 dev = device_create(vfio.class, NULL,
245 MKDEV(MAJOR(vfio.group_devt), minor),
246 group, "%d", iommu_group_id(iommu_group));
247 if (IS_ERR(dev)) {
248 vfio_free_group_minor(minor);
249 vfio_group_unlock_and_free(group);
250 return (struct vfio_group *)dev; /* ERR_PTR */
253 group->minor = minor;
254 group->dev = dev;
256 list_add(&group->vfio_next, &vfio.group_list);
258 mutex_unlock(&vfio.group_lock);
260 return group;
263 /* called with vfio.group_lock held */
264 static void vfio_group_release(struct kref *kref)
266 struct vfio_group *group = container_of(kref, struct vfio_group, kref);
268 WARN_ON(!list_empty(&group->device_list));
270 device_destroy(vfio.class, MKDEV(MAJOR(vfio.group_devt), group->minor));
271 list_del(&group->vfio_next);
272 vfio_free_group_minor(group->minor);
273 vfio_group_unlock_and_free(group);
276 static void vfio_group_put(struct vfio_group *group)
278 kref_put_mutex(&group->kref, vfio_group_release, &vfio.group_lock);
281 /* Assume group_lock or group reference is held */
282 static void vfio_group_get(struct vfio_group *group)
284 kref_get(&group->kref);
288 * Not really a try as we will sleep for mutex, but we need to make
289 * sure the group pointer is valid under lock and get a reference.
291 static struct vfio_group *vfio_group_try_get(struct vfio_group *group)
293 struct vfio_group *target = group;
295 mutex_lock(&vfio.group_lock);
296 list_for_each_entry(group, &vfio.group_list, vfio_next) {
297 if (group == target) {
298 vfio_group_get(group);
299 mutex_unlock(&vfio.group_lock);
300 return group;
303 mutex_unlock(&vfio.group_lock);
305 return NULL;
308 static
309 struct vfio_group *vfio_group_get_from_iommu(struct iommu_group *iommu_group)
311 struct vfio_group *group;
313 mutex_lock(&vfio.group_lock);
314 list_for_each_entry(group, &vfio.group_list, vfio_next) {
315 if (group->iommu_group == iommu_group) {
316 vfio_group_get(group);
317 mutex_unlock(&vfio.group_lock);
318 return group;
321 mutex_unlock(&vfio.group_lock);
323 return NULL;
326 static struct vfio_group *vfio_group_get_from_minor(int minor)
328 struct vfio_group *group;
330 mutex_lock(&vfio.group_lock);
331 group = idr_find(&vfio.group_idr, minor);
332 if (!group) {
333 mutex_unlock(&vfio.group_lock);
334 return NULL;
336 vfio_group_get(group);
337 mutex_unlock(&vfio.group_lock);
339 return group;
343 * Device objects - create, release, get, put, search
345 static
346 struct vfio_device *vfio_group_create_device(struct vfio_group *group,
347 struct device *dev,
348 const struct vfio_device_ops *ops,
349 void *device_data)
351 struct vfio_device *device;
352 int ret;
354 device = kzalloc(sizeof(*device), GFP_KERNEL);
355 if (!device)
356 return ERR_PTR(-ENOMEM);
358 kref_init(&device->kref);
359 device->dev = dev;
360 device->group = group;
361 device->ops = ops;
362 device->device_data = device_data;
364 ret = dev_set_drvdata(dev, device);
365 if (ret) {
366 kfree(device);
367 return ERR_PTR(ret);
370 /* No need to get group_lock, caller has group reference */
371 vfio_group_get(group);
373 mutex_lock(&group->device_lock);
374 list_add(&device->group_next, &group->device_list);
375 mutex_unlock(&group->device_lock);
377 return device;
380 static void vfio_device_release(struct kref *kref)
382 struct vfio_device *device = container_of(kref,
383 struct vfio_device, kref);
384 struct vfio_group *group = device->group;
386 list_del(&device->group_next);
387 mutex_unlock(&group->device_lock);
389 dev_set_drvdata(device->dev, NULL);
391 kfree(device);
393 /* vfio_del_group_dev may be waiting for this device */
394 wake_up(&vfio.release_q);
397 /* Device reference always implies a group reference */
398 void vfio_device_put(struct vfio_device *device)
400 struct vfio_group *group = device->group;
401 kref_put_mutex(&device->kref, vfio_device_release, &group->device_lock);
402 vfio_group_put(group);
404 EXPORT_SYMBOL_GPL(vfio_device_put);
406 static void vfio_device_get(struct vfio_device *device)
408 vfio_group_get(device->group);
409 kref_get(&device->kref);
412 static struct vfio_device *vfio_group_get_device(struct vfio_group *group,
413 struct device *dev)
415 struct vfio_device *device;
417 mutex_lock(&group->device_lock);
418 list_for_each_entry(device, &group->device_list, group_next) {
419 if (device->dev == dev) {
420 vfio_device_get(device);
421 mutex_unlock(&group->device_lock);
422 return device;
425 mutex_unlock(&group->device_lock);
426 return NULL;
430 * Whitelist some drivers that we know are safe (no dma) or just sit on
431 * a device. It's not always practical to leave a device within a group
432 * driverless as it could get re-bound to something unsafe.
434 static const char * const vfio_driver_whitelist[] = { "pci-stub", "pcieport" };
436 static bool vfio_whitelisted_driver(struct device_driver *drv)
438 int i;
440 for (i = 0; i < ARRAY_SIZE(vfio_driver_whitelist); i++) {
441 if (!strcmp(drv->name, vfio_driver_whitelist[i]))
442 return true;
445 return false;
449 * A vfio group is viable for use by userspace if all devices are either
450 * driver-less or bound to a vfio or whitelisted driver. We test the
451 * latter by the existence of a struct vfio_device matching the dev.
453 static int vfio_dev_viable(struct device *dev, void *data)
455 struct vfio_group *group = data;
456 struct vfio_device *device;
457 struct device_driver *drv = ACCESS_ONCE(dev->driver);
459 if (!drv || vfio_whitelisted_driver(drv))
460 return 0;
462 device = vfio_group_get_device(group, dev);
463 if (device) {
464 vfio_device_put(device);
465 return 0;
468 return -EINVAL;
472 * Async device support
474 static int vfio_group_nb_add_dev(struct vfio_group *group, struct device *dev)
476 struct vfio_device *device;
478 /* Do we already know about it? We shouldn't */
479 device = vfio_group_get_device(group, dev);
480 if (WARN_ON_ONCE(device)) {
481 vfio_device_put(device);
482 return 0;
485 /* Nothing to do for idle groups */
486 if (!atomic_read(&group->container_users))
487 return 0;
489 /* TODO Prevent device auto probing */
490 WARN("Device %s added to live group %d!\n", dev_name(dev),
491 iommu_group_id(group->iommu_group));
493 return 0;
496 static int vfio_group_nb_verify(struct vfio_group *group, struct device *dev)
498 /* We don't care what happens when the group isn't in use */
499 if (!atomic_read(&group->container_users))
500 return 0;
502 return vfio_dev_viable(dev, group);
505 static int vfio_iommu_group_notifier(struct notifier_block *nb,
506 unsigned long action, void *data)
508 struct vfio_group *group = container_of(nb, struct vfio_group, nb);
509 struct device *dev = data;
512 * Need to go through a group_lock lookup to get a reference or we
513 * risk racing a group being removed. Ignore spurious notifies.
515 group = vfio_group_try_get(group);
516 if (!group)
517 return NOTIFY_OK;
519 switch (action) {
520 case IOMMU_GROUP_NOTIFY_ADD_DEVICE:
521 vfio_group_nb_add_dev(group, dev);
522 break;
523 case IOMMU_GROUP_NOTIFY_DEL_DEVICE:
525 * Nothing to do here. If the device is in use, then the
526 * vfio sub-driver should block the remove callback until
527 * it is unused. If the device is unused or attached to a
528 * stub driver, then it should be released and we don't
529 * care that it will be going away.
531 break;
532 case IOMMU_GROUP_NOTIFY_BIND_DRIVER:
533 pr_debug("%s: Device %s, group %d binding to driver\n",
534 __func__, dev_name(dev),
535 iommu_group_id(group->iommu_group));
536 break;
537 case IOMMU_GROUP_NOTIFY_BOUND_DRIVER:
538 pr_debug("%s: Device %s, group %d bound to driver %s\n",
539 __func__, dev_name(dev),
540 iommu_group_id(group->iommu_group), dev->driver->name);
541 BUG_ON(vfio_group_nb_verify(group, dev));
542 break;
543 case IOMMU_GROUP_NOTIFY_UNBIND_DRIVER:
544 pr_debug("%s: Device %s, group %d unbinding from driver %s\n",
545 __func__, dev_name(dev),
546 iommu_group_id(group->iommu_group), dev->driver->name);
547 break;
548 case IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER:
549 pr_debug("%s: Device %s, group %d unbound from driver\n",
550 __func__, dev_name(dev),
551 iommu_group_id(group->iommu_group));
553 * XXX An unbound device in a live group is ok, but we'd
554 * really like to avoid the above BUG_ON by preventing other
555 * drivers from binding to it. Once that occurs, we have to
556 * stop the system to maintain isolation. At a minimum, we'd
557 * want a toggle to disable driver auto probe for this device.
559 break;
562 vfio_group_put(group);
563 return NOTIFY_OK;
567 * VFIO driver API
569 int vfio_add_group_dev(struct device *dev,
570 const struct vfio_device_ops *ops, void *device_data)
572 struct iommu_group *iommu_group;
573 struct vfio_group *group;
574 struct vfio_device *device;
576 iommu_group = iommu_group_get(dev);
577 if (!iommu_group)
578 return -EINVAL;
580 group = vfio_group_get_from_iommu(iommu_group);
581 if (!group) {
582 group = vfio_create_group(iommu_group);
583 if (IS_ERR(group)) {
584 iommu_group_put(iommu_group);
585 return PTR_ERR(group);
589 device = vfio_group_get_device(group, dev);
590 if (device) {
591 WARN(1, "Device %s already exists on group %d\n",
592 dev_name(dev), iommu_group_id(iommu_group));
593 vfio_device_put(device);
594 vfio_group_put(group);
595 iommu_group_put(iommu_group);
596 return -EBUSY;
599 device = vfio_group_create_device(group, dev, ops, device_data);
600 if (IS_ERR(device)) {
601 vfio_group_put(group);
602 iommu_group_put(iommu_group);
603 return PTR_ERR(device);
607 * Added device holds reference to iommu_group and vfio_device
608 * (which in turn holds reference to vfio_group). Drop extra
609 * group reference used while acquiring device.
611 vfio_group_put(group);
613 return 0;
615 EXPORT_SYMBOL_GPL(vfio_add_group_dev);
618 * Get a reference to the vfio_device for a device that is known to
619 * be bound to a vfio driver. The driver implicitly holds a
620 * vfio_device reference between vfio_add_group_dev and
621 * vfio_del_group_dev. We can therefore use drvdata to increment
622 * that reference from the struct device. This additional
623 * reference must be released by calling vfio_device_put.
625 struct vfio_device *vfio_device_get_from_dev(struct device *dev)
627 struct vfio_device *device = dev_get_drvdata(dev);
629 vfio_device_get(device);
631 return device;
633 EXPORT_SYMBOL_GPL(vfio_device_get_from_dev);
636 * Caller must hold a reference to the vfio_device
638 void *vfio_device_data(struct vfio_device *device)
640 return device->device_data;
642 EXPORT_SYMBOL_GPL(vfio_device_data);
644 /* Given a referenced group, check if it contains the device */
645 static bool vfio_dev_present(struct vfio_group *group, struct device *dev)
647 struct vfio_device *device;
649 device = vfio_group_get_device(group, dev);
650 if (!device)
651 return false;
653 vfio_device_put(device);
654 return true;
658 * Decrement the device reference count and wait for the device to be
659 * removed. Open file descriptors for the device... */
660 void *vfio_del_group_dev(struct device *dev)
662 struct vfio_device *device = dev_get_drvdata(dev);
663 struct vfio_group *group = device->group;
664 struct iommu_group *iommu_group = group->iommu_group;
665 void *device_data = device->device_data;
668 * The group exists so long as we have a device reference. Get
669 * a group reference and use it to scan for the device going away.
671 vfio_group_get(group);
673 vfio_device_put(device);
675 /* TODO send a signal to encourage this to be released */
676 wait_event(vfio.release_q, !vfio_dev_present(group, dev));
678 vfio_group_put(group);
680 iommu_group_put(iommu_group);
682 return device_data;
684 EXPORT_SYMBOL_GPL(vfio_del_group_dev);
687 * VFIO base fd, /dev/vfio/vfio
689 static long vfio_ioctl_check_extension(struct vfio_container *container,
690 unsigned long arg)
692 struct vfio_iommu_driver *driver;
693 long ret = 0;
695 down_read(&container->group_lock);
697 driver = container->iommu_driver;
699 switch (arg) {
700 /* No base extensions yet */
701 default:
703 * If no driver is set, poll all registered drivers for
704 * extensions and return the first positive result. If
705 * a driver is already set, further queries will be passed
706 * only to that driver.
708 if (!driver) {
709 mutex_lock(&vfio.iommu_drivers_lock);
710 list_for_each_entry(driver, &vfio.iommu_drivers_list,
711 vfio_next) {
712 if (!try_module_get(driver->ops->owner))
713 continue;
715 ret = driver->ops->ioctl(NULL,
716 VFIO_CHECK_EXTENSION,
717 arg);
718 module_put(driver->ops->owner);
719 if (ret > 0)
720 break;
722 mutex_unlock(&vfio.iommu_drivers_lock);
723 } else
724 ret = driver->ops->ioctl(container->iommu_data,
725 VFIO_CHECK_EXTENSION, arg);
728 up_read(&container->group_lock);
730 return ret;
733 /* hold write lock on container->group_lock */
734 static int __vfio_container_attach_groups(struct vfio_container *container,
735 struct vfio_iommu_driver *driver,
736 void *data)
738 struct vfio_group *group;
739 int ret = -ENODEV;
741 list_for_each_entry(group, &container->group_list, container_next) {
742 ret = driver->ops->attach_group(data, group->iommu_group);
743 if (ret)
744 goto unwind;
747 return ret;
749 unwind:
750 list_for_each_entry_continue_reverse(group, &container->group_list,
751 container_next) {
752 driver->ops->detach_group(data, group->iommu_group);
755 return ret;
758 static long vfio_ioctl_set_iommu(struct vfio_container *container,
759 unsigned long arg)
761 struct vfio_iommu_driver *driver;
762 long ret = -ENODEV;
764 down_write(&container->group_lock);
767 * The container is designed to be an unprivileged interface while
768 * the group can be assigned to specific users. Therefore, only by
769 * adding a group to a container does the user get the privilege of
770 * enabling the iommu, which may allocate finite resources. There
771 * is no unset_iommu, but by removing all the groups from a container,
772 * the container is deprivileged and returns to an unset state.
774 if (list_empty(&container->group_list) || container->iommu_driver) {
775 up_write(&container->group_lock);
776 return -EINVAL;
779 mutex_lock(&vfio.iommu_drivers_lock);
780 list_for_each_entry(driver, &vfio.iommu_drivers_list, vfio_next) {
781 void *data;
783 if (!try_module_get(driver->ops->owner))
784 continue;
787 * The arg magic for SET_IOMMU is the same as CHECK_EXTENSION,
788 * so test which iommu driver reported support for this
789 * extension and call open on them. We also pass them the
790 * magic, allowing a single driver to support multiple
791 * interfaces if they'd like.
793 if (driver->ops->ioctl(NULL, VFIO_CHECK_EXTENSION, arg) <= 0) {
794 module_put(driver->ops->owner);
795 continue;
798 /* module reference holds the driver we're working on */
799 mutex_unlock(&vfio.iommu_drivers_lock);
801 data = driver->ops->open(arg);
802 if (IS_ERR(data)) {
803 ret = PTR_ERR(data);
804 module_put(driver->ops->owner);
805 goto skip_drivers_unlock;
808 ret = __vfio_container_attach_groups(container, driver, data);
809 if (!ret) {
810 container->iommu_driver = driver;
811 container->iommu_data = data;
812 } else {
813 driver->ops->release(data);
814 module_put(driver->ops->owner);
817 goto skip_drivers_unlock;
820 mutex_unlock(&vfio.iommu_drivers_lock);
821 skip_drivers_unlock:
822 up_write(&container->group_lock);
824 return ret;
827 static long vfio_fops_unl_ioctl(struct file *filep,
828 unsigned int cmd, unsigned long arg)
830 struct vfio_container *container = filep->private_data;
831 struct vfio_iommu_driver *driver;
832 void *data;
833 long ret = -EINVAL;
835 if (!container)
836 return ret;
838 switch (cmd) {
839 case VFIO_GET_API_VERSION:
840 ret = VFIO_API_VERSION;
841 break;
842 case VFIO_CHECK_EXTENSION:
843 ret = vfio_ioctl_check_extension(container, arg);
844 break;
845 case VFIO_SET_IOMMU:
846 ret = vfio_ioctl_set_iommu(container, arg);
847 break;
848 default:
849 down_read(&container->group_lock);
851 driver = container->iommu_driver;
852 data = container->iommu_data;
854 if (driver) /* passthrough all unrecognized ioctls */
855 ret = driver->ops->ioctl(data, cmd, arg);
857 up_read(&container->group_lock);
860 return ret;
863 #ifdef CONFIG_COMPAT
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);
870 #endif /* CONFIG_COMPAT */
872 static int vfio_fops_open(struct inode *inode, struct file *filep)
874 struct vfio_container *container;
876 container = kzalloc(sizeof(*container), GFP_KERNEL);
877 if (!container)
878 return -ENOMEM;
880 INIT_LIST_HEAD(&container->group_list);
881 init_rwsem(&container->group_lock);
882 kref_init(&container->kref);
884 filep->private_data = container;
886 return 0;
889 static int vfio_fops_release(struct inode *inode, struct file *filep)
891 struct vfio_container *container = filep->private_data;
893 filep->private_data = NULL;
895 vfio_container_put(container);
897 return 0;
901 * Once an iommu driver is set, we optionally pass read/write/mmap
902 * on to the driver, allowing management interfaces beyond ioctl.
904 static ssize_t vfio_fops_read(struct file *filep, char __user *buf,
905 size_t count, loff_t *ppos)
907 struct vfio_container *container = filep->private_data;
908 struct vfio_iommu_driver *driver;
909 ssize_t ret = -EINVAL;
911 down_read(&container->group_lock);
913 driver = container->iommu_driver;
914 if (likely(driver && driver->ops->read))
915 ret = driver->ops->read(container->iommu_data,
916 buf, count, ppos);
918 up_read(&container->group_lock);
920 return ret;
923 static ssize_t vfio_fops_write(struct file *filep, const char __user *buf,
924 size_t count, loff_t *ppos)
926 struct vfio_container *container = filep->private_data;
927 struct vfio_iommu_driver *driver;
928 ssize_t ret = -EINVAL;
930 down_read(&container->group_lock);
932 driver = container->iommu_driver;
933 if (likely(driver && driver->ops->write))
934 ret = driver->ops->write(container->iommu_data,
935 buf, count, ppos);
937 up_read(&container->group_lock);
939 return ret;
942 static int vfio_fops_mmap(struct file *filep, struct vm_area_struct *vma)
944 struct vfio_container *container = filep->private_data;
945 struct vfio_iommu_driver *driver;
946 int ret = -EINVAL;
948 down_read(&container->group_lock);
950 driver = container->iommu_driver;
951 if (likely(driver && driver->ops->mmap))
952 ret = driver->ops->mmap(container->iommu_data, vma);
954 up_read(&container->group_lock);
956 return ret;
959 static const struct file_operations vfio_fops = {
960 .owner = THIS_MODULE,
961 .open = vfio_fops_open,
962 .release = vfio_fops_release,
963 .read = vfio_fops_read,
964 .write = vfio_fops_write,
965 .unlocked_ioctl = vfio_fops_unl_ioctl,
966 #ifdef CONFIG_COMPAT
967 .compat_ioctl = vfio_fops_compat_ioctl,
968 #endif
969 .mmap = vfio_fops_mmap,
973 * VFIO Group fd, /dev/vfio/$GROUP
975 static void __vfio_group_unset_container(struct vfio_group *group)
977 struct vfio_container *container = group->container;
978 struct vfio_iommu_driver *driver;
980 down_write(&container->group_lock);
982 driver = container->iommu_driver;
983 if (driver)
984 driver->ops->detach_group(container->iommu_data,
985 group->iommu_group);
987 group->container = NULL;
988 list_del(&group->container_next);
990 /* Detaching the last group deprivileges a container, remove iommu */
991 if (driver && list_empty(&container->group_list)) {
992 driver->ops->release(container->iommu_data);
993 module_put(driver->ops->owner);
994 container->iommu_driver = NULL;
995 container->iommu_data = NULL;
998 up_write(&container->group_lock);
1000 vfio_container_put(container);
1004 * VFIO_GROUP_UNSET_CONTAINER should fail if there are other users or
1005 * if there was no container to unset. Since the ioctl is called on
1006 * the group, we know that still exists, therefore the only valid
1007 * transition here is 1->0.
1009 static int vfio_group_unset_container(struct vfio_group *group)
1011 int users = atomic_cmpxchg(&group->container_users, 1, 0);
1013 if (!users)
1014 return -EINVAL;
1015 if (users != 1)
1016 return -EBUSY;
1018 __vfio_group_unset_container(group);
1020 return 0;
1024 * When removing container users, anything that removes the last user
1025 * implicitly removes the group from the container. That is, if the
1026 * group file descriptor is closed, as well as any device file descriptors,
1027 * the group is free.
1029 static void vfio_group_try_dissolve_container(struct vfio_group *group)
1031 if (0 == atomic_dec_if_positive(&group->container_users))
1032 __vfio_group_unset_container(group);
1035 static int vfio_group_set_container(struct vfio_group *group, int container_fd)
1037 struct fd f;
1038 struct vfio_container *container;
1039 struct vfio_iommu_driver *driver;
1040 int ret = 0;
1042 if (atomic_read(&group->container_users))
1043 return -EINVAL;
1045 f = fdget(container_fd);
1046 if (!f.file)
1047 return -EBADF;
1049 /* Sanity check, is this really our fd? */
1050 if (f.file->f_op != &vfio_fops) {
1051 fdput(f);
1052 return -EINVAL;
1055 container = f.file->private_data;
1056 WARN_ON(!container); /* fget ensures we don't race vfio_release */
1058 down_write(&container->group_lock);
1060 driver = container->iommu_driver;
1061 if (driver) {
1062 ret = driver->ops->attach_group(container->iommu_data,
1063 group->iommu_group);
1064 if (ret)
1065 goto unlock_out;
1068 group->container = container;
1069 list_add(&group->container_next, &container->group_list);
1071 /* Get a reference on the container and mark a user within the group */
1072 vfio_container_get(container);
1073 atomic_inc(&group->container_users);
1075 unlock_out:
1076 up_write(&container->group_lock);
1077 fdput(f);
1078 return ret;
1081 static bool vfio_group_viable(struct vfio_group *group)
1083 return (iommu_group_for_each_dev(group->iommu_group,
1084 group, vfio_dev_viable) == 0);
1087 static const struct file_operations vfio_device_fops;
1089 static int vfio_group_get_device_fd(struct vfio_group *group, char *buf)
1091 struct vfio_device *device;
1092 struct file *filep;
1093 int ret = -ENODEV;
1095 if (0 == atomic_read(&group->container_users) ||
1096 !group->container->iommu_driver || !vfio_group_viable(group))
1097 return -EINVAL;
1099 mutex_lock(&group->device_lock);
1100 list_for_each_entry(device, &group->device_list, group_next) {
1101 if (strcmp(dev_name(device->dev), buf))
1102 continue;
1104 ret = device->ops->open(device->device_data);
1105 if (ret)
1106 break;
1108 * We can't use anon_inode_getfd() because we need to modify
1109 * the f_mode flags directly to allow more than just ioctls
1111 ret = get_unused_fd_flags(O_CLOEXEC);
1112 if (ret < 0) {
1113 device->ops->release(device->device_data);
1114 break;
1117 filep = anon_inode_getfile("[vfio-device]", &vfio_device_fops,
1118 device, O_RDWR);
1119 if (IS_ERR(filep)) {
1120 put_unused_fd(ret);
1121 ret = PTR_ERR(filep);
1122 device->ops->release(device->device_data);
1123 break;
1127 * TODO: add an anon_inode interface to do this.
1128 * Appears to be missing by lack of need rather than
1129 * explicitly prevented. Now there's need.
1131 filep->f_mode |= (FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1133 vfio_device_get(device);
1134 atomic_inc(&group->container_users);
1136 fd_install(ret, filep);
1137 break;
1139 mutex_unlock(&group->device_lock);
1141 return ret;
1144 static long vfio_group_fops_unl_ioctl(struct file *filep,
1145 unsigned int cmd, unsigned long arg)
1147 struct vfio_group *group = filep->private_data;
1148 long ret = -ENOTTY;
1150 switch (cmd) {
1151 case VFIO_GROUP_GET_STATUS:
1153 struct vfio_group_status status;
1154 unsigned long minsz;
1156 minsz = offsetofend(struct vfio_group_status, flags);
1158 if (copy_from_user(&status, (void __user *)arg, minsz))
1159 return -EFAULT;
1161 if (status.argsz < minsz)
1162 return -EINVAL;
1164 status.flags = 0;
1166 if (vfio_group_viable(group))
1167 status.flags |= VFIO_GROUP_FLAGS_VIABLE;
1169 if (group->container)
1170 status.flags |= VFIO_GROUP_FLAGS_CONTAINER_SET;
1172 if (copy_to_user((void __user *)arg, &status, minsz))
1173 return -EFAULT;
1175 ret = 0;
1176 break;
1178 case VFIO_GROUP_SET_CONTAINER:
1180 int fd;
1182 if (get_user(fd, (int __user *)arg))
1183 return -EFAULT;
1185 if (fd < 0)
1186 return -EINVAL;
1188 ret = vfio_group_set_container(group, fd);
1189 break;
1191 case VFIO_GROUP_UNSET_CONTAINER:
1192 ret = vfio_group_unset_container(group);
1193 break;
1194 case VFIO_GROUP_GET_DEVICE_FD:
1196 char *buf;
1198 buf = strndup_user((const char __user *)arg, PAGE_SIZE);
1199 if (IS_ERR(buf))
1200 return PTR_ERR(buf);
1202 ret = vfio_group_get_device_fd(group, buf);
1203 kfree(buf);
1204 break;
1208 return ret;
1211 #ifdef CONFIG_COMPAT
1212 static long vfio_group_fops_compat_ioctl(struct file *filep,
1213 unsigned int cmd, unsigned long arg)
1215 arg = (unsigned long)compat_ptr(arg);
1216 return vfio_group_fops_unl_ioctl(filep, cmd, arg);
1218 #endif /* CONFIG_COMPAT */
1220 static int vfio_group_fops_open(struct inode *inode, struct file *filep)
1222 struct vfio_group *group;
1223 int opened;
1225 group = vfio_group_get_from_minor(iminor(inode));
1226 if (!group)
1227 return -ENODEV;
1229 /* Do we need multiple instances of the group open? Seems not. */
1230 opened = atomic_cmpxchg(&group->opened, 0, 1);
1231 if (opened) {
1232 vfio_group_put(group);
1233 return -EBUSY;
1236 /* Is something still in use from a previous open? */
1237 if (group->container) {
1238 atomic_dec(&group->opened);
1239 vfio_group_put(group);
1240 return -EBUSY;
1243 filep->private_data = group;
1245 return 0;
1248 static int vfio_group_fops_release(struct inode *inode, struct file *filep)
1250 struct vfio_group *group = filep->private_data;
1252 filep->private_data = NULL;
1254 vfio_group_try_dissolve_container(group);
1256 atomic_dec(&group->opened);
1258 vfio_group_put(group);
1260 return 0;
1263 static const struct file_operations vfio_group_fops = {
1264 .owner = THIS_MODULE,
1265 .unlocked_ioctl = vfio_group_fops_unl_ioctl,
1266 #ifdef CONFIG_COMPAT
1267 .compat_ioctl = vfio_group_fops_compat_ioctl,
1268 #endif
1269 .open = vfio_group_fops_open,
1270 .release = vfio_group_fops_release,
1274 * VFIO Device fd
1276 static int vfio_device_fops_release(struct inode *inode, struct file *filep)
1278 struct vfio_device *device = filep->private_data;
1280 device->ops->release(device->device_data);
1282 vfio_group_try_dissolve_container(device->group);
1284 vfio_device_put(device);
1286 return 0;
1289 static long vfio_device_fops_unl_ioctl(struct file *filep,
1290 unsigned int cmd, unsigned long arg)
1292 struct vfio_device *device = filep->private_data;
1294 if (unlikely(!device->ops->ioctl))
1295 return -EINVAL;
1297 return device->ops->ioctl(device->device_data, cmd, arg);
1300 static ssize_t vfio_device_fops_read(struct file *filep, char __user *buf,
1301 size_t count, loff_t *ppos)
1303 struct vfio_device *device = filep->private_data;
1305 if (unlikely(!device->ops->read))
1306 return -EINVAL;
1308 return device->ops->read(device->device_data, buf, count, ppos);
1311 static ssize_t vfio_device_fops_write(struct file *filep,
1312 const char __user *buf,
1313 size_t count, loff_t *ppos)
1315 struct vfio_device *device = filep->private_data;
1317 if (unlikely(!device->ops->write))
1318 return -EINVAL;
1320 return device->ops->write(device->device_data, buf, count, ppos);
1323 static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma)
1325 struct vfio_device *device = filep->private_data;
1327 if (unlikely(!device->ops->mmap))
1328 return -EINVAL;
1330 return device->ops->mmap(device->device_data, vma);
1333 #ifdef CONFIG_COMPAT
1334 static long vfio_device_fops_compat_ioctl(struct file *filep,
1335 unsigned int cmd, unsigned long arg)
1337 arg = (unsigned long)compat_ptr(arg);
1338 return vfio_device_fops_unl_ioctl(filep, cmd, arg);
1340 #endif /* CONFIG_COMPAT */
1342 static const struct file_operations vfio_device_fops = {
1343 .owner = THIS_MODULE,
1344 .release = vfio_device_fops_release,
1345 .read = vfio_device_fops_read,
1346 .write = vfio_device_fops_write,
1347 .unlocked_ioctl = vfio_device_fops_unl_ioctl,
1348 #ifdef CONFIG_COMPAT
1349 .compat_ioctl = vfio_device_fops_compat_ioctl,
1350 #endif
1351 .mmap = vfio_device_fops_mmap,
1355 * External user API, exported by symbols to be linked dynamically.
1357 * The protocol includes:
1358 * 1. do normal VFIO init operation:
1359 * - opening a new container;
1360 * - attaching group(s) to it;
1361 * - setting an IOMMU driver for a container.
1362 * When IOMMU is set for a container, all groups in it are
1363 * considered ready to use by an external user.
1365 * 2. User space passes a group fd to an external user.
1366 * The external user calls vfio_group_get_external_user()
1367 * to verify that:
1368 * - the group is initialized;
1369 * - IOMMU is set for it.
1370 * If both checks passed, vfio_group_get_external_user()
1371 * increments the container user counter to prevent
1372 * the VFIO group from disposal before KVM exits.
1374 * 3. The external user calls vfio_external_user_iommu_id()
1375 * to know an IOMMU ID.
1377 * 4. When the external KVM finishes, it calls
1378 * vfio_group_put_external_user() to release the VFIO group.
1379 * This call decrements the container user counter.
1381 struct vfio_group *vfio_group_get_external_user(struct file *filep)
1383 struct vfio_group *group = filep->private_data;
1385 if (filep->f_op != &vfio_group_fops)
1386 return ERR_PTR(-EINVAL);
1388 if (!atomic_inc_not_zero(&group->container_users))
1389 return ERR_PTR(-EINVAL);
1391 if (!group->container->iommu_driver ||
1392 !vfio_group_viable(group)) {
1393 atomic_dec(&group->container_users);
1394 return ERR_PTR(-EINVAL);
1397 vfio_group_get(group);
1399 return group;
1401 EXPORT_SYMBOL_GPL(vfio_group_get_external_user);
1403 void vfio_group_put_external_user(struct vfio_group *group)
1405 vfio_group_put(group);
1406 vfio_group_try_dissolve_container(group);
1408 EXPORT_SYMBOL_GPL(vfio_group_put_external_user);
1410 int vfio_external_user_iommu_id(struct vfio_group *group)
1412 return iommu_group_id(group->iommu_group);
1414 EXPORT_SYMBOL_GPL(vfio_external_user_iommu_id);
1417 * Module/class support
1419 static char *vfio_devnode(struct device *dev, umode_t *mode)
1421 return kasprintf(GFP_KERNEL, "vfio/%s", dev_name(dev));
1424 static struct miscdevice vfio_dev = {
1425 .minor = VFIO_MINOR,
1426 .name = "vfio",
1427 .fops = &vfio_fops,
1428 .nodename = "vfio/vfio",
1429 .mode = S_IRUGO | S_IWUGO,
1432 static int __init vfio_init(void)
1434 int ret;
1436 idr_init(&vfio.group_idr);
1437 mutex_init(&vfio.group_lock);
1438 mutex_init(&vfio.iommu_drivers_lock);
1439 INIT_LIST_HEAD(&vfio.group_list);
1440 INIT_LIST_HEAD(&vfio.iommu_drivers_list);
1441 init_waitqueue_head(&vfio.release_q);
1443 ret = misc_register(&vfio_dev);
1444 if (ret) {
1445 pr_err("vfio: misc device register failed\n");
1446 return ret;
1449 /* /dev/vfio/$GROUP */
1450 vfio.class = class_create(THIS_MODULE, "vfio");
1451 if (IS_ERR(vfio.class)) {
1452 ret = PTR_ERR(vfio.class);
1453 goto err_class;
1456 vfio.class->devnode = vfio_devnode;
1458 ret = alloc_chrdev_region(&vfio.group_devt, 0, MINORMASK, "vfio");
1459 if (ret)
1460 goto err_alloc_chrdev;
1462 cdev_init(&vfio.group_cdev, &vfio_group_fops);
1463 ret = cdev_add(&vfio.group_cdev, vfio.group_devt, MINORMASK);
1464 if (ret)
1465 goto err_cdev_add;
1467 pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
1470 * Attempt to load known iommu-drivers. This gives us a working
1471 * environment without the user needing to explicitly load iommu
1472 * drivers.
1474 request_module_nowait("vfio_iommu_type1");
1475 request_module_nowait("vfio_iommu_spapr_tce");
1477 return 0;
1479 err_cdev_add:
1480 unregister_chrdev_region(vfio.group_devt, MINORMASK);
1481 err_alloc_chrdev:
1482 class_destroy(vfio.class);
1483 vfio.class = NULL;
1484 err_class:
1485 misc_deregister(&vfio_dev);
1486 return ret;
1489 static void __exit vfio_cleanup(void)
1491 WARN_ON(!list_empty(&vfio.group_list));
1493 idr_destroy(&vfio.group_idr);
1494 cdev_del(&vfio.group_cdev);
1495 unregister_chrdev_region(vfio.group_devt, MINORMASK);
1496 class_destroy(vfio.class);
1497 vfio.class = NULL;
1498 misc_deregister(&vfio_dev);
1501 module_init(vfio_init);
1502 module_exit(vfio_cleanup);
1504 MODULE_VERSION(DRIVER_VERSION);
1505 MODULE_LICENSE("GPL v2");
1506 MODULE_AUTHOR(DRIVER_AUTHOR);
1507 MODULE_DESCRIPTION(DRIVER_DESC);
1508 MODULE_ALIAS_MISCDEV(VFIO_MINOR);
1509 MODULE_ALIAS("devname:vfio/vfio");