diff mbox series

[4/7] vfio,vfio-pci: Add vma to pfn callback

Message ID 162818326742.1511194.1366505678218237973.stgit@omen (mailing list archive)
State New, archived
Headers show
Series vfio: device fd address space and vfio-pci mmap invalidation cleanup | expand

Commit Message

Alex Williamson Aug. 5, 2021, 5:07 p.m. UTC
Add a new vfio_device_ops callback to allow the vfio device driver to
translate a vma mapping of a vfio device fd to a pfn.  Implementation
limited to vfio-pci here for the purpose of supporting the reverse of
unmap_mapping_range(), but expected to be implemented for all vfio
device drivers supporting DMA mapping of device memory mmaps.

Suggested-by: Jason Gunthorpe <jgg@nvidia.com>
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
---
 drivers/vfio/pci/vfio_pci.c |    9 ++++++---
 drivers/vfio/vfio.c         |   18 ++++++++++++++++--
 include/linux/vfio.h        |    6 ++++++
 3 files changed, 28 insertions(+), 5 deletions(-)

Comments

Jason Gunthorpe Aug. 6, 2021, 1:01 a.m. UTC | #1
On Thu, Aug 05, 2021 at 11:07:47AM -0600, Alex Williamson wrote:
> diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
> index 1e4fc69fee7d..42ca93be152a 100644
> +++ b/drivers/vfio/vfio.c
> @@ -875,6 +875,22 @@ struct vfio_device *vfio_device_get_from_dev(struct device *dev)
>  }
>  EXPORT_SYMBOL_GPL(vfio_device_get_from_dev);
>  
> +static const struct file_operations vfio_device_fops;
> +
> +int vfio_device_vma_to_pfn(struct vfio_device *device,
> +			   struct vm_area_struct *vma, unsigned long *pfn)

A comment here describing the locking conditions the caller must meet
would be a good addition.. It looks like this can only work under the
i_mmap_lock and the returned pfn can only be taken outside that lock
if it is placed in a VMA

Maybe this is not a great API then? Should it be 'populate vma' and
call io_remap_pfn_range under the op?

Jason
Christoph Hellwig Aug. 10, 2021, 9 a.m. UTC | #2
On Thu, Aug 05, 2021 at 10:01:46PM -0300, Jason Gunthorpe wrote:
> On Thu, Aug 05, 2021 at 11:07:47AM -0600, Alex Williamson wrote:
> > diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
> > index 1e4fc69fee7d..42ca93be152a 100644
> > +++ b/drivers/vfio/vfio.c
> > @@ -875,6 +875,22 @@ struct vfio_device *vfio_device_get_from_dev(struct device *dev)
> >  }
> >  EXPORT_SYMBOL_GPL(vfio_device_get_from_dev);
> >  
> > +static const struct file_operations vfio_device_fops;
> > +
> > +int vfio_device_vma_to_pfn(struct vfio_device *device,
> > +			   struct vm_area_struct *vma, unsigned long *pfn)
> 
> A comment here describing the locking conditions the caller must meet
> would be a good addition.. It looks like this can only work under the
> i_mmap_lock and the returned pfn can only be taken outside that lock
> if it is placed in a VMA
> 
> Maybe this is not a great API then? Should it be 'populate vma' and
> call io_remap_pfn_range under the op?

Yes, I think that would be a better API.
Christoph Hellwig Aug. 10, 2021, 9 a.m. UTC | #3
>  static int vfio_pci_reflck_attach(struct vfio_pci_device *vdev);
> diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
> index 1e4fc69fee7d..42ca93be152a 100644
> --- a/drivers/vfio/vfio.c
> +++ b/drivers/vfio/vfio.c
> @@ -875,6 +875,22 @@ struct vfio_device *vfio_device_get_from_dev(struct device *dev)
>  }
>  EXPORT_SYMBOL_GPL(vfio_device_get_from_dev);
>  
> +static const struct file_operations vfio_device_fops;

If we ned a forward declaration here it would be nice to keep it at the top
of the file.  Finding a way to not need it would be even better.

> +
> +int vfio_device_vma_to_pfn(struct vfio_device *device,
> +			   struct vm_area_struct *vma, unsigned long *pfn)
> +{
> +	if (WARN_ON(!vma->vm_file || vma->vm_file->f_op != &vfio_device_fops ||
> +		    vma->vm_file->private_data != device))
> +		return -EINVAL;

WARN_ON_ONCE?

> +
> +	if (unlikely(!device->ops->vma_to_pfn))
> +		return -EPERM;
> +
> +	return device->ops->vma_to_pfn(device, vma, pfn);
> +}
> +EXPORT_SYMBOL_GPL(vfio_device_vma_to_pfn);

This function is only used in vfio.c, so it can be marked static instead of
being exported.
diff mbox series

Patch

diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c
index c526edbf1173..7a9f67cfc0a2 100644
--- a/drivers/vfio/pci/vfio_pci.c
+++ b/drivers/vfio/pci/vfio_pci.c
@@ -1440,10 +1440,12 @@  void vfio_pci_memory_unlock_and_restore(struct vfio_pci_device *vdev, u16 cmd)
 	up_write(&vdev->memory_lock);
 }
 
-static int vfio_pci_bar_vma_to_pfn(struct vm_area_struct *vma,
+static int vfio_pci_bar_vma_to_pfn(struct vfio_device *core_vdev,
+				   struct vm_area_struct *vma,
 				   unsigned long *pfn)
 {
-	struct vfio_pci_device *vdev = vma->vm_private_data;
+	struct vfio_pci_device *vdev =
+			container_of(core_vdev, struct vfio_pci_device, vdev);
 	struct pci_dev *pdev = vdev->pdev;
 	int index;
 	u64 pgoff;
@@ -1469,7 +1471,7 @@  static vm_fault_t vfio_pci_mmap_fault(struct vm_fault *vmf)
 	unsigned long vaddr, pfn;
 	vm_fault_t ret = VM_FAULT_SIGBUS;
 
-	if (vfio_pci_bar_vma_to_pfn(vma, &pfn))
+	if (vfio_pci_bar_vma_to_pfn(&vdev->vdev, vma, &pfn))
 		return ret;
 
 	down_read(&vdev->memory_lock);
@@ -1742,6 +1744,7 @@  static const struct vfio_device_ops vfio_pci_ops = {
 	.mmap		= vfio_pci_mmap,
 	.request	= vfio_pci_request,
 	.match		= vfio_pci_match,
+	.vma_to_pfn	= vfio_pci_bar_vma_to_pfn,
 };
 
 static int vfio_pci_reflck_attach(struct vfio_pci_device *vdev);
diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
index 1e4fc69fee7d..42ca93be152a 100644
--- a/drivers/vfio/vfio.c
+++ b/drivers/vfio/vfio.c
@@ -875,6 +875,22 @@  struct vfio_device *vfio_device_get_from_dev(struct device *dev)
 }
 EXPORT_SYMBOL_GPL(vfio_device_get_from_dev);
 
+static const struct file_operations vfio_device_fops;
+
+int vfio_device_vma_to_pfn(struct vfio_device *device,
+			   struct vm_area_struct *vma, unsigned long *pfn)
+{
+	if (WARN_ON(!vma->vm_file || vma->vm_file->f_op != &vfio_device_fops ||
+		    vma->vm_file->private_data != device))
+		return -EINVAL;
+
+	if (unlikely(!device->ops->vma_to_pfn))
+		return -EPERM;
+
+	return device->ops->vma_to_pfn(device, vma, pfn);
+}
+EXPORT_SYMBOL_GPL(vfio_device_vma_to_pfn);
+
 static struct vfio_device *vfio_device_get_from_name(struct vfio_group *group,
 						     char *buf)
 {
@@ -1407,8 +1423,6 @@  static int vfio_group_add_container_user(struct vfio_group *group)
 	return 0;
 }
 
-static const struct file_operations vfio_device_fops;
-
 static int vfio_group_get_device_fd(struct vfio_group *group, char *buf)
 {
 	struct vfio_device *device;
diff --git a/include/linux/vfio.h b/include/linux/vfio.h
index 712813703e5a..5f07ebe0f85d 100644
--- a/include/linux/vfio.h
+++ b/include/linux/vfio.h
@@ -41,6 +41,7 @@  struct vfio_device {
  * @match: Optional device name match callback (return: 0 for no-match, >0 for
  *         match, -errno for abort (ex. match with insufficient or incorrect
  *         additional args)
+ * @vma_to_pfn: Optional pfn from vma lookup against vma mapping device fd
  */
 struct vfio_device_ops {
 	char	*name;
@@ -55,6 +56,8 @@  struct vfio_device_ops {
 	int	(*mmap)(struct vfio_device *vdev, struct vm_area_struct *vma);
 	void	(*request)(struct vfio_device *vdev, unsigned int count);
 	int	(*match)(struct vfio_device *vdev, char *buf);
+	int	(*vma_to_pfn)(struct vfio_device *vdev,
+			      struct vm_area_struct *vma, unsigned long *pfn);
 };
 
 extern struct iommu_group *vfio_iommu_group_get(struct device *dev);
@@ -68,6 +71,9 @@  extern struct vfio_device *vfio_device_get_from_dev(struct device *dev);
 extern void vfio_device_put(struct vfio_device *device);
 extern void vfio_device_unmap_mapping_range(struct vfio_device *device,
 					    loff_t start, loff_t len);
+extern int vfio_device_vma_to_pfn(struct vfio_device *device,
+				  struct vm_area_struct *vma,
+				  unsigned long *pfn);
 
 /* events for the backend driver notify callback */
 enum vfio_iommu_notify_type {