Add Mediated device support
Subsystems (5)
Kirti Wankhede
maintainerv11 cover Add Mediated device support
This series adds Mediated device support to Linux host kernel. Purpose of this series is to provide a common interface for mediated device management that can be used by different devices. This series introduces Mdev core module that creates and manages mediated devices, VFIO based driver for mediated devices that are created by mdev core module and update VFIO type1 IOMMU module to support pinning & unpinning for mediated devices.
What changed in v11?
mdev core:
Register mdev_bus class when first device is registed to avoid panic if any vendor driver and mdev driver are selected as built-in but vendor driver loads first and then mdev module.vfio_mdev:
Added notifier callback function to mdev parent's ops so that notifer is registered from vfio_mdev module during device open and unregistered it from device close call. This is a optional callback. Some drivers using mdev framework might not pin or unpin pages, for example the sample mtty driver that simulates serial port. Vendor driver who need to pin/unpin pages should provide this callback. Otherwise pin request would fail.vfio_iommu_type1:
Updated to keep track of who (task and address space) mapped iova range. During DMA_UNMAP, same task who mapped it or other task who shares same address space is allowed to unmap, otherwise unmap fails. QEMU maps few iova ranges initially, then fork threads and from the child thread calls DMA_UNMAP on previously mapped iova. Since child shares same address space, DMA_UNMAP is successful.
Address space keeps track of pages pinned (pfn_list) by external user / mdev devices. This pfn_list is used to verify pfn during unpin_request, re-accounting of pages when direct device assigned in hot-unplugged and mdev device is present in same container.
When the container is released, all mapped iova from all tasks are unmapped and removed.
Tested by assigning below combinations of devices to a single VM:
- GPU pass through only
- vGPU device only
- One GPU pass through and one vGPU device
- Linux VM hot plug and unplug vGPU device while GPU pass through device exist
- Linux VM hot plug and unplug GPU pass through device while vGPU device exist
Patch series tested with linux-next upto commit 14970f204b19 @Fri Oct 28 Resolved against conflicting change:
05692d7005a3 vfio/pci: Fix integer overflows, bitmask check
Kirti Wankhede (22):
vfio: Mediated device Core driver
vfio: VFIO based driver for Mediated devices
vfio: Rearrange functions to get vfio_group from dev
vfio: Common function to increment container_users
vfio iommu: Added pin and unpin callback functions to
vfio_iommu_driver_ops
vfio iommu type1: Update arguments of vfio_lock_acct vfio iommu type1: Update argument of vaddr_get_pfn() vfio iommu type1: Add find_iommu_group() function
vfio iommu type1: Add task structure to vfio_dma
vfio iommu type1: Add support for mediated devices
vfio iommu: Add blocking notifier to notify DMA_UNMAP
vfio: Add notifier callback to parent's ops structure of mdev
vfio: Introduce common function to add capabilities
vfio_pci: Update vfio_pci to use vfio_info_add_capability()
vfio: Introduce vfio_set_irqs_validate_and_prepare()
vfio_pci: Updated to use vfio_set_irqs_validate_and_prepare()
vfio_platform: Updated to use vfio_set_irqs_validate_and_prepare()
vfio: Define device_api strings
docs: Add Documentation for Mediated devices
docs: Sysfs ABI for mediated device framework
docs: Sample driver to demonstrate how to use Mediated device
framework.
MAINTAINERS: Add entry VFIO based Mediated device drivers
Documentation/ABI/testing/sysfs-bus-vfio-mdev | 111 ++ Documentation/vfio-mediated-device.txt | 399 +++++++ MAINTAINERS | 9 + drivers/vfio/Kconfig | 1 + drivers/vfio/Makefile | 1 + drivers/vfio/mdev/Kconfig | 17 + drivers/vfio/mdev/Makefile | 5 + drivers/vfio/mdev/mdev_core.c | 388 +++++++ drivers/vfio/mdev/mdev_driver.c | 122 ++ drivers/vfio/mdev/mdev_private.h | 41 + drivers/vfio/mdev/mdev_sysfs.c | 286 +++++ drivers/vfio/mdev/vfio_mdev.c | 167 +++ drivers/vfio/pci/vfio_pci.c | 83 +- drivers/vfio/platform/vfio_platform_common.c | 31 +- drivers/vfio/vfio.c | 334 +++++- drivers/vfio/vfio_iommu_type1.c | 831 ++++++++++++-- include/linux/mdev.h | 176 +++ include/linux/vfio.h | 32 +- include/uapi/linux/vfio.h | 10 + samples/vfio-mdev/Makefile | 13 + samples/vfio-mdev/mtty.c | 1503 +++++++++++++++++++++++++ 21 files changed, 4342 insertions(+), 218 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-bus-vfio-mdev create mode 100644 Documentation/vfio-mediated-device.txt create mode 100644 drivers/vfio/mdev/Kconfig create mode 100644 drivers/vfio/mdev/Makefile create mode 100644 drivers/vfio/mdev/mdev_core.c create mode 100644 drivers/vfio/mdev/mdev_driver.c create mode 100644 drivers/vfio/mdev/mdev_private.h create mode 100644 drivers/vfio/mdev/mdev_sysfs.c create mode 100644 drivers/vfio/mdev/vfio_mdev.c create mode 100644 include/linux/mdev.h create mode 100644 samples/vfio-mdev/Makefile create mode 100644 samples/vfio-mdev/mtty.c
Signature · 1 line
2.7.0
Kirti Wankhede
maintainerPATCH v11 1/22 vfio: Mediated device Core driver
Design for Mediated Device Driver:
Main purpose of this driver is to provide a common interface for mediated device management that can be used by different drivers of different devices.
This module provides a generic interface to create the device, add it to mediated bus, add device to IOMMU group and then add it to vfio group.
Below is the high Level block diagram, with Nvidia, Intel and IBM devices as example, since these are the devices which are going to actively use this module as of now.
+---------------+ | | | +-----------+ | mdev_register_driver() +--------------+ | | | +<------------------------+ __init() | | | mdev | | | | | | bus | +------------------------>+ |<-> VFIO user | | driver | | probe()/remove() | vfio_mdev.ko | APIs | | | | | | | +-----------+ | +--------------+ | | | MDEV CORE | | MODULE | | mdev.ko | | +-----------+ | mdev_register_device() +--------------+ | | | +<------------------------+ | | | | | | nvidia.ko |<-> physical | | | +------------------------>+ | device | | | | callback +--------------+ | | Physical | | | | device | | mdev_register_device() +--------------+ | | interface | |<------------------------+ | | | | | | i915.ko |<-> physical | | | +------------------------>+ | device | | | | callback +--------------+ | | | | | | | | mdev_register_device() +--------------+ | | | +<------------------------+ | | | | | | ccw_device.ko|<-> physical | | | +------------------------>+ | device | | | | callback +--------------+ | +-----------+ | +---------------+
Core driver provides two types of registration interfaces:
1. Registration interface for mediated bus driver:
/**
* struct mdev_driver - Mediated device's driver
* @name: driver name
* @probe: called when new device created
* @remove:called when device removed
* @driver:device driver structure
*
**/
struct mdev_driver {
const char *name;
int (*probe) (struct device *dev);
void (*remove) (struct device *dev);
struct device_driver driver;
};Mediated bus driver for mdev device should use this interface to register and unregister with core driver respectively:
int mdev_register_driver(struct mdev_driver *drv, struct module *owner); void mdev_unregister_driver(struct mdev_driver *drv);
Mediated bus driver is responsible to add/delete mediated devices to/from VFIO group when devices are bound and unbound to the driver.
2. Physical device driver interface
This interface provides vendor driver the set APIs to manage physical device related work in its driver. APIs are :
* dev_attr_groups: attributes of the parent device.
* mdev_attr_groups: attributes of the mediated device.
* supported_type_groups: attributes to define supported type. This is mandatory field.
* create: to allocate basic resources in vendor driver for a mediated device. This is mandatory to be provided by vendor driver.
* remove: to free resources in vendor driver when mediated device is destroyed. This is mandatory to be provided by vendor driver.
* open: open callback of mediated device
* release: release callback of mediated device
* read : read emulation callback.
* write: write emulation callback.
* ioctl: ioctl callback.
* mmap: mmap emulation callback.
Drivers should use these interfaces to register and unregister device to mdev core driver respectively:
extern int mdev_register_device(struct device *dev,
const struct parent_ops *ops);
extern void mdev_unregister_device(struct device *dev);
There are no locks to serialize above callbacks in mdev driver and vfio_mdev driver. If required, vendor driver can have locks to serialize above APIs in their driver.
--- drivers/vfio/Kconfig | 1 + drivers/vfio/Makefile | 1 + drivers/vfio/mdev/Kconfig | 10 + drivers/vfio/mdev/Makefile | 4 + drivers/vfio/mdev/mdev_core.c | 388 +++++++++++++++++++++++++++++++++++++++ drivers/vfio/mdev/mdev_driver.c | 122 ++++++++++++ drivers/vfio/mdev/mdev_private.h | 41 +++++ drivers/vfio/mdev/mdev_sysfs.c | 286 +++++++++++++++++++++++++++++ include/linux/mdev.h | 167 +++++++++++++++++ 9 files changed, 1020 insertions(+) create mode 100644 drivers/vfio/mdev/Kconfig create mode 100644 drivers/vfio/mdev/Makefile create mode 100644 drivers/vfio/mdev/mdev_core.c create mode 100644 drivers/vfio/mdev/mdev_driver.c create mode 100644 drivers/vfio/mdev/mdev_private.h create mode 100644 drivers/vfio/mdev/mdev_sysfs.c create mode 100644 include/linux/mdev.h
drivers/vfio/Kconfig +1 -0
@@ -48,4 +48,5 @@ menuconfig VFIO_NOIOMMU source "drivers/vfio/pci/Kconfig" source "drivers/vfio/platform/Kconfig" +source "drivers/vfio/mdev/Kconfig" source "virt/lib/Kconfig"
drivers/vfio/Makefile +1 -0
@@ -7,3 +7,4 @@ obj-$(CONFIG_VFIO_IOMMU_SPAPR_TCE) += vfio_iommu_spapr_tce.o obj-$(CONFIG_VFIO_SPAPR_EEH) += vfio_spapr_eeh.o obj-$(CONFIG_VFIO_PCI) += pci/ obj-$(CONFIG_VFIO_PLATFORM) += platform/ +obj-$(CONFIG_VFIO_MDEV) += mdev/
drivers/vfio/mdev/Kconfig new file +10 -0
@@ -0,0 +1,10 @@ + +config VFIO_MDEV + tristate "Mediated device driver framework" + depends on VFIO + default n + help + Provides a framework to virtualize devices. + See Documentation/vfio-mdev/vfio-mediated-device.txt for more details. + + If you don't know what do here, say N.
drivers/vfio/mdev/Makefile new file +4 -0
@@ -0,0 +1,4 @@ + +mdev-y := mdev_core.o mdev_sysfs.o mdev_driver.o + +obj-$(CONFIG_VFIO_MDEV) += mdev.o
drivers/vfio/mdev/mdev_core.c new file +388 -0
388 lines not on this page · raw message
drivers/vfio/mdev/mdev_driver.c new file +122 -0
122 lines not on this page · raw message
drivers/vfio/mdev/mdev_private.h new file +41 -0
41 lines not on this page · raw message
drivers/vfio/mdev/mdev_sysfs.c new file +286 -0
286 lines not on this page · raw message
include/linux/mdev.h new file +167 -0
167 lines not on this page · raw message
Signature · 1 line
2.7.0
Kirti Wankhede
maintainerPATCH v11 2/22 vfio: VFIO based driver for Mediated devices
In reply to Kirti Wankhedevfio_mdev driver registers with mdev core driver.
mdev core driver creates mediated device and calls probe routine of vfio_mdev driver for each device.
Probe routine of vfio_mdev driver adds mediated device to VFIO core module
This driver forms a shim layer that pass through VFIO devices operations to vendor driver for mediated devices.
--- drivers/vfio/mdev/Kconfig | 9 ++- drivers/vfio/mdev/Makefile | 1 + drivers/vfio/mdev/vfio_mdev.c | 148 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 drivers/vfio/mdev/vfio_mdev.c
drivers/vfio/mdev/Kconfig +8 -1
@@ -5,6 +5,13 @@ config VFIO_MDEV default n help Provides a framework to virtualize devices. - See Documentation/vfio-mdev/vfio-mediated-device.txt for more details. + See Documentation/vfio-mediated-device.txt for more details. If you don't know what do here, say N. + +config VFIO_MDEV_DEVICE + tristate "VFIO support for Mediated devices" + depends on VFIO && VFIO_MDEV + default n + help + VFIO based driver for mediated devices.
drivers/vfio/mdev/Makefile +1 -0
@@ -2,3 +2,4 @@ mdev-y := mdev_core.o mdev_sysfs.o mdev_driver.o obj-$(CONFIG_VFIO_MDEV) += mdev.o +obj-$(CONFIG_VFIO_MDEV_DEVICE) += vfio_mdev.o
drivers/vfio/mdev/vfio_mdev.c new file +148 -0
148 lines not on this page · raw message
Signature · 1 line
2.7.0
Kirti Wankhede
maintainerPATCH v11 4/22 vfio: Common function to increment container_users
In reply to Kirti WankhedeThis change rearrange functions to have common function to increment container_users
--- drivers/vfio/vfio.c | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-)
drivers/vfio/vfio.c +21 -13
@@ -1385,6 +1385,23 @@ static bool vfio_group_viable(struct vfio_group *group) group, vfio_dev_viable) == 0); } +static int vfio_group_add_container_user (struct vfio_group *group) +{ +if (!atomic_inc_not_zero (&group->container_users)) +return -EINVAL ; + +if (group->noiommu) { +atomic_dec (&group->container_users); +return -EPERM ; + } +if (!group->container->iommu_driver || !vfio_group_viable (group)) { +atomic_dec (&group->container_users); +return -EINVAL ; + } + +return 0 ; +} +static const struct file_operations vfio_device_fops;static int vfio_group_get_device_fd (struct vfio_group *group,char *buf)
@@ -1694,23 +1711,14 @@ static const struct file_operations vfio_device_fops = {struct vfio_group *vfio_group_get_external_user (struct file *filep) {struct vfio_group *group = filep->private_data; +int ret;if (filep->f_op != &vfio_group_fops)return ERR_PTR (-EINVAL ); -if (!atomic_inc_not_zero (&group->container_users)) -return ERR_PTR (-EINVAL ); - -if (group->noiommu) { -atomic_dec (&group->container_users); -return ERR_PTR (-EPERM ); - } - -if (!group->container->iommu_driver || - !vfio_group_viable (group)) { -atomic_dec (&group->container_users); -return ERR_PTR (-EINVAL ); - } + ret =vfio_group_add_container_user (group); +if (ret) +return ERR_PTR (ret);vfio_group_get (group);
Signature · 1 line
2.7.0
Kirti Wankhede
maintainerPATCH v11 5/22 vfio iommu: Added pin and unpin callback functions to vfio_iommu_driver_ops
In reply to Kirti WankhedeAdded two new callback functions to struct vfio_iommu_driver_ops. Backend IOMMU module that supports pining and unpinning pages for mdev devices should provide these functions.
Added APIs for pining and unpining pages to VFIO module. These calls back into backend iommu module to actually pin and unpin pages. Renamed static functions in vfio_type1_iommu.c to resolve conflicts
--- drivers/vfio/vfio.c | 96 +++++++++++++++++++++++++++++++++++++++++ drivers/vfio/vfio_iommu_type1.c | 20 ++++----- include/linux/vfio.h | 14 +++++- 3 files changed, 119 insertions(+), 11 deletions(-)
drivers/vfio/vfio.c +96 -0
@@ -1799,6 +1799,102 @@ void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset) }EXPORT_SYMBOL_GPL (vfio_info_cap_shift); + +/* +* Pin a set of guest PFNs and return their associated host PFNs for local +* domain only. +* @dev [in] : device +* @user_pfn [in]: array of user/guest PFNs +* @npage [in]: count of array elements +* @prot [in] : protection flags +* @phys_pfn[out] : array of host PFNs +* Return error or number of pages pinned. +*/ +int vfio_pin_pages (struct device *dev,unsigned long *user_pfn, +int npage,int prot,unsigned long *phys_pfn) +{ +struct vfio_container *container; +struct vfio_group *group; +struct vfio_iommu_driver *driver; +int ret; + +if (!dev || !user_pfn || !phys_pfn) +return -EINVAL ; + + group =vfio_group_get_from_dev (dev); +if (IS_ERR (group)) +return PTR_ERR (group); + + ret =vfio_group_add_container_user (group); +if (ret) + goto err_pin_pages; + + container = group->container; +down_read (&container->group_lock); + + driver = container->iommu_driver; +if (likely (driver && driver->ops->pin_pages)) + ret = driver->ops->pin_pages (container->iommu_data, user_pfn, + npage, prot, phys_pfn); +else + ret = -EINVAL ; + +up_read (&container->group_lock); +vfio_group_try_dissolve_container (group); + +err_pin_pages: +vfio_group_put (group); +return ret; + +} +EXPORT_SYMBOL (vfio_pin_pages); + +/* +* Unpin set of host PFNs for local domain only. +* @dev [in] : device +* @user_pfn [in]: array of user/guest PFNs to be unpinned +* @pfn [in] : array of host PFNs to be unpinned. +* @npage [in] :count of elements in array, that is number of pages. +* Return error or number of pages unpinned. +*/ +int vfio_unpin_pages (struct device *dev,unsigned long *user_pfn, +unsigned long *pfn,int npage) +{ +struct vfio_container *container; +struct vfio_group *group; +struct vfio_iommu_driver *driver; +int ret; + +if (!dev || !pfn) +return -EINVAL ; + + group =vfio_group_get_from_dev (dev); +if (IS_ERR (group)) +return PTR_ERR (group); + + ret =vfio_group_add_container_user (group); +if (ret) + goto err_unpin_pages; + + container = group->container; +down_read (&container->group_lock); + + driver = container->iommu_driver; +if (likely (driver && driver->ops->unpin_pages)) + ret = driver->ops->unpin_pages (container->iommu_data, user_pfn, + pfn, npage); +else + ret = -EINVAL ; + +up_read (&container->group_lock); +vfio_group_try_dissolve_container (group); + +err_unpin_pages: +vfio_group_put (group); +return ret; +} +EXPORT_SYMBOL (vfio_unpin_pages); +/** * Module/class support */
drivers/vfio/vfio_iommu_type1.c +10 -10
@@ -259,8 +259,8 @@ static int vaddr_get_pfn(unsigned long vaddr, int prot, unsigned long *pfn)* the iommu can only map chunks of consecutive pfns anyway, so get the * first page and all consecutive pages with the same locking. */ -static long vfio_pin_pages (unsigned long vaddr,long npage, -int prot,unsigned long *pfn_base) +static long __vfio_pin_pages_remote (unsigned long vaddr,long npage, +int prot,unsigned long *pfn_base) {unsigned long limit =rlimit (RLIMIT_MEMLOCK ) >>PAGE_SHIFT ;bool lock_cap =capable (CAP_IPC_LOCK );
@@ -318,8 +318,8 @@ static long vfio_pin_pages(unsigned long vaddr, long npage,return i; } -static long vfio_unpin_pages (unsigned long pfn,long npage, -int prot,bool do_accounting) +static long __vfio_unpin_pages_remote (unsigned long pfn,long npage, +int prot,bool do_accounting) {unsigned long unlocked =0 ;long i;
@@ -382,9 +382,9 @@ static void vfio_unmap_unpin(struct vfio_iommu *iommu, struct vfio_dma *dma)if (WARN_ON (!unmapped))break ; - unlocked +=vfio_unpin_pages (phys >>PAGE_SHIFT , - unmapped >>PAGE_SHIFT , - dma->prot, false); + unlocked +=__vfio_unpin_pages_remote (phys >>PAGE_SHIFT , + unmapped >>PAGE_SHIFT , + dma->prot, false); iova += unmapped;cond_resched ();
@@ -613,8 +613,8 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu,while (size) {/* Pin a contiguous chunk of memory */ - npage =vfio_pin_pages (vaddr + dma->size, - size >>PAGE_SHIFT , prot, &pfn); + npage =__vfio_pin_pages_remote (vaddr + dma->size, + size >>PAGE_SHIFT , prot, &pfn);if (npage <=0 ) {WARN_ON (!npage); ret = (int )npage;
@@ -624,7 +624,7 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu,/* Map it! */ ret =vfio_iommu_map (iommu, iova + dma->size, pfn, npage, prot);if (ret) { -vfio_unpin_pages (pfn, npage, prot, true); +__vfio_unpin_pages_remote (pfn, npage, prot, true);break ; }
include/linux/vfio.h +13 -1
@@ -75,7 +75,13 @@ struct vfio_iommu_driver_ops { struct iommu_group *group);void (*detach_group)(void *iommu_data,struct iommu_group *group); - +int (*pin_pages)(void *iommu_data,unsigned long *user_pfn, +int npage,int prot, +unsigned long *phys_pfn); +int (*unpin_pages)(void *iommu_data, +unsigned long *user_pfn, +unsigned long *pfn, +int npage); };extern int vfio_register_iommu_driver (const struct vfio_iommu_driver_ops *ops);
@@ -127,6 +133,12 @@ static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group, }#endif /* CONFIG_EEH */ +extern int vfio_pin_pages (struct device *dev,unsigned long *user_pfn, +int npage,int prot,unsigned long *phys_pfn); + +extern int vfio_unpin_pages (struct device *dev,unsigned long *user_pfn, +unsigned long *pfn,int npage); +/* * IRQfd - generic */
Signature · 1 line
2.7.0
Kirti Wankhede
maintainerPATCH v11 7/22 vfio iommu type1: Update argument of vaddr_get_pfn()
In reply to Kirti WankhedeUpdate arguments of vaddr_get_pfn() to take struct mm_struct *mm as input argument.
--- drivers/vfio/vfio_iommu_type1.c | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-)
drivers/vfio/vfio_iommu_type1.c +23 -7
@@ -226,20 +226,36 @@ static int put_pfn(unsigned long pfn, int prot)return 0 ; } -static int vaddr_get_pfn (unsigned long vaddr,int prot,unsigned long *pfn) +static int vaddr_get_pfn (struct mm_struct *mm,unsigned long vaddr, +int prot,unsigned long *pfn) {struct page *page[1 ];struct vm_area_struct *vma;int ret = -EFAULT ; -if (get_user_pages_fast (vaddr,1 , !!(prot &IOMMU_WRITE ), page) ==1 ) { +if (mm == current->mm) + ret =get_user_pages_fast (vaddr,1 , !!(prot &IOMMU_WRITE ), + page); +else { +unsigned int flags =0 ; + +if (prot &IOMMU_WRITE ) + flags |=FOLL_WRITE ; + +down_read (&mm->mmap_sem); + ret =get_user_pages_remote (NULL , mm, vaddr,1 , flags, page, +NULL ); +up_read (&mm->mmap_sem); + } + +if (ret ==1 ) { *pfn =page_to_pfn (page[0 ]);return 0 ; } -down_read (¤t->mm->mmap_sem); +down_read (&mm->mmap_sem); - vma =find_vma_intersection (current->mm, vaddr, vaddr +1 ); + vma =find_vma_intersection (mm, vaddr, vaddr +1 );if (vma && vma->vm_flags &VM_PFNMAP ) { *pfn = ((vaddr - vma->vm_start) >>PAGE_SHIFT ) + vma->vm_pgoff;
@@ -247,7 +263,7 @@ static int vaddr_get_pfn(unsigned long vaddr, int prot, unsigned long *pfn) ret =0 ; } -up_read (¤t->mm->mmap_sem); +up_read (&mm->mmap_sem);return ret; }
@@ -268,7 +284,7 @@ static long __vfio_pin_pages_remote(unsigned long vaddr, long npage,if (!current->mm)return -ENODEV ; - ret =vaddr_get_pfn (vaddr, prot, pfn_base); + ret =vaddr_get_pfn (current->mm, vaddr, prot, pfn_base);if (ret)return ret;
@@ -291,7 +307,7 @@ static long __vfio_pin_pages_remote(unsigned long vaddr, long npage,for (i =1 , vaddr +=PAGE_SIZE ; i < npage; i++, vaddr +=PAGE_SIZE ) {unsigned long pfn =0 ; - ret =vaddr_get_pfn (vaddr, prot, &pfn); + ret =vaddr_get_pfn (current->mm, vaddr, prot, &pfn);if (ret)break ;
Signature · 1 line
2.7.0
Kirti Wankhede
maintainerPATCH v11 6/22 vfio iommu type1: Update arguments of vfio_lock_acct
In reply to Kirti WankhedeUpdated arguments of vfio_lock_acct to take mm structure as input argument
--- drivers/vfio/vfio_iommu_type1.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-)
drivers/vfio/vfio_iommu_type1.c +10 -12
@@ -150,17 +150,16 @@ static void vfio_lock_acct_bg(struct work_struct *work)kfree (vwork); } -static void vfio_lock_acct (long npage) +static void vfio_lock_acct (struct mm_struct *mm,long npage) {struct vwork *vwork; -struct mm_struct *mm; -if (!current->mm || !npage) +if (!mm || !npage)return ;/* process exited or nothing to do */ -if (down_write_trylock (¤t->mm->mmap_sem)) { - current->mm->locked_vm += npage; -up_write (¤t->mm->mmap_sem); +if (down_write_trylock (&mm->mmap_sem)) { + mm->locked_vm += npage; +up_write (&mm->mmap_sem);return ; }
@@ -172,8 +171,7 @@ static void vfio_lock_acct(long npage) vwork =kmalloc (sizeof (struct vwork ),GFP_KERNEL );if (!vwork)return ; - mm =get_task_mm (current); -if (!mm) { +if (!mmget_not_zero (mm)) {kfree (vwork);return ; }
@@ -285,7 +283,7 @@ static long __vfio_pin_pages_remote(unsigned long vaddr, long npage,if (unlikely (disable_hugepages)) {if (!rsvd) -vfio_lock_acct (1 ); +vfio_lock_acct (current->mm,1 );return 1 ; }
@@ -313,7 +311,7 @@ static long __vfio_pin_pages_remote(unsigned long vaddr, long npage, }if (!rsvd) -vfio_lock_acct (i); +vfio_lock_acct (current->mm, i);return i; }
@@ -328,7 +326,7 @@ static long __vfio_unpin_pages_remote(unsigned long pfn, long npage, unlocked +=put_pfn (pfn++, prot);if (do_accounting) -vfio_lock_acct (-unlocked); +vfio_lock_acct (current->mm, -unlocked);return unlocked; }
@@ -390,7 +388,7 @@ static void vfio_unmap_unpin(struct vfio_iommu *iommu, struct vfio_dma *dma)cond_resched (); } -vfio_lock_acct (-unlocked); +vfio_lock_acct (current->mm, -unlocked); }static void vfio_remove_dma (struct vfio_iommu *iommu,struct vfio_dma *dma)
Signature · 1 line
2.7.0
Kirti Wankhede
maintainerPATCH v11 8/22 vfio iommu type1: Add find_iommu_group() function
In reply to Kirti WankhedeAdd find_iommu_group()
--- drivers/vfio/vfio_iommu_type1.c | 58 ++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 24 deletions(-)
drivers/vfio/vfio_iommu_type1.c +34 -24
@@ -748,11 +748,24 @@ static void vfio_test_domain_fgsp(struct vfio_domain *domain)__free_pages (pages, order); } +static struct vfio_group *find_iommu_group (struct vfio_domain *domain, +struct iommu_group *iommu_group) +{ +struct vfio_group *g; + +list_for_each_entry (g, &domain->group_list, next) { +if (g->iommu_group == iommu_group) +return g; + } + +return NULL ; +} +static int vfio_iommu_type1_attach_group (void *iommu_data,struct iommu_group *iommu_group) {struct vfio_iommu *iommu = iommu_data; -struct vfio_group *group, *g; +struct vfio_group *group;struct vfio_domain *domain, *d;struct bus_type *bus =NULL ;int ret;
@@ -760,10 +773,7 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,mutex_lock (&iommu->lock);list_for_each_entry (d, &iommu->domain_list, next) { -list_for_each_entry (g, &d->group_list, next) { -if (g->iommu_group != iommu_group) -continue ; - +if (find_iommu_group (d, iommu_group)) {mutex_unlock (&iommu->lock);return -EINVAL ; }
@@ -882,28 +892,28 @@ static void vfio_iommu_type1_detach_group(void *iommu_data,mutex_lock (&iommu->lock); +list_for_each_entry (domain, &iommu->domain_list, next) { -list_for_each_entry (group, &domain->group_list, next) { -if (group->iommu_group != iommu_group) -continue ; + group =find_iommu_group (domain, iommu_group); +if (!group) +continue ; -iommu_detach_group (domain->domain, iommu_group); -list_del (&group->next); -kfree (group); -/* -* Group ownership provides privilege, if the group -* list is empty, the domain goes away. If it's the -* last domain, then all the mappings go away too. -*/ -if (list_empty (&domain->group_list)) { -if (list_is_singular (&iommu->domain_list)) -vfio_iommu_unmap_unpin_all (iommu); -iommu_domain_free (domain->domain); -list_del (&domain->next); -kfree (domain); - } - goto done; +iommu_detach_group (domain->domain, iommu_group); +list_del (&group->next); +kfree (group); +/* +* Group ownership provides privilege, if the group +* list is empty, the domain goes away. If it's the +* last domain, then all the mappings go away too. +*/ +if (list_empty (&domain->group_list)) { +if (list_is_singular (&iommu->domain_list)) +vfio_iommu_unmap_unpin_all (iommu); +iommu_domain_free (domain->domain); +list_del (&domain->next); +kfree (domain); } + goto done; } done:
Signature · 1 line
2.7.0
Kirti Wankhede
maintainerPATCH v11 9/22 vfio iommu type1: Add task structure to vfio_dma
In reply to Kirti WankhedeAdd task structure to vfio_dma.
Add address space structure. Each vfio_dma structure points to the address space of the task who mapped it.
List of address spaces is maintained in vfio_iommu structure.
Quoted 1 line
From
DMA_MAPcall if address space already exist in address space list,
vfio_dma points to it. If address space doesn't exist, allocate address space, save pointer of mm to it and vfio_dma points to it. Two tasks can share same address space and so we need keep address space structure different from task in vfio_dma structure. vfio_dma keeps pointer to its corresponding address space.
During DMA_UNMAP, same task who mapped it or other task who shares same address space is allowed to unmap, otherwise unmap fails. QEMU maps few iova ranges initially, then fork threads and from the child thread calls DMA_UNMAP on previously mapped iova. Since child shares same address space, DMA_UNMAP is successful.
This address space structure is used to track pages pinned by external user in later changes.
--- drivers/vfio/vfio_iommu_type1.c | 182 +++++++++++++++++++++++++++++----------- 1 file changed, 134 insertions(+), 48 deletions(-)
drivers/vfio/vfio_iommu_type1.c +134 -48
@@ -55,12 +55,20 @@ MODULE_PARM_DESC(disable_hugepages,struct vfio_iommu {struct list_head domain_list; +struct list_head addr_space_list;struct mutex lock;struct rb_root dma_list;bool v2;bool nesting; }; +/* address space */ +struct vfio_addr_space { +struct mm_struct *mm; +struct list_head next; +atomic_t ref_count; +}; +struct vfio_domain {struct iommu_domain *domain;struct list_head next;
@@ -75,6 +83,9 @@ struct vfio_dma {unsigned long vaddr;/* Process virtual addr */ size_t size;/* Map size (bytes) */ int prot;/* IOMMU_READ/WRITE */ +struct vfio_addr_space *addr_space; +struct task_struct *task; +bool mlock_cap; };struct vfio_group {
@@ -130,6 +141,18 @@ static void vfio_unlink_dma(struct vfio_iommu *iommu, struct vfio_dma *old)rb_erase (&old->node, &iommu->dma_list); } +static struct vfio_addr_space *vfio_find_addr_space (struct vfio_iommu *iommu, +struct mm_struct *mm) +{ +struct vfio_addr_space *as; + +list_for_each_entry (as, &iommu->addr_space_list, next) { +if (as->mm == mm) +return as; + } +return NULL ; +} +struct vwork {struct mm_struct *mm;long npage;
@@ -273,24 +296,24 @@ static int vaddr_get_pfn(struct mm_struct *mm, unsigned long vaddr,* the iommu can only map chunks of consecutive pfns anyway, so get the * first page and all consecutive pages with the same locking. */ -static long __vfio_pin_pages_remote (unsigned long vaddr,long npage, -int prot,unsigned long *pfn_base) +static long __vfio_pin_pages_remote (struct vfio_dma *dma,unsigned long vaddr, +long npage,int prot, +unsigned long *pfn_base) { -unsigned long limit =rlimit (RLIMIT_MEMLOCK ) >>PAGE_SHIFT ; -bool lock_cap =capable (CAP_IPC_LOCK ); +struct task_struct *task = dma->task; +unsigned long limit =task_rlimit (task,RLIMIT_MEMLOCK ) >>PAGE_SHIFT ; +bool lock_cap = dma->mlock_cap; +struct mm_struct *mm = dma->addr_space->mm;long ret, i;bool rsvd; -if (!current->mm) -return -ENODEV ; - - ret =vaddr_get_pfn (current->mm, vaddr, prot, pfn_base); + ret =vaddr_get_pfn (mm, vaddr, prot, pfn_base);if (ret)return ret; rsvd =is_invalid_reserved_pfn (*pfn_base); -if (!rsvd && !lock_cap && current->mm->locked_vm +1 > limit) { +if (!rsvd && !lock_cap && mm->locked_vm +1 > limit) {put_pfn (*pfn_base, prot);pr_warn ("%s: RLIMIT_MEMLOCK (%ld) exceeded\n" , __func__, limit <<PAGE_SHIFT );
@@ -299,7 +322,7 @@ static long __vfio_pin_pages_remote(unsigned long vaddr, long npage,if (unlikely (disable_hugepages)) {if (!rsvd) -vfio_lock_acct (current->mm,1 ); +vfio_lock_acct (mm,1 );return 1 ; }
@@ -307,7 +330,7 @@ static long __vfio_pin_pages_remote(unsigned long vaddr, long npage,for (i =1 , vaddr +=PAGE_SIZE ; i < npage; i++, vaddr +=PAGE_SIZE ) {unsigned long pfn =0 ; - ret =vaddr_get_pfn (current->mm, vaddr, prot, &pfn); + ret =vaddr_get_pfn (mm, vaddr, prot, &pfn);if (ret)break ;
@@ -318,7 +341,7 @@ static long __vfio_pin_pages_remote(unsigned long vaddr, long npage, }if (!rsvd && !lock_cap && - current->mm->locked_vm + i +1 > limit) { + mm->locked_vm + i +1 > limit) {put_pfn (pfn, prot);pr_warn ("%s: RLIMIT_MEMLOCK (%ld) exceeded\n" , __func__, limit <<PAGE_SHIFT );
@@ -327,13 +350,13 @@ static long __vfio_pin_pages_remote(unsigned long vaddr, long npage, }if (!rsvd) -vfio_lock_acct (current->mm, i); +vfio_lock_acct (mm, i);return i; } -static long __vfio_unpin_pages_remote (unsigned long pfn,long npage, -int prot,bool do_accounting) +static long __vfio_unpin_pages_remote (struct vfio_dma *dma,unsigned long pfn, +long npage,int prot,bool do_accounting) {unsigned long unlocked =0 ;long i;
@@ -342,7 +365,7 @@ static long __vfio_unpin_pages_remote(unsigned long pfn, long npage, unlocked +=put_pfn (pfn++, prot);if (do_accounting) -vfio_lock_acct (current->mm, -unlocked); +vfio_lock_acct (dma->addr_space->mm, -unlocked);return unlocked; }
@@ -396,7 +419,7 @@ static void vfio_unmap_unpin(struct vfio_iommu *iommu, struct vfio_dma *dma)if (WARN_ON (!unmapped))break ; - unlocked +=__vfio_unpin_pages_remote (phys >>PAGE_SHIFT , + unlocked +=__vfio_unpin_pages_remote (dma, phys >>PAGE_SHIFT , unmapped >>PAGE_SHIFT , dma->prot, false); iova += unmapped;
@@ -404,13 +427,20 @@ static void vfio_unmap_unpin(struct vfio_iommu *iommu, struct vfio_dma *dma)cond_resched (); } -vfio_lock_acct (current->mm, -unlocked); +vfio_lock_acct (dma->addr_space->mm, -unlocked); }static void vfio_remove_dma (struct vfio_iommu *iommu,struct vfio_dma *dma) {vfio_unmap_unpin (iommu, dma);vfio_unlink_dma (iommu, dma); + +if (atomic_dec_and_test (&dma->addr_space->ref_count)) { +mmput (dma->addr_space->mm); +put_task_struct (dma->task); +list_del (&dma->addr_space->next); +kfree (dma->addr_space); + }kfree (dma); }
@@ -506,6 +536,12 @@ static int vfio_dma_do_unmap(struct vfio_iommu *iommu,while ((dma =vfio_find_dma (iommu, unmap->iova, unmap->size))) {if (!iommu->v2 && unmap->iova > dma->iova)break ; +/* +* Task with same address space who mapped this iova range is +* allowed to unmap the iova range. +*/ +if (dma->task->mm != current->mm) +break ; unmapped += dma->size;vfio_remove_dma (iommu, dma); }
@@ -572,17 +608,58 @@ unwind:return ret; } +static int vfio_pin_map_dma (struct vfio_iommu *iommu,struct vfio_dma *dma, +size_t map_size) +{ +dma_addr_t iova = dma->iova; +unsigned long vaddr = dma->vaddr; +size_t size = map_size; +long npage; +unsigned long pfn; +int ret =0 ; + +while (size) { +/* Pin a contiguous chunk of memory */ + npage =__vfio_pin_pages_remote (dma, vaddr + dma->size, + size >>PAGE_SHIFT , dma->prot, + &pfn); +if (npage <=0 ) { +WARN_ON (!npage); + ret = (int )npage; +break ; + } + +/* Map it! */ + ret =vfio_iommu_map (iommu, iova + dma->size, pfn, npage, + dma->prot); +if (ret) { +__vfio_unpin_pages_remote (dma, pfn, npage, dma->prot, + true); +break ; + } + + size -= npage <<PAGE_SHIFT ; + dma->size += npage <<PAGE_SHIFT ; + } + +if (ret) +vfio_remove_dma (iommu, dma); + +return ret; +} +static int vfio_dma_do_map (struct vfio_iommu *iommu,struct vfio_iommu_type1_dma_map *map) {dma_addr_t iova = map->iova;unsigned long vaddr = map->vaddr;size_t size = map->size; -long npage;int ret =0 , prot =0 ;uint64_t mask;struct vfio_dma *dma; -unsigned long pfn; +struct vfio_addr_space *addr_space; +struct mm_struct *mm; +bool free_addr_space_on_err = false;/* Verify that none of our __u64 fields overflow */ if (map->size != size || map->vaddr != vaddr || map->iova != iova)
@@ -608,47 +685,56 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu,mutex_lock (&iommu->lock);if (vfio_find_dma (iommu, iova, size)) { -mutex_unlock (&iommu->lock); -return -EEXIST ; + ret = -EEXIST ; + goto do_map_err; + } + + mm =get_task_mm (current); +if (!mm) { + ret = -ENODEV ; + goto do_map_err; + } + + addr_space =vfio_find_addr_space (iommu, mm); +if (addr_space) { +atomic_inc (&addr_space->ref_count); +mmput (mm); + }else { + addr_space =kzalloc (sizeof (*addr_space),GFP_KERNEL ); +if (!addr_space) { + ret = -ENOMEM ; + goto do_map_err; + } + addr_space->mm = mm; +atomic_set (&addr_space->ref_count,1 ); +list_add (&addr_space->next, &iommu->addr_space_list); + free_addr_space_on_err = true; } dma =kzalloc (sizeof (*dma),GFP_KERNEL );if (!dma) { -mutex_unlock (&iommu->lock); -return -ENOMEM ; +if (free_addr_space_on_err) { +mmput (mm); +list_del (&addr_space->next); +kfree (addr_space); + } + ret = -ENOMEM ; + goto do_map_err; } dma->iova = iova; dma->vaddr = vaddr; dma->prot = prot; + dma->addr_space = addr_space; +get_task_struct (current); + dma->task = current; + dma->mlock_cap =capable (CAP_IPC_LOCK );/* Insert zero-sized and grow as we map chunks of it */ vfio_link_dma (iommu, dma); -while (size) { -/* Pin a contiguous chunk of memory */ - npage =__vfio_pin_pages_remote (vaddr + dma->size, - size >>PAGE_SHIFT , prot, &pfn); -if (npage <=0 ) { -WARN_ON (!npage); - ret = (int )npage; -break ; - } - -/* Map it! */ - ret =vfio_iommu_map (iommu, iova + dma->size, pfn, npage, prot); -if (ret) { -__vfio_unpin_pages_remote (pfn, npage, prot, true); -break ; - } - - size -= npage <<PAGE_SHIFT ; - dma->size += npage <<PAGE_SHIFT ; - } - -if (ret) -vfio_remove_dma (iommu, dma); - + ret =vfio_pin_map_dma (iommu, dma, size); +do_map_err:mutex_unlock (&iommu->lock);return ret; }
Signature · 1 line
2.7.0
Kirti Wankhede
maintainerPATCH v11 10/22 vfio iommu type1: Add support for mediated devices
In reply to Kirti WankhedeVFIO IOMMU drivers are designed for the devices which are IOMMU capable. Mediated device only uses IOMMU APIs, the underlying hardware can be managed by an IOMMU domain.
Aim of this change is:
- To use most of the code of TYPE1 IOMMU driver for mediated devices
- To support direct assigned device and mediated device in single module
This change adds pin and unpin support for mediated device to TYPE1 IOMMU backend module. More details:
- vfio_pin_pages() callback here uses task and address space of vfio_dma, that is, of the process who mapped that iova range.
- Added pfn_list tracking logic to address space structure. All pages pinned through this interface are trached in its address space.
- Pinned pages list is used to verify unpinning request and to unpin remaining pages while detaching the group for that device.
- Page accounting is updated to account in its address space where the pages are pinned/unpinned.
- Accouting for mdev device is only done if there is no iommu capable domain in the container. When there is a direct device assigned to the container and that domain is iommu capable, all pages are already pinned during DMA_MAP.
- Page accouting is updated on hot plug and unplug mdev device and pass through device.
Tested by assigning below combinations of devices to a single VM:
- GPU pass through only
- vGPU device only
- One GPU pass through and one vGPU device
- Linux VM hot plug and unplug vGPU device while GPU pass through device exist
- Linux VM hot plug and unplug GPU pass through device while vGPU device exist
--- drivers/vfio/vfio_iommu_type1.c | 538 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 500 insertions(+), 38 deletions(-)
drivers/vfio/vfio_iommu_type1.c +500 -38
@@ -36,6 +36,7 @@#include <linux/uaccess.h> #include <linux/vfio.h> #include <linux/workqueue.h> +#include <linux/mdev.h> #define DRIVER_VERSION "0.2"#define DRIVER_AUTHOR "Alex Williamson <[email protected]>"
@@ -56,6 +57,7 @@ MODULE_PARM_DESC(disable_hugepages,struct vfio_iommu {struct list_head domain_list;struct list_head addr_space_list; +struct vfio_domain *external_domain;/* domain for external user */ struct mutex lock;struct rb_root dma_list;bool v2;
@@ -67,6 +69,9 @@ struct vfio_addr_space {struct mm_struct *mm;struct list_head next;atomic_t ref_count; +/* external user pinned pfns */ +struct rb_root pfn_list;/* pinned Host pfn list */ +struct mutex pfn_list_lock;/* mutex for pfn_list */ };struct vfio_domain {
@@ -83,6 +88,7 @@ struct vfio_dma {unsigned long vaddr;/* Process virtual addr */ size_t size;/* Map size (bytes) */ int prot;/* IOMMU_READ/WRITE */ +bool iommu_mapped;struct vfio_addr_space *addr_space;struct task_struct *task;bool mlock_cap;
@@ -94,6 +100,19 @@ struct vfio_group { };/* +* Guest RAM pinning working set or DMA target +*/ +struct vfio_pfn { +struct rb_node node; +unsigned long pfn;/* Host pfn */ +int prot; +atomic_t ref_count; +}; + +#define IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu) \ + (!list_empty(&iommu->domain_list)) + +/* * This code handles mapping and unmapping of user data buffers * into DMA'ble space using the IOMMU */
@@ -153,6 +172,93 @@ static struct vfio_addr_space *vfio_find_addr_space(struct vfio_iommu *iommu,return NULL ; } +/* +* Helper Functions for host pfn list +*/ +static struct vfio_pfn *vfio_find_pfn (struct vfio_addr_space *addr_space, +unsigned long pfn) +{ +struct vfio_pfn *vpfn; +struct rb_node *node = addr_space->pfn_list.rb_node; + +while (node) { + vpfn = rb_entry(node, struct vfio_pfn, node); + +if (pfn < vpfn->pfn) + node = node->rb_left; +else if (pfn > vpfn->pfn) + node = node->rb_right; +else +return vpfn; + } + +return NULL ; +} + +static void vfio_link_pfn (struct vfio_addr_space *addr_space, +struct vfio_pfn *new) +{ +struct rb_node **link, *parent =NULL ; +struct vfio_pfn *vpfn; + + link = &addr_space->pfn_list.rb_node; +while (*link) { + parent = *link; + vpfn = rb_entry(parent, struct vfio_pfn, node); + +if (new->pfn < vpfn->pfn) + link = &(*link)->rb_left; +else + link = &(*link)->rb_right; + } + +rb_link_node (&new->node, parent, link); +rb_insert_color (&new->node, &addr_space->pfn_list); +} + +static void vfio_unlink_pfn (struct vfio_addr_space *addr_space, +struct vfio_pfn *old) +{ +rb_erase (&old->node, &addr_space->pfn_list); +} + +static int vfio_add_to_pfn_list (struct vfio_addr_space *addr_space, +unsigned long pfn,int prot) +{ +struct vfio_pfn *vpfn; + + vpfn =kzalloc (sizeof (*vpfn),GFP_KERNEL ); +if (!vpfn) +return -ENOMEM ; + + vpfn->pfn = pfn; + vpfn->prot = prot; +atomic_set (&vpfn->ref_count,1 ); +vfio_link_pfn (addr_space, vpfn); +return 0 ; +} + +static void vfio_remove_from_pfn_list (struct vfio_addr_space *addr_space, +struct vfio_pfn *vpfn) +{ +vfio_unlink_pfn (addr_space, vpfn); +kfree (vpfn); +} + +static int vfio_pfn_account (struct vfio_addr_space *addr_space, +unsigned long pfn) +{ +struct vfio_pfn *p; +int ret =1 ; + +mutex_lock (&addr_space->pfn_list_lock); + p =vfio_find_pfn (addr_space, pfn); +if (p) + ret =0 ; +mutex_unlock (&addr_space->pfn_list_lock); +return ret; +} +struct vwork {struct mm_struct *mm;long npage;
@@ -304,16 +410,18 @@ static long __vfio_pin_pages_remote(struct vfio_dma *dma, unsigned long vaddr,unsigned long limit =task_rlimit (task,RLIMIT_MEMLOCK ) >>PAGE_SHIFT ;bool lock_cap = dma->mlock_cap;struct mm_struct *mm = dma->addr_space->mm; -long ret, i; +long ret, i, lock_acct;bool rsvd; ret =vaddr_get_pfn (mm, vaddr, prot, pfn_base);if (ret)return ret; + lock_acct =vfio_pfn_account (dma->addr_space, *pfn_base); + rsvd =is_invalid_reserved_pfn (*pfn_base); -if (!rsvd && !lock_cap && mm->locked_vm +1 > limit) { +if (!rsvd && !lock_cap && mm->locked_vm + lock_acct > limit) {put_pfn (*pfn_base, prot);pr_warn ("%s: RLIMIT_MEMLOCK (%ld) exceeded\n" , __func__, limit <<PAGE_SHIFT );
@@ -340,8 +448,10 @@ static long __vfio_pin_pages_remote(struct vfio_dma *dma, unsigned long vaddr,break ; } + lock_acct +=vfio_pfn_account (dma->addr_space, pfn); +if (!rsvd && !lock_cap && - mm->locked_vm + i +1 > limit) { + mm->locked_vm + lock_acct +1 > limit) {put_pfn (pfn, prot);pr_warn ("%s: RLIMIT_MEMLOCK (%ld) exceeded\n" , __func__, limit <<PAGE_SHIFT );
@@ -350,7 +460,7 @@ static long __vfio_pin_pages_remote(struct vfio_dma *dma, unsigned long vaddr, }if (!rsvd) -vfio_lock_acct (mm, i); +vfio_lock_acct (mm, lock_acct);return i; }
@@ -370,14 +480,214 @@ static long __vfio_unpin_pages_remote(struct vfio_dma *dma, unsigned long pfn,return unlocked; } -static void vfio_unmap_unpin (struct vfio_iommu *iommu,struct vfio_dma *dma) +static int __vfio_pin_page_external (struct vfio_dma *dma,unsigned long vaddr, +int prot,unsigned long *pfn_base, +bool do_accounting) +{ +struct task_struct *task = dma->task; +unsigned long limit =task_rlimit (task,RLIMIT_MEMLOCK ) >>PAGE_SHIFT ; +bool lock_cap = dma->mlock_cap; +struct mm_struct *mm = dma->addr_space->mm; +int ret; +bool rsvd; + + ret =vaddr_get_pfn (mm, vaddr, prot, pfn_base); +if (ret) +return ret; + + rsvd =is_invalid_reserved_pfn (*pfn_base); + +if (!rsvd && !lock_cap && mm->locked_vm +1 > limit) { +put_pfn (*pfn_base, prot); +pr_warn ("%s: Task %s (%d) RLIMIT_MEMLOCK (%ld) exceeded\n" , + __func__, task->comm,task_pid_nr (task), + limit <<PAGE_SHIFT ); +return -ENOMEM ; + } + +if (!rsvd && do_accounting) +vfio_lock_acct (mm,1 ); + +return 1 ; +} + +static void __vfio_unpin_page_external (struct vfio_addr_space *addr_space, +unsigned long pfn,int prot, +bool do_accounting) +{ +put_pfn (pfn, prot); + +if (do_accounting) +vfio_lock_acct (addr_space->mm,-1 ); +} + +static int vfio_unpin_pfn (struct vfio_addr_space *addr_space, +struct vfio_pfn *vpfn,bool do_accounting) +{ +__vfio_unpin_page_external (addr_space, vpfn->pfn, vpfn->prot, + do_accounting); + +if (atomic_dec_and_test (&vpfn->ref_count)) +vfio_remove_from_pfn_list (addr_space, vpfn); + +return 1 ; +} + +static int vfio_iommu_type1_pin_pages (void *iommu_data, +unsigned long *user_pfn, +int npage,int prot, +unsigned long *phys_pfn) +{ +struct vfio_iommu *iommu = iommu_data; +int i, j, ret; +unsigned long remote_vaddr; +unsigned long *pfn = phys_pfn; +struct vfio_dma *dma; +bool do_accounting; + +if (!iommu || !user_pfn || !phys_pfn) +return -EINVAL ; + +mutex_lock (&iommu->lock); + +if (!iommu->external_domain) { + ret = -EINVAL ; + goto pin_done; + } + +/* +* If iommu capable domain exist in the container then all pages are +* already pinned and accounted. Accouting should be done if there is no +* iommu capable domain in the container. +*/ + do_accounting = !IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu); + +for (i =0 ; i < npage; i++) { +struct vfio_pfn *p; +dma_addr_t iova; + + iova = user_pfn[i] <<PAGE_SHIFT ; + + dma =vfio_find_dma (iommu, iova,0 ); +if (!dma) { + ret = -EINVAL ; + goto pin_unwind; + } + + remote_vaddr = dma->vaddr + iova - dma->iova; + + ret =__vfio_pin_page_external (dma, remote_vaddr, prot, + &pfn[i], do_accounting); +if (ret <=0 ) { +WARN_ON (!ret); + goto pin_unwind; + } + +mutex_lock (&dma->addr_space->pfn_list_lock); + +/* search if pfn exist */ + p =vfio_find_pfn (dma->addr_space, pfn[i]); +if (p) { +atomic_inc (&p->ref_count); +mutex_unlock (&dma->addr_space->pfn_list_lock); +continue ; + } + + ret =vfio_add_to_pfn_list (dma->addr_space, pfn[i], prot); +mutex_unlock (&dma->addr_space->pfn_list_lock); + +if (ret) { +__vfio_unpin_page_external (dma->addr_space, pfn[i], + prot, do_accounting); + goto pin_unwind; + } + } + + ret = i; + goto pin_done; + +pin_unwind: + pfn[i] =0 ; +for (j =0 ; j < i; j++) { +struct vfio_pfn *p; +dma_addr_t iova; + + iova = user_pfn[j] <<PAGE_SHIFT ; + + dma =vfio_find_dma (iommu, iova,0 ); + +mutex_lock (&dma->addr_space->pfn_list_lock); + p =vfio_find_pfn (dma->addr_space, pfn[j]); +if (p) +vfio_unpin_pfn (dma->addr_space, p, do_accounting); + +mutex_unlock (&dma->addr_space->pfn_list_lock); + pfn[j] =0 ; + } + +pin_done: +mutex_unlock (&iommu->lock); +return ret; +} + +static int vfio_iommu_type1_unpin_pages (void *iommu_data, +unsigned long *user_pfn, +unsigned long *pfn, +int npage) +{ +struct vfio_iommu *iommu = iommu_data; +bool do_accounting; +int unlocked =0 , i; + +if (!iommu || !user_pfn || !pfn) +return -EINVAL ; + +mutex_lock (&iommu->lock); + +if (!iommu->external_domain) { +mutex_unlock (&iommu->lock); +return -EINVAL ; + } + + do_accounting = !IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu); + +for (i =0 ; i < npage; i++) { +struct vfio_pfn *p; +struct vfio_dma *dma; +dma_addr_t iova; + + iova = user_pfn[i] <<PAGE_SHIFT ; + + dma =vfio_find_dma (iommu, iova,0 ); +if (!dma) + goto unpin_exit; + +mutex_lock (&dma->addr_space->pfn_list_lock); +/* verify if pfn exist in pfn_list */ + p =vfio_find_pfn (dma->addr_space, pfn[i]); +if (p) + unlocked +=vfio_unpin_pfn (dma->addr_space, p, + do_accounting); +mutex_unlock (&dma->addr_space->pfn_list_lock); + } +unpin_exit: +mutex_unlock (&iommu->lock); +return unlocked; +} + +static long vfio_unmap_unpin (struct vfio_iommu *iommu,struct vfio_dma *dma, +bool do_accounting) {dma_addr_t iova = dma->iova, end = dma->iova + dma->size;struct vfio_domain *domain, *d;long unlocked =0 ;if (!dma->size) -return ; +return 0 ; + +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) +return 0 ; +/* * We use the IOMMU to track the physical addresses, otherwise we'd * need a much more complicated tracking system. Unfortunately that
@@ -427,12 +737,17 @@ static void vfio_unmap_unpin(struct vfio_iommu *iommu, struct vfio_dma *dma)cond_resched (); } -vfio_lock_acct (dma->addr_space->mm, -unlocked); + dma->iommu_mapped = false; +if (do_accounting) { +vfio_lock_acct (dma->addr_space->mm, -unlocked); +return 0 ; + } +return unlocked; }static void vfio_remove_dma (struct vfio_iommu *iommu,struct vfio_dma *dma) { -vfio_unmap_unpin (iommu, dma); +vfio_unmap_unpin (iommu, dma, true);vfio_unlink_dma (iommu, dma);if (atomic_dec_and_test (&dma->addr_space->ref_count)) {
@@ -642,6 +957,8 @@ static int vfio_pin_map_dma(struct vfio_iommu *iommu, struct vfio_dma *dma, dma->size += npage <<PAGE_SHIFT ; } + dma->iommu_mapped = true; +if (ret)vfio_remove_dma (iommu, dma);
@@ -706,6 +1023,8 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu, goto do_map_err; } addr_space->mm = mm; + addr_space->pfn_list =RB_ROOT ; +mutex_init (&addr_space->pfn_list_lock);atomic_set (&addr_space->ref_count,1 );list_add (&addr_space->next, &iommu->addr_space_list); free_addr_space_on_err = true;
@@ -733,7 +1052,11 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu,/* Insert zero-sized and grow as we map chunks of it */ vfio_link_dma (iommu, dma); - ret =vfio_pin_map_dma (iommu, dma, size); +/* Don't pin and map if container doesn't contain IOMMU capable domain*/ +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) + dma->size = size; +else + ret =vfio_pin_map_dma (iommu, dma, size); do_map_err:mutex_unlock (&iommu->lock);return ret;
@@ -762,10 +1085,6 @@ static int vfio_iommu_replay(struct vfio_iommu *iommu, d = list_first_entry(&iommu->domain_list, struct vfio_domain, next); n =rb_first (&iommu->dma_list); -/* If there's not a domain, there better not be any mappings */ -if (WARN_ON (n && !d)) -return -EINVAL ; -for (; n; n =rb_next (n)) {struct vfio_dma *dma;dma_addr_t iova;
@@ -774,20 +1093,43 @@ static int vfio_iommu_replay(struct vfio_iommu *iommu, iova = dma->iova;while (iova < dma->iova + dma->size) { -phys_addr_t phys =iommu_iova_to_phys (d->domain, iova); +phys_addr_t phys;size_t size; -if (WARN_ON (!phys)) { - iova +=PAGE_SIZE ; -continue ; - } +if (dma->iommu_mapped) { + phys =iommu_iova_to_phys (d->domain, iova); + +if (WARN_ON (!phys)) { + iova +=PAGE_SIZE ; +continue ; + } - size =PAGE_SIZE ; + size =PAGE_SIZE ; -while (iova + size < dma->iova + dma->size && - phys + size ==iommu_iova_to_phys (d->domain, +while (iova + size < dma->iova + dma->size && + phys + size ==iommu_iova_to_phys (d->domain, iova + size)) - size +=PAGE_SIZE ; + size +=PAGE_SIZE ; + }else { +unsigned long pfn; +unsigned long vaddr = dma->vaddr + + (iova - dma->iova); +size_t n = dma->iova + dma->size - iova; +long npage; + + npage =__vfio_pin_pages_remote (dma, vaddr, + n >>PAGE_SHIFT , + dma->prot, + &pfn); +if (npage <=0 ) { +WARN_ON (!npage); + ret = (int )npage; +return ret; + } + + phys = pfn <<PAGE_SHIFT ; + size = npage <<PAGE_SHIFT ; + } ret =iommu_map (domain->domain, iova, phys, size, dma->prot | domain->prot);
@@ -796,6 +1138,8 @@ static int vfio_iommu_replay(struct vfio_iommu *iommu, iova += size; } + + dma->iommu_mapped = true; }return 0 ;
@@ -853,7 +1197,7 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,struct vfio_iommu *iommu = iommu_data;struct vfio_group *group;struct vfio_domain *domain, *d; -struct bus_type *bus =NULL ; +struct bus_type *bus =NULL , *mdev_bus;int ret;mutex_lock (&iommu->lock);
@@ -865,6 +1209,13 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, } } +if (iommu->external_domain) { +if (find_iommu_group (iommu->external_domain, iommu_group)) { +mutex_unlock (&iommu->lock); +return -EINVAL ; + } + } + group =kzalloc (sizeof (*group),GFP_KERNEL ); domain =kzalloc (sizeof (*domain),GFP_KERNEL );if (!group || !domain) {
@@ -879,6 +1230,25 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,if (ret) goto out_free; + mdev_bus =symbol_get (mdev_bus_type); + +if (mdev_bus) { +if ((bus == mdev_bus) && !iommu_present (bus)) { +symbol_put (mdev_bus_type); +if (!iommu->external_domain) { +INIT_LIST_HEAD (&domain->group_list); + iommu->external_domain = domain; + }else +kfree (domain); + +list_add (&group->next, + &iommu->external_domain->group_list); +mutex_unlock (&iommu->lock); +return 0 ; + } +symbol_put (mdev_bus_type); + } + domain->domain =iommu_domain_alloc (bus);if (!domain->domain) { ret = -EIO ;
@@ -969,6 +1339,51 @@ static void vfio_iommu_unmap_unpin_all(struct vfio_iommu *iommu) vfio_remove_dma(iommu, rb_entry(node, struct vfio_dma, node)); } +static void vfio_iommu_unmap_unpin_reaccount (struct vfio_iommu *iommu) +{ +struct vfio_addr_space *as; + +list_for_each_entry (as, &iommu->addr_space_list, next) { +struct rb_node *n, *p; +long locked =0 , unlocked =0 ; + + n =rb_first (&iommu->dma_list); +for (; n; n =rb_next (n)) { +struct vfio_dma *dma; + + dma = rb_entry(n, struct vfio_dma, node); +if (dma->addr_space == as) + unlocked +=vfio_unmap_unpin (iommu, dma, false); + } + +mutex_lock (&as->pfn_list_lock); + p =rb_first (&as->pfn_list); +for (; p; p =rb_next (p)) + locked++; + +mutex_unlock (&as->pfn_list_lock); +vfio_lock_acct (as->mm, locked - unlocked); + } +} + +static void vfio_external_unpin_all (struct vfio_iommu *iommu, +bool do_accounting) +{ +struct vfio_addr_space *as; + +list_for_each_entry (as, &iommu->addr_space_list, next) { +struct rb_node *node; + +mutex_lock (&as->pfn_list_lock); +while ((node =rb_first (&as->pfn_list))) +vfio_unpin_pfn (as, + rb_entry(node, struct vfio_pfn, node), + do_accounting); + +mutex_unlock (&as->pfn_list_lock); + } +} +static void vfio_iommu_type1_detach_group (void *iommu_data,struct iommu_group *iommu_group) {
@@ -978,6 +1393,28 @@ static void vfio_iommu_type1_detach_group(void *iommu_data,mutex_lock (&iommu->lock); +if (iommu->external_domain) { + domain = iommu->external_domain; + group =find_iommu_group (domain, iommu_group); +if (group) { +list_del (&group->next); +kfree (group); + +if (list_empty (&domain->group_list)) { +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { +vfio_external_unpin_all (iommu, true); +vfio_iommu_unmap_unpin_all (iommu); + }else +vfio_external_unpin_all (iommu, false); +kfree (domain); + iommu->external_domain =NULL ; + } + goto detach_group_done; + } + } + +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) + goto detach_group_done;list_for_each_entry (domain, &iommu->domain_list, next) { group =find_iommu_group (domain, iommu_group);
@@ -988,21 +1425,27 @@ static void vfio_iommu_type1_detach_group(void *iommu_data,list_del (&group->next);kfree (group);/* -* Group ownership provides privilege, if the group -* list is empty, the domain goes away. If it's the -* last domain, then all the mappings go away too. +* Group ownership provides privilege, if the group list is +* empty, the domain goes away. If it's the last domain with +* iommu and external domain doesn't exist, then all the +* mappings go away too. If it's the last domain with iommu and +* external domain exist, update accounting */ if (list_empty (&domain->group_list)) { -if (list_is_singular (&iommu->domain_list)) -vfio_iommu_unmap_unpin_all (iommu); +if (list_is_singular (&iommu->domain_list)) { +if (!iommu->external_domain) +vfio_iommu_unmap_unpin_all (iommu); +else +vfio_iommu_unmap_unpin_reaccount (iommu); + }iommu_domain_free (domain->domain);list_del (&domain->next);kfree (domain); } - goto done; +break ; } -done: +detach_group_done:mutex_unlock (&iommu->lock); }
@@ -1028,29 +1471,46 @@ static void *vfio_iommu_type1_open(unsigned long arg) }INIT_LIST_HEAD (&iommu->domain_list); +INIT_LIST_HEAD (&iommu->addr_space_list); iommu->dma_list =RB_ROOT ;mutex_init (&iommu->lock);return iommu; } +static void vfio_release_domain (struct vfio_domain *domain,bool external) +{ +struct vfio_group *group, *group_tmp; + +list_for_each_entry_safe (group, group_tmp, + &domain->group_list, next) { +if (!external) +iommu_detach_group (domain->domain, group->iommu_group); +list_del (&group->next); +kfree (group); + } + +if (!external) +iommu_domain_free (domain->domain); +} +static void vfio_iommu_type1_release (void *iommu_data) {struct vfio_iommu *iommu = iommu_data;struct vfio_domain *domain, *domain_tmp; -struct vfio_group *group, *group_tmp; + +if (iommu->external_domain) { +vfio_release_domain (iommu->external_domain, true); +vfio_external_unpin_all (iommu, false); +kfree (iommu->external_domain); + iommu->external_domain =NULL ; + }vfio_iommu_unmap_unpin_all (iommu);list_for_each_entry_safe (domain, domain_tmp, &iommu->domain_list, next) { -list_for_each_entry_safe (group, group_tmp, - &domain->group_list, next) { -iommu_detach_group (domain->domain, group->iommu_group); -list_del (&group->next); -kfree (group); - } -iommu_domain_free (domain->domain); +vfio_release_domain (domain, false);list_del (&domain->next);kfree (domain); }
@@ -1158,6 +1618,8 @@ static const struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1 = { .ioctl = vfio_iommu_type1_ioctl, .attach_group = vfio_iommu_type1_attach_group, .detach_group = vfio_iommu_type1_detach_group, + .pin_pages = vfio_iommu_type1_pin_pages, + .unpin_pages = vfio_iommu_type1_unpin_pages, };static int __init vfio_iommu_type1_init (void )
Signature · 1 line
2.7.0
Kirti Wankhede
maintainerPATCH v11 11/22 vfio iommu: Add blocking notifier to notify DMA_UNMAP
In reply to Kirti WankhedeAdded blocking notifier to IOMMU TYPE1 driver to notify vendor drivers about DMA_UNMAP.
Exported two APIs vfio_register_notifier() and vfio_unregister_notifier(). Notifier should be registered, if external user wants to use vfio_pin_pages()/vfio_unpin_pages() APIs to pin/unpin pages. Vendor driver should use VFIO_IOMMU_NOTIFY_DMA_UNMAP action to invalidate mappings.
--- drivers/vfio/vfio.c | 73 +++++++++++++++++++++++++++++++++++++++++ drivers/vfio/vfio_iommu_type1.c | 47 ++++++++++++++++++++------ include/linux/vfio.h | 11 +++++++ 3 files changed, 121 insertions(+), 10 deletions(-)
drivers/vfio/vfio.c +73 -0
@@ -1895,6 +1895,79 @@ err_unpin_pages: }EXPORT_SYMBOL (vfio_unpin_pages); +int vfio_register_notifier (struct device *dev,struct notifier_block *nb) +{ +struct vfio_container *container; +struct vfio_group *group; +struct vfio_iommu_driver *driver; +ssize_t ret; + +if (!dev || !nb) +return -EINVAL ; + + group =vfio_group_get_from_dev (dev); +if (IS_ERR (group)) +return PTR_ERR (group); + + ret =vfio_group_add_container_user (group); +if (ret) + goto err_register_nb; + + container = group->container; +down_read (&container->group_lock); + + driver = container->iommu_driver; +if (likely (driver && driver->ops->register_notifier)) + ret = driver->ops->register_notifier (container->iommu_data, nb); +else + ret = -EINVAL ; + +up_read (&container->group_lock); +vfio_group_try_dissolve_container (group); + +err_register_nb: +vfio_group_put (group); +return ret; +} +EXPORT_SYMBOL (vfio_register_notifier); + +int vfio_unregister_notifier (struct device *dev,struct notifier_block *nb) +{ +struct vfio_container *container; +struct vfio_group *group; +struct vfio_iommu_driver *driver; +ssize_t ret; + +if (!dev || !nb) +return -EINVAL ; + + group =vfio_group_get_from_dev (dev); +if (IS_ERR (group)) +return PTR_ERR (group); + + ret =vfio_group_add_container_user (group); +if (ret) + goto err_unregister_nb; + + container = group->container; +down_read (&container->group_lock); + + driver = container->iommu_driver; +if (likely (driver && driver->ops->unregister_notifier)) + ret = driver->ops->unregister_notifier (container->iommu_data, + nb); +else + ret = -EINVAL ; + +up_read (&container->group_lock); +vfio_group_try_dissolve_container (group); + +err_unregister_nb: +vfio_group_put (group); +return ret; +} +EXPORT_SYMBOL (vfio_unregister_notifier); +/** * Module/class support */
drivers/vfio/vfio_iommu_type1.c +37 -10
84 lines not on this page · raw message
include/linux/vfio.h +11 -0
@@ -82,6 +82,10 @@ struct vfio_iommu_driver_ops {unsigned long *user_pfn, unsigned long *pfn, int npage); +int (*register_notifier)(void *iommu_data, +struct notifier_block *nb); +int (*unregister_notifier)(void *iommu_data, +struct notifier_block *nb); };extern int vfio_register_iommu_driver (const struct vfio_iommu_driver_ops *ops);
@@ -139,6 +143,13 @@ extern int vfio_pin_pages(struct device *dev, unsigned long *user_pfn,extern int vfio_unpin_pages (struct device *dev,unsigned long *user_pfn,unsigned long *pfn,int npage); +#define VFIO_IOMMU_NOTIFY_DMA_UNMAP 1 + +extern int vfio_register_notifier (struct device *dev, +struct notifier_block *nb); + +extern int vfio_unregister_notifier (struct device *dev, +struct notifier_block *nb);/* * IRQfd - generic */
Signature · 1 line
2.7.0
Kirti Wankhede
maintainerPATCH v11 3/22 vfio: Rearrange functions to get vfio_group from dev
In reply to Kirti WankhedeThis patch rearranges functions to get vfio_group from device
--- drivers/vfio/vfio.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-)
drivers/vfio/vfio.c +16 -7
@@ -480,6 +480,21 @@ static struct vfio_group *vfio_group_get_from_minor(int minor)return group; } +static struct vfio_group *vfio_group_get_from_dev (struct device *dev) +{ +struct iommu_group *iommu_group; +struct vfio_group *group; + + iommu_group =iommu_group_get (dev); +if (!iommu_group) +return NULL ; + + group =vfio_group_get_from_iommu (iommu_group); +iommu_group_put (iommu_group); + +return group; +} +/** * Device objects - create, release, get, put, search */
@@ -811,16 +826,10 @@ EXPORT_SYMBOL_GPL(vfio_add_group_dev);*/ struct vfio_device *vfio_device_get_from_dev (struct device *dev) { -struct iommu_group *iommu_group;struct vfio_group *group;struct vfio_device *device; - iommu_group =iommu_group_get (dev); -if (!iommu_group) -return NULL ; - - group =vfio_group_get_from_iommu (iommu_group); -iommu_group_put (iommu_group); + group =vfio_group_get_from_dev (dev);if (!group)return NULL ;
Signature · 1 line
2.7.0
Kirti Wankhede
maintainerPATCH v11 12/22 vfio: Add notifier callback to parent's ops structure of mdev
In reply to Kirti WankhedeAdd a notifier calback to parent's ops structure of mdev device so that per device notifer for vfio module is registered through vfio_mdev module.
--- drivers/vfio/mdev/vfio_mdev.c | 19 +++++++++++++++++++ include/linux/mdev.h | 9 +++++++++ 2 files changed, 28 insertions(+)
drivers/vfio/mdev/vfio_mdev.c +19 -0
@@ -24,6 +24,15 @@#define DRIVER_AUTHOR "NVIDIA Corporation"#define DRIVER_DESC "VFIO based driver for Mediated device" +static int vfio_mdev_notifier (struct notifier_block *nb,unsigned long action, +void *data) +{ + struct mdev_device *mdev = container_of(nb, struct mdev_device, nb); +struct parent_device *parent = mdev->parent; + +return parent->ops->notifier (mdev, action, data); +} +static int vfio_mdev_open (void *device_data) {struct mdev_device *mdev = device_data;
@@ -40,6 +49,11 @@ static int vfio_mdev_open(void *device_data)if (ret)module_put (THIS_MODULE ); +if (likely (parent->ops->notifier)) { + mdev->nb.notifier_call = vfio_mdev_notifier; +if (vfio_register_notifier (&mdev->dev, &mdev->nb)) +pr_err ("Failed to register notifier for mdev\n" ); + }return ret; }
@@ -48,6 +62,11 @@ static void vfio_mdev_release(void *device_data)struct mdev_device *mdev = device_data;struct parent_device *parent = mdev->parent; +if (likely (parent->ops->notifier)) { +if (vfio_unregister_notifier (&mdev->dev, &mdev->nb)) +pr_err ("Failed to unregister notifier for mdev\n" ); + } +if (likely (parent->ops->release)) parent->ops->release (mdev);
include/linux/mdev.h +9 -0
@@ -37,6 +37,7 @@ struct mdev_device {struct kref ref;struct list_head next;struct kobject *type_kobj; +struct notifier_block nb; };
@@ -84,6 +85,12 @@ struct mdev_device {* @cmd: mediated device structure * @arg: mediated device structure * @mmap: mmap callback +* @mdev: mediated device structure +* @vma: vma structure +* @notifer: Notifier callback +* @mdev: mediated device structure +* @action: Action for which notifier is called +* @data: Data associated with the notifier * Parent device that support mediated device should be registered with mdev * module with parent_ops structure. **/
@@ -105,6 +112,8 @@ struct parent_ops {ssize_t (*ioctl)(struct mdev_device *mdev,unsigned int cmd,unsigned long arg);int (*mmap)(struct mdev_device *mdev,struct vm_area_struct *vma); +int (*notifier)(struct mdev_device *mdev,unsigned long action, +void *data); };/* interface for exporting mdev supported type attributes */
Signature · 1 line
2.7.0
Kirti Wankhede
maintainerPATCH v11 13/22 vfio: Introduce common function to add capabilities
In reply to Kirti WankhedeVendor driver using mediated device framework should use vfio_info_add_capability() to add capabilities.
Introduced this function to reduce code duplication in vendor drivers.
--- drivers/vfio/vfio.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++- include/linux/vfio.h | 3 +++ 2 files changed, 62 insertions(+), 1 deletion(-)
drivers/vfio/vfio.c +59 -1
@@ -1797,8 +1797,66 @@ void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset)for (tmp = caps->buf; tmp->next; tmp = (void *)tmp + tmp->next - offset) tmp->next += offset; } -EXPORT_SYMBOL_GPL (vfio_info_cap_shift); +EXPORT_SYMBOL (vfio_info_cap_shift); +static int sparse_mmap_cap (struct vfio_info_cap *caps,void *cap_type) +{ +struct vfio_info_cap_header *header; +struct vfio_region_info_cap_sparse_mmap *sparse_cap, *sparse = cap_type; +size_t size; + + size =sizeof (*sparse) + 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_cap =container_of (header, + struct vfio_region_info_cap_sparse_mmap, header); + sparse_cap->nr_areas = sparse->nr_areas; +memcpy (sparse_cap->areas, sparse->areas, + sparse->nr_areas *sizeof (*sparse->areas)); +return 0 ; +} + +static int region_type_cap (struct vfio_info_cap *caps,void *cap_type) +{ +struct vfio_info_cap_header *header; +struct vfio_region_info_cap_type *type_cap, *cap = cap_type; + + header =vfio_info_cap_add (caps,sizeof (*cap), +VFIO_REGION_INFO_CAP_TYPE ,1 ); +if (IS_ERR (header)) +return PTR_ERR (header); + + type_cap = container_of(header, struct vfio_region_info_cap_type, + header); + type_cap->type = cap->type; + type_cap->subtype = cap->subtype; +return 0 ; +} + +int vfio_info_add_capability (struct vfio_info_cap *caps,int cap_type_id, +void *cap_type) +{ +int ret = -EINVAL ; + +if (!cap_type) +return 0 ; + +switch (cap_type_id) { +case VFIO_REGION_INFO_CAP_SPARSE_MMAP : + ret =sparse_mmap_cap (caps, cap_type); +break ; + +case VFIO_REGION_INFO_CAP_TYPE : + ret =region_type_cap (caps, cap_type); +break ; + } + +return ret; +} +EXPORT_SYMBOL (vfio_info_add_capability);/* * Pin a set of guest PFNs and return their associated host PFNs for local
include/linux/vfio.h +3 -0
@@ -113,6 +113,9 @@ extern struct vfio_info_cap_header *vfio_info_cap_add( struct vfio_info_cap *caps, size_t size, u16 id, u16 version);extern void vfio_info_cap_shift (struct vfio_info_cap *caps,size_t offset); +extern int vfio_info_add_capability (struct vfio_info_cap *caps, +int cap_type_id,void *cap_type); +struct pci_dev ;#ifdef CONFIG_EEH extern void vfio_spapr_pci_eeh_open (struct pci_dev *pdev);
Signature · 1 line
2.7.0
Kirti Wankhede
maintainerPATCH v11 14/22 vfio_pci: Update vfio_pci to use vfio_info_add_capability()
In reply to Kirti WankhedeUpdate msix_sparse_mmap_cap() to use vfio_info_add_capability() Update region type capability to use vfio_info_add_capability()
--- drivers/vfio/pci/vfio_pci.c | 49 ++++++++++++++++++--------------------------- 1 file changed, 19 insertions(+), 30 deletions(-)
drivers/vfio/pci/vfio_pci.c +19 -30
@@ -558,10 +558,9 @@ static int vfio_pci_for_each_slot_or_bus(struct pci_dev *pdev,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 ; +int nr_areas =2 , i =0 , ret; end =pci_resource_len (vdev->pdev, vdev->msix_bar);
@@ -572,13 +571,10 @@ static int msix_sparse_mmap_cap(struct vfio_pci_device *vdev, 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 =kzalloc (size,GFP_KERNEL ); +if (!sparse) +return -ENOMEM ; - sparse =container_of (header, - struct vfio_region_info_cap_sparse_mmap, header); sparse->nr_areas = nr_areas;if (vdev->msix_offset &PAGE_MASK ) {
@@ -594,26 +590,11 @@ static int msix_sparse_mmap_cap(struct vfio_pci_device *vdev, i++; } -return 0 ; -} - -static int region_type_cap (struct vfio_pci_device *vdev, -struct vfio_info_cap *caps, -unsigned int type,unsigned int subtype) -{ -struct vfio_info_cap_header *header; -struct vfio_region_info_cap_type *cap; - - header =vfio_info_cap_add (caps,sizeof (*cap), -VFIO_REGION_INFO_CAP_TYPE ,1 ); -if (IS_ERR (header)) -return PTR_ERR (header); - - cap = container_of(header, struct vfio_region_info_cap_type, header); - cap->type = type; - cap->subtype = subtype; + ret =vfio_info_add_capability (caps,VFIO_REGION_INFO_CAP_SPARSE_MMAP , + sparse); +kfree (sparse); -return 0 ; +return ret; } int vfio_pci_register_dev_region(struct vfio_pci_device *vdev,
@@ -752,6 +733,9 @@ static long vfio_pci_ioctl(void *device_data,break ;default : + { +struct vfio_region_info_cap_type cap_type; +if (info.index >=VFIO_PCI_NUM_REGIONS + vdev->num_regions)return -EINVAL ;
@@ -762,11 +746,16 @@ static long vfio_pci_ioctl(void *device_data, info.size = vdev->region[i].size; info.flags = vdev->region[i].flags; - ret =region_type_cap (vdev, &caps, - vdev->region[i].type, - vdev->region[i].subtype); + cap_type.type = vdev->region[i].type; + cap_type.subtype = vdev->region[i].subtype; + + ret =vfio_info_add_capability (&caps, +VFIO_REGION_INFO_CAP_TYPE , + &cap_type);if (ret)return ret; + + } }if (caps.size) {
Signature · 1 line
2.7.0
Kirti Wankhede
maintainerPATCH v11 15/22 vfio: Introduce vfio_set_irqs_validate_and_prepare()
In reply to Kirti WankhedeVendor driver using mediated device framework would use same mechnism to validate and prepare IRQs. Introducing this function to reduce code replication in multiple drivers.
--- drivers/vfio/vfio.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/vfio.h | 4 ++++ 2 files changed, 52 insertions(+)
drivers/vfio/vfio.c +48 -0
@@ -1858,6 +1858,54 @@ int vfio_info_add_capability(struct vfio_info_cap *caps, int cap_type_id, }EXPORT_SYMBOL (vfio_info_add_capability); +int vfio_set_irqs_validate_and_prepare (struct vfio_irq_set *hdr,int num_irqs, +int max_irq_type,size_t *data_size) +{ +unsigned long minsz; +size_t size; + + minsz = offsetofend(struct vfio_irq_set, count); + +if ((hdr->argsz < minsz) || (hdr->index >= max_irq_type) || + (hdr->count >= (U32_MAX - hdr->start)) || + (hdr->flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK | +VFIO_IRQ_SET_ACTION_TYPE_MASK ))) +return -EINVAL ; + +if (data_size) + *data_size =0 ; + +if (hdr->start >= num_irqs || hdr->start + hdr->count > num_irqs) +return -EINVAL ; + +switch (hdr->flags &VFIO_IRQ_SET_DATA_TYPE_MASK ) { +case VFIO_IRQ_SET_DATA_NONE : + size =0 ; +break ; +case VFIO_IRQ_SET_DATA_BOOL : + size =sizeof (uint8_t ); +break ; +case VFIO_IRQ_SET_DATA_EVENTFD : + size =sizeof (int32_t ); +break ; +default : +return -EINVAL ; + } + +if (size) { +if (hdr->argsz - minsz < hdr->count * size) +return -EINVAL ; + +if (!data_size) +return -EINVAL ; + + *data_size = hdr->count * size; + } + +return 0 ; +} +EXPORT_SYMBOL (vfio_set_irqs_validate_and_prepare); +/* * Pin a set of guest PFNs and return their associated host PFNs for local * domain only.
include/linux/vfio.h +4 -0
@@ -116,6 +116,10 @@ extern void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);extern int vfio_info_add_capability (struct vfio_info_cap *caps,int cap_type_id,void *cap_type); +extern int vfio_set_irqs_validate_and_prepare (struct vfio_irq_set *hdr, +int num_irqs,int max_irq_type, +size_t *data_size); +struct pci_dev ;#ifdef CONFIG_EEH extern void vfio_spapr_pci_eeh_open (struct pci_dev *pdev);
Signature · 1 line
2.7.0
Kirti Wankhede
maintainerPATCH v11 16/22 vfio_pci: Updated to use vfio_set_irqs_validate_and_prepare()
In reply to Kirti WankhedeUpdated vfio_pci.c file to use vfio_set_irqs_validate_and_prepare()
--- drivers/vfio/pci/vfio_pci.c | 34 +++++++--------------------------- 1 file changed, 7 insertions(+), 27 deletions(-)
drivers/vfio/pci/vfio_pci.c +7 -27
@@ -818,45 +818,25 @@ static long vfio_pci_ioctl(void *device_data, }else if (cmd ==VFIO_DEVICE_SET_IRQS ) {struct vfio_irq_set hdr; -size_t size;u8 *data =NULL ;int max, ret =0 ; +size_t data_size =0 ; minsz = offsetofend(struct vfio_irq_set, count);if (copy_from_user (&hdr, (void __user *)arg, minsz))return -EFAULT ; -if (hdr.argsz < minsz || hdr.index >=VFIO_PCI_NUM_IRQS || - hdr.count >= (U32_MAX - hdr.start) || - hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK | -VFIO_IRQ_SET_ACTION_TYPE_MASK )) -return -EINVAL ; - max =vfio_pci_get_irq_count (vdev, hdr.index); -if (hdr.start >= max || hdr.start + hdr.count > max) -return -EINVAL ; -switch (hdr.flags &VFIO_IRQ_SET_DATA_TYPE_MASK ) { -case VFIO_IRQ_SET_DATA_NONE : - size =0 ; -break ; -case VFIO_IRQ_SET_DATA_BOOL : - size =sizeof (uint8_t ); -break ; -case VFIO_IRQ_SET_DATA_EVENTFD : - size =sizeof (int32_t ); -break ; -default : -return -EINVAL ; - } - -if (size) { -if (hdr.argsz - minsz < hdr.count * size) -return -EINVAL ; + ret =vfio_set_irqs_validate_and_prepare (&hdr, max, +VFIO_PCI_NUM_IRQS , &data_size); +if (ret) +return ret; +if (data_size) { data =memdup_user ((void __user *)(arg + minsz), - hdr.count * size); + data_size);if (IS_ERR (data))return PTR_ERR (data); }
Signature · 1 line
2.7.0
Kirti Wankhede
maintainerPATCH v11 17/22 vfio_platform: Updated to use vfio_set_irqs_validate_and_prepare()
In reply to Kirti WankhedeUpdated vfio_platform_common.c file to usevfio_set_irqs_validate_and_prepare()
--- drivers/vfio/platform/vfio_platform_common.c | 31 +++++++--------------------- 1 file changed, 8 insertions(+), 23 deletions(-)
drivers/vfio/platform/vfio_platform_common.c +8 -23
@@ -364,36 +364,21 @@ static long vfio_platform_ioctl(void *device_data,struct vfio_irq_set hdr;u8 *data =NULL ;int ret =0 ; +size_t data_size =0 ; minsz = offsetofend(struct vfio_irq_set, count);if (copy_from_user (&hdr, (void __user *)arg, minsz))return -EFAULT ; -if (hdr.argsz < minsz) -return -EINVAL ; - -if (hdr.index >= vdev->num_irqs) -return -EINVAL ; - -if (hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK | -VFIO_IRQ_SET_ACTION_TYPE_MASK )) -return -EINVAL ; - -if (!(hdr.flags &VFIO_IRQ_SET_DATA_NONE )) { -size_t size; - -if (hdr.flags &VFIO_IRQ_SET_DATA_BOOL ) - size =sizeof (uint8_t ); -else if (hdr.flags &VFIO_IRQ_SET_DATA_EVENTFD ) - size =sizeof (int32_t ); -else -return -EINVAL ; - -if (hdr.argsz - minsz < size) -return -EINVAL ; + ret =vfio_set_irqs_validate_and_prepare (&hdr, vdev->num_irqs, + vdev->num_irqs, &data_size); +if (ret) +return ret; - data =memdup_user ((void __user *)(arg + minsz), size); +if (data_size) { + data =memdup_user ((void __user *)(arg + minsz), + data_size);if (IS_ERR (data))return PTR_ERR (data); }
Signature · 1 line
2.7.0
Kirti Wankhede
maintainerPATCH v11 18/22 vfio: Define device_api strings
In reply to Kirti WankhedeDefined device API strings. Vendor driver using mediated device framework should use corresponding string for device_api attribute.
--- include/uapi/linux/vfio.h | 10 ++++++++++ 1 file changed, 10 insertions(+)
include/uapi/linux/vfio.h +10 -0
@@ -203,6 +203,16 @@ struct vfio_device_info { };#define VFIO_DEVICE_GET_INFO _IO(VFIO_TYPE, VFIO_BASE + 7) +/* +* Vendor driver using Mediated device framework should provide device_api +* attribute in supported type attribute groups. Device API string should be one +* of the following corresponding to device flags in vfio_device_info structure. +*/ + +#define VFIO_DEVICE_API_PCI_STRING "vfio-pci" +#define VFIO_DEVICE_API_PLATFORM_STRING "vfio-platform" +#define VFIO_DEVICE_API_AMBA_STRING "vfio-amba" +/** * VFIO_DEVICE_GET_REGION_INFO - _IOWR(VFIO_TYPE, VFIO_BASE + 8, * struct vfio_region_info)
Signature · 1 line
2.7.0
Kirti Wankhede
maintainerPATCH v11 19/22 docs: Add Documentation for Mediated devices
In reply to Kirti WankhedeAdd file Documentation/vfio-mediated-device.txt that include details of mediated device framework.
--- Documentation/vfio-mediated-device.txt | 298 +++++++++++++++++++++++++++++++++ 1 file changed, 298 insertions(+) create mode 100644 Documentation/vfio-mediated-device.txt
Documentation/vfio-mediated-device.txt new file +298 -0
@@ -0,0 +1,298 @@ +/* + * VFIO Mediated devices + * + * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. + * Author: Neo Jia <[email protected]> + * Kirti Wankhede <[email protected]> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ + +Virtual Function I/O (VFIO) Mediated devices[1] +=============================================== + +The number of use cases for virtualizing DMA devices that do not have built-in +SR_IOV capability is increasing. Previously, to virtualize such devices, +developers had to create their own management interfaces and APIs, and then +integrate them with user space software. To simplify integration with user space +software, we have identified common requirements and a unified management +interface for such devices. + +The VFIO driver framework provides unified APIs for direct device access. It is +an IOMMU/device-agnostic framework for exposing direct device access to user +space in a secure, IOMMU-protected environment. This framework is used for +multiple devices, such as GPUs, network adapters, and compute accelerators. With +direct device access, virtual machines or user space applications have direct +access to the physical device. This framework is reused for mediated devices. + +The mediated core driver provides a common interface for mediated device +management that can be used by drivers of different devices. This module +provides a generic interface to perform these operations: + +* Create and destroy a mediated device +* Add a mediated device to and remove it from a mediated bus driver +* Add a mediated device to and remove it from an IOMMU group + +The mediated core driver also provides an interface to register a bus driver. +For example, the mediated VFIO mdev driver is designed for mediated devices and +supports VFIO APIs. The mediated bus driver adds a mediated device to and +removes it from a VFIO group. + +The following high-level block diagram shows the main components and interfaces +in the VFIO mediated driver framework. The diagram shows NVIDIA, Intel, and IBM +devices as examples, as these devices are the first devices to use this module. + + +---------------+ + | | + | +-----------+ | mdev_register_driver() +--------------+ + | | | +<------------------------+ | + | | mdev | | | | + | | bus | +------------------------>+ vfio_mdev.ko |<-> VFIO user + | | driver | | probe()/remove() | | APIs + | | | | +--------------+ + | +-----------+ | + | | + | MDEV CORE | + | MODULE | + | mdev.ko | + | +-----------+ | mdev_register_device() +--------------+ + | | | +<------------------------+ | + | | | | | nvidia.ko |<-> physical + | | | +------------------------>+ | device + | | | | callbacks +--------------+ + | | Physical | | + | | device | | mdev_register_device() +--------------+ + | | interface | |<------------------------+ | + | | | | | i915.ko |<-> physical + | | | +------------------------>+ | device + | | | | callbacks +--------------+ + | | | | + | | | | mdev_register_device() +--------------+ + | | | +<------------------------+ | + | | | | | ccw_device.ko|<-> physical + | | | +------------------------>+ | device + | | | | callbacks +--------------+ + | +-----------+ | + +---------------+ + + +Registration Interfaces +======================= + +The mediated core driver provides the following types of registration +interfaces: + +* Registration interface for a mediated bus driver +* Physical device driver interface + +Registration Interface for a Mediated Bus Driver +------------------------------------------------ + +The registration interface for a mediated bus driver provides the following +structure to represent a mediated device's driver: + + /* + * struct mdev_driver [2] - Mediated device's driver + * @name: driver name + * @probe: called when new device created + * @remove: called when device removed + * @driver: device driver structure + */ + struct mdev_driver { + const char *name; + int (*probe) (struct device *dev); + void (*remove) (struct device *dev); + struct device_driver driver; + }; + +A mediated bus driver for mdev should use this structure in the function calls +to register and unregister itself with the core driver: + +* Register: + + extern int mdev_register_driver(struct mdev_driver *drv, + struct module *owner); + +* Unregister: + + extern void mdev_unregister_driver(struct mdev_driver *drv); + +The mediated bus driver is responsible for adding mediated devices to the VFIO +group when devices are bound to the driver and removing mediated devices from +the VFIO when devices are unbound from the driver. + + +Physical Device Driver Interface +-------------------------------- + +The physical device driver interface provides the parent_ops[3] structure to +define the APIs to manage work in the mediated core driver that is related to +the physical device. + +The structures in the parent_ops structure are as follows: + +* dev_attr_groups: attributes of the parent device +* mdev_attr_groups: attributes of the mediated device +* supported_config: attributes to define supported configurations + +The functions in the parent_ops structure are as follows: + +* create: allocate basic resources in a driver for a mediated device +* remove: free resources in a driver when a mediated device is destroyed + +The callbacks in the parent_ops structure are as follows: + +* open: open callback of mediated device +* close: close callback of mediated device +* ioctl: ioctl callback of mediated device +* read : read emulation callback +* write: write emulation callback +* mmap: mmap emulation callback +* notifier: notifier callback + +A driver should use the parent_ops structure in the function call to register +itself with the mdev core driver: + +extern int mdev_register_device(struct device *dev, + const struct parent_ops *ops); + +However, the parent_ops structure is not required in the function call that a +driver should use to unregister itself with the mdev core driver: + +extern void mdev_unregister_device(struct device *dev); + + +Mediated Device Management Interface Through sysfs +================================================== + +The management interface through sysfs enables user space software, such as +libvirt, to query and configure mediated devices in a hardware-agnostic fashion. +This management interface provides flexibility to the underlying physical +device's driver to support features such as: + +* Mediated device hot plug +* Multiple mediated devices in a single virtual machine +* Multiple mediated devices from different physical devices + +Links in the mdev_bus Class Directory +------------------------------------- +The /sys/class/mdev_bus/ directory contains links to devices that are registered +with the mdev core driver. + +Directories and files under the sysfs for Each Physical Device +-------------------------------------------------------------- + +|- [parent physical device] +|--- Vendor-specific-attributes [optional] +|--- [mdev_supported_types] +| |--- [<type-id>] +| | |--- create +| | |--- name +| | |--- available_instances +| | |--- device_api +| | |--- description +| | |--- [devices] +| |--- [<type-id>] +| | |--- create +| | |--- name +| | |--- available_instances +| | |--- device_api +| | |--- description +| | |--- [devices] +| |--- [<type-id>] +| |--- create +| |--- name +| |--- available_instances +| |--- device_api +| |--- description +| |--- [devices] + +* [mdev_supported_types] + + The list of currently supported mediated device types and their details. + + [<type-id>], device_api, and available_instances are mandatory attributes + that should be provided by vendor driver. + +* [<type-id>] + + The [<type-id>] name is created by adding the the device driver string as a + prefix to the string provided by the vendor driver. This format of this name + is as follows: + + sprintf(buf, "%s-%s", dev_driver_string(parent->dev), group->name); + +* device_api + + This attribute should show which device API is being created, for example, + "vfio-pci" for a PCI device. + +* available_instances + + This attribute should show the number of devices of type <type-id> that can be + created. + +* [device] + + This directory contains links to the devices of type <type-id> that have been +created. + +* name + + This attribute should show human readable name. This is optional attribute. + +* description + + This attribute should show brief features/description of the type. This is + optional attribute. + +Directories and Files Under the sysfs for Each mdev Device +---------------------------------------------------------- + +|- [parent phy device] +|--- [$MDEV_UUID] + |--- remove + |--- mdev_type {link to its type} + |--- vendor-specific-attributes [optional] + +* remove (write only) +Writing '1' to the 'remove' file destroys the mdev device. The vendor driver can +fail the remove() callback if that device is active and the vendor driver +doesn't support hot unplug. + +Example: + # echo 1 > /sys/bus/mdev/devices/$mdev_UUID/remove + +Mediated device Hot plug: +------------------------ + +Mediated devices can be created and assigned at runtime. The procedure to hot +plug a mediated device is the same as the procedure to hot plug a PCI device. + +Translation APIs for Mediated Devices +===================================== + +The following APIs are provided for translating user pfn to host pfn in a VFIO +driver: + + extern long vfio_pin_pages(struct device *dev, unsigned long *user_pfn, + long npage, int prot, unsigned long *phys_pfn); + + extern long vfio_unpin_pages(struct device *dev, unsigned long *pfn, + long npage); + +These functions call back into the back-end IOMMU module by using the pin_pages +and unpin_pages callbacks of the struct vfio_iommu_driver_ops[4]. Currently +these callbacks are supported in the TYPE1 IOMMU module. To enable them for +other IOMMU backend modules, such as PPC64 sPAPR module, they need to provide +these two callback functions. + +References +---------- + +[1] See Documentation/vfio.txt for more information on VFIO. +[2] struct mdev_driver in include/linux/mdev.h +[3] struct parent_ops in include/linux/mdev.h +[4] struct vfio_iommu_driver_ops in include/linux/vfio.h
Signature · 1 line
2.7.0
Kirti Wankhede
maintainerPATCH v11 20/22 docs: Sysfs ABI for mediated device framework
In reply to Kirti WankhedeAdded details of sysfs ABI for mediated device framework
--- Documentation/ABI/testing/sysfs-bus-vfio-mdev | 111 ++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-bus-vfio-mdev
Documentation/ABI/testing/sysfs-bus-vfio-mdev new file +111 -0
@@ -0,0 +1,111 @@ +What: /sys/.../<device>/mdev_supported_types/ +Date: October 2016 +Contact: Kirti Wankhede <[email protected]> +Description: + This directory contains list of directories of currently + supported mediated device types and their details for + <device>. Supported type attributes are defined by the + vendor driver who registers with Mediated device framework. + Each supported type is a directory whose name is created + by adding the device driver string as a prefix to the + string provided by the vendor driver. + +What: /sys/.../<device>/mdev_supported_types/<type-id>/ +Date: October 2016 +Contact: Kirti Wankhede <[email protected]> +Description: + This directory gives details of supported type, like name, + description, available_instances, device_api etc. + 'device_api' and 'available_instances' are mandatory + attributes to be provided by vendor driver. 'name', + 'description' and other vendor driver specific attributes + are optional. + +What: /sys/.../mdev_supported_types/<type-id>/create +Date: October 2016 +Contact: Kirti Wankhede <[email protected]> +Description: + Writing UUID to this file will create mediated device of + type <type-id> for parent device <device>. This is a + write-only file. + For example: + # echo "83b8f4f2-509f-382f-3c1e-e6bfe0fa1001" > \ + /sys/devices/foo/mdev_supported_types/foo-1/create + +What: /sys/.../mdev_supported_types/<type-id>/devices/ +Date: October 2016 +Contact: Kirti Wankhede <[email protected]> +Description: + This directory contains symbolic links pointing to mdev + devices sysfs entries which are created of this <type-id>. + +What: /sys/.../mdev_supported_types/<type-id>/available_instances +Date: October 2016 +Contact: Kirti Wankhede <[email protected]> +Description: + Reading this attribute will show the number of mediated + devices of type <type-id> that can be created. This is a + readonly file. +Users: + Userspace applications interested in creating mediated + device of that type. Userspace application should check + the number of available instances could be created before + creating mediated device of this type. + +What: /sys/.../mdev_supported_types/<type-id>/device_api +Date: October 2016 +Contact: Kirti Wankhede <[email protected]> +Description: + Reading this attribute will show VFIO device API supported + by this type. For example, "vfio-pci" for a PCI device, + "vfio-platform" for platform device. + +What: /sys/.../mdev_supported_types/<type-id>/name +Date: October 2016 +Contact: Kirti Wankhede <[email protected]> +Description: + Reading this attribute will show human readable name of the + mediated device that will get created of type <type-id>. + This is optional attribute. For example: "Grid M60-0Q" +Users: + Userspace applications interested in knowing the name of + a particular <type-id> that can help in understanding the + type of mediated device. + +What: /sys/.../mdev_supported_types/<type-id>/description +Date: October 2016 +Contact: Kirti Wankhede <[email protected]> +Description: + Reading this attribute will show description of the type of + mediated device that will get created of type <type-id>. + This is optional attribute. For example: + "2 heads, 512M FB, 2560x1600 maximum resolution" +Users: + Userspace applications interested in knowing the details of + a particular <type-id> that can help in understanding the + features provided by that type of mediated device. + +What: /sys/.../<device>/<UUID>/ +Date: October 2016 +Contact: Kirti Wankhede <[email protected]> +Description: + This directory represents device directory of mediated + device. It contains all the attributes related to mediated + device. + +What: /sys/.../<device>/<UUID>/mdev_type +Date: October 2016 +Contact: Kirti Wankhede <[email protected]> +Description: + This is symbolic link pointing to supported type, <type-id> + directory of which this mediated device is created. + +What: /sys/.../<device>/<UUID>/remove +Date: October 2016 +Contact: Kirti Wankhede <[email protected]> +Description: + Writing '1' to this file destroys the mediated device. The + vendor driver can fail the remove() callback if that device + is active and the vendor driver doesn't support hot unplug. + Example: + # echo 1 > /sys/bus/mdev/devices/<UUID>/remove
Signature · 1 line
2.7.0
Kirti Wankhede
maintainerPATCH v11 21/22 docs: Sample driver to demonstrate how to use Mediated device framework.
In reply to Kirti WankhedeThe Sample driver creates mdev device that simulates serial port over PCI card.
--- Documentation/vfio-mediated-device.txt | 103 ++- samples/vfio-mdev/Makefile | 13 + samples/vfio-mdev/mtty.c | 1503 ++++++++++++++++++++++++++++++++ 3 files changed, 1618 insertions(+), 1 deletion(-) create mode 100644 samples/vfio-mdev/Makefile create mode 100644 samples/vfio-mdev/mtty.c
Documentation/vfio-mediated-device.txt +102 -1
@@ -289,8 +289,109 @@ these callbacks are supported in the TYPE1 IOMMU module. To enable them for other IOMMU backend modules, such as PPC64 sPAPR module, they need to provide these two callback functions. +Using the Sample Code +===================== + +mtty.c in samples/vfio-mdev/ directory is a sample driver program to +demonstrate how to use the mediated device framework. + +The sample driver creates an mdev device that simulates a serial port over a PCI +card. + +1. Build and load the mtty.ko module. + + This step creates a dummy device, /sys/devices/virtual/mtty/mtty/ + + Files in this device directory in sysfs are similar to the following: + + # tree /sys/devices/virtual/mtty/mtty/ + /sys/devices/virtual/mtty/mtty/ + |-- mdev_supported_types + | |-- mtty-1 + | | |-- available_instances + | | |-- create + | | |-- device_api + | | |-- devices + | | `-- name + | `-- mtty-2 + | |-- available_instances + | |-- create + | |-- device_api + | |-- devices + | `-- name + |-- mtty_dev + | `-- sample_mtty_dev + |-- power + | |-- autosuspend_delay_ms + | |-- control + | |-- runtime_active_time + | |-- runtime_status + | `-- runtime_suspended_time + |-- subsystem -> ../../../../class/mtty + `-- uevent + +2. Create a mediated device by using the dummy device that you created in the + previous step. + + # echo "83b8f4f2-509f-382f-3c1e-e6bfe0fa1001" > \ + /sys/devices/virtual/mtty/mtty/mdev_supported_types/mtty-2/create + +3. Add parameters to qemu-kvm. + + -device vfio-pci,\ + sysfsdev=/sys/bus/mdev/devices/83b8f4f2-509f-382f-3c1e-e6bfe0fa1001 + +4. Boot the VM. + + In the Linux guest VM, with no hardware on the host, the device appears + as follows: + + # lspci -s 00:05.0 -xxvv + 00:05.0 Serial controller: Device 4348:3253 (rev 10) (prog-if 02 [16550]) + Subsystem: Device 4348:3253 + Physical Slot: 5 + Control: I/O+ Mem- BusMaster- SpecCycle- MemWINV- VGASnoop- ParErr- + Stepping- SERR- FastB2B- DisINTx- + Status: Cap- 66MHz- UDF- FastB2B- ParErr- DEVSEL=medium >TAbort- + <TAbort- <MAbort- >SERR- <PERR- INTx- + Interrupt: pin A routed to IRQ 10 + Region 0: I/O ports at c150 [size=8] + Region 1: I/O ports at c158 [size=8] + Kernel driver in use: serial + 00: 48 43 53 32 01 00 00 02 10 02 00 07 00 00 00 00 + 10: 51 c1 00 00 59 c1 00 00 00 00 00 00 00 00 00 00 + 20: 00 00 00 00 00 00 00 00 00 00 00 00 48 43 53 32 + 30: 00 00 00 00 00 00 00 00 00 00 00 00 0a 01 00 00 + + In the Linux guest VM, dmesg output for the device is as follows: + + serial 0000:00:05.0: PCI INT A -> Link[LNKA] -> GSI 10 (level, high) -> IRQ +10 + 0000:00:05.0: ttyS1 at I/O 0xc150 (irq = 10) is a 16550A + 0000:00:05.0: ttyS2 at I/O 0xc158 (irq = 10) is a 16550A + + +5. In the Linux guest VM, check the serial ports. + + # setserial -g /dev/ttyS* + /dev/ttyS0, UART: 16550A, Port: 0x03f8, IRQ: 4 + /dev/ttyS1, UART: 16550A, Port: 0xc150, IRQ: 10 + /dev/ttyS2, UART: 16550A, Port: 0xc158, IRQ: 10 + +6. Using a minicom or any terminal enulation program, open port /dev/ttyS1 or + /dev/ttyS2 with hardware flow control disabled. + +7. Type data on the minicom terminal or send data to the terminal emulation + program and read the data. + + Data is loop backed from hosts mtty driver. + +8. Destroy the mediated device that you created. + + # echo 1 > /sys/bus/mdev/devices/83b8f4f2-509f-382f-3c1e-e6bfe0fa1001/remove + References ----------- +========== [1] See Documentation/vfio.txt for more information on VFIO. [2] struct mdev_driver in include/linux/mdev.h
samples/vfio-mdev/Makefile new file +13 -0
@@ -0,0 +1,13 @@ +# +# Makefile for mtty.c file +# +KERNEL_DIR:=/lib/modules/$(shell uname -r)/build + +obj-m:=mtty.o + +modules clean modules_install: + $(MAKE) -C $(KERNEL_DIR) SUBDIRS=$(PWD) $@ + +default: modules + +module: modules
samples/vfio-mdev/mtty.c new file +1503 -0
1,503 lines not on this page · raw message
Signature · 1 line
2.7.0
Kirti Wankhede
maintainerPATCH v11 22/22 MAINTAINERS: Add entry VFIO based Mediated device drivers
In reply to Kirti WankhedeAdding myself as a maintainer of mediated device framework, a sub module of VFIO.
--- MAINTAINERS | 9 +++++++++ 1 file changed, 9 insertions(+)
MAINTAINERS +9 -0
@@ -12729,6 +12729,15 @@ F: drivers/vfio/ F: include/linux/vfio.h F: include/uapi/linux/vfio.h +VFIO MEDIATED DEVICE DRIVERS +M: Kirti Wankhede <[email protected]> +L: [email protected] +S: Maintained +F: Documentation/vfio-mediated-device.txt +F: drivers/vfio/mdev/ +F: include/linux/mdev.h +F: samples/vfio-mdev/ + VFIO PLATFORM DRIVER M: Baptiste Reynal <[email protected]> L: [email protected]
Signature · 1 line
2.7.0
Quoted 44 lines · Kirti Wankhede
This series adds Mediated device support to Linux host kernel. Purpose of this series is to provide a common interface for mediated device management that can be used by different devices. This series introduces Mdev core module that creates and manages mediated devices, VFIO based driver for mediated devices that are created by mdev core module and update VFIO type1 IOMMU module to support pinning & unpinning for mediated devices.
What changed in v11?
mdev core:
Registermdev_busclass when first device is registed to avoid panic if any vendor driver and mdev driver are selected as built-in but vendor driver loads first and then mdev module.vfio_mdev:
Added notifier callback function to mdev parent's ops so that notifer is registered fromvfio_mdevmodule during device open and unregistered it from device close call. This is a optional callback. Some drivers using mdev framework might not pin or unpin pages, for example the sample mtty driver that simulates serial port. Vendor driver who need to pin/unpin pages should provide this callback. Otherwise pin request would fail.vfio_iommu_type1:
Updated to keep track of who (task and address space) mapped iova range. DuringDMA_UNMAP, same task who mapped it or other task who shares same address space is allowed to unmap, otherwise unmap fails. QEMU maps few iova ranges initially, then fork threads and from the child thread callsDMA_UNMAPon previously mapped iova. Since child shares same address space,DMA_UNMAPis successful.
Address space keeps track of pages pinned (pfn_list) by external user / mdev devices. Thispfn_listis used to verify pfn duringunpin_request, re-accounting of pages when direct device assigned in hot-unplugged and mdev device is present in same container.
When the container is released, all mapped iova from all tasks are unmapped and removed.Tested by assigning below combinations of devices to a single VM:
- GPU pass through only
- vGPU device only
- One GPU pass through and one vGPU device
- Linux VM hot plug and unplug vGPU device while GPU pass through device exist
- Linux VM hot plug and unplug GPU pass through device while vGPU device exist
Quoted 2 lines
Patch series tested with linux-next upto commit
14970f204b19@Fri Oct 28 Resolved against conflicting change:
Patch 11/22 does not apply if the patchset is applied on top of 14970f204b19.
Quoted 64 lines
05692d7005a3vfio/pci: Fix integer overflows, bitmask checkKirti Wankhede (22):
vfio: Mediated device Core driver
vfio: VFIO based driver for Mediated devices
vfio: Rearrange functions to getvfio_groupfrom dev
vfio: Common function to incrementcontainer_users
vfio iommu: Added pin and unpin callback functions to
vfio_iommu_driver_ops
vfio iommu type1: Update arguments ofvfio_lock_acctvfio iommu type1: Update argument ofvaddr_get_pfn()vfio iommu type1: Addfind_iommu_group()function
vfio iommu type1: Add task structure tovfio_dma
vfio iommu type1: Add support for mediated devices
vfio iommu: Add blocking notifier to notifyDMA_UNMAP
vfio: Add notifier callback to parent's ops structure of mdev
vfio: Introduce common function to add capabilities
vfio_pci: Updatevfio_pcito usevfio_info_add_capability()
vfio: Introducevfio_set_irqs_validate_and_prepare()
vfio_pci: Updated to usevfio_set_irqs_validate_and_prepare()
vfio_platform: Updated to usevfio_set_irqs_validate_and_prepare()
vfio: Definedevice_apistrings
docs: Add Documentation for Mediated devices
docs: Sysfs ABI for mediated device framework
docs: Sample driver to demonstrate how to use Mediated device
framework.
MAINTAINERS: Add entry VFIO based Mediated device driversDocumentation/ABI/testing/sysfs-bus-vfio-mdev | 111 ++ Documentation/vfio-mediated-device.txt | 399 +++++++ MAINTAINERS | 9 + drivers/vfio/Kconfig | 1 + drivers/vfio/Makefile | 1 + drivers/vfio/mdev/Kconfig | 17 + drivers/vfio/mdev/Makefile | 5 + drivers/vfio/mdev/mdev_core.c | 388 +++++++ drivers/vfio/mdev/mdev_driver.c | 122 ++ drivers/vfio/mdev/mdev_private.h | 41 + drivers/vfio/mdev/mdev_sysfs.c | 286 +++++ drivers/vfio/mdev/vfio_mdev.c | 167 +++ drivers/vfio/pci/vfio_pci.c | 83 +- drivers/vfio/platform/vfio_platform_common.c | 31 +- drivers/vfio/vfio.c | 334 +++++- drivers/vfio/vfio_iommu_type1.c | 831 ++++++++++++-- include/linux/mdev.h | 176 +++ include/linux/vfio.h | 32 +- include/uapi/linux/vfio.h | 10 + samples/vfio-mdev/Makefile | 13 + samples/vfio-mdev/mtty.c | 1503 +++++++++++++++++++++++++ 21 files changed, 4342 insertions(+), 218 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-bus-vfio-mdev create mode 100644 Documentation/vfio-mediated-device.txt create mode 100644 drivers/vfio/mdev/Kconfig create mode 100644 drivers/vfio/mdev/Makefile create mode 100644 drivers/vfio/mdev/mdev_core.c create mode 100644 drivers/vfio/mdev/mdev_driver.c create mode 100644 drivers/vfio/mdev/mdev_private.h create mode 100644 drivers/vfio/mdev/mdev_sysfs.c create mode 100644 drivers/vfio/mdev/vfio_mdev.c create mode 100644 include/linux/mdev.h create mode 100644 samples/vfio-mdev/Makefile create mode 100644 samples/vfio-mdev/mtty.c
Signature · 1 line
Alexey
Kirti Wankhede
maintainerQuoted 45 lines · Alexey Kardashevskiy
Quoted 44 lines · Kirti Wankhede
This series adds Mediated device support to Linux host kernel. Purpose of this series is to provide a common interface for mediated device management that can be used by different devices. This series introduces Mdev core module that creates and manages mediated devices, VFIO based driver for mediated devices that are created by mdev core module and update VFIO type1 IOMMU module to support pinning & unpinning for mediated devices.
What changed in v11?
mdev core:
Registermdev_busclass when first device is registed to avoid panic if any vendor driver and mdev driver are selected as built-in but vendor driver loads first and then mdev module.vfio_mdev:
Added notifier callback function to mdev parent's ops so that notifer is registered fromvfio_mdevmodule during device open and unregistered it from device close call. This is a optional callback. Some drivers using mdev framework might not pin or unpin pages, for example the sample mtty driver that simulates serial port. Vendor driver who need to pin/unpin pages should provide this callback. Otherwise pin request would fail.vfio_iommu_type1:
Updated to keep track of who (task and address space) mapped iova range. DuringDMA_UNMAP, same task who mapped it or other task who shares same address space is allowed to unmap, otherwise unmap fails. QEMU maps few iova ranges initially, then fork threads and from the child thread callsDMA_UNMAPon previously mapped iova. Since child shares same address space,DMA_UNMAPis successful.
Address space keeps track of pages pinned (pfn_list) by external user / mdev devices. Thispfn_listis used to verify pfn duringunpin_request, re-accounting of pages when direct device assigned in hot-unplugged and mdev device is present in same container.
When the container is released, all mapped iova from all tasks are unmapped and removed.Tested by assigning below combinations of devices to a single VM:
- GPU pass through only
- vGPU device only
- One GPU pass through and one vGPU device
- Linux VM hot plug and unplug vGPU device while GPU pass through device exist
- Linux VM hot plug and unplug GPU pass through device while vGPU device exist
Quoted 6 lines
Quoted 2 lines
Patch series tested with linux-next upto commit
14970f204b19@Fri Oct 28 Resolved against conflicting change:Patch 11/22 does not apply if the patchset is applied on top of
14970f204b19.
Pull commit 05692d7005a3 (below) commit.
Quoted 3 lines
Quoted 3 lines
05692d7005a3vfio/pci: Fix integer overflows, bitmask check
Thanks,
Kirti
Quoted 64 lines
Quoted 62 lines
Kirti Wankhede (22):
vfio: Mediated device Core driver
vfio: VFIO based driver for Mediated devices
vfio: Rearrange functions to getvfio_groupfrom dev
vfio: Common function to incrementcontainer_users
vfio iommu: Added pin and unpin callback functions to
vfio_iommu_driver_ops
vfio iommu type1: Update arguments ofvfio_lock_acctvfio iommu type1: Update argument ofvaddr_get_pfn()vfio iommu type1: Addfind_iommu_group()function
vfio iommu type1: Add task structure tovfio_dma
vfio iommu type1: Add support for mediated devices
vfio iommu: Add blocking notifier to notifyDMA_UNMAP
vfio: Add notifier callback to parent's ops structure of mdev
vfio: Introduce common function to add capabilities
vfio_pci: Updatevfio_pcito usevfio_info_add_capability()
vfio: Introducevfio_set_irqs_validate_and_prepare()
vfio_pci: Updated to usevfio_set_irqs_validate_and_prepare()
vfio_platform: Updated to usevfio_set_irqs_validate_and_prepare()
vfio: Definedevice_apistrings
docs: Add Documentation for Mediated devices
docs: Sysfs ABI for mediated device framework
docs: Sample driver to demonstrate how to use Mediated device
framework.
MAINTAINERS: Add entry VFIO based Mediated device driversDocumentation/ABI/testing/sysfs-bus-vfio-mdev | 111 ++ Documentation/vfio-mediated-device.txt | 399 +++++++ MAINTAINERS | 9 + drivers/vfio/Kconfig | 1 + drivers/vfio/Makefile | 1 + drivers/vfio/mdev/Kconfig | 17 + drivers/vfio/mdev/Makefile | 5 + drivers/vfio/mdev/mdev_core.c | 388 +++++++ drivers/vfio/mdev/mdev_driver.c | 122 ++ drivers/vfio/mdev/mdev_private.h | 41 + drivers/vfio/mdev/mdev_sysfs.c | 286 +++++ drivers/vfio/mdev/vfio_mdev.c | 167 +++ drivers/vfio/pci/vfio_pci.c | 83 +- drivers/vfio/platform/vfio_platform_common.c | 31 +- drivers/vfio/vfio.c | 334 +++++- drivers/vfio/vfio_iommu_type1.c | 831 ++++++++++++-- include/linux/mdev.h | 176 +++ include/linux/vfio.h | 32 +- include/uapi/linux/vfio.h | 10 + samples/vfio-mdev/Makefile | 13 + samples/vfio-mdev/mtty.c | 1503 +++++++++++++++++++++++++ 21 files changed, 4342 insertions(+), 218 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-bus-vfio-mdev create mode 100644 Documentation/vfio-mediated-device.txt create mode 100644 drivers/vfio/mdev/Kconfig create mode 100644 drivers/vfio/mdev/Makefile create mode 100644 drivers/vfio/mdev/mdev_core.c create mode 100644 drivers/vfio/mdev/mdev_driver.c create mode 100644 drivers/vfio/mdev/mdev_private.h create mode 100644 drivers/vfio/mdev/mdev_sysfs.c create mode 100644 drivers/vfio/mdev/vfio_mdev.c create mode 100644 include/linux/mdev.h create mode 100644 samples/vfio-mdev/Makefile create mode 100644 samples/vfio-mdev/mtty.c
Kirti Wankhede
maintainerVerified that this patch series gets applied cleanly to Linux-next upto:bc33b0ca11e3 Linux 4.9-rc4
Also tested this patch series with above kernel.
Thanks,
Kirti
Quoted 128 lines · Kirti Wankhede
Quoted 45 lines · Alexey Kardashevskiy
Quoted 44 lines · Kirti Wankhede
This series adds Mediated device support to Linux host kernel. Purpose of this series is to provide a common interface for mediated device management that can be used by different devices. This series introduces Mdev core module that creates and manages mediated devices, VFIO based driver for mediated devices that are created by mdev core module and update VFIO type1 IOMMU module to support pinning & unpinning for mediated devices.
What changed in v11?
mdev core:
Registermdev_busclass when first device is registed to avoid panic if any vendor driver and mdev driver are selected as built-in but vendor driver loads first and then mdev module.vfio_mdev:
Added notifier callback function to mdev parent's ops so that notifer is registered fromvfio_mdevmodule during device open and unregistered it from device close call. This is a optional callback. Some drivers using mdev framework might not pin or unpin pages, for example the sample mtty driver that simulates serial port. Vendor driver who need to pin/unpin pages should provide this callback. Otherwise pin request would fail.vfio_iommu_type1:
Updated to keep track of who (task and address space) mapped iova range. DuringDMA_UNMAP, same task who mapped it or other task who shares same address space is allowed to unmap, otherwise unmap fails. QEMU maps few iova ranges initially, then fork threads and from the child thread callsDMA_UNMAPon previously mapped iova. Since child shares same address space,DMA_UNMAPis successful.
Address space keeps track of pages pinned (pfn_list) by external user / mdev devices. Thispfn_listis used to verify pfn duringunpin_request, re-accounting of pages when direct device assigned in hot-unplugged and mdev device is present in same container.
When the container is released, all mapped iova from all tasks are unmapped and removed.Tested by assigning below combinations of devices to a single VM:
- GPU pass through only
- vGPU device only
- One GPU pass through and one vGPU device
- Linux VM hot plug and unplug vGPU device while GPU pass through device exist
- Linux VM hot plug and unplug GPU pass through device while vGPU device existQuoted 6 lines
Quoted 2 lines
Patch series tested with linux-next upto commit
14970f204b19@Fri Oct 28 Resolved against conflicting change:Patch 11/22 does not apply if the patchset is applied on top of
14970f204b19.Pull commit
05692d7005a3(below) commit.Quoted 3 lines
Quoted 3 lines
05692d7005a3vfio/pci: Fix integer overflows, bitmask checkThanks,
KirtiQuoted 64 lines
Quoted 62 lines
Kirti Wankhede (22):
vfio: Mediated device Core driver
vfio: VFIO based driver for Mediated devices
vfio: Rearrange functions to getvfio_groupfrom dev
vfio: Common function to incrementcontainer_users
vfio iommu: Added pin and unpin callback functions to
vfio_iommu_driver_ops
vfio iommu type1: Update arguments ofvfio_lock_acctvfio iommu type1: Update argument ofvaddr_get_pfn()vfio iommu type1: Addfind_iommu_group()function
vfio iommu type1: Add task structure tovfio_dma
vfio iommu type1: Add support for mediated devices
vfio iommu: Add blocking notifier to notifyDMA_UNMAP
vfio: Add notifier callback to parent's ops structure of mdev
vfio: Introduce common function to add capabilities
vfio_pci: Updatevfio_pcito usevfio_info_add_capability()
vfio: Introducevfio_set_irqs_validate_and_prepare()
vfio_pci: Updated to usevfio_set_irqs_validate_and_prepare()
vfio_platform: Updated to usevfio_set_irqs_validate_and_prepare()
vfio: Definedevice_apistrings
docs: Add Documentation for Mediated devices
docs: Sysfs ABI for mediated device framework
docs: Sample driver to demonstrate how to use Mediated device
framework.
MAINTAINERS: Add entry VFIO based Mediated device driversDocumentation/ABI/testing/sysfs-bus-vfio-mdev | 111 ++ Documentation/vfio-mediated-device.txt | 399 +++++++ MAINTAINERS | 9 + drivers/vfio/Kconfig | 1 + drivers/vfio/Makefile | 1 + drivers/vfio/mdev/Kconfig | 17 + drivers/vfio/mdev/Makefile | 5 + drivers/vfio/mdev/mdev_core.c | 388 +++++++ drivers/vfio/mdev/mdev_driver.c | 122 ++ drivers/vfio/mdev/mdev_private.h | 41 + drivers/vfio/mdev/mdev_sysfs.c | 286 +++++ drivers/vfio/mdev/vfio_mdev.c | 167 +++ drivers/vfio/pci/vfio_pci.c | 83 +- drivers/vfio/platform/vfio_platform_common.c | 31 +- drivers/vfio/vfio.c | 334 +++++- drivers/vfio/vfio_iommu_type1.c | 831 ++++++++++++-- include/linux/mdev.h | 176 +++ include/linux/vfio.h | 32 +- include/uapi/linux/vfio.h | 10 + samples/vfio-mdev/Makefile | 13 + samples/vfio-mdev/mtty.c | 1503 +++++++++++++++++++++++++ 21 files changed, 4342 insertions(+), 218 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-bus-vfio-mdev create mode 100644 Documentation/vfio-mediated-device.txt create mode 100644 drivers/vfio/mdev/Kconfig create mode 100644 drivers/vfio/mdev/Makefile create mode 100644 drivers/vfio/mdev/mdev_core.c create mode 100644 drivers/vfio/mdev/mdev_driver.c create mode 100644 drivers/vfio/mdev/mdev_private.h create mode 100644 drivers/vfio/mdev/mdev_sysfs.c create mode 100644 drivers/vfio/mdev/vfio_mdev.c create mode 100644 include/linux/mdev.h create mode 100644 samples/vfio-mdev/Makefile create mode 100644 samples/vfio-mdev/mtty.c
Quoted 48 lines · Kirti Wankhede
Quoted 45 lines · Alexey Kardashevskiy
Quoted 44 lines · Kirti Wankhede
This series adds Mediated device support to Linux host kernel. Purpose of this series is to provide a common interface for mediated device management that can be used by different devices. This series introduces Mdev core module that creates and manages mediated devices, VFIO based driver for mediated devices that are created by mdev core module and update VFIO type1 IOMMU module to support pinning & unpinning for mediated devices.
What changed in v11?
mdev core:
Registermdev_busclass when first device is registed to avoid panic if any vendor driver and mdev driver are selected as built-in but vendor driver loads first and then mdev module.vfio_mdev:
Added notifier callback function to mdev parent's ops so that notifer is registered fromvfio_mdevmodule during device open and unregistered it from device close call. This is a optional callback. Some drivers using mdev framework might not pin or unpin pages, for example the sample mtty driver that simulates serial port. Vendor driver who need to pin/unpin pages should provide this callback. Otherwise pin request would fail.vfio_iommu_type1:
Updated to keep track of who (task and address space) mapped iova range. DuringDMA_UNMAP, same task who mapped it or other task who shares same address space is allowed to unmap, otherwise unmap fails. QEMU maps few iova ranges initially, then fork threads and from the child thread callsDMA_UNMAPon previously mapped iova. Since child shares same address space,DMA_UNMAPis successful.
Address space keeps track of pages pinned (pfn_list) by external user / mdev devices. Thispfn_listis used to verify pfn duringunpin_request, re-accounting of pages when direct device assigned in hot-unplugged and mdev device is present in same container.
When the container is released, all mapped iova from all tasks are unmapped and removed.Tested by assigning below combinations of devices to a single VM:
- GPU pass through only
- vGPU device only
- One GPU pass through and one vGPU device
- Linux VM hot plug and unplug vGPU device while GPU pass through device exist
- Linux VM hot plug and unplug GPU pass through device while vGPU device exist
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
Patch series tested with linux-next upto commit
14970f204b19@Fri Oct 28 Resolved against conflicting change:Patch 11/22 does not apply if the patchset is applied on top of
14970f204b19.Pull commit
05692d7005a3(below) commit.
Nah, it was my fault, thunderbird's plugin saved mails in wrong order, false alarm, sorry.
Speaking of order, 11/22 is better be squashed into 02/22 (as it does not seem to make sense alone; it also does not depend on something between 02/22 and 11/22); and both 01/22 and 02/22 better be moved where "docs" patches start as they do not makes sense earlier anyway, or do they?
Quoted 72 lines
Quoted 4 lines
Quoted 4 lines
05692d7005a3vfio/pci: Fix integer overflows, bitmask checkThanks,
KirtiQuoted 64 lines
Quoted 62 lines
Kirti Wankhede (22):
vfio: Mediated device Core driver
vfio: VFIO based driver for Mediated devices
vfio: Rearrange functions to getvfio_groupfrom dev
vfio: Common function to incrementcontainer_users
vfio iommu: Added pin and unpin callback functions to
vfio_iommu_driver_ops
vfio iommu type1: Update arguments ofvfio_lock_acctvfio iommu type1: Update argument ofvaddr_get_pfn()vfio iommu type1: Addfind_iommu_group()function
vfio iommu type1: Add task structure tovfio_dma
vfio iommu type1: Add support for mediated devices
vfio iommu: Add blocking notifier to notifyDMA_UNMAP
vfio: Add notifier callback to parent's ops structure of mdev
vfio: Introduce common function to add capabilities
vfio_pci: Updatevfio_pcito usevfio_info_add_capability()
vfio: Introducevfio_set_irqs_validate_and_prepare()
vfio_pci: Updated to usevfio_set_irqs_validate_and_prepare()
vfio_platform: Updated to usevfio_set_irqs_validate_and_prepare()
vfio: Definedevice_apistrings
docs: Add Documentation for Mediated devices
docs: Sysfs ABI for mediated device framework
docs: Sample driver to demonstrate how to use Mediated device
framework.
MAINTAINERS: Add entry VFIO based Mediated device driversDocumentation/ABI/testing/sysfs-bus-vfio-mdev | 111 ++ Documentation/vfio-mediated-device.txt | 399 +++++++ MAINTAINERS | 9 + drivers/vfio/Kconfig | 1 + drivers/vfio/Makefile | 1 + drivers/vfio/mdev/Kconfig | 17 + drivers/vfio/mdev/Makefile | 5 + drivers/vfio/mdev/mdev_core.c | 388 +++++++ drivers/vfio/mdev/mdev_driver.c | 122 ++ drivers/vfio/mdev/mdev_private.h | 41 + drivers/vfio/mdev/mdev_sysfs.c | 286 +++++ drivers/vfio/mdev/vfio_mdev.c | 167 +++ drivers/vfio/pci/vfio_pci.c | 83 +- drivers/vfio/platform/vfio_platform_common.c | 31 +- drivers/vfio/vfio.c | 334 +++++- drivers/vfio/vfio_iommu_type1.c | 831 ++++++++++++-- include/linux/mdev.h | 176 +++ include/linux/vfio.h | 32 +- include/uapi/linux/vfio.h | 10 + samples/vfio-mdev/Makefile | 13 + samples/vfio-mdev/mtty.c | 1503 +++++++++++++++++++++++++ 21 files changed, 4342 insertions(+), 218 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-bus-vfio-mdev create mode 100644 Documentation/vfio-mediated-device.txt create mode 100644 drivers/vfio/mdev/Kconfig create mode 100644 drivers/vfio/mdev/Makefile create mode 100644 drivers/vfio/mdev/mdev_core.c create mode 100644 drivers/vfio/mdev/mdev_driver.c create mode 100644 drivers/vfio/mdev/mdev_private.h create mode 100644 drivers/vfio/mdev/mdev_sysfs.c create mode 100644 drivers/vfio/mdev/vfio_mdev.c create mode 100644 include/linux/mdev.h create mode 100644 samples/vfio-mdev/Makefile create mode 100644 samples/vfio-mdev/mtty.c
Signature · 1 line
Alexey
Kirti Wankhede
maintainerQuoted 5 lines · Alexey Kardashevskiy
Quoted 4 lines · Kirti Wankhede
Quoted 1 line · Alexey Kardashevskiy
On 05/11/16 08:10, Kirti Wankhede wrote:
...
Quoted 10 lines
Quoted 9 lines
Quoted 7 lines
Quoted 3 lines
Patch series tested with linux-next upto commit
14970f204b19@Fri Oct 28 Resolved against conflicting change:Patch 11/22 does not apply if the patchset is applied on top of
14970f204b19.Pull commit
05692d7005a3(below) commit.
Quoted 9 lines
Nah, it was my fault, thunderbird's plugin saved mails in wrong order, false alarm, sorry.
Speaking of order, 11/22 is better be squashed into 02/22 (as it does not seem to make sense alone; it also does not depend on something between 02/22 and 11/22); and both 01/22 and 02/22 better be moved where "docs" patches start as they do not makes sense earlier anyway, or do they?
There are dependencies in the patch series:
Patch 10/22 depends on patch 01/22 and patch 03/22 to 09/22 patch.
Patch 02/22 depends on patch 01/22.
Patch 11/22 doesn't make sense without patch 10/22.
Patch 12/22 depends on patch 11/22.
Patch 13/22 to 18/22 are independent, pulling common code in functions. But patch 21/22 uses these APIs in sample driver, so has to be before that.
Patch 19/22 adds basic documentation.
Patch 21/22 depends on 19/22 patch because that adds sample driver and its explanation to documentation.
Thanks,
Kirti
RE: [PATCH v11 01/22] vfio: Mediated device Core driver
In reply to Kirti WankhedeQuoted 3 lines
From: Kirti Wankhede [mailto:[email protected]]
Sent: Saturday, November 05, 2016 5:11 AM
[...]
Quoted 4 lines
Signed-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id I73a5084574270b14541c529461ea2f03c292d510
Jike has given his reviewed-by for some patches in v10. Please include his reviewed-by in new version, which represent acknowledgements from our side and also be informative to other reviewers.
Thanks
Kevin
Quoted 38 lines · Kirti Wankhede
Quoted 5 lines · Alexey Kardashevskiy
Quoted 4 lines · Kirti Wankhede
Quoted 1 line · Alexey Kardashevskiy
On 05/11/16 08:10, Kirti Wankhede wrote:
...
Quoted 10 lines
Quoted 9 lines
Quoted 7 lines
Quoted 3 lines
Patch series tested with linux-next upto commit
14970f204b19@Fri Oct 28 Resolved against conflicting change:Patch 11/22 does not apply if the patchset is applied on top of
14970f204b19.Pull commit
05692d7005a3(below) commit.Quoted 9 lines
Nah, it was my fault, thunderbird's plugin saved mails in wrong order, false alarm, sorry.
Speaking of order, 11/22 is better be squashed into 02/22 (as it does not seem to make sense alone; it also does not depend on something between 02/22 and 11/22); and both 01/22 and 02/22 better be moved where "docs" patches start as they do not makes sense earlier anyway, or do they?
There are dependencies in the patch series:
Patch 10/22 depends on patch 01/22 and patch 03/22 to 09/22 patch.Patch 02/22 depends on patch 01/22.
Patch 11/22 doesn't make sense without patch 10/22.
Quoted 8 lines
Patch 12/22 depends on patch 11/22.
Patch 13/22 to 18/22 are independent, pulling common code in functions. But patch 21/22 uses these APIs in sample driver, so has to be before that.
Patch 19/22 adds basic documentation.
Patch 21/22 depends on 19/22 patch because that adds sample driver and its explanation to documentation.
Ouch, I get it now, sorry. I was compiling on ppc64 and everything related to iommu type1 (i.e. x86) just got skipped.
Signature · 1 line
Alexey
Re: [PATCH v11 07/22] vfio iommu type1: Update argument of vaddr_get_pfn()
In reply to Kirti WankhedeQuoted 18 lines · Kirti Wankhede
Update arguments of
vaddr_get_pfn()to take structmm_struct*mm as input argument.Signed-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id I885fd4cd4a9f66f4ee2c1caf58267464ec239f52--- drivers/vfio/vfio_iommu_type1.c | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-)drivers/vfio/vfio_iommu_type1.c +0
-0@@ -226,20 +226,36 @@ static int put_pfn(unsigned long pfn, int prot)return 0 ; }
drivers/vfio/vfio_iommu_type1.c static int put_pfn(unsigned long pfn, int prot) · line 236
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +3
-2-static int vaddr_get_pfn (unsigned long vaddr,int prot,unsigned long *pfn) +static int vaddr_get_pfn (struct mm_struct *mm,unsigned long vaddr, +int prot,unsigned long *pfn) {struct page *page[1 ];struct vm_area_struct *vma;int ret = -EFAULT ; -if (get_user_pages_fast (vaddr,1 , !!(prot &IOMMU_WRITE ), page) ==1 ) { +if (mm == current->mm)
afaik the rule is if one branch of "if" uses curly braces, the other should do so too.
drivers/vfio/vfio_iommu_type1.c static int put_pfn(unsigned long pfn, int prot) · line 248
Quoted 12 lines
drivers/vfio/vfio_iommu_type1.c +12
-0+ ret =get_user_pages_fast (vaddr,1 , !!(prot &IOMMU_WRITE ), + page); + else { +unsigned int flags =0 ; + +if (prot &IOMMU_WRITE ) + flags |=FOLL_WRITE ; + +down_read (&mm->mmap_sem); + ret =get_user_pages_remote (NULL , mm, vaddr,1 , flags, page, +NULL ); +up_read (&mm->mmap_sem);
This chunk is not just about passing mm everywhere, it would be nice to see in the commit log why this change is in this patch (may be it was commented already, and I just missed it?).
Quoted 43 lines
drivers/vfio/vfio_iommu_type1.c +8
-5+ } + +if (ret ==1 ) { *pfn =page_to_pfn (page[0 ]);return 0 ; } -down_read (¤t->mm->mmap_sem); +down_read (&mm->mmap_sem); - vma =find_vma_intersection (current->mm, vaddr, vaddr +1 ); + vma =find_vma_intersection (mm, vaddr, vaddr +1 );if (vma && vma->vm_flags &VM_PFNMAP ) { *pfn = ((vaddr - vma->vm_start) >>PAGE_SHIFT ) + vma->vm_pgoff;@@ -247,7 +263,7 @@ static int vaddr_get_pfn(unsigned long vaddr, int prot, unsigned long *pfn) ret =0 ; } -up_read (¤t->mm->mmap_sem); +up_read (&mm->mmap_sem);return ret; }@@ -268,7 +284,7 @@ static long __vfio_pin_pages_remote(unsigned long vaddr, long npage,if (!current->mm)return -ENODEV ; - ret =vaddr_get_pfn (vaddr, prot, pfn_base); + ret =vaddr_get_pfn (current->mm, vaddr, prot, pfn_base);if (ret)return ret;@@ -291,7 +307,7 @@ static long __vfio_pin_pages_remote(unsigned long vaddr, long npage,for (i =1 , vaddr +=PAGE_SIZE ; i < npage; i++, vaddr +=PAGE_SIZE ) {unsigned long pfn =0 ; - ret =vaddr_get_pfn (vaddr, prot, &pfn); + ret =vaddr_get_pfn (current->mm, vaddr, prot, &pfn);if (ret)break ;
Signature · 1 line
Alexey
Re: [PATCH v11 05/22] vfio iommu: Added pin and unpin callback functions to vfio_iommu_driver_ops
In reply to Kirti WankhedeQuoted 53 lines · On Sat, 5 Nov 2016 02:40:39 +0530 Kirti Wankhede <[email protected]> wrote:
Added two new callback functions to struct
vfio_iommu_driver_ops. Backend IOMMU module that supports pining and unpinning pages for mdev devices should provide these functions.
Added APIs for pining and unpining pages to VFIO module. These calls back into backend iommu module to actually pin and unpin pages. Renamed static functions invfio_type1_iommu.cto resolve conflictsSigned-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id Ia7417723aaae86bec2959ad9ae6c2915ddd340e0--- drivers/vfio/vfio.c | 96 +++++++++++++++++++++++++++++++++++++++++ drivers/vfio/vfio_iommu_type1.c | 20 ++++----- include/linux/vfio.h | 14 +++++- 3 files changed, 119 insertions(+), 11 deletions(-)drivers/vfio/vfio.c +29
-0@@ -1799,6 +1799,102 @@ void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset) }EXPORT_SYMBOL_GPL (vfio_info_cap_shift); + +/* +* Pin a set of guest PFNs and return their associated host PFNs for local +* domain only. +* @dev [in] : device +* @user_pfn [in]: array of user/guest PFNs +* @npage [in]: count of array elements +* @prot [in] : protection flags +* @phys_pfn[out] : array of host PFNs +* Return error or number of pages pinned. +*/ +int vfio_pin_pages (struct device *dev,unsigned long *user_pfn, +int npage,int prot,unsigned long *phys_pfn) +{ +struct vfio_container *container; +struct vfio_group *group; +struct vfio_iommu_driver *driver; +int ret; + +if (!dev || !user_pfn || !phys_pfn) +return -EINVAL ; + + group =vfio_group_get_from_dev (dev); +if (IS_ERR (group)) +return PTR_ERR (group); + + ret =vfio_group_add_container_user (group); +if (ret) + goto err_pin_pages;
drivers/vfio/vfio.c void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset) · line 1840
Quoted 10 lines
drivers/vfio/vfio.c +10
-0+ + container = group->container; +down_read (&container->group_lock); + + driver = container->iommu_driver; +if (likely (driver && driver->ops->pin_pages)) + ret = driver->ops->pin_pages (container->iommu_data, user_pfn, + npage, prot, phys_pfn); +else + ret = -EINVAL ;
-ENOTTY might be a more appropriate error return here and below since we're not signaling invalid argument, we're signaling lack of support.
drivers/vfio/vfio.c void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset) · line 1848
Quoted 8 lines
drivers/vfio/vfio.c +8
-0+ +up_read (&container->group_lock); +vfio_group_try_dissolve_container (group); + +err_pin_pages: +vfio_group_put (group); +return ret; +
Unnecessary extra line
Quoted 57 lines
drivers/vfio/vfio.c +49
-0+} +EXPORT_SYMBOL (vfio_pin_pages); + +/* +* Unpin set of host PFNs for local domain only. +* @dev [in] : device +* @user_pfn [in]: array of user/guest PFNs to be unpinned +* @pfn [in] : array of host PFNs to be unpinned. +* @npage [in] :count of elements in array, that is number of pages. +* Return error or number of pages unpinned. +*/ +int vfio_unpin_pages (struct device *dev,unsigned long *user_pfn, +unsigned long *pfn,int npage) +{ +struct vfio_container *container; +struct vfio_group *group; +struct vfio_iommu_driver *driver; +int ret; + +if (!dev || !pfn) +return -EINVAL ; + + group =vfio_group_get_from_dev (dev); +if (IS_ERR (group)) +return PTR_ERR (group); + + ret =vfio_group_add_container_user (group); +if (ret) + goto err_unpin_pages; + + container = group->container; +down_read (&container->group_lock); + + driver = container->iommu_driver; +if (likely (driver && driver->ops->unpin_pages)) + ret = driver->ops->unpin_pages (container->iommu_data, user_pfn, + pfn, npage); +else + ret = -EINVAL ; + +up_read (&container->group_lock); +vfio_group_try_dissolve_container (group); + +err_unpin_pages: +vfio_group_put (group); +return ret; +} +EXPORT_SYMBOL (vfio_unpin_pages); +/** * Module/class support */ drivers/vfio/vfio_iommu_type1.c +0
-0
drivers/vfio/vfio_iommu_type1.c static int vaddr_get_pfn(unsigned long vaddr, int prot, unsigned long *pfn) · line 263
Quoted 7 lines
drivers/vfio/vfio_iommu_type1.c +2
-2@@ -259,8 +259,8 @@ static int vaddr_get_pfn(unsigned long vaddr, int prot, unsigned long *pfn)* the iommu can only map chunks of consecutive pfns anyway, so get the * first page and all consecutive pages with the same locking. */ -static long vfio_pin_pages (unsigned long vaddr,long npage, -int prot,unsigned long *pfn_base) +static long __vfio_pin_pages_remote (unsigned long vaddr,long npage, +int prot,unsigned long *pfn_base)
nit, what is the additional underscore prefix intended to imply? Appending _remote is sufficient to avoid the symbol conflict.
Quoted 52 lines
drivers/vfio/vfio_iommu_type1.c +8
-8{unsigned long limit =rlimit (RLIMIT_MEMLOCK ) >>PAGE_SHIFT ;bool lock_cap =capable (CAP_IPC_LOCK );@@ -318,8 +318,8 @@ static long vfio_pin_pages(unsigned long vaddr, long npage,return i; } -static long vfio_unpin_pages (unsigned long pfn,long npage, -int prot,bool do_accounting) +static long __vfio_unpin_pages_remote (unsigned long pfn,long npage, +int prot,bool do_accounting) {unsigned long unlocked =0 ;long i;@@ -382,9 +382,9 @@ static void vfio_unmap_unpin(struct vfio_iommu *iommu, struct vfio_dma *dma)if (WARN_ON (!unmapped))break ; - unlocked +=vfio_unpin_pages (phys >>PAGE_SHIFT , - unmapped >>PAGE_SHIFT , - dma->prot, false); + unlocked +=__vfio_unpin_pages_remote (phys >>PAGE_SHIFT , + unmapped >>PAGE_SHIFT , + dma->prot, false); iova += unmapped;cond_resched ();@@ -613,8 +613,8 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu,while (size) {/* Pin a contiguous chunk of memory */ - npage =vfio_pin_pages (vaddr + dma->size, - size >>PAGE_SHIFT , prot, &pfn); + npage =__vfio_pin_pages_remote (vaddr + dma->size, + size >>PAGE_SHIFT , prot, &pfn);if (npage <=0 ) {WARN_ON (!npage); ret = (int )npage;@@ -624,7 +624,7 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu,/* Map it! */ ret =vfio_iommu_map (iommu, iova + dma->size, pfn, npage, prot);if (ret) { -vfio_unpin_pages (pfn, npage, prot, true); +__vfio_unpin_pages_remote (pfn, npage, prot, true);break ; }include/linux/vfio.h +0
-0
include/linux/vfio.h struct vfio_iommu_driver_ops { · line 81
Quoted 8 lines
include/linux/vfio.h +4
-1@@ -75,7 +75,13 @@ struct vfio_iommu_driver_ops { struct iommu_group *group);void (*detach_group)(void *iommu_data,struct iommu_group *group); - +int (*pin_pages)(void *iommu_data,unsigned long *user_pfn, +int npage,int prot, +unsigned long *phys_pfn); + int (*unpin_pages)(void *iommu_data,
Are we changing from long to int here simply because of the absurdity in passing in more than a 2^31 entry array, that would already consume more than 16GB itself?
include/linux/vfio.h struct vfio_iommu_driver_ops { · line 83
Quoted 2 lines
include/linux/vfio.h +2
-0+unsigned long *user_pfn, + unsigned long *pfn,
nit, use phys_pfn so as to match the pin function?
Quoted 17 lines
include/linux/vfio.h +7
-0+ int npage); };extern int vfio_register_iommu_driver (const struct vfio_iommu_driver_ops *ops);@@ -127,6 +133,12 @@ static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group, }#endif /* CONFIG_EEH */ +extern int vfio_pin_pages (struct device *dev,unsigned long *user_pfn, +int npage,int prot,unsigned long *phys_pfn); + +extern int vfio_unpin_pages (struct device *dev,unsigned long *user_pfn, +unsigned long *pfn,int npage); +/* * IRQfd - generic */
Re: [PATCH v11 09/22] vfio iommu type1: Add task structure to vfio_dma
In reply to Kirti WankhedeQuoted 274 lines · On Sat, 5 Nov 2016 02:40:43 +0530 Kirti Wankhede <[email protected]> wrote:
Add task structure to
vfio_dma.
Add address space structure. Eachvfio_dmastructure points to the address space of the task who mapped it.
List of address spaces is maintained invfio_iommustructure. FromDMA_MAPcall if address space already exist in address space list,vfio_dmapoints to it. If address space doesn't exist, allocate address space, save pointer of mm to it andvfio_dmapoints to it.
Two tasks can share same address space and so we need keep address space structure different from task invfio_dmastructure.vfio_dmakeeps pointer to its corresponding address space.
DuringDMA_UNMAP, same task who mapped it or other task who shares same address space is allowed to unmap, otherwise unmap fails.
QEMU maps few iova ranges initially, then fork threads and from the child thread callsDMA_UNMAPon previously mapped iova. Since child shares same address space,DMA_UNMAPis successful.
This address space structure is used to track pages pinned by external user in later changes.Signed-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id I7600f1bea6b384fd589fa72421ccf031bcfd9ac5--- drivers/vfio/vfio_iommu_type1.c | 182 +++++++++++++++++++++++++++++----------- 1 file changed, 134 insertions(+), 48 deletions(-)drivers/vfio/vfio_iommu_type1.c +97
-20@@ -55,12 +55,20 @@ MODULE_PARM_DESC(disable_hugepages,struct vfio_iommu {struct list_head domain_list; +struct list_head addr_space_list;struct mutex lock;struct rb_root dma_list;bool v2;bool nesting; }; +/* address space */ +struct vfio_addr_space { +struct mm_struct *mm; +struct list_head next; +atomic_t ref_count; +}; +struct vfio_domain {struct iommu_domain *domain;struct list_head next;@@ -75,6 +83,9 @@ struct vfio_dma {unsigned long vaddr;/* Process virtual addr */ size_t size;/* Map size (bytes) */ int prot;/* IOMMU_READ/WRITE */ +struct vfio_addr_space *addr_space; +struct task_struct *task; +bool mlock_cap; };struct vfio_group {@@ -130,6 +141,18 @@ static void vfio_unlink_dma(struct vfio_iommu *iommu, struct vfio_dma *old)rb_erase (&old->node, &iommu->dma_list); } +static struct vfio_addr_space *vfio_find_addr_space (struct vfio_iommu *iommu, +struct mm_struct *mm) +{ +struct vfio_addr_space *as; + +list_for_each_entry (as, &iommu->addr_space_list, next) { +if (as->mm == mm) +return as; + } +return NULL ; +} +struct vwork {struct mm_struct *mm;long npage;@@ -273,24 +296,24 @@ static int vaddr_get_pfn(struct mm_struct *mm, unsigned long vaddr,* the iommu can only map chunks of consecutive pfns anyway, so get the * first page and all consecutive pages with the same locking. */ -static long __vfio_pin_pages_remote (unsigned long vaddr,long npage, -int prot,unsigned long *pfn_base) +static long __vfio_pin_pages_remote (struct vfio_dma *dma,unsigned long vaddr, +long npage,int prot, +unsigned long *pfn_base) { -unsigned long limit =rlimit (RLIMIT_MEMLOCK ) >>PAGE_SHIFT ; -bool lock_cap =capable (CAP_IPC_LOCK ); +struct task_struct *task = dma->task; +unsigned long limit =task_rlimit (task,RLIMIT_MEMLOCK ) >>PAGE_SHIFT ; +bool lock_cap = dma->mlock_cap; +struct mm_struct *mm = dma->addr_space->mm;long ret, i;bool rsvd; -if (!current->mm) -return -ENODEV ; - - ret =vaddr_get_pfn (current->mm, vaddr, prot, pfn_base); + ret =vaddr_get_pfn (mm, vaddr, prot, pfn_base);if (ret)return ret; rsvd =is_invalid_reserved_pfn (*pfn_base); -if (!rsvd && !lock_cap && current->mm->locked_vm +1 > limit) { +if (!rsvd && !lock_cap && mm->locked_vm +1 > limit) {put_pfn (*pfn_base, prot);pr_warn ("%s: RLIMIT_MEMLOCK (%ld) exceeded\n" , __func__, limit <<PAGE_SHIFT );@@ -299,7 +322,7 @@ static long __vfio_pin_pages_remote(unsigned long vaddr, long npage,if (unlikely (disable_hugepages)) {if (!rsvd) -vfio_lock_acct (current->mm,1 ); +vfio_lock_acct (mm,1 );return 1 ; }@@ -307,7 +330,7 @@ static long __vfio_pin_pages_remote(unsigned long vaddr, long npage,for (i =1 , vaddr +=PAGE_SIZE ; i < npage; i++, vaddr +=PAGE_SIZE ) {unsigned long pfn =0 ; - ret =vaddr_get_pfn (current->mm, vaddr, prot, &pfn); + ret =vaddr_get_pfn (mm, vaddr, prot, &pfn);if (ret)break ;@@ -318,7 +341,7 @@ static long __vfio_pin_pages_remote(unsigned long vaddr, long npage, }if (!rsvd && !lock_cap && - current->mm->locked_vm + i +1 > limit) { + mm->locked_vm + i +1 > limit) {put_pfn (pfn, prot);pr_warn ("%s: RLIMIT_MEMLOCK (%ld) exceeded\n" , __func__, limit <<PAGE_SHIFT );@@ -327,13 +350,13 @@ static long __vfio_pin_pages_remote(unsigned long vaddr, long npage, }if (!rsvd) -vfio_lock_acct (current->mm, i); +vfio_lock_acct (mm, i);return i; } -static long __vfio_unpin_pages_remote (unsigned long pfn,long npage, -int prot,bool do_accounting) +static long __vfio_unpin_pages_remote (struct vfio_dma *dma,unsigned long pfn, +long npage,int prot,bool do_accounting) {unsigned long unlocked =0 ;long i;@@ -342,7 +365,7 @@ static long __vfio_unpin_pages_remote(unsigned long pfn, long npage, unlocked +=put_pfn (pfn++, prot);if (do_accounting) -vfio_lock_acct (current->mm, -unlocked); +vfio_lock_acct (dma->addr_space->mm, -unlocked);return unlocked; }@@ -396,7 +419,7 @@ static void vfio_unmap_unpin(struct vfio_iommu *iommu, struct vfio_dma *dma)if (WARN_ON (!unmapped))break ; - unlocked +=__vfio_unpin_pages_remote (phys >>PAGE_SHIFT , + unlocked +=__vfio_unpin_pages_remote (dma, phys >>PAGE_SHIFT , unmapped >>PAGE_SHIFT , dma->prot, false); iova += unmapped;@@ -404,13 +427,20 @@ static void vfio_unmap_unpin(struct vfio_iommu *iommu, struct vfio_dma *dma)cond_resched (); } -vfio_lock_acct (current->mm, -unlocked); +vfio_lock_acct (dma->addr_space->mm, -unlocked); }static void vfio_remove_dma (struct vfio_iommu *iommu,struct vfio_dma *dma) {vfio_unmap_unpin (iommu, dma);vfio_unlink_dma (iommu, dma); + +if (atomic_dec_and_test (&dma->addr_space->ref_count)) { +mmput (dma->addr_space->mm); +put_task_struct (dma->task); +list_del (&dma->addr_space->next); +kfree (dma->addr_space); + }kfree (dma); }@@ -506,6 +536,12 @@ static int vfio_dma_do_unmap(struct vfio_iommu *iommu,while ((dma =vfio_find_dma (iommu, unmap->iova, unmap->size))) {if (!iommu->v2 && unmap->iova > dma->iova)break ; +/* +* Task with same address space who mapped this iova range is +* allowed to unmap the iova range. +*/ +if (dma->task->mm != current->mm) +break ; unmapped += dma->size;vfio_remove_dma (iommu, dma); }@@ -572,17 +608,58 @@ unwind:return ret; } +static int vfio_pin_map_dma (struct vfio_iommu *iommu,struct vfio_dma *dma, +size_t map_size) +{ +dma_addr_t iova = dma->iova; +unsigned long vaddr = dma->vaddr; +size_t size = map_size; +long npage; +unsigned long pfn; +int ret =0 ; + +while (size) { +/* Pin a contiguous chunk of memory */ + npage =__vfio_pin_pages_remote (dma, vaddr + dma->size, + size >>PAGE_SHIFT , dma->prot, + &pfn); +if (npage <=0 ) { +WARN_ON (!npage); + ret = (int )npage; +break ; + } + +/* Map it! */ + ret =vfio_iommu_map (iommu, iova + dma->size, pfn, npage, + dma->prot); +if (ret) { +__vfio_unpin_pages_remote (dma, pfn, npage, dma->prot, + true); +break ; + } + + size -= npage <<PAGE_SHIFT ; + dma->size += npage <<PAGE_SHIFT ; + } + +if (ret) +vfio_remove_dma (iommu, dma); + +return ret; +} +static int vfio_dma_do_map (struct vfio_iommu *iommu,struct vfio_iommu_type1_dma_map *map) {dma_addr_t iova = map->iova;unsigned long vaddr = map->vaddr;size_t size = map->size; -long npage;int ret =0 , prot =0 ;uint64_t mask;struct vfio_dma *dma; -unsigned long pfn; +struct vfio_addr_space *addr_space; +struct mm_struct *mm; +bool free_addr_space_on_err = false;/* Verify that none of our __u64 fields overflow */ if (map->size != size || map->vaddr != vaddr || map->iova != iova)@@ -608,47 +685,56 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu,mutex_lock (&iommu->lock);
drivers/vfio/vfio_iommu_type1.c static int vfio_dma_do_map(struct vfio_iommu *iommu, · line 694
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +7
-2if (vfio_find_dma (iommu, iova, size)) { -mutex_unlock (&iommu->lock); -return -EEXIST ; + ret = -EEXIST ; + goto do_map_err; + } + + mm =get_task_mm (current); +if (!mm) { + ret = -ENODEV ;
-EFAULT?
Quoted 29 lines
drivers/vfio/vfio_iommu_type1.c +23
-2+ goto do_map_err; + } + + addr_space =vfio_find_addr_space (iommu, mm); +if (addr_space) { +atomic_inc (&addr_space->ref_count); +mmput (mm); + }else { + addr_space =kzalloc (sizeof (*addr_space),GFP_KERNEL ); +if (!addr_space) { + ret = -ENOMEM ; + goto do_map_err; + } + addr_space->mm = mm; +atomic_set (&addr_space->ref_count,1 ); +list_add (&addr_space->next, &iommu->addr_space_list); + free_addr_space_on_err = true; } dma =kzalloc (sizeof (*dma),GFP_KERNEL );if (!dma) { -mutex_unlock (&iommu->lock); -return -ENOMEM ; +if (free_addr_space_on_err) { +mmput (mm); +list_del (&addr_space->next); +kfree (addr_space); + } + ret = -ENOMEM ;
drivers/vfio/vfio_iommu_type1.c static int vfio_dma_do_map(struct vfio_iommu *iommu, · line 731
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +5
-0+ goto do_map_err; } dma->iova = iova; dma->vaddr = vaddr; dma->prot = prot; + dma->addr_space = addr_space; +get_task_struct (current); + dma->task = current; + dma->mlock_cap =capable (CAP_IPC_LOCK );
How do you reason we can cache this? Does the fact that the process had this capability at the time that it did a DMA_MAP imply that it necessarily still has this capability when an external user (vendor driver) tries to pin pages? I don't see how we can make that assumption.
Quoted 33 lines
drivers/vfio/vfio_iommu_type1.c +2
-24/* Insert zero-sized and grow as we map chunks of it */ vfio_link_dma (iommu, dma); -while (size) { -/* Pin a contiguous chunk of memory */ - npage =__vfio_pin_pages_remote (vaddr + dma->size, - size >>PAGE_SHIFT , prot, &pfn); -if (npage <=0 ) { -WARN_ON (!npage); - ret = (int )npage; -break ; - } - -/* Map it! */ - ret =vfio_iommu_map (iommu, iova + dma->size, pfn, npage, prot); -if (ret) { -__vfio_unpin_pages_remote (pfn, npage, prot, true); -break ; - } - - size -= npage <<PAGE_SHIFT ; - dma->size += npage <<PAGE_SHIFT ; - } - -if (ret) -vfio_remove_dma (iommu, dma); - + ret =vfio_pin_map_dma (iommu, dma, size); +do_map_err:mutex_unlock (&iommu->lock);return ret; }
Re: [PATCH v11 10/22] vfio iommu type1: Add support for mediated devices
In reply to Kirti WankhedeQuoted 8 lines · On Sat, 5 Nov 2016 02:40:44 +0530 Kirti Wankhede <[email protected]> wrote:
VFIO IOMMU drivers are designed for the devices which are IOMMU capable. Mediated device only uses IOMMU APIs, the underlying hardware can be managed by an IOMMU domain.
Aim of this change is:
- To use most of the code of TYPE1 IOMMU driver for mediated devices
- To support direct assigned device and mediated device in single module
Quoted 6 lines
This change adds pin and unpin support for mediated device to TYPE1 IOMMU backend module. More details:
-vfio_pin_pages()callback here uses task and address space ofvfio_dma, that is, of the process who mapped that iova range.
- Addedpfn_listtracking logic to address space structure. All pages pinned through this interface are trached in its address space.
^ k
------------------------------------------|
Quoted 255 lines
- Pinned pages list is used to verify unpinning request and to unpin remaining pages while detaching the group for that device.
- Page accounting is updated to account in its address space where the pages are pinned/unpinned.
- Accouting for mdev device is only done if there is no iommu capable domain in the container. When there is a direct device assigned to the container and that domain is iommu capable, all pages are already pinned duringDMA_MAP.
- Page accouting is updated on hot plug and unplug mdev device and pass through device.Tested by assigning below combinations of devices to a single VM:
- GPU pass through only
- vGPU device only
- One GPU pass through and one vGPU device
- Linux VM hot plug and unplug vGPU device while GPU pass through device exist
- Linux VM hot plug and unplug GPU pass through device while vGPU device existSigned-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id I295d6f0f2e0579b8d9882bfd8fd5a4194b97bd9a--- drivers/vfio/vfio_iommu_type1.c | 538 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 500 insertions(+), 38 deletions(-)drivers/vfio/vfio_iommu_type1.c +143
-5@@ -36,6 +36,7 @@#include <linux/uaccess.h> #include <linux/vfio.h> #include <linux/workqueue.h> +#include <linux/mdev.h> #define DRIVER_VERSION "0.2"#define DRIVER_AUTHOR "Alex Williamson <[email protected]>"@@ -56,6 +57,7 @@ MODULE_PARM_DESC(disable_hugepages,struct vfio_iommu {struct list_head domain_list;struct list_head addr_space_list; +struct vfio_domain *external_domain;/* domain for external user */ struct mutex lock;struct rb_root dma_list;bool v2;@@ -67,6 +69,9 @@ struct vfio_addr_space {struct mm_struct *mm;struct list_head next;atomic_t ref_count; +/* external user pinned pfns */ +struct rb_root pfn_list;/* pinned Host pfn list */ +struct mutex pfn_list_lock;/* mutex for pfn_list */ };struct vfio_domain {@@ -83,6 +88,7 @@ struct vfio_dma {unsigned long vaddr;/* Process virtual addr */ size_t size;/* Map size (bytes) */ int prot;/* IOMMU_READ/WRITE */ +bool iommu_mapped;struct vfio_addr_space *addr_space;struct task_struct *task;bool mlock_cap;@@ -94,6 +100,19 @@ struct vfio_group { };/* +* Guest RAM pinning working set or DMA target +*/ +struct vfio_pfn { +struct rb_node node; +unsigned long pfn;/* Host pfn */ +int prot; +atomic_t ref_count; +}; + +#define IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu) \ + (!list_empty(&iommu->domain_list)) + +/* * This code handles mapping and unmapping of user data buffers * into DMA'ble space using the IOMMU */ @@ -153,6 +172,93 @@ static struct vfio_addr_space *vfio_find_addr_space(struct vfio_iommu *iommu,return NULL ; } +/* +* Helper Functions for host pfn list +*/ +static struct vfio_pfn *vfio_find_pfn (struct vfio_addr_space *addr_space, +unsigned long pfn) +{ +struct vfio_pfn *vpfn; +struct rb_node *node = addr_space->pfn_list.rb_node; + +while (node) { + vpfn = rb_entry(node, struct vfio_pfn, node); + +if (pfn < vpfn->pfn) + node = node->rb_left; +else if (pfn > vpfn->pfn) + node = node->rb_right; +else +return vpfn; + } + +return NULL ; +} + +static void vfio_link_pfn (struct vfio_addr_space *addr_space, +struct vfio_pfn *new) +{ +struct rb_node **link, *parent =NULL ; +struct vfio_pfn *vpfn; + + link = &addr_space->pfn_list.rb_node; +while (*link) { + parent = *link; + vpfn = rb_entry(parent, struct vfio_pfn, node); + +if (new->pfn < vpfn->pfn) + link = &(*link)->rb_left; +else + link = &(*link)->rb_right; + } + +rb_link_node (&new->node, parent, link); +rb_insert_color (&new->node, &addr_space->pfn_list); +} + +static void vfio_unlink_pfn (struct vfio_addr_space *addr_space, +struct vfio_pfn *old) +{ +rb_erase (&old->node, &addr_space->pfn_list); +} + +static int vfio_add_to_pfn_list (struct vfio_addr_space *addr_space, +unsigned long pfn,int prot) +{ +struct vfio_pfn *vpfn; + + vpfn =kzalloc (sizeof (*vpfn),GFP_KERNEL ); +if (!vpfn) +return -ENOMEM ; + + vpfn->pfn = pfn; + vpfn->prot = prot; +atomic_set (&vpfn->ref_count,1 ); +vfio_link_pfn (addr_space, vpfn); +return 0 ; +} + +static void vfio_remove_from_pfn_list (struct vfio_addr_space *addr_space, +struct vfio_pfn *vpfn) +{ +vfio_unlink_pfn (addr_space, vpfn); +kfree (vpfn); +} + +static int vfio_pfn_account (struct vfio_addr_space *addr_space, +unsigned long pfn) +{ +struct vfio_pfn *p; +int ret =1 ; + +mutex_lock (&addr_space->pfn_list_lock); + p =vfio_find_pfn (addr_space, pfn); +if (p) + ret =0 ; +mutex_unlock (&addr_space->pfn_list_lock); +return ret; +} +struct vwork {struct mm_struct *mm;long npage;@@ -304,16 +410,18 @@ static long __vfio_pin_pages_remote(struct vfio_dma *dma, unsigned long vaddr,unsigned long limit =task_rlimit (task,RLIMIT_MEMLOCK ) >>PAGE_SHIFT ;bool lock_cap = dma->mlock_cap;struct mm_struct *mm = dma->addr_space->mm; -long ret, i; +long ret, i, lock_acct;bool rsvd; ret =vaddr_get_pfn (mm, vaddr, prot, pfn_base);if (ret)return ret; + lock_acct =vfio_pfn_account (dma->addr_space, *pfn_base); + rsvd =is_invalid_reserved_pfn (*pfn_base); -if (!rsvd && !lock_cap && mm->locked_vm +1 > limit) { +if (!rsvd && !lock_cap && mm->locked_vm + lock_acct > limit) {put_pfn (*pfn_base, prot);pr_warn ("%s: RLIMIT_MEMLOCK (%ld) exceeded\n" , __func__, limit <<PAGE_SHIFT );@@ -340,8 +448,10 @@ static long __vfio_pin_pages_remote(struct vfio_dma *dma, unsigned long vaddr,break ; } + lock_acct +=vfio_pfn_account (dma->addr_space, pfn); +if (!rsvd && !lock_cap && - mm->locked_vm + i +1 > limit) { + mm->locked_vm + lock_acct +1 > limit) {put_pfn (pfn, prot);pr_warn ("%s: RLIMIT_MEMLOCK (%ld) exceeded\n" , __func__, limit <<PAGE_SHIFT );@@ -350,7 +460,7 @@ static long __vfio_pin_pages_remote(struct vfio_dma *dma, unsigned long vaddr, }if (!rsvd) -vfio_lock_acct (mm, i); +vfio_lock_acct (mm, lock_acct);return i; }@@ -370,14 +480,214 @@ static long __vfio_unpin_pages_remote(struct vfio_dma *dma, unsigned long pfn,return unlocked; } -static void vfio_unmap_unpin (struct vfio_iommu *iommu,struct vfio_dma *dma) +static int __vfio_pin_page_external (struct vfio_dma *dma,unsigned long vaddr, +int prot,unsigned long *pfn_base, +bool do_accounting) +{ +struct task_struct *task = dma->task; +unsigned long limit =task_rlimit (task,RLIMIT_MEMLOCK ) >>PAGE_SHIFT ; +bool lock_cap = dma->mlock_cap; +struct mm_struct *mm = dma->addr_space->mm; +int ret; +bool rsvd; + + ret =vaddr_get_pfn (mm, vaddr, prot, pfn_base); +if (ret) +return ret; + + rsvd =is_invalid_reserved_pfn (*pfn_base); + +if (!rsvd && !lock_cap && mm->locked_vm +1 > limit) { +put_pfn (*pfn_base, prot); +pr_warn ("%s: Task %s (%d) RLIMIT_MEMLOCK (%ld) exceeded\n" , + __func__, task->comm,task_pid_nr (task), + limit <<PAGE_SHIFT ); +return -ENOMEM ; + } + +if (!rsvd && do_accounting) +vfio_lock_acct (mm,1 ); + +return 1 ;
drivers/vfio/vfio_iommu_type1.c static long __vfio_unpin_pages_remote(struct vfio_dma *dma, unsigned long pfn, · line 521
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10
-0+} + +static void __vfio_unpin_page_external (struct vfio_addr_space *addr_space, +unsigned long pfn,int prot, +bool do_accounting) +{ +put_pfn (pfn, prot); + +if (do_accounting) +vfio_lock_acct (addr_space->mm,-1 );
Can't we batch this like we do elsewhere? Intel folks, AIUI you intend to pin all VM memory through this side channel, have you tested the scalability and performance of this with larger VMs? Our vfio_pfn data structure alone is 40 bytes per pinned page, which means for each 1GB of VM memory, we have 10MBs worth of struct vfio_pfn! Additionally, unmapping each 1GB of VM memory will result in 256k separate vfio_lock_acct() callbacks. I'm concerned that we're not being efficient enough in either space or time.
One thought might be whether we really need to save the pfn, we better always get the same result if we pin it again, or maybe we can just do a lookup through the mm at that point without re-pinning. Could we get to the point where we only need an atomic_t ref count per page in a linear array relative to the IOVA? That would give us 1MB per 1GB overhead. The semantics of the pin and unpin would make more sense then too, both would take an IOVA range, only pinning would need a return mechanism. For instance:
int pin_pages(void *iommu_data, dma_addr_t iova_base,
int npage, unsigned long *pfn_base);
This would pin physically contiguous pages up to npage, returning the base pfn and returning the number of pages pinned (<= npage). The vendor driver would make multiple calls to fill the necessary range. Unpin would then simply be:
void unpin_pages(void *iommu_data, dma_addr_t iova_base, int npage);
Hugepage usage would really make such an interface shine (ie. 2MB+ contiguous ranges). A downside would be the overhead of getting the group and container reference in vfio for each callback, perhaps we'd need to figure out how the vendor driver could hold that reference. The current API of passing around pfn arrays, further increasing the overhead of the whole ecosystem just makes me cringe though.
Quoted 49 lines
drivers/vfio/vfio_iommu_type1.c +49
-0+} + +static int vfio_unpin_pfn (struct vfio_addr_space *addr_space, +struct vfio_pfn *vpfn,bool do_accounting) +{ +__vfio_unpin_page_external (addr_space, vpfn->pfn, vpfn->prot, + do_accounting); + +if (atomic_dec_and_test (&vpfn->ref_count)) +vfio_remove_from_pfn_list (addr_space, vpfn); + +return 1 ; +} + +static int vfio_iommu_type1_pin_pages (void *iommu_data, +unsigned long *user_pfn, +int npage,int prot, +unsigned long *phys_pfn) +{ +struct vfio_iommu *iommu = iommu_data; +int i, j, ret; +unsigned long remote_vaddr; +unsigned long *pfn = phys_pfn; +struct vfio_dma *dma; +bool do_accounting; + +if (!iommu || !user_pfn || !phys_pfn) +return -EINVAL ; + +mutex_lock (&iommu->lock); + +if (!iommu->external_domain) { + ret = -EINVAL ; + goto pin_done; + } + +/* +* If iommu capable domain exist in the container then all pages are +* already pinned and accounted. Accouting should be done if there is no +* iommu capable domain in the container. +*/ + do_accounting = !IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu); + +for (i =0 ; i < npage; i++) { +struct vfio_pfn *p; +dma_addr_t iova; + + iova = user_pfn[i] <<PAGE_SHIFT ; +
drivers/vfio/vfio_iommu_type1.c static long __vfio_unpin_pages_remote(struct vfio_dma *dma, unsigned long pfn, · line 580
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10
-0+ dma =vfio_find_dma (iommu, iova,0 ); +if (!dma) { + ret = -EINVAL ; + goto pin_unwind; + } + + remote_vaddr = dma->vaddr + iova - dma->iova; + + ret =__vfio_pin_page_external (dma, remote_vaddr, prot, + &pfn[i], do_accounting);
Please wrap such that the following line falls within the relevant ()s when possible. Above and below on unpin calls.
drivers/vfio/vfio_iommu_type1.c static long __vfio_unpin_pages_remote(struct vfio_dma *dma, unsigned long pfn, · line 591
Quoted 11 lines
drivers/vfio/vfio_iommu_type1.c +11
-0+if (ret <=0 ) { +WARN_ON (!ret); + goto pin_unwind; + } + +mutex_lock (&dma->addr_space->pfn_list_lock); + +/* search if pfn exist */ + p =vfio_find_pfn (dma->addr_space, pfn[i]); +if (p) { +atomic_inc (&p->ref_count);
We never test whether (p->prot == prot), shouldn't we be doing something in that case? In fact, why do we allow the side-channel through the .{un}pin_pages to specify page protection flags that might be different than the user specified for the DMA_MAP? If the user specified read-only, the vendor driver should not be allowed to override with read-write access.
Quoted 456 lines
drivers/vfio/vfio_iommu_type1.c +277
-33+mutex_unlock (&dma->addr_space->pfn_list_lock); +continue ; + } + + ret =vfio_add_to_pfn_list (dma->addr_space, pfn[i], prot); +mutex_unlock (&dma->addr_space->pfn_list_lock); + +if (ret) { +__vfio_unpin_page_external (dma->addr_space, pfn[i], + prot, do_accounting); + goto pin_unwind; + } + } + + ret = i; + goto pin_done; + +pin_unwind: + pfn[i] =0 ; +for (j =0 ; j < i; j++) { +struct vfio_pfn *p; +dma_addr_t iova; + + iova = user_pfn[j] <<PAGE_SHIFT ; + + dma =vfio_find_dma (iommu, iova,0 ); + +mutex_lock (&dma->addr_space->pfn_list_lock); + p =vfio_find_pfn (dma->addr_space, pfn[j]); +if (p) +vfio_unpin_pfn (dma->addr_space, p, do_accounting); + +mutex_unlock (&dma->addr_space->pfn_list_lock); + pfn[j] =0 ; + } + +pin_done: +mutex_unlock (&iommu->lock); +return ret; +} + +static int vfio_iommu_type1_unpin_pages (void *iommu_data, +unsigned long *user_pfn, +unsigned long *pfn, +int npage) +{ +struct vfio_iommu *iommu = iommu_data; +bool do_accounting; +int unlocked =0 , i; + +if (!iommu || !user_pfn || !pfn) +return -EINVAL ; + +mutex_lock (&iommu->lock); + +if (!iommu->external_domain) { +mutex_unlock (&iommu->lock); +return -EINVAL ; + } + + do_accounting = !IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu); + +for (i =0 ; i < npage; i++) { +struct vfio_pfn *p; +struct vfio_dma *dma; +dma_addr_t iova; + + iova = user_pfn[i] <<PAGE_SHIFT ; + + dma =vfio_find_dma (iommu, iova,0 ); +if (!dma) + goto unpin_exit; + +mutex_lock (&dma->addr_space->pfn_list_lock); +/* verify if pfn exist in pfn_list */ + p =vfio_find_pfn (dma->addr_space, pfn[i]); +if (p) + unlocked +=vfio_unpin_pfn (dma->addr_space, p, + do_accounting); +mutex_unlock (&dma->addr_space->pfn_list_lock); + } +unpin_exit: +mutex_unlock (&iommu->lock); +return unlocked; +} + +static long vfio_unmap_unpin (struct vfio_iommu *iommu,struct vfio_dma *dma, +bool do_accounting) {dma_addr_t iova = dma->iova, end = dma->iova + dma->size;struct vfio_domain *domain, *d;long unlocked =0 ;if (!dma->size) -return ; +return 0 ; + +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) +return 0 ; +/* * We use the IOMMU to track the physical addresses, otherwise we'd * need a much more complicated tracking system. Unfortunately that @@ -427,12 +737,17 @@ static void vfio_unmap_unpin(struct vfio_iommu *iommu, struct vfio_dma *dma)cond_resched (); } -vfio_lock_acct (dma->addr_space->mm, -unlocked); + dma->iommu_mapped = false; +if (do_accounting) { +vfio_lock_acct (dma->addr_space->mm, -unlocked); +return 0 ; + } +return unlocked; }static void vfio_remove_dma (struct vfio_iommu *iommu,struct vfio_dma *dma) { -vfio_unmap_unpin (iommu, dma); +vfio_unmap_unpin (iommu, dma, true);vfio_unlink_dma (iommu, dma);if (atomic_dec_and_test (&dma->addr_space->ref_count)) {@@ -642,6 +957,8 @@ static int vfio_pin_map_dma(struct vfio_iommu *iommu, struct vfio_dma *dma, dma->size += npage <<PAGE_SHIFT ; } + dma->iommu_mapped = true; +if (ret)vfio_remove_dma (iommu, dma);@@ -706,6 +1023,8 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu, goto do_map_err; } addr_space->mm = mm; + addr_space->pfn_list =RB_ROOT ; +mutex_init (&addr_space->pfn_list_lock);atomic_set (&addr_space->ref_count,1 );list_add (&addr_space->next, &iommu->addr_space_list); free_addr_space_on_err = true;@@ -733,7 +1052,11 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu,/* Insert zero-sized and grow as we map chunks of it */ vfio_link_dma (iommu, dma); - ret =vfio_pin_map_dma (iommu, dma, size); +/* Don't pin and map if container doesn't contain IOMMU capable domain*/ +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) + dma->size = size; +else + ret =vfio_pin_map_dma (iommu, dma, size); do_map_err:mutex_unlock (&iommu->lock);return ret;@@ -762,10 +1085,6 @@ static int vfio_iommu_replay(struct vfio_iommu *iommu, d = list_first_entry(&iommu->domain_list, struct vfio_domain, next); n =rb_first (&iommu->dma_list); -/* If there's not a domain, there better not be any mappings */ -if (WARN_ON (n && !d)) -return -EINVAL ; -for (; n; n =rb_next (n)) {struct vfio_dma *dma;dma_addr_t iova;@@ -774,20 +1093,43 @@ static int vfio_iommu_replay(struct vfio_iommu *iommu, iova = dma->iova;while (iova < dma->iova + dma->size) { -phys_addr_t phys =iommu_iova_to_phys (d->domain, iova); +phys_addr_t phys;size_t size; -if (WARN_ON (!phys)) { - iova +=PAGE_SIZE ; -continue ; - } +if (dma->iommu_mapped) { + phys =iommu_iova_to_phys (d->domain, iova); + +if (WARN_ON (!phys)) { + iova +=PAGE_SIZE ; +continue ; + } - size =PAGE_SIZE ; + size =PAGE_SIZE ; -while (iova + size < dma->iova + dma->size && - phys + size ==iommu_iova_to_phys (d->domain, +while (iova + size < dma->iova + dma->size && + phys + size ==iommu_iova_to_phys (d->domain, iova + size)) - size +=PAGE_SIZE ; + size +=PAGE_SIZE ; + }else { +unsigned long pfn; +unsigned long vaddr = dma->vaddr + + (iova - dma->iova); +size_t n = dma->iova + dma->size - iova; +long npage; + + npage =__vfio_pin_pages_remote (dma, vaddr, + n >>PAGE_SHIFT , + dma->prot, + &pfn); +if (npage <=0 ) { +WARN_ON (!npage); + ret = (int )npage; +return ret; + } + + phys = pfn <<PAGE_SHIFT ; + size = npage <<PAGE_SHIFT ; + } ret =iommu_map (domain->domain, iova, phys, size, dma->prot | domain->prot);@@ -796,6 +1138,8 @@ static int vfio_iommu_replay(struct vfio_iommu *iommu, iova += size; } + + dma->iommu_mapped = true; }return 0 ;@@ -853,7 +1197,7 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,struct vfio_iommu *iommu = iommu_data;struct vfio_group *group;struct vfio_domain *domain, *d; -struct bus_type *bus =NULL ; +struct bus_type *bus =NULL , *mdev_bus;int ret;mutex_lock (&iommu->lock);@@ -865,6 +1209,13 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, } } +if (iommu->external_domain) { +if (find_iommu_group (iommu->external_domain, iommu_group)) { +mutex_unlock (&iommu->lock); +return -EINVAL ; + } + } + group =kzalloc (sizeof (*group),GFP_KERNEL ); domain =kzalloc (sizeof (*domain),GFP_KERNEL );if (!group || !domain) {@@ -879,6 +1230,25 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,if (ret) goto out_free; + mdev_bus =symbol_get (mdev_bus_type); + +if (mdev_bus) { +if ((bus == mdev_bus) && !iommu_present (bus)) { +symbol_put (mdev_bus_type); +if (!iommu->external_domain) { +INIT_LIST_HEAD (&domain->group_list); + iommu->external_domain = domain; + }else +kfree (domain); + +list_add (&group->next, + &iommu->external_domain->group_list); +mutex_unlock (&iommu->lock); +return 0 ; + } +symbol_put (mdev_bus_type); + } + domain->domain =iommu_domain_alloc (bus);if (!domain->domain) { ret = -EIO ;@@ -969,6 +1339,51 @@ static void vfio_iommu_unmap_unpin_all(struct vfio_iommu *iommu) vfio_remove_dma(iommu, rb_entry(node, struct vfio_dma, node)); } +static void vfio_iommu_unmap_unpin_reaccount (struct vfio_iommu *iommu) +{ +struct vfio_addr_space *as; + +list_for_each_entry (as, &iommu->addr_space_list, next) { +struct rb_node *n, *p; +long locked =0 , unlocked =0 ; + + n =rb_first (&iommu->dma_list); +for (; n; n =rb_next (n)) { +struct vfio_dma *dma; + + dma = rb_entry(n, struct vfio_dma, node); +if (dma->addr_space == as) + unlocked +=vfio_unmap_unpin (iommu, dma, false); + } + +mutex_lock (&as->pfn_list_lock); + p =rb_first (&as->pfn_list); +for (; p; p =rb_next (p)) + locked++; + +mutex_unlock (&as->pfn_list_lock); +vfio_lock_acct (as->mm, locked - unlocked); + } +} + +static void vfio_external_unpin_all (struct vfio_iommu *iommu, +bool do_accounting) +{ +struct vfio_addr_space *as; + +list_for_each_entry (as, &iommu->addr_space_list, next) { +struct rb_node *node; + +mutex_lock (&as->pfn_list_lock); +while ((node =rb_first (&as->pfn_list))) +vfio_unpin_pfn (as, + rb_entry(node, struct vfio_pfn, node), + do_accounting); + +mutex_unlock (&as->pfn_list_lock); + } +} +static void vfio_iommu_type1_detach_group (void *iommu_data,struct iommu_group *iommu_group) {@@ -978,6 +1393,28 @@ static void vfio_iommu_type1_detach_group(void *iommu_data,mutex_lock (&iommu->lock); +if (iommu->external_domain) { + domain = iommu->external_domain; + group =find_iommu_group (domain, iommu_group); +if (group) { +list_del (&group->next); +kfree (group); + +if (list_empty (&domain->group_list)) { +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { +vfio_external_unpin_all (iommu, true); +vfio_iommu_unmap_unpin_all (iommu); + }else +vfio_external_unpin_all (iommu, false); +kfree (domain); + iommu->external_domain =NULL ; + } + goto detach_group_done; + } + } + +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) + goto detach_group_done;list_for_each_entry (domain, &iommu->domain_list, next) { group =find_iommu_group (domain, iommu_group);@@ -988,21 +1425,27 @@ static void vfio_iommu_type1_detach_group(void *iommu_data,list_del (&group->next);kfree (group);/* -* Group ownership provides privilege, if the group -* list is empty, the domain goes away. If it's the -* last domain, then all the mappings go away too. +* Group ownership provides privilege, if the group list is +* empty, the domain goes away. If it's the last domain with +* iommu and external domain doesn't exist, then all the +* mappings go away too. If it's the last domain with iommu and +* external domain exist, update accounting */ if (list_empty (&domain->group_list)) { -if (list_is_singular (&iommu->domain_list)) -vfio_iommu_unmap_unpin_all (iommu); +if (list_is_singular (&iommu->domain_list)) { +if (!iommu->external_domain) +vfio_iommu_unmap_unpin_all (iommu); +else +vfio_iommu_unmap_unpin_reaccount (iommu); + }iommu_domain_free (domain->domain);list_del (&domain->next);kfree (domain); } - goto done; +break ; } -done: +detach_group_done:mutex_unlock (&iommu->lock); }@@ -1028,29 +1471,46 @@ static void *vfio_iommu_type1_open(unsigned long arg) }INIT_LIST_HEAD (&iommu->domain_list); +INIT_LIST_HEAD (&iommu->addr_space_list); iommu->dma_list =RB_ROOT ;mutex_init (&iommu->lock);return iommu; } +static void vfio_release_domain (struct vfio_domain *domain,bool external) +{ +struct vfio_group *group, *group_tmp; + +list_for_each_entry_safe (group, group_tmp, + &domain->group_list, next) { +if (!external) +iommu_detach_group (domain->domain, group->iommu_group); +list_del (&group->next); +kfree (group); + } + +if (!external) +iommu_domain_free (domain->domain); +} +static void vfio_iommu_type1_release (void *iommu_data) {struct vfio_iommu *iommu = iommu_data;struct vfio_domain *domain, *domain_tmp; -struct vfio_group *group, *group_tmp; + +if (iommu->external_domain) { +vfio_release_domain (iommu->external_domain, true); +vfio_external_unpin_all (iommu, false); +kfree (iommu->external_domain); + iommu->external_domain =NULL ; + }vfio_iommu_unmap_unpin_all (iommu);list_for_each_entry_safe (domain, domain_tmp, &iommu->domain_list, next) { -list_for_each_entry_safe (group, group_tmp, - &domain->group_list, next) { -iommu_detach_group (domain->domain, group->iommu_group); -list_del (&group->next); -kfree (group); - } -iommu_domain_free (domain->domain); +vfio_release_domain (domain, false);list_del (&domain->next);kfree (domain); }@@ -1158,6 +1618,8 @@ static const struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1 = { .ioctl = vfio_iommu_type1_ioctl, .attach_group = vfio_iommu_type1_attach_group, .detach_group = vfio_iommu_type1_detach_group, + .pin_pages = vfio_iommu_type1_pin_pages, + .unpin_pages = vfio_iommu_type1_unpin_pages, };static int __init vfio_iommu_type1_init (void )
Re: [PATCH v11 11/22] vfio iommu: Add blocking notifier to notify DMA_UNMAP
In reply to Kirti WankhedeQuoted 22 lines · On Sat, 5 Nov 2016 02:40:45 +0530 Kirti Wankhede <[email protected]> wrote:
Added blocking notifier to IOMMU TYPE1 driver to notify vendor drivers about
DMA_UNMAP.
Exported two APIsvfio_register_notifier()andvfio_unregister_notifier(). Notifier should be registered, if external user wants to use vfio_pin_pages()/vfio_unpin_pages() APIs to pin/unpin pages. Vendor driver should useVFIO_IOMMU_NOTIFY_DMA_UNMAPaction to invalidate mappings.Signed-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id I5910d0024d6be87f3e8d3e0ca0eaeaaa0b17f271--- drivers/vfio/vfio.c | 73 +++++++++++++++++++++++++++++++++++++++++ drivers/vfio/vfio_iommu_type1.c | 47 ++++++++++++++++++++------ include/linux/vfio.h | 11 +++++++ 3 files changed, 121 insertions(+), 10 deletions(-)drivers/vfio/vfio.c +0
-0
drivers/vfio/vfio.c err_unpin_pages: · line 1898
Quoted 4 lines
drivers/vfio/vfio.c +1
-0@@ -1895,6 +1895,79 @@ err_unpin_pages: }EXPORT_SYMBOL (vfio_unpin_pages); +int vfio_register_notifier (struct device *dev,struct notifier_block *nb)
Is the expectation here that this is a generic notifier for all vfio->mdev signaling? That should probably be made clear in the mdev API to avoid vendor drivers assuming their notifier callback only occurs for unmaps, even if that's currently the case.
Quoted 15 lines
drivers/vfio/vfio.c +15
-0+{ +struct vfio_container *container; +struct vfio_group *group; +struct vfio_iommu_driver *driver; +ssize_t ret; + +if (!dev || !nb) +return -EINVAL ; + + group =vfio_group_get_from_dev (dev); +if (IS_ERR (group)) +return PTR_ERR (group); + + ret =vfio_group_add_container_user (group); +if (ret)
drivers/vfio/vfio.c err_unpin_pages: · line 1923
Quoted 10 lines
drivers/vfio/vfio.c +10
-0+ goto err_register_nb; + + container = group->container; +down_read (&container->group_lock); + + driver = container->iommu_driver; +if (likely (driver && driver->ops->register_notifier)) + ret = driver->ops->register_notifier (container->iommu_data, nb); +else + ret = -EINVAL ;
-ENOTTY again? And below.
Quoted 30 lines
drivers/vfio/vfio.c +30
-0+ +up_read (&container->group_lock); +vfio_group_try_dissolve_container (group); + +err_register_nb: +vfio_group_put (group); +return ret; +} +EXPORT_SYMBOL (vfio_register_notifier); + +int vfio_unregister_notifier (struct device *dev,struct notifier_block *nb) +{ +struct vfio_container *container; +struct vfio_group *group; +struct vfio_iommu_driver *driver; +ssize_t ret; + +if (!dev || !nb) +return -EINVAL ; + + group =vfio_group_get_from_dev (dev); +if (IS_ERR (group)) +return PTR_ERR (group); + + ret =vfio_group_add_container_user (group); +if (ret) + goto err_unregister_nb; + + container = group->container; +down_read (&container->group_lock);
drivers/vfio/vfio.c err_unpin_pages: · line 1963
Quoted 10 lines
drivers/vfio/vfio.c +10
-0+ + driver = container->iommu_driver; +if (likely (driver && driver->ops->unregister_notifier)) + ret = driver->ops->unregister_notifier (container->iommu_data, + nb); +else + ret = -EINVAL ; + +up_read (&container->group_lock); +vfio_group_try_dissolve_container (group);
The concern any time we have an unregister like this is whether the vendor driver does proper cleanup. Maybe we don't even need an unregister, could we track this on the group such that releasing the group automatically unregisters the notifier? Maybe a single nb slot and -EBUSY if already set, cleared on release? Along those lines, automatically unpinning anything would also be a nice feature (ie. if an mdev device is unplugged while other devices are still in the container), but then we'd need to track pinning per group and we already have too much overhead in tracking pinning.
Quoted 41 lines
drivers/vfio/vfio.c +7
-0+ +err_unregister_nb: +vfio_group_put (group); +return ret; +} +EXPORT_SYMBOL (vfio_unregister_notifier); +/** * Module/class support */ drivers/vfio/vfio_iommu_type1.c +4
-1@@ -37,6 +37,7 @@#include <linux/vfio.h> #include <linux/workqueue.h> #include <linux/mdev.h> +#include <linux/notifier.h> #define DRIVER_VERSION "0.2"#define DRIVER_AUTHOR "Alex Williamson <[email protected]>"@@ -60,6 +61,7 @@ struct vfio_iommu {struct vfio_domain *external_domain;/* domain for external user */ struct mutex lock;struct rb_root dma_list; +struct blocking_notifier_head notifier;bool v2;bool nesting; };@@ -550,7 +552,8 @@ static int vfio_iommu_type1_pin_pages(void *iommu_data,mutex_lock (&iommu->lock); -if (!iommu->external_domain) { +/* Fail if notifier list is empty */ +if ((!iommu->external_domain) || (!iommu->notifier.head)) { ret = -EINVAL ; goto pin_done; }
drivers/vfio/vfio_iommu_type1.c unlock: · line 876
Quoted 7 lines
drivers/vfio/vfio_iommu_type1.c +4
-0@@ -867,6 +870,11 @@ unlock:/* Report how much was unmapped */ unmap->size = unmapped; +if (unmapped && iommu->external_domain) +blocking_notifier_call_chain (&iommu->notifier, +VFIO_IOMMU_NOTIFY_DMA_UNMAP , + unmap);
This is after the fact, there's already a gap here where pages are unpinned and the mdev device is still running. The notifier needs to happen prior to that and I suspect that we need to validate that we have no remaining external pfn references within this vfio_dma block. It seems like we need to root our pfn tracking in the vfio_dma so that we can see that it's empty after the notifier chain and BUG_ON if not. I would also add some enforcement that external pinning is only enabled when vfio_iommu_type1 is configured for v2 semantics (ie. we only support unmaps exactly matching previous maps).
Quoted 85 lines
drivers/vfio/vfio_iommu_type1.c +29
-9+return ret; }@@ -1474,6 +1482,7 @@ static void *vfio_iommu_type1_open(unsigned long arg)INIT_LIST_HEAD (&iommu->addr_space_list); iommu->dma_list =RB_ROOT ;mutex_init (&iommu->lock); +BLOCKING_INIT_NOTIFIER_HEAD (&iommu->notifier);return iommu; }@@ -1610,16 +1619,34 @@ static long vfio_iommu_type1_ioctl(void *iommu_data,return -ENOTTY ; } +static int vfio_iommu_type1_register_notifier (void *iommu_data, +struct notifier_block *nb) +{ +struct vfio_iommu *iommu = iommu_data; + +return blocking_notifier_chain_register (&iommu->notifier, nb); +} + +static int vfio_iommu_type1_unregister_notifier (void *iommu_data, +struct notifier_block *nb) +{ +struct vfio_iommu *iommu = iommu_data; + +return blocking_notifier_chain_unregister (&iommu->notifier, nb); +} +static const struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1 = { - .name ="vfio-iommu-type1" , - .owner =THIS_MODULE , - .open = vfio_iommu_type1_open, - .release = vfio_iommu_type1_release, - .ioctl = vfio_iommu_type1_ioctl, - .attach_group = vfio_iommu_type1_attach_group, - .detach_group = vfio_iommu_type1_detach_group, - .pin_pages = vfio_iommu_type1_pin_pages, - .unpin_pages = vfio_iommu_type1_unpin_pages, + .name ="vfio-iommu-type1" , + .owner =THIS_MODULE , + .open = vfio_iommu_type1_open, + .release = vfio_iommu_type1_release, + .ioctl = vfio_iommu_type1_ioctl, + .attach_group = vfio_iommu_type1_attach_group, + .detach_group = vfio_iommu_type1_detach_group, + .pin_pages = vfio_iommu_type1_pin_pages, + .unpin_pages = vfio_iommu_type1_unpin_pages, + .register_notifier = vfio_iommu_type1_register_notifier, + .unregister_notifier = vfio_iommu_type1_unregister_notifier, };static int __init vfio_iommu_type1_init (void )include/linux/vfio.h +11
-0@@ -82,6 +82,10 @@ struct vfio_iommu_driver_ops {unsigned long *user_pfn, unsigned long *pfn, int npage); +int (*register_notifier)(void *iommu_data, +struct notifier_block *nb); +int (*unregister_notifier)(void *iommu_data, +struct notifier_block *nb); };extern int vfio_register_iommu_driver (const struct vfio_iommu_driver_ops *ops);@@ -139,6 +143,13 @@ extern int vfio_pin_pages(struct device *dev, unsigned long *user_pfn,extern int vfio_unpin_pages (struct device *dev,unsigned long *user_pfn,unsigned long *pfn,int npage); +#define VFIO_IOMMU_NOTIFY_DMA_UNMAP 1 + +extern int vfio_register_notifier (struct device *dev, +struct notifier_block *nb); + +extern int vfio_unregister_notifier (struct device *dev, +struct notifier_block *nb);/* * IRQfd - generic */
Re: [PATCH v11 12/22] vfio: Add notifier callback to parent's ops structure of mdev
In reply to Kirti WankhedeQuoted 44 lines · On Sat, 5 Nov 2016 02:40:46 +0530 Kirti Wankhede <[email protected]> wrote:
Add a notifier calback to parent's ops structure of mdev device so that per device notifer for vfio module is registered through
vfio_mdevmodule.Signed-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id Iafa6f1721aecdd6e50eb93b153b5621e6d29b637--- drivers/vfio/mdev/vfio_mdev.c | 19 +++++++++++++++++++ include/linux/mdev.h | 9 +++++++++ 2 files changed, 28 insertions(+)drivers/vfio/mdev/vfio_mdev.c +14
-0@@ -24,6 +24,15 @@#define DRIVER_AUTHOR "NVIDIA Corporation"#define DRIVER_DESC "VFIO based driver for Mediated device" +static int vfio_mdev_notifier (struct notifier_block *nb,unsigned long action, +void *data) +{ + struct mdev_device *mdev = container_of(nb, struct mdev_device, nb); +struct parent_device *parent = mdev->parent; + +return parent->ops->notifier (mdev, action, data); +} +static int vfio_mdev_open (void *device_data) {struct mdev_device *mdev = device_data;@@ -40,6 +49,11 @@ static int vfio_mdev_open(void *device_data)if (ret)module_put (THIS_MODULE ); +if (likely (parent->ops->notifier)) { + mdev->nb.notifier_call = vfio_mdev_notifier; +if (vfio_register_notifier (&mdev->dev, &mdev->nb)) +pr_err ("Failed to register notifier for mdev\n" ); + }return ret; }
drivers/vfio/mdev/vfio_mdev.c static void vfio_mdev_release(void *device_data) · line 69
Quoted 8 lines
drivers/vfio/mdev/vfio_mdev.c +5
-0@@ -48,6 +62,11 @@ static void vfio_mdev_release(void *device_data)struct mdev_device *mdev = device_data;struct parent_device *parent = mdev->parent; +if (likely (parent->ops->notifier)) { +if (vfio_unregister_notifier (&mdev->dev, &mdev->nb)) +pr_err ("Failed to unregister notifier for mdev\n" ); + } +
Ok, I guess this is sufficient to automatically handle the unregister at the mdev layer. No need for my comments on the previous other than the ordering of when the callback is called. Thanks,
Alex
Quoted 37 lines
drivers/vfio/mdev/vfio_mdev.c +0
-0if (likely (parent->ops->release)) parent->ops->release (mdev);include/linux/mdev.h +9
-0@@ -37,6 +37,7 @@ struct mdev_device {struct kref ref;struct list_head next;struct kobject *type_kobj; +struct notifier_block nb; };@@ -84,6 +85,12 @@ struct mdev_device {* @cmd: mediated device structure * @arg: mediated device structure * @mmap: mmap callback +* @mdev: mediated device structure +* @vma: vma structure +* @notifer: Notifier callback +* @mdev: mediated device structure +* @action: Action for which notifier is called +* @data: Data associated with the notifier * Parent device that support mediated device should be registered with mdev * module with parent_ops structure. **/ @@ -105,6 +112,8 @@ struct parent_ops {ssize_t (*ioctl)(struct mdev_device *mdev,unsigned int cmd,unsigned long arg);int (*mmap)(struct mdev_device *mdev,struct vm_area_struct *vma); +int (*notifier)(struct mdev_device *mdev,unsigned long action, +void *data); };/* interface for exporting mdev supported type attributes */
Re: [PATCH v11 10/22] vfio iommu type1: Add support for mediated devices
In reply to Alex WilliamsonQuoted 286 lines · Alex Williamson
Quoted 8 lines · On Sat, 5 Nov 2016 02:40:44 +0530 Kirti Wankhede <[email protected]> wrote:
VFIO IOMMU drivers are designed for the devices which are IOMMU capable. Mediated device only uses IOMMU APIs, the underlying hardware can be managed by an IOMMU domain.
Aim of this change is:
- To use most of the code of TYPE1 IOMMU driver for mediated devices
- To support direct assigned device and mediated device in single moduleQuoted 6 lines
This change adds pin and unpin support for mediated device to TYPE1 IOMMU backend module. More details:
-vfio_pin_pages()callback here uses task and address space ofvfio_dma, that is, of the process who mapped that iova range.
- Addedpfn_listtracking logic to address space structure. All pages pinned through this interface are trached in its address space.^ k------------------------------------------|
Quoted 255 lines
- Pinned pages list is used to verify unpinning request and to unpin remaining pages while detaching the group for that device.
- Page accounting is updated to account in its address space where the pages are pinned/unpinned.
- Accouting for mdev device is only done if there is no iommu capable domain in the container. When there is a direct device assigned to the container and that domain is iommu capable, all pages are already pinned duringDMA_MAP.
- Page accouting is updated on hot plug and unplug mdev device and pass through device.Tested by assigning below combinations of devices to a single VM:
- GPU pass through only
- vGPU device only
- One GPU pass through and one vGPU device
- Linux VM hot plug and unplug vGPU device while GPU pass through device exist
- Linux VM hot plug and unplug GPU pass through device while vGPU device existSigned-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id I295d6f0f2e0579b8d9882bfd8fd5a4194b97bd9a--- drivers/vfio/vfio_iommu_type1.c | 538 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 500 insertions(+), 38 deletions(-)drivers/vfio/vfio_iommu_type1.c +143
-5@@ -36,6 +36,7 @@#include <linux/uaccess.h> #include <linux/vfio.h> #include <linux/workqueue.h> +#include <linux/mdev.h> #define DRIVER_VERSION "0.2"#define DRIVER_AUTHOR "Alex Williamson <[email protected]>"@@ -56,6 +57,7 @@ MODULE_PARM_DESC(disable_hugepages,struct vfio_iommu {struct list_head domain_list;struct list_head addr_space_list; +struct vfio_domain *external_domain;/* domain for external user */ struct mutex lock;struct rb_root dma_list;bool v2;@@ -67,6 +69,9 @@ struct vfio_addr_space {struct mm_struct *mm;struct list_head next;atomic_t ref_count; +/* external user pinned pfns */ +struct rb_root pfn_list;/* pinned Host pfn list */ +struct mutex pfn_list_lock;/* mutex for pfn_list */ };struct vfio_domain {@@ -83,6 +88,7 @@ struct vfio_dma {unsigned long vaddr;/* Process virtual addr */ size_t size;/* Map size (bytes) */ int prot;/* IOMMU_READ/WRITE */ +bool iommu_mapped;struct vfio_addr_space *addr_space;struct task_struct *task;bool mlock_cap;@@ -94,6 +100,19 @@ struct vfio_group { };/* +* Guest RAM pinning working set or DMA target +*/ +struct vfio_pfn { +struct rb_node node; +unsigned long pfn;/* Host pfn */ +int prot; +atomic_t ref_count; +}; + +#define IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu) \ + (!list_empty(&iommu->domain_list)) + +/* * This code handles mapping and unmapping of user data buffers * into DMA'ble space using the IOMMU */ @@ -153,6 +172,93 @@ static struct vfio_addr_space *vfio_find_addr_space(struct vfio_iommu *iommu,return NULL ; } +/* +* Helper Functions for host pfn list +*/ +static struct vfio_pfn *vfio_find_pfn (struct vfio_addr_space *addr_space, +unsigned long pfn) +{ +struct vfio_pfn *vpfn; +struct rb_node *node = addr_space->pfn_list.rb_node; + +while (node) { + vpfn = rb_entry(node, struct vfio_pfn, node); + +if (pfn < vpfn->pfn) + node = node->rb_left; +else if (pfn > vpfn->pfn) + node = node->rb_right; +else +return vpfn; + } + +return NULL ; +} + +static void vfio_link_pfn (struct vfio_addr_space *addr_space, +struct vfio_pfn *new) +{ +struct rb_node **link, *parent =NULL ; +struct vfio_pfn *vpfn; + + link = &addr_space->pfn_list.rb_node; +while (*link) { + parent = *link; + vpfn = rb_entry(parent, struct vfio_pfn, node); + +if (new->pfn < vpfn->pfn) + link = &(*link)->rb_left; +else + link = &(*link)->rb_right; + } + +rb_link_node (&new->node, parent, link); +rb_insert_color (&new->node, &addr_space->pfn_list); +} + +static void vfio_unlink_pfn (struct vfio_addr_space *addr_space, +struct vfio_pfn *old) +{ +rb_erase (&old->node, &addr_space->pfn_list); +} + +static int vfio_add_to_pfn_list (struct vfio_addr_space *addr_space, +unsigned long pfn,int prot) +{ +struct vfio_pfn *vpfn; + + vpfn =kzalloc (sizeof (*vpfn),GFP_KERNEL ); +if (!vpfn) +return -ENOMEM ; + + vpfn->pfn = pfn; + vpfn->prot = prot; +atomic_set (&vpfn->ref_count,1 ); +vfio_link_pfn (addr_space, vpfn); +return 0 ; +} + +static void vfio_remove_from_pfn_list (struct vfio_addr_space *addr_space, +struct vfio_pfn *vpfn) +{ +vfio_unlink_pfn (addr_space, vpfn); +kfree (vpfn); +} + +static int vfio_pfn_account (struct vfio_addr_space *addr_space, +unsigned long pfn) +{ +struct vfio_pfn *p; +int ret =1 ; + +mutex_lock (&addr_space->pfn_list_lock); + p =vfio_find_pfn (addr_space, pfn); +if (p) + ret =0 ; +mutex_unlock (&addr_space->pfn_list_lock); +return ret; +} +struct vwork {struct mm_struct *mm;long npage;@@ -304,16 +410,18 @@ static long __vfio_pin_pages_remote(struct vfio_dma *dma, unsigned long vaddr,unsigned long limit =task_rlimit (task,RLIMIT_MEMLOCK ) >>PAGE_SHIFT ;bool lock_cap = dma->mlock_cap;struct mm_struct *mm = dma->addr_space->mm; -long ret, i; +long ret, i, lock_acct;bool rsvd; ret =vaddr_get_pfn (mm, vaddr, prot, pfn_base);if (ret)return ret; + lock_acct =vfio_pfn_account (dma->addr_space, *pfn_base); + rsvd =is_invalid_reserved_pfn (*pfn_base); -if (!rsvd && !lock_cap && mm->locked_vm +1 > limit) { +if (!rsvd && !lock_cap && mm->locked_vm + lock_acct > limit) {put_pfn (*pfn_base, prot);pr_warn ("%s: RLIMIT_MEMLOCK (%ld) exceeded\n" , __func__, limit <<PAGE_SHIFT );@@ -340,8 +448,10 @@ static long __vfio_pin_pages_remote(struct vfio_dma *dma, unsigned long vaddr,break ; } + lock_acct +=vfio_pfn_account (dma->addr_space, pfn); +if (!rsvd && !lock_cap && - mm->locked_vm + i +1 > limit) { + mm->locked_vm + lock_acct +1 > limit) {put_pfn (pfn, prot);pr_warn ("%s: RLIMIT_MEMLOCK (%ld) exceeded\n" , __func__, limit <<PAGE_SHIFT );@@ -350,7 +460,7 @@ static long __vfio_pin_pages_remote(struct vfio_dma *dma, unsigned long vaddr, }if (!rsvd) -vfio_lock_acct (mm, i); +vfio_lock_acct (mm, lock_acct);return i; }@@ -370,14 +480,214 @@ static long __vfio_unpin_pages_remote(struct vfio_dma *dma, unsigned long pfn,return unlocked; } -static void vfio_unmap_unpin (struct vfio_iommu *iommu,struct vfio_dma *dma) +static int __vfio_pin_page_external (struct vfio_dma *dma,unsigned long vaddr, +int prot,unsigned long *pfn_base, +bool do_accounting) +{ +struct task_struct *task = dma->task; +unsigned long limit =task_rlimit (task,RLIMIT_MEMLOCK ) >>PAGE_SHIFT ; +bool lock_cap = dma->mlock_cap; +struct mm_struct *mm = dma->addr_space->mm; +int ret; +bool rsvd; + + ret =vaddr_get_pfn (mm, vaddr, prot, pfn_base); +if (ret) +return ret; + + rsvd =is_invalid_reserved_pfn (*pfn_base); + +if (!rsvd && !lock_cap && mm->locked_vm +1 > limit) { +put_pfn (*pfn_base, prot); +pr_warn ("%s: Task %s (%d) RLIMIT_MEMLOCK (%ld) exceeded\n" , + __func__, task->comm,task_pid_nr (task), + limit <<PAGE_SHIFT ); +return -ENOMEM ; + } + +if (!rsvd && do_accounting) +vfio_lock_acct (mm,1 ); + +return 1 ;drivers/vfio/vfio_iommu_type1.c static long __vfio_unpin_pages_remote(struct vfio_dma *dma, unsigned long pfn, · line 521
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10
-0+} + +static void __vfio_unpin_page_external (struct vfio_addr_space *addr_space, +unsigned long pfn,int prot, +bool do_accounting) +{ +put_pfn (pfn, prot); + +if (do_accounting) +vfio_lock_acct (addr_space->mm,-1 );
Quoted 8 lines
Can't we batch this like we do elsewhere? Intel folks, AIUI you intend to pin all VM memory through this side channel, have you tested the scalability and performance of this with larger VMs? Our
vfio_pfndata structure alone is 40 bytes per pinned page, which means for each 1GB of VM memory, we have 10MBs worth of structvfio_pfn! Additionally, unmapping each 1GB of VM memory will result in 256k separatevfio_lock_acct()callbacks. I'm concerned that we're not being efficient enough in either space or time.
Hi Alex,
Sorry for being confusing, Intel vGPU actually doesn't necessarily need to pin all guest memory. A vGPU has its page table (GTT), whose access is trapped. Whenever guest driver wants to specify a page for DMA, it writes the GTT entry - thereby we could know the event and pin that page only.
Performance data will be shared once available. Thanks :)
--
Thanks,
Jike
Re: [PATCH v11 10/22] vfio iommu type1: Add support for mediated devices
In reply to Kirti WankhedeQuoted 25 lines · Kirti Wankhede
VFIO IOMMU drivers are designed for the devices which are IOMMU capable. Mediated device only uses IOMMU APIs, the underlying hardware can be managed by an IOMMU domain.
Aim of this change is:
- To use most of the code of TYPE1 IOMMU driver for mediated devices
- To support direct assigned device and mediated device in single moduleThis change adds pin and unpin support for mediated device to TYPE1 IOMMU backend module. More details:
-vfio_pin_pages()callback here uses task and address space ofvfio_dma, that is, of the process who mapped that iova range.
- Addedpfn_listtracking logic to address space structure. All pages pinned through this interface are trached in its address space.
- Pinned pages list is used to verify unpinning request and to unpin remaining pages while detaching the group for that device.
- Page accounting is updated to account in its address space where the pages are pinned/unpinned.
- Accouting for mdev device is only done if there is no iommu capable domain in the container. When there is a direct device assigned to the container and that domain is iommu capable, all pages are already pinned duringDMA_MAP.
- Page accouting is updated on hot plug and unplug mdev device and pass through device.
Quoted 2 lines
Tested by assigning below combinations of devices to a single VM:
- GPU pass through only
This does not require this patchset, right?
Quoted 1 line
- vGPU device only
Out of curiosity - how exactly did you test this? The exact GPU, how to create vGPU, what was the QEMU command line and the guest does with this passed device? Thanks.
Signature · 1 line
Alexey
Re: [PATCH v11 13/22] vfio: Introduce common function to add capabilities
In reply to Kirti WankhedeQuoted 17 lines · Kirti Wankhede
Vendor driver using mediated device framework should use
vfio_info_add_capability()to add capabilities.
Introduced this function to reduce code duplication in vendor drivers.Signed-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id I6fca329fa2291f37a2c859d0bc97574d9e2ce1a6--- drivers/vfio/vfio.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++- include/linux/vfio.h | 3 +++ 2 files changed, 62 insertions(+), 1 deletion(-)drivers/vfio/vfio.c +0
-0
drivers/vfio/vfio.c void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset) · line 1800
Quoted 5 lines
drivers/vfio/vfio.c +1
-1@@ -1797,8 +1797,66 @@ void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset)for (tmp = caps->buf; tmp->next; tmp = (void *)tmp + tmp->next - offset) tmp->next += offset; } -EXPORT_SYMBOL_GPL (vfio_info_cap_shift); +EXPORT_SYMBOL (vfio_info_cap_shift);
Why this change?
Quoted 67 lines
drivers/vfio/vfio.c +58
-0+static int sparse_mmap_cap (struct vfio_info_cap *caps,void *cap_type) +{ +struct vfio_info_cap_header *header; +struct vfio_region_info_cap_sparse_mmap *sparse_cap, *sparse = cap_type; +size_t size; + + size =sizeof (*sparse) + 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_cap =container_of (header, + struct vfio_region_info_cap_sparse_mmap, header); + sparse_cap->nr_areas = sparse->nr_areas; +memcpy (sparse_cap->areas, sparse->areas, + sparse->nr_areas *sizeof (*sparse->areas)); +return 0 ; +} + +static int region_type_cap (struct vfio_info_cap *caps,void *cap_type) +{ +struct vfio_info_cap_header *header; +struct vfio_region_info_cap_type *type_cap, *cap = cap_type; + + header =vfio_info_cap_add (caps,sizeof (*cap), +VFIO_REGION_INFO_CAP_TYPE ,1 ); +if (IS_ERR (header)) +return PTR_ERR (header); + + type_cap = container_of(header, struct vfio_region_info_cap_type, + header); + type_cap->type = cap->type; + type_cap->subtype = cap->subtype; +return 0 ; +} + +int vfio_info_add_capability (struct vfio_info_cap *caps,int cap_type_id, +void *cap_type) +{ +int ret = -EINVAL ; + +if (!cap_type) +return 0 ; + +switch (cap_type_id) { +case VFIO_REGION_INFO_CAP_SPARSE_MMAP : + ret =sparse_mmap_cap (caps, cap_type); +break ; + +case VFIO_REGION_INFO_CAP_TYPE : + ret =region_type_cap (caps, cap_type); +break ; + } + +return ret; +} +EXPORT_SYMBOL (vfio_info_add_capability);/* * Pin a set of guest PFNs and return their associated host PFNs for local include/linux/vfio.h +0
-0
include/linux/vfio.h extern struct vfio_info_cap_header *vfio_info_cap_add( · line 118
Quoted 6 lines
include/linux/vfio.h +3
-0@@ -113,6 +113,9 @@ extern struct vfio_info_cap_header *vfio_info_cap_add( struct vfio_info_cap *caps, size_t size, u16 id, u16 version);extern void vfio_info_cap_shift (struct vfio_info_cap *caps,size_t offset); +extern int vfio_info_add_capability (struct vfio_info_cap *caps, +int cap_type_id,void *cap_type); +
It would make it easier to review and bisect if 14/22 was squashed into this one. In the resulting patch, vfio_info_cap_add() can be made static as it will only be used in drivers/vfio/vfio.c from now.
Quoted 4 lines
include/linux/vfio.h +0
-0struct pci_dev ;#ifdef CONFIG_EEH extern void vfio_spapr_pci_eeh_open (struct pci_dev *pdev);
Signature · 1 line
Alexey
Re: [PATCH v11 15/22] vfio: Introduce vfio_set_irqs_validate_and_prepare()
In reply to Kirti WankhedeQuoted 25 lines · Kirti Wankhede
Vendor driver using mediated device framework would use same mechnism to validate and prepare IRQs. Introducing this function to reduce code replication in multiple drivers.
Signed-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id Ie201f269dda0713ca18a07dc4852500bd8b48309--- drivers/vfio/vfio.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/vfio.h | 4 ++++ 2 files changed, 52 insertions(+)drivers/vfio/vfio.c +5
-0@@ -1858,6 +1858,54 @@ int vfio_info_add_capability(struct vfio_info_cap *caps, int cap_type_id, }EXPORT_SYMBOL (vfio_info_add_capability); +int vfio_set_irqs_validate_and_prepare (struct vfio_irq_set *hdr,int num_irqs, +int max_irq_type,size_t *data_size) +{ +unsigned long minsz; +size_t size;
drivers/vfio/vfio.c int vfio_info_add_capability(struct vfio_info_cap *caps, int cap_type_id, · line 1875
Quoted 10 lines
drivers/vfio/vfio.c +10
-0+ + minsz = offsetofend(struct vfio_irq_set, count); + +if ((hdr->argsz < minsz) || (hdr->index >= max_irq_type) || + (hdr->count >= (U32_MAX - hdr->start)) || + (hdr->flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK | +VFIO_IRQ_SET_ACTION_TYPE_MASK ))) +return -EINVAL ; + +if (data_size)
Pointless check, the callers will pass non null pointer with value initialized to 0 anyway.
Quoted 10 lines
drivers/vfio/vfio.c +10
-0+ *data_size =0 ; + +if (hdr->start >= num_irqs || hdr->start + hdr->count > num_irqs) +return -EINVAL ; + +switch (hdr->flags &VFIO_IRQ_SET_DATA_TYPE_MASK ) { +case VFIO_IRQ_SET_DATA_NONE : + size =0 ; +break ; +case VFIO_IRQ_SET_DATA_BOOL :
drivers/vfio/vfio.c int vfio_info_add_capability(struct vfio_info_cap *caps, int cap_type_id, · line 1895
Quoted 10 lines
drivers/vfio/vfio.c +10
-0+ size =sizeof (uint8_t ); +break ; +case VFIO_IRQ_SET_DATA_EVENTFD : + size =sizeof (int32_t ); +break ; +default : +return -EINVAL ; + } + +if (size) {
The whole branch would even work for size == 0.
drivers/vfio/vfio.c int vfio_info_add_capability(struct vfio_info_cap *caps, int cap_type_id, · line 1900
Quoted 5 lines
drivers/vfio/vfio.c +5
-0+if (hdr->argsz - minsz < hdr->count * size) +return -EINVAL ; + +if (!data_size) +return -EINVAL ;
Redundant check as well.
drivers/vfio/vfio.c int vfio_info_add_capability(struct vfio_info_cap *caps, int cap_type_id, · line 1906
Quoted 6 lines
drivers/vfio/vfio.c +6
-0+ + *data_size = hdr->count * size; + } + +return 0 ; +}
It does not really prepare anything as the name suggests. It looks like this is 2 different helpers actually:
int vfio_set_irqs_validate()
andsize_t vfio_set_irqs_hdr_to_data_size()
And it would make it easier to review/bisect if 16/22 and 17/22 were merged into this one as this patch alone adds new code which it does not use and all 3 patches are fairly small.
drivers/vfio/vfio.c int vfio_info_add_capability(struct vfio_info_cap *caps, int cap_type_id, · line 1907
Quoted 1 line
drivers/vfio/vfio.c +1
-0+EXPORT_SYMBOL (vfio_set_irqs_validate_and_prepare);
Everything you export in this patchset is EXPORT_SYMBOL() while the existing code uses EXPORT_SYMBOL_GPL(), is this for a reason?
Quoted 20 lines
drivers/vfio/vfio.c +1
-0+/* * Pin a set of guest PFNs and return their associated host PFNs for local * domain only. include/linux/vfio.h +4
-0@@ -116,6 +116,10 @@ extern void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);extern int vfio_info_add_capability (struct vfio_info_cap *caps,int cap_type_id,void *cap_type); +extern int vfio_set_irqs_validate_and_prepare (struct vfio_irq_set *hdr, +int num_irqs,int max_irq_type, +size_t *data_size); +struct pci_dev ;#ifdef CONFIG_EEH extern void vfio_spapr_pci_eeh_open (struct pci_dev *pdev);
Signature · 1 line
Alexey
Re: [PATCH v11 17/22] vfio_platform: Updated to use vfio_set_irqs_validate_and_prepare()
In reply to Kirti WankhedeQuoted 39 lines · Kirti Wankhede
Updated
vfio_platform_common.cfile to usevfio_set_irqs_validate_and_prepare()Signed-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id Id87cd6b78ae901610b39bf957974baa6f40cd7b0--- drivers/vfio/platform/vfio_platform_common.c | 31 +++++++--------------------- 1 file changed, 8 insertions(+), 23 deletions(-)drivers/vfio/platform/vfio_platform_common.c +1
-14@@ -364,36 +364,21 @@ static long vfio_platform_ioctl(void *device_data,struct vfio_irq_set hdr;u8 *data =NULL ;int ret =0 ; +size_t data_size =0 ; minsz = offsetofend(struct vfio_irq_set, count);if (copy_from_user (&hdr, (void __user *)arg, minsz))return -EFAULT ; -if (hdr.argsz < minsz) -return -EINVAL ; - -if (hdr.index >= vdev->num_irqs) -return -EINVAL ; - -if (hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK | -VFIO_IRQ_SET_ACTION_TYPE_MASK )) -return -EINVAL ; - -if (!(hdr.flags &VFIO_IRQ_SET_DATA_NONE )) { -size_t size; - -if (hdr.flags &VFIO_IRQ_SET_DATA_BOOL )
drivers/vfio/platform/vfio_platform_common.c static long vfio_platform_ioctl(void *device_data, · line 375
Quoted 10 lines
drivers/vfio/platform/vfio_platform_common.c +2
-8- size =sizeof (uint8_t ); - else if (hdr.flags & VFIO_IRQ_SET_DATA_EVENTFD) - size =sizeof (int32_t ); - else - return -EINVAL; - -if (hdr.argsz - minsz < size) -return -EINVAL ; + ret =vfio_set_irqs_validate_and_prepare (&hdr, vdev->num_irqs, + vdev->num_irqs, &data_size);
The patch does not change this but I am still curious:
is not the second vdev->num_irqs supposed to be one of VFIO_PCI_INTX_IRQ_INDEX..VFIO_PCI_NUM_IRQS, not the actual number of interrupt vectors (as in vfio-pci)?
Quoted 11 lines
drivers/vfio/platform/vfio_platform_common.c +5
-1+if (ret) +return ret; - data =memdup_user ((void __user *)(arg + minsz), size); +if (data_size) { + data =memdup_user ((void __user *)(arg + minsz), + data_size);if (IS_ERR (data))return PTR_ERR (data); }
Signature · 1 line
Alexey
Re: [PATCH v11 01/22] vfio: Mediated device Core driver
In reply to Kirti Wankhede* Kirti Wankhede <[email protected]> [2016-11-05 02:40:35 +0530]:
Hi Kirti,
[...]
Quoted 25 lines
drivers/vfio/Kconfig +1
-0@@ -48,4 +48,5 @@ menuconfig VFIO_NOIOMMU source "drivers/vfio/pci/Kconfig" source "drivers/vfio/platform/Kconfig" +source "drivers/vfio/mdev/Kconfig" source "virt/lib/Kconfig"drivers/vfio/Makefile +1
-0@@ -7,3 +7,4 @@ obj-$(CONFIG_VFIO_IOMMU_SPAPR_TCE) += vfio_iommu_spapr_tce.o obj-$(CONFIG_VFIO_SPAPR_EEH) += vfio_spapr_eeh.o obj-$(CONFIG_VFIO_PCI) += pci/ obj-$(CONFIG_VFIO_PLATFORM) += platform/ +obj-$(CONFIG_VFIO_MDEV) += mdev/drivers/vfio/mdev/Kconfig new file +0
-0
drivers/vfio/mdev/Kconfig line 8
Quoted 8 lines
drivers/vfio/mdev/Kconfig new file +8
-0@@ -0,0 +1,10 @@ + +config VFIO_MDEV + tristate "Mediated device driver framework" + depends on VFIO + default n + help + Provides a framework to virtualize devices. + See Documentation/vfio-mdev/vfio-mediated-device.txt for more details.
We don't have this doc at this point of time.
Quoted 17 lines
drivers/vfio/mdev/Kconfig new file +2
-0+ + If you don't know what do here, say N.drivers/vfio/mdev/Makefile new file +4
-0@@ -0,0 +1,4 @@ + +mdev-y := mdev_core.o mdev_sysfs.o mdev_driver.o + +obj-$(CONFIG_VFIO_MDEV) += mdev.odrivers/vfio/mdev/mdev_core.c new file +0
-0
[...]
drivers/vfio/mdev/mdev_core.c line 123
Quoted 12 lines
drivers/vfio/mdev/mdev_core.c new file +12
-0+ +/* +* mdev_device_remove_ops gets called from sysfs's 'remove' and when parent +* device is being unregistered from mdev device framework. +* - 'force_remove' is set to 'false' when called from sysfs's 'remove' which +* indicates that if the mdev device is active, used by VMM or userspace +* application, vendor driver could return error then don't remove the device. +* - 'force_remove' is set to 'true' when called from mdev_unregister_device() +* which indicate that parent device is being removed from mdev device +* framework so remove mdev device forcefully. +*/ +static int mdev_device_remove_ops (struct mdev_device *mdev,bool force_remove)
?
s/force_remove/force/
Quoted 82 lines
drivers/vfio/mdev/mdev_core.c new file +82
-0+{ +struct parent_device *parent = mdev->parent; +int ret; + +/* +* Vendor driver can return error if VMM or userspace application is +* using this mdev device. +*/ + ret = parent->ops->remove (mdev); +if (ret && !force_remove) +return -EBUSY ; + +sysfs_remove_groups (&mdev->dev.kobj, parent->ops->mdev_attr_groups); +return 0 ; +} + +static int mdev_device_remove_cb (struct device *dev,void *data) +{ +if (!dev_is_mdev (dev)) +return 0 ; + +return mdev_device_remove (dev, data ? *(bool *)data : true); +} + +/* +* mdev_register_device : Register a device +* @dev: device structure representing parent device. +* @ops: Parent device operation structure to be registered. +* +* Add device to list of registered parent devices. +* Returns a negative value on error, otherwise 0. +*/ +int mdev_register_device (struct device *dev,const struct parent_ops *ops) +{ +int ret; +struct parent_device *parent; + +/* check for mandatory ops */ +if (!ops || !ops->create || !ops->remove || !ops->supported_type_groups) +return -EINVAL ; + + dev =get_device (dev); +if (!dev) +return -EINVAL ; + +mutex_lock (&parent_list_lock); + +/* Check for duplicate */ + parent =__find_parent_device (dev); +if (parent) { + ret = -EEXIST ; + goto add_dev_err; + } + + parent =kzalloc (sizeof (*parent),GFP_KERNEL ); +if (!parent) { + ret = -ENOMEM ; + goto add_dev_err; + } + +kref_init (&parent->ref); +mutex_init (&parent->lock); + + parent->dev = dev; + parent->ops = ops; + +if (!mdev_bus_compat_class) { + mdev_bus_compat_class =class_compat_register ("mdev_bus" ); +if (!mdev_bus_compat_class) { + ret = -ENOMEM ; + goto add_dev_err; + } + } + + ret =parent_create_sysfs_files (parent); +if (ret) + goto add_dev_err; + + ret =class_compat_create_link (mdev_bus_compat_class, dev,NULL ); +if (ret) +dev_warn (dev,"Failed to create compatibility class link\n" ); +
drivers/vfio/mdev/mdev_core.c line 215
Quoted 10 lines
drivers/vfio/mdev/mdev_core.c new file +10
-0+list_add (&parent->next, &parent_list); +mutex_unlock (&parent_list_lock); + +dev_info (dev,"MDEV: Registered\n" ); +return 0 ; + +add_dev_err: +mutex_unlock (&parent_list_lock); +if (parent) +mdev_put_parent (parent);
Why do this? I don't find the place that you call mdev_get_parent above.
drivers/vfio/mdev/mdev_core.c line 217
Quoted 2 lines
drivers/vfio/mdev/mdev_core.c new file +2
-0+else +put_device (dev);
Shouldn't we always do this?
Quoted 26 lines
drivers/vfio/mdev/mdev_core.c new file +26
-0+return ret; +} +EXPORT_SYMBOL (mdev_register_device); + +/* +* mdev_unregister_device : Unregister a parent device +* @dev: device structure representing parent device. +* +* Remove device from list of registered parent devices. Give a chance to free +* existing mediated devices for given device. +*/ + +void mdev_unregister_device (struct device *dev) +{ +struct parent_device *parent; +bool force_remove = true; + +mutex_lock (&parent_list_lock); + parent =__find_parent_device (dev); + +if (!parent) { +mutex_unlock (&parent_list_lock); +return ; + } +dev_info (dev,"MDEV: Unregistering\n" ); +
drivers/vfio/mdev/mdev_core.c line 253
Quoted 10 lines
drivers/vfio/mdev/mdev_core.c new file +10
-0+list_del (&parent->next); +class_compat_remove_link (mdev_bus_compat_class, dev,NULL ); + +device_for_each_child (dev, (void *)&force_remove, + mdev_device_remove_cb); + +parent_remove_sysfs_files (parent); + +mutex_unlock (&parent_list_lock); +mdev_put_parent (parent);
We also need to call put_device(dev) here since we have a get_device during registration, no?
Or I must miss something...
Quoted 3 lines
drivers/vfio/mdev/mdev_core.c new file +3
-0+} +EXPORT_SYMBOL (mdev_unregister_device); +
[...]
Quoted 5 lines
drivers/vfio/mdev/mdev_core.c new file +5
-0+static int __init mdev_init (void ) +{ +int ret; + + ret =mdev_bus_register ();
drivers/vfio/mdev/mdev_core.c line 369
Quoted 10 lines
drivers/vfio/mdev/mdev_core.c new file +10
-0+if (ret) { +pr_err ("Failed to register mdev bus\n" ); +return ret; + } + +/* +* Attempt to load known vfio_mdev. This gives us a working environment +* without the user needing to explicitly load vfio_mdev driver. +*/ +request_module_nowait ("vfio_mdev" );
We don't have this module yet.
Quoted 25 lines
drivers/vfio/mdev/mdev_core.c new file +19
-0+ +return ret; +} + +static void __exit mdev_exit(void) +{ +if (mdev_bus_compat_class) +class_compat_unregister (mdev_bus_compat_class); + +mdev_bus_unregister (); +} + +module_init(mdev_init) +module_exit(mdev_exit) + +MODULE_VERSION(DRIVER_VERSION); +MODULE_LICENSE("GPL"); +MODULE_AUTHOR(DRIVER_AUTHOR); +MODULE_DESCRIPTION(DRIVER_DESC);drivers/vfio/mdev/mdev_driver.c new file +0
-0
drivers/vfio/mdev/mdev_driver.c line 6
Quoted 6 lines
drivers/vfio/mdev/mdev_driver.c new file +6
-0@@ -0,0 +1,122 @@ +/* +* MDEV driver +* +* Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. +* Author: Neo Jia <[email protected]> +* Kirti Wankhede <[email protected]>
Don't know if you care much for this:
There is a TAB before your name. :>
Quoted 17 lines
drivers/vfio/mdev/mdev_driver.c new file +17
-0+* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#include <linux/device.h> +#include <linux/iommu.h> +#include <linux/mdev.h> + +#include "mdev_private.h" + +static int mdev_attach_iommu (struct mdev_device *mdev) +{ +int ret; +struct iommu_group *group; +
drivers/vfio/mdev/mdev_driver.c line 33
Quoted 10 lines
drivers/vfio/mdev/mdev_driver.c new file +10
-0+ group =iommu_group_alloc (); +if (IS_ERR (group)) +return PTR_ERR (group); + + ret =iommu_group_add_device (group, &mdev->dev); +if (ret) + goto attach_fail; + +dev_info (&mdev->dev,"MDEV: group_id = %d\n" , +iommu_group_id (group));
nit: strange indentation.
The above two lines could be safely merge into one line.
Quoted 16 lines
drivers/vfio/mdev/mdev_driver.c new file +16
-0+attach_fail: +iommu_group_put (group); +return ret; +} + +static void mdev_detach_iommu (struct mdev_device *mdev) +{ +iommu_group_remove_device (&mdev->dev); +dev_info (&mdev->dev,"MDEV: detaching iommu\n" ); +} + +static int mdev_probe (struct device *dev) +{ +struct mdev_driver *drv =to_mdev_driver (dev->driver); +struct mdev_device *mdev =to_mdev_device (dev); +int ret;
drivers/vfio/mdev/mdev_driver.c line 59
Quoted 10 lines
drivers/vfio/mdev/mdev_driver.c new file +10
-0+ + ret =mdev_attach_iommu (mdev); +if (ret) +return ret; + +if (drv && drv->probe) + ret = drv->probe (dev); + +if (ret) +mdev_detach_iommu (mdev);
?
if (drv && drv->probe) {
ret = drv->probe(dev);
if (ret)
mdev_detach_iommu(mdev);
}Quoted 15 lines
drivers/vfio/mdev/mdev_driver.c new file +15
-0+ +return ret; +} + +static int mdev_remove (struct device *dev) +{ +struct mdev_driver *drv =to_mdev_driver (dev->driver); +struct mdev_device *mdev =to_mdev_device (dev); + +if (drv && drv->remove) + drv->remove (dev); + +mdev_detach_iommu (mdev); + +return 0 ;
drivers/vfio/mdev/mdev_driver.c line 84
Quoted 10 lines
drivers/vfio/mdev/mdev_driver.c new file +10
-0+} + +struct bus_type mdev_bus_type = { + .name ="mdev" , + .probe = mdev_probe, + .remove = mdev_remove, +}; +EXPORT_SYMBOL_GPL (mdev_bus_type); + +/*
Is this a kernel-doc comment?
It should be started with:
/**
Quoted 12 lines
drivers/vfio/mdev/mdev_driver.c new file +12
-0+* mdev_register_driver - register a new MDEV driver +* @drv: the driver to register +* @owner: module owner of driver to be registered +* +* Returns a negative value on error, otherwise 0. +*/ +int mdev_register_driver (struct mdev_driver *drv,struct module *owner) +{ +/* initialize common driver fields */ + drv->driver.name = drv->name; + drv->driver.bus = &mdev_bus_type; + drv->driver.owner = owner;
drivers/vfio/mdev/mdev_driver.c line 106
Quoted 10 lines
drivers/vfio/mdev/mdev_driver.c new file +10
-0+ +/* register with core */ +return driver_register (&drv->driver); +} +EXPORT_SYMBOL (mdev_register_driver); + +/* +* mdev_unregister_driver - unregister MDEV driver +* @drv: the driver to unregister +*
Empty line.
Quoted 16 lines
drivers/vfio/mdev/mdev_driver.c new file +16
-0+*/ +void mdev_unregister_driver (struct mdev_driver *drv) +{ +driver_unregister (&drv->driver); +} +EXPORT_SYMBOL (mdev_unregister_driver); + +int mdev_bus_register (void ) +{ +return bus_register (&mdev_bus_type); +} + +void mdev_bus_unregister (void ) +{ +bus_unregister (&mdev_bus_type); +}
[...]
Quoted 5 lines
include/linux/mdev.h +4
-0+/* Mediated device */ +struct mdev_device { +struct device dev; +struct parent_device *parent;
include/linux/mdev.h line 42
Quoted 10 lines
include/linux/mdev.h +10
-0+uuid_le uuid; +void *driver_data; + +/* internal */ +struct kref ref; +struct list_head next; +struct kobject *type_kobj; +}; + +
Empty line.
Quoted 4 lines
+/**
+ * structparent_ops- Structure to be registered for each parent device to
+ * register the device to mdev module.
+ *
[...]
Quoted 1 line
+ * @mmap: mmap callback
No need a piece of description for arguments of the mmap callback?
Quoted 23 lines
+ * Parent device that support mediated device should be registered with mdev
+ * module withparent_opsstructure.
+ **/include/linux/mdev.h +20
-0+ +struct parent_ops { +struct module *owner; +const struct attribute_group **dev_attr_groups; +const struct attribute_group **mdev_attr_groups; +struct attribute_group **supported_type_groups; + +int (*create)(struct kobject *kobj,struct mdev_device *mdev); +int (*remove)(struct mdev_device *mdev); +int (*open)(struct mdev_device *mdev); +void (*release)(struct mdev_device *mdev); +ssize_t (*read)(struct mdev_device *mdev,char __user *buf, +size_t count,loff_t *ppos); +ssize_t (*write)(struct mdev_device *mdev,const char __user *buf, +size_t count,loff_t *ppos); +ssize_t (*ioctl)(struct mdev_device *mdev,unsigned int cmd, +unsigned long arg); +int (*mmap)(struct mdev_device *mdev,struct vm_area_struct *vma); +}; +
[...]
Signature · 1 line
Dong Jia
Re: [Qemu-devel] [PATCH v11 01/22] vfio: Mediated device Core driver
In reply to Kirti Wankhede* Kirti Wankhede <[email protected]> [2016-11-05 02:40:35 +0530]:
Hi Kirti,
[...]
Quoted 25 lines
drivers/vfio/Kconfig +1
-0@@ -48,4 +48,5 @@ menuconfig VFIO_NOIOMMU source "drivers/vfio/pci/Kconfig" source "drivers/vfio/platform/Kconfig" +source "drivers/vfio/mdev/Kconfig" source "virt/lib/Kconfig"drivers/vfio/Makefile +1
-0@@ -7,3 +7,4 @@ obj-$(CONFIG_VFIO_IOMMU_SPAPR_TCE) += vfio_iommu_spapr_tce.o obj-$(CONFIG_VFIO_SPAPR_EEH) += vfio_spapr_eeh.o obj-$(CONFIG_VFIO_PCI) += pci/ obj-$(CONFIG_VFIO_PLATFORM) += platform/ +obj-$(CONFIG_VFIO_MDEV) += mdev/drivers/vfio/mdev/Kconfig new file +0
-0
drivers/vfio/mdev/Kconfig line 8
Quoted 8 lines
drivers/vfio/mdev/Kconfig new file +8
-0@@ -0,0 +1,10 @@ + +config VFIO_MDEV + tristate "Mediated device driver framework" + depends on VFIO + default n + help + Provides a framework to virtualize devices. + See Documentation/vfio-mdev/vfio-mediated-device.txt for more details.
We don't have this doc at this point of time.
Quoted 17 lines
drivers/vfio/mdev/Kconfig new file +2
-0+ + If you don't know what do here, say N.drivers/vfio/mdev/Makefile new file +4
-0@@ -0,0 +1,4 @@ + +mdev-y := mdev_core.o mdev_sysfs.o mdev_driver.o + +obj-$(CONFIG_VFIO_MDEV) += mdev.odrivers/vfio/mdev/mdev_core.c new file +0
-0
[...]
drivers/vfio/mdev/mdev_core.c line 123
Quoted 12 lines
drivers/vfio/mdev/mdev_core.c new file +12
-0+ +/* +* mdev_device_remove_ops gets called from sysfs's 'remove' and when parent +* device is being unregistered from mdev device framework. +* - 'force_remove' is set to 'false' when called from sysfs's 'remove' which +* indicates that if the mdev device is active, used by VMM or userspace +* application, vendor driver could return error then don't remove the device. +* - 'force_remove' is set to 'true' when called from mdev_unregister_device() +* which indicate that parent device is being removed from mdev device +* framework so remove mdev device forcefully. +*/ +static int mdev_device_remove_ops (struct mdev_device *mdev,bool force_remove)
?
s/force_remove/force/
Quoted 82 lines
drivers/vfio/mdev/mdev_core.c new file +82
-0+{ +struct parent_device *parent = mdev->parent; +int ret; + +/* +* Vendor driver can return error if VMM or userspace application is +* using this mdev device. +*/ + ret = parent->ops->remove (mdev); +if (ret && !force_remove) +return -EBUSY ; + +sysfs_remove_groups (&mdev->dev.kobj, parent->ops->mdev_attr_groups); +return 0 ; +} + +static int mdev_device_remove_cb (struct device *dev,void *data) +{ +if (!dev_is_mdev (dev)) +return 0 ; + +return mdev_device_remove (dev, data ? *(bool *)data : true); +} + +/* +* mdev_register_device : Register a device +* @dev: device structure representing parent device. +* @ops: Parent device operation structure to be registered. +* +* Add device to list of registered parent devices. +* Returns a negative value on error, otherwise 0. +*/ +int mdev_register_device (struct device *dev,const struct parent_ops *ops) +{ +int ret; +struct parent_device *parent; + +/* check for mandatory ops */ +if (!ops || !ops->create || !ops->remove || !ops->supported_type_groups) +return -EINVAL ; + + dev =get_device (dev); +if (!dev) +return -EINVAL ; + +mutex_lock (&parent_list_lock); + +/* Check for duplicate */ + parent =__find_parent_device (dev); +if (parent) { + ret = -EEXIST ; + goto add_dev_err; + } + + parent =kzalloc (sizeof (*parent),GFP_KERNEL ); +if (!parent) { + ret = -ENOMEM ; + goto add_dev_err; + } + +kref_init (&parent->ref); +mutex_init (&parent->lock); + + parent->dev = dev; + parent->ops = ops; + +if (!mdev_bus_compat_class) { + mdev_bus_compat_class =class_compat_register ("mdev_bus" ); +if (!mdev_bus_compat_class) { + ret = -ENOMEM ; + goto add_dev_err; + } + } + + ret =parent_create_sysfs_files (parent); +if (ret) + goto add_dev_err; + + ret =class_compat_create_link (mdev_bus_compat_class, dev,NULL ); +if (ret) +dev_warn (dev,"Failed to create compatibility class link\n" ); +
drivers/vfio/mdev/mdev_core.c line 215
Quoted 10 lines
drivers/vfio/mdev/mdev_core.c new file +10
-0+list_add (&parent->next, &parent_list); +mutex_unlock (&parent_list_lock); + +dev_info (dev,"MDEV: Registered\n" ); +return 0 ; + +add_dev_err: +mutex_unlock (&parent_list_lock); +if (parent) +mdev_put_parent (parent);
Why do this? I don't find the place that you call mdev_get_parent above.
drivers/vfio/mdev/mdev_core.c line 217
Quoted 2 lines
drivers/vfio/mdev/mdev_core.c new file +2
-0+else +put_device (dev);
Shouldn't we always do this?
Quoted 26 lines
drivers/vfio/mdev/mdev_core.c new file +26
-0+return ret; +} +EXPORT_SYMBOL (mdev_register_device); + +/* +* mdev_unregister_device : Unregister a parent device +* @dev: device structure representing parent device. +* +* Remove device from list of registered parent devices. Give a chance to free +* existing mediated devices for given device. +*/ + +void mdev_unregister_device (struct device *dev) +{ +struct parent_device *parent; +bool force_remove = true; + +mutex_lock (&parent_list_lock); + parent =__find_parent_device (dev); + +if (!parent) { +mutex_unlock (&parent_list_lock); +return ; + } +dev_info (dev,"MDEV: Unregistering\n" ); +
drivers/vfio/mdev/mdev_core.c line 253
Quoted 10 lines
drivers/vfio/mdev/mdev_core.c new file +10
-0+list_del (&parent->next); +class_compat_remove_link (mdev_bus_compat_class, dev,NULL ); + +device_for_each_child (dev, (void *)&force_remove, + mdev_device_remove_cb); + +parent_remove_sysfs_files (parent); + +mutex_unlock (&parent_list_lock); +mdev_put_parent (parent);
We also need to call put_device(dev) here since we have a get_device during registration, no?
Or I must miss something...
Quoted 3 lines
drivers/vfio/mdev/mdev_core.c new file +3
-0+} +EXPORT_SYMBOL (mdev_unregister_device); +
[...]
Quoted 5 lines
drivers/vfio/mdev/mdev_core.c new file +5
-0+static int __init mdev_init (void ) +{ +int ret; + + ret =mdev_bus_register ();
drivers/vfio/mdev/mdev_core.c line 369
Quoted 10 lines
drivers/vfio/mdev/mdev_core.c new file +10
-0+if (ret) { +pr_err ("Failed to register mdev bus\n" ); +return ret; + } + +/* +* Attempt to load known vfio_mdev. This gives us a working environment +* without the user needing to explicitly load vfio_mdev driver. +*/ +request_module_nowait ("vfio_mdev" );
We don't have this module yet.
Quoted 25 lines
drivers/vfio/mdev/mdev_core.c new file +19
-0+ +return ret; +} + +static void __exit mdev_exit(void) +{ +if (mdev_bus_compat_class) +class_compat_unregister (mdev_bus_compat_class); + +mdev_bus_unregister (); +} + +module_init(mdev_init) +module_exit(mdev_exit) + +MODULE_VERSION(DRIVER_VERSION); +MODULE_LICENSE("GPL"); +MODULE_AUTHOR(DRIVER_AUTHOR); +MODULE_DESCRIPTION(DRIVER_DESC);drivers/vfio/mdev/mdev_driver.c new file +0
-0
drivers/vfio/mdev/mdev_driver.c line 6
Quoted 6 lines
drivers/vfio/mdev/mdev_driver.c new file +6
-0@@ -0,0 +1,122 @@ +/* +* MDEV driver +* +* Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. +* Author: Neo Jia <[email protected]> +* Kirti Wankhede <[email protected]>
Don't know if you care much for this:
There is a TAB before your name. :>
Quoted 17 lines
drivers/vfio/mdev/mdev_driver.c new file +17
-0+* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#include <linux/device.h> +#include <linux/iommu.h> +#include <linux/mdev.h> + +#include "mdev_private.h" + +static int mdev_attach_iommu (struct mdev_device *mdev) +{ +int ret; +struct iommu_group *group; +
drivers/vfio/mdev/mdev_driver.c line 33
Quoted 10 lines
drivers/vfio/mdev/mdev_driver.c new file +10
-0+ group =iommu_group_alloc (); +if (IS_ERR (group)) +return PTR_ERR (group); + + ret =iommu_group_add_device (group, &mdev->dev); +if (ret) + goto attach_fail; + +dev_info (&mdev->dev,"MDEV: group_id = %d\n" , +iommu_group_id (group));
nit: strange indentation.
The above two lines could be safely merge into one line.
Quoted 16 lines
drivers/vfio/mdev/mdev_driver.c new file +16
-0+attach_fail: +iommu_group_put (group); +return ret; +} + +static void mdev_detach_iommu (struct mdev_device *mdev) +{ +iommu_group_remove_device (&mdev->dev); +dev_info (&mdev->dev,"MDEV: detaching iommu\n" ); +} + +static int mdev_probe (struct device *dev) +{ +struct mdev_driver *drv =to_mdev_driver (dev->driver); +struct mdev_device *mdev =to_mdev_device (dev); +int ret;
drivers/vfio/mdev/mdev_driver.c line 59
Quoted 10 lines
drivers/vfio/mdev/mdev_driver.c new file +10
-0+ + ret =mdev_attach_iommu (mdev); +if (ret) +return ret; + +if (drv && drv->probe) + ret = drv->probe (dev); + +if (ret) +mdev_detach_iommu (mdev);
?
if (drv && drv->probe) {
ret = drv->probe(dev);
if (ret)
mdev_detach_iommu(mdev);
}Quoted 15 lines
drivers/vfio/mdev/mdev_driver.c new file +15
-0+ +return ret; +} + +static int mdev_remove (struct device *dev) +{ +struct mdev_driver *drv =to_mdev_driver (dev->driver); +struct mdev_device *mdev =to_mdev_device (dev); + +if (drv && drv->remove) + drv->remove (dev); + +mdev_detach_iommu (mdev); + +return 0 ;
drivers/vfio/mdev/mdev_driver.c line 84
Quoted 10 lines
drivers/vfio/mdev/mdev_driver.c new file +10
-0+} + +struct bus_type mdev_bus_type = { + .name ="mdev" , + .probe = mdev_probe, + .remove = mdev_remove, +}; +EXPORT_SYMBOL_GPL (mdev_bus_type); + +/*
Is this a kernel-doc comment?
It should be started with:
/**
Quoted 12 lines
drivers/vfio/mdev/mdev_driver.c new file +12
-0+* mdev_register_driver - register a new MDEV driver +* @drv: the driver to register +* @owner: module owner of driver to be registered +* +* Returns a negative value on error, otherwise 0. +*/ +int mdev_register_driver (struct mdev_driver *drv,struct module *owner) +{ +/* initialize common driver fields */ + drv->driver.name = drv->name; + drv->driver.bus = &mdev_bus_type; + drv->driver.owner = owner;
drivers/vfio/mdev/mdev_driver.c line 106
Quoted 10 lines
drivers/vfio/mdev/mdev_driver.c new file +10
-0+ +/* register with core */ +return driver_register (&drv->driver); +} +EXPORT_SYMBOL (mdev_register_driver); + +/* +* mdev_unregister_driver - unregister MDEV driver +* @drv: the driver to unregister +*
Empty line.
Quoted 16 lines
drivers/vfio/mdev/mdev_driver.c new file +16
-0+*/ +void mdev_unregister_driver (struct mdev_driver *drv) +{ +driver_unregister (&drv->driver); +} +EXPORT_SYMBOL (mdev_unregister_driver); + +int mdev_bus_register (void ) +{ +return bus_register (&mdev_bus_type); +} + +void mdev_bus_unregister (void ) +{ +bus_unregister (&mdev_bus_type); +}
[...]
Quoted 5 lines
include/linux/mdev.h +4
-0+/* Mediated device */ +struct mdev_device { +struct device dev; +struct parent_device *parent;
include/linux/mdev.h line 42
Quoted 10 lines
include/linux/mdev.h +10
-0+uuid_le uuid; +void *driver_data; + +/* internal */ +struct kref ref; +struct list_head next; +struct kobject *type_kobj; +}; + +
Empty line.
Quoted 4 lines
+/**
+ * structparent_ops- Structure to be registered for each parent device to
+ * register the device to mdev module.
+ *
[...]
Quoted 1 line
+ * @mmap: mmap callback
No need a piece of description for arguments of the mmap callback?
Quoted 23 lines
+ * Parent device that support mediated device should be registered with mdev
+ * module withparent_opsstructure.
+ **/include/linux/mdev.h +20
-0+ +struct parent_ops { +struct module *owner; +const struct attribute_group **dev_attr_groups; +const struct attribute_group **mdev_attr_groups; +struct attribute_group **supported_type_groups; + +int (*create)(struct kobject *kobj,struct mdev_device *mdev); +int (*remove)(struct mdev_device *mdev); +int (*open)(struct mdev_device *mdev); +void (*release)(struct mdev_device *mdev); +ssize_t (*read)(struct mdev_device *mdev,char __user *buf, +size_t count,loff_t *ppos); +ssize_t (*write)(struct mdev_device *mdev,const char __user *buf, +size_t count,loff_t *ppos); +ssize_t (*ioctl)(struct mdev_device *mdev,unsigned int cmd, +unsigned long arg); +int (*mmap)(struct mdev_device *mdev,struct vm_area_struct *vma); +}; +
[...]
Signature · 1 line
Dong Jia
Kirti Wankhede
maintainerRe: [PATCH v11 05/22] vfio iommu: Added pin and unpin callback functions to vfio_iommu_driver_ops
In reply to Alex WilliamsonQuoted 3 lines · Alex Williamson
On Sat, 5 Nov 2016 02:40:39 +0530
Kirti Wankhede <[email protected]> wrote:
...
Quoted 23 lines
Quoted 23 lines
drivers/vfio/vfio.c +23
-0+int vfio_pin_pages (struct device *dev,unsigned long *user_pfn, +int npage,int prot,unsigned long *phys_pfn) +{ +struct vfio_container *container; +struct vfio_group *group; +struct vfio_iommu_driver *driver; +int ret; + +if (!dev || !user_pfn || !phys_pfn) +return -EINVAL ; + + group =vfio_group_get_from_dev (dev); +if (IS_ERR (group)) +return PTR_ERR (group); + + ret =vfio_group_add_container_user (group); +if (ret) + goto err_pin_pages; + + container = group->container; +down_read (&container->group_lock); + + driver = container->iommu_driver;
Quoted 9 lines
drivers/vfio/vfio.c void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset) · line 1840
Quoted 5 lines
drivers/vfio/vfio.c +5
-0+if (likely (driver && driver->ops->pin_pages)) + ret = driver->ops->pin_pages (container->iommu_data, user_pfn, + npage, prot, phys_pfn); +else + ret = -EINVAL ;
-ENOTTYmight be a more appropriate error return here and below since we're not signaling invalid argument, we're signaling lack of support.
Used -EINVAL in sync with other driver->ops like read, write and mmap. Changing it to -ENOTTY as you suggested above since these ops are optional.
...
Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c static int vaddr_get_pfn(unsigned long vaddr, int prot, unsigned long *pfn) · line 263
Quoted 4 lines
drivers/vfio/vfio_iommu_type1.c +2
-2-static long vfio_pin_pages (unsigned long vaddr,long npage, -int prot,unsigned long *pfn_base) +static long __vfio_pin_pages_remote (unsigned long vaddr,long npage, +int prot,unsigned long *pfn_base)nit, what is the additional underscore prefix intended to imply? Appending
_remoteis sufficient to avoid the symbol conflict.
This function name changed in review process from start, we started with changing to __vfio_pin_pages and then added _remote to it later. We can remove '__' from it. Updating.
...
Quoted 10 lines
include/linux/vfio.h struct vfio_iommu_driver_ops { · line 81
Quoted 5 lines
include/linux/vfio.h +4
-1- +int (*pin_pages)(void *iommu_data,unsigned long *user_pfn, +int npage,int prot, +unsigned long *phys_pfn); + int (*unpin_pages)(void *iommu_data,Are we changing from long to int here simply because of the absurdity in passing in more than a 2^31 entry array, that would already consume more than 16GB itself?
These are on demand pin/unpin request, will that request go beyond 16GB limit? For Nvidia vGPU solution, pin request will not go beyond this limit.
Quoted 5 lines
include/linux/vfio.h struct vfio_iommu_driver_ops { · line 83
Quoted 2 lines
include/linux/vfio.h +2
-0+unsigned long *user_pfn, + unsigned long *pfn,nit, use
phys_pfnso as to match the pin function?
Ok.
Quoted 8 lines
Quoted 8 lines
include/linux/vfio.h +1
-0+ int npage); };extern int vfio_register_iommu_driver (const struct vfio_iommu_driver_ops *ops);@@ -127,6 +133,12 @@ static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group, }#endif /* CONFIG_EEH */
Quoted 10 lines
include/linux/vfio.h static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group, · line 144
Quoted 9 lines
include/linux/vfio.h +6
-0+extern int vfio_pin_pages (struct device *dev,unsigned long *user_pfn, +int npage,int prot,unsigned long *phys_pfn); + +extern int vfio_unpin_pages (struct device *dev,unsigned long *user_pfn, +unsigned long *pfn,int npage); +/* * IRQfd - generic */
Thanks,
Kirti
Kirti Wankhede
maintainerRe: [PATCH v11 09/22] vfio iommu type1: Add task structure to vfio_dma
In reply to Alex WilliamsonQuoted 3 lines · Alex Williamson
On Sat, 5 Nov 2016 02:40:43 +0530
Kirti Wankhede <[email protected]> wrote:
...
Quoted 20 lines
Quoted 20 lines
static int vfio_dma_do_map(struct
vfio_iommu*iommu,drivers/vfio/vfio_iommu_type1.c +3
-2struct vfio_iommu_type1_dma_map *map) {dma_addr_t iova = map->iova;unsigned long vaddr = map->vaddr;size_t size = map->size; -long npage;int ret =0 , prot =0 ;uint64_t mask;struct vfio_dma *dma; -unsigned long pfn; +struct vfio_addr_space *addr_space; +struct mm_struct *mm; +bool free_addr_space_on_err = false;/* Verify that none of our __u64 fields overflow */ if (map->size != size || map->vaddr != vaddr || map->iova != iova)@@ -608,47 +685,56 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu,mutex_lock (&iommu->lock);
Quoted 13 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_dma_do_map(struct vfio_iommu *iommu, · line 694
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +7
-2if (vfio_find_dma (iommu, iova, size)) { -mutex_unlock (&iommu->lock); -return -EEXIST ; + ret = -EEXIST ; + goto do_map_err; + } + + mm =get_task_mm (current); +if (!mm) { + ret = -ENODEV ;
-EFAULT?
-ENODEV return is in original code from vfio_pin_pages()
if (!current->mm)
return -ENODEV;Once I thought of changing it to -EFAULT, but then again changed to -ENODEV to be consistent with original error code.
Should I still change this return to -EFAULT?
Quoted 32 lines
Quoted 32 lines
drivers/vfio/vfio_iommu_type1.c +24
-2+ goto do_map_err; + } + + addr_space =vfio_find_addr_space (iommu, mm); +if (addr_space) { +atomic_inc (&addr_space->ref_count); +mmput (mm); + }else { + addr_space =kzalloc (sizeof (*addr_space),GFP_KERNEL ); +if (!addr_space) { + ret = -ENOMEM ; + goto do_map_err; + } + addr_space->mm = mm; +atomic_set (&addr_space->ref_count,1 ); +list_add (&addr_space->next, &iommu->addr_space_list); + free_addr_space_on_err = true; } dma =kzalloc (sizeof (*dma),GFP_KERNEL );if (!dma) { -mutex_unlock (&iommu->lock); -return -ENOMEM ; +if (free_addr_space_on_err) { +mmput (mm); +list_del (&addr_space->next); +kfree (addr_space); + } + ret = -ENOMEM ; + goto do_map_err; }
Quoted 16 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_dma_do_map(struct vfio_iommu *iommu, · line 731
Quoted 7 lines
drivers/vfio/vfio_iommu_type1.c +4
-0dma->iova = iova; dma->vaddr = vaddr; dma->prot = prot; + dma->addr_space = addr_space; +get_task_struct (current); + dma->task = current; + dma->mlock_cap =capable (CAP_IPC_LOCK );How do you reason we can cache this? Does the fact that the process had this capability at the time that it did a
DMA_MAPimply that it necessarily still has this capability when an external user (vendor driver) tries to pin pages? I don't see how we can make that assumption.
Will process change MEMLOCK limit at runtime? I think it shouldn't, correct me if I'm wrong. QEMU doesn't do that, right?
The function capable() determines current task's capability. But when vfio_pin_pages() is called, it could come from other task but pages are pinned from address space of task who mapped it. So we can't use capable() in vfio_pin_pages()
If this capability shouldn't be cached, we have to use has_capability() with dma->task as argument in vfio_pin_pages()
bool has_capability(struct task_struct *t, int cap)
Thanks,
Kirti
Kirti Wankhede
maintainerRe: [PATCH v11 10/22] vfio iommu type1: Add support for mediated devices
In reply to Alex WilliamsonQuoted 3 lines · Alex Williamson
On Sat, 5 Nov 2016 02:40:44 +0530
Kirti Wankhede <[email protected]> wrote:
...
Quoted 50 lines
Quoted 30 lines
drivers/vfio/vfio_iommu_type1.c +29
-1-static void vfio_unmap_unpin (struct vfio_iommu *iommu,struct vfio_dma *dma) +static int __vfio_pin_page_external (struct vfio_dma *dma,unsigned long vaddr, +int prot,unsigned long *pfn_base, +bool do_accounting) +{ +struct task_struct *task = dma->task; +unsigned long limit =task_rlimit (task,RLIMIT_MEMLOCK ) >>PAGE_SHIFT ; +bool lock_cap = dma->mlock_cap; +struct mm_struct *mm = dma->addr_space->mm; +int ret; +bool rsvd; + + ret =vaddr_get_pfn (mm, vaddr, prot, pfn_base); +if (ret) +return ret; + + rsvd =is_invalid_reserved_pfn (*pfn_base); + +if (!rsvd && !lock_cap && mm->locked_vm +1 > limit) { +put_pfn (*pfn_base, prot); +pr_warn ("%s: Task %s (%d) RLIMIT_MEMLOCK (%ld) exceeded\n" , + __func__, task->comm,task_pid_nr (task), + limit <<PAGE_SHIFT ); +return -ENOMEM ; + } + +if (!rsvd && do_accounting) +vfio_lock_acct (mm,1 ); + +return 1 ;drivers/vfio/vfio_iommu_type1.c static long __vfio_unpin_pages_remote(struct vfio_dma *dma, unsigned long pfn, · line 521
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10
-0+} + +static void __vfio_unpin_page_external (struct vfio_addr_space *addr_space, +unsigned long pfn,int prot, +bool do_accounting) +{ +put_pfn (pfn, prot); + +if (do_accounting) +vfio_lock_acct (addr_space->mm,-1 );Can't we batch this like we do elsewhere? Intel folks, AIUI you intend to pin all VM memory through this side channel, have you tested the scalability and performance of this with larger VMs? Our
vfio_pfndata structure alone is 40 bytes per pinned page, which means for each 1GB of VM memory, we have 10MBs worth of structvfio_pfn! Additionally, unmapping each 1GB of VM memory will result in 256k separatevfio_lock_acct()callbacks. I'm concerned that we're not being efficient enough in either space or time.
Quoted 5 lines
One thought might be whether we really need to save the pfn, we better always get the same result if we pin it again, or maybe we can just do a lookup through the mm at that point without re-pinning. Could we get to the point where we only need an
atomic_tref count per page in a linear array relative to the IOVA?
Ok. Is System RAM hot-plug supported? How is system RAM hot-plug handled? Are there DMA_MAP calls on such hot-plug for additional range? If we have a linear array/memory, we will have to realloc it on memory hot-plug?
Quoted 11 lines
That would give us 1MB per 1GB
overhead. The semantics of the pin and unpin would make more sense then too, both would take an IOVA range, only pinning would need a return mechanism. For instance:int pin_pages(void
*iommu_data,dma_addr_tiova_base,int npage, unsigned long *pfn_base);This would pin physically contiguous pages up to npage, returning the base pfn and returning the number of pages pinned (<= npage). The vendor driver would make multiple calls to fill the necessary range.
With the current patch, input is user_pfn[] array and npages.
int vfio_pin_pages(struct device *dev, unsigned long *user_pfn,
int npage, int prot, unsigned long *phys_pfn)
When guest allocates memory with malloc(), gfns would not be contiguous, right? These gfns (user_pfns) are passed as argument here.
Is there any case where we could get pin/unpin request for contiguous pages?
Quoted 8 lines
Unpin would then simply be:
void
unpin_pages(void *iommu_data, dma_addr_t iova_base, int npage);Hugepage usage would really make such an interface shine (ie. 2MB+ contiguous ranges). A downside would be the overhead of getting the group and container reference in vfio for each callback, perhaps we'd need to figure out how the vendor driver could hold that reference.
In very initial phases of proposal, I had suggested to keep pointer to container->iommu_data in struct mdev_device. But that was discarded.
Quoted 3 lines
The current API of passing around pfn arrays, further increasing the overhead of the whole ecosystem just makes me cringe though.
...
Quoted 10 lines
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10
-0+if (ret <=0 ) { +WARN_ON (!ret); + goto pin_unwind; + } + +mutex_lock (&dma->addr_space->pfn_list_lock); + +/* search if pfn exist */ + p =vfio_find_pfn (dma->addr_space, pfn[i]); +if (p) {
Quoted 9 lines
drivers/vfio/vfio_iommu_type1.c static long __vfio_unpin_pages_remote(struct vfio_dma *dma, unsigned long pfn, · line 591
Quoted 1 line
drivers/vfio/vfio_iommu_type1.c +1
-0+atomic_inc (&p->ref_count);We never test whether (
p->prot== prot), shouldn't we be doing something in that case? In fact, why do we allow the side-channel through the .{un}pin_pages to specify page protection flags that might be different than the user specified for theDMA_MAP? If the user specified read-only, the vendor driver should not be allowed to override with read-write access.
If user specified protection flags for DMA_MAP could be prot = IOMMU_WRITE | IOMMU_READ;
But vendor driver can request to pin page to be readonly, i.e. IOMMU_READ. In that case, pin pages should be allowed, right?
Then the check should be if (p->prot & prot).
Thanks,
Kirti
Re: [PATCH v11 10/22] vfio iommu type1: Add support for mediated devices
In reply to Jike SongQuoted 298 lines · On Tue, 08 Nov 2016 10:20:14 +0800 Jike Song <[email protected]> wrote:
Quoted 286 lines · Alex Williamson
Quoted 8 lines · On Sat, 5 Nov 2016 02:40:44 +0530 Kirti Wankhede <[email protected]> wrote:
VFIO IOMMU drivers are designed for the devices which are IOMMU capable. Mediated device only uses IOMMU APIs, the underlying hardware can be managed by an IOMMU domain.
Aim of this change is:
- To use most of the code of TYPE1 IOMMU driver for mediated devices
- To support direct assigned device and mediated device in single moduleQuoted 6 lines
This change adds pin and unpin support for mediated device to TYPE1 IOMMU backend module. More details:
-vfio_pin_pages()callback here uses task and address space ofvfio_dma, that is, of the process who mapped that iova range.
- Addedpfn_listtracking logic to address space structure. All pages pinned through this interface are trached in its address space.^ k------------------------------------------|
Quoted 255 lines
- Pinned pages list is used to verify unpinning request and to unpin remaining pages while detaching the group for that device.
- Page accounting is updated to account in its address space where the pages are pinned/unpinned.
- Accouting for mdev device is only done if there is no iommu capable domain in the container. When there is a direct device assigned to the container and that domain is iommu capable, all pages are already pinned duringDMA_MAP.
- Page accouting is updated on hot plug and unplug mdev device and pass through device.Tested by assigning below combinations of devices to a single VM:
- GPU pass through only
- vGPU device only
- One GPU pass through and one vGPU device
- Linux VM hot plug and unplug vGPU device while GPU pass through device exist
- Linux VM hot plug and unplug GPU pass through device while vGPU device existSigned-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id I295d6f0f2e0579b8d9882bfd8fd5a4194b97bd9a--- drivers/vfio/vfio_iommu_type1.c | 538 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 500 insertions(+), 38 deletions(-)drivers/vfio/vfio_iommu_type1.c +143
-5@@ -36,6 +36,7 @@#include <linux/uaccess.h> #include <linux/vfio.h> #include <linux/workqueue.h> +#include <linux/mdev.h> #define DRIVER_VERSION "0.2"#define DRIVER_AUTHOR "Alex Williamson <[email protected]>"@@ -56,6 +57,7 @@ MODULE_PARM_DESC(disable_hugepages,struct vfio_iommu {struct list_head domain_list;struct list_head addr_space_list; +struct vfio_domain *external_domain;/* domain for external user */ struct mutex lock;struct rb_root dma_list;bool v2;@@ -67,6 +69,9 @@ struct vfio_addr_space {struct mm_struct *mm;struct list_head next;atomic_t ref_count; +/* external user pinned pfns */ +struct rb_root pfn_list;/* pinned Host pfn list */ +struct mutex pfn_list_lock;/* mutex for pfn_list */ };struct vfio_domain {@@ -83,6 +88,7 @@ struct vfio_dma {unsigned long vaddr;/* Process virtual addr */ size_t size;/* Map size (bytes) */ int prot;/* IOMMU_READ/WRITE */ +bool iommu_mapped;struct vfio_addr_space *addr_space;struct task_struct *task;bool mlock_cap;@@ -94,6 +100,19 @@ struct vfio_group { };/* +* Guest RAM pinning working set or DMA target +*/ +struct vfio_pfn { +struct rb_node node; +unsigned long pfn;/* Host pfn */ +int prot; +atomic_t ref_count; +}; + +#define IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu) \ + (!list_empty(&iommu->domain_list)) + +/* * This code handles mapping and unmapping of user data buffers * into DMA'ble space using the IOMMU */ @@ -153,6 +172,93 @@ static struct vfio_addr_space *vfio_find_addr_space(struct vfio_iommu *iommu,return NULL ; } +/* +* Helper Functions for host pfn list +*/ +static struct vfio_pfn *vfio_find_pfn (struct vfio_addr_space *addr_space, +unsigned long pfn) +{ +struct vfio_pfn *vpfn; +struct rb_node *node = addr_space->pfn_list.rb_node; + +while (node) { + vpfn = rb_entry(node, struct vfio_pfn, node); + +if (pfn < vpfn->pfn) + node = node->rb_left; +else if (pfn > vpfn->pfn) + node = node->rb_right; +else +return vpfn; + } + +return NULL ; +} + +static void vfio_link_pfn (struct vfio_addr_space *addr_space, +struct vfio_pfn *new) +{ +struct rb_node **link, *parent =NULL ; +struct vfio_pfn *vpfn; + + link = &addr_space->pfn_list.rb_node; +while (*link) { + parent = *link; + vpfn = rb_entry(parent, struct vfio_pfn, node); + +if (new->pfn < vpfn->pfn) + link = &(*link)->rb_left; +else + link = &(*link)->rb_right; + } + +rb_link_node (&new->node, parent, link); +rb_insert_color (&new->node, &addr_space->pfn_list); +} + +static void vfio_unlink_pfn (struct vfio_addr_space *addr_space, +struct vfio_pfn *old) +{ +rb_erase (&old->node, &addr_space->pfn_list); +} + +static int vfio_add_to_pfn_list (struct vfio_addr_space *addr_space, +unsigned long pfn,int prot) +{ +struct vfio_pfn *vpfn; + + vpfn =kzalloc (sizeof (*vpfn),GFP_KERNEL ); +if (!vpfn) +return -ENOMEM ; + + vpfn->pfn = pfn; + vpfn->prot = prot; +atomic_set (&vpfn->ref_count,1 ); +vfio_link_pfn (addr_space, vpfn); +return 0 ; +} + +static void vfio_remove_from_pfn_list (struct vfio_addr_space *addr_space, +struct vfio_pfn *vpfn) +{ +vfio_unlink_pfn (addr_space, vpfn); +kfree (vpfn); +} + +static int vfio_pfn_account (struct vfio_addr_space *addr_space, +unsigned long pfn) +{ +struct vfio_pfn *p; +int ret =1 ; + +mutex_lock (&addr_space->pfn_list_lock); + p =vfio_find_pfn (addr_space, pfn); +if (p) + ret =0 ; +mutex_unlock (&addr_space->pfn_list_lock); +return ret; +} +struct vwork {struct mm_struct *mm;long npage;@@ -304,16 +410,18 @@ static long __vfio_pin_pages_remote(struct vfio_dma *dma, unsigned long vaddr,unsigned long limit =task_rlimit (task,RLIMIT_MEMLOCK ) >>PAGE_SHIFT ;bool lock_cap = dma->mlock_cap;struct mm_struct *mm = dma->addr_space->mm; -long ret, i; +long ret, i, lock_acct;bool rsvd; ret =vaddr_get_pfn (mm, vaddr, prot, pfn_base);if (ret)return ret; + lock_acct =vfio_pfn_account (dma->addr_space, *pfn_base); + rsvd =is_invalid_reserved_pfn (*pfn_base); -if (!rsvd && !lock_cap && mm->locked_vm +1 > limit) { +if (!rsvd && !lock_cap && mm->locked_vm + lock_acct > limit) {put_pfn (*pfn_base, prot);pr_warn ("%s: RLIMIT_MEMLOCK (%ld) exceeded\n" , __func__, limit <<PAGE_SHIFT );@@ -340,8 +448,10 @@ static long __vfio_pin_pages_remote(struct vfio_dma *dma, unsigned long vaddr,break ; } + lock_acct +=vfio_pfn_account (dma->addr_space, pfn); +if (!rsvd && !lock_cap && - mm->locked_vm + i +1 > limit) { + mm->locked_vm + lock_acct +1 > limit) {put_pfn (pfn, prot);pr_warn ("%s: RLIMIT_MEMLOCK (%ld) exceeded\n" , __func__, limit <<PAGE_SHIFT );@@ -350,7 +460,7 @@ static long __vfio_pin_pages_remote(struct vfio_dma *dma, unsigned long vaddr, }if (!rsvd) -vfio_lock_acct (mm, i); +vfio_lock_acct (mm, lock_acct);return i; }@@ -370,14 +480,214 @@ static long __vfio_unpin_pages_remote(struct vfio_dma *dma, unsigned long pfn,return unlocked; } -static void vfio_unmap_unpin (struct vfio_iommu *iommu,struct vfio_dma *dma) +static int __vfio_pin_page_external (struct vfio_dma *dma,unsigned long vaddr, +int prot,unsigned long *pfn_base, +bool do_accounting) +{ +struct task_struct *task = dma->task; +unsigned long limit =task_rlimit (task,RLIMIT_MEMLOCK ) >>PAGE_SHIFT ; +bool lock_cap = dma->mlock_cap; +struct mm_struct *mm = dma->addr_space->mm; +int ret; +bool rsvd; + + ret =vaddr_get_pfn (mm, vaddr, prot, pfn_base); +if (ret) +return ret; + + rsvd =is_invalid_reserved_pfn (*pfn_base); + +if (!rsvd && !lock_cap && mm->locked_vm +1 > limit) { +put_pfn (*pfn_base, prot); +pr_warn ("%s: Task %s (%d) RLIMIT_MEMLOCK (%ld) exceeded\n" , + __func__, task->comm,task_pid_nr (task), + limit <<PAGE_SHIFT ); +return -ENOMEM ; + } + +if (!rsvd && do_accounting) +vfio_lock_acct (mm,1 ); + +return 1 ;drivers/vfio/vfio_iommu_type1.c static long __vfio_unpin_pages_remote(struct vfio_dma *dma, unsigned long pfn, · line 521
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10
-0+} + +static void __vfio_unpin_page_external (struct vfio_addr_space *addr_space, +unsigned long pfn,int prot, +bool do_accounting) +{ +put_pfn (pfn, prot); + +if (do_accounting) +vfio_lock_acct (addr_space->mm,-1 );Quoted 8 lines
Can't we batch this like we do elsewhere? Intel folks, AIUI you intend to pin all VM memory through this side channel, have you tested the scalability and performance of this with larger VMs? Our
vfio_pfndata structure alone is 40 bytes per pinned page, which means for each 1GB of VM memory, we have 10MBs worth of structvfio_pfn! Additionally, unmapping each 1GB of VM memory will result in 256k separatevfio_lock_acct()callbacks. I'm concerned that we're not being efficient enough in either space or time.Hi Alex,
Quoted 7 lines
Sorry for being confusing, Intel vGPU actually doesn't necessarily need to pin all guest memory. A vGPU has its page table (GTT), whose access is trapped. Whenever guest driver wants to specify a page for DMA, it writes the GTT entry - thereby we could know the event and pin that page only.
Performance data will be shared once available. Thanks :)
Ok, so maybe if none of the initial users are mapping full VM memory then scalability can be saved for future optimization. Thanks,
Alex
Kirti Wankhede
maintainerRe: [PATCH v11 11/22] vfio iommu: Add blocking notifier to notify DMA_UNMAP
In reply to Alex WilliamsonQuoted 3 lines · Alex Williamson
On Sat, 5 Nov 2016 02:40:45 +0530
Kirti Wankhede <[email protected]> wrote:
...
Quoted 8 lines
Quoted 2 lines
+int
vfio_register_notifier(struct device *dev, struct notifier_block *nb)Is the expectation here that this is a generic notifier for all
vfio->mdevsignaling? That should probably be made clear in the mdev API to avoid vendor drivers assuming their notifier callback only occurs for unmaps, even if that's currently the case.
Ok. Adding comment about notifier callback in mdev_device which is part of next patch.
...
Quoted 12 lines
Quoted 12 lines
drivers/vfio/vfio_iommu_type1.c +2
-1mutex_lock (&iommu->lock); -if (!iommu->external_domain) { +/* Fail if notifier list is empty */ +if ((!iommu->external_domain) || (!iommu->notifier.head)) { ret = -EINVAL ; goto pin_done; }@@ -867,6 +870,11 @@ unlock:/* Report how much was unmapped */ unmap->size = unmapped;
Quoted 7 lines
drivers/vfio/vfio_iommu_type1.c unlock: · line 876
Quoted 4 lines
drivers/vfio/vfio_iommu_type1.c +4
-0+if (unmapped && iommu->external_domain) +blocking_notifier_call_chain (&iommu->notifier, +VFIO_IOMMU_NOTIFY_DMA_UNMAP , + unmap);This is after the fact, there's already a gap here where pages are unpinned and the mdev device is still running.
Oh, there is a bug here, now unpin_pages() take user_pfn as argument and find vfio_dma. If its not found, it doesn't unpin pages. We have to call this notifier before vfio_remove_dma(). But if we call this before vfio_remove_dma() there will be deadlock since iommu->lock is already held here and vfio_iommu_type1_unpin_pages() will also try to hold iommu->lock.
If we want to call blocking_notifier_call_chain() before vfio_remove_dma(), sequence should be:
unmapped += dma->size;mutex_unlock(&iommu->lock);
if (iommu->external_domain)) {
struct vfio_iommu_type1_dma_unmap nb_unmap;
nb_unmap.iova = dma->iova;
nb_unmap.size = dma->size;
blocking_notifier_call_chain(&iommu->notifier,
VFIO_IOMMU_NOTIFY_DMA_UNMAP,
&nb_unmap);
}mutex_lock(&iommu->lock);vfio_remove_dma(iommu, dma);
Quoted 5 lines
The notifier needs to
happen prior to that and I suspect that we need to validate that we have no remaining external pfn references within thisvfio_dmablock. It seems like we need to root our pfn tracking in thevfio_dmaso that we can see that it's empty after the notifier chain andBUG_ONif not.
There is no way to find pfns from that iova range with current implementation. We can have this validate if we go with linear array of iova to track pfns.
Quoted 4 lines
I would also add some enforcement that external pinning is only enabled when
vfio_iommu_type1is configured for v2 semantics (ie. we only support unmaps exactly matching previous maps).
Ok I'll add that check.
Thanks,
Kirti
Re: [PATCH v11 05/22] vfio iommu: Added pin and unpin callback functions to vfio_iommu_driver_ops
In reply to Kirti WankhedeQuoted 38 lines · On Tue, 8 Nov 2016 19:25:35 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 3 lines · Alex Williamson
On Sat, 5 Nov 2016 02:40:39 +0530
Kirti Wankhede <[email protected]> wrote:...
Quoted 23 lines
Quoted 23 lines
drivers/vfio/vfio.c +23
-0+int vfio_pin_pages (struct device *dev,unsigned long *user_pfn, +int npage,int prot,unsigned long *phys_pfn) +{ +struct vfio_container *container; +struct vfio_group *group; +struct vfio_iommu_driver *driver; +int ret; + +if (!dev || !user_pfn || !phys_pfn) +return -EINVAL ; + + group =vfio_group_get_from_dev (dev); +if (IS_ERR (group)) +return PTR_ERR (group); + + ret =vfio_group_add_container_user (group); +if (ret) + goto err_pin_pages; + + container = group->container; +down_read (&container->group_lock); + + driver = container->iommu_driver;Quoted 9 lines
drivers/vfio/vfio.c void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset) · line 1840
Quoted 5 lines
drivers/vfio/vfio.c +5
-0+if (likely (driver && driver->ops->pin_pages)) + ret = driver->ops->pin_pages (container->iommu_data, user_pfn, + npage, prot, phys_pfn); +else + ret = -EINVAL ;
-ENOTTYmight be a more appropriate error return here and below since we're not signaling invalid argument, we're signaling lack of support.
Quoted 2 lines
Used
-EINVALin sync with otherdriver->opslike read, write and mmap. Changing it to-ENOTTYas you suggested above since these ops are optional.
TBH, I'm not sure which is better, but it's nice to try to differentiate different error paths with different errno values.
Quoted 28 lines
...
Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c static int vaddr_get_pfn(unsigned long vaddr, int prot, unsigned long *pfn) · line 263
Quoted 4 lines
drivers/vfio/vfio_iommu_type1.c +2
-2-static long vfio_pin_pages (unsigned long vaddr,long npage, -int prot,unsigned long *pfn_base) +static long __vfio_pin_pages_remote (unsigned long vaddr,long npage, +int prot,unsigned long *pfn_base)nit, what is the additional underscore prefix intended to imply? Appending
_remoteis sufficient to avoid the symbol conflict.This function name changed in review process from start, we started with changing to
__vfio_pin_pagesand then added_remoteto it later. We can remove '__' from it. Updating....
Quoted 10 lines
include/linux/vfio.h struct vfio_iommu_driver_ops { · line 81
Quoted 5 lines
include/linux/vfio.h +4
-1- +int (*pin_pages)(void *iommu_data,unsigned long *user_pfn, +int npage,int prot, +unsigned long *phys_pfn); + int (*unpin_pages)(void *iommu_data,Are we changing from long to int here simply because of the absurdity in passing in more than a 2^31 entry array, that would already consume more than 16GB itself?
Quoted 2 lines
These are on demand pin/unpin request, will that request go beyond 16GB limit? For Nvidia vGPU solution, pin request will not go beyond this limit.
16G is simply the size of the user_pfn or phys_pfn arrays at a maximal int32_t npage value, the interface actually allows mapping up to 8TB per call, but at that point we have 16GB of input, 16GB of output, and 80GB of vfio_pfns created. So I don't really have a problem changing form long to int given lack of scalability in the API in general, but it does make me second guess the API itself. Thanks,
Alex
Quoted 29 lines
Quoted 5 lines
include/linux/vfio.h struct vfio_iommu_driver_ops { · line 83
Quoted 2 lines
include/linux/vfio.h +2
-0+unsigned long *user_pfn, + unsigned long *pfn,nit, use
phys_pfnso as to match the pin function?Ok.
Quoted 8 lines
Quoted 8 lines
include/linux/vfio.h +1
-0+ int npage); };extern int vfio_register_iommu_driver (const struct vfio_iommu_driver_ops *ops);@@ -127,6 +133,12 @@ static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group, }#endif /* CONFIG_EEH */ Quoted 10 lines
include/linux/vfio.h static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group, · line 144
Quoted 9 lines
include/linux/vfio.h +6
-0+extern int vfio_pin_pages (struct device *dev,unsigned long *user_pfn, +int npage,int prot,unsigned long *phys_pfn); + +extern int vfio_unpin_pages (struct device *dev,unsigned long *user_pfn, +unsigned long *pfn,int npage); +/* * IRQfd - generic */ Thanks,
Kirti
Re: [PATCH v11 09/22] vfio iommu type1: Add task structure to vfio_dma
In reply to Kirti WankhedeQuoted 41 lines · On Tue, 8 Nov 2016 19:43:25 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 3 lines · Alex Williamson
On Sat, 5 Nov 2016 02:40:43 +0530
Kirti Wankhede <[email protected]> wrote:...
Quoted 20 lines
Quoted 20 lines
static int vfio_dma_do_map(struct
vfio_iommu*iommu,drivers/vfio/vfio_iommu_type1.c +3
-2struct vfio_iommu_type1_dma_map *map) {dma_addr_t iova = map->iova;unsigned long vaddr = map->vaddr;size_t size = map->size; -long npage;int ret =0 , prot =0 ;uint64_t mask;struct vfio_dma *dma; -unsigned long pfn; +struct vfio_addr_space *addr_space; +struct mm_struct *mm; +bool free_addr_space_on_err = false;/* Verify that none of our __u64 fields overflow */ if (map->size != size || map->vaddr != vaddr || map->iova != iova)@@ -608,47 +685,56 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu,mutex_lock (&iommu->lock);Quoted 13 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_dma_do_map(struct vfio_iommu *iommu, · line 694
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +7
-2if (vfio_find_dma (iommu, iova, size)) { -mutex_unlock (&iommu->lock); -return -EEXIST ; + ret = -EEXIST ; + goto do_map_err; + } + + mm =get_task_mm (current); +if (!mm) { + ret = -ENODEV ;
-EFAULT?
Quoted 8 lines
-ENODEVreturn is in original code fromvfio_pin_pages()if (!current->mm) return -ENODEV;Once I thought of changing it to
-EFAULT, but then again changed to-ENODEVto be consistent with original error code.Should I still change this return to
-EFAULT?
Let's keep ENODEV for less code churn, I guess.
Quoted 49 lines
Quoted 32 lines
Quoted 32 lines
drivers/vfio/vfio_iommu_type1.c +24
-2+ goto do_map_err; + } + + addr_space =vfio_find_addr_space (iommu, mm); +if (addr_space) { +atomic_inc (&addr_space->ref_count); +mmput (mm); + }else { + addr_space =kzalloc (sizeof (*addr_space),GFP_KERNEL ); +if (!addr_space) { + ret = -ENOMEM ; + goto do_map_err; + } + addr_space->mm = mm; +atomic_set (&addr_space->ref_count,1 ); +list_add (&addr_space->next, &iommu->addr_space_list); + free_addr_space_on_err = true; } dma =kzalloc (sizeof (*dma),GFP_KERNEL );if (!dma) { -mutex_unlock (&iommu->lock); -return -ENOMEM ; +if (free_addr_space_on_err) { +mmput (mm); +list_del (&addr_space->next); +kfree (addr_space); + } + ret = -ENOMEM ; + goto do_map_err; }Quoted 16 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_dma_do_map(struct vfio_iommu *iommu, · line 731
Quoted 7 lines
drivers/vfio/vfio_iommu_type1.c +4
-0dma->iova = iova; dma->vaddr = vaddr; dma->prot = prot; + dma->addr_space = addr_space; +get_task_struct (current); + dma->task = current; + dma->mlock_cap =capable (CAP_IPC_LOCK );How do you reason we can cache this? Does the fact that the process had this capability at the time that it did a
DMA_MAPimply that it necessarily still has this capability when an external user (vendor driver) tries to pin pages? I don't see how we can make that assumption.
Quoted 2 lines
Will process change MEMLOCK limit at runtime? I think it shouldn't, correct me if I'm wrong. QEMU doesn't do that, right?
What QEMU does or doesn't do isn't relevant, the question is could a process change CAP_IPC_LOCK runtime. It seems plausible to me.
Quoted 9 lines
The function
capable()determines current task's capability. But whenvfio_pin_pages()is called, it could come from other task but pages are pinned from address space of task who mapped it. So we can't usecapable()invfio_pin_pages()If this capability shouldn't be cached, we have to use
has_capability()withdma->taskas argument invfio_pin_pages()bool
has_capability(struct task_struct *t, int cap)
Yep, that sounds better. Thanks,
Alex
Re: [PATCH v11 10/22] vfio iommu type1: Add support for mediated devices
In reply to Kirti WankhedeQuoted 62 lines · On Tue, 8 Nov 2016 20:36:34 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 3 lines · Alex Williamson
On Sat, 5 Nov 2016 02:40:44 +0530
Kirti Wankhede <[email protected]> wrote:...
Quoted 50 lines
Quoted 30 lines
drivers/vfio/vfio_iommu_type1.c +29
-1-static void vfio_unmap_unpin (struct vfio_iommu *iommu,struct vfio_dma *dma) +static int __vfio_pin_page_external (struct vfio_dma *dma,unsigned long vaddr, +int prot,unsigned long *pfn_base, +bool do_accounting) +{ +struct task_struct *task = dma->task; +unsigned long limit =task_rlimit (task,RLIMIT_MEMLOCK ) >>PAGE_SHIFT ; +bool lock_cap = dma->mlock_cap; +struct mm_struct *mm = dma->addr_space->mm; +int ret; +bool rsvd; + + ret =vaddr_get_pfn (mm, vaddr, prot, pfn_base); +if (ret) +return ret; + + rsvd =is_invalid_reserved_pfn (*pfn_base); + +if (!rsvd && !lock_cap && mm->locked_vm +1 > limit) { +put_pfn (*pfn_base, prot); +pr_warn ("%s: Task %s (%d) RLIMIT_MEMLOCK (%ld) exceeded\n" , + __func__, task->comm,task_pid_nr (task), + limit <<PAGE_SHIFT ); +return -ENOMEM ; + } + +if (!rsvd && do_accounting) +vfio_lock_acct (mm,1 ); + +return 1 ;drivers/vfio/vfio_iommu_type1.c static long __vfio_unpin_pages_remote(struct vfio_dma *dma, unsigned long pfn, · line 521
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10
-0+} + +static void __vfio_unpin_page_external (struct vfio_addr_space *addr_space, +unsigned long pfn,int prot, +bool do_accounting) +{ +put_pfn (pfn, prot); + +if (do_accounting) +vfio_lock_acct (addr_space->mm,-1 );Can't we batch this like we do elsewhere? Intel folks, AIUI you intend to pin all VM memory through this side channel, have you tested the scalability and performance of this with larger VMs? Our
vfio_pfndata structure alone is 40 bytes per pinned page, which means for each 1GB of VM memory, we have 10MBs worth of structvfio_pfn! Additionally, unmapping each 1GB of VM memory will result in 256k separatevfio_lock_acct()callbacks. I'm concerned that we're not being efficient enough in either space or time.Quoted 5 lines
One thought might be whether we really need to save the pfn, we better always get the same result if we pin it again, or maybe we can just do a lookup through the mm at that point without re-pinning. Could we get to the point where we only need an
atomic_tref count per page in a linear array relative to the IOVA?
Quoted 4 lines
Ok. Is System RAM hot-plug supported? How is system RAM hot-plug handled? Are there
DMA_MAPcalls on such hot-plug for additional range? If we have a linear array/memory, we will have to realloc it on memory hot-plug?
I was thinking a linear array for each IOVA page within a vfio_dma. The array would track the number of references (pins) of each page. It might actually need to be a page table given that a single vfio_dma can nearly map the entire 64bit address space. I don't think RAM hotplug is a factor here, we need to support and properly account for multiple IOVAs mapping to the same pfn, but the typical case will be a 1:1 mapping, I think that's what we'd optimize for.
Quoted 15 lines
Quoted 11 lines
That would give us 1MB per 1GB
overhead. The semantics of the pin and unpin would make more sense then too, both would take an IOVA range, only pinning would need a return mechanism. For instance:int pin_pages(void
*iommu_data,dma_addr_tiova_base,int npage, unsigned long *pfn_base);This would pin physically contiguous pages up to npage, returning the base pfn and returning the number of pages pinned (<= npage). The vendor driver would make multiple calls to fill the necessary range.
With the current patch, input is user_pfn[] array and npages.
Quoted 7 lines
int vfio_pin_pages(struct device *dev, unsigned long
*user_pfn,
int npage, int prot, unsigned long*phys_pfn)When guest allocates memory with
malloc(), gfns would not be contiguous, right? These gfns (user_pfns) are passed as argument here.
Is there any case where we could get pin/unpin request for contiguous pages?
It would depend on whether the user within the guest is actually optimizing for hugepages.
Quoted 11 lines
Quoted 8 lines
Unpin would then simply be:
void
unpin_pages(void *iommu_data, dma_addr_t iova_base, int npage);Hugepage usage would really make such an interface shine (ie. 2MB+ contiguous ranges). A downside would be the overhead of getting the group and container reference in vfio for each callback, perhaps we'd need to figure out how the vendor driver could hold that reference.
In very initial phases of proposal, I had suggested to keep pointer to
container->iommu_datain structmdev_device. But that was discarded.
The referencing is definitely tricky, we run into the same problem that Alexey had in trying to do that where holding a reference to the container doesn't actually keep the container. The user can tear down the container or remove groups from the container, which automatically removes the iommu backend.
Quoted 26 lines
Quoted 3 lines
The current API of passing around pfn arrays, further increasing the overhead of the whole ecosystem just makes me cringe though.
...
Quoted 10 lines
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10
-0+if (ret <=0 ) { +WARN_ON (!ret); + goto pin_unwind; + } + +mutex_lock (&dma->addr_space->pfn_list_lock); + +/* search if pfn exist */ + p =vfio_find_pfn (dma->addr_space, pfn[i]); +if (p) {Quoted 9 lines
drivers/vfio/vfio_iommu_type1.c static long __vfio_unpin_pages_remote(struct vfio_dma *dma, unsigned long pfn, · line 591
Quoted 1 line
drivers/vfio/vfio_iommu_type1.c +1
-0+atomic_inc (&p->ref_count);We never test whether (
p->prot== prot), shouldn't we be doing something in that case? In fact, why do we allow the side-channel through the .{un}pin_pages to specify page protection flags that might be different than the user specified for theDMA_MAP? If the user specified read-only, the vendor driver should not be allowed to override with read-write access.
Quoted 7 lines
If user specified protection flags for
DMA_MAPcould be prot =IOMMU_WRITE|IOMMU_READ;But vendor driver can request to pin page to be readonly, i.e.
IOMMU_READ. In that case, pin pages should be allowed, right?Then the check should be if (
p->prot& prot).
That doesn't solve the problem, we might have an existing pfn with prot R and we're asking for RW, (R & RW) is true, but that's not what we asked for. I think the solution would be to test the vendor driver requested prot against vfio_dma.prot, but always pin the pages using the vfio_dma.prot flags. Thus if the vendor driver asks for read-only and the vfio_dma.prot is read-write, these are compatible and the page is pinned read-write since that's the user mapping. Otherwise you need to save prot per page (which you already do), but you're also going to need to handle promoting pages if we have multiple vendors asking for different prot attributes. Thanks,
Alex
Re: [PATCH v11 11/22] vfio iommu: Add blocking notifier to notify DMA_UNMAP
In reply to Kirti WankhedeQuoted 59 lines · On Tue, 8 Nov 2016 21:56:29 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 3 lines · Alex Williamson
On Sat, 5 Nov 2016 02:40:45 +0530
Kirti Wankhede <[email protected]> wrote:...
Quoted 8 lines
Quoted 2 lines
+int
vfio_register_notifier(struct device *dev, struct notifier_block *nb)Is the expectation here that this is a generic notifier for all
vfio->mdevsignaling? That should probably be made clear in the mdev API to avoid vendor drivers assuming their notifier callback only occurs for unmaps, even if that's currently the case.Ok. Adding comment about notifier callback in
mdev_devicewhich is part of next patch....
Quoted 12 lines
Quoted 12 lines
drivers/vfio/vfio_iommu_type1.c +2
-1mutex_lock (&iommu->lock); -if (!iommu->external_domain) { +/* Fail if notifier list is empty */ +if ((!iommu->external_domain) || (!iommu->notifier.head)) { ret = -EINVAL ; goto pin_done; }@@ -867,6 +870,11 @@ unlock:/* Report how much was unmapped */ unmap->size = unmapped;Quoted 7 lines
drivers/vfio/vfio_iommu_type1.c unlock: · line 876
Quoted 4 lines
drivers/vfio/vfio_iommu_type1.c +4
-0+if (unmapped && iommu->external_domain) +blocking_notifier_call_chain (&iommu->notifier, +VFIO_IOMMU_NOTIFY_DMA_UNMAP , + unmap);This is after the fact, there's already a gap here where pages are unpinned and the mdev device is still running.
Oh, there is a bug here, now
unpin_pages()takeuser_pfnas argument and findvfio_dma. If its not found, it doesn't unpin pages. We have to call this notifier beforevfio_remove_dma(). But if we call this beforevfio_remove_dma()there will be deadlock sinceiommu->lockis already held here andvfio_iommu_type1_unpin_pages()will also try to holdiommu->lock.
If we want to callblocking_notifier_call_chain()beforevfio_remove_dma(), sequence should be:unmapped +=
dma->size;mutex_unlock(&iommu->lock);if (iommu->external_domain)) { struct vfio_iommu_type1_dma_unmap nb_unmap; nb_unmap.iova = dma->iova; nb_unmap.size = dma->size; blocking_notifier_call_chain(&iommu->notifier, VFIO_IOMMU_NOTIFY_DMA_UNMAP, &nb_unmap); }
Quoted 2 lines
mutex_lock(&iommu->lock);vfio_remove_dma(iommu, dma);
It seems like it would be worthwhile to have the rb-tree rooted in the vfio-dma, then we only need to call the notifier if there are pages pinned within that vfio-dma (ie. the rb-tree is not empty). We can then release the lock call the notifier, re-acquire the lock, and BUG_ON if the rb-tree still is not empty. We might get duplicate pfns between separate vfio_dma structs, but as I mentioned in other replies, that seems like an exception that we don't need to optimize for.
Quoted 9 lines
Quoted 5 lines
The notifier needs to
happen prior to that and I suspect that we need to validate that we have no remaining external pfn references within thisvfio_dmablock. It seems like we need to root our pfn tracking in thevfio_dmaso that we can see that it's empty after the notifier chain andBUG_ONif not.There is no way to find pfns from that iova range with current implementation. We can have this validate if we go with linear array of iova to track pfns.
Right, I was still hoping to avoid storing the pfn even with the array/page-table approach though, ask the mm layer for the mapping again. Is that too much overhead? Maybe the page table could store the phys addr and we could use PAGE_MASK to store the reference count so that each entry is still only 8bytes(?)
Quoted 9 lines
Quoted 4 lines
I would also add some enforcement that external pinning is only enabled when
vfio_iommu_type1is configured for v2 semantics (ie. we only support unmaps exactly matching previous maps).Ok I'll add that check.
Thanks,
Kirti
Kirti Wankhede
maintainerRe: [PATCH v11 05/22] vfio iommu: Added pin and unpin callback functions to vfio_iommu_driver_ops
In reply to Alex WilliamsonQuoted 3 lines · Alex Williamson
On Tue, 8 Nov 2016 19:25:35 +0530
Kirti Wankhede <[email protected]> wrote:
...
Quoted 14 lines
Quoted 11 lines
Quoted 10 lines
include/linux/vfio.h struct vfio_iommu_driver_ops { · line 81
Quoted 5 lines
include/linux/vfio.h +4
-1- +int (*pin_pages)(void *iommu_data,unsigned long *user_pfn, +int npage,int prot, +unsigned long *phys_pfn); + int (*unpin_pages)(void *iommu_data,Are we changing from long to int here simply because of the absurdity in passing in more than a 2^31 entry array, that would already consume more than 16GB itself?
Quoted 2 lines
These are on demand pin/unpin request, will that request go beyond 16GB limit? For Nvidia vGPU solution, pin request will not go beyond this limit.
Quoted 7 lines
16G is simply the size of the
user_pfnorphys_pfnarrays at a maximalint32_tnpage value, the interface actually allows mapping up to 8TB per call, but at that point we have 16GB of input, 16GB of output, and 80GB ofvfio_pfnscreated. So I don't really have a problem changing form long to int given lack of scalability in the API in general, but it does make me second guess the API itself. Thanks,
Changing to 'long', in future we might enhance this API without changing it signature.
Thanks,
Kirti
Re: [PATCH v11 05/22] vfio iommu: Added pin and unpin callback functions to vfio_iommu_driver_ops
Quoted 28 lines · On Wed, 9 Nov 2016 00:17:53 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 3 lines · Alex Williamson
On Tue, 8 Nov 2016 19:25:35 +0530
Kirti Wankhede <[email protected]> wrote:...
Quoted 14 lines
Quoted 11 lines
Quoted 10 lines
include/linux/vfio.h struct vfio_iommu_driver_ops { · line 81
Quoted 5 lines
include/linux/vfio.h +4
-1- +int (*pin_pages)(void *iommu_data,unsigned long *user_pfn, +int npage,int prot, +unsigned long *phys_pfn); + int (*unpin_pages)(void *iommu_data,Are we changing from long to int here simply because of the absurdity in passing in more than a 2^31 entry array, that would already consume more than 16GB itself?
Quoted 2 lines
These are on demand pin/unpin request, will that request go beyond 16GB limit? For Nvidia vGPU solution, pin request will not go beyond this limit.
Quoted 7 lines
16G is simply the size of the
user_pfnorphys_pfnarrays at a maximalint32_tnpage value, the interface actually allows mapping up to 8TB per call, but at that point we have 16GB of input, 16GB of output, and 80GB ofvfio_pfnscreated. So I don't really have a problem changing form long to int given lack of scalability in the API in general, but it does make me second guess the API itself. Thanks,
Quoted 2 lines
Changing to 'long', in future we might enhance this API without changing it signature.
I think the pfn arrays are more of a problem long term than whether we can only map 2^31 pfns in one call. I particularly dislike that the caller provides both the iova and pfn arrays for unpinning. Being an in-kernel driver, we should trust it, but it makes the interface difficult to use and seems like it indicates that our tracking data structures aren't architected the way they should be. Upstream, this API will need to be flexible and change over time, it's only the downstream distros that may lock in a kABI. Not breaking them should be a consideration, but also needs to be weighted against long term upstream goals. Thanks,
Alex
Kirti Wankhede
maintainerRe: [PATCH v11 11/22] vfio iommu: Add blocking notifier to notify DMA_UNMAP
In reply to Alex WilliamsonQuoted 65 lines · Alex Williamson
Quoted 59 lines · On Tue, 8 Nov 2016 21:56:29 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 3 lines · Alex Williamson
On Sat, 5 Nov 2016 02:40:45 +0530
Kirti Wankhede <[email protected]> wrote:...
Quoted 8 lines
Quoted 2 lines
+int
vfio_register_notifier(struct device *dev, struct notifier_block *nb)Is the expectation here that this is a generic notifier for all
vfio->mdevsignaling? That should probably be made clear in the mdev API to avoid vendor drivers assuming their notifier callback only occurs for unmaps, even if that's currently the case.Ok. Adding comment about notifier callback in
mdev_devicewhich is part of next patch....
Quoted 12 lines
Quoted 12 lines
drivers/vfio/vfio_iommu_type1.c +2
-1mutex_lock (&iommu->lock); -if (!iommu->external_domain) { +/* Fail if notifier list is empty */ +if ((!iommu->external_domain) || (!iommu->notifier.head)) { ret = -EINVAL ; goto pin_done; }@@ -867,6 +870,11 @@ unlock:/* Report how much was unmapped */ unmap->size = unmapped;Quoted 7 lines
drivers/vfio/vfio_iommu_type1.c unlock: · line 876
Quoted 4 lines
drivers/vfio/vfio_iommu_type1.c +4
-0+if (unmapped && iommu->external_domain) +blocking_notifier_call_chain (&iommu->notifier, +VFIO_IOMMU_NOTIFY_DMA_UNMAP , + unmap);This is after the fact, there's already a gap here where pages are unpinned and the mdev device is still running.
Oh, there is a bug here, now
unpin_pages()takeuser_pfnas argument and findvfio_dma. If its not found, it doesn't unpin pages. We have to call this notifier beforevfio_remove_dma(). But if we call this beforevfio_remove_dma()there will be deadlock sinceiommu->lockis already held here andvfio_iommu_type1_unpin_pages()will also try to holdiommu->lock.
If we want to callblocking_notifier_call_chain()beforevfio_remove_dma(), sequence should be:unmapped +=
dma->size;mutex_unlock(&iommu->lock);if (iommu->external_domain)) { struct vfio_iommu_type1_dma_unmap nb_unmap; nb_unmap.iova = dma->iova; nb_unmap.size = dma->size; blocking_notifier_call_chain(&iommu->notifier, VFIO_IOMMU_NOTIFY_DMA_UNMAP, &nb_unmap); }Quoted 2 lines
mutex_lock(&iommu->lock);vfio_remove_dma(iommu, dma);
Quoted 8 lines
It seems like it would be worthwhile to have the rb-tree rooted in the vfio-dma, then we only need to call the notifier if there are pages pinned within that vfio-dma (ie. the rb-tree is not empty). We can then release the lock call the notifier, re-acquire the lock, and
BUG_ONif the rb-tree still is not empty. We might get duplicate pfns between separatevfio_dmastructs, but as I mentioned in other replies, that seems like an exception that we don't need to optimize for.
If we don't optimize for the case where iova from different vfio_dma are mapped to same pfn and we would not consider this case for page accounting then:
- have rb tree of pinned iova, where key would be iova, in each vfio_dma structure.
- iova tracking structure would have iova and ref_count only.
- page accounting would only count number of iova's in rb_tree, case where different iova could map to same pfn would not be considered in this implementation for now.
- vfio_unpin_pages() would have user_pfn and pfn as input, we would validate that iova exist in rb tree and trust vendor driver that corresponding pfn is correct, there is no validation of pfn. If want validate pfn, call GUP, verify pfn and call put_pfn().
- In .release() or .detach_group() path, if there are entries in this rb tree, call GUP again using that iova, get pfn and then call put_pfn(pfn) for ref_count+1 times. This is because we are not keeping pfn in our tracking logic.
Does this sound reasonable?
Thanks,
Kirti
Quoted 26 lines
Quoted 9 lines
Quoted 5 lines
The notifier needs to
happen prior to that and I suspect that we need to validate that we have no remaining external pfn references within thisvfio_dmablock. It seems like we need to root our pfn tracking in thevfio_dmaso that we can see that it's empty after the notifier chain andBUG_ONif not.There is no way to find pfns from that iova range with current implementation. We can have this validate if we go with linear array of iova to track pfns.
Right, I was still hoping to avoid storing the pfn even with the array/page-table approach though, ask the mm layer for the mapping again. Is that too much overhead? Maybe the page table could store the phys addr and we could use
PAGE_MASKto store the reference count so that each entry is still only 8bytes(?)Quoted 9 lines
Quoted 4 lines
I would also add some enforcement that external pinning is only enabled when
vfio_iommu_type1is configured for v2 semantics (ie. we only support unmaps exactly matching previous maps).Ok I'll add that check.
Thanks,
Kirti
Kirti Wankhede
maintainerRe: [PATCH v11 15/22] vfio: Introduce vfio_set_irqs_validate_and_prepare()
In reply to Alexey KardashevskiyQuoted 31 lines · Alexey Kardashevskiy
Quoted 30 lines · Kirti Wankhede
Vendor driver using mediated device framework would use same mechnism to validate and prepare IRQs. Introducing this function to reduce code replication in multiple drivers.
Signed-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id Ie201f269dda0713ca18a07dc4852500bd8b48309--- drivers/vfio/vfio.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/vfio.h | 4 ++++ 2 files changed, 52 insertions(+)drivers/vfio/vfio.c +10
-0@@ -1858,6 +1858,54 @@ int vfio_info_add_capability(struct vfio_info_cap *caps, int cap_type_id, }EXPORT_SYMBOL (vfio_info_add_capability); +int vfio_set_irqs_validate_and_prepare (struct vfio_irq_set *hdr,int num_irqs, +int max_irq_type,size_t *data_size) +{ +unsigned long minsz; +size_t size; + + minsz = offsetofend(struct vfio_irq_set, count); + + if ((hdr->argsz < minsz) || (hdr->index >= max_irq_type) || + (hdr->count >= (U32_MAX - hdr->start)) ||
Quoted 9 lines
drivers/vfio/vfio.c int vfio_info_add_capability(struct vfio_info_cap *caps, int cap_type_id, · line 1875
Quoted 5 lines
drivers/vfio/vfio.c +5
-0+ (hdr->flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK | + VFIO_IRQ_SET_ACTION_TYPE_MASK))) + return -EINVAL; + +if (data_size)Pointless check, the callers will pass non null pointer with value initialized to 0 anyway.
Not always, When VFIO_IRQ_SET_DATA_NONE flag is set, caller can pass data_size = NULL.
Quoted 15 lines
Quoted 15 lines
drivers/vfio/vfio.c +14
-0+ *data_size =0 ; + +if (hdr->start >= num_irqs || hdr->start + hdr->count > num_irqs) +return -EINVAL ; + +switch (hdr->flags &VFIO_IRQ_SET_DATA_TYPE_MASK ) { +case VFIO_IRQ_SET_DATA_NONE : + size =0 ; +break ; +case VFIO_IRQ_SET_DATA_BOOL : + size =sizeof (uint8_t ); +break ; +case VFIO_IRQ_SET_DATA_EVENTFD : + size =sizeof (int32_t );
Quoted 9 lines
drivers/vfio/vfio.c int vfio_info_add_capability(struct vfio_info_cap *caps, int cap_type_id, · line 1895
Quoted 6 lines
drivers/vfio/vfio.c +6
-0+break ; +default : +return -EINVAL ; + } + +if (size) {The whole branch would even work for size == 0.
In that case below check (!data_size) might result in error if data_size == NULL, whereas its not error case when size == 0, i.e. VFIO_IRQ_SET_DATA_NONE flag set.
Quoted 8 lines
drivers/vfio/vfio.c int vfio_info_add_capability(struct vfio_info_cap *caps, int cap_type_id, · line 1900
Quoted 5 lines
drivers/vfio/vfio.c +5
-0+if (hdr->argsz - minsz < hdr->count * size) +return -EINVAL ; + +if (!data_size) +return -EINVAL ;Redundant check as well.
This is not redundant. If you see above check, it sets its init value to 0 but doesn't fail.
Quoted 10 lines
drivers/vfio/vfio.c int vfio_info_add_capability(struct vfio_info_cap *caps, int cap_type_id, · line 1906
Quoted 6 lines
drivers/vfio/vfio.c +6
-0+ + *data_size = hdr->count * size; + } + +return 0 ; +}It does not really prepare anything as the name suggests. It looks like this is 2 different helpers actually:
Quoted 4 lines
int
vfio_set_irqs_validate()
andsize_tvfio_set_irqs_hdr_to_data_size()
Later one is the prepare.
Quoted 5 lines
And it would make it easier to review/bisect if 16/22 and 17/22 were merged into this one as this patch alone adds new code which it does not use and all 3 patches are fairly small.
I do had all 3 patch merged in one in earlier version of patchset. This is split as per Alex's suggestion.
Quoted 7 lines
drivers/vfio/vfio.c int vfio_info_add_capability(struct vfio_info_cap *caps, int cap_type_id, · line 1907
Quoted 2 lines
drivers/vfio/vfio.c +1
-0+EXPORT_SYMBOL (vfio_set_irqs_validate_and_prepare);Everything you export in this patchset is
EXPORT_SYMBOL()while the existing code usesEXPORT_SYMBOL_GPL(), is this for a reason?
We want these symbols to be available to all drivers.
Thanks,
Kirti
Quoted 22 lines
Quoted 20 lines
drivers/vfio/vfio.c +1
-0+/* * Pin a set of guest PFNs and return their associated host PFNs for local * domain only. include/linux/vfio.h +4
-0@@ -116,6 +116,10 @@ extern void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);extern int vfio_info_add_capability (struct vfio_info_cap *caps,int cap_type_id,void *cap_type); +extern int vfio_set_irqs_validate_and_prepare (struct vfio_irq_set *hdr, +int num_irqs,int max_irq_type, +size_t *data_size); +struct pci_dev ;#ifdef CONFIG_EEH extern void vfio_spapr_pci_eeh_open (struct pci_dev *pdev);
Kirti Wankhede
maintainerRe: [PATCH v11 17/22] vfio_platform: Updated to use vfio_set_irqs_validate_and_prepare()
In reply to Alexey KardashevskiyQuoted 53 lines · Alexey Kardashevskiy
Quoted 39 lines · Kirti Wankhede
Updated
vfio_platform_common.cfile to usevfio_set_irqs_validate_and_prepare()Signed-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id Id87cd6b78ae901610b39bf957974baa6f40cd7b0--- drivers/vfio/platform/vfio_platform_common.c | 31 +++++++--------------------- 1 file changed, 8 insertions(+), 23 deletions(-)drivers/vfio/platform/vfio_platform_common.c +1
-14@@ -364,36 +364,21 @@ static long vfio_platform_ioctl(void *device_data,struct vfio_irq_set hdr;u8 *data =NULL ;int ret =0 ; +size_t data_size =0 ; minsz = offsetofend(struct vfio_irq_set, count);if (copy_from_user (&hdr, (void __user *)arg, minsz))return -EFAULT ; -if (hdr.argsz < minsz) -return -EINVAL ; - -if (hdr.index >= vdev->num_irqs) -return -EINVAL ; - -if (hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK | -VFIO_IRQ_SET_ACTION_TYPE_MASK )) -return -EINVAL ; - -if (!(hdr.flags &VFIO_IRQ_SET_DATA_NONE )) { -size_t size; - -if (hdr.flags &VFIO_IRQ_SET_DATA_BOOL )drivers/vfio/platform/vfio_platform_common.c static long vfio_platform_ioctl(void *device_data, · line 375
Quoted 10 lines
drivers/vfio/platform/vfio_platform_common.c +2
-8- size =sizeof (uint8_t ); - else if (hdr.flags & VFIO_IRQ_SET_DATA_EVENTFD) - size =sizeof (int32_t ); - else - return -EINVAL; - -if (hdr.argsz - minsz < size) -return -EINVAL ; + ret =vfio_set_irqs_validate_and_prepare (&hdr, vdev->num_irqs, + vdev->num_irqs, &data_size);The patch does not change this but I am still curious:
Quoted 5 lines
is not the second
vdev->num_irqssupposed to be one of VFIO_PCI_INTX_IRQ_INDEX..VFIO_PCI_NUM_IRQS, not the actual number of interrupt vectors (as in vfio-pci)?
Those are PCI specific. I don't think those counts are applicable here.
If you see the prototype, second argument and third argument have different meaning.
int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr, int num_irqs, int max_irq_type, size_t *data_size)
- num_irqs are number of irqs caller want to setup and
- max_irq_type is the one which is return to user in VFIO_DEVICE_GET_INFO ioctl's info.num_irqs.
For platform these two are same.
Thanks,
Kirti
Quoted 15 lines
Quoted 13 lines
drivers/vfio/platform/vfio_platform_common.c +5
-1+if (ret) +return ret; - data =memdup_user ((void __user *)(arg + minsz), size); +if (data_size) { + data =memdup_user ((void __user *)(arg + minsz), + data_size);if (IS_ERR (data))return PTR_ERR (data); }
Kirti Wankhede
maintainerRe: [PATCH v11 13/22] vfio: Introduce common function to add capabilities
In reply to Alexey KardashevskiyQuoted 13 lines · Alexey Kardashevskiy
Quoted 12 lines · Kirti Wankhede
Vendor driver using mediated device framework should use
vfio_info_add_capability()to add capabilities.
Introduced this function to reduce code duplication in vendor drivers.Signed-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id I6fca329fa2291f37a2c859d0bc97574d9e2ce1a6--- drivers/vfio/vfio.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++- include/linux/vfio.h | 3 +++ 2 files changed, 62 insertions(+), 1 deletion(-)
Quoted 15 lines
drivers/vfio/vfio.c void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset) · line 1800
Quoted 10 lines
drivers/vfio/vfio.c +1
-1@@ -1797,8 +1797,66 @@ void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset)for (tmp = caps->buf; tmp->next; tmp = (void *)tmp + tmp->next - offset) tmp->next += offset; } -EXPORT_SYMBOL_GPL (vfio_info_cap_shift); +EXPORT_SYMBOL (vfio_info_cap_shift);Why this change?
We want this symbol to be available to all drivers.
Quoted 70 lines
Quoted 70 lines
drivers/vfio/vfio.c +58
-0+static int sparse_mmap_cap (struct vfio_info_cap *caps,void *cap_type) +{ +struct vfio_info_cap_header *header; +struct vfio_region_info_cap_sparse_mmap *sparse_cap, *sparse = cap_type; +size_t size; + + size =sizeof (*sparse) + 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_cap =container_of (header, + struct vfio_region_info_cap_sparse_mmap, header); + sparse_cap->nr_areas = sparse->nr_areas; +memcpy (sparse_cap->areas, sparse->areas, + sparse->nr_areas *sizeof (*sparse->areas)); +return 0 ; +} + +static int region_type_cap (struct vfio_info_cap *caps,void *cap_type) +{ +struct vfio_info_cap_header *header; +struct vfio_region_info_cap_type *type_cap, *cap = cap_type; + + header =vfio_info_cap_add (caps,sizeof (*cap), +VFIO_REGION_INFO_CAP_TYPE ,1 ); +if (IS_ERR (header)) +return PTR_ERR (header); + + type_cap = container_of(header, struct vfio_region_info_cap_type, + header); + type_cap->type = cap->type; + type_cap->subtype = cap->subtype; +return 0 ; +} + +int vfio_info_add_capability (struct vfio_info_cap *caps,int cap_type_id, +void *cap_type) +{ +int ret = -EINVAL ; + +if (!cap_type) +return 0 ; + +switch (cap_type_id) { +case VFIO_REGION_INFO_CAP_SPARSE_MMAP : + ret =sparse_mmap_cap (caps, cap_type); +break ; + +case VFIO_REGION_INFO_CAP_TYPE : + ret =region_type_cap (caps, cap_type); +break ; + } + +return ret; +} +EXPORT_SYMBOL (vfio_info_add_capability);/* * Pin a set of guest PFNs and return their associated host PFNs for local include/linux/vfio.h +0
-0@@ -113,6 +113,9 @@ extern struct vfio_info_cap_header *vfio_info_cap_add( struct vfio_info_cap *caps, size_t size, u16 id, u16 version);extern void vfio_info_cap_shift (struct vfio_info_cap *caps,size_t offset);
Quoted 7 lines
include/linux/vfio.h extern struct vfio_info_cap_header *vfio_info_cap_add( · line 118
Quoted 3 lines
include/linux/vfio.h +3
-0+extern int vfio_info_add_capability (struct vfio_info_cap *caps, +int cap_type_id,void *cap_type); +It would make it easier to review and bisect if 14/22 was squashed into this one.
This was split based on Alex's suggestion on earlier version of this patchset.
Quoted 3 lines
In the resulting patch,
vfio_info_cap_add()can be made static as it will only be used indrivers/vfio/vfio.cfrom now.
Not sure that vfio_info_cap_add() should be made static. If there are any other drivers using this symbol outside kernel might break with that change.
Thanks,
Kirti
Quoted 9 lines
Quoted 7 lines
include/linux/vfio.h +0
-0struct pci_dev ;#ifdef CONFIG_EEH extern void vfio_spapr_pci_eeh_open (struct pci_dev *pdev);
Kirti Wankhede
maintainerRe: [PATCH v11 01/22] vfio: Mediated device Core driver
In reply to Dong Jia ShiQuoted 31 lines · Dong Jia Shi
* Kirti Wankhede <[email protected]> [2016-11-05 02:40:35 +0530]:
Hi Kirti,
[...]
Quoted 26 lines
drivers/vfio/Kconfig +1
-0@@ -48,4 +48,5 @@ menuconfig VFIO_NOIOMMU source "drivers/vfio/pci/Kconfig" source "drivers/vfio/platform/Kconfig" +source "drivers/vfio/mdev/Kconfig" source "virt/lib/Kconfig"drivers/vfio/Makefile +1
-0@@ -7,3 +7,4 @@ obj-$(CONFIG_VFIO_IOMMU_SPAPR_TCE) += vfio_iommu_spapr_tce.o obj-$(CONFIG_VFIO_SPAPR_EEH) += vfio_spapr_eeh.o obj-$(CONFIG_VFIO_PCI) += pci/ obj-$(CONFIG_VFIO_PLATFORM) += platform/ +obj-$(CONFIG_VFIO_MDEV) += mdev/drivers/vfio/mdev/Kconfig new file +1
-0@@ -0,0 +1,10 @@ +
Quoted 9 lines
drivers/vfio/mdev/Kconfig line 8
Quoted 7 lines
drivers/vfio/mdev/Kconfig new file +7
-0+config VFIO_MDEV + tristate "Mediated device driver framework" + depends on VFIO + default n + help + Provides a framework to virtualize devices. + See Documentation/vfio-mdev/vfio-mediated-device.txt for more details.We don't have this doc at this point of time.
Yes, but I have this doc in this patch series.
Quoted 119 lines
Quoted 17 lines
drivers/vfio/mdev/Kconfig new file +2
-0+ + If you don't know what do here, say N.drivers/vfio/mdev/Makefile new file +4
-0@@ -0,0 +1,4 @@ + +mdev-y := mdev_core.o mdev_sysfs.o mdev_driver.o + +obj-$(CONFIG_VFIO_MDEV) += mdev.odrivers/vfio/mdev/mdev_core.c new file +0
-0[...]
drivers/vfio/mdev/mdev_core.c line 123
Quoted 12 lines
drivers/vfio/mdev/mdev_core.c new file +12
-0+ +/* +* mdev_device_remove_ops gets called from sysfs's 'remove' and when parent +* device is being unregistered from mdev device framework. +* - 'force_remove' is set to 'false' when called from sysfs's 'remove' which +* indicates that if the mdev device is active, used by VMM or userspace +* application, vendor driver could return error then don't remove the device. +* - 'force_remove' is set to 'true' when called from mdev_unregister_device() +* which indicate that parent device is being removed from mdev device +* framework so remove mdev device forcefully. +*/ +static int mdev_device_remove_ops (struct mdev_device *mdev,bool force_remove)?
s/force_remove/force/Quoted 85 lines
drivers/vfio/mdev/mdev_core.c new file +85
-0+{ +struct parent_device *parent = mdev->parent; +int ret; + +/* +* Vendor driver can return error if VMM or userspace application is +* using this mdev device. +*/ + ret = parent->ops->remove (mdev); +if (ret && !force_remove) +return -EBUSY ; + +sysfs_remove_groups (&mdev->dev.kobj, parent->ops->mdev_attr_groups); +return 0 ; +} + +static int mdev_device_remove_cb (struct device *dev,void *data) +{ +if (!dev_is_mdev (dev)) +return 0 ; + +return mdev_device_remove (dev, data ? *(bool *)data : true); +} + +/* +* mdev_register_device : Register a device +* @dev: device structure representing parent device. +* @ops: Parent device operation structure to be registered. +* +* Add device to list of registered parent devices. +* Returns a negative value on error, otherwise 0. +*/ +int mdev_register_device (struct device *dev,const struct parent_ops *ops) +{ +int ret; +struct parent_device *parent; + +/* check for mandatory ops */ +if (!ops || !ops->create || !ops->remove || !ops->supported_type_groups) +return -EINVAL ; + + dev =get_device (dev); +if (!dev) +return -EINVAL ; + +mutex_lock (&parent_list_lock); + +/* Check for duplicate */ + parent =__find_parent_device (dev); +if (parent) { + ret = -EEXIST ; + goto add_dev_err; + } + + parent =kzalloc (sizeof (*parent),GFP_KERNEL ); +if (!parent) { + ret = -ENOMEM ; + goto add_dev_err; + } + +kref_init (&parent->ref); +mutex_init (&parent->lock); + + parent->dev = dev; + parent->ops = ops; + +if (!mdev_bus_compat_class) { + mdev_bus_compat_class =class_compat_register ("mdev_bus" ); +if (!mdev_bus_compat_class) { + ret = -ENOMEM ; + goto add_dev_err; + } + } + + ret =parent_create_sysfs_files (parent); +if (ret) + goto add_dev_err; + + ret =class_compat_create_link (mdev_bus_compat_class, dev,NULL ); +if (ret) +dev_warn (dev,"Failed to create compatibility class link\n" ); + +list_add (&parent->next, &parent_list); +mutex_unlock (&parent_list_lock); +
Quoted 9 lines
drivers/vfio/mdev/mdev_core.c line 215
Quoted 7 lines
drivers/vfio/mdev/mdev_core.c new file +7
-0+dev_info (dev,"MDEV: Registered\n" ); +return 0 ; + +add_dev_err: +mutex_unlock (&parent_list_lock); +if (parent) +mdev_put_parent (parent);Why do this? I don't find the place that you call
mdev_get_parentabove.
kref_init(&parent->ref);
Above increments the ref_count, so mdev_put_parent() should be called if anything fails.
Quoted 4 lines
drivers/vfio/mdev/mdev_core.c line 217
Quoted 2 lines
drivers/vfio/mdev/mdev_core.c new file +2
-0+else +put_device (dev);Shouldn't we always do this?
When mdev_put_parent() is called, its release function do this. So if mdev_put_parent() is called, we don't need this.
Quoted 31 lines
Quoted 31 lines
drivers/vfio/mdev/mdev_core.c new file +31
-0+return ret; +} +EXPORT_SYMBOL (mdev_register_device); + +/* +* mdev_unregister_device : Unregister a parent device +* @dev: device structure representing parent device. +* +* Remove device from list of registered parent devices. Give a chance to free +* existing mediated devices for given device. +*/ + +void mdev_unregister_device (struct device *dev) +{ +struct parent_device *parent; +bool force_remove = true; + +mutex_lock (&parent_list_lock); + parent =__find_parent_device (dev); + +if (!parent) { +mutex_unlock (&parent_list_lock); +return ; + } +dev_info (dev,"MDEV: Unregistering\n" ); + +list_del (&parent->next); +class_compat_remove_link (mdev_bus_compat_class, dev,NULL ); + +device_for_each_child (dev, (void *)&force_remove, + mdev_device_remove_cb);
Quoted 9 lines
drivers/vfio/mdev/mdev_core.c line 253
Quoted 5 lines
drivers/vfio/mdev/mdev_core.c new file +5
-0+ +parent_remove_sysfs_files (parent); + +mutex_unlock (&parent_list_lock); +mdev_put_parent (parent);We also need to call
put_device(dev)here since we have aget_deviceduring registration, no?
Or I must miss something...
As explained above.
Quoted 5 lines
Quoted 3 lines
drivers/vfio/mdev/mdev_core.c new file +3
-0+} +EXPORT_SYMBOL (mdev_unregister_device); +[...]
Quoted 17 lines
Quoted 5 lines
drivers/vfio/mdev/mdev_core.c new file +5
-0+static int __init mdev_init (void ) +{ +int ret; + + ret =mdev_bus_register ();drivers/vfio/mdev/mdev_core.c line 369
Quoted 10 lines
drivers/vfio/mdev/mdev_core.c new file +10
-0+if (ret) { +pr_err ("Failed to register mdev bus\n" ); +return ret; + } + +/* +* Attempt to load known vfio_mdev. This gives us a working environment +* without the user needing to explicitly load vfio_mdev driver. +*/ +request_module_nowait ("vfio_mdev" );We don't have this module yet.
Yes, this module is added in 02/22 patch. I'll move only this call to 02/22 patch in my next update.
But ideally it should not fails anything if this vfio_mdev module is not found.
Quoted 25 lines
Quoted 25 lines
drivers/vfio/mdev/mdev_core.c new file +19
-0+ +return ret; +} + +static void __exit mdev_exit(void) +{ +if (mdev_bus_compat_class) +class_compat_unregister (mdev_bus_compat_class); + +mdev_bus_unregister (); +} + +module_init(mdev_init) +module_exit(mdev_exit) + +MODULE_VERSION(DRIVER_VERSION); +MODULE_LICENSE("GPL"); +MODULE_AUTHOR(DRIVER_AUTHOR); +MODULE_DESCRIPTION(DRIVER_DESC);drivers/vfio/mdev/mdev_driver.c new file +0
-0@@ -0,0 +1,122 @@
Quoted 9 lines
drivers/vfio/mdev/mdev_driver.c line 6
Quoted 6 lines
drivers/vfio/mdev/mdev_driver.c new file +6
-0+/* +* MDEV driver +* +* Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. +* Author: Neo Jia <[email protected]> +* Kirti Wankhede <[email protected]> Don't know if you care much for this:
There is a TAB before your name. :>
Oh ok, Thanks for pointing that, I'll remove TAB. I'll also take care of all the nits you suggested below.
Thanks,
Kirti
Quoted 185 lines
Quoted 17 lines
drivers/vfio/mdev/mdev_driver.c new file +17
-0+* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#include <linux/device.h> +#include <linux/iommu.h> +#include <linux/mdev.h> + +#include "mdev_private.h" + +static int mdev_attach_iommu (struct mdev_device *mdev) +{ +int ret; +struct iommu_group *group; +drivers/vfio/mdev/mdev_driver.c line 33
Quoted 10 lines
drivers/vfio/mdev/mdev_driver.c new file +10
-0+ group =iommu_group_alloc (); +if (IS_ERR (group)) +return PTR_ERR (group); + + ret =iommu_group_add_device (group, &mdev->dev); +if (ret) + goto attach_fail; + +dev_info (&mdev->dev,"MDEV: group_id = %d\n" , +iommu_group_id (group));nit: strange indentation.
The above two lines could be safely merge into one line.Quoted 16 lines
drivers/vfio/mdev/mdev_driver.c new file +16
-0+attach_fail: +iommu_group_put (group); +return ret; +} + +static void mdev_detach_iommu (struct mdev_device *mdev) +{ +iommu_group_remove_device (&mdev->dev); +dev_info (&mdev->dev,"MDEV: detaching iommu\n" ); +} + +static int mdev_probe (struct device *dev) +{ +struct mdev_driver *drv =to_mdev_driver (dev->driver); +struct mdev_device *mdev =to_mdev_device (dev); +int ret;drivers/vfio/mdev/mdev_driver.c line 59
Quoted 10 lines
drivers/vfio/mdev/mdev_driver.c new file +10
-0+ + ret =mdev_attach_iommu (mdev); +if (ret) +return ret; + +if (drv && drv->probe) + ret = drv->probe (dev); + +if (ret) +mdev_detach_iommu (mdev);?
if (drv && drv->probe) { ret = drv->probe(dev); if (ret) mdev_detach_iommu(mdev); }Quoted 15 lines
drivers/vfio/mdev/mdev_driver.c new file +15
-0+ +return ret; +} + +static int mdev_remove (struct device *dev) +{ +struct mdev_driver *drv =to_mdev_driver (dev->driver); +struct mdev_device *mdev =to_mdev_device (dev); + +if (drv && drv->remove) + drv->remove (dev); + +mdev_detach_iommu (mdev); + +return 0 ;drivers/vfio/mdev/mdev_driver.c line 84
Quoted 10 lines
drivers/vfio/mdev/mdev_driver.c new file +10
-0+} + +struct bus_type mdev_bus_type = { + .name ="mdev" , + .probe = mdev_probe, + .remove = mdev_remove, +}; +EXPORT_SYMBOL_GPL (mdev_bus_type); + +/* Is this a kernel-doc comment?
It should be started with:
/**Quoted 12 lines
drivers/vfio/mdev/mdev_driver.c new file +12
-0+* mdev_register_driver - register a new MDEV driver +* @drv: the driver to register +* @owner: module owner of driver to be registered +* +* Returns a negative value on error, otherwise 0. +*/ +int mdev_register_driver (struct mdev_driver *drv,struct module *owner) +{ +/* initialize common driver fields */ + drv->driver.name = drv->name; + drv->driver.bus = &mdev_bus_type; + drv->driver.owner = owner;drivers/vfio/mdev/mdev_driver.c line 106
Quoted 10 lines
drivers/vfio/mdev/mdev_driver.c new file +10
-0+ +/* register with core */ +return driver_register (&drv->driver); +} +EXPORT_SYMBOL (mdev_register_driver); + +/* +* mdev_unregister_driver - unregister MDEV driver +* @drv: the driver to unregister +* Empty line.
Quoted 16 lines
drivers/vfio/mdev/mdev_driver.c new file +16
-0+*/ +void mdev_unregister_driver (struct mdev_driver *drv) +{ +driver_unregister (&drv->driver); +} +EXPORT_SYMBOL (mdev_unregister_driver); + +int mdev_bus_register (void ) +{ +return bus_register (&mdev_bus_type); +} + +void mdev_bus_unregister (void ) +{ +bus_unregister (&mdev_bus_type); +}[...]
Quoted 5 lines
include/linux/mdev.h +4
-0+/* Mediated device */ +struct mdev_device { +struct device dev; +struct parent_device *parent;include/linux/mdev.h line 42
Quoted 10 lines
include/linux/mdev.h +10
-0+uuid_le uuid; +void *driver_data; + +/* internal */ +struct kref ref; +struct list_head next; +struct kobject *type_kobj; +}; + +Empty line.
Quoted 4 lines
+/**
+ * structparent_ops- Structure to be registered for each parent device to
+ * register the device to mdev module.
+ *[...]
Quoted 1 line
+ * @mmap: mmap callback
No need a piece of description for arguments of the mmap callback?
Quoted 23 lines
+ * Parent device that support mediated device should be registered with mdev
+ * module withparent_opsstructure.
+ **/include/linux/mdev.h +20
-0+ +struct parent_ops { +struct module *owner; +const struct attribute_group **dev_attr_groups; +const struct attribute_group **mdev_attr_groups; +struct attribute_group **supported_type_groups; + +int (*create)(struct kobject *kobj,struct mdev_device *mdev); +int (*remove)(struct mdev_device *mdev); +int (*open)(struct mdev_device *mdev); +void (*release)(struct mdev_device *mdev); +ssize_t (*read)(struct mdev_device *mdev,char __user *buf, +size_t count,loff_t *ppos); +ssize_t (*write)(struct mdev_device *mdev,const char __user *buf, +size_t count,loff_t *ppos); +ssize_t (*ioctl)(struct mdev_device *mdev,unsigned int cmd, +unsigned long arg); +int (*mmap)(struct mdev_device *mdev,struct vm_area_struct *vma); +}; +[...]
Re: [PATCH v11 11/22] vfio iommu: Add blocking notifier to notify DMA_UNMAP
In reply to Kirti WankhedeQuoted 75 lines · On Wed, 9 Nov 2016 01:29:19 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 65 lines · Alex Williamson
Quoted 59 lines · On Tue, 8 Nov 2016 21:56:29 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 3 lines · Alex Williamson
On Sat, 5 Nov 2016 02:40:45 +0530
Kirti Wankhede <[email protected]> wrote:...
Quoted 8 lines
Quoted 2 lines
+int
vfio_register_notifier(struct device *dev, struct notifier_block *nb)Is the expectation here that this is a generic notifier for all
vfio->mdevsignaling? That should probably be made clear in the mdev API to avoid vendor drivers assuming their notifier callback only occurs for unmaps, even if that's currently the case.Ok. Adding comment about notifier callback in
mdev_devicewhich is part of next patch....
Quoted 12 lines
Quoted 12 lines
drivers/vfio/vfio_iommu_type1.c +2
-1mutex_lock (&iommu->lock); -if (!iommu->external_domain) { +/* Fail if notifier list is empty */ +if ((!iommu->external_domain) || (!iommu->notifier.head)) { ret = -EINVAL ; goto pin_done; }@@ -867,6 +870,11 @@ unlock:/* Report how much was unmapped */ unmap->size = unmapped;Quoted 7 lines
drivers/vfio/vfio_iommu_type1.c unlock: · line 876
Quoted 4 lines
drivers/vfio/vfio_iommu_type1.c +4
-0+if (unmapped && iommu->external_domain) +blocking_notifier_call_chain (&iommu->notifier, +VFIO_IOMMU_NOTIFY_DMA_UNMAP , + unmap);This is after the fact, there's already a gap here where pages are unpinned and the mdev device is still running.
Oh, there is a bug here, now
unpin_pages()takeuser_pfnas argument and findvfio_dma. If its not found, it doesn't unpin pages. We have to call this notifier beforevfio_remove_dma(). But if we call this beforevfio_remove_dma()there will be deadlock sinceiommu->lockis already held here andvfio_iommu_type1_unpin_pages()will also try to holdiommu->lock.
If we want to callblocking_notifier_call_chain()beforevfio_remove_dma(), sequence should be:unmapped +=
dma->size;mutex_unlock(&iommu->lock);if (iommu->external_domain)) { struct vfio_iommu_type1_dma_unmap nb_unmap; nb_unmap.iova = dma->iova; nb_unmap.size = dma->size; blocking_notifier_call_chain(&iommu->notifier, VFIO_IOMMU_NOTIFY_DMA_UNMAP, &nb_unmap); }Quoted 2 lines
mutex_lock(&iommu->lock);vfio_remove_dma(iommu, dma);Quoted 8 lines
It seems like it would be worthwhile to have the rb-tree rooted in the vfio-dma, then we only need to call the notifier if there are pages pinned within that vfio-dma (ie. the rb-tree is not empty). We can then release the lock call the notifier, re-acquire the lock, and
BUG_ONif the rb-tree still is not empty. We might get duplicate pfns between separatevfio_dmastructs, but as I mentioned in other replies, that seems like an exception that we don't need to optimize for.
Quoted 3 lines
If we don't optimize for the case where iova from different
vfio_dmaare mapped to same pfn and we would not consider this case for page accounting then:
Just to clarify, the current code (not handling mdevs) will pin and do page accounting per iova, regardless of whether the iova translates to a unique pfn. As long as we do no worse than that, I'm ok.
Quoted 6 lines
- have rb tree of pinned iova, where key would be iova, in each
vfio_dmastructure.
- iova tracking structure would have iova andref_countonly.
- page accounting would only count number of iova's inrb_tree, case where different iova could map to same pfn would not be considered in this implementation for now.
Quoted 8 lines
-
vfio_unpin_pages()would haveuser_pfnand pfn as input, we would validate that iova exist in rb tree and trust vendor driver that corresponding pfn is correct, there is no validation of pfn. If want validate pfn, call GUP, verify pfn and callput_pfn().
- In .release() or .detach_group() path, if there are entries in this rb tree, call GUP again using that iova, get pfn and then callput_pfn(pfn)for ref_count+1 times. This is because we are not keeping pfn in our tracking logic.
Wait a sec, if we detach a group from the container and it's not the last group in the container (which would trigger a release), we can't assume anything about which vfio_dma entries were associated with that device. The vendor driver, through the release of the device(s) within that group, needs to unpin. In a container release, we need to send a notifier to the vendor driver(s) to cause an unpin. This is the only mechanism we have to ensure that vendor drivers are not leaking references. If during the release, after the notifier, if any vfio_pfns remain, we need to BUG_ON, just like we need to do for any other DMA_UNMAP.
Also, I'll say it again, I also don't like this API of passing around potentially giant arrays, and especially the API of relying on the vendor driver to tell us an arbitrary pfn to unpin. If we make the assumption that vendor drivers do not pin lots and lots of memory, perhaps we could use a struct vfio_pfn as:
struct vfio_pfn {
struct rb_node node;
dma_addr_t iova; /* key */
unsigned long pfn;
atomic_t ref_count;
};This puts us at 44-bytes per pfn, which isn't great, but I think it puts us in a better position with the API that we could make use of a page-table or sparse array in the future that would eliminate the rb_node and make the iova implicit in the location of the data structure. That would leave only the pfn and ref_count, which could potentially be combined into a single 8-byte field if we had per vfio_dma (or higher) locking to avoid the atomic_t (and we're happy that the reference count is always less than PAGE_SIZE, ie. we could fail pinning if we get to that point).
That would allow for the unpin call to not provide the pfn, so could we then look at whether we need the batching provided by the iova array at all? I don't have a feel for the size of memory that gets pinned by the vendor driver, the frequency of pinning, or whether usage of hugepages for the guest is likely to translate into contiguous memory requests through this API. What's your feeling? Thanks,
Alex
Re: [PATCH v11 13/22] vfio: Introduce common function to add capabilities
In reply to Kirti WankhedeQuoted 30 lines · On Wed, 9 Nov 2016 02:16:17 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 13 lines · Alexey Kardashevskiy
Quoted 12 lines · Kirti Wankhede
Vendor driver using mediated device framework should use
vfio_info_add_capability()to add capabilities.
Introduced this function to reduce code duplication in vendor drivers.Signed-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id I6fca329fa2291f37a2c859d0bc97574d9e2ce1a6--- drivers/vfio/vfio.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++- include/linux/vfio.h | 3 +++ 2 files changed, 62 insertions(+), 1 deletion(-)Quoted 15 lines
drivers/vfio/vfio.c void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset) · line 1800
Quoted 10 lines
drivers/vfio/vfio.c +1
-1@@ -1797,8 +1797,66 @@ void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset)for (tmp = caps->buf; tmp->next; tmp = (void *)tmp + tmp->next - offset) tmp->next += offset; } -EXPORT_SYMBOL_GPL (vfio_info_cap_shift); +EXPORT_SYMBOL (vfio_info_cap_shift);Why this change?
Quoted 1 line
We want this symbol to be available to all drivers.
IOW, from proprietary drivers. It makes me uncomfortable how many non-GPL symbols we're adding (or converting) in this effort, but I'm trying to look objectively at every export as to whether a non-GPL caller of the function is legitimately separate from in-kernel code. For instance are they making use of data structures intrinsic to GPL'd code. In this case we're converting a symbol that's just manipulating a data buffer to add an offset to each element in a chain. The entries are documented in a uapi header. Kirti asked me about this one, and I couldn't find any basis to raise an objection. If you spot any reason that any of the export symbols in these series really should be GPL, please raise the issue.
Quoted 78 lines
Quoted 70 lines
Quoted 70 lines
drivers/vfio/vfio.c +58
-0+static int sparse_mmap_cap (struct vfio_info_cap *caps,void *cap_type) +{ +struct vfio_info_cap_header *header; +struct vfio_region_info_cap_sparse_mmap *sparse_cap, *sparse = cap_type; +size_t size; + + size =sizeof (*sparse) + 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_cap =container_of (header, + struct vfio_region_info_cap_sparse_mmap, header); + sparse_cap->nr_areas = sparse->nr_areas; +memcpy (sparse_cap->areas, sparse->areas, + sparse->nr_areas *sizeof (*sparse->areas)); +return 0 ; +} + +static int region_type_cap (struct vfio_info_cap *caps,void *cap_type) +{ +struct vfio_info_cap_header *header; +struct vfio_region_info_cap_type *type_cap, *cap = cap_type; + + header =vfio_info_cap_add (caps,sizeof (*cap), +VFIO_REGION_INFO_CAP_TYPE ,1 ); +if (IS_ERR (header)) +return PTR_ERR (header); + + type_cap = container_of(header, struct vfio_region_info_cap_type, + header); + type_cap->type = cap->type; + type_cap->subtype = cap->subtype; +return 0 ; +} + +int vfio_info_add_capability (struct vfio_info_cap *caps,int cap_type_id, +void *cap_type) +{ +int ret = -EINVAL ; + +if (!cap_type) +return 0 ; + +switch (cap_type_id) { +case VFIO_REGION_INFO_CAP_SPARSE_MMAP : + ret =sparse_mmap_cap (caps, cap_type); +break ; + +case VFIO_REGION_INFO_CAP_TYPE : + ret =region_type_cap (caps, cap_type); +break ; + } + +return ret; +} +EXPORT_SYMBOL (vfio_info_add_capability);/* * Pin a set of guest PFNs and return their associated host PFNs for local include/linux/vfio.h +0
-0@@ -113,6 +113,9 @@ extern struct vfio_info_cap_header *vfio_info_cap_add( struct vfio_info_cap *caps, size_t size, u16 id, u16 version);extern void vfio_info_cap_shift (struct vfio_info_cap *caps,size_t offset);Quoted 7 lines
include/linux/vfio.h extern struct vfio_info_cap_header *vfio_info_cap_add( · line 118
Quoted 3 lines
include/linux/vfio.h +3
-0+extern int vfio_info_add_capability (struct vfio_info_cap *caps, +int cap_type_id,void *cap_type); +It would make it easier to review and bisect if 14/22 was squashed into this one.
Quoted 2 lines
This was split based on Alex's suggestion on earlier version of this patchset.
Yeah, generally squashing patches together is the opposite of what we want for review and bisect. In this case the symbol exports should avoid any defined-but-unused warnings. Thanks,
Alex
Re: [PATCH v11 01/22] vfio: Mediated device Core driver
In reply to Kirti Wankhede* Kirti Wankhede <[email protected]> [2016-11-09 02:36:12 +0530]:
[...]
Quoted 75 lines
Quoted 61 lines
Quoted 61 lines
drivers/vfio/mdev/mdev_core.c new file +61
-0+/* +* mdev_register_device : Register a device +* @dev: device structure representing parent device. +* @ops: Parent device operation structure to be registered. +* +* Add device to list of registered parent devices. +* Returns a negative value on error, otherwise 0. +*/ +int mdev_register_device (struct device *dev,const struct parent_ops *ops) +{ +int ret; +struct parent_device *parent; + +/* check for mandatory ops */ +if (!ops || !ops->create || !ops->remove || !ops->supported_type_groups) +return -EINVAL ; + + dev =get_device (dev); +if (!dev) +return -EINVAL ; + +mutex_lock (&parent_list_lock); + +/* Check for duplicate */ + parent =__find_parent_device (dev); +if (parent) { + ret = -EEXIST ; + goto add_dev_err; + } + + parent =kzalloc (sizeof (*parent),GFP_KERNEL ); +if (!parent) { + ret = -ENOMEM ; + goto add_dev_err; + } + +kref_init (&parent->ref); +mutex_init (&parent->lock); + + parent->dev = dev; + parent->ops = ops; + +if (!mdev_bus_compat_class) { + mdev_bus_compat_class =class_compat_register ("mdev_bus" ); +if (!mdev_bus_compat_class) { + ret = -ENOMEM ; + goto add_dev_err; + } + } + + ret =parent_create_sysfs_files (parent); +if (ret) + goto add_dev_err; + + ret =class_compat_create_link (mdev_bus_compat_class, dev,NULL ); +if (ret) +dev_warn (dev,"Failed to create compatibility class link\n" ); + +list_add (&parent->next, &parent_list); +mutex_unlock (&parent_list_lock); +Quoted 9 lines
drivers/vfio/mdev/mdev_core.c line 215
Quoted 7 lines
drivers/vfio/mdev/mdev_core.c new file +7
-0+dev_info (dev,"MDEV: Registered\n" ); +return 0 ; + +add_dev_err: +mutex_unlock (&parent_list_lock); +if (parent) +mdev_put_parent (parent);Why do this? I don't find the place that you call
mdev_get_parentabove.kref_init(&parent->ref);Above increments the
ref_count, somdev_put_parent()should be called if anything fails.
Quoted 7 lines
Quoted 4 lines
drivers/vfio/mdev/mdev_core.c line 217
Quoted 2 lines
drivers/vfio/mdev/mdev_core.c new file +2
-0+else +put_device (dev);Shouldn't we always do this?
When
mdev_put_parent()is called, its release function do this. So ifmdev_put_parent()is called, we don't need this.
Sorry for missing that. Thanks for the explanation!
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
drivers/vfio/mdev/mdev_core.c new file +4
-0+return ret; +} +EXPORT_SYMBOL (mdev_register_device); +
[...]
Signature · 1 line
Dong Jia
Re: [Qemu-devel] [PATCH v11 01/22] vfio: Mediated device Core driver
In reply to Kirti Wankhede* Kirti Wankhede <[email protected]> [2016-11-09 02:36:12 +0530]:
[...]
Quoted 75 lines
Quoted 61 lines
Quoted 61 lines
drivers/vfio/mdev/mdev_core.c new file +61
-0+/* +* mdev_register_device : Register a device +* @dev: device structure representing parent device. +* @ops: Parent device operation structure to be registered. +* +* Add device to list of registered parent devices. +* Returns a negative value on error, otherwise 0. +*/ +int mdev_register_device (struct device *dev,const struct parent_ops *ops) +{ +int ret; +struct parent_device *parent; + +/* check for mandatory ops */ +if (!ops || !ops->create || !ops->remove || !ops->supported_type_groups) +return -EINVAL ; + + dev =get_device (dev); +if (!dev) +return -EINVAL ; + +mutex_lock (&parent_list_lock); + +/* Check for duplicate */ + parent =__find_parent_device (dev); +if (parent) { + ret = -EEXIST ; + goto add_dev_err; + } + + parent =kzalloc (sizeof (*parent),GFP_KERNEL ); +if (!parent) { + ret = -ENOMEM ; + goto add_dev_err; + } + +kref_init (&parent->ref); +mutex_init (&parent->lock); + + parent->dev = dev; + parent->ops = ops; + +if (!mdev_bus_compat_class) { + mdev_bus_compat_class =class_compat_register ("mdev_bus" ); +if (!mdev_bus_compat_class) { + ret = -ENOMEM ; + goto add_dev_err; + } + } + + ret =parent_create_sysfs_files (parent); +if (ret) + goto add_dev_err; + + ret =class_compat_create_link (mdev_bus_compat_class, dev,NULL ); +if (ret) +dev_warn (dev,"Failed to create compatibility class link\n" ); + +list_add (&parent->next, &parent_list); +mutex_unlock (&parent_list_lock); +Quoted 9 lines
drivers/vfio/mdev/mdev_core.c line 215
Quoted 7 lines
drivers/vfio/mdev/mdev_core.c new file +7
-0+dev_info (dev,"MDEV: Registered\n" ); +return 0 ; + +add_dev_err: +mutex_unlock (&parent_list_lock); +if (parent) +mdev_put_parent (parent);Why do this? I don't find the place that you call
mdev_get_parentabove.kref_init(&parent->ref);Above increments the
ref_count, somdev_put_parent()should be called if anything fails.
Quoted 7 lines
Quoted 4 lines
drivers/vfio/mdev/mdev_core.c line 217
Quoted 2 lines
drivers/vfio/mdev/mdev_core.c new file +2
-0+else +put_device (dev);Shouldn't we always do this?
When
mdev_put_parent()is called, its release function do this. So ifmdev_put_parent()is called, we don't need this.
Sorry for missing that. Thanks for the explanation!
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
drivers/vfio/mdev/mdev_core.c new file +4
-0+return ret; +} +EXPORT_SYMBOL (mdev_register_device); +
[...]
Signature · 1 line
Dong Jia
Re: [PATCH v11 13/22] vfio: Introduce common function to add capabilities
In reply to Alex WilliamsonQuoted 35 lines · Alex Williamson
Quoted 30 lines · On Wed, 9 Nov 2016 02:16:17 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 13 lines · Alexey Kardashevskiy
Quoted 12 lines · Kirti Wankhede
Vendor driver using mediated device framework should use
vfio_info_add_capability()to add capabilities.
Introduced this function to reduce code duplication in vendor drivers.Signed-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id I6fca329fa2291f37a2c859d0bc97574d9e2ce1a6--- drivers/vfio/vfio.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++- include/linux/vfio.h | 3 +++ 2 files changed, 62 insertions(+), 1 deletion(-)Quoted 15 lines
drivers/vfio/vfio.c void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset) · line 1800
Quoted 10 lines
drivers/vfio/vfio.c +1
-1@@ -1797,8 +1797,66 @@ void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset)for (tmp = caps->buf; tmp->next; tmp = (void *)tmp + tmp->next - offset) tmp->next += offset; } -EXPORT_SYMBOL_GPL (vfio_info_cap_shift); +EXPORT_SYMBOL (vfio_info_cap_shift);Why this change?
Quoted 1 line
We want this symbol to be available to all drivers.
Quoted 92 lines
IOW, from proprietary drivers. It makes me uncomfortable how many non-GPL symbols we're adding (or converting) in this effort, but I'm trying to look objectively at every export as to whether a non-GPL caller of the function is legitimately separate from in-kernel code. For instance are they making use of data structures intrinsic to GPL'd code. In this case we're converting a symbol that's just manipulating a data buffer to add an offset to each element in a chain. The entries are documented in a uapi header. Kirti asked me about this one, and I couldn't find any basis to raise an objection. If you spot any reason that any of the export symbols in these series really should be GPL, please raise the issue.
Quoted 80 lines
Quoted 70 lines
Quoted 70 lines
drivers/vfio/vfio.c +58
-0+static int sparse_mmap_cap (struct vfio_info_cap *caps,void *cap_type) +{ +struct vfio_info_cap_header *header; +struct vfio_region_info_cap_sparse_mmap *sparse_cap, *sparse = cap_type; +size_t size; + + size =sizeof (*sparse) + 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_cap =container_of (header, + struct vfio_region_info_cap_sparse_mmap, header); + sparse_cap->nr_areas = sparse->nr_areas; +memcpy (sparse_cap->areas, sparse->areas, + sparse->nr_areas *sizeof (*sparse->areas)); +return 0 ; +} + +static int region_type_cap (struct vfio_info_cap *caps,void *cap_type) +{ +struct vfio_info_cap_header *header; +struct vfio_region_info_cap_type *type_cap, *cap = cap_type; + + header =vfio_info_cap_add (caps,sizeof (*cap), +VFIO_REGION_INFO_CAP_TYPE ,1 ); +if (IS_ERR (header)) +return PTR_ERR (header); + + type_cap = container_of(header, struct vfio_region_info_cap_type, + header); + type_cap->type = cap->type; + type_cap->subtype = cap->subtype; +return 0 ; +} + +int vfio_info_add_capability (struct vfio_info_cap *caps,int cap_type_id, +void *cap_type) +{ +int ret = -EINVAL ; + +if (!cap_type) +return 0 ; + +switch (cap_type_id) { +case VFIO_REGION_INFO_CAP_SPARSE_MMAP : + ret =sparse_mmap_cap (caps, cap_type); +break ; + +case VFIO_REGION_INFO_CAP_TYPE : + ret =region_type_cap (caps, cap_type); +break ; + } + +return ret; +} +EXPORT_SYMBOL (vfio_info_add_capability);/* * Pin a set of guest PFNs and return their associated host PFNs for local include/linux/vfio.h +0
-0@@ -113,6 +113,9 @@ extern struct vfio_info_cap_header *vfio_info_cap_add( struct vfio_info_cap *caps, size_t size, u16 id, u16 version);extern void vfio_info_cap_shift (struct vfio_info_cap *caps,size_t offset);Quoted 7 lines
include/linux/vfio.h extern struct vfio_info_cap_header *vfio_info_cap_add( · line 118
Quoted 3 lines
include/linux/vfio.h +3
-0+extern int vfio_info_add_capability (struct vfio_info_cap *caps, +int cap_type_id,void *cap_type); +It would make it easier to review and bisect if 14/22 was squashed into this one.
This was split based on Alex's suggestion on earlier version of this patchset.
This could have been mentioned in changelog...
Quoted 3 lines
Yeah, generally squashing patches together is the opposite of what we want for review and bisect.
13/22 adds a helper which is not used in it so if there is an error, it won't be caught by bisecting, bisect will incorrectly point to 14/22.
Also, since the new helper in 13/22 is made from chunks removed in 14/22, I'd like to see both changes in one patch to make sure that nothing was lost during cut-n-paste. Especially when it is not just 2 patches, like 3 patches later in the series.
imho splitting like this only makes sense (or, rather, just make life easier) when patches go via different maintainers trees.
However, since Alex is happy, you can ignore me.
Quoted 5 lines
In this case the symbol exports should
avoid any defined-but-unused warnings. Thanks,Alex
Signature · 1 line
Alexey
Re: [PATCH v11 15/22] vfio: Introduce vfio_set_irqs_validate_and_prepare()
In reply to Kirti WankhedeQuoted 44 lines · Kirti Wankhede
Quoted 31 lines · Alexey Kardashevskiy
Quoted 30 lines · Kirti Wankhede
Vendor driver using mediated device framework would use same mechnism to validate and prepare IRQs. Introducing this function to reduce code replication in multiple drivers.
Signed-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id Ie201f269dda0713ca18a07dc4852500bd8b48309--- drivers/vfio/vfio.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/vfio.h | 4 ++++ 2 files changed, 52 insertions(+)drivers/vfio/vfio.c +10
-0@@ -1858,6 +1858,54 @@ int vfio_info_add_capability(struct vfio_info_cap *caps, int cap_type_id, }EXPORT_SYMBOL (vfio_info_add_capability); +int vfio_set_irqs_validate_and_prepare (struct vfio_irq_set *hdr,int num_irqs, +int max_irq_type,size_t *data_size) +{ +unsigned long minsz; +size_t size; + + minsz = offsetofend(struct vfio_irq_set, count); + + if ((hdr->argsz < minsz) || (hdr->index >= max_irq_type) || + (hdr->count >= (U32_MAX - hdr->start)) ||Quoted 9 lines
drivers/vfio/vfio.c int vfio_info_add_capability(struct vfio_info_cap *caps, int cap_type_id, · line 1875
Quoted 5 lines
drivers/vfio/vfio.c +5
-0+ (hdr->flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK | + VFIO_IRQ_SET_ACTION_TYPE_MASK))) + return -EINVAL; + +if (data_size)Pointless check, the callers will pass non null pointer with value initialized to 0 anyway.
Quoted 2 lines
Not always, When
VFIO_IRQ_SET_DATA_NONEflag is set, caller can passdata_size= NULL.
Today data_size is not NULL in all cases and the way it is used now (ioctl VFIO_DEVICE_SET_IRQS) gives me an idea that this is not going to change.
Quoted 52 lines
Quoted 16 lines
Quoted 16 lines
drivers/vfio/vfio.c +14
-0+ *data_size =0 ; + +if (hdr->start >= num_irqs || hdr->start + hdr->count > num_irqs) +return -EINVAL ; + +switch (hdr->flags &VFIO_IRQ_SET_DATA_TYPE_MASK ) { +case VFIO_IRQ_SET_DATA_NONE : + size =0 ; +break ; +case VFIO_IRQ_SET_DATA_BOOL : + size =sizeof (uint8_t ); +break ; +case VFIO_IRQ_SET_DATA_EVENTFD : + size =sizeof (int32_t );Quoted 9 lines
drivers/vfio/vfio.c int vfio_info_add_capability(struct vfio_info_cap *caps, int cap_type_id, · line 1895
Quoted 6 lines
drivers/vfio/vfio.c +6
-0+break ; +default : +return -EINVAL ; + } + +if (size) {The whole branch would even work for size == 0.
In that case below check (!data_size) might result in error if
data_size== NULL, whereas its not error case when size == 0, i.e.VFIO_IRQ_SET_DATA_NONEflag set.Quoted 8 lines
drivers/vfio/vfio.c int vfio_info_add_capability(struct vfio_info_cap *caps, int cap_type_id, · line 1900
Quoted 5 lines
drivers/vfio/vfio.c +5
-0+if (hdr->argsz - minsz < hdr->count * size) +return -EINVAL ; + +if (!data_size) +return -EINVAL ;Redundant check as well.
This is not redundant. If you see above check, it sets its init value to 0 but doesn't fail.
Quoted 10 lines
drivers/vfio/vfio.c int vfio_info_add_capability(struct vfio_info_cap *caps, int cap_type_id, · line 1906
Quoted 6 lines
drivers/vfio/vfio.c +6
-0+ + *data_size = hdr->count * size; + } + +return 0 ; +}It does not really prepare anything as the name suggests. It looks like this is 2 different helpers actually:
Quoted 6 lines
Quoted 4 lines
int
vfio_set_irqs_validate()
andsize_tvfio_set_irqs_hdr_to_data_size()Later one is the prepare.
Does not like it prepares anything, just a simple converter.
Quoted 7 lines
Quoted 4 lines
And it would make it easier to review/bisect if 16/22 and 17/22 were merged into this one as this patch alone adds new code which it does not use and all 3 patches are fairly small.
I do had all 3 patch merged in one in earlier version of patchset. This is split as per Alex's suggestion.
I got this from another mail from Alex. Which I find strange but whatever, this is his realm anyway :)
Quoted 10 lines
Quoted 8 lines
drivers/vfio/vfio.c int vfio_info_add_capability(struct vfio_info_cap *caps, int cap_type_id, · line 1907
Quoted 3 lines
drivers/vfio/vfio.c +1
-0+EXPORT_SYMBOL (vfio_set_irqs_validate_and_prepare);Everything you export in this patchset is
EXPORT_SYMBOL()while the existing code usesEXPORT_SYMBOL_GPL(), is this for a reason?We want these symbols to be available to all drivers.
Right, got it from another mail from Alex as well. Ok, seems all right so far. A note in the commit log would be useful though.
Quoted 26 lines
Thanks,
KirtiQuoted 22 lines
Quoted 20 lines
drivers/vfio/vfio.c +1
-0+/* * Pin a set of guest PFNs and return their associated host PFNs for local * domain only. include/linux/vfio.h +4
-0@@ -116,6 +116,10 @@ extern void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);extern int vfio_info_add_capability (struct vfio_info_cap *caps,int cap_type_id,void *cap_type); +extern int vfio_set_irqs_validate_and_prepare (struct vfio_irq_set *hdr, +int num_irqs,int max_irq_type, +size_t *data_size); +struct pci_dev ;#ifdef CONFIG_EEH extern void vfio_spapr_pci_eeh_open (struct pci_dev *pdev);
Signature · 1 line
Alexey
Re: [PATCH v11 02/22] vfio: VFIO based driver for Mediated devices
In reply to Kirti Wankhede* Kirti Wankhede <[email protected]> [2016-11-05 02:40:36 +0530]:
Hi Kirti,
Quoted 23 lines
vfio_mdevdriver registers with mdev core driver.
mdev core driver creates mediated device and calls probe routine ofvfio_mdevdriver for each device.
Probe routine ofvfio_mdevdriver adds mediated device to VFIO core moduleThis driver forms a shim layer that pass through VFIO devices operations to vendor driver for mediated devices.
Signed-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id I583f4734752971d3d112324d69e2508c88f359ec--- drivers/vfio/mdev/Kconfig | 9 ++- drivers/vfio/mdev/Makefile | 1 + drivers/vfio/mdev/vfio_mdev.c | 148 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 drivers/vfio/mdev/vfio_mdev.cdrivers/vfio/mdev/Kconfig +0
-0
drivers/vfio/mdev/Kconfig config VFIO_MDEV · line 8
Quoted 5 lines
drivers/vfio/mdev/Kconfig +1
-1@@ -5,6 +5,13 @@ config VFIO_MDEV default n help Provides a framework to virtualize devices. - See Documentation/vfio-mdev/vfio-mediated-device.txt for more details. + See Documentation/vfio-mediated-device.txt for more details.
So patch #01 has a wrong doc path.
drivers/vfio/mdev/Kconfig config VFIO_MDEV · line 17
Quoted 9 lines
drivers/vfio/mdev/Kconfig +7
-0If you don't know what do here, say N. + +config VFIO_MDEV_DEVICE + tristate "VFIO support for Mediated devices" + depends on VFIO && VFIO_MDEV + default n + help + VFIO based driver for mediated devices.
I just think the names of the config entries here are a bit strange, but I'm not sure if there is a better way. Maybe (?):
s/VFIO_MDEV/VFIO_MDEV_SUPPORT/
s/VFIO_MDEV_DEVICE/VFIO_MDEV/
Quoted 10 lines
drivers/vfio/mdev/Makefile +1
-0@@ -2,3 +2,4 @@ mdev-y := mdev_core.o mdev_sysfs.o mdev_driver.o obj-$(CONFIG_VFIO_MDEV) += mdev.o +obj-$(CONFIG_VFIO_MDEV_DEVICE) += vfio_mdev.odrivers/vfio/mdev/vfio_mdev.c +0
-0
[...]
drivers/vfio/mdev/vfio_mdev.c line 146
Quoted 3 lines
drivers/vfio/mdev/vfio_mdev.c +3
-0+ +MODULE_VERSION (DRIVER_VERSION ); +MODULE_LICENSE ("GPL" );
?
GPL V2
Quoted 5 lines
drivers/vfio/mdev/vfio_mdev.c +2
-0+MODULE_AUTHOR (DRIVER_AUTHOR ); +MODULE_DESCRIPTION (DRIVER_DESC );Signature · 1 line
2.7.0
Signature · 1 line
Dong Jia
Re: [Qemu-devel] [PATCH v11 02/22] vfio: VFIO based driver for Mediated devices
In reply to Kirti Wankhede* Kirti Wankhede <[email protected]> [2016-11-05 02:40:36 +0530]:
Hi Kirti,
Quoted 23 lines
vfio_mdevdriver registers with mdev core driver.
mdev core driver creates mediated device and calls probe routine ofvfio_mdevdriver for each device.
Probe routine ofvfio_mdevdriver adds mediated device to VFIO core moduleThis driver forms a shim layer that pass through VFIO devices operations to vendor driver for mediated devices.
Signed-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id I583f4734752971d3d112324d69e2508c88f359ec--- drivers/vfio/mdev/Kconfig | 9 ++- drivers/vfio/mdev/Makefile | 1 + drivers/vfio/mdev/vfio_mdev.c | 148 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 drivers/vfio/mdev/vfio_mdev.cdrivers/vfio/mdev/Kconfig +0
-0
drivers/vfio/mdev/Kconfig config VFIO_MDEV · line 8
Quoted 5 lines
drivers/vfio/mdev/Kconfig +1
-1@@ -5,6 +5,13 @@ config VFIO_MDEV default n help Provides a framework to virtualize devices. - See Documentation/vfio-mdev/vfio-mediated-device.txt for more details. + See Documentation/vfio-mediated-device.txt for more details.
So patch #01 has a wrong doc path.
drivers/vfio/mdev/Kconfig config VFIO_MDEV · line 17
Quoted 9 lines
drivers/vfio/mdev/Kconfig +7
-0If you don't know what do here, say N. + +config VFIO_MDEV_DEVICE + tristate "VFIO support for Mediated devices" + depends on VFIO && VFIO_MDEV + default n + help + VFIO based driver for mediated devices.
I just think the names of the config entries here are a bit strange, but I'm not sure if there is a better way. Maybe (?):
s/VFIO_MDEV/VFIO_MDEV_SUPPORT/
s/VFIO_MDEV_DEVICE/VFIO_MDEV/
Quoted 10 lines
drivers/vfio/mdev/Makefile +1
-0@@ -2,3 +2,4 @@ mdev-y := mdev_core.o mdev_sysfs.o mdev_driver.o obj-$(CONFIG_VFIO_MDEV) += mdev.o +obj-$(CONFIG_VFIO_MDEV_DEVICE) += vfio_mdev.odrivers/vfio/mdev/vfio_mdev.c +0
-0
[...]
drivers/vfio/mdev/vfio_mdev.c line 146
Quoted 3 lines
drivers/vfio/mdev/vfio_mdev.c +3
-0+ +MODULE_VERSION (DRIVER_VERSION ); +MODULE_LICENSE ("GPL" );
?
GPL V2
Quoted 5 lines
drivers/vfio/mdev/vfio_mdev.c +2
-0+MODULE_AUTHOR (DRIVER_AUTHOR ); +MODULE_DESCRIPTION (DRIVER_DESC );Signature · 1 line
2.7.0
Signature · 1 line
Dong Jia
Re: [PATCH v11 15/22] vfio: Introduce vfio_set_irqs_validate_and_prepare()
In reply to Alexey KardashevskiyQuoted 118 lines · On Wed, 9 Nov 2016 14:07:58 +1100 Alexey Kardashevskiy <[email protected]> wrote:
Quoted 42 lines · Kirti Wankhede
Quoted 31 lines · Alexey Kardashevskiy
Quoted 30 lines · Kirti Wankhede
Vendor driver using mediated device framework would use same mechnism to validate and prepare IRQs. Introducing this function to reduce code replication in multiple drivers.
Signed-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id Ie201f269dda0713ca18a07dc4852500bd8b48309--- drivers/vfio/vfio.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/vfio.h | 4 ++++ 2 files changed, 52 insertions(+)drivers/vfio/vfio.c +10
-0@@ -1858,6 +1858,54 @@ int vfio_info_add_capability(struct vfio_info_cap *caps, int cap_type_id, }EXPORT_SYMBOL (vfio_info_add_capability); +int vfio_set_irqs_validate_and_prepare (struct vfio_irq_set *hdr,int num_irqs, +int max_irq_type,size_t *data_size) +{ +unsigned long minsz; +size_t size; + + minsz = offsetofend(struct vfio_irq_set, count); + + if ((hdr->argsz < minsz) || (hdr->index >= max_irq_type) || + (hdr->count >= (U32_MAX - hdr->start)) ||Quoted 9 lines
drivers/vfio/vfio.c int vfio_info_add_capability(struct vfio_info_cap *caps, int cap_type_id, · line 1875
Quoted 5 lines
drivers/vfio/vfio.c +5
-0+ (hdr->flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK | + VFIO_IRQ_SET_ACTION_TYPE_MASK))) + return -EINVAL; + +if (data_size)Pointless check, the callers will pass non null pointer with value initialized to 0 anyway.
Quoted 2 lines
Not always, When
VFIO_IRQ_SET_DATA_NONEflag is set, caller can passdata_size= NULL.Today
data_sizeis not NULL in all cases and the way it is used now (ioctlVFIO_DEVICE_SET_IRQS) gives me an idea that this is not going to change.Quoted 52 lines
Quoted 16 lines
Quoted 16 lines
drivers/vfio/vfio.c +14
-0+ *data_size =0 ; + +if (hdr->start >= num_irqs || hdr->start + hdr->count > num_irqs) +return -EINVAL ; + +switch (hdr->flags &VFIO_IRQ_SET_DATA_TYPE_MASK ) { +case VFIO_IRQ_SET_DATA_NONE : + size =0 ; +break ; +case VFIO_IRQ_SET_DATA_BOOL : + size =sizeof (uint8_t ); +break ; +case VFIO_IRQ_SET_DATA_EVENTFD : + size =sizeof (int32_t );Quoted 9 lines
drivers/vfio/vfio.c int vfio_info_add_capability(struct vfio_info_cap *caps, int cap_type_id, · line 1895
Quoted 6 lines
drivers/vfio/vfio.c +6
-0+break ; +default : +return -EINVAL ; + } + +if (size) {The whole branch would even work for size == 0.
In that case below check (!data_size) might result in error if
data_size== NULL, whereas its not error case when size == 0, i.e.VFIO_IRQ_SET_DATA_NONEflag set.Quoted 8 lines
drivers/vfio/vfio.c int vfio_info_add_capability(struct vfio_info_cap *caps, int cap_type_id, · line 1900
Quoted 5 lines
drivers/vfio/vfio.c +5
-0+if (hdr->argsz - minsz < hdr->count * size) +return -EINVAL ; + +if (!data_size) +return -EINVAL ;Redundant check as well.
This is not redundant. If you see above check, it sets its init value to 0 but doesn't fail.
Quoted 10 lines
drivers/vfio/vfio.c int vfio_info_add_capability(struct vfio_info_cap *caps, int cap_type_id, · line 1906
Quoted 6 lines
drivers/vfio/vfio.c +6
-0+ + *data_size = hdr->count * size; + } + +return 0 ; +}It does not really prepare anything as the name suggests. It looks like this is 2 different helpers actually:
Quoted 6 lines
Quoted 4 lines
int
vfio_set_irqs_validate()
andsize_tvfio_set_irqs_hdr_to_data_size()Later one is the prepare.
Does not like it prepares anything, just a simple converter.
Quoted 5 lines
Quoted 4 lines
And it would make it easier to review/bisect if 16/22 and 17/22 were merged into this one as this patch alone adds new code which it does not use and all 3 patches are fairly small.
Quoted 5 lines
Quoted 2 lines
I do had all 3 patch merged in one in earlier version of patchset. This is split as per Alex's suggestion.
I got this from another mail from Alex. Which I find strange but whatever, this is his realm anyway :)
Maybe you haven't noticed, but your patch series are often difficult to deal with, they almost always split across functional areas and maintainers. Splitting out code to common functions and _then_ updating the callers to make use of it is a common way to deal with that. We're in the same functional area here, but it's still good practice. Thanks,
Alex
Re: [PATCH v11 08/22] vfio iommu type1: Add find_iommu_group() function
In reply to Kirti Wankhede* Kirti Wankhede <[email protected]> [2016-11-05 02:40:42 +0530]:
Hi Kirti,
Quoted 14 lines
Add
find_iommu_group()Signed-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id I9d372f1ebe9eb01a5a21374b8a2b03f7df73601f--- drivers/vfio/vfio_iommu_type1.c | 58 ++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 24 deletions(-)drivers/vfio/vfio_iommu_type1.c +0
-0
drivers/vfio/vfio_iommu_type1.c static void vfio_test_domain_fgsp(struct vfio_domain *domain) · line 752
Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +2
-0@@ -748,11 +748,24 @@ static void vfio_test_domain_fgsp(struct vfio_domain *domain)__free_pages (pages, order); } +static struct vfio_group *find_iommu_group (struct vfio_domain *domain, +struct iommu_group *iommu_group)
nit: broken indentation.
Quoted 33 lines
drivers/vfio/vfio_iommu_type1.c +13
-5+{ +struct vfio_group *g; + +list_for_each_entry (g, &domain->group_list, next) { +if (g->iommu_group == iommu_group) +return g; + } + +return NULL ; +} +static int vfio_iommu_type1_attach_group (void *iommu_data,struct iommu_group *iommu_group) {struct vfio_iommu *iommu = iommu_data; -struct vfio_group *group, *g; +struct vfio_group *group;struct vfio_domain *domain, *d;struct bus_type *bus =NULL ;int ret;@@ -760,10 +773,7 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,mutex_lock (&iommu->lock);list_for_each_entry (d, &iommu->domain_list, next) { -list_for_each_entry (g, &d->group_list, next) { -if (g->iommu_group != iommu_group) -continue ; - +if (find_iommu_group (d, iommu_group)) {mutex_unlock (&iommu->lock);return -EINVAL ; }
drivers/vfio/vfio_iommu_type1.c static void vfio_iommu_type1_detach_group(void *iommu_data, · line 895
Quoted 4 lines
drivers/vfio/vfio_iommu_type1.c +1
-0@@ -882,28 +892,28 @@ static void vfio_iommu_type1_detach_group(void *iommu_data,mutex_lock (&iommu->lock); +
nit: empty line.
drivers/vfio/vfio_iommu_type1.c static void vfio_iommu_type1_detach_group(void *iommu_data, · line 899
Quoted 7 lines
drivers/vfio/vfio_iommu_type1.c +3
-3list_for_each_entry (domain, &iommu->domain_list, next) { -list_for_each_entry (group, &domain->group_list, next) { -if (group->iommu_group != iommu_group) -continue ; + group =find_iommu_group (domain, iommu_group); +if (!group) +continue ;
[...]
Looks good to me!
Signature · 1 line
Dong Jia
Re: [Qemu-devel] [PATCH v11 08/22] vfio iommu type1: Add find_iommu_group() function
In reply to Kirti Wankhede* Kirti Wankhede <[email protected]> [2016-11-05 02:40:42 +0530]:
Hi Kirti,
Quoted 14 lines
Add
find_iommu_group()Signed-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id I9d372f1ebe9eb01a5a21374b8a2b03f7df73601f--- drivers/vfio/vfio_iommu_type1.c | 58 ++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 24 deletions(-)drivers/vfio/vfio_iommu_type1.c +0
-0
drivers/vfio/vfio_iommu_type1.c static void vfio_test_domain_fgsp(struct vfio_domain *domain) · line 752
Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +2
-0@@ -748,11 +748,24 @@ static void vfio_test_domain_fgsp(struct vfio_domain *domain)__free_pages (pages, order); } +static struct vfio_group *find_iommu_group (struct vfio_domain *domain, +struct iommu_group *iommu_group)
nit: broken indentation.
Quoted 33 lines
drivers/vfio/vfio_iommu_type1.c +13
-5+{ +struct vfio_group *g; + +list_for_each_entry (g, &domain->group_list, next) { +if (g->iommu_group == iommu_group) +return g; + } + +return NULL ; +} +static int vfio_iommu_type1_attach_group (void *iommu_data,struct iommu_group *iommu_group) {struct vfio_iommu *iommu = iommu_data; -struct vfio_group *group, *g; +struct vfio_group *group;struct vfio_domain *domain, *d;struct bus_type *bus =NULL ;int ret;@@ -760,10 +773,7 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,mutex_lock (&iommu->lock);list_for_each_entry (d, &iommu->domain_list, next) { -list_for_each_entry (g, &d->group_list, next) { -if (g->iommu_group != iommu_group) -continue ; - +if (find_iommu_group (d, iommu_group)) {mutex_unlock (&iommu->lock);return -EINVAL ; }
drivers/vfio/vfio_iommu_type1.c static void vfio_iommu_type1_detach_group(void *iommu_data, · line 895
Quoted 4 lines
drivers/vfio/vfio_iommu_type1.c +1
-0@@ -882,28 +892,28 @@ static void vfio_iommu_type1_detach_group(void *iommu_data,mutex_lock (&iommu->lock); +
nit: empty line.
drivers/vfio/vfio_iommu_type1.c static void vfio_iommu_type1_detach_group(void *iommu_data, · line 899
Quoted 7 lines
drivers/vfio/vfio_iommu_type1.c +3
-3list_for_each_entry (domain, &iommu->domain_list, next) { -list_for_each_entry (group, &domain->group_list, next) { -if (group->iommu_group != iommu_group) -continue ; + group =find_iommu_group (domain, iommu_group); +if (!group) +continue ;
[...]
Looks good to me!
Signature · 1 line
Dong Jia
Re: [PATCH v11 09/22] vfio iommu type1: Add task structure to vfio_dma
In reply to Kirti Wankhede* Kirti Wankhede <[email protected]> [2016-11-05 02:40:43 +0530]:
Hi Kirti,
[...]
Quoted 31 lines
static int vfio_dma_do_map(struct
vfio_iommu*iommu,drivers/vfio/vfio_iommu_type1.c +11
-4struct vfio_iommu_type1_dma_map *map) {dma_addr_t iova = map->iova;unsigned long vaddr = map->vaddr;size_t size = map->size; -long npage;int ret =0 , prot =0 ;uint64_t mask;struct vfio_dma *dma; -unsigned long pfn; +struct vfio_addr_space *addr_space; +struct mm_struct *mm; +bool free_addr_space_on_err = false;/* Verify that none of our __u64 fields overflow */ if (map->size != size || map->vaddr != vaddr || map->iova != iova)@@ -608,47 +685,56 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu,mutex_lock (&iommu->lock);if (vfio_find_dma (iommu, iova, size)) { -mutex_unlock (&iommu->lock); -return -EEXIST ; + ret = -EEXIST ; + goto do_map_err; + } + + mm =get_task_mm (current); +if (!mm) { + ret = -ENODEV ; + goto do_map_err;
drivers/vfio/vfio_iommu_type1.c static int vfio_dma_do_map(struct vfio_iommu *iommu, · line 705
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10
-0+ } + + addr_space =vfio_find_addr_space (iommu, mm); +if (addr_space) { +atomic_inc (&addr_space->ref_count); +mmput (mm); + }else { + addr_space =kzalloc (sizeof (*addr_space),GFP_KERNEL ); +if (!addr_space) { + ret = -ENOMEM ;
No need to call (?):
mmput(mm);
Quoted 64 lines
drivers/vfio/vfio_iommu_type1.c +19
-26+ goto do_map_err; + } + addr_space->mm = mm; +atomic_set (&addr_space->ref_count,1 ); +list_add (&addr_space->next, &iommu->addr_space_list); + free_addr_space_on_err = true; } dma =kzalloc (sizeof (*dma),GFP_KERNEL );if (!dma) { -mutex_unlock (&iommu->lock); -return -ENOMEM ; +if (free_addr_space_on_err) { +mmput (mm); +list_del (&addr_space->next); +kfree (addr_space); + } + ret = -ENOMEM ; + goto do_map_err; } dma->iova = iova; dma->vaddr = vaddr; dma->prot = prot; + dma->addr_space = addr_space; +get_task_struct (current); + dma->task = current; + dma->mlock_cap =capable (CAP_IPC_LOCK );/* Insert zero-sized and grow as we map chunks of it */ vfio_link_dma (iommu, dma); -while (size) { -/* Pin a contiguous chunk of memory */ - npage =__vfio_pin_pages_remote (vaddr + dma->size, - size >>PAGE_SHIFT , prot, &pfn); -if (npage <=0 ) { -WARN_ON (!npage); - ret = (int )npage; -break ; - } - -/* Map it! */ - ret =vfio_iommu_map (iommu, iova + dma->size, pfn, npage, prot); -if (ret) { -__vfio_unpin_pages_remote (pfn, npage, prot, true); -break ; - } - - size -= npage <<PAGE_SHIFT ; - dma->size += npage <<PAGE_SHIFT ; - } - -if (ret) -vfio_remove_dma (iommu, dma); - + ret =vfio_pin_map_dma (iommu, dma, size); +do_map_err:mutex_unlock (&iommu->lock);return ret; }Signature · 1 line
2.7.0
Signature · 1 line
Dong Jia
Re: [Qemu-devel] [PATCH v11 09/22] vfio iommu type1: Add task structure to vfio_dma
In reply to Kirti Wankhede* Kirti Wankhede <[email protected]> [2016-11-05 02:40:43 +0530]:
Hi Kirti,
[...]
Quoted 31 lines
static int vfio_dma_do_map(struct
vfio_iommu*iommu,drivers/vfio/vfio_iommu_type1.c +11
-4struct vfio_iommu_type1_dma_map *map) {dma_addr_t iova = map->iova;unsigned long vaddr = map->vaddr;size_t size = map->size; -long npage;int ret =0 , prot =0 ;uint64_t mask;struct vfio_dma *dma; -unsigned long pfn; +struct vfio_addr_space *addr_space; +struct mm_struct *mm; +bool free_addr_space_on_err = false;/* Verify that none of our __u64 fields overflow */ if (map->size != size || map->vaddr != vaddr || map->iova != iova)@@ -608,47 +685,56 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu,mutex_lock (&iommu->lock);if (vfio_find_dma (iommu, iova, size)) { -mutex_unlock (&iommu->lock); -return -EEXIST ; + ret = -EEXIST ; + goto do_map_err; + } + + mm =get_task_mm (current); +if (!mm) { + ret = -ENODEV ; + goto do_map_err;
drivers/vfio/vfio_iommu_type1.c static int vfio_dma_do_map(struct vfio_iommu *iommu, · line 705
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10
-0+ } + + addr_space =vfio_find_addr_space (iommu, mm); +if (addr_space) { +atomic_inc (&addr_space->ref_count); +mmput (mm); + }else { + addr_space =kzalloc (sizeof (*addr_space),GFP_KERNEL ); +if (!addr_space) { + ret = -ENOMEM ;
No need to call (?):
mmput(mm);
Quoted 64 lines
drivers/vfio/vfio_iommu_type1.c +19
-26+ goto do_map_err; + } + addr_space->mm = mm; +atomic_set (&addr_space->ref_count,1 ); +list_add (&addr_space->next, &iommu->addr_space_list); + free_addr_space_on_err = true; } dma =kzalloc (sizeof (*dma),GFP_KERNEL );if (!dma) { -mutex_unlock (&iommu->lock); -return -ENOMEM ; +if (free_addr_space_on_err) { +mmput (mm); +list_del (&addr_space->next); +kfree (addr_space); + } + ret = -ENOMEM ; + goto do_map_err; } dma->iova = iova; dma->vaddr = vaddr; dma->prot = prot; + dma->addr_space = addr_space; +get_task_struct (current); + dma->task = current; + dma->mlock_cap =capable (CAP_IPC_LOCK );/* Insert zero-sized and grow as we map chunks of it */ vfio_link_dma (iommu, dma); -while (size) { -/* Pin a contiguous chunk of memory */ - npage =__vfio_pin_pages_remote (vaddr + dma->size, - size >>PAGE_SHIFT , prot, &pfn); -if (npage <=0 ) { -WARN_ON (!npage); - ret = (int )npage; -break ; - } - -/* Map it! */ - ret =vfio_iommu_map (iommu, iova + dma->size, pfn, npage, prot); -if (ret) { -__vfio_unpin_pages_remote (pfn, npage, prot, true); -break ; - } - - size -= npage <<PAGE_SHIFT ; - dma->size += npage <<PAGE_SHIFT ; - } - -if (ret) -vfio_remove_dma (iommu, dma); - + ret =vfio_pin_map_dma (iommu, dma, size); +do_map_err:mutex_unlock (&iommu->lock);return ret; }Signature · 1 line
2.7.0
Signature · 1 line
Dong Jia
Re: [PATCH v11 10/22] vfio iommu type1: Add support for mediated devices
In reply to Kirti Wankhede* Kirti Wankhede <[email protected]> [2016-11-05 02:40:44 +0530]:
Hi Kirti,
[...]
Quoted 4 lines
drivers/vfio/vfio_iommu_type1.c +0
-0
[...]
drivers/vfio/vfio_iommu_type1.c static struct vfio_addr_space *vfio_find_addr_space(struct vfio_iommu *iommu, · line 260
Quoted 13 lines
drivers/vfio/vfio_iommu_type1.c +13
-0+static int vfio_pfn_account (struct vfio_addr_space *addr_space, +unsigned long pfn) +{ +struct vfio_pfn *p; +int ret =1 ; + +mutex_lock (&addr_space->pfn_list_lock); + p =vfio_find_pfn (addr_space, pfn); +if (p) + ret =0 ; +mutex_unlock (&addr_space->pfn_list_lock); +return ret; +}
We could save a local variables(@ret) here by:
return (p ? 0 : 1);
Quoted 4 lines
drivers/vfio/vfio_iommu_type1.c +1
-0+struct vwork {struct mm_struct *mm;long npage;
[...]
Signature · 1 line
Dong Jia
Re: [Qemu-devel] [PATCH v11 10/22] vfio iommu type1: Add support for mediated devices
In reply to Kirti Wankhede* Kirti Wankhede <[email protected]> [2016-11-05 02:40:44 +0530]:
Hi Kirti,
[...]
Quoted 4 lines
drivers/vfio/vfio_iommu_type1.c +0
-0
[...]
drivers/vfio/vfio_iommu_type1.c static struct vfio_addr_space *vfio_find_addr_space(struct vfio_iommu *iommu, · line 260
Quoted 13 lines
drivers/vfio/vfio_iommu_type1.c +13
-0+static int vfio_pfn_account (struct vfio_addr_space *addr_space, +unsigned long pfn) +{ +struct vfio_pfn *p; +int ret =1 ; + +mutex_lock (&addr_space->pfn_list_lock); + p =vfio_find_pfn (addr_space, pfn); +if (p) + ret =0 ; +mutex_unlock (&addr_space->pfn_list_lock); +return ret; +}
We could save a local variables(@ret) here by:
return (p ? 0 : 1);
Quoted 4 lines
drivers/vfio/vfio_iommu_type1.c +1
-0+struct vwork {struct mm_struct *mm;long npage;
[...]
Signature · 1 line
Dong Jia
Kirti Wankhede
maintainerRe: [PATCH v11 11/22] vfio iommu: Add blocking notifier to notify DMA_UNMAP
In reply to Alex WilliamsonQuoted 125 lines · Alex Williamson
Quoted 75 lines · On Wed, 9 Nov 2016 01:29:19 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 65 lines · Alex Williamson
Quoted 59 lines · On Tue, 8 Nov 2016 21:56:29 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 3 lines · Alex Williamson
On Sat, 5 Nov 2016 02:40:45 +0530
Kirti Wankhede <[email protected]> wrote:...
Quoted 8 lines
Quoted 2 lines
+int
vfio_register_notifier(struct device *dev, struct notifier_block *nb)Is the expectation here that this is a generic notifier for all
vfio->mdevsignaling? That should probably be made clear in the mdev API to avoid vendor drivers assuming their notifier callback only occurs for unmaps, even if that's currently the case.Ok. Adding comment about notifier callback in
mdev_devicewhich is part of next patch....
Quoted 12 lines
Quoted 12 lines
drivers/vfio/vfio_iommu_type1.c +2
-1mutex_lock (&iommu->lock); -if (!iommu->external_domain) { +/* Fail if notifier list is empty */ +if ((!iommu->external_domain) || (!iommu->notifier.head)) { ret = -EINVAL ; goto pin_done; }@@ -867,6 +870,11 @@ unlock:/* Report how much was unmapped */ unmap->size = unmapped;Quoted 7 lines
drivers/vfio/vfio_iommu_type1.c unlock: · line 876
Quoted 4 lines
drivers/vfio/vfio_iommu_type1.c +4
-0+if (unmapped && iommu->external_domain) +blocking_notifier_call_chain (&iommu->notifier, +VFIO_IOMMU_NOTIFY_DMA_UNMAP , + unmap);This is after the fact, there's already a gap here where pages are unpinned and the mdev device is still running.
Oh, there is a bug here, now
unpin_pages()takeuser_pfnas argument and findvfio_dma. If its not found, it doesn't unpin pages. We have to call this notifier beforevfio_remove_dma(). But if we call this beforevfio_remove_dma()there will be deadlock sinceiommu->lockis already held here andvfio_iommu_type1_unpin_pages()will also try to holdiommu->lock.
If we want to callblocking_notifier_call_chain()beforevfio_remove_dma(), sequence should be:unmapped +=
dma->size;mutex_unlock(&iommu->lock);if (iommu->external_domain)) { struct vfio_iommu_type1_dma_unmap nb_unmap; nb_unmap.iova = dma->iova; nb_unmap.size = dma->size; blocking_notifier_call_chain(&iommu->notifier, VFIO_IOMMU_NOTIFY_DMA_UNMAP, &nb_unmap); }Quoted 2 lines
mutex_lock(&iommu->lock);vfio_remove_dma(iommu, dma);Quoted 8 lines
It seems like it would be worthwhile to have the rb-tree rooted in the vfio-dma, then we only need to call the notifier if there are pages pinned within that vfio-dma (ie. the rb-tree is not empty). We can then release the lock call the notifier, re-acquire the lock, and
BUG_ONif the rb-tree still is not empty. We might get duplicate pfns between separatevfio_dmastructs, but as I mentioned in other replies, that seems like an exception that we don't need to optimize for.Quoted 3 lines
If we don't optimize for the case where iova from different
vfio_dmaare mapped to same pfn and we would not consider this case for page accounting then:Just to clarify, the current code (not handling mdevs) will pin and do page accounting per iova, regardless of whether the iova translates to a unique pfn. As long as we do no worse than that, I'm ok.
Quoted 6 lines
- have rb tree of pinned iova, where key would be iova, in each
vfio_dmastructure.
- iova tracking structure would have iova andref_countonly.
- page accounting would only count number of iova's inrb_tree, case where different iova could map to same pfn would not be considered in this implementation for now.Quoted 8 lines
-
vfio_unpin_pages()would haveuser_pfnand pfn as input, we would validate that iova exist in rb tree and trust vendor driver that corresponding pfn is correct, there is no validation of pfn. If want validate pfn, call GUP, verify pfn and callput_pfn().
- In .release() or .detach_group() path, if there are entries in this rb tree, call GUP again using that iova, get pfn and then callput_pfn(pfn)for ref_count+1 times. This is because we are not keeping pfn in our tracking logic.Wait a sec, if we detach a group from the container and it's not the last group in the container (which would trigger a release), we can't assume anything about which
vfio_dmaentries were associated with that device. The vendor driver, through the release of the device(s) within that group, needs to unpin. In a container release, we need to send a notifier to the vendor driver(s) to cause an unpin. This is the only mechanism we have to ensure that vendor drivers are not leaking references. If during the release, after the notifier, if anyvfio_pfnsremain, we need toBUG_ON, just like we need to do for any otherDMA_UNMAP.Also, I'll say it again, I also don't like this API of passing around potentially giant arrays, and especially the API of relying on the vendor driver to tell us an arbitrary pfn to unpin. If we make the assumption that vendor drivers do not pin lots and lots of memory, perhaps we could use a struct
vfio_pfnas:struct vfio_pfn { struct rb_node node; dma_addr_t iova; /* key */ unsigned long pfn; atomic_t ref_count; };
Quoted 10 lines
This puts us at 44-bytes per pfn, which isn't great, but I think it puts us in a better position with the API that we could make use of a page-table or sparse array in the future that would eliminate the
rb_nodeand make the iova implicit in the location of the data structure. That would leave only the pfn andref_count, which could potentially be combined into a single 8-byte field if we had pervfio_dma(or higher) locking to avoid theatomic_t(and we're happy that the reference count is always less thanPAGE_SIZE, ie. we could fail pinning if we get to that point).
Ok.
- I'll have above structure to track pinned pfn for now and a rb-tree in vfio_dma structure that would keep track of pages pinned in that range, dma->iova to dma->iova + dma->size.
- Key for pfn_list rb-tree would be iova, instead of pfn.
- Removing address space structure. vfio_dma keeps task structure, which would be used to get mm structure (using get_task_mm(task) and mmput(mm)) for pin/unpin and page accounting.
- vfio_unpin_pages() would have array of user_pfns as input argument, instead of array of pfns.
- On vfio_pin_pages(), pinning would happen once. On later call to vfio_pin_pages() with same user_pfn, if iova is found in pfn_list, only ref_count would be incremented.
- In vfio_unpin_pages(), ref_count is decremented and page will be unpinned when ref_count is 0.
- For vfio_pin_pages() and vfio_unpin_pages() input array, number of elements in array should be less that PAGE_SIZE. If vendor driver wants to use for more pages, array should be split it in chunks of PAGE_SIZE.
- Updating page accounting logic with above changes.
Thanks,
Kirti
Quoted 7 lines
That would allow for the unpin call to not provide the pfn, so could we then look at whether we need the batching provided by the iova array at all? I don't have a feel for the size of memory that gets pinned by the vendor driver, the frequency of pinning, or whether usage of hugepages for the guest is likely to translate into contiguous memory requests through this API. What's your feeling? Thanks,
Re: [PATCH v11 11/22] vfio iommu: Add blocking notifier to notify DMA_UNMAP
Quoted 147 lines · On Mon, 14 Nov 2016 13:22:08 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 125 lines · Alex Williamson
Quoted 75 lines · On Wed, 9 Nov 2016 01:29:19 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 65 lines · Alex Williamson
Quoted 59 lines · On Tue, 8 Nov 2016 21:56:29 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 3 lines · Alex Williamson
On Sat, 5 Nov 2016 02:40:45 +0530
Kirti Wankhede <[email protected]> wrote:...
Quoted 8 lines
Quoted 2 lines
+int
vfio_register_notifier(struct device *dev, struct notifier_block *nb)Is the expectation here that this is a generic notifier for all
vfio->mdevsignaling? That should probably be made clear in the mdev API to avoid vendor drivers assuming their notifier callback only occurs for unmaps, even if that's currently the case.Ok. Adding comment about notifier callback in
mdev_devicewhich is part of next patch....
Quoted 12 lines
Quoted 12 lines
(quoted diff) +2
-1mutex_lock(&iommu->lock); - if (!iommu->external_domain) { + /* Fail if notifier list is empty */ + if ((!iommu->external_domain) || (!iommu->notifier.head)) { ret = -EINVAL; goto pin_done; }@@ -867,6 +870,11 @@ unlock: /* Report how much was unmapped */ unmap->size = unmapped;Quoted 7 lines
Review unlock: · line 876
Quoted 4 lines
(quoted diff) +4
-0+ if (unmapped && iommu->external_domain) + blocking_notifier_call_chain(&iommu->notifier, + VFIO_IOMMU_NOTIFY_DMA_UNMAP, + unmap);This is after the fact, there's already a gap here where pages are unpinned and the mdev device is still running.
Oh, there is a bug here, now
unpin_pages()takeuser_pfnas argument and findvfio_dma. If its not found, it doesn't unpin pages. We have to call this notifier beforevfio_remove_dma(). But if we call this beforevfio_remove_dma()there will be deadlock sinceiommu->lockis already held here andvfio_iommu_type1_unpin_pages()will also try to holdiommu->lock.
If we want to callblocking_notifier_call_chain()beforevfio_remove_dma(), sequence should be:unmapped +=
dma->size;mutex_unlock(&iommu->lock);if (iommu->external_domain)) { struct vfio_iommu_type1_dma_unmap nb_unmap; nb_unmap.iova = dma->iova; nb_unmap.size = dma->size; blocking_notifier_call_chain(&iommu->notifier, VFIO_IOMMU_NOTIFY_DMA_UNMAP, &nb_unmap); }Quoted 2 lines
mutex_lock(&iommu->lock);vfio_remove_dma(iommu, dma);Quoted 8 lines
It seems like it would be worthwhile to have the rb-tree rooted in the vfio-dma, then we only need to call the notifier if there are pages pinned within that vfio-dma (ie. the rb-tree is not empty). We can then release the lock call the notifier, re-acquire the lock, and
BUG_ONif the rb-tree still is not empty. We might get duplicate pfns between separatevfio_dmastructs, but as I mentioned in other replies, that seems like an exception that we don't need to optimize for.Quoted 3 lines
If we don't optimize for the case where iova from different
vfio_dmaare mapped to same pfn and we would not consider this case for page accounting then:Just to clarify, the current code (not handling mdevs) will pin and do page accounting per iova, regardless of whether the iova translates to a unique pfn. As long as we do no worse than that, I'm ok.
Quoted 6 lines
- have rb tree of pinned iova, where key would be iova, in each
vfio_dmastructure.
- iova tracking structure would have iova andref_countonly.
- page accounting would only count number of iova's inrb_tree, case where different iova could map to same pfn would not be considered in this implementation for now.Quoted 8 lines
-
vfio_unpin_pages()would haveuser_pfnand pfn as input, we would validate that iova exist in rb tree and trust vendor driver that corresponding pfn is correct, there is no validation of pfn. If want validate pfn, call GUP, verify pfn and callput_pfn().
- In .release() or .detach_group() path, if there are entries in this rb tree, call GUP again using that iova, get pfn and then callput_pfn(pfn)for ref_count+1 times. This is because we are not keeping pfn in our tracking logic.Wait a sec, if we detach a group from the container and it's not the last group in the container (which would trigger a release), we can't assume anything about which
vfio_dmaentries were associated with that device. The vendor driver, through the release of the device(s) within that group, needs to unpin. In a container release, we need to send a notifier to the vendor driver(s) to cause an unpin. This is the only mechanism we have to ensure that vendor drivers are not leaking references. If during the release, after the notifier, if anyvfio_pfnsremain, we need toBUG_ON, just like we need to do for any otherDMA_UNMAP.Also, I'll say it again, I also don't like this API of passing around potentially giant arrays, and especially the API of relying on the vendor driver to tell us an arbitrary pfn to unpin. If we make the assumption that vendor drivers do not pin lots and lots of memory, perhaps we could use a struct
vfio_pfnas:struct vfio_pfn { struct rb_node node; dma_addr_t iova; /* key */ unsigned long pfn; atomic_t ref_count; };Quoted 10 lines
This puts us at 44-bytes per pfn, which isn't great, but I think it puts us in a better position with the API that we could make use of a page-table or sparse array in the future that would eliminate the
rb_nodeand make the iova implicit in the location of the data structure. That would leave only the pfn andref_count, which could potentially be combined into a single 8-byte field if we had pervfio_dma(or higher) locking to avoid theatomic_t(and we're happy that the reference count is always less thanPAGE_SIZE, ie. we could fail pinning if we get to that point).Ok.
- I'll have above structure to track pinned pfn for now and a rb-tree invfio_dmastructure that would keep track of pages pinned in that range,dma->iovatodma->iova+dma->size.
- Key forpfn_listrb-tree would be iova, instead of pfn.
- Removing address space structure.vfio_dmakeeps task structure, which would be used to get mm structure (usingget_task_mm(task)and mmput(mm)) for pin/unpin and page accounting.
-vfio_unpin_pages()would have array ofuser_pfnsas input argument, instead of array of pfns.
Quoted 8 lines
- On
vfio_pin_pages(), pinning would happen once. On later call tovfio_pin_pages()with sameuser_pfn, if iova is found inpfn_list, onlyref_countwould be incremented.
- Invfio_unpin_pages(),ref_countis decremented and page will be unpinned whenref_countis 0.
- Forvfio_pin_pages()andvfio_unpin_pages()input array, number of elements in array should be less thatPAGE_SIZE. If vendor driver wants to use for more pages, array should be split it in chunks ofPAGE_SIZE.
Yes, this is what we discussed offline, the size of the arrays should never exceed PAGE_SIZE, therefore the number of entries should never exceed PAGE_SIZE/sizeof(pfn). The iommu driver should fault with -E2BIG if the vendor driver attempts to exceed this.
Quoted 1 line
- Updating page accounting logic with above changes.
Thanks,
Alex
Re: [PATCH v11 10/22] vfio iommu type1: Add support for mediated devices
In reply to Alexey KardashevskiyQuoted 29 lines · Alexey Kardashevskiy
Quoted 25 lines · Kirti Wankhede
VFIO IOMMU drivers are designed for the devices which are IOMMU capable. Mediated device only uses IOMMU APIs, the underlying hardware can be managed by an IOMMU domain.
Aim of this change is:
- To use most of the code of TYPE1 IOMMU driver for mediated devices
- To support direct assigned device and mediated device in single moduleThis change adds pin and unpin support for mediated device to TYPE1 IOMMU backend module. More details:
-vfio_pin_pages()callback here uses task and address space ofvfio_dma, that is, of the process who mapped that iova range.
- Addedpfn_listtracking logic to address space structure. All pages pinned through this interface are trached in its address space.
- Pinned pages list is used to verify unpinning request and to unpin remaining pages while detaching the group for that device.
- Page accounting is updated to account in its address space where the pages are pinned/unpinned.
- Accouting for mdev device is only done if there is no iommu capable domain in the container. When there is a direct device assigned to the container and that domain is iommu capable, all pages are already pinned duringDMA_MAP.
- Page accouting is updated on hot plug and unplug mdev device and pass through device.Quoted 2 lines
Tested by assigning below combinations of devices to a single VM:
- GPU pass through only
Quoted 7 lines
This does not require this patchset, right?
Quoted 1 line
- vGPU device only
Out of curiosity - how exactly did you test this? The exact GPU, how to create vGPU, what was the QEMU command line and the guest does with this passed device? Thanks.
ping?
Signature · 1 line
Alexey
Kirti Wankhede
maintainerRe: [PATCH v11 10/22] vfio iommu type1: Add support for mediated devices
Quoted 27 lines · Alexey Kardashevskiy
Quoted 26 lines · Alexey Kardashevskiy
Quoted 25 lines · Kirti Wankhede
VFIO IOMMU drivers are designed for the devices which are IOMMU capable. Mediated device only uses IOMMU APIs, the underlying hardware can be managed by an IOMMU domain.
Aim of this change is:
- To use most of the code of TYPE1 IOMMU driver for mediated devices
- To support direct assigned device and mediated device in single moduleThis change adds pin and unpin support for mediated device to TYPE1 IOMMU backend module. More details:
-vfio_pin_pages()callback here uses task and address space ofvfio_dma, that is, of the process who mapped that iova range.
- Addedpfn_listtracking logic to address space structure. All pages pinned through this interface are trached in its address space.
- Pinned pages list is used to verify unpinning request and to unpin remaining pages while detaching the group for that device.
- Page accounting is updated to account in its address space where the pages are pinned/unpinned.
- Accouting for mdev device is only done if there is no iommu capable domain in the container. When there is a direct device assigned to the container and that domain is iommu capable, all pages are already pinned duringDMA_MAP.
- Page accouting is updated on hot plug and unplug mdev device and pass through device.
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Tested by assigning below combinations of devices to a single VM:
- GPU pass through onlyThis does not require this patchset, right?
Sorry I missed this earlier.
This testing is required for this patch, because this patch touches code that is used for direct device assignment. Also for page accounting, all cases are considered i.e. when there is only pass through device in a container, when there is pass through device + vGPU device in a container. Also have to test that pages are pinned properly when device is hotplugged. In that case vfio_iommu_replay() is called to take necessary action.
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
- vGPU device only
Out of curiosity - how exactly did you test this? The exact GPU, how to create vGPU, what was the QEMU command line and the guest does with this passed device? Thanks.
ping?
I'm testing this code with M60, with custom changes in our driver. Steps how to create mediated device are listed in Documentation/vfio-mediated-device.txt for sample mtty driver. Same steps I'm following for GPU. Quoting those steps here for you:
2. Create a mediated device by using the dummy device that you created in the
previous step.
# echo "83b8f4f2-509f-382f-3c1e-e6bfe0fa1001" > \
/sys/devices/virtual/mtty/mtty/mdev_supported_types/mtty-2/create
3. Add parameters to qemu-kvm.
-device vfio-pci,\
sysfsdev=/sys/bus/mdev/devices/83b8f4f2-509f-382f-3c1e-e6bfe0fa1001Thanks,
Kirti
Re: [PATCH v11 10/22] vfio iommu type1: Add support for mediated devices
Quoted 36 lines · Kirti Wankhede
Quoted 27 lines · Alexey Kardashevskiy
Quoted 26 lines · Alexey Kardashevskiy
Quoted 25 lines · Kirti Wankhede
VFIO IOMMU drivers are designed for the devices which are IOMMU capable. Mediated device only uses IOMMU APIs, the underlying hardware can be managed by an IOMMU domain.
Aim of this change is:
- To use most of the code of TYPE1 IOMMU driver for mediated devices
- To support direct assigned device and mediated device in single moduleThis change adds pin and unpin support for mediated device to TYPE1 IOMMU backend module. More details:
-vfio_pin_pages()callback here uses task and address space ofvfio_dma, that is, of the process who mapped that iova range.
- Addedpfn_listtracking logic to address space structure. All pages pinned through this interface are trached in its address space.
- Pinned pages list is used to verify unpinning request and to unpin remaining pages while detaching the group for that device.
- Page accounting is updated to account in its address space where the pages are pinned/unpinned.
- Accouting for mdev device is only done if there is no iommu capable domain in the container. When there is a direct device assigned to the container and that domain is iommu capable, all pages are already pinned duringDMA_MAP.
- Page accouting is updated on hot plug and unplug mdev device and pass through device.Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Tested by assigning below combinations of devices to a single VM:
- GPU pass through onlyThis does not require this patchset, right?
Quoted 8 lines
Sorry I missed this earlier.
This testing is required for this patch, because this patch touches code that is used for direct device assignment. Also for page accounting, all cases are considered i.e. when there is only pass through device in a container, when there is pass through device + vGPU device in a container. Also have to test that pages are pinned properly when device is hotplugged. In that casevfio_iommu_replay()is called to take necessary action.
So in this particular test you are only testing that the patchset did not break the already existing functionality, is that correct?
Quoted 11 lines
Quoted 9 lines
Quoted 6 lines
Quoted 2 lines
- vGPU device only
Out of curiosity - how exactly did you test this? The exact GPU, how to create vGPU, what was the QEMU command line and the guest does with this passed device? Thanks.
ping?
I'm testing this code with M60, with custom changes in our driver.
Is this shared anywhere? What does the mediated driver do? Can Tesla K80 do the same thing, or [10de:15fe] (whatever its name is)?
Quoted 3 lines
Steps how to create mediated device are listed in
Documentation/vfio-mediated-device.txtfor sample mtty driver. Same steps I'm following for GPU. Quoting those steps here for you:
Nah, I saw this, I was wondering about actual hardware :) Like when you say "tested with vGPU" - I am wondering what is passed to the guest and how the guest is actually using it.
Quoted 13 lines
2. Create a mediated device by using the dummy device that you created in the
previous step.# echo "83b8f4f2-509f-382f-3c1e-e6bfe0fa1001" > \
/sys/devices/virtual/mtty/mtty/mdev_supported_types/mtty-2/create3. Add parameters to qemu-kvm.
-device vfio-pci,\ sysfsdev=/sys/bus/mdev/devices/83b8f4f2-509f-382f-3c1e-e6bfe0fa1001
Signature · 1 line
Alexey
Kirti Wankhede
maintainerRe: [PATCH v11 10/22] vfio iommu type1: Add support for mediated devices
Quoted 46 lines · Alexey Kardashevskiy
Quoted 36 lines · Kirti Wankhede
Quoted 27 lines · Alexey Kardashevskiy
Quoted 26 lines · Alexey Kardashevskiy
Quoted 25 lines · Kirti Wankhede
VFIO IOMMU drivers are designed for the devices which are IOMMU capable. Mediated device only uses IOMMU APIs, the underlying hardware can be managed by an IOMMU domain.
Aim of this change is:
- To use most of the code of TYPE1 IOMMU driver for mediated devices
- To support direct assigned device and mediated device in single moduleThis change adds pin and unpin support for mediated device to TYPE1 IOMMU backend module. More details:
-vfio_pin_pages()callback here uses task and address space ofvfio_dma, that is, of the process who mapped that iova range.
- Addedpfn_listtracking logic to address space structure. All pages pinned through this interface are trached in its address space.
- Pinned pages list is used to verify unpinning request and to unpin remaining pages while detaching the group for that device.
- Page accounting is updated to account in its address space where the pages are pinned/unpinned.
- Accouting for mdev device is only done if there is no iommu capable domain in the container. When there is a direct device assigned to the container and that domain is iommu capable, all pages are already pinned duringDMA_MAP.
- Page accouting is updated on hot plug and unplug mdev device and pass through device.Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Tested by assigning below combinations of devices to a single VM:
- GPU pass through onlyThis does not require this patchset, right?
Quoted 8 lines
Sorry I missed this earlier.
This testing is required for this patch, because this patch touches code that is used for direct device assignment. Also for page accounting, all cases are considered i.e. when there is only pass through device in a container, when there is pass through device + vGPU device in a container. Also have to test that pages are pinned properly when device is hotplugged. In that casevfio_iommu_replay()is called to take necessary action.
Quoted 4 lines
So in this particular test you are only testing that the patchset did not break the already existing functionality, is that correct?
Yes.
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 2 lines
- vGPU device only
Out of curiosity - how exactly did you test this? The exact GPU, how to create vGPU, what was the QEMU command line and the guest does with this passed device? Thanks.
Quoted 10 lines
Quoted 4 lines
Quoted 2 lines
ping?
I'm testing this code with M60, with custom changes in our driver.
Is this shared anywhere? What does the mediated driver do? Can Tesla K80 do the same thing, or [10de:15fe] (whatever its name is)?
No, we are still in the process of upstreaming this patchset. After that we would prepare official drivers. There is no timeline for the driver release yet.
Quoted 10 lines
Quoted 3 lines
Steps how to create mediated device are listed in
Documentation/vfio-mediated-device.txtfor sample mtty driver. Same steps I'm following for GPU. Quoting those steps here for you:Nah, I saw this, I was wondering about actual hardware :) Like when you say "tested with vGPU" - I am wondering what is passed to the guest and how the guest is actually using it.
Its similar to mtty sample driver, that simulates serial port and so you see serial ports in guest. For vGPU, you see a virtual GPU in guest.
Thanks,
Kirti
Quoted 17 lines
Quoted 13 lines
2. Create a mediated device by using the dummy device that you created in the
previous step.# echo "83b8f4f2-509f-382f-3c1e-e6bfe0fa1001" > \
/sys/devices/virtual/mtty/mtty/mdev_supported_types/mtty-2/create3. Add parameters to qemu-kvm.
-device vfio-pci,\ sysfsdev=/sys/bus/mdev/devices/83b8f4f2-509f-382f-3c1e-e6bfe0fa1001