drivers/iommu/iommufd/device.c
Source file repositories/reference/linux-study-clean/drivers/iommu/iommufd/device.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/iommu/iommufd/device.c- Extension
.c- Size
- 45747 bytes
- Lines
- 1668
- Domain
- Driver Families
- Bucket
- drivers/iommu
- Inferred role
- Driver Families: exported/initcall integration point
- Status
- integration 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.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- 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.
- Touches IRQ or DMA behavior; this matters for the representative real-device path.
- 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/iommu.hlinux/iommufd.hlinux/pci-ats.hlinux/slab.huapi/linux/iommufd.h../iommu-priv.hio_pagetable.hiommufd_private.h
Detected Declarations
struct iommufd_attachfunction iommufd_group_releasefunction iommufd_put_groupfunction iommufd_group_try_getfunction iommufd_device_remove_vdevfunction iommufd_device_pre_destroyfunction iommufd_device_destroyfunction iommufd_device_unbindfunction devicefunction iommufd_device_bindfunction iommufd_device_unbindfunction iommufd_device_to_idfunction iommufd_group_device_numfunction iommufd_group_setup_msifunction iommufd_group_setup_msifunction iommufd_group_first_attachfunction iommufd_device_attach_reserved_iovafunction iommufd_device_is_attachedfunction iommufd_hwpt_pasid_compatfunction iommufd_hwpt_compatible_devicefunction iommufd_hwpt_attach_devicefunction iommufd_device_get_attach_handlefunction iommufd_hwpt_detach_devicefunction iommufd_hwpt_replace_devicefunction iommufd_hw_pagetable_attachfunction iommufd_hw_pagetable_detachfunction iommufd_device_do_attachfunction iommufd_group_remove_reserved_iovafunction iommufd_group_do_replace_reserved_iovafunction xa_for_eachfunction iommufd_device_do_replacefunction iommufd_device_auto_get_domainfunction iommufd_device_change_ptfunction iommufd_device_detachfunction iommufd_device_attachfunction iommufd_device_attachfunction cur_ioasfunction iommufd_access_change_ioas_idfunction iommufd_access_destroy_objectfunction iommufd_access_pin_pagesfunction iommufd_access_destroyfunction iommufd_access_detachfunction iommufd_access_attachfunction iommufd_access_attach_internalfunction iommufd_access_replacefunction iopt_access_pagesfunction iommufd_access_unpin_pagesfunction iommufd_access_attach
Annotated Snippet
struct iommufd_attach {
struct iommufd_hw_pagetable *hwpt;
struct xarray device_array;
};
static void iommufd_group_release(struct kref *kref)
{
struct iommufd_group *igroup =
container_of(kref, struct iommufd_group, ref);
WARN_ON(!xa_empty(&igroup->pasid_attach));
xa_cmpxchg(&igroup->ictx->groups, iommu_group_id(igroup->group), igroup,
NULL, GFP_KERNEL);
iommu_group_put(igroup->group);
mutex_destroy(&igroup->lock);
kfree(igroup);
}
static void iommufd_put_group(struct iommufd_group *group)
{
kref_put(&group->ref, iommufd_group_release);
}
static bool iommufd_group_try_get(struct iommufd_group *igroup,
struct iommu_group *group)
{
if (!igroup)
return false;
/*
* group ID's cannot be re-used until the group is put back which does
* not happen if we could get an igroup pointer under the xa_lock.
*/
if (WARN_ON(igroup->group != group))
return false;
return kref_get_unless_zero(&igroup->ref);
}
/*
* iommufd needs to store some more data for each iommu_group, we keep a
* parallel xarray indexed by iommu_group id to hold this instead of putting it
* in the core structure. To keep things simple the iommufd_group memory is
* unique within the iommufd_ctx. This makes it easy to check there are no
* memory leaks.
*/
static struct iommufd_group *iommufd_get_group(struct iommufd_ctx *ictx,
struct device *dev)
{
struct iommufd_group *new_igroup;
struct iommufd_group *cur_igroup;
struct iommufd_group *igroup;
struct iommu_group *group;
unsigned int id;
group = iommu_group_get(dev);
if (!group)
return ERR_PTR(-ENODEV);
id = iommu_group_id(group);
xa_lock(&ictx->groups);
igroup = xa_load(&ictx->groups, id);
if (iommufd_group_try_get(igroup, group)) {
xa_unlock(&ictx->groups);
iommu_group_put(group);
return igroup;
}
xa_unlock(&ictx->groups);
new_igroup = kzalloc_obj(*new_igroup);
if (!new_igroup) {
iommu_group_put(group);
return ERR_PTR(-ENOMEM);
}
kref_init(&new_igroup->ref);
mutex_init(&new_igroup->lock);
xa_init(&new_igroup->pasid_attach);
new_igroup->sw_msi_start = PHYS_ADDR_MAX;
/* group reference moves into new_igroup */
new_igroup->group = group;
/*
* The ictx is not additionally refcounted here becase all objects using
* an igroup must put it before their destroy completes.
*/
new_igroup->ictx = ictx;
/*
* We dropped the lock so igroup is invalid. NULL is a safe and likely
Annotation
- Immediate include surface: `linux/iommu.h`, `linux/iommufd.h`, `linux/pci-ats.h`, `linux/slab.h`, `uapi/linux/iommufd.h`, `../iommu-priv.h`, `io_pagetable.h`, `iommufd_private.h`.
- Detected declarations: `struct iommufd_attach`, `function iommufd_group_release`, `function iommufd_put_group`, `function iommufd_group_try_get`, `function iommufd_device_remove_vdev`, `function iommufd_device_pre_destroy`, `function iommufd_device_destroy`, `function iommufd_device_unbind`, `function device`, `function iommufd_device_bind`.
- Atlas domain: Driver Families / drivers/iommu.
- Implementation status: integration 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.
- IRQ or DMA behavior appears here, which is relevant to the selected PCIe/NVMe device path.
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.