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