Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1333223

[PATCH v2 04/11] vfio/pci: Include sparse mmap capability for MSI-X table regions

From Alex Williamson <alex.williamson@redhat.com>
Newsgroups linux.kernel
Subject [PATCH v2 04/11] vfio/pci: Include sparse mmap capability for MSI-X table regions
Date 2016-02-13 01:10 +0100
Message-ID <r1vWF-5KL-11@gated-at.bofh.it> (permalink)
References <r1vWF-5KL-3@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


vfio-pci has never allowed the user to directly mmap the MSI-X vector
table, but we've always relied on implicit knowledge of the user that
they cannot do this.  Now that we have capability chains that we can
expose in the region info ioctl and a sparse mmap capability that
represents the sub-areas within the region that can be mmap'd, we can
make the mmap constraints more explicit.

Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
---
 drivers/vfio/pci/vfio_pci.c |   73 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 72 insertions(+), 1 deletion(-)

diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c
index 2760a7b..4682207 100644
--- a/drivers/vfio/pci/vfio_pci.c
+++ b/drivers/vfio/pci/vfio_pci.c
@@ -421,6 +421,48 @@ static int vfio_pci_for_each_slot_or_bus(struct pci_dev *pdev,
 	return walk.ret;
 }
 
+static int msix_sparse_mmap_cap(struct vfio_pci_device *vdev,
+				struct vfio_info_cap *caps)
+{
+	struct vfio_info_cap_header *header;
+	struct vfio_region_info_cap_sparse_mmap *sparse;
+	size_t end, size;
+	int nr_areas = 2, i = 0;
+
+	end = pci_resource_len(vdev->pdev, vdev->msix_bar);
+
+	/* If MSI-X table is aligned to the start or end, only one area */
+	if (((vdev->msix_offset & PAGE_MASK) == 0) ||
+	    (PAGE_ALIGN(vdev->msix_offset + vdev->msix_size) >= end))
+		nr_areas = 1;
+
+	size = sizeof(*sparse) + (nr_areas * sizeof(*sparse->areas));
+
+	header = vfio_info_cap_add(caps, size,
+				   VFIO_REGION_INFO_CAP_SPARSE_MMAP, 1);
+	if (IS_ERR(header))
+		return PTR_ERR(header);
+
+	sparse = container_of(header,
+			      struct vfio_region_info_cap_sparse_mmap, header);
+	sparse->nr_areas = nr_areas;
+
+	if (vdev->msix_offset & PAGE_MASK) {
+		sparse->areas[i].offset = 0;
+		sparse->areas[i].size = vdev->msix_offset & PAGE_MASK;
+		i++;
+	}
+
+	if (PAGE_ALIGN(vdev->msix_offset + vdev->msix_size) < end) {
+		sparse->areas[i].offset = PAGE_ALIGN(vdev->msix_offset +
+						     vdev->msix_size);
+		sparse->areas[i].size = end - sparse->areas[i].offset;
+		i++;
+	}
+
+	return 0;
+}
+
 static long vfio_pci_ioctl(void *device_data,
 			   unsigned int cmd, unsigned long arg)
 {
@@ -451,6 +493,8 @@ static long vfio_pci_ioctl(void *device_data,
 	} else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
 		struct pci_dev *pdev = vdev->pdev;
 		struct vfio_region_info info;
+		struct vfio_info_cap caps = { .buf = NULL, .size = 0 };
+		int ret;
 
 		minsz = offsetofend(struct vfio_region_info, offset);
 
@@ -479,8 +523,15 @@ static long vfio_pci_ioctl(void *device_data,
 				     VFIO_REGION_INFO_FLAG_WRITE;
 			if (IS_ENABLED(CONFIG_VFIO_PCI_MMAP) &&
 			    pci_resource_flags(pdev, info.index) &
-			    IORESOURCE_MEM && info.size >= PAGE_SIZE)
+			    IORESOURCE_MEM && info.size >= PAGE_SIZE) {
 				info.flags |= VFIO_REGION_INFO_FLAG_MMAP;
+				if (info.index == vdev->msix_bar) {
+					ret = msix_sparse_mmap_cap(vdev, &caps);
+					if (ret)
+						return ret;
+				}
+			}
+
 			break;
 		case VFIO_PCI_ROM_REGION_INDEX:
 		{
@@ -520,6 +571,26 @@ static long vfio_pci_ioctl(void *device_data,
 			return -EINVAL;
 		}
 
+		if (caps.size) {
+			info.flags |= VFIO_REGION_INFO_FLAG_CAPS;
+			if (info.argsz < sizeof(info) + caps.size) {
+				info.argsz = sizeof(info) + caps.size;
+				info.cap_offset = 0;
+			} else {
+				vfio_info_cap_shift(&caps, sizeof(info));
+				ret = copy_to_user((void __user *)arg +
+						   sizeof(info), caps.buf,
+						   caps.size);
+				if (ret) {
+					kfree(caps.buf);
+					return ret;
+				}
+				info.cap_offset = sizeof(info);
+			}
+
+			kfree(caps.buf);
+		}
+
 		return copy_to_user((void __user *)arg, &info, minsz);
 
 	} else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH v2 00/11] vfio: capability chains, sparse mmaps,  device specific regions, IGD support Alex Williamson <alex.williamson@redhat.com> - 2016-02-13 01:10 +0100
  [PATCH v2 05/11] vfio: Define device specific region type capability Alex Williamson <alex.williamson@redhat.com> - 2016-02-13 01:10 +0100
  [PATCH v2 06/11] vfio/pci: Add infrastructure for additional device  specific regions Alex Williamson <alex.williamson@redhat.com> - 2016-02-13 01:10 +0100
  [PATCH v2 08/11] vfio/pci: Intel IGD OpRegion support Alex Williamson <alex.williamson@redhat.com> - 2016-02-13 01:10 +0100
  [PATCH v2 04/11] vfio/pci: Include sparse mmap capability for MSI-X  table regions Alex Williamson <alex.williamson@redhat.com> - 2016-02-13 01:10 +0100
  [PATCH v2 10/11] vfio/pci: Hide stolen memory from the user Alex Williamson <alex.williamson@redhat.com> - 2016-02-13 01:10 +0100
  [PATCH v2 02/11] vfio: Add capability chain helpers Alex Williamson <alex.williamson@redhat.com> - 2016-02-13 01:10 +0100
  [PATCH v2 01/11] vfio: Define capability chains Alex Williamson <alex.williamson@redhat.com> - 2016-02-13 01:10 +0100
  [PATCH v2 09/11] vfio/pci: Intel IGD host and LCP bridge config  space access Alex Williamson <alex.williamson@redhat.com> - 2016-02-13 01:10 +0100
  [PATCH v2 11/11] vfio/pci: Expose shadow ROM as PCI option ROM Alex Williamson <alex.williamson@redhat.com> - 2016-02-13 01:10 +0100
  [PATCH v2 07/11] vfio/pci: Enable virtual register in PCI config  space Alex Williamson <alex.williamson@redhat.com> - 2016-02-13 01:10 +0100

csiph-web