1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
6 * Author: Alex Williamson <alex.williamson@redhat.com>
8 * Derived from original vfio:
9 * Copyright 2010 Cisco Systems, Inc. All rights reserved.
10 * Author: Tom Lyon, pugs@cisco.com
13 #include <linux/vfio.h>
14 #include <linux/iommufd.h>
15 #include <linux/anon_inodes.h>
20 struct list_head group_list;
21 struct mutex group_lock; /* locks group_list */
26 static struct vfio_device *vfio_device_get_from_name(struct vfio_group *group,
29 struct vfio_device *it, *device = ERR_PTR(-ENODEV);
31 mutex_lock(&group->device_lock);
32 list_for_each_entry(it, &group->device_list, group_next) {
36 ret = it->ops->match(it, buf);
38 device = ERR_PTR(ret);
42 ret = !strcmp(dev_name(it->dev), buf);
45 if (ret && vfio_device_try_get_registration(it)) {
50 mutex_unlock(&group->device_lock);
56 * VFIO Group fd, /dev/vfio/$GROUP
58 static bool vfio_group_has_iommu(struct vfio_group *group)
60 lockdep_assert_held(&group->group_lock);
62 * There can only be users if there is a container, and if there is a
63 * container there must be users.
65 WARN_ON(!group->container != !group->container_users);
67 return group->container || group->iommufd;
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.
76 static int vfio_group_ioctl_unset_container(struct vfio_group *group)
80 mutex_lock(&group->group_lock);
81 if (!vfio_group_has_iommu(group)) {
85 if (group->container) {
86 if (group->container_users != 1) {
90 vfio_group_detach_container(group);
93 iommufd_ctx_put(group->iommufd);
94 group->iommufd = NULL;
98 mutex_unlock(&group->group_lock);
102 static int vfio_group_ioctl_set_container(struct vfio_group *group,
105 struct vfio_container *container;
106 struct iommufd_ctx *iommufd;
111 if (get_user(fd, arg))
118 mutex_lock(&group->group_lock);
119 if (vfio_group_has_iommu(group)) {
123 if (!group->iommu_group) {
128 container = vfio_container_from_file(f.file);
130 ret = vfio_container_attach_group(container, group);
134 iommufd = iommufd_ctx_from_file(f.file);
135 if (!IS_ERR(iommufd)) {
138 ret = iommufd_vfio_compat_ioas_id(iommufd, &ioas_id);
140 iommufd_ctx_put(group->iommufd);
144 group->iommufd = iommufd;
148 /* The FD passed is not recognized. */
152 mutex_unlock(&group->group_lock);
157 static int vfio_device_group_open(struct vfio_device *device)
161 mutex_lock(&device->group->group_lock);
162 if (!vfio_group_has_iommu(device->group)) {
168 * Here we pass the KVM pointer with the group under the lock. If the
169 * device driver will use it, it must obtain a reference and release it
170 * during close_device.
172 ret = vfio_device_open(device, device->group->iommufd,
176 mutex_unlock(&device->group->group_lock);
180 void vfio_device_group_close(struct vfio_device *device)
182 mutex_lock(&device->group->group_lock);
183 vfio_device_close(device, device->group->iommufd);
184 mutex_unlock(&device->group->group_lock);
187 static struct file *vfio_device_open_file(struct vfio_device *device)
192 ret = vfio_device_group_open(device);
197 * We can't use anon_inode_getfd() because we need to modify
198 * the f_mode flags directly to allow more than just ioctls
200 filep = anon_inode_getfile("[vfio-device]", &vfio_device_fops,
203 ret = PTR_ERR(filep);
204 goto err_close_device;
208 * TODO: add an anon_inode interface to do this.
209 * Appears to be missing by lack of need rather than
210 * explicitly prevented. Now there's need.
212 filep->f_mode |= (FMODE_PREAD | FMODE_PWRITE);
214 if (device->group->type == VFIO_NO_IOMMU)
215 dev_warn(device->dev, "vfio-noiommu device opened by user "
216 "(%s:%d)\n", current->comm, task_pid_nr(current));
218 * On success the ref of device is moved to the file and
219 * put in vfio_device_fops_release()
224 vfio_device_group_close(device);
229 static int vfio_group_ioctl_get_device_fd(struct vfio_group *group,
232 struct vfio_device *device;
238 buf = strndup_user(arg, PAGE_SIZE);
242 device = vfio_device_get_from_name(group, buf);
245 return PTR_ERR(device);
247 fdno = get_unused_fd_flags(O_CLOEXEC);
253 filep = vfio_device_open_file(device);
255 ret = PTR_ERR(filep);
259 fd_install(fdno, filep);
265 vfio_device_put_registration(device);
269 static int vfio_group_ioctl_get_status(struct vfio_group *group,
270 struct vfio_group_status __user *arg)
272 unsigned long minsz = offsetofend(struct vfio_group_status, flags);
273 struct vfio_group_status status;
275 if (copy_from_user(&status, arg, minsz))
278 if (status.argsz < minsz)
283 mutex_lock(&group->group_lock);
284 if (!group->iommu_group) {
285 mutex_unlock(&group->group_lock);
290 * With the container FD the iommu_group_claim_dma_owner() is done
291 * during SET_CONTAINER but for IOMMFD this is done during
292 * VFIO_GROUP_GET_DEVICE_FD. Meaning that with iommufd
293 * VFIO_GROUP_FLAGS_VIABLE could be set but GET_DEVICE_FD will fail due
296 if (vfio_group_has_iommu(group))
297 status.flags |= VFIO_GROUP_FLAGS_CONTAINER_SET |
298 VFIO_GROUP_FLAGS_VIABLE;
299 else if (!iommu_group_dma_owner_claimed(group->iommu_group))
300 status.flags |= VFIO_GROUP_FLAGS_VIABLE;
301 mutex_unlock(&group->group_lock);
303 if (copy_to_user(arg, &status, minsz))
308 static long vfio_group_fops_unl_ioctl(struct file *filep,
309 unsigned int cmd, unsigned long arg)
311 struct vfio_group *group = filep->private_data;
312 void __user *uarg = (void __user *)arg;
315 case VFIO_GROUP_GET_DEVICE_FD:
316 return vfio_group_ioctl_get_device_fd(group, uarg);
317 case VFIO_GROUP_GET_STATUS:
318 return vfio_group_ioctl_get_status(group, uarg);
319 case VFIO_GROUP_SET_CONTAINER:
320 return vfio_group_ioctl_set_container(group, uarg);
321 case VFIO_GROUP_UNSET_CONTAINER:
322 return vfio_group_ioctl_unset_container(group);
328 static int vfio_group_fops_open(struct inode *inode, struct file *filep)
330 struct vfio_group *group =
331 container_of(inode->i_cdev, struct vfio_group, cdev);
334 mutex_lock(&group->group_lock);
337 * drivers can be zero if this races with vfio_device_remove_group(), it
338 * will be stable at 0 under the group rwsem
340 if (refcount_read(&group->drivers) == 0) {
345 if (group->type == VFIO_NO_IOMMU && !capable(CAP_SYS_RAWIO)) {
351 * Do we need multiple instances of the group open? Seems not.
353 if (group->opened_file) {
357 group->opened_file = filep;
358 filep->private_data = group;
361 mutex_unlock(&group->group_lock);
365 static int vfio_group_fops_release(struct inode *inode, struct file *filep)
367 struct vfio_group *group = filep->private_data;
369 filep->private_data = NULL;
371 mutex_lock(&group->group_lock);
373 * Device FDs hold a group file reference, therefore the group release
374 * is only called when there are no open devices.
376 WARN_ON(group->notifier.head);
377 if (group->container)
378 vfio_group_detach_container(group);
379 if (group->iommufd) {
380 iommufd_ctx_put(group->iommufd);
381 group->iommufd = NULL;
383 group->opened_file = NULL;
384 mutex_unlock(&group->group_lock);
388 static const struct file_operations vfio_group_fops = {
389 .owner = THIS_MODULE,
390 .unlocked_ioctl = vfio_group_fops_unl_ioctl,
391 .compat_ioctl = compat_ptr_ioctl,
392 .open = vfio_group_fops_open,
393 .release = vfio_group_fops_release,
397 * Group objects - create, release, get, put, search
399 static struct vfio_group *
400 vfio_group_find_from_iommu(struct iommu_group *iommu_group)
402 struct vfio_group *group;
404 lockdep_assert_held(&vfio.group_lock);
407 * group->iommu_group from the vfio.group_list cannot be NULL
408 * under the vfio.group_lock.
410 list_for_each_entry(group, &vfio.group_list, vfio_next) {
411 if (group->iommu_group == iommu_group)
417 static void vfio_group_release(struct device *dev)
419 struct vfio_group *group = container_of(dev, struct vfio_group, dev);
421 mutex_destroy(&group->device_lock);
422 mutex_destroy(&group->group_lock);
423 WARN_ON(group->iommu_group);
424 ida_free(&vfio.group_ida, MINOR(group->dev.devt));
428 static struct vfio_group *vfio_group_alloc(struct iommu_group *iommu_group,
429 enum vfio_group_type type)
431 struct vfio_group *group;
434 group = kzalloc(sizeof(*group), GFP_KERNEL);
436 return ERR_PTR(-ENOMEM);
438 minor = ida_alloc_max(&vfio.group_ida, MINORMASK, GFP_KERNEL);
441 return ERR_PTR(minor);
444 device_initialize(&group->dev);
445 group->dev.devt = MKDEV(MAJOR(vfio.group_devt), minor);
446 group->dev.class = vfio.class;
447 group->dev.release = vfio_group_release;
448 cdev_init(&group->cdev, &vfio_group_fops);
449 group->cdev.owner = THIS_MODULE;
451 refcount_set(&group->drivers, 1);
452 mutex_init(&group->group_lock);
453 INIT_LIST_HEAD(&group->device_list);
454 mutex_init(&group->device_lock);
455 group->iommu_group = iommu_group;
456 /* put in vfio_group_release() */
457 iommu_group_ref_get(iommu_group);
459 BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
464 static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group,
465 enum vfio_group_type type)
467 struct vfio_group *group;
468 struct vfio_group *ret;
471 lockdep_assert_held(&vfio.group_lock);
473 group = vfio_group_alloc(iommu_group, type);
477 err = dev_set_name(&group->dev, "%s%d",
478 group->type == VFIO_NO_IOMMU ? "noiommu-" : "",
479 iommu_group_id(iommu_group));
485 err = cdev_device_add(&group->cdev, &group->dev);
491 list_add(&group->vfio_next, &vfio.group_list);
496 put_device(&group->dev);
500 static struct vfio_group *vfio_noiommu_group_alloc(struct device *dev,
501 enum vfio_group_type type)
503 struct iommu_group *iommu_group;
504 struct vfio_group *group;
507 iommu_group = iommu_group_alloc();
508 if (IS_ERR(iommu_group))
509 return ERR_CAST(iommu_group);
511 ret = iommu_group_set_name(iommu_group, "vfio-noiommu");
514 ret = iommu_group_add_device(iommu_group, dev);
518 mutex_lock(&vfio.group_lock);
519 group = vfio_create_group(iommu_group, type);
520 mutex_unlock(&vfio.group_lock);
522 ret = PTR_ERR(group);
523 goto out_remove_device;
525 iommu_group_put(iommu_group);
529 iommu_group_remove_device(dev);
531 iommu_group_put(iommu_group);
535 static bool vfio_group_has_device(struct vfio_group *group, struct device *dev)
537 struct vfio_device *device;
539 mutex_lock(&group->device_lock);
540 list_for_each_entry(device, &group->device_list, group_next) {
541 if (device->dev == dev) {
542 mutex_unlock(&group->device_lock);
546 mutex_unlock(&group->device_lock);
550 static struct vfio_group *vfio_group_find_or_alloc(struct device *dev)
552 struct iommu_group *iommu_group;
553 struct vfio_group *group;
555 iommu_group = iommu_group_get(dev);
556 if (!iommu_group && vfio_noiommu) {
558 * With noiommu enabled, create an IOMMU group for devices that
559 * don't already have one, implying no IOMMU hardware/driver
560 * exists. Taint the kernel because we're about to give a DMA
561 * capable device to a user without IOMMU protection.
563 group = vfio_noiommu_group_alloc(dev, VFIO_NO_IOMMU);
564 if (!IS_ERR(group)) {
565 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
566 dev_warn(dev, "Adding kernel taint for vfio-noiommu group on device\n");
572 return ERR_PTR(-EINVAL);
575 * VFIO always sets IOMMU_CACHE because we offer no way for userspace to
576 * restore cache coherency. It has to be checked here because it is only
577 * valid for cases where we are using iommu groups.
579 if (!device_iommu_capable(dev, IOMMU_CAP_CACHE_COHERENCY)) {
580 iommu_group_put(iommu_group);
581 return ERR_PTR(-EINVAL);
584 mutex_lock(&vfio.group_lock);
585 group = vfio_group_find_from_iommu(iommu_group);
587 if (WARN_ON(vfio_group_has_device(group, dev)))
588 group = ERR_PTR(-EINVAL);
590 refcount_inc(&group->drivers);
592 group = vfio_create_group(iommu_group, VFIO_IOMMU);
594 mutex_unlock(&vfio.group_lock);
596 /* The vfio_group holds a reference to the iommu_group */
597 iommu_group_put(iommu_group);
601 int vfio_device_set_group(struct vfio_device *device,
602 enum vfio_group_type type)
604 struct vfio_group *group;
606 if (type == VFIO_IOMMU)
607 group = vfio_group_find_or_alloc(device->dev);
609 group = vfio_noiommu_group_alloc(device->dev, type);
612 return PTR_ERR(group);
614 /* Our reference on group is moved to the device */
615 device->group = group;
619 void vfio_device_remove_group(struct vfio_device *device)
621 struct vfio_group *group = device->group;
622 struct iommu_group *iommu_group;
624 if (group->type == VFIO_NO_IOMMU || group->type == VFIO_EMULATED_IOMMU)
625 iommu_group_remove_device(device->dev);
627 /* Pairs with vfio_create_group() / vfio_group_get_from_iommu() */
628 if (!refcount_dec_and_mutex_lock(&group->drivers, &vfio.group_lock))
630 list_del(&group->vfio_next);
633 * We could concurrently probe another driver in the group that might
634 * race vfio_device_remove_group() with vfio_get_group(), so we have to
635 * ensure that the sysfs is all cleaned up under lock otherwise the
636 * cdev_device_add() will fail due to the name aready existing.
638 cdev_device_del(&group->cdev, &group->dev);
640 mutex_lock(&group->group_lock);
642 * These data structures all have paired operations that can only be
643 * undone when the caller holds a live reference on the device. Since
644 * all pairs must be undone these WARN_ON's indicate some caller did not
645 * properly hold the group reference.
647 WARN_ON(!list_empty(&group->device_list));
648 WARN_ON(group->notifier.head);
651 * Revoke all users of group->iommu_group. At this point we know there
652 * are no devices active because we are unplugging the last one. Setting
653 * iommu_group to NULL blocks all new users.
655 if (group->container)
656 vfio_group_detach_container(group);
657 iommu_group = group->iommu_group;
658 group->iommu_group = NULL;
659 mutex_unlock(&group->group_lock);
660 mutex_unlock(&vfio.group_lock);
662 iommu_group_put(iommu_group);
663 put_device(&group->dev);
666 void vfio_device_group_register(struct vfio_device *device)
668 mutex_lock(&device->group->device_lock);
669 list_add(&device->group_next, &device->group->device_list);
670 mutex_unlock(&device->group->device_lock);
673 void vfio_device_group_unregister(struct vfio_device *device)
675 mutex_lock(&device->group->device_lock);
676 list_del(&device->group_next);
677 mutex_unlock(&device->group->device_lock);
680 int vfio_device_group_use_iommu(struct vfio_device *device)
682 struct vfio_group *group = device->group;
685 lockdep_assert_held(&group->group_lock);
687 if (WARN_ON(!group->container))
690 ret = vfio_group_use_container(group);
693 vfio_device_container_register(device);
697 void vfio_device_group_unuse_iommu(struct vfio_device *device)
699 struct vfio_group *group = device->group;
701 lockdep_assert_held(&group->group_lock);
703 if (WARN_ON(!group->container))
706 vfio_device_container_unregister(device);
707 vfio_group_unuse_container(group);
710 bool vfio_device_has_container(struct vfio_device *device)
712 return device->group->container;
716 * vfio_file_iommu_group - Return the struct iommu_group for the vfio group file
717 * @file: VFIO group file
719 * The returned iommu_group is valid as long as a ref is held on the file. This
720 * returns a reference on the group. This function is deprecated, only the SPAPR
721 * path in kvm should call it.
723 struct iommu_group *vfio_file_iommu_group(struct file *file)
725 struct vfio_group *group = file->private_data;
726 struct iommu_group *iommu_group = NULL;
728 if (!IS_ENABLED(CONFIG_SPAPR_TCE_IOMMU))
731 if (!vfio_file_is_group(file))
734 mutex_lock(&group->group_lock);
735 if (group->iommu_group) {
736 iommu_group = group->iommu_group;
737 iommu_group_ref_get(iommu_group);
739 mutex_unlock(&group->group_lock);
742 EXPORT_SYMBOL_GPL(vfio_file_iommu_group);
745 * vfio_file_is_group - True if the file is usable with VFIO aPIS
746 * @file: VFIO group file
748 bool vfio_file_is_group(struct file *file)
750 return file->f_op == &vfio_group_fops;
752 EXPORT_SYMBOL_GPL(vfio_file_is_group);
755 * vfio_file_enforced_coherent - True if the DMA associated with the VFIO file
756 * is always CPU cache coherent
757 * @file: VFIO group file
759 * Enforced coherency means that the IOMMU ignores things like the PCIe no-snoop
760 * bit in DMA transactions. A return of false indicates that the user has
761 * rights to access additional instructions such as wbinvd on x86.
763 bool vfio_file_enforced_coherent(struct file *file)
765 struct vfio_group *group = file->private_data;
766 struct vfio_device *device;
769 if (!vfio_file_is_group(file))
773 * If the device does not have IOMMU_CAP_ENFORCE_CACHE_COHERENCY then
774 * any domain later attached to it will also not support it. If the cap
775 * is set then the iommu_domain eventually attached to the device/group
776 * must use a domain with enforce_cache_coherency().
778 mutex_lock(&group->device_lock);
779 list_for_each_entry(device, &group->device_list, group_next) {
780 if (!device_iommu_capable(device->dev,
781 IOMMU_CAP_ENFORCE_CACHE_COHERENCY)) {
786 mutex_unlock(&group->device_lock);
789 EXPORT_SYMBOL_GPL(vfio_file_enforced_coherent);
792 * vfio_file_set_kvm - Link a kvm with VFIO drivers
793 * @file: VFIO group file
796 * When a VFIO device is first opened the KVM will be available in
797 * device->kvm if one was associated with the group.
799 void vfio_file_set_kvm(struct file *file, struct kvm *kvm)
801 struct vfio_group *group = file->private_data;
803 if (!vfio_file_is_group(file))
806 mutex_lock(&group->group_lock);
808 mutex_unlock(&group->group_lock);
810 EXPORT_SYMBOL_GPL(vfio_file_set_kvm);
813 * vfio_file_has_dev - True if the VFIO file is a handle for device
814 * @file: VFIO file to check
815 * @device: Device that must be part of the file
817 * Returns true if given file has permission to manipulate the given device.
819 bool vfio_file_has_dev(struct file *file, struct vfio_device *device)
821 struct vfio_group *group = file->private_data;
823 if (!vfio_file_is_group(file))
826 return group == device->group;
828 EXPORT_SYMBOL_GPL(vfio_file_has_dev);
830 static char *vfio_devnode(const struct device *dev, umode_t *mode)
832 return kasprintf(GFP_KERNEL, "vfio/%s", dev_name(dev));
835 int __init vfio_group_init(void)
839 ida_init(&vfio.group_ida);
840 mutex_init(&vfio.group_lock);
841 INIT_LIST_HEAD(&vfio.group_list);
843 ret = vfio_container_init();
847 /* /dev/vfio/$GROUP */
848 vfio.class = class_create(THIS_MODULE, "vfio");
849 if (IS_ERR(vfio.class)) {
850 ret = PTR_ERR(vfio.class);
851 goto err_group_class;
854 vfio.class->devnode = vfio_devnode;
856 ret = alloc_chrdev_region(&vfio.group_devt, 0, MINORMASK + 1, "vfio");
858 goto err_alloc_chrdev;
862 class_destroy(vfio.class);
865 vfio_container_cleanup();
869 void vfio_group_cleanup(void)
871 WARN_ON(!list_empty(&vfio.group_list));
872 ida_destroy(&vfio.group_ida);
873 unregister_chrdev_region(vfio.group_devt, MINORMASK + 1);
874 class_destroy(vfio.class);
876 vfio_container_cleanup();