drivers/vfio/container.c
Source file repositories/reference/linux-study-clean/drivers/vfio/container.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/vfio/container.c- Extension
.c- Size
- 14900 bytes
- Lines
- 608
- 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.
- 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/file.hlinux/slab.hlinux/fs.hlinux/capability.hlinux/iommu.hlinux/miscdevice.hlinux/vfio.huapi/linux/vfio.hvfio.h
Detected Declarations
struct vfio_containerfunction vfio_noiommu_releasefunction vfio_noiommu_attach_groupfunction vfio_noiommu_detach_groupfunction vfio_iommu_driver_allowedfunction vfio_register_iommu_driverfunction vfio_unregister_iommu_driverfunction vfio_container_releasefunction vfio_container_getfunction vfio_container_putfunction vfio_device_container_registerfunction vfio_device_container_unregisterfunction vfio_container_ioctl_check_extensionfunction list_for_each_entryfunction __vfio_container_attach_groupsfunction list_for_each_entryfunction vfio_ioctl_set_iommufunction vfio_fops_unl_ioctlfunction vfio_fops_openfunction vfio_fops_releasefunction vfio_container_attach_groupfunction vfio_group_detach_containerfunction vfio_group_use_containerfunction vfio_group_unuse_containerfunction vfio_device_container_pin_pagesfunction vfio_device_container_unpin_pagesfunction vfio_device_container_dma_rwfunction vfio_container_initfunction vfio_container_cleanupexport vfio_register_iommu_driverexport vfio_unregister_iommu_driver
Annotated Snippet
static const struct file_operations vfio_fops = {
.owner = THIS_MODULE,
.open = vfio_fops_open,
.release = vfio_fops_release,
.unlocked_ioctl = vfio_fops_unl_ioctl,
.compat_ioctl = compat_ptr_ioctl,
};
struct vfio_container *vfio_container_from_file(struct file *file)
{
struct vfio_container *container;
/* Sanity check, is this really our fd? */
if (file->f_op != &vfio_fops)
return NULL;
container = file->private_data;
WARN_ON(!container); /* fget ensures we don't race vfio_release */
return container;
}
static struct miscdevice vfio_dev = {
.minor = VFIO_MINOR,
.name = "vfio",
.fops = &vfio_fops,
.nodename = "vfio/vfio",
.mode = S_IRUGO | S_IWUGO,
};
int vfio_container_attach_group(struct vfio_container *container,
struct vfio_group *group)
{
struct vfio_iommu_driver *driver;
int ret = 0;
lockdep_assert_held(&group->group_lock);
if (group->type == VFIO_NO_IOMMU && !capable(CAP_SYS_RAWIO))
return -EPERM;
down_write(&container->group_lock);
/* Real groups and fake groups cannot mix */
if (!list_empty(&container->group_list) &&
container->noiommu != (group->type == VFIO_NO_IOMMU)) {
ret = -EPERM;
goto out_unlock_container;
}
if (group->type == VFIO_IOMMU) {
ret = iommu_group_claim_dma_owner(group->iommu_group, group);
if (ret)
goto out_unlock_container;
}
driver = container->iommu_driver;
if (driver) {
ret = driver->ops->attach_group(container->iommu_data,
group->iommu_group,
group->type);
if (ret) {
if (group->type == VFIO_IOMMU)
iommu_group_release_dma_owner(
group->iommu_group);
goto out_unlock_container;
}
}
group->container = container;
group->container_users = 1;
container->noiommu = (group->type == VFIO_NO_IOMMU);
list_add(&group->container_next, &container->group_list);
/* Get a reference on the container and mark a user within the group */
vfio_container_get(container);
out_unlock_container:
up_write(&container->group_lock);
return ret;
}
void vfio_group_detach_container(struct vfio_group *group)
{
struct vfio_container *container = group->container;
struct vfio_iommu_driver *driver;
lockdep_assert_held(&group->group_lock);
WARN_ON(group->container_users != 1);
down_write(&container->group_lock);
Annotation
- Immediate include surface: `linux/file.h`, `linux/slab.h`, `linux/fs.h`, `linux/capability.h`, `linux/iommu.h`, `linux/miscdevice.h`, `linux/vfio.h`, `uapi/linux/vfio.h`.
- Detected declarations: `struct vfio_container`, `function vfio_noiommu_release`, `function vfio_noiommu_attach_group`, `function vfio_noiommu_detach_group`, `function vfio_iommu_driver_allowed`, `function vfio_register_iommu_driver`, `function vfio_unregister_iommu_driver`, `function vfio_container_release`, `function vfio_container_get`, `function vfio_container_put`.
- Atlas domain: Driver Families / drivers/vfio.
- Implementation status: pattern implementation candidate.
- 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.