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