xref: /linux/drivers/vfio/group.c (revision 7fc2cd2e4b398c57c9cf961cfea05eadbf34c05c)
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 			goto out_put_kvm;
198 		}
199 	}
200 
201 	ret = vfio_df_open(df);
202 	if (ret)
203 		goto out_put_kvm;
204 
205 	if (df->iommufd && device->open_count == 1) {
206 		ret = vfio_iommufd_compat_attach_ioas(device, df->iommufd);
207 		if (ret)
208 			goto out_close_device;
209 	}
210 
211 	/*
212 	 * Paired with smp_load_acquire() in vfio_device_fops::ioctl/
213 	 * read/write/mmap and vfio_file_has_device_access()
214 	 */
215 	smp_store_release(&df->access_granted, true);
216 
217 	mutex_unlock(&device->dev_set->lock);
218 	mutex_unlock(&device->group->group_lock);
219 	return 0;
220 
221 out_close_device:
222 	vfio_df_close(df);
223 out_put_kvm:
224 	df->iommufd = NULL;
225 	if (device->open_count == 0)
226 		vfio_device_put_kvm(device);
227 	mutex_unlock(&device->dev_set->lock);
228 out_unlock:
229 	mutex_unlock(&device->group->group_lock);
230 	return ret;
231 }
232 
233 void vfio_df_group_close(struct vfio_device_file *df)
234 {
235 	struct vfio_device *device = df->device;
236 
237 	mutex_lock(&device->group->group_lock);
238 	mutex_lock(&device->dev_set->lock);
239 
240 	vfio_df_close(df);
241 	df->iommufd = NULL;
242 
243 	if (device->open_count == 0)
244 		vfio_device_put_kvm(device);
245 
246 	mutex_unlock(&device->dev_set->lock);
247 	mutex_unlock(&device->group->group_lock);
248 }
249 
250 static struct file *vfio_device_open_file(struct vfio_device *device)
251 {
252 	struct vfio_device_file *df;
253 	struct file *filep;
254 	int ret;
255 
256 	df = vfio_allocate_device_file(device);
257 	if (IS_ERR(df)) {
258 		ret = PTR_ERR(df);
259 		goto err_out;
260 	}
261 
262 	df->group = device->group;
263 
264 	ret = vfio_df_group_open(df);
265 	if (ret)
266 		goto err_free;
267 
268 	filep = anon_inode_getfile_fmode("[vfio-device]", &vfio_device_fops,
269 				   df, O_RDWR, FMODE_PREAD | FMODE_PWRITE);
270 	if (IS_ERR(filep)) {
271 		ret = PTR_ERR(filep);
272 		goto err_close_device;
273 	}
274 	/*
275 	 * Use the pseudo fs inode on the device to link all mmaps
276 	 * to the same address space, allowing us to unmap all vmas
277 	 * associated to this device using unmap_mapping_range().
278 	 */
279 	filep->f_mapping = device->inode->i_mapping;
280 
281 	if (device->group->type == VFIO_NO_IOMMU)
282 		dev_warn(device->dev, "vfio-noiommu device opened by user "
283 			 "(%s:%d)\n", current->comm, task_pid_nr(current));
284 	/*
285 	 * On success the ref of device is moved to the file and
286 	 * put in vfio_device_fops_release()
287 	 */
288 	return filep;
289 
290 err_close_device:
291 	vfio_df_group_close(df);
292 err_free:
293 	kfree(df);
294 err_out:
295 	return ERR_PTR(ret);
296 }
297 
298 static int vfio_group_ioctl_get_device_fd(struct vfio_group *group,
299 					  char __user *arg)
300 {
301 	struct vfio_device *device;
302 	char *buf;
303 	int fd;
304 
305 	buf = strndup_user(arg, PAGE_SIZE);
306 	if (IS_ERR(buf))
307 		return PTR_ERR(buf);
308 
309 	device = vfio_device_get_from_name(group, buf);
310 	kfree(buf);
311 	if (IS_ERR(device))
312 		return PTR_ERR(device);
313 
314 	fd = FD_ADD(O_CLOEXEC, vfio_device_open_file(device));
315 	if (fd < 0)
316 		vfio_device_put_registration(device);
317 	return fd;
318 }
319 
320 static int vfio_group_ioctl_get_status(struct vfio_group *group,
321 				       struct vfio_group_status __user *arg)
322 {
323 	unsigned long minsz = offsetofend(struct vfio_group_status, flags);
324 	struct vfio_group_status status;
325 
326 	if (copy_from_user(&status, arg, minsz))
327 		return -EFAULT;
328 
329 	if (status.argsz < minsz)
330 		return -EINVAL;
331 
332 	status.flags = 0;
333 
334 	mutex_lock(&group->group_lock);
335 	if (!group->iommu_group) {
336 		mutex_unlock(&group->group_lock);
337 		return -ENODEV;
338 	}
339 
340 	/*
341 	 * With the container FD the iommu_group_claim_dma_owner() is done
342 	 * during SET_CONTAINER but for IOMMFD this is done during
343 	 * VFIO_GROUP_GET_DEVICE_FD. Meaning that with iommufd
344 	 * VFIO_GROUP_FLAGS_VIABLE could be set but GET_DEVICE_FD will fail due
345 	 * to viability.
346 	 */
347 	if (vfio_group_has_iommu(group))
348 		status.flags |= VFIO_GROUP_FLAGS_CONTAINER_SET |
349 				VFIO_GROUP_FLAGS_VIABLE;
350 	else if (!iommu_group_dma_owner_claimed(group->iommu_group))
351 		status.flags |= VFIO_GROUP_FLAGS_VIABLE;
352 	mutex_unlock(&group->group_lock);
353 
354 	if (copy_to_user(arg, &status, minsz))
355 		return -EFAULT;
356 	return 0;
357 }
358 
359 static long vfio_group_fops_unl_ioctl(struct file *filep,
360 				      unsigned int cmd, unsigned long arg)
361 {
362 	struct vfio_group *group = filep->private_data;
363 	void __user *uarg = (void __user *)arg;
364 
365 	switch (cmd) {
366 	case VFIO_GROUP_GET_DEVICE_FD:
367 		return vfio_group_ioctl_get_device_fd(group, uarg);
368 	case VFIO_GROUP_GET_STATUS:
369 		return vfio_group_ioctl_get_status(group, uarg);
370 	case VFIO_GROUP_SET_CONTAINER:
371 		return vfio_group_ioctl_set_container(group, uarg);
372 	case VFIO_GROUP_UNSET_CONTAINER:
373 		return vfio_group_ioctl_unset_container(group);
374 	default:
375 		return -ENOTTY;
376 	}
377 }
378 
379 int vfio_device_block_group(struct vfio_device *device)
380 {
381 	struct vfio_group *group = device->group;
382 	int ret = 0;
383 
384 	mutex_lock(&group->group_lock);
385 	if (group->opened_file) {
386 		ret = -EBUSY;
387 		goto out_unlock;
388 	}
389 
390 	group->cdev_device_open_cnt++;
391 
392 out_unlock:
393 	mutex_unlock(&group->group_lock);
394 	return ret;
395 }
396 
397 void vfio_device_unblock_group(struct vfio_device *device)
398 {
399 	struct vfio_group *group = device->group;
400 
401 	mutex_lock(&group->group_lock);
402 	group->cdev_device_open_cnt--;
403 	mutex_unlock(&group->group_lock);
404 }
405 
406 static int vfio_group_fops_open(struct inode *inode, struct file *filep)
407 {
408 	struct vfio_group *group =
409 		container_of(inode->i_cdev, struct vfio_group, cdev);
410 	int ret;
411 
412 	mutex_lock(&group->group_lock);
413 
414 	/*
415 	 * drivers can be zero if this races with vfio_device_remove_group(), it
416 	 * will be stable at 0 under the group rwsem
417 	 */
418 	if (refcount_read(&group->drivers) == 0) {
419 		ret = -ENODEV;
420 		goto out_unlock;
421 	}
422 
423 	if (group->type == VFIO_NO_IOMMU && !capable(CAP_SYS_RAWIO)) {
424 		ret = -EPERM;
425 		goto out_unlock;
426 	}
427 
428 	if (group->cdev_device_open_cnt) {
429 		ret = -EBUSY;
430 		goto out_unlock;
431 	}
432 
433 	/*
434 	 * Do we need multiple instances of the group open?  Seems not.
435 	 */
436 	if (group->opened_file) {
437 		ret = -EBUSY;
438 		goto out_unlock;
439 	}
440 	group->opened_file = filep;
441 	filep->private_data = group;
442 	ret = 0;
443 out_unlock:
444 	mutex_unlock(&group->group_lock);
445 	return ret;
446 }
447 
448 static int vfio_group_fops_release(struct inode *inode, struct file *filep)
449 {
450 	struct vfio_group *group = filep->private_data;
451 
452 	filep->private_data = NULL;
453 
454 	mutex_lock(&group->group_lock);
455 	/*
456 	 * Device FDs hold a group file reference, therefore the group release
457 	 * is only called when there are no open devices.
458 	 */
459 	WARN_ON(group->notifier.head);
460 	if (group->container)
461 		vfio_group_detach_container(group);
462 	if (group->iommufd) {
463 		iommufd_ctx_put(group->iommufd);
464 		group->iommufd = NULL;
465 	}
466 	group->opened_file = NULL;
467 	mutex_unlock(&group->group_lock);
468 	return 0;
469 }
470 
471 static const struct file_operations vfio_group_fops = {
472 	.owner		= THIS_MODULE,
473 	.unlocked_ioctl	= vfio_group_fops_unl_ioctl,
474 	.compat_ioctl	= compat_ptr_ioctl,
475 	.open		= vfio_group_fops_open,
476 	.release	= vfio_group_fops_release,
477 };
478 
479 /*
480  * Group objects - create, release, get, put, search
481  */
482 static struct vfio_group *
483 vfio_group_find_from_iommu(struct iommu_group *iommu_group)
484 {
485 	struct vfio_group *group;
486 
487 	lockdep_assert_held(&vfio.group_lock);
488 
489 	/*
490 	 * group->iommu_group from the vfio.group_list cannot be NULL
491 	 * under the vfio.group_lock.
492 	 */
493 	list_for_each_entry(group, &vfio.group_list, vfio_next) {
494 		if (group->iommu_group == iommu_group)
495 			return group;
496 	}
497 	return NULL;
498 }
499 
500 static void vfio_group_release(struct device *dev)
501 {
502 	struct vfio_group *group = container_of(dev, struct vfio_group, dev);
503 
504 	mutex_destroy(&group->device_lock);
505 	mutex_destroy(&group->group_lock);
506 	WARN_ON(group->iommu_group);
507 	WARN_ON(group->cdev_device_open_cnt);
508 	ida_free(&vfio.group_ida, MINOR(group->dev.devt));
509 	kfree(group);
510 }
511 
512 static struct vfio_group *vfio_group_alloc(struct iommu_group *iommu_group,
513 					   enum vfio_group_type type)
514 {
515 	struct vfio_group *group;
516 	int minor;
517 
518 	group = kzalloc(sizeof(*group), GFP_KERNEL);
519 	if (!group)
520 		return ERR_PTR(-ENOMEM);
521 
522 	minor = ida_alloc_max(&vfio.group_ida, MINORMASK, GFP_KERNEL);
523 	if (minor < 0) {
524 		kfree(group);
525 		return ERR_PTR(minor);
526 	}
527 
528 	device_initialize(&group->dev);
529 	group->dev.devt = MKDEV(MAJOR(vfio.group_devt), minor);
530 	group->dev.class = vfio.class;
531 	group->dev.release = vfio_group_release;
532 	cdev_init(&group->cdev, &vfio_group_fops);
533 	group->cdev.owner = THIS_MODULE;
534 
535 	refcount_set(&group->drivers, 1);
536 	mutex_init(&group->group_lock);
537 	spin_lock_init(&group->kvm_ref_lock);
538 	INIT_LIST_HEAD(&group->device_list);
539 	mutex_init(&group->device_lock);
540 	group->iommu_group = iommu_group;
541 	/* put in vfio_group_release() */
542 	iommu_group_ref_get(iommu_group);
543 	group->type = type;
544 	BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
545 
546 	return group;
547 }
548 
549 static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group,
550 		enum vfio_group_type type)
551 {
552 	struct vfio_group *group;
553 	struct vfio_group *ret;
554 	int err;
555 
556 	lockdep_assert_held(&vfio.group_lock);
557 
558 	group = vfio_group_alloc(iommu_group, type);
559 	if (IS_ERR(group))
560 		return group;
561 
562 	err = dev_set_name(&group->dev, "%s%d",
563 			   group->type == VFIO_NO_IOMMU ? "noiommu-" : "",
564 			   iommu_group_id(iommu_group));
565 	if (err) {
566 		ret = ERR_PTR(err);
567 		goto err_put;
568 	}
569 
570 	err = cdev_device_add(&group->cdev, &group->dev);
571 	if (err) {
572 		ret = ERR_PTR(err);
573 		goto err_put;
574 	}
575 
576 	list_add(&group->vfio_next, &vfio.group_list);
577 
578 	return group;
579 
580 err_put:
581 	put_device(&group->dev);
582 	return ret;
583 }
584 
585 static struct vfio_group *vfio_noiommu_group_alloc(struct device *dev,
586 		enum vfio_group_type type)
587 {
588 	struct iommu_group *iommu_group;
589 	struct vfio_group *group;
590 	int ret;
591 
592 	iommu_group = iommu_group_alloc();
593 	if (IS_ERR(iommu_group))
594 		return ERR_CAST(iommu_group);
595 
596 	ret = iommu_group_set_name(iommu_group, "vfio-noiommu");
597 	if (ret)
598 		goto out_put_group;
599 	ret = iommu_group_add_device(iommu_group, dev);
600 	if (ret)
601 		goto out_put_group;
602 
603 	mutex_lock(&vfio.group_lock);
604 	group = vfio_create_group(iommu_group, type);
605 	mutex_unlock(&vfio.group_lock);
606 	if (IS_ERR(group)) {
607 		ret = PTR_ERR(group);
608 		goto out_remove_device;
609 	}
610 	iommu_group_put(iommu_group);
611 	return group;
612 
613 out_remove_device:
614 	iommu_group_remove_device(dev);
615 out_put_group:
616 	iommu_group_put(iommu_group);
617 	return ERR_PTR(ret);
618 }
619 
620 static bool vfio_group_has_device(struct vfio_group *group, struct device *dev)
621 {
622 	struct vfio_device *device;
623 
624 	mutex_lock(&group->device_lock);
625 	list_for_each_entry(device, &group->device_list, group_next) {
626 		if (device->dev == dev) {
627 			mutex_unlock(&group->device_lock);
628 			return true;
629 		}
630 	}
631 	mutex_unlock(&group->device_lock);
632 	return false;
633 }
634 
635 static struct vfio_group *vfio_group_find_or_alloc(struct device *dev)
636 {
637 	struct iommu_group *iommu_group;
638 	struct vfio_group *group;
639 
640 	iommu_group = iommu_group_get(dev);
641 	if (!iommu_group && vfio_noiommu) {
642 		/*
643 		 * With noiommu enabled, create an IOMMU group for devices that
644 		 * don't already have one, implying no IOMMU hardware/driver
645 		 * exists.  Taint the kernel because we're about to give a DMA
646 		 * capable device to a user without IOMMU protection.
647 		 */
648 		group = vfio_noiommu_group_alloc(dev, VFIO_NO_IOMMU);
649 		if (!IS_ERR(group)) {
650 			add_taint(TAINT_USER, LOCKDEP_STILL_OK);
651 			dev_warn(dev, "Adding kernel taint for vfio-noiommu group on device\n");
652 		}
653 		return group;
654 	}
655 
656 	if (!iommu_group)
657 		return ERR_PTR(-EINVAL);
658 
659 	mutex_lock(&vfio.group_lock);
660 	group = vfio_group_find_from_iommu(iommu_group);
661 	if (group) {
662 		if (WARN_ON(vfio_group_has_device(group, dev)))
663 			group = ERR_PTR(-EINVAL);
664 		else
665 			refcount_inc(&group->drivers);
666 	} else {
667 		group = vfio_create_group(iommu_group, VFIO_IOMMU);
668 	}
669 	mutex_unlock(&vfio.group_lock);
670 
671 	/* The vfio_group holds a reference to the iommu_group */
672 	iommu_group_put(iommu_group);
673 	return group;
674 }
675 
676 int vfio_device_set_group(struct vfio_device *device,
677 			  enum vfio_group_type type)
678 {
679 	struct vfio_group *group;
680 
681 	if (type == VFIO_IOMMU)
682 		group = vfio_group_find_or_alloc(device->dev);
683 	else
684 		group = vfio_noiommu_group_alloc(device->dev, type);
685 
686 	if (IS_ERR(group))
687 		return PTR_ERR(group);
688 
689 	/* Our reference on group is moved to the device */
690 	device->group = group;
691 	return 0;
692 }
693 
694 void vfio_device_remove_group(struct vfio_device *device)
695 {
696 	struct vfio_group *group = device->group;
697 	struct iommu_group *iommu_group;
698 
699 	if (group->type == VFIO_NO_IOMMU || group->type == VFIO_EMULATED_IOMMU)
700 		iommu_group_remove_device(device->dev);
701 
702 	/* Pairs with vfio_create_group() / vfio_group_get_from_iommu() */
703 	if (!refcount_dec_and_mutex_lock(&group->drivers, &vfio.group_lock))
704 		return;
705 	list_del(&group->vfio_next);
706 
707 	/*
708 	 * We could concurrently probe another driver in the group that might
709 	 * race vfio_device_remove_group() with vfio_get_group(), so we have to
710 	 * ensure that the sysfs is all cleaned up under lock otherwise the
711 	 * cdev_device_add() will fail due to the name aready existing.
712 	 */
713 	cdev_device_del(&group->cdev, &group->dev);
714 
715 	mutex_lock(&group->group_lock);
716 	/*
717 	 * These data structures all have paired operations that can only be
718 	 * undone when the caller holds a live reference on the device. Since
719 	 * all pairs must be undone these WARN_ON's indicate some caller did not
720 	 * properly hold the group reference.
721 	 */
722 	WARN_ON(!list_empty(&group->device_list));
723 	WARN_ON(group->notifier.head);
724 
725 	/*
726 	 * Revoke all users of group->iommu_group. At this point we know there
727 	 * are no devices active because we are unplugging the last one. Setting
728 	 * iommu_group to NULL blocks all new users.
729 	 */
730 	if (group->container)
731 		vfio_group_detach_container(group);
732 	iommu_group = group->iommu_group;
733 	group->iommu_group = NULL;
734 	mutex_unlock(&group->group_lock);
735 	mutex_unlock(&vfio.group_lock);
736 
737 	iommu_group_put(iommu_group);
738 	put_device(&group->dev);
739 }
740 
741 void vfio_device_group_register(struct vfio_device *device)
742 {
743 	mutex_lock(&device->group->device_lock);
744 	list_add(&device->group_next, &device->group->device_list);
745 	mutex_unlock(&device->group->device_lock);
746 }
747 
748 void vfio_device_group_unregister(struct vfio_device *device)
749 {
750 	mutex_lock(&device->group->device_lock);
751 	list_del(&device->group_next);
752 	mutex_unlock(&device->group->device_lock);
753 }
754 
755 int vfio_device_group_use_iommu(struct vfio_device *device)
756 {
757 	struct vfio_group *group = device->group;
758 	int ret = 0;
759 
760 	lockdep_assert_held(&group->group_lock);
761 
762 	if (WARN_ON(!group->container))
763 		return -EINVAL;
764 
765 	ret = vfio_group_use_container(group);
766 	if (ret)
767 		return ret;
768 	vfio_device_container_register(device);
769 	return 0;
770 }
771 
772 void vfio_device_group_unuse_iommu(struct vfio_device *device)
773 {
774 	struct vfio_group *group = device->group;
775 
776 	lockdep_assert_held(&group->group_lock);
777 
778 	if (WARN_ON(!group->container))
779 		return;
780 
781 	vfio_device_container_unregister(device);
782 	vfio_group_unuse_container(group);
783 }
784 
785 bool vfio_device_has_container(struct vfio_device *device)
786 {
787 	return device->group->container;
788 }
789 
790 struct vfio_group *vfio_group_from_file(struct file *file)
791 {
792 	struct vfio_group *group = file->private_data;
793 
794 	if (file->f_op != &vfio_group_fops)
795 		return NULL;
796 	return group;
797 }
798 
799 /**
800  * vfio_file_iommu_group - Return the struct iommu_group for the vfio group file
801  * @file: VFIO group file
802  *
803  * The returned iommu_group is valid as long as a ref is held on the file. This
804  * returns a reference on the group. This function is deprecated, only the SPAPR
805  * path in kvm should call it.
806  */
807 struct iommu_group *vfio_file_iommu_group(struct file *file)
808 {
809 	struct vfio_group *group = vfio_group_from_file(file);
810 	struct iommu_group *iommu_group = NULL;
811 
812 	if (!IS_ENABLED(CONFIG_SPAPR_TCE_IOMMU))
813 		return NULL;
814 
815 	if (!group)
816 		return NULL;
817 
818 	mutex_lock(&group->group_lock);
819 	if (group->iommu_group) {
820 		iommu_group = group->iommu_group;
821 		iommu_group_ref_get(iommu_group);
822 	}
823 	mutex_unlock(&group->group_lock);
824 	return iommu_group;
825 }
826 EXPORT_SYMBOL_GPL(vfio_file_iommu_group);
827 
828 /**
829  * vfio_file_is_group - True if the file is a vfio group file
830  * @file: VFIO group file
831  */
832 bool vfio_file_is_group(struct file *file)
833 {
834 	return vfio_group_from_file(file);
835 }
836 EXPORT_SYMBOL_GPL(vfio_file_is_group);
837 
838 bool vfio_group_enforced_coherent(struct vfio_group *group)
839 {
840 	struct vfio_device *device;
841 	bool ret = true;
842 
843 	/*
844 	 * If the device does not have IOMMU_CAP_ENFORCE_CACHE_COHERENCY then
845 	 * any domain later attached to it will also not support it. If the cap
846 	 * is set then the iommu_domain eventually attached to the device/group
847 	 * must use a domain with enforce_cache_coherency().
848 	 */
849 	mutex_lock(&group->device_lock);
850 	list_for_each_entry(device, &group->device_list, group_next) {
851 		if (!device_iommu_capable(device->dev,
852 					  IOMMU_CAP_ENFORCE_CACHE_COHERENCY)) {
853 			ret = false;
854 			break;
855 		}
856 	}
857 	mutex_unlock(&group->device_lock);
858 	return ret;
859 }
860 
861 void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm)
862 {
863 	spin_lock(&group->kvm_ref_lock);
864 	group->kvm = kvm;
865 	spin_unlock(&group->kvm_ref_lock);
866 }
867 
868 /**
869  * vfio_file_has_dev - True if the VFIO file is a handle for device
870  * @file: VFIO file to check
871  * @device: Device that must be part of the file
872  *
873  * Returns true if given file has permission to manipulate the given device.
874  */
875 bool vfio_file_has_dev(struct file *file, struct vfio_device *device)
876 {
877 	struct vfio_group *group = vfio_group_from_file(file);
878 
879 	if (!group)
880 		return false;
881 
882 	return group == device->group;
883 }
884 EXPORT_SYMBOL_GPL(vfio_file_has_dev);
885 
886 static char *vfio_devnode(const struct device *dev, umode_t *mode)
887 {
888 	return kasprintf(GFP_KERNEL, "vfio/%s", dev_name(dev));
889 }
890 
891 int __init vfio_group_init(void)
892 {
893 	int ret;
894 
895 	ida_init(&vfio.group_ida);
896 	mutex_init(&vfio.group_lock);
897 	INIT_LIST_HEAD(&vfio.group_list);
898 
899 	ret = vfio_container_init();
900 	if (ret)
901 		return ret;
902 
903 	/* /dev/vfio/$GROUP */
904 	vfio.class = class_create("vfio");
905 	if (IS_ERR(vfio.class)) {
906 		ret = PTR_ERR(vfio.class);
907 		goto err_group_class;
908 	}
909 
910 	vfio.class->devnode = vfio_devnode;
911 
912 	ret = alloc_chrdev_region(&vfio.group_devt, 0, MINORMASK + 1, "vfio");
913 	if (ret)
914 		goto err_alloc_chrdev;
915 	return 0;
916 
917 err_alloc_chrdev:
918 	class_destroy(vfio.class);
919 	vfio.class = NULL;
920 err_group_class:
921 	vfio_container_cleanup();
922 	return ret;
923 }
924 
925 void vfio_group_cleanup(void)
926 {
927 	WARN_ON(!list_empty(&vfio.group_list));
928 	ida_destroy(&vfio.group_ida);
929 	unregister_chrdev_region(vfio.group_devt, MINORMASK + 1);
930 	class_destroy(vfio.class);
931 	vfio.class = NULL;
932 	vfio_container_cleanup();
933 }
934