[RFC PATCH 6/7] vfio-pci: add alias_region mmap ops

Mahmoud Adam posted 7 patches 1 week ago
[RFC PATCH 6/7] vfio-pci: add alias_region mmap ops
Posted by Mahmoud Adam 1 week ago
Implement struct vfio_pci_regops for alias regions. Where it
implements the mmap ops. When mmap is called on these regions it
translates the vm_pgoff to match the aliased region. Then it calls the
required mmap for the target region. It updates the vm_page_prot
afterwards with the requested flags.

The call path would be:
vfio_pci_core_mmap (index >= VFIO_PCI_NUM_REGIONS)
 vfio_pci_alias_region_mmap (update vm_pgoff)
  vfio_pci_core_mmap

For now no more information is needed more than the aliased index. So
we use region->data to save the aliased index number.

Note: Alias regions can't alias another alias.

Signed-off-by: Mahmoud Adam <mngyadam@amazon.de>
---
 drivers/vfio/pci/vfio_pci_core.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index 04b93bd55a5c2..962d3eda1ea9f 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -1528,6 +1528,33 @@ static int vfio_pci_core_feature_token(struct vfio_device *device, u32 flags,
 	return 0;
 }
 
+static int vfio_pci_alias_region_mmap(struct vfio_pci_core_device *vdev,
+				      struct vfio_pci_region *region,
+				      struct vm_area_struct *vma)
+{
+	unsigned int alias_index = (uintptr_t) region->data;
+	unsigned long vm_pgoff;
+	int ret;
+
+	/* change the pgoff to the corresponding alias */
+	vm_pgoff = alias_index << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
+	vm_pgoff |= vma->vm_pgoff &
+		    ((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
+	vma->vm_pgoff = vm_pgoff;
+
+	ret = vdev->vdev.ops->mmap(&vdev->vdev, vma);
+
+	/* overwrite prot with the alias flags */
+	if (region->flags & VFIO_REGION_INFO_FLAG_WC)
+		vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
+
+	return ret;
+}
+
+struct vfio_pci_regops vfio_pci_alias_region_ops = {
+	.mmap = vfio_pci_alias_region_mmap,
+};
+
 int vfio_pci_core_ioctl_feature(struct vfio_device *device, u32 flags,
 				void __user *arg, size_t argsz)
 {
-- 
2.47.3




Amazon Web Services Development Center Germany GmbH
Tamara-Danz-Str. 13
10243 Berlin
Geschaeftsfuehrung: Christian Schlaeger
Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B
Sitz: Berlin
Ust-ID: DE 365 538 597