1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * VFIO core
4  *
5  * Copyright (C) 2012 Red Hat, Inc.  All rights reserved.
6  *     Author: Alex Williamson <alex.williamson@redhat.com>
7  *
8  * Derived from original vfio:
9  * Copyright 2010 Cisco Systems, Inc.  All rights reserved.
10  * Author: Tom Lyon, pugs@cisco.com
11  */
12 
13 #include <linux/vfio.h>
14 #include <linux/iommufd.h>
15 #include <linux/anon_inodes.h>
16 #include "vfio.h"
17 
18 static struct vfio {
19 	struct class			*class;
20 	struct list_head		group_list;
21 	struct mutex			group_lock; /* locks group_list */
22 	struct ida			group_ida;
23 	dev_t				group_devt;
24 } vfio;
25 
26 static struct vfio_device *vfio_device_get_from_name(struct vfio_group *group,
27 						     char *buf)
28 {
29 	struct vfio_device *it, *device = ERR_PTR(-ENODEV);
30 
31 	mutex_lock(&group->device_lock);
32 	list_for_each_entry(it, &group->device_list, group_next) {
33 		int ret;
34 
35 		if (it->ops->match) {
36 			ret = it->ops->match(it, buf);
37 			if (ret < 0) {
38 				device = ERR_PTR(ret);
39 				break;
40 			}
41 		} else {
42 			ret = !strcmp(dev_name(it->dev), buf);
43 		}
44 
45 		if (ret && vfio_device_try_get_registration(it)) {
46 			device = it;
47 			break;
48 		}
49 	}
50 	mutex_unlock(&group->device_lock);
51 
52 	return device;
53 }
54 
55 /*
56  * VFIO Group fd, /dev/vfio/$GROUP
57  */
58 static bool vfio_group_has_iommu(struct vfio_group *group)
59 {
60 	lockdep_assert_held(&group->group_lock);
61 	/*
62 	 * There can only be users if there is a container, and if there is a
63 	 * container there must be users.
64 	 */
65 	WARN_ON(!group->container != !group->container_users);
66 
67 	return group->container || group->iommufd;
68 }
69 
70 /*
71  * VFIO_GROUP_UNSET_CONTAINER should fail if there are other users or
72  * if there was no container to unset.  Since the ioctl is called on
73  * the group, we know that still exists, therefore the only valid
74  * transition here is 1->0.
75  */
76 static int vfio_group_ioctl_unset_container(struct vfio_group *group)
77 {
78 	int ret = 0;
79 
80 	mutex_lock(&group->group_lock);
81 	if (!vfio_group_has_iommu(group)) {
82 		ret = -EINVAL;
83 		goto out_unlock;
84 	}
85 	if (group->container) {
86 		if (group->container_users != 1) {
87 			ret = -EBUSY;
88 			goto out_unlock;
89 		}
90 		vfio_group_detach_container(group);
91 	}
92 	if (group->iommufd) {
93 		iommufd_ctx_put(group->iommufd);
94 		group->iommufd = NULL;
95 	}
96 
97 out_unlock:
98 	mutex_unlock(&group->group_lock);
99 	return ret;
100 }
101 
102 static int vfio_group_ioctl_set_container(struct vfio_group *group,
103 					  int __user *arg)
104 {
105 	struct vfio_container *container;
106 	struct iommufd_ctx *iommufd;
107 	int ret;
108 	int fd;
109 
110 	if (get_user(fd, arg))
111 		return -EFAULT;
112 
113 	CLASS(fd, f)(fd);
114 	if (fd_empty(f))
115 		return -EBADF;
116 
117 	mutex_lock(&group->group_lock);
118 	if (vfio_group_has_iommu(group)) {
119 		ret = -EINVAL;
120 		goto out_unlock;
121 	}
122 	if (!group->iommu_group) {
123 		ret = -ENODEV;
124 		goto out_unlock;
125 	}
126 
127 	container = vfio_container_from_file(fd_file(f));
128 	if (container) {
129 		ret = vfio_container_attach_group(container, group);
130 		goto out_unlock;
131 	}
132 
133 	iommufd = iommufd_ctx_from_file(fd_file(f));
134 	if (!IS_ERR(iommufd)) {
135 		if (IS_ENABLED(CONFIG_VFIO_NOIOMMU) &&
136 		    group->type == VFIO_NO_IOMMU)
137 			ret = iommufd_vfio_compat_set_no_iommu(iommufd);
138 		else
139 			ret = iommufd_vfio_compat_ioas_create(iommufd);
140 
141 		if (ret) {
142 			iommufd_ctx_put(iommufd);
143 			goto out_unlock;
144 		}
145 
146 		group->iommufd = iommufd;
147 		goto out_unlock;
148 	}
149 
150 	/* The FD passed is not recognized. */
151 	ret = -EBADFD;
152 
153 out_unlock:
154 	mutex_unlock(&group->group_lock);
155 	return ret;
156 }
157 
158 static void vfio_device_group_get_kvm_safe(struct vfio_device *device)
159 {
160 	spin_lock(&device->group->kvm_ref_lock);
161 	vfio_device_get_kvm_safe(device, device->group->kvm);
162 	spin_unlock(&device->group->kvm_ref_lock);
163 }
164 
165 static int vfio_df_group_open(struct vfio_device_file *df)
166 {
167 	struct vfio_device *device = df->device;
168 	int ret;
169 
170 	mutex_lock(&device->group->group_lock);
171 	if (!vfio_group_has_iommu(device->group)) {
172 		ret = -EINVAL;
173 		goto out_unlock;
174 	}
175 
176 	mutex_lock(&device->dev_set->lock);
177 
178 	/*
179 	 * Before the first device open, get the KVM pointer currently
180 	 * associated with the group (if there is one) and obtain a reference
181 	 * now that will be held until the open_count reaches 0 again.  Save
182 	 * the pointer in the device for use by drivers.
183 	 */
184 	if (device->open_count == 0)
185 		vfio_device_group_get_kvm_safe(device);
186 
187 	df->iommufd = device->group->iommufd;
188 	if (df->iommufd && vfio_device_is_noiommu(device) && device->open_count == 0) {
189 		/*
190 		 * Require no compat ioas to be assigned to proceed.  The basic
191 		 * statement is that the user cannot have done something that
192 		 * implies they expected translation to exist
193 		 */
194 		if (!capable(CAP_SYS_RAWIO) ||
195 		    vfio_iommufd_device_has_compat_ioas(device, df->iommufd))
196 			ret = -EPERM;
197 		else
198 			ret = 0;
199 		goto out_put_kvm;
200 	}
201 
202 	ret = vfio_df_open(df);
203 	if (ret)
204 		goto out_put_kvm;
205 
206 	if (df->iommufd && device->open_count == 1) {
207 		ret = vfio_iommufd_compat_attach_ioas(device, df->iommufd);
208 		if (ret)
209 			goto out_close_device;
210 	}
211 
212 	/*
213 	 * Paired with smp_load_acquire() in vfio_device_fops::ioctl/
214 	 * read/write/mmap and vfio_file_has_device_access()
215 	 */
216 	smp_store_release(&df->access_granted, true);
217 
218 	mutex_unlock(&device->dev_set->lock);
219 	mutex_unlock(&device->group->group_lock);
220 	return 0;
221 
222 out_close_device:
223 	vfio_df_close(df);
224 out_put_kvm:
225 	df->iommufd = NULL;
226 	if (device->open_count == 0)
227 		vfio_device_put_kvm(device);
228 	mutex_unlock(&device->dev_set->lock);
229 out_unlock:
230 	mutex_unlock(&device->group->group_lock);
231 	return ret;
232 }
233 
234 void vfio_df_group_close(struct vfio_device_file *df)
235 {
236 	struct vfio_device *device = df->device;
237 
238 	mutex_lock(&device->group->group_lock);
239 	mutex_lock(&device->dev_set->lock);
240 
241 	vfio_df_close(df);
242 	df->iommufd = NULL;
243 
244 	if (device->open_count == 0)
245 		vfio_device_put_kvm(device);
246 
247 	mutex_unlock(&device->dev_set->lock);
248 	mutex_unlock(&device->group->group_lock);
249 }
250 
251 static struct file *vfio_device_open_file(struct vfio_device *device)
252 {
253 	struct vfio_device_file *df;
254 	struct file *filep;
255 	int ret;
256 
257 	df = vfio_allocate_device_file(device);
258 	if (IS_ERR(df)) {
259 		ret = PTR_ERR(df);
260 		goto err_out;
261 	}
262 
263 	df->group = device->group;
264 
265 	ret = vfio_df_group_open(df);
266 	if (ret)
267 		goto err_free;
268 
269 	filep = anon_inode_getfile_fmode("[vfio-device]", &vfio_device_fops,
270 				   df, O_RDWR, FMODE_PREAD | FMODE_PWRITE);
271 	if (IS_ERR(filep)) {
272 		ret = PTR_ERR(filep);
273 		goto err_close_device;
274 	}
275 	/*
276 	 * Use the pseudo fs inode on the device to link all mmaps
277 	 * to the same address space, allowing us to unmap all vmas
278 	 * associated to this device using unmap_mapping_range().
279 	 */
280 	filep->f_mapping = device->inode->i_mapping;
281 
282 	if (device->group->type == VFIO_NO_IOMMU)
283 		dev_warn(device->dev, "vfio-noiommu device opened by user "
284 			 "(%s:%d)\n", current->comm, task_pid_nr(current));
285 	/*
286 	 * On success the ref of device is moved to the file and
287 	 * put in vfio_device_fops_release()
288 	 */
289 	return filep;
290 
291 err_close_device:
292 	vfio_df_group_close(df);
293 err_free:
294 	kfree(df);
295 err_out:
296 	return ERR_PTR(ret);
297 }
298 
299 static int vfio_group_ioctl_get_device_fd(struct vfio_group *group,
300 					  char __user *arg)
301 {
302 	struct vfio_device *device;
303 	struct file *filep;
304 	char *buf;
305 	int fdno;
306 	int ret;
307 
308 	buf = strndup_user(arg, PAGE_SIZE);
309 	if (IS_ERR(buf))
310 		return PTR_ERR(buf);
311 
312 	device = vfio_device_get_from_name(group, buf);
313 	kfree(buf);
314 	if (IS_ERR(device))
315 		return PTR_ERR(device);
316 
317 	fdno = get_unused_fd_flags(O_CLOEXEC);
318 	if (fdno < 0) {
319 		ret = fdno;
320 		goto err_put_device;
321 	}
322 
323 	filep = vfio_device_open_file(device);
324 	if (IS_ERR(filep)) {
325 		ret = PTR_ERR(filep);
326 		goto err_put_fdno;
327 	}
328 
329 	fd_install(fdno, filep);
330 	return fdno;
331 
332 err_put_fdno:
333 	put_unused_fd(fdno);
334 err_put_device:
335 	vfio_device_put_registration(device);
336 	return ret;
337 }
338 
339 static int vfio_group_ioctl_get_status(struct vfio_group *group,
340 				       struct vfio_group_status __user *arg)
341 {
342 	unsigned long minsz = offsetofend(struct vfio_group_status, flags);
343 	struct vfio_group_status status;
344 
345 	if (copy_from_user(&status, arg, minsz))
346 		return -EFAULT;
347 
348 	if (status.argsz < minsz)
349 		return -EINVAL;
350 
351 	status.flags = 0;
352 
353 	mutex_lock(&group->group_lock);
354 	if (!group->iommu_group) {
355 		mutex_unlock(&group->group_lock);
356 		return -ENODEV;
357 	}
358 
359 	/*
360 	 * With the container FD the iommu_group_claim_dma_owner() is done
361 	 * during SET_CONTAINER but for IOMMFD this is done during
362 	 * VFIO_GROUP_GET_DEVICE_FD. Meaning that with iommufd
363 	 * VFIO_GROUP_FLAGS_VIABLE could be set but GET_DEVICE_FD will fail due
364 	 * to viability.
365 	 */
366 	if (vfio_group_has_iommu(group))
367 		status.flags |= VFIO_GROUP_FLAGS_CONTAINER_SET |
368 				VFIO_GROUP_FLAGS_VIABLE;
369 	else if (!iommu_group_dma_owner_claimed(group->iommu_group))
370 		status.flags |= VFIO_GROUP_FLAGS_VIABLE;
371 	mutex_unlock(&group->group_lock);
372 
373 	if (copy_to_user(arg, &status, minsz))
374 		return -EFAULT;
375 	return 0;
376 }
377 
378 static long vfio_group_fops_unl_ioctl(struct file *filep,
379 				      unsigned int cmd, unsigned long arg)
380 {
381 	struct vfio_group *group = filep->private_data;
382 	void __user *uarg = (void __user *)arg;
383 
384 	switch (cmd) {
385 	case VFIO_GROUP_GET_DEVICE_FD:
386 		return vfio_group_ioctl_get_device_fd(group, uarg);
387 	case VFIO_GROUP_GET_STATUS:
388 		return vfio_group_ioctl_get_status(group, uarg);
389 	case VFIO_GROUP_SET_CONTAINER:
390 		return vfio_group_ioctl_set_container(group, uarg);
391 	case VFIO_GROUP_UNSET_CONTAINER:
392 		return vfio_group_ioctl_unset_container(group);
393 	default:
394 		return -ENOTTY;
395 	}
396 }
397 
398 int vfio_device_block_group(struct vfio_device *device)
399 {
400 	struct vfio_group *group = device->group;
401 	int ret = 0;
402 
403 	mutex_lock(&group->group_lock);
404 	if (group->opened_file) {
405 		ret = -EBUSY;
406 		goto out_unlock;
407 	}
408 
409 	group->cdev_device_open_cnt++;
410 
411 out_unlock:
412 	mutex_unlock(&group->group_lock);
413 	return ret;
414 }
415 
416 void vfio_device_unblock_group(struct vfio_device *device)
417 {
418 	struct vfio_group *group = device->group;
419 
420 	mutex_lock(&group->group_lock);
421 	group->cdev_device_open_cnt--;
422 	mutex_unlock(&group->group_lock);
423 }
424 
425 static int vfio_group_fops_open(struct inode *inode, struct file *filep)
426 {
427 	struct vfio_group *group =
428 		container_of(inode->i_cdev, struct vfio_group, cdev);
429 	int ret;
430 
431 	mutex_lock(&group->group_lock);
432 
433 	/*
434 	 * drivers can be zero if this races with vfio_device_remove_group(), it
435 	 * will be stable at 0 under the group rwsem
436 	 */
437 	if (refcount_read(&group->drivers) == 0) {
438 		ret = -ENODEV;
439 		goto out_unlock;
440 	}
441 
442 	if (group->type == VFIO_NO_IOMMU && !capable(CAP_SYS_RAWIO)) {
443 		ret = -EPERM;
444 		goto out_unlock;
445 	}
446 
447 	if (group->cdev_device_open_cnt) {
448 		ret = -EBUSY;
449 		goto out_unlock;
450 	}
451 
452 	/*
453 	 * Do we need multiple instances of the group open?  Seems not.
454 	 */
455 	if (group->opened_file) {
456 		ret = -EBUSY;
457 		goto out_unlock;
458 	}
459 	group->opened_file = filep;
460 	filep->private_data = group;
461 	ret = 0;
462 out_unlock:
463 	mutex_unlock(&group->group_lock);
464 	return ret;
465 }
466 
467 static int vfio_group_fops_release(struct inode *inode, struct file *filep)
468 {
469 	struct vfio_group *group = filep->private_data;
470 
471 	filep->private_data = NULL;
472 
473 	mutex_lock(&group->group_lock);
474 	/*
475 	 * Device FDs hold a group file reference, therefore the group release
476 	 * is only called when there are no open devices.
477 	 */
478 	WARN_ON(group->notifier.head);
479 	if (group->container)
480 		vfio_group_detach_container(group);
481 	if (group->iommufd) {
482 		iommufd_ctx_put(group->iommufd);
483 		group->iommufd = NULL;
484 	}
485 	group->opened_file = NULL;
486 	mutex_unlock(&group->group_lock);
487 	return 0;
488 }
489 
490 static const struct file_operations vfio_group_fops = {
491 	.owner		= THIS_MODULE,
492 	.unlocked_ioctl	= vfio_group_fops_unl_ioctl,
493 	.compat_ioctl	= compat_ptr_ioctl,
494 	.open		= vfio_group_fops_open,
495 	.release	= vfio_group_fops_release,
496 };
497 
498 /*
499  * Group objects - create, release, get, put, search
500  */
501 static struct vfio_group *
502 vfio_group_find_from_iommu(struct iommu_group *iommu_group)
503 {
504 	struct vfio_group *group;
505 
506 	lockdep_assert_held(&vfio.group_lock);
507 
508 	/*
509 	 * group->iommu_group from the vfio.group_list cannot be NULL
510 	 * under the vfio.group_lock.
511 	 */
512 	list_for_each_entry(group, &vfio.group_list, vfio_next) {
513 		if (group->iommu_group == iommu_group)
514 			return group;
515 	}
516 	return NULL;
517 }
518 
519 static void vfio_group_release(struct device *dev)
520 {
521 	struct vfio_group *group = container_of(dev, struct vfio_group, dev);
522 
523 	mutex_destroy(&group->device_lock);
524 	mutex_destroy(&group->group_lock);
525 	WARN_ON(group->iommu_group);
526 	WARN_ON(group->cdev_device_open_cnt);
527 	ida_free(&vfio.group_ida, MINOR(group->dev.devt));
528 	kfree(group);
529 }
530 
531 static struct vfio_group *vfio_group_alloc(struct iommu_group *iommu_group,
532 					   enum vfio_group_type type)
533 {
534 	struct vfio_group *group;
535 	int minor;
536 
537 	group = kzalloc(sizeof(*group), GFP_KERNEL);
538 	if (!group)
539 		return ERR_PTR(-ENOMEM);
540 
541 	minor = ida_alloc_max(&vfio.group_ida, MINORMASK, GFP_KERNEL);
542 	if (minor < 0) {
543 		kfree(group);
544 		return ERR_PTR(minor);
545 	}
546 
547 	device_initialize(&group->dev);
548 	group->dev.devt = MKDEV(MAJOR(vfio.group_devt), minor);
549 	group->dev.class = vfio.class;
550 	group->dev.release = vfio_group_release;
551 	cdev_init(&group->cdev, &vfio_group_fops);
552 	group->cdev.owner = THIS_MODULE;
553 
554 	refcount_set(&group->drivers, 1);
555 	mutex_init(&group->group_lock);
556 	spin_lock_init(&group->kvm_ref_lock);
557 	INIT_LIST_HEAD(&group->device_list);
558 	mutex_init(&group->device_lock);
559 	group->iommu_group = iommu_group;
560 	/* put in vfio_group_release() */
561 	iommu_group_ref_get(iommu_group);
562 	group->type = type;
563 	BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
564 
565 	return group;
566 }
567 
568 static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group,
569 		enum vfio_group_type type)
570 {
571 	struct vfio_group *group;
572 	struct vfio_group *ret;
573 	int err;
574 
575 	lockdep_assert_held(&vfio.group_lock);
576 
577 	group = vfio_group_alloc(iommu_group, type);
578 	if (IS_ERR(group))
579 		return group;
580 
581 	err = dev_set_name(&group->dev, "%s%d",
582 			   group->type == VFIO_NO_IOMMU ? "noiommu-" : "",
583 			   iommu_group_id(iommu_group));
584 	if (err) {
585 		ret = ERR_PTR(err);
586 		goto err_put;
587 	}
588 
589 	err = cdev_device_add(&group->cdev, &group->dev);
590 	if (err) {
591 		ret = ERR_PTR(err);
592 		goto err_put;
593 	}
594 
595 	list_add(&group->vfio_next, &vfio.group_list);
596 
597 	return group;
598 
599 err_put:
600 	put_device(&group->dev);
601 	return ret;
602 }
603 
604 static struct vfio_group *vfio_noiommu_group_alloc(struct device *dev,
605 		enum vfio_group_type type)
606 {
607 	struct iommu_group *iommu_group;
608 	struct vfio_group *group;
609 	int ret;
610 
611 	iommu_group = iommu_group_alloc();
612 	if (IS_ERR(iommu_group))
613 		return ERR_CAST(iommu_group);
614 
615 	ret = iommu_group_set_name(iommu_group, "vfio-noiommu");
616 	if (ret)
617 		goto out_put_group;
618 	ret = iommu_group_add_device(iommu_group, dev);
619 	if (ret)
620 		goto out_put_group;
621 
622 	mutex_lock(&vfio.group_lock);
623 	group = vfio_create_group(iommu_group, type);
624 	mutex_unlock(&vfio.group_lock);
625 	if (IS_ERR(group)) {
626 		ret = PTR_ERR(group);
627 		goto out_remove_device;
628 	}
629 	iommu_group_put(iommu_group);
630 	return group;
631 
632 out_remove_device:
633 	iommu_group_remove_device(dev);
634 out_put_group:
635 	iommu_group_put(iommu_group);
636 	return ERR_PTR(ret);
637 }
638 
639 static bool vfio_group_has_device(struct vfio_group *group, struct device *dev)
640 {
641 	struct vfio_device *device;
642 
643 	mutex_lock(&group->device_lock);
644 	list_for_each_entry(device, &group->device_list, group_next) {
645 		if (device->dev == dev) {
646 			mutex_unlock(&group->device_lock);
647 			return true;
648 		}
649 	}
650 	mutex_unlock(&group->device_lock);
651 	return false;
652 }
653 
654 static struct vfio_group *vfio_group_find_or_alloc(struct device *dev)
655 {
656 	struct iommu_group *iommu_group;
657 	struct vfio_group *group;
658 
659 	iommu_group = iommu_group_get(dev);
660 	if (!iommu_group && vfio_noiommu) {
661 		/*
662 		 * With noiommu enabled, create an IOMMU group for devices that
663 		 * don't already have one, implying no IOMMU hardware/driver
664 		 * exists.  Taint the kernel because we're about to give a DMA
665 		 * capable device to a user without IOMMU protection.
666 		 */
667 		group = vfio_noiommu_group_alloc(dev, VFIO_NO_IOMMU);
668 		if (!IS_ERR(group)) {
669 			add_taint(TAINT_USER, LOCKDEP_STILL_OK);
670 			dev_warn(dev, "Adding kernel taint for vfio-noiommu group on device\n");
671 		}
672 		return group;
673 	}
674 
675 	if (!iommu_group)
676 		return ERR_PTR(-EINVAL);
677 
678 	mutex_lock(&vfio.group_lock);
679 	group = vfio_group_find_from_iommu(iommu_group);
680 	if (group) {
681 		if (WARN_ON(vfio_group_has_device(group, dev)))
682 			group = ERR_PTR(-EINVAL);
683 		else
684 			refcount_inc(&group->drivers);
685 	} else {
686 		group = vfio_create_group(iommu_group, VFIO_IOMMU);
687 	}
688 	mutex_unlock(&vfio.group_lock);
689 
690 	/* The vfio_group holds a reference to the iommu_group */
691 	iommu_group_put(iommu_group);
692 	return group;
693 }
694 
695 int vfio_device_set_group(struct vfio_device *device,
696 			  enum vfio_group_type type)
697 {
698 	struct vfio_group *group;
699 
700 	if (type == VFIO_IOMMU)
701 		group = vfio_group_find_or_alloc(device->dev);
702 	else
703 		group = vfio_noiommu_group_alloc(device->dev, type);
704 
705 	if (IS_ERR(group))
706 		return PTR_ERR(group);
707 
708 	/* Our reference on group is moved to the device */
709 	device->group = group;
710 	return 0;
711 }
712 
713 void vfio_device_remove_group(struct vfio_device *device)
714 {
715 	struct vfio_group *group = device->group;
716 	struct iommu_group *iommu_group;
717 
718 	if (group->type == VFIO_NO_IOMMU || group->type == VFIO_EMULATED_IOMMU)
719 		iommu_group_remove_device(device->dev);
720 
721 	/* Pairs with vfio_create_group() / vfio_group_get_from_iommu() */
722 	if (!refcount_dec_and_mutex_lock(&group->drivers, &vfio.group_lock))
723 		return;
724 	list_del(&group->vfio_next);
725 
726 	/*
727 	 * We could concurrently probe another driver in the group that might
728 	 * race vfio_device_remove_group() with vfio_get_group(), so we have to
729 	 * ensure that the sysfs is all cleaned up under lock otherwise the
730 	 * cdev_device_add() will fail due to the name aready existing.
731 	 */
732 	cdev_device_del(&group->cdev, &group->dev);
733 
734 	mutex_lock(&group->group_lock);
735 	/*
736 	 * These data structures all have paired operations that can only be
737 	 * undone when the caller holds a live reference on the device. Since
738 	 * all pairs must be undone these WARN_ON's indicate some caller did not
739 	 * properly hold the group reference.
740 	 */
741 	WARN_ON(!list_empty(&group->device_list));
742 	WARN_ON(group->notifier.head);
743 
744 	/*
745 	 * Revoke all users of group->iommu_group. At this point we know there
746 	 * are no devices active because we are unplugging the last one. Setting
747 	 * iommu_group to NULL blocks all new users.
748 	 */
749 	if (group->container)
750 		vfio_group_detach_container(group);
751 	iommu_group = group->iommu_group;
752 	group->iommu_group = NULL;
753 	mutex_unlock(&group->group_lock);
754 	mutex_unlock(&vfio.group_lock);
755 
756 	iommu_group_put(iommu_group);
757 	put_device(&group->dev);
758 }
759 
760 void vfio_device_group_register(struct vfio_device *device)
761 {
762 	mutex_lock(&device->group->device_lock);
763 	list_add(&device->group_next, &device->group->device_list);
764 	mutex_unlock(&device->group->device_lock);
765 }
766 
767 void vfio_device_group_unregister(struct vfio_device *device)
768 {
769 	mutex_lock(&device->group->device_lock);
770 	list_del(&device->group_next);
771 	mutex_unlock(&device->group->device_lock);
772 }
773 
774 int vfio_device_group_use_iommu(struct vfio_device *device)
775 {
776 	struct vfio_group *group = device->group;
777 	int ret = 0;
778 
779 	lockdep_assert_held(&group->group_lock);
780 
781 	if (WARN_ON(!group->container))
782 		return -EINVAL;
783 
784 	ret = vfio_group_use_container(group);
785 	if (ret)
786 		return ret;
787 	vfio_device_container_register(device);
788 	return 0;
789 }
790 
791 void vfio_device_group_unuse_iommu(struct vfio_device *device)
792 {
793 	struct vfio_group *group = device->group;
794 
795 	lockdep_assert_held(&group->group_lock);
796 
797 	if (WARN_ON(!group->container))
798 		return;
799 
800 	vfio_device_container_unregister(device);
801 	vfio_group_unuse_container(group);
802 }
803 
804 bool vfio_device_has_container(struct vfio_device *device)
805 {
806 	return device->group->container;
807 }
808 
809 struct vfio_group *vfio_group_from_file(struct file *file)
810 {
811 	struct vfio_group *group = file->private_data;
812 
813 	if (file->f_op != &vfio_group_fops)
814 		return NULL;
815 	return group;
816 }
817 
818 /**
819  * vfio_file_iommu_group - Return the struct iommu_group for the vfio group file
820  * @file: VFIO group file
821  *
822  * The returned iommu_group is valid as long as a ref is held on the file. This
823  * returns a reference on the group. This function is deprecated, only the SPAPR
824  * path in kvm should call it.
825  */
826 struct iommu_group *vfio_file_iommu_group(struct file *file)
827 {
828 	struct vfio_group *group = vfio_group_from_file(file);
829 	struct iommu_group *iommu_group = NULL;
830 
831 	if (!IS_ENABLED(CONFIG_SPAPR_TCE_IOMMU))
832 		return NULL;
833 
834 	if (!group)
835 		return NULL;
836 
837 	mutex_lock(&group->group_lock);
838 	if (group->iommu_group) {
839 		iommu_group = group->iommu_group;
840 		iommu_group_ref_get(iommu_group);
841 	}
842 	mutex_unlock(&group->group_lock);
843 	return iommu_group;
844 }
845 EXPORT_SYMBOL_GPL(vfio_file_iommu_group);
846 
847 /**
848  * vfio_file_is_group - True if the file is a vfio group file
849  * @file: VFIO group file
850  */
851 bool vfio_file_is_group(struct file *file)
852 {
853 	return vfio_group_from_file(file);
854 }
855 EXPORT_SYMBOL_GPL(vfio_file_is_group);
856 
857 bool vfio_group_enforced_coherent(struct vfio_group *group)
858 {
859 	struct vfio_device *device;
860 	bool ret = true;
861 
862 	/*
863 	 * If the device does not have IOMMU_CAP_ENFORCE_CACHE_COHERENCY then
864 	 * any domain later attached to it will also not support it. If the cap
865 	 * is set then the iommu_domain eventually attached to the device/group
866 	 * must use a domain with enforce_cache_coherency().
867 	 */
868 	mutex_lock(&group->device_lock);
869 	list_for_each_entry(device, &group->device_list, group_next) {
870 		if (!device_iommu_capable(device->dev,
871 					  IOMMU_CAP_ENFORCE_CACHE_COHERENCY)) {
872 			ret = false;
873 			break;
874 		}
875 	}
876 	mutex_unlock(&group->device_lock);
877 	return ret;
878 }
879 
880 void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm)
881 {
882 	spin_lock(&group->kvm_ref_lock);
883 	group->kvm = kvm;
884 	spin_unlock(&group->kvm_ref_lock);
885 }
886 
887 /**
888  * vfio_file_has_dev - True if the VFIO file is a handle for device
889  * @file: VFIO file to check
890  * @device: Device that must be part of the file
891  *
892  * Returns true if given file has permission to manipulate the given device.
893  */
894 bool vfio_file_has_dev(struct file *file, struct vfio_device *device)
895 {
896 	struct vfio_group *group = vfio_group_from_file(file);
897 
898 	if (!group)
899 		return false;
900 
901 	return group == device->group;
902 }
903 EXPORT_SYMBOL_GPL(vfio_file_has_dev);
904 
905 static char *vfio_devnode(const struct device *dev, umode_t *mode)
906 {
907 	return kasprintf(GFP_KERNEL, "vfio/%s", dev_name(dev));
908 }
909 
910 int __init vfio_group_init(void)
911 {
912 	int ret;
913 
914 	ida_init(&vfio.group_ida);
915 	mutex_init(&vfio.group_lock);
916 	INIT_LIST_HEAD(&vfio.group_list);
917 
918 	ret = vfio_container_init();
919 	if (ret)
920 		return ret;
921 
922 	/* /dev/vfio/$GROUP */
923 	vfio.class = class_create("vfio");
924 	if (IS_ERR(vfio.class)) {
925 		ret = PTR_ERR(vfio.class);
926 		goto err_group_class;
927 	}
928 
929 	vfio.class->devnode = vfio_devnode;
930 
931 	ret = alloc_chrdev_region(&vfio.group_devt, 0, MINORMASK + 1, "vfio");
932 	if (ret)
933 		goto err_alloc_chrdev;
934 	return 0;
935 
936 err_alloc_chrdev:
937 	class_destroy(vfio.class);
938 	vfio.class = NULL;
939 err_group_class:
940 	vfio_container_cleanup();
941 	return ret;
942 }
943 
944 void vfio_group_cleanup(void)
945 {
946 	WARN_ON(!list_empty(&vfio.group_list));
947 	ida_destroy(&vfio.group_ida);
948 	unregister_chrdev_region(vfio.group_devt, MINORMASK + 1);
949 	class_destroy(vfio.class);
950 	vfio.class = NULL;
951 	vfio_container_cleanup();
952 }
953