drivers/vfio/pci/vfio_pci_dmabuf.c
Source file repositories/reference/linux-study-clean/drivers/vfio/pci/vfio_pci_dmabuf.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/vfio/pci/vfio_pci_dmabuf.c- Extension
.c- Size
- 10604 bytes
- Lines
- 407
- Domain
- Driver Families
- Bucket
- drivers/vfio
- 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.
- 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/dma-buf-mapping.hlinux/pci-p2pdma.hlinux/dma-resv.hvfio_pci_priv.h
Detected Declarations
struct vfio_pci_dma_buffunction vfio_pci_dma_buf_attachfunction vfio_pci_dma_buf_donefunction vfio_pci_dma_buf_mapfunction vfio_pci_dma_buf_unmapfunction vfio_pci_dma_buf_releasefunction vfio_pci_dma_buf_cleanupfunction vfio_pci_dma_buf_iommufd_mapfunction vfio_pci_core_fill_phys_vecfunction vfio_pci_core_get_dmabuf_physfunction validate_dmabuf_inputfunction vfio_pci_core_feature_dma_buffunction vfio_pci_dma_buf_movefunction list_for_each_entry_safefunction vfio_pci_dma_buf_cleanupfunction list_for_each_entry_safeexport vfio_pci_core_fill_phys_vecexport vfio_pci_core_get_dmabuf_phys
Annotated Snippet
struct vfio_pci_dma_buf {
struct dma_buf *dmabuf;
struct vfio_pci_core_device *vdev;
struct list_head dmabufs_elm;
size_t size;
struct phys_vec *phys_vec;
struct p2pdma_provider *provider;
u32 nr_ranges;
struct kref kref;
struct completion comp;
u8 revoked : 1;
};
static int vfio_pci_dma_buf_attach(struct dma_buf *dmabuf,
struct dma_buf_attachment *attachment)
{
struct vfio_pci_dma_buf *priv = dmabuf->priv;
if (!attachment->peer2peer)
return -EOPNOTSUPP;
if (priv->revoked)
return -ENODEV;
if (!dma_buf_attach_revocable(attachment))
return -EOPNOTSUPP;
return 0;
}
static void vfio_pci_dma_buf_done(struct kref *kref)
{
struct vfio_pci_dma_buf *priv =
container_of(kref, struct vfio_pci_dma_buf, kref);
complete(&priv->comp);
}
static struct sg_table *
vfio_pci_dma_buf_map(struct dma_buf_attachment *attachment,
enum dma_data_direction dir)
{
struct vfio_pci_dma_buf *priv = attachment->dmabuf->priv;
struct sg_table *ret;
dma_resv_assert_held(priv->dmabuf->resv);
if (priv->revoked)
return ERR_PTR(-ENODEV);
ret = dma_buf_phys_vec_to_sgt(attachment, priv->provider,
priv->phys_vec, priv->nr_ranges,
priv->size, dir);
if (IS_ERR(ret))
return ret;
kref_get(&priv->kref);
return ret;
}
static void vfio_pci_dma_buf_unmap(struct dma_buf_attachment *attachment,
struct sg_table *sgt,
enum dma_data_direction dir)
{
struct vfio_pci_dma_buf *priv = attachment->dmabuf->priv;
dma_resv_assert_held(priv->dmabuf->resv);
dma_buf_free_sgt(attachment, sgt, dir);
kref_put(&priv->kref, vfio_pci_dma_buf_done);
}
static void vfio_pci_dma_buf_release(struct dma_buf *dmabuf)
{
struct vfio_pci_dma_buf *priv = dmabuf->priv;
/*
* Either this or vfio_pci_dma_buf_cleanup() will remove from the list.
* The refcount prevents both.
*/
if (priv->vdev) {
down_write(&priv->vdev->memory_lock);
list_del_init(&priv->dmabufs_elm);
up_write(&priv->vdev->memory_lock);
vfio_device_put_registration(&priv->vdev->vdev);
}
kfree(priv->phys_vec);
kfree(priv);
}
Annotation
- Immediate include surface: `linux/dma-buf-mapping.h`, `linux/pci-p2pdma.h`, `linux/dma-resv.h`, `vfio_pci_priv.h`.
- Detected declarations: `struct vfio_pci_dma_buf`, `function vfio_pci_dma_buf_attach`, `function vfio_pci_dma_buf_done`, `function vfio_pci_dma_buf_map`, `function vfio_pci_dma_buf_unmap`, `function vfio_pci_dma_buf_release`, `function vfio_pci_dma_buf_cleanup`, `function vfio_pci_dma_buf_iommufd_map`, `function vfio_pci_core_fill_phys_vec`, `function vfio_pci_core_get_dmabuf_phys`.
- Atlas domain: Driver Families / drivers/vfio.
- Implementation status: integration implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
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.