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