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