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