// SPDX-License-Identifier: GPL-2.0-only /* * VFIO core * * Copyright (C) 2012 Red Hat, Inc. All rights reserved. * Author: Alex Williamson <alex.williamson@redhat.com> * * Derived from original vfio: * Copyright 2010 Cisco Systems, Inc. All rights reserved. * Author: Tom Lyon, pugs@cisco.com
*/
/* * VFIO Group fd, /dev/vfio/$GROUP
*/ staticbool vfio_group_has_iommu(struct vfio_group *group)
{
lockdep_assert_held(&group->group_lock); /* * There can only be users if there is a container, and if there is a * container there must be users.
*/
WARN_ON(!group->container != !group->container_users);
return group->container || group->iommufd;
}
/* * VFIO_GROUP_UNSET_CONTAINER should fail if there are other users or * if there was no container to unset. Since the ioctl is called on * the group, we know that still exists, therefore the only valid * transition here is 1->0.
*/ staticint vfio_group_ioctl_unset_container(struct vfio_group *group)
{ int ret = 0;
mutex_lock(&group->group_lock); if (!vfio_group_has_iommu(group)) {
ret = -EINVAL; goto out_unlock;
} if (group->container) { if (group->container_users != 1) {
ret = -EBUSY; goto out_unlock;
}
vfio_group_detach_container(group);
} if (group->iommufd) {
iommufd_ctx_put(group->iommufd);
group->iommufd = NULL;
}
staticint vfio_group_ioctl_set_container(struct vfio_group *group, int __user *arg)
{ struct vfio_container *container; struct iommufd_ctx *iommufd; int ret; int fd;
if (get_user(fd, arg)) return -EFAULT;
CLASS(fd, f)(fd); if (fd_empty(f)) return -EBADF;
mutex_lock(&group->group_lock); if (vfio_group_has_iommu(group)) {
ret = -EINVAL; goto out_unlock;
} if (!group->iommu_group) {
ret = -ENODEV; goto out_unlock;
}
container = vfio_container_from_file(fd_file(f)); if (container) {
ret = vfio_container_attach_group(container, group); goto out_unlock;
}
iommufd = iommufd_ctx_from_file(fd_file(f)); if (!IS_ERR(iommufd)) { if (IS_ENABLED(CONFIG_VFIO_NOIOMMU) &&
group->type == VFIO_NO_IOMMU)
ret = iommufd_vfio_compat_set_no_iommu(iommufd); else
ret = iommufd_vfio_compat_ioas_create(iommufd);
if (ret) {
iommufd_ctx_put(iommufd); goto out_unlock;
}
group->iommufd = iommufd; goto out_unlock;
}
/* The FD passed is not recognized. */
ret = -EBADFD;
mutex_lock(&device->group->group_lock); if (!vfio_group_has_iommu(device->group)) {
ret = -EINVAL; goto out_unlock;
}
mutex_lock(&device->dev_set->lock);
/* * Before the first device open, get the KVM pointer currently * associated with the group (if there is one) and obtain a reference * now that will be held until the open_count reaches 0 again. Save * the pointer in the device for use by drivers.
*/ if (device->open_count == 0)
vfio_device_group_get_kvm_safe(device);
df->iommufd = device->group->iommufd; if (df->iommufd && vfio_device_is_noiommu(device) && device->open_count == 0) { /* * Require no compat ioas to be assigned to proceed. The basic * statement is that the user cannot have done something that * implies they expected translation to exist
*/ if (!capable(CAP_SYS_RAWIO) ||
vfio_iommufd_device_has_compat_ioas(device, df->iommufd)) {
ret = -EPERM; goto out_put_kvm;
}
}
ret = vfio_df_open(df); if (ret) goto out_put_kvm;
if (df->iommufd && device->open_count == 1) {
ret = vfio_iommufd_compat_attach_ioas(device, df->iommufd); if (ret) goto out_close_device;
}
/* * Paired with smp_load_acquire() in vfio_device_fops::ioctl/ * read/write/mmap and vfio_file_has_device_access()
*/
smp_store_release(&df->access_granted, true);
df = vfio_allocate_device_file(device); if (IS_ERR(df)) {
ret = PTR_ERR(df); goto err_out;
}
df->group = device->group;
ret = vfio_df_group_open(df); if (ret) goto err_free;
filep = anon_inode_getfile_fmode("[vfio-device]", &vfio_device_fops,
df, O_RDWR, FMODE_PREAD | FMODE_PWRITE); if (IS_ERR(filep)) {
ret = PTR_ERR(filep); goto err_close_device;
} /* * Use the pseudo fs inode on the device to link all mmaps * to the same address space, allowing us to unmap all vmas * associated to this device using unmap_mapping_range().
*/
filep->f_mapping = device->inode->i_mapping;
if (device->group->type == VFIO_NO_IOMMU)
dev_warn(device->dev, "vfio-noiommu device opened by user " "(%s:%d)\n", current->comm, task_pid_nr(current)); /* * On success the ref of device is moved to the file and * put in vfio_device_fops_release()
*/ return filep;
if (copy_from_user(&status, arg, minsz)) return -EFAULT;
if (status.argsz < minsz) return -EINVAL;
status.flags = 0;
mutex_lock(&group->group_lock); if (!group->iommu_group) {
mutex_unlock(&group->group_lock); return -ENODEV;
}
/* * With the container FD the iommu_group_claim_dma_owner() is done * during SET_CONTAINER but for IOMMFD this is done during * VFIO_GROUP_GET_DEVICE_FD. Meaning that with iommufd * VFIO_GROUP_FLAGS_VIABLE could be set but GET_DEVICE_FD will fail due * to viability.
*/ if (vfio_group_has_iommu(group))
status.flags |= VFIO_GROUP_FLAGS_CONTAINER_SET |
VFIO_GROUP_FLAGS_VIABLE; elseif (!iommu_group_dma_owner_claimed(group->iommu_group))
status.flags |= VFIO_GROUP_FLAGS_VIABLE;
mutex_unlock(&group->group_lock);
if (copy_to_user(arg, &status, minsz)) return -EFAULT; return 0;
}
/* * drivers can be zero if this races with vfio_device_remove_group(), it * will be stable at 0 under the group rwsem
*/ if (refcount_read(&group->drivers) == 0) {
ret = -ENODEV; goto out_unlock;
}
if (group->type == VFIO_NO_IOMMU && !capable(CAP_SYS_RAWIO)) {
ret = -EPERM; goto out_unlock;
}
if (group->cdev_device_open_cnt) {
ret = -EBUSY; goto out_unlock;
}
/* * Do we need multiple instances of the group open? Seems not.
*/ if (group->opened_file) {
ret = -EBUSY; goto out_unlock;
}
group->opened_file = filep;
filep->private_data = group;
ret = 0;
out_unlock:
mutex_unlock(&group->group_lock); return ret;
}
mutex_lock(&group->group_lock); /* * Device FDs hold a group file reference, therefore the group release * is only called when there are no open devices.
*/
WARN_ON(group->notifier.head); if (group->container)
vfio_group_detach_container(group); if (group->iommufd) {
iommufd_ctx_put(group->iommufd);
group->iommufd = NULL;
}
group->opened_file = NULL;
mutex_unlock(&group->group_lock); return 0;
}
/* * group->iommu_group from the vfio.group_list cannot be NULL * under the vfio.group_lock.
*/
list_for_each_entry(group, &vfio.group_list, vfio_next) { if (group->iommu_group == iommu_group) return group;
} return NULL;
}
iommu_group = iommu_group_alloc(); if (IS_ERR(iommu_group)) return ERR_CAST(iommu_group);
ret = iommu_group_set_name(iommu_group, "vfio-noiommu"); if (ret) goto out_put_group;
ret = iommu_group_add_device(iommu_group, dev); if (ret) goto out_put_group;
mutex_lock(&vfio.group_lock);
group = vfio_create_group(iommu_group, type);
mutex_unlock(&vfio.group_lock); if (IS_ERR(group)) {
ret = PTR_ERR(group); goto out_remove_device;
}
iommu_group_put(iommu_group); return group;
iommu_group = iommu_group_get(dev); if (!iommu_group && vfio_noiommu) { /* * With noiommu enabled, create an IOMMU group for devices that * don't already have one, implying no IOMMU hardware/driver * exists. Taint the kernel because we're about to give a DMA * capable device to a user without IOMMU protection.
*/
group = vfio_noiommu_group_alloc(dev, VFIO_NO_IOMMU); if (!IS_ERR(group)) {
add_taint(TAINT_USER, LOCKDEP_STILL_OK);
dev_warn(dev, "Adding kernel taint for vfio-noiommu group on device\n");
} return group;
}
if (!iommu_group) return ERR_PTR(-EINVAL);
mutex_lock(&vfio.group_lock);
group = vfio_group_find_from_iommu(iommu_group); if (group) { if (WARN_ON(vfio_group_has_device(group, dev)))
group = ERR_PTR(-EINVAL); else
refcount_inc(&group->drivers);
} else {
group = vfio_create_group(iommu_group, VFIO_IOMMU);
}
mutex_unlock(&vfio.group_lock);
/* The vfio_group holds a reference to the iommu_group */
iommu_group_put(iommu_group); return group;
}
if (group->type == VFIO_NO_IOMMU || group->type == VFIO_EMULATED_IOMMU)
iommu_group_remove_device(device->dev);
/* Pairs with vfio_create_group() / vfio_group_get_from_iommu() */ if (!refcount_dec_and_mutex_lock(&group->drivers, &vfio.group_lock)) return;
list_del(&group->vfio_next);
/* * We could concurrently probe another driver in the group that might * race vfio_device_remove_group() with vfio_get_group(), so we have to * ensure that the sysfs is all cleaned up under lock otherwise the * cdev_device_add() will fail due to the name aready existing.
*/
cdev_device_del(&group->cdev, &group->dev);
mutex_lock(&group->group_lock); /* * These data structures all have paired operations that can only be * undone when the caller holds a live reference on the device. Since * all pairs must be undone these WARN_ON's indicate some caller did not * properly hold the group reference.
*/
WARN_ON(!list_empty(&group->device_list));
WARN_ON(group->notifier.head);
/* * Revoke all users of group->iommu_group. At this point we know there * are no devices active because we are unplugging the last one. Setting * iommu_group to NULL blocks all new users.
*/ if (group->container)
vfio_group_detach_container(group);
iommu_group = group->iommu_group;
group->iommu_group = NULL;
mutex_unlock(&group->group_lock);
mutex_unlock(&vfio.group_lock);
if (file->f_op != &vfio_group_fops) return NULL; return group;
}
/** * vfio_file_iommu_group - Return the struct iommu_group for the vfio group file * @file: VFIO group file * * The returned iommu_group is valid as long as a ref is held on the file. This * returns a reference on the group. This function is deprecated, only the SPAPR * path in kvm should call it.
*/ struct iommu_group *vfio_file_iommu_group(struct file *file)
{ struct vfio_group *group = vfio_group_from_file(file); struct iommu_group *iommu_group = NULL;
if (!IS_ENABLED(CONFIG_SPAPR_TCE_IOMMU)) return NULL;
/** * vfio_file_is_group - True if the file is a vfio group file * @file: VFIO group file
*/ bool vfio_file_is_group(struct file *file)
{ return vfio_group_from_file(file);
}
EXPORT_SYMBOL_GPL(vfio_file_is_group);
/* * If the device does not have IOMMU_CAP_ENFORCE_CACHE_COHERENCY then * any domain later attached to it will also not support it. If the cap * is set then the iommu_domain eventually attached to the device/group * must use a domain with enforce_cache_coherency().
*/
mutex_lock(&group->device_lock);
list_for_each_entry(device, &group->device_list, group_next) { if (!device_iommu_capable(device->dev,
IOMMU_CAP_ENFORCE_CACHE_COHERENCY)) {
ret = false; break;
}
}
mutex_unlock(&group->device_lock); return ret;
}
/** * vfio_file_has_dev - True if the VFIO file is a handle for device * @file: VFIO file to check * @device: Device that must be part of the file * * Returns true if given file has permission to manipulate the given device.
*/ bool vfio_file_has_dev(struct file *file, struct vfio_device *device)
{ struct vfio_group *group = vfio_group_from_file(file);
if (!group) returnfalse;
return group == device->group;
}
EXPORT_SYMBOL_GPL(vfio_file_has_dev);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.