blob: 92b85676e6bed3ca908866c74b62fab197876757 [file] [log] [blame]
Alex Williamsoncba33452012-07-31 08:16:22 -06001/*
2 * VFIO core
3 *
4 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
5 * Author: Alex Williamson <alex.williamson@redhat.com>
6 *
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.
10 *
11 * Derived from original vfio:
12 * Copyright 2010 Cisco Systems, Inc. All rights reserved.
13 * Author: Tom Lyon, pugs@cisco.com
14 */
15
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/module.h>
26#include <linux/mutex.h>
27#include <linux/sched.h>
28#include <linux/slab.h>
29#include <linux/string.h>
30#include <linux/uaccess.h>
31#include <linux/vfio.h>
32#include <linux/wait.h>
33
34#define DRIVER_VERSION "0.3"
35#define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>"
36#define DRIVER_DESC "VFIO - User Level meta-driver"
37
38static struct vfio {
39 struct class *class;
40 struct list_head iommu_drivers_list;
41 struct mutex iommu_drivers_lock;
42 struct list_head group_list;
43 struct idr group_idr;
44 struct mutex group_lock;
45 struct cdev group_cdev;
46 struct device *dev;
47 dev_t devt;
48 struct cdev cdev;
49 wait_queue_head_t release_q;
50} vfio;
51
52struct vfio_iommu_driver {
53 const struct vfio_iommu_driver_ops *ops;
54 struct list_head vfio_next;
55};
56
57struct vfio_container {
58 struct kref kref;
59 struct list_head group_list;
60 struct mutex group_lock;
61 struct vfio_iommu_driver *iommu_driver;
62 void *iommu_data;
63};
64
65struct vfio_group {
66 struct kref kref;
67 int minor;
68 atomic_t container_users;
69 struct iommu_group *iommu_group;
70 struct vfio_container *container;
71 struct list_head device_list;
72 struct mutex device_lock;
73 struct device *dev;
74 struct notifier_block nb;
75 struct list_head vfio_next;
76 struct list_head container_next;
77};
78
79struct vfio_device {
80 struct kref kref;
81 struct device *dev;
82 const struct vfio_device_ops *ops;
83 struct vfio_group *group;
84 struct list_head group_next;
85 void *device_data;
86};
87
88/**
89 * IOMMU driver registration
90 */
91int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops)
92{
93 struct vfio_iommu_driver *driver, *tmp;
94
95 driver = kzalloc(sizeof(*driver), GFP_KERNEL);
96 if (!driver)
97 return -ENOMEM;
98
99 driver->ops = ops;
100
101 mutex_lock(&vfio.iommu_drivers_lock);
102
103 /* Check for duplicates */
104 list_for_each_entry(tmp, &vfio.iommu_drivers_list, vfio_next) {
105 if (tmp->ops == ops) {
106 mutex_unlock(&vfio.iommu_drivers_lock);
107 kfree(driver);
108 return -EINVAL;
109 }
110 }
111
112 list_add(&driver->vfio_next, &vfio.iommu_drivers_list);
113
114 mutex_unlock(&vfio.iommu_drivers_lock);
115
116 return 0;
117}
118EXPORT_SYMBOL_GPL(vfio_register_iommu_driver);
119
120void vfio_unregister_iommu_driver(const struct vfio_iommu_driver_ops *ops)
121{
122 struct vfio_iommu_driver *driver;
123
124 mutex_lock(&vfio.iommu_drivers_lock);
125 list_for_each_entry(driver, &vfio.iommu_drivers_list, vfio_next) {
126 if (driver->ops == ops) {
127 list_del(&driver->vfio_next);
128 mutex_unlock(&vfio.iommu_drivers_lock);
129 kfree(driver);
130 return;
131 }
132 }
133 mutex_unlock(&vfio.iommu_drivers_lock);
134}
135EXPORT_SYMBOL_GPL(vfio_unregister_iommu_driver);
136
137/**
138 * Group minor allocation/free - both called with vfio.group_lock held
139 */
140static int vfio_alloc_group_minor(struct vfio_group *group)
141{
142 int ret, minor;
143
144again:
145 if (unlikely(idr_pre_get(&vfio.group_idr, GFP_KERNEL) == 0))
146 return -ENOMEM;
147
148 /* index 0 is used by /dev/vfio/vfio */
149 ret = idr_get_new_above(&vfio.group_idr, group, 1, &minor);
150 if (ret == -EAGAIN)
151 goto again;
152 if (ret || minor > MINORMASK) {
153 if (minor > MINORMASK)
154 idr_remove(&vfio.group_idr, minor);
155 return -ENOSPC;
156 }
157
158 return minor;
159}
160
161static void vfio_free_group_minor(int minor)
162{
163 idr_remove(&vfio.group_idr, minor);
164}
165
166static int vfio_iommu_group_notifier(struct notifier_block *nb,
167 unsigned long action, void *data);
168static void vfio_group_get(struct vfio_group *group);
169
170/**
171 * Container objects - containers are created when /dev/vfio/vfio is
172 * opened, but their lifecycle extends until the last user is done, so
173 * it's freed via kref. Must support container/group/device being
174 * closed in any order.
175 */
176static void vfio_container_get(struct vfio_container *container)
177{
178 kref_get(&container->kref);
179}
180
181static void vfio_container_release(struct kref *kref)
182{
183 struct vfio_container *container;
184 container = container_of(kref, struct vfio_container, kref);
185
186 kfree(container);
187}
188
189static void vfio_container_put(struct vfio_container *container)
190{
191 kref_put(&container->kref, vfio_container_release);
192}
193
194/**
195 * Group objects - create, release, get, put, search
196 */
197static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group)
198{
199 struct vfio_group *group, *tmp;
200 struct device *dev;
201 int ret, minor;
202
203 group = kzalloc(sizeof(*group), GFP_KERNEL);
204 if (!group)
205 return ERR_PTR(-ENOMEM);
206
207 kref_init(&group->kref);
208 INIT_LIST_HEAD(&group->device_list);
209 mutex_init(&group->device_lock);
210 atomic_set(&group->container_users, 0);
211 group->iommu_group = iommu_group;
212
213 group->nb.notifier_call = vfio_iommu_group_notifier;
214
215 /*
216 * blocking notifiers acquire a rwsem around registering and hold
217 * it around callback. Therefore, need to register outside of
218 * vfio.group_lock to avoid A-B/B-A contention. Our callback won't
219 * do anything unless it can find the group in vfio.group_list, so
220 * no harm in registering early.
221 */
222 ret = iommu_group_register_notifier(iommu_group, &group->nb);
223 if (ret) {
224 kfree(group);
225 return ERR_PTR(ret);
226 }
227
228 mutex_lock(&vfio.group_lock);
229
230 minor = vfio_alloc_group_minor(group);
231 if (minor < 0) {
232 mutex_unlock(&vfio.group_lock);
233 kfree(group);
234 return ERR_PTR(minor);
235 }
236
237 /* Did we race creating this group? */
238 list_for_each_entry(tmp, &vfio.group_list, vfio_next) {
239 if (tmp->iommu_group == iommu_group) {
240 vfio_group_get(tmp);
241 vfio_free_group_minor(minor);
242 mutex_unlock(&vfio.group_lock);
243 kfree(group);
244 return tmp;
245 }
246 }
247
248 dev = device_create(vfio.class, NULL, MKDEV(MAJOR(vfio.devt), minor),
249 group, "%d", iommu_group_id(iommu_group));
250 if (IS_ERR(dev)) {
251 vfio_free_group_minor(minor);
252 mutex_unlock(&vfio.group_lock);
253 kfree(group);
254 return (struct vfio_group *)dev; /* ERR_PTR */
255 }
256
257 group->minor = minor;
258 group->dev = dev;
259
260 list_add(&group->vfio_next, &vfio.group_list);
261
262 mutex_unlock(&vfio.group_lock);
263
264 return group;
265}
266
Al Viro6d2cd3c2012-08-17 21:27:32 -0400267/* called with vfio.group_lock held */
Alex Williamsoncba33452012-07-31 08:16:22 -0600268static void vfio_group_release(struct kref *kref)
269{
270 struct vfio_group *group = container_of(kref, struct vfio_group, kref);
271
272 WARN_ON(!list_empty(&group->device_list));
273
274 device_destroy(vfio.class, MKDEV(MAJOR(vfio.devt), group->minor));
275 list_del(&group->vfio_next);
276 vfio_free_group_minor(group->minor);
277
278 mutex_unlock(&vfio.group_lock);
279
280 /*
281 * Unregister outside of lock. A spurious callback is harmless now
282 * that the group is no longer in vfio.group_list.
283 */
284 iommu_group_unregister_notifier(group->iommu_group, &group->nb);
285
286 kfree(group);
287}
288
289static void vfio_group_put(struct vfio_group *group)
290{
Al Viro6d2cd3c2012-08-17 21:27:32 -0400291 kref_put_mutex(&group->kref, vfio_group_release, &vfio.group_lock);
Alex Williamsoncba33452012-07-31 08:16:22 -0600292}
293
294/* Assume group_lock or group reference is held */
295static void vfio_group_get(struct vfio_group *group)
296{
297 kref_get(&group->kref);
298}
299
300/*
301 * Not really a try as we will sleep for mutex, but we need to make
302 * sure the group pointer is valid under lock and get a reference.
303 */
304static struct vfio_group *vfio_group_try_get(struct vfio_group *group)
305{
306 struct vfio_group *target = group;
307
308 mutex_lock(&vfio.group_lock);
309 list_for_each_entry(group, &vfio.group_list, vfio_next) {
310 if (group == target) {
311 vfio_group_get(group);
312 mutex_unlock(&vfio.group_lock);
313 return group;
314 }
315 }
316 mutex_unlock(&vfio.group_lock);
317
318 return NULL;
319}
320
321static
322struct vfio_group *vfio_group_get_from_iommu(struct iommu_group *iommu_group)
323{
324 struct vfio_group *group;
325
326 mutex_lock(&vfio.group_lock);
327 list_for_each_entry(group, &vfio.group_list, vfio_next) {
328 if (group->iommu_group == iommu_group) {
329 vfio_group_get(group);
330 mutex_unlock(&vfio.group_lock);
331 return group;
332 }
333 }
334 mutex_unlock(&vfio.group_lock);
335
336 return NULL;
337}
338
339static struct vfio_group *vfio_group_get_from_minor(int minor)
340{
341 struct vfio_group *group;
342
343 mutex_lock(&vfio.group_lock);
344 group = idr_find(&vfio.group_idr, minor);
345 if (!group) {
346 mutex_unlock(&vfio.group_lock);
347 return NULL;
348 }
349 vfio_group_get(group);
350 mutex_unlock(&vfio.group_lock);
351
352 return group;
353}
354
355/**
356 * Device objects - create, release, get, put, search
357 */
358static
359struct vfio_device *vfio_group_create_device(struct vfio_group *group,
360 struct device *dev,
361 const struct vfio_device_ops *ops,
362 void *device_data)
363{
364 struct vfio_device *device;
365 int ret;
366
367 device = kzalloc(sizeof(*device), GFP_KERNEL);
368 if (!device)
369 return ERR_PTR(-ENOMEM);
370
371 kref_init(&device->kref);
372 device->dev = dev;
373 device->group = group;
374 device->ops = ops;
375 device->device_data = device_data;
376
377 ret = dev_set_drvdata(dev, device);
378 if (ret) {
379 kfree(device);
380 return ERR_PTR(ret);
381 }
382
383 /* No need to get group_lock, caller has group reference */
384 vfio_group_get(group);
385
386 mutex_lock(&group->device_lock);
387 list_add(&device->group_next, &group->device_list);
388 mutex_unlock(&group->device_lock);
389
390 return device;
391}
392
393static void vfio_device_release(struct kref *kref)
394{
395 struct vfio_device *device = container_of(kref,
396 struct vfio_device, kref);
397 struct vfio_group *group = device->group;
398
399 mutex_lock(&group->device_lock);
400 list_del(&device->group_next);
401 mutex_unlock(&group->device_lock);
402
403 dev_set_drvdata(device->dev, NULL);
404
405 kfree(device);
406
407 /* vfio_del_group_dev may be waiting for this device */
408 wake_up(&vfio.release_q);
409}
410
411/* Device reference always implies a group reference */
412static void vfio_device_put(struct vfio_device *device)
413{
Al Viro934ad4c2012-08-17 19:49:09 -0400414 struct vfio_group *group = device->group;
Alex Williamsoncba33452012-07-31 08:16:22 -0600415 kref_put(&device->kref, vfio_device_release);
Al Viro934ad4c2012-08-17 19:49:09 -0400416 vfio_group_put(group);
Alex Williamsoncba33452012-07-31 08:16:22 -0600417}
418
419static void vfio_device_get(struct vfio_device *device)
420{
421 vfio_group_get(device->group);
422 kref_get(&device->kref);
423}
424
425static struct vfio_device *vfio_group_get_device(struct vfio_group *group,
426 struct device *dev)
427{
428 struct vfio_device *device;
429
430 mutex_lock(&group->device_lock);
431 list_for_each_entry(device, &group->device_list, group_next) {
432 if (device->dev == dev) {
433 vfio_device_get(device);
434 mutex_unlock(&group->device_lock);
435 return device;
436 }
437 }
438 mutex_unlock(&group->device_lock);
439 return NULL;
440}
441
442/*
443 * Whitelist some drivers that we know are safe (no dma) or just sit on
444 * a device. It's not always practical to leave a device within a group
445 * driverless as it could get re-bound to something unsafe.
446 */
447static const char * const vfio_driver_whitelist[] = { "pci-stub" };
448
449static bool vfio_whitelisted_driver(struct device_driver *drv)
450{
451 int i;
452
453 for (i = 0; i < ARRAY_SIZE(vfio_driver_whitelist); i++) {
454 if (!strcmp(drv->name, vfio_driver_whitelist[i]))
455 return true;
456 }
457
458 return false;
459}
460
461/*
462 * A vfio group is viable for use by userspace if all devices are either
463 * driver-less or bound to a vfio or whitelisted driver. We test the
464 * latter by the existence of a struct vfio_device matching the dev.
465 */
466static int vfio_dev_viable(struct device *dev, void *data)
467{
468 struct vfio_group *group = data;
469 struct vfio_device *device;
470
471 if (!dev->driver || vfio_whitelisted_driver(dev->driver))
472 return 0;
473
474 device = vfio_group_get_device(group, dev);
475 if (device) {
476 vfio_device_put(device);
477 return 0;
478 }
479
480 return -EINVAL;
481}
482
483/**
484 * Async device support
485 */
486static int vfio_group_nb_add_dev(struct vfio_group *group, struct device *dev)
487{
488 struct vfio_device *device;
489
490 /* Do we already know about it? We shouldn't */
491 device = vfio_group_get_device(group, dev);
492 if (WARN_ON_ONCE(device)) {
493 vfio_device_put(device);
494 return 0;
495 }
496
497 /* Nothing to do for idle groups */
498 if (!atomic_read(&group->container_users))
499 return 0;
500
501 /* TODO Prevent device auto probing */
502 WARN("Device %s added to live group %d!\n", dev_name(dev),
503 iommu_group_id(group->iommu_group));
504
505 return 0;
506}
507
508static int vfio_group_nb_del_dev(struct vfio_group *group, struct device *dev)
509{
510 struct vfio_device *device;
511
512 /*
513 * Expect to fall out here. If a device was in use, it would
514 * have been bound to a vfio sub-driver, which would have blocked
515 * in .remove at vfio_del_group_dev. Sanity check that we no
516 * longer track the device, so it's safe to remove.
517 */
518 device = vfio_group_get_device(group, dev);
519 if (likely(!device))
520 return 0;
521
522 WARN("Device %s removed from live group %d!\n", dev_name(dev),
523 iommu_group_id(group->iommu_group));
524
525 vfio_device_put(device);
526 return 0;
527}
528
529static int vfio_group_nb_verify(struct vfio_group *group, struct device *dev)
530{
531 /* We don't care what happens when the group isn't in use */
532 if (!atomic_read(&group->container_users))
533 return 0;
534
535 return vfio_dev_viable(dev, group);
536}
537
538static int vfio_iommu_group_notifier(struct notifier_block *nb,
539 unsigned long action, void *data)
540{
541 struct vfio_group *group = container_of(nb, struct vfio_group, nb);
542 struct device *dev = data;
543
544 /*
545 * Need to go through a group_lock lookup to get a reference or
546 * we risk racing a group being removed. Leave a WARN_ON for
547 * debuging, but if the group no longer exists, a spurious notify
548 * is harmless.
549 */
550 group = vfio_group_try_get(group);
551 if (WARN_ON(!group))
552 return NOTIFY_OK;
553
554 switch (action) {
555 case IOMMU_GROUP_NOTIFY_ADD_DEVICE:
556 vfio_group_nb_add_dev(group, dev);
557 break;
558 case IOMMU_GROUP_NOTIFY_DEL_DEVICE:
559 vfio_group_nb_del_dev(group, dev);
560 break;
561 case IOMMU_GROUP_NOTIFY_BIND_DRIVER:
562 pr_debug("%s: Device %s, group %d binding to driver\n",
563 __func__, dev_name(dev),
564 iommu_group_id(group->iommu_group));
565 break;
566 case IOMMU_GROUP_NOTIFY_BOUND_DRIVER:
567 pr_debug("%s: Device %s, group %d bound to driver %s\n",
568 __func__, dev_name(dev),
569 iommu_group_id(group->iommu_group), dev->driver->name);
570 BUG_ON(vfio_group_nb_verify(group, dev));
571 break;
572 case IOMMU_GROUP_NOTIFY_UNBIND_DRIVER:
573 pr_debug("%s: Device %s, group %d unbinding from driver %s\n",
574 __func__, dev_name(dev),
575 iommu_group_id(group->iommu_group), dev->driver->name);
576 break;
577 case IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER:
578 pr_debug("%s: Device %s, group %d unbound from driver\n",
579 __func__, dev_name(dev),
580 iommu_group_id(group->iommu_group));
581 /*
582 * XXX An unbound device in a live group is ok, but we'd
583 * really like to avoid the above BUG_ON by preventing other
584 * drivers from binding to it. Once that occurs, we have to
585 * stop the system to maintain isolation. At a minimum, we'd
586 * want a toggle to disable driver auto probe for this device.
587 */
588 break;
589 }
590
591 vfio_group_put(group);
592 return NOTIFY_OK;
593}
594
595/**
596 * VFIO driver API
597 */
598int vfio_add_group_dev(struct device *dev,
599 const struct vfio_device_ops *ops, void *device_data)
600{
601 struct iommu_group *iommu_group;
602 struct vfio_group *group;
603 struct vfio_device *device;
604
605 iommu_group = iommu_group_get(dev);
606 if (!iommu_group)
607 return -EINVAL;
608
609 group = vfio_group_get_from_iommu(iommu_group);
610 if (!group) {
611 group = vfio_create_group(iommu_group);
612 if (IS_ERR(group)) {
613 iommu_group_put(iommu_group);
614 return PTR_ERR(group);
615 }
616 }
617
618 device = vfio_group_get_device(group, dev);
619 if (device) {
620 WARN(1, "Device %s already exists on group %d\n",
621 dev_name(dev), iommu_group_id(iommu_group));
622 vfio_device_put(device);
623 vfio_group_put(group);
624 iommu_group_put(iommu_group);
625 return -EBUSY;
626 }
627
628 device = vfio_group_create_device(group, dev, ops, device_data);
629 if (IS_ERR(device)) {
630 vfio_group_put(group);
631 iommu_group_put(iommu_group);
632 return PTR_ERR(device);
633 }
634
635 /*
636 * Added device holds reference to iommu_group and vfio_device
637 * (which in turn holds reference to vfio_group). Drop extra
638 * group reference used while acquiring device.
639 */
640 vfio_group_put(group);
641
642 return 0;
643}
644EXPORT_SYMBOL_GPL(vfio_add_group_dev);
645
646/* Test whether a struct device is present in our tracking */
647static bool vfio_dev_present(struct device *dev)
648{
649 struct iommu_group *iommu_group;
650 struct vfio_group *group;
651 struct vfio_device *device;
652
653 iommu_group = iommu_group_get(dev);
654 if (!iommu_group)
655 return false;
656
657 group = vfio_group_get_from_iommu(iommu_group);
658 if (!group) {
659 iommu_group_put(iommu_group);
660 return false;
661 }
662
663 device = vfio_group_get_device(group, dev);
664 if (!device) {
665 vfio_group_put(group);
666 iommu_group_put(iommu_group);
667 return false;
668 }
669
670 vfio_device_put(device);
671 vfio_group_put(group);
672 iommu_group_put(iommu_group);
673 return true;
674}
675
676/*
677 * Decrement the device reference count and wait for the device to be
678 * removed. Open file descriptors for the device... */
679void *vfio_del_group_dev(struct device *dev)
680{
681 struct vfio_device *device = dev_get_drvdata(dev);
682 struct vfio_group *group = device->group;
683 struct iommu_group *iommu_group = group->iommu_group;
684 void *device_data = device->device_data;
685
686 vfio_device_put(device);
687
688 /* TODO send a signal to encourage this to be released */
689 wait_event(vfio.release_q, !vfio_dev_present(dev));
690
691 iommu_group_put(iommu_group);
692
693 return device_data;
694}
695EXPORT_SYMBOL_GPL(vfio_del_group_dev);
696
697/**
698 * VFIO base fd, /dev/vfio/vfio
699 */
700static long vfio_ioctl_check_extension(struct vfio_container *container,
701 unsigned long arg)
702{
703 struct vfio_iommu_driver *driver = container->iommu_driver;
704 long ret = 0;
705
706 switch (arg) {
707 /* No base extensions yet */
708 default:
709 /*
710 * If no driver is set, poll all registered drivers for
711 * extensions and return the first positive result. If
712 * a driver is already set, further queries will be passed
713 * only to that driver.
714 */
715 if (!driver) {
716 mutex_lock(&vfio.iommu_drivers_lock);
717 list_for_each_entry(driver, &vfio.iommu_drivers_list,
718 vfio_next) {
719 if (!try_module_get(driver->ops->owner))
720 continue;
721
722 ret = driver->ops->ioctl(NULL,
723 VFIO_CHECK_EXTENSION,
724 arg);
725 module_put(driver->ops->owner);
726 if (ret > 0)
727 break;
728 }
729 mutex_unlock(&vfio.iommu_drivers_lock);
730 } else
731 ret = driver->ops->ioctl(container->iommu_data,
732 VFIO_CHECK_EXTENSION, arg);
733 }
734
735 return ret;
736}
737
738/* hold container->group_lock */
739static int __vfio_container_attach_groups(struct vfio_container *container,
740 struct vfio_iommu_driver *driver,
741 void *data)
742{
743 struct vfio_group *group;
744 int ret = -ENODEV;
745
746 list_for_each_entry(group, &container->group_list, container_next) {
747 ret = driver->ops->attach_group(data, group->iommu_group);
748 if (ret)
749 goto unwind;
750 }
751
752 return ret;
753
754unwind:
755 list_for_each_entry_continue_reverse(group, &container->group_list,
756 container_next) {
757 driver->ops->detach_group(data, group->iommu_group);
758 }
759
760 return ret;
761}
762
763static long vfio_ioctl_set_iommu(struct vfio_container *container,
764 unsigned long arg)
765{
766 struct vfio_iommu_driver *driver;
767 long ret = -ENODEV;
768
769 mutex_lock(&container->group_lock);
770
771 /*
772 * The container is designed to be an unprivileged interface while
773 * the group can be assigned to specific users. Therefore, only by
774 * adding a group to a container does the user get the privilege of
775 * enabling the iommu, which may allocate finite resources. There
776 * is no unset_iommu, but by removing all the groups from a container,
777 * the container is deprivileged and returns to an unset state.
778 */
779 if (list_empty(&container->group_list) || container->iommu_driver) {
780 mutex_unlock(&container->group_lock);
781 return -EINVAL;
782 }
783
784 mutex_lock(&vfio.iommu_drivers_lock);
785 list_for_each_entry(driver, &vfio.iommu_drivers_list, vfio_next) {
786 void *data;
787
788 if (!try_module_get(driver->ops->owner))
789 continue;
790
791 /*
792 * The arg magic for SET_IOMMU is the same as CHECK_EXTENSION,
793 * so test which iommu driver reported support for this
794 * extension and call open on them. We also pass them the
795 * magic, allowing a single driver to support multiple
796 * interfaces if they'd like.
797 */
798 if (driver->ops->ioctl(NULL, VFIO_CHECK_EXTENSION, arg) <= 0) {
799 module_put(driver->ops->owner);
800 continue;
801 }
802
803 /* module reference holds the driver we're working on */
804 mutex_unlock(&vfio.iommu_drivers_lock);
805
806 data = driver->ops->open(arg);
807 if (IS_ERR(data)) {
808 ret = PTR_ERR(data);
809 module_put(driver->ops->owner);
810 goto skip_drivers_unlock;
811 }
812
813 ret = __vfio_container_attach_groups(container, driver, data);
814 if (!ret) {
815 container->iommu_driver = driver;
816 container->iommu_data = data;
817 } else {
818 driver->ops->release(data);
819 module_put(driver->ops->owner);
820 }
821
822 goto skip_drivers_unlock;
823 }
824
825 mutex_unlock(&vfio.iommu_drivers_lock);
826skip_drivers_unlock:
827 mutex_unlock(&container->group_lock);
828
829 return ret;
830}
831
832static long vfio_fops_unl_ioctl(struct file *filep,
833 unsigned int cmd, unsigned long arg)
834{
835 struct vfio_container *container = filep->private_data;
836 struct vfio_iommu_driver *driver;
837 void *data;
838 long ret = -EINVAL;
839
840 if (!container)
841 return ret;
842
843 driver = container->iommu_driver;
844 data = container->iommu_data;
845
846 switch (cmd) {
847 case VFIO_GET_API_VERSION:
848 ret = VFIO_API_VERSION;
849 break;
850 case VFIO_CHECK_EXTENSION:
851 ret = vfio_ioctl_check_extension(container, arg);
852 break;
853 case VFIO_SET_IOMMU:
854 ret = vfio_ioctl_set_iommu(container, arg);
855 break;
856 default:
857 if (driver) /* passthrough all unrecognized ioctls */
858 ret = driver->ops->ioctl(data, cmd, arg);
859 }
860
861 return ret;
862}
863
864#ifdef CONFIG_COMPAT
865static long vfio_fops_compat_ioctl(struct file *filep,
866 unsigned int cmd, unsigned long arg)
867{
868 arg = (unsigned long)compat_ptr(arg);
869 return vfio_fops_unl_ioctl(filep, cmd, arg);
870}
871#endif /* CONFIG_COMPAT */
872
873static int vfio_fops_open(struct inode *inode, struct file *filep)
874{
875 struct vfio_container *container;
876
877 container = kzalloc(sizeof(*container), GFP_KERNEL);
878 if (!container)
879 return -ENOMEM;
880
881 INIT_LIST_HEAD(&container->group_list);
882 mutex_init(&container->group_lock);
883 kref_init(&container->kref);
884
885 filep->private_data = container;
886
887 return 0;
888}
889
890static int vfio_fops_release(struct inode *inode, struct file *filep)
891{
892 struct vfio_container *container = filep->private_data;
893
894 filep->private_data = NULL;
895
896 vfio_container_put(container);
897
898 return 0;
899}
900
901/*
902 * Once an iommu driver is set, we optionally pass read/write/mmap
903 * on to the driver, allowing management interfaces beyond ioctl.
904 */
905static ssize_t vfio_fops_read(struct file *filep, char __user *buf,
906 size_t count, loff_t *ppos)
907{
908 struct vfio_container *container = filep->private_data;
909 struct vfio_iommu_driver *driver = container->iommu_driver;
910
911 if (unlikely(!driver || !driver->ops->read))
912 return -EINVAL;
913
914 return driver->ops->read(container->iommu_data, buf, count, ppos);
915}
916
917static ssize_t vfio_fops_write(struct file *filep, const char __user *buf,
918 size_t count, loff_t *ppos)
919{
920 struct vfio_container *container = filep->private_data;
921 struct vfio_iommu_driver *driver = container->iommu_driver;
922
923 if (unlikely(!driver || !driver->ops->write))
924 return -EINVAL;
925
926 return driver->ops->write(container->iommu_data, buf, count, ppos);
927}
928
929static int vfio_fops_mmap(struct file *filep, struct vm_area_struct *vma)
930{
931 struct vfio_container *container = filep->private_data;
932 struct vfio_iommu_driver *driver = container->iommu_driver;
933
934 if (unlikely(!driver || !driver->ops->mmap))
935 return -EINVAL;
936
937 return driver->ops->mmap(container->iommu_data, vma);
938}
939
940static const struct file_operations vfio_fops = {
941 .owner = THIS_MODULE,
942 .open = vfio_fops_open,
943 .release = vfio_fops_release,
944 .read = vfio_fops_read,
945 .write = vfio_fops_write,
946 .unlocked_ioctl = vfio_fops_unl_ioctl,
947#ifdef CONFIG_COMPAT
948 .compat_ioctl = vfio_fops_compat_ioctl,
949#endif
950 .mmap = vfio_fops_mmap,
951};
952
953/**
954 * VFIO Group fd, /dev/vfio/$GROUP
955 */
956static void __vfio_group_unset_container(struct vfio_group *group)
957{
958 struct vfio_container *container = group->container;
959 struct vfio_iommu_driver *driver;
960
961 mutex_lock(&container->group_lock);
962
963 driver = container->iommu_driver;
964 if (driver)
965 driver->ops->detach_group(container->iommu_data,
966 group->iommu_group);
967
968 group->container = NULL;
969 list_del(&group->container_next);
970
971 /* Detaching the last group deprivileges a container, remove iommu */
972 if (driver && list_empty(&container->group_list)) {
973 driver->ops->release(container->iommu_data);
974 module_put(driver->ops->owner);
975 container->iommu_driver = NULL;
976 container->iommu_data = NULL;
977 }
978
979 mutex_unlock(&container->group_lock);
980
981 vfio_container_put(container);
982}
983
984/*
985 * VFIO_GROUP_UNSET_CONTAINER should fail if there are other users or
986 * if there was no container to unset. Since the ioctl is called on
987 * the group, we know that still exists, therefore the only valid
988 * transition here is 1->0.
989 */
990static int vfio_group_unset_container(struct vfio_group *group)
991{
992 int users = atomic_cmpxchg(&group->container_users, 1, 0);
993
994 if (!users)
995 return -EINVAL;
996 if (users != 1)
997 return -EBUSY;
998
999 __vfio_group_unset_container(group);
1000
1001 return 0;
1002}
1003
1004/*
1005 * When removing container users, anything that removes the last user
1006 * implicitly removes the group from the container. That is, if the
1007 * group file descriptor is closed, as well as any device file descriptors,
1008 * the group is free.
1009 */
1010static void vfio_group_try_dissolve_container(struct vfio_group *group)
1011{
1012 if (0 == atomic_dec_if_positive(&group->container_users))
1013 __vfio_group_unset_container(group);
1014}
1015
1016static int vfio_group_set_container(struct vfio_group *group, int container_fd)
1017{
1018 struct file *filep;
1019 struct vfio_container *container;
1020 struct vfio_iommu_driver *driver;
1021 int ret = 0;
1022
1023 if (atomic_read(&group->container_users))
1024 return -EINVAL;
1025
1026 filep = fget(container_fd);
1027 if (!filep)
1028 return -EBADF;
1029
1030 /* Sanity check, is this really our fd? */
1031 if (filep->f_op != &vfio_fops) {
1032 fput(filep);
1033 return -EINVAL;
1034 }
1035
1036 container = filep->private_data;
1037 WARN_ON(!container); /* fget ensures we don't race vfio_release */
1038
1039 mutex_lock(&container->group_lock);
1040
1041 driver = container->iommu_driver;
1042 if (driver) {
1043 ret = driver->ops->attach_group(container->iommu_data,
1044 group->iommu_group);
1045 if (ret)
1046 goto unlock_out;
1047 }
1048
1049 group->container = container;
1050 list_add(&group->container_next, &container->group_list);
1051
1052 /* Get a reference on the container and mark a user within the group */
1053 vfio_container_get(container);
1054 atomic_inc(&group->container_users);
1055
1056unlock_out:
1057 mutex_unlock(&container->group_lock);
1058 fput(filep);
1059
1060 return ret;
1061}
1062
1063static bool vfio_group_viable(struct vfio_group *group)
1064{
1065 return (iommu_group_for_each_dev(group->iommu_group,
1066 group, vfio_dev_viable) == 0);
1067}
1068
1069static const struct file_operations vfio_device_fops;
1070
1071static int vfio_group_get_device_fd(struct vfio_group *group, char *buf)
1072{
1073 struct vfio_device *device;
1074 struct file *filep;
1075 int ret = -ENODEV;
1076
1077 if (0 == atomic_read(&group->container_users) ||
1078 !group->container->iommu_driver || !vfio_group_viable(group))
1079 return -EINVAL;
1080
1081 mutex_lock(&group->device_lock);
1082 list_for_each_entry(device, &group->device_list, group_next) {
1083 if (strcmp(dev_name(device->dev), buf))
1084 continue;
1085
1086 ret = device->ops->open(device->device_data);
1087 if (ret)
1088 break;
1089 /*
1090 * We can't use anon_inode_getfd() because we need to modify
1091 * the f_mode flags directly to allow more than just ioctls
1092 */
1093 ret = get_unused_fd();
1094 if (ret < 0) {
1095 device->ops->release(device->device_data);
1096 break;
1097 }
1098
1099 filep = anon_inode_getfile("[vfio-device]", &vfio_device_fops,
1100 device, O_RDWR);
1101 if (IS_ERR(filep)) {
1102 put_unused_fd(ret);
1103 ret = PTR_ERR(filep);
1104 device->ops->release(device->device_data);
1105 break;
1106 }
1107
1108 /*
1109 * TODO: add an anon_inode interface to do this.
1110 * Appears to be missing by lack of need rather than
1111 * explicitly prevented. Now there's need.
1112 */
1113 filep->f_mode |= (FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1114
1115 fd_install(ret, filep);
1116
1117 vfio_device_get(device);
1118 atomic_inc(&group->container_users);
1119 break;
1120 }
1121 mutex_unlock(&group->device_lock);
1122
1123 return ret;
1124}
1125
1126static long vfio_group_fops_unl_ioctl(struct file *filep,
1127 unsigned int cmd, unsigned long arg)
1128{
1129 struct vfio_group *group = filep->private_data;
1130 long ret = -ENOTTY;
1131
1132 switch (cmd) {
1133 case VFIO_GROUP_GET_STATUS:
1134 {
1135 struct vfio_group_status status;
1136 unsigned long minsz;
1137
1138 minsz = offsetofend(struct vfio_group_status, flags);
1139
1140 if (copy_from_user(&status, (void __user *)arg, minsz))
1141 return -EFAULT;
1142
1143 if (status.argsz < minsz)
1144 return -EINVAL;
1145
1146 status.flags = 0;
1147
1148 if (vfio_group_viable(group))
1149 status.flags |= VFIO_GROUP_FLAGS_VIABLE;
1150
1151 if (group->container)
1152 status.flags |= VFIO_GROUP_FLAGS_CONTAINER_SET;
1153
1154 if (copy_to_user((void __user *)arg, &status, minsz))
1155 return -EFAULT;
1156
1157 ret = 0;
1158 break;
1159 }
1160 case VFIO_GROUP_SET_CONTAINER:
1161 {
1162 int fd;
1163
1164 if (get_user(fd, (int __user *)arg))
1165 return -EFAULT;
1166
1167 if (fd < 0)
1168 return -EINVAL;
1169
1170 ret = vfio_group_set_container(group, fd);
1171 break;
1172 }
1173 case VFIO_GROUP_UNSET_CONTAINER:
1174 ret = vfio_group_unset_container(group);
1175 break;
1176 case VFIO_GROUP_GET_DEVICE_FD:
1177 {
1178 char *buf;
1179
1180 buf = strndup_user((const char __user *)arg, PAGE_SIZE);
1181 if (IS_ERR(buf))
1182 return PTR_ERR(buf);
1183
1184 ret = vfio_group_get_device_fd(group, buf);
1185 kfree(buf);
1186 break;
1187 }
1188 }
1189
1190 return ret;
1191}
1192
1193#ifdef CONFIG_COMPAT
1194static long vfio_group_fops_compat_ioctl(struct file *filep,
1195 unsigned int cmd, unsigned long arg)
1196{
1197 arg = (unsigned long)compat_ptr(arg);
1198 return vfio_group_fops_unl_ioctl(filep, cmd, arg);
1199}
1200#endif /* CONFIG_COMPAT */
1201
1202static int vfio_group_fops_open(struct inode *inode, struct file *filep)
1203{
1204 struct vfio_group *group;
1205
1206 group = vfio_group_get_from_minor(iminor(inode));
1207 if (!group)
1208 return -ENODEV;
1209
1210 if (group->container) {
1211 vfio_group_put(group);
1212 return -EBUSY;
1213 }
1214
1215 filep->private_data = group;
1216
1217 return 0;
1218}
1219
1220static int vfio_group_fops_release(struct inode *inode, struct file *filep)
1221{
1222 struct vfio_group *group = filep->private_data;
1223
1224 filep->private_data = NULL;
1225
1226 vfio_group_try_dissolve_container(group);
1227
1228 vfio_group_put(group);
1229
1230 return 0;
1231}
1232
1233static const struct file_operations vfio_group_fops = {
1234 .owner = THIS_MODULE,
1235 .unlocked_ioctl = vfio_group_fops_unl_ioctl,
1236#ifdef CONFIG_COMPAT
1237 .compat_ioctl = vfio_group_fops_compat_ioctl,
1238#endif
1239 .open = vfio_group_fops_open,
1240 .release = vfio_group_fops_release,
1241};
1242
1243/**
1244 * VFIO Device fd
1245 */
1246static int vfio_device_fops_release(struct inode *inode, struct file *filep)
1247{
1248 struct vfio_device *device = filep->private_data;
1249
1250 device->ops->release(device->device_data);
1251
1252 vfio_group_try_dissolve_container(device->group);
1253
1254 vfio_device_put(device);
1255
1256 return 0;
1257}
1258
1259static long vfio_device_fops_unl_ioctl(struct file *filep,
1260 unsigned int cmd, unsigned long arg)
1261{
1262 struct vfio_device *device = filep->private_data;
1263
1264 if (unlikely(!device->ops->ioctl))
1265 return -EINVAL;
1266
1267 return device->ops->ioctl(device->device_data, cmd, arg);
1268}
1269
1270static ssize_t vfio_device_fops_read(struct file *filep, char __user *buf,
1271 size_t count, loff_t *ppos)
1272{
1273 struct vfio_device *device = filep->private_data;
1274
1275 if (unlikely(!device->ops->read))
1276 return -EINVAL;
1277
1278 return device->ops->read(device->device_data, buf, count, ppos);
1279}
1280
1281static ssize_t vfio_device_fops_write(struct file *filep,
1282 const char __user *buf,
1283 size_t count, loff_t *ppos)
1284{
1285 struct vfio_device *device = filep->private_data;
1286
1287 if (unlikely(!device->ops->write))
1288 return -EINVAL;
1289
1290 return device->ops->write(device->device_data, buf, count, ppos);
1291}
1292
1293static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma)
1294{
1295 struct vfio_device *device = filep->private_data;
1296
1297 if (unlikely(!device->ops->mmap))
1298 return -EINVAL;
1299
1300 return device->ops->mmap(device->device_data, vma);
1301}
1302
1303#ifdef CONFIG_COMPAT
1304static long vfio_device_fops_compat_ioctl(struct file *filep,
1305 unsigned int cmd, unsigned long arg)
1306{
1307 arg = (unsigned long)compat_ptr(arg);
1308 return vfio_device_fops_unl_ioctl(filep, cmd, arg);
1309}
1310#endif /* CONFIG_COMPAT */
1311
1312static const struct file_operations vfio_device_fops = {
1313 .owner = THIS_MODULE,
1314 .release = vfio_device_fops_release,
1315 .read = vfio_device_fops_read,
1316 .write = vfio_device_fops_write,
1317 .unlocked_ioctl = vfio_device_fops_unl_ioctl,
1318#ifdef CONFIG_COMPAT
1319 .compat_ioctl = vfio_device_fops_compat_ioctl,
1320#endif
1321 .mmap = vfio_device_fops_mmap,
1322};
1323
1324/**
1325 * Module/class support
1326 */
1327static char *vfio_devnode(struct device *dev, umode_t *mode)
1328{
1329 return kasprintf(GFP_KERNEL, "vfio/%s", dev_name(dev));
1330}
1331
1332static int __init vfio_init(void)
1333{
1334 int ret;
1335
1336 idr_init(&vfio.group_idr);
1337 mutex_init(&vfio.group_lock);
1338 mutex_init(&vfio.iommu_drivers_lock);
1339 INIT_LIST_HEAD(&vfio.group_list);
1340 INIT_LIST_HEAD(&vfio.iommu_drivers_list);
1341 init_waitqueue_head(&vfio.release_q);
1342
1343 vfio.class = class_create(THIS_MODULE, "vfio");
1344 if (IS_ERR(vfio.class)) {
1345 ret = PTR_ERR(vfio.class);
1346 goto err_class;
1347 }
1348
1349 vfio.class->devnode = vfio_devnode;
1350
1351 ret = alloc_chrdev_region(&vfio.devt, 0, MINORMASK, "vfio");
1352 if (ret)
1353 goto err_base_chrdev;
1354
1355 cdev_init(&vfio.cdev, &vfio_fops);
1356 ret = cdev_add(&vfio.cdev, vfio.devt, 1);
1357 if (ret)
1358 goto err_base_cdev;
1359
1360 vfio.dev = device_create(vfio.class, NULL, vfio.devt, NULL, "vfio");
1361 if (IS_ERR(vfio.dev)) {
1362 ret = PTR_ERR(vfio.dev);
1363 goto err_base_dev;
1364 }
1365
1366 /* /dev/vfio/$GROUP */
1367 cdev_init(&vfio.group_cdev, &vfio_group_fops);
1368 ret = cdev_add(&vfio.group_cdev,
1369 MKDEV(MAJOR(vfio.devt), 1), MINORMASK - 1);
1370 if (ret)
1371 goto err_groups_cdev;
1372
1373 pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
1374
Alex Williamson73fa0d12012-07-31 08:16:23 -06001375 /*
1376 * Attempt to load known iommu-drivers. This gives us a working
1377 * environment without the user needing to explicitly load iommu
1378 * drivers.
1379 */
1380 request_module_nowait("vfio_iommu_type1");
1381
Alex Williamsoncba33452012-07-31 08:16:22 -06001382 return 0;
1383
1384err_groups_cdev:
1385 device_destroy(vfio.class, vfio.devt);
1386err_base_dev:
1387 cdev_del(&vfio.cdev);
1388err_base_cdev:
1389 unregister_chrdev_region(vfio.devt, MINORMASK);
1390err_base_chrdev:
1391 class_destroy(vfio.class);
1392 vfio.class = NULL;
1393err_class:
1394 return ret;
1395}
1396
1397static void __exit vfio_cleanup(void)
1398{
1399 WARN_ON(!list_empty(&vfio.group_list));
1400
1401 idr_destroy(&vfio.group_idr);
1402 cdev_del(&vfio.group_cdev);
1403 device_destroy(vfio.class, vfio.devt);
1404 cdev_del(&vfio.cdev);
1405 unregister_chrdev_region(vfio.devt, MINORMASK);
1406 class_destroy(vfio.class);
1407 vfio.class = NULL;
1408}
1409
1410module_init(vfio_init);
1411module_exit(vfio_cleanup);
1412
1413MODULE_VERSION(DRIVER_VERSION);
1414MODULE_LICENSE("GPL v2");
1415MODULE_AUTHOR(DRIVER_AUTHOR);
1416MODULE_DESCRIPTION(DRIVER_DESC);