vfio: fix noiommu vfio_iommu_group_get reference count
[linux-2.6-block.git] / drivers / vfio / vfio.c
CommitLineData
cba3345c
AW
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>
d1099901 25#include <linux/miscdevice.h>
cba3345c
AW
26#include <linux/module.h>
27#include <linux/mutex.h>
5f096b14 28#include <linux/pci.h>
9587f44a 29#include <linux/rwsem.h>
cba3345c
AW
30#include <linux/sched.h>
31#include <linux/slab.h>
664e9386 32#include <linux/stat.h>
cba3345c
AW
33#include <linux/string.h>
34#include <linux/uaccess.h>
35#include <linux/vfio.h>
36#include <linux/wait.h>
37
38#define DRIVER_VERSION "0.3"
39#define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>"
40#define DRIVER_DESC "VFIO - User Level meta-driver"
41
42static struct vfio {
43 struct class *class;
44 struct list_head iommu_drivers_list;
45 struct mutex iommu_drivers_lock;
46 struct list_head group_list;
47 struct idr group_idr;
48 struct mutex group_lock;
49 struct cdev group_cdev;
d1099901 50 dev_t group_devt;
cba3345c
AW
51 wait_queue_head_t release_q;
52} vfio;
53
54struct vfio_iommu_driver {
55 const struct vfio_iommu_driver_ops *ops;
56 struct list_head vfio_next;
57};
58
59struct vfio_container {
60 struct kref kref;
61 struct list_head group_list;
9587f44a 62 struct rw_semaphore group_lock;
cba3345c
AW
63 struct vfio_iommu_driver *iommu_driver;
64 void *iommu_data;
03a76b60 65 bool noiommu;
cba3345c
AW
66};
67
60720a0f
AW
68struct vfio_unbound_dev {
69 struct device *dev;
70 struct list_head unbound_next;
71};
72
cba3345c
AW
73struct vfio_group {
74 struct kref kref;
75 int minor;
76 atomic_t container_users;
77 struct iommu_group *iommu_group;
78 struct vfio_container *container;
79 struct list_head device_list;
80 struct mutex device_lock;
81 struct device *dev;
82 struct notifier_block nb;
83 struct list_head vfio_next;
84 struct list_head container_next;
60720a0f
AW
85 struct list_head unbound_list;
86 struct mutex unbound_lock;
6d6768c6 87 atomic_t opened;
03a76b60 88 bool noiommu;
ccd46dba
JS
89 struct kvm *kvm;
90 struct blocking_notifier_head notifier;
cba3345c
AW
91};
92
93struct vfio_device {
94 struct kref kref;
95 struct device *dev;
96 const struct vfio_device_ops *ops;
97 struct vfio_group *group;
98 struct list_head group_next;
99 void *device_data;
100};
101
03a76b60
AW
102#ifdef CONFIG_VFIO_NOIOMMU
103static bool noiommu __read_mostly;
104module_param_named(enable_unsafe_noiommu_mode,
105 noiommu, bool, S_IRUGO | S_IWUSR);
106MODULE_PARM_DESC(enable_unsafe_noiommu_mode, "Enable UNSAFE, no-IOMMU mode. This mode provides no device isolation, no DMA translation, no host kernel protection, cannot be used for device assignment to virtual machines, requires RAWIO permissions, and will taint the kernel. If you do not know what this is for, step away. (default: false)");
107#endif
108
109/*
110 * vfio_iommu_group_{get,put} are only intended for VFIO bus driver probe
111 * and remove functions, any use cases other than acquiring the first
112 * reference for the purpose of calling vfio_add_group_dev() or removing
113 * that symmetric reference after vfio_del_group_dev() should use the raw
114 * iommu_group_{get,put} functions. In particular, vfio_iommu_group_put()
115 * removes the device from the dummy group and cannot be nested.
116 */
117struct iommu_group *vfio_iommu_group_get(struct device *dev)
118{
119 struct iommu_group *group;
120 int __maybe_unused ret;
121
122 group = iommu_group_get(dev);
123
124#ifdef CONFIG_VFIO_NOIOMMU
125 /*
126 * With noiommu enabled, an IOMMU group will be created for a device
127 * that doesn't already have one and doesn't have an iommu_ops on their
16ab8a5c
AW
128 * bus. We set iommudata simply to be able to identify these groups
129 * as special use and for reclamation later.
03a76b60
AW
130 */
131 if (group || !noiommu || iommu_present(dev->bus))
132 return group;
133
134 group = iommu_group_alloc();
135 if (IS_ERR(group))
136 return NULL;
137
138 iommu_group_set_name(group, "vfio-noiommu");
16ab8a5c 139 iommu_group_set_iommudata(group, &noiommu, NULL);
03a76b60 140 ret = iommu_group_add_device(group, dev);
d935ad91
EA
141 if (ret) {
142 iommu_group_put(group);
03a76b60 143 return NULL;
d935ad91 144 }
03a76b60
AW
145
146 /*
147 * Where to taint? At this point we've added an IOMMU group for a
148 * device that is not backed by iommu_ops, therefore any iommu_
149 * callback using iommu_ops can legitimately Oops. So, while we may
150 * be about to give a DMA capable device to a user without IOMMU
151 * protection, which is clearly taint-worthy, let's go ahead and do
152 * it here.
153 */
154 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
155 dev_warn(dev, "Adding kernel taint for vfio-noiommu group on device\n");
156#endif
157
158 return group;
159}
160EXPORT_SYMBOL_GPL(vfio_iommu_group_get);
161
162void vfio_iommu_group_put(struct iommu_group *group, struct device *dev)
163{
164#ifdef CONFIG_VFIO_NOIOMMU
16ab8a5c 165 if (iommu_group_get_iommudata(group) == &noiommu)
03a76b60
AW
166 iommu_group_remove_device(dev);
167#endif
168
169 iommu_group_put(group);
170}
171EXPORT_SYMBOL_GPL(vfio_iommu_group_put);
172
173#ifdef CONFIG_VFIO_NOIOMMU
174static void *vfio_noiommu_open(unsigned long arg)
175{
176 if (arg != VFIO_NOIOMMU_IOMMU)
177 return ERR_PTR(-EINVAL);
178 if (!capable(CAP_SYS_RAWIO))
179 return ERR_PTR(-EPERM);
180
181 return NULL;
182}
183
184static void vfio_noiommu_release(void *iommu_data)
185{
186}
187
188static long vfio_noiommu_ioctl(void *iommu_data,
189 unsigned int cmd, unsigned long arg)
190{
191 if (cmd == VFIO_CHECK_EXTENSION)
192 return noiommu && (arg == VFIO_NOIOMMU_IOMMU) ? 1 : 0;
193
194 return -ENOTTY;
195}
196
03a76b60
AW
197static int vfio_noiommu_attach_group(void *iommu_data,
198 struct iommu_group *iommu_group)
199{
16ab8a5c 200 return iommu_group_get_iommudata(iommu_group) == &noiommu ? 0 : -EINVAL;
03a76b60
AW
201}
202
203static void vfio_noiommu_detach_group(void *iommu_data,
204 struct iommu_group *iommu_group)
205{
206}
207
208static const struct vfio_iommu_driver_ops vfio_noiommu_ops = {
209 .name = "vfio-noiommu",
210 .owner = THIS_MODULE,
211 .open = vfio_noiommu_open,
212 .release = vfio_noiommu_release,
213 .ioctl = vfio_noiommu_ioctl,
214 .attach_group = vfio_noiommu_attach_group,
215 .detach_group = vfio_noiommu_detach_group,
216};
217#endif
218
219
cba3345c
AW
220/**
221 * IOMMU driver registration
222 */
223int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops)
224{
225 struct vfio_iommu_driver *driver, *tmp;
226
227 driver = kzalloc(sizeof(*driver), GFP_KERNEL);
228 if (!driver)
229 return -ENOMEM;
230
231 driver->ops = ops;
232
233 mutex_lock(&vfio.iommu_drivers_lock);
234
235 /* Check for duplicates */
236 list_for_each_entry(tmp, &vfio.iommu_drivers_list, vfio_next) {
237 if (tmp->ops == ops) {
238 mutex_unlock(&vfio.iommu_drivers_lock);
239 kfree(driver);
240 return -EINVAL;
241 }
242 }
243
244 list_add(&driver->vfio_next, &vfio.iommu_drivers_list);
245
246 mutex_unlock(&vfio.iommu_drivers_lock);
247
248 return 0;
249}
250EXPORT_SYMBOL_GPL(vfio_register_iommu_driver);
251
252void vfio_unregister_iommu_driver(const struct vfio_iommu_driver_ops *ops)
253{
254 struct vfio_iommu_driver *driver;
255
256 mutex_lock(&vfio.iommu_drivers_lock);
257 list_for_each_entry(driver, &vfio.iommu_drivers_list, vfio_next) {
258 if (driver->ops == ops) {
259 list_del(&driver->vfio_next);
260 mutex_unlock(&vfio.iommu_drivers_lock);
261 kfree(driver);
262 return;
263 }
264 }
265 mutex_unlock(&vfio.iommu_drivers_lock);
266}
267EXPORT_SYMBOL_GPL(vfio_unregister_iommu_driver);
268
269/**
270 * Group minor allocation/free - both called with vfio.group_lock held
271 */
272static int vfio_alloc_group_minor(struct vfio_group *group)
273{
d1099901 274 return idr_alloc(&vfio.group_idr, group, 0, MINORMASK + 1, GFP_KERNEL);
cba3345c
AW
275}
276
277static void vfio_free_group_minor(int minor)
278{
279 idr_remove(&vfio.group_idr, minor);
280}
281
282static int vfio_iommu_group_notifier(struct notifier_block *nb,
283 unsigned long action, void *data);
284static void vfio_group_get(struct vfio_group *group);
285
286/**
287 * Container objects - containers are created when /dev/vfio/vfio is
288 * opened, but their lifecycle extends until the last user is done, so
289 * it's freed via kref. Must support container/group/device being
290 * closed in any order.
291 */
292static void vfio_container_get(struct vfio_container *container)
293{
294 kref_get(&container->kref);
295}
296
297static void vfio_container_release(struct kref *kref)
298{
299 struct vfio_container *container;
300 container = container_of(kref, struct vfio_container, kref);
301
302 kfree(container);
303}
304
305static void vfio_container_put(struct vfio_container *container)
306{
307 kref_put(&container->kref, vfio_container_release);
308}
309
9df7b25a
JL
310static void vfio_group_unlock_and_free(struct vfio_group *group)
311{
312 mutex_unlock(&vfio.group_lock);
313 /*
314 * Unregister outside of lock. A spurious callback is harmless now
315 * that the group is no longer in vfio.group_list.
316 */
317 iommu_group_unregister_notifier(group->iommu_group, &group->nb);
318 kfree(group);
319}
320
cba3345c
AW
321/**
322 * Group objects - create, release, get, put, search
323 */
16ab8a5c 324static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group)
cba3345c
AW
325{
326 struct vfio_group *group, *tmp;
327 struct device *dev;
328 int ret, minor;
329
330 group = kzalloc(sizeof(*group), GFP_KERNEL);
331 if (!group)
332 return ERR_PTR(-ENOMEM);
333
334 kref_init(&group->kref);
335 INIT_LIST_HEAD(&group->device_list);
336 mutex_init(&group->device_lock);
60720a0f
AW
337 INIT_LIST_HEAD(&group->unbound_list);
338 mutex_init(&group->unbound_lock);
cba3345c 339 atomic_set(&group->container_users, 0);
6d6768c6 340 atomic_set(&group->opened, 0);
cba3345c 341 group->iommu_group = iommu_group;
16ab8a5c
AW
342#ifdef CONFIG_VFIO_NOIOMMU
343 group->noiommu = (iommu_group_get_iommudata(iommu_group) == &noiommu);
344#endif
ccd46dba 345 BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
cba3345c
AW
346
347 group->nb.notifier_call = vfio_iommu_group_notifier;
348
349 /*
350 * blocking notifiers acquire a rwsem around registering and hold
351 * it around callback. Therefore, need to register outside of
352 * vfio.group_lock to avoid A-B/B-A contention. Our callback won't
353 * do anything unless it can find the group in vfio.group_list, so
354 * no harm in registering early.
355 */
356 ret = iommu_group_register_notifier(iommu_group, &group->nb);
357 if (ret) {
358 kfree(group);
359 return ERR_PTR(ret);
360 }
361
362 mutex_lock(&vfio.group_lock);
363
cba3345c
AW
364 /* Did we race creating this group? */
365 list_for_each_entry(tmp, &vfio.group_list, vfio_next) {
366 if (tmp->iommu_group == iommu_group) {
367 vfio_group_get(tmp);
9df7b25a 368 vfio_group_unlock_and_free(group);
cba3345c
AW
369 return tmp;
370 }
371 }
372
2f51bf4b
ZL
373 minor = vfio_alloc_group_minor(group);
374 if (minor < 0) {
375 vfio_group_unlock_and_free(group);
376 return ERR_PTR(minor);
377 }
378
d1099901
AW
379 dev = device_create(vfio.class, NULL,
380 MKDEV(MAJOR(vfio.group_devt), minor),
03a76b60
AW
381 group, "%s%d", group->noiommu ? "noiommu-" : "",
382 iommu_group_id(iommu_group));
cba3345c
AW
383 if (IS_ERR(dev)) {
384 vfio_free_group_minor(minor);
9df7b25a 385 vfio_group_unlock_and_free(group);
7b3a10df 386 return ERR_CAST(dev);
cba3345c
AW
387 }
388
389 group->minor = minor;
390 group->dev = dev;
391
392 list_add(&group->vfio_next, &vfio.group_list);
393
394 mutex_unlock(&vfio.group_lock);
395
396 return group;
397}
398
6d2cd3ce 399/* called with vfio.group_lock held */
cba3345c
AW
400static void vfio_group_release(struct kref *kref)
401{
402 struct vfio_group *group = container_of(kref, struct vfio_group, kref);
60720a0f 403 struct vfio_unbound_dev *unbound, *tmp;
4a68810d 404 struct iommu_group *iommu_group = group->iommu_group;
cba3345c
AW
405
406 WARN_ON(!list_empty(&group->device_list));
65b1adeb 407 WARN_ON(group->notifier.head);
cba3345c 408
60720a0f
AW
409 list_for_each_entry_safe(unbound, tmp,
410 &group->unbound_list, unbound_next) {
411 list_del(&unbound->unbound_next);
412 kfree(unbound);
413 }
414
d1099901 415 device_destroy(vfio.class, MKDEV(MAJOR(vfio.group_devt), group->minor));
cba3345c
AW
416 list_del(&group->vfio_next);
417 vfio_free_group_minor(group->minor);
9df7b25a 418 vfio_group_unlock_and_free(group);
4a68810d 419 iommu_group_put(iommu_group);
cba3345c
AW
420}
421
422static void vfio_group_put(struct vfio_group *group)
423{
6d2cd3ce 424 kref_put_mutex(&group->kref, vfio_group_release, &vfio.group_lock);
cba3345c
AW
425}
426
811642d8
AW
427struct vfio_group_put_work {
428 struct work_struct work;
429 struct vfio_group *group;
430};
431
432static void vfio_group_put_bg(struct work_struct *work)
433{
434 struct vfio_group_put_work *do_work;
435
436 do_work = container_of(work, struct vfio_group_put_work, work);
437
438 vfio_group_put(do_work->group);
439 kfree(do_work);
440}
441
442static void vfio_group_schedule_put(struct vfio_group *group)
443{
444 struct vfio_group_put_work *do_work;
445
446 do_work = kmalloc(sizeof(*do_work), GFP_KERNEL);
447 if (WARN_ON(!do_work))
448 return;
449
450 INIT_WORK(&do_work->work, vfio_group_put_bg);
451 do_work->group = group;
452 schedule_work(&do_work->work);
453}
454
cba3345c
AW
455/* Assume group_lock or group reference is held */
456static void vfio_group_get(struct vfio_group *group)
457{
458 kref_get(&group->kref);
459}
460
461/*
462 * Not really a try as we will sleep for mutex, but we need to make
463 * sure the group pointer is valid under lock and get a reference.
464 */
465static struct vfio_group *vfio_group_try_get(struct vfio_group *group)
466{
467 struct vfio_group *target = group;
468
469 mutex_lock(&vfio.group_lock);
470 list_for_each_entry(group, &vfio.group_list, vfio_next) {
471 if (group == target) {
472 vfio_group_get(group);
473 mutex_unlock(&vfio.group_lock);
474 return group;
475 }
476 }
477 mutex_unlock(&vfio.group_lock);
478
479 return NULL;
480}
481
482static
483struct vfio_group *vfio_group_get_from_iommu(struct iommu_group *iommu_group)
484{
485 struct vfio_group *group;
486
487 mutex_lock(&vfio.group_lock);
488 list_for_each_entry(group, &vfio.group_list, vfio_next) {
489 if (group->iommu_group == iommu_group) {
490 vfio_group_get(group);
491 mutex_unlock(&vfio.group_lock);
492 return group;
493 }
494 }
495 mutex_unlock(&vfio.group_lock);
496
497 return NULL;
498}
499
500static struct vfio_group *vfio_group_get_from_minor(int minor)
501{
502 struct vfio_group *group;
503
504 mutex_lock(&vfio.group_lock);
505 group = idr_find(&vfio.group_idr, minor);
506 if (!group) {
507 mutex_unlock(&vfio.group_lock);
508 return NULL;
509 }
510 vfio_group_get(group);
511 mutex_unlock(&vfio.group_lock);
512
513 return group;
514}
515
7ed3ea8a
KW
516static struct vfio_group *vfio_group_get_from_dev(struct device *dev)
517{
518 struct iommu_group *iommu_group;
519 struct vfio_group *group;
520
521 iommu_group = iommu_group_get(dev);
522 if (!iommu_group)
523 return NULL;
524
525 group = vfio_group_get_from_iommu(iommu_group);
526 iommu_group_put(iommu_group);
527
528 return group;
529}
530
cba3345c
AW
531/**
532 * Device objects - create, release, get, put, search
533 */
534static
535struct vfio_device *vfio_group_create_device(struct vfio_group *group,
536 struct device *dev,
537 const struct vfio_device_ops *ops,
538 void *device_data)
539{
540 struct vfio_device *device;
cba3345c
AW
541
542 device = kzalloc(sizeof(*device), GFP_KERNEL);
543 if (!device)
544 return ERR_PTR(-ENOMEM);
545
546 kref_init(&device->kref);
547 device->dev = dev;
548 device->group = group;
549 device->ops = ops;
550 device->device_data = device_data;
8283b491 551 dev_set_drvdata(dev, device);
cba3345c
AW
552
553 /* No need to get group_lock, caller has group reference */
554 vfio_group_get(group);
555
556 mutex_lock(&group->device_lock);
557 list_add(&device->group_next, &group->device_list);
558 mutex_unlock(&group->device_lock);
559
560 return device;
561}
562
563static void vfio_device_release(struct kref *kref)
564{
565 struct vfio_device *device = container_of(kref,
566 struct vfio_device, kref);
567 struct vfio_group *group = device->group;
568
cba3345c
AW
569 list_del(&device->group_next);
570 mutex_unlock(&group->device_lock);
571
572 dev_set_drvdata(device->dev, NULL);
573
574 kfree(device);
575
576 /* vfio_del_group_dev may be waiting for this device */
577 wake_up(&vfio.release_q);
578}
579
580/* Device reference always implies a group reference */
44f50716 581void vfio_device_put(struct vfio_device *device)
cba3345c 582{
934ad4c2 583 struct vfio_group *group = device->group;
90b1253e 584 kref_put_mutex(&device->kref, vfio_device_release, &group->device_lock);
934ad4c2 585 vfio_group_put(group);
cba3345c 586}
44f50716 587EXPORT_SYMBOL_GPL(vfio_device_put);
cba3345c
AW
588
589static void vfio_device_get(struct vfio_device *device)
590{
591 vfio_group_get(device->group);
592 kref_get(&device->kref);
593}
594
595static struct vfio_device *vfio_group_get_device(struct vfio_group *group,
596 struct device *dev)
597{
598 struct vfio_device *device;
599
600 mutex_lock(&group->device_lock);
601 list_for_each_entry(device, &group->device_list, group_next) {
602 if (device->dev == dev) {
603 vfio_device_get(device);
604 mutex_unlock(&group->device_lock);
605 return device;
606 }
607 }
608 mutex_unlock(&group->device_lock);
609 return NULL;
610}
611
612/*
5f096b14
AW
613 * Some drivers, like pci-stub, are only used to prevent other drivers from
614 * claiming a device and are therefore perfectly legitimate for a user owned
615 * group. The pci-stub driver has no dependencies on DMA or the IOVA mapping
616 * of the device, but it does prevent the user from having direct access to
617 * the device, which is useful in some circumstances.
618 *
619 * We also assume that we can include PCI interconnect devices, ie. bridges.
620 * IOMMU grouping on PCI necessitates that if we lack isolation on a bridge
621 * then all of the downstream devices will be part of the same IOMMU group as
622 * the bridge. Thus, if placing the bridge into the user owned IOVA space
623 * breaks anything, it only does so for user owned devices downstream. Note
624 * that error notification via MSI can be affected for platforms that handle
625 * MSI within the same IOVA space as DMA.
cba3345c 626 */
5f096b14 627static const char * const vfio_driver_whitelist[] = { "pci-stub" };
cba3345c 628
5f096b14 629static bool vfio_dev_whitelisted(struct device *dev, struct device_driver *drv)
cba3345c
AW
630{
631 int i;
632
5f096b14
AW
633 if (dev_is_pci(dev)) {
634 struct pci_dev *pdev = to_pci_dev(dev);
635
636 if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL)
637 return true;
638 }
639
cba3345c
AW
640 for (i = 0; i < ARRAY_SIZE(vfio_driver_whitelist); i++) {
641 if (!strcmp(drv->name, vfio_driver_whitelist[i]))
642 return true;
643 }
644
645 return false;
646}
647
648/*
60720a0f
AW
649 * A vfio group is viable for use by userspace if all devices are in
650 * one of the following states:
651 * - driver-less
652 * - bound to a vfio driver
653 * - bound to a whitelisted driver
5f096b14 654 * - a PCI interconnect device
60720a0f
AW
655 *
656 * We use two methods to determine whether a device is bound to a vfio
657 * driver. The first is to test whether the device exists in the vfio
658 * group. The second is to test if the device exists on the group
659 * unbound_list, indicating it's in the middle of transitioning from
660 * a vfio driver to driver-less.
cba3345c
AW
661 */
662static int vfio_dev_viable(struct device *dev, void *data)
663{
664 struct vfio_group *group = data;
665 struct vfio_device *device;
de2b3eea 666 struct device_driver *drv = ACCESS_ONCE(dev->driver);
60720a0f
AW
667 struct vfio_unbound_dev *unbound;
668 int ret = -EINVAL;
669
670 mutex_lock(&group->unbound_lock);
671 list_for_each_entry(unbound, &group->unbound_list, unbound_next) {
672 if (dev == unbound->dev) {
673 ret = 0;
674 break;
675 }
676 }
677 mutex_unlock(&group->unbound_lock);
cba3345c 678
5f096b14 679 if (!ret || !drv || vfio_dev_whitelisted(dev, drv))
cba3345c
AW
680 return 0;
681
682 device = vfio_group_get_device(group, dev);
683 if (device) {
684 vfio_device_put(device);
685 return 0;
686 }
687
60720a0f 688 return ret;
cba3345c
AW
689}
690
691/**
692 * Async device support
693 */
694static int vfio_group_nb_add_dev(struct vfio_group *group, struct device *dev)
695{
696 struct vfio_device *device;
697
698 /* Do we already know about it? We shouldn't */
699 device = vfio_group_get_device(group, dev);
700 if (WARN_ON_ONCE(device)) {
701 vfio_device_put(device);
702 return 0;
703 }
704
705 /* Nothing to do for idle groups */
706 if (!atomic_read(&group->container_users))
707 return 0;
708
709 /* TODO Prevent device auto probing */
049af106 710 WARN(1, "Device %s added to live group %d!\n", dev_name(dev),
cba3345c
AW
711 iommu_group_id(group->iommu_group));
712
713 return 0;
714}
715
cba3345c
AW
716static int vfio_group_nb_verify(struct vfio_group *group, struct device *dev)
717{
718 /* We don't care what happens when the group isn't in use */
719 if (!atomic_read(&group->container_users))
720 return 0;
721
722 return vfio_dev_viable(dev, group);
723}
724
725static int vfio_iommu_group_notifier(struct notifier_block *nb,
726 unsigned long action, void *data)
727{
728 struct vfio_group *group = container_of(nb, struct vfio_group, nb);
729 struct device *dev = data;
60720a0f 730 struct vfio_unbound_dev *unbound;
cba3345c
AW
731
732 /*
c6401930
AW
733 * Need to go through a group_lock lookup to get a reference or we
734 * risk racing a group being removed. Ignore spurious notifies.
cba3345c
AW
735 */
736 group = vfio_group_try_get(group);
c6401930 737 if (!group)
cba3345c
AW
738 return NOTIFY_OK;
739
740 switch (action) {
741 case IOMMU_GROUP_NOTIFY_ADD_DEVICE:
742 vfio_group_nb_add_dev(group, dev);
743 break;
744 case IOMMU_GROUP_NOTIFY_DEL_DEVICE:
de9c7602
AW
745 /*
746 * Nothing to do here. If the device is in use, then the
747 * vfio sub-driver should block the remove callback until
748 * it is unused. If the device is unused or attached to a
749 * stub driver, then it should be released and we don't
750 * care that it will be going away.
751 */
cba3345c
AW
752 break;
753 case IOMMU_GROUP_NOTIFY_BIND_DRIVER:
754 pr_debug("%s: Device %s, group %d binding to driver\n",
755 __func__, dev_name(dev),
756 iommu_group_id(group->iommu_group));
757 break;
758 case IOMMU_GROUP_NOTIFY_BOUND_DRIVER:
759 pr_debug("%s: Device %s, group %d bound to driver %s\n",
760 __func__, dev_name(dev),
761 iommu_group_id(group->iommu_group), dev->driver->name);
762 BUG_ON(vfio_group_nb_verify(group, dev));
763 break;
764 case IOMMU_GROUP_NOTIFY_UNBIND_DRIVER:
765 pr_debug("%s: Device %s, group %d unbinding from driver %s\n",
766 __func__, dev_name(dev),
767 iommu_group_id(group->iommu_group), dev->driver->name);
768 break;
769 case IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER:
770 pr_debug("%s: Device %s, group %d unbound from driver\n",
771 __func__, dev_name(dev),
772 iommu_group_id(group->iommu_group));
773 /*
774 * XXX An unbound device in a live group is ok, but we'd
775 * really like to avoid the above BUG_ON by preventing other
776 * drivers from binding to it. Once that occurs, we have to
777 * stop the system to maintain isolation. At a minimum, we'd
778 * want a toggle to disable driver auto probe for this device.
779 */
60720a0f
AW
780
781 mutex_lock(&group->unbound_lock);
782 list_for_each_entry(unbound,
783 &group->unbound_list, unbound_next) {
784 if (dev == unbound->dev) {
785 list_del(&unbound->unbound_next);
786 kfree(unbound);
787 break;
788 }
789 }
790 mutex_unlock(&group->unbound_lock);
cba3345c
AW
791 break;
792 }
793
811642d8
AW
794 /*
795 * If we're the last reference to the group, the group will be
796 * released, which includes unregistering the iommu group notifier.
797 * We hold a read-lock on that notifier list, unregistering needs
798 * a write-lock... deadlock. Release our reference asynchronously
799 * to avoid that situation.
800 */
801 vfio_group_schedule_put(group);
cba3345c
AW
802 return NOTIFY_OK;
803}
804
805/**
806 * VFIO driver API
807 */
808int vfio_add_group_dev(struct device *dev,
809 const struct vfio_device_ops *ops, void *device_data)
810{
811 struct iommu_group *iommu_group;
812 struct vfio_group *group;
813 struct vfio_device *device;
814
815 iommu_group = iommu_group_get(dev);
816 if (!iommu_group)
817 return -EINVAL;
818
819 group = vfio_group_get_from_iommu(iommu_group);
820 if (!group) {
16ab8a5c 821 group = vfio_create_group(iommu_group);
cba3345c
AW
822 if (IS_ERR(group)) {
823 iommu_group_put(iommu_group);
824 return PTR_ERR(group);
825 }
4a68810d
AW
826 } else {
827 /*
828 * A found vfio_group already holds a reference to the
829 * iommu_group. A created vfio_group keeps the reference.
830 */
831 iommu_group_put(iommu_group);
cba3345c
AW
832 }
833
834 device = vfio_group_get_device(group, dev);
835 if (device) {
836 WARN(1, "Device %s already exists on group %d\n",
837 dev_name(dev), iommu_group_id(iommu_group));
838 vfio_device_put(device);
839 vfio_group_put(group);
cba3345c
AW
840 return -EBUSY;
841 }
842
843 device = vfio_group_create_device(group, dev, ops, device_data);
844 if (IS_ERR(device)) {
845 vfio_group_put(group);
cba3345c
AW
846 return PTR_ERR(device);
847 }
848
849 /*
4a68810d
AW
850 * Drop all but the vfio_device reference. The vfio_device holds
851 * a reference to the vfio_group, which holds a reference to the
852 * iommu_group.
cba3345c
AW
853 */
854 vfio_group_put(group);
855
856 return 0;
857}
858EXPORT_SYMBOL_GPL(vfio_add_group_dev);
859
44f50716 860/**
20f30017
AW
861 * Get a reference to the vfio_device for a device. Even if the
862 * caller thinks they own the device, they could be racing with a
863 * release call path, so we can't trust drvdata for the shortcut.
864 * Go the long way around, from the iommu_group to the vfio_group
865 * to the vfio_device.
44f50716
VMP
866 */
867struct vfio_device *vfio_device_get_from_dev(struct device *dev)
868{
20f30017
AW
869 struct vfio_group *group;
870 struct vfio_device *device;
871
7ed3ea8a 872 group = vfio_group_get_from_dev(dev);
20f30017
AW
873 if (!group)
874 return NULL;
875
876 device = vfio_group_get_device(group, dev);
877 vfio_group_put(group);
44f50716
VMP
878
879 return device;
880}
881EXPORT_SYMBOL_GPL(vfio_device_get_from_dev);
882
4bc94d5d
AW
883static struct vfio_device *vfio_device_get_from_name(struct vfio_group *group,
884 char *buf)
885{
e324fc82 886 struct vfio_device *it, *device = NULL;
4bc94d5d
AW
887
888 mutex_lock(&group->device_lock);
e324fc82
JR
889 list_for_each_entry(it, &group->device_list, group_next) {
890 if (!strcmp(dev_name(it->dev), buf)) {
891 device = it;
4bc94d5d
AW
892 vfio_device_get(device);
893 break;
894 }
895 }
896 mutex_unlock(&group->device_lock);
897
898 return device;
899}
900
44f50716
VMP
901/*
902 * Caller must hold a reference to the vfio_device
903 */
904void *vfio_device_data(struct vfio_device *device)
905{
906 return device->device_data;
907}
908EXPORT_SYMBOL_GPL(vfio_device_data);
909
e014e944
AW
910/* Given a referenced group, check if it contains the device */
911static bool vfio_dev_present(struct vfio_group *group, struct device *dev)
cba3345c 912{
cba3345c
AW
913 struct vfio_device *device;
914
cba3345c 915 device = vfio_group_get_device(group, dev);
e014e944 916 if (!device)
cba3345c 917 return false;
cba3345c
AW
918
919 vfio_device_put(device);
cba3345c
AW
920 return true;
921}
922
923/*
924 * Decrement the device reference count and wait for the device to be
925 * removed. Open file descriptors for the device... */
926void *vfio_del_group_dev(struct device *dev)
927{
928 struct vfio_device *device = dev_get_drvdata(dev);
929 struct vfio_group *group = device->group;
cba3345c 930 void *device_data = device->device_data;
60720a0f 931 struct vfio_unbound_dev *unbound;
13060b64 932 unsigned int i = 0;
db7d4d7f
AW
933 long ret;
934 bool interrupted = false;
cba3345c 935
e014e944
AW
936 /*
937 * The group exists so long as we have a device reference. Get
938 * a group reference and use it to scan for the device going away.
939 */
940 vfio_group_get(group);
941
60720a0f
AW
942 /*
943 * When the device is removed from the group, the group suddenly
944 * becomes non-viable; the device has a driver (until the unbind
945 * completes), but it's not present in the group. This is bad news
946 * for any external users that need to re-acquire a group reference
947 * in order to match and release their existing reference. To
948 * solve this, we track such devices on the unbound_list to bridge
949 * the gap until they're fully unbound.
950 */
951 unbound = kzalloc(sizeof(*unbound), GFP_KERNEL);
952 if (unbound) {
953 unbound->dev = dev;
954 mutex_lock(&group->unbound_lock);
955 list_add(&unbound->unbound_next, &group->unbound_list);
956 mutex_unlock(&group->unbound_lock);
957 }
958 WARN_ON(!unbound);
959
cba3345c
AW
960 vfio_device_put(device);
961
13060b64
AW
962 /*
963 * If the device is still present in the group after the above
964 * 'put', then it is in use and we need to request it from the
965 * bus driver. The driver may in turn need to request the
966 * device from the user. We send the request on an arbitrary
967 * interval with counter to allow the driver to take escalating
968 * measures to release the device if it has the ability to do so.
969 */
970 do {
971 device = vfio_group_get_device(group, dev);
972 if (!device)
973 break;
974
975 if (device->ops->request)
976 device->ops->request(device_data, i++);
977
978 vfio_device_put(device);
979
db7d4d7f
AW
980 if (interrupted) {
981 ret = wait_event_timeout(vfio.release_q,
982 !vfio_dev_present(group, dev), HZ * 10);
983 } else {
984 ret = wait_event_interruptible_timeout(vfio.release_q,
985 !vfio_dev_present(group, dev), HZ * 10);
986 if (ret == -ERESTARTSYS) {
987 interrupted = true;
988 dev_warn(dev,
989 "Device is currently in use, task"
990 " \"%s\" (%d) "
991 "blocked until device is released",
992 current->comm, task_pid_nr(current));
993 }
994 }
995 } while (ret <= 0);
e014e944
AW
996
997 vfio_group_put(group);
cba3345c 998
cba3345c
AW
999 return device_data;
1000}
1001EXPORT_SYMBOL_GPL(vfio_del_group_dev);
1002
1003/**
1004 * VFIO base fd, /dev/vfio/vfio
1005 */
1006static long vfio_ioctl_check_extension(struct vfio_container *container,
1007 unsigned long arg)
1008{
0b43c082 1009 struct vfio_iommu_driver *driver;
cba3345c
AW
1010 long ret = 0;
1011
0b43c082
AW
1012 down_read(&container->group_lock);
1013
1014 driver = container->iommu_driver;
1015
cba3345c
AW
1016 switch (arg) {
1017 /* No base extensions yet */
1018 default:
1019 /*
1020 * If no driver is set, poll all registered drivers for
1021 * extensions and return the first positive result. If
1022 * a driver is already set, further queries will be passed
1023 * only to that driver.
1024 */
1025 if (!driver) {
1026 mutex_lock(&vfio.iommu_drivers_lock);
ae5515d6
AW
1027 list_for_each_entry(driver, &vfio.iommu_drivers_list,
1028 vfio_next) {
03a76b60
AW
1029
1030#ifdef CONFIG_VFIO_NOIOMMU
1031 if (!list_empty(&container->group_list) &&
1032 (container->noiommu !=
1033 (driver->ops == &vfio_noiommu_ops)))
1034 continue;
1035#endif
1036
cba3345c
AW
1037 if (!try_module_get(driver->ops->owner))
1038 continue;
1039
1040 ret = driver->ops->ioctl(NULL,
1041 VFIO_CHECK_EXTENSION,
1042 arg);
1043 module_put(driver->ops->owner);
1044 if (ret > 0)
1045 break;
1046 }
1047 mutex_unlock(&vfio.iommu_drivers_lock);
1048 } else
1049 ret = driver->ops->ioctl(container->iommu_data,
1050 VFIO_CHECK_EXTENSION, arg);
1051 }
1052
0b43c082
AW
1053 up_read(&container->group_lock);
1054
cba3345c
AW
1055 return ret;
1056}
1057
9587f44a 1058/* hold write lock on container->group_lock */
cba3345c
AW
1059static int __vfio_container_attach_groups(struct vfio_container *container,
1060 struct vfio_iommu_driver *driver,
1061 void *data)
1062{
1063 struct vfio_group *group;
1064 int ret = -ENODEV;
1065
1066 list_for_each_entry(group, &container->group_list, container_next) {
1067 ret = driver->ops->attach_group(data, group->iommu_group);
1068 if (ret)
1069 goto unwind;
1070 }
1071
1072 return ret;
1073
1074unwind:
1075 list_for_each_entry_continue_reverse(group, &container->group_list,
1076 container_next) {
1077 driver->ops->detach_group(data, group->iommu_group);
1078 }
1079
1080 return ret;
1081}
1082
1083static long vfio_ioctl_set_iommu(struct vfio_container *container,
1084 unsigned long arg)
1085{
1086 struct vfio_iommu_driver *driver;
1087 long ret = -ENODEV;
1088
9587f44a 1089 down_write(&container->group_lock);
cba3345c
AW
1090
1091 /*
1092 * The container is designed to be an unprivileged interface while
1093 * the group can be assigned to specific users. Therefore, only by
1094 * adding a group to a container does the user get the privilege of
1095 * enabling the iommu, which may allocate finite resources. There
1096 * is no unset_iommu, but by removing all the groups from a container,
1097 * the container is deprivileged and returns to an unset state.
1098 */
1099 if (list_empty(&container->group_list) || container->iommu_driver) {
9587f44a 1100 up_write(&container->group_lock);
cba3345c
AW
1101 return -EINVAL;
1102 }
1103
1104 mutex_lock(&vfio.iommu_drivers_lock);
ae5515d6 1105 list_for_each_entry(driver, &vfio.iommu_drivers_list, vfio_next) {
cba3345c
AW
1106 void *data;
1107
03a76b60
AW
1108#ifdef CONFIG_VFIO_NOIOMMU
1109 /*
1110 * Only noiommu containers can use vfio-noiommu and noiommu
1111 * containers can only use vfio-noiommu.
1112 */
1113 if (container->noiommu != (driver->ops == &vfio_noiommu_ops))
1114 continue;
1115#endif
1116
cba3345c
AW
1117 if (!try_module_get(driver->ops->owner))
1118 continue;
1119
1120 /*
1121 * The arg magic for SET_IOMMU is the same as CHECK_EXTENSION,
1122 * so test which iommu driver reported support for this
1123 * extension and call open on them. We also pass them the
1124 * magic, allowing a single driver to support multiple
1125 * interfaces if they'd like.
1126 */
1127 if (driver->ops->ioctl(NULL, VFIO_CHECK_EXTENSION, arg) <= 0) {
1128 module_put(driver->ops->owner);
1129 continue;
1130 }
1131
cba3345c
AW
1132 data = driver->ops->open(arg);
1133 if (IS_ERR(data)) {
1134 ret = PTR_ERR(data);
1135 module_put(driver->ops->owner);
7c435b46 1136 continue;
cba3345c
AW
1137 }
1138
1139 ret = __vfio_container_attach_groups(container, driver, data);
7c435b46 1140 if (ret) {
cba3345c
AW
1141 driver->ops->release(data);
1142 module_put(driver->ops->owner);
7c435b46 1143 continue;
cba3345c
AW
1144 }
1145
7c435b46
AW
1146 container->iommu_driver = driver;
1147 container->iommu_data = data;
1148 break;
cba3345c
AW
1149 }
1150
1151 mutex_unlock(&vfio.iommu_drivers_lock);
9587f44a 1152 up_write(&container->group_lock);
cba3345c
AW
1153
1154 return ret;
1155}
1156
1157static long vfio_fops_unl_ioctl(struct file *filep,
1158 unsigned int cmd, unsigned long arg)
1159{
1160 struct vfio_container *container = filep->private_data;
1161 struct vfio_iommu_driver *driver;
1162 void *data;
1163 long ret = -EINVAL;
1164
1165 if (!container)
1166 return ret;
1167
cba3345c
AW
1168 switch (cmd) {
1169 case VFIO_GET_API_VERSION:
1170 ret = VFIO_API_VERSION;
1171 break;
1172 case VFIO_CHECK_EXTENSION:
1173 ret = vfio_ioctl_check_extension(container, arg);
1174 break;
1175 case VFIO_SET_IOMMU:
1176 ret = vfio_ioctl_set_iommu(container, arg);
1177 break;
1178 default:
0b43c082
AW
1179 driver = container->iommu_driver;
1180 data = container->iommu_data;
1181
cba3345c
AW
1182 if (driver) /* passthrough all unrecognized ioctls */
1183 ret = driver->ops->ioctl(data, cmd, arg);
1184 }
1185
1186 return ret;
1187}
1188
1189#ifdef CONFIG_COMPAT
1190static long vfio_fops_compat_ioctl(struct file *filep,
1191 unsigned int cmd, unsigned long arg)
1192{
1193 arg = (unsigned long)compat_ptr(arg);
1194 return vfio_fops_unl_ioctl(filep, cmd, arg);
1195}
1196#endif /* CONFIG_COMPAT */
1197
1198static int vfio_fops_open(struct inode *inode, struct file *filep)
1199{
1200 struct vfio_container *container;
1201
1202 container = kzalloc(sizeof(*container), GFP_KERNEL);
1203 if (!container)
1204 return -ENOMEM;
1205
1206 INIT_LIST_HEAD(&container->group_list);
9587f44a 1207 init_rwsem(&container->group_lock);
cba3345c
AW
1208 kref_init(&container->kref);
1209
1210 filep->private_data = container;
1211
1212 return 0;
1213}
1214
1215static int vfio_fops_release(struct inode *inode, struct file *filep)
1216{
1217 struct vfio_container *container = filep->private_data;
1218
1219 filep->private_data = NULL;
1220
1221 vfio_container_put(container);
1222
1223 return 0;
1224}
1225
1226/*
1227 * Once an iommu driver is set, we optionally pass read/write/mmap
1228 * on to the driver, allowing management interfaces beyond ioctl.
1229 */
1230static ssize_t vfio_fops_read(struct file *filep, char __user *buf,
1231 size_t count, loff_t *ppos)
1232{
1233 struct vfio_container *container = filep->private_data;
0b43c082
AW
1234 struct vfio_iommu_driver *driver;
1235 ssize_t ret = -EINVAL;
cba3345c 1236
0b43c082
AW
1237 driver = container->iommu_driver;
1238 if (likely(driver && driver->ops->read))
1239 ret = driver->ops->read(container->iommu_data,
1240 buf, count, ppos);
cba3345c 1241
0b43c082 1242 return ret;
cba3345c
AW
1243}
1244
1245static ssize_t vfio_fops_write(struct file *filep, const char __user *buf,
1246 size_t count, loff_t *ppos)
1247{
1248 struct vfio_container *container = filep->private_data;
0b43c082
AW
1249 struct vfio_iommu_driver *driver;
1250 ssize_t ret = -EINVAL;
cba3345c 1251
0b43c082
AW
1252 driver = container->iommu_driver;
1253 if (likely(driver && driver->ops->write))
1254 ret = driver->ops->write(container->iommu_data,
1255 buf, count, ppos);
1256
0b43c082 1257 return ret;
cba3345c
AW
1258}
1259
1260static int vfio_fops_mmap(struct file *filep, struct vm_area_struct *vma)
1261{
1262 struct vfio_container *container = filep->private_data;
0b43c082
AW
1263 struct vfio_iommu_driver *driver;
1264 int ret = -EINVAL;
cba3345c 1265
0b43c082
AW
1266 driver = container->iommu_driver;
1267 if (likely(driver && driver->ops->mmap))
1268 ret = driver->ops->mmap(container->iommu_data, vma);
1269
0b43c082 1270 return ret;
cba3345c
AW
1271}
1272
1273static const struct file_operations vfio_fops = {
1274 .owner = THIS_MODULE,
1275 .open = vfio_fops_open,
1276 .release = vfio_fops_release,
1277 .read = vfio_fops_read,
1278 .write = vfio_fops_write,
1279 .unlocked_ioctl = vfio_fops_unl_ioctl,
1280#ifdef CONFIG_COMPAT
1281 .compat_ioctl = vfio_fops_compat_ioctl,
1282#endif
1283 .mmap = vfio_fops_mmap,
1284};
1285
1286/**
1287 * VFIO Group fd, /dev/vfio/$GROUP
1288 */
1289static void __vfio_group_unset_container(struct vfio_group *group)
1290{
1291 struct vfio_container *container = group->container;
1292 struct vfio_iommu_driver *driver;
1293
9587f44a 1294 down_write(&container->group_lock);
cba3345c
AW
1295
1296 driver = container->iommu_driver;
1297 if (driver)
1298 driver->ops->detach_group(container->iommu_data,
1299 group->iommu_group);
1300
1301 group->container = NULL;
1302 list_del(&group->container_next);
1303
1304 /* Detaching the last group deprivileges a container, remove iommu */
1305 if (driver && list_empty(&container->group_list)) {
1306 driver->ops->release(container->iommu_data);
1307 module_put(driver->ops->owner);
1308 container->iommu_driver = NULL;
1309 container->iommu_data = NULL;
1310 }
1311
9587f44a 1312 up_write(&container->group_lock);
cba3345c
AW
1313
1314 vfio_container_put(container);
1315}
1316
1317/*
1318 * VFIO_GROUP_UNSET_CONTAINER should fail if there are other users or
1319 * if there was no container to unset. Since the ioctl is called on
1320 * the group, we know that still exists, therefore the only valid
1321 * transition here is 1->0.
1322 */
1323static int vfio_group_unset_container(struct vfio_group *group)
1324{
1325 int users = atomic_cmpxchg(&group->container_users, 1, 0);
1326
1327 if (!users)
1328 return -EINVAL;
1329 if (users != 1)
1330 return -EBUSY;
1331
1332 __vfio_group_unset_container(group);
1333
1334 return 0;
1335}
1336
1337/*
1338 * When removing container users, anything that removes the last user
1339 * implicitly removes the group from the container. That is, if the
1340 * group file descriptor is closed, as well as any device file descriptors,
1341 * the group is free.
1342 */
1343static void vfio_group_try_dissolve_container(struct vfio_group *group)
1344{
1345 if (0 == atomic_dec_if_positive(&group->container_users))
1346 __vfio_group_unset_container(group);
1347}
1348
1349static int vfio_group_set_container(struct vfio_group *group, int container_fd)
1350{
2903ff01 1351 struct fd f;
cba3345c
AW
1352 struct vfio_container *container;
1353 struct vfio_iommu_driver *driver;
2903ff01 1354 int ret = 0;
cba3345c
AW
1355
1356 if (atomic_read(&group->container_users))
1357 return -EINVAL;
1358
03a76b60
AW
1359 if (group->noiommu && !capable(CAP_SYS_RAWIO))
1360 return -EPERM;
1361
2903ff01
AV
1362 f = fdget(container_fd);
1363 if (!f.file)
cba3345c
AW
1364 return -EBADF;
1365
1366 /* Sanity check, is this really our fd? */
2903ff01
AV
1367 if (f.file->f_op != &vfio_fops) {
1368 fdput(f);
cba3345c
AW
1369 return -EINVAL;
1370 }
1371
2903ff01 1372 container = f.file->private_data;
cba3345c
AW
1373 WARN_ON(!container); /* fget ensures we don't race vfio_release */
1374
9587f44a 1375 down_write(&container->group_lock);
cba3345c 1376
03a76b60
AW
1377 /* Real groups and fake groups cannot mix */
1378 if (!list_empty(&container->group_list) &&
1379 container->noiommu != group->noiommu) {
1380 ret = -EPERM;
1381 goto unlock_out;
1382 }
1383
cba3345c
AW
1384 driver = container->iommu_driver;
1385 if (driver) {
1386 ret = driver->ops->attach_group(container->iommu_data,
1387 group->iommu_group);
1388 if (ret)
1389 goto unlock_out;
1390 }
1391
1392 group->container = container;
03a76b60 1393 container->noiommu = group->noiommu;
cba3345c
AW
1394 list_add(&group->container_next, &container->group_list);
1395
1396 /* Get a reference on the container and mark a user within the group */
1397 vfio_container_get(container);
1398 atomic_inc(&group->container_users);
1399
1400unlock_out:
9587f44a 1401 up_write(&container->group_lock);
2903ff01 1402 fdput(f);
cba3345c
AW
1403 return ret;
1404}
1405
1406static bool vfio_group_viable(struct vfio_group *group)
1407{
1408 return (iommu_group_for_each_dev(group->iommu_group,
1409 group, vfio_dev_viable) == 0);
1410}
1411
32f55d83
KW
1412static int vfio_group_add_container_user(struct vfio_group *group)
1413{
1414 if (!atomic_inc_not_zero(&group->container_users))
1415 return -EINVAL;
1416
1417 if (group->noiommu) {
1418 atomic_dec(&group->container_users);
1419 return -EPERM;
1420 }
1421 if (!group->container->iommu_driver || !vfio_group_viable(group)) {
1422 atomic_dec(&group->container_users);
1423 return -EINVAL;
1424 }
1425
1426 return 0;
1427}
1428
cba3345c
AW
1429static const struct file_operations vfio_device_fops;
1430
1431static int vfio_group_get_device_fd(struct vfio_group *group, char *buf)
1432{
1433 struct vfio_device *device;
1434 struct file *filep;
4bc94d5d 1435 int ret;
cba3345c
AW
1436
1437 if (0 == atomic_read(&group->container_users) ||
1438 !group->container->iommu_driver || !vfio_group_viable(group))
1439 return -EINVAL;
1440
03a76b60
AW
1441 if (group->noiommu && !capable(CAP_SYS_RAWIO))
1442 return -EPERM;
1443
4bc94d5d
AW
1444 device = vfio_device_get_from_name(group, buf);
1445 if (!device)
1446 return -ENODEV;
cba3345c 1447
4bc94d5d
AW
1448 ret = device->ops->open(device->device_data);
1449 if (ret) {
1450 vfio_device_put(device);
1451 return ret;
1452 }
cba3345c 1453
4bc94d5d
AW
1454 /*
1455 * We can't use anon_inode_getfd() because we need to modify
1456 * the f_mode flags directly to allow more than just ioctls
1457 */
1458 ret = get_unused_fd_flags(O_CLOEXEC);
1459 if (ret < 0) {
1460 device->ops->release(device->device_data);
1461 vfio_device_put(device);
1462 return ret;
1463 }
cba3345c 1464
4bc94d5d
AW
1465 filep = anon_inode_getfile("[vfio-device]", &vfio_device_fops,
1466 device, O_RDWR);
1467 if (IS_ERR(filep)) {
1468 put_unused_fd(ret);
1469 ret = PTR_ERR(filep);
1470 device->ops->release(device->device_data);
1471 vfio_device_put(device);
1472 return ret;
1473 }
1474
1475 /*
1476 * TODO: add an anon_inode interface to do this.
1477 * Appears to be missing by lack of need rather than
1478 * explicitly prevented. Now there's need.
1479 */
1480 filep->f_mode |= (FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
cba3345c 1481
4bc94d5d 1482 atomic_inc(&group->container_users);
31605deb 1483
4bc94d5d 1484 fd_install(ret, filep);
cba3345c 1485
03a76b60
AW
1486 if (group->noiommu)
1487 dev_warn(device->dev, "vfio-noiommu device opened by user "
1488 "(%s:%d)\n", current->comm, task_pid_nr(current));
1489
cba3345c
AW
1490 return ret;
1491}
1492
1493static long vfio_group_fops_unl_ioctl(struct file *filep,
1494 unsigned int cmd, unsigned long arg)
1495{
1496 struct vfio_group *group = filep->private_data;
1497 long ret = -ENOTTY;
1498
1499 switch (cmd) {
1500 case VFIO_GROUP_GET_STATUS:
1501 {
1502 struct vfio_group_status status;
1503 unsigned long minsz;
1504
1505 minsz = offsetofend(struct vfio_group_status, flags);
1506
1507 if (copy_from_user(&status, (void __user *)arg, minsz))
1508 return -EFAULT;
1509
1510 if (status.argsz < minsz)
1511 return -EINVAL;
1512
1513 status.flags = 0;
1514
1515 if (vfio_group_viable(group))
1516 status.flags |= VFIO_GROUP_FLAGS_VIABLE;
1517
1518 if (group->container)
1519 status.flags |= VFIO_GROUP_FLAGS_CONTAINER_SET;
1520
1521 if (copy_to_user((void __user *)arg, &status, minsz))
1522 return -EFAULT;
1523
1524 ret = 0;
1525 break;
1526 }
1527 case VFIO_GROUP_SET_CONTAINER:
1528 {
1529 int fd;
1530
1531 if (get_user(fd, (int __user *)arg))
1532 return -EFAULT;
1533
1534 if (fd < 0)
1535 return -EINVAL;
1536
1537 ret = vfio_group_set_container(group, fd);
1538 break;
1539 }
1540 case VFIO_GROUP_UNSET_CONTAINER:
1541 ret = vfio_group_unset_container(group);
1542 break;
1543 case VFIO_GROUP_GET_DEVICE_FD:
1544 {
1545 char *buf;
1546
1547 buf = strndup_user((const char __user *)arg, PAGE_SIZE);
1548 if (IS_ERR(buf))
1549 return PTR_ERR(buf);
1550
1551 ret = vfio_group_get_device_fd(group, buf);
1552 kfree(buf);
1553 break;
1554 }
1555 }
1556
1557 return ret;
1558}
1559
1560#ifdef CONFIG_COMPAT
1561static long vfio_group_fops_compat_ioctl(struct file *filep,
1562 unsigned int cmd, unsigned long arg)
1563{
1564 arg = (unsigned long)compat_ptr(arg);
1565 return vfio_group_fops_unl_ioctl(filep, cmd, arg);
1566}
1567#endif /* CONFIG_COMPAT */
1568
1569static int vfio_group_fops_open(struct inode *inode, struct file *filep)
1570{
1571 struct vfio_group *group;
6d6768c6 1572 int opened;
cba3345c
AW
1573
1574 group = vfio_group_get_from_minor(iminor(inode));
1575 if (!group)
1576 return -ENODEV;
1577
03a76b60
AW
1578 if (group->noiommu && !capable(CAP_SYS_RAWIO)) {
1579 vfio_group_put(group);
1580 return -EPERM;
1581 }
1582
6d6768c6
AW
1583 /* Do we need multiple instances of the group open? Seems not. */
1584 opened = atomic_cmpxchg(&group->opened, 0, 1);
1585 if (opened) {
1586 vfio_group_put(group);
1587 return -EBUSY;
1588 }
1589
1590 /* Is something still in use from a previous open? */
cba3345c 1591 if (group->container) {
6d6768c6 1592 atomic_dec(&group->opened);
cba3345c
AW
1593 vfio_group_put(group);
1594 return -EBUSY;
1595 }
1596
65b1adeb
AW
1597 /* Warn if previous user didn't cleanup and re-init to drop them */
1598 if (WARN_ON(group->notifier.head))
1599 BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
1600
cba3345c
AW
1601 filep->private_data = group;
1602
1603 return 0;
1604}
1605
1606static int vfio_group_fops_release(struct inode *inode, struct file *filep)
1607{
1608 struct vfio_group *group = filep->private_data;
1609
1610 filep->private_data = NULL;
1611
1612 vfio_group_try_dissolve_container(group);
1613
6d6768c6
AW
1614 atomic_dec(&group->opened);
1615
cba3345c
AW
1616 vfio_group_put(group);
1617
1618 return 0;
1619}
1620
1621static const struct file_operations vfio_group_fops = {
1622 .owner = THIS_MODULE,
1623 .unlocked_ioctl = vfio_group_fops_unl_ioctl,
1624#ifdef CONFIG_COMPAT
1625 .compat_ioctl = vfio_group_fops_compat_ioctl,
1626#endif
1627 .open = vfio_group_fops_open,
1628 .release = vfio_group_fops_release,
1629};
1630
1631/**
1632 * VFIO Device fd
1633 */
1634static int vfio_device_fops_release(struct inode *inode, struct file *filep)
1635{
1636 struct vfio_device *device = filep->private_data;
1637
1638 device->ops->release(device->device_data);
1639
1640 vfio_group_try_dissolve_container(device->group);
1641
1642 vfio_device_put(device);
1643
1644 return 0;
1645}
1646
1647static long vfio_device_fops_unl_ioctl(struct file *filep,
1648 unsigned int cmd, unsigned long arg)
1649{
1650 struct vfio_device *device = filep->private_data;
1651
1652 if (unlikely(!device->ops->ioctl))
1653 return -EINVAL;
1654
1655 return device->ops->ioctl(device->device_data, cmd, arg);
1656}
1657
1658static ssize_t vfio_device_fops_read(struct file *filep, char __user *buf,
1659 size_t count, loff_t *ppos)
1660{
1661 struct vfio_device *device = filep->private_data;
1662
1663 if (unlikely(!device->ops->read))
1664 return -EINVAL;
1665
1666 return device->ops->read(device->device_data, buf, count, ppos);
1667}
1668
1669static ssize_t vfio_device_fops_write(struct file *filep,
1670 const char __user *buf,
1671 size_t count, loff_t *ppos)
1672{
1673 struct vfio_device *device = filep->private_data;
1674
1675 if (unlikely(!device->ops->write))
1676 return -EINVAL;
1677
1678 return device->ops->write(device->device_data, buf, count, ppos);
1679}
1680
1681static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma)
1682{
1683 struct vfio_device *device = filep->private_data;
1684
1685 if (unlikely(!device->ops->mmap))
1686 return -EINVAL;
1687
1688 return device->ops->mmap(device->device_data, vma);
1689}
1690
1691#ifdef CONFIG_COMPAT
1692static long vfio_device_fops_compat_ioctl(struct file *filep,
1693 unsigned int cmd, unsigned long arg)
1694{
1695 arg = (unsigned long)compat_ptr(arg);
1696 return vfio_device_fops_unl_ioctl(filep, cmd, arg);
1697}
1698#endif /* CONFIG_COMPAT */
1699
1700static const struct file_operations vfio_device_fops = {
1701 .owner = THIS_MODULE,
1702 .release = vfio_device_fops_release,
1703 .read = vfio_device_fops_read,
1704 .write = vfio_device_fops_write,
1705 .unlocked_ioctl = vfio_device_fops_unl_ioctl,
1706#ifdef CONFIG_COMPAT
1707 .compat_ioctl = vfio_device_fops_compat_ioctl,
1708#endif
1709 .mmap = vfio_device_fops_mmap,
1710};
1711
6cdd9782
AK
1712/**
1713 * External user API, exported by symbols to be linked dynamically.
1714 *
1715 * The protocol includes:
1716 * 1. do normal VFIO init operation:
1717 * - opening a new container;
1718 * - attaching group(s) to it;
1719 * - setting an IOMMU driver for a container.
1720 * When IOMMU is set for a container, all groups in it are
1721 * considered ready to use by an external user.
1722 *
1723 * 2. User space passes a group fd to an external user.
1724 * The external user calls vfio_group_get_external_user()
1725 * to verify that:
1726 * - the group is initialized;
1727 * - IOMMU is set for it.
1728 * If both checks passed, vfio_group_get_external_user()
1729 * increments the container user counter to prevent
1730 * the VFIO group from disposal before KVM exits.
1731 *
1732 * 3. The external user calls vfio_external_user_iommu_id()
1733 * to know an IOMMU ID.
1734 *
1735 * 4. When the external KVM finishes, it calls
1736 * vfio_group_put_external_user() to release the VFIO group.
1737 * This call decrements the container user counter.
1738 */
1739struct vfio_group *vfio_group_get_external_user(struct file *filep)
1740{
1741 struct vfio_group *group = filep->private_data;
32f55d83 1742 int ret;
6cdd9782
AK
1743
1744 if (filep->f_op != &vfio_group_fops)
1745 return ERR_PTR(-EINVAL);
1746
32f55d83
KW
1747 ret = vfio_group_add_container_user(group);
1748 if (ret)
1749 return ERR_PTR(ret);
6cdd9782
AK
1750
1751 vfio_group_get(group);
1752
1753 return group;
1754}
1755EXPORT_SYMBOL_GPL(vfio_group_get_external_user);
1756
1757void vfio_group_put_external_user(struct vfio_group *group)
1758{
6cdd9782 1759 vfio_group_try_dissolve_container(group);
d370c917 1760 vfio_group_put(group);
6cdd9782
AK
1761}
1762EXPORT_SYMBOL_GPL(vfio_group_put_external_user);
1763
5d6dee80
AW
1764bool vfio_external_group_match_file(struct vfio_group *test_group,
1765 struct file *filep)
1766{
1767 struct vfio_group *group = filep->private_data;
1768
1769 return (filep->f_op == &vfio_group_fops) && (group == test_group);
1770}
1771EXPORT_SYMBOL_GPL(vfio_external_group_match_file);
1772
6cdd9782
AK
1773int vfio_external_user_iommu_id(struct vfio_group *group)
1774{
1775 return iommu_group_id(group->iommu_group);
1776}
1777EXPORT_SYMBOL_GPL(vfio_external_user_iommu_id);
1778
88d7ab89
AW
1779long vfio_external_check_extension(struct vfio_group *group, unsigned long arg)
1780{
1781 return vfio_ioctl_check_extension(group->container, arg);
1782}
1783EXPORT_SYMBOL_GPL(vfio_external_check_extension);
1784
d7a8d5ed
AW
1785/**
1786 * Sub-module support
1787 */
1788/*
1789 * Helper for managing a buffer of info chain capabilities, allocate or
1790 * reallocate a buffer with additional @size, filling in @id and @version
1791 * of the capability. A pointer to the new capability is returned.
1792 *
1793 * NB. The chain is based at the head of the buffer, so new entries are
1794 * added to the tail, vfio_info_cap_shift() should be called to fixup the
1795 * next offsets prior to copying to the user buffer.
1796 */
1797struct vfio_info_cap_header *vfio_info_cap_add(struct vfio_info_cap *caps,
1798 size_t size, u16 id, u16 version)
1799{
1800 void *buf;
1801 struct vfio_info_cap_header *header, *tmp;
1802
1803 buf = krealloc(caps->buf, caps->size + size, GFP_KERNEL);
1804 if (!buf) {
1805 kfree(caps->buf);
1806 caps->size = 0;
1807 return ERR_PTR(-ENOMEM);
1808 }
1809
1810 caps->buf = buf;
1811 header = buf + caps->size;
1812
1813 /* Eventually copied to user buffer, zero */
1814 memset(header, 0, size);
1815
1816 header->id = id;
1817 header->version = version;
1818
1819 /* Add to the end of the capability chain */
5ba6de98 1820 for (tmp = buf; tmp->next; tmp = buf + tmp->next)
d7a8d5ed
AW
1821 ; /* nothing */
1822
1823 tmp->next = caps->size;
1824 caps->size += size;
1825
1826 return header;
1827}
1828EXPORT_SYMBOL_GPL(vfio_info_cap_add);
1829
1830void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset)
1831{
1832 struct vfio_info_cap_header *tmp;
5ba6de98 1833 void *buf = (void *)caps->buf;
d7a8d5ed 1834
5ba6de98 1835 for (tmp = buf; tmp->next; tmp = buf + tmp->next - offset)
d7a8d5ed
AW
1836 tmp->next += offset;
1837}
b3c0a866 1838EXPORT_SYMBOL(vfio_info_cap_shift);
d7a8d5ed 1839
b3c0a866
KW
1840static int sparse_mmap_cap(struct vfio_info_cap *caps, void *cap_type)
1841{
1842 struct vfio_info_cap_header *header;
1843 struct vfio_region_info_cap_sparse_mmap *sparse_cap, *sparse = cap_type;
1844 size_t size;
1845
1846 size = sizeof(*sparse) + sparse->nr_areas * sizeof(*sparse->areas);
1847 header = vfio_info_cap_add(caps, size,
1848 VFIO_REGION_INFO_CAP_SPARSE_MMAP, 1);
1849 if (IS_ERR(header))
1850 return PTR_ERR(header);
1851
1852 sparse_cap = container_of(header,
1853 struct vfio_region_info_cap_sparse_mmap, header);
1854 sparse_cap->nr_areas = sparse->nr_areas;
1855 memcpy(sparse_cap->areas, sparse->areas,
1856 sparse->nr_areas * sizeof(*sparse->areas));
1857 return 0;
1858}
1859
1860static int region_type_cap(struct vfio_info_cap *caps, void *cap_type)
1861{
1862 struct vfio_info_cap_header *header;
1863 struct vfio_region_info_cap_type *type_cap, *cap = cap_type;
1864
1865 header = vfio_info_cap_add(caps, sizeof(*cap),
1866 VFIO_REGION_INFO_CAP_TYPE, 1);
1867 if (IS_ERR(header))
1868 return PTR_ERR(header);
1869
1870 type_cap = container_of(header, struct vfio_region_info_cap_type,
1871 header);
1872 type_cap->type = cap->type;
1873 type_cap->subtype = cap->subtype;
1874 return 0;
1875}
1876
1877int vfio_info_add_capability(struct vfio_info_cap *caps, int cap_type_id,
1878 void *cap_type)
1879{
1880 int ret = -EINVAL;
1881
1882 if (!cap_type)
1883 return 0;
1884
1885 switch (cap_type_id) {
1886 case VFIO_REGION_INFO_CAP_SPARSE_MMAP:
1887 ret = sparse_mmap_cap(caps, cap_type);
1888 break;
1889
1890 case VFIO_REGION_INFO_CAP_TYPE:
1891 ret = region_type_cap(caps, cap_type);
1892 break;
1893 }
1894
1895 return ret;
1896}
1897EXPORT_SYMBOL(vfio_info_add_capability);
2169037d 1898
c747f08a
KW
1899int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr, int num_irqs,
1900 int max_irq_type, size_t *data_size)
1901{
1902 unsigned long minsz;
1903 size_t size;
1904
1905 minsz = offsetofend(struct vfio_irq_set, count);
1906
1907 if ((hdr->argsz < minsz) || (hdr->index >= max_irq_type) ||
1908 (hdr->count >= (U32_MAX - hdr->start)) ||
1909 (hdr->flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
1910 VFIO_IRQ_SET_ACTION_TYPE_MASK)))
1911 return -EINVAL;
1912
1913 if (data_size)
1914 *data_size = 0;
1915
1916 if (hdr->start >= num_irqs || hdr->start + hdr->count > num_irqs)
1917 return -EINVAL;
1918
1919 switch (hdr->flags & VFIO_IRQ_SET_DATA_TYPE_MASK) {
1920 case VFIO_IRQ_SET_DATA_NONE:
1921 size = 0;
1922 break;
1923 case VFIO_IRQ_SET_DATA_BOOL:
1924 size = sizeof(uint8_t);
1925 break;
1926 case VFIO_IRQ_SET_DATA_EVENTFD:
1927 size = sizeof(int32_t);
1928 break;
1929 default:
1930 return -EINVAL;
1931 }
1932
1933 if (size) {
1934 if (hdr->argsz - minsz < hdr->count * size)
1935 return -EINVAL;
1936
1937 if (!data_size)
1938 return -EINVAL;
1939
1940 *data_size = hdr->count * size;
1941 }
1942
1943 return 0;
1944}
1945EXPORT_SYMBOL(vfio_set_irqs_validate_and_prepare);
1946
2169037d
KW
1947/*
1948 * Pin a set of guest PFNs and return their associated host PFNs for local
1949 * domain only.
1950 * @dev [in] : device
d9d84780 1951 * @user_pfn [in]: array of user/guest PFNs to be pinned.
2169037d
KW
1952 * @npage [in] : count of elements in user_pfn array. This count should not
1953 * be greater VFIO_PIN_PAGES_MAX_ENTRIES.
1954 * @prot [in] : protection flags
1955 * @phys_pfn[out]: array of host PFNs
1956 * Return error or number of pages pinned.
1957 */
1958int vfio_pin_pages(struct device *dev, unsigned long *user_pfn, int npage,
1959 int prot, unsigned long *phys_pfn)
1960{
1961 struct vfio_container *container;
1962 struct vfio_group *group;
1963 struct vfio_iommu_driver *driver;
1964 int ret;
1965
1966 if (!dev || !user_pfn || !phys_pfn || !npage)
1967 return -EINVAL;
1968
1969 if (npage > VFIO_PIN_PAGES_MAX_ENTRIES)
1970 return -E2BIG;
1971
1972 group = vfio_group_get_from_dev(dev);
d256459f
CJ
1973 if (!group)
1974 return -ENODEV;
2169037d
KW
1975
1976 ret = vfio_group_add_container_user(group);
1977 if (ret)
1978 goto err_pin_pages;
1979
1980 container = group->container;
2169037d
KW
1981 driver = container->iommu_driver;
1982 if (likely(driver && driver->ops->pin_pages))
1983 ret = driver->ops->pin_pages(container->iommu_data, user_pfn,
1984 npage, prot, phys_pfn);
1985 else
1986 ret = -ENOTTY;
1987
2169037d
KW
1988 vfio_group_try_dissolve_container(group);
1989
1990err_pin_pages:
1991 vfio_group_put(group);
1992 return ret;
1993}
1994EXPORT_SYMBOL(vfio_pin_pages);
1995
1996/*
1997 * Unpin set of host PFNs for local domain only.
1998 * @dev [in] : device
1999 * @user_pfn [in]: array of user/guest PFNs to be unpinned. Number of user/guest
2000 * PFNs should not be greater than VFIO_PIN_PAGES_MAX_ENTRIES.
2001 * @npage [in] : count of elements in user_pfn array. This count should not
2002 * be greater than VFIO_PIN_PAGES_MAX_ENTRIES.
2003 * Return error or number of pages unpinned.
2004 */
2005int vfio_unpin_pages(struct device *dev, unsigned long *user_pfn, int npage)
2006{
2007 struct vfio_container *container;
2008 struct vfio_group *group;
2009 struct vfio_iommu_driver *driver;
2010 int ret;
2011
2012 if (!dev || !user_pfn || !npage)
2013 return -EINVAL;
2014
2015 if (npage > VFIO_PIN_PAGES_MAX_ENTRIES)
2016 return -E2BIG;
2017
2018 group = vfio_group_get_from_dev(dev);
d256459f
CJ
2019 if (!group)
2020 return -ENODEV;
2169037d
KW
2021
2022 ret = vfio_group_add_container_user(group);
2023 if (ret)
2024 goto err_unpin_pages;
2025
2026 container = group->container;
2169037d
KW
2027 driver = container->iommu_driver;
2028 if (likely(driver && driver->ops->unpin_pages))
2029 ret = driver->ops->unpin_pages(container->iommu_data, user_pfn,
2030 npage);
2031 else
2032 ret = -ENOTTY;
2033
2169037d
KW
2034 vfio_group_try_dissolve_container(group);
2035
2036err_unpin_pages:
2037 vfio_group_put(group);
2038 return ret;
2039}
2040EXPORT_SYMBOL(vfio_unpin_pages);
2041
22195cbd
JS
2042static int vfio_register_iommu_notifier(struct vfio_group *group,
2043 unsigned long *events,
2044 struct notifier_block *nb)
c086de81
KW
2045{
2046 struct vfio_container *container;
c086de81
KW
2047 struct vfio_iommu_driver *driver;
2048 int ret;
2049
c086de81
KW
2050 ret = vfio_group_add_container_user(group);
2051 if (ret)
22195cbd 2052 return -EINVAL;
c086de81
KW
2053
2054 container = group->container;
c086de81
KW
2055 driver = container->iommu_driver;
2056 if (likely(driver && driver->ops->register_notifier))
22195cbd
JS
2057 ret = driver->ops->register_notifier(container->iommu_data,
2058 events, nb);
c086de81
KW
2059 else
2060 ret = -ENOTTY;
2061
c086de81
KW
2062 vfio_group_try_dissolve_container(group);
2063
c086de81
KW
2064 return ret;
2065}
c086de81 2066
22195cbd
JS
2067static int vfio_unregister_iommu_notifier(struct vfio_group *group,
2068 struct notifier_block *nb)
c086de81
KW
2069{
2070 struct vfio_container *container;
c086de81
KW
2071 struct vfio_iommu_driver *driver;
2072 int ret;
2073
c086de81
KW
2074 ret = vfio_group_add_container_user(group);
2075 if (ret)
22195cbd 2076 return -EINVAL;
c086de81
KW
2077
2078 container = group->container;
c086de81
KW
2079 driver = container->iommu_driver;
2080 if (likely(driver && driver->ops->unregister_notifier))
2081 ret = driver->ops->unregister_notifier(container->iommu_data,
2082 nb);
2083 else
2084 ret = -ENOTTY;
2085
c086de81
KW
2086 vfio_group_try_dissolve_container(group);
2087
22195cbd
JS
2088 return ret;
2089}
2090
ccd46dba
JS
2091void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm)
2092{
2093 group->kvm = kvm;
2094 blocking_notifier_call_chain(&group->notifier,
2095 VFIO_GROUP_NOTIFY_SET_KVM, kvm);
2096}
2097EXPORT_SYMBOL_GPL(vfio_group_set_kvm);
2098
2099static int vfio_register_group_notifier(struct vfio_group *group,
2100 unsigned long *events,
2101 struct notifier_block *nb)
2102{
ccd46dba
JS
2103 int ret;
2104 bool set_kvm = false;
2105
2106 if (*events & VFIO_GROUP_NOTIFY_SET_KVM)
2107 set_kvm = true;
2108
2109 /* clear known events */
2110 *events &= ~VFIO_GROUP_NOTIFY_SET_KVM;
2111
2112 /* refuse to continue if still events remaining */
2113 if (*events)
2114 return -EINVAL;
2115
2116 ret = vfio_group_add_container_user(group);
2117 if (ret)
2118 return -EINVAL;
2119
ccd46dba
JS
2120 ret = blocking_notifier_chain_register(&group->notifier, nb);
2121
2122 /*
2123 * The attaching of kvm and vfio_group might already happen, so
2124 * here we replay once upon registration.
2125 */
2126 if (!ret && set_kvm && group->kvm)
2127 blocking_notifier_call_chain(&group->notifier,
2128 VFIO_GROUP_NOTIFY_SET_KVM, group->kvm);
2129
ccd46dba
JS
2130 vfio_group_try_dissolve_container(group);
2131
2132 return ret;
2133}
2134
2135static int vfio_unregister_group_notifier(struct vfio_group *group,
2136 struct notifier_block *nb)
2137{
ccd46dba
JS
2138 int ret;
2139
2140 ret = vfio_group_add_container_user(group);
2141 if (ret)
2142 return -EINVAL;
2143
ccd46dba
JS
2144 ret = blocking_notifier_chain_unregister(&group->notifier, nb);
2145
ccd46dba
JS
2146 vfio_group_try_dissolve_container(group);
2147
2148 return ret;
2149}
2150
22195cbd
JS
2151int vfio_register_notifier(struct device *dev, enum vfio_notify_type type,
2152 unsigned long *events, struct notifier_block *nb)
2153{
2154 struct vfio_group *group;
2155 int ret;
2156
2157 if (!dev || !nb || !events || (*events == 0))
2158 return -EINVAL;
2159
2160 group = vfio_group_get_from_dev(dev);
2161 if (!group)
2162 return -ENODEV;
2163
2164 switch (type) {
2165 case VFIO_IOMMU_NOTIFY:
2166 ret = vfio_register_iommu_notifier(group, events, nb);
2167 break;
ccd46dba
JS
2168 case VFIO_GROUP_NOTIFY:
2169 ret = vfio_register_group_notifier(group, events, nb);
2170 break;
22195cbd
JS
2171 default:
2172 ret = -EINVAL;
2173 }
2174
2175 vfio_group_put(group);
2176 return ret;
2177}
2178EXPORT_SYMBOL(vfio_register_notifier);
2179
2180int vfio_unregister_notifier(struct device *dev, enum vfio_notify_type type,
2181 struct notifier_block *nb)
2182{
2183 struct vfio_group *group;
2184 int ret;
2185
2186 if (!dev || !nb)
2187 return -EINVAL;
2188
2189 group = vfio_group_get_from_dev(dev);
2190 if (!group)
2191 return -ENODEV;
2192
2193 switch (type) {
2194 case VFIO_IOMMU_NOTIFY:
2195 ret = vfio_unregister_iommu_notifier(group, nb);
2196 break;
ccd46dba
JS
2197 case VFIO_GROUP_NOTIFY:
2198 ret = vfio_unregister_group_notifier(group, nb);
2199 break;
22195cbd
JS
2200 default:
2201 ret = -EINVAL;
2202 }
2203
c086de81
KW
2204 vfio_group_put(group);
2205 return ret;
2206}
2207EXPORT_SYMBOL(vfio_unregister_notifier);
2208
cba3345c
AW
2209/**
2210 * Module/class support
2211 */
2212static char *vfio_devnode(struct device *dev, umode_t *mode)
2213{
2214 return kasprintf(GFP_KERNEL, "vfio/%s", dev_name(dev));
2215}
2216
d1099901
AW
2217static struct miscdevice vfio_dev = {
2218 .minor = VFIO_MINOR,
2219 .name = "vfio",
2220 .fops = &vfio_fops,
2221 .nodename = "vfio/vfio",
2222 .mode = S_IRUGO | S_IWUGO,
2223};
2224
cba3345c
AW
2225static int __init vfio_init(void)
2226{
2227 int ret;
2228
2229 idr_init(&vfio.group_idr);
2230 mutex_init(&vfio.group_lock);
2231 mutex_init(&vfio.iommu_drivers_lock);
2232 INIT_LIST_HEAD(&vfio.group_list);
2233 INIT_LIST_HEAD(&vfio.iommu_drivers_list);
2234 init_waitqueue_head(&vfio.release_q);
2235
d1099901
AW
2236 ret = misc_register(&vfio_dev);
2237 if (ret) {
2238 pr_err("vfio: misc device register failed\n");
2239 return ret;
2240 }
2241
2242 /* /dev/vfio/$GROUP */
cba3345c
AW
2243 vfio.class = class_create(THIS_MODULE, "vfio");
2244 if (IS_ERR(vfio.class)) {
2245 ret = PTR_ERR(vfio.class);
2246 goto err_class;
2247 }
2248
2249 vfio.class->devnode = vfio_devnode;
2250
d1099901 2251 ret = alloc_chrdev_region(&vfio.group_devt, 0, MINORMASK, "vfio");
cba3345c 2252 if (ret)
d1099901 2253 goto err_alloc_chrdev;
cba3345c 2254
cba3345c 2255 cdev_init(&vfio.group_cdev, &vfio_group_fops);
d1099901 2256 ret = cdev_add(&vfio.group_cdev, vfio.group_devt, MINORMASK);
cba3345c 2257 if (ret)
d1099901 2258 goto err_cdev_add;
cba3345c
AW
2259
2260 pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
2261
03a76b60
AW
2262#ifdef CONFIG_VFIO_NOIOMMU
2263 vfio_register_iommu_driver(&vfio_noiommu_ops);
2264#endif
cba3345c
AW
2265 return 0;
2266
d1099901
AW
2267err_cdev_add:
2268 unregister_chrdev_region(vfio.group_devt, MINORMASK);
2269err_alloc_chrdev:
cba3345c
AW
2270 class_destroy(vfio.class);
2271 vfio.class = NULL;
2272err_class:
d1099901 2273 misc_deregister(&vfio_dev);
cba3345c
AW
2274 return ret;
2275}
2276
2277static void __exit vfio_cleanup(void)
2278{
2279 WARN_ON(!list_empty(&vfio.group_list));
2280
03a76b60
AW
2281#ifdef CONFIG_VFIO_NOIOMMU
2282 vfio_unregister_iommu_driver(&vfio_noiommu_ops);
2283#endif
cba3345c
AW
2284 idr_destroy(&vfio.group_idr);
2285 cdev_del(&vfio.group_cdev);
d1099901 2286 unregister_chrdev_region(vfio.group_devt, MINORMASK);
cba3345c
AW
2287 class_destroy(vfio.class);
2288 vfio.class = NULL;
d1099901 2289 misc_deregister(&vfio_dev);
cba3345c
AW
2290}
2291
2292module_init(vfio_init);
2293module_exit(vfio_cleanup);
2294
2295MODULE_VERSION(DRIVER_VERSION);
2296MODULE_LICENSE("GPL v2");
2297MODULE_AUTHOR(DRIVER_AUTHOR);
2298MODULE_DESCRIPTION(DRIVER_DESC);
d1099901
AW
2299MODULE_ALIAS_MISCDEV(VFIO_MINOR);
2300MODULE_ALIAS("devname:vfio/vfio");
0ca582fd 2301MODULE_SOFTDEP("post: vfio_iommu_type1 vfio_iommu_spapr_tce");