drivers/vfio/vfio_main.c
Source file repositories/reference/linux-study-clean/drivers/vfio/vfio_main.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/vfio/vfio_main.c- Extension
.c- Size
- 49745 bytes
- Lines
- 1858
- Domain
- Driver Families
- Bucket
- drivers/vfio
- Inferred role
- Driver Families: operation-table or driver-model contract
- Status
- pattern implementation candidate
Why This File Exists
Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Defines an operation table; this is where Linux turns generic core objects into subsystem-specific behavior.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/cdev.hlinux/compat.hlinux/device.hlinux/fs.hlinux/idr.hlinux/iommu.hlinux/kvm_host.hlinux/list.hlinux/miscdevice.hlinux/module.hlinux/mount.hlinux/mutex.hlinux/pci.hlinux/pseudo_fs.hlinux/rwsem.hlinux/sched.hlinux/seq_file.hlinux/slab.hlinux/stat.hlinux/string.hlinux/uaccess.hlinux/vfio.hlinux/wait.hlinux/sched/signal.hlinux/pm_runtime.hlinux/interval_tree.hlinux/iova_bitmap.hlinux/iommufd.hvfio.h
Detected Declarations
function vfio_assign_device_setfunction vfio_release_device_setfunction vfio_device_set_open_countfunction vfio_find_device_in_devsetfunction vfio_device_put_registrationfunction vfio_device_try_get_registrationfunction vfio_device_releasefunction vfio_alloc_devicefunction vfio_fs_init_fs_contextfunction vfio_init_devicefunction __vfio_register_devfunction vfio_register_group_devfunction vfio_register_emulated_iommu_devfunction vfio_unregister_group_devfunction vfio_device_get_kvm_safefunction vfio_device_put_kvmfunction vfio_assert_device_openfunction vfio_allocate_device_filefunction vfio_df_device_first_openfunction vfio_df_device_last_closefunction vfio_df_openfunction vfio_df_closefunction pm_runtime_resume_and_getfunction pm_runtime_putfunction vfio_device_fops_releasefunction vfio_mig_get_next_statefunction vfio_ioct_mig_return_fdfunction vfio_ioctl_device_feature_mig_device_statefunction vfio_ioctl_device_feature_migration_data_sizefunction vfio_ioctl_device_feature_migration_precopy_info_v2function vfio_ioctl_device_feature_migrationfunction vfio_combine_iova_rangesfunction vfio_ioctl_device_feature_logging_startfunction vfio_ioctl_device_feature_logging_stopfunction vfio_device_log_read_and_clearfunction vfio_ioctl_device_feature_logging_reportfunction vfio_ioctl_device_featurefunction vfio_get_region_infofunction vfio_device_fops_unl_ioctlfunction vfio_device_fops_readfunction vfio_device_fops_writefunction vfio_device_fops_mmapfunction vfio_device_show_fdinfofunction vfio_file_is_validfunction vfio_file_enforced_coherentfunction vfio_device_file_set_kvmfunction vfio_file_set_kvmfunction vfio_info_cap_shift
Annotated Snippet
const struct file_operations vfio_device_fops = {
.owner = THIS_MODULE,
.open = vfio_device_fops_cdev_open,
.release = vfio_device_fops_release,
.read = vfio_device_fops_read,
.write = vfio_device_fops_write,
.unlocked_ioctl = vfio_device_fops_unl_ioctl,
.compat_ioctl = compat_ptr_ioctl,
.mmap = vfio_device_fops_mmap,
#ifdef CONFIG_PROC_FS
.show_fdinfo = vfio_device_show_fdinfo,
#endif
};
static struct vfio_device *vfio_device_from_file(struct file *file)
{
struct vfio_device_file *df = file->private_data;
if (file->f_op != &vfio_device_fops)
return NULL;
return df->device;
}
/**
* vfio_file_is_valid - True if the file is valid vfio file
* @file: VFIO group file or VFIO device file
*/
bool vfio_file_is_valid(struct file *file)
{
return vfio_group_from_file(file) ||
vfio_device_from_file(file);
}
EXPORT_SYMBOL_GPL(vfio_file_is_valid);
/**
* vfio_file_enforced_coherent - True if the DMA associated with the VFIO file
* is always CPU cache coherent
* @file: VFIO group file or VFIO device file
*
* Enforced coherency means that the IOMMU ignores things like the PCIe no-snoop
* bit in DMA transactions. A return of false indicates that the user has
* rights to access additional instructions such as wbinvd on x86.
*/
bool vfio_file_enforced_coherent(struct file *file)
{
struct vfio_device *device;
struct vfio_group *group;
group = vfio_group_from_file(file);
if (group)
return vfio_group_enforced_coherent(group);
device = vfio_device_from_file(file);
if (device)
return device_iommu_capable(device->dev,
IOMMU_CAP_ENFORCE_CACHE_COHERENCY);
return true;
}
EXPORT_SYMBOL_GPL(vfio_file_enforced_coherent);
static void vfio_device_file_set_kvm(struct file *file, struct kvm *kvm)
{
struct vfio_device_file *df = file->private_data;
/*
* The kvm is first recorded in the vfio_device_file, and will
* be propagated to vfio_device::kvm when the file is bound to
* iommufd successfully in the vfio device cdev path.
*/
spin_lock(&df->kvm_ref_lock);
df->kvm = kvm;
spin_unlock(&df->kvm_ref_lock);
}
/**
* vfio_file_set_kvm - Link a kvm with VFIO drivers
* @file: VFIO group file or VFIO device file
* @kvm: KVM to link
*
* When a VFIO device is first opened the KVM will be available in
* device->kvm if one was associated with the file.
*/
void vfio_file_set_kvm(struct file *file, struct kvm *kvm)
{
struct vfio_group *group;
group = vfio_group_from_file(file);
if (group)
vfio_group_set_kvm(group, kvm);
Annotation
- Immediate include surface: `linux/cdev.h`, `linux/compat.h`, `linux/device.h`, `linux/fs.h`, `linux/idr.h`, `linux/iommu.h`, `linux/kvm_host.h`, `linux/list.h`.
- Detected declarations: `function vfio_assign_device_set`, `function vfio_release_device_set`, `function vfio_device_set_open_count`, `function vfio_find_device_in_devset`, `function vfio_device_put_registration`, `function vfio_device_try_get_registration`, `function vfio_device_release`, `function vfio_alloc_device`, `function vfio_fs_init_fs_context`, `function vfio_init_device`.
- Atlas domain: Driver Families / drivers/vfio.
- Implementation status: pattern implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.