ACPI: fix acpi_debugfs_init prototype
[cascardo/linux.git] / drivers / vfio / vfio.c
1 /*
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/miscdevice.h>
26 #include <linux/module.h>
27 #include <linux/mutex.h>
28 #include <linux/rwsem.h>
29 #include <linux/sched.h>
30 #include <linux/slab.h>
31 #include <linux/stat.h>
32 #include <linux/string.h>
33 #include <linux/uaccess.h>
34 #include <linux/vfio.h>
35 #include <linux/wait.h>
36
37 #define DRIVER_VERSION  "0.3"
38 #define DRIVER_AUTHOR   "Alex Williamson <alex.williamson@redhat.com>"
39 #define DRIVER_DESC     "VFIO - User Level meta-driver"
40
41 static struct vfio {
42         struct class                    *class;
43         struct list_head                iommu_drivers_list;
44         struct mutex                    iommu_drivers_lock;
45         struct list_head                group_list;
46         struct idr                      group_idr;
47         struct mutex                    group_lock;
48         struct cdev                     group_cdev;
49         dev_t                           group_devt;
50         wait_queue_head_t               release_q;
51 } vfio;
52
53 struct vfio_iommu_driver {
54         const struct vfio_iommu_driver_ops      *ops;
55         struct list_head                        vfio_next;
56 };
57
58 struct vfio_container {
59         struct kref                     kref;
60         struct list_head                group_list;
61         struct rw_semaphore             group_lock;
62         struct vfio_iommu_driver        *iommu_driver;
63         void                            *iommu_data;
64 };
65
66 struct vfio_unbound_dev {
67         struct device                   *dev;
68         struct list_head                unbound_next;
69 };
70
71 struct vfio_group {
72         struct kref                     kref;
73         int                             minor;
74         atomic_t                        container_users;
75         struct iommu_group              *iommu_group;
76         struct vfio_container           *container;
77         struct list_head                device_list;
78         struct mutex                    device_lock;
79         struct device                   *dev;
80         struct notifier_block           nb;
81         struct list_head                vfio_next;
82         struct list_head                container_next;
83         struct list_head                unbound_list;
84         struct mutex                    unbound_lock;
85         atomic_t                        opened;
86 };
87
88 struct vfio_device {
89         struct kref                     kref;
90         struct device                   *dev;
91         const struct vfio_device_ops    *ops;
92         struct vfio_group               *group;
93         struct list_head                group_next;
94         void                            *device_data;
95 };
96
97 /**
98  * IOMMU driver registration
99  */
100 int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops)
101 {
102         struct vfio_iommu_driver *driver, *tmp;
103
104         driver = kzalloc(sizeof(*driver), GFP_KERNEL);
105         if (!driver)
106                 return -ENOMEM;
107
108         driver->ops = ops;
109
110         mutex_lock(&vfio.iommu_drivers_lock);
111
112         /* Check for duplicates */
113         list_for_each_entry(tmp, &vfio.iommu_drivers_list, vfio_next) {
114                 if (tmp->ops == ops) {
115                         mutex_unlock(&vfio.iommu_drivers_lock);
116                         kfree(driver);
117                         return -EINVAL;
118                 }
119         }
120
121         list_add(&driver->vfio_next, &vfio.iommu_drivers_list);
122
123         mutex_unlock(&vfio.iommu_drivers_lock);
124
125         return 0;
126 }
127 EXPORT_SYMBOL_GPL(vfio_register_iommu_driver);
128
129 void vfio_unregister_iommu_driver(const struct vfio_iommu_driver_ops *ops)
130 {
131         struct vfio_iommu_driver *driver;
132
133         mutex_lock(&vfio.iommu_drivers_lock);
134         list_for_each_entry(driver, &vfio.iommu_drivers_list, vfio_next) {
135                 if (driver->ops == ops) {
136                         list_del(&driver->vfio_next);
137                         mutex_unlock(&vfio.iommu_drivers_lock);
138                         kfree(driver);
139                         return;
140                 }
141         }
142         mutex_unlock(&vfio.iommu_drivers_lock);
143 }
144 EXPORT_SYMBOL_GPL(vfio_unregister_iommu_driver);
145
146 /**
147  * Group minor allocation/free - both called with vfio.group_lock held
148  */
149 static int vfio_alloc_group_minor(struct vfio_group *group)
150 {
151         return idr_alloc(&vfio.group_idr, group, 0, MINORMASK + 1, GFP_KERNEL);
152 }
153
154 static void vfio_free_group_minor(int minor)
155 {
156         idr_remove(&vfio.group_idr, minor);
157 }
158
159 static int vfio_iommu_group_notifier(struct notifier_block *nb,
160                                      unsigned long action, void *data);
161 static void vfio_group_get(struct vfio_group *group);
162
163 /**
164  * Container objects - containers are created when /dev/vfio/vfio is
165  * opened, but their lifecycle extends until the last user is done, so
166  * it's freed via kref.  Must support container/group/device being
167  * closed in any order.
168  */
169 static void vfio_container_get(struct vfio_container *container)
170 {
171         kref_get(&container->kref);
172 }
173
174 static void vfio_container_release(struct kref *kref)
175 {
176         struct vfio_container *container;
177         container = container_of(kref, struct vfio_container, kref);
178
179         kfree(container);
180 }
181
182 static void vfio_container_put(struct vfio_container *container)
183 {
184         kref_put(&container->kref, vfio_container_release);
185 }
186
187 static void vfio_group_unlock_and_free(struct vfio_group *group)
188 {
189         mutex_unlock(&vfio.group_lock);
190         /*
191          * Unregister outside of lock.  A spurious callback is harmless now
192          * that the group is no longer in vfio.group_list.
193          */
194         iommu_group_unregister_notifier(group->iommu_group, &group->nb);
195         kfree(group);
196 }
197
198 /**
199  * Group objects - create, release, get, put, search
200  */
201 static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group)
202 {
203         struct vfio_group *group, *tmp;
204         struct device *dev;
205         int ret, minor;
206
207         group = kzalloc(sizeof(*group), GFP_KERNEL);
208         if (!group)
209                 return ERR_PTR(-ENOMEM);
210
211         kref_init(&group->kref);
212         INIT_LIST_HEAD(&group->device_list);
213         mutex_init(&group->device_lock);
214         INIT_LIST_HEAD(&group->unbound_list);
215         mutex_init(&group->unbound_lock);
216         atomic_set(&group->container_users, 0);
217         atomic_set(&group->opened, 0);
218         group->iommu_group = iommu_group;
219
220         group->nb.notifier_call = vfio_iommu_group_notifier;
221
222         /*
223          * blocking notifiers acquire a rwsem around registering and hold
224          * it around callback.  Therefore, need to register outside of
225          * vfio.group_lock to avoid A-B/B-A contention.  Our callback won't
226          * do anything unless it can find the group in vfio.group_list, so
227          * no harm in registering early.
228          */
229         ret = iommu_group_register_notifier(iommu_group, &group->nb);
230         if (ret) {
231                 kfree(group);
232                 return ERR_PTR(ret);
233         }
234
235         mutex_lock(&vfio.group_lock);
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_group_unlock_and_free(group);
242                         return tmp;
243                 }
244         }
245
246         minor = vfio_alloc_group_minor(group);
247         if (minor < 0) {
248                 vfio_group_unlock_and_free(group);
249                 return ERR_PTR(minor);
250         }
251
252         dev = device_create(vfio.class, NULL,
253                             MKDEV(MAJOR(vfio.group_devt), minor),
254                             group, "%d", iommu_group_id(iommu_group));
255         if (IS_ERR(dev)) {
256                 vfio_free_group_minor(minor);
257                 vfio_group_unlock_and_free(group);
258                 return (struct vfio_group *)dev; /* ERR_PTR */
259         }
260
261         group->minor = minor;
262         group->dev = dev;
263
264         list_add(&group->vfio_next, &vfio.group_list);
265
266         mutex_unlock(&vfio.group_lock);
267
268         return group;
269 }
270
271 /* called with vfio.group_lock held */
272 static void vfio_group_release(struct kref *kref)
273 {
274         struct vfio_group *group = container_of(kref, struct vfio_group, kref);
275         struct vfio_unbound_dev *unbound, *tmp;
276         struct iommu_group *iommu_group = group->iommu_group;
277
278         WARN_ON(!list_empty(&group->device_list));
279
280         list_for_each_entry_safe(unbound, tmp,
281                                  &group->unbound_list, unbound_next) {
282                 list_del(&unbound->unbound_next);
283                 kfree(unbound);
284         }
285
286         device_destroy(vfio.class, MKDEV(MAJOR(vfio.group_devt), group->minor));
287         list_del(&group->vfio_next);
288         vfio_free_group_minor(group->minor);
289         vfio_group_unlock_and_free(group);
290         iommu_group_put(iommu_group);
291 }
292
293 static void vfio_group_put(struct vfio_group *group)
294 {
295         kref_put_mutex(&group->kref, vfio_group_release, &vfio.group_lock);
296 }
297
298 /* Assume group_lock or group reference is held */
299 static void vfio_group_get(struct vfio_group *group)
300 {
301         kref_get(&group->kref);
302 }
303
304 /*
305  * Not really a try as we will sleep for mutex, but we need to make
306  * sure the group pointer is valid under lock and get a reference.
307  */
308 static struct vfio_group *vfio_group_try_get(struct vfio_group *group)
309 {
310         struct vfio_group *target = group;
311
312         mutex_lock(&vfio.group_lock);
313         list_for_each_entry(group, &vfio.group_list, vfio_next) {
314                 if (group == target) {
315                         vfio_group_get(group);
316                         mutex_unlock(&vfio.group_lock);
317                         return group;
318                 }
319         }
320         mutex_unlock(&vfio.group_lock);
321
322         return NULL;
323 }
324
325 static
326 struct vfio_group *vfio_group_get_from_iommu(struct iommu_group *iommu_group)
327 {
328         struct vfio_group *group;
329
330         mutex_lock(&vfio.group_lock);
331         list_for_each_entry(group, &vfio.group_list, vfio_next) {
332                 if (group->iommu_group == iommu_group) {
333                         vfio_group_get(group);
334                         mutex_unlock(&vfio.group_lock);
335                         return group;
336                 }
337         }
338         mutex_unlock(&vfio.group_lock);
339
340         return NULL;
341 }
342
343 static struct vfio_group *vfio_group_get_from_minor(int minor)
344 {
345         struct vfio_group *group;
346
347         mutex_lock(&vfio.group_lock);
348         group = idr_find(&vfio.group_idr, minor);
349         if (!group) {
350                 mutex_unlock(&vfio.group_lock);
351                 return NULL;
352         }
353         vfio_group_get(group);
354         mutex_unlock(&vfio.group_lock);
355
356         return group;
357 }
358
359 /**
360  * Device objects - create, release, get, put, search
361  */
362 static
363 struct vfio_device *vfio_group_create_device(struct vfio_group *group,
364                                              struct device *dev,
365                                              const struct vfio_device_ops *ops,
366                                              void *device_data)
367 {
368         struct vfio_device *device;
369
370         device = kzalloc(sizeof(*device), GFP_KERNEL);
371         if (!device)
372                 return ERR_PTR(-ENOMEM);
373
374         kref_init(&device->kref);
375         device->dev = dev;
376         device->group = group;
377         device->ops = ops;
378         device->device_data = device_data;
379         dev_set_drvdata(dev, device);
380
381         /* No need to get group_lock, caller has group reference */
382         vfio_group_get(group);
383
384         mutex_lock(&group->device_lock);
385         list_add(&device->group_next, &group->device_list);
386         mutex_unlock(&group->device_lock);
387
388         return device;
389 }
390
391 static void vfio_device_release(struct kref *kref)
392 {
393         struct vfio_device *device = container_of(kref,
394                                                   struct vfio_device, kref);
395         struct vfio_group *group = device->group;
396
397         list_del(&device->group_next);
398         mutex_unlock(&group->device_lock);
399
400         dev_set_drvdata(device->dev, NULL);
401
402         kfree(device);
403
404         /* vfio_del_group_dev may be waiting for this device */
405         wake_up(&vfio.release_q);
406 }
407
408 /* Device reference always implies a group reference */
409 void vfio_device_put(struct vfio_device *device)
410 {
411         struct vfio_group *group = device->group;
412         kref_put_mutex(&device->kref, vfio_device_release, &group->device_lock);
413         vfio_group_put(group);
414 }
415 EXPORT_SYMBOL_GPL(vfio_device_put);
416
417 static void vfio_device_get(struct vfio_device *device)
418 {
419         vfio_group_get(device->group);
420         kref_get(&device->kref);
421 }
422
423 static 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  */
445 static const char * const vfio_driver_whitelist[] = { "pci-stub", "pcieport" };
446
447 static 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 in
461  * one of the following states:
462  *  - driver-less
463  *  - bound to a vfio driver
464  *  - bound to a whitelisted driver
465  *
466  * We use two methods to determine whether a device is bound to a vfio
467  * driver.  The first is to test whether the device exists in the vfio
468  * group.  The second is to test if the device exists on the group
469  * unbound_list, indicating it's in the middle of transitioning from
470  * a vfio driver to driver-less.
471  */
472 static int vfio_dev_viable(struct device *dev, void *data)
473 {
474         struct vfio_group *group = data;
475         struct vfio_device *device;
476         struct device_driver *drv = ACCESS_ONCE(dev->driver);
477         struct vfio_unbound_dev *unbound;
478         int ret = -EINVAL;
479
480         mutex_lock(&group->unbound_lock);
481         list_for_each_entry(unbound, &group->unbound_list, unbound_next) {
482                 if (dev == unbound->dev) {
483                         ret = 0;
484                         break;
485                 }
486         }
487         mutex_unlock(&group->unbound_lock);
488
489         if (!ret || !drv || vfio_whitelisted_driver(drv))
490                 return 0;
491
492         device = vfio_group_get_device(group, dev);
493         if (device) {
494                 vfio_device_put(device);
495                 return 0;
496         }
497
498         return ret;
499 }
500
501 /**
502  * Async device support
503  */
504 static int vfio_group_nb_add_dev(struct vfio_group *group, struct device *dev)
505 {
506         struct vfio_device *device;
507
508         /* Do we already know about it?  We shouldn't */
509         device = vfio_group_get_device(group, dev);
510         if (WARN_ON_ONCE(device)) {
511                 vfio_device_put(device);
512                 return 0;
513         }
514
515         /* Nothing to do for idle groups */
516         if (!atomic_read(&group->container_users))
517                 return 0;
518
519         /* TODO Prevent device auto probing */
520         WARN("Device %s added to live group %d!\n", dev_name(dev),
521              iommu_group_id(group->iommu_group));
522
523         return 0;
524 }
525
526 static int vfio_group_nb_verify(struct vfio_group *group, struct device *dev)
527 {
528         /* We don't care what happens when the group isn't in use */
529         if (!atomic_read(&group->container_users))
530                 return 0;
531
532         return vfio_dev_viable(dev, group);
533 }
534
535 static int vfio_iommu_group_notifier(struct notifier_block *nb,
536                                      unsigned long action, void *data)
537 {
538         struct vfio_group *group = container_of(nb, struct vfio_group, nb);
539         struct device *dev = data;
540         struct vfio_unbound_dev *unbound;
541
542         /*
543          * Need to go through a group_lock lookup to get a reference or we
544          * risk racing a group being removed.  Ignore spurious notifies.
545          */
546         group = vfio_group_try_get(group);
547         if (!group)
548                 return NOTIFY_OK;
549
550         switch (action) {
551         case IOMMU_GROUP_NOTIFY_ADD_DEVICE:
552                 vfio_group_nb_add_dev(group, dev);
553                 break;
554         case IOMMU_GROUP_NOTIFY_DEL_DEVICE:
555                 /*
556                  * Nothing to do here.  If the device is in use, then the
557                  * vfio sub-driver should block the remove callback until
558                  * it is unused.  If the device is unused or attached to a
559                  * stub driver, then it should be released and we don't
560                  * care that it will be going away.
561                  */
562                 break;
563         case IOMMU_GROUP_NOTIFY_BIND_DRIVER:
564                 pr_debug("%s: Device %s, group %d binding to driver\n",
565                          __func__, dev_name(dev),
566                          iommu_group_id(group->iommu_group));
567                 break;
568         case IOMMU_GROUP_NOTIFY_BOUND_DRIVER:
569                 pr_debug("%s: Device %s, group %d bound to driver %s\n",
570                          __func__, dev_name(dev),
571                          iommu_group_id(group->iommu_group), dev->driver->name);
572                 BUG_ON(vfio_group_nb_verify(group, dev));
573                 break;
574         case IOMMU_GROUP_NOTIFY_UNBIND_DRIVER:
575                 pr_debug("%s: Device %s, group %d unbinding from driver %s\n",
576                          __func__, dev_name(dev),
577                          iommu_group_id(group->iommu_group), dev->driver->name);
578                 break;
579         case IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER:
580                 pr_debug("%s: Device %s, group %d unbound from driver\n",
581                          __func__, dev_name(dev),
582                          iommu_group_id(group->iommu_group));
583                 /*
584                  * XXX An unbound device in a live group is ok, but we'd
585                  * really like to avoid the above BUG_ON by preventing other
586                  * drivers from binding to it.  Once that occurs, we have to
587                  * stop the system to maintain isolation.  At a minimum, we'd
588                  * want a toggle to disable driver auto probe for this device.
589                  */
590
591                 mutex_lock(&group->unbound_lock);
592                 list_for_each_entry(unbound,
593                                     &group->unbound_list, unbound_next) {
594                         if (dev == unbound->dev) {
595                                 list_del(&unbound->unbound_next);
596                                 kfree(unbound);
597                                 break;
598                         }
599                 }
600                 mutex_unlock(&group->unbound_lock);
601                 break;
602         }
603
604         vfio_group_put(group);
605         return NOTIFY_OK;
606 }
607
608 /**
609  * VFIO driver API
610  */
611 int vfio_add_group_dev(struct device *dev,
612                        const struct vfio_device_ops *ops, void *device_data)
613 {
614         struct iommu_group *iommu_group;
615         struct vfio_group *group;
616         struct vfio_device *device;
617
618         iommu_group = iommu_group_get(dev);
619         if (!iommu_group)
620                 return -EINVAL;
621
622         group = vfio_group_get_from_iommu(iommu_group);
623         if (!group) {
624                 group = vfio_create_group(iommu_group);
625                 if (IS_ERR(group)) {
626                         iommu_group_put(iommu_group);
627                         return PTR_ERR(group);
628                 }
629         } else {
630                 /*
631                  * A found vfio_group already holds a reference to the
632                  * iommu_group.  A created vfio_group keeps the reference.
633                  */
634                 iommu_group_put(iommu_group);
635         }
636
637         device = vfio_group_get_device(group, dev);
638         if (device) {
639                 WARN(1, "Device %s already exists on group %d\n",
640                      dev_name(dev), iommu_group_id(iommu_group));
641                 vfio_device_put(device);
642                 vfio_group_put(group);
643                 return -EBUSY;
644         }
645
646         device = vfio_group_create_device(group, dev, ops, device_data);
647         if (IS_ERR(device)) {
648                 vfio_group_put(group);
649                 return PTR_ERR(device);
650         }
651
652         /*
653          * Drop all but the vfio_device reference.  The vfio_device holds
654          * a reference to the vfio_group, which holds a reference to the
655          * iommu_group.
656          */
657         vfio_group_put(group);
658
659         return 0;
660 }
661 EXPORT_SYMBOL_GPL(vfio_add_group_dev);
662
663 /**
664  * Get a reference to the vfio_device for a device.  Even if the
665  * caller thinks they own the device, they could be racing with a
666  * release call path, so we can't trust drvdata for the shortcut.
667  * Go the long way around, from the iommu_group to the vfio_group
668  * to the vfio_device.
669  */
670 struct vfio_device *vfio_device_get_from_dev(struct device *dev)
671 {
672         struct iommu_group *iommu_group;
673         struct vfio_group *group;
674         struct vfio_device *device;
675
676         iommu_group = iommu_group_get(dev);
677         if (!iommu_group)
678                 return NULL;
679
680         group = vfio_group_get_from_iommu(iommu_group);
681         iommu_group_put(iommu_group);
682         if (!group)
683                 return NULL;
684
685         device = vfio_group_get_device(group, dev);
686         vfio_group_put(group);
687
688         return device;
689 }
690 EXPORT_SYMBOL_GPL(vfio_device_get_from_dev);
691
692 /*
693  * Caller must hold a reference to the vfio_device
694  */
695 void *vfio_device_data(struct vfio_device *device)
696 {
697         return device->device_data;
698 }
699 EXPORT_SYMBOL_GPL(vfio_device_data);
700
701 /* Given a referenced group, check if it contains the device */
702 static bool vfio_dev_present(struct vfio_group *group, struct device *dev)
703 {
704         struct vfio_device *device;
705
706         device = vfio_group_get_device(group, dev);
707         if (!device)
708                 return false;
709
710         vfio_device_put(device);
711         return true;
712 }
713
714 /*
715  * Decrement the device reference count and wait for the device to be
716  * removed.  Open file descriptors for the device... */
717 void *vfio_del_group_dev(struct device *dev)
718 {
719         struct vfio_device *device = dev_get_drvdata(dev);
720         struct vfio_group *group = device->group;
721         void *device_data = device->device_data;
722         struct vfio_unbound_dev *unbound;
723         unsigned int i = 0;
724         long ret;
725         bool interrupted = false;
726
727         /*
728          * The group exists so long as we have a device reference.  Get
729          * a group reference and use it to scan for the device going away.
730          */
731         vfio_group_get(group);
732
733         /*
734          * When the device is removed from the group, the group suddenly
735          * becomes non-viable; the device has a driver (until the unbind
736          * completes), but it's not present in the group.  This is bad news
737          * for any external users that need to re-acquire a group reference
738          * in order to match and release their existing reference.  To
739          * solve this, we track such devices on the unbound_list to bridge
740          * the gap until they're fully unbound.
741          */
742         unbound = kzalloc(sizeof(*unbound), GFP_KERNEL);
743         if (unbound) {
744                 unbound->dev = dev;
745                 mutex_lock(&group->unbound_lock);
746                 list_add(&unbound->unbound_next, &group->unbound_list);
747                 mutex_unlock(&group->unbound_lock);
748         }
749         WARN_ON(!unbound);
750
751         vfio_device_put(device);
752
753         /*
754          * If the device is still present in the group after the above
755          * 'put', then it is in use and we need to request it from the
756          * bus driver.  The driver may in turn need to request the
757          * device from the user.  We send the request on an arbitrary
758          * interval with counter to allow the driver to take escalating
759          * measures to release the device if it has the ability to do so.
760          */
761         do {
762                 device = vfio_group_get_device(group, dev);
763                 if (!device)
764                         break;
765
766                 if (device->ops->request)
767                         device->ops->request(device_data, i++);
768
769                 vfio_device_put(device);
770
771                 if (interrupted) {
772                         ret = wait_event_timeout(vfio.release_q,
773                                         !vfio_dev_present(group, dev), HZ * 10);
774                 } else {
775                         ret = wait_event_interruptible_timeout(vfio.release_q,
776                                         !vfio_dev_present(group, dev), HZ * 10);
777                         if (ret == -ERESTARTSYS) {
778                                 interrupted = true;
779                                 dev_warn(dev,
780                                          "Device is currently in use, task"
781                                          " \"%s\" (%d) "
782                                          "blocked until device is released",
783                                          current->comm, task_pid_nr(current));
784                         }
785                 }
786         } while (ret <= 0);
787
788         vfio_group_put(group);
789
790         return device_data;
791 }
792 EXPORT_SYMBOL_GPL(vfio_del_group_dev);
793
794 /**
795  * VFIO base fd, /dev/vfio/vfio
796  */
797 static long vfio_ioctl_check_extension(struct vfio_container *container,
798                                        unsigned long arg)
799 {
800         struct vfio_iommu_driver *driver;
801         long ret = 0;
802
803         down_read(&container->group_lock);
804
805         driver = container->iommu_driver;
806
807         switch (arg) {
808                 /* No base extensions yet */
809         default:
810                 /*
811                  * If no driver is set, poll all registered drivers for
812                  * extensions and return the first positive result.  If
813                  * a driver is already set, further queries will be passed
814                  * only to that driver.
815                  */
816                 if (!driver) {
817                         mutex_lock(&vfio.iommu_drivers_lock);
818                         list_for_each_entry(driver, &vfio.iommu_drivers_list,
819                                             vfio_next) {
820                                 if (!try_module_get(driver->ops->owner))
821                                         continue;
822
823                                 ret = driver->ops->ioctl(NULL,
824                                                          VFIO_CHECK_EXTENSION,
825                                                          arg);
826                                 module_put(driver->ops->owner);
827                                 if (ret > 0)
828                                         break;
829                         }
830                         mutex_unlock(&vfio.iommu_drivers_lock);
831                 } else
832                         ret = driver->ops->ioctl(container->iommu_data,
833                                                  VFIO_CHECK_EXTENSION, arg);
834         }
835
836         up_read(&container->group_lock);
837
838         return ret;
839 }
840
841 /* hold write lock on container->group_lock */
842 static int __vfio_container_attach_groups(struct vfio_container *container,
843                                           struct vfio_iommu_driver *driver,
844                                           void *data)
845 {
846         struct vfio_group *group;
847         int ret = -ENODEV;
848
849         list_for_each_entry(group, &container->group_list, container_next) {
850                 ret = driver->ops->attach_group(data, group->iommu_group);
851                 if (ret)
852                         goto unwind;
853         }
854
855         return ret;
856
857 unwind:
858         list_for_each_entry_continue_reverse(group, &container->group_list,
859                                              container_next) {
860                 driver->ops->detach_group(data, group->iommu_group);
861         }
862
863         return ret;
864 }
865
866 static long vfio_ioctl_set_iommu(struct vfio_container *container,
867                                  unsigned long arg)
868 {
869         struct vfio_iommu_driver *driver;
870         long ret = -ENODEV;
871
872         down_write(&container->group_lock);
873
874         /*
875          * The container is designed to be an unprivileged interface while
876          * the group can be assigned to specific users.  Therefore, only by
877          * adding a group to a container does the user get the privilege of
878          * enabling the iommu, which may allocate finite resources.  There
879          * is no unset_iommu, but by removing all the groups from a container,
880          * the container is deprivileged and returns to an unset state.
881          */
882         if (list_empty(&container->group_list) || container->iommu_driver) {
883                 up_write(&container->group_lock);
884                 return -EINVAL;
885         }
886
887         mutex_lock(&vfio.iommu_drivers_lock);
888         list_for_each_entry(driver, &vfio.iommu_drivers_list, vfio_next) {
889                 void *data;
890
891                 if (!try_module_get(driver->ops->owner))
892                         continue;
893
894                 /*
895                  * The arg magic for SET_IOMMU is the same as CHECK_EXTENSION,
896                  * so test which iommu driver reported support for this
897                  * extension and call open on them.  We also pass them the
898                  * magic, allowing a single driver to support multiple
899                  * interfaces if they'd like.
900                  */
901                 if (driver->ops->ioctl(NULL, VFIO_CHECK_EXTENSION, arg) <= 0) {
902                         module_put(driver->ops->owner);
903                         continue;
904                 }
905
906                 /* module reference holds the driver we're working on */
907                 mutex_unlock(&vfio.iommu_drivers_lock);
908
909                 data = driver->ops->open(arg);
910                 if (IS_ERR(data)) {
911                         ret = PTR_ERR(data);
912                         module_put(driver->ops->owner);
913                         goto skip_drivers_unlock;
914                 }
915
916                 ret = __vfio_container_attach_groups(container, driver, data);
917                 if (!ret) {
918                         container->iommu_driver = driver;
919                         container->iommu_data = data;
920                 } else {
921                         driver->ops->release(data);
922                         module_put(driver->ops->owner);
923                 }
924
925                 goto skip_drivers_unlock;
926         }
927
928         mutex_unlock(&vfio.iommu_drivers_lock);
929 skip_drivers_unlock:
930         up_write(&container->group_lock);
931
932         return ret;
933 }
934
935 static long vfio_fops_unl_ioctl(struct file *filep,
936                                 unsigned int cmd, unsigned long arg)
937 {
938         struct vfio_container *container = filep->private_data;
939         struct vfio_iommu_driver *driver;
940         void *data;
941         long ret = -EINVAL;
942
943         if (!container)
944                 return ret;
945
946         switch (cmd) {
947         case VFIO_GET_API_VERSION:
948                 ret = VFIO_API_VERSION;
949                 break;
950         case VFIO_CHECK_EXTENSION:
951                 ret = vfio_ioctl_check_extension(container, arg);
952                 break;
953         case VFIO_SET_IOMMU:
954                 ret = vfio_ioctl_set_iommu(container, arg);
955                 break;
956         default:
957                 down_read(&container->group_lock);
958
959                 driver = container->iommu_driver;
960                 data = container->iommu_data;
961
962                 if (driver) /* passthrough all unrecognized ioctls */
963                         ret = driver->ops->ioctl(data, cmd, arg);
964
965                 up_read(&container->group_lock);
966         }
967
968         return ret;
969 }
970
971 #ifdef CONFIG_COMPAT
972 static long vfio_fops_compat_ioctl(struct file *filep,
973                                    unsigned int cmd, unsigned long arg)
974 {
975         arg = (unsigned long)compat_ptr(arg);
976         return vfio_fops_unl_ioctl(filep, cmd, arg);
977 }
978 #endif  /* CONFIG_COMPAT */
979
980 static int vfio_fops_open(struct inode *inode, struct file *filep)
981 {
982         struct vfio_container *container;
983
984         container = kzalloc(sizeof(*container), GFP_KERNEL);
985         if (!container)
986                 return -ENOMEM;
987
988         INIT_LIST_HEAD(&container->group_list);
989         init_rwsem(&container->group_lock);
990         kref_init(&container->kref);
991
992         filep->private_data = container;
993
994         return 0;
995 }
996
997 static int vfio_fops_release(struct inode *inode, struct file *filep)
998 {
999         struct vfio_container *container = filep->private_data;
1000
1001         filep->private_data = NULL;
1002
1003         vfio_container_put(container);
1004
1005         return 0;
1006 }
1007
1008 /*
1009  * Once an iommu driver is set, we optionally pass read/write/mmap
1010  * on to the driver, allowing management interfaces beyond ioctl.
1011  */
1012 static ssize_t vfio_fops_read(struct file *filep, char __user *buf,
1013                               size_t count, loff_t *ppos)
1014 {
1015         struct vfio_container *container = filep->private_data;
1016         struct vfio_iommu_driver *driver;
1017         ssize_t ret = -EINVAL;
1018
1019         down_read(&container->group_lock);
1020
1021         driver = container->iommu_driver;
1022         if (likely(driver && driver->ops->read))
1023                 ret = driver->ops->read(container->iommu_data,
1024                                         buf, count, ppos);
1025
1026         up_read(&container->group_lock);
1027
1028         return ret;
1029 }
1030
1031 static ssize_t vfio_fops_write(struct file *filep, const char __user *buf,
1032                                size_t count, loff_t *ppos)
1033 {
1034         struct vfio_container *container = filep->private_data;
1035         struct vfio_iommu_driver *driver;
1036         ssize_t ret = -EINVAL;
1037
1038         down_read(&container->group_lock);
1039
1040         driver = container->iommu_driver;
1041         if (likely(driver && driver->ops->write))
1042                 ret = driver->ops->write(container->iommu_data,
1043                                          buf, count, ppos);
1044
1045         up_read(&container->group_lock);
1046
1047         return ret;
1048 }
1049
1050 static int vfio_fops_mmap(struct file *filep, struct vm_area_struct *vma)
1051 {
1052         struct vfio_container *container = filep->private_data;
1053         struct vfio_iommu_driver *driver;
1054         int ret = -EINVAL;
1055
1056         down_read(&container->group_lock);
1057
1058         driver = container->iommu_driver;
1059         if (likely(driver && driver->ops->mmap))
1060                 ret = driver->ops->mmap(container->iommu_data, vma);
1061
1062         up_read(&container->group_lock);
1063
1064         return ret;
1065 }
1066
1067 static const struct file_operations vfio_fops = {
1068         .owner          = THIS_MODULE,
1069         .open           = vfio_fops_open,
1070         .release        = vfio_fops_release,
1071         .read           = vfio_fops_read,
1072         .write          = vfio_fops_write,
1073         .unlocked_ioctl = vfio_fops_unl_ioctl,
1074 #ifdef CONFIG_COMPAT
1075         .compat_ioctl   = vfio_fops_compat_ioctl,
1076 #endif
1077         .mmap           = vfio_fops_mmap,
1078 };
1079
1080 /**
1081  * VFIO Group fd, /dev/vfio/$GROUP
1082  */
1083 static void __vfio_group_unset_container(struct vfio_group *group)
1084 {
1085         struct vfio_container *container = group->container;
1086         struct vfio_iommu_driver *driver;
1087
1088         down_write(&container->group_lock);
1089
1090         driver = container->iommu_driver;
1091         if (driver)
1092                 driver->ops->detach_group(container->iommu_data,
1093                                           group->iommu_group);
1094
1095         group->container = NULL;
1096         list_del(&group->container_next);
1097
1098         /* Detaching the last group deprivileges a container, remove iommu */
1099         if (driver && list_empty(&container->group_list)) {
1100                 driver->ops->release(container->iommu_data);
1101                 module_put(driver->ops->owner);
1102                 container->iommu_driver = NULL;
1103                 container->iommu_data = NULL;
1104         }
1105
1106         up_write(&container->group_lock);
1107
1108         vfio_container_put(container);
1109 }
1110
1111 /*
1112  * VFIO_GROUP_UNSET_CONTAINER should fail if there are other users or
1113  * if there was no container to unset.  Since the ioctl is called on
1114  * the group, we know that still exists, therefore the only valid
1115  * transition here is 1->0.
1116  */
1117 static int vfio_group_unset_container(struct vfio_group *group)
1118 {
1119         int users = atomic_cmpxchg(&group->container_users, 1, 0);
1120
1121         if (!users)
1122                 return -EINVAL;
1123         if (users != 1)
1124                 return -EBUSY;
1125
1126         __vfio_group_unset_container(group);
1127
1128         return 0;
1129 }
1130
1131 /*
1132  * When removing container users, anything that removes the last user
1133  * implicitly removes the group from the container.  That is, if the
1134  * group file descriptor is closed, as well as any device file descriptors,
1135  * the group is free.
1136  */
1137 static void vfio_group_try_dissolve_container(struct vfio_group *group)
1138 {
1139         if (0 == atomic_dec_if_positive(&group->container_users))
1140                 __vfio_group_unset_container(group);
1141 }
1142
1143 static int vfio_group_set_container(struct vfio_group *group, int container_fd)
1144 {
1145         struct fd f;
1146         struct vfio_container *container;
1147         struct vfio_iommu_driver *driver;
1148         int ret = 0;
1149
1150         if (atomic_read(&group->container_users))
1151                 return -EINVAL;
1152
1153         f = fdget(container_fd);
1154         if (!f.file)
1155                 return -EBADF;
1156
1157         /* Sanity check, is this really our fd? */
1158         if (f.file->f_op != &vfio_fops) {
1159                 fdput(f);
1160                 return -EINVAL;
1161         }
1162
1163         container = f.file->private_data;
1164         WARN_ON(!container); /* fget ensures we don't race vfio_release */
1165
1166         down_write(&container->group_lock);
1167
1168         driver = container->iommu_driver;
1169         if (driver) {
1170                 ret = driver->ops->attach_group(container->iommu_data,
1171                                                 group->iommu_group);
1172                 if (ret)
1173                         goto unlock_out;
1174         }
1175
1176         group->container = container;
1177         list_add(&group->container_next, &container->group_list);
1178
1179         /* Get a reference on the container and mark a user within the group */
1180         vfio_container_get(container);
1181         atomic_inc(&group->container_users);
1182
1183 unlock_out:
1184         up_write(&container->group_lock);
1185         fdput(f);
1186         return ret;
1187 }
1188
1189 static bool vfio_group_viable(struct vfio_group *group)
1190 {
1191         return (iommu_group_for_each_dev(group->iommu_group,
1192                                          group, vfio_dev_viable) == 0);
1193 }
1194
1195 static const struct file_operations vfio_device_fops;
1196
1197 static int vfio_group_get_device_fd(struct vfio_group *group, char *buf)
1198 {
1199         struct vfio_device *device;
1200         struct file *filep;
1201         int ret = -ENODEV;
1202
1203         if (0 == atomic_read(&group->container_users) ||
1204             !group->container->iommu_driver || !vfio_group_viable(group))
1205                 return -EINVAL;
1206
1207         mutex_lock(&group->device_lock);
1208         list_for_each_entry(device, &group->device_list, group_next) {
1209                 if (strcmp(dev_name(device->dev), buf))
1210                         continue;
1211
1212                 ret = device->ops->open(device->device_data);
1213                 if (ret)
1214                         break;
1215                 /*
1216                  * We can't use anon_inode_getfd() because we need to modify
1217                  * the f_mode flags directly to allow more than just ioctls
1218                  */
1219                 ret = get_unused_fd_flags(O_CLOEXEC);
1220                 if (ret < 0) {
1221                         device->ops->release(device->device_data);
1222                         break;
1223                 }
1224
1225                 filep = anon_inode_getfile("[vfio-device]", &vfio_device_fops,
1226                                            device, O_RDWR);
1227                 if (IS_ERR(filep)) {
1228                         put_unused_fd(ret);
1229                         ret = PTR_ERR(filep);
1230                         device->ops->release(device->device_data);
1231                         break;
1232                 }
1233
1234                 /*
1235                  * TODO: add an anon_inode interface to do this.
1236                  * Appears to be missing by lack of need rather than
1237                  * explicitly prevented.  Now there's need.
1238                  */
1239                 filep->f_mode |= (FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1240
1241                 vfio_device_get(device);
1242                 atomic_inc(&group->container_users);
1243
1244                 fd_install(ret, filep);
1245                 break;
1246         }
1247         mutex_unlock(&group->device_lock);
1248
1249         return ret;
1250 }
1251
1252 static long vfio_group_fops_unl_ioctl(struct file *filep,
1253                                       unsigned int cmd, unsigned long arg)
1254 {
1255         struct vfio_group *group = filep->private_data;
1256         long ret = -ENOTTY;
1257
1258         switch (cmd) {
1259         case VFIO_GROUP_GET_STATUS:
1260         {
1261                 struct vfio_group_status status;
1262                 unsigned long minsz;
1263
1264                 minsz = offsetofend(struct vfio_group_status, flags);
1265
1266                 if (copy_from_user(&status, (void __user *)arg, minsz))
1267                         return -EFAULT;
1268
1269                 if (status.argsz < minsz)
1270                         return -EINVAL;
1271
1272                 status.flags = 0;
1273
1274                 if (vfio_group_viable(group))
1275                         status.flags |= VFIO_GROUP_FLAGS_VIABLE;
1276
1277                 if (group->container)
1278                         status.flags |= VFIO_GROUP_FLAGS_CONTAINER_SET;
1279
1280                 if (copy_to_user((void __user *)arg, &status, minsz))
1281                         return -EFAULT;
1282
1283                 ret = 0;
1284                 break;
1285         }
1286         case VFIO_GROUP_SET_CONTAINER:
1287         {
1288                 int fd;
1289
1290                 if (get_user(fd, (int __user *)arg))
1291                         return -EFAULT;
1292
1293                 if (fd < 0)
1294                         return -EINVAL;
1295
1296                 ret = vfio_group_set_container(group, fd);
1297                 break;
1298         }
1299         case VFIO_GROUP_UNSET_CONTAINER:
1300                 ret = vfio_group_unset_container(group);
1301                 break;
1302         case VFIO_GROUP_GET_DEVICE_FD:
1303         {
1304                 char *buf;
1305
1306                 buf = strndup_user((const char __user *)arg, PAGE_SIZE);
1307                 if (IS_ERR(buf))
1308                         return PTR_ERR(buf);
1309
1310                 ret = vfio_group_get_device_fd(group, buf);
1311                 kfree(buf);
1312                 break;
1313         }
1314         }
1315
1316         return ret;
1317 }
1318
1319 #ifdef CONFIG_COMPAT
1320 static long vfio_group_fops_compat_ioctl(struct file *filep,
1321                                          unsigned int cmd, unsigned long arg)
1322 {
1323         arg = (unsigned long)compat_ptr(arg);
1324         return vfio_group_fops_unl_ioctl(filep, cmd, arg);
1325 }
1326 #endif  /* CONFIG_COMPAT */
1327
1328 static int vfio_group_fops_open(struct inode *inode, struct file *filep)
1329 {
1330         struct vfio_group *group;
1331         int opened;
1332
1333         group = vfio_group_get_from_minor(iminor(inode));
1334         if (!group)
1335                 return -ENODEV;
1336
1337         /* Do we need multiple instances of the group open?  Seems not. */
1338         opened = atomic_cmpxchg(&group->opened, 0, 1);
1339         if (opened) {
1340                 vfio_group_put(group);
1341                 return -EBUSY;
1342         }
1343
1344         /* Is something still in use from a previous open? */
1345         if (group->container) {
1346                 atomic_dec(&group->opened);
1347                 vfio_group_put(group);
1348                 return -EBUSY;
1349         }
1350
1351         filep->private_data = group;
1352
1353         return 0;
1354 }
1355
1356 static int vfio_group_fops_release(struct inode *inode, struct file *filep)
1357 {
1358         struct vfio_group *group = filep->private_data;
1359
1360         filep->private_data = NULL;
1361
1362         vfio_group_try_dissolve_container(group);
1363
1364         atomic_dec(&group->opened);
1365
1366         vfio_group_put(group);
1367
1368         return 0;
1369 }
1370
1371 static const struct file_operations vfio_group_fops = {
1372         .owner          = THIS_MODULE,
1373         .unlocked_ioctl = vfio_group_fops_unl_ioctl,
1374 #ifdef CONFIG_COMPAT
1375         .compat_ioctl   = vfio_group_fops_compat_ioctl,
1376 #endif
1377         .open           = vfio_group_fops_open,
1378         .release        = vfio_group_fops_release,
1379 };
1380
1381 /**
1382  * VFIO Device fd
1383  */
1384 static int vfio_device_fops_release(struct inode *inode, struct file *filep)
1385 {
1386         struct vfio_device *device = filep->private_data;
1387
1388         device->ops->release(device->device_data);
1389
1390         vfio_group_try_dissolve_container(device->group);
1391
1392         vfio_device_put(device);
1393
1394         return 0;
1395 }
1396
1397 static long vfio_device_fops_unl_ioctl(struct file *filep,
1398                                        unsigned int cmd, unsigned long arg)
1399 {
1400         struct vfio_device *device = filep->private_data;
1401
1402         if (unlikely(!device->ops->ioctl))
1403                 return -EINVAL;
1404
1405         return device->ops->ioctl(device->device_data, cmd, arg);
1406 }
1407
1408 static ssize_t vfio_device_fops_read(struct file *filep, char __user *buf,
1409                                      size_t count, loff_t *ppos)
1410 {
1411         struct vfio_device *device = filep->private_data;
1412
1413         if (unlikely(!device->ops->read))
1414                 return -EINVAL;
1415
1416         return device->ops->read(device->device_data, buf, count, ppos);
1417 }
1418
1419 static ssize_t vfio_device_fops_write(struct file *filep,
1420                                       const char __user *buf,
1421                                       size_t count, loff_t *ppos)
1422 {
1423         struct vfio_device *device = filep->private_data;
1424
1425         if (unlikely(!device->ops->write))
1426                 return -EINVAL;
1427
1428         return device->ops->write(device->device_data, buf, count, ppos);
1429 }
1430
1431 static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma)
1432 {
1433         struct vfio_device *device = filep->private_data;
1434
1435         if (unlikely(!device->ops->mmap))
1436                 return -EINVAL;
1437
1438         return device->ops->mmap(device->device_data, vma);
1439 }
1440
1441 #ifdef CONFIG_COMPAT
1442 static long vfio_device_fops_compat_ioctl(struct file *filep,
1443                                           unsigned int cmd, unsigned long arg)
1444 {
1445         arg = (unsigned long)compat_ptr(arg);
1446         return vfio_device_fops_unl_ioctl(filep, cmd, arg);
1447 }
1448 #endif  /* CONFIG_COMPAT */
1449
1450 static const struct file_operations vfio_device_fops = {
1451         .owner          = THIS_MODULE,
1452         .release        = vfio_device_fops_release,
1453         .read           = vfio_device_fops_read,
1454         .write          = vfio_device_fops_write,
1455         .unlocked_ioctl = vfio_device_fops_unl_ioctl,
1456 #ifdef CONFIG_COMPAT
1457         .compat_ioctl   = vfio_device_fops_compat_ioctl,
1458 #endif
1459         .mmap           = vfio_device_fops_mmap,
1460 };
1461
1462 /**
1463  * External user API, exported by symbols to be linked dynamically.
1464  *
1465  * The protocol includes:
1466  *  1. do normal VFIO init operation:
1467  *      - opening a new container;
1468  *      - attaching group(s) to it;
1469  *      - setting an IOMMU driver for a container.
1470  * When IOMMU is set for a container, all groups in it are
1471  * considered ready to use by an external user.
1472  *
1473  * 2. User space passes a group fd to an external user.
1474  * The external user calls vfio_group_get_external_user()
1475  * to verify that:
1476  *      - the group is initialized;
1477  *      - IOMMU is set for it.
1478  * If both checks passed, vfio_group_get_external_user()
1479  * increments the container user counter to prevent
1480  * the VFIO group from disposal before KVM exits.
1481  *
1482  * 3. The external user calls vfio_external_user_iommu_id()
1483  * to know an IOMMU ID.
1484  *
1485  * 4. When the external KVM finishes, it calls
1486  * vfio_group_put_external_user() to release the VFIO group.
1487  * This call decrements the container user counter.
1488  */
1489 struct vfio_group *vfio_group_get_external_user(struct file *filep)
1490 {
1491         struct vfio_group *group = filep->private_data;
1492
1493         if (filep->f_op != &vfio_group_fops)
1494                 return ERR_PTR(-EINVAL);
1495
1496         if (!atomic_inc_not_zero(&group->container_users))
1497                 return ERR_PTR(-EINVAL);
1498
1499         if (!group->container->iommu_driver ||
1500                         !vfio_group_viable(group)) {
1501                 atomic_dec(&group->container_users);
1502                 return ERR_PTR(-EINVAL);
1503         }
1504
1505         vfio_group_get(group);
1506
1507         return group;
1508 }
1509 EXPORT_SYMBOL_GPL(vfio_group_get_external_user);
1510
1511 void vfio_group_put_external_user(struct vfio_group *group)
1512 {
1513         vfio_group_put(group);
1514         vfio_group_try_dissolve_container(group);
1515 }
1516 EXPORT_SYMBOL_GPL(vfio_group_put_external_user);
1517
1518 int vfio_external_user_iommu_id(struct vfio_group *group)
1519 {
1520         return iommu_group_id(group->iommu_group);
1521 }
1522 EXPORT_SYMBOL_GPL(vfio_external_user_iommu_id);
1523
1524 long vfio_external_check_extension(struct vfio_group *group, unsigned long arg)
1525 {
1526         return vfio_ioctl_check_extension(group->container, arg);
1527 }
1528 EXPORT_SYMBOL_GPL(vfio_external_check_extension);
1529
1530 /**
1531  * Module/class support
1532  */
1533 static char *vfio_devnode(struct device *dev, umode_t *mode)
1534 {
1535         return kasprintf(GFP_KERNEL, "vfio/%s", dev_name(dev));
1536 }
1537
1538 static struct miscdevice vfio_dev = {
1539         .minor = VFIO_MINOR,
1540         .name = "vfio",
1541         .fops = &vfio_fops,
1542         .nodename = "vfio/vfio",
1543         .mode = S_IRUGO | S_IWUGO,
1544 };
1545
1546 static int __init vfio_init(void)
1547 {
1548         int ret;
1549
1550         idr_init(&vfio.group_idr);
1551         mutex_init(&vfio.group_lock);
1552         mutex_init(&vfio.iommu_drivers_lock);
1553         INIT_LIST_HEAD(&vfio.group_list);
1554         INIT_LIST_HEAD(&vfio.iommu_drivers_list);
1555         init_waitqueue_head(&vfio.release_q);
1556
1557         ret = misc_register(&vfio_dev);
1558         if (ret) {
1559                 pr_err("vfio: misc device register failed\n");
1560                 return ret;
1561         }
1562
1563         /* /dev/vfio/$GROUP */
1564         vfio.class = class_create(THIS_MODULE, "vfio");
1565         if (IS_ERR(vfio.class)) {
1566                 ret = PTR_ERR(vfio.class);
1567                 goto err_class;
1568         }
1569
1570         vfio.class->devnode = vfio_devnode;
1571
1572         ret = alloc_chrdev_region(&vfio.group_devt, 0, MINORMASK, "vfio");
1573         if (ret)
1574                 goto err_alloc_chrdev;
1575
1576         cdev_init(&vfio.group_cdev, &vfio_group_fops);
1577         ret = cdev_add(&vfio.group_cdev, vfio.group_devt, MINORMASK);
1578         if (ret)
1579                 goto err_cdev_add;
1580
1581         pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
1582
1583         /*
1584          * Attempt to load known iommu-drivers.  This gives us a working
1585          * environment without the user needing to explicitly load iommu
1586          * drivers.
1587          */
1588         request_module_nowait("vfio_iommu_type1");
1589         request_module_nowait("vfio_iommu_spapr_tce");
1590
1591         return 0;
1592
1593 err_cdev_add:
1594         unregister_chrdev_region(vfio.group_devt, MINORMASK);
1595 err_alloc_chrdev:
1596         class_destroy(vfio.class);
1597         vfio.class = NULL;
1598 err_class:
1599         misc_deregister(&vfio_dev);
1600         return ret;
1601 }
1602
1603 static void __exit vfio_cleanup(void)
1604 {
1605         WARN_ON(!list_empty(&vfio.group_list));
1606
1607         idr_destroy(&vfio.group_idr);
1608         cdev_del(&vfio.group_cdev);
1609         unregister_chrdev_region(vfio.group_devt, MINORMASK);
1610         class_destroy(vfio.class);
1611         vfio.class = NULL;
1612         misc_deregister(&vfio_dev);
1613 }
1614
1615 module_init(vfio_init);
1616 module_exit(vfio_cleanup);
1617
1618 MODULE_VERSION(DRIVER_VERSION);
1619 MODULE_LICENSE("GPL v2");
1620 MODULE_AUTHOR(DRIVER_AUTHOR);
1621 MODULE_DESCRIPTION(DRIVER_DESC);
1622 MODULE_ALIAS_MISCDEV(VFIO_MINOR);
1623 MODULE_ALIAS("devname:vfio/vfio");