Introduce a vfio bus driver policy where using the exported vfio_device_vma_open() as the vm_ops.open for a vma indicates vm_private_data is the struct vfio_device pointer associated to the vma. This allows a direct vma to device lookup. Operating on an active, open vma to the device, we should be able to directly increment the vfio_device reference. Implemented only for vfio-pci for now. Suggested-by: Jason Gunthorpe <jgg@xxxxxxxxxx> Signed-off-by: Alex Williamson <alex.williamson@xxxxxxxxxx> --- drivers/vfio/pci/vfio_pci.c | 6 ++++-- drivers/vfio/vfio.c | 24 ++++++++++++++++++++++++ include/linux/vfio.h | 2 ++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c index 115f10f7b096..f9529bac6c97 100644 --- a/drivers/vfio/pci/vfio_pci.c +++ b/drivers/vfio/pci/vfio_pci.c @@ -1469,7 +1469,8 @@ void vfio_pci_memory_unlock_and_restore(struct vfio_pci_device *vdev, u16 cmd) static vm_fault_t vfio_pci_mmap_fault(struct vm_fault *vmf) { struct vm_area_struct *vma = vmf->vma; - struct vfio_pci_device *vdev = vma->vm_private_data; + struct vfio_device *device = vma->vm_private_data; + struct vfio_pci_device *vdev = vfio_device_data(device); vm_fault_t ret = VM_FAULT_SIGBUS; down_read(&vdev->memory_lock); @@ -1485,6 +1486,7 @@ static vm_fault_t vfio_pci_mmap_fault(struct vm_fault *vmf) } static const struct vm_operations_struct vfio_pci_mmap_ops = { + .open = vfio_device_vma_open, .fault = vfio_pci_mmap_fault, }; @@ -1542,7 +1544,7 @@ static int vfio_pci_mmap(void *device_data, struct vm_area_struct *vma) } } - vma->vm_private_data = vdev; + vma->vm_private_data = vdev->device; vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); vma->vm_pgoff = (pci_resource_start(pdev, index) >> PAGE_SHIFT) + pgoff; diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c index da212425ab30..399c42b77fbb 100644 --- a/drivers/vfio/vfio.c +++ b/drivers/vfio/vfio.c @@ -572,6 +572,15 @@ void vfio_device_unmap_mapping_range(struct vfio_device *device, } EXPORT_SYMBOL_GPL(vfio_device_unmap_mapping_range); +/* + * A VFIO bus driver using this open callback will provide a + * struct vfio_device pointer in the vm_private_data field. + */ +void vfio_device_vma_open(struct vm_area_struct *vma) +{ +} +EXPORT_SYMBOL_GPL(vfio_device_vma_open); + /** * Device objects - create, release, get, put, search */ @@ -927,6 +936,21 @@ struct vfio_device *vfio_device_get_from_dev(struct device *dev) } EXPORT_SYMBOL_GPL(vfio_device_get_from_dev); +struct vfio_device *vfio_device_get_from_vma(struct vm_area_struct *vma) +{ + struct vfio_device *device; + + if (vma->vm_ops->open != vfio_device_vma_open) + return ERR_PTR(-ENODEV); + + device = vma->vm_private_data; + + vfio_device_get(device); + + return device; +} +EXPORT_SYMBOL_GPL(vfio_device_get_from_vma); + static struct vfio_device *vfio_device_get_from_name(struct vfio_group *group, char *buf) { diff --git a/include/linux/vfio.h b/include/linux/vfio.h index f435dfca15eb..188c2f3feed9 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -58,6 +58,8 @@ extern void vfio_device_put(struct vfio_device *device); extern void *vfio_device_data(struct vfio_device *device); extern void vfio_device_unmap_mapping_range(struct vfio_device *device, loff_t start, loff_t len); +extern void vfio_device_vma_open(struct vm_area_struct *vma); +extern struct vfio_device *vfio_device_get_from_vma(struct vm_area_struct *vma); /* events for the backend driver notify callback */ enum vfio_iommu_notify_type {