Re: [RFC PATCH 2/3] vfio-pci: Allow to mmap sub-page MMIO BARs if all MMIO BARs are page aligned

From: Alex Williamson
Date: Wed Dec 16 2015 - 15:04:47 EST


On Fri, 2015-12-11 at 16:53 +0800, Yongji Xie wrote:
> Current vfio-pci implementation disallows to mmap
> sub-page(size < PAGE_SIZE) MMIO BARs because these BARs' mmio page
> may be shared with other BARs.
>
> But we should allow to mmap these sub-page MMIO BARs if all MMIO BARs
> are page aligned which leads the BARs' mmio page would not be shared
> with other BARs.
>
> This patch adds support for this case and we also add a
> VFIO_DEVICE_FLAGS_PCI_PAGE_ALIGNED flag to notify userspace that
> platform supports all MMIO BARs to be page aligned.
>
> Signed-off-by: Yongji Xie <xyjxie@xxxxxxxxxxxxxxxxxx>
> ---
> Âdrivers/vfio/pci/vfio_pci.cÂÂÂÂÂÂÂÂÂ|ÂÂÂ10 +++++++++-
> Âdrivers/vfio/pci/vfio_pci_private.h |ÂÂÂÂ5 +++++
> Âinclude/uapi/linux/vfio.hÂÂÂÂÂÂÂÂÂÂÂ|ÂÂÂÂ2 ++
> Â3 files changed, 16 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/vfio/pci/vfio_pci.c
> b/drivers/vfio/pci/vfio_pci.c
> index 32b88bd..dbcad99 100644
> --- a/drivers/vfio/pci/vfio_pci.c
> +++ b/drivers/vfio/pci/vfio_pci.c
> @@ -443,6 +443,9 @@ static long vfio_pci_ioctl(void *device_data,
> Â if (vdev->reset_works)
> Â info.flags |= VFIO_DEVICE_FLAGS_RESET;
> Â
> + if (vfio_pci_bar_page_aligned())
> + info.flags |=
> VFIO_DEVICE_FLAGS_PCI_PAGE_ALIGNED;
> +
> Â info.num_regions = VFIO_PCI_NUM_REGIONS;
> Â info.num_irqs = VFIO_PCI_NUM_IRQS;
> Â
> @@ -479,7 +482,8 @@ 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 ||
> + ÂÂÂÂvfio_pci_bar_page_aligned()))
> Â info.flags |=
> VFIO_REGION_INFO_FLAG_MMAP;
> Â break;
> Â case VFIO_PCI_ROM_REGION_INDEX:
> @@ -855,6 +859,10 @@ static int vfio_pci_mmap(void *device_data,
> struct vm_area_struct *vma)
> Â return -EINVAL;
> Â
> Â phys_len = pci_resource_len(pdev, index);
> +
> + if (vfio_pci_bar_page_aligned())
> + phys_len = PAGE_ALIGN(phys_len);
> +
> Â req_len = vma->vm_end - vma->vm_start;
> Â pgoff = vma->vm_pgoff &
> Â ((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
> diff --git a/drivers/vfio/pci/vfio_pci_private.h
> b/drivers/vfio/pci/vfio_pci_private.h
> index 0e7394f..319352a 100644
> --- a/drivers/vfio/pci/vfio_pci_private.h
> +++ b/drivers/vfio/pci/vfio_pci_private.h
> @@ -69,6 +69,11 @@ struct vfio_pci_device {
> Â#define is_irq_none(vdev) (!(is_intx(vdev) || is_msi(vdev) ||
> is_msix(vdev)))
> Â#define irq_is(vdev, type) (vdev->irq_type == type)
> Â
> +static inline bool vfio_pci_bar_page_aligned(void)
> +{
> + return IS_ENABLED(CONFIG_PPC64);
> +}

I really dislike this. ÂThis is a problem for any architecture that
runs on larger pages, and even an annoyance on 4k hosts. ÂWhy are we
only solving it for PPC64? ÂCan't we do something similar in the core
PCI code and detect it?

> +
> Âextern void vfio_pci_intx_mask(struct vfio_pci_device *vdev);
> Âextern void vfio_pci_intx_unmask(struct vfio_pci_device *vdev);
> Â
> diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h
> index 751b69f..1fc8066 100644
> --- a/include/uapi/linux/vfio.h
> +++ b/include/uapi/linux/vfio.h
> @@ -171,6 +171,8 @@ struct vfio_device_info {
> Â#define VFIO_DEVICE_FLAGS_PCI (1 << 1) /* vfio-pci
> device */
> Â#define VFIO_DEVICE_FLAGS_PLATFORM (1 << 2) /* vfio-platform
> device */
> Â#define VFIO_DEVICE_FLAGS_AMBAÂÂ(1 << 3) /* vfio-amba device
> */
> +/* Platform support all PCI MMIO BARs to be page aligned */
> +#define VFIO_DEVICE_FLAGS_PCI_PAGE_ALIGNED (1 << 4)
> Â __u32 num_regions; /* Max region index + 1 */
> Â __u32 num_irqs; /* Max IRQ index + 1 */
> Â};

Why is this on the device info, shouldn't it be per region? ÂDo we even
need a flag or can we just set the existing mmap flag with the
clarification that sub-host page size regions can mmap an entire host-
page aligned, sized area in the documentation? ÂThanks,

Alex
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/