Skip to contentSkip to conversation

Add Mediated device support

PATCH Superseded Kirti Wankhede 7 participants 75 messages Oct 17, 2016 – Dec 7, 2016

Subsystems (5)

v9 of 14 Superseded Latest is v14 →

lore
Share & tools
lore

Kirti Wankhede

author maintainer
#0 lore raw

v9 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 v9?
mdev-core:
- added class named 'mdev_bus' that contains links to devices that are registered with the mdev core driver.
- The [<type-id>] name is created by adding the the device driver string as a prefix to the string provided by the vendor driver.
- 'device_api' attribute should be provided by vendor driver and should show which device API is being created, for example, "vfio-pci" for a PCI device.
- Renamed link to its type in mdev device directory to 'mdev_type'

vfio:
- Split commits in multple individual commits
- Added function to get device_api string based on vfio_device_info.flags.

vfio_iommu_type1:
- Handled the case if all devices attached to the normal IOMMU API domain go away and mdev device still exist in domain. Updated page accounting for local domain.
- Similarly if device is attached to normal IOMMU API domain, mappings are establised and page accounting is updated accordingly.
- Tested hot-plug and hot-unplug of vGPU and GPU pass through device with Linux VM.

Documentation:
- Updated Documentation and sample driver, mtty.c, accordingly.

Kirti Wankhede (12):
vfio: Mediated device Core driver
vfio: VFIO based driver for Mediated devices
vfio: Rearrange functions to get vfio_group from dev vfio iommu: Add support for mediated devices
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: Add function to get device_api string from
vfio_device_info.flags
docs: Add Documentation for Mediated devices
docs: Sample driver to demonstrate how to use Mediated device
framework.

 Documentation/vfio-mdev/Makefile                 |   13 +
 Documentation/vfio-mdev/mtty.c                   | 1429 ++++++++++++++++++++++
 Documentation/vfio-mdev/vfio-mediated-device.txt |  389 ++++++
 drivers/vfio/Kconfig                             |    1 +
 drivers/vfio/Makefile                            |    1 +
 drivers/vfio/mdev/Kconfig                        |   18 +
 drivers/vfio/mdev/Makefile                       |    5 +
 drivers/vfio/mdev/mdev_core.c                    |  372 ++++++
 drivers/vfio/mdev/mdev_driver.c                  |  128 ++
 drivers/vfio/mdev/mdev_private.h                 |   41 +
 drivers/vfio/mdev/mdev_sysfs.c                   |  296 +++++
 drivers/vfio/mdev/vfio_mdev.c                    |  148 +++
 drivers/vfio/pci/vfio_pci.c                      |  101 +-
 drivers/vfio/platform/vfio_platform_common.c     |   31 +-
 drivers/vfio/vfio.c                              |  287 ++++-
 drivers/vfio/vfio_iommu_type1.c                  |  692 +++++++++--
 include/linux/mdev.h                             |  177 +++
 include/linux/vfio.h                             |   23 +-
 18 files changed, 3948 insertions(+), 204 deletions(-)
 create mode 100644 Documentation/vfio-mdev/Makefile
 create mode 100644 Documentation/vfio-mdev/mtty.c
 create mode 100644 Documentation/vfio-mdev/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
Signature · 1 line
2.7.0
author maintainer
#1 lore raw

PATCH v9 1/12 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);

Medisted 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 driver for a mediated device.
* remove: to free resources in driver when mediated device is destroyed.
* open: open callback of mediated device
* release: release callback of mediated device
* read : read emulation callback.
* write: write emulation callback.
* mmap: mmap emulation callback.
* ioctl: ioctl 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.

Change-Id I73a5084574270b14541c529461ea2f03c292d510
---
 drivers/vfio/Kconfig             |   1 +
 drivers/vfio/Makefile            |   1 +
 drivers/vfio/mdev/Kconfig        |  11 ++
 drivers/vfio/mdev/Makefile       |   4 +
 drivers/vfio/mdev/mdev_core.c    | 372 +++++++++++++++++++++++++++++++++++++++
 drivers/vfio/mdev/mdev_driver.c  | 128 ++++++++++++++
 drivers/vfio/mdev/mdev_private.h |  41 +++++
 drivers/vfio/mdev/mdev_sysfs.c   | 296 +++++++++++++++++++++++++++++++
 include/linux/mdev.h             | 177 +++++++++++++++++++
 9 files changed, 1031 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
index da6e2ce77495..23eced02aaf6 100644
@@ -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
index 7b8a31f63fea..4a23c13b6be4 100644
@@ -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 +11 -0
new file mode 100644
index 000000000000..93addace9a67
@@ -0,0 +1,11 @@
+
+config VFIO_MDEV
+    tristate "Mediated device driver framework"
+    depends on VFIO
+    default n
+    help
+        Provides a framework to virtualize devices which don't have SR_IOV
+	capability built-in.
+	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
new file mode 100644
index 000000000000..31bc04801d94
@@ -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 +372 -0

372 lines not on this page · raw message

drivers/vfio/mdev/mdev_driver.c new file +128 -0

128 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 +296 -0

296 lines not on this page · raw message

include/linux/mdev.h new file +177 -0

177 lines not on this page · raw message

Signature · 1 line
2.7.0
author maintainer
#11 lore raw

PATCH v9 2/12 vfio: VFIO based driver for Mediated devices

In reply to Kirti Wankhede

vfio_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.

Change-Id I583f4734752971d3d112324d69e2508c88f359ec
---
 drivers/vfio/mdev/Kconfig     |   7 ++
 drivers/vfio/mdev/Makefile    |   1 +
 drivers/vfio/mdev/vfio_mdev.c | 148 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 156 insertions(+)
 create mode 100644 drivers/vfio/mdev/vfio_mdev.c
drivers/vfio/mdev/Kconfig +7 -0
index 93addace9a67..6cef0c4d2ceb 100644
@@ -9,3 +9,10 @@ config VFIO_MDEV
 	See Documentation/vfio-mdev/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
index 31bc04801d94..fa2d5ea466ee 100644
@@ -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
author maintainer
#14 lore raw

PATCH v9 3/12 vfio: Rearrange functions to get vfio_group from dev

In reply to Kirti Wankhede

Rearrange functions to have common function to increment container_users.

Change-Id I1f93262bdbab75094bc24b087b29da35ba70c4c6
---
 drivers/vfio/vfio.c | 57 ++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 37 insertions(+), 20 deletions(-)
drivers/vfio/vfio.c +37 -20
index d1d70e0b011b..2e83bdf007fe 100644
@@ -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;
 
@@ -1376,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)
@@ -1685,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
author maintainer
#44 lore raw

PATCH v9 5/12 vfio: Introduce common function to add capabilities

In reply to 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.

Change-Id I6fca329fa2291f37a2c859d0bc97574d9e2ce1a6
---
 drivers/vfio/vfio.c  | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/vfio.h |  4 +++
 2 files changed, 82 insertions(+)
drivers/vfio/vfio.c +78 -0
index a5a210005b65..e96cb3f7a23c 100644
@@ -1799,6 +1799,84 @@ void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset)
 }
 EXPORT_SYMBOL_GPL(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_region_info *info,
+			     struct vfio_info_cap *caps,
+			     int cap_type_id,
+			     void *cap_type)
+{
+	int ret;
+
+	if (!cap_type)
+		return 0;
+
+	switch (cap_type_id) {
+	case VFIO_REGION_INFO_CAP_SPARSE_MMAP:
+		ret = sparse_mmap_cap(caps, cap_type);
+		if (ret)
+			return ret;
+		break;
+
+	case VFIO_REGION_INFO_CAP_TYPE:
+		ret = region_type_cap(caps, cap_type);
+		if (ret)
+			return ret;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	info->flags |= VFIO_REGION_INFO_FLAG_CAPS;
+
+	if (caps->size) {
+		if (info->argsz < sizeof(*info) + caps->size) {
+			info->argsz = sizeof(*info) + caps->size;
+			info->cap_offset = 0;
+		} else {
+			vfio_info_cap_shift(caps, sizeof(*info));
+			info->cap_offset = sizeof(*info);
+		}
+	}
+	return 0;
+}
+EXPORT_SYMBOL(vfio_info_add_capability);
+
 
 /*
  * Pin a set of guest PFNs and return their associated host PFNs for local
include/linux/vfio.h +4 -0
index 0bd25ba6223d..854a4b40be02 100644
@@ -108,6 +108,10 @@ 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_region_info *info,
+				    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
author maintainer
#16 lore raw

PATCH v9 4/12 vfio iommu: Add support for mediated devices

In reply to 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 module

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.

This change adds pin and unpin support for mediated device to TYPE1 IOMMU backend module. More details:
- When iommu_group of mediated devices is attached, task structure is cached which is used later to pin pages and page accounting.
- It keeps track of pinned pages for mediated domain. This data is used to verify unpinning request and to unpin remaining pages while detaching, if there are any.
- Used existing mechanism for page accounting. If iommu capable domain exist in the container then all pages are already pinned and accounted. Accouting for mdev device is only done if there is no iommu capable domain in the container.
- 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

Change-Id I295d6f0f2e0579b8d9882bfd8fd5a4194b97bd9a
---
 drivers/vfio/vfio.c             |  98 ++++++
 drivers/vfio/vfio_iommu_type1.c | 692 ++++++++++++++++++++++++++++++++++------
 include/linux/vfio.h            |  13 +-
 3 files changed, 707 insertions(+), 96 deletions(-)
drivers/vfio/vfio.c +98 -0
index 2e83bdf007fe..a5a210005b65 100644
@@ -1799,6 +1799,104 @@ 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
+ */
+long vfio_pin_pages(struct device *dev, unsigned long *user_pfn,
+		    long npage, int prot, unsigned long *phys_pfn)
+{
+	struct vfio_container *container;
+	struct vfio_group *group;
+	struct vfio_iommu_driver *driver;
+	ssize_t ret = -EINVAL;
+
+	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;
+	if (IS_ERR(container)) {
+		ret = PTR_ERR(container);
+		goto err_pin_pages;
+	}
+
+	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);
+
+	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
+ * @pfn [in] : array of host PFNs to be unpinned.
+ * @npage [in] :count of elements in array, that is number of pages.
+ */
+long vfio_unpin_pages(struct device *dev, unsigned long *pfn, long npage)
+{
+	struct vfio_container *container;
+	struct vfio_group *group;
+	struct vfio_iommu_driver *driver;
+	ssize_t ret = -EINVAL;
+
+	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;
+	if (IS_ERR(container)) {
+		ret = PTR_ERR(container);
+		goto err_unpin_pages;
+	}
+
+	down_read(&container->group_lock);
+
+	driver = container->iommu_driver;
+	if (likely(driver && driver->ops->unpin_pages))
+		ret = driver->ops->unpin_pages(container->iommu_data, pfn,
+					       npage);
+
+	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 +597 -95

953 lines not on this page · raw message

include/linux/vfio.h +12 -1
index 0ecae0b1cd34..0bd25ba6223d 100644
@@ -17,6 +17,7 @@
 #include <linux/workqueue.h>
 #include <linux/poll.h>
 #include <uapi/linux/vfio.h>
+#include <linux/mdev.h>
 
 /**
  * struct vfio_device_ops - VFIO bus driver device callbacks
@@ -75,7 +76,11 @@ struct vfio_iommu_driver_ops {
 					struct iommu_group *group);
 	void		(*detach_group)(void *iommu_data,
 					struct iommu_group *group);
-
+	long		(*pin_pages)(void *iommu_data, unsigned long *user_pfn,
+				     long npage, int prot,
+				     unsigned long *phys_pfn);
+	long		(*unpin_pages)(void *iommu_data, unsigned long *pfn,
+				       long npage);
 };
 
 extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
@@ -127,6 +132,12 @@ static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
 }
 #endif /* CONFIG_EEH */
 
+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);
+
 /*
  * IRQfd - generic
  */
Signature · 1 line
2.7.0
author maintainer
#48 lore raw

PATCH v9 6/12 vfio_pci: Update vfio_pci to use vfio_info_add_capability()

In reply to Kirti Wankhede

Update msix_sparse_mmap_cap() to use vfio_info_add_capability() Update region type capability to use vfio_info_add_capability() Can't split this commit for MSIx and region_type cap since there is a common code which need to be updated for both the cases.

Change-Id I52bb28c7875a6da5a79ddad1843e6088aff58a45
---
 drivers/vfio/pci/vfio_pci.c | 72 +++++++++++++++++----------------------------
 1 file changed, 27 insertions(+), 45 deletions(-)
drivers/vfio/pci/vfio_pci.c +27 -45
index d624a527777f..1ec0565b48ea 100644
@@ -556,12 +556,12 @@ 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_region_info *info,
 				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 +572,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 +591,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);
+	ret = vfio_info_add_capability(info, caps,
+				      VFIO_REGION_INFO_CAP_SPARSE_MMAP, sparse);
+	kfree(sparse);
 
-	cap = container_of(header, struct vfio_region_info_cap_type, header);
-	cap->type = type;
-	cap->subtype = subtype;
-
-	return 0;
+	return ret;
 }
 
 int vfio_pci_register_dev_region(struct vfio_pci_device *vdev,
@@ -704,7 +686,8 @@ static long vfio_pci_ioctl(void *device_data,
 			if (vdev->bar_mmap_supported[info.index]) {
 				info.flags |= VFIO_REGION_INFO_FLAG_MMAP;
 				if (info.index == vdev->msix_bar) {
-					ret = msix_sparse_mmap_cap(vdev, &caps);
+					ret = msix_sparse_mmap_cap(vdev, &info,
+								   &caps);
 					if (ret)
 						return ret;
 				}
@@ -752,6 +735,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,27 +748,23 @@ 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(&info, &caps,
+						      VFIO_REGION_INFO_CAP_TYPE,
+						      &cap_type);
 			if (ret)
 				return ret;
+
+		}
 		}
 
-		if (caps.size) {
-			info.flags |= VFIO_REGION_INFO_FLAG_CAPS;
-			if (info.argsz < sizeof(info) + caps.size) {
-				info.argsz = sizeof(info) + caps.size;
-				info.cap_offset = 0;
-			} else {
-				vfio_info_cap_shift(&caps, sizeof(info));
-				if (copy_to_user((void __user *)arg +
-						  sizeof(info), caps.buf,
-						  caps.size)) {
-					kfree(caps.buf);
-					return -EFAULT;
-				}
-				info.cap_offset = sizeof(info);
+		if (info.cap_offset) {
+			if (copy_to_user((void __user *)arg + info.cap_offset,
+					 caps.buf, caps.size)) {
+				kfree(caps.buf);
+				return -EFAULT;
 			}
 
 			kfree(caps.buf);
Signature · 1 line
2.7.0
author maintainer
#53 lore raw

PATCH v9 8/12 vfio_pci: Updated to use vfio_set_irqs_validate_and_prepare()

In reply to Kirti Wankhede

Updated vfio_pci.c file to use vfio_set_irqs_validate_and_prepare()

Change-Id I9f3daba89d8dba5cb5b01a8cff420412f30686c7
---
 drivers/vfio/pci/vfio_pci.c | 29 +++++++++--------------------
 1 file changed, 9 insertions(+), 20 deletions(-)
drivers/vfio/pci/vfio_pci.c +9 -20
index 1ec0565b48ea..23e7f32a4a07 100644
@@ -812,35 +812,24 @@ static long vfio_pci_ioctl(void *device_data,
 	} else if (cmd == VFIO_DEVICE_SET_IRQS) {
 		struct vfio_irq_set hdr;
 		u8 *data = NULL;
-		int ret = 0;
+		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.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.flags & VFIO_IRQ_SET_DATA_NONE)) {
-			size_t size;
-			int max = vfio_pci_get_irq_count(vdev, hdr.index);
-
-			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 < hdr.count * size ||
-			    hdr.start >= max || hdr.start + hdr.count > max)
-				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
author maintainer
#52 lore raw

PATCH v9 7/12 vfio: Introduce vfio_set_irqs_validate_and_prepare()

In reply to 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.

Change-Id Ie201f269dda0713ca18a07dc4852500bd8b48309
---
 drivers/vfio/vfio.c  | 39 +++++++++++++++++++++++++++++++++++++++
 include/linux/vfio.h |  4 ++++
 2 files changed, 43 insertions(+)
drivers/vfio/vfio.c +39 -0
index e96cb3f7a23c..10ef1c5fa762 100644
@@ -1877,6 +1877,45 @@ int vfio_info_add_capability(struct vfio_region_info *info,
 }
 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;
+
+	minsz = offsetofend(struct vfio_irq_set, count);
+
+	if ((hdr->argsz < minsz) || (hdr->index >= max_irq_type) ||
+	    (hdr->flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
+				VFIO_IRQ_SET_ACTION_TYPE_MASK)))
+		return -EINVAL;
+
+	if (data_size)
+		*data_size = 0;
+
+	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 < hdr->count * size) ||
+		    (hdr->start >= num_irqs) ||
+		    (hdr->start + hdr->count > num_irqs))
+			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
include/linux/vfio.h +4 -0
index 854a4b40be02..31d059f1649b 100644
@@ -112,6 +112,10 @@ extern int vfio_info_add_capability(struct vfio_region_info *info,
 				    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
author maintainer
#54 lore raw

PATCH v9 9/12 vfio_platform: Updated to use vfio_set_irqs_validate_and_prepare()

In reply to Kirti Wankhede

Updated vfio_platform_common.c file to use
vfio_set_irqs_validate_and_prepare()

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 +8 -23
index d78142830754..4c27f4be3c3d 100644
@@ -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
author maintainer
#55 lore raw

PATCH v9 10/12 vfio: Add function to get device_api string from vfio_device_info.flags

In reply to Kirti Wankhede

Function vfio_device_api_string() returns string based on flag set in vfio_device_info's flag. This should be used by vendor driver to get string based on flag for device_api attribute.

Change-Id I42d29f475f02a7132ce13297fbf2b48f1da10995
---
 drivers/vfio/vfio.c  | 15 +++++++++++++++
 include/linux/vfio.h |  2 ++
 2 files changed, 17 insertions(+)
drivers/vfio/vfio.c +15 -0
index 10ef1c5fa762..aec470454a13 100644
@@ -1917,6 +1917,21 @@ int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr, int num_irqs,
 }
 EXPORT_SYMBOL(vfio_set_irqs_validate_and_prepare);
 
+const char *vfio_device_api_string(u32 flags)
+{
+	if (flags & VFIO_DEVICE_FLAGS_PCI)
+		return "vfio-pci";
+
+	if (flags & VFIO_DEVICE_FLAGS_PLATFORM)
+		return "vfio-platform";
+
+	if (flags & VFIO_DEVICE_FLAGS_AMBA)
+		return "vfio-amba";
+
+	return "";
+}
+EXPORT_SYMBOL(vfio_device_api_string);
+
 /*
  * Pin a set of guest PFNs and return their associated host PFNs for local
  * domain only.
include/linux/vfio.h +2 -0
index 31d059f1649b..fca2bf23c4f1 100644
@@ -116,6 +116,8 @@ extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr,
 					      int num_irqs, int max_irq_type,
 					      size_t *data_size);
 
+extern const char *vfio_device_api_string(u32 flags);
+
 struct pci_dev;
 #ifdef CONFIG_EEH
 extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);
Signature · 1 line
2.7.0
author maintainer
#63 lore raw

PATCH v9 11/12 docs: Add Documentation for Mediated devices

In reply to Kirti Wankhede

Add file Documentation/vfio-mediated-device.txt that include details of mediated device framework.

Change-Id I137dd646442936090d92008b115908b7b2c7bc5d
---
 Documentation/vfio-mdev/vfio-mediated-device.txt | 289 +++++++++++++++++++++++
 1 file changed, 289 insertions(+)
 create mode 100644 Documentation/vfio-mdev/vfio-mediated-device.txt
Documentation/vfio-mdev/vfio-mediated-device.txt new file +289 -0
new file mode 100644
index 000000000000..8746e88dca4d
@@ -0,0 +1,289 @@
+/*
+ * 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
+
+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.
+
+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
author maintainer
#65 lore raw

PATCH v9 12/12 docs: Sample driver to demonstrate how to use Mediated device framework.

In reply to Kirti Wankhede

The Sample driver creates mdev device that simulates serial port over PCI card.

Change-Id I857f8f12f8b275f2498dfe8c628a5cdc7193b1b2
---
 Documentation/vfio-mdev/Makefile                 |   13 +
 Documentation/vfio-mdev/mtty.c                   | 1429 ++++++++++++++++++++++
 Documentation/vfio-mdev/vfio-mediated-device.txt |  104 +-
 3 files changed, 1544 insertions(+), 2 deletions(-)
 create mode 100644 Documentation/vfio-mdev/Makefile
 create mode 100644 Documentation/vfio-mdev/mtty.c
Documentation/vfio-mdev/Makefile new file +13 -0
new file mode 100644
index 000000000000..a932edbe38eb
@@ -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
Documentation/vfio-mdev/mtty.c new file +1429 -0

1,429 lines not on this page · raw message

Documentation/vfio-mdev/vfio-mediated-device.txt +102 -2

113 lines not on this page · raw message

Signature · 1 line
2.7.0
#70 lore raw
In reply to Kirti Wankhede
Quoted 22 lines · On Tue, 18 Oct 2016 02:52:00 +0530 Kirti Wankhede <[email protected]> wrote:

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 v9?
mdev-core:
- added class named 'mdev_bus' that contains links to devices that are registered with the mdev core driver.
- The [<type-id>] name is created by adding the the device driver string as a prefix to the string provided by the vendor driver.
- 'device_api' attribute should be provided by vendor driver and should show which device API is being created, for example, "vfio-pci" for a PCI device.
- Renamed link to its type in mdev device directory to 'mdev_type'

vfio:
- Split commits in multple individual commits
- Added function to get device_api string based on vfio_device_info.flags.

Quoted 8 lines

vfio_iommu_type1:
- Handled the case if all devices attached to the normal IOMMU API domain go away and mdev device still exist in domain. Updated page accounting for local domain.
- Similarly if device is attached to normal IOMMU API domain, mappings are establised and page accounting is updated accordingly.
- Tested hot-plug and hot-unplug of vGPU and GPU pass through device with Linux VM.

Hi,

I also commented that there must be an invalidation mechanism for pages pinned by the vendor driver. This is where pfn pinning was adjusting accounting after a DMA_MAP, where the pfn should have been invalidated on user unmap. Userspace is in control of page mappings, the vendor driver cannot maintain references to pages unmapped by the user. I would suggest that minimally some sort of callback needs to be registered for every set of pinned pages to be called when the user unmaps those IOVAs. Thanks,

Alex

Quoted 51 lines

Documentation:
- Updated Documentation and sample driver, mtty.c, accordingly.

Kirti Wankhede (12):
vfio: Mediated device Core driver
vfio: VFIO based driver for Mediated devices
vfio: Rearrange functions to get vfio_group from dev vfio iommu: Add support for mediated devices
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: Add function to get device_api string from
vfio_device_info.flags
docs: Add Documentation for Mediated devices
docs: Sample driver to demonstrate how to use Mediated device
framework.

 Documentation/vfio-mdev/Makefile                 |   13 +
 Documentation/vfio-mdev/mtty.c                   | 1429 ++++++++++++++++++++++
 Documentation/vfio-mdev/vfio-mediated-device.txt |  389 ++++++
 drivers/vfio/Kconfig                             |    1 +
 drivers/vfio/Makefile                            |    1 +
 drivers/vfio/mdev/Kconfig                        |   18 +
 drivers/vfio/mdev/Makefile                       |    5 +
 drivers/vfio/mdev/mdev_core.c                    |  372 ++++++
 drivers/vfio/mdev/mdev_driver.c                  |  128 ++
 drivers/vfio/mdev/mdev_private.h                 |   41 +
 drivers/vfio/mdev/mdev_sysfs.c                   |  296 +++++
 drivers/vfio/mdev/vfio_mdev.c                    |  148 +++
 drivers/vfio/pci/vfio_pci.c                      |  101 +-
 drivers/vfio/platform/vfio_platform_common.c     |   31 +-
 drivers/vfio/vfio.c                              |  287 ++++-
 drivers/vfio/vfio_iommu_type1.c                  |  692 +++++++++--
 include/linux/mdev.h                             |  177 +++
 include/linux/vfio.h                             |   23 +-
 18 files changed, 3948 insertions(+), 204 deletions(-)
 create mode 100644 Documentation/vfio-mdev/Makefile
 create mode 100644 Documentation/vfio-mdev/mtty.c
 create mode 100644 Documentation/vfio-mdev/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
#66 lore raw

Re: [PATCH v9 12/12] docs: Sample driver to demonstrate how to use Mediated device framework.

In reply to Kirti Wankhede

* Kirti Wankhede <[email protected]> [2016-10-18 02:52:12 +0530]:

...snip...

Quoted 24 lines
Documentation/vfio-mdev/mtty.c new file +24 -0
+static ssize_t mdev_access(struct mdev_device *mdev, char *buf,
+		size_t count, loff_t pos, bool is_write)
+{
+	struct mdev_state *mdev_state;
+	unsigned int index;
+	loff_t offset;
+	int ret = 0;
+
+	if (!mdev || !buf)
+		return -EINVAL;
+
+	mdev_state = mdev_get_drvdata(mdev);
+	if (!mdev_state) {
+		pr_err("%s mdev_state not found\n", __func__);
+		return -EINVAL;
+	}
+
+	mutex_lock(&mdev_state->ops_lock);
+
+	index = MTTY_VFIO_PCI_OFFSET_TO_INDEX(pos);
+	offset = pos & MTTY_VFIO_PCI_OFFSET_MASK;
+	switch (index) {
+	case VFIO_PCI_CONFIG_REGION_INDEX:
+

Documentation/vfio-mdev/mtty.c line 681

Quoted 10 lines
Documentation/vfio-mdev/mtty.c new file +10 -0
+#if defined(DEBUG)
+		pr_info("%s: PCI config space %s at offset 0x%llx\n",
+			 __func__, is_write ? "write" : "read", offset);
+#endif
+		if (is_write) {
+			dump_buffer(buf, count);
+			handle_pci_cfg_write(mdev_state, offset, buf, count);
+		} else {
+			memcpy(buf, (mdev_state->vconfig + offset), count);
+			dump_buffer(buf, count);

Dear Kirti:

Shouldn't we use copy_from_user instead of memcpy on @buf here? And I'm wondering if dump_buffer could really work since it tries to dereference a *__user* marked pointor.

Otherwise, this is a good example driver. Thanks!

Quoted 43 lines
Documentation/vfio-mdev/mtty.c new file +43 -0
+		}
+
+		break;
+
+	case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
+		if (!mdev_state->region_info[index].start)
+			mdev_read_base(mdev_state);
+
+		if (is_write) {
+			dump_buffer(buf, count);
+
+#if defined(DEBUG_REGS)
+			pr_info("%s: BAR%d  WR @0x%llx %s val:0x%02x dlab:%d\n",
+				__func__, index, offset, wr_reg[offset],
+				(u8)*buf, mdev_state->s[index].dlab);
+#endif
+			handle_bar_write(index, mdev_state, offset, buf, count);
+		} else {
+			handle_bar_read(index, mdev_state, offset, buf, count);
+			dump_buffer(buf, count);
+
+#if defined(DEBUG_REGS)
+			pr_info("%s: BAR%d  RD @0x%llx %s val:0x%02x dlab:%d\n",
+				__func__, index, offset, rd_reg[offset],
+				(u8)*buf, mdev_state->s[index].dlab);
+#endif
+		}
+		break;
+
+	default:
+		ret = -1;
+		goto accessfailed;
+	}
+
+	ret = count;
+
+
+accessfailed:
+	mutex_unlock(&mdev_state->ops_lock);
+
+	return ret;
+}
+

...snip...

Quoted 12 lines
Documentation/vfio-mdev/mtty.c new file +12 -0
+ssize_t mtty_read(struct mdev_device *mdev, char __user *buf,
+			size_t count, loff_t *ppos)
+{
+	return mdev_access(mdev, buf, count, *ppos, false);
+}
+
+ssize_t mtty_write(struct mdev_device *mdev, const char __user *buf,
+			 size_t count, loff_t *ppos)
+{
+	return mdev_access(mdev, (char *)buf, count, *ppos, true);
+}
+

...snip...

Signature · 1 line
Dong Jia
#67 lore raw

Re: [Qemu-devel] [PATCH v9 12/12] docs: Sample driver to demonstrate how to use Mediated device framework.

In reply to Kirti Wankhede

* Kirti Wankhede <[email protected]> [2016-10-18 02:52:12 +0530]:

...snip...

Quoted 24 lines
Documentation/vfio-mdev/mtty.c new file +24 -0
+static ssize_t mdev_access(struct mdev_device *mdev, char *buf,
+		size_t count, loff_t pos, bool is_write)
+{
+	struct mdev_state *mdev_state;
+	unsigned int index;
+	loff_t offset;
+	int ret = 0;
+
+	if (!mdev || !buf)
+		return -EINVAL;
+
+	mdev_state = mdev_get_drvdata(mdev);
+	if (!mdev_state) {
+		pr_err("%s mdev_state not found\n", __func__);
+		return -EINVAL;
+	}
+
+	mutex_lock(&mdev_state->ops_lock);
+
+	index = MTTY_VFIO_PCI_OFFSET_TO_INDEX(pos);
+	offset = pos & MTTY_VFIO_PCI_OFFSET_MASK;
+	switch (index) {
+	case VFIO_PCI_CONFIG_REGION_INDEX:
+

Documentation/vfio-mdev/mtty.c line 681

Quoted 10 lines
Documentation/vfio-mdev/mtty.c new file +10 -0
+#if defined(DEBUG)
+		pr_info("%s: PCI config space %s at offset 0x%llx\n",
+			 __func__, is_write ? "write" : "read", offset);
+#endif
+		if (is_write) {
+			dump_buffer(buf, count);
+			handle_pci_cfg_write(mdev_state, offset, buf, count);
+		} else {
+			memcpy(buf, (mdev_state->vconfig + offset), count);
+			dump_buffer(buf, count);

Dear Kirti:

Shouldn't we use copy_from_user instead of memcpy on @buf here? And I'm wondering if dump_buffer could really work since it tries to dereference a *__user* marked pointor.

Otherwise, this is a good example driver. Thanks!

Quoted 43 lines
Documentation/vfio-mdev/mtty.c new file +43 -0
+		}
+
+		break;
+
+	case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
+		if (!mdev_state->region_info[index].start)
+			mdev_read_base(mdev_state);
+
+		if (is_write) {
+			dump_buffer(buf, count);
+
+#if defined(DEBUG_REGS)
+			pr_info("%s: BAR%d  WR @0x%llx %s val:0x%02x dlab:%d\n",
+				__func__, index, offset, wr_reg[offset],
+				(u8)*buf, mdev_state->s[index].dlab);
+#endif
+			handle_bar_write(index, mdev_state, offset, buf, count);
+		} else {
+			handle_bar_read(index, mdev_state, offset, buf, count);
+			dump_buffer(buf, count);
+
+#if defined(DEBUG_REGS)
+			pr_info("%s: BAR%d  RD @0x%llx %s val:0x%02x dlab:%d\n",
+				__func__, index, offset, rd_reg[offset],
+				(u8)*buf, mdev_state->s[index].dlab);
+#endif
+		}
+		break;
+
+	default:
+		ret = -1;
+		goto accessfailed;
+	}
+
+	ret = count;
+
+
+accessfailed:
+	mutex_unlock(&mdev_state->ops_lock);
+
+	return ret;
+}
+

...snip...

Quoted 12 lines
Documentation/vfio-mdev/mtty.c new file +12 -0
+ssize_t mtty_read(struct mdev_device *mdev, char __user *buf,
+			size_t count, loff_t *ppos)
+{
+	return mdev_access(mdev, buf, count, *ppos, false);
+}
+
+ssize_t mtty_write(struct mdev_device *mdev, const char __user *buf,
+			 size_t count, loff_t *ppos)
+{
+	return mdev_access(mdev, (char *)buf, count, *ppos, true);
+}
+

...snip...

Signature · 1 line
Dong Jia
#68 lore raw

Re: [PATCH v9 12/12] docs: Sample driver to demonstrate how to use Mediated device framework.

Quoted 40 lines · On Tue, 18 Oct 2016 10:54:11 +0800 Dong Jia Shi <[email protected]>…

* Kirti Wankhede <[email protected]> [2016-10-18 02:52:12 +0530]:

...snip...

Quoted 24 lines
Documentation/vfio-mdev/mtty.c new file +24 -0
+static ssize_t mdev_access(struct mdev_device *mdev, char *buf,
+		size_t count, loff_t pos, bool is_write)
+{
+	struct mdev_state *mdev_state;
+	unsigned int index;
+	loff_t offset;
+	int ret = 0;
+
+	if (!mdev || !buf)
+		return -EINVAL;
+
+	mdev_state = mdev_get_drvdata(mdev);
+	if (!mdev_state) {
+		pr_err("%s mdev_state not found\n", __func__);
+		return -EINVAL;
+	}
+
+	mutex_lock(&mdev_state->ops_lock);
+
+	index = MTTY_VFIO_PCI_OFFSET_TO_INDEX(pos);
+	offset = pos & MTTY_VFIO_PCI_OFFSET_MASK;
+	switch (index) {
+	case VFIO_PCI_CONFIG_REGION_INDEX:
+

Documentation/vfio-mdev/mtty.c line 681

Quoted 10 lines
Documentation/vfio-mdev/mtty.c new file +10 -0
+#if defined(DEBUG)
+		pr_info("%s: PCI config space %s at offset 0x%llx\n",
+			 __func__, is_write ? "write" : "read", offset);
+#endif
+		if (is_write) {
+			dump_buffer(buf, count);
+			handle_pci_cfg_write(mdev_state, offset, buf, count);
+		} else {
+			memcpy(buf, (mdev_state->vconfig + offset), count);
+			dump_buffer(buf, count);  

Dear Kirti:

Quoted 3 lines

Shouldn't we use copy_from_user instead of memcpy on @buf here? And I'm wondering if dump_buffer could really work since it tries to dereference a *__user* marked pointor.

I agree, the __user attribute is getting lost here and we're operating on user buffers as if they were kernel buffers. That's a bug. Thanks,

Alex

Quoted 61 lines

Otherwise, this is a good example driver. Thanks!

Quoted 43 lines
Documentation/vfio-mdev/mtty.c new file +43 -0
+		}
+
+		break;
+
+	case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
+		if (!mdev_state->region_info[index].start)
+			mdev_read_base(mdev_state);
+
+		if (is_write) {
+			dump_buffer(buf, count);
+
+#if defined(DEBUG_REGS)
+			pr_info("%s: BAR%d  WR @0x%llx %s val:0x%02x dlab:%d\n",
+				__func__, index, offset, wr_reg[offset],
+				(u8)*buf, mdev_state->s[index].dlab);
+#endif
+			handle_bar_write(index, mdev_state, offset, buf, count);
+		} else {
+			handle_bar_read(index, mdev_state, offset, buf, count);
+			dump_buffer(buf, count);
+
+#if defined(DEBUG_REGS)
+			pr_info("%s: BAR%d  RD @0x%llx %s val:0x%02x dlab:%d\n",
+				__func__, index, offset, rd_reg[offset],
+				(u8)*buf, mdev_state->s[index].dlab);
+#endif
+		}
+		break;
+
+	default:
+		ret = -1;
+		goto accessfailed;
+	}
+
+	ret = count;
+
+
+accessfailed:
+	mutex_unlock(&mdev_state->ops_lock);
+
+	return ret;
+}
+  

...snip...

Quoted 12 lines
Documentation/vfio-mdev/mtty.c new file +12 -0
+ssize_t mtty_read(struct mdev_device *mdev, char __user *buf,
+			size_t count, loff_t *ppos)
+{
+	return mdev_access(mdev, buf, count, *ppos, false);
+}
+
+ssize_t mtty_write(struct mdev_device *mdev, const char __user *buf,
+			 size_t count, loff_t *ppos)
+{
+	return mdev_access(mdev, (char *)buf, count, *ppos, true);
+}
+  

...snip...

#2 lore raw

Re: [PATCH v9 01/12] vfio: Mediated device Core driver

In reply to Kirti Wankhede
Quoted 221 lines · On Tue, 18 Oct 2016 02:52:01 +0530 Kirti Wankhede <[email protected]> wrote:

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);

Medisted 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 driver for a mediated device.
* remove: to free resources in driver when mediated device is destroyed.
* open: open callback of mediated device
* release: release callback of mediated device
* read : read emulation callback.
* write: write emulation callback.
* mmap: mmap emulation callback.
* ioctl: ioctl 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.

Change-Id I73a5084574270b14541c529461ea2f03c292d510
---
 drivers/vfio/Kconfig             |   1 +
 drivers/vfio/Makefile            |   1 +
 drivers/vfio/mdev/Kconfig        |  11 ++
 drivers/vfio/mdev/Makefile       |   4 +
 drivers/vfio/mdev/mdev_core.c    | 372 +++++++++++++++++++++++++++++++++++++++
 drivers/vfio/mdev/mdev_driver.c  | 128 ++++++++++++++
 drivers/vfio/mdev/mdev_private.h |  41 +++++
 drivers/vfio/mdev/mdev_sysfs.c   | 296 +++++++++++++++++++++++++++++++
 include/linux/mdev.h             | 177 +++++++++++++++++++
 9 files changed, 1031 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
index da6e2ce77495..23eced02aaf6 100644
@@ -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
index 7b8a31f63fea..4a23c13b6be4 100644
@@ -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 +11 -0
new file mode 100644
index 000000000000..93addace9a67
@@ -0,0 +1,11 @@
+
+config VFIO_MDEV
+    tristate "Mediated device driver framework"
+    depends on VFIO
+    default n
+    help
+        Provides a framework to virtualize devices which don't have SR_IOV
+	capability built-in.
+	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
new file mode 100644
index 000000000000..31bc04801d94
@@ -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 +47 -0
new file mode 100644
index 000000000000..7db5ec164aeb
@@ -0,0 +1,372 @@
+/*
+ * Mediated device Core Driver
+ *
+ * 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.
+ */
+
+#include <linux/module.h>
+#include <linux/device.h>
+#include <linux/slab.h>
+#include <linux/uuid.h>
+#include <linux/sysfs.h>
+#include <linux/mdev.h>
+
+#include "mdev_private.h"
+
+#define DRIVER_VERSION		"0.1"
+#define DRIVER_AUTHOR		"NVIDIA Corporation"
+#define DRIVER_DESC		"Mediated device Core Driver"
+
+static LIST_HEAD(parent_list);
+static DEFINE_MUTEX(parent_list_lock);
+static struct class_compat *mdev_bus_compat_class;
+
+static int _find_mdev_device(struct device *dev, void *data)
+{
+	struct mdev_device *mdev;
+
+	if (!dev_is_mdev(dev))
+		return 0;
+
+	mdev = to_mdev_device(dev);
+
+	if (uuid_le_cmp(mdev->uuid, *(uuid_le *)data) == 0)
+		return 1;
+
+	return 0;
+}
+
+static struct mdev_device *__find_mdev_device(struct parent_device *parent,
+					      uuid_le uuid)
+{

drivers/vfio/mdev/mdev_core.c line 57

Quoted 10 lines
drivers/vfio/mdev/mdev_core.c new file +10 -0
+	struct device *dev;
+
+	dev = device_find_child(parent->dev, &uuid, _find_mdev_device);
+	if (!dev)
+		return NULL;
+
+	put_device(dev);
+
+	return to_mdev_device(dev);
+}

This function is only used by mdev_device_create() for the purpose of checking whether a given uuid for a parent already exists, so the returned device is not actually used. However, at the point where we're using to_mdev_device() here, we don't actually hold a reference to the device, so that function call and any possible use of the returned pointer by the callee is invalid. I would either turn this into a "get" function where the callee has a device reference and needs to do a "put" on it or change this to a "exists" test where true/false is returned and the function cannot be later mis-used to do a device lookup where the reference isn't actually valid.

Quoted 198 lines
drivers/vfio/mdev/mdev_core.c new file +198 -0
+
+/* Should be called holding parent_list_lock */
+static struct parent_device *__find_parent_device(struct device *dev)
+{
+	struct parent_device *parent;
+
+	list_for_each_entry(parent, &parent_list, next) {
+		if (parent->dev == dev)
+			return parent;
+	}
+	return NULL;
+}
+
+static void mdev_release_parent(struct kref *kref)
+{
+	struct parent_device *parent = container_of(kref, struct parent_device,
+						    ref);
+	struct device *dev = parent->dev;
+
+	kfree(parent);
+	put_device(dev);
+}
+
+static
+inline struct parent_device *mdev_get_parent(struct parent_device *parent)
+{
+	if (parent)
+		kref_get(&parent->ref);
+
+	return parent;
+}
+
+static inline void mdev_put_parent(struct parent_device *parent)
+{
+	if (parent)
+		kref_put(&parent->ref, mdev_release_parent);
+}
+
+static int mdev_device_create_ops(struct kobject *kobj,
+				  struct mdev_device *mdev)
+{
+	struct parent_device *parent = mdev->parent;
+	int ret;
+
+	ret = parent->ops->create(kobj, mdev);
+	if (ret)
+		return ret;
+
+	ret = sysfs_create_groups(&mdev->dev.kobj,
+				  parent->ops->mdev_attr_groups);
+	if (ret)
+		parent->ops->remove(mdev);
+
+	return ret;
+}
+
+static int mdev_device_remove_ops(struct mdev_device *mdev, bool force_remove)
+{
+	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)
+{
+	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 = 0;
+	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);
+
+	parent->dev = dev;
+	parent->ops = ops;
+
+	ret = parent_create_sysfs_files(parent);
+	if (ret) {
+		mutex_unlock(&parent_list_lock);
+		mdev_put_parent(parent);
+		return ret;
+	}
+
+	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);
+
+	dev_info(dev, "MDEV: Registered\n");
+	return 0;
+
+add_dev_err:
+	mutex_unlock(&parent_list_lock);
+	put_device(dev);
+	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");
+
+	/*
+	 * Remove parent from the list and remove "mdev_supported_types"
+	 * sysfs files so that no new mediated device could be
+	 * created for this parent
+	 */
+	list_del(&parent->next);
+	parent_remove_sysfs_files(parent);
+
+	mutex_unlock(&parent_list_lock);
+
+	class_compat_remove_link(mdev_bus_compat_class, dev, NULL);
+
+	device_for_each_child(dev, (void *)&force_remove,
+			      mdev_device_remove_cb);
+	mdev_put_parent(parent);
+}
+EXPORT_SYMBOL(mdev_unregister_device);
+
+static void mdev_device_release(struct device *dev)
+{
+	struct mdev_device *mdev = to_mdev_device(dev);
+
+	dev_dbg(&mdev->dev, "MDEV: destroying\n");
+	kfree(mdev);
+}
+
+int mdev_device_create(struct kobject *kobj, struct device *dev, uuid_le uuid)
+{
+	int ret;
+	struct mdev_device *mdev;
+	struct parent_device *parent;
+	struct mdev_type *type = to_mdev_type(kobj);
+

drivers/vfio/mdev/mdev_core.c line 265

Quoted 10 lines
drivers/vfio/mdev/mdev_core.c new file +10 -0
+	parent = mdev_get_parent(type->parent);
+	if (!parent)
+		return -EINVAL;
+
+	/* Check for duplicate */
+	mdev = __find_mdev_device(parent, uuid);
+	if (mdev) {
+		ret = -EEXIST;
+		goto create_err;
+	}

We check here whether the {parent,uuid} already exists, but what prevents us racing with another create call with the same uuid? ie. neither exists at this point. Will device_register() fail if the device name already exists? If so, should we just rely on the error there and skip this duplicate check? If not, we need a mutex to avoid the race.

Quoted 346 lines
drivers/vfio/mdev/mdev_core.c new file +107 -0
+
+	mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
+	if (!mdev) {
+		ret = -ENOMEM;
+		goto create_err;
+	}
+
+	memcpy(&mdev->uuid, &uuid, sizeof(uuid_le));
+	mdev->parent = parent;
+	kref_init(&mdev->ref);
+
+	mdev->dev.parent  = dev;
+	mdev->dev.bus     = &mdev_bus_type;
+	mdev->dev.release = mdev_device_release;
+	dev_set_name(&mdev->dev, "%pUl", uuid.b);
+
+	ret = device_register(&mdev->dev);
+	if (ret) {
+		put_device(&mdev->dev);
+		goto create_err;
+	}
+
+	ret = mdev_device_create_ops(kobj, mdev);
+	if (ret)
+		goto create_failed;
+
+	ret = mdev_create_sysfs_files(&mdev->dev, type);
+	if (ret) {
+		mdev_device_remove_ops(mdev, true);
+		goto create_failed;
+	}
+
+	mdev->type_kobj = kobj;
+	dev_dbg(&mdev->dev, "MDEV: created\n");
+
+	return ret;
+
+create_failed:
+	device_unregister(&mdev->dev);
+
+create_err:
+	mdev_put_parent(parent);
+	return ret;
+}
+
+int mdev_device_remove(struct device *dev, bool force_remove)
+{
+	struct mdev_device *mdev;
+	struct parent_device *parent;
+	struct mdev_type *type;
+	int ret = 0;
+
+	if (!dev_is_mdev(dev))
+		return 0;
+
+	mdev = to_mdev_device(dev);
+	parent = mdev->parent;
+	type = to_mdev_type(mdev->type_kobj);
+
+	ret = mdev_device_remove_ops(mdev, force_remove);
+	if (ret)
+		return ret;
+
+	mdev_remove_sysfs_files(dev, type);
+	device_unregister(dev);
+	mdev_put_parent(parent);
+	return ret;
+}
+
+static int __init mdev_init(void)
+{
+	int ret;
+
+	ret = mdev_bus_register();
+	if (ret) {
+		pr_err("Failed to register mdev bus\n");
+		return ret;
+	}
+
+	mdev_bus_compat_class = class_compat_register("mdev_bus");
+	if (!mdev_bus_compat_class) {
+		mdev_bus_unregister();
+		return -ENOMEM;
+	}
+
+	/*
+	 * 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");
+
+	return ret;
+}
+
+static void __exit mdev_exit(void)
+{
+	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 +128 -0
new file mode 100644
index 000000000000..7768ef87f528
@@ -0,0 +1,128 @@
+/*
+ * MDEV driver
+ *
+ * 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.
+ */
+
+#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;
+
+	group = iommu_group_alloc();
+	if (IS_ERR(group)) {
+		dev_err(&mdev->dev, "MDEV: failed to allocate group!\n");
+		return PTR_ERR(group);
+	}
+
+	ret = iommu_group_add_device(group, &mdev->dev);
+	if (ret) {
+		dev_err(&mdev->dev, "MDEV: failed to add dev to group!\n");
+		goto attach_fail;
+	}
+
+	dev_info(&mdev->dev, "MDEV: group_id = %d\n",
+				 iommu_group_id(group));
+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;
+
+	ret = mdev_attach_iommu(mdev);
+	if (ret) {
+		dev_err(dev, "Failed to attach IOMMU\n");
+		return ret;
+	}
+
+	if (drv && drv->probe)
+		ret = drv->probe(dev);
+
+	if (ret)
+		mdev_detach_iommu(mdev);
+
+	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;
+}
+
+struct bus_type mdev_bus_type = {
+	.name		= "mdev",
+	.probe		= mdev_probe,
+	.remove		= mdev_remove,
+};
+EXPORT_SYMBOL_GPL(mdev_bus_type);
+
+/*
+ * 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;
+
+	/* register with core */
+	return driver_register(&drv->driver);
+}
+EXPORT_SYMBOL(mdev_register_driver);
+
+/*
+ * mdev_unregister_driver - unregister MDEV driver
+ * @drv: the driver to unregister
+ *
+ */
+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);
+}
drivers/vfio/mdev/mdev_private.h new file +41 -0
new file mode 100644
index 000000000000..000c93fcfdbd
@@ -0,0 +1,41 @@
+/*
+ * Mediated device interal definitions
+ *
+ * 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.
+ */
+
+#ifndef MDEV_PRIVATE_H
+#define MDEV_PRIVATE_H
+
+int  mdev_bus_register(void);
+void mdev_bus_unregister(void);
+
+struct mdev_type {
+	struct kobject kobj;
+	struct kobject *devices_kobj;
+	struct parent_device *parent;
+	struct list_head next;
+	struct attribute_group *group;
+};
+
+#define to_mdev_type_attr(_attr)	\
+	container_of(_attr, struct mdev_type_attribute, attr)
+#define to_mdev_type(_kobj)		\
+	container_of(_kobj, struct mdev_type, kobj)
+
+int  parent_create_sysfs_files(struct parent_device *parent);
+void parent_remove_sysfs_files(struct parent_device *parent);
+
+int  mdev_create_sysfs_files(struct device *dev, struct mdev_type *type);
+void mdev_remove_sysfs_files(struct device *dev, struct mdev_type *type);
+
+int  mdev_device_create(struct kobject *kobj, struct device *dev, uuid_le uuid);
+int  mdev_device_remove(struct device *dev, bool force_remove);
+
+#endif /* MDEV_PRIVATE_H */
drivers/vfio/mdev/mdev_sysfs.c new file +52 -0
new file mode 100644
index 000000000000..426e35cf79d0
@@ -0,0 +1,296 @@
+/*
+ * File attributes for 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.
+ */
+
+#include <linux/sysfs.h>
+#include <linux/ctype.h>
+#include <linux/device.h>
+#include <linux/slab.h>
+#include <linux/uuid.h>
+#include <linux/mdev.h>
+
+#include "mdev_private.h"
+
+/* Static functions */
+
+static ssize_t mdev_type_attr_show(struct kobject *kobj,
+				     struct attribute *__attr, char *buf)
+{
+	struct mdev_type_attribute *attr = to_mdev_type_attr(__attr);
+	struct mdev_type *type = to_mdev_type(kobj);
+	ssize_t ret = -EIO;
+
+	if (attr->show)
+		ret = attr->show(kobj, type->parent->dev, buf);
+	return ret;
+}
+
+static ssize_t mdev_type_attr_store(struct kobject *kobj,
+				      struct attribute *__attr,
+				      const char *buf, size_t count)
+{
+	struct mdev_type_attribute *attr = to_mdev_type_attr(__attr);
+	struct mdev_type *type = to_mdev_type(kobj);
+	ssize_t ret = -EIO;
+
+	if (attr->store)
+		ret = attr->store(&type->kobj, type->parent->dev, buf, count);
+	return ret;
+}
+
+static const struct sysfs_ops mdev_type_sysfs_ops = {
+	.show = mdev_type_attr_show,
+	.store = mdev_type_attr_store,
+};

drivers/vfio/mdev/mdev_sysfs.c line 62

Quoted 10 lines
drivers/vfio/mdev/mdev_sysfs.c new file +10 -0
+
+static ssize_t create_store(struct kobject *kobj, struct device *dev,
+			    const char *buf, size_t count)
+{
+	char *str;
+	uuid_le uuid;
+	int ret;
+
+	if (count < UUID_STRING_LEN)
+		return -EINVAL;

Can't we also test for something unreasonably large?

drivers/vfio/mdev/mdev_sysfs.c line 68

Quoted 6 lines
drivers/vfio/mdev/mdev_sysfs.c new file +6 -0
+
+	str = kstrndup(buf, count, GFP_KERNEL);
+	if (!str)
+		return -ENOMEM;
+
+	ret = uuid_le_to_bin(str, &uuid);

nit, we can kfree(str) here regardless of the return.

drivers/vfio/mdev/mdev_sysfs.c line 73

Quoted 5 lines
drivers/vfio/mdev/mdev_sysfs.c new file +5 -0
+	if (!ret) {
+
+		ret = mdev_device_create(kobj, dev, uuid);
+		if (ret)
+			pr_err("mdev_create: Failed to create mdev device\n");

What value does this pr_err add? It doesn't tell us why it failed and the user will already know if failed by the return value of their write.

Quoted 372 lines
drivers/vfio/mdev/mdev_sysfs.c new file +223 -0
+		else
+			ret = count;
+	}
+
+	kfree(str);
+	return ret;
+}
+
+MDEV_TYPE_ATTR_WO(create);
+
+static void mdev_type_release(struct kobject *kobj)
+{
+	struct mdev_type *type = to_mdev_type(kobj);
+
+	pr_debug("Releasing group %s\n", kobj->name);
+	kfree(type);
+}
+
+static struct kobj_type mdev_type_ktype = {
+	.sysfs_ops = &mdev_type_sysfs_ops,
+	.release = mdev_type_release,
+};
+
+struct mdev_type *add_mdev_supported_type(struct parent_device *parent,
+					  struct attribute_group *group)
+{
+	struct mdev_type *type;
+	int ret;
+
+	if (!group->name) {
+		pr_err("%s: Type name empty!\n", __func__);
+		return ERR_PTR(-EINVAL);
+	}
+
+	type = kzalloc(sizeof(*type), GFP_KERNEL);
+	if (!type)
+		return ERR_PTR(-ENOMEM);
+
+	type->kobj.kset = parent->mdev_types_kset;
+
+	ret = kobject_init_and_add(&type->kobj, &mdev_type_ktype, NULL,
+				   "%s-%s", dev_driver_string(parent->dev),
+				   group->name);
+	if (ret) {
+		kfree(type);
+		return ERR_PTR(ret);
+	}
+
+	ret = sysfs_create_file(&type->kobj, &mdev_type_attr_create.attr);
+	if (ret)
+		goto attr_create_failed;
+
+	type->devices_kobj = kobject_create_and_add("devices", &type->kobj);
+	if (!type->devices_kobj) {
+		ret = -ENOMEM;
+		goto attr_devices_failed;
+	}
+
+	ret = sysfs_create_files(&type->kobj,
+				 (const struct attribute **)group->attrs);
+	if (ret) {
+		ret = -ENOMEM;
+		goto attrs_failed;
+	}
+
+	type->group = group;
+	type->parent = parent;
+	return type;
+
+attrs_failed:
+	kobject_put(type->devices_kobj);
+attr_devices_failed:
+	sysfs_remove_file(&type->kobj, &mdev_type_attr_create.attr);
+attr_create_failed:
+	kobject_del(&type->kobj);
+	kobject_put(&type->kobj);
+	return ERR_PTR(ret);
+}
+
+static void remove_mdev_supported_type(struct mdev_type *type)
+{
+	sysfs_remove_files(&type->kobj,
+			   (const struct attribute **)type->group->attrs);
+	kobject_put(type->devices_kobj);
+	sysfs_remove_file(&type->kobj, &mdev_type_attr_create.attr);
+	kobject_del(&type->kobj);
+	kobject_put(&type->kobj);
+}
+
+static int add_mdev_supported_type_groups(struct parent_device *parent)
+{
+	int i;
+
+	for (i = 0; parent->ops->supported_type_groups[i]; i++) {
+		struct mdev_type *type;
+
+		type = add_mdev_supported_type(parent,
+					parent->ops->supported_type_groups[i]);
+		if (IS_ERR(type)) {
+			struct mdev_type *ltype, *tmp;
+
+			list_for_each_entry_safe(ltype, tmp, &parent->type_list,
+						  next) {
+				list_del(&ltype->next);
+				remove_mdev_supported_type(ltype);
+			}
+			return PTR_ERR(type);
+		}
+		list_add(&type->next, &parent->type_list);
+	}
+	return 0;
+}
+
+/* mdev sysfs Functions */
+
+void parent_remove_sysfs_files(struct parent_device *parent)
+{
+	struct mdev_type *type, *tmp;
+
+	list_for_each_entry_safe(type, tmp, &parent->type_list, next) {
+		list_del(&type->next);
+		remove_mdev_supported_type(type);
+	}
+
+	sysfs_remove_groups(&parent->dev->kobj, parent->ops->dev_attr_groups);
+	kset_unregister(parent->mdev_types_kset);
+}
+
+int parent_create_sysfs_files(struct parent_device *parent)
+{
+	int ret;
+
+	parent->mdev_types_kset = kset_create_and_add("mdev_supported_types",
+					       NULL, &parent->dev->kobj);
+
+	if (!parent->mdev_types_kset)
+		return -ENOMEM;
+
+	INIT_LIST_HEAD(&parent->type_list);
+
+	ret = sysfs_create_groups(&parent->dev->kobj,
+				  parent->ops->dev_attr_groups);
+	if (ret)
+		goto create_err;
+
+	ret = add_mdev_supported_type_groups(parent);
+	if (ret)
+		sysfs_remove_groups(&parent->dev->kobj,
+				    parent->ops->dev_attr_groups);
+	else
+		return ret;
+
+create_err:
+	kset_unregister(parent->mdev_types_kset);
+	return ret;
+}
+
+static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
+			    const char *buf, size_t count)
+{
+	unsigned long val;
+
+	if (kstrtoul(buf, 0, &val) < 0)
+		return -EINVAL;
+
+	if (val && device_remove_file_self(dev, attr)) {
+		int ret;
+
+		ret = mdev_device_remove(dev, false);
+		if (ret) {
+			device_create_file(dev, attr);
+			return ret;
+		}
+	}
+
+	return count;
+}
+
+static DEVICE_ATTR_WO(remove);
+
+static const struct attribute *mdev_device_attrs[] = {
+	&dev_attr_remove.attr,
+	NULL,
+};
+
+int  mdev_create_sysfs_files(struct device *dev, struct mdev_type *type)
+{
+	int ret;
+
+	ret = sysfs_create_files(&dev->kobj, mdev_device_attrs);
+	if (ret) {
+		pr_err("Failed to create remove sysfs entry\n");
+		return ret;
+	}
+
+	ret = sysfs_create_link(type->devices_kobj, &dev->kobj, dev_name(dev));
+	if (ret) {
+		pr_err("Failed to create symlink in types\n");
+		goto device_link_failed;
+	}
+
+	ret = sysfs_create_link(&dev->kobj, &type->kobj, "mdev_type");
+	if (ret) {
+		pr_err("Failed to create symlink in device directory\n");
+		goto type_link_failed;
+	}
+
+	return ret;
+
+type_link_failed:
+	sysfs_remove_link(type->devices_kobj, dev_name(dev));
+device_link_failed:
+	sysfs_remove_files(&dev->kobj, mdev_device_attrs);
+	return ret;
+}
+
+void mdev_remove_sysfs_files(struct device *dev, struct mdev_type *type)
+{
+	sysfs_remove_link(&dev->kobj, "mdev_type");
+	sysfs_remove_link(type->devices_kobj, dev_name(dev));
+	sysfs_remove_files(&dev->kobj, mdev_device_attrs);
+
+}
include/linux/mdev.h new file +143 -0
new file mode 100644
index 000000000000..727209b2a67f
@@ -0,0 +1,177 @@
+/*
+ * Mediated device definition
+ *
+ * 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.
+ */
+
+#ifndef MDEV_H
+#define MDEV_H
+
+#include <uapi/linux/vfio.h>
+
+struct parent_device;
+
+/* Mediated device */
+struct mdev_device {
+	struct device		dev;
+	struct parent_device	*parent;
+	uuid_le			uuid;
+	void			*driver_data;
+
+	/* internal only */
+	struct kref		ref;
+	struct list_head	next;
+	struct kobject		*type_kobj;
+};
+
+
+/**
+ * struct parent_ops - Structure to be registered for each parent device to
+ * register the device to mdev module.
+ *
+ * @owner:		The module owner.
+ * @dev_attr_groups:	Attributes of the parent device.
+ * @mdev_attr_groups:	Attributes of the mediated device.
+ * @supported_type_groups: Attributes to define supported types. It is mandatory
+ *			to provide supported types.
+ * @create:		Called to allocate basic resources in parent device's
+ *			driver for a particular mediated device. It is
+ *			mandatory to provide create ops.
+ *			@kobj: kobject of type for which 'create' is called.
+ *			@mdev: mdev_device structure on of mediated device
+ *			      that is being created
+ *			Returns integer: success (0) or error (< 0)
+ * @remove:		Called to free resources in parent device's driver for a
+ *			a mediated device. It is mandatory to provide 'remove'
+ *			ops.
+ *			@mdev: mdev_device device structure which is being
+ *			       destroyed
+ *			Returns integer: success (0) or error (< 0)
+ * @open:		Open mediated device.
+ *			@mdev: mediated device.
+ *			Returns integer: success (0) or error (< 0)
+ * @release:		release mediated device
+ *			@mdev: mediated device.
+ * @read:		Read emulation callback
+ *			@mdev: mediated device structure
+ *			@buf: read buffer
+ *			@count: number of bytes to read
+ *			@ppos: address.
+ *			Retuns number on bytes read on success or error.
+ * @write:		Write emulation callback
+ *			@mdev: mediated device structure
+ *			@buf: write buffer
+ *			@count: number of bytes to be written
+ *			@ppos: address.
+ *			Retuns number on bytes written on success or error.
+ * @ioctl:		IOCTL callback
+ *			@mdev: mediated device structure
+ *			@cmd: mediated device structure
+ *			@arg: mediated device structure
+ * @mmap:		mmap callback
+ * Parent device that support mediated device should be registered with mdev
+ * module with parent_ops structure.
+ */
+
+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);
+};
+
+/* Parent Device */
+struct parent_device {
+	struct device		*dev;
+	const struct parent_ops	*ops;
+
+	/* internal */
+	struct kref		ref;
+	struct list_head	next;
+	struct kset *mdev_types_kset;
+	struct list_head	type_list;
+};
+
+/* interface for exporting mdev supported type attributes */
+struct mdev_type_attribute {
+	struct attribute attr;
+	ssize_t (*show)(struct kobject *kobj, struct device *dev, char *buf);
+	ssize_t (*store)(struct kobject *kobj, struct device *dev,
+			 const char *buf, size_t count);
+};
+
+#define MDEV_TYPE_ATTR(_name, _mode, _show, _store)		\
+struct mdev_type_attribute mdev_type_attr_##_name =		\
+	__ATTR(_name, _mode, _show, _store)
+#define MDEV_TYPE_ATTR_RW(_name) \
+	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RW(_name)
+#define MDEV_TYPE_ATTR_RO(_name) \
+	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RO(_name)
+#define MDEV_TYPE_ATTR_WO(_name) \
+	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_WO(_name)
+
+/**
+ * struct mdev_driver - Mediated device 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;

include/linux/mdev.h line 153

Quoted 10 lines
include/linux/mdev.h new file +10 -0
+};
+
+static inline struct mdev_driver *to_mdev_driver(struct device_driver *drv)
+{
+	return drv ? container_of(drv, struct mdev_driver, driver) : NULL;
+}
+
+static inline struct mdev_device *to_mdev_device(struct device *dev)
+{
+	return dev ? container_of(dev, struct mdev_device, dev) : NULL;

Do we really need this NULL dev/drv behavior? I don't see that any of the callers can pass NULL into these. The PCI equivalents don't support this behavior and it doesn't seem they need to. Thanks,

Alex

Quoted 24 lines
include/linux/mdev.h new file +24 -0
+}
+
+static inline void *mdev_get_drvdata(struct mdev_device *mdev)
+{
+	return mdev->driver_data;
+}
+
+static inline void mdev_set_drvdata(struct mdev_device *mdev, void *data)
+{
+	mdev->driver_data = data;
+}
+
+extern struct bus_type mdev_bus_type;
+
+#define dev_is_mdev(d) ((d)->bus == &mdev_bus_type)
+
+extern int  mdev_register_device(struct device *dev,
+				 const struct parent_ops *ops);
+extern void mdev_unregister_device(struct device *dev);
+
+extern int  mdev_register_driver(struct mdev_driver *drv, struct module *owner);
+extern void mdev_unregister_driver(struct mdev_driver *drv);
+
+#endif /* MDEV_H */
#15 lore raw

Re: [PATCH v9 03/12] vfio: Rearrange functions to get vfio_group from dev

In reply to Kirti Wankhede
Quoted 8 lines · On Tue, 18 Oct 2016 02:52:03 +0530 Kirti Wankhede <[email protected]> wrote:

Rearrange functions to have common function to increment container_users.

Change-Id I1f93262bdbab75094bc24b087b29da35ba70c4c6
---
 drivers/vfio/vfio.c | 57 ++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 37 insertions(+), 20 deletions(-)

Ideally these would be two separate patches, one pulling vfio_group_get_from_dev() out of vfio_device_get_from_dev() and replacing the existing use, the other doing the same for vfio_group_add_container_user(). Otherwise it looks good. Thanks,

Alex

Quoted 96 lines
drivers/vfio/vfio.c +37 -20
index d1d70e0b011b..2e83bdf007fe 100644
@@ -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;
 
@@ -1376,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)
@@ -1685,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);
 
author maintainer
#3 lore raw

Re: [PATCH v9 01/12] vfio: Mediated device Core driver

In reply to Alex Williamson
Quoted 3 lines · Alex Williamson

On Tue, 18 Oct 2016 02:52:01 +0530
Kirti Wankhede <[email protected]> wrote:

...

Quoted 16 lines

drivers/vfio/mdev/mdev_core.c line 57

Quoted 13 lines
drivers/vfio/mdev/mdev_core.c new file +13 -0
+static struct mdev_device *__find_mdev_device(struct parent_device *parent,
+					      uuid_le uuid)
+{
+	struct device *dev;
+
+	dev = device_find_child(parent->dev, &uuid, _find_mdev_device);
+	if (!dev)
+		return NULL;
+
+	put_device(dev);
+
+	return to_mdev_device(dev);
+}

This function is only used by mdev_device_create() for the purpose of checking whether a given uuid for a parent already exists, so the

Quoted 9 lines

returned device is not actually used. However, at the point where we're using to_mdev_device() here, we don't actually hold a reference to the device, so that function call and any possible use of the returned pointer by the callee is invalid. I would either turn this into a "get" function where the callee has a device reference and needs to do a "put" on it or change this to a "exists" test where true/false is returned and the function cannot be later mis-used to do a device lookup where the reference isn't actually valid.

I'll change it to return 0 if not found and -EEXIST if found.

Quoted 16 lines
Quoted 16 lines
drivers/vfio/mdev/mdev_core.c new file +16 -0
+int mdev_device_create(struct kobject *kobj, struct device *dev, uuid_le uuid)
+{
+	int ret;
+	struct mdev_device *mdev;
+	struct parent_device *parent;
+	struct mdev_type *type = to_mdev_type(kobj);
+
+	parent = mdev_get_parent(type->parent);
+	if (!parent)
+		return -EINVAL;
+
+	/* Check for duplicate */
+	mdev = __find_mdev_device(parent, uuid);
+	if (mdev) {
+		ret = -EEXIST;
+		goto create_err;
Quoted 9 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+	}

We check here whether the {parent,uuid} already exists, but what prevents us racing with another create call with the same uuid? ie. neither exists at this point. Will device_register() fail if the device name already exists? If so, should we just rely on the error there and skip this duplicate check? If not, we need a mutex to avoid the race.

Yes, device_register() fails if device exists already with below warning. Is it ok to dump such warning? I think, this should be fine, right? then we can remove duplicate check.

If we want to avoid such warning, we should have duplication check.

[  610.847958] ------------[ cut here ]------------
[  610.855377] WARNING: CPU: 15 PID: 19839 at fs/sysfs/dir.c:31
sysfs_warn_dup+0x64/0x80
[  610.865798] sysfs: cannot create duplicate filename
'/devices/pci0000:80/0000:80:02.0/0000:83:00.0/0000:84:08.0/0000:85:00.0/83b8f4f2-509f-382f-3c1e-e6bfe0fa1234'
[  610.885101] Modules linked in:[  610.888039]  nvidia(POE)

vfio_iommu_type1 vfio_mdev mdev vfio nfsv4 dns_resolver nfs fscache sb_edac edac_core x86_pkg_temp_thermal intel_powerclamp coretemp kvm_intel kvm irqbypass crct10dif_pclmul crc32_pclmul crc32c_intel ghash_clmulni_intel aesni_intel glue_helper lrw gf128mul ablk_helper cryptd nfsd auth_rpcgss nfs_acl lockd mei_me grace iTCO_wdt iTCO_vendor_support mei ipmi_si pcspkr ioatdma i2c_i801 lpc_ich shpchp i2c_smbus mfd_core ipmi_msghandler acpi_pad uinput sunrpc xfs libcrc32c sd_mod mgag200 drm_kms_helper syscopyarea sysfillrect sysimgblt fb_sys_fops ttm drm igb ahci libahci ptp libata pps_core dca i2c_algo_bit i2c_core dm_mirror dm_region_hash dm_log dm_mod [last
unloaded: mdev]

[  610.963835] CPU: 15 PID: 19839 Comm: bash Tainted: P           OE
4.8.0-next-20161013+ #0
[  610.973779] Hardware name: Supermicro
SYS-2027GR-5204A-NC024/X9DRG-HF, BIOS 1.0c 02/28/2013
[  610.983769]  ffffc90009323ae0 ffffffff813568bf ffffc90009323b30
0000000000000000
[  610.992867]  ffffc90009323b20 ffffffff81085511 0000001f00001000
ffff8808839ef000
[  611.001954]  ffff88108b30f900 ffff88109ae368e8 ffff88109ae580b0
ffff881099cc0818
[  611.011055] Call Trace:
[  611.015087]  [<ffffffff813568bf>] dump_stack+0x63/0x84
[  611.021784]  [<ffffffff81085511>] __warn+0xd1/0xf0
[  611.028115]  [<ffffffff8108558f>] warn_slowpath_fmt+0x5f/0x80
[  611.035379]  [<ffffffff812a4e80>] ? kernfs_path_from_node+0x50/0x60
[  611.043148]  [<ffffffff812a86c4>] sysfs_warn_dup+0x64/0x80
[  611.050109]  [<ffffffff812a87ae>] sysfs_create_dir_ns+0x7e/0x90
[  611.057481]  [<ffffffff81359891>] kobject_add_internal+0xc1/0x340
[  611.065018]  [<ffffffff81359d45>] kobject_add+0x75/0xd0
[  611.071635]  [<ffffffff81483829>] device_add+0x119/0x610
[  611.078314]  [<ffffffff81483d3a>] device_register+0x1a/0x20
[  611.085261]  [<ffffffffa03c748d>] mdev_device_create+0xdd/0x200 [mdev]
[  611.093143]  [<ffffffffa03c7768>] create_store+0xa8/0xe0 [mdev]
[  611.100385]  [<ffffffffa03c76ab>] mdev_type_attr_store+0x1b/0x30 [mdev]
[  611.108309]  [<ffffffff812a7d8a>] sysfs_kf_write+0x3a/0x50
[  611.115096]  [<ffffffff812a78bb>] kernfs_fop_write+0x10b/0x190
[  611.122231]  [<ffffffff81224e97>] __vfs_write+0x37/0x140
[  611.128817]  [<ffffffff811cea84>] ? handle_mm_fault+0x724/0xd80
[  611.135976]  [<ffffffff81225da2>] vfs_write+0xb2/0x1b0
[  611.142354]  [<ffffffff81003510>] ? syscall_trace_enter+0x1d0/0x2b0
[  611.149836]  [<ffffffff812271f5>] SyS_write+0x55/0xc0
[  611.156065]  [<ffffffff81003a47>] do_syscall_64+0x67/0x180
[  611.162734]  [<ffffffff816d41eb>] entry_SYSCALL64_slow_path+0x25/0x25
[  611.170345] ---[ end trace b05a73599da2ba3f ]---
[  611.175940] ------------[ cut here ]------------
Quoted 4 lines
Quoted 4 lines
drivers/vfio/mdev/mdev_sysfs.c new file +4 -0
+static ssize_t create_store(struct kobject *kobj, struct device *dev,
+			    const char *buf, size_t count)
+{
+	char *str;
Quoted 9 lines

drivers/vfio/mdev/mdev_sysfs.c line 62

Quoted 5 lines
drivers/vfio/mdev/mdev_sysfs.c new file +5 -0
+	uuid_le uuid;
+	int ret;
+
+	if (count < UUID_STRING_LEN)
+		return -EINVAL;

Can't we also test for something unreasonably large?

Ok. I'll add that check.

Quoted 10 lines

drivers/vfio/mdev/mdev_sysfs.c line 68

Quoted 7 lines
drivers/vfio/mdev/mdev_sysfs.c new file +6 -0
+
+	str = kstrndup(buf, count, GFP_KERNEL);
+	if (!str)
+		return -ENOMEM;
+
+	ret = uuid_le_to_bin(str, &uuid);

nit, we can kfree(str) here regardless of the return.

Quoted 9 lines

drivers/vfio/mdev/mdev_sysfs.c line 73

Quoted 5 lines
drivers/vfio/mdev/mdev_sysfs.c new file +5 -0
+	if (!ret) {
+
+		ret = mdev_device_create(kobj, dev, uuid);
+		if (ret)
+			pr_err("mdev_create: Failed to create mdev device\n");

What value does this pr_err add? It doesn't tell us why it failed and the user will already know if failed by the return value of their write.

Ok, will remove it.

Quoted 7 lines
Quoted 7 lines
drivers/vfio/mdev/mdev_sysfs.c new file +7 -0
+		else
+			ret = count;
+	}
+
+	kfree(str);
+	return ret;
+}

...

Quoted 5 lines
Quoted 5 lines
include/linux/mdev.h new file +5 -0
+static inline struct mdev_driver *to_mdev_driver(struct device_driver *drv)
+{
+	return drv ? container_of(drv, struct mdev_driver, driver) : NULL;
+}
+
Quoted 9 lines

include/linux/mdev.h line 153

Quoted 3 lines
include/linux/mdev.h new file +3 -0
+static inline struct mdev_device *to_mdev_device(struct device *dev)
+{
+	return dev ? container_of(dev, struct mdev_device, dev) : NULL;

Do we really need this NULL dev/drv behavior? I don't see that any of the callers can pass NULL into these. The PCI equivalents don't support this behavior and it doesn't seem they need to. Thanks,

Ok, I'll update that.

Kirti

author maintainer
#69 lore raw

Re: [PATCH v9 12/12] docs: Sample driver to demonstrate how to use Mediated device framework.

In reply to Alex Williamson
Quoted 43 lines · Alex Williamson
Quoted 40 lines · On Tue, 18 Oct 2016 10:54:11 +0800 Dong Jia Shi <[email protected]>…

* Kirti Wankhede <[email protected]> [2016-10-18 02:52:12 +0530]:

...snip...

Quoted 24 lines
Documentation/vfio-mdev/mtty.c new file +24 -0
+static ssize_t mdev_access(struct mdev_device *mdev, char *buf,
+		size_t count, loff_t pos, bool is_write)
+{
+	struct mdev_state *mdev_state;
+	unsigned int index;
+	loff_t offset;
+	int ret = 0;
+
+	if (!mdev || !buf)
+		return -EINVAL;
+
+	mdev_state = mdev_get_drvdata(mdev);
+	if (!mdev_state) {
+		pr_err("%s mdev_state not found\n", __func__);
+		return -EINVAL;
+	}
+
+	mutex_lock(&mdev_state->ops_lock);
+
+	index = MTTY_VFIO_PCI_OFFSET_TO_INDEX(pos);
+	offset = pos & MTTY_VFIO_PCI_OFFSET_MASK;
+	switch (index) {
+	case VFIO_PCI_CONFIG_REGION_INDEX:
+

Documentation/vfio-mdev/mtty.c line 681

Quoted 10 lines
Documentation/vfio-mdev/mtty.c new file +10 -0
+#if defined(DEBUG)
+		pr_info("%s: PCI config space %s at offset 0x%llx\n",
+			 __func__, is_write ? "write" : "read", offset);
+#endif
+		if (is_write) {
+			dump_buffer(buf, count);
+			handle_pci_cfg_write(mdev_state, offset, buf, count);
+		} else {
+			memcpy(buf, (mdev_state->vconfig + offset), count);
+			dump_buffer(buf, count);  

Dear Kirti:

Quoted 7 lines
Quoted 3 lines

Shouldn't we use copy_from_user instead of memcpy on @buf here? And I'm wondering if dump_buffer could really work since it tries to dereference a *__user* marked pointor.

I agree, the __user attribute is getting lost here and we're operating on user buffers as if they were kernel buffers. That's a bug. Thanks,

Oh, yes. Thanks for catching that. While transiting all changes from v7 to v8 I missed to update these. I'll have this corrected.

Kirti.

#17 lore raw

Re: [PATCH v9 04/12] vfio iommu: Add support for mediated devices

In reply to Kirti Wankhede
Quoted 28 lines · On Tue, 18 Oct 2016 02:52:04 +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

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.

This change adds pin and unpin support for mediated device to TYPE1 IOMMU backend module. More details:
- When iommu_group of mediated devices is attached, task structure is cached which is used later to pin pages and page accounting.
- It keeps track of pinned pages for mediated domain. This data is used to verify unpinning request and to unpin remaining pages while detaching, if there are any.
- Used existing mechanism for page accounting. If iommu capable domain exist in the container then all pages are already pinned and accounted. Accouting for mdev device is only done if there is no iommu capable domain in the container.
- Page accouting is updated on hot plug and unplug mdev device and pass through device.

Quoted 8 lines

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

Were you able to do these with the locked memory limit of the user set to the minimum required for existing GPU assignment?

Quoted 25 lines
Change-Id I295d6f0f2e0579b8d9882bfd8fd5a4194b97bd9a
---
 drivers/vfio/vfio.c             |  98 ++++++
 drivers/vfio/vfio_iommu_type1.c | 692 ++++++++++++++++++++++++++++++++++------
 include/linux/vfio.h            |  13 +-
 3 files changed, 707 insertions(+), 96 deletions(-)
drivers/vfio/vfio.c +7 -0
index 2e83bdf007fe..a5a210005b65 100644
@@ -1799,6 +1799,104 @@ 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

drivers/vfio/vfio.c void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset) · line 1818

Quoted 10 lines
drivers/vfio/vfio.c +10 -0
+ * @prot [in] : protection flags
+ * @phys_pfn[out] : array of host PFNs
+ */
+long vfio_pin_pages(struct device *dev, unsigned long *user_pfn,
+		    long npage, int prot, unsigned long *phys_pfn)
+{
+	struct vfio_container *container;
+	struct vfio_group *group;
+	struct vfio_iommu_driver *driver;
+	ssize_t ret = -EINVAL;

Unused initialization.

Quoted 4 lines
drivers/vfio/vfio.c +4 -0
+
+	if (!dev || !user_pfn || !phys_pfn)
+		return -EINVAL;
+

drivers/vfio/vfio.c void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset) · line 1832

Quoted 10 lines
drivers/vfio/vfio.c +10 -0
+	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;
+	if (IS_ERR(container)) {

I don't see that we ever use an ERR_PTR to set group->container, it should either be NULL or valid and the fact that we added ourselves to container_users should mean that it's valid. The paranoia test here would be if container is NULL, but IS_ERR() doesn't check NULL. If we need that paranoia test, maybe we should just:

if (WARN_ON(!container)) {

I'm not fully convinced it's needed though.

drivers/vfio/vfio.c void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset) · line 1842

Quoted 10 lines
drivers/vfio/vfio.c +10 -0
+		ret = PTR_ERR(container);
+		goto err_pin_pages;
+	}
+
+	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);

The caller is going to need to provide some means for us to callback to invalidate pinned pages.

ret has already been used, so it's zero at this point. I expect the original intention was to let the initialization above fall through here so that the caller gets an errno if the driver doesn't support pin_pages. Returning zero without actually doing anything seems like an unexpected return value.

Quoted 13 lines
drivers/vfio/vfio.c +13 -0
+
+	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.

drivers/vfio/vfio.c void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset) · line 1865

Quoted 10 lines
drivers/vfio/vfio.c +10 -0
+ * @dev [in] : device
+ * @pfn [in] : array of host PFNs to be unpinned.
+ * @npage [in] :count of elements in array, that is number of pages.
+ */
+long vfio_unpin_pages(struct device *dev, unsigned long *pfn, long npage)
+{
+	struct vfio_container *container;
+	struct vfio_group *group;
+	struct vfio_iommu_driver *driver;
+	ssize_t ret = -EINVAL;

Same unused initialization.

Quoted 4 lines
drivers/vfio/vfio.c +4 -0
+
+	if (!dev || !pfn)
+		return -EINVAL;
+

drivers/vfio/vfio.c void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset) · line 1879

Quoted 10 lines
drivers/vfio/vfio.c +10 -0
+	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;
+	if (IS_ERR(container)) {

Same container not as above.

drivers/vfio/vfio.c void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset) · line 1889

Quoted 10 lines
drivers/vfio/vfio.c +10 -0
+		ret = PTR_ERR(container);
+		goto err_unpin_pages;
+	}
+
+	down_read(&container->group_lock);
+
+	driver = container->iommu_driver;
+	if (likely(driver && driver->ops->unpin_pages))
+		ret = driver->ops->unpin_pages(container->iommu_data, pfn,
+					       npage);

Same fall through, zero return value as above.

Quoted 54 lines
drivers/vfio/vfio.c +10 -0
+
+	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 +9 -0
index 2ba19424e4a1..5d67058a611d 100644
@@ -55,16 +55,24 @@ MODULE_PARM_DESC(disable_hugepages,
 
 struct vfio_iommu {
 	struct list_head	domain_list;
+	struct vfio_domain	*local_domain;
 	struct mutex		lock;
 	struct rb_root		dma_list;
 	bool			v2;
 	bool			nesting;
 };
 
+struct local_addr_space {
+	struct task_struct	*task;
+	struct rb_root		pfn_list;	/* pinned Host pfn list */
+	struct mutex		pfn_list_lock;	/* mutex for pfn_list */
+};
+
 struct vfio_domain {
 	struct iommu_domain	*domain;
 	struct list_head	next;
 	struct list_head	group_list;
+	struct local_addr_space	*local_addr_space;
 	int			prot;		/* IOMMU_CACHE */
 	bool			fgsp;		/* Fine-grained super pages */
 };
@@ -75,6 +83,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_group {
@@ -83,6 +92,21 @@ struct vfio_group {
 };
 
 /*

drivers/vfio/vfio_iommu_type1.c struct vfio_group { · line 104

Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10 -0
+ * Guest RAM pinning working set or DMA target
+ */
+struct vfio_pfn {
+	struct rb_node		node;
+	unsigned long		vaddr;		/* virtual addr */
+	dma_addr_t		iova;		/* IOVA */
+	unsigned long		pfn;		/* Host pfn */
+	int			prot;
+	atomic_t		ref_count;
+};

Somehow we're going to need to fit an invalidation callback here too. How would we handle a case where there are multiple mdev devices, from different vendor drivers, that all have the same pfn pinned? I'm already concerned about the per pfn overhead we're introducing here so clearly we cannot store an invalidation callback per pinned page, per vendor driver. Perhaps invalidations should be done using a notifier chain per vfio_iommu, the vendor drivers are required to register on that chain (fail pinning with empty notifier list) user unmapping will be broadcast to the notifier chain, the vendor driver will be responsible for deciding if each unmap is relevant to them (potentially it's for a pinning from another driver).

I expect we also need to enforce that vendors perform a synchronous unmap such that after returning from the notifier list call, the vfio_pfn should no longer exist. If it does we might need to BUG_ON. Also be careful to pay attention to the locking of the notifier vs unpin callbacks to avoid deadlocks.

Quoted 96 lines
drivers/vfio/vfio_iommu_type1.c +89 -0
+
+#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
  */
@@ -130,6 +154,101 @@ static void vfio_unlink_dma(struct vfio_iommu *iommu, struct vfio_dma *old)
 	rb_erase(&old->node, &iommu->dma_list);
 }
 
+/*
+ * Helper Functions for host pfn list
+ */
+
+static struct vfio_pfn *vfio_find_pfn(struct vfio_domain *domain,
+				      unsigned long pfn)
+{
+	struct rb_node *node;
+	struct vfio_pfn *vpfn;
+
+	node = domain->local_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_domain *domain, struct vfio_pfn *new)
+{
+	struct rb_node **link, *parent = NULL;
+	struct vfio_pfn *vpfn;
+
+	link = &domain->local_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, &domain->local_addr_space->pfn_list);
+}
+
+static void vfio_unlink_pfn(struct vfio_domain *domain, struct vfio_pfn *old)
+{
+	rb_erase(&old->node, &domain->local_addr_space->pfn_list);
+}
+
+static int vfio_add_to_pfn_list(struct vfio_domain *domain, unsigned long vaddr,
+				dma_addr_t iova, unsigned long pfn, int prot)
+{
+	struct vfio_pfn *vpfn;
+
+	vpfn = kzalloc(sizeof(*vpfn), GFP_KERNEL);
+	if (!vpfn)
+		return -ENOMEM;
+
+	vpfn->vaddr = vaddr;
+	vpfn->iova = iova;
+	vpfn->pfn = pfn;
+	vpfn->prot = prot;
+	atomic_set(&vpfn->ref_count, 1);
+	vfio_link_pfn(domain, vpfn);
+	return 0;
+}
+
+static void vfio_remove_from_pfn_list(struct vfio_domain *domain,
+				      struct vfio_pfn *vpfn)
+{
+	vfio_unlink_pfn(domain, vpfn);
+	kfree(vpfn);
+}
+
+static int vfio_pfn_account(struct vfio_iommu *iommu, unsigned long pfn)
+{
+	struct vfio_pfn *p;
+	struct vfio_domain *domain = iommu->local_domain;
+	int ret = 1;
+
+	if (!domain)
+		return 1;

drivers/vfio/vfio_iommu_type1.c static void vfio_unlink_dma(struct vfio_iommu *iommu, struct vfio_dma *old) · line 250

Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10 -0
+
+	mutex_lock(&domain->local_addr_space->pfn_list_lock);
+
+	p = vfio_find_pfn(domain, pfn);
+	if (p)
+		ret = 0;
+
+	mutex_unlock(&domain->local_addr_space->pfn_list_lock);
+	return ret;
+}

So if the vfio_pfn for a given pfn exists, return 0, else return 1. But do we know that the vfio_pfn exists at the point where we actually do that accounting?

Quoted 37 lines
drivers/vfio/vfio_iommu_type1.c +7 -6
+
 struct vwork {
 	struct mm_struct	*mm;
 	long			npage;
@@ -150,17 +269,17 @@ 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 task_struct *task, long npage)
 {
 	struct vwork *vwork;
 	struct mm_struct *mm;
 
-	if (!current->mm || !npage)
+	if (!task->mm || !npage)
 		return; /* process exited or nothing to do */
 
-	if (down_write_trylock(&current->mm->mmap_sem)) {
-		current->mm->locked_vm += npage;
-		up_write(&current->mm->mmap_sem);
+	if (down_write_trylock(&task->mm->mmap_sem)) {
+		task->mm->locked_vm += npage;
+		up_write(&task->mm->mmap_sem);
 		return;
 	}
 
@@ -172,7 +291,7 @@ static void vfio_lock_acct(long npage)
 	vwork = kmalloc(sizeof(struct vwork), GFP_KERNEL);
 	if (!vwork)
 		return;
-	mm = get_task_mm(current);
+	mm = get_task_mm(task);
 	if (!mm) {
 		kfree(vwork);
 		return;

drivers/vfio/vfio_iommu_type1.c static int put_pfn(unsigned long pfn, int prot) · line 348

Quoted 2 lines
drivers/vfio/vfio_iommu_type1.c +0 -0
@@ -228,20 +347,31 @@ static int put_pfn(unsigned long pfn, int prot)
 	return 0;
 }

This coversion of vfio_lock_acct() to pass a task_struct and updating existing callers to pass current would be a great separate, easily review-able patch.

Quoted 34 lines
drivers/vfio/vfio_iommu_type1.c +15 -4
-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;
+	struct mm_struct *local_mm = (mm ? mm : current->mm);
 	int ret = -EFAULT;
 
-	if (get_user_pages_fast(vaddr, 1, !!(prot & IOMMU_WRITE), page) == 1) {
+	if (mm) {
+		down_read(&local_mm->mmap_sem);
+		ret = get_user_pages_remote(NULL, local_mm, vaddr, 1,
+					!!(prot & IOMMU_WRITE), 0, page, NULL);
+		up_read(&local_mm->mmap_sem);
+	} else
+		ret = get_user_pages_fast(vaddr, 1,
+					  !!(prot & IOMMU_WRITE), page);
+
+	if (ret == 1) {
 		*pfn = page_to_pfn(page[0]);
 		return 0;
 	}
 
-	down_read(&current->mm->mmap_sem);
+	down_read(&local_mm->mmap_sem);
 
-	vma = find_vma_intersection(current->mm, vaddr, vaddr + 1);
+	vma = find_vma_intersection(local_mm, vaddr, vaddr + 1);
 
 	if (vma && vma->vm_flags & VM_PFNMAP) {
 		*pfn = ((vaddr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;

drivers/vfio/vfio_iommu_type1.c static int vaddr_get_pfn(unsigned long vaddr, int prot, unsigned long *pfn) · line 385

Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c +1 -1
@@ -249,7 +379,7 @@ static int vaddr_get_pfn(unsigned long vaddr, int prot, unsigned long *pfn)
 			ret = 0;
 	}
 
-	up_read(&current->mm->mmap_sem);
+	up_read(&local_mm->mmap_sem);
 
 	return ret;
 }

This would also be a great separate patch. Have you considered renaming the mm_struct function arg to "remote_mm" and making the local variable simply "mm"? It seems like it would tie nicely with the remote_mm path using get_user_pages_remote() while passing NULL for remote_mm uses current->mm and the existing path (and avoid the general oddness of passing local_mm to a "remote" function).

Quoted 27 lines
drivers/vfio/vfio_iommu_type1.c +7 -4
@@ -259,33 +389,37 @@ 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(struct vfio_iommu *iommu,
+				    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);
-	long ret, i;
+	long ret, i, lock_acct = 0;
 	bool rsvd;
 
 	if (!current->mm)
 		return -ENODEV;
 
-	ret = vaddr_get_pfn(vaddr, prot, pfn_base);
+	ret = vaddr_get_pfn(NULL, vaddr, prot, pfn_base);
 	if (ret)
 		return ret;
 
+	lock_acct = vfio_pfn_account(iommu, *pfn_base);
+
 	rsvd = is_invalid_reserved_pfn(*pfn_base);

drivers/vfio/vfio_iommu_type1.c static int vaddr_get_pfn(unsigned long vaddr, int prot, unsigned long *pfn) · line 419

Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +2 -1
 
-	if (!rsvd && !lock_cap && current->mm->locked_vm + 1 > limit) {
+	if (!rsvd && !lock_cap && current->mm->locked_vm + lock_acct > limit) {
 		put_pfn(*pfn_base, prot);
 		pr_warn("%s: RLIMIT_MEMLOCK (%ld) exceeded\n", __func__,
 			limit << PAGE_SHIFT);
 		return -ENOMEM;
 	}
 
+

Extra whitespace

Quoted 17 lines
drivers/vfio/vfio_iommu_type1.c +2 -2
 	if (unlikely(disable_hugepages)) {
 		if (!rsvd)
-			vfio_lock_acct(1);
+			vfio_lock_acct(current, lock_acct);
 		return 1;
 	}
 
@@ -293,7 +427,7 @@ static long vfio_pin_pages(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(NULL, vaddr, prot, &pfn);
 		if (ret)
 			break;
 

drivers/vfio/vfio_iommu_type1.c static long vfio_pin_pages(unsigned long vaddr, long npage, · line 441

Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +2 -0
@@ -303,8 +437,10 @@ static long vfio_pin_pages(unsigned long vaddr, long npage,
 			break;
 		}
 
+		lock_acct += vfio_pfn_account(iommu, pfn);
+

I take it that this is the new technique for keeping the accounting accurate, we only increment the locked accounting by the amount not already pinned in a vfio_pfn.

Quoted 11 lines
drivers/vfio/vfio_iommu_type1.c +1 -2
 		if (!rsvd && !lock_cap &&
-		    current->mm->locked_vm + i + 1 > limit) {
+		    current->mm->locked_vm + lock_acct > limit) {
 			put_pfn(pfn, prot);
 			pr_warn("%s: RLIMIT_MEMLOCK (%ld) exceeded\n",
 				__func__, limit << PAGE_SHIFT);
@@ -313,23 +449,216 @@ static long vfio_pin_pages(unsigned long vaddr, long npage,
 	}
 
 	if (!rsvd)
-		vfio_lock_acct(i);

drivers/vfio/vfio_iommu_type1.c static long vfio_pin_pages(unsigned long vaddr, long npage, · line 459

Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +4 -2
+		vfio_lock_acct(current, lock_acct);
 
 	return i;
 }
 
-static long vfio_unpin_pages(unsigned long pfn, long npage,
-			     int prot, bool do_accounting)
+static long __vfio_unpin_pages_remote(struct vfio_iommu *iommu,
+				      unsigned long pfn, long npage, int prot,
+				      bool do_accounting)

Have you noticed that it's kind of confusing that __vfio_{un}pin_pages_remote() uses current, which does a get_user_pages_fast() while "local" uses a provided task_struct and uses get_user_pages_*remote*()? And also what was effectively local (ie. we're pinning for our own use here) is now "remote" and pinning for a remote, vendor driver consumer, is now "local". It's not very intuitive.

Quoted 436 lines
drivers/vfio/vfio_iommu_type1.c +299 -46
 {
-	unsigned long unlocked = 0;
+	unsigned long unlocked = 0, unlock_acct = 0;
 	long i;
 
-	for (i = 0; i < npage; i++)
+	for (i = 0; i < npage; i++) {
+		if (do_accounting)
+			unlock_acct += vfio_pfn_account(iommu, pfn);
+
 		unlocked += put_pfn(pfn++, prot);
+	}
 
 	if (do_accounting)
-		vfio_lock_acct(-unlocked);
+		vfio_lock_acct(current, -unlock_acct);
+
+	return unlocked;
+}
+
+static long __vfio_pin_page_local(struct vfio_domain *domain,
+				  unsigned long vaddr, int prot,
+				  unsigned long *pfn_base,
+				  bool do_accounting)
+{
+	unsigned long limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
+	bool lock_cap = capable(CAP_IPC_LOCK);
+	long ret;
+	bool rsvd;
+	struct task_struct *task = domain->local_addr_space->task;
+
+	if (!task->mm)
+		return -ENODEV;
+
+	ret = vaddr_get_pfn(task->mm, vaddr, prot, pfn_base);
+	if (ret)
+		return ret;
+
+	rsvd = is_invalid_reserved_pfn(*pfn_base);
+
+	if (!rsvd && !lock_cap && task->mm->locked_vm + 1 > limit) {
+		put_pfn(*pfn_base, prot);
+		pr_warn("%s: RLIMIT_MEMLOCK (%ld) exceeded\n", __func__,
+			limit << PAGE_SHIFT);
+		return -ENOMEM;
+	}
+
+	if (!rsvd && do_accounting)
+		vfio_lock_acct(task, 1);
+
+	return 1;
+}
+
+static void __vfio_unpin_page_local(struct vfio_domain *domain,
+				    unsigned long pfn, int prot,
+				    bool do_accounting)
+{
+	put_pfn(pfn, prot);
+
+	if (do_accounting)
+		vfio_lock_acct(domain->local_addr_space->task, -1);
+}
+
+static int vfio_unpin_pfn(struct vfio_domain *domain,
+			  struct vfio_pfn *vpfn, bool do_accounting)
+{
+	__vfio_unpin_page_local(domain, vpfn->pfn, vpfn->prot,
+				do_accounting);
+
+	if (atomic_dec_and_test(&vpfn->ref_count))
+		vfio_remove_from_pfn_list(domain, vpfn);
+
+	return 1;
+}
+
+static long vfio_iommu_type1_pin_pages(void *iommu_data,
+				       unsigned long *user_pfn,
+				       long npage, int prot,
+				       unsigned long *phys_pfn)
+{
+	struct vfio_iommu *iommu = iommu_data;
+	struct vfio_domain *domain;
+	int i, j, ret;
+	long retpage;
+	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->local_domain) {
+		ret = -EINVAL;
+		goto pin_done;
+	}
+
+	domain = iommu->local_domain;
+
+	/*
+	 * 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;
+
+		retpage = __vfio_pin_page_local(domain, remote_vaddr, prot,
+						&pfn[i], do_accounting);
+		if (retpage <= 0) {
+			WARN_ON(!retpage);
+			ret = (int)retpage;
+			goto pin_unwind;
+		}
+
+		mutex_lock(&domain->local_addr_space->pfn_list_lock);
+
+		/* search if pfn exist */
+		p = vfio_find_pfn(domain, pfn[i]);
+		if (p) {
+			atomic_inc(&p->ref_count);
+			mutex_unlock(&domain->local_addr_space->pfn_list_lock);
+			continue;
+		}
+
+		ret = vfio_add_to_pfn_list(domain, remote_vaddr, iova,
+					   pfn[i], prot);
+		mutex_unlock(&domain->local_addr_space->pfn_list_lock);
+
+		if (ret) {
+			__vfio_unpin_page_local(domain, pfn[i], prot,
+						do_accounting);
+			goto pin_unwind;
+		}
+	}
+
+	ret = i;
+	goto pin_done;
+
+pin_unwind:
+	pfn[i] = 0;
+	mutex_lock(&domain->local_addr_space->pfn_list_lock);
+	for (j = 0; j < i; j++) {
+		struct vfio_pfn *p;
+
+		p = vfio_find_pfn(domain, pfn[j]);
+		if (p)
+			vfio_unpin_pfn(domain, p, do_accounting);
+
+		pfn[j] = 0;
+	}
+	mutex_unlock(&domain->local_addr_space->pfn_list_lock);
+
+pin_done:
+	mutex_unlock(&iommu->lock);
+	return ret;
+}
+
+static long vfio_iommu_type1_unpin_pages(void *iommu_data, unsigned long *pfn,
+					 long npage)
+{
+	struct vfio_iommu *iommu = iommu_data;
+	struct vfio_domain *domain = NULL;
+	bool do_accounting;
+	long unlocked = 0;
+	int i;
+
+	if (!iommu || !pfn)
+		return -EINVAL;
+
+	mutex_lock(&iommu->lock);
+
+	domain = iommu->local_domain;
+
+	do_accounting = !IS_IOMMU_CAP_DOMAIN_IN_CONTAINER(iommu);
+
+	mutex_lock(&domain->local_addr_space->pfn_list_lock);
+
+	for (i = 0; i < npage; i++) {
+		struct vfio_pfn *p;
 
+		/* verify if pfn exist in pfn_list */
+		p = vfio_find_pfn(domain, pfn[i]);
+		if (p)
+			unlocked += vfio_unpin_pfn(domain, p, do_accounting);
+
+	}
+	mutex_unlock(&domain->local_addr_space->pfn_list_lock);
+
+	mutex_unlock(&iommu->lock);
 	return unlocked;
 }
 
@@ -341,6 +670,10 @@ static void vfio_unmap_unpin(struct vfio_iommu *iommu, struct vfio_dma *dma)
 
 	if (!dma->size)
 		return;
+
+	if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER(iommu))
+		return;
+
 	/*
 	 * We use the IOMMU to track the physical addresses, otherwise we'd
 	 * need a much more complicated tracking system.  Unfortunately that
@@ -382,15 +715,16 @@ 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(iommu, phys >> PAGE_SHIFT,
+						      unmapped >> PAGE_SHIFT,
+						      dma->prot, false);
 		iova += unmapped;
 
 		cond_resched();
 	}
 
-	vfio_lock_acct(-unlocked);
+	dma->iommu_mapped = false;
+	vfio_lock_acct(current, -unlocked);
 }
 
 static void vfio_remove_dma(struct vfio_iommu *iommu, struct vfio_dma *dma)
@@ -558,17 +892,57 @@ 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(iommu, 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(iommu, pfn, npage, dma->prot,
+						  true);
+			break;
+		}
+
+		size -= npage << PAGE_SHIFT;
+		dma->size += npage << PAGE_SHIFT;
+	}
+
+	dma->iommu_mapped = true;
+
+	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;
 
 	/* Verify that none of our __u64 fields overflow */
 	if (map->size != size || map->vaddr != vaddr || map->iova != iova)
@@ -611,29 +985,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);
 
-	while (size) {
-		/* Pin a contiguous chunk of memory */
-		npage = vfio_pin_pages(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(pfn, npage, prot, true);
-			break;
-		}
-
-		size -= npage << PAGE_SHIFT;
-		dma->size += npage << PAGE_SHIFT;
-	}
-
-	if (ret)
-		vfio_remove_dma(iommu, dma);
+	/* 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);
 
 	mutex_unlock(&iommu->lock);
 	return ret;
@@ -662,10 +1018,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;
@@ -674,20 +1026,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(iommu, 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);
@@ -696,6 +1071,8 @@ static int vfio_iommu_replay(struct vfio_iommu *iommu,
 
 			iova += size;
 		}
+
+		dma->iommu_mapped = true;
 	}
 
 	return 0;
@@ -734,11 +1111,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;
@@ -746,10 +1136,14 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,
 	mutex_lock(&iommu->lock);

drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_attach_group(void *iommu_data, · line 1143

Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +5 -3
 
 	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;
+		}
+	}

The find_iommu_group() conversion would also be an easy separate patch.

Quoted 20 lines
drivers/vfio/vfio_iommu_type1.c +12 -0
+	if (iommu->local_domain) {
+		if (find_iommu_group(iommu->local_domain, iommu_group)) {
 			mutex_unlock(&iommu->lock);
 			return -EINVAL;
 		}
@@ -769,6 +1163,30 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,
 	if (ret)
 		goto out_free;
 
+	if (IS_ENABLED(CONFIG_VFIO_MDEV) && !iommu_present(bus) &&
+	    (bus == &mdev_bus_type)) {
+		if (!iommu->local_domain) {
+			domain->local_addr_space =
+				kzalloc(sizeof(*domain->local_addr_space),
+						GFP_KERNEL);
+			if (!domain->local_addr_space) {
+				ret = -ENOMEM;
+				goto out_free;
+			}

drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_attach_group(void *iommu_data, · line 1185

Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10 -0
+
+			domain->local_addr_space->task = current;
+			INIT_LIST_HEAD(&domain->group_list);
+			domain->local_addr_space->pfn_list = RB_ROOT;
+			mutex_init(&domain->local_addr_space->pfn_list_lock);
+			iommu->local_domain = domain;
+		} else
+			kfree(domain);
+
+		list_add(&group->next, &domain->group_list);

I think you mean s/domain/iommu->local_domain/ here, we just freed domain in the else path.

Quoted 23 lines
drivers/vfio/vfio_iommu_type1.c +16 -0
+		mutex_unlock(&iommu->lock);
+		return 0;
+	}
+
 	domain->domain = iommu_domain_alloc(bus);
 	if (!domain->domain) {
 		ret = -EIO;
@@ -859,6 +1277,41 @@ 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_domain *domain = iommu->local_domain;
+	struct vfio_dma *dma, *tdma;
+	struct rb_node *n;
+	long locked = 0;
+
+	rbtree_postorder_for_each_entry_safe(dma, tdma, &iommu->dma_list,
+					     node) {
+		vfio_unmap_unpin(iommu, dma);
+	}
+

drivers/vfio/vfio_iommu_type1.c static void vfio_iommu_unmap_unpin_all(struct vfio_iommu *iommu) · line 1301

Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10 -0
+	mutex_lock(&domain->local_addr_space->pfn_list_lock);
+
+	n = rb_first(&domain->local_addr_space->pfn_list);
+
+	for (; n; n = rb_next(n))
+		locked++;
+
+	vfio_lock_acct(domain->local_addr_space->task, locked);
+	mutex_unlock(&domain->local_addr_space->pfn_list_lock);
+}

Couldn't a properly timed mlock by the user allow them to lock more memory than they're allowed here? For instance imagine the vendor driver has pinned the entire VM memory and the user has exactly the locked memory limit for that VM. During the gap here between unpinning the entire vfio_dma list and re-accounting for the pfn_list, the user can mlock up to their limit again an now they've doubled the locked memory they're allowed.

Quoted 38 lines
drivers/vfio/vfio_iommu_type1.c +18 -11
+
+static void vfio_local_unpin_all(struct vfio_domain *domain)
+{
+	struct rb_node *node;
+
+	mutex_lock(&domain->local_addr_space->pfn_list_lock);
+	while ((node = rb_first(&domain->local_addr_space->pfn_list)))
+		vfio_unpin_pfn(domain,
+				rb_entry(node, struct vfio_pfn, node), false);
+
+	mutex_unlock(&domain->local_addr_space->pfn_list_lock);
+}
+
 static void vfio_iommu_type1_detach_group(void *iommu_data,
 					  struct iommu_group *iommu_group)
 {
@@ -868,31 +1321,57 @@ 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;
-
-			iommu_detach_group(domain->domain, iommu_group);
+	if (iommu->local_domain) {
+		domain = iommu->local_domain;
+		group = find_iommu_group(domain, iommu_group);
+		if (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.
-			 */
+

drivers/vfio/vfio_iommu_type1.c static void vfio_iommu_type1_detach_group(void *iommu_data, · line 1337

Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +4 -3
 			if (list_empty(&domain->group_list)) {
-				if (list_is_singular(&iommu->domain_list))
+				vfio_local_unpin_all(domain);
+				if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER(iommu))
 					vfio_iommu_unmap_unpin_all(iommu);
-				iommu_domain_free(domain->domain);
-				list_del(&domain->next);
 				kfree(domain);
+				iommu->local_domain = NULL;
+			}

I can't quite wrap my head around this, if we have mdev groups attached and this iommu group matches an mdev group, remove from list and free the group. If there are now no more groups in the mdev group list, then for each vfio_pfn, unpin the pfn, /without/ doing accounting udpates and remove the vfio_pfn, but only if the ref_count is now zero. We free the domain, so if the ref_count was non-zero we've now just leaked memory. I think that means that if a vendor driver pins a given page twice, that leak occurs. Furthermore, if there is not an iommu capable domain in the container, we remove all the vfio_dma entries as well, ok. Maybe the only issue is those leaked vfio_pfns.

Quoted 70 lines
drivers/vfio/vfio_iommu_type1.c +52 -3
+			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);
+		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 with
+		 * iommu and local domain doesn't exist, then all the mappings
+		 * go away too. If it's the last domain with iommu and local
+		 * domain exist, update accounting
+		 */
+		if (list_empty(&domain->group_list)) {
+			if (list_is_singular(&iommu->domain_list)) {
+				if (!iommu->local_domain)
+					vfio_iommu_unmap_unpin_all(iommu);
+				else
+					vfio_iommu_unmap_unpin_reaccount(iommu);
 			}
-			goto done;
+			iommu_domain_free(domain->domain);
+			list_del(&domain->next);
+			kfree(domain);
 		}
+		break;
 	}
 
-done:
+detach_group_done:
 	mutex_unlock(&iommu->lock);
 }
 
@@ -924,27 +1403,48 @@ static void *vfio_iommu_type1_open(unsigned long arg)
 	return iommu;
 }
 
+static void vfio_release_domain(struct vfio_domain *domain)
+{
+	struct vfio_group *group, *group_tmp;
+
+	list_for_each_entry_safe(group, group_tmp,
+				 &domain->group_list, next) {
+		if (!domain->local_addr_space)
+			iommu_detach_group(domain->domain, group->iommu_group);
+		list_del(&group->next);
+		kfree(group);
+	}
+
+	if (domain->local_addr_space)
+		vfio_local_unpin_all(domain);
+	else
+		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;
+

drivers/vfio/vfio_iommu_type1.c static void *vfio_iommu_type1_open(unsigned long arg) · line 1438

Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +7 -0
+	if (iommu->local_domain) {
+		vfio_release_domain(iommu->local_domain);
+		kfree(iommu->local_domain);
+		iommu->local_domain = NULL;
+	}
 
 	vfio_iommu_unmap_unpin_all(iommu);
 
+	if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER(iommu))
+		goto release_exit;

This is a bit redundant, the below for_each should just have no entries and we skip to there anyway. Thanks,

Alex

Quoted 66 lines
drivers/vfio/vfio_iommu_type1.c +5 -7
+
 	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);
 		list_del(&domain->next);
 		kfree(domain);
 	}
 
+release_exit:
 	kfree(iommu);
 }
 
@@ -1048,6 +1548,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)
include/linux/vfio.h +12 -1
index 0ecae0b1cd34..0bd25ba6223d 100644
@@ -17,6 +17,7 @@
 #include <linux/workqueue.h>
 #include <linux/poll.h>
 #include <uapi/linux/vfio.h>
+#include <linux/mdev.h>
 
 /**
  * struct vfio_device_ops - VFIO bus driver device callbacks
@@ -75,7 +76,11 @@ struct vfio_iommu_driver_ops {
 					struct iommu_group *group);
 	void		(*detach_group)(void *iommu_data,
 					struct iommu_group *group);
-
+	long		(*pin_pages)(void *iommu_data, unsigned long *user_pfn,
+				     long npage, int prot,
+				     unsigned long *phys_pfn);
+	long		(*unpin_pages)(void *iommu_data, unsigned long *pfn,
+				       long npage);
 };
 
 extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
@@ -127,6 +132,12 @@ static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
 }
 #endif /* CONFIG_EEH */
 
+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);
+
 /*
  * IRQfd - generic
  */
#4 lore raw

Re: [PATCH v9 01/12] vfio: Mediated device Core driver

In reply to Kirti Wankhede
Quoted 60 lines · On Thu, 20 Oct 2016 00:46:48 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 3 lines · Alex Williamson

On Tue, 18 Oct 2016 02:52:01 +0530
Kirti Wankhede <[email protected]> wrote:

...

Quoted 16 lines

drivers/vfio/mdev/mdev_core.c line 57

Quoted 13 lines
drivers/vfio/mdev/mdev_core.c new file +13 -0
+static struct mdev_device *__find_mdev_device(struct parent_device *parent,
+					      uuid_le uuid)
+{
+	struct device *dev;
+
+	dev = device_find_child(parent->dev, &uuid, _find_mdev_device);
+	if (!dev)
+		return NULL;
+
+	put_device(dev);
+
+	return to_mdev_device(dev);
+}  

This function is only used by mdev_device_create() for the purpose of checking whether a given uuid for a parent already exists, so the

Quoted 9 lines

returned device is not actually used. However, at the point where we're using to_mdev_device() here, we don't actually hold a reference to the device, so that function call and any possible use of the returned pointer by the callee is invalid. I would either turn this into a "get" function where the callee has a device reference and needs to do a "put" on it or change this to a "exists" test where true/false is returned and the function cannot be later mis-used to do a device lookup where the reference isn't actually valid.

I'll change it to return 0 if not found and -EEXIST if found.

Quoted 16 lines
Quoted 16 lines
drivers/vfio/mdev/mdev_core.c new file +16 -0
+int mdev_device_create(struct kobject *kobj, struct device *dev, uuid_le uuid)
+{
+	int ret;
+	struct mdev_device *mdev;
+	struct parent_device *parent;
+	struct mdev_type *type = to_mdev_type(kobj);
+
+	parent = mdev_get_parent(type->parent);
+	if (!parent)
+		return -EINVAL;
+
+	/* Check for duplicate */
+	mdev = __find_mdev_device(parent, uuid);
+	if (mdev) {
+		ret = -EEXIST;
+		goto create_err;
Quoted 9 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+	}  

We check here whether the {parent,uuid} already exists, but what prevents us racing with another create call with the same uuid? ie. neither exists at this point. Will device_register() fail if the device name already exists? If so, should we just rely on the error there and skip this duplicate check? If not, we need a mutex to avoid the race.

Quoted 5 lines

Yes, device_register() fails if device exists already with below warning. Is it ok to dump such warning? I think, this should be fine, right? then we can remove duplicate check.

If we want to avoid such warning, we should have duplication check.

We should avoid such warnings, bugs will get filed otherwise. Thanks for checking. Thanks,

Alex

Quoted 121 lines
[  610.847958] ------------[ cut here ]------------
[  610.855377] WARNING: CPU: 15 PID: 19839 at fs/sysfs/dir.c:31
sysfs_warn_dup+0x64/0x80
[  610.865798] sysfs: cannot create duplicate filename
'/devices/pci0000:80/0000:80:02.0/0000:83:00.0/0000:84:08.0/0000:85:00.0/83b8f4f2-509f-382f-3c1e-e6bfe0fa1234'
[  610.885101] Modules linked in:[  610.888039]  nvidia(POE)

vfio_iommu_type1 vfio_mdev mdev vfio nfsv4 dns_resolver nfs fscache sb_edac edac_core x86_pkg_temp_thermal intel_powerclamp coretemp kvm_intel kvm irqbypass crct10dif_pclmul crc32_pclmul crc32c_intel ghash_clmulni_intel aesni_intel glue_helper lrw gf128mul ablk_helper cryptd nfsd auth_rpcgss nfs_acl lockd mei_me grace iTCO_wdt iTCO_vendor_support mei ipmi_si pcspkr ioatdma i2c_i801 lpc_ich shpchp i2c_smbus mfd_core ipmi_msghandler acpi_pad uinput sunrpc xfs libcrc32c sd_mod mgag200 drm_kms_helper syscopyarea sysfillrect sysimgblt fb_sys_fops ttm drm igb ahci libahci ptp libata pps_core dca i2c_algo_bit i2c_core dm_mirror dm_region_hash dm_log dm_mod [last
unloaded: mdev]

[  610.963835] CPU: 15 PID: 19839 Comm: bash Tainted: P           OE
4.8.0-next-20161013+ #0
[  610.973779] Hardware name: Supermicro
SYS-2027GR-5204A-NC024/X9DRG-HF, BIOS 1.0c 02/28/2013
[  610.983769]  ffffc90009323ae0 ffffffff813568bf ffffc90009323b30
0000000000000000
[  610.992867]  ffffc90009323b20 ffffffff81085511 0000001f00001000
ffff8808839ef000
[  611.001954]  ffff88108b30f900 ffff88109ae368e8 ffff88109ae580b0
ffff881099cc0818
[  611.011055] Call Trace:
[  611.015087]  [<ffffffff813568bf>] dump_stack+0x63/0x84
[  611.021784]  [<ffffffff81085511>] __warn+0xd1/0xf0
[  611.028115]  [<ffffffff8108558f>] warn_slowpath_fmt+0x5f/0x80
[  611.035379]  [<ffffffff812a4e80>] ? kernfs_path_from_node+0x50/0x60
[  611.043148]  [<ffffffff812a86c4>] sysfs_warn_dup+0x64/0x80
[  611.050109]  [<ffffffff812a87ae>] sysfs_create_dir_ns+0x7e/0x90
[  611.057481]  [<ffffffff81359891>] kobject_add_internal+0xc1/0x340
[  611.065018]  [<ffffffff81359d45>] kobject_add+0x75/0xd0
[  611.071635]  [<ffffffff81483829>] device_add+0x119/0x610
[  611.078314]  [<ffffffff81483d3a>] device_register+0x1a/0x20
[  611.085261]  [<ffffffffa03c748d>] mdev_device_create+0xdd/0x200 [mdev]
[  611.093143]  [<ffffffffa03c7768>] create_store+0xa8/0xe0 [mdev]
[  611.100385]  [<ffffffffa03c76ab>] mdev_type_attr_store+0x1b/0x30 [mdev]
[  611.108309]  [<ffffffff812a7d8a>] sysfs_kf_write+0x3a/0x50
[  611.115096]  [<ffffffff812a78bb>] kernfs_fop_write+0x10b/0x190
[  611.122231]  [<ffffffff81224e97>] __vfs_write+0x37/0x140
[  611.128817]  [<ffffffff811cea84>] ? handle_mm_fault+0x724/0xd80
[  611.135976]  [<ffffffff81225da2>] vfs_write+0xb2/0x1b0
[  611.142354]  [<ffffffff81003510>] ? syscall_trace_enter+0x1d0/0x2b0
[  611.149836]  [<ffffffff812271f5>] SyS_write+0x55/0xc0
[  611.156065]  [<ffffffff81003a47>] do_syscall_64+0x67/0x180
[  611.162734]  [<ffffffff816d41eb>] entry_SYSCALL64_slow_path+0x25/0x25
[  611.170345] ---[ end trace b05a73599da2ba3f ]---
[  611.175940] ------------[ cut here ]------------
Quoted 4 lines
Quoted 4 lines
drivers/vfio/mdev/mdev_sysfs.c new file +4 -0
+static ssize_t create_store(struct kobject *kobj, struct device *dev,
+			    const char *buf, size_t count)
+{
+	char *str;
Quoted 9 lines

drivers/vfio/mdev/mdev_sysfs.c line 62

Quoted 5 lines
drivers/vfio/mdev/mdev_sysfs.c new file +5 -0
+	uuid_le uuid;
+	int ret;
+
+	if (count < UUID_STRING_LEN)
+		return -EINVAL;  

Can't we also test for something unreasonably large?

Ok. I'll add that check.

Quoted 10 lines

drivers/vfio/mdev/mdev_sysfs.c line 68

Quoted 7 lines
drivers/vfio/mdev/mdev_sysfs.c new file +6 -0
+
+	str = kstrndup(buf, count, GFP_KERNEL);
+	if (!str)
+		return -ENOMEM;
+
+	ret = uuid_le_to_bin(str, &uuid);  

nit, we can kfree(str) here regardless of the return.

Quoted 9 lines

drivers/vfio/mdev/mdev_sysfs.c line 73

Quoted 5 lines
drivers/vfio/mdev/mdev_sysfs.c new file +5 -0
+	if (!ret) {
+
+		ret = mdev_device_create(kobj, dev, uuid);
+		if (ret)
+			pr_err("mdev_create: Failed to create mdev device\n");  

What value does this pr_err add? It doesn't tell us why it failed and the user will already know if failed by the return value of their write.

Ok, will remove it.

Quoted 7 lines
Quoted 7 lines
drivers/vfio/mdev/mdev_sysfs.c new file +7 -0
+		else
+			ret = count;
+	}
+
+	kfree(str);
+	return ret;
+}  

...

Quoted 5 lines
Quoted 5 lines
include/linux/mdev.h new file +5 -0
+static inline struct mdev_driver *to_mdev_driver(struct device_driver *drv)
+{
+	return drv ? container_of(drv, struct mdev_driver, driver) : NULL;
+}
+
Quoted 9 lines

include/linux/mdev.h line 153

Quoted 3 lines
include/linux/mdev.h new file +3 -0
+static inline struct mdev_device *to_mdev_device(struct device *dev)
+{
+	return dev ? container_of(dev, struct mdev_device, dev) : NULL;  

Do we really need this NULL dev/drv behavior? I don't see that any of the callers can pass NULL into these. The PCI equivalents don't support this behavior and it doesn't seem they need to. Thanks,

Ok, I'll update that.

Kirti

#5 lore raw

Re: [PATCH v9 01/12] vfio: Mediated device Core driver

In reply to Kirti Wankhede
Quoted 125 lines · Kirti Wankhede
drivers/vfio/mdev/mdev_core.c new file +117 -0
new file mode 100644
index 000000000000..7db5ec164aeb
@@ -0,0 +1,372 @@
+/*
+ * Mediated device Core Driver
+ *
+ * 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.
+ */
+
+#include <linux/module.h>
+#include <linux/device.h>
+#include <linux/slab.h>
+#include <linux/uuid.h>
+#include <linux/sysfs.h>
+#include <linux/mdev.h>
+
+#include "mdev_private.h"
+
+#define DRIVER_VERSION		"0.1"
+#define DRIVER_AUTHOR		"NVIDIA Corporation"
+#define DRIVER_DESC		"Mediated device Core Driver"
+
+static LIST_HEAD(parent_list);
+static DEFINE_MUTEX(parent_list_lock);
+static struct class_compat *mdev_bus_compat_class;
+
 
+
+/*
+ * 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 = 0;
+	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);
+
+	parent->dev = dev;
+	parent->ops = ops;
+
+	ret = parent_create_sysfs_files(parent);
+	if (ret) {
+		mutex_unlock(&parent_list_lock);
+		mdev_put_parent(parent);
+		return ret;
+	}
+
+	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);
+
+	dev_info(dev, "MDEV: Registered\n");
+	return 0;
+
+add_dev_err:
+	mutex_unlock(&parent_list_lock);
+	put_device(dev);
+	return ret;
+}
+EXPORT_SYMBOL(mdev_register_device);
 
+static int __init mdev_init(void)
+{
+	int ret;
+
+	ret = mdev_bus_register();
+	if (ret) {
+		pr_err("Failed to register mdev bus\n");
+		return ret;
+	}
+
+	mdev_bus_compat_class = class_compat_register("mdev_bus");
+	if (!mdev_bus_compat_class) {
+		mdev_bus_unregister();
+		return -ENOMEM;
+	}
+
+	/*
+	 * 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");
+
+	return ret;

drivers/vfio/mdev/mdev_core.c line 367

Quoted 10 lines
drivers/vfio/mdev/mdev_core.c new file +10 -0
+}
+
+static void __exit mdev_exit(void)
+{
+	class_compat_unregister(mdev_bus_compat_class);
+	mdev_bus_unregister();
+}
+
+module_init(mdev_init)
+module_exit(mdev_exit)

Hi Kirti,

There is a possible issue: mdev_bus_register is called from mdev_init, a module_init, equal to device_initcall if builtin to vmlinux; however, the vendor driver, say i915.ko for intel case, have to call mdev_register_device from its module_init: at that time, mdev_init is still not called.

Not sure if this issue exists with nvidia.ko. Though in most cases we are expecting users select mdev as a standalone module, we still won't break builtin case.

Hi Alex, do you have any suggestion here?

--
Thanks,
Jike

#6 lore raw

Re: [PATCH v9 01/12] vfio: Mediated device Core driver

Quoted 145 lines · On Thu, 20 Oct 2016 15:23:53 +0800 Jike Song <[email protected]> wrote:
Quoted 125 lines · Kirti Wankhede
drivers/vfio/mdev/mdev_core.c new file +117 -0
new file mode 100644
index 000000000000..7db5ec164aeb
@@ -0,0 +1,372 @@
+/*
+ * Mediated device Core Driver
+ *
+ * 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.
+ */
+
+#include <linux/module.h>
+#include <linux/device.h>
+#include <linux/slab.h>
+#include <linux/uuid.h>
+#include <linux/sysfs.h>
+#include <linux/mdev.h>
+
+#include "mdev_private.h"
+
+#define DRIVER_VERSION		"0.1"
+#define DRIVER_AUTHOR		"NVIDIA Corporation"
+#define DRIVER_DESC		"Mediated device Core Driver"
+
+static LIST_HEAD(parent_list);
+static DEFINE_MUTEX(parent_list_lock);
+static struct class_compat *mdev_bus_compat_class;
+  
 
+
+/*
+ * 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 = 0;
+	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);
+
+	parent->dev = dev;
+	parent->ops = ops;
+
+	ret = parent_create_sysfs_files(parent);
+	if (ret) {
+		mutex_unlock(&parent_list_lock);
+		mdev_put_parent(parent);
+		return ret;
+	}
+
+	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);
+
+	dev_info(dev, "MDEV: Registered\n");
+	return 0;
+
+add_dev_err:
+	mutex_unlock(&parent_list_lock);
+	put_device(dev);
+	return ret;
+}
+EXPORT_SYMBOL(mdev_register_device);  
 
+static int __init mdev_init(void)
+{
+	int ret;
+
+	ret = mdev_bus_register();
+	if (ret) {
+		pr_err("Failed to register mdev bus\n");
+		return ret;
+	}
+
+	mdev_bus_compat_class = class_compat_register("mdev_bus");
+	if (!mdev_bus_compat_class) {
+		mdev_bus_unregister();
+		return -ENOMEM;
+	}
+
+	/*
+	 * 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");
+
+	return ret;

drivers/vfio/mdev/mdev_core.c line 367

Quoted 10 lines
drivers/vfio/mdev/mdev_core.c new file +10 -0
+}
+
+static void __exit mdev_exit(void)
+{
+	class_compat_unregister(mdev_bus_compat_class);
+	mdev_bus_unregister();
+}
+
+module_init(mdev_init)
+module_exit(mdev_exit)  

Hi Kirti,

There is a possible issue: mdev_bus_register is called from mdev_init, a module_init, equal to device_initcall if builtin to vmlinux; however, the vendor driver, say i915.ko for intel case, have to call mdev_register_device from its module_init: at that time, mdev_init is still not called.

Quoted 6 lines

Not sure if this issue exists with nvidia.ko. Though in most cases we are expecting users select mdev as a standalone module, we still won't break builtin case.

Hi Alex, do you have any suggestion here?

To fully solve the problem of built-in drivers making use of the mdev infrastructure we'd need to make mdev itself builtin and possibly a subsystem that is initialized prior to device drivers. Is that really necessary? Even though i915.ko is often loaded as part of an initramfs, most systems still build it as a module. I would expect that standard module dependencies will pull in the necessary mdev and vfio modules to make this work correctly. I can't say that I'm prepared to make mdev be a subsystem as would be necessary for builtin drivers to make use of. Perhaps if such a driver exists it could somehow do late binding with mdev. i915 should certainly be tested as a builtin driver to make sure it doesn't fail with mdev support added. The kvm-vfio device (virt/kvm/vfio.c) makes use of symbol tricks to avoid hard dependencies between kvm and vfio, perhaps when builtin to the kernel, i915 could use something like that. Thanks,

Alex

#49 lore raw

Re: [PATCH v9 06/12] vfio_pci: Update vfio_pci to use vfio_info_add_capability()

In reply to Kirti Wankhede
Quoted 122 lines · On Tue, 18 Oct 2016 02:52:06 +0530 Kirti Wankhede <[email protected]> wrote:

Update msix_sparse_mmap_cap() to use vfio_info_add_capability() Update region type capability to use vfio_info_add_capability() Can't split this commit for MSIx and region_type cap since there is a common code which need to be updated for both the cases.

Change-Id I52bb28c7875a6da5a79ddad1843e6088aff58a45
---
 drivers/vfio/pci/vfio_pci.c | 72 +++++++++++++++++----------------------------
 1 file changed, 27 insertions(+), 45 deletions(-)
drivers/vfio/pci/vfio_pci.c +22 -35
index d624a527777f..1ec0565b48ea 100644
@@ -556,12 +556,12 @@ 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_region_info *info,
 				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 +572,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 +591,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);
+	ret = vfio_info_add_capability(info, caps,
+				      VFIO_REGION_INFO_CAP_SPARSE_MMAP, sparse);
+	kfree(sparse);
 
-	cap = container_of(header, struct vfio_region_info_cap_type, header);
-	cap->type = type;
-	cap->subtype = subtype;
-
-	return 0;
+	return ret;
 }
 
 int vfio_pci_register_dev_region(struct vfio_pci_device *vdev,
@@ -704,7 +686,8 @@ static long vfio_pci_ioctl(void *device_data,
 			if (vdev->bar_mmap_supported[info.index]) {
 				info.flags |= VFIO_REGION_INFO_FLAG_MMAP;
 				if (info.index == vdev->msix_bar) {
-					ret = msix_sparse_mmap_cap(vdev, &caps);
+					ret = msix_sparse_mmap_cap(vdev, &info,
+								   &caps);
 					if (ret)
 						return ret;
 				}
@@ -752,6 +735,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,27 +748,23 @@ 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(&info, &caps,
+						      VFIO_REGION_INFO_CAP_TYPE,
+						      &cap_type);
 			if (ret)
 				return ret;
+
+		}
 		}
 
-		if (caps.size) {
-			info.flags |= VFIO_REGION_INFO_FLAG_CAPS;
-			if (info.argsz < sizeof(info) + caps.size) {
-				info.argsz = sizeof(info) + caps.size;

drivers/vfio/pci/vfio_pci.c static long vfio_pci_ioctl(void *device_data, · line 785

Quoted 10 lines
drivers/vfio/pci/vfio_pci.c +0 -10
-				info.cap_offset = 0;
-			} else {
-				vfio_info_cap_shift(&caps, sizeof(info));
-				if (copy_to_user((void __user *)arg +
-						  sizeof(info), caps.buf,
-						  caps.size)) {
-					kfree(caps.buf);
-					return -EFAULT;
-				}
-				info.cap_offset = sizeof(info);

I prefer the case above, I'm fine with breaking out helpers to build a buffer containing the capability chain, but I would rather have the caller manage placing that back into the return structure. That also allows the helper to be independent of the structure we're operating on, it could be a region_info, irq_info, device_info, etc. It only needs to know the layout of the capability type we're trying to add, not the info structure itself. Thanks,

Alex

Quoted 8 lines
drivers/vfio/pci/vfio_pci.c +5 -0
+		if (info.cap_offset) {
+			if (copy_to_user((void __user *)arg + info.cap_offset,
+					 caps.buf, caps.size)) {
+				kfree(caps.buf);
+				return -EFAULT;
 			}
 
 			kfree(caps.buf);
#45 lore raw

Re: [PATCH v9 05/12] vfio: Introduce common function to add capabilities

In reply to Kirti Wankhede
Quoted 82 lines · On Tue, 18 Oct 2016 02:52:05 +0530 Kirti Wankhede <[email protected]> wrote:

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.

Change-Id I6fca329fa2291f37a2c859d0bc97574d9e2ce1a6
---
 drivers/vfio/vfio.c  | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/vfio.h |  4 +++
 2 files changed, 82 insertions(+)
drivers/vfio/vfio.c +62 -0
index a5a210005b65..e96cb3f7a23c 100644
@@ -1799,6 +1799,84 @@ void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset)
 }
 EXPORT_SYMBOL_GPL(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_region_info *info,
+			     struct vfio_info_cap *caps,
+			     int cap_type_id,
+			     void *cap_type)
+{
+	int ret;
+
+	if (!cap_type)
+		return 0;
+
+	switch (cap_type_id) {
+	case VFIO_REGION_INFO_CAP_SPARSE_MMAP:
+		ret = sparse_mmap_cap(caps, cap_type);
+		if (ret)
+			return ret;
+		break;
+
+	case VFIO_REGION_INFO_CAP_TYPE:
+		ret = region_type_cap(caps, cap_type);
+		if (ret)
+			return ret;
+		break;
+	default:
+		return -EINVAL;
+	}

drivers/vfio/vfio.c void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset) · line 1873

Quoted 10 lines
drivers/vfio/vfio.c +10 -0
+
+	info->flags |= VFIO_REGION_INFO_FLAG_CAPS;
+
+	if (caps->size) {
+		if (info->argsz < sizeof(*info) + caps->size) {
+			info->argsz = sizeof(*info) + caps->size;
+			info->cap_offset = 0;
+		} else {
+			vfio_info_cap_shift(caps, sizeof(*info));
+			info->cap_offset = sizeof(*info);

This doesn't work. We build the capability chain in a buffer and vfio_info_cap_add() expects the chain to be zero-based as each capability is added. vfio_info_cap_shift() is meant to be called once on that buffer immediately before copying it back to the user buffer to adjust the chain offsets to account for the offset within the buffer. vfio_info_cap_shift() cannot be called repeatedly on the buffer as we do support multiple capabilities in a chain.

Quoted 24 lines
drivers/vfio/vfio.c +6 -0
+		}
+	}
+	return 0;
+}
+EXPORT_SYMBOL(vfio_info_add_capability);
+
 
 /*
  * Pin a set of guest PFNs and return their associated host PFNs for

local diff --git a/include/linux/vfio.h b/include/linux/vfio.h index 0bd25ba6223d..854a4b40be02 100644

include/linux/vfio.h +0 -0
@@ -108,6 +108,10 @@ 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);

include/linux/vfio.h +4 -0
+extern int vfio_info_add_capability(struct vfio_region_info *info,
+				    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);
#56 lore raw

Re: [PATCH v9 10/12] vfio: Add function to get device_api string from vfio_device_info.flags

In reply to Kirti Wankhede
Quoted 43 lines · On Tue, 18 Oct 2016 02:52:10 +0530 Kirti Wankhede <[email protected]> wrote:

Function vfio_device_api_string() returns string based on flag set in vfio_device_info's flag. This should be used by vendor driver to get string based on flag for device_api attribute.

Change-Id I42d29f475f02a7132ce13297fbf2b48f1da10995
---
 drivers/vfio/vfio.c  | 15 +++++++++++++++
 include/linux/vfio.h |  2 ++
 2 files changed, 17 insertions(+)
drivers/vfio/vfio.c +15 -0
index 10ef1c5fa762..aec470454a13 100644
@@ -1917,6 +1917,21 @@ int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr, int num_irqs,
 }
 EXPORT_SYMBOL(vfio_set_irqs_validate_and_prepare);
 
+const char *vfio_device_api_string(u32 flags)
+{
+	if (flags & VFIO_DEVICE_FLAGS_PCI)
+		return "vfio-pci";
+
+	if (flags & VFIO_DEVICE_FLAGS_PLATFORM)
+		return "vfio-platform";
+
+	if (flags & VFIO_DEVICE_FLAGS_AMBA)
+		return "vfio-amba";
+
+	return "";
+}
+EXPORT_SYMBOL(vfio_device_api_string);
+
 /*
  * Pin a set of guest PFNs and return their associated host PFNs for local
  * domain only.
include/linux/vfio.h +0 -0
index 31d059f1649b..fca2bf23c4f1 100644

include/linux/vfio.h extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr, · line 123

Quoted 8 lines
include/linux/vfio.h +2 -0
@@ -116,6 +116,8 @@ extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr,
 					      int num_irqs, int max_irq_type,
 					      size_t *data_size);
 
+extern const char *vfio_device_api_string(u32 flags);
+
 struct pci_dev;
 #ifdef CONFIG_EEH
 extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);

Couldn't this simply be a #define in the uapi header?

#define VFIO_DEVICE_PCI_API_STRING "vfio-pci"

I don't really see why we need a lookup function.

author maintainer
#18 lore raw

Re: [PATCH v9 04/12] vfio iommu: Add support for mediated devices

In reply to Alex Williamson

Alex,

Addressing your comments other than invalidation part.

Quoted 3 lines · Alex Williamson

On Tue, 18 Oct 2016 02:52:04 +0530
Kirti Wankhede <[email protected]> wrote:

...

Quoted 9 lines
Quoted 8 lines

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 3 lines

Were you able to do these with the locked memory limit of the user set to the minimum required for existing GPU assignment?

No, is there a way to set memory limit through livbirt so that it would set memory limit to system memory assigned to VM?

Quoted 1 line
Quoted 1 line

...

Quoted 9 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	container = group->container;
+	if (IS_ERR(container)) {

I don't see that we ever use an ERR_PTR to set group->container, it should either be NULL or valid and the fact that we added ourselves to container_users should mean that it's valid. The paranoia test here would be if container is NULL, but IS_ERR() doesn't check NULL. If we need that paranoia test, maybe we should just:

Quoted 4 lines

if (WARN_ON(!container)) {

I'm not fully convinced it's needed though.

Ok removing this check.

Quoted 14 lines

drivers/vfio/vfio.c void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset) · line 1842

Quoted 10 lines
drivers/vfio/vfio.c +10 -0
+		ret = PTR_ERR(container);
+		goto err_pin_pages;
+	}
+
+	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);

The caller is going to need to provide some means for us to callback to invalidate pinned pages.

Quoted 6 lines

ret has already been used, so it's zero at this point. I expect the original intention was to let the initialization above fall through here so that the caller gets an errno if the driver doesn't support pin_pages. Returning zero without actually doing anything seems like an unexpected return value.

yes, changing it to:

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;
Quoted 14 lines
Quoted 14 lines
drivers/vfio/vfio_iommu_type1.c +14 -0
+static int vfio_pfn_account(struct vfio_iommu *iommu, unsigned long pfn)
+{
+	struct vfio_pfn *p;
+	struct vfio_domain *domain = iommu->local_domain;
+	int ret = 1;
+
+	if (!domain)
+		return 1;
+
+	mutex_lock(&domain->local_addr_space->pfn_list_lock);
+
+	p = vfio_find_pfn(domain, pfn);
+	if (p)
+		ret = 0;
Quoted 9 lines

drivers/vfio/vfio_iommu_type1.c static void vfio_unlink_dma(struct vfio_iommu *iommu, struct vfio_dma *old) · line 250

Quoted 4 lines
drivers/vfio/vfio_iommu_type1.c +4 -0
+
+	mutex_unlock(&domain->local_addr_space->pfn_list_lock);
+	return ret;
+}

So if the vfio_pfn for a given pfn exists, return 0, else return 1. But do we know that the vfio_pfn exists at the point where we actually do that accounting?

Only below functions call vfio_pfn_account()
__vfio_pin_pages_remote() -> vfio_pfn_account()
__vfio_unpin_pages_remote() -> vfio_pfn_account()

Consider the case when mdev device is already assigned to VM, run some app in VM that pins some pages, then hotplug pass through device. Then __vfio_pin_pages_remote() is called when iommu capable domain is attached to container to pin all pages from vfio_iommu_replay(). So if at this time vfio_pfn exist means that the page is pinned through local_domain when iommu capable domain was not present, so accounting was already done for that pages. Hence returned 0 here which mean don't add this page in accounting.

Quoted 27 lines
Quoted 27 lines
drivers/vfio/vfio_iommu_type1.c +6 -5
+
 struct vwork {
 	struct mm_struct	*mm;
 	long			npage;
@@ -150,17 +269,17 @@ 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 task_struct *task, long npage)
 {
 	struct vwork *vwork;
 	struct mm_struct *mm;
 
-	if (!current->mm || !npage)
+	if (!task->mm || !npage)
 		return; /* process exited or nothing to do */
 
-	if (down_write_trylock(&current->mm->mmap_sem)) {
-		current->mm->locked_vm += npage;
-		up_write(&current->mm->mmap_sem);
+	if (down_write_trylock(&task->mm->mmap_sem)) {
+		task->mm->locked_vm += npage;
+		up_write(&task->mm->mmap_sem);
 		return;
 	}
 
Quoted 17 lines

drivers/vfio/vfio_iommu_type1.c static int put_pfn(unsigned long pfn, int prot) · line 348

Quoted 12 lines
drivers/vfio/vfio_iommu_type1.c +1 -1
@@ -172,7 +291,7 @@ static void vfio_lock_acct(long npage)
 	vwork = kmalloc(sizeof(struct vwork), GFP_KERNEL);
 	if (!vwork)
 		return;
-	mm = get_task_mm(current);
+	mm = get_task_mm(task);
 	if (!mm) {
 		kfree(vwork);
 		return;
@@ -228,20 +347,31 @@ static int put_pfn(unsigned long pfn, int prot)
 	return 0;
 }

This coversion of vfio_lock_acct() to pass a task_struct and updating existing callers to pass current would be a great separate, easily review-able patch.

Ok. I'll split this in separate commit.

Quoted 37 lines
Quoted 37 lines
drivers/vfio/vfio_iommu_type1.c +15 -4
-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;
+	struct mm_struct *local_mm = (mm ? mm : current->mm);
 	int ret = -EFAULT;
 
-	if (get_user_pages_fast(vaddr, 1, !!(prot & IOMMU_WRITE), page) == 1) {
+	if (mm) {
+		down_read(&local_mm->mmap_sem);
+		ret = get_user_pages_remote(NULL, local_mm, vaddr, 1,
+					!!(prot & IOMMU_WRITE), 0, page, NULL);
+		up_read(&local_mm->mmap_sem);
+	} else
+		ret = get_user_pages_fast(vaddr, 1,
+					  !!(prot & IOMMU_WRITE), page);
+
+	if (ret == 1) {
 		*pfn = page_to_pfn(page[0]);
 		return 0;
 	}
 
-	down_read(&current->mm->mmap_sem);
+	down_read(&local_mm->mmap_sem);
 
-	vma = find_vma_intersection(current->mm, vaddr, vaddr + 1);
+	vma = find_vma_intersection(local_mm, vaddr, vaddr + 1);
 
 	if (vma && vma->vm_flags & VM_PFNMAP) {
 		*pfn = ((vaddr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
@@ -249,7 +379,7 @@ static int vaddr_get_pfn(unsigned long vaddr, int prot, unsigned long *pfn)
 			ret = 0;
 	}
 
Quoted 7 lines

drivers/vfio/vfio_iommu_type1.c static int vaddr_get_pfn(unsigned long vaddr, int prot, unsigned long *pfn) · line 385

Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +1 -1
-	up_read(&current->mm->mmap_sem);
+	up_read(&local_mm->mmap_sem);
 
 	return ret;
 }

This would also be a great separate patch.

Ok.

Quoted 7 lines

Have you considered
renaming the mm_struct function arg to "remote_mm" and making the local variable simply "mm"? It seems like it would tie nicely with the remote_mm path using get_user_pages_remote() while passing NULL for remote_mm uses current->mm and the existing path (and avoid the general oddness of passing local_mm to a "remote" function).

Yes, your suggestion looks good. Updating.

Quoted 9 lines
Quoted 9 lines
drivers/vfio/vfio_iommu_type1.c +3 -2
@@ -259,33 +389,37 @@ 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(struct vfio_iommu *iommu,
+				    unsigned long vaddr, long npage,
+				    int prot, unsigned long *pfn_base)

...

Quoted 11 lines

drivers/vfio/vfio_iommu_type1.c static long vfio_pin_pages(unsigned long vaddr, long npage, · line 441

Quoted 6 lines
drivers/vfio/vfio_iommu_type1.c +2 -0
@@ -303,8 +437,10 @@ static long vfio_pin_pages(unsigned long vaddr, long npage,
 			break;
 		}
 
+		lock_acct += vfio_pfn_account(iommu, pfn);
+

I take it that this is the new technique for keeping the accounting accurate, we only increment the locked accounting by the amount not already pinned in a vfio_pfn.

That's correct.

Quoted 16 lines
Quoted 16 lines
drivers/vfio/vfio_iommu_type1.c +2 -2
 		if (!rsvd && !lock_cap &&
-		    current->mm->locked_vm + i + 1 > limit) {
+		    current->mm->locked_vm + lock_acct > limit) {
 			put_pfn(pfn, prot);
 			pr_warn("%s: RLIMIT_MEMLOCK (%ld) exceeded\n",
 				__func__, limit << PAGE_SHIFT);
@@ -313,23 +449,216 @@ static long vfio_pin_pages(unsigned long vaddr, long npage,
 	}
 
 	if (!rsvd)
-		vfio_lock_acct(i);
+		vfio_lock_acct(current, lock_acct);
 
 	return i;
 }
 
Quoted 14 lines

drivers/vfio/vfio_iommu_type1.c static long vfio_pin_pages(unsigned long vaddr, long npage, · line 459

Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +3 -2
-static long vfio_unpin_pages(unsigned long pfn, long npage,
-			     int prot, bool do_accounting)
+static long __vfio_unpin_pages_remote(struct vfio_iommu *iommu,
+				      unsigned long pfn, long npage, int prot,
+				      bool do_accounting)

Have you noticed that it's kind of confusing that __vfio_{un}pin_pages_remote() uses current, which does a get_user_pages_fast() while "local" uses a provided task_struct and uses get_user_pages_*remote*()? And also what was effectively local (ie. we're pinning for our own use here) is now "remote" and pinning for a remote, vendor driver consumer, is now "local". It's not very intuitive.

'local' in local_domain was suggested to describe the domain for local page tracking. Earlier suggestions to have 'mdev' or 'noimmu' in this name were discarded. May be we should revisit what the name should be. Any suggestion?

For local_domain, to pin pages, flow is:

for local_domain

    |- vfio_pin_pages()
        |- vfio_iommu_type1_pin_pages()
            |- __vfio_pin_page_local()
                |-  vaddr_get_pfn(task->mm)
                    |- get_user_pages_remote()

__vfio_pin_page_local() --> get_user_pages_remote()

Quoted 12 lines
Quoted 12 lines
drivers/vfio/vfio_iommu_type1.c +1 -1
 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;
@@ -746,10 +1136,14 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,
 	mutex_lock(&iommu->lock);
 
Quoted 12 lines

drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_attach_group(void *iommu_data, · line 1143

Quoted 9 lines
drivers/vfio/vfio_iommu_type1.c +5 -3
 	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;
+		}
+	}

The find_iommu_group() conversion would also be an easy separate patch.

Ok.

Quoted 25 lines
Quoted 25 lines
drivers/vfio/vfio_iommu_type1.c +17 -0
+	if (iommu->local_domain) {
+		if (find_iommu_group(iommu->local_domain, iommu_group)) {
 			mutex_unlock(&iommu->lock);
 			return -EINVAL;
 		}
@@ -769,6 +1163,30 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,
 	if (ret)
 		goto out_free;
 
+	if (IS_ENABLED(CONFIG_VFIO_MDEV) && !iommu_present(bus) &&
+	    (bus == &mdev_bus_type)) {
+		if (!iommu->local_domain) {
+			domain->local_addr_space =
+				kzalloc(sizeof(*domain->local_addr_space),
+						GFP_KERNEL);
+			if (!domain->local_addr_space) {
+				ret = -ENOMEM;
+				goto out_free;
+			}
+
+			domain->local_addr_space->task = current;
+			INIT_LIST_HEAD(&domain->group_list);
+			domain->local_addr_space->pfn_list = RB_ROOT;
+			mutex_init(&domain->local_addr_space->pfn_list_lock);
Quoted 9 lines

drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_attach_group(void *iommu_data, · line 1185

Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +5 -0
+			iommu->local_domain = domain;
+		} else
+			kfree(domain);
+
+		list_add(&group->next, &domain->group_list);

I think you mean s/domain/iommu->local_domain/ here, we just freed domain in the else path.

Yes, corrected.

Quoted 34 lines
Quoted 23 lines
drivers/vfio/vfio_iommu_type1.c +16 -0
+		mutex_unlock(&iommu->lock);
+		return 0;
+	}
+
 	domain->domain = iommu_domain_alloc(bus);
 	if (!domain->domain) {
 		ret = -EIO;
@@ -859,6 +1277,41 @@ 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_domain *domain = iommu->local_domain;
+	struct vfio_dma *dma, *tdma;
+	struct rb_node *n;
+	long locked = 0;
+
+	rbtree_postorder_for_each_entry_safe(dma, tdma, &iommu->dma_list,
+					     node) {
+		vfio_unmap_unpin(iommu, dma);
+	}
+

drivers/vfio/vfio_iommu_type1.c static void vfio_iommu_unmap_unpin_all(struct vfio_iommu *iommu) · line 1301

Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10 -0
+	mutex_lock(&domain->local_addr_space->pfn_list_lock);
+
+	n = rb_first(&domain->local_addr_space->pfn_list);
+
+	for (; n; n = rb_next(n))
+		locked++;
+
+	vfio_lock_acct(domain->local_addr_space->task, locked);
+	mutex_unlock(&domain->local_addr_space->pfn_list_lock);
+}
Quoted 8 lines

Couldn't a properly timed mlock by the user allow them to lock more memory than they're allowed here? For instance imagine the vendor driver has pinned the entire VM memory and the user has exactly the locked memory limit for that VM. During the gap here between unpinning the entire vfio_dma list and re-accounting for the pfn_list, the user can mlock up to their limit again an now they've doubled the locked memory they're allowed.

As per original code, vfio_unmap_unpin() calls __vfio_unpin_pages_remote(.., false) with do_accounting set to false, why is that so?

Here if accounting is set to true then we don't have to do re-accounting here.

Quoted 47 lines
Quoted 47 lines
drivers/vfio/vfio_iommu_type1.c +21 -14
+
+static void vfio_local_unpin_all(struct vfio_domain *domain)
+{
+	struct rb_node *node;
+
+	mutex_lock(&domain->local_addr_space->pfn_list_lock);
+	while ((node = rb_first(&domain->local_addr_space->pfn_list)))
+		vfio_unpin_pfn(domain,
+				rb_entry(node, struct vfio_pfn, node), false);
+
+	mutex_unlock(&domain->local_addr_space->pfn_list_lock);
+}
+
 static void vfio_iommu_type1_detach_group(void *iommu_data,
 					  struct iommu_group *iommu_group)
 {
@@ -868,31 +1321,57 @@ 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;
-
-			iommu_detach_group(domain->domain, iommu_group);
+	if (iommu->local_domain) {
+		domain = iommu->local_domain;
+		group = find_iommu_group(domain, iommu_group);
+		if (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_local_unpin_all(domain);
+				if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER(iommu))
 					vfio_iommu_unmap_unpin_all(iommu);
-				iommu_domain_free(domain->domain);
-				list_del(&domain->next);
 				kfree(domain);
+				iommu->local_domain = NULL;
Quoted 8 lines

Review static void vfio_iommu_type1_detach_group(void *iommu_data, · line 1337

Quoted 1 line
(quoted diff) +1 -0
+			}

I can't quite wrap my head around this, if we have mdev groups attached and this iommu group matches an mdev group, remove from list and free the group. If there are now no more groups in the mdev group list, then for each vfio_pfn, unpin the pfn, /without/ doing accounting udpates

corrected the code to do accounting here.

Quoted 2 lines

and remove the vfio_pfn, but only if the ref_count is now zero.

Yes, If you see the loop vfio_local_unpin_all(), it iterates till the node in rb tree exist

Quoted 4 lines

drivers/vfio/vfio_iommu_type1.c static void vfio_iommu_unmap_unpin_all(struct vfio_iommu *iommu) · line 1311

Quoted 4 lines
drivers/vfio/vfio_iommu_type1.c +4 -0
+	while ((node = rb_first(&domain->local_addr_space->pfn_list)))
+		vfio_unpin_pfn(domain,
+				rb_entry(node, struct vfio_pfn, node), false);
+

and vfio_unpin_pfn() only remove the node from rb tree if ref count is zero.

static int vfio_unpin_pfn(struct vfio_domain *domain,
struct vfio_pfn *vpfn, bool do_accounting)

{
        __vfio_unpin_page_local(domain, vpfn->pfn, vpfn->prot,
                                do_accounting);

        if (atomic_dec_and_test(&vpfn->ref_count))
                vfio_remove_from_pfn_list(domain, vpfn);

        return 1;
}

so for example for a vfio_pfn ref_count is 2, first iteration would be:
- call __vfio_unpin_page_local()
- atomic_dec(ref_count), so now ref_count is 1, but node is not removed
from rb tree.

In next iteration:
- call __vfio_unpin_page_local()
- atomic_dec(ref_count), so now ref_count is 0, remove node from rb tree.

Quoted 6 lines

We free the domain, so if the ref_count was non-zero we've now
just leaked memory. I think that means that if a vendor driver pins a given page twice, that leak occurs. Furthermore, if there is not an iommu capable domain in the container, we remove all the vfio_dma entries as well, ok. Maybe the only issue is those leaked vfio_pfns.

So if vendor driver pins a page twice, vfio_unpin_pfn() would get called twice and only when ref count is zero that node is removed from rb tree. So there is no memory leak.

Kirti

author maintainer
#57 lore raw

Re: [PATCH v9 10/12] vfio: Add function to get device_api string from vfio_device_info.flags

In reply to Alex Williamson
Quoted 57 lines · Alex Williamson
Quoted 43 lines · On Tue, 18 Oct 2016 02:52:10 +0530 Kirti Wankhede <[email protected]> wrote:

Function vfio_device_api_string() returns string based on flag set in vfio_device_info's flag. This should be used by vendor driver to get string based on flag for device_api attribute.

Change-Id I42d29f475f02a7132ce13297fbf2b48f1da10995
---
 drivers/vfio/vfio.c  | 15 +++++++++++++++
 include/linux/vfio.h |  2 ++
 2 files changed, 17 insertions(+)
drivers/vfio/vfio.c +15 -0
index 10ef1c5fa762..aec470454a13 100644
@@ -1917,6 +1917,21 @@ int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr, int num_irqs,
 }
 EXPORT_SYMBOL(vfio_set_irqs_validate_and_prepare);
 
+const char *vfio_device_api_string(u32 flags)
+{
+	if (flags & VFIO_DEVICE_FLAGS_PCI)
+		return "vfio-pci";
+
+	if (flags & VFIO_DEVICE_FLAGS_PLATFORM)
+		return "vfio-platform";
+
+	if (flags & VFIO_DEVICE_FLAGS_AMBA)
+		return "vfio-amba";
+
+	return "";
+}
+EXPORT_SYMBOL(vfio_device_api_string);
+
 /*
  * Pin a set of guest PFNs and return their associated host PFNs for local
  * domain only.
include/linux/vfio.h +0 -0
index 31d059f1649b..fca2bf23c4f1 100644

include/linux/vfio.h extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr, · line 123

Quoted 8 lines
include/linux/vfio.h +2 -0
@@ -116,6 +116,8 @@ extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr,
 					      int num_irqs, int max_irq_type,
 					      size_t *data_size);
 
+extern const char *vfio_device_api_string(u32 flags);
+
 struct pci_dev;
 #ifdef CONFIG_EEH
 extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);

Couldn't this simply be a #define in the uapi header?

Quoted 4 lines

#define VFIO_DEVICE_PCI_API_STRING "vfio-pci"

I don't really see why we need a lookup function.

String is tightly coupled with the FLAG, right?
Instead user need to take care of making sure to return proper string, and don't mis-match the string, I think having function is easier. Vendor driver should decide the type of device they want to expose and set the flag, using this function vendor driver would return string which is based on flag they set.

Kirti

#58 lore raw

Re: [PATCH v9 10/12] vfio: Add function to get device_api string from vfio_device_info.flags

Quoted 58 lines · On Fri, 21 Oct 2016 01:59:55 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 57 lines · Alex Williamson
Quoted 43 lines · On Tue, 18 Oct 2016 02:52:10 +0530 Kirti Wankhede <[email protected]> wrote:

Function vfio_device_api_string() returns string based on flag set in vfio_device_info's flag. This should be used by vendor driver to get string based on flag for device_api attribute.

Change-Id I42d29f475f02a7132ce13297fbf2b48f1da10995
---
 drivers/vfio/vfio.c  | 15 +++++++++++++++
 include/linux/vfio.h |  2 ++
 2 files changed, 17 insertions(+)
drivers/vfio/vfio.c +15 -0
index 10ef1c5fa762..aec470454a13 100644
@@ -1917,6 +1917,21 @@ int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr, int num_irqs,
 }
 EXPORT_SYMBOL(vfio_set_irqs_validate_and_prepare);
 
+const char *vfio_device_api_string(u32 flags)
+{
+	if (flags & VFIO_DEVICE_FLAGS_PCI)
+		return "vfio-pci";
+
+	if (flags & VFIO_DEVICE_FLAGS_PLATFORM)
+		return "vfio-platform";
+
+	if (flags & VFIO_DEVICE_FLAGS_AMBA)
+		return "vfio-amba";
+
+	return "";
+}
+EXPORT_SYMBOL(vfio_device_api_string);
+
 /*
  * Pin a set of guest PFNs and return their associated host PFNs for local
  * domain only.
include/linux/vfio.h +0 -0
index 31d059f1649b..fca2bf23c4f1 100644

include/linux/vfio.h extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr, · line 123

Quoted 8 lines
include/linux/vfio.h +2 -0
@@ -116,6 +116,8 @@ extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr,
 					      int num_irqs, int max_irq_type,
 					      size_t *data_size);
 
+extern const char *vfio_device_api_string(u32 flags);
+
 struct pci_dev;
 #ifdef CONFIG_EEH
 extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);  

Couldn't this simply be a #define in the uapi header?

Quoted 8 lines
Quoted 4 lines

#define VFIO_DEVICE_PCI_API_STRING "vfio-pci"

I don't really see why we need a lookup function.

String is tightly coupled with the FLAG, right?
Instead user need to take care of making sure to return proper string, and don't mis-match the string, I think having function is easier.

That's exactly why I proposed putting the #define string in the uapi, by that I mean the vfio uapi header. That keeps the tight coupling to the flag, they're both defined in the same place, plus it gives userspace a reference so they're not just inventing a string to compare against. IOW, the vendor driver simply does an sprintf of VFIO_DEVICE_PCI_API_STRING and userspace (ie. libvirt) can do a strcmp with VFIO_DEVICE_PCI_API_STRING from the same header and everybody arrives at the same result.

Quoted 3 lines

Vendor driver should decide the type of device they want to expose and set the flag, using this function vendor driver would return string which is based on flag they set.

Being a function adds no intrinsic value and being in a uapi header does add value to userspace. Thanks,

Alex

author maintainer
#59 lore raw

Re: [PATCH v9 10/12] vfio: Add function to get device_api string from vfio_device_info.flags

Quoted 79 lines · Alex Williamson
Quoted 58 lines · On Fri, 21 Oct 2016 01:59:55 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 57 lines · Alex Williamson
Quoted 43 lines · On Tue, 18 Oct 2016 02:52:10 +0530 Kirti Wankhede <[email protected]> wrote:

Function vfio_device_api_string() returns string based on flag set in vfio_device_info's flag. This should be used by vendor driver to get string based on flag for device_api attribute.

Change-Id I42d29f475f02a7132ce13297fbf2b48f1da10995
---
 drivers/vfio/vfio.c  | 15 +++++++++++++++
 include/linux/vfio.h |  2 ++
 2 files changed, 17 insertions(+)
drivers/vfio/vfio.c +15 -0
index 10ef1c5fa762..aec470454a13 100644
@@ -1917,6 +1917,21 @@ int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr, int num_irqs,
 }
 EXPORT_SYMBOL(vfio_set_irqs_validate_and_prepare);
 
+const char *vfio_device_api_string(u32 flags)
+{
+	if (flags & VFIO_DEVICE_FLAGS_PCI)
+		return "vfio-pci";
+
+	if (flags & VFIO_DEVICE_FLAGS_PLATFORM)
+		return "vfio-platform";
+
+	if (flags & VFIO_DEVICE_FLAGS_AMBA)
+		return "vfio-amba";
+
+	return "";
+}
+EXPORT_SYMBOL(vfio_device_api_string);
+
 /*
  * Pin a set of guest PFNs and return their associated host PFNs for local
  * domain only.
include/linux/vfio.h +0 -0
index 31d059f1649b..fca2bf23c4f1 100644

include/linux/vfio.h extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr, · line 123

Quoted 8 lines
include/linux/vfio.h +2 -0
@@ -116,6 +116,8 @@ extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr,
 					      int num_irqs, int max_irq_type,
 					      size_t *data_size);
 
+extern const char *vfio_device_api_string(u32 flags);
+
 struct pci_dev;
 #ifdef CONFIG_EEH
 extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);  

Couldn't this simply be a #define in the uapi header?

Quoted 8 lines
Quoted 4 lines

#define VFIO_DEVICE_PCI_API_STRING "vfio-pci"

I don't really see why we need a lookup function.

String is tightly coupled with the FLAG, right?
Instead user need to take care of making sure to return proper string, and don't mis-match the string, I think having function is easier.

That's exactly why I proposed putting the #define string in the uapi, by that I mean the vfio uapi header. That keeps the tight coupling to the flag, they're both defined in the same place, plus it gives userspace a reference so they're not just inventing a string to compare against. IOW, the vendor driver simply does an sprintf of VFIO_DEVICE_PCI_API_STRING and userspace (ie. libvirt) can do a strcmp with VFIO_DEVICE_PCI_API_STRING from the same header and everybody arrives at the same result.

Quoted 7 lines
Quoted 3 lines

Vendor driver should decide the type of device they want to expose and set the flag, using this function vendor driver would return string which is based on flag they set.

Being a function adds no intrinsic value and being in a uapi header does add value to userspace. Thanks,

Ok. The strings should be in uapi, but having function (like below) to return proper string based on flag would be good to have for vendor driver.

+const char *vfio_device_api_string(u32 flags)
+{
+ if (flags & VFIO_DEVICE_FLAGS_PCI)
+ return VFIO_DEVICE_API_PCI_STRING;
+
+ if (flags & VFIO_DEVICE_FLAGS_PLATFORM)
+ return VFIO_DEVICE_API_PLATFORM_STRING;
+
+ if (flags & VFIO_DEVICE_FLAGS_AMBA)
+ return VFIO_DEVICE_API_AMBA_STRING;
+
+ return "";
+}
+EXPORT_SYMBOL(vfio_device_api_string);

Kirti

#60 lore raw

Re: [PATCH v9 10/12] vfio: Add function to get device_api string from vfio_device_info.flags

Quoted 91 lines · On Fri, 21 Oct 2016 02:44:37 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 79 lines · Alex Williamson
Quoted 58 lines · On Fri, 21 Oct 2016 01:59:55 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 57 lines · Alex Williamson
Quoted 43 lines · On Tue, 18 Oct 2016 02:52:10 +0530 Kirti Wankhede <[email protected]> wrote:

Function vfio_device_api_string() returns string based on flag set in vfio_device_info's flag. This should be used by vendor driver to get string based on flag for device_api attribute.

Change-Id I42d29f475f02a7132ce13297fbf2b48f1da10995
---
 drivers/vfio/vfio.c  | 15 +++++++++++++++
 include/linux/vfio.h |  2 ++
 2 files changed, 17 insertions(+)
drivers/vfio/vfio.c +15 -0
index 10ef1c5fa762..aec470454a13 100644
@@ -1917,6 +1917,21 @@ int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr, int num_irqs,
 }
 EXPORT_SYMBOL(vfio_set_irqs_validate_and_prepare);
 
+const char *vfio_device_api_string(u32 flags)
+{
+	if (flags & VFIO_DEVICE_FLAGS_PCI)
+		return "vfio-pci";
+
+	if (flags & VFIO_DEVICE_FLAGS_PLATFORM)
+		return "vfio-platform";
+
+	if (flags & VFIO_DEVICE_FLAGS_AMBA)
+		return "vfio-amba";
+
+	return "";
+}
+EXPORT_SYMBOL(vfio_device_api_string);
+
 /*
  * Pin a set of guest PFNs and return their associated host PFNs for local
  * domain only.
include/linux/vfio.h +0 -0
index 31d059f1649b..fca2bf23c4f1 100644

include/linux/vfio.h extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr, · line 123

Quoted 8 lines
include/linux/vfio.h +2 -0
@@ -116,6 +116,8 @@ extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr,
 					      int num_irqs, int max_irq_type,
 					      size_t *data_size);
 
+extern const char *vfio_device_api_string(u32 flags);
+
 struct pci_dev;
 #ifdef CONFIG_EEH
 extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);    

Couldn't this simply be a #define in the uapi header?

Quoted 8 lines
Quoted 4 lines

#define VFIO_DEVICE_PCI_API_STRING "vfio-pci"

I don't really see why we need a lookup function.

String is tightly coupled with the FLAG, right?
Instead user need to take care of making sure to return proper string, and don't mis-match the string, I think having function is easier.

That's exactly why I proposed putting the #define string in the uapi, by that I mean the vfio uapi header. That keeps the tight coupling to the flag, they're both defined in the same place, plus it gives userspace a reference so they're not just inventing a string to compare against. IOW, the vendor driver simply does an sprintf of VFIO_DEVICE_PCI_API_STRING and userspace (ie. libvirt) can do a strcmp with VFIO_DEVICE_PCI_API_STRING from the same header and everybody arrives at the same result.

Quoted 7 lines
Quoted 3 lines

Vendor driver should decide the type of device they want to expose and set the flag, using this function vendor driver would return string which is based on flag they set.

Being a function adds no intrinsic value and being in a uapi header does add value to userspace. Thanks,

Ok. The strings should be in uapi, but having function (like below) to return proper string based on flag would be good to have for vendor driver.

Quoted 14 lines

+const char *vfio_device_api_string(u32 flags)
+{
+ if (flags & VFIO_DEVICE_FLAGS_PCI)
+ return VFIO_DEVICE_API_PCI_STRING;
+
+ if (flags & VFIO_DEVICE_FLAGS_PLATFORM)
+ return VFIO_DEVICE_API_PLATFORM_STRING;
+
+ if (flags & VFIO_DEVICE_FLAGS_AMBA)
+ return VFIO_DEVICE_API_AMBA_STRING;
+
+ return "";
+}
+EXPORT_SYMBOL(vfio_device_api_string);

I disagree, it's pointless maintenance overhead. It's yet another function that we need to care about for kABI and it offers almost no value. Thanks,

Alex

#7 lore raw

Re: [PATCH v9 01/12] vfio: Mediated device Core driver

In reply to Alex Williamson
Quoted 155 lines · Alex Williamson
Quoted 145 lines · On Thu, 20 Oct 2016 15:23:53 +0800 Jike Song <[email protected]> wrote:
Quoted 125 lines · Kirti Wankhede
drivers/vfio/mdev/mdev_core.c new file +117 -0
new file mode 100644
index 000000000000..7db5ec164aeb
@@ -0,0 +1,372 @@
+/*
+ * Mediated device Core Driver
+ *
+ * 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.
+ */
+
+#include <linux/module.h>
+#include <linux/device.h>
+#include <linux/slab.h>
+#include <linux/uuid.h>
+#include <linux/sysfs.h>
+#include <linux/mdev.h>
+
+#include "mdev_private.h"
+
+#define DRIVER_VERSION		"0.1"
+#define DRIVER_AUTHOR		"NVIDIA Corporation"
+#define DRIVER_DESC		"Mediated device Core Driver"
+
+static LIST_HEAD(parent_list);
+static DEFINE_MUTEX(parent_list_lock);
+static struct class_compat *mdev_bus_compat_class;
+  
 
+
+/*
+ * 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 = 0;
+	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);
+
+	parent->dev = dev;
+	parent->ops = ops;
+
+	ret = parent_create_sysfs_files(parent);
+	if (ret) {
+		mutex_unlock(&parent_list_lock);
+		mdev_put_parent(parent);
+		return ret;
+	}
+
+	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);
+
+	dev_info(dev, "MDEV: Registered\n");
+	return 0;
+
+add_dev_err:
+	mutex_unlock(&parent_list_lock);
+	put_device(dev);
+	return ret;
+}
+EXPORT_SYMBOL(mdev_register_device);  
 
+static int __init mdev_init(void)
+{
+	int ret;
+
+	ret = mdev_bus_register();
+	if (ret) {
+		pr_err("Failed to register mdev bus\n");
+		return ret;
+	}
+
+	mdev_bus_compat_class = class_compat_register("mdev_bus");
+	if (!mdev_bus_compat_class) {
+		mdev_bus_unregister();
+		return -ENOMEM;
+	}
+
+	/*
+	 * 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");
+
+	return ret;

drivers/vfio/mdev/mdev_core.c line 367

Quoted 10 lines
drivers/vfio/mdev/mdev_core.c new file +10 -0
+}
+
+static void __exit mdev_exit(void)
+{
+	class_compat_unregister(mdev_bus_compat_class);
+	mdev_bus_unregister();
+}
+
+module_init(mdev_init)
+module_exit(mdev_exit)  

Hi Kirti,

There is a possible issue: mdev_bus_register is called from mdev_init, a module_init, equal to device_initcall if builtin to vmlinux; however, the vendor driver, say i915.ko for intel case, have to call mdev_register_device from its module_init: at that time, mdev_init is still not called.

Quoted 6 lines

Not sure if this issue exists with nvidia.ko. Though in most cases we are expecting users select mdev as a standalone module, we still won't break builtin case.

Hi Alex, do you have any suggestion here?

Quoted 14 lines

To fully solve the problem of built-in drivers making use of the mdev infrastructure we'd need to make mdev itself builtin and possibly a subsystem that is initialized prior to device drivers. Is that really necessary? Even though i915.ko is often loaded as part of an initramfs, most systems still build it as a module. I would expect that standard module dependencies will pull in the necessary mdev and vfio modules to make this work correctly. I can't say that I'm prepared to make mdev be a subsystem as would be necessary for builtin drivers to make use of. Perhaps if such a driver exists it could somehow do late binding with mdev. i915 should certainly be tested as a builtin driver to make sure it doesn't fail with mdev support added. The kvm-vfio device (virt/kvm/vfio.c) makes use of symbol tricks to avoid hard dependencies between kvm and vfio, perhaps when builtin to the kernel, i915 could use something like that. Thanks,

Fair enough, I'll use symbol_get to avoid the problem. Thanks!

--
Thanks,
Jike

author maintainer
#61 lore raw

Re: [PATCH v9 10/12] vfio: Add function to get device_api string from vfio_device_info.flags

In reply to Alex Williamson
Quoted 3 lines · Alex Williamson

On Fri, 21 Oct 2016 02:44:37 +0530
Kirti Wankhede <[email protected]> wrote:

...

Quoted 53 lines
Quoted 38 lines
Quoted 27 lines
Quoted 9 lines
Quoted 9 lines

include/linux/vfio.h extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr, · line 123

Quoted 6 lines
include/linux/vfio.h +2 -0
+extern const char *vfio_device_api_string(u32 flags);
+
 struct pci_dev;
 #ifdef CONFIG_EEH
 extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);    

Couldn't this simply be a #define in the uapi header?

Quoted 8 lines
Quoted 4 lines

#define VFIO_DEVICE_PCI_API_STRING "vfio-pci"

I don't really see why we need a lookup function.

String is tightly coupled with the FLAG, right?
Instead user need to take care of making sure to return proper string, and don't mis-match the string, I think having function is easier.

That's exactly why I proposed putting the #define string in the uapi, by that I mean the vfio uapi header. That keeps the tight coupling to the flag, they're both defined in the same place, plus it gives userspace a reference so they're not just inventing a string to compare against. IOW, the vendor driver simply does an sprintf of VFIO_DEVICE_PCI_API_STRING and userspace (ie. libvirt) can do a strcmp with VFIO_DEVICE_PCI_API_STRING from the same header and everybody arrives at the same result.

Quoted 7 lines
Quoted 3 lines

Vendor driver should decide the type of device they want to expose and set the flag, using this function vendor driver would return string which is based on flag they set.

Being a function adds no intrinsic value and being in a uapi header does add value to userspace. Thanks,

Ok. The strings should be in uapi, but having function (like below) to return proper string based on flag would be good to have for vendor driver.

Quoted 14 lines

+const char *vfio_device_api_string(u32 flags)
+{
+ if (flags & VFIO_DEVICE_FLAGS_PCI)
+ return VFIO_DEVICE_API_PCI_STRING;
+
+ if (flags & VFIO_DEVICE_FLAGS_PLATFORM)
+ return VFIO_DEVICE_API_PLATFORM_STRING;
+
+ if (flags & VFIO_DEVICE_FLAGS_AMBA)
+ return VFIO_DEVICE_API_AMBA_STRING;
+
+ return "";
+}
+EXPORT_SYMBOL(vfio_device_api_string);

Quoted 4 lines

I disagree, it's pointless maintenance overhead. It's yet another function that we need to care about for kABI and it offers almost no value. Thanks,

If any vendor driver sets VFIO_DEVICE_FLAGS_PLATFORM flag but sets VFIO_DEVICE_API_PCI_STRING, we don't have a way to verify this in kernel driver. Is that acceptable?

Kirti

#62 lore raw

Re: [PATCH v9 10/12] vfio: Add function to get device_api string from vfio_device_info.flags

Quoted 59 lines · On Fri, 21 Oct 2016 08:30:53 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 3 lines · Alex Williamson

On Fri, 21 Oct 2016 02:44:37 +0530
Kirti Wankhede <[email protected]> wrote:

...

Quoted 53 lines
Quoted 52 lines
Quoted 27 lines
Quoted 9 lines
Quoted 9 lines

Review

Quoted 6 lines
(quoted diff) +2 -0
+extern const char *vfio_device_api_string(u32 flags);
+
 struct pci_dev;
 #ifdef CONFIG_EEH
 extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);      

Couldn't this simply be a #define in the uapi header?

Quoted 8 lines
Quoted 4 lines

#define VFIO_DEVICE_PCI_API_STRING "vfio-pci"

I don't really see why we need a lookup function.

String is tightly coupled with the FLAG, right?
Instead user need to take care of making sure to return proper string, and don't mis-match the string, I think having function is easier.

That's exactly why I proposed putting the #define string in the uapi, by that I mean the vfio uapi header. That keeps the tight coupling to the flag, they're both defined in the same place, plus it gives userspace a reference so they're not just inventing a string to compare against. IOW, the vendor driver simply does an sprintf of VFIO_DEVICE_PCI_API_STRING and userspace (ie. libvirt) can do a strcmp with VFIO_DEVICE_PCI_API_STRING from the same header and everybody arrives at the same result.

Quoted 7 lines
Quoted 3 lines

Vendor driver should decide the type of device they want to expose and set the flag, using this function vendor driver would return string which is based on flag they set.

Being a function adds no intrinsic value and being in a uapi header does add value to userspace. Thanks,

Ok. The strings should be in uapi, but having function (like below) to return proper string based on flag would be good to have for vendor driver.

+const char *vfio_device_api_string(u32 flags)
+{
+ if (flags & VFIO_DEVICE_FLAGS_PCI)
+ return VFIO_DEVICE_API_PCI_STRING;
+
+ if (flags & VFIO_DEVICE_FLAGS_PLATFORM)
+ return VFIO_DEVICE_API_PLATFORM_STRING;
+
+ if (flags & VFIO_DEVICE_FLAGS_AMBA)
+ return VFIO_DEVICE_API_AMBA_STRING;
+
+ return "";
+}
+EXPORT_SYMBOL(vfio_device_api_string);

Quoted 8 lines
Quoted 4 lines

I disagree, it's pointless maintenance overhead. It's yet another function that we need to care about for kABI and it offers almost no value. Thanks,

If any vendor driver sets VFIO_DEVICE_FLAGS_PLATFORM flag but sets VFIO_DEVICE_API_PCI_STRING, we don't have a way to verify this in kernel driver. Is that acceptable?

a) The function doesn't solve that problem, as seen in the mtty sample driver there's no guarantee that the vendor driver passes device_info.flags vs the #define for the flag itself. So we're already expecting them to get it right in two separate places.

b) It's not going to take them very long to figure this out if they care about userspace tools that make use of this field to determine the device API. This is a very obvious and simple bug.

c) We can check and correct how open source vendor drivers work.

Thanks,
Alex

#26 lore raw

Re: [PATCH v9 04/12] vfio iommu: Add support for mediated devices

In reply to Kirti Wankhede
Quoted 4 lines · Kirti Wankhede
drivers/vfio/vfio_iommu_type1.c +0 -0
index 2ba19424e4a1..5d67058a611d 100644

[snip]

Quoted 28 lines

static int vfio_iommu_type1_attach_group(void *iommu_data,

drivers/vfio/vfio_iommu_type1.c +8 -4
 					 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;
@@ -746,10 +1136,14 @@ 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;
+		}
+	}
 
+	if (iommu->local_domain) {
+		if (find_iommu_group(iommu->local_domain, iommu_group)) {
 			mutex_unlock(&iommu->lock);
 			return -EINVAL;
 		}

drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_attach_group(void *iommu_data, · line 1167

Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +2 -0
@@ -769,6 +1163,30 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,
 	if (ret)
 		goto out_free;
 
+	if (IS_ENABLED(CONFIG_VFIO_MDEV) && !iommu_present(bus) &&
+	    (bus == &mdev_bus_type)) {

Hi Kirti,

By refering mdev_bus_type directly you are making vfio_iommu_type1.ko depends on mdev.ko, but in Kconfig doesn't guarantee the dependency. For example, if CONFIG_VFIO_IOMMU_TYPE1=y and CONFIG_VFIO_MDEV=m, the building will fail.

--
Thanks,
Jike

#27 lore raw

Re: [PATCH v9 04/12] vfio iommu: Add support for mediated devices

Quoted 37 lines · On Fri, 21 Oct 2016 15:49:07 +0800 Jike Song <[email protected]> wrote:
Quoted 4 lines · Kirti Wankhede
drivers/vfio/vfio_iommu_type1.c +0 -0
index 2ba19424e4a1..5d67058a611d 100644

[snip]

Quoted 31 lines

static int vfio_iommu_type1_attach_group(void *iommu_data,

drivers/vfio/vfio_iommu_type1.c +8 -4
 					 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;
@@ -746,10 +1136,14 @@ 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;
+		}
+	}
 
+	if (iommu->local_domain) {
+		if (find_iommu_group(iommu->local_domain, iommu_group)) {
 			mutex_unlock(&iommu->lock);
 			return -EINVAL;
 		}
@@ -769,6 +1163,30 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,
 	if (ret)
 		goto out_free;
 
Quoted 8 lines

drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_attach_group(void *iommu_data, · line 1167

Quoted 2 lines
drivers/vfio/vfio_iommu_type1.c +2 -0
+	if (IS_ENABLED(CONFIG_VFIO_MDEV) && !iommu_present(bus) &&
+	    (bus == &mdev_bus_type)) {  

Hi Kirti,

By refering mdev_bus_type directly you are making vfio_iommu_type1.ko depends on mdev.ko, but in Kconfig doesn't guarantee the dependency. For example, if CONFIG_VFIO_IOMMU_TYPE1=y and CONFIG_VFIO_MDEV=m, the building will fail.

Good point, Jike. I don't think we want to make existing vfio modules dependent on mdev modules. I wonder if we can lookup the mdev_bus_type symbol w/o triggering the module load. Thanks,

Alex

#19 lore raw

Re: [PATCH v9 04/12] vfio iommu: Add support for mediated devices

In reply to Kirti Wankhede
Quoted 18 lines · On Fri, 21 Oct 2016 01:47:25 +0530 Kirti Wankhede <[email protected]> wrote:

Alex,

Addressing your comments other than invalidation part.

Quoted 3 lines · Alex Williamson

On Tue, 18 Oct 2016 02:52:04 +0530
Kirti Wankhede <[email protected]> wrote:

...

Quoted 9 lines
Quoted 8 lines

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 3 lines

Were you able to do these with the locked memory limit of the user set to the minimum required for existing GPU assignment?

No, is there a way to set memory limit through livbirt so that it would set memory limit to system memory assigned to VM?

Not that I know of, but I also don't know how you're making use of an mdev device through libvirt yet since they don't have support for the vfio-pci sysfsdev option. I would recommend testing with QEMU manually.

Quoted 78 lines

...

Quoted 9 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	container = group->container;
+	if (IS_ERR(container)) {  

I don't see that we ever use an ERR_PTR to set group->container, it should either be NULL or valid and the fact that we added ourselves to container_users should mean that it's valid. The paranoia test here would be if container is NULL, but IS_ERR() doesn't check NULL. If we need that paranoia test, maybe we should just:

Quoted 4 lines

if (WARN_ON(!container)) {

I'm not fully convinced it's needed though.

Ok removing this check.

Quoted 14 lines

drivers/vfio/vfio.c void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset) · line 1842

Quoted 10 lines
drivers/vfio/vfio.c +10 -0
+		ret = PTR_ERR(container);
+		goto err_pin_pages;
+	}
+
+	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);  

The caller is going to need to provide some means for us to callback to invalidate pinned pages.

Quoted 6 lines

ret has already been used, so it's zero at this point. I expect the original intention was to let the initialization above fall through here so that the caller gets an errno if the driver doesn't support pin_pages. Returning zero without actually doing anything seems like an unexpected return value.

yes, changing it to:

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;
Quoted 14 lines
Quoted 14 lines
drivers/vfio/vfio_iommu_type1.c +14 -0
+static int vfio_pfn_account(struct vfio_iommu *iommu, unsigned long pfn)
+{
+	struct vfio_pfn *p;
+	struct vfio_domain *domain = iommu->local_domain;
+	int ret = 1;
+
+	if (!domain)
+		return 1;
+
+	mutex_lock(&domain->local_addr_space->pfn_list_lock);
+
+	p = vfio_find_pfn(domain, pfn);
+	if (p)
+		ret = 0;
Quoted 9 lines

drivers/vfio/vfio_iommu_type1.c static void vfio_unlink_dma(struct vfio_iommu *iommu, struct vfio_dma *old) · line 250

Quoted 4 lines
drivers/vfio/vfio_iommu_type1.c +4 -0
+
+	mutex_unlock(&domain->local_addr_space->pfn_list_lock);
+	return ret;
+}  

So if the vfio_pfn for a given pfn exists, return 0, else return 1. But do we know that the vfio_pfn exists at the point where we actually do that accounting?

Only below functions call vfio_pfn_account()
__vfio_pin_pages_remote() -> vfio_pfn_account()
__vfio_unpin_pages_remote() -> vfio_pfn_account()

Quoted 8 lines

Consider the case when mdev device is already assigned to VM, run some app in VM that pins some pages, then hotplug pass through device. Then __vfio_pin_pages_remote() is called when iommu capable domain is attached to container to pin all pages from vfio_iommu_replay(). So if at this time vfio_pfn exist means that the page is pinned through local_domain when iommu capable domain was not present, so accounting was already done for that pages. Hence returned 0 here which mean don't add this page in accounting.

Right, I see that's the intention, I can't pick any holes in the concept, but I'll continue to try to look for bugs.

Quoted 172 lines
Quoted 27 lines
Quoted 27 lines
drivers/vfio/vfio_iommu_type1.c +6 -5
+
 struct vwork {
 	struct mm_struct	*mm;
 	long			npage;
@@ -150,17 +269,17 @@ 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 task_struct *task, long npage)
 {
 	struct vwork *vwork;
 	struct mm_struct *mm;
 
-	if (!current->mm || !npage)
+	if (!task->mm || !npage)
 		return; /* process exited or nothing to do */
 
-	if (down_write_trylock(&current->mm->mmap_sem)) {
-		current->mm->locked_vm += npage;
-		up_write(&current->mm->mmap_sem);
+	if (down_write_trylock(&task->mm->mmap_sem)) {
+		task->mm->locked_vm += npage;
+		up_write(&task->mm->mmap_sem);
 		return;
 	}
 
Quoted 17 lines

drivers/vfio/vfio_iommu_type1.c static int put_pfn(unsigned long pfn, int prot) · line 348

Quoted 12 lines
drivers/vfio/vfio_iommu_type1.c +1 -1
@@ -172,7 +291,7 @@ static void vfio_lock_acct(long npage)
 	vwork = kmalloc(sizeof(struct vwork), GFP_KERNEL);
 	if (!vwork)
 		return;
-	mm = get_task_mm(current);
+	mm = get_task_mm(task);
 	if (!mm) {
 		kfree(vwork);
 		return;
@@ -228,20 +347,31 @@ static int put_pfn(unsigned long pfn, int prot)
 	return 0;
 }  

This coversion of vfio_lock_acct() to pass a task_struct and updating existing callers to pass current would be a great separate, easily review-able patch.

Ok. I'll split this in separate commit.

Quoted 37 lines
Quoted 37 lines
drivers/vfio/vfio_iommu_type1.c +15 -4
-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;
+	struct mm_struct *local_mm = (mm ? mm : current->mm);
 	int ret = -EFAULT;
 
-	if (get_user_pages_fast(vaddr, 1, !!(prot & IOMMU_WRITE), page) == 1) {
+	if (mm) {
+		down_read(&local_mm->mmap_sem);
+		ret = get_user_pages_remote(NULL, local_mm, vaddr, 1,
+					!!(prot & IOMMU_WRITE), 0, page, NULL);
+		up_read(&local_mm->mmap_sem);
+	} else
+		ret = get_user_pages_fast(vaddr, 1,
+					  !!(prot & IOMMU_WRITE), page);
+
+	if (ret == 1) {
 		*pfn = page_to_pfn(page[0]);
 		return 0;
 	}
 
-	down_read(&current->mm->mmap_sem);
+	down_read(&local_mm->mmap_sem);
 
-	vma = find_vma_intersection(current->mm, vaddr, vaddr + 1);
+	vma = find_vma_intersection(local_mm, vaddr, vaddr + 1);
 
 	if (vma && vma->vm_flags & VM_PFNMAP) {
 		*pfn = ((vaddr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
@@ -249,7 +379,7 @@ static int vaddr_get_pfn(unsigned long vaddr, int prot, unsigned long *pfn)
 			ret = 0;
 	}
 
Quoted 7 lines

drivers/vfio/vfio_iommu_type1.c static int vaddr_get_pfn(unsigned long vaddr, int prot, unsigned long *pfn) · line 385

Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +1 -1
-	up_read(&current->mm->mmap_sem);
+	up_read(&local_mm->mmap_sem);
 
 	return ret;
 }  

This would also be a great separate patch.

Ok.

Quoted 7 lines

Have you considered
renaming the mm_struct function arg to "remote_mm" and making the local variable simply "mm"? It seems like it would tie nicely with the remote_mm path using get_user_pages_remote() while passing NULL for remote_mm uses current->mm and the existing path (and avoid the general oddness of passing local_mm to a "remote" function).

Yes, your suggestion looks good. Updating.

Quoted 9 lines
Quoted 9 lines
drivers/vfio/vfio_iommu_type1.c +3 -2
@@ -259,33 +389,37 @@ 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(struct vfio_iommu *iommu,
+				    unsigned long vaddr, long npage,
+				    int prot, unsigned long *pfn_base)  

...

Quoted 11 lines

drivers/vfio/vfio_iommu_type1.c static long vfio_pin_pages(unsigned long vaddr, long npage, · line 441

Quoted 6 lines
drivers/vfio/vfio_iommu_type1.c +2 -0
@@ -303,8 +437,10 @@ static long vfio_pin_pages(unsigned long vaddr, long npage,
 			break;
 		}
 
+		lock_acct += vfio_pfn_account(iommu, pfn);
+  

I take it that this is the new technique for keeping the accounting accurate, we only increment the locked accounting by the amount not already pinned in a vfio_pfn.

That's correct.

Quoted 16 lines
Quoted 16 lines
drivers/vfio/vfio_iommu_type1.c +2 -2
 		if (!rsvd && !lock_cap &&
-		    current->mm->locked_vm + i + 1 > limit) {
+		    current->mm->locked_vm + lock_acct > limit) {
 			put_pfn(pfn, prot);
 			pr_warn("%s: RLIMIT_MEMLOCK (%ld) exceeded\n",
 				__func__, limit << PAGE_SHIFT);
@@ -313,23 +449,216 @@ static long vfio_pin_pages(unsigned long vaddr, long npage,
 	}
 
 	if (!rsvd)
-		vfio_lock_acct(i);
+		vfio_lock_acct(current, lock_acct);
 
 	return i;
 }
 
Quoted 14 lines

drivers/vfio/vfio_iommu_type1.c static long vfio_pin_pages(unsigned long vaddr, long npage, · line 459

Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +3 -2
-static long vfio_unpin_pages(unsigned long pfn, long npage,
-			     int prot, bool do_accounting)
+static long __vfio_unpin_pages_remote(struct vfio_iommu *iommu,
+				      unsigned long pfn, long npage, int prot,
+				      bool do_accounting)  

Have you noticed that it's kind of confusing that __vfio_{un}pin_pages_remote() uses current, which does a get_user_pages_fast() while "local" uses a provided task_struct and uses get_user_pages_*remote*()? And also what was effectively local (ie. we're pinning for our own use here) is now "remote" and pinning for a remote, vendor driver consumer, is now "local". It's not very intuitive.

'local' in local_domain was suggested to describe the domain for local page tracking. Earlier suggestions to have 'mdev' or 'noimmu' in this name were discarded. May be we should revisit what the name should be. Any suggestion?

For local_domain, to pin pages, flow is:

Quoted 8 lines

for local_domain

    |- vfio_pin_pages()
        |- vfio_iommu_type1_pin_pages()
            |- __vfio_pin_page_local()
                |-  vaddr_get_pfn(task->mm)
                    |- get_user_pages_remote()

__vfio_pin_page_local() --> get_user_pages_remote()

In vfio.c we have the concept of an external user, perhaps that could be continued here. An mdev driver would be an external, or remote pinning.

Quoted 107 lines
Quoted 12 lines
Quoted 12 lines
drivers/vfio/vfio_iommu_type1.c +1 -1
 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;
@@ -746,10 +1136,14 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,
 	mutex_lock(&iommu->lock);
 
Quoted 12 lines

drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_attach_group(void *iommu_data, · line 1143

Quoted 9 lines
drivers/vfio/vfio_iommu_type1.c +5 -3
 	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;
+		}
+	}  

The find_iommu_group() conversion would also be an easy separate patch.

Ok.

Quoted 25 lines
Quoted 25 lines
drivers/vfio/vfio_iommu_type1.c +17 -0
+	if (iommu->local_domain) {
+		if (find_iommu_group(iommu->local_domain, iommu_group)) {
 			mutex_unlock(&iommu->lock);
 			return -EINVAL;
 		}
@@ -769,6 +1163,30 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,
 	if (ret)
 		goto out_free;
 
+	if (IS_ENABLED(CONFIG_VFIO_MDEV) && !iommu_present(bus) &&
+	    (bus == &mdev_bus_type)) {
+		if (!iommu->local_domain) {
+			domain->local_addr_space =
+				kzalloc(sizeof(*domain->local_addr_space),
+						GFP_KERNEL);
+			if (!domain->local_addr_space) {
+				ret = -ENOMEM;
+				goto out_free;
+			}
+
+			domain->local_addr_space->task = current;
+			INIT_LIST_HEAD(&domain->group_list);
+			domain->local_addr_space->pfn_list = RB_ROOT;
+			mutex_init(&domain->local_addr_space->pfn_list_lock);
Quoted 9 lines

drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_attach_group(void *iommu_data, · line 1185

Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +5 -0
+			iommu->local_domain = domain;
+		} else
+			kfree(domain);
+
+		list_add(&group->next, &domain->group_list);  

I think you mean s/domain/iommu->local_domain/ here, we just freed domain in the else path.

Yes, corrected.

Quoted 34 lines
Quoted 23 lines
drivers/vfio/vfio_iommu_type1.c +16 -0
+		mutex_unlock(&iommu->lock);
+		return 0;
+	}
+
 	domain->domain = iommu_domain_alloc(bus);
 	if (!domain->domain) {
 		ret = -EIO;
@@ -859,6 +1277,41 @@ 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_domain *domain = iommu->local_domain;
+	struct vfio_dma *dma, *tdma;
+	struct rb_node *n;
+	long locked = 0;
+
+	rbtree_postorder_for_each_entry_safe(dma, tdma, &iommu->dma_list,
+					     node) {
+		vfio_unmap_unpin(iommu, dma);
+	}
+

drivers/vfio/vfio_iommu_type1.c static void vfio_iommu_unmap_unpin_all(struct vfio_iommu *iommu) · line 1301

Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10 -0
+	mutex_lock(&domain->local_addr_space->pfn_list_lock);
+
+	n = rb_first(&domain->local_addr_space->pfn_list);
+
+	for (; n; n = rb_next(n))
+		locked++;
+
+	vfio_lock_acct(domain->local_addr_space->task, locked);
+	mutex_unlock(&domain->local_addr_space->pfn_list_lock);
+}  
Quoted 8 lines

Couldn't a properly timed mlock by the user allow them to lock more memory than they're allowed here? For instance imagine the vendor driver has pinned the entire VM memory and the user has exactly the locked memory limit for that VM. During the gap here between unpinning the entire vfio_dma list and re-accounting for the pfn_list, the user can mlock up to their limit again an now they've doubled the locked memory they're allowed.

Quoted 3 lines

As per original code, vfio_unmap_unpin() calls __vfio_unpin_pages_remote(.., false) with do_accounting set to false, why is that so?

Because vfio_dma tracks the user granularity of calling MAP_DMA, not the granularity with which the iommu mapping was actually done. There might be multiple non-contiguous chunks to make that mapping and we don't know how the iommu chose to map a given chunk to support large page sizes. If we chose to do accounting on the iommu_unmap() granularity, we might account for every 4k page separately. We choose not to do accounting there so that we can batch the accounting into one update per range.

Quoted 2 lines

Here if accounting is set to true then we don't have to do re-accounting here.

If vfio_unmap_unpin() did not do accounting, you could update accounting once with the difference between what was pinned and what remains pinned via the mdev and avoid the gap caused by de-accounting everything and then re-accounting only for the mdev pinnings.

Quoted 85 lines
Quoted 47 lines
Quoted 47 lines
drivers/vfio/vfio_iommu_type1.c +21 -14
+
+static void vfio_local_unpin_all(struct vfio_domain *domain)
+{
+	struct rb_node *node;
+
+	mutex_lock(&domain->local_addr_space->pfn_list_lock);
+	while ((node = rb_first(&domain->local_addr_space->pfn_list)))
+		vfio_unpin_pfn(domain,
+				rb_entry(node, struct vfio_pfn, node), false);
+
+	mutex_unlock(&domain->local_addr_space->pfn_list_lock);
+}
+
 static void vfio_iommu_type1_detach_group(void *iommu_data,
 					  struct iommu_group *iommu_group)
 {
@@ -868,31 +1321,57 @@ 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;
-
-			iommu_detach_group(domain->domain, iommu_group);
+	if (iommu->local_domain) {
+		domain = iommu->local_domain;
+		group = find_iommu_group(domain, iommu_group);
+		if (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_local_unpin_all(domain);
+				if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER(iommu))
 					vfio_iommu_unmap_unpin_all(iommu);
-				iommu_domain_free(domain->domain);
-				list_del(&domain->next);
 				kfree(domain);
+				iommu->local_domain = NULL;
Quoted 8 lines

Review static void vfio_iommu_type1_detach_group(void *iommu_data, · line 1337

Quoted 1 line
(quoted diff) +1 -0
+			}  

I can't quite wrap my head around this, if we have mdev groups attached and this iommu group matches an mdev group, remove from list and free the group. If there are now no more groups in the mdev group list, then for each vfio_pfn, unpin the pfn, /without/ doing accounting udpates

corrected the code to do accounting here.

Quoted 2 lines

and remove the vfio_pfn, but only if the ref_count is now zero.

Yes, If you see the loop vfio_local_unpin_all(), it iterates till the node in rb tree exist

Quoted 4 lines

drivers/vfio/vfio_iommu_type1.c static void vfio_iommu_unmap_unpin_all(struct vfio_iommu *iommu) · line 1311

Quoted 4 lines
drivers/vfio/vfio_iommu_type1.c +4 -0
+	while ((node = rb_first(&domain->local_addr_space->pfn_list)))
+		vfio_unpin_pfn(domain,
+				rb_entry(node, struct vfio_pfn, node), false);
+  

and vfio_unpin_pfn() only remove the node from rb tree if ref count is zero.

static int vfio_unpin_pfn(struct vfio_domain *domain,
struct vfio_pfn *vpfn, bool do_accounting)

{
        __vfio_unpin_page_local(domain, vpfn->pfn, vpfn->prot,
                                do_accounting);

        if (atomic_dec_and_test(&vpfn->ref_count))
                vfio_remove_from_pfn_list(domain, vpfn);

        return 1;
}
Quoted 8 lines

so for example for a vfio_pfn ref_count is 2, first iteration would be:
- call __vfio_unpin_page_local()
- atomic_dec(ref_count), so now ref_count is 1, but node is not removed
from rb tree.

In next iteration:
- call __vfio_unpin_page_local()
- atomic_dec(ref_count), so now ref_count is 0, remove node from rb tree.

Ok, I missed that, thanks.

Quoted 10 lines
Quoted 6 lines

We free the domain, so if the ref_count was non-zero we've now
just leaked memory. I think that means that if a vendor driver pins a given page twice, that leak occurs. Furthermore, if there is not an iommu capable domain in the container, we remove all the vfio_dma entries as well, ok. Maybe the only issue is those leaked vfio_pfns.

So if vendor driver pins a page twice, vfio_unpin_pfn() would get called twice and only when ref count is zero that node is removed from rb tree. So there is no memory leak.

Ok

#71 lore raw
In reply to Kirti Wankhede
Quoted 9 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 v9?

Hi Alex, Kirti and Neo

Just want to share that we have published a KVMGT implementation based on this v9 patchset, to:

https://github.com/01org/gvt-linux/tree/gvt-next-kvmgt

It doesn't utilize common routines introduced by 05+ patches yet. The complete intel vGPU device-model is contained.

--
Thanks,
Jike

author maintainer
#28 lore raw

Re: [PATCH v9 04/12] vfio iommu: Add support for mediated devices

In reply to Alex Williamson
Quoted 45 lines · Alex Williamson
Quoted 42 lines · On Fri, 21 Oct 2016 15:49:07 +0800 Jike Song <[email protected]> wrote:
Quoted 4 lines · Kirti Wankhede
drivers/vfio/vfio_iommu_type1.c +0 -0
index 2ba19424e4a1..5d67058a611d 100644

[snip]

Quoted 28 lines

static int vfio_iommu_type1_attach_group(void *iommu_data,

drivers/vfio/vfio_iommu_type1.c +8 -4
 					 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;
@@ -746,10 +1136,14 @@ 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;
+		}
+	}
 
+	if (iommu->local_domain) {
+		if (find_iommu_group(iommu->local_domain, iommu_group)) {
 			mutex_unlock(&iommu->lock);
 			return -EINVAL;
 		}

drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_attach_group(void *iommu_data, · line 1167

Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +2 -0
@@ -769,6 +1163,30 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,
 	if (ret)
 		goto out_free;
 
+	if (IS_ENABLED(CONFIG_VFIO_MDEV) && !iommu_present(bus) &&
+	    (bus == &mdev_bus_type)) {  

Hi Kirti,

Quoted 8 lines
Quoted 3 lines

By refering mdev_bus_type directly you are making vfio_iommu_type1.ko depends on mdev.ko, but in Kconfig doesn't guarantee the dependency. For example, if CONFIG_VFIO_IOMMU_TYPE1=y and CONFIG_VFIO_MDEV=m, the building will fail.

Good point, Jike. I don't think we want to make existing vfio modules dependent on mdev modules. I wonder if we can lookup the mdev_bus_type symbol w/o triggering the module load. Thanks,

Ok. Modifying the check as below works in above case:

        mdev_bus = symbol_get(mdev_bus_type);

        if (mdev_bus && (bus == mdev_bus) && !iommu_present(bus) ) {
                symbol_put(mdev_bus_type);
                ...
        }

Kirti

author maintainer
#50 lore raw

Re: [PATCH v9 06/12] vfio_pci: Update vfio_pci to use vfio_info_add_capability()

In reply to Alex Williamson
Quoted 136 lines · Alex Williamson
Quoted 122 lines · On Tue, 18 Oct 2016 02:52:06 +0530 Kirti Wankhede <[email protected]> wrote:

Update msix_sparse_mmap_cap() to use vfio_info_add_capability() Update region type capability to use vfio_info_add_capability() Can't split this commit for MSIx and region_type cap since there is a common code which need to be updated for both the cases.

Change-Id I52bb28c7875a6da5a79ddad1843e6088aff58a45
---
 drivers/vfio/pci/vfio_pci.c | 72 +++++++++++++++++----------------------------
 1 file changed, 27 insertions(+), 45 deletions(-)
drivers/vfio/pci/vfio_pci.c +22 -35
index d624a527777f..1ec0565b48ea 100644
@@ -556,12 +556,12 @@ 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_region_info *info,
 				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 +572,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 +591,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);
+	ret = vfio_info_add_capability(info, caps,
+				      VFIO_REGION_INFO_CAP_SPARSE_MMAP, sparse);
+	kfree(sparse);
 
-	cap = container_of(header, struct vfio_region_info_cap_type, header);
-	cap->type = type;
-	cap->subtype = subtype;
-
-	return 0;
+	return ret;
 }
 
 int vfio_pci_register_dev_region(struct vfio_pci_device *vdev,
@@ -704,7 +686,8 @@ static long vfio_pci_ioctl(void *device_data,
 			if (vdev->bar_mmap_supported[info.index]) {
 				info.flags |= VFIO_REGION_INFO_FLAG_MMAP;
 				if (info.index == vdev->msix_bar) {
-					ret = msix_sparse_mmap_cap(vdev, &caps);
+					ret = msix_sparse_mmap_cap(vdev, &info,
+								   &caps);
 					if (ret)
 						return ret;
 				}
@@ -752,6 +735,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,27 +748,23 @@ 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(&info, &caps,
+						      VFIO_REGION_INFO_CAP_TYPE,
+						      &cap_type);
 			if (ret)
 				return ret;
+
+		}
 		}
 
-		if (caps.size) {
-			info.flags |= VFIO_REGION_INFO_FLAG_CAPS;
-			if (info.argsz < sizeof(info) + caps.size) {
-				info.argsz = sizeof(info) + caps.size;

drivers/vfio/pci/vfio_pci.c static long vfio_pci_ioctl(void *device_data, · line 785

Quoted 10 lines
drivers/vfio/pci/vfio_pci.c +0 -10
-				info.cap_offset = 0;
-			} else {
-				vfio_info_cap_shift(&caps, sizeof(info));
-				if (copy_to_user((void __user *)arg +
-						  sizeof(info), caps.buf,
-						  caps.size)) {
-					kfree(caps.buf);
-					return -EFAULT;
-				}
-				info.cap_offset = sizeof(info);
Quoted 8 lines

I prefer the case above, I'm fine with breaking out helpers to build a buffer containing the capability chain, but I would rather have the caller manage placing that back into the return structure. That also allows the helper to be independent of the structure we're operating on, it could be a region_info, irq_info, device_info, etc. It only needs to know the layout of the capability type we're trying to add, not the info structure itself. Thanks,

The capability feature is for region_info (VFIO_REGION_INFO_FLAG_CAPS), then structure we are operating couldn't be irq_info or device_info, right?

Kirti.

author maintainer
#46 lore raw

Re: [PATCH v9 05/12] vfio: Introduce common function to add capabilities

In reply to Alex Williamson
Quoted 105 lines · Alex Williamson
Quoted 82 lines · On Tue, 18 Oct 2016 02:52:05 +0530 Kirti Wankhede <[email protected]> wrote:

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.

Change-Id I6fca329fa2291f37a2c859d0bc97574d9e2ce1a6
---
 drivers/vfio/vfio.c  | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/vfio.h |  4 +++
 2 files changed, 82 insertions(+)
drivers/vfio/vfio.c +62 -0
index a5a210005b65..e96cb3f7a23c 100644
@@ -1799,6 +1799,84 @@ void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset)
 }
 EXPORT_SYMBOL_GPL(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_region_info *info,
+			     struct vfio_info_cap *caps,
+			     int cap_type_id,
+			     void *cap_type)
+{
+	int ret;
+
+	if (!cap_type)
+		return 0;
+
+	switch (cap_type_id) {
+	case VFIO_REGION_INFO_CAP_SPARSE_MMAP:
+		ret = sparse_mmap_cap(caps, cap_type);
+		if (ret)
+			return ret;
+		break;
+
+	case VFIO_REGION_INFO_CAP_TYPE:
+		ret = region_type_cap(caps, cap_type);
+		if (ret)
+			return ret;
+		break;
+	default:
+		return -EINVAL;
+	}

drivers/vfio/vfio.c void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset) · line 1873

Quoted 10 lines
drivers/vfio/vfio.c +10 -0
+
+	info->flags |= VFIO_REGION_INFO_FLAG_CAPS;
+
+	if (caps->size) {
+		if (info->argsz < sizeof(*info) + caps->size) {
+			info->argsz = sizeof(*info) + caps->size;
+			info->cap_offset = 0;
+		} else {
+			vfio_info_cap_shift(caps, sizeof(*info));
+			info->cap_offset = sizeof(*info);

This doesn't work. We build the capability chain in a buffer and vfio_info_cap_add() expects the chain to be zero-based as each capability is added. vfio_info_cap_shift() is meant to be called once on that buffer immediately before copying it back to the user buffer to adjust the chain offsets to account for the offset within the buffer. vfio_info_cap_shift() cannot be called repeatedly on the buffer as we do support multiple capabilities in a chain.

Quoted 1 line

From the code I see, we add one type of capability at a time, either

VFIO_REGION_INFO_CAP_SPARSE_MMAP or VFIO_REGION_INFO_CAP_TYPE. Both are not the part of same case in the switch, right?
I do tested VFIO_REGION_INFO_CAP_SPARSE_MMAP by mapping some part of BAR0 and that works.

Kirti.

#51 lore raw

Re: [PATCH v9 06/12] vfio_pci: Update vfio_pci to use vfio_info_add_capability()

In reply to Kirti Wankhede
Quoted 146 lines · On Tue, 25 Oct 2016 02:52:39 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 136 lines · Alex Williamson
Quoted 122 lines · On Tue, 18 Oct 2016 02:52:06 +0530 Kirti Wankhede <[email protected]> wrote:

Update msix_sparse_mmap_cap() to use vfio_info_add_capability() Update region type capability to use vfio_info_add_capability() Can't split this commit for MSIx and region_type cap since there is a common code which need to be updated for both the cases.

Change-Id I52bb28c7875a6da5a79ddad1843e6088aff58a45
---
 drivers/vfio/pci/vfio_pci.c | 72 +++++++++++++++++----------------------------
 1 file changed, 27 insertions(+), 45 deletions(-)
drivers/vfio/pci/vfio_pci.c +22 -35
index d624a527777f..1ec0565b48ea 100644
@@ -556,12 +556,12 @@ 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_region_info *info,
 				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 +572,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 +591,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);
+	ret = vfio_info_add_capability(info, caps,
+				      VFIO_REGION_INFO_CAP_SPARSE_MMAP, sparse);
+	kfree(sparse);
 
-	cap = container_of(header, struct vfio_region_info_cap_type, header);
-	cap->type = type;
-	cap->subtype = subtype;
-
-	return 0;
+	return ret;
 }
 
 int vfio_pci_register_dev_region(struct vfio_pci_device *vdev,
@@ -704,7 +686,8 @@ static long vfio_pci_ioctl(void *device_data,
 			if (vdev->bar_mmap_supported[info.index]) {
 				info.flags |= VFIO_REGION_INFO_FLAG_MMAP;
 				if (info.index == vdev->msix_bar) {
-					ret = msix_sparse_mmap_cap(vdev, &caps);
+					ret = msix_sparse_mmap_cap(vdev, &info,
+								   &caps);
 					if (ret)
 						return ret;
 				}
@@ -752,6 +735,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,27 +748,23 @@ 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(&info, &caps,
+						      VFIO_REGION_INFO_CAP_TYPE,
+						      &cap_type);
 			if (ret)
 				return ret;
+
+		}
 		}
 
-		if (caps.size) {
-			info.flags |= VFIO_REGION_INFO_FLAG_CAPS;
-			if (info.argsz < sizeof(info) + caps.size) {
-				info.argsz = sizeof(info) + caps.size;

drivers/vfio/pci/vfio_pci.c static long vfio_pci_ioctl(void *device_data, · line 785

Quoted 10 lines
drivers/vfio/pci/vfio_pci.c +0 -10
-				info.cap_offset = 0;
-			} else {
-				vfio_info_cap_shift(&caps, sizeof(info));
-				if (copy_to_user((void __user *)arg +
-						  sizeof(info), caps.buf,
-						  caps.size)) {
-					kfree(caps.buf);
-					return -EFAULT;
-				}
-				info.cap_offset = sizeof(info);  
Quoted 8 lines

I prefer the case above, I'm fine with breaking out helpers to build a buffer containing the capability chain, but I would rather have the caller manage placing that back into the return structure. That also allows the helper to be independent of the structure we're operating on, it could be a region_info, irq_info, device_info, etc. It only needs to know the layout of the capability type we're trying to add, not the info structure itself. Thanks,

Quoted 2 lines

The capability feature is for region_info (VFIO_REGION_INFO_FLAG_CAPS), then structure we are operating couldn't be irq_info or device_info, right?

My point was that I prefer the caller managing region_info.flags and region_info.cap_offset, the helper only managing creating the capability chain within the buffer. There is a namespace conflict for the various capabilities though, each foo_info has their own space, which can conflict, which make make a common helper impractical. Thanks,

Alex

#47 lore raw

Re: [PATCH v9 05/12] vfio: Introduce common function to add capabilities

In reply to Kirti Wankhede
Quoted 106 lines · On Tue, 25 Oct 2016 02:57:58 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 96 lines · Alex Williamson
Quoted 82 lines · On Tue, 18 Oct 2016 02:52:05 +0530 Kirti Wankhede <[email protected]> wrote:

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.

Change-Id I6fca329fa2291f37a2c859d0bc97574d9e2ce1a6
---
 drivers/vfio/vfio.c  | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/vfio.h |  4 +++
 2 files changed, 82 insertions(+)
drivers/vfio/vfio.c +62 -0
index a5a210005b65..e96cb3f7a23c 100644
@@ -1799,6 +1799,84 @@ void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset)
 }
 EXPORT_SYMBOL_GPL(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_region_info *info,
+			     struct vfio_info_cap *caps,
+			     int cap_type_id,
+			     void *cap_type)
+{
+	int ret;
+
+	if (!cap_type)
+		return 0;
+
+	switch (cap_type_id) {
+	case VFIO_REGION_INFO_CAP_SPARSE_MMAP:
+		ret = sparse_mmap_cap(caps, cap_type);
+		if (ret)
+			return ret;
+		break;
+
+	case VFIO_REGION_INFO_CAP_TYPE:
+		ret = region_type_cap(caps, cap_type);
+		if (ret)
+			return ret;
+		break;
+	default:
+		return -EINVAL;
+	}

drivers/vfio/vfio.c void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset) · line 1873

Quoted 10 lines
drivers/vfio/vfio.c +10 -0
+
+	info->flags |= VFIO_REGION_INFO_FLAG_CAPS;
+
+	if (caps->size) {
+		if (info->argsz < sizeof(*info) + caps->size) {
+			info->argsz = sizeof(*info) + caps->size;
+			info->cap_offset = 0;
+		} else {
+			vfio_info_cap_shift(caps, sizeof(*info));
+			info->cap_offset = sizeof(*info);  
Quoted 8 lines

This doesn't work. We build the capability chain in a buffer and vfio_info_cap_add() expects the chain to be zero-based as each capability is added. vfio_info_cap_shift() is meant to be called once on that buffer immediately before copying it back to the user buffer to adjust the chain offsets to account for the offset within the buffer. vfio_info_cap_shift() cannot be called repeatedly on the buffer as we do support multiple capabilities in a chain.

Quoted 5 lines

From the code I see, we add one type of capability at a time, either VFIO_REGION_INFO_CAP_SPARSE_MMAP or VFIO_REGION_INFO_CAP_TYPE. Both are not the part of same case in the switch, right?
I do tested VFIO_REGION_INFO_CAP_SPARSE_MMAP by mapping some part of BAR0 and that works.

That simply means that we don't _currently_ have a user that implements multiple chain entries. The interface is however designed to support multiple entries and this breaks that goal. Thanks,

Alex

#64 lore raw

Re: [PATCH v9 11/12] docs: Add Documentation for Mediated devices

In reply to Kirti Wankhede
Quoted 10 lines · On Tue, 18 Oct 2016 02:52:11 +0530 Kirti Wankhede <[email protected]> wrote:

Add file Documentation/vfio-mediated-device.txt that include details of mediated device framework.

Change-Id I137dd646442936090d92008b115908b7b2c7bc5d
---
 Documentation/vfio-mdev/vfio-mediated-device.txt | 289 +++++++++++++++++++++++
 1 file changed, 289 insertions(+)
 create mode 100644 Documentation/vfio-mdev/vfio-mediated-device.txt

A missing component of the documentation is that the entire sysfs ABI needs to be added to Documentation/ABI. There's a README and plenty of examples there. Thanks,

Alex

#9 lore raw

RE: [PATCH v9 01/12] vfio: Mediated device Core driver

In reply to Kirti Wankhede
Quoted 66 lines

From: Kirti Wankhede [mailto:[email protected]]
Sent: Tuesday, October 18, 2016 5:22 AM

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;
};
Quoted 7 lines

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);

Medisted bus driver is responsible to add/delete mediated devices to/from

Medisted -> Mediated

Quoted 11 lines

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 driver for a mediated device.

in 'which driver'? it should be clear to remove 'in driver' here

Quoted 7 lines

* remove: to free resources in driver when mediated device is destroyed.
* open: open callback of mediated device
* release: release callback of mediated device
* read : read emulation callback.
* write: write emulation callback.
* mmap: mmap emulation callback.
* ioctl: ioctl callback.

You only highlight 'mandatory field' for supported_type_groups. What about other fields? Are all of them optional? Please clarify and also stay consistent to later code comment.

Quoted 62 lines

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.

Change-Id I73a5084574270b14541c529461ea2f03c292d510
---
 drivers/vfio/Kconfig             |   1 +
 drivers/vfio/Makefile            |   1 +
 drivers/vfio/mdev/Kconfig        |  11 ++
 drivers/vfio/mdev/Makefile       |   4 +
 drivers/vfio/mdev/mdev_core.c    | 372
+++++++++++++++++++++++++++++++++++++++
 drivers/vfio/mdev/mdev_driver.c  | 128 ++++++++++++++
 drivers/vfio/mdev/mdev_private.h |  41 +++++
 drivers/vfio/mdev/mdev_sysfs.c   | 296
+++++++++++++++++++++++++++++++
 include/linux/mdev.h             | 177 +++++++++++++++++++
 9 files changed, 1031 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
index da6e2ce77495..23eced02aaf6 100644
@@ -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 +0 -0
index 7b8a31f63fea..4a23c13b6be4 100644
@@ -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/

drivers/vfio/Makefile +1 -0
+obj-$(CONFIG_VFIO_MDEV) += mdev/
drivers/vfio/mdev/Kconfig new file +0 -0
new file mode 100644
index 000000000000..93addace9a67

drivers/vfio/mdev/Kconfig line 8

Quoted 8 lines
drivers/vfio/mdev/Kconfig new file +8 -0
@@ -0,0 +1,11 @@
+
+config VFIO_MDEV
+    tristate "Mediated device driver framework"
+    depends on VFIO
+    default n
+    help
+        Provides a framework to virtualize devices which don't have SR_IOV
+	capability built-in.

This statement is not accurate. A device can support SR-IOV, but in the same time using this mediated technology w/ SR-IOV capability disabled.

Quoted 55 lines
drivers/vfio/mdev/Kconfig new file +3 -0
+	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
new file mode 100644
index 000000000000..31bc04801d94
@@ -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 +36 -0
new file mode 100644
index 000000000000..7db5ec164aeb
@@ -0,0 +1,372 @@
+/*
+ * Mediated device Core Driver
+ *
+ * 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.
+ */
+
+#include <linux/module.h>
+#include <linux/device.h>
+#include <linux/slab.h>
+#include <linux/uuid.h>
+#include <linux/sysfs.h>
+#include <linux/mdev.h>
+
+#include "mdev_private.h"
+
+#define DRIVER_VERSION		"0.1"
+#define DRIVER_AUTHOR		"NVIDIA Corporation"
+#define DRIVER_DESC		"Mediated device Core Driver"
+
+static LIST_HEAD(parent_list);
+static DEFINE_MUTEX(parent_list_lock);
+static struct class_compat *mdev_bus_compat_class;
+
+static int _find_mdev_device(struct device *dev, void *data)
+{
+	struct mdev_device *mdev;
+
+	if (!dev_is_mdev(dev))
+		return 0;
+

drivers/vfio/mdev/mdev_core.c line 46

Quoted 10 lines
drivers/vfio/mdev/mdev_core.c new file +10 -0
+	mdev = to_mdev_device(dev);
+
+	if (uuid_le_cmp(mdev->uuid, *(uuid_le *)data) == 0)
+		return 1;
+
+	return 0;
+}
+
+static struct mdev_device *__find_mdev_device(struct parent_device *parent,
+					      uuid_le uuid)

parent_find_mdev_device?

Quoted 59 lines
drivers/vfio/mdev/mdev_core.c new file +59 -0
+{
+	struct device *dev;
+
+	dev = device_find_child(parent->dev, &uuid, _find_mdev_device);
+	if (!dev)
+		return NULL;
+
+	put_device(dev);
+
+	return to_mdev_device(dev);
+}
+
+/* Should be called holding parent_list_lock */
+static struct parent_device *__find_parent_device(struct device *dev)
+{
+	struct parent_device *parent;
+
+	list_for_each_entry(parent, &parent_list, next) {
+		if (parent->dev == dev)
+			return parent;
+	}
+	return NULL;
+}
+
+static void mdev_release_parent(struct kref *kref)
+{
+	struct parent_device *parent = container_of(kref, struct parent_device,
+						    ref);
+	struct device *dev = parent->dev;
+
+	kfree(parent);
+	put_device(dev);
+}
+
+static
+inline struct parent_device *mdev_get_parent(struct parent_device *parent)
+{
+	if (parent)
+		kref_get(&parent->ref);
+
+	return parent;
+}
+
+static inline void mdev_put_parent(struct parent_device *parent)
+{
+	if (parent)
+		kref_put(&parent->ref, mdev_release_parent);
+}
+
+static int mdev_device_create_ops(struct kobject *kobj,
+				  struct mdev_device *mdev)
+{
+	struct parent_device *parent = mdev->parent;
+	int ret;
+
+	ret = parent->ops->create(kobj, mdev);
+	if (ret)
+		return ret;
+

drivers/vfio/mdev/mdev_core.c line 115

Quoted 10 lines
drivers/vfio/mdev/mdev_core.c new file +10 -0
+	ret = sysfs_create_groups(&mdev->dev.kobj,
+				  parent->ops->mdev_attr_groups);
+	if (ret)
+		parent->ops->remove(mdev);
+
+	return ret;
+}
+
+static int mdev_device_remove_ops(struct mdev_device *mdev, bool force_remove)
+{

Can you add some comment here about when force_remove may be expected here, which would help others understand immediately instead of walking through the whole patch set?

drivers/vfio/mdev/mdev_core.c line 123

Quoted 8 lines
drivers/vfio/mdev/mdev_core.c new file +8 -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);

what about passing force_remove flag to remove callback, so vendor driver can decide whether any force cleanup required?

Quoted 697 lines
drivers/vfio/mdev/mdev_core.c new file +249 -0
+	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)
+{
+	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 = 0;
+	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);
+
+	parent->dev = dev;
+	parent->ops = ops;
+
+	ret = parent_create_sysfs_files(parent);
+	if (ret) {
+		mutex_unlock(&parent_list_lock);
+		mdev_put_parent(parent);
+		return ret;
+	}
+
+	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);
+
+	dev_info(dev, "MDEV: Registered\n");
+	return 0;
+
+add_dev_err:
+	mutex_unlock(&parent_list_lock);
+	put_device(dev);
+	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");
+
+	/*
+	 * Remove parent from the list and remove "mdev_supported_types"
+	 * sysfs files so that no new mediated device could be
+	 * created for this parent
+	 */
+	list_del(&parent->next);
+	parent_remove_sysfs_files(parent);
+
+	mutex_unlock(&parent_list_lock);
+
+	class_compat_remove_link(mdev_bus_compat_class, dev, NULL);
+
+	device_for_each_child(dev, (void *)&force_remove,
+			      mdev_device_remove_cb);
+	mdev_put_parent(parent);
+}
+EXPORT_SYMBOL(mdev_unregister_device);
+
+static void mdev_device_release(struct device *dev)
+{
+	struct mdev_device *mdev = to_mdev_device(dev);
+
+	dev_dbg(&mdev->dev, "MDEV: destroying\n");
+	kfree(mdev);
+}
+
+int mdev_device_create(struct kobject *kobj, struct device *dev, uuid_le uuid)
+{
+	int ret;
+	struct mdev_device *mdev;
+	struct parent_device *parent;
+	struct mdev_type *type = to_mdev_type(kobj);
+
+	parent = mdev_get_parent(type->parent);
+	if (!parent)
+		return -EINVAL;
+
+	/* Check for duplicate */
+	mdev = __find_mdev_device(parent, uuid);
+	if (mdev) {
+		ret = -EEXIST;
+		goto create_err;
+	}
+
+	mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
+	if (!mdev) {
+		ret = -ENOMEM;
+		goto create_err;
+	}
+
+	memcpy(&mdev->uuid, &uuid, sizeof(uuid_le));
+	mdev->parent = parent;
+	kref_init(&mdev->ref);
+
+	mdev->dev.parent  = dev;
+	mdev->dev.bus     = &mdev_bus_type;
+	mdev->dev.release = mdev_device_release;
+	dev_set_name(&mdev->dev, "%pUl", uuid.b);
+
+	ret = device_register(&mdev->dev);
+	if (ret) {
+		put_device(&mdev->dev);
+		goto create_err;
+	}
+
+	ret = mdev_device_create_ops(kobj, mdev);
+	if (ret)
+		goto create_failed;
+
+	ret = mdev_create_sysfs_files(&mdev->dev, type);
+	if (ret) {
+		mdev_device_remove_ops(mdev, true);
+		goto create_failed;
+	}
+
+	mdev->type_kobj = kobj;
+	dev_dbg(&mdev->dev, "MDEV: created\n");
+
+	return ret;
+
+create_failed:
+	device_unregister(&mdev->dev);
+
+create_err:
+	mdev_put_parent(parent);
+	return ret;
+}
+
+int mdev_device_remove(struct device *dev, bool force_remove)
+{
+	struct mdev_device *mdev;
+	struct parent_device *parent;
+	struct mdev_type *type;
+	int ret = 0;
+
+	if (!dev_is_mdev(dev))
+		return 0;
+
+	mdev = to_mdev_device(dev);
+	parent = mdev->parent;
+	type = to_mdev_type(mdev->type_kobj);
+
+	ret = mdev_device_remove_ops(mdev, force_remove);
+	if (ret)
+		return ret;
+
+	mdev_remove_sysfs_files(dev, type);
+	device_unregister(dev);
+	mdev_put_parent(parent);
+	return ret;
+}
+
+static int __init mdev_init(void)
+{
+	int ret;
+
+	ret = mdev_bus_register();
+	if (ret) {
+		pr_err("Failed to register mdev bus\n");
+		return ret;
+	}
+
+	mdev_bus_compat_class = class_compat_register("mdev_bus");
+	if (!mdev_bus_compat_class) {
+		mdev_bus_unregister();
+		return -ENOMEM;
+	}
+
+	/*
+	 * 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");
+
+	return ret;
+}
+
+static void __exit mdev_exit(void)
+{
+	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 +128 -0
new file mode 100644
index 000000000000..7768ef87f528
@@ -0,0 +1,128 @@
+/*
+ * MDEV driver
+ *
+ * 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.
+ */
+
+#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;
+
+	group = iommu_group_alloc();
+	if (IS_ERR(group)) {
+		dev_err(&mdev->dev, "MDEV: failed to allocate group!\n");
+		return PTR_ERR(group);
+	}
+
+	ret = iommu_group_add_device(group, &mdev->dev);
+	if (ret) {
+		dev_err(&mdev->dev, "MDEV: failed to add dev to group!\n");
+		goto attach_fail;
+	}
+
+	dev_info(&mdev->dev, "MDEV: group_id = %d\n",
+				 iommu_group_id(group));
+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;
+
+	ret = mdev_attach_iommu(mdev);
+	if (ret) {
+		dev_err(dev, "Failed to attach IOMMU\n");
+		return ret;
+	}
+
+	if (drv && drv->probe)
+		ret = drv->probe(dev);
+
+	if (ret)
+		mdev_detach_iommu(mdev);
+
+	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;
+}
+
+struct bus_type mdev_bus_type = {
+	.name		= "mdev",
+	.probe		= mdev_probe,
+	.remove		= mdev_remove,
+};
+EXPORT_SYMBOL_GPL(mdev_bus_type);
+
+/*
+ * 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;
+
+	/* register with core */
+	return driver_register(&drv->driver);
+}
+EXPORT_SYMBOL(mdev_register_driver);
+
+/*
+ * mdev_unregister_driver - unregister MDEV driver
+ * @drv: the driver to unregister
+ *
+ */
+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);
+}
drivers/vfio/mdev/mdev_private.h new file +41 -0
new file mode 100644
index 000000000000..000c93fcfdbd
@@ -0,0 +1,41 @@
+/*
+ * Mediated device interal definitions
+ *
+ * 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.
+ */
+
+#ifndef MDEV_PRIVATE_H
+#define MDEV_PRIVATE_H
+
+int  mdev_bus_register(void);
+void mdev_bus_unregister(void);
+
+struct mdev_type {
+	struct kobject kobj;
+	struct kobject *devices_kobj;
+	struct parent_device *parent;
+	struct list_head next;
+	struct attribute_group *group;
+};
+
+#define to_mdev_type_attr(_attr)	\
+	container_of(_attr, struct mdev_type_attribute, attr)
+#define to_mdev_type(_kobj)		\
+	container_of(_kobj, struct mdev_type, kobj)
+
+int  parent_create_sysfs_files(struct parent_device *parent);
+void parent_remove_sysfs_files(struct parent_device *parent);
+
+int  mdev_create_sysfs_files(struct device *dev, struct mdev_type *type);
+void mdev_remove_sysfs_files(struct device *dev, struct mdev_type *type);
+
+int  mdev_device_create(struct kobject *kobj, struct device *dev, uuid_le uuid);
+int  mdev_device_remove(struct device *dev, bool force_remove);
+
+#endif /* MDEV_PRIVATE_H */
drivers/vfio/mdev/mdev_sysfs.c new file +261 -0
new file mode 100644
index 000000000000..426e35cf79d0
@@ -0,0 +1,296 @@
+/*
+ * File attributes for 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.
+ */
+
+#include <linux/sysfs.h>
+#include <linux/ctype.h>
+#include <linux/device.h>
+#include <linux/slab.h>
+#include <linux/uuid.h>
+#include <linux/mdev.h>
+
+#include "mdev_private.h"
+
+/* Static functions */
+
+static ssize_t mdev_type_attr_show(struct kobject *kobj,
+				     struct attribute *__attr, char *buf)
+{
+	struct mdev_type_attribute *attr = to_mdev_type_attr(__attr);
+	struct mdev_type *type = to_mdev_type(kobj);
+	ssize_t ret = -EIO;
+
+	if (attr->show)
+		ret = attr->show(kobj, type->parent->dev, buf);
+	return ret;
+}
+
+static ssize_t mdev_type_attr_store(struct kobject *kobj,
+				      struct attribute *__attr,
+				      const char *buf, size_t count)
+{
+	struct mdev_type_attribute *attr = to_mdev_type_attr(__attr);
+	struct mdev_type *type = to_mdev_type(kobj);
+	ssize_t ret = -EIO;
+
+	if (attr->store)
+		ret = attr->store(&type->kobj, type->parent->dev, buf, count);
+	return ret;
+}
+
+static const struct sysfs_ops mdev_type_sysfs_ops = {
+	.show = mdev_type_attr_show,
+	.store = mdev_type_attr_store,
+};
+
+static ssize_t create_store(struct kobject *kobj, struct device *dev,
+			    const char *buf, size_t count)
+{
+	char *str;
+	uuid_le uuid;
+	int ret;
+
+	if (count < UUID_STRING_LEN)
+		return -EINVAL;
+
+	str = kstrndup(buf, count, GFP_KERNEL);
+	if (!str)
+		return -ENOMEM;
+
+	ret = uuid_le_to_bin(str, &uuid);
+	if (!ret) {
+
+		ret = mdev_device_create(kobj, dev, uuid);
+		if (ret)
+			pr_err("mdev_create: Failed to create mdev device\n");
+		else
+			ret = count;
+	}
+
+	kfree(str);
+	return ret;
+}
+
+MDEV_TYPE_ATTR_WO(create);
+
+static void mdev_type_release(struct kobject *kobj)
+{
+	struct mdev_type *type = to_mdev_type(kobj);
+
+	pr_debug("Releasing group %s\n", kobj->name);
+	kfree(type);
+}
+
+static struct kobj_type mdev_type_ktype = {
+	.sysfs_ops = &mdev_type_sysfs_ops,
+	.release = mdev_type_release,
+};
+
+struct mdev_type *add_mdev_supported_type(struct parent_device *parent,
+					  struct attribute_group *group)
+{
+	struct mdev_type *type;
+	int ret;
+
+	if (!group->name) {
+		pr_err("%s: Type name empty!\n", __func__);
+		return ERR_PTR(-EINVAL);
+	}
+
+	type = kzalloc(sizeof(*type), GFP_KERNEL);
+	if (!type)
+		return ERR_PTR(-ENOMEM);
+
+	type->kobj.kset = parent->mdev_types_kset;
+
+	ret = kobject_init_and_add(&type->kobj, &mdev_type_ktype, NULL,
+				   "%s-%s", dev_driver_string(parent->dev),
+				   group->name);
+	if (ret) {
+		kfree(type);
+		return ERR_PTR(ret);
+	}
+
+	ret = sysfs_create_file(&type->kobj, &mdev_type_attr_create.attr);
+	if (ret)
+		goto attr_create_failed;
+
+	type->devices_kobj = kobject_create_and_add("devices", &type->kobj);
+	if (!type->devices_kobj) {
+		ret = -ENOMEM;
+		goto attr_devices_failed;
+	}
+
+	ret = sysfs_create_files(&type->kobj,
+				 (const struct attribute **)group->attrs);
+	if (ret) {
+		ret = -ENOMEM;
+		goto attrs_failed;
+	}
+
+	type->group = group;
+	type->parent = parent;
+	return type;
+
+attrs_failed:
+	kobject_put(type->devices_kobj);
+attr_devices_failed:
+	sysfs_remove_file(&type->kobj, &mdev_type_attr_create.attr);
+attr_create_failed:
+	kobject_del(&type->kobj);
+	kobject_put(&type->kobj);
+	return ERR_PTR(ret);
+}
+
+static void remove_mdev_supported_type(struct mdev_type *type)
+{
+	sysfs_remove_files(&type->kobj,
+			   (const struct attribute **)type->group->attrs);
+	kobject_put(type->devices_kobj);
+	sysfs_remove_file(&type->kobj, &mdev_type_attr_create.attr);
+	kobject_del(&type->kobj);
+	kobject_put(&type->kobj);
+}
+
+static int add_mdev_supported_type_groups(struct parent_device *parent)
+{
+	int i;
+
+	for (i = 0; parent->ops->supported_type_groups[i]; i++) {
+		struct mdev_type *type;
+
+		type = add_mdev_supported_type(parent,
+					parent->ops->supported_type_groups[i]);
+		if (IS_ERR(type)) {
+			struct mdev_type *ltype, *tmp;
+
+			list_for_each_entry_safe(ltype, tmp, &parent->type_list,
+						  next) {
+				list_del(&ltype->next);
+				remove_mdev_supported_type(ltype);
+			}
+			return PTR_ERR(type);
+		}
+		list_add(&type->next, &parent->type_list);
+	}
+	return 0;
+}
+
+/* mdev sysfs Functions */
+
+void parent_remove_sysfs_files(struct parent_device *parent)
+{
+	struct mdev_type *type, *tmp;
+
+	list_for_each_entry_safe(type, tmp, &parent->type_list, next) {
+		list_del(&type->next);
+		remove_mdev_supported_type(type);
+	}
+
+	sysfs_remove_groups(&parent->dev->kobj, parent->ops->dev_attr_groups);
+	kset_unregister(parent->mdev_types_kset);
+}
+
+int parent_create_sysfs_files(struct parent_device *parent)
+{
+	int ret;
+
+	parent->mdev_types_kset = kset_create_and_add("mdev_supported_types",
+					       NULL, &parent->dev->kobj);
+
+	if (!parent->mdev_types_kset)
+		return -ENOMEM;
+
+	INIT_LIST_HEAD(&parent->type_list);
+
+	ret = sysfs_create_groups(&parent->dev->kobj,
+				  parent->ops->dev_attr_groups);
+	if (ret)
+		goto create_err;
+
+	ret = add_mdev_supported_type_groups(parent);
+	if (ret)
+		sysfs_remove_groups(&parent->dev->kobj,
+				    parent->ops->dev_attr_groups);
+	else
+		return ret;
+
+create_err:
+	kset_unregister(parent->mdev_types_kset);
+	return ret;
+}
+
+static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
+			    const char *buf, size_t count)
+{
+	unsigned long val;
+
+	if (kstrtoul(buf, 0, &val) < 0)
+		return -EINVAL;
+
+	if (val && device_remove_file_self(dev, attr)) {
+		int ret;
+
+		ret = mdev_device_remove(dev, false);
+		if (ret) {
+			device_create_file(dev, attr);
+			return ret;
+		}
+	}
+
+	return count;
+}
+
+static DEVICE_ATTR_WO(remove);
+
+static const struct attribute *mdev_device_attrs[] = {
+	&dev_attr_remove.attr,
+	NULL,
+};
+
+int  mdev_create_sysfs_files(struct device *dev, struct mdev_type *type)
+{
+	int ret;

drivers/vfio/mdev/mdev_sysfs.c line 271

Quoted 10 lines
drivers/vfio/mdev/mdev_sysfs.c new file +10 -0
+
+	ret = sysfs_create_files(&dev->kobj, mdev_device_attrs);
+	if (ret) {
+		pr_err("Failed to create remove sysfs entry\n");
+		return ret;
+	}
+
+	ret = sysfs_create_link(type->devices_kobj, &dev->kobj, dev_name(dev));
+	if (ret) {
+		pr_err("Failed to create symlink in types\n");

looks wrong place...

drivers/vfio/mdev/mdev_sysfs.c line 277

Quoted 6 lines
drivers/vfio/mdev/mdev_sysfs.c new file +6 -0
+		goto device_link_failed;
+	}
+
+	ret = sysfs_create_link(&dev->kobj, &type->kobj, "mdev_type");
+	if (ret) {
+		pr_err("Failed to create symlink in device directory\n");

exchange with above.

Quoted 204 lines
drivers/vfio/mdev/mdev_sysfs.c new file +19 -0
+		goto type_link_failed;
+	}
+
+	return ret;
+
+type_link_failed:
+	sysfs_remove_link(type->devices_kobj, dev_name(dev));
+device_link_failed:
+	sysfs_remove_files(&dev->kobj, mdev_device_attrs);
+	return ret;
+}
+
+void mdev_remove_sysfs_files(struct device *dev, struct mdev_type *type)
+{
+	sysfs_remove_link(&dev->kobj, "mdev_type");
+	sysfs_remove_link(type->devices_kobj, dev_name(dev));
+	sysfs_remove_files(&dev->kobj, mdev_device_attrs);
+
+}
include/linux/mdev.h new file +177 -1
new file mode 100644
index 000000000000..727209b2a67f
@@ -0,0 +1,177 @@
+/*
+ * Mediated device definition
+ *
+ * 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.
+ */
+
+#ifndef MDEV_H
+#define MDEV_H
+
+#include <uapi/linux/vfio.h>
+
+struct parent_device;
+
+/* Mediated device */
+struct mdev_device {
+	struct device		dev;
+	struct parent_device	*parent;
+	uuid_le			uuid;
+	void			*driver_data;
+
+	/* internal only */
+	struct kref		ref;
+	struct list_head	next;
+	struct kobject		*type_kobj;
+};
+
+
+/**
+ * struct parent_ops - Structure to be registered for each parent device to
+ * register the device to mdev module.
+ *
+ * @owner:		The module owner.
+ * @dev_attr_groups:	Attributes of the parent device.
+ * @mdev_attr_groups:	Attributes of the mediated device.
+ * @supported_type_groups: Attributes to define supported types. It is mandatory
+ *			to provide supported types.
+ * @create:		Called to allocate basic resources in parent device's
+ *			driver for a particular mediated device. It is
+ *			mandatory to provide create ops.
+ *			@kobj: kobject of type for which 'create' is called.
+ *			@mdev: mdev_device structure on of mediated device
+ *			      that is being created
+ *			Returns integer: success (0) or error (< 0)
+ * @remove:		Called to free resources in parent device's driver for a
+ *			a mediated device. It is mandatory to provide 'remove'
+ *			ops.
+ *			@mdev: mdev_device device structure which is being
+ *			       destroyed
+ *			Returns integer: success (0) or error (< 0)
+ * @open:		Open mediated device.
+ *			@mdev: mediated device.
+ *			Returns integer: success (0) or error (< 0)
+ * @release:		release mediated device
+ *			@mdev: mediated device.
+ * @read:		Read emulation callback
+ *			@mdev: mediated device structure
+ *			@buf: read buffer
+ *			@count: number of bytes to read
+ *			@ppos: address.
+ *			Retuns number on bytes read on success or error.
+ * @write:		Write emulation callback
+ *			@mdev: mediated device structure
+ *			@buf: write buffer
+ *			@count: number of bytes to be written
+ *			@ppos: address.
+ *			Retuns number on bytes written on success or error.
+ * @ioctl:		IOCTL callback
+ *			@mdev: mediated device structure
+ *			@cmd: mediated device structure
+ *			@arg: mediated device structure
+ * @mmap:		mmap callback
+ * Parent device that support mediated device should be registered with mdev
+ * module with parent_ops structure.
+ */
+
+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);
+};
+
+/* Parent Device */
+struct parent_device {
+	struct device		*dev;
+	const struct parent_ops	*ops;
+
+	/* internal */
+	struct kref		ref;
+	struct list_head	next;
+	struct kset *mdev_types_kset;
+	struct list_head	type_list;
+};
+
+/* interface for exporting mdev supported type attributes */
+struct mdev_type_attribute {
+	struct attribute attr;
+	ssize_t (*show)(struct kobject *kobj, struct device *dev, char *buf);
+	ssize_t (*store)(struct kobject *kobj, struct device *dev,
+			 const char *buf, size_t count);
+};
+
+#define MDEV_TYPE_ATTR(_name, _mode, _show, _store)		\
+struct mdev_type_attribute mdev_type_attr_##_name =		\
+	__ATTR(_name, _mode, _show, _store)
+#define MDEV_TYPE_ATTR_RW(_name) \
+	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RW(_name)
+#define MDEV_TYPE_ATTR_RO(_name) \
+	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RO(_name)
+#define MDEV_TYPE_ATTR_WO(_name) \
+	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_WO(_name)
+
+/**
+ * struct mdev_driver - Mediated device 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;
+};
+
+static inline struct mdev_driver *to_mdev_driver(struct device_driver *drv)
+{
+	return drv ? container_of(drv, struct mdev_driver, driver) : NULL;
+}
+
+static inline struct mdev_device *to_mdev_device(struct device *dev)
+{
+	return dev ? container_of(dev, struct mdev_device, dev) : NULL;
+}
+
+static inline void *mdev_get_drvdata(struct mdev_device *mdev)
+{
+	return mdev->driver_data;
+}
+
+static inline void mdev_set_drvdata(struct mdev_device *mdev, void *data)
+{
+	mdev->driver_data = data;
+}
+
+extern struct bus_type mdev_bus_type;
+
+#define dev_is_mdev(d) ((d)->bus == &mdev_bus_type)
+
+extern int  mdev_register_device(struct device *dev,
+				 const struct parent_ops *ops);
+extern void mdev_unregister_device(struct device *dev);
+
+extern int  mdev_register_driver(struct mdev_driver *drv, struct module *owner);
+extern void mdev_unregister_driver(struct mdev_driver *drv);
+
+#endif /* MDEV_H */
--

2.7.0

#12 lore raw

RE: [PATCH v9 02/12] vfio: VFIO based driver for Mediated devices

In reply to Kirti Wankhede
Quoted 5 lines

From: Kirti Wankhede [mailto:[email protected]]
Sent: Tuesday, October 18, 2016 5:22 AM

vfio_mdev driver registers with mdev core driver.
MDEV core driver creates mediated device and calls probe routine of

use same case - either 'mdev core' or 'MDEV core'

Quoted 88 lines

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.

Change-Id I583f4734752971d3d112324d69e2508c88f359ec
---
 drivers/vfio/mdev/Kconfig     |   7 ++
 drivers/vfio/mdev/Makefile    |   1 +
 drivers/vfio/mdev/vfio_mdev.c | 148
++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 156 insertions(+)
 create mode 100644 drivers/vfio/mdev/vfio_mdev.c
drivers/vfio/mdev/Kconfig +7 -0
index 93addace9a67..6cef0c4d2ceb 100644
@@ -9,3 +9,10 @@ config VFIO_MDEV
 	See Documentation/vfio-mdev/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
index 31bc04801d94..fa2d5ea466ee 100644
@@ -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 +41 -0
new file mode 100644
index 000000000000..b7b47604ce7a
@@ -0,0 +1,148 @@
+/*
+ * VFIO based driver for Mediated device
+ *
+ * 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.
+ */
+
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/device.h>
+#include <linux/kernel.h>
+#include <linux/slab.h>
+#include <linux/vfio.h>
+#include <linux/mdev.h>
+
+#include "mdev_private.h"
+
+#define DRIVER_VERSION  "0.1"
+#define DRIVER_AUTHOR   "NVIDIA Corporation"
+#define DRIVER_DESC     "VFIO based driver for Mediated device"
+
+static int vfio_mdev_open(void *device_data)
+{
+	struct mdev_device *mdev = device_data;
+	struct parent_device *parent = mdev->parent;
+	int ret;
+
+	if (unlikely(!parent->ops->open))
+		return -EINVAL;
+
+	if (!try_module_get(THIS_MODULE))
+		return -ENODEV;
+
+	ret = parent->ops->open(mdev);
+	if (ret)
+		module_put(THIS_MODULE);

drivers/vfio/mdev/vfio_mdev.c line 51

Quoted 10 lines
drivers/vfio/mdev/vfio_mdev.c new file +10 -0
+
+	return ret;
+}
+
+static void vfio_mdev_release(void *device_data)
+{
+	struct mdev_device *mdev = device_data;
+	struct parent_device *parent = mdev->parent;
+
+	if (parent->ops->release)

likely()

Quoted 99 lines
drivers/vfio/mdev/vfio_mdev.c new file +97 -1
+		parent->ops->release(mdev);
+
+	module_put(THIS_MODULE);
+}
+
+static long vfio_mdev_unlocked_ioctl(void *device_data,
+				     unsigned int cmd, unsigned long arg)
+{
+	struct mdev_device *mdev = device_data;
+	struct parent_device *parent = mdev->parent;
+
+	if (unlikely(!parent->ops->ioctl))
+		return -EINVAL;
+
+	return parent->ops->ioctl(mdev, cmd, arg);
+}
+
+static ssize_t vfio_mdev_read(void *device_data, char __user *buf,
+			      size_t count, loff_t *ppos)
+{
+	struct mdev_device *mdev = device_data;
+	struct parent_device *parent = mdev->parent;
+
+	if (unlikely(!parent->ops->read))
+		return -EINVAL;
+
+	return parent->ops->read(mdev, buf, count, ppos);
+}
+
+static ssize_t vfio_mdev_write(void *device_data, const char __user *buf,
+			       size_t count, loff_t *ppos)
+{
+	struct mdev_device *mdev = device_data;
+	struct parent_device *parent = mdev->parent;
+
+	if (unlikely(!parent->ops->write))
+		return -EINVAL;
+
+	return parent->ops->write(mdev, buf, count, ppos);
+}
+
+static int vfio_mdev_mmap(void *device_data, struct vm_area_struct *vma)
+{
+	struct mdev_device *mdev = device_data;
+	struct parent_device *parent = mdev->parent;
+
+	if (unlikely(!parent->ops->mmap))
+		return -EINVAL;
+
+	return parent->ops->mmap(mdev, vma);
+}
+
+static const struct vfio_device_ops vfio_mdev_dev_ops = {
+	.name		= "vfio-mdev",
+	.open		= vfio_mdev_open,
+	.release	= vfio_mdev_release,
+	.ioctl		= vfio_mdev_unlocked_ioctl,
+	.read		= vfio_mdev_read,
+	.write		= vfio_mdev_write,
+	.mmap		= vfio_mdev_mmap,
+};
+
+int vfio_mdev_probe(struct device *dev)
+{
+	struct mdev_device *mdev = to_mdev_device(dev);
+
+	return vfio_add_group_dev(dev, &vfio_mdev_dev_ops, mdev);
+}
+
+void vfio_mdev_remove(struct device *dev)
+{
+	vfio_del_group_dev(dev);
+}
+
+struct mdev_driver vfio_mdev_driver = {
+	.name	= "vfio_mdev",
+	.probe	= vfio_mdev_probe,
+	.remove	= vfio_mdev_remove,
+};
+
+static int __init vfio_mdev_init(void)
+{
+	return mdev_register_driver(&vfio_mdev_driver, THIS_MODULE);
+}
+
+static void __exit vfio_mdev_exit(void)
+{
+	mdev_unregister_driver(&vfio_mdev_driver);
+}
+
+module_init(vfio_mdev_init)
+module_exit(vfio_mdev_exit)
+
+MODULE_VERSION(DRIVER_VERSION);
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR(DRIVER_AUTHOR);
+MODULE_DESCRIPTION(DRIVER_DESC);
--

2.7.0

#20 lore raw

RE: [PATCH v9 04/12] vfio iommu: Add support for mediated devices

In reply to Alex Williamson
Quoted 17 lines

From: Alex Williamson [mailto:[email protected]]
Sent: Monday, October 24, 2016 10:32 AM

Quoted 14 lines
Quoted 14 lines

drivers/vfio/vfio_iommu_type1.c static long vfio_pin_pages(unsigned long vaddr, long npage, · line 459

Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +3 -2
-static long vfio_unpin_pages(unsigned long pfn, long npage,
-			     int prot, bool do_accounting)
+static long __vfio_unpin_pages_remote(struct vfio_iommu *iommu,
+				      unsigned long pfn, long npage, int prot,
+				      bool do_accounting)

Have you noticed that it's kind of confusing that __vfio_{un}pin_pages_remote() uses current, which does a get_user_pages_fast() while "local" uses a provided task_struct and uses get_user_pages_*remote*()? And also what was effectively local (ie. we're pinning for our own use here) is now "remote" and pinning for a remote, vendor driver consumer, is now "local". It's not very intuitive.

I questioned this confusing naming in v8 too...

Quoted 15 lines
Quoted 15 lines

'local' in local_domain was suggested to describe the domain for local page tracking. Earlier suggestions to have 'mdev' or 'noimmu' in this name were discarded. May be we should revisit what the name should be. Any suggestion?

For local_domain, to pin pages, flow is:

for local_domain

    |- vfio_pin_pages()
        |- vfio_iommu_type1_pin_pages()
            |- __vfio_pin_page_local()
                |-  vaddr_get_pfn(task->mm)
                    |- get_user_pages_remote()
Quoted 7 lines
Quoted 1 line

__vfio_pin_page_local() --> get_user_pages_remote()

In vfio.c we have the concept of an external user, perhaps that could be continued here. An mdev driver would be an external, or remote pinning.

I prefer to use remote here. It's aligned with underlying mm operations

Thanks
Kevin

#22 lore raw

RE: [PATCH v9 04/12] vfio iommu: Add support for mediated devices

In reply to Alex Williamson
Quoted 29 lines

From: Alex Williamson [mailto:[email protected]]
Sent: Thursday, October 20, 2016 5:03 AM

Quoted 4 lines
drivers/vfio/vfio_iommu_type1.c +0 -0
@@ -83,6 +92,21 @@ struct vfio_group {
 };
 
 /*

drivers/vfio/vfio_iommu_type1.c struct vfio_group { · line 104

Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10 -0
+ * Guest RAM pinning working set or DMA target
+ */
+struct vfio_pfn {
+	struct rb_node		node;
+	unsigned long		vaddr;		/* virtual addr */
+	dma_addr_t		iova;		/* IOVA */
+	unsigned long		pfn;		/* Host pfn */
+	int			prot;
+	atomic_t		ref_count;
+};

Somehow we're going to need to fit an invalidation callback here too. How would we handle a case where there are multiple mdev devices, from different vendor drivers, that all have the same pfn pinned? I'm already concerned about the per pfn overhead we're introducing here so clearly we cannot store an invalidation callback per pinned page, per vendor driver. Perhaps invalidations should be done using a notifier chain per vfio_iommu, the vendor drivers are required to register on that chain (fail pinning with empty notifier list) user unmapping will be broadcast to the notifier chain, the vendor driver will be responsible for deciding if each unmap is relevant to them (potentially it's for a pinning from another driver).

Quoted 6 lines

I expect we also need to enforce that vendors perform a synchronous unmap such that after returning from the notifier list call, the vfio_pfn should no longer exist. If it does we might need to BUG_ON. Also be careful to pay attention to the locking of the notifier vs unpin callbacks to avoid deadlocks.

What about just requesting vendor driver to provide a callback in parent device ops?

Curious in which scenario the user application (say Qemu here) may unmap memory pages which are still pinned by vendor driver... Is it purely about a corner case which we want to handle elegantly?

If yes, possibly a simpler way is to force destroying mdev instead of asking vendor driver to take care of each invalidation request under such situation. Since anyway the mdev device won't be in an usable state anymore... (sorry if I missed the key problem here.)

Thanks
Kevin

#24 lore raw

RE: [PATCH v9 04/12] vfio iommu: Add support for mediated devices

In reply to Alex Williamson
Quoted 42 lines

From: Tian, Kevin
Sent: Wednesday, October 26, 2016 3:54 PM

Quoted 29 lines

From: Alex Williamson [mailto:[email protected]]
Sent: Thursday, October 20, 2016 5:03 AM

Quoted 4 lines
drivers/vfio/vfio_iommu_type1.c +0 -0
@@ -83,6 +92,21 @@ struct vfio_group {
 };
 
 /*

drivers/vfio/vfio_iommu_type1.c struct vfio_group { · line 104

Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10 -0
+ * Guest RAM pinning working set or DMA target
+ */
+struct vfio_pfn {
+	struct rb_node		node;
+	unsigned long		vaddr;		/* virtual addr */
+	dma_addr_t		iova;		/* IOVA */
+	unsigned long		pfn;		/* Host pfn */
+	int			prot;
+	atomic_t		ref_count;
+};

Somehow we're going to need to fit an invalidation callback here too. How would we handle a case where there are multiple mdev devices, from different vendor drivers, that all have the same pfn pinned? I'm already concerned about the per pfn overhead we're introducing here so clearly we cannot store an invalidation callback per pinned page, per vendor driver. Perhaps invalidations should be done using a notifier chain per vfio_iommu, the vendor drivers are required to register on that chain (fail pinning with empty notifier list) user unmapping will be broadcast to the notifier chain, the vendor driver will be responsible for deciding if each unmap is relevant to them (potentially it's for a pinning from another driver).

Quoted 6 lines

I expect we also need to enforce that vendors perform a synchronous unmap such that after returning from the notifier list call, the vfio_pfn should no longer exist. If it does we might need to BUG_ON. Also be careful to pay attention to the locking of the notifier vs unpin callbacks to avoid deadlocks.

What about just requesting vendor driver to provide a callback in parent device ops?

Quoted 9 lines

Curious in which scenario the user application (say Qemu here) may unmap memory pages which are still pinned by vendor driver... Is it purely about a corner case which we want to handle elegantly?

If yes, possibly a simpler way is to force destroying mdev instead of asking vendor driver to take care of each invalidation request under such situation. Since anyway the mdev device won't be in an usable state anymore... (sorry if I missed the key problem here.)

or calling reset callback of parent device driver, if we don't want to break libvirt's expectation by blindly removing mdev device...

author maintainer
#10 lore raw

Re: [PATCH v9 01/12] vfio: Mediated device Core driver

In reply to Tian, Kevin
Quoted 5 lines
Quoted 2 lines

Medisted bus driver is responsible to add/delete mediated devices to/from

Medisted -> Mediated

Thanks for pointing out the typeo. Correcting it.

Quoted 22 lines
Quoted 11 lines

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 driver for a mediated device.

in 'which driver'? it should be clear to remove 'in driver' here

Quoted 7 lines

* remove: to free resources in driver when mediated device is destroyed.
* open: open callback of mediated device
* release: release callback of mediated device
* read : read emulation callback.
* write: write emulation callback.
* mmap: mmap emulation callback.
* ioctl: ioctl callback.

Quoted 4 lines

You only highlight 'mandatory field' for supported_type_groups. What about other fields? Are all of them optional? Please clarify and also stay consistent to later code comment.

'create' and 'remove' are mandatory. Updating the description here. Rest all are not cross-checked in mdev core driver, like 'create' and 'remove' but yes rest are optional. If vendor driver don't want to support emulated region they don't need read/write callbacks. Similarly if vendor driver don't want to support mmap region, they don't need mmap callback.

Code comments are consistent with this description.

...

Quoted 12 lines

drivers/vfio/mdev/Kconfig line 8

Quoted 8 lines
drivers/vfio/mdev/Kconfig new file +8 -0
+
+config VFIO_MDEV
+    tristate "Mediated device driver framework"
+    depends on VFIO
+    default n
+    help
+        Provides a framework to virtualize devices which don't have SR_IOV
+	capability built-in.

This statement is not accurate. A device can support SR-IOV, but in the same time using this mediated technology w/ SR-IOV capability disabled.

If SR-IOV is supported why would user use this framework? SR-IOV would give better performance.

...

Quoted 6 lines

drivers/vfio/mdev/mdev_core.c line 46

Quoted 3 lines
drivers/vfio/mdev/mdev_core.c new file +3 -0
+
+static struct mdev_device *__find_mdev_device(struct parent_device *parent,
+					      uuid_le uuid)

parent_find_mdev_device?

This function search for mdev device with given UUID, so I think its consistent what we have below for parent, __find_parent_device().

...

Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+static int mdev_device_remove_ops(struct mdev_device *mdev, bool force_remove)
+{

Can you add some comment here about when force_remove may be expected here, which would help others understand immediately instead of walking through the whole patch set?

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.

Quoted 13 lines

drivers/vfio/mdev/mdev_core.c line 123

Quoted 9 lines
drivers/vfio/mdev/mdev_core.c new file +8 -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);

what about passing force_remove flag to remove callback, so vendor driver can decide whether any force cleanup required?

'remove' getting called from sysfs is asynchronous, so vendor driver can retrun failure in that case if vendor driver finds that mdev device is being actively used.

mdev_unregister_device() is going to be called from vendor driver itself when device is being unbound or driver is being unloaded. In this case vendor driver can identify itself that its in its own teardown path.

So I feel there is no need to pass force_remove flag to 'remove' callback.

Quoted 7 lines
Quoted 7 lines
drivers/vfio/mdev/mdev_sysfs.c new file +7 -0
+int  mdev_create_sysfs_files(struct device *dev, struct mdev_type *type)
+{
+	int ret;
+
+	ret = sysfs_create_files(&dev->kobj, mdev_device_attrs);
+	if (ret) {
+		pr_err("Failed to create remove sysfs entry\n");
Quoted 9 lines

drivers/vfio/mdev/mdev_sysfs.c line 271

Quoted 6 lines
drivers/vfio/mdev/mdev_sysfs.c new file +6 -0
+		return ret;
+	}
+
+	ret = sysfs_create_link(type->devices_kobj, &dev->kobj, dev_name(dev));
+	if (ret) {
+		pr_err("Failed to create symlink in types\n");

looks wrong place...

No, this is correct. Above function creates symlink in mdev_supported_types/<type>/devices directory.

Quoted 9 lines

drivers/vfio/mdev/mdev_sysfs.c line 277

Quoted 6 lines
drivers/vfio/mdev/mdev_sysfs.c new file +6 -0
+		goto device_link_failed;
+	}
+
+	ret = sysfs_create_link(&dev->kobj, &type->kobj, "mdev_type");
+	if (ret) {
+		pr_err("Failed to create symlink in device directory\n");

exchange with above.

Again this is also correct. Above creates 'mdev_type' symlink in mdev device's directory.

Although these are correct, removing these error prints. You can find it from its return type or if sysfs functions through warnings.

Kirti.

author maintainer
#13 lore raw

Re: [PATCH v9 02/12] vfio: VFIO based driver for Mediated devices

In reply to Tian, Kevin
Quoted 10 lines

drivers/vfio/mdev/vfio_mdev.c line 51

Quoted 7 lines
drivers/vfio/mdev/vfio_mdev.c new file +6 -0
+static void vfio_mdev_release(void *device_data)
+{
+	struct mdev_device *mdev = device_data;
+	struct parent_device *parent = mdev->parent;
+
+	if (parent->ops->release)

likely()

Quoted 5 lines

drivers/vfio/mdev/vfio_mdev.c line 56

Quoted 5 lines
drivers/vfio/mdev/vfio_mdev.c new file +5 -0
+		parent->ops->release(mdev);
+
+	module_put(THIS_MODULE);
+}
+

Thanks for pointing that out. Fixing this in next set of patch.

Kirti

author maintainer
#21 lore raw

Re: [PATCH v9 04/12] vfio iommu: Add support for mediated devices

In reply to Tian, Kevin
Quoted 18 lines · Kevin
Quoted 17 lines

From: Alex Williamson [mailto:[email protected]]
Sent: Monday, October 24, 2016 10:32 AM

Quoted 14 lines
Quoted 14 lines

drivers/vfio/vfio_iommu_type1.c static long vfio_pin_pages(unsigned long vaddr, long npage, · line 459

Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +3 -2
-static long vfio_unpin_pages(unsigned long pfn, long npage,
-			     int prot, bool do_accounting)
+static long __vfio_unpin_pages_remote(struct vfio_iommu *iommu,
+				      unsigned long pfn, long npage, int prot,
+				      bool do_accounting)

Have you noticed that it's kind of confusing that __vfio_{un}pin_pages_remote() uses current, which does a get_user_pages_fast() while "local" uses a provided task_struct and uses get_user_pages_*remote*()? And also what was effectively local (ie. we're pinning for our own use here) is now "remote" and pinning for a remote, vendor driver consumer, is now "local". It's not very intuitive.

Quoted 2 lines

I questioned this confusing naming in v8 too...

I do tried to address your concerns on v8.

Quoted 18 lines
Quoted 18 lines
Quoted 16 lines

'local' in local_domain was suggested to describe the domain for local page tracking. Earlier suggestions to have 'mdev' or 'noimmu' in this name were discarded. May be we should revisit what the name should be. Any suggestion?

For local_domain, to pin pages, flow is:

for local_domain

    |- vfio_pin_pages()
        |- vfio_iommu_type1_pin_pages()
            |- __vfio_pin_page_local()
                |-  vaddr_get_pfn(task->mm)
                    |- get_user_pages_remote()

__vfio_pin_page_local() --> get_user_pages_remote()

Quoted 7 lines
Quoted 4 lines

In vfio.c we have the concept of an external user, perhaps that could be continued here. An mdev driver would be an external, or remote pinning.

I prefer to use remote here. It's aligned with underlying mm operations

Using 'remote' in this case is also confusing since it is already used in this file. I liked Alex's suggestion to use external and I'll have those changed in next version of patch set.

Kirti

#23 lore raw

Re: [PATCH v9 04/12] vfio iommu: Add support for mediated devices

In reply to Tian, Kevin
Quoted 36 lines · Kevin
Quoted 29 lines

From: Alex Williamson [mailto:[email protected]]
Sent: Thursday, October 20, 2016 5:03 AM

Quoted 4 lines
drivers/vfio/vfio_iommu_type1.c +0 -0
@@ -83,6 +92,21 @@ struct vfio_group {
 };
 
 /*

drivers/vfio/vfio_iommu_type1.c struct vfio_group { · line 104

Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10 -0
+ * Guest RAM pinning working set or DMA target
+ */
+struct vfio_pfn {
+	struct rb_node		node;
+	unsigned long		vaddr;		/* virtual addr */
+	dma_addr_t		iova;		/* IOVA */
+	unsigned long		pfn;		/* Host pfn */
+	int			prot;
+	atomic_t		ref_count;
+};  

Somehow we're going to need to fit an invalidation callback here too. How would we handle a case where there are multiple mdev devices, from different vendor drivers, that all have the same pfn pinned? I'm already concerned about the per pfn overhead we're introducing here so clearly we cannot store an invalidation callback per pinned page, per vendor driver. Perhaps invalidations should be done using a notifier chain per vfio_iommu, the vendor drivers are required to register on that chain (fail pinning with empty notifier list) user unmapping will be broadcast to the notifier chain, the vendor driver will be responsible for deciding if each unmap is relevant to them (potentially it's for a pinning from another driver).

Quoted 6 lines

I expect we also need to enforce that vendors perform a synchronous unmap such that after returning from the notifier list call, the vfio_pfn should no longer exist. If it does we might need to BUG_ON. Also be careful to pay attention to the locking of the notifier vs unpin callbacks to avoid deadlocks.

Quoted 2 lines

What about just requesting vendor driver to provide a callback in parent device ops?

How does the iommu driver get to the mdev vendor driver callback? We can also have pages pinned by multiple vendor drivers, I don't think we want the additional overhead of a per page list of invalidation callbacks.

Quoted 3 lines

Curious in which scenario the user application (say Qemu here) may unmap memory pages which are still pinned by vendor driver... Is it purely about a corner case which we want to handle elegantly?

The vfio type1 iommu API provides a MAP and UNMAP interface. The unmap call is expected to work regardless of how it might inhibit the device from working. This is currently true of iommu protected devices today, a user can unmap pages which might be DMA targets for the device and the iommu prevents further access to those pages, possibly at the expense of device operation. We cannot support an interface where a user can unmap a set of pages and map in new pages to replace them when the vendor driver might be caching stale mappings.

In normal VM operation perhaps this is a corner case, but the API is not defined only for the normal and expected behavior of a VM.

Quoted 4 lines

If yes, possibly a simpler way is to force destroying mdev instead of asking vendor driver to take care of each invalidation request under such situation. Since anyway the mdev device won't be in an usable state anymore... (sorry if I missed the key problem here.)

That's a pretty harsh response for an operation which is completely valid from an API perspective. What if the VM does an unmap of all memory around reset? We cannot guarantee that the guest driver will have a chance to do cleanup, the guest may have crashed or a system_reset invoked. Would you have the mdev destroyed in this case? How could QEMU, which has no device specific driver to know that vendor pinnings are present, recover from this? Thanks,

Alex

#25 lore raw

Re: [PATCH v9 04/12] vfio iommu: Add support for mediated devices

In reply to Tian, Kevin
Quoted 46 lines · Kevin
Quoted 46 lines

From: Tian, Kevin
Sent: Wednesday, October 26, 2016 3:54 PM

Quoted 29 lines

From: Alex Williamson [mailto:[email protected]]
Sent: Thursday, October 20, 2016 5:03 AM

Quoted 4 lines
drivers/vfio/vfio_iommu_type1.c +0 -0
@@ -83,6 +92,21 @@ struct vfio_group {
 };
 
 /*

drivers/vfio/vfio_iommu_type1.c struct vfio_group { · line 104

Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10 -0
+ * Guest RAM pinning working set or DMA target
+ */
+struct vfio_pfn {
+	struct rb_node		node;
+	unsigned long		vaddr;		/* virtual addr */
+	dma_addr_t		iova;		/* IOVA */
+	unsigned long		pfn;		/* Host pfn */
+	int			prot;
+	atomic_t		ref_count;
+};  

Somehow we're going to need to fit an invalidation callback here too. How would we handle a case where there are multiple mdev devices, from different vendor drivers, that all have the same pfn pinned? I'm already concerned about the per pfn overhead we're introducing here so clearly we cannot store an invalidation callback per pinned page, per vendor driver. Perhaps invalidations should be done using a notifier chain per vfio_iommu, the vendor drivers are required to register on that chain (fail pinning with empty notifier list) user unmapping will be broadcast to the notifier chain, the vendor driver will be responsible for deciding if each unmap is relevant to them (potentially it's for a pinning from another driver).

Quoted 6 lines

I expect we also need to enforce that vendors perform a synchronous unmap such that after returning from the notifier list call, the vfio_pfn should no longer exist. If it does we might need to BUG_ON. Also be careful to pay attention to the locking of the notifier vs unpin callbacks to avoid deadlocks.

What about just requesting vendor driver to provide a callback in parent device ops?

Curious in which scenario the user application (say Qemu here) may unmap memory pages which are still pinned by vendor driver... Is it purely about a corner case which we want to handle elegantly?

Quoted 8 lines
Quoted 5 lines

If yes, possibly a simpler way is to force destroying mdev instead of asking vendor driver to take care of each invalidation request under such situation. Since anyway the mdev device won't be in an usable state anymore... (sorry if I missed the key problem here.)

or calling reset callback of parent device driver, if we don't want to break libvirt's expectation by blindly removing mdev device...

I think we're going off into the weeds here. mdev devices need to honor the existing API, therefore an unmap should result in preventing the device from further access to the unmapped pages, nothing more, nothing less. Thanks,

Alex

#8 lore raw

Re: [PATCH v9 01/12] vfio: Mediated device Core driver

In reply to Alex Williamson
Quoted 155 lines · Alex Williamson
Quoted 145 lines · On Thu, 20 Oct 2016 15:23:53 +0800 Jike Song <[email protected]> wrote:
Quoted 125 lines · Kirti Wankhede
drivers/vfio/mdev/mdev_core.c new file +117 -0
new file mode 100644
index 000000000000..7db5ec164aeb
@@ -0,0 +1,372 @@
+/*
+ * Mediated device Core Driver
+ *
+ * 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.
+ */
+
+#include <linux/module.h>
+#include <linux/device.h>
+#include <linux/slab.h>
+#include <linux/uuid.h>
+#include <linux/sysfs.h>
+#include <linux/mdev.h>
+
+#include "mdev_private.h"
+
+#define DRIVER_VERSION		"0.1"
+#define DRIVER_AUTHOR		"NVIDIA Corporation"
+#define DRIVER_DESC		"Mediated device Core Driver"
+
+static LIST_HEAD(parent_list);
+static DEFINE_MUTEX(parent_list_lock);
+static struct class_compat *mdev_bus_compat_class;
+  
 
+
+/*
+ * 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 = 0;
+	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);
+
+	parent->dev = dev;
+	parent->ops = ops;
+
+	ret = parent_create_sysfs_files(parent);
+	if (ret) {
+		mutex_unlock(&parent_list_lock);
+		mdev_put_parent(parent);
+		return ret;
+	}
+
+	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);
+
+	dev_info(dev, "MDEV: Registered\n");
+	return 0;
+
+add_dev_err:
+	mutex_unlock(&parent_list_lock);
+	put_device(dev);
+	return ret;
+}
+EXPORT_SYMBOL(mdev_register_device);  
 
+static int __init mdev_init(void)
+{
+	int ret;
+
+	ret = mdev_bus_register();
+	if (ret) {
+		pr_err("Failed to register mdev bus\n");
+		return ret;
+	}
+
+	mdev_bus_compat_class = class_compat_register("mdev_bus");
+	if (!mdev_bus_compat_class) {
+		mdev_bus_unregister();
+		return -ENOMEM;
+	}
+
+	/*
+	 * 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");
+
+	return ret;

drivers/vfio/mdev/mdev_core.c line 367

Quoted 10 lines
drivers/vfio/mdev/mdev_core.c new file +10 -0
+}
+
+static void __exit mdev_exit(void)
+{
+	class_compat_unregister(mdev_bus_compat_class);
+	mdev_bus_unregister();
+}
+
+module_init(mdev_init)
+module_exit(mdev_exit)  

Hi Kirti,

There is a possible issue: mdev_bus_register is called from mdev_init, a module_init, equal to device_initcall if builtin to vmlinux; however, the vendor driver, say i915.ko for intel case, have to call mdev_register_device from its module_init: at that time, mdev_init is still not called.

Quoted 6 lines

Not sure if this issue exists with nvidia.ko. Though in most cases we are expecting users select mdev as a standalone module, we still won't break builtin case.

Hi Alex, do you have any suggestion here?

Quoted 9 lines

To fully solve the problem of built-in drivers making use of the mdev infrastructure we'd need to make mdev itself builtin and possibly a subsystem that is initialized prior to device drivers. Is that really necessary? Even though i915.ko is often loaded as part of an initramfs, most systems still build it as a module. I would expect that standard module dependencies will pull in the necessary mdev and vfio modules to make this work correctly. I can't say that I'm prepared to make mdev be a subsystem as would be necessary for builtin drivers to make use of.

Hi Alex,

I'm sorry to say that my previous understanding is not fully correct. Current combination of mdev and i915 are prone to panic the system as long as both built into vmlinux.

mdev_init:

	mdev_bus_register();
	mdev_bus_compat_class = class_compat_register("mdev_bus");
	request_module_nowait("vfio_mdev");

mdev_register_device:

	class_compat_create_link(mdev_bus_compat_class, dev, NULL);

If both mdev and i915 are builtin, the class_compat_create_link call will simply panic the system. People having such .config will be annoyed, for example, when he tries to bisect.

I'm not arguing that mdev should be a subsys here, but there still be a possible way out. Adding the class_compat_register into mdev_register_device if not yet registered, will help out. Maybe it isn't the typical location to register a class, nonetheless it fix the problem here with least impact.

Looking forward to your opinion :)

--
Thanks,
Jike

#29 lore raw

Re: [Qemu-devel] [PATCH v9 04/12] vfio iommu: Add support for mediated devices

In reply to Kirti Wankhede
Quoted 14 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 module

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.

Quoted 4 lines

This change adds pin and unpin support for mediated device to TYPE1 IOMMU backend module. More details:
- When iommu_group of mediated devices is attached, task structure is cached which is used later to pin pages and page accounting.

For SPAPR TCE IOMMU driver, I ended up caching mm_struct with atomic_inc(&container->mm->mm_count) (patches are on the way) instead of using @current or task as the process might be gone while VFIO container is still alive and @mm might be needed to do proper cleanup; this might not be an issue with this patchset now but still you seem to only use @mm from task_struct.

Quoted 22 lines

- It keeps track of pinned pages for mediated domain. This data is used to verify unpinning request and to unpin remaining pages while detaching, if there are any.
- Used existing mechanism for page accounting. If iommu capable domain exist in the container then all pages are already pinned and accounted. Accouting for mdev device is only done if there is no iommu capable domain in the container.
- 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

Change-Id I295d6f0f2e0579b8d9882bfd8fd5a4194b97bd9a
Signature · 1 line
Alexey
author maintainer
#30 lore raw

Re: [Qemu-devel] [PATCH v9 04/12] vfio iommu: Add support for mediated devices

Quoted 21 lines · Alexey Kardashevskiy
Quoted 14 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 module

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.

Quoted 4 lines

This change adds pin and unpin support for mediated device to TYPE1 IOMMU backend module. More details:
- When iommu_group of mediated devices is attached, task structure is cached which is used later to pin pages and page accounting.

Quoted 7 lines

For SPAPR TCE IOMMU driver, I ended up caching mm_struct with atomic_inc(&container->mm->mm_count) (patches are on the way) instead of using @current or task as the process might be gone while VFIO container is still alive and @mm might be needed to do proper cleanup; this might not be an issue with this patchset now but still you seem to only use @mm from task_struct.

Consider the example of QEMU process which creates VFIO container, QEMU in its teardown path would release the container. How could container be alive when process is gone?

Kirti

Quoted 26 lines
Quoted 24 lines

- It keeps track of pinned pages for mediated domain. This data is used to verify unpinning request and to unpin remaining pages while detaching, if there are any.
- Used existing mechanism for page accounting. If iommu capable domain exist in the container then all pages are already pinned and accounted. Accouting for mdev device is only done if there is no iommu capable domain in the container.
- 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

Change-Id I295d6f0f2e0579b8d9882bfd8fd5a4194b97bd9a
#31 lore raw

Re: [Qemu-devel] [PATCH v9 04/12] vfio iommu: Add support for mediated devices

Quoted 30 lines · On Thu, 27 Oct 2016 18:01:51 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 21 lines · Alexey Kardashevskiy
Quoted 14 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 module

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.

Quoted 4 lines

This change adds pin and unpin support for mediated device to TYPE1 IOMMU backend module. More details:
- When iommu_group of mediated devices is attached, task structure is cached which is used later to pin pages and page accounting.

Quoted 7 lines

For SPAPR TCE IOMMU driver, I ended up caching mm_struct with atomic_inc(&container->mm->mm_count) (patches are on the way) instead of using @current or task as the process might be gone while VFIO container is still alive and @mm might be needed to do proper cleanup; this might not be an issue with this patchset now but still you seem to only use @mm from task_struct.

Quoted 3 lines

Consider the example of QEMU process which creates VFIO container, QEMU in its teardown path would release the container. How could container be alive when process is gone?

If QEMU is sent a SIGKILL, does the process still exist? We must be able to perform cleanup regardless of the state, or existence, of the task that created it. Thanks,

Alex

author maintainer
#32 lore raw

Re: [Qemu-devel] [PATCH v9 04/12] vfio iommu: Add support for mediated devices

Quoted 33 lines · Alex Williamson
Quoted 30 lines · On Thu, 27 Oct 2016 18:01:51 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 28 lines · Alexey Kardashevskiy
Quoted 14 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 module

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.

Quoted 4 lines

This change adds pin and unpin support for mediated device to TYPE1 IOMMU backend module. More details:
- When iommu_group of mediated devices is attached, task structure is cached which is used later to pin pages and page accounting.

For SPAPR TCE IOMMU driver, I ended up caching mm_struct with atomic_inc(&container->mm->mm_count) (patches are on the way) instead of using @current or task as the process might be gone while VFIO container is still alive and @mm might be needed to do proper cleanup; this might not be an issue with this patchset now but still you seem to only use @mm from task_struct.

Quoted 8 lines
Quoted 3 lines

Consider the example of QEMU process which creates VFIO container, QEMU in its teardown path would release the container. How could container be alive when process is gone?

If QEMU is sent a SIGKILL, does the process still exist? We must be able to perform cleanup regardless of the state, or existence, of the task that created it. Thanks,

The kernel closes all open file descriptors when any process is terminated, so .release() from struct vfio_iommu_driver_ops gets called on SIGKILL or SIGTERM and release() function do all cleanup.

Kirti

#33 lore raw

Re: [Qemu-devel] [PATCH v9 04/12] vfio iommu: Add support for mediated devices

In reply to Kirti Wankhede
Quoted 32 lines · Kirti Wankhede
Quoted 21 lines · Alexey Kardashevskiy
Quoted 14 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 module

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.

Quoted 4 lines

This change adds pin and unpin support for mediated device to TYPE1 IOMMU backend module. More details:
- When iommu_group of mediated devices is attached, task structure is cached which is used later to pin pages and page accounting.

Quoted 7 lines

For SPAPR TCE IOMMU driver, I ended up caching mm_struct with atomic_inc(&container->mm->mm_count) (patches are on the way) instead of using @current or task as the process might be gone while VFIO container is still alive and @mm might be needed to do proper cleanup; this might not be an issue with this patchset now but still you seem to only use @mm from task_struct.

Quoted 3 lines

Consider the example of QEMU process which creates VFIO container, QEMU in its teardown path would release the container. How could container be alive when process is gone?

do_exit() in kernel/exit.c calls exit_mm() (which sets NULL to tsk->mm) first, and then releases open files by calling exit_files(). So container's release() does not have current->mm.

Quoted 29 lines

Kirti

Quoted 26 lines
Quoted 24 lines

- It keeps track of pinned pages for mediated domain. This data is used to verify unpinning request and to unpin remaining pages while detaching, if there are any.
- Used existing mechanism for page accounting. If iommu capable domain exist in the container then all pages are already pinned and accounted. Accouting for mdev device is only done if there is no iommu capable domain in the container.
- 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

Change-Id I295d6f0f2e0579b8d9882bfd8fd5a4194b97bd9a
Signature · 1 line
Alexey
author maintainer
#34 lore raw

Re: [Qemu-devel] [PATCH v9 04/12] vfio iommu: Add support for mediated devices

Quoted 33 lines · Alexey Kardashevskiy
Quoted 32 lines · Kirti Wankhede
Quoted 28 lines · Alexey Kardashevskiy
Quoted 14 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 module

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.

Quoted 4 lines

This change adds pin and unpin support for mediated device to TYPE1 IOMMU backend module. More details:
- When iommu_group of mediated devices is attached, task structure is cached which is used later to pin pages and page accounting.

For SPAPR TCE IOMMU driver, I ended up caching mm_struct with atomic_inc(&container->mm->mm_count) (patches are on the way) instead of using @current or task as the process might be gone while VFIO container is still alive and @mm might be needed to do proper cleanup; this might not be an issue with this patchset now but still you seem to only use @mm from task_struct.

Quoted 8 lines
Quoted 3 lines

Consider the example of QEMU process which creates VFIO container, QEMU in its teardown path would release the container. How could container be alive when process is gone?

do_exit() in kernel/exit.c calls exit_mm() (which sets NULL to tsk->mm) first, and then releases open files by calling exit_files(). So container's release() does not have current->mm.

Incrementing usage count (get_task_struct()) while saving task structure and decementing it (put_task_struct()) from release() should work here. Updating the patch.

Thanks,
Kirti

#35 lore raw

Re: [Qemu-devel] [PATCH v9 04/12] vfio iommu: Add support for mediated devices

Quoted 40 lines · Kirti Wankhede
Quoted 37 lines · Alexey Kardashevskiy
Quoted 35 lines · Kirti Wankhede
Quoted 21 lines · Alexey Kardashevskiy
Quoted 14 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 module

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.

Quoted 4 lines

This change adds pin and unpin support for mediated device to TYPE1 IOMMU backend module. More details:
- When iommu_group of mediated devices is attached, task structure is cached which is used later to pin pages and page accounting.

Quoted 7 lines

For SPAPR TCE IOMMU driver, I ended up caching mm_struct with atomic_inc(&container->mm->mm_count) (patches are on the way) instead of using @current or task as the process might be gone while VFIO container is still alive and @mm might be needed to do proper cleanup; this might not be an issue with this patchset now but still you seem to only use @mm from task_struct.

Consider the example of QEMU process which creates VFIO container, QEMU in its teardown path would release the container. How could container be alive when process is gone?

Quoted 8 lines
Quoted 4 lines

do_exit() in kernel/exit.c calls exit_mm() (which sets NULL to tsk->mm) first, and then releases open files by calling exit_files(). So container's release() does not have current->mm.

Incrementing usage count (get_task_struct()) while saving task structure and decementing it (put_task_struct()) from release() should work here. Updating the patch.

I cannot see how the task->usage counter prevents do_exit() from performing the exit, can you?

Signature · 1 line
Alexey
author maintainer
#36 lore raw

Re: [Qemu-devel] [PATCH v9 04/12] vfio iommu: Add support for mediated devices

Quoted 46 lines · Alexey Kardashevskiy
Quoted 45 lines · Kirti Wankhede
Quoted 41 lines · Alexey Kardashevskiy
Quoted 32 lines · Kirti Wankhede
Quoted 21 lines · Alexey Kardashevskiy
Quoted 14 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 module

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.

Quoted 4 lines

This change adds pin and unpin support for mediated device to TYPE1 IOMMU backend module. More details:
- When iommu_group of mediated devices is attached, task structure is cached which is used later to pin pages and page accounting.

Quoted 7 lines

For SPAPR TCE IOMMU driver, I ended up caching mm_struct with atomic_inc(&container->mm->mm_count) (patches are on the way) instead of using @current or task as the process might be gone while VFIO container is still alive and @mm might be needed to do proper cleanup; this might not be an issue with this patchset now but still you seem to only use @mm from task_struct.

Quoted 3 lines

Consider the example of QEMU process which creates VFIO container, QEMU in its teardown path would release the container. How could container be alive when process is gone?

do_exit() in kernel/exit.c calls exit_mm() (which sets NULL to tsk->mm) first, and then releases open files by calling exit_files(). So container's release() does not have current->mm.

Quoted 7 lines
Quoted 3 lines

Incrementing usage count (get_task_struct()) while saving task structure and decementing it (put_task_struct()) from release() should work here. Updating the patch.

I cannot see how the task->usage counter prevents do_exit() from performing the exit, can you?

It will not prevent exit from do_exit(), but that will make sure that we don't have stale pointer of task structure. Then we can check whether the task is alive and get mm pointer in teardown path as below:

{
        struct task_struct *task = domain->external_addr_space->task;
        struct mm_struct *mm = NULL;

        put_pfn(pfn, prot);

        if (pid_alive(task))
                mm = get_task_mm(task);

        if (mm) {
                if (do_accounting)
                        vfio_lock_acct(task, -1);

                mmput(mm);
        }
}

Thanks,
Kirti

#37 lore raw

Re: [Qemu-devel] [PATCH v9 04/12] vfio iommu: Add support for mediated devices

Quoted 53 lines · Kirti Wankhede
Quoted 50 lines · Alexey Kardashevskiy
Quoted 48 lines · Kirti Wankhede
Quoted 33 lines · Alexey Kardashevskiy
Quoted 32 lines · Kirti Wankhede
Quoted 28 lines · Alexey Kardashevskiy
Quoted 14 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 module

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.

Quoted 4 lines

This change adds pin and unpin support for mediated device to TYPE1 IOMMU backend module. More details:
- When iommu_group of mediated devices is attached, task structure is cached which is used later to pin pages and page accounting.

For SPAPR TCE IOMMU driver, I ended up caching mm_struct with atomic_inc(&container->mm->mm_count) (patches are on the way) instead of using @current or task as the process might be gone while VFIO container is still alive and @mm might be needed to do proper cleanup; this might not be an issue with this patchset now but still you seem to only use @mm from task_struct.

Quoted 8 lines
Quoted 3 lines

Consider the example of QEMU process which creates VFIO container, QEMU in its teardown path would release the container. How could container be alive when process is gone?

do_exit() in kernel/exit.c calls exit_mm() (which sets NULL to tsk->mm) first, and then releases open files by calling exit_files(). So container's release() does not have current->mm.

Incrementing usage count (get_task_struct()) while saving task structure and decementing it (put_task_struct()) from release() should work here. Updating the patch.

Quoted 7 lines
Quoted 3 lines

I cannot see how the task->usage counter prevents do_exit() from performing the exit, can you?

It will not prevent exit from do_exit(), but that will make sure that we don't have stale pointer of task structure. Then we can check whether the task is alive and get mm pointer in teardown path as below:

Or you could just reference and use @mm as KVM and others do. Or there is anything else you need from @current than just @mm?

Quoted 17 lines
{
        struct task_struct *task = domain->external_addr_space->task;
        struct mm_struct *mm = NULL;

        put_pfn(pfn, prot);

        if (pid_alive(task))
                mm = get_task_mm(task);

        if (mm) {
                if (do_accounting)
                        vfio_lock_acct(task, -1);

                mmput(mm);
        }
}
Signature · 1 line
Alexey
#38 lore raw

Re: [Qemu-devel] [PATCH v9 04/12] vfio iommu: Add support for mediated devices

Quoted 58 lines · Alexey Kardashevskiy
Quoted 57 lines · Kirti Wankhede
Quoted 53 lines · Alexey Kardashevskiy
Quoted 40 lines · Kirti Wankhede
Quoted 37 lines · Alexey Kardashevskiy
Quoted 35 lines · Kirti Wankhede
Quoted 21 lines · Alexey Kardashevskiy
Quoted 14 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 module

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.

Quoted 4 lines

This change adds pin and unpin support for mediated device to TYPE1 IOMMU backend module. More details:
- When iommu_group of mediated devices is attached, task structure is cached which is used later to pin pages and page accounting.

Quoted 7 lines

For SPAPR TCE IOMMU driver, I ended up caching mm_struct with atomic_inc(&container->mm->mm_count) (patches are on the way) instead of using @current or task as the process might be gone while VFIO container is still alive and @mm might be needed to do proper cleanup; this might not be an issue with this patchset now but still you seem to only use @mm from task_struct.

Consider the example of QEMU process which creates VFIO container, QEMU in its teardown path would release the container. How could container be alive when process is gone?

Quoted 8 lines
Quoted 4 lines

do_exit() in kernel/exit.c calls exit_mm() (which sets NULL to tsk->mm) first, and then releases open files by calling exit_files(). So container's release() does not have current->mm.

Incrementing usage count (get_task_struct()) while saving task structure and decementing it (put_task_struct()) from release() should work here. Updating the patch.

I cannot see how the task->usage counter prevents do_exit() from performing the exit, can you?

Quoted 8 lines
Quoted 3 lines

It will not prevent exit from do_exit(), but that will make sure that we don't have stale pointer of task structure. Then we can check whether the task is alive and get mm pointer in teardown path as below:

Or you could just reference and use @mm as KVM and others do. Or there is anything else you need from @current than just @mm?

I agree. If @mm is the only thing needed, there is really no reason to refer to the @task :-)

--
Thanks,
Jike

author maintainer
#39 lore raw

Re: [Qemu-devel] [PATCH v9 04/12] vfio iommu: Add support for mediated devices

Quoted 64 lines · Jike Song
Quoted 63 lines · Alexey Kardashevskiy
Quoted 60 lines · Kirti Wankhede
Quoted 46 lines · Alexey Kardashevskiy
Quoted 45 lines · Kirti Wankhede
Quoted 41 lines · Alexey Kardashevskiy
Quoted 32 lines · Kirti Wankhede
Quoted 21 lines · Alexey Kardashevskiy
Quoted 14 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 module

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.

Quoted 4 lines

This change adds pin and unpin support for mediated device to TYPE1 IOMMU backend module. More details:
- When iommu_group of mediated devices is attached, task structure is cached which is used later to pin pages and page accounting.

Quoted 7 lines

For SPAPR TCE IOMMU driver, I ended up caching mm_struct with atomic_inc(&container->mm->mm_count) (patches are on the way) instead of using @current or task as the process might be gone while VFIO container is still alive and @mm might be needed to do proper cleanup; this might not be an issue with this patchset now but still you seem to only use @mm from task_struct.

Quoted 3 lines

Consider the example of QEMU process which creates VFIO container, QEMU in its teardown path would release the container. How could container be alive when process is gone?

do_exit() in kernel/exit.c calls exit_mm() (which sets NULL to tsk->mm) first, and then releases open files by calling exit_files(). So container's release() does not have current->mm.

Quoted 7 lines
Quoted 3 lines

Incrementing usage count (get_task_struct()) while saving task structure and decementing it (put_task_struct()) from release() should work here. Updating the patch.

I cannot see how the task->usage counter prevents do_exit() from performing the exit, can you?

It will not prevent exit from do_exit(), but that will make sure that we don't have stale pointer of task structure. Then we can check whether the task is alive and get mm pointer in teardown path as below:

Quoted 7 lines
Quoted 3 lines

Or you could just reference and use @mm as KVM and others do. Or there is anything else you need from @current than just @mm?

I agree. If @mm is the only thing needed, there is really no reason to refer to the @task :-)

In vfio_lock_acct(), that is for page accounting, if mm->mmap_sem is already held then page accounting is deferred, where task structure is used to get mm and work is deferred only if mm exist:

	mm = get_task_mm(task);

That is where this module need task structure.

Thanks,
Kirti

#40 lore raw

Re: [Qemu-devel] [PATCH v9 04/12] vfio iommu: Add support for mediated devices

Quoted 10 lines · Kirti Wankhede
Quoted 8 lines · Jike Song
Quoted 3 lines · Alexey Kardashevskiy

Or you could just reference and use @mm as KVM and others do. Or there is anything else you need from @current than just @mm?

I agree. If @mm is the only thing needed, there is really no reason to refer to the @task :-)

Quoted 6 lines

In vfio_lock_acct(), that is for page accounting, if mm->mmap_sem is already held then page accounting is deferred, where task structure is used to get mm and work is deferred only if mm exist:

	mm = get_task_mm(task);

That is where this module need task structure.

Kirti,

By calling get_task_mm you hold a ref on @mm and save it in iommu, whenever you want to do something like vfio_lock_acct(), use that mm (as you said, if mmap_sem not accessible then defer it to a work, but still @mm is the whole information), and put it after the usage.

I still can't see any reason that the @task have to be saved. It's always the @mm all the time. Did I miss anything?

--
Thanks,
Jike

author maintainer
#41 lore raw

Re: [Qemu-devel] [PATCH v9 04/12] vfio iommu: Add support for mediated devices

Quoted 20 lines · Jike Song
Quoted 10 lines · Kirti Wankhede
Quoted 8 lines · Jike Song
Quoted 3 lines · Alexey Kardashevskiy

Or you could just reference and use @mm as KVM and others do. Or there is anything else you need from @current than just @mm?

I agree. If @mm is the only thing needed, there is really no reason to refer to the @task :-)

Quoted 6 lines

In vfio_lock_acct(), that is for page accounting, if mm->mmap_sem is already held then page accounting is deferred, where task structure is used to get mm and work is deferred only if mm exist:

	mm = get_task_mm(task);

That is where this module need task structure.

Kirti,

Quoted 8 lines

By calling get_task_mm you hold a ref on @mm and save it in iommu, whenever you want to do something like vfio_lock_acct(), use that mm (as you said, if mmap_sem not accessible then defer it to a work, but still @mm is the whole information), and put it after the usage.

I still can't see any reason that the @task have to be saved. It's always the @mm all the time. Did I miss anything?

If the process is terminated by SIGKILL, as Alexey mentioned in this mail thread earlier exit_mm() is called first and then all files are closed. From exit_mm(), task->mm is set to NULL. So from teardown path, we should call get_task_mm(task) to get current status intsead of using stale pointer.

Thanks,
Kirti.

#42 lore raw

Re: [Qemu-devel] [PATCH v9 04/12] vfio iommu: Add support for mediated devices

Quoted 30 lines · Kirti Wankhede
Quoted 20 lines · Jike Song
Quoted 10 lines · Kirti Wankhede
Quoted 8 lines · Jike Song
Quoted 3 lines · Alexey Kardashevskiy

Or you could just reference and use @mm as KVM and others do. Or there is anything else you need from @current than just @mm?

I agree. If @mm is the only thing needed, there is really no reason to refer to the @task :-)

Quoted 6 lines

In vfio_lock_acct(), that is for page accounting, if mm->mmap_sem is already held then page accounting is deferred, where task structure is used to get mm and work is deferred only if mm exist:

	mm = get_task_mm(task);

That is where this module need task structure.

Kirti,

Quoted 8 lines

By calling get_task_mm you hold a ref on @mm and save it in iommu, whenever you want to do something like vfio_lock_acct(), use that mm (as you said, if mmap_sem not accessible then defer it to a work, but still @mm is the whole information), and put it after the usage.

I still can't see any reason that the @task have to be saved. It's always the @mm all the time. Did I miss anything?

Quoted 5 lines

If the process is terminated by SIGKILL, as Alexey mentioned in this mail thread earlier exit_mm() is called first and then all files are closed. From exit_mm(), task->mm is set to NULL. So from teardown path, we should call get_task_mm(task) to get current status intsead of using stale pointer.

You have got the ref on a task->mm and stored it somewhere, then after that at some time the task->mm was set to NULL -- what's exactly the problem here? It's perfectly okay per my understanding ...

--
Thanks,
Jike

#43 lore raw

Re: [Qemu-devel] [PATCH v9 04/12] vfio iommu: Add support for mediated devices

In reply to Kirti Wankhede
Quoted 10 lines · Kirti Wankhede
Quoted 7 lines · Jike Song
Quoted 6 lines · Kirti Wankhede
Quoted 5 lines · Jike Song
Quoted 3 lines · Alexey Kardashevskiy

Or you could just reference and use @mm as KVM and others do. Or there is anything else you need from @current than just @mm?

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines

I agree. If @mm is the only thing needed, there is really no reason to refer to the @task :-)

In vfio_lock_acct(), that is for page accounting, if mm->mmap_sem is already held then page accounting is deferred, where task structure is used to get mm and work is deferred only if mm exist:

	mm = get_task_mm(task);

get_task_mm() increments mm_users which is basically a number of userspaces holding the reference to mm. As this case it is not a userspace, mm_count needs to be incremented imho.

Quoted 10 lines
Quoted 10 lines
Quoted 2 lines

That is where this module need task structure.

Kirti,

By calling get_task_mm you hold a ref on @mm and save it in iommu, whenever you want to do something like vfio_lock_acct(), use that mm (as you said, if mmap_sem not accessible then defer it to a work, but still @mm is the whole information), and put it after the usage.

Quoted 8 lines
Quoted 3 lines

I still can't see any reason that the @task have to be saved. It's always the @mm all the time. Did I miss anything?

If the process is terminated by SIGKILL, as Alexey mentioned in this mail thread earlier exit_mm() is called first and then all files are closed. From exit_mm(), task->mm is set to NULL. So from teardown path, we should call get_task_mm(task)

... which will return NULL, no?

Quoted 2 lines

to get current status intsead of using
stale pointer.

If you increment either mm_users or mm_count at the exact place where you want to cache task pointer, why would mm pointer become stale until you do mmdrop() or mmput()?

Signature · 1 line
Alexey
#72 lore raw
In reply to Jike Song

Hi,

Quoted 7 lines

Just want to share that we have published a KVMGT implementation based on this v9 patchset, to:

https://github.com/01org/gvt-linux/tree/gvt-next-kvmgt

It doesn't utilize common routines introduced by 05+ patches yet. The complete intel vGPU device-model is contained.

Tried to use this implementation. Used the topic/gvt-next-kvmgt-mdev-2016-11-18 branch which looked like the most recent one. Setup:

* Everything compiled as modules.
* iommu turned off for the igd (intel_iommu=on,igfx_off).
* Blacklisted i915 so dracut initrd doesn't load it

    (rd.driver.blacklist=i915)

* tweaked module config so kvmgt is loaded before i915, also enable gvt:

# cat /etc/modprobe.d/kraxel-gvt.conf
options i915 enable_gvt=1
softdep i915 pre: kvmgt

Everything seems to load fine. Sysfs files are there, and I can create vgpus.

Trying to assign a vgpu this way:

-device vfio-pci,sysfsdev=/sys/class/mdev_bus/0000:00:02.0/<uuid>

fails though and gives this message in the kernel log:

  [  402.560350] [drm:intel_vgpu_open [kvmgt]] *ERROR* gvt: KVM is

required to use Intel vGPU

Trying the same with a mtty sample device works and I can see the pci serial device in the guest.

Any clues what is going wrong?

Has this version any support for exporting the guest display as dma-buf, so qemu can show it? Or is this a headless vgpu?

thanks,
Gerd

#73 lore raw
Quoted 5 lines · Gerd Hoffmann

Hi,

Quoted 3 lines

Just want to share that we have published a KVMGT implementation based on this v9 patchset, to:

Quoted 9 lines
Quoted 4 lines

https://github.com/01org/gvt-linux/tree/gvt-next-kvmgt

It doesn't utilize common routines introduced by 05+ patches yet. The complete intel vGPU device-model is contained.

Tried to use this implementation. Used the topic/gvt-next-kvmgt-mdev-2016-11-18 branch which looked like the most recent one. Setup:

Hi Gerd,

We didn't catch up with updating the newest kvmgt code accordingly, partly because we are preparing the 'final' version to be upstreamed.

Will update a topic/gvt-next-kvmgt-2016-12-06 today, sorry for the inconvenience :)

Quoted 7 lines

* Everything compiled as modules.
* iommu turned off for the igd (intel_iommu=on,igfx_off).
* Blacklisted i915 so dracut initrd doesn't load it

    (rd.driver.blacklist=i915)

* tweaked module config so kvmgt is loaded before i915, also enable gvt:

Quoted 7 lines

# cat /etc/modprobe.d/kraxel-gvt.conf
options i915 enable_gvt=1
softdep i915 pre: kvmgt

Everything seems to load fine. Sysfs files are there, and I can create vgpus.

Yes, everything looks good so far.

Quoted 6 lines

Trying to assign a vgpu this way:

-device vfio-pci,sysfsdev=/sys/class/mdev_bus/0000:00:02.0/<uuid>

fails though and gives this message in the kernel log:

Quoted 7 lines
  [  402.560350] [drm:intel_vgpu_open [kvmgt]] *ERROR* gvt: KVM is

required to use Intel vGPU

Trying the same with a mtty sample device works and I can see the pci serial device in the guest.

Any clues what is going wrong?

The getting kvm instance code is missing in that branch, will be contained in the new one.

Quoted 2 lines

Has this version any support for exporting the guest display as dma-buf, so qemu can show it? Or is this a headless vgpu?

No, this version doesn't have dma-buf support yet, we were using x11vnc in guest to test it internally. I'll include you in the igvt-g-dev mailing list for further discussion :)

--
Thanks,
Jike

#74 lore raw

Hi,

Quoted 2 lines

Will update a topic/gvt-next-kvmgt-2016-12-06 today, sorry for the inconvenience :)

Thanks, that brings us one step forward.

Linux guest can see the device (in lspci). Trying to load i915.ko leads to kernel oopses on both guest and host though. So I guess the guest driver can't handle the device.

I've tried RHEL-7.3, kernel 3.10.0-514.el7.x86_64. The drivers/gpu/drm/ subsystem is roughly at upstream kernel v4.6 level. Is that too old? IIRC some vgpu guest patches have been merged in v4.2 timeframe, so I kind-of expected this to work ...

I'll go try the kvmgt branch in the guest next.

What is the upstream merge status btw? As far I know the bulk of the gvt code is scheduled for 4.10. What about the gvt/mdev plumbing? Will that land in 4.10 too or is that planned for 4.11?

thanks,
Gerd