blob: 4ee4f361fe9fc32b1a68aabe362cd459c5ccabf1 [file] [log] [blame]
Alex Williamsoncba33452012-07-31 08:16:22 -06001/*
2 * VFIO core
3 *
4 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
5 * Author: Alex Williamson <alex.williamson@redhat.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * Derived from original vfio:
12 * Copyright 2010 Cisco Systems, Inc. All rights reserved.
13 * Author: Tom Lyon, pugs@cisco.com
14 */
15
16#include <linux/cdev.h>
17#include <linux/compat.h>
18#include <linux/device.h>
19#include <linux/file.h>
20#include <linux/anon_inodes.h>
21#include <linux/fs.h>
22#include <linux/idr.h>
23#include <linux/iommu.h>
24#include <linux/list.h>
Alex Williamsond1099902013-12-19 10:17:13 -070025#include <linux/miscdevice.h>
Alex Williamsoncba33452012-07-31 08:16:22 -060026#include <linux/module.h>
27#include <linux/mutex.h>
Alex Williamson5f096b12015-10-27 14:53:04 -060028#include <linux/pci.h>
Alex Williamson9587f442013-04-25 16:12:38 -060029#include <linux/rwsem.h>
Alex Williamsoncba33452012-07-31 08:16:22 -060030#include <linux/sched.h>
31#include <linux/slab.h>
Alex Williamson664e9382013-04-30 15:42:28 -060032#include <linux/stat.h>
Alex Williamsoncba33452012-07-31 08:16:22 -060033#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;
Alex Williamsond1099902013-12-19 10:17:13 -070050 dev_t group_devt;
Alex Williamsoncba33452012-07-31 08:16:22 -060051 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;
Alex Williamson9587f442013-04-25 16:12:38 -060062 struct rw_semaphore group_lock;
Alex Williamsoncba33452012-07-31 08:16:22 -060063 struct vfio_iommu_driver *iommu_driver;
64 void *iommu_data;
Alex Williamson03a76b62015-12-21 15:13:33 -070065 bool noiommu;
Alex Williamsoncba33452012-07-31 08:16:22 -060066};
67
Alex Williamson60720a02015-02-06 15:05:06 -070068struct vfio_unbound_dev {
69 struct device *dev;
70 struct list_head unbound_next;
71};
72
Alex Williamsoncba33452012-07-31 08:16:22 -060073struct 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;
Alex Williamson60720a02015-02-06 15:05:06 -070085 struct list_head unbound_list;
86 struct mutex unbound_lock;
Alex Williamson6d6768c2013-06-25 16:06:54 -060087 atomic_t opened;
Alex Williamson03a76b62015-12-21 15:13:33 -070088 bool noiommu;
Jike Songccd46db2016-12-01 13:20:06 +080089 struct kvm *kvm;
90 struct blocking_notifier_head notifier;
Alex Williamsoncba33452012-07-31 08:16:22 -060091};
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
Alex Williamson03a76b62015-12-21 15:13:33 -0700102#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
Alex Williamson16ab8a52016-01-27 11:22:25 -0700128 * bus. We set iommudata simply to be able to identify these groups
129 * as special use and for reclamation later.
Alex Williamson03a76b62015-12-21 15:13:33 -0700130 */
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");
Alex Williamson16ab8a52016-01-27 11:22:25 -0700139 iommu_group_set_iommudata(group, &noiommu, NULL);
Alex Williamson03a76b62015-12-21 15:13:33 -0700140 ret = iommu_group_add_device(group, dev);
Eric Augerd935ad92017-08-11 15:16:06 +0200141 if (ret) {
142 iommu_group_put(group);
Alex Williamson03a76b62015-12-21 15:13:33 -0700143 return NULL;
Eric Augerd935ad92017-08-11 15:16:06 +0200144 }
Alex Williamson03a76b62015-12-21 15:13:33 -0700145
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
Alex Williamson16ab8a52016-01-27 11:22:25 -0700165 if (iommu_group_get_iommudata(group) == &noiommu)
Alex Williamson03a76b62015-12-21 15:13:33 -0700166 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
Alex Williamson03a76b62015-12-21 15:13:33 -0700197static int vfio_noiommu_attach_group(void *iommu_data,
198 struct iommu_group *iommu_group)
199{
Alex Williamson16ab8a52016-01-27 11:22:25 -0700200 return iommu_group_get_iommudata(iommu_group) == &noiommu ? 0 : -EINVAL;
Alex Williamson03a76b62015-12-21 15:13:33 -0700201}
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
Alex Williamsoncba33452012-07-31 08:16:22 -0600220/**
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{
Alex Williamsond1099902013-12-19 10:17:13 -0700274 return idr_alloc(&vfio.group_idr, group, 0, MINORMASK + 1, GFP_KERNEL);
Alex Williamsoncba33452012-07-31 08:16:22 -0600275}
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
Jiang Liu9df7b252012-12-07 13:43:50 -0700310static 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
Alex Williamsoncba33452012-07-31 08:16:22 -0600321/**
322 * Group objects - create, release, get, put, search
323 */
Alex Williamson16ab8a52016-01-27 11:22:25 -0700324static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group)
Alex Williamsoncba33452012-07-31 08:16:22 -0600325{
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);
Alex Williamson60720a02015-02-06 15:05:06 -0700337 INIT_LIST_HEAD(&group->unbound_list);
338 mutex_init(&group->unbound_lock);
Alex Williamsoncba33452012-07-31 08:16:22 -0600339 atomic_set(&group->container_users, 0);
Alex Williamson6d6768c2013-06-25 16:06:54 -0600340 atomic_set(&group->opened, 0);
Alex Williamsoncba33452012-07-31 08:16:22 -0600341 group->iommu_group = iommu_group;
Alex Williamson16ab8a52016-01-27 11:22:25 -0700342#ifdef CONFIG_VFIO_NOIOMMU
343 group->noiommu = (iommu_group_get_iommudata(iommu_group) == &noiommu);
344#endif
Jike Songccd46db2016-12-01 13:20:06 +0800345 BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
Alex Williamsoncba33452012-07-31 08:16:22 -0600346
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
Alex Williamsoncba33452012-07-31 08:16:22 -0600364 /* 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);
Jiang Liu9df7b252012-12-07 13:43:50 -0700368 vfio_group_unlock_and_free(group);
Alex Williamsoncba33452012-07-31 08:16:22 -0600369 return tmp;
370 }
371 }
372
Zhen Lei2f51bf42015-03-16 14:08:56 -0600373 minor = vfio_alloc_group_minor(group);
374 if (minor < 0) {
375 vfio_group_unlock_and_free(group);
376 return ERR_PTR(minor);
377 }
378
Alex Williamsond1099902013-12-19 10:17:13 -0700379 dev = device_create(vfio.class, NULL,
380 MKDEV(MAJOR(vfio.group_devt), minor),
Alex Williamson03a76b62015-12-21 15:13:33 -0700381 group, "%s%d", group->noiommu ? "noiommu-" : "",
382 iommu_group_id(iommu_group));
Alex Williamsoncba33452012-07-31 08:16:22 -0600383 if (IS_ERR(dev)) {
384 vfio_free_group_minor(minor);
Jiang Liu9df7b252012-12-07 13:43:50 -0700385 vfio_group_unlock_and_free(group);
Dan Carpenter7b3a10d2017-05-18 10:34:31 +0300386 return ERR_CAST(dev);
Alex Williamsoncba33452012-07-31 08:16:22 -0600387 }
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
Al Viro6d2cd3c2012-08-17 21:27:32 -0400399/* called with vfio.group_lock held */
Alex Williamsoncba33452012-07-31 08:16:22 -0600400static void vfio_group_release(struct kref *kref)
401{
402 struct vfio_group *group = container_of(kref, struct vfio_group, kref);
Alex Williamson60720a02015-02-06 15:05:06 -0700403 struct vfio_unbound_dev *unbound, *tmp;
Alex Williamson4a688102015-02-06 15:05:06 -0700404 struct iommu_group *iommu_group = group->iommu_group;
Alex Williamsoncba33452012-07-31 08:16:22 -0600405
406 WARN_ON(!list_empty(&group->device_list));
Alex Williamson65b1ade2017-03-21 13:19:09 -0600407 WARN_ON(group->notifier.head);
Alex Williamsoncba33452012-07-31 08:16:22 -0600408
Alex Williamson60720a02015-02-06 15:05:06 -0700409 list_for_each_entry_safe(unbound, tmp,
410 &group->unbound_list, unbound_next) {
411 list_del(&unbound->unbound_next);
412 kfree(unbound);
413 }
414
Alex Williamsond1099902013-12-19 10:17:13 -0700415 device_destroy(vfio.class, MKDEV(MAJOR(vfio.group_devt), group->minor));
Alex Williamsoncba33452012-07-31 08:16:22 -0600416 list_del(&group->vfio_next);
417 vfio_free_group_minor(group->minor);
Jiang Liu9df7b252012-12-07 13:43:50 -0700418 vfio_group_unlock_and_free(group);
Alex Williamson4a688102015-02-06 15:05:06 -0700419 iommu_group_put(iommu_group);
Alex Williamsoncba33452012-07-31 08:16:22 -0600420}
421
422static void vfio_group_put(struct vfio_group *group)
423{
Al Viro6d2cd3c2012-08-17 21:27:32 -0400424 kref_put_mutex(&group->kref, vfio_group_release, &vfio.group_lock);
Alex Williamsoncba33452012-07-31 08:16:22 -0600425}
426
Alex Williamson811642d2017-06-19 09:10:32 -0600427struct 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
Alex Williamsoncba33452012-07-31 08:16:22 -0600455/* 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
Kirti Wankhede7ed3ea82016-11-17 02:16:15 +0530516static 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
Alex Williamsoncba33452012-07-31 08:16:22 -0600531/**
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;
Alex Williamsoncba33452012-07-31 08:16:22 -0600541
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;
Jean Delvare8283b492014-04-14 12:55:38 +0200551 dev_set_drvdata(dev, device);
Alex Williamsoncba33452012-07-31 08:16:22 -0600552
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
Alex Williamsoncba33452012-07-31 08:16:22 -0600569 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 */
Vijay Mohan Pandarathil44f50712013-03-11 09:28:44 -0600581void vfio_device_put(struct vfio_device *device)
Alex Williamsoncba33452012-07-31 08:16:22 -0600582{
Al Viro934ad4c2012-08-17 19:49:09 -0400583 struct vfio_group *group = device->group;
Al Viro90b12532012-08-17 21:29:06 -0400584 kref_put_mutex(&device->kref, vfio_device_release, &group->device_lock);
Al Viro934ad4c2012-08-17 19:49:09 -0400585 vfio_group_put(group);
Alex Williamsoncba33452012-07-31 08:16:22 -0600586}
Vijay Mohan Pandarathil44f50712013-03-11 09:28:44 -0600587EXPORT_SYMBOL_GPL(vfio_device_put);
Alex Williamsoncba33452012-07-31 08:16:22 -0600588
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/*
Alex Williamson5f096b12015-10-27 14:53:04 -0600613 * 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.
Alex Williamsoncba33452012-07-31 08:16:22 -0600626 */
Alex Williamson5f096b12015-10-27 14:53:04 -0600627static const char * const vfio_driver_whitelist[] = { "pci-stub" };
Alex Williamsoncba33452012-07-31 08:16:22 -0600628
Alex Williamson5f096b12015-10-27 14:53:04 -0600629static bool vfio_dev_whitelisted(struct device *dev, struct device_driver *drv)
Alex Williamsoncba33452012-07-31 08:16:22 -0600630{
631 int i;
632
Alex Williamson5f096b12015-10-27 14:53:04 -0600633 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
Alex Williamsoncba33452012-07-31 08:16:22 -0600640 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/*
Alex Williamson60720a02015-02-06 15:05:06 -0700649 * 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
Alex Williamson5f096b12015-10-27 14:53:04 -0600654 * - a PCI interconnect device
Alex Williamson60720a02015-02-06 15:05:06 -0700655 *
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.
Alex Williamsoncba33452012-07-31 08:16:22 -0600661 */
662static int vfio_dev_viable(struct device *dev, void *data)
663{
664 struct vfio_group *group = data;
665 struct vfio_device *device;
Jiang Liude2b3ee2012-12-07 13:43:50 -0700666 struct device_driver *drv = ACCESS_ONCE(dev->driver);
Alex Williamson60720a02015-02-06 15:05:06 -0700667 struct vfio_unbound_dev *unbound;
668 int ret = -EINVAL;
Alex Williamsoncba33452012-07-31 08:16:22 -0600669
Alex Williamson60720a02015-02-06 15:05:06 -0700670 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);
678
Alex Williamson5f096b12015-10-27 14:53:04 -0600679 if (!ret || !drv || vfio_dev_whitelisted(dev, drv))
Alex Williamsoncba33452012-07-31 08:16:22 -0600680 return 0;
681
682 device = vfio_group_get_device(group, dev);
683 if (device) {
684 vfio_device_put(device);
685 return 0;
686 }
687
Alex Williamson60720a02015-02-06 15:05:06 -0700688 return ret;
Alex Williamsoncba33452012-07-31 08:16:22 -0600689}
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 */
Dan Carpenter049af102015-11-21 13:32:21 +0300710 WARN(1, "Device %s added to live group %d!\n", dev_name(dev),
Alex Williamsoncba33452012-07-31 08:16:22 -0600711 iommu_group_id(group->iommu_group));
712
713 return 0;
714}
715
Alex Williamsoncba33452012-07-31 08:16:22 -0600716static 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;
Alex Williamson60720a02015-02-06 15:05:06 -0700730 struct vfio_unbound_dev *unbound;
Alex Williamsoncba33452012-07-31 08:16:22 -0600731
732 /*
Alex Williamsonc6401932013-06-10 16:40:56 -0600733 * Need to go through a group_lock lookup to get a reference or we
734 * risk racing a group being removed. Ignore spurious notifies.
Alex Williamsoncba33452012-07-31 08:16:22 -0600735 */
736 group = vfio_group_try_get(group);
Alex Williamsonc6401932013-06-10 16:40:56 -0600737 if (!group)
Alex Williamsoncba33452012-07-31 08:16:22 -0600738 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:
Alex Williamsonde9c7602013-06-10 16:40:56 -0600745 /*
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 */
Alex Williamsoncba33452012-07-31 08:16:22 -0600752 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 */
Alex Williamson60720a02015-02-06 15:05:06 -0700780
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);
Alex Williamsoncba33452012-07-31 08:16:22 -0600791 break;
792 }
793
Alex Williamson811642d2017-06-19 09:10:32 -0600794 /*
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);
Alex Williamsoncba33452012-07-31 08:16:22 -0600802 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) {
Alex Williamson16ab8a52016-01-27 11:22:25 -0700821 group = vfio_create_group(iommu_group);
Alex Williamsoncba33452012-07-31 08:16:22 -0600822 if (IS_ERR(group)) {
823 iommu_group_put(iommu_group);
824 return PTR_ERR(group);
825 }
Alex Williamson4a688102015-02-06 15:05:06 -0700826 } 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);
Alex Williamsoncba33452012-07-31 08:16:22 -0600832 }
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);
Alex Williamsoncba33452012-07-31 08:16:22 -0600840 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);
Alex Williamsoncba33452012-07-31 08:16:22 -0600846 return PTR_ERR(device);
847 }
848
849 /*
Alex Williamson4a688102015-02-06 15:05:06 -0700850 * 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.
Alex Williamsoncba33452012-07-31 08:16:22 -0600853 */
854 vfio_group_put(group);
855
856 return 0;
857}
858EXPORT_SYMBOL_GPL(vfio_add_group_dev);
859
Vijay Mohan Pandarathil44f50712013-03-11 09:28:44 -0600860/**
Alex Williamson20f30012015-06-09 10:08:57 -0600861 * 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.
Vijay Mohan Pandarathil44f50712013-03-11 09:28:44 -0600866 */
867struct vfio_device *vfio_device_get_from_dev(struct device *dev)
868{
Alex Williamson20f30012015-06-09 10:08:57 -0600869 struct vfio_group *group;
870 struct vfio_device *device;
Vijay Mohan Pandarathil44f50712013-03-11 09:28:44 -0600871
Kirti Wankhede7ed3ea82016-11-17 02:16:15 +0530872 group = vfio_group_get_from_dev(dev);
Alex Williamson20f30012015-06-09 10:08:57 -0600873 if (!group)
874 return NULL;
875
876 device = vfio_group_get_device(group, dev);
877 vfio_group_put(group);
Vijay Mohan Pandarathil44f50712013-03-11 09:28:44 -0600878
879 return device;
880}
881EXPORT_SYMBOL_GPL(vfio_device_get_from_dev);
882
Alex Williamson4bc94d52015-07-24 15:14:04 -0600883static struct vfio_device *vfio_device_get_from_name(struct vfio_group *group,
884 char *buf)
885{
Joerg Roedele324fc82015-11-04 13:53:26 +0100886 struct vfio_device *it, *device = NULL;
Alex Williamson4bc94d52015-07-24 15:14:04 -0600887
888 mutex_lock(&group->device_lock);
Joerg Roedele324fc82015-11-04 13:53:26 +0100889 list_for_each_entry(it, &group->device_list, group_next) {
890 if (!strcmp(dev_name(it->dev), buf)) {
891 device = it;
Alex Williamson4bc94d52015-07-24 15:14:04 -0600892 vfio_device_get(device);
893 break;
894 }
895 }
896 mutex_unlock(&group->device_lock);
897
898 return device;
899}
900
Vijay Mohan Pandarathil44f50712013-03-11 09:28:44 -0600901/*
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
Alex Williamsone014e942013-02-14 14:02:13 -0700910/* Given a referenced group, check if it contains the device */
911static bool vfio_dev_present(struct vfio_group *group, struct device *dev)
Alex Williamsoncba33452012-07-31 08:16:22 -0600912{
Alex Williamsoncba33452012-07-31 08:16:22 -0600913 struct vfio_device *device;
914
Alex Williamsoncba33452012-07-31 08:16:22 -0600915 device = vfio_group_get_device(group, dev);
Alex Williamsone014e942013-02-14 14:02:13 -0700916 if (!device)
Alex Williamsoncba33452012-07-31 08:16:22 -0600917 return false;
Alex Williamsoncba33452012-07-31 08:16:22 -0600918
919 vfio_device_put(device);
Alex Williamsoncba33452012-07-31 08:16:22 -0600920 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;
Alex Williamsoncba33452012-07-31 08:16:22 -0600930 void *device_data = device->device_data;
Alex Williamson60720a02015-02-06 15:05:06 -0700931 struct vfio_unbound_dev *unbound;
Alex Williamson13060b62015-02-06 15:05:07 -0700932 unsigned int i = 0;
Alex Williamsondb7d4d72015-05-01 16:31:41 -0600933 long ret;
934 bool interrupted = false;
Alex Williamsoncba33452012-07-31 08:16:22 -0600935
Alex Williamsone014e942013-02-14 14:02:13 -0700936 /*
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
Alex Williamson60720a02015-02-06 15:05:06 -0700942 /*
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
Alex Williamsoncba33452012-07-31 08:16:22 -0600960 vfio_device_put(device);
961
Alex Williamson13060b62015-02-06 15:05:07 -0700962 /*
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
Alex Williamsondb7d4d72015-05-01 16:31:41 -0600980 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);
Alex Williamsone014e942013-02-14 14:02:13 -0700996
997 vfio_group_put(group);
Alex Williamsoncba33452012-07-31 08:16:22 -0600998
Alex Williamsoncba33452012-07-31 08:16:22 -0600999 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{
Alex Williamson0b43c082013-04-29 08:41:36 -06001009 struct vfio_iommu_driver *driver;
Alex Williamsoncba33452012-07-31 08:16:22 -06001010 long ret = 0;
1011
Alex Williamson0b43c082013-04-29 08:41:36 -06001012 down_read(&container->group_lock);
1013
1014 driver = container->iommu_driver;
1015
Alex Williamsoncba33452012-07-31 08:16:22 -06001016 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);
Alex Williamsonae5515d2015-12-04 08:38:42 -07001027 list_for_each_entry(driver, &vfio.iommu_drivers_list,
1028 vfio_next) {
Alex Williamson03a76b62015-12-21 15:13:33 -07001029
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
Alex Williamsoncba33452012-07-31 08:16:22 -06001037 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
Alex Williamson0b43c082013-04-29 08:41:36 -06001053 up_read(&container->group_lock);
1054
Alex Williamsoncba33452012-07-31 08:16:22 -06001055 return ret;
1056}
1057
Alex Williamson9587f442013-04-25 16:12:38 -06001058/* hold write lock on container->group_lock */
Alex Williamsoncba33452012-07-31 08:16:22 -06001059static 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
Alex Williamson9587f442013-04-25 16:12:38 -06001089 down_write(&container->group_lock);
Alex Williamsoncba33452012-07-31 08:16:22 -06001090
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) {
Alex Williamson9587f442013-04-25 16:12:38 -06001100 up_write(&container->group_lock);
Alex Williamsoncba33452012-07-31 08:16:22 -06001101 return -EINVAL;
1102 }
1103
1104 mutex_lock(&vfio.iommu_drivers_lock);
Alex Williamsonae5515d2015-12-04 08:38:42 -07001105 list_for_each_entry(driver, &vfio.iommu_drivers_list, vfio_next) {
Alex Williamsoncba33452012-07-31 08:16:22 -06001106 void *data;
1107
Alex Williamson03a76b62015-12-21 15:13:33 -07001108#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
Alex Williamsoncba33452012-07-31 08:16:22 -06001117 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
Alex Williamsoncba33452012-07-31 08:16:22 -06001132 data = driver->ops->open(arg);
1133 if (IS_ERR(data)) {
1134 ret = PTR_ERR(data);
1135 module_put(driver->ops->owner);
Alex Williamson7c435b42016-02-22 16:02:30 -07001136 continue;
Alex Williamsoncba33452012-07-31 08:16:22 -06001137 }
1138
1139 ret = __vfio_container_attach_groups(container, driver, data);
Alex Williamson7c435b42016-02-22 16:02:30 -07001140 if (ret) {
Alex Williamsoncba33452012-07-31 08:16:22 -06001141 driver->ops->release(data);
1142 module_put(driver->ops->owner);
Alex Williamson7c435b42016-02-22 16:02:30 -07001143 continue;
Alex Williamsoncba33452012-07-31 08:16:22 -06001144 }
1145
Alex Williamson7c435b42016-02-22 16:02:30 -07001146 container->iommu_driver = driver;
1147 container->iommu_data = data;
1148 break;
Alex Williamsoncba33452012-07-31 08:16:22 -06001149 }
1150
1151 mutex_unlock(&vfio.iommu_drivers_lock);
Alex Williamson9587f442013-04-25 16:12:38 -06001152 up_write(&container->group_lock);
Alex Williamsoncba33452012-07-31 08:16:22 -06001153
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
Alex Williamsoncba33452012-07-31 08:16:22 -06001168 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:
Alex Williamson0b43c082013-04-29 08:41:36 -06001179 driver = container->iommu_driver;
1180 data = container->iommu_data;
1181
Alex Williamsoncba33452012-07-31 08:16:22 -06001182 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);
Alex Williamson9587f442013-04-25 16:12:38 -06001207 init_rwsem(&container->group_lock);
Alex Williamsoncba33452012-07-31 08:16:22 -06001208 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;
Alex Williamson0b43c082013-04-29 08:41:36 -06001234 struct vfio_iommu_driver *driver;
1235 ssize_t ret = -EINVAL;
Alex Williamsoncba33452012-07-31 08:16:22 -06001236
Alex Williamson0b43c082013-04-29 08:41:36 -06001237 driver = container->iommu_driver;
1238 if (likely(driver && driver->ops->read))
1239 ret = driver->ops->read(container->iommu_data,
1240 buf, count, ppos);
1241
Alex Williamson0b43c082013-04-29 08:41:36 -06001242 return ret;
Alex Williamsoncba33452012-07-31 08:16:22 -06001243}
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;
Alex Williamson0b43c082013-04-29 08:41:36 -06001249 struct vfio_iommu_driver *driver;
1250 ssize_t ret = -EINVAL;
Alex Williamsoncba33452012-07-31 08:16:22 -06001251
Alex Williamson0b43c082013-04-29 08:41:36 -06001252 driver = container->iommu_driver;
1253 if (likely(driver && driver->ops->write))
1254 ret = driver->ops->write(container->iommu_data,
1255 buf, count, ppos);
1256
Alex Williamson0b43c082013-04-29 08:41:36 -06001257 return ret;
Alex Williamsoncba33452012-07-31 08:16:22 -06001258}
1259
1260static int vfio_fops_mmap(struct file *filep, struct vm_area_struct *vma)
1261{
1262 struct vfio_container *container = filep->private_data;
Alex Williamson0b43c082013-04-29 08:41:36 -06001263 struct vfio_iommu_driver *driver;
1264 int ret = -EINVAL;
Alex Williamsoncba33452012-07-31 08:16:22 -06001265
Alex Williamson0b43c082013-04-29 08:41:36 -06001266 driver = container->iommu_driver;
1267 if (likely(driver && driver->ops->mmap))
1268 ret = driver->ops->mmap(container->iommu_data, vma);
1269
Alex Williamson0b43c082013-04-29 08:41:36 -06001270 return ret;
Alex Williamsoncba33452012-07-31 08:16:22 -06001271}
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
Alex Williamson9587f442013-04-25 16:12:38 -06001294 down_write(&container->group_lock);
Alex Williamsoncba33452012-07-31 08:16:22 -06001295
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
Alex Williamson9587f442013-04-25 16:12:38 -06001312 up_write(&container->group_lock);
Alex Williamsoncba33452012-07-31 08:16:22 -06001313
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{
Al Viro2903ff02012-08-28 12:52:22 -04001351 struct fd f;
Alex Williamsoncba33452012-07-31 08:16:22 -06001352 struct vfio_container *container;
1353 struct vfio_iommu_driver *driver;
Al Viro2903ff02012-08-28 12:52:22 -04001354 int ret = 0;
Alex Williamsoncba33452012-07-31 08:16:22 -06001355
1356 if (atomic_read(&group->container_users))
1357 return -EINVAL;
1358
Alex Williamson03a76b62015-12-21 15:13:33 -07001359 if (group->noiommu && !capable(CAP_SYS_RAWIO))
1360 return -EPERM;
1361
Al Viro2903ff02012-08-28 12:52:22 -04001362 f = fdget(container_fd);
1363 if (!f.file)
Alex Williamsoncba33452012-07-31 08:16:22 -06001364 return -EBADF;
1365
1366 /* Sanity check, is this really our fd? */
Al Viro2903ff02012-08-28 12:52:22 -04001367 if (f.file->f_op != &vfio_fops) {
1368 fdput(f);
Alex Williamsoncba33452012-07-31 08:16:22 -06001369 return -EINVAL;
1370 }
1371
Al Viro2903ff02012-08-28 12:52:22 -04001372 container = f.file->private_data;
Alex Williamsoncba33452012-07-31 08:16:22 -06001373 WARN_ON(!container); /* fget ensures we don't race vfio_release */
1374
Alex Williamson9587f442013-04-25 16:12:38 -06001375 down_write(&container->group_lock);
Alex Williamsoncba33452012-07-31 08:16:22 -06001376
Alex Williamson03a76b62015-12-21 15:13:33 -07001377 /* 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
Alex Williamsoncba33452012-07-31 08:16:22 -06001384 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;
Alex Williamson03a76b62015-12-21 15:13:33 -07001393 container->noiommu = group->noiommu;
Alex Williamsoncba33452012-07-31 08:16:22 -06001394 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:
Alex Williamson9587f442013-04-25 16:12:38 -06001401 up_write(&container->group_lock);
Al Viro2903ff02012-08-28 12:52:22 -04001402 fdput(f);
Alex Williamsoncba33452012-07-31 08:16:22 -06001403 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
Kirti Wankhede32f55d82016-11-17 02:16:16 +05301412static 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
Alex Williamsoncba33452012-07-31 08:16:22 -06001429static 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;
Alex Williamson4bc94d52015-07-24 15:14:04 -06001435 int ret;
Alex Williamsoncba33452012-07-31 08:16:22 -06001436
1437 if (0 == atomic_read(&group->container_users) ||
1438 !group->container->iommu_driver || !vfio_group_viable(group))
1439 return -EINVAL;
1440
Alex Williamson03a76b62015-12-21 15:13:33 -07001441 if (group->noiommu && !capable(CAP_SYS_RAWIO))
1442 return -EPERM;
1443
Alex Williamson4bc94d52015-07-24 15:14:04 -06001444 device = vfio_device_get_from_name(group, buf);
1445 if (!device)
1446 return -ENODEV;
Alex Williamsoncba33452012-07-31 08:16:22 -06001447
Alex Williamson4bc94d52015-07-24 15:14:04 -06001448 ret = device->ops->open(device->device_data);
1449 if (ret) {
1450 vfio_device_put(device);
1451 return ret;
Alex Williamsoncba33452012-07-31 08:16:22 -06001452 }
Alex Williamson4bc94d52015-07-24 15:14:04 -06001453
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 }
1464
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);
1481
1482 atomic_inc(&group->container_users);
1483
1484 fd_install(ret, filep);
Alex Williamsoncba33452012-07-31 08:16:22 -06001485
Alex Williamson03a76b62015-12-21 15:13:33 -07001486 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
Alex Williamsoncba33452012-07-31 08:16:22 -06001490 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;
Alex Williamson6d6768c2013-06-25 16:06:54 -06001572 int opened;
Alex Williamsoncba33452012-07-31 08:16:22 -06001573
1574 group = vfio_group_get_from_minor(iminor(inode));
1575 if (!group)
1576 return -ENODEV;
1577
Alex Williamson03a76b62015-12-21 15:13:33 -07001578 if (group->noiommu && !capable(CAP_SYS_RAWIO)) {
1579 vfio_group_put(group);
1580 return -EPERM;
1581 }
1582
Alex Williamson6d6768c2013-06-25 16:06:54 -06001583 /* 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? */
Alex Williamsoncba33452012-07-31 08:16:22 -06001591 if (group->container) {
Alex Williamson6d6768c2013-06-25 16:06:54 -06001592 atomic_dec(&group->opened);
Alex Williamsoncba33452012-07-31 08:16:22 -06001593 vfio_group_put(group);
1594 return -EBUSY;
1595 }
1596
Alex Williamson65b1ade2017-03-21 13:19:09 -06001597 /* 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
Alex Williamsoncba33452012-07-31 08:16:22 -06001601 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
Alex Williamson6d6768c2013-06-25 16:06:54 -06001614 atomic_dec(&group->opened);
1615
Alex Williamsoncba33452012-07-31 08:16:22 -06001616 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
1712/**
Alexey Kardashevskiy6cdd9782013-08-05 10:52:36 -06001713 * 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;
Kirti Wankhede32f55d82016-11-17 02:16:16 +05301742 int ret;
Alexey Kardashevskiy6cdd9782013-08-05 10:52:36 -06001743
1744 if (filep->f_op != &vfio_group_fops)
1745 return ERR_PTR(-EINVAL);
1746
Kirti Wankhede32f55d82016-11-17 02:16:16 +05301747 ret = vfio_group_add_container_user(group);
1748 if (ret)
1749 return ERR_PTR(ret);
Alexey Kardashevskiy6cdd9782013-08-05 10:52:36 -06001750
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{
Alexey Kardashevskiy6cdd9782013-08-05 10:52:36 -06001759 vfio_group_try_dissolve_container(group);
Ilya Lesokhind370c912016-07-14 16:50:19 +03001760 vfio_group_put(group);
Alexey Kardashevskiy6cdd9782013-08-05 10:52:36 -06001761}
1762EXPORT_SYMBOL_GPL(vfio_group_put_external_user);
1763
Alex Williamson5d6dee82017-06-28 13:50:05 -06001764bool 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
Alexey Kardashevskiy6cdd9782013-08-05 10:52:36 -06001773int 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
Alex Williamson88d7ab82014-02-26 11:38:39 -07001779long 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
Alexey Kardashevskiy6cdd9782013-08-05 10:52:36 -06001785/**
Alex Williamsond7a8d5e2016-02-22 16:02:33 -07001786 * 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 */
Eric Auger5ba6de92016-11-21 07:21:02 +01001820 for (tmp = buf; tmp->next; tmp = buf + tmp->next)
Alex Williamsond7a8d5e2016-02-22 16:02:33 -07001821 ; /* 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;
Eric Auger5ba6de92016-11-21 07:21:02 +01001833 void *buf = (void *)caps->buf;
Alex Williamsond7a8d5e2016-02-22 16:02:33 -07001834
Eric Auger5ba6de92016-11-21 07:21:02 +01001835 for (tmp = buf; tmp->next; tmp = buf + tmp->next - offset)
Alex Williamsond7a8d5e2016-02-22 16:02:33 -07001836 tmp->next += offset;
1837}
Kirti Wankhedeb3c0a862016-11-17 02:16:25 +05301838EXPORT_SYMBOL(vfio_info_cap_shift);
Alex Williamsond7a8d5e2016-02-22 16:02:33 -07001839
Kirti Wankhedeb3c0a862016-11-17 02:16:25 +05301840static 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);
Kirti Wankhede21690372016-11-17 02:16:17 +05301898
Kirti Wankhedec747f082016-11-17 02:16:27 +05301899int 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
Kirti Wankhede21690372016-11-17 02:16:17 +05301947/*
1948 * Pin a set of guest PFNs and return their associated host PFNs for local
1949 * domain only.
1950 * @dev [in] : device
Changbin Dud9d84782017-02-06 15:03:37 +08001951 * @user_pfn [in]: array of user/guest PFNs to be pinned.
Kirti Wankhede21690372016-11-17 02:16:17 +05301952 * @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);
Christophe JAILLETd2564592016-11-30 08:06:12 +01001973 if (!group)
1974 return -ENODEV;
Kirti Wankhede21690372016-11-17 02:16:17 +05301975
1976 ret = vfio_group_add_container_user(group);
1977 if (ret)
1978 goto err_pin_pages;
1979
1980 container = group->container;
Kirti Wankhede21690372016-11-17 02:16:17 +05301981 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
Kirti Wankhede21690372016-11-17 02:16:17 +05301988 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);
Christophe JAILLETd2564592016-11-30 08:06:12 +01002019 if (!group)
2020 return -ENODEV;
Kirti Wankhede21690372016-11-17 02:16:17 +05302021
2022 ret = vfio_group_add_container_user(group);
2023 if (ret)
2024 goto err_unpin_pages;
2025
2026 container = group->container;
Kirti Wankhede21690372016-11-17 02:16:17 +05302027 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
Kirti Wankhede21690372016-11-17 02:16:17 +05302034 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
Jike Song22195cb2016-12-01 13:20:05 +08002042static int vfio_register_iommu_notifier(struct vfio_group *group,
2043 unsigned long *events,
2044 struct notifier_block *nb)
Kirti Wankhedec086de812016-11-17 10:28:26 +05302045{
2046 struct vfio_container *container;
Kirti Wankhedec086de812016-11-17 10:28:26 +05302047 struct vfio_iommu_driver *driver;
2048 int ret;
2049
Kirti Wankhedec086de812016-11-17 10:28:26 +05302050 ret = vfio_group_add_container_user(group);
2051 if (ret)
Jike Song22195cb2016-12-01 13:20:05 +08002052 return -EINVAL;
Kirti Wankhedec086de812016-11-17 10:28:26 +05302053
2054 container = group->container;
Kirti Wankhedec086de812016-11-17 10:28:26 +05302055 driver = container->iommu_driver;
2056 if (likely(driver && driver->ops->register_notifier))
Jike Song22195cb2016-12-01 13:20:05 +08002057 ret = driver->ops->register_notifier(container->iommu_data,
2058 events, nb);
Kirti Wankhedec086de812016-11-17 10:28:26 +05302059 else
2060 ret = -ENOTTY;
2061
Kirti Wankhedec086de812016-11-17 10:28:26 +05302062 vfio_group_try_dissolve_container(group);
2063
Kirti Wankhedec086de812016-11-17 10:28:26 +05302064 return ret;
2065}
Kirti Wankhedec086de812016-11-17 10:28:26 +05302066
Jike Song22195cb2016-12-01 13:20:05 +08002067static int vfio_unregister_iommu_notifier(struct vfio_group *group,
2068 struct notifier_block *nb)
Kirti Wankhedec086de812016-11-17 10:28:26 +05302069{
2070 struct vfio_container *container;
Kirti Wankhedec086de812016-11-17 10:28:26 +05302071 struct vfio_iommu_driver *driver;
2072 int ret;
2073
Kirti Wankhedec086de812016-11-17 10:28:26 +05302074 ret = vfio_group_add_container_user(group);
2075 if (ret)
Jike Song22195cb2016-12-01 13:20:05 +08002076 return -EINVAL;
Kirti Wankhedec086de812016-11-17 10:28:26 +05302077
2078 container = group->container;
Kirti Wankhedec086de812016-11-17 10:28:26 +05302079 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
Kirti Wankhedec086de812016-11-17 10:28:26 +05302086 vfio_group_try_dissolve_container(group);
2087
Jike Song22195cb2016-12-01 13:20:05 +08002088 return ret;
2089}
2090
Jike Songccd46db2016-12-01 13:20:06 +08002091void 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{
Jike Songccd46db2016-12-01 13:20:06 +08002103 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
Jike Songccd46db2016-12-01 13:20:06 +08002120 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
Jike Songccd46db2016-12-01 13:20:06 +08002130 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{
Jike Songccd46db2016-12-01 13:20:06 +08002138 int ret;
2139
2140 ret = vfio_group_add_container_user(group);
2141 if (ret)
2142 return -EINVAL;
2143
Jike Songccd46db2016-12-01 13:20:06 +08002144 ret = blocking_notifier_chain_unregister(&group->notifier, nb);
2145
Jike Songccd46db2016-12-01 13:20:06 +08002146 vfio_group_try_dissolve_container(group);
2147
2148 return ret;
2149}
2150
Jike Song22195cb2016-12-01 13:20:05 +08002151int 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;
Jike Songccd46db2016-12-01 13:20:06 +08002168 case VFIO_GROUP_NOTIFY:
2169 ret = vfio_register_group_notifier(group, events, nb);
2170 break;
Jike Song22195cb2016-12-01 13:20:05 +08002171 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;
Jike Songccd46db2016-12-01 13:20:06 +08002197 case VFIO_GROUP_NOTIFY:
2198 ret = vfio_unregister_group_notifier(group, nb);
2199 break;
Jike Song22195cb2016-12-01 13:20:05 +08002200 default:
2201 ret = -EINVAL;
2202 }
2203
Kirti Wankhedec086de812016-11-17 10:28:26 +05302204 vfio_group_put(group);
2205 return ret;
2206}
2207EXPORT_SYMBOL(vfio_unregister_notifier);
2208
Alex Williamsond7a8d5e2016-02-22 16:02:33 -07002209/**
Alex Williamsoncba33452012-07-31 08:16:22 -06002210 * 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
Alex Williamsond1099902013-12-19 10:17:13 -07002217static 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
Alex Williamsoncba33452012-07-31 08:16:22 -06002225static 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
Alex Williamsond1099902013-12-19 10:17:13 -07002236 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 */
Alex Williamsoncba33452012-07-31 08:16:22 -06002243 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
Alex Williamsond1099902013-12-19 10:17:13 -07002251 ret = alloc_chrdev_region(&vfio.group_devt, 0, MINORMASK, "vfio");
Alex Williamsoncba33452012-07-31 08:16:22 -06002252 if (ret)
Alex Williamsond1099902013-12-19 10:17:13 -07002253 goto err_alloc_chrdev;
Alex Williamsoncba33452012-07-31 08:16:22 -06002254
Alex Williamsoncba33452012-07-31 08:16:22 -06002255 cdev_init(&vfio.group_cdev, &vfio_group_fops);
Alex Williamsond1099902013-12-19 10:17:13 -07002256 ret = cdev_add(&vfio.group_cdev, vfio.group_devt, MINORMASK);
Alex Williamsoncba33452012-07-31 08:16:22 -06002257 if (ret)
Alex Williamsond1099902013-12-19 10:17:13 -07002258 goto err_cdev_add;
Alex Williamsoncba33452012-07-31 08:16:22 -06002259
2260 pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
2261
Alex Williamson03a76b62015-12-21 15:13:33 -07002262#ifdef CONFIG_VFIO_NOIOMMU
2263 vfio_register_iommu_driver(&vfio_noiommu_ops);
2264#endif
Alex Williamsoncba33452012-07-31 08:16:22 -06002265 return 0;
2266
Alex Williamsond1099902013-12-19 10:17:13 -07002267err_cdev_add:
2268 unregister_chrdev_region(vfio.group_devt, MINORMASK);
2269err_alloc_chrdev:
Alex Williamsoncba33452012-07-31 08:16:22 -06002270 class_destroy(vfio.class);
2271 vfio.class = NULL;
2272err_class:
Alex Williamsond1099902013-12-19 10:17:13 -07002273 misc_deregister(&vfio_dev);
Alex Williamsoncba33452012-07-31 08:16:22 -06002274 return ret;
2275}
2276
2277static void __exit vfio_cleanup(void)
2278{
2279 WARN_ON(!list_empty(&vfio.group_list));
2280
Alex Williamson03a76b62015-12-21 15:13:33 -07002281#ifdef CONFIG_VFIO_NOIOMMU
2282 vfio_unregister_iommu_driver(&vfio_noiommu_ops);
2283#endif
Alex Williamsoncba33452012-07-31 08:16:22 -06002284 idr_destroy(&vfio.group_idr);
2285 cdev_del(&vfio.group_cdev);
Alex Williamsond1099902013-12-19 10:17:13 -07002286 unregister_chrdev_region(vfio.group_devt, MINORMASK);
Alex Williamsoncba33452012-07-31 08:16:22 -06002287 class_destroy(vfio.class);
2288 vfio.class = NULL;
Alex Williamsond1099902013-12-19 10:17:13 -07002289 misc_deregister(&vfio_dev);
Alex Williamsoncba33452012-07-31 08:16:22 -06002290}
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);
Alex Williamsond1099902013-12-19 10:17:13 -07002299MODULE_ALIAS_MISCDEV(VFIO_MINOR);
2300MODULE_ALIAS("devname:vfio/vfio");
Alex Williamson0ca582f2017-02-08 13:13:26 -07002301MODULE_SOFTDEP("post: vfio_iommu_type1 vfio_iommu_spapr_tce");