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