Add Mediated device support
Subsystems (5)
Kirti Wankhede
maintainerv10 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 v10?
vfio:
- Split commits in multple individual commits.
- Removed the function added in v9 to get device_api string.
- Defined constant strings in include/uapi/linux/vfio.h that should be used by vendor driver for device_api attribute.
vfio_iommu_type1:
- Fixed accounting when pass through device is unplugged while mdev device exist in a domain.
- Added blocking notifier to notify DMA_UNMAP to vendor driver to invalidate mappings.
- Exported APIs to register notifier for DMA_UNMAP action.
Documentation:
- Added sysfs ABI for mediated device framework.
- Updated Documentation/vfio-mdev/vfio-mediated-device.txt.
- Updated mtty.c with bug fixes.
Kirti Wankhede (19):
vfio: Mediated device Core driver
vfio: VFIO based driver for Mediated devices
vfio: Rearrange functions to get vfio_group from dev
vfio: Common function to increment container_users
vfio iommu: Added pin and unpin callback functions to
vfio_iommu_driver_ops
vfio iommu type1: Update arguments of vfio_lock_acct vfio iommu type1: Update argument of vaddr_get_pfn() vfio iommu type1: Add find_iommu_group() function
vfio iommu type1: Add support for mediated devices
vfio iommu: Add blocking notifier to notify DMA_UNMAP
vfio: Introduce common function to add capabilities
vfio_pci: Update vfio_pci to use vfio_info_add_capability()
vfio: Introduce vfio_set_irqs_validate_and_prepare()
vfio_pci: Updated to use vfio_set_irqs_validate_and_prepare()
vfio_platform: Updated to use vfio_set_irqs_validate_and_prepare()
vfio: Define device_api strings
docs: Add Documentation for Mediated devices
docs: Sysfs ABI for mediated device framework
docs: Sample driver to demonstrate how to use Mediated device
framework.
Documentation/ABI/testing/sysfs-bus-vfio-mdev | 111 ++ Documentation/vfio-mdev/Makefile | 13 + Documentation/vfio-mdev/mtty.c | 1503 ++++++++++++++++++++++ Documentation/vfio-mdev/vfio-mediated-device.txt | 398 ++++++ drivers/vfio/Kconfig | 1 + drivers/vfio/Makefile | 1 + drivers/vfio/mdev/Kconfig | 17 + drivers/vfio/mdev/Makefile | 5 + drivers/vfio/mdev/mdev_core.c | 384 ++++++ drivers/vfio/mdev/mdev_driver.c | 122 ++ drivers/vfio/mdev/mdev_private.h | 41 + drivers/vfio/mdev/mdev_sysfs.c | 286 ++++ drivers/vfio/mdev/vfio_mdev.c | 148 +++ drivers/vfio/pci/vfio_pci.c | 78 +- drivers/vfio/platform/vfio_platform_common.c | 31 +- drivers/vfio/vfio.c | 322 ++++- drivers/vfio/vfio_iommu_type1.c | 808 ++++++++++-- include/linux/mdev.h | 167 +++ include/linux/vfio.h | 30 +- include/uapi/linux/vfio.h | 10 + 20 files changed, 4270 insertions(+), 206 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-bus-vfio-mdev 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
Kirti Wankhede
maintainerPATCH v10 1/19 vfio: Mediated device Core driver
Design for Mediated Device Driver:
Main purpose of this driver is to provide a common interface for mediated device management that can be used by different drivers of different devices.
This module provides a generic interface to create the device, add it to mediated bus, add device to IOMMU group and then add it to vfio group.
Below is the high Level block diagram, with Nvidia, Intel and IBM devices as example, since these are the devices which are going to actively use this module as of now.
+---------------+ | | | +-----------+ | mdev_register_driver() +--------------+ | | | +<------------------------+ __init() | | | mdev | | | | | | bus | +------------------------>+ |<-> VFIO user | | driver | | probe()/remove() | vfio_mdev.ko | APIs | | | | | | | +-----------+ | +--------------+ | | | MDEV CORE | | MODULE | | mdev.ko | | +-----------+ | mdev_register_device() +--------------+ | | | +<------------------------+ | | | | | | nvidia.ko |<-> physical | | | +------------------------>+ | device | | | | callback +--------------+ | | Physical | | | | device | | mdev_register_device() +--------------+ | | interface | |<------------------------+ | | | | | | i915.ko |<-> physical | | | +------------------------>+ | device | | | | callback +--------------+ | | | | | | | | mdev_register_device() +--------------+ | | | +<------------------------+ | | | | | | ccw_device.ko|<-> physical | | | +------------------------>+ | device | | | | callback +--------------+ | +-----------+ | +---------------+
Core driver provides two types of registration interfaces:
1. Registration interface for mediated bus driver:
/**
* struct mdev_driver - Mediated device's driver
* @name: driver name
* @probe: called when new device created
* @remove:called when device removed
* @driver:device driver structure
*
**/
struct mdev_driver {
const char *name;
int (*probe) (struct device *dev);
void (*remove) (struct device *dev);
struct device_driver driver;
};Mediated bus driver for mdev device should use this interface to register and unregister with core driver respectively:
int mdev_register_driver(struct mdev_driver *drv, struct module *owner); void mdev_unregister_driver(struct mdev_driver *drv);
Mediated bus driver is responsible to add/delete mediated devices to/from VFIO group when devices are bound and unbound to the driver.
2. Physical device driver interface
This interface provides vendor driver the set APIs to manage physical device related work in its driver. APIs are :
* dev_attr_groups: attributes of the parent device.
* mdev_attr_groups: attributes of the mediated device.
* supported_type_groups: attributes to define supported type. This is mandatory field.
* create: to allocate basic resources in vendor driver for a mediated device. This is mandatory to be provided by vendor driver.
* remove: to free resources in vendor driver when mediated device is destroyed. This is mandatory to be provided by vendor driver.
* open: open callback of mediated device
* release: release callback of mediated device
* read : read emulation callback.
* write: write emulation callback.
* 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.
--- drivers/vfio/Kconfig | 1 + drivers/vfio/Makefile | 1 + drivers/vfio/mdev/Kconfig | 10 + drivers/vfio/mdev/Makefile | 4 + drivers/vfio/mdev/mdev_core.c | 384 +++++++++++++++++++++++++++++++++++++++ drivers/vfio/mdev/mdev_driver.c | 122 +++++++++++++ drivers/vfio/mdev/mdev_private.h | 41 +++++ drivers/vfio/mdev/mdev_sysfs.c | 286 +++++++++++++++++++++++++++++ include/linux/mdev.h | 167 +++++++++++++++++ 9 files changed, 1016 insertions(+) create mode 100644 drivers/vfio/mdev/Kconfig create mode 100644 drivers/vfio/mdev/Makefile create mode 100644 drivers/vfio/mdev/mdev_core.c create mode 100644 drivers/vfio/mdev/mdev_driver.c create mode 100644 drivers/vfio/mdev/mdev_private.h create mode 100644 drivers/vfio/mdev/mdev_sysfs.c create mode 100644 include/linux/mdev.h
drivers/vfio/Kconfig +1 -0
@@ -48,4 +48,5 @@ menuconfig VFIO_NOIOMMU source "drivers/vfio/pci/Kconfig" source "drivers/vfio/platform/Kconfig" +source "drivers/vfio/mdev/Kconfig" source "virt/lib/Kconfig"
drivers/vfio/Makefile +1 -0
@@ -7,3 +7,4 @@ obj-$(CONFIG_VFIO_IOMMU_SPAPR_TCE) += vfio_iommu_spapr_tce.o obj-$(CONFIG_VFIO_SPAPR_EEH) += vfio_spapr_eeh.o obj-$(CONFIG_VFIO_PCI) += pci/ obj-$(CONFIG_VFIO_PLATFORM) += platform/ +obj-$(CONFIG_VFIO_MDEV) += mdev/
drivers/vfio/mdev/Kconfig new file +10 -0
@@ -0,0 +1,10 @@ + +config VFIO_MDEV + tristate "Mediated device driver framework" + depends on VFIO + default n + help + Provides a framework to virtualize devices. + See Documentation/vfio-mdev/vfio-mediated-device.txt for more details. + + If you don't know what do here, say N.
drivers/vfio/mdev/Makefile new file +4 -0
@@ -0,0 +1,4 @@ + +mdev-y := mdev_core.o mdev_sysfs.o mdev_driver.o + +obj-$(CONFIG_VFIO_MDEV) += mdev.o
drivers/vfio/mdev/mdev_core.c new file +384 -0
384 lines not on this page · raw message
drivers/vfio/mdev/mdev_driver.c new file +122 -0
122 lines not on this page · raw message
drivers/vfio/mdev/mdev_private.h new file +41 -0
41 lines not on this page · raw message
drivers/vfio/mdev/mdev_sysfs.c new file +286 -0
286 lines not on this page · raw message
include/linux/mdev.h new file +167 -0
167 lines not on this page · raw message
Signature · 1 line
2.7.0
Re: [PATCH v10 01/19] vfio: Mediated device Core driver
Quoted 35 lines · Kirti Wankhede
drivers/vfio/mdev/mdev_core.c new file +35
-0+int mdev_register_device (struct device *dev,const struct parent_ops *ops) +{ +int ret; +struct parent_device *parent; + +/* check for mandatory ops */ +if (!ops || !ops->create || !ops->remove || !ops->supported_type_groups) +return -EINVAL ; + + dev =get_device (dev); +if (!dev) +return -EINVAL ; + +mutex_lock (&parent_list_lock); + +/* Check for duplicate */ + parent =__find_parent_device (dev); +if (parent) { + ret = -EEXIST ; + goto add_dev_err; + } + + parent =kzalloc (sizeof (*parent),GFP_KERNEL ); +if (!parent) { + ret = -ENOMEM ; + goto add_dev_err; + } + +kref_init (&parent->ref); +mutex_init (&parent->lock); + + parent->dev = dev; + parent->ops = ops; + + ret =parent_create_sysfs_files (parent);
drivers/vfio/mdev/mdev_core.c line 200
Quoted 10 lines
drivers/vfio/mdev/mdev_core.c new file +10
-0+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" ); +
Hi Kirti,
Like I replied to previous version:
http://www.spinics.net/lists/kvm/msg139331.html
You can always check if mdev_bus_compat_class already registered here, and register it if not yet. Same logic should be adopted to mdev_init.
Current implementation will simply panic if configured as builtin, which is rare but far from impossible.
--
Thanks,
Jike
Quoted 176 lines
drivers/vfio/mdev/mdev_core.c new file +176
-0+list_add (&parent->next, &parent_list); +mutex_unlock (&parent_list_lock); + +dev_info (dev,"MDEV: Registered\n" ); +return 0 ; + +add_dev_err: +mutex_unlock (&parent_list_lock); +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" ); + +list_del (&parent->next); +class_compat_remove_link (mdev_bus_compat_class, dev,NULL ); + +device_for_each_child (dev, (void *)&force_remove, + mdev_device_remove_cb); + +parent_remove_sysfs_files (parent); + +mutex_unlock (&parent_list_lock); +mdev_put_parent (parent); +} +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 ; + +mutex_lock (&parent->lock); + +/* Check for duplicate */ +if (mdev_device_exist (parent, uuid)) { + 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" ); + +mutex_unlock (&parent->lock); +return ret; + +create_failed: +device_unregister (&mdev->dev); + +create_err: +mutex_unlock (&parent->lock); +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; + + mdev =to_mdev_device (dev); + type =to_mdev_type (mdev->type_kobj); + parent = mdev->parent; +mutex_lock (&parent->lock); + + ret =mdev_device_remove_ops (mdev, force_remove); +if (ret) { +mutex_unlock (&parent->lock); +return ret; + } + +mdev_remove_sysfs_files (dev, type); +device_unregister (dev); +mutex_unlock (&parent->lock); +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 (); +}
Kirti Wankhede
maintainerRe: [PATCH v10 01/19] vfio: Mediated device Core driver
Quoted 49 lines · Jike Song
Quoted 35 lines · Kirti Wankhede
(quoted diff) +35
-0+int mdev_register_device(struct device *dev, const struct parent_ops *ops) +{ + int ret; + struct parent_device *parent; + + /* check for mandatory ops */ + if (!ops || !ops->create || !ops->remove || !ops->supported_type_groups) + return -EINVAL; + + dev = get_device(dev); + if (!dev) + return -EINVAL; + + mutex_lock(&parent_list_lock); + + /* Check for duplicate */ + parent = __find_parent_device(dev); + if (parent) { + ret = -EEXIST; + goto add_dev_err; + } + + parent = kzalloc(sizeof(*parent), GFP_KERNEL); + if (!parent) { + ret = -ENOMEM; + goto add_dev_err; + } + + kref_init(&parent->ref); + mutex_init(&parent->lock); + + parent->dev = dev; + parent->ops = ops; + + ret = parent_create_sysfs_files(parent);Review
Quoted 10 lines
(quoted diff) +10
-0+ 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"); +Hi Kirti,
Quoted 4 lines
Like I replied to previous version:
http://www.spinics.net/lists/kvm/msg139331.html
Hi Jike,
I saw your reply but by that time v10 version of patch series was out for review.
Quoted 7 lines
You can always check if
mdev_bus_compat_classalready registered here, and register it if not yet. Same logic should be adopted tomdev_init.Current implementation will simply panic if configured as builtin, which is rare but far from impossible.
Can you verify attached patch with v10 patch-set whether this works for you? I'll incorporate this change in my next version.
Thanks,
Kirti
From: Kirti Wankhede <[email protected]>
Date: Sat, 29 Oct 2016 15:12:01 +0530
Subject: [PATCH 1/1] Register mdev_bus class on first mdev_device_register
--- drivers/vfio/mdev/mdev_core.c | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-)
drivers/vfio/mdev/mdev_core.c +17 -13
@@ -187,13 +187,18 @@ int mdev_register_device(struct device *dev, const struct parent_ops *ops) 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; +if (!mdev_bus_compat_class) { + mdev_bus_compat_class =class_compat_register ("mdev_bus" ); +if (!mdev_bus_compat_class) { + ret = -ENOMEM ; + goto add_dev_err; + } } + ret =parent_create_sysfs_files (parent); +if (ret) + goto add_dev_err; + ret =class_compat_create_link (mdev_bus_compat_class, dev,NULL );if (ret)dev_warn (dev,"Failed to create compatibility class link\n" );
@@ -206,7 +211,10 @@ int mdev_register_device(struct device *dev, const struct parent_ops *ops) add_dev_err:mutex_unlock (&parent_list_lock); -put_device (dev); +if (parent) +mdev_put_parent (parent); +else +put_device (dev);return ret; }EXPORT_SYMBOL (mdev_register_device);
@@ -354,12 +362,6 @@ static int __init mdev_init(void)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.
@@ -371,7 +373,9 @@ static int __init mdev_init(void) static void __exit mdev_exit(void) { -class_compat_unregister (mdev_bus_compat_class); +if (mdev_bus_compat_class) +class_compat_unregister (mdev_bus_compat_class); +mdev_bus_unregister (); }
Signature · 1 line
2.7.0
Re: [PATCH v10 01/19] vfio: Mediated device Core driver
Quoted 54 lines · Kirti Wankhede
Quoted 51 lines · Jike Song
Quoted 35 lines · Kirti Wankhede
(quoted diff) +35
-0+int mdev_register_device(struct device *dev, const struct parent_ops *ops) +{ + int ret; + struct parent_device *parent; + + /* check for mandatory ops */ + if (!ops || !ops->create || !ops->remove || !ops->supported_type_groups) + return -EINVAL; + + dev = get_device(dev); + if (!dev) + return -EINVAL; + + mutex_lock(&parent_list_lock); + + /* Check for duplicate */ + parent = __find_parent_device(dev); + if (parent) { + ret = -EEXIST; + goto add_dev_err; + } + + parent = kzalloc(sizeof(*parent), GFP_KERNEL); + if (!parent) { + ret = -ENOMEM; + goto add_dev_err; + } + + kref_init(&parent->ref); + mutex_init(&parent->lock); + + parent->dev = dev; + parent->ops = ops; + + ret = parent_create_sysfs_files(parent);Review
Quoted 10 lines
(quoted diff) +10
-0+ 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"); +Hi Kirti,
Like I replied to previous version:
Quoted 8 lines
Quoted 2 lines
http://www.spinics.net/lists/kvm/msg139331.htmlHi Jike,
I saw your reply but by that time v10 version of patch series was out for review.
Ah..yes, I forgot that :)
Quoted 11 lines
Quoted 7 lines
You can always check if
mdev_bus_compat_classalready registered here, and register it if not yet. Same logic should be adopted tomdev_init.Current implementation will simply panic if configured as builtin, which is rare but far from impossible.
Can you verify attached patch with v10 patch-set whether this works for you? I'll incorporate this change in my next version.
Seems cool. But would you please also keep the register in mdev_init(), just check the 'in case it was already registered' case? Thanks!
--
Thanks,
Jike
Kirti Wankhede
maintainerRe: [PATCH v10 01/19] vfio: Mediated device Core driver
Quoted 74 lines · Jike Song
Quoted 54 lines · Kirti Wankhede
Quoted 51 lines · Jike Song
Quoted 35 lines · Kirti Wankhede
(quoted diff) +35
-0+int mdev_register_device(struct device *dev, const struct parent_ops *ops) +{ + int ret; + struct parent_device *parent; + + /* check for mandatory ops */ + if (!ops || !ops->create || !ops->remove || !ops->supported_type_groups) + return -EINVAL; + + dev = get_device(dev); + if (!dev) + return -EINVAL; + + mutex_lock(&parent_list_lock); + + /* Check for duplicate */ + parent = __find_parent_device(dev); + if (parent) { + ret = -EEXIST; + goto add_dev_err; + } + + parent = kzalloc(sizeof(*parent), GFP_KERNEL); + if (!parent) { + ret = -ENOMEM; + goto add_dev_err; + } + + kref_init(&parent->ref); + mutex_init(&parent->lock); + + parent->dev = dev; + parent->ops = ops; + + ret = parent_create_sysfs_files(parent);Review
Quoted 10 lines
(quoted diff) +10
-0+ 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"); +Hi Kirti,
Like I replied to previous version:
Quoted 8 lines
Quoted 2 lines
http://www.spinics.net/lists/kvm/msg139331.htmlHi Jike,
I saw your reply but by that time v10 version of patch series was out for review.
Ah..yes, I forgot that :)
Quoted 8 lines
Quoted 7 lines
You can always check if
mdev_bus_compat_classalready registered here, and register it if not yet. Same logic should be adopted tomdev_init.Current implementation will simply panic if configured as builtin, which is rare but far from impossible.
Quoted 7 lines
Quoted 3 lines
Can you verify attached patch with v10 patch-set whether this works for you? I'll incorporate this change in my next version.
Seems cool. But would you please also keep the register in
mdev_init(), just check the 'in case it was already registered' case? Thanks!
The class is used only to keep symbolic to the devices which are registered to mdev framework. So I think its ok to register this class when first device is registered.
Thanks,
Kirti
Jike Song
Reviewed-byRe: [Qemu-devel] [PATCH v10 01/19] vfio: Mediated device Core driver
Quoted 79 lines · Kirti Wankhede
Quoted 78 lines · Jike Song
Quoted 54 lines · Kirti Wankhede
Quoted 51 lines · Jike Song
Quoted 35 lines · Kirti Wankhede
(quoted diff) +35
-0+int mdev_register_device(struct device *dev, const struct parent_ops *ops) +{ + int ret; + struct parent_device *parent; + + /* check for mandatory ops */ + if (!ops || !ops->create || !ops->remove || !ops->supported_type_groups) + return -EINVAL; + + dev = get_device(dev); + if (!dev) + return -EINVAL; + + mutex_lock(&parent_list_lock); + + /* Check for duplicate */ + parent = __find_parent_device(dev); + if (parent) { + ret = -EEXIST; + goto add_dev_err; + } + + parent = kzalloc(sizeof(*parent), GFP_KERNEL); + if (!parent) { + ret = -ENOMEM; + goto add_dev_err; + } + + kref_init(&parent->ref); + mutex_init(&parent->lock); + + parent->dev = dev; + parent->ops = ops; + + ret = parent_create_sysfs_files(parent);Review
Quoted 10 lines
(quoted diff) +10
-0+ 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"); +Hi Kirti,
Like I replied to previous version:
Quoted 8 lines
Quoted 2 lines
http://www.spinics.net/lists/kvm/msg139331.htmlHi Jike,
I saw your reply but by that time v10 version of patch series was out for review.
Ah..yes, I forgot that :)
Quoted 11 lines
Quoted 7 lines
You can always check if
mdev_bus_compat_classalready registered here, and register it if not yet. Same logic should be adopted tomdev_init.Current implementation will simply panic if configured as builtin, which is rare but far from impossible.
Can you verify attached patch with v10 patch-set whether this works for you? I'll incorporate this change in my next version.
Quoted 8 lines
Quoted 3 lines
Seems cool. But would you please also keep the register in
mdev_init(), just check the 'in case it was already registered' case? Thanks!The class is used only to keep symbolic to the devices which are registered to mdev framework. So I think its ok to register this class when first device is registered.
That's also cool :-)
So if you like it:
--
Thanks,
Jike
Re: [PATCH v10 01/19] vfio: Mediated device Core driver
In reply to Kirti WankhedeQuoted 94 lines · Kirti Wankhede
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:/**
* structmdev_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); voidmdev_unregister_driver(struct mdev_driver *drv);Mediated bus driver is responsible to add/delete mediated devices to/from VFIO group when devices are bound and unbound to the driver.
2. Physical device driver interface
This interface provides vendor driver the set APIs to manage physical device related work in its driver. APIs are :*
dev_attr_groups: attributes of the parent device.
*mdev_attr_groups: attributes of the mediated device.
*supported_type_groups: attributes to define supported type. This is mandatory field.
* create: to allocate basic resources in vendor driver for a mediated device. This is mandatory to be provided by vendor driver.
* remove: to free resources in vendor driver when mediated device is destroyed. This is mandatory to be provided by vendor driver.
* open: open callback of mediated device
* release: release callback of mediated device
* read : read emulation callback.
* write: write emulation callback.
* mmap: mmap emulation callback.
* ioctl: ioctl callback.Drivers should use these interfaces to register and unregister device to mdev core driver respectively:
Quoted 7 lines
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_mdevdriver. If required, vendor driver can have locks to serialize above APIs in their driver.
Maybe some information above could be placed under Documentation instead? It's kind of weird to have the block diagram and interfaces documentation in the git commit message :-)
Quoted 114 lines
Signed-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id I73a5084574270b14541c529461ea2f03c292d510--- drivers/vfio/Kconfig | 1 + drivers/vfio/Makefile | 1 + drivers/vfio/mdev/Kconfig | 10 + drivers/vfio/mdev/Makefile | 4 + drivers/vfio/mdev/mdev_core.c | 384 +++++++++++++++++++++++++++++++++++++++ drivers/vfio/mdev/mdev_driver.c | 122 +++++++++++++ drivers/vfio/mdev/mdev_private.h | 41 +++++ drivers/vfio/mdev/mdev_sysfs.c | 286 +++++++++++++++++++++++++++++ include/linux/mdev.h | 167 +++++++++++++++++ 9 files changed, 1016 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.hdrivers/vfio/Kconfig +1
-0@@ -48,4 +48,5 @@ menuconfig VFIO_NOIOMMU source "drivers/vfio/pci/Kconfig" source "drivers/vfio/platform/Kconfig" +source "drivers/vfio/mdev/Kconfig" source "virt/lib/Kconfig"drivers/vfio/Makefile +1
-0@@ -7,3 +7,4 @@ obj-$(CONFIG_VFIO_IOMMU_SPAPR_TCE) += vfio_iommu_spapr_tce.o obj-$(CONFIG_VFIO_SPAPR_EEH) += vfio_spapr_eeh.o obj-$(CONFIG_VFIO_PCI) += pci/ obj-$(CONFIG_VFIO_PLATFORM) += platform/ +obj-$(CONFIG_VFIO_MDEV) += mdev/drivers/vfio/mdev/Kconfig new file +10
-0@@ -0,0 +1,10 @@ + +config VFIO_MDEV + tristate "Mediated device driver framework" + depends on VFIO + default n + help + Provides a framework to virtualize devices. + See Documentation/vfio-mdev/vfio-mediated-device.txt for more details. + + If you don't know what do here, say N.drivers/vfio/mdev/Makefile new file +4
-0@@ -0,0 +1,4 @@ + +mdev-y := mdev_core.o mdev_sysfs.o mdev_driver.o + +obj-$(CONFIG_VFIO_MDEV) += mdev.odrivers/vfio/mdev/mdev_core.c new file +40
-0@@ -0,0 +1,384 @@ +/* +* 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 ;
drivers/vfio/mdev/mdev_core.c line 49
Quoted 10 lines
drivers/vfio/mdev/mdev_core.c new file +9
-0+ +return 0 ; +} + +static bool mdev_device_exist (struct parent_device *parent,uuid_le uuid) +{ +struct device *dev; + + dev =device_find_child (parent->dev, &uuid, _find_mdev_device);
'_find_mdev_device' is actually a match, better rename to '__match_mdev' or something alike.
Quoted 357 lines
drivers/vfio/mdev/mdev_core.c new file +335
-0+if (dev) { +put_device (dev); +return true; + } + +return false; +} + +/* 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; +} + +/* +* mdev_device_remove_ops gets called from sysfs's 'remove' and when parent +* device is being unregistered from mdev device framework. +* - 'force_remove' is set to 'false' when called from sysfs's 'remove' which +* indicates that if the mdev device is active, used by VMM or userspace +* application, vendor driver could return error then don't remove the device. +* - 'force_remove' is set to 'true' when called from mdev_unregister_device() +* which indicate that parent device is being removed from mdev device +* framework so remove mdev device forcefully. +*/ +static int mdev_device_remove_ops (struct mdev_device *mdev,bool force_remove) +{ +struct parent_device *parent = mdev->parent; +int ret; + +/* +* Vendor driver can return error if VMM or userspace application is +* using this mdev device. +*/ + ret = parent->ops->remove (mdev); +if (ret && !force_remove) +return -EBUSY ; + +sysfs_remove_groups (&mdev->dev.kobj, parent->ops->mdev_attr_groups); +return 0 ; +} + +static int mdev_device_remove_cb (struct device *dev,void *data) +{ +if (!dev_is_mdev (dev)) +return 0 ; + +return mdev_device_remove (dev, data ? *(bool *)data : true); +} + +/* +* mdev_register_device : Register a device +* @dev: device structure representing parent device. +* @ops: Parent device operation structure to be registered. +* +* Add device to list of registered parent devices. +* Returns a negative value on error, otherwise 0. +*/ +int mdev_register_device (struct device *dev,const struct parent_ops *ops) +{ +int ret; +struct parent_device *parent; + +/* check for mandatory ops */ +if (!ops || !ops->create || !ops->remove || !ops->supported_type_groups) +return -EINVAL ; + + dev =get_device (dev); +if (!dev) +return -EINVAL ; + +mutex_lock (&parent_list_lock); + +/* Check for duplicate */ + parent =__find_parent_device (dev); +if (parent) { + ret = -EEXIST ; + goto add_dev_err; + } + + parent =kzalloc (sizeof (*parent),GFP_KERNEL ); +if (!parent) { + ret = -ENOMEM ; + goto add_dev_err; + } + +kref_init (&parent->ref); +mutex_init (&parent->lock); + + parent->dev = dev; + parent->ops = ops; + + 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" ); + +list_del (&parent->next); +class_compat_remove_link (mdev_bus_compat_class, dev,NULL ); + +device_for_each_child (dev, (void *)&force_remove, + mdev_device_remove_cb); + +parent_remove_sysfs_files (parent); + +mutex_unlock (&parent_list_lock); +mdev_put_parent (parent); +} +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 ; + +mutex_lock (&parent->lock); + +/* Check for duplicate */ +if (mdev_device_exist (parent, uuid)) { + 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" ); + +mutex_unlock (&parent->lock); +return ret; + +create_failed: +device_unregister (&mdev->dev); + +create_err: +mutex_unlock (&parent->lock); +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; + + mdev =to_mdev_device (dev); + type =to_mdev_type (mdev->type_kobj); + parent = mdev->parent; +mutex_lock (&parent->lock); + + ret =mdev_device_remove_ops (mdev, force_remove); +if (ret) { +mutex_unlock (&parent->lock); +return ret; + } + +mdev_remove_sysfs_files (dev, type); +device_unregister (dev); +mutex_unlock (&parent->lock); +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 +16
-0@@ -0,0 +1,122 @@ +/* +* 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> +
drivers/vfio/mdev/mdev_driver.c line 26
Quoted 10 lines
drivers/vfio/mdev/mdev_driver.c new file +10
-0+#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)) +return PTR_ERR (group);
Maybe I overthought, but where the iommu_group is released? On successful return you already have a ref, I didn't find an iommu_group_put after that.
--
Thanks,
Jike
Quoted 18 lines
drivers/vfio/mdev/mdev_driver.c new file +18
-0+ + ret =iommu_group_add_device (group, &mdev->dev); +if (ret) + goto attach_fail; + +dev_info (&mdev->dev,"MDEV: group_id = %d\n" , +iommu_group_id (group)); +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" ); +} +
Re: [PATCH v10 01/19] vfio: Mediated device Core driver
Quoted 12 lines · On Tue, 01 Nov 2016 11:08:15 +0800 Jike Song <[email protected]> wrote:
drivers/vfio/mdev/mdev_driver.c line 26
Quoted 8 lines · Kirti Wankhede
drivers/vfio/mdev/mdev_driver.c new file +8
-0+static int mdev_attach_iommu (struct mdev_device *mdev) +{ +int ret; +struct iommu_group *group; + + group =iommu_group_alloc (); +if (IS_ERR (group)) +return PTR_ERR (group);Maybe I overthought, but where the
iommu_groupis released? On successful return you already have a ref, I didn't find aniommu_group_putafter that.
...
Quoted 7 lines
Quoted 7 lines
drivers/vfio/mdev/mdev_driver.c new file +7
-0+ + ret =iommu_group_add_device (group, &mdev->dev); +if (ret) + goto attach_fail; + +dev_info (&mdev->dev,"MDEV: group_id = %d\n" , +iommu_group_id (group));
Quoted 8 lines
drivers/vfio/mdev/mdev_driver.c line 41
Quoted 8 lines
drivers/vfio/mdev/mdev_driver.c new file +8
-0+attach_fail: +iommu_group_put (group); +return ret; +} + +static void mdev_detach_iommu (struct mdev_device *mdev) +{ +iommu_group_remove_device (&mdev->dev);
Here. Adding a device to the group takes a group reference so we can 'put' the group to release the reference from the alloc as soon as a device is added. When we remove the device, that reference is removed and this should result in a release of the group if all other references are balanced. Thanks,
Alex
Quoted 4 lines
Quoted 3 lines
drivers/vfio/mdev/mdev_driver.c new file +3
-0+dev_info (&mdev->dev,"MDEV: detaching iommu\n" ); +} +
Re: [PATCH v10 01/19] vfio: Mediated device Core driver
Quoted 28 lines · Alex Williamson
Quoted 12 lines · On Tue, 01 Nov 2016 11:08:15 +0800 Jike Song <[email protected]> wrote:
drivers/vfio/mdev/mdev_driver.c line 26
Quoted 8 lines · Kirti Wankhede
drivers/vfio/mdev/mdev_driver.c new file +8
-0+static int mdev_attach_iommu (struct mdev_device *mdev) +{ +int ret; +struct iommu_group *group; + + group =iommu_group_alloc (); +if (IS_ERR (group)) +return PTR_ERR (group);Maybe I overthought, but where the
iommu_groupis released? On successful return you already have a ref, I didn't find aniommu_group_putafter that....
Quoted 13 lines
Quoted 13 lines
drivers/vfio/mdev/mdev_driver.c new file +13
-0+ + ret =iommu_group_add_device (group, &mdev->dev); +if (ret) + goto attach_fail; + +dev_info (&mdev->dev,"MDEV: group_id = %d\n" , +iommu_group_id (group)); +attach_fail: +iommu_group_put (group); +return ret; +} + +static void mdev_detach_iommu (struct mdev_device *mdev)
Quoted 8 lines
Quoted 2 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+{ + iommu_group_remove_device(&mdev->dev);Here. Adding a device to the group takes a group reference so we can 'put' the group to release the reference from the alloc as soon as a device is added. When we remove the device, that reference is removed and this should result in a release of the group if all other references are balanced. Thanks,
Now I understand, thanks :)
--
Thanks,
Jike
Kirti Wankhede
maintainerPATCH v10 2/19 vfio: VFIO based driver for Mediated devices
In reply to Kirti Wankhedevfio_mdev driver registers with mdev core driver.
mdev core driver creates mediated device and calls probe routine of vfio_mdev driver for each device.
Probe routine of vfio_mdev driver adds mediated device to VFIO core module
This driver forms a shim layer that pass through VFIO devices operations to vendor driver for mediated devices.
--- drivers/vfio/mdev/Kconfig | 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
@@ -8,3 +8,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
@@ -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
Re: [PATCH v10 02/19] vfio: VFIO based driver for Mediated devices
Quoted 187 lines · Kirti Wankhede
vfio_mdevdriver registers with mdev core driver.
mdev core driver creates mediated device and calls probe routine ofvfio_mdevdriver for each device.
Probe routine ofvfio_mdevdriver adds mediated device to VFIO core moduleThis driver forms a shim layer that pass through VFIO devices operations to vendor driver for mediated devices.
Signed-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id I583f4734752971d3d112324d69e2508c88f359ec--- drivers/vfio/mdev/Kconfig | 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.cdrivers/vfio/mdev/Kconfig +7
-0@@ -8,3 +8,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@@ -2,3 +2,4 @@ mdev-y := mdev_core.o mdev_sysfs.o mdev_driver.o obj-$(CONFIG_VFIO_MDEV) += mdev.o +obj-$(CONFIG_VFIO_MDEV_DEVICE) += vfio_mdev.odrivers/vfio/mdev/vfio_mdev.c new file +138
-0@@ -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 ); + +return ret; +} + +static void vfio_mdev_release (void *device_data) +{ +struct mdev_device *mdev = device_data; +struct parent_device *parent = mdev->parent; + +if (likely (parent->ops->release)) + 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) +{
drivers/vfio/mdev/vfio_mdev.c line 148
Quoted 10 lines
drivers/vfio/mdev/vfio_mdev.c new file +10
-0+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);
--
Thanks,
Jike
Kirti Wankhede
maintainerPATCH v10 3/19 vfio: Rearrange functions to get vfio_group from dev
In reply to Kirti WankhedeThis patch rearranges functions to get vfio_group from device
--- drivers/vfio/vfio.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-)
drivers/vfio/vfio.c +16 -7
@@ -480,6 +480,21 @@ static struct vfio_group *vfio_group_get_from_minor(int minor)return group; } +static struct vfio_group *vfio_group_get_from_dev (struct device *dev) +{ +struct iommu_group *iommu_group; +struct vfio_group *group; + + iommu_group =iommu_group_get (dev); +if (!iommu_group) +return NULL ; + + group =vfio_group_get_from_iommu (iommu_group); +iommu_group_put (iommu_group); + +return group; +} +/** * Device objects - create, release, get, put, search */
@@ -811,16 +826,10 @@ EXPORT_SYMBOL_GPL(vfio_add_group_dev);*/ struct vfio_device *vfio_device_get_from_dev (struct device *dev) { -struct iommu_group *iommu_group;struct vfio_group *group;struct vfio_device *device; - iommu_group =iommu_group_get (dev); -if (!iommu_group) -return NULL ; - - group =vfio_group_get_from_iommu (iommu_group); -iommu_group_put (iommu_group); + group =vfio_group_get_from_dev (dev);if (!group)return NULL ;
Signature · 1 line
2.7.0
Re: [PATCH v10 03/19] vfio: Rearrange functions to get vfio_group from dev
Quoted 43 lines · Kirti Wankhede
This patch rearranges functions to get
vfio_groupfrom deviceSigned-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id I1f93262bdbab75094bc24b087b29da35ba70c4c6--- drivers/vfio/vfio.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-)drivers/vfio/vfio.c +15
-1@@ -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;
drivers/vfio/vfio.c EXPORT_SYMBOL_GPL(vfio_add_group_dev); · line 835
Quoted 10 lines
drivers/vfio/vfio.c +1
-6- 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 ;
--
Thanks,
Jike
Kirti Wankhede
maintainerPATCH v10 4/19 vfio: Common function to increment container_users
In reply to Kirti WankhedeThis change rearrange functions to have common function to increment container_users
--- drivers/vfio/vfio.c | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-)
drivers/vfio/vfio.c +21 -13
@@ -1385,6 +1385,23 @@ static bool vfio_group_viable(struct vfio_group *group) group, vfio_dev_viable) == 0); } +static int vfio_group_add_container_user (struct vfio_group *group) +{ +if (!atomic_inc_not_zero (&group->container_users)) +return -EINVAL ; + +if (group->noiommu) { +atomic_dec (&group->container_users); +return -EPERM ; + } +if (!group->container->iommu_driver || !vfio_group_viable (group)) { +atomic_dec (&group->container_users); +return -EINVAL ; + } + +return 0 ; +} +static const struct file_operations vfio_device_fops;static int vfio_group_get_device_fd (struct vfio_group *group,char *buf)
@@ -1694,23 +1711,14 @@ static const struct file_operations vfio_device_fops = {struct vfio_group *vfio_group_get_external_user (struct file *filep) {struct vfio_group *group = filep->private_data; +int ret;if (filep->f_op != &vfio_group_fops)return ERR_PTR (-EINVAL ); -if (!atomic_inc_not_zero (&group->container_users)) -return ERR_PTR (-EINVAL ); - -if (group->noiommu) { -atomic_dec (&group->container_users); -return ERR_PTR (-EPERM ); - } - -if (!group->container->iommu_driver || - !vfio_group_viable (group)) { -atomic_dec (&group->container_users); -return ERR_PTR (-EINVAL ); - } + ret =vfio_group_add_container_user (group); +if (ret) +return ERR_PTR (ret);vfio_group_get (group);
Signature · 1 line
2.7.0
Re: [PATCH v10 04/19] vfio: Common function to increment container_users
Quoted 17 lines · Kirti Wankhede
This change rearrange functions to have common function to increment
container_usersSigned-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id I8bdeb352bc8439b107ffd519480fd4dc238677f2--- drivers/vfio/vfio.c | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-)drivers/vfio/vfio.c +0
-0@@ -1385,6 +1385,23 @@ static bool vfio_group_viable(struct vfio_group *group) group, vfio_dev_viable) == 0); }
drivers/vfio/vfio.c static bool vfio_group_viable(struct vfio_group *group) · line 1396
Quoted 10 lines
drivers/vfio/vfio.c +9
-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 ; + }
trivial: a blank line here
Quoted 29 lines
drivers/vfio/vfio.c +9
-9+if (!group->container->iommu_driver || !vfio_group_viable (group)) { +atomic_dec (&group->container_users); +return -EINVAL ; + } + +return 0 ; +} +static const struct file_operations vfio_device_fops;static int vfio_group_get_device_fd (struct vfio_group *group,char *buf)@@ -1694,23 +1711,14 @@ static const struct file_operations vfio_device_fops = {struct vfio_group *vfio_group_get_external_user (struct file *filep) {struct vfio_group *group = filep->private_data; +int ret;if (filep->f_op != &vfio_group_fops)return ERR_PTR (-EINVAL ); -if (!atomic_inc_not_zero (&group->container_users)) -return ERR_PTR (-EINVAL ); - -if (group->noiommu) { -atomic_dec (&group->container_users); -return ERR_PTR (-EPERM ); - } - - if (!group->container->iommu_driver ||
drivers/vfio/vfio.c static const struct file_operations vfio_device_fops = { · line 1724
Quoted 10 lines
drivers/vfio/vfio.c +3
-4- !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);
--
Thanks,
Jike
Kirti Wankhede
maintainerPATCH v10 5/19 vfio iommu: Added pin and unpin callback functions to vfio_iommu_driver_ops
In reply to Kirti WankhedeAdded two new callback functions to struct vfio_iommu_driver_ops. Backend IOMMU module that supports pining and unpinning pages for mdev devices should provide these functions.
Added APIs for pining and unpining pages to VFIO module. These calls back into backend iommu module to actually pin and unpin pages.
--- drivers/vfio/vfio.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/vfio.h | 12 ++++++- 2 files changed, 103 insertions(+), 1 deletion(-)
drivers/vfio/vfio.c +92 -0
@@ -1799,6 +1799,98 @@ 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; + +if (!dev || !user_pfn || !phys_pfn) +return -EINVAL ; + + group =vfio_group_get_from_dev (dev); +if (IS_ERR (group)) +return PTR_ERR (group); + + ret =vfio_group_add_container_user (group); +if (ret) + goto err_pin_pages; + + container = group->container; +down_read (&container->group_lock); + + driver = container->iommu_driver; +if (likely (driver && driver->ops->pin_pages)) + ret = driver->ops->pin_pages (container->iommu_data, user_pfn, + npage, prot, phys_pfn); +else + ret = -EINVAL ; + +up_read (&container->group_lock); +vfio_group_try_dissolve_container (group); + +err_pin_pages: +vfio_group_put (group); +return ret; + +} +EXPORT_SYMBOL (vfio_pin_pages); + +/* +* Unpin set of host PFNs for local domain only. +* @dev [in] : device +* @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; + +if (!dev || !pfn) +return -EINVAL ; + + group =vfio_group_get_from_dev (dev); +if (IS_ERR (group)) +return PTR_ERR (group); + + ret =vfio_group_add_container_user (group); +if (ret) + goto err_unpin_pages; + + container = group->container; +down_read (&container->group_lock); + + driver = container->iommu_driver; +if (likely (driver && driver->ops->unpin_pages)) + ret = driver->ops->unpin_pages (container->iommu_data, pfn, + npage); +else + ret = -EINVAL ; + +up_read (&container->group_lock); +vfio_group_try_dissolve_container (group); + +err_unpin_pages: +vfio_group_put (group); +return ret; +} +EXPORT_SYMBOL (vfio_unpin_pages); +/** * Module/class support */
include/linux/vfio.h +11 -1
@@ -75,7 +75,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 +131,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
Re: [PATCH v10 05/19] vfio iommu: Added pin and unpin callback functions to vfio_iommu_driver_ops
Quoted 22 lines · Kirti Wankhede
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.Signed-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id Ia7417723aaae86bec2959ad9ae6c2915ddd340e0--- drivers/vfio/vfio.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/vfio.h | 12 ++++++- 2 files changed, 103 insertions(+), 1 deletion(-)drivers/vfio/vfio.c +0
-0@@ -1799,6 +1799,98 @@ void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset) }EXPORT_SYMBOL_GPL (vfio_info_cap_shift);
drivers/vfio/vfio.c void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset) · line 1811
Quoted 10 lines
drivers/vfio/vfio.c +10
-0+ +/* +* 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 +*/
Hi Kirti,
Would you also add the documentation what the return value is? It's kind not clear, and any reason to use long instead of int?
Quoted 35 lines
drivers/vfio/vfio.c +35
-0+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; + +if (!dev || !user_pfn || !phys_pfn) +return -EINVAL ; + + group =vfio_group_get_from_dev (dev); +if (IS_ERR (group)) +return PTR_ERR (group); + + ret =vfio_group_add_container_user (group); +if (ret) + goto err_pin_pages; + + container = group->container; +down_read (&container->group_lock); + + driver = container->iommu_driver; +if (likely (driver && driver->ops->pin_pages)) + ret = driver->ops->pin_pages (container->iommu_data, user_pfn, + npage, prot, phys_pfn); +else + ret = -EINVAL ; + +up_read (&container->group_lock); +vfio_group_try_dissolve_container (group); + +err_pin_pages: +vfio_group_put (group); +return ret;
drivers/vfio/vfio.c void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset) · line 1856
Quoted 10 lines
drivers/vfio/vfio.c +10
-0+ +} +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. +*/
Ditto
--
Thanks,
Jike
Quoted 71 lines
drivers/vfio/vfio.c +37
-0+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; + +if (!dev || !pfn) +return -EINVAL ; + + group =vfio_group_get_from_dev (dev); +if (IS_ERR (group)) +return PTR_ERR (group); + + ret =vfio_group_add_container_user (group); +if (ret) + goto err_unpin_pages; + + container = group->container; +down_read (&container->group_lock); + + driver = container->iommu_driver; +if (likely (driver && driver->ops->unpin_pages)) + ret = driver->ops->unpin_pages (container->iommu_data, pfn, + npage); +else + ret = -EINVAL ; + +up_read (&container->group_lock); +vfio_group_try_dissolve_container (group); + +err_unpin_pages: +vfio_group_put (group); +return ret; +} +EXPORT_SYMBOL (vfio_unpin_pages); +/** * Module/class support */ include/linux/vfio.h +11
-1@@ -75,7 +75,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 +131,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 */
Kirti Wankhede
maintainerPATCH v10 6/19 vfio iommu type1: Update arguments of vfio_lock_acct
In reply to Kirti WankhedeUpdated arguments of vfio_lock_acct to take task as input argument
--- drivers/vfio/vfio_iommu_type1.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
drivers/vfio/vfio_iommu_type1.c +10 -10
@@ -150,17 +150,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 (¤t->mm->mmap_sem)) { - current->mm->locked_vm += npage; -up_write (¤t->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 +172,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 ;
@@ -285,7 +285,7 @@ static long vfio_pin_pages(unsigned long vaddr, long npage,if (unlikely (disable_hugepages)) {if (!rsvd) -vfio_lock_acct (1 ); +vfio_lock_acct (current,1 );return 1 ; }
@@ -313,7 +313,7 @@ static long vfio_pin_pages(unsigned long vaddr, long npage, }if (!rsvd) -vfio_lock_acct (i); +vfio_lock_acct (current, i);return i; }
@@ -328,7 +328,7 @@ static long vfio_unpin_pages(unsigned long pfn, long npage, unlocked +=put_pfn (pfn++, prot);if (do_accounting) -vfio_lock_acct (-unlocked); +vfio_lock_acct (current, -unlocked);return unlocked; }
@@ -390,7 +390,7 @@ static void vfio_unmap_unpin(struct vfio_iommu *iommu, struct vfio_dma *dma)cond_resched (); } -vfio_lock_acct (-unlocked); +vfio_lock_acct (current, -unlocked); }static void vfio_remove_dma (struct vfio_iommu *iommu,struct vfio_dma *dma)
Signature · 1 line
2.7.0
Kirti Wankhede
maintainerPATCH v10 7/19 vfio iommu type1: Update argument of vaddr_get_pfn()
In reply to Kirti WankhedeUpdate arguments of vaddr_get_pfn() to take struct mm_struct *mm as input argument.
--- drivers/vfio/vfio_iommu_type1.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-)
drivers/vfio/vfio_iommu_type1.c +18 -7
@@ -228,20 +228,31 @@ static int put_pfn(unsigned long pfn, int prot)return 0 ; } -static int vaddr_get_pfn (unsigned long vaddr,int prot,unsigned long *pfn) +static int vaddr_get_pfn (struct mm_struct *remote_mm,unsigned long vaddr, +int prot,unsigned long *pfn) {struct page *page[1 ];struct vm_area_struct *vma; +struct mm_struct *mm = (remote_mm ? remote_mm : current->mm);int ret = -EFAULT ; -if (get_user_pages_fast (vaddr,1 , !!(prot &IOMMU_WRITE ), page) ==1 ) { +if (remote_mm) { +down_read (&mm->mmap_sem); + ret =get_user_pages_remote (NULL , mm, vaddr,1 , + !!(prot &IOMMU_WRITE ),0 , page,NULL ); +up_read (&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 (¤t->mm->mmap_sem); +down_read (&mm->mmap_sem); - vma =find_vma_intersection (current->mm, vaddr, vaddr +1 ); + vma =find_vma_intersection (mm, vaddr, vaddr +1 );if (vma && vma->vm_flags &VM_PFNMAP ) { *pfn = ((vaddr - vma->vm_start) >>PAGE_SHIFT ) + vma->vm_pgoff;
@@ -249,7 +260,7 @@ static int vaddr_get_pfn(unsigned long vaddr, int prot, unsigned long *pfn) ret =0 ; } -up_read (¤t->mm->mmap_sem); +up_read (&mm->mmap_sem);return ret; }
@@ -270,7 +281,7 @@ static long vfio_pin_pages(unsigned long vaddr, long npage,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;
@@ -293,7 +304,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 ;
Signature · 1 line
2.7.0
Re: [PATCH v10 07/19] vfio iommu type1: Update argument of vaddr_get_pfn()
Quoted 22 lines · Kirti Wankhede
Update arguments of
vaddr_get_pfn()to take structmm_struct*mm as input argument.Signed-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id I885fd4cd4a9f66f4ee2c1caf58267464ec239f52--- drivers/vfio/vfio_iommu_type1.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-)drivers/vfio/vfio_iommu_type1.c +2
-1@@ -228,20 +228,31 @@ static int put_pfn(unsigned long pfn, int prot)return 0 ; } -static int vaddr_get_pfn (unsigned long vaddr,int prot,unsigned long *pfn) +static int vaddr_get_pfn (struct mm_struct *remote_mm,unsigned long vaddr, +int prot,unsigned long *pfn) {
drivers/vfio/vfio_iommu_type1.c static int put_pfn(unsigned long pfn, int prot) · line 242
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +5
-1struct page *page[1 ];struct vm_area_struct *vma; +struct mm_struct *mm = (remote_mm ? remote_mm : current->mm);int ret = -EFAULT ; -if (get_user_pages_fast (vaddr,1 , !!(prot &IOMMU_WRITE ), page) ==1 ) { +if (remote_mm) { +down_read (&mm->mmap_sem); + ret =get_user_pages_remote (NULL , mm, vaddr,1 , + !!(prot &IOMMU_WRITE ),0 , page,NULL );
Hi Kirti,
It seems that get_user_pages_remote has only 7 args but you somehow gives 8 here?
--
Thanks,
Jike
Quoted 46 lines
drivers/vfio/vfio_iommu_type1.c +11
-5+up_read (&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 (¤t->mm->mmap_sem); +down_read (&mm->mmap_sem); - vma =find_vma_intersection (current->mm, vaddr, vaddr +1 ); + vma =find_vma_intersection (mm, vaddr, vaddr +1 );if (vma && vma->vm_flags &VM_PFNMAP ) { *pfn = ((vaddr - vma->vm_start) >>PAGE_SHIFT ) + vma->vm_pgoff;@@ -249,7 +260,7 @@ static int vaddr_get_pfn(unsigned long vaddr, int prot, unsigned long *pfn) ret =0 ; } -up_read (¤t->mm->mmap_sem); +up_read (&mm->mmap_sem);return ret; }@@ -270,7 +281,7 @@ static long vfio_pin_pages(unsigned long vaddr, long npage,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;@@ -293,7 +304,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 ;
Kirti Wankhede
maintainerRe: [PATCH v10 07/19] vfio iommu type1: Update argument of vaddr_get_pfn()
Quoted 36 lines · Jike Song
Quoted 22 lines · Kirti Wankhede
Update arguments of
vaddr_get_pfn()to take structmm_struct*mm as input argument.Signed-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id I885fd4cd4a9f66f4ee2c1caf58267464ec239f52--- drivers/vfio/vfio_iommu_type1.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-)drivers/vfio/vfio_iommu_type1.c +2
-1@@ -228,20 +228,31 @@ static int put_pfn(unsigned long pfn, int prot)return 0 ; } -static int vaddr_get_pfn (unsigned long vaddr,int prot,unsigned long *pfn) +static int vaddr_get_pfn (struct mm_struct *remote_mm,unsigned long vaddr, +int prot,unsigned long *pfn) {drivers/vfio/vfio_iommu_type1.c static int put_pfn(unsigned long pfn, int prot) · line 242
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +5
-1struct page *page[1 ];struct vm_area_struct *vma; +struct mm_struct *mm = (remote_mm ? remote_mm : current->mm);int ret = -EFAULT ; -if (get_user_pages_fast (vaddr,1 , !!(prot &IOMMU_WRITE ), page) ==1 ) { +if (remote_mm) { +down_read (&mm->mmap_sem); + ret =get_user_pages_remote (NULL , mm, vaddr,1 , + !!(prot &IOMMU_WRITE ),0 , page,NULL );Hi Kirti,
Quoted 3 lines
It seems that
get_user_pages_remotehas only 7 args but you somehow gives 8 here?
I had tested this patch with linux-next. It has 8 arguments, see mm/gup.c:
long get_user_pages_remote(struct task_struct *tsk, struct mm_struct *mm,
unsigned long start, unsigned long nr_pages, int write, int force, struct page **pages, struct vm_area_struct **vmas)
{
return __get_user_pages_locked(tsk, mm, start, nr_pages, write,
force,
pages, vmas, NULL, false, FOLL_TOUCH | FOLL_REMOTE);
}EXPORT_SYMBOL(get_user_pages_remote);
Thanks,
Kirti
Quoted 50 lines
--
Thanks,
JikeQuoted 46 lines
drivers/vfio/vfio_iommu_type1.c +11
-5+up_read (&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 (¤t->mm->mmap_sem); +down_read (&mm->mmap_sem); - vma =find_vma_intersection (current->mm, vaddr, vaddr +1 ); + vma =find_vma_intersection (mm, vaddr, vaddr +1 );if (vma && vma->vm_flags &VM_PFNMAP ) { *pfn = ((vaddr - vma->vm_start) >>PAGE_SHIFT ) + vma->vm_pgoff;@@ -249,7 +260,7 @@ static int vaddr_get_pfn(unsigned long vaddr, int prot, unsigned long *pfn) ret =0 ; } -up_read (¤t->mm->mmap_sem); +up_read (&mm->mmap_sem);return ret; }@@ -270,7 +281,7 @@ static long vfio_pin_pages(unsigned long vaddr, long npage,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;@@ -293,7 +304,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 ;
Re: [PATCH v10 07/19] vfio iommu type1: Update argument of vaddr_get_pfn()
Quoted 45 lines · Kirti Wankhede
Quoted 36 lines · Jike Song
Quoted 22 lines · Kirti Wankhede
Update arguments of
vaddr_get_pfn()to take structmm_struct*mm as input argument.Signed-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id I885fd4cd4a9f66f4ee2c1caf58267464ec239f52--- drivers/vfio/vfio_iommu_type1.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-)drivers/vfio/vfio_iommu_type1.c +2
-1@@ -228,20 +228,31 @@ static int put_pfn(unsigned long pfn, int prot)return 0 ; } -static int vaddr_get_pfn (unsigned long vaddr,int prot,unsigned long *pfn) +static int vaddr_get_pfn (struct mm_struct *remote_mm,unsigned long vaddr, +int prot,unsigned long *pfn) {drivers/vfio/vfio_iommu_type1.c static int put_pfn(unsigned long pfn, int prot) · line 242
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +5
-1struct page *page[1 ];struct vm_area_struct *vma; +struct mm_struct *mm = (remote_mm ? remote_mm : current->mm);int ret = -EFAULT ; -if (get_user_pages_fast (vaddr,1 , !!(prot &IOMMU_WRITE ), page) ==1 ) { +if (remote_mm) { +down_read (&mm->mmap_sem); + ret =get_user_pages_remote (NULL , mm, vaddr,1 , + !!(prot &IOMMU_WRITE ),0 , page,NULL );Hi Kirti,
Quoted 3 lines
It seems that
get_user_pages_remotehas only 7 args but you somehow gives 8 here?I had tested this patch with linux-next. It has 8 arguments, see
mm/gup.c:
Quoted 11 lines
long get_user_pages_remote(struct
task_struct*tsk, structmm_struct*mm,
unsigned long start, unsigned longnr_pages, int write, int force, struct page **pages, structvm_area_struct**vmas)
{
return __get_user_pages_locked(tsk, mm, start,nr_pages, write,
force,
pages, vmas, NULL, false,FOLL_TOUCH|FOLL_REMOTE);
}EXPORT_SYMBOL(get_user_pages_remote);
It's queer. I checked:
https://git.kernel.org/cgit/linux/kernel/git/next/linux-next.git/tree/mm/gup.c
It still has 7 args.
I believe you have seen this change somewhere, would you share the repo url and branch name of the linux-next you based on?
--
Thanks,
Jike
Kirti Wankhede
maintainerRe: [PATCH v10 07/19] vfio iommu type1: Update argument of vaddr_get_pfn()
Quoted 58 lines · Jike Song
Quoted 45 lines · Kirti Wankhede
Quoted 36 lines · Jike Song
Quoted 22 lines · Kirti Wankhede
Update arguments of
vaddr_get_pfn()to take structmm_struct*mm as input argument.Signed-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id I885fd4cd4a9f66f4ee2c1caf58267464ec239f52--- drivers/vfio/vfio_iommu_type1.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-)drivers/vfio/vfio_iommu_type1.c +2
-1@@ -228,20 +228,31 @@ static int put_pfn(unsigned long pfn, int prot)return 0 ; } -static int vaddr_get_pfn (unsigned long vaddr,int prot,unsigned long *pfn) +static int vaddr_get_pfn (struct mm_struct *remote_mm,unsigned long vaddr, +int prot,unsigned long *pfn) {drivers/vfio/vfio_iommu_type1.c static int put_pfn(unsigned long pfn, int prot) · line 242
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +5
-1struct page *page[1 ];struct vm_area_struct *vma; +struct mm_struct *mm = (remote_mm ? remote_mm : current->mm);int ret = -EFAULT ; -if (get_user_pages_fast (vaddr,1 , !!(prot &IOMMU_WRITE ), page) ==1 ) { +if (remote_mm) { +down_read (&mm->mmap_sem); + ret =get_user_pages_remote (NULL , mm, vaddr,1 , + !!(prot &IOMMU_WRITE ),0 , page,NULL );Hi Kirti,
Quoted 3 lines
It seems that
get_user_pages_remotehas only 7 args but you somehow gives 8 here?I had tested this patch with linux-next. It has 8 arguments, see
mm/gup.c:Quoted 11 lines
long get_user_pages_remote(struct
task_struct*tsk, structmm_struct*mm,
unsigned long start, unsigned longnr_pages, int write, int force, struct page **pages, structvm_area_struct**vmas)
{
return __get_user_pages_locked(tsk, mm, start,nr_pages, write,
force,
pages, vmas, NULL, false,FOLL_TOUCH|FOLL_REMOTE);
}EXPORT_SYMBOL(get_user_pages_remote);
Quoted 9 lines
It's queer. I checked:
https://git.kernel.org/cgit/linux/kernel/git/next/linux-next.git/tree/mm/gup.c
It still has 7 args.
I believe you have seen this change somewhere, would you share the repo url and branch name of the linux-next you based on?
Sorry, my git was synced just few changes behind this change. I'll make sure that I test the patch set at top of tree.
Thanks,
Kirti
Kirti Wankhede
maintainerPATCH v10 8/19 vfio iommu type1: Add find_iommu_group() function
In reply to Kirti WankhedeAdd find_iommu_group()
--- drivers/vfio/vfio_iommu_type1.c | 58 ++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 24 deletions(-)
drivers/vfio/vfio_iommu_type1.c +34 -24
@@ -745,11 +745,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;
@@ -757,10 +770,7 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,mutex_lock (&iommu->lock);list_for_each_entry (d, &iommu->domain_list, next) { -list_for_each_entry (g, &d->group_list, next) { -if (g->iommu_group != iommu_group) -continue ; - +if (find_iommu_group (d, iommu_group)) {mutex_unlock (&iommu->lock);return -EINVAL ; }
@@ -879,28 +889,28 @@ static void vfio_iommu_type1_detach_group(void *iommu_data,mutex_lock (&iommu->lock); +list_for_each_entry (domain, &iommu->domain_list, next) { -list_for_each_entry (group, &domain->group_list, next) { -if (group->iommu_group != iommu_group) -continue ; + group =find_iommu_group (domain, iommu_group); +if (!group) +continue ; -iommu_detach_group (domain->domain, iommu_group); -list_del (&group->next); -kfree (group); -/* -* Group ownership provides privilege, if the group -* list is empty, the domain goes away. If it's the -* last domain, then all the mappings go away too. -*/ -if (list_empty (&domain->group_list)) { -if (list_is_singular (&iommu->domain_list)) -vfio_iommu_unmap_unpin_all (iommu); -iommu_domain_free (domain->domain); -list_del (&domain->next); -kfree (domain); - } - goto done; +iommu_detach_group (domain->domain, iommu_group); +list_del (&group->next); +kfree (group); +/* +* Group ownership provides privilege, if the group +* list is empty, the domain goes away. If it's the +* last domain, then all the mappings go away too. +*/ +if (list_empty (&domain->group_list)) { +if (list_is_singular (&iommu->domain_list)) +vfio_iommu_unmap_unpin_all (iommu); +iommu_domain_free (domain->domain); +list_del (&domain->next); +kfree (domain); } + goto done; } done:
Signature · 1 line
2.7.0
Re: [PATCH v10 08/19] vfio iommu type1: Add find_iommu_group() function
Quoted 90 lines · Kirti Wankhede
Add
find_iommu_group()Signed-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id I9d372f1ebe9eb01a5a21374b8a2b03f7df73601f--- drivers/vfio/vfio_iommu_type1.c | 58 ++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 24 deletions(-)drivers/vfio/vfio_iommu_type1.c +28
-24@@ -745,11 +745,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;@@ -757,10 +770,7 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,mutex_lock (&iommu->lock);list_for_each_entry (d, &iommu->domain_list, next) { -list_for_each_entry (g, &d->group_list, next) { -if (g->iommu_group != iommu_group) -continue ; - +if (find_iommu_group (d, iommu_group)) {mutex_unlock (&iommu->lock);return -EINVAL ; }@@ -879,28 +889,28 @@ static void vfio_iommu_type1_detach_group(void *iommu_data,mutex_lock (&iommu->lock); +list_for_each_entry (domain, &iommu->domain_list, next) { -list_for_each_entry (group, &domain->group_list, next) { -if (group->iommu_group != iommu_group) -continue ; + group =find_iommu_group (domain, iommu_group); +if (!group) +continue ; -iommu_detach_group (domain->domain, iommu_group); -list_del (&group->next); -kfree (group); -/* -* Group ownership provides privilege, if the group -* list is empty, the domain goes away. If it's the -* last domain, then all the mappings go away too. -*/ -if (list_empty (&domain->group_list)) { -if (list_is_singular (&iommu->domain_list)) -vfio_iommu_unmap_unpin_all (iommu); -iommu_domain_free (domain->domain); -list_del (&domain->next); -kfree (domain); - } - goto done; +iommu_detach_group (domain->domain, iommu_group); +list_del (&group->next); +kfree (group); +/* +* Group ownership provides privilege, if the group +* list is empty, the domain goes away. If it's the +* last domain, then all the mappings go away too. +*/ +if (list_empty (&domain->group_list)) {
drivers/vfio/vfio_iommu_type1.c static void vfio_iommu_type1_detach_group(void *iommu_data, · line 916
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +6
-0+if (list_is_singular (&iommu->domain_list)) +vfio_iommu_unmap_unpin_all (iommu); +iommu_domain_free (domain->domain); +list_del (&domain->next); +kfree (domain); } + goto done; } done:
--
Thanks,
Jike
Kirti Wankhede
maintainerPATCH v10 9/19 vfio iommu type1: Add support for mediated devices
In reply to Kirti WankhedeVFIO IOMMU drivers are designed for the devices which are IOMMU capable. Mediated device only uses IOMMU APIs, the underlying hardware can be managed by an IOMMU domain.
Aim of this change is:
- To use most of the code of TYPE1 IOMMU driver for mediated devices
- To support direct assigned device and mediated device in single module
This change adds pin and unpin support for mediated device to TYPE1 IOMMU backend module. More details:
- 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
--- drivers/vfio/vfio_iommu_type1.c | 646 +++++++++++++++++++++++++++++++++++----- 1 file changed, 571 insertions(+), 75 deletions(-)
drivers/vfio/vfio_iommu_type1.c +571 -75
@@ -36,6 +36,7 @@#include <linux/uaccess.h> #include <linux/vfio.h> #include <linux/workqueue.h> +#include <linux/mdev.h> #define DRIVER_VERSION "0.2"#define DRIVER_AUTHOR "Alex Williamson <[email protected]>"
@@ -55,18 +56,26 @@ MODULE_PARM_DESC(disable_hugepages,struct vfio_iommu {struct list_head domain_list; +struct vfio_domain *external_domain;/* domain for external user */ struct mutex lock;struct rb_root dma_list;bool v2;bool nesting; }; +struct external_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; -int prot;/* IOMMU_CACHE */ -bool fgsp;/* Fine-grained super pages */ +struct iommu_domain *domain; +struct list_head next; +struct list_head group_list; +struct external_addr_space *external_addr_space; +int prot;/* IOMMU_CACHE */ +bool fgsp;/* Fine-grained super pages */ };struct vfio_dma {
@@ -75,6 +84,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 +93,21 @@ struct vfio_group { };/* +* 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; +}; + +#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 +155,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->external_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->external_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->external_addr_space->pfn_list); +} + +static void vfio_unlink_pfn (struct vfio_domain *domain,struct vfio_pfn *old) +{ +rb_erase (&old->node, &domain->external_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->external_domain; +int ret =1 ; + +if (!domain) +return 1 ; + +mutex_lock (&domain->external_addr_space->pfn_list_lock); + + p =vfio_find_pfn (domain, pfn); +if (p) + ret =0 ; + +mutex_unlock (&domain->external_addr_space->pfn_list_lock); +return ret; +} +struct vwork {struct mm_struct *mm;long npage;
@@ -270,12 +390,13 @@ static int vaddr_get_pfn(struct mm_struct *remote_mm, unsigned long vaddr,* the iommu can only map chunks of consecutive pfns anyway, so get the * first page and all consecutive pages with the same locking. */ -static long vfio_pin_pages (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)
@@ -285,9 +406,11 @@ static long vfio_pin_pages(unsigned long vaddr, long npage,if (ret)return ret; + lock_acct =vfio_pfn_account (iommu, *pfn_base); + rsvd =is_invalid_reserved_pfn (*pfn_base); -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 );
@@ -314,8 +437,10 @@ static long vfio_pin_pages(unsigned long vaddr, long npage,break ; } + lock_acct +=vfio_pfn_account (iommu, pfn); +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 );
@@ -329,14 +454,19 @@ static long vfio_pin_pages(unsigned long vaddr, long npage,return i; } -static long vfio_unpin_pages (unsigned long pfn,long npage, -int prot,bool do_accounting) +static long __vfio_unpin_pages_remote (struct vfio_iommu *iommu, +unsigned long pfn,long npage,int prot, +bool do_accounting) { -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 (current, -unlocked);
@@ -344,14 +474,208 @@ static long vfio_unpin_pages(unsigned long pfn, long npage,return unlocked; } -static void vfio_unmap_unpin (struct vfio_iommu *iommu,struct vfio_dma *dma) +static long __vfio_pin_page_external (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->external_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_external (struct vfio_domain *domain, +unsigned long pfn,int prot, +bool do_accounting) +{ +put_pfn (pfn, prot); + +if (do_accounting) +vfio_lock_acct (domain->external_addr_space->task,-1 ); +} + +static int vfio_unpin_pfn (struct vfio_domain *domain, +struct vfio_pfn *vpfn,bool do_accounting) +{ +__vfio_unpin_page_external (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; +struct external_addr_space *addr_space; +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->external_domain) { + ret = -EINVAL ; + goto pin_done; + } + + domain = iommu->external_domain; + addr_space = domain->external_addr_space; +/* +* 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_external (domain, remote_vaddr, prot, + &pfn[i], do_accounting); +if (retpage <=0 ) { +WARN_ON (!retpage); + ret = (int )retpage; + goto pin_unwind; + } + +mutex_lock (&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 (&addr_space->pfn_list_lock); +continue ; + } + + ret =vfio_add_to_pfn_list (domain, remote_vaddr, iova, + pfn[i], prot); +mutex_unlock (&addr_space->pfn_list_lock); + +if (ret) { +__vfio_unpin_page_external (domain, pfn[i], prot, + do_accounting); + goto pin_unwind; + } + } + + ret = i; + goto pin_done; + +pin_unwind: + pfn[i] =0 ; +mutex_lock (&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 (&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->external_domain; + + do_accounting = !IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu); + +mutex_lock (&domain->external_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->external_addr_space->pfn_list_lock); + +mutex_unlock (&iommu->lock); +return unlocked; +} + +static long vfio_unmap_unpin (struct vfio_iommu *iommu,struct vfio_dma *dma, +bool do_accounting) {dma_addr_t iova = dma->iova, end = dma->iova + dma->size;struct vfio_domain *domain, *d;long unlocked =0 ;if (!dma->size) -return ; +return 0 ; + +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) +return 0 ; +/* * We use the IOMMU to track the physical addresses, otherwise we'd * need a much more complicated tracking system. Unfortunately that
@@ -393,20 +717,27 @@ 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 (current, -unlocked); + dma->iommu_mapped = false; + +if (do_accounting) { +vfio_lock_acct (current, -unlocked); +return 0 ; + } + +return unlocked; }static void vfio_remove_dma (struct vfio_iommu *iommu,struct vfio_dma *dma) { -vfio_unmap_unpin (iommu, dma); +vfio_unmap_unpin (iommu, dma, true);vfio_unlink_dma (iommu, dma);kfree (dma); }
@@ -569,17 +900,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)
@@ -622,29 +993,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;
@@ -673,10 +1026,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;
@@ -685,20 +1034,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); - size =PAGE_SIZE ; +if (WARN_ON (!phys)) { + iova +=PAGE_SIZE ; +continue ; + } -while (iova + size < dma->iova + dma->size && - phys + size ==iommu_iova_to_phys (d->domain, + size =PAGE_SIZE ; + +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);
@@ -707,6 +1079,8 @@ static int vfio_iommu_replay(struct vfio_iommu *iommu, iova += size; } + + dma->iommu_mapped = true; }return 0 ;
@@ -764,7 +1138,7 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,struct vfio_iommu *iommu = iommu_data;struct vfio_group *group;struct vfio_domain *domain, *d; -struct bus_type *bus =NULL ; +struct bus_type *bus =NULL , *mdev_bus;int ret;mutex_lock (&iommu->lock);
@@ -776,6 +1150,13 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, } } +if (iommu->external_domain) { +if (find_iommu_group (iommu->external_domain, iommu_group)) { +mutex_unlock (&iommu->lock); +return -EINVAL ; + } + } + group =kzalloc (sizeof (*group),GFP_KERNEL ); domain =kzalloc (sizeof (*domain),GFP_KERNEL );if (!group || !domain) {
@@ -790,6 +1171,38 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,if (ret) goto out_free; + mdev_bus =symbol_get (mdev_bus_type); + +if (mdev_bus) { +if ((bus == mdev_bus) && !iommu_present (bus)) { +symbol_put (mdev_bus_type); +if (!iommu->external_domain) { +struct external_addr_space *addr_space; + + addr_space =kzalloc (sizeof (*addr_space), +GFP_KERNEL ); +if (!addr_space) { + ret = -ENOMEM ; + goto out_free; + } + +INIT_LIST_HEAD (&domain->group_list); + addr_space->task = current; + addr_space->pfn_list =RB_ROOT ; +mutex_init (&addr_space->pfn_list_lock); + domain->external_addr_space = addr_space; + iommu->external_domain = domain; + }else +kfree (domain); + +list_add (&group->next, + &iommu->external_domain->group_list); +mutex_unlock (&iommu->lock); +return 0 ; + } +symbol_put (mdev_bus_type); + } + domain->domain =iommu_domain_alloc (bus);if (!domain->domain) { ret = -EIO ;
@@ -880,6 +1293,42 @@ 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->external_domain; +struct vfio_dma *dma, *tdma; +struct rb_node *n; +long locked =0 , unlocked =0 ; + +rbtree_postorder_for_each_entry_safe (dma, tdma, &iommu->dma_list, + node) { + unlocked +=vfio_unmap_unpin (iommu, dma, false); + } + +mutex_lock (&domain->external_addr_space->pfn_list_lock); + + n =rb_first (&domain->external_addr_space->pfn_list); + +for (; n; n =rb_next (n)) + locked++; + +vfio_lock_acct (domain->external_addr_space->task, locked - unlocked); +mutex_unlock (&domain->external_addr_space->pfn_list_lock); +} + +static void vfio_external_unpin_all (struct vfio_domain *domain, +bool do_accounting) +{ +struct rb_node *node; + +mutex_lock (&domain->external_addr_space->pfn_list_lock); +while ((node =rb_first (&domain->external_addr_space->pfn_list))) + vfio_unpin_pfn(domain, rb_entry(node, struct vfio_pfn, node), + do_accounting); + +mutex_unlock (&domain->external_addr_space->pfn_list_lock); +} +static void vfio_iommu_type1_detach_group (void *iommu_data,struct iommu_group *iommu_group) {
@@ -889,6 +1338,28 @@ static void vfio_iommu_type1_detach_group(void *iommu_data,mutex_lock (&iommu->lock); +if (iommu->external_domain) { + domain = iommu->external_domain; + group =find_iommu_group (domain, iommu_group); +if (group) { +list_del (&group->next); +kfree (group); + +if (list_empty (&domain->group_list)) { +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { +vfio_external_unpin_all (domain, true); +vfio_iommu_unmap_unpin_all (iommu); + }else +vfio_external_unpin_all (domain, false); +kfree (domain); + iommu->external_domain =NULL ; + } + goto detach_group_done; + } + } + +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) + goto detach_group_done;list_for_each_entry (domain, &iommu->domain_list, next) { group =find_iommu_group (domain, iommu_group);
@@ -899,21 +1370,27 @@ static void vfio_iommu_type1_detach_group(void *iommu_data,list_del (&group->next);kfree (group);/* -* Group ownership provides privilege, if the group -* list is empty, the domain goes away. If it's the -* last domain, then all the mappings go away too. +* Group ownership provides privilege, if the group list is +* empty, the domain goes away. If it's the last domain with +* iommu and external domain doesn't exist, then all the +* mappings go away too. If it's the last domain with iommu and +* external domain exist, update accounting */ if (list_empty (&domain->group_list)) { -if (list_is_singular (&iommu->domain_list)) -vfio_iommu_unmap_unpin_all (iommu); +if (list_is_singular (&iommu->domain_list)) { +if (!iommu->external_domain) +vfio_iommu_unmap_unpin_all (iommu); +else +vfio_iommu_unmap_unpin_reaccount (iommu); + }iommu_domain_free (domain->domain);list_del (&domain->next);kfree (domain); } - goto done; +break ; } -done: +detach_group_done:mutex_unlock (&iommu->lock); }
@@ -945,23 +1422,40 @@ 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->external_addr_space) +iommu_detach_group (domain->domain, group->iommu_group); +list_del (&group->next); +kfree (group); + } + +if (domain->external_addr_space) +vfio_external_unpin_all (domain, false); +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; + +if (iommu->external_domain) { +vfio_release_domain (iommu->external_domain); +kfree (iommu->external_domain); + iommu->external_domain =NULL ; + }vfio_iommu_unmap_unpin_all (iommu);list_for_each_entry_safe (domain, domain_tmp, &iommu->domain_list, next) { -list_for_each_entry_safe (group, group_tmp, - &domain->group_list, next) { -iommu_detach_group (domain->domain, group->iommu_group); -list_del (&group->next); -kfree (group); - } -iommu_domain_free (domain->domain); +vfio_release_domain (domain);list_del (&domain->next);kfree (domain); }
@@ -1069,6 +1563,8 @@ static const struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1 = { .ioctl = vfio_iommu_type1_ioctl, .attach_group = vfio_iommu_type1_attach_group, .detach_group = vfio_iommu_type1_detach_group, + .pin_pages = vfio_iommu_type1_pin_pages, + .unpin_pages = vfio_iommu_type1_unpin_pages, };static int __init vfio_iommu_type1_init (void )
Signature · 1 line
2.7.0
Re: [PATCH v10 09/19] vfio iommu type1: Add support for mediated devices
Quoted 218 lines · On Thu, 27 Oct 2016 02:59:16 +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 moduleThis change adds pin and unpin support for mediated device to TYPE1 IOMMU backend module. More details:
- Wheniommu_groupof 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 existSigned-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id I295d6f0f2e0579b8d9882bfd8fd5a4194b97bd9a--- drivers/vfio/vfio_iommu_type1.c | 646 +++++++++++++++++++++++++++++++++++----- 1 file changed, 571 insertions(+), 75 deletions(-)drivers/vfio/vfio_iommu_type1.c +125
-5@@ -36,6 +36,7 @@#include <linux/uaccess.h> #include <linux/vfio.h> #include <linux/workqueue.h> +#include <linux/mdev.h> #define DRIVER_VERSION "0.2"#define DRIVER_AUTHOR "Alex Williamson <[email protected]>"@@ -55,18 +56,26 @@ MODULE_PARM_DESC(disable_hugepages,struct vfio_iommu {struct list_head domain_list; +struct vfio_domain *external_domain;/* domain for external user */ struct mutex lock;struct rb_root dma_list;bool v2;bool nesting; }; +struct external_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; -int prot;/* IOMMU_CACHE */ -bool fgsp;/* Fine-grained super pages */ +struct iommu_domain *domain; +struct list_head next; +struct list_head group_list; +struct external_addr_space *external_addr_space; +int prot;/* IOMMU_CACHE */ +bool fgsp;/* Fine-grained super pages */ };struct vfio_dma {@@ -75,6 +84,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 +93,21 @@ struct vfio_group { };/* +* 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; +}; + +#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 +155,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->external_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->external_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->external_addr_space->pfn_list); +} + +static void vfio_unlink_pfn (struct vfio_domain *domain,struct vfio_pfn *old) +{ +rb_erase (&old->node, &domain->external_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->external_domain; +int ret =1 ; + +if (!domain) +return 1 ; + +mutex_lock (&domain->external_addr_space->pfn_list_lock); + + p =vfio_find_pfn (domain, pfn); +if (p) + ret =0 ; + +mutex_unlock (&domain->external_addr_space->pfn_list_lock); +return ret; +} +struct vwork {struct mm_struct *mm;long npage;@@ -270,12 +390,13 @@ static int vaddr_get_pfn(struct mm_struct *remote_mm, unsigned long vaddr,* the iommu can only map chunks of consecutive pfns anyway, so get the * first page and all consecutive pages with the same locking. */
drivers/vfio/vfio_iommu_type1.c static int vaddr_get_pfn(struct mm_struct *remote_mm, unsigned long vaddr, · line 399
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +4
-3-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 ;
Initialization to zero is unnecessary here
Quoted 39 lines
drivers/vfio/vfio_iommu_type1.c +9
-5bool rsvd;if (!current->mm)@@ -285,9 +406,11 @@ static long vfio_pin_pages(unsigned long vaddr, long npage,if (ret)return ret; + lock_acct =vfio_pfn_account (iommu, *pfn_base); + rsvd =is_invalid_reserved_pfn (*pfn_base); -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 );@@ -314,8 +437,10 @@ static long vfio_pin_pages(unsigned long vaddr, long npage,break ; } + lock_acct +=vfio_pfn_account (iommu, pfn); +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 );@@ -329,14 +454,19 @@ static long vfio_pin_pages(unsigned long vaddr, long npage,return i; } -static long vfio_unpin_pages (unsigned long pfn,long npage, -int prot,bool do_accounting) +static long __vfio_unpin_pages_remote (struct vfio_iommu *iommu, +unsigned long pfn,long npage,int prot, +bool do_accounting) { -unsigned long unlocked =0 ;
drivers/vfio/vfio_iommu_type1.c static long vfio_pin_pages(unsigned long vaddr, long npage, · line 469
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +6
-1+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); + }
Something is wrong here, we never do anything with unlock_acct
Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +0
-0if (do_accounting)vfio_lock_acct (current, -unlocked);@@ -344,14 +474,208 @@ static long vfio_unpin_pages(unsigned long pfn, long npage,return unlocked;
drivers/vfio/vfio_iommu_type1.c static long vfio_unpin_pages(unsigned long pfn, long npage, · line 483
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +7
-1} -static void vfio_unmap_unpin (struct vfio_iommu *iommu,struct vfio_dma *dma) +static long __vfio_pin_page_external (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 );
capable() checks if the current user has this capability, but this path is not user invoked. Whose capabilities are we testing here?
drivers/vfio/vfio_iommu_type1.c static long vfio_unpin_pages(unsigned long pfn, long npage, · line 486
Quoted 3 lines
drivers/vfio/vfio_iommu_type1.c +3
-0+long ret; +bool rsvd; +struct task_struct *task = domain->external_addr_space->task;
@task is set from @current when the group is attached, did we get a reference to that task, how do we know it's still valid? Could a given vfio_dma use a different task_struct? I think these are the kinds of issues Alexey was noting.
Quoted 4 lines
drivers/vfio/vfio_iommu_type1.c +4
-0+ +if (!task->mm) +return -ENODEV ; +
drivers/vfio/vfio_iommu_type1.c static long vfio_unpin_pages(unsigned long pfn, long npage, · line 500
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10
-0+ 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 );
This happens when the vendor driver tries to pin pages, asynchronous to any discernible user action, what are we supposed to do when we see this in dmesg? How can we actually figure out what this is associated with?
Quoted 7 lines
drivers/vfio/vfio_iommu_type1.c +7
-0+return -ENOMEM ; + } + +if (!rsvd && do_accounting) +vfio_lock_acct (task,1 ); + +return 1 ;
drivers/vfio/vfio_iommu_type1.c static long vfio_unpin_pages(unsigned long pfn, long npage, · line 517
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10
-0+} + +static void __vfio_unpin_page_external (struct vfio_domain *domain, +unsigned long pfn,int prot, +bool do_accounting) +{ +put_pfn (pfn, prot); + +if (do_accounting) +vfio_lock_acct (domain->external_addr_space->task,-1 );
What if this task no longer exists.
Quoted 65 lines
drivers/vfio/vfio_iommu_type1.c +65
-0+} + +static int vfio_unpin_pfn (struct vfio_domain *domain, +struct vfio_pfn *vpfn,bool do_accounting) +{ +__vfio_unpin_page_external (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; +struct external_addr_space *addr_space; +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->external_domain) { + ret = -EINVAL ; + goto pin_done; + } + + domain = iommu->external_domain; + addr_space = domain->external_addr_space; +/* +* 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_external (domain, remote_vaddr, prot, + &pfn[i], do_accounting); +if (retpage <=0 ) {
drivers/vfio/vfio_iommu_type1.c static long vfio_unpin_pages(unsigned long pfn, long npage, · line 592
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10
-0+WARN_ON (!retpage); + ret = (int )retpage; + goto pin_unwind; + } + +mutex_lock (&addr_space->pfn_list_lock); + +/* search if pfn exist */ + p =vfio_find_pfn (domain, pfn[i]); +if (p) {
if @do_accounting is true, aren't we now double accounting this pfn. I think we're trying to pin it twice, but not account for it twice. Also the problem with what if the user is running right at their locked memory limit and the vendor driver pins a page (maybe it's already pinned by another vendor driver), they're going to cross the limit when really that page has already been accounted.
Quoted 325 lines
drivers/vfio/vfio_iommu_type1.c +188
-45+atomic_inc (&p->ref_count); +mutex_unlock (&addr_space->pfn_list_lock); +continue ; + } + + ret =vfio_add_to_pfn_list (domain, remote_vaddr, iova, + pfn[i], prot); +mutex_unlock (&addr_space->pfn_list_lock); + +if (ret) { +__vfio_unpin_page_external (domain, pfn[i], prot, + do_accounting); + goto pin_unwind; + } + } + + ret = i; + goto pin_done; + +pin_unwind: + pfn[i] =0 ; +mutex_lock (&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 (&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->external_domain; + + do_accounting = !IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu); + +mutex_lock (&domain->external_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->external_addr_space->pfn_list_lock); + +mutex_unlock (&iommu->lock); +return unlocked; +} + +static long vfio_unmap_unpin (struct vfio_iommu *iommu,struct vfio_dma *dma, +bool do_accounting) {dma_addr_t iova = dma->iova, end = dma->iova + dma->size;struct vfio_domain *domain, *d;long unlocked =0 ;if (!dma->size) -return ; +return 0 ; + +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) +return 0 ; +/* * We use the IOMMU to track the physical addresses, otherwise we'd * need a much more complicated tracking system. Unfortunately that @@ -393,20 +717,27 @@ 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 (current, -unlocked); + dma->iommu_mapped = false; + +if (do_accounting) { +vfio_lock_acct (current, -unlocked); +return 0 ; + } + +return unlocked; }static void vfio_remove_dma (struct vfio_iommu *iommu,struct vfio_dma *dma) { -vfio_unmap_unpin (iommu, dma); +vfio_unmap_unpin (iommu, dma, true);vfio_unlink_dma (iommu, dma);kfree (dma); }@@ -569,17 +900,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)@@ -622,29 +993,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;@@ -673,10 +1026,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;@@ -685,20 +1034,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); - size =PAGE_SIZE ; +if (WARN_ON (!phys)) { + iova +=PAGE_SIZE ; +continue ; + } -while (iova + size < dma->iova + dma->size && - phys + size ==iommu_iova_to_phys (d->domain, + size =PAGE_SIZE ; + +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);@@ -707,6 +1079,8 @@ static int vfio_iommu_replay(struct vfio_iommu *iommu, iova += size; } + + dma->iommu_mapped = true; }return 0 ;@@ -764,7 +1138,7 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,struct vfio_iommu *iommu = iommu_data;struct vfio_group *group;struct vfio_domain *domain, *d; -struct bus_type *bus =NULL ; +struct bus_type *bus =NULL , *mdev_bus;int ret;mutex_lock (&iommu->lock);@@ -776,6 +1150,13 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, } } +if (iommu->external_domain) { +if (find_iommu_group (iommu->external_domain, iommu_group)) { +mutex_unlock (&iommu->lock); +return -EINVAL ; + } + } + group =kzalloc (sizeof (*group),GFP_KERNEL ); domain =kzalloc (sizeof (*domain),GFP_KERNEL );if (!group || !domain) {@@ -790,6 +1171,38 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,if (ret) goto out_free; + mdev_bus =symbol_get (mdev_bus_type); + +if (mdev_bus) { +if ((bus == mdev_bus) && !iommu_present (bus)) { +symbol_put (mdev_bus_type); +if (!iommu->external_domain) { +struct external_addr_space *addr_space;
drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_attach_group(void *iommu_data, · line 1190
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10
-0+ + addr_space =kzalloc (sizeof (*addr_space), +GFP_KERNEL ); +if (!addr_space) { + ret = -ENOMEM ; + goto out_free; + } + +INIT_LIST_HEAD (&domain->group_list); + addr_space->task = current;
Regarding current here, could one process open the vfio container and vfio group and perform this operation, then pass the file descriptors for each to another task via SCM_RIGHTS? I don't think we can prevent it. Wouldn't that mean the vendor driver is pinning pages in the wrong task_struct? Do we need to track (and reference) the task_struct for each vfio_dma? Maybe we need this anyway so that only the task_struct that performed a DMA_MAP can do a DMA_UNMAP of that range.
Quoted 183 lines
drivers/vfio/vfio_iommu_type1.c +113
-15+ addr_space->pfn_list =RB_ROOT ; +mutex_init (&addr_space->pfn_list_lock); + domain->external_addr_space = addr_space; + iommu->external_domain = domain; + }else +kfree (domain); + +list_add (&group->next, + &iommu->external_domain->group_list); +mutex_unlock (&iommu->lock); +return 0 ; + } +symbol_put (mdev_bus_type); + } + domain->domain =iommu_domain_alloc (bus);if (!domain->domain) { ret = -EIO ;@@ -880,6 +1293,42 @@ 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->external_domain; +struct vfio_dma *dma, *tdma; +struct rb_node *n; +long locked =0 , unlocked =0 ; + +rbtree_postorder_for_each_entry_safe (dma, tdma, &iommu->dma_list, + node) { + unlocked +=vfio_unmap_unpin (iommu, dma, false); + } + +mutex_lock (&domain->external_addr_space->pfn_list_lock); + + n =rb_first (&domain->external_addr_space->pfn_list); + +for (; n; n =rb_next (n)) + locked++; + +vfio_lock_acct (domain->external_addr_space->task, locked - unlocked); +mutex_unlock (&domain->external_addr_space->pfn_list_lock); +} + +static void vfio_external_unpin_all (struct vfio_domain *domain, +bool do_accounting) +{ +struct rb_node *node; + +mutex_lock (&domain->external_addr_space->pfn_list_lock); +while ((node =rb_first (&domain->external_addr_space->pfn_list))) + vfio_unpin_pfn(domain, rb_entry(node, struct vfio_pfn, node), + do_accounting); + +mutex_unlock (&domain->external_addr_space->pfn_list_lock); +} +static void vfio_iommu_type1_detach_group (void *iommu_data,struct iommu_group *iommu_group) {@@ -889,6 +1338,28 @@ static void vfio_iommu_type1_detach_group(void *iommu_data,mutex_lock (&iommu->lock); +if (iommu->external_domain) { + domain = iommu->external_domain; + group =find_iommu_group (domain, iommu_group); +if (group) { +list_del (&group->next); +kfree (group); + +if (list_empty (&domain->group_list)) { +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { +vfio_external_unpin_all (domain, true); +vfio_iommu_unmap_unpin_all (iommu); + }else +vfio_external_unpin_all (domain, false); +kfree (domain); + iommu->external_domain =NULL ; + } + goto detach_group_done; + } + } + +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) + goto detach_group_done;list_for_each_entry (domain, &iommu->domain_list, next) { group =find_iommu_group (domain, iommu_group);@@ -899,21 +1370,27 @@ static void vfio_iommu_type1_detach_group(void *iommu_data,list_del (&group->next);kfree (group);/* -* Group ownership provides privilege, if the group -* list is empty, the domain goes away. If it's the -* last domain, then all the mappings go away too. +* Group ownership provides privilege, if the group list is +* empty, the domain goes away. If it's the last domain with +* iommu and external domain doesn't exist, then all the +* mappings go away too. If it's the last domain with iommu and +* external domain exist, update accounting */ if (list_empty (&domain->group_list)) { -if (list_is_singular (&iommu->domain_list)) -vfio_iommu_unmap_unpin_all (iommu); +if (list_is_singular (&iommu->domain_list)) { +if (!iommu->external_domain) +vfio_iommu_unmap_unpin_all (iommu); +else +vfio_iommu_unmap_unpin_reaccount (iommu); + }iommu_domain_free (domain->domain);list_del (&domain->next);kfree (domain); } - goto done; +break ; } -done: +detach_group_done:mutex_unlock (&iommu->lock); }@@ -945,23 +1422,40 @@ 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->external_addr_space) +iommu_detach_group (domain->domain, group->iommu_group); +list_del (&group->next); +kfree (group); + } + +if (domain->external_addr_space) +vfio_external_unpin_all (domain, false); +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; + +if (iommu->external_domain) { +vfio_release_domain (iommu->external_domain); +kfree (iommu->external_domain); + iommu->external_domain =NULL ; + }vfio_iommu_unmap_unpin_all (iommu);list_for_each_entry_safe (domain, domain_tmp, &iommu->domain_list, next) { -list_for_each_entry_safe (group, group_tmp, - &domain->group_list, next) { -iommu_detach_group (domain->domain, group->iommu_group); -list_del (&group->next); -kfree (group); - } -iommu_domain_free (domain->domain); +vfio_release_domain (domain);list_del (&domain->next);kfree (domain); }@@ -1069,6 +1563,8 @@ static const struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1 = { .ioctl = vfio_iommu_type1_ioctl, .attach_group = vfio_iommu_type1_attach_group, .detach_group = vfio_iommu_type1_detach_group, + .pin_pages = vfio_iommu_type1_pin_pages, + .unpin_pages = vfio_iommu_type1_unpin_pages, };static int __init vfio_iommu_type1_init (void )
Re: [PATCH v10 09/19] vfio iommu type1: Add support for mediated devices
In reply to Kirti WankhedeQuoted 290 lines · Kirti Wankhede
VFIO IOMMU drivers are designed for the devices which are IOMMU capable. Mediated device only uses IOMMU APIs, the underlying hardware can be managed by an IOMMU domain.
Aim of this change is:
- To use most of the code of TYPE1 IOMMU driver for mediated devices
- To support direct assigned device and mediated device in single moduleThis change adds pin and unpin support for mediated device to TYPE1 IOMMU backend module. More details:
- Wheniommu_groupof 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 existSigned-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id I295d6f0f2e0579b8d9882bfd8fd5a4194b97bd9a--- drivers/vfio/vfio_iommu_type1.c | 646 +++++++++++++++++++++++++++++++++++----- 1 file changed, 571 insertions(+), 75 deletions(-)drivers/vfio/vfio_iommu_type1.c +149
-15@@ -36,6 +36,7 @@#include <linux/uaccess.h> #include <linux/vfio.h> #include <linux/workqueue.h> +#include <linux/mdev.h> #define DRIVER_VERSION "0.2"#define DRIVER_AUTHOR "Alex Williamson <[email protected]>"@@ -55,18 +56,26 @@ MODULE_PARM_DESC(disable_hugepages,struct vfio_iommu {struct list_head domain_list; +struct vfio_domain *external_domain;/* domain for external user */ struct mutex lock;struct rb_root dma_list;bool v2;bool nesting; }; +struct external_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; -int prot;/* IOMMU_CACHE */ -bool fgsp;/* Fine-grained super pages */ +struct iommu_domain *domain; +struct list_head next; +struct list_head group_list; +struct external_addr_space *external_addr_space; +int prot;/* IOMMU_CACHE */ +bool fgsp;/* Fine-grained super pages */ };struct vfio_dma {@@ -75,6 +84,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 +93,21 @@ struct vfio_group { };/* +* 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; +}; + +#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 +155,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->external_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->external_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->external_addr_space->pfn_list); +} + +static void vfio_unlink_pfn (struct vfio_domain *domain,struct vfio_pfn *old) +{ +rb_erase (&old->node, &domain->external_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->external_domain; +int ret =1 ; + +if (!domain) +return 1 ; + +mutex_lock (&domain->external_addr_space->pfn_list_lock); + + p =vfio_find_pfn (domain, pfn); +if (p) + ret =0 ; + +mutex_unlock (&domain->external_addr_space->pfn_list_lock); +return ret; +} +struct vwork {struct mm_struct *mm;long npage;@@ -270,12 +390,13 @@ static int vaddr_get_pfn(struct mm_struct *remote_mm, unsigned long vaddr,* the iommu can only map chunks of consecutive pfns anyway, so get the * first page and all consecutive pages with the same locking. */ -static long vfio_pin_pages (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)@@ -285,9 +406,11 @@ static long vfio_pin_pages(unsigned long vaddr, long npage,if (ret)return ret; + lock_acct =vfio_pfn_account (iommu, *pfn_base); + rsvd =is_invalid_reserved_pfn (*pfn_base); -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 );@@ -314,8 +437,10 @@ static long vfio_pin_pages(unsigned long vaddr, long npage,break ; } + lock_acct +=vfio_pfn_account (iommu, pfn); +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 );@@ -329,14 +454,19 @@ static long vfio_pin_pages(unsigned long vaddr, long npage,return i; } -static long vfio_unpin_pages (unsigned long pfn,long npage, -int prot,bool do_accounting) +static long __vfio_unpin_pages_remote (struct vfio_iommu *iommu, +unsigned long pfn,long npage,int prot, +bool do_accounting) { -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 (current, -unlocked);@@ -344,14 +474,208 @@ static long vfio_unpin_pages(unsigned long pfn, long npage,return unlocked; } -static void vfio_unmap_unpin (struct vfio_iommu *iommu,struct vfio_dma *dma) +static long __vfio_pin_page_external (struct vfio_domain *domain, +unsigned long vaddr,int prot, +unsigned long *pfn_base, +bool do_accounting) +{
drivers/vfio/vfio_iommu_type1.c static long vfio_unpin_pages(unsigned long pfn, long npage, · line 491
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10
-0+unsigned long limit =rlimit (RLIMIT_MEMLOCK ) >>PAGE_SHIFT ; +bool lock_cap =capable (CAP_IPC_LOCK ); +long ret; +bool rsvd; +struct task_struct *task = domain->external_addr_space->task; + +if (!task->mm) +return -ENODEV ; + + ret =vaddr_get_pfn (task->mm, vaddr, prot, pfn_base);
Hi Kirti,
In your implementation of vaddr_get_pfn, as long as the 1st argument is not NULL, you will call get_user_pages_remote other than get_user_pages_fast.
But still there is a possibility here @task is actually @current, thereby task->mm == current->mm. That should not be rare, but probably happen at most times: the QEMU process emulates MMIO read/write, and call vfio_pin_pages in its process context.
If I read correctly, current implementation enforces a locked GUP, no matter which process context it is in.
--
Thanks,
Jike
Kirti Wankhede
maintainerPATCH v10 10/19 vfio iommu: Add blocking notifier to notify DMA_UNMAP
In reply to Kirti WankhedeAdded blocking notifier to IOMMU TYPE1 driver to notify vendor drivers about DMA_UNMAP.
Exported two APIs vfio_register_notifier() and vfio_unregister_notifier(). Vendor driver should register notifer using these APIs.
Vendor driver should use VFIO_IOMMU_NOTIFY_DMA_UNMAP action to invalidate mappings.
--- drivers/vfio/vfio.c | 73 +++++++++++++++++++++++++++++++++ drivers/vfio/vfio_iommu_type1.c | 89 ++++++++++++++++++++++++++++++++++++----- include/linux/vfio.h | 11 +++++ 3 files changed, 163 insertions(+), 10 deletions(-)
drivers/vfio/vfio.c +73 -0
@@ -1891,6 +1891,79 @@ err_unpin_pages: }EXPORT_SYMBOL (vfio_unpin_pages); +int vfio_register_notifier (struct device *dev,struct notifier_block *nb) +{ +struct vfio_container *container; +struct vfio_group *group; +struct vfio_iommu_driver *driver; +ssize_t ret; + +if (!dev || !nb) +return -EINVAL ; + + group =vfio_group_get_from_dev (dev); +if (IS_ERR (group)) +return PTR_ERR (group); + + ret =vfio_group_add_container_user (group); +if (ret) + goto err_register_nb; + + container = group->container; +down_read (&container->group_lock); + + driver = container->iommu_driver; +if (likely (driver && driver->ops->register_notifier)) + ret = driver->ops->register_notifier (container->iommu_data, nb); +else + ret = -EINVAL ; + +up_read (&container->group_lock); +vfio_group_try_dissolve_container (group); + +err_register_nb: +vfio_group_put (group); +return ret; +} +EXPORT_SYMBOL (vfio_register_notifier); + +int vfio_unregister_notifier (struct device *dev,struct notifier_block *nb) +{ +struct vfio_container *container; +struct vfio_group *group; +struct vfio_iommu_driver *driver; +ssize_t ret; + +if (!dev || !nb) +return -EINVAL ; + + group =vfio_group_get_from_dev (dev); +if (IS_ERR (group)) +return PTR_ERR (group); + + ret =vfio_group_add_container_user (group); +if (ret) + goto err_unregister_nb; + + container = group->container; +down_read (&container->group_lock); + + driver = container->iommu_driver; +if (likely (driver && driver->ops->unregister_notifier)) + ret = driver->ops->unregister_notifier (container->iommu_data, + nb); +else + ret = -EINVAL ; + +up_read (&container->group_lock); +vfio_group_try_dissolve_container (group); + +err_unregister_nb: +vfio_group_put (group); +return ret; +} +EXPORT_SYMBOL (vfio_unregister_notifier); +/** * Module/class support */
drivers/vfio/vfio_iommu_type1.c +79 -10
132 lines not on this page · raw message
include/linux/vfio.h +11 -0
@@ -80,6 +80,10 @@ struct vfio_iommu_driver_ops { unsigned long *phys_pfn);long (*unpin_pages)(void *iommu_data,unsigned long *pfn,long npage); +int (*register_notifier)(void *iommu_data, +struct notifier_block *nb); +int (*unregister_notifier)(void *iommu_data, +struct notifier_block *nb); };extern int vfio_register_iommu_driver (const struct vfio_iommu_driver_ops *ops);
@@ -137,6 +141,13 @@ extern long vfio_pin_pages(struct device *dev, unsigned long *user_pfn,extern long vfio_unpin_pages (struct device *dev,unsigned long *pfn,long npage); +#define VFIO_IOMMU_NOTIFY_DMA_UNMAP 1 + +extern int vfio_register_notifier (struct device *dev, +struct notifier_block *nb); + +extern int vfio_unregister_notifier (struct device *dev, +struct notifier_block *nb);/* * IRQfd - generic */
Signature · 1 line
2.7.0
Re: [PATCH v10 10/19] vfio iommu: Add blocking notifier to notify DMA_UNMAP
Quoted 163 lines · Kirti Wankhede
Added blocking notifier to IOMMU TYPE1 driver to notify vendor drivers about
DMA_UNMAP.
Exported two APIsvfio_register_notifier()andvfio_unregister_notifier(). Vendor driver should register notifer using these APIs.
Vendor driver should useVFIO_IOMMU_NOTIFY_DMA_UNMAPaction to invalidate mappings.Signed-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id I5910d0024d6be87f3e8d3e0ca0eaeaaa0b17f271--- drivers/vfio/vfio.c | 73 +++++++++++++++++++++++++++++++++ drivers/vfio/vfio_iommu_type1.c | 89 ++++++++++++++++++++++++++++++++++++----- include/linux/vfio.h | 11 +++++ 3 files changed, 163 insertions(+), 10 deletions(-)drivers/vfio/vfio.c +73
-0@@ -1891,6 +1891,79 @@ err_unpin_pages: }EXPORT_SYMBOL (vfio_unpin_pages); +int vfio_register_notifier (struct device *dev,struct notifier_block *nb) +{ +struct vfio_container *container; +struct vfio_group *group; +struct vfio_iommu_driver *driver; +ssize_t ret; + +if (!dev || !nb) +return -EINVAL ; + + group =vfio_group_get_from_dev (dev); +if (IS_ERR (group)) +return PTR_ERR (group); + + ret =vfio_group_add_container_user (group); +if (ret) + goto err_register_nb; + + container = group->container; +down_read (&container->group_lock); + + driver = container->iommu_driver; +if (likely (driver && driver->ops->register_notifier)) + ret = driver->ops->register_notifier (container->iommu_data, nb); +else + ret = -EINVAL ; + +up_read (&container->group_lock); +vfio_group_try_dissolve_container (group); + +err_register_nb: +vfio_group_put (group); +return ret; +} +EXPORT_SYMBOL (vfio_register_notifier); + +int vfio_unregister_notifier (struct device *dev,struct notifier_block *nb) +{ +struct vfio_container *container; +struct vfio_group *group; +struct vfio_iommu_driver *driver; +ssize_t ret; + +if (!dev || !nb) +return -EINVAL ; + + group =vfio_group_get_from_dev (dev); +if (IS_ERR (group)) +return PTR_ERR (group); + + ret =vfio_group_add_container_user (group); +if (ret) + goto err_unregister_nb; + + container = group->container; +down_read (&container->group_lock); + + driver = container->iommu_driver; +if (likely (driver && driver->ops->unregister_notifier)) + ret = driver->ops->unregister_notifier (container->iommu_data, + nb); +else + ret = -EINVAL ; + +up_read (&container->group_lock); +vfio_group_try_dissolve_container (group); + +err_unregister_nb: +vfio_group_put (group); +return ret; +} +EXPORT_SYMBOL (vfio_unregister_notifier); +/** * Module/class support */ drivers/vfio/vfio_iommu_type1.c +33
-1@@ -37,6 +37,7 @@#include <linux/vfio.h> #include <linux/workqueue.h> #include <linux/mdev.h> +#include <linux/notifier.h> #define DRIVER_VERSION "0.2"#define DRIVER_AUTHOR "Alex Williamson <[email protected]>"@@ -59,6 +60,7 @@ struct vfio_iommu {struct vfio_domain *external_domain;/* domain for external user */ struct mutex lock;struct rb_root dma_list; +struct blocking_notifier_head notifier;bool v2;bool nesting; };@@ -549,7 +551,8 @@ static long vfio_iommu_type1_pin_pages(void *iommu_data,mutex_lock (&iommu->lock); -if (!iommu->external_domain) { +/* Fail if notifier list is empty */ +if ((!iommu->external_domain) || (!iommu->notifier.head)) { ret = -EINVAL ; goto pin_done; }@@ -768,6 +771,50 @@ static unsigned long vfio_pgsize_bitmap(struct vfio_iommu *iommu)return bitmap; } +/* +* This function finds pfn in domain->external_addr_space->pfn_list for given +* iova range. If pfn exist, notify pfn to registered notifier list. On +* receiving notifier callback, vendor driver should invalidate the mapping and +* call vfio_unpin_pages() to unpin this pfn. With that vfio_pfn for this pfn +* gets removed from rb tree of pfn_list. That re-arranges rb tree, so while +* searching for next vfio_pfn in rb tree, start search from first node again. +* If any vendor driver doesn't unpin that pfn, vfio_pfn would not get removed +* from rb tree and so in next search vfio_pfn would be same as previous +* vfio_pfn. In that case, exit from loop. +*/ +static void vfio_notifier_call_chain (struct vfio_iommu *iommu, +struct vfio_iommu_type1_dma_unmap *unmap) +{ +struct vfio_domain *domain = iommu->external_domain; +struct rb_node *n; +struct vfio_pfn *vpfn =NULL , *prev_vpfn; + + do { + prev_vpfn = vpfn; + mutex_lock(&domain->external_addr_space->pfn_list_lock); + + n = rb_first(&domain->external_addr_space->pfn_list); + + for (; n; n = rb_next(n), vpfn = NULL) { + vpfn = rb_entry(n, struct vfio_pfn, node); + + if ((vpfn->iova >= unmap->iova) && + (vpfn->iova < unmap->iova + unmap->size))
drivers/vfio/vfio_iommu_type1.c static unsigned long vfio_pgsize_bitmap(struct vfio_iommu *iommu) · line 812
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10
-0+break ; + } + +mutex_unlock (&domain->external_addr_space->pfn_list_lock); + +/* Notify any listeners about DMA_UNMAP */ +if (vpfn) +blocking_notifier_call_chain (&iommu->notifier, +VFIO_IOMMU_NOTIFY_DMA_UNMAP , + &vpfn->pfn);
Hi Kirti,
The information carried by notifier is only a pfn.
Since your pin/unpin interfaces design, it's the vendor driver who should guarantee pin/unpin same times. To achieve that, the vendor driver must cache it's iova->pfn mapping on its side, to avoid pinning a same page for multiple times.
With the notifier carrying only a pfn, to find the iova by this pfn, the vendor driver must *also* keep a reverse-mapping. That's a bit too much.
Since the vendor could also suffer from IOMMU-compatible problem, which means a local cache is always helpful, so I'd like to have the iova carried to the notifier.
What'd you say?
--
Thanks,
Jike
Quoted 100 lines
drivers/vfio/vfio_iommu_type1.c +36
-9+ }while (vpfn && (prev_vpfn != vpfn)); + +WARN_ON (vpfn); +} +static int vfio_dma_do_unmap (struct vfio_iommu *iommu,struct vfio_iommu_type1_dma_unmap *unmap) {@@ -844,6 +891,9 @@ unlock:/* Report how much was unmapped */ unmap->size = unmapped; +if (unmapped && iommu->external_domain) +vfio_notifier_call_chain (iommu, unmap); +return ret; }@@ -1418,6 +1468,7 @@ static void *vfio_iommu_type1_open(unsigned long arg)INIT_LIST_HEAD (&iommu->domain_list); iommu->dma_list =RB_ROOT ;mutex_init (&iommu->lock); +BLOCKING_INIT_NOTIFIER_HEAD (&iommu->notifier);return iommu; }@@ -1555,16 +1606,34 @@ static long vfio_iommu_type1_ioctl(void *iommu_data,return -ENOTTY ; } +static int vfio_iommu_type1_register_notifier (void *iommu_data, +struct notifier_block *nb) +{ +struct vfio_iommu *iommu = iommu_data; + +return blocking_notifier_chain_register (&iommu->notifier, nb); +} + +static int vfio_iommu_type1_unregister_notifier (void *iommu_data, +struct notifier_block *nb) +{ +struct vfio_iommu *iommu = iommu_data; + +return blocking_notifier_chain_unregister (&iommu->notifier, nb); +} +static const struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1 = { - .name ="vfio-iommu-type1" , - .owner =THIS_MODULE , - .open = vfio_iommu_type1_open, - .release = vfio_iommu_type1_release, - .ioctl = vfio_iommu_type1_ioctl, - .attach_group = vfio_iommu_type1_attach_group, - .detach_group = vfio_iommu_type1_detach_group, - .pin_pages = vfio_iommu_type1_pin_pages, - .unpin_pages = vfio_iommu_type1_unpin_pages, + .name ="vfio-iommu-type1" , + .owner =THIS_MODULE , + .open = vfio_iommu_type1_open, + .release = vfio_iommu_type1_release, + .ioctl = vfio_iommu_type1_ioctl, + .attach_group = vfio_iommu_type1_attach_group, + .detach_group = vfio_iommu_type1_detach_group, + .pin_pages = vfio_iommu_type1_pin_pages, + .unpin_pages = vfio_iommu_type1_unpin_pages, + .register_notifier = vfio_iommu_type1_register_notifier, + .unregister_notifier = vfio_iommu_type1_unregister_notifier, };static int __init vfio_iommu_type1_init (void )include/linux/vfio.h +11
-0@@ -80,6 +80,10 @@ struct vfio_iommu_driver_ops { unsigned long *phys_pfn);long (*unpin_pages)(void *iommu_data,unsigned long *pfn,long npage); +int (*register_notifier)(void *iommu_data, +struct notifier_block *nb); +int (*unregister_notifier)(void *iommu_data, +struct notifier_block *nb); };extern int vfio_register_iommu_driver (const struct vfio_iommu_driver_ops *ops);@@ -137,6 +141,13 @@ extern long vfio_pin_pages(struct device *dev, unsigned long *user_pfn,extern long vfio_unpin_pages (struct device *dev,unsigned long *pfn,long npage); +#define VFIO_IOMMU_NOTIFY_DMA_UNMAP 1 + +extern int vfio_register_notifier (struct device *dev, +struct notifier_block *nb); + +extern int vfio_unregister_notifier (struct device *dev, +struct notifier_block *nb);/* * IRQfd - generic */
Kirti Wankhede
maintainerRe: [PATCH v10 10/19] vfio iommu: Add blocking notifier to notify DMA_UNMAP
Quoted 44 lines · Jike Song
Quoted 30 lines · Kirti Wankhede
drivers/vfio/vfio_iommu_type1.c +29
-0+/* +* This function finds pfn in domain->external_addr_space->pfn_list for given +* iova range. If pfn exist, notify pfn to registered notifier list. On +* receiving notifier callback, vendor driver should invalidate the mapping and +* call vfio_unpin_pages() to unpin this pfn. With that vfio_pfn for this pfn +* gets removed from rb tree of pfn_list. That re-arranges rb tree, so while +* searching for next vfio_pfn in rb tree, start search from first node again. +* If any vendor driver doesn't unpin that pfn, vfio_pfn would not get removed +* from rb tree and so in next search vfio_pfn would be same as previous +* vfio_pfn. In that case, exit from loop. +*/ +static void vfio_notifier_call_chain (struct vfio_iommu *iommu, +struct vfio_iommu_type1_dma_unmap *unmap) +{ +struct vfio_domain *domain = iommu->external_domain; +struct rb_node *n; +struct vfio_pfn *vpfn =NULL , *prev_vpfn; + + do { + prev_vpfn = vpfn; + mutex_lock(&domain->external_addr_space->pfn_list_lock); + + n = rb_first(&domain->external_addr_space->pfn_list); + + for (; n; n = rb_next(n), vpfn = NULL) { + vpfn = rb_entry(n, struct vfio_pfn, node); + + if ((vpfn->iova >= unmap->iova) && + (vpfn->iova < unmap->iova + unmap->size))drivers/vfio/vfio_iommu_type1.c static unsigned long vfio_pgsize_bitmap(struct vfio_iommu *iommu) · line 812
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10
-0+break ; + } + +mutex_unlock (&domain->external_addr_space->pfn_list_lock); + +/* Notify any listeners about DMA_UNMAP */ +if (vpfn) +blocking_notifier_call_chain (&iommu->notifier, +VFIO_IOMMU_NOTIFY_DMA_UNMAP , + &vpfn->pfn);Hi Kirti,
Quoted 4 lines
The information carried by notifier is only a pfn.
Since your pin/unpin interfaces design, it's the vendor driver who should guarantee pin/unpin same times.
That because there could be multiple mdev devices from different vendor driver in a domain, and pin request for a page can come from multiple vendor driver for same page and in pin/unpin page interface we don't keep track of who had pinned pages. So its vendor driver's responsibility to unpin pages which he had pinned.
Quoted 4 lines
To achieve that, the vendor driver must
cache it'siova->pfnmapping on its side, to avoid pinning a same page for multiple times.
That's right.
Quoted 4 lines
With the notifier carrying only a pfn, to find the iova by this pfn, the vendor driver must *also* keep a reverse-mapping. That's a bit too much.
Don't know what the reserve mapping is, vendor should know the pfn that he has previously pinned to call the unpin anyway so there is no need to duplicate the logic here.
Kirti
Quoted 6 lines
Since the vendor could also suffer from IOMMU-compatible problem, which means a local cache is always helpful, so I'd like to have the iova carried to the notifier.
What'd you say?
Re: [PATCH v10 10/19] vfio iommu: Add blocking notifier to notify DMA_UNMAP
In reply to Jike SongQuoted 188 lines · On Fri, 28 Oct 2016 15:33:58 +0800 Jike Song <[email protected]> wrote:
Quoted 163 lines · Kirti Wankhede
Added blocking notifier to IOMMU TYPE1 driver to notify vendor drivers about
DMA_UNMAP.
Exported two APIsvfio_register_notifier()andvfio_unregister_notifier(). Vendor driver should register notifer using these APIs.
Vendor driver should useVFIO_IOMMU_NOTIFY_DMA_UNMAPaction to invalidate mappings.Signed-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id I5910d0024d6be87f3e8d3e0ca0eaeaaa0b17f271--- drivers/vfio/vfio.c | 73 +++++++++++++++++++++++++++++++++ drivers/vfio/vfio_iommu_type1.c | 89 ++++++++++++++++++++++++++++++++++++----- include/linux/vfio.h | 11 +++++ 3 files changed, 163 insertions(+), 10 deletions(-)drivers/vfio/vfio.c +73
-0@@ -1891,6 +1891,79 @@ err_unpin_pages: }EXPORT_SYMBOL (vfio_unpin_pages); +int vfio_register_notifier (struct device *dev,struct notifier_block *nb) +{ +struct vfio_container *container; +struct vfio_group *group; +struct vfio_iommu_driver *driver; +ssize_t ret; + +if (!dev || !nb) +return -EINVAL ; + + group =vfio_group_get_from_dev (dev); +if (IS_ERR (group)) +return PTR_ERR (group); + + ret =vfio_group_add_container_user (group); +if (ret) + goto err_register_nb; + + container = group->container; +down_read (&container->group_lock); + + driver = container->iommu_driver; +if (likely (driver && driver->ops->register_notifier)) + ret = driver->ops->register_notifier (container->iommu_data, nb); +else + ret = -EINVAL ; + +up_read (&container->group_lock); +vfio_group_try_dissolve_container (group); + +err_register_nb: +vfio_group_put (group); +return ret; +} +EXPORT_SYMBOL (vfio_register_notifier); + +int vfio_unregister_notifier (struct device *dev,struct notifier_block *nb) +{ +struct vfio_container *container; +struct vfio_group *group; +struct vfio_iommu_driver *driver; +ssize_t ret; + +if (!dev || !nb) +return -EINVAL ; + + group =vfio_group_get_from_dev (dev); +if (IS_ERR (group)) +return PTR_ERR (group); + + ret =vfio_group_add_container_user (group); +if (ret) + goto err_unregister_nb; + + container = group->container; +down_read (&container->group_lock); + + driver = container->iommu_driver; +if (likely (driver && driver->ops->unregister_notifier)) + ret = driver->ops->unregister_notifier (container->iommu_data, + nb); +else + ret = -EINVAL ; + +up_read (&container->group_lock); +vfio_group_try_dissolve_container (group); + +err_unregister_nb: +vfio_group_put (group); +return ret; +} +EXPORT_SYMBOL (vfio_unregister_notifier); +/** * Module/class support */ drivers/vfio/vfio_iommu_type1.c +33
-1@@ -37,6 +37,7 @@#include <linux/vfio.h> #include <linux/workqueue.h> #include <linux/mdev.h> +#include <linux/notifier.h> #define DRIVER_VERSION "0.2"#define DRIVER_AUTHOR "Alex Williamson <[email protected]>"@@ -59,6 +60,7 @@ struct vfio_iommu {struct vfio_domain *external_domain;/* domain for external user */ struct mutex lock;struct rb_root dma_list; +struct blocking_notifier_head notifier;bool v2;bool nesting; };@@ -549,7 +551,8 @@ static long vfio_iommu_type1_pin_pages(void *iommu_data,mutex_lock (&iommu->lock); -if (!iommu->external_domain) { +/* Fail if notifier list is empty */ +if ((!iommu->external_domain) || (!iommu->notifier.head)) { ret = -EINVAL ; goto pin_done; }@@ -768,6 +771,50 @@ static unsigned long vfio_pgsize_bitmap(struct vfio_iommu *iommu)return bitmap; } +/* +* This function finds pfn in domain->external_addr_space->pfn_list for given +* iova range. If pfn exist, notify pfn to registered notifier list. On +* receiving notifier callback, vendor driver should invalidate the mapping and +* call vfio_unpin_pages() to unpin this pfn. With that vfio_pfn for this pfn +* gets removed from rb tree of pfn_list. That re-arranges rb tree, so while +* searching for next vfio_pfn in rb tree, start search from first node again. +* If any vendor driver doesn't unpin that pfn, vfio_pfn would not get removed +* from rb tree and so in next search vfio_pfn would be same as previous +* vfio_pfn. In that case, exit from loop. +*/ +static void vfio_notifier_call_chain (struct vfio_iommu *iommu, +struct vfio_iommu_type1_dma_unmap *unmap) +{ +struct vfio_domain *domain = iommu->external_domain; +struct rb_node *n; +struct vfio_pfn *vpfn =NULL , *prev_vpfn; + + do { + prev_vpfn = vpfn; + mutex_lock(&domain->external_addr_space->pfn_list_lock); + + n = rb_first(&domain->external_addr_space->pfn_list); + + for (; n; n = rb_next(n), vpfn = NULL) { + vpfn = rb_entry(n, struct vfio_pfn, node); + + if ((vpfn->iova >= unmap->iova) && + (vpfn->iova < unmap->iova + unmap->size))drivers/vfio/vfio_iommu_type1.c static unsigned long vfio_pgsize_bitmap(struct vfio_iommu *iommu) · line 812
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10
-0+break ; + } + +mutex_unlock (&domain->external_addr_space->pfn_list_lock); + +/* Notify any listeners about DMA_UNMAP */ +if (vpfn) +blocking_notifier_call_chain (&iommu->notifier, +VFIO_IOMMU_NOTIFY_DMA_UNMAP , + &vpfn->pfn);Hi Kirti,
The information carried by notifier is only a pfn.
Since your pin/unpin interfaces design, it's the vendor driver who should guarantee pin/unpin same times. To achieve that, the vendor driver must cache it's
iova->pfnmapping on its side, to avoid pinning a same page for multiple times.With the notifier carrying only a pfn, to find the iova by this pfn, the vendor driver must *also* keep a reverse-mapping. That's a bit too much.
Quoted 5 lines
Since the vendor could also suffer from IOMMU-compatible problem, which means a local cache is always helpful, so I'd like to have the iova carried to the notifier.
What'd you say?
I agree, the pfn is not unique, multiple guest pfns (iovas) might be backed by the same host pfn. DMA_UNMAP calls are based on iova, the notifier through to the vendor driver must be based on the same. Thanks,
Alex
Quoted 101 lines
Quoted 100 lines
drivers/vfio/vfio_iommu_type1.c +36
-9+ }while (vpfn && (prev_vpfn != vpfn)); + +WARN_ON (vpfn); +} +static int vfio_dma_do_unmap (struct vfio_iommu *iommu,struct vfio_iommu_type1_dma_unmap *unmap) {@@ -844,6 +891,9 @@ unlock:/* Report how much was unmapped */ unmap->size = unmapped; +if (unmapped && iommu->external_domain) +vfio_notifier_call_chain (iommu, unmap); +return ret; }@@ -1418,6 +1468,7 @@ static void *vfio_iommu_type1_open(unsigned long arg)INIT_LIST_HEAD (&iommu->domain_list); iommu->dma_list =RB_ROOT ;mutex_init (&iommu->lock); +BLOCKING_INIT_NOTIFIER_HEAD (&iommu->notifier);return iommu; }@@ -1555,16 +1606,34 @@ static long vfio_iommu_type1_ioctl(void *iommu_data,return -ENOTTY ; } +static int vfio_iommu_type1_register_notifier (void *iommu_data, +struct notifier_block *nb) +{ +struct vfio_iommu *iommu = iommu_data; + +return blocking_notifier_chain_register (&iommu->notifier, nb); +} + +static int vfio_iommu_type1_unregister_notifier (void *iommu_data, +struct notifier_block *nb) +{ +struct vfio_iommu *iommu = iommu_data; + +return blocking_notifier_chain_unregister (&iommu->notifier, nb); +} +static const struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1 = { - .name ="vfio-iommu-type1" , - .owner =THIS_MODULE , - .open = vfio_iommu_type1_open, - .release = vfio_iommu_type1_release, - .ioctl = vfio_iommu_type1_ioctl, - .attach_group = vfio_iommu_type1_attach_group, - .detach_group = vfio_iommu_type1_detach_group, - .pin_pages = vfio_iommu_type1_pin_pages, - .unpin_pages = vfio_iommu_type1_unpin_pages, + .name ="vfio-iommu-type1" , + .owner =THIS_MODULE , + .open = vfio_iommu_type1_open, + .release = vfio_iommu_type1_release, + .ioctl = vfio_iommu_type1_ioctl, + .attach_group = vfio_iommu_type1_attach_group, + .detach_group = vfio_iommu_type1_detach_group, + .pin_pages = vfio_iommu_type1_pin_pages, + .unpin_pages = vfio_iommu_type1_unpin_pages, + .register_notifier = vfio_iommu_type1_register_notifier, + .unregister_notifier = vfio_iommu_type1_unregister_notifier, };static int __init vfio_iommu_type1_init (void )include/linux/vfio.h +11
-0@@ -80,6 +80,10 @@ struct vfio_iommu_driver_ops { unsigned long *phys_pfn);long (*unpin_pages)(void *iommu_data,unsigned long *pfn,long npage); +int (*register_notifier)(void *iommu_data, +struct notifier_block *nb); +int (*unregister_notifier)(void *iommu_data, +struct notifier_block *nb); };extern int vfio_register_iommu_driver (const struct vfio_iommu_driver_ops *ops);@@ -137,6 +141,13 @@ extern long vfio_pin_pages(struct device *dev, unsigned long *user_pfn,extern long vfio_unpin_pages (struct device *dev,unsigned long *pfn,long npage); +#define VFIO_IOMMU_NOTIFY_DMA_UNMAP 1 + +extern int vfio_register_notifier (struct device *dev, +struct notifier_block *nb); + +extern int vfio_unregister_notifier (struct device *dev, +struct notifier_block *nb);/* * IRQfd - generic */
Kirti Wankhede
maintainerRe: [PATCH v10 10/19] vfio iommu: Add blocking notifier to notify DMA_UNMAP
Quoted 195 lines · Alex Williamson
Quoted 192 lines · On Fri, 28 Oct 2016 15:33:58 +0800 Jike Song <[email protected]> wrote:
Quoted 163 lines · Kirti Wankhede
Added blocking notifier to IOMMU TYPE1 driver to notify vendor drivers about
DMA_UNMAP.
Exported two APIsvfio_register_notifier()andvfio_unregister_notifier(). Vendor driver should register notifer using these APIs.
Vendor driver should useVFIO_IOMMU_NOTIFY_DMA_UNMAPaction to invalidate mappings.Signed-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id I5910d0024d6be87f3e8d3e0ca0eaeaaa0b17f271--- drivers/vfio/vfio.c | 73 +++++++++++++++++++++++++++++++++ drivers/vfio/vfio_iommu_type1.c | 89 ++++++++++++++++++++++++++++++++++++----- include/linux/vfio.h | 11 +++++ 3 files changed, 163 insertions(+), 10 deletions(-)drivers/vfio/vfio.c +73
-0@@ -1891,6 +1891,79 @@ err_unpin_pages: }EXPORT_SYMBOL (vfio_unpin_pages); +int vfio_register_notifier (struct device *dev,struct notifier_block *nb) +{ +struct vfio_container *container; +struct vfio_group *group; +struct vfio_iommu_driver *driver; +ssize_t ret; + +if (!dev || !nb) +return -EINVAL ; + + group =vfio_group_get_from_dev (dev); +if (IS_ERR (group)) +return PTR_ERR (group); + + ret =vfio_group_add_container_user (group); +if (ret) + goto err_register_nb; + + container = group->container; +down_read (&container->group_lock); + + driver = container->iommu_driver; +if (likely (driver && driver->ops->register_notifier)) + ret = driver->ops->register_notifier (container->iommu_data, nb); +else + ret = -EINVAL ; + +up_read (&container->group_lock); +vfio_group_try_dissolve_container (group); + +err_register_nb: +vfio_group_put (group); +return ret; +} +EXPORT_SYMBOL (vfio_register_notifier); + +int vfio_unregister_notifier (struct device *dev,struct notifier_block *nb) +{ +struct vfio_container *container; +struct vfio_group *group; +struct vfio_iommu_driver *driver; +ssize_t ret; + +if (!dev || !nb) +return -EINVAL ; + + group =vfio_group_get_from_dev (dev); +if (IS_ERR (group)) +return PTR_ERR (group); + + ret =vfio_group_add_container_user (group); +if (ret) + goto err_unregister_nb; + + container = group->container; +down_read (&container->group_lock); + + driver = container->iommu_driver; +if (likely (driver && driver->ops->unregister_notifier)) + ret = driver->ops->unregister_notifier (container->iommu_data, + nb); +else + ret = -EINVAL ; + +up_read (&container->group_lock); +vfio_group_try_dissolve_container (group); + +err_unregister_nb: +vfio_group_put (group); +return ret; +} +EXPORT_SYMBOL (vfio_unregister_notifier); +/** * Module/class support */ drivers/vfio/vfio_iommu_type1.c +33
-1@@ -37,6 +37,7 @@#include <linux/vfio.h> #include <linux/workqueue.h> #include <linux/mdev.h> +#include <linux/notifier.h> #define DRIVER_VERSION "0.2"#define DRIVER_AUTHOR "Alex Williamson <[email protected]>"@@ -59,6 +60,7 @@ struct vfio_iommu {struct vfio_domain *external_domain;/* domain for external user */ struct mutex lock;struct rb_root dma_list; +struct blocking_notifier_head notifier;bool v2;bool nesting; };@@ -549,7 +551,8 @@ static long vfio_iommu_type1_pin_pages(void *iommu_data,mutex_lock (&iommu->lock); -if (!iommu->external_domain) { +/* Fail if notifier list is empty */ +if ((!iommu->external_domain) || (!iommu->notifier.head)) { ret = -EINVAL ; goto pin_done; }@@ -768,6 +771,50 @@ static unsigned long vfio_pgsize_bitmap(struct vfio_iommu *iommu)return bitmap; } +/* +* This function finds pfn in domain->external_addr_space->pfn_list for given +* iova range. If pfn exist, notify pfn to registered notifier list. On +* receiving notifier callback, vendor driver should invalidate the mapping and +* call vfio_unpin_pages() to unpin this pfn. With that vfio_pfn for this pfn +* gets removed from rb tree of pfn_list. That re-arranges rb tree, so while +* searching for next vfio_pfn in rb tree, start search from first node again. +* If any vendor driver doesn't unpin that pfn, vfio_pfn would not get removed +* from rb tree and so in next search vfio_pfn would be same as previous +* vfio_pfn. In that case, exit from loop. +*/ +static void vfio_notifier_call_chain (struct vfio_iommu *iommu, +struct vfio_iommu_type1_dma_unmap *unmap) +{ +struct vfio_domain *domain = iommu->external_domain; +struct rb_node *n; +struct vfio_pfn *vpfn =NULL , *prev_vpfn; + + do { + prev_vpfn = vpfn; + mutex_lock(&domain->external_addr_space->pfn_list_lock); + + n = rb_first(&domain->external_addr_space->pfn_list); + + for (; n; n = rb_next(n), vpfn = NULL) { + vpfn = rb_entry(n, struct vfio_pfn, node); + + if ((vpfn->iova >= unmap->iova) && + (vpfn->iova < unmap->iova + unmap->size))drivers/vfio/vfio_iommu_type1.c static unsigned long vfio_pgsize_bitmap(struct vfio_iommu *iommu) · line 812
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10
-0+break ; + } + +mutex_unlock (&domain->external_addr_space->pfn_list_lock); + +/* Notify any listeners about DMA_UNMAP */ +if (vpfn) +blocking_notifier_call_chain (&iommu->notifier, +VFIO_IOMMU_NOTIFY_DMA_UNMAP , + &vpfn->pfn);Hi Kirti,
The information carried by notifier is only a pfn.
Since your pin/unpin interfaces design, it's the vendor driver who should guarantee pin/unpin same times. To achieve that, the vendor driver must cache it's
iova->pfnmapping on its side, to avoid pinning a same page for multiple times.With the notifier carrying only a pfn, to find the iova by this pfn, the vendor driver must *also* keep a reverse-mapping. That's a bit too much.
Since the vendor could also suffer from IOMMU-compatible problem, which means a local cache is always helpful, so I'd like to have the iova carried to the notifier.
Quoted 5 lines
Quoted 1 line
What'd you say?
I agree, the pfn is not unique, multiple guest pfns (iovas) might be backed by the same host pfn.
DMA_UNMAPcalls are based on iova, the notifier through to the vendor driver must be based on the same.
Host pfn should be unique, right?
Kirti.
Re: [PATCH v10 10/19] vfio iommu: Add blocking notifier to notify DMA_UNMAP
Quoted 196 lines · On Sat, 29 Oct 2016 01:32:35 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 195 lines · Alex Williamson
Quoted 192 lines · On Fri, 28 Oct 2016 15:33:58 +0800 Jike Song <[email protected]> wrote:
Quoted 163 lines · Kirti Wankhede
Added blocking notifier to IOMMU TYPE1 driver to notify vendor drivers about
DMA_UNMAP.
Exported two APIsvfio_register_notifier()andvfio_unregister_notifier(). Vendor driver should register notifer using these APIs.
Vendor driver should useVFIO_IOMMU_NOTIFY_DMA_UNMAPaction to invalidate mappings.Signed-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id I5910d0024d6be87f3e8d3e0ca0eaeaaa0b17f271--- drivers/vfio/vfio.c | 73 +++++++++++++++++++++++++++++++++ drivers/vfio/vfio_iommu_type1.c | 89 ++++++++++++++++++++++++++++++++++++----- include/linux/vfio.h | 11 +++++ 3 files changed, 163 insertions(+), 10 deletions(-)drivers/vfio/vfio.c +73
-0@@ -1891,6 +1891,79 @@ err_unpin_pages: }EXPORT_SYMBOL (vfio_unpin_pages); +int vfio_register_notifier (struct device *dev,struct notifier_block *nb) +{ +struct vfio_container *container; +struct vfio_group *group; +struct vfio_iommu_driver *driver; +ssize_t ret; + +if (!dev || !nb) +return -EINVAL ; + + group =vfio_group_get_from_dev (dev); +if (IS_ERR (group)) +return PTR_ERR (group); + + ret =vfio_group_add_container_user (group); +if (ret) + goto err_register_nb; + + container = group->container; +down_read (&container->group_lock); + + driver = container->iommu_driver; +if (likely (driver && driver->ops->register_notifier)) + ret = driver->ops->register_notifier (container->iommu_data, nb); +else + ret = -EINVAL ; + +up_read (&container->group_lock); +vfio_group_try_dissolve_container (group); + +err_register_nb: +vfio_group_put (group); +return ret; +} +EXPORT_SYMBOL (vfio_register_notifier); + +int vfio_unregister_notifier (struct device *dev,struct notifier_block *nb) +{ +struct vfio_container *container; +struct vfio_group *group; +struct vfio_iommu_driver *driver; +ssize_t ret; + +if (!dev || !nb) +return -EINVAL ; + + group =vfio_group_get_from_dev (dev); +if (IS_ERR (group)) +return PTR_ERR (group); + + ret =vfio_group_add_container_user (group); +if (ret) + goto err_unregister_nb; + + container = group->container; +down_read (&container->group_lock); + + driver = container->iommu_driver; +if (likely (driver && driver->ops->unregister_notifier)) + ret = driver->ops->unregister_notifier (container->iommu_data, + nb); +else + ret = -EINVAL ; + +up_read (&container->group_lock); +vfio_group_try_dissolve_container (group); + +err_unregister_nb: +vfio_group_put (group); +return ret; +} +EXPORT_SYMBOL (vfio_unregister_notifier); +/** * Module/class support */ drivers/vfio/vfio_iommu_type1.c +33
-1@@ -37,6 +37,7 @@#include <linux/vfio.h> #include <linux/workqueue.h> #include <linux/mdev.h> +#include <linux/notifier.h> #define DRIVER_VERSION "0.2"#define DRIVER_AUTHOR "Alex Williamson <[email protected]>"@@ -59,6 +60,7 @@ struct vfio_iommu {struct vfio_domain *external_domain;/* domain for external user */ struct mutex lock;struct rb_root dma_list; +struct blocking_notifier_head notifier;bool v2;bool nesting; };@@ -549,7 +551,8 @@ static long vfio_iommu_type1_pin_pages(void *iommu_data,mutex_lock (&iommu->lock); -if (!iommu->external_domain) { +/* Fail if notifier list is empty */ +if ((!iommu->external_domain) || (!iommu->notifier.head)) { ret = -EINVAL ; goto pin_done; }@@ -768,6 +771,50 @@ static unsigned long vfio_pgsize_bitmap(struct vfio_iommu *iommu)return bitmap; } +/* +* This function finds pfn in domain->external_addr_space->pfn_list for given +* iova range. If pfn exist, notify pfn to registered notifier list. On +* receiving notifier callback, vendor driver should invalidate the mapping and +* call vfio_unpin_pages() to unpin this pfn. With that vfio_pfn for this pfn +* gets removed from rb tree of pfn_list. That re-arranges rb tree, so while +* searching for next vfio_pfn in rb tree, start search from first node again. +* If any vendor driver doesn't unpin that pfn, vfio_pfn would not get removed +* from rb tree and so in next search vfio_pfn would be same as previous +* vfio_pfn. In that case, exit from loop. +*/ +static void vfio_notifier_call_chain (struct vfio_iommu *iommu, +struct vfio_iommu_type1_dma_unmap *unmap) +{ +struct vfio_domain *domain = iommu->external_domain; +struct rb_node *n; +struct vfio_pfn *vpfn =NULL , *prev_vpfn; + + do { + prev_vpfn = vpfn; + mutex_lock(&domain->external_addr_space->pfn_list_lock); + + n = rb_first(&domain->external_addr_space->pfn_list); + + for (; n; n = rb_next(n), vpfn = NULL) { + vpfn = rb_entry(n, struct vfio_pfn, node); + + if ((vpfn->iova >= unmap->iova) && + (vpfn->iova < unmap->iova + unmap->size))drivers/vfio/vfio_iommu_type1.c static unsigned long vfio_pgsize_bitmap(struct vfio_iommu *iommu) · line 812
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10
-0+break ; + } + +mutex_unlock (&domain->external_addr_space->pfn_list_lock); + +/* Notify any listeners about DMA_UNMAP */ +if (vpfn) +blocking_notifier_call_chain (&iommu->notifier, +VFIO_IOMMU_NOTIFY_DMA_UNMAP , + &vpfn->pfn);Hi Kirti,
The information carried by notifier is only a pfn.
Since your pin/unpin interfaces design, it's the vendor driver who should guarantee pin/unpin same times. To achieve that, the vendor driver must cache it's
iova->pfnmapping on its side, to avoid pinning a same page for multiple times.With the notifier carrying only a pfn, to find the iova by this pfn, the vendor driver must *also* keep a reverse-mapping. That's a bit too much.
Since the vendor could also suffer from IOMMU-compatible problem, which means a local cache is always helpful, so I'd like to have the iova carried to the notifier.
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
What'd you say?
I agree, the pfn is not unique, multiple guest pfns (iovas) might be backed by the same host pfn.
DMA_UNMAPcalls are based on iova, the notifier through to the vendor driver must be based on the same.Host pfn should be unique, right?
Let's say a user does a malloc of a single page and does 100 calls to MAP_DMA populating 100 pages of IOVA space all backed by the same malloc'd page. This is valid, I have unit tests that do essentially this. Those will all have the same pfn. The user then does an UNMAP_DMA to a single one of those IOVA pages. Did the user unmap everything matching that pfn? Of course not, they only unmapped that one IOVA page. There is no guarantee of a 1:1 mapping of pfn to IOVA. UNMAP_DMA works based on IOVA. Invalidation broadcasts to the vendor driver MUST therefore also work based on IOVA. This is not an academic problem, address space aliases exist in real VMs, imagine a virtual IOMMU. Thanks,
Alex
Kirti Wankhede
maintainerRe: [PATCH v10 10/19] vfio iommu: Add blocking notifier to notify DMA_UNMAP
Quoted 7 lines · Alex Williamson
Quoted 4 lines · On Sat, 29 Oct 2016 01:32:35 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 3 lines · Alex Williamson
On Fri, 28 Oct 2016 15:33:58 +0800
Jike Song <[email protected]> wrote:
...
Quoted 66 lines
Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
Quoted 30 lines
drivers/vfio/vfio_iommu_type1.c +29
-0+/* +* This function finds pfn in domain->external_addr_space->pfn_list for given +* iova range. If pfn exist, notify pfn to registered notifier list. On +* receiving notifier callback, vendor driver should invalidate the mapping and +* call vfio_unpin_pages() to unpin this pfn. With that vfio_pfn for this pfn +* gets removed from rb tree of pfn_list. That re-arranges rb tree, so while +* searching for next vfio_pfn in rb tree, start search from first node again. +* If any vendor driver doesn't unpin that pfn, vfio_pfn would not get removed +* from rb tree and so in next search vfio_pfn would be same as previous +* vfio_pfn. In that case, exit from loop. +*/ +static void vfio_notifier_call_chain (struct vfio_iommu *iommu, +struct vfio_iommu_type1_dma_unmap *unmap) +{ +struct vfio_domain *domain = iommu->external_domain; +struct rb_node *n; +struct vfio_pfn *vpfn =NULL , *prev_vpfn; + + do { + prev_vpfn = vpfn; + mutex_lock(&domain->external_addr_space->pfn_list_lock); + + n = rb_first(&domain->external_addr_space->pfn_list); + + for (; n; n = rb_next(n), vpfn = NULL) { + vpfn = rb_entry(n, struct vfio_pfn, node); + + if ((vpfn->iova >= unmap->iova) && + (vpfn->iova < unmap->iova + unmap->size))drivers/vfio/vfio_iommu_type1.c static unsigned long vfio_pgsize_bitmap(struct vfio_iommu *iommu) · line 812
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10
-0+break ; + } + +mutex_unlock (&domain->external_addr_space->pfn_list_lock); + +/* Notify any listeners about DMA_UNMAP */ +if (vpfn) +blocking_notifier_call_chain (&iommu->notifier, +VFIO_IOMMU_NOTIFY_DMA_UNMAP , + &vpfn->pfn);Hi Kirti,
The information carried by notifier is only a pfn.
Since your pin/unpin interfaces design, it's the vendor driver who should guarantee pin/unpin same times. To achieve that, the vendor driver must cache it's
iova->pfnmapping on its side, to avoid pinning a same page for multiple times.With the notifier carrying only a pfn, to find the iova by this pfn, the vendor driver must *also* keep a reverse-mapping. That's a bit too much.
Since the vendor could also suffer from IOMMU-compatible problem, which means a local cache is always helpful, so I'd like to have the iova carried to the notifier.
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
What'd you say?
I agree, the pfn is not unique, multiple guest pfns (iovas) might be backed by the same host pfn.
DMA_UNMAPcalls are based on iova, the notifier through to the vendor driver must be based on the same.Host pfn should be unique, right?
Quoted 12 lines
Let's say a user does a malloc of a single page and does 100 calls to
MAP_DMApopulating 100 pages of IOVA space all backed by the same malloc'd page. This is valid, I have unit tests that do essentially this. Those will all have the same pfn. The user then does anUNMAP_DMAto a single one of those IOVA pages. Did the user unmap everything matching that pfn? Of course not, they only unmapped that one IOVA page. There is no guarantee of a 1:1 mapping of pfn to IOVA.UNMAP_DMAworks based on IOVA. Invalidation broadcasts to the vendor driver MUST therefore also work based on IOVA. This is not an academic problem, address space aliases exist in real VMs, imagine a virtual IOMMU. Thanks,
So struct vfio_iommu_type1_dma_unmap should be passed as argument to notifier callback:
if (unmapped && iommu->external_domain)
- vfio_notifier_call_chain(iommu, unmap);
+ blocking_notifier_call_chain(&iommu->notifier,
+ VFIO_IOMMU_NOTIFY_DMA_UNMAP,
+ unmap);
Then vendor driver should find pfns he has pinned from this range of iovas, then invalidate and unpin pfns. Right?
Thanks,
Kirti
Re: [PATCH v10 10/19] vfio iommu: Add blocking notifier to notify DMA_UNMAP
Quoted 98 lines · On Sat, 29 Oct 2016 16:07:05 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 7 lines · Alex Williamson
Quoted 4 lines · On Sat, 29 Oct 2016 01:32:35 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 3 lines · Alex Williamson
On Fri, 28 Oct 2016 15:33:58 +0800
Jike Song <[email protected]> wrote:...
Quoted 66 lines
Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
Quoted 30 lines
drivers/vfio/vfio_iommu_type1.c +29
-0+/* +* This function finds pfn in domain->external_addr_space->pfn_list for given +* iova range. If pfn exist, notify pfn to registered notifier list. On +* receiving notifier callback, vendor driver should invalidate the mapping and +* call vfio_unpin_pages() to unpin this pfn. With that vfio_pfn for this pfn +* gets removed from rb tree of pfn_list. That re-arranges rb tree, so while +* searching for next vfio_pfn in rb tree, start search from first node again. +* If any vendor driver doesn't unpin that pfn, vfio_pfn would not get removed +* from rb tree and so in next search vfio_pfn would be same as previous +* vfio_pfn. In that case, exit from loop. +*/ +static void vfio_notifier_call_chain (struct vfio_iommu *iommu, +struct vfio_iommu_type1_dma_unmap *unmap) +{ +struct vfio_domain *domain = iommu->external_domain; +struct rb_node *n; +struct vfio_pfn *vpfn =NULL , *prev_vpfn; + + do { + prev_vpfn = vpfn; + mutex_lock(&domain->external_addr_space->pfn_list_lock); + + n = rb_first(&domain->external_addr_space->pfn_list); + + for (; n; n = rb_next(n), vpfn = NULL) { + vpfn = rb_entry(n, struct vfio_pfn, node); + + if ((vpfn->iova >= unmap->iova) && + (vpfn->iova < unmap->iova + unmap->size))drivers/vfio/vfio_iommu_type1.c static unsigned long vfio_pgsize_bitmap(struct vfio_iommu *iommu) · line 812
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10
-0+break ; + } + +mutex_unlock (&domain->external_addr_space->pfn_list_lock); + +/* Notify any listeners about DMA_UNMAP */ +if (vpfn) +blocking_notifier_call_chain (&iommu->notifier, +VFIO_IOMMU_NOTIFY_DMA_UNMAP , + &vpfn->pfn);Hi Kirti,
The information carried by notifier is only a pfn.
Since your pin/unpin interfaces design, it's the vendor driver who should guarantee pin/unpin same times. To achieve that, the vendor driver must cache it's
iova->pfnmapping on its side, to avoid pinning a same page for multiple times.With the notifier carrying only a pfn, to find the iova by this pfn, the vendor driver must *also* keep a reverse-mapping. That's a bit too much.
Since the vendor could also suffer from IOMMU-compatible problem, which means a local cache is always helpful, so I'd like to have the iova carried to the notifier.
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
What'd you say?
I agree, the pfn is not unique, multiple guest pfns (iovas) might be backed by the same host pfn.
DMA_UNMAPcalls are based on iova, the notifier through to the vendor driver must be based on the same.Host pfn should be unique, right?
Quoted 12 lines
Let's say a user does a malloc of a single page and does 100 calls to
MAP_DMApopulating 100 pages of IOVA space all backed by the same malloc'd page. This is valid, I have unit tests that do essentially this. Those will all have the same pfn. The user then does anUNMAP_DMAto a single one of those IOVA pages. Did the user unmap everything matching that pfn? Of course not, they only unmapped that one IOVA page. There is no guarantee of a 1:1 mapping of pfn to IOVA.UNMAP_DMAworks based on IOVA. Invalidation broadcasts to the vendor driver MUST therefore also work based on IOVA. This is not an academic problem, address space aliases exist in real VMs, imagine a virtual IOMMU. Thanks,So struct
vfio_iommu_type1_dma_unmapshould be passed as argument to notifier callback:if (unmapped && iommu->external_domain)drivers/vfio/vfio_iommu_type1.c +3
-1-vfio_notifier_call_chain (iommu, unmap); +blocking_notifier_call_chain (&iommu->notifier, +VFIO_IOMMU_NOTIFY_DMA_UNMAP , + unmap);
Quoted 2 lines
Then vendor driver should find pfns he has pinned from this range of iovas, then invalidate and unpin pfns. Right?
That seems like a valid choice. It's probably better than calling the notifier for each page of iova. Thanks,
Alex
Re: [PATCH v10 10/19] vfio iommu: Add blocking notifier to notify DMA_UNMAP
Quoted 101 lines · Alex Williamson
Quoted 98 lines · On Sat, 29 Oct 2016 16:07:05 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 7 lines · Alex Williamson
Quoted 4 lines · On Sat, 29 Oct 2016 01:32:35 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 3 lines · Alex Williamson
On Fri, 28 Oct 2016 15:33:58 +0800
Jike Song <[email protected]> wrote:...
Quoted 66 lines
Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
Quoted 30 lines
(quoted diff) +29
-0+/* + * This function finds pfn in domain->external_addr_space->pfn_list for given + * iova range. If pfn exist, notify pfn to registered notifier list. On + * receiving notifier callback, vendor driver should invalidate the mapping and + * call vfio_unpin_pages() to unpin this pfn. With that vfio_pfn for this pfn + * gets removed from rb tree of pfn_list. That re-arranges rb tree, so while + * searching for next vfio_pfn in rb tree, start search from first node again. + * If any vendor driver doesn't unpin that pfn, vfio_pfn would not get removed + * from rb tree and so in next search vfio_pfn would be same as previous + * vfio_pfn. In that case, exit from loop. + */ +static void vfio_notifier_call_chain(struct vfio_iommu *iommu, + struct vfio_iommu_type1_dma_unmap *unmap) +{ + struct vfio_domain *domain = iommu->external_domain; + struct rb_node *n; + struct vfio_pfn *vpfn = NULL, *prev_vpfn; + + do { + prev_vpfn = vpfn; + mutex_lock(&domain->external_addr_space->pfn_list_lock); + + n = rb_first(&domain->external_addr_space->pfn_list); + + for (; n; n = rb_next(n), vpfn = NULL) { + vpfn = rb_entry(n, struct vfio_pfn, node); + + if ((vpfn->iova >= unmap->iova) && + (vpfn->iova < unmap->iova + unmap->size))Review
Quoted 10 lines
(quoted diff) +10
-0+ break; + } + + mutex_unlock(&domain->external_addr_space->pfn_list_lock); + + /* Notify any listeners about DMA_UNMAP */ + if (vpfn) + blocking_notifier_call_chain(&iommu->notifier, + VFIO_IOMMU_NOTIFY_DMA_UNMAP, + &vpfn->pfn);Hi Kirti,
The information carried by notifier is only a pfn.
Since your pin/unpin interfaces design, it's the vendor driver who should guarantee pin/unpin same times. To achieve that, the vendor driver must cache it's
iova->pfnmapping on its side, to avoid pinning a same page for multiple times.With the notifier carrying only a pfn, to find the iova by this pfn, the vendor driver must *also* keep a reverse-mapping. That's a bit too much.
Since the vendor could also suffer from IOMMU-compatible problem, which means a local cache is always helpful, so I'd like to have the iova carried to the notifier.
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
What'd you say?
I agree, the pfn is not unique, multiple guest pfns (iovas) might be backed by the same host pfn.
DMA_UNMAPcalls are based on iova, the notifier through to the vendor driver must be based on the same.Host pfn should be unique, right?
Quoted 12 lines
Let's say a user does a malloc of a single page and does 100 calls to
MAP_DMApopulating 100 pages of IOVA space all backed by the same malloc'd page. This is valid, I have unit tests that do essentially this. Those will all have the same pfn. The user then does anUNMAP_DMAto a single one of those IOVA pages. Did the user unmap everything matching that pfn? Of course not, they only unmapped that one IOVA page. There is no guarantee of a 1:1 mapping of pfn to IOVA.UNMAP_DMAworks based on IOVA. Invalidation broadcasts to the vendor driver MUST therefore also work based on IOVA. This is not an academic problem, address space aliases exist in real VMs, imagine a virtual IOMMU. Thanks,So struct
vfio_iommu_type1_dma_unmapshould be passed as argument to notifier callback:if (unmapped && iommu->external_domain)(quoted diff) +3
-1- vfio_notifier_call_chain(iommu, unmap); + blocking_notifier_call_chain(&iommu->notifier, + VFIO_IOMMU_NOTIFY_DMA_UNMAP, + unmap);
Quoted 8 lines
Quoted 2 lines
Then vendor driver should find pfns he has pinned from this range of iovas, then invalidate and unpin pfns. Right?
That seems like a valid choice. It's probably better than calling the notifier for each page of iova. Thanks,
Alex
Hi Kirti,
This version requires the *vendor driver* call vfio_register_notifier for an mdev device before any pinning operations. I guess all of the vendor drivers may have some alike code for notifier registration/unregistration.
My question is, how about letting the mdev framework managing the notifier registration/unregistration process?
We could add a notifier_fn_t callback to "struct parent_ops", then the mdev framework should make sure that the vendor driver assigned a value to this callback. The mdev core could initiate a notifier_block for each parent driver with its callback, and register/unregister it to vfio in the right time.
Signature · 1 line
Dong Jia
Re: [Qemu-devel] [PATCH v10 10/19] vfio iommu: Add blocking notifier to notify DMA_UNMAP
In reply to Alex WilliamsonQuoted 101 lines · Alex Williamson
Quoted 98 lines · On Sat, 29 Oct 2016 16:07:05 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 7 lines · Alex Williamson
Quoted 4 lines · On Sat, 29 Oct 2016 01:32:35 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 3 lines · Alex Williamson
On Fri, 28 Oct 2016 15:33:58 +0800
Jike Song <[email protected]> wrote:...
Quoted 66 lines
Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
Quoted 30 lines
(quoted diff) +29
-0+/* + * This function finds pfn in domain->external_addr_space->pfn_list for given + * iova range. If pfn exist, notify pfn to registered notifier list. On + * receiving notifier callback, vendor driver should invalidate the mapping and + * call vfio_unpin_pages() to unpin this pfn. With that vfio_pfn for this pfn + * gets removed from rb tree of pfn_list. That re-arranges rb tree, so while + * searching for next vfio_pfn in rb tree, start search from first node again. + * If any vendor driver doesn't unpin that pfn, vfio_pfn would not get removed + * from rb tree and so in next search vfio_pfn would be same as previous + * vfio_pfn. In that case, exit from loop. + */ +static void vfio_notifier_call_chain(struct vfio_iommu *iommu, + struct vfio_iommu_type1_dma_unmap *unmap) +{ + struct vfio_domain *domain = iommu->external_domain; + struct rb_node *n; + struct vfio_pfn *vpfn = NULL, *prev_vpfn; + + do { + prev_vpfn = vpfn; + mutex_lock(&domain->external_addr_space->pfn_list_lock); + + n = rb_first(&domain->external_addr_space->pfn_list); + + for (; n; n = rb_next(n), vpfn = NULL) { + vpfn = rb_entry(n, struct vfio_pfn, node); + + if ((vpfn->iova >= unmap->iova) && + (vpfn->iova < unmap->iova + unmap->size))Review
Quoted 10 lines
(quoted diff) +10
-0+ break; + } + + mutex_unlock(&domain->external_addr_space->pfn_list_lock); + + /* Notify any listeners about DMA_UNMAP */ + if (vpfn) + blocking_notifier_call_chain(&iommu->notifier, + VFIO_IOMMU_NOTIFY_DMA_UNMAP, + &vpfn->pfn);Hi Kirti,
The information carried by notifier is only a pfn.
Since your pin/unpin interfaces design, it's the vendor driver who should guarantee pin/unpin same times. To achieve that, the vendor driver must cache it's
iova->pfnmapping on its side, to avoid pinning a same page for multiple times.With the notifier carrying only a pfn, to find the iova by this pfn, the vendor driver must *also* keep a reverse-mapping. That's a bit too much.
Since the vendor could also suffer from IOMMU-compatible problem, which means a local cache is always helpful, so I'd like to have the iova carried to the notifier.
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
What'd you say?
I agree, the pfn is not unique, multiple guest pfns (iovas) might be backed by the same host pfn.
DMA_UNMAPcalls are based on iova, the notifier through to the vendor driver must be based on the same.Host pfn should be unique, right?
Quoted 12 lines
Let's say a user does a malloc of a single page and does 100 calls to
MAP_DMApopulating 100 pages of IOVA space all backed by the same malloc'd page. This is valid, I have unit tests that do essentially this. Those will all have the same pfn. The user then does anUNMAP_DMAto a single one of those IOVA pages. Did the user unmap everything matching that pfn? Of course not, they only unmapped that one IOVA page. There is no guarantee of a 1:1 mapping of pfn to IOVA.UNMAP_DMAworks based on IOVA. Invalidation broadcasts to the vendor driver MUST therefore also work based on IOVA. This is not an academic problem, address space aliases exist in real VMs, imagine a virtual IOMMU. Thanks,So struct
vfio_iommu_type1_dma_unmapshould be passed as argument to notifier callback:if (unmapped && iommu->external_domain)(quoted diff) +3
-1- vfio_notifier_call_chain(iommu, unmap); + blocking_notifier_call_chain(&iommu->notifier, + VFIO_IOMMU_NOTIFY_DMA_UNMAP, + unmap);
Quoted 8 lines
Quoted 2 lines
Then vendor driver should find pfns he has pinned from this range of iovas, then invalidate and unpin pfns. Right?
That seems like a valid choice. It's probably better than calling the notifier for each page of iova. Thanks,
Alex
Hi Kirti,
This version requires the *vendor driver* call vfio_register_notifier for an mdev device before any pinning operations. I guess all of the vendor drivers may have some alike code for notifier registration/unregistration.
My question is, how about letting the mdev framework managing the notifier registration/unregistration process?
We could add a notifier_fn_t callback to "struct parent_ops", then the mdev framework should make sure that the vendor driver assigned a value to this callback. The mdev core could initiate a notifier_block for each parent driver with its callback, and register/unregister it to vfio in the right time.
Signature · 1 line
Dong Jia
Kirti Wankhede
maintainerRe: [PATCH v10 10/19] vfio iommu: Add blocking notifier to notify DMA_UNMAP
Quoted 118 lines · Dong Jia Shi
Quoted 101 lines · Alex Williamson
Quoted 98 lines · On Sat, 29 Oct 2016 16:07:05 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 7 lines · Alex Williamson
Quoted 4 lines · On Sat, 29 Oct 2016 01:32:35 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 3 lines · Alex Williamson
On Fri, 28 Oct 2016 15:33:58 +0800
Jike Song <[email protected]> wrote:...
Quoted 66 lines
Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
Quoted 30 lines
(quoted diff) +29
-0+/* + * This function finds pfn in domain->external_addr_space->pfn_list for given + * iova range. If pfn exist, notify pfn to registered notifier list. On + * receiving notifier callback, vendor driver should invalidate the mapping and + * call vfio_unpin_pages() to unpin this pfn. With that vfio_pfn for this pfn + * gets removed from rb tree of pfn_list. That re-arranges rb tree, so while + * searching for next vfio_pfn in rb tree, start search from first node again. + * If any vendor driver doesn't unpin that pfn, vfio_pfn would not get removed + * from rb tree and so in next search vfio_pfn would be same as previous + * vfio_pfn. In that case, exit from loop. + */ +static void vfio_notifier_call_chain(struct vfio_iommu *iommu, + struct vfio_iommu_type1_dma_unmap *unmap) +{ + struct vfio_domain *domain = iommu->external_domain; + struct rb_node *n; + struct vfio_pfn *vpfn = NULL, *prev_vpfn; + + do { + prev_vpfn = vpfn; + mutex_lock(&domain->external_addr_space->pfn_list_lock); + + n = rb_first(&domain->external_addr_space->pfn_list); + + for (; n; n = rb_next(n), vpfn = NULL) { + vpfn = rb_entry(n, struct vfio_pfn, node); + + if ((vpfn->iova >= unmap->iova) && + (vpfn->iova < unmap->iova + unmap->size))Review
Quoted 10 lines
(quoted diff) +10
-0+ break; + } + + mutex_unlock(&domain->external_addr_space->pfn_list_lock); + + /* Notify any listeners about DMA_UNMAP */ + if (vpfn) + blocking_notifier_call_chain(&iommu->notifier, + VFIO_IOMMU_NOTIFY_DMA_UNMAP, + &vpfn->pfn);Hi Kirti,
The information carried by notifier is only a pfn.
Since your pin/unpin interfaces design, it's the vendor driver who should guarantee pin/unpin same times. To achieve that, the vendor driver must cache it's
iova->pfnmapping on its side, to avoid pinning a same page for multiple times.With the notifier carrying only a pfn, to find the iova by this pfn, the vendor driver must *also* keep a reverse-mapping. That's a bit too much.
Since the vendor could also suffer from IOMMU-compatible problem, which means a local cache is always helpful, so I'd like to have the iova carried to the notifier.
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
What'd you say?
I agree, the pfn is not unique, multiple guest pfns (iovas) might be backed by the same host pfn.
DMA_UNMAPcalls are based on iova, the notifier through to the vendor driver must be based on the same.Host pfn should be unique, right?
Quoted 12 lines
Let's say a user does a malloc of a single page and does 100 calls to
MAP_DMApopulating 100 pages of IOVA space all backed by the same malloc'd page. This is valid, I have unit tests that do essentially this. Those will all have the same pfn. The user then does anUNMAP_DMAto a single one of those IOVA pages. Did the user unmap everything matching that pfn? Of course not, they only unmapped that one IOVA page. There is no guarantee of a 1:1 mapping of pfn to IOVA.UNMAP_DMAworks based on IOVA. Invalidation broadcasts to the vendor driver MUST therefore also work based on IOVA. This is not an academic problem, address space aliases exist in real VMs, imagine a virtual IOMMU. Thanks,So struct
vfio_iommu_type1_dma_unmapshould be passed as argument to notifier callback:if (unmapped && iommu->external_domain)(quoted diff) +3
-1- vfio_notifier_call_chain(iommu, unmap); + blocking_notifier_call_chain(&iommu->notifier, + VFIO_IOMMU_NOTIFY_DMA_UNMAP, + unmap);Quoted 8 lines
Quoted 2 lines
Then vendor driver should find pfns he has pinned from this range of iovas, then invalidate and unpin pfns. Right?
That seems like a valid choice. It's probably better than calling the notifier for each page of iova. Thanks,
Alex
Hi Kirti,
This version requires the *vendor driver* call
vfio_register_notifierfor an mdev device before any pinning operations. I guess all of the vendor drivers may have some alike code for notifier registration/unregistration.
Quoted 9 lines
My question is, how about letting the mdev framework managing the notifier registration/unregistration process?
We could add a
notifier_fn_tcallback to "structparent_ops", then the mdev framework should make sure that the vendor driver assigned a value to this callback. The mdev core could initiate anotifier_blockfor each parent driver with its callback, and register/unregister it to vfio in the right time.
Module mdev_core is independent of VFIO so far and it should be independent of VFIO module.
Its a good suggestion to have a notifier callback in parent_ops, but we shouldn't call vfio_register_notifier()/ vfio_unregister_notifier() from mdev core module. vfio_mdev module would take care to register/unregister notifier to vfio from its vfio_mdev_open()/ vfio_mdev_release() call. That looks cleaner to me.
Notifier callback in parent_ops should be optional since all vendor drivers might not pin/unpin pages, for example sample mtty driver. Notifier would be registered to vfio module only if the notifier callback is provided by vendor driver.
If this looks reasonable, I'll have this patch in my next version of patch series.
Thanks,
Kirti
Re: [PATCH v10 10/19] vfio iommu: Add blocking notifier to notify DMA_UNMAP
Quoted 131 lines · Kirti Wankhede
Quoted 118 lines · Dong Jia Shi
Quoted 101 lines · Alex Williamson
Quoted 98 lines · On Sat, 29 Oct 2016 16:07:05 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 7 lines · Alex Williamson
Quoted 4 lines · On Sat, 29 Oct 2016 01:32:35 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 3 lines · Alex Williamson
On Fri, 28 Oct 2016 15:33:58 +0800
Jike Song <[email protected]> wrote:...
Quoted 66 lines
Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
Quoted 30 lines
(quoted diff) +29
-0+/* + * This function finds pfn in domain->external_addr_space->pfn_list for given + * iova range. If pfn exist, notify pfn to registered notifier list. On + * receiving notifier callback, vendor driver should invalidate the mapping and + * call vfio_unpin_pages() to unpin this pfn. With that vfio_pfn for this pfn + * gets removed from rb tree of pfn_list. That re-arranges rb tree, so while + * searching for next vfio_pfn in rb tree, start search from first node again. + * If any vendor driver doesn't unpin that pfn, vfio_pfn would not get removed + * from rb tree and so in next search vfio_pfn would be same as previous + * vfio_pfn. In that case, exit from loop. + */ +static void vfio_notifier_call_chain(struct vfio_iommu *iommu, + struct vfio_iommu_type1_dma_unmap *unmap) +{ + struct vfio_domain *domain = iommu->external_domain; + struct rb_node *n; + struct vfio_pfn *vpfn = NULL, *prev_vpfn; + + do { + prev_vpfn = vpfn; + mutex_lock(&domain->external_addr_space->pfn_list_lock); + + n = rb_first(&domain->external_addr_space->pfn_list); + + for (; n; n = rb_next(n), vpfn = NULL) { + vpfn = rb_entry(n, struct vfio_pfn, node); + + if ((vpfn->iova >= unmap->iova) && + (vpfn->iova < unmap->iova + unmap->size))Review
Quoted 10 lines
(quoted diff) +10
-0+ break; + } + + mutex_unlock(&domain->external_addr_space->pfn_list_lock); + + /* Notify any listeners about DMA_UNMAP */ + if (vpfn) + blocking_notifier_call_chain(&iommu->notifier, + VFIO_IOMMU_NOTIFY_DMA_UNMAP, + &vpfn->pfn);Hi Kirti,
The information carried by notifier is only a pfn.
Since your pin/unpin interfaces design, it's the vendor driver who should guarantee pin/unpin same times. To achieve that, the vendor driver must cache it's
iova->pfnmapping on its side, to avoid pinning a same page for multiple times.With the notifier carrying only a pfn, to find the iova by this pfn, the vendor driver must *also* keep a reverse-mapping. That's a bit too much.
Since the vendor could also suffer from IOMMU-compatible problem, which means a local cache is always helpful, so I'd like to have the iova carried to the notifier.
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
What'd you say?
I agree, the pfn is not unique, multiple guest pfns (iovas) might be backed by the same host pfn.
DMA_UNMAPcalls are based on iova, the notifier through to the vendor driver must be based on the same.Host pfn should be unique, right?
Quoted 12 lines
Let's say a user does a malloc of a single page and does 100 calls to
MAP_DMApopulating 100 pages of IOVA space all backed by the same malloc'd page. This is valid, I have unit tests that do essentially this. Those will all have the same pfn. The user then does anUNMAP_DMAto a single one of those IOVA pages. Did the user unmap everything matching that pfn? Of course not, they only unmapped that one IOVA page. There is no guarantee of a 1:1 mapping of pfn to IOVA.UNMAP_DMAworks based on IOVA. Invalidation broadcasts to the vendor driver MUST therefore also work based on IOVA. This is not an academic problem, address space aliases exist in real VMs, imagine a virtual IOMMU. Thanks,So struct
vfio_iommu_type1_dma_unmapshould be passed as argument to notifier callback:if (unmapped && iommu->external_domain)(quoted diff) +3
-1- vfio_notifier_call_chain(iommu, unmap); + blocking_notifier_call_chain(&iommu->notifier, + VFIO_IOMMU_NOTIFY_DMA_UNMAP, + unmap);Quoted 8 lines
Quoted 2 lines
Then vendor driver should find pfns he has pinned from this range of iovas, then invalidate and unpin pfns. Right?
That seems like a valid choice. It's probably better than calling the notifier for each page of iova. Thanks,
Alex
Hi Kirti,
This version requires the *vendor driver* call
vfio_register_notifierfor an mdev device before any pinning operations. I guess all of the vendor drivers may have some alike code for notifier registration/unregistration.Quoted 9 lines
My question is, how about letting the mdev framework managing the notifier registration/unregistration process?
We could add a
notifier_fn_tcallback to "structparent_ops", then the mdev framework should make sure that the vendor driver assigned a value to this callback. The mdev core could initiate anotifier_blockfor each parent driver with its callback, and register/unregister it to vfio in the right time.
Quoted 8 lines
Module
mdev_coreis independent of VFIO so far and it should be independent of VFIO module.Its a good suggestion to have a notifier callback in
parent_ops, but we shouldn't call vfio_register_notifier()/vfio_unregister_notifier()from mdev core module.vfio_mdevmodule would take care to register/unregister notifier to vfio from its vfio_mdev_open()/vfio_mdev_release()call. That looks cleaner to me.
Nod.
Quoted 8 lines
Notifier callback in
parent_opsshould be optional since all vendor drivers might not pin/unpin pages, for example sample mtty driver. Notifier would be registered to vfio module only if the notifier callback is provided by vendor driver.If this looks reasonable, I'll have this patch in my next version of patch series.
Sounds reasonable for me. Thanks,
Quoted 4 lines
Thanks,
Kirti
Signature · 1 line
Dong Jia
Re: [Qemu-devel] [PATCH v10 10/19] vfio iommu: Add blocking notifier to notify DMA_UNMAP
In reply to Kirti WankhedeQuoted 131 lines · Kirti Wankhede
Quoted 118 lines · Dong Jia Shi
Quoted 101 lines · Alex Williamson
Quoted 98 lines · On Sat, 29 Oct 2016 16:07:05 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 7 lines · Alex Williamson
Quoted 4 lines · On Sat, 29 Oct 2016 01:32:35 +0530 Kirti Wankhede <[email protected]> wrote:
Quoted 3 lines · Alex Williamson
On Fri, 28 Oct 2016 15:33:58 +0800
Jike Song <[email protected]> wrote:...
Quoted 66 lines
Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
Quoted 30 lines
(quoted diff) +29
-0+/* + * This function finds pfn in domain->external_addr_space->pfn_list for given + * iova range. If pfn exist, notify pfn to registered notifier list. On + * receiving notifier callback, vendor driver should invalidate the mapping and + * call vfio_unpin_pages() to unpin this pfn. With that vfio_pfn for this pfn + * gets removed from rb tree of pfn_list. That re-arranges rb tree, so while + * searching for next vfio_pfn in rb tree, start search from first node again. + * If any vendor driver doesn't unpin that pfn, vfio_pfn would not get removed + * from rb tree and so in next search vfio_pfn would be same as previous + * vfio_pfn. In that case, exit from loop. + */ +static void vfio_notifier_call_chain(struct vfio_iommu *iommu, + struct vfio_iommu_type1_dma_unmap *unmap) +{ + struct vfio_domain *domain = iommu->external_domain; + struct rb_node *n; + struct vfio_pfn *vpfn = NULL, *prev_vpfn; + + do { + prev_vpfn = vpfn; + mutex_lock(&domain->external_addr_space->pfn_list_lock); + + n = rb_first(&domain->external_addr_space->pfn_list); + + for (; n; n = rb_next(n), vpfn = NULL) { + vpfn = rb_entry(n, struct vfio_pfn, node); + + if ((vpfn->iova >= unmap->iova) && + (vpfn->iova < unmap->iova + unmap->size))Review
Quoted 10 lines
(quoted diff) +10
-0+ break; + } + + mutex_unlock(&domain->external_addr_space->pfn_list_lock); + + /* Notify any listeners about DMA_UNMAP */ + if (vpfn) + blocking_notifier_call_chain(&iommu->notifier, + VFIO_IOMMU_NOTIFY_DMA_UNMAP, + &vpfn->pfn);Hi Kirti,
The information carried by notifier is only a pfn.
Since your pin/unpin interfaces design, it's the vendor driver who should guarantee pin/unpin same times. To achieve that, the vendor driver must cache it's
iova->pfnmapping on its side, to avoid pinning a same page for multiple times.With the notifier carrying only a pfn, to find the iova by this pfn, the vendor driver must *also* keep a reverse-mapping. That's a bit too much.
Since the vendor could also suffer from IOMMU-compatible problem, which means a local cache is always helpful, so I'd like to have the iova carried to the notifier.
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
What'd you say?
I agree, the pfn is not unique, multiple guest pfns (iovas) might be backed by the same host pfn.
DMA_UNMAPcalls are based on iova, the notifier through to the vendor driver must be based on the same.Host pfn should be unique, right?
Quoted 12 lines
Let's say a user does a malloc of a single page and does 100 calls to
MAP_DMApopulating 100 pages of IOVA space all backed by the same malloc'd page. This is valid, I have unit tests that do essentially this. Those will all have the same pfn. The user then does anUNMAP_DMAto a single one of those IOVA pages. Did the user unmap everything matching that pfn? Of course not, they only unmapped that one IOVA page. There is no guarantee of a 1:1 mapping of pfn to IOVA.UNMAP_DMAworks based on IOVA. Invalidation broadcasts to the vendor driver MUST therefore also work based on IOVA. This is not an academic problem, address space aliases exist in real VMs, imagine a virtual IOMMU. Thanks,So struct
vfio_iommu_type1_dma_unmapshould be passed as argument to notifier callback:if (unmapped && iommu->external_domain)(quoted diff) +3
-1- vfio_notifier_call_chain(iommu, unmap); + blocking_notifier_call_chain(&iommu->notifier, + VFIO_IOMMU_NOTIFY_DMA_UNMAP, + unmap);Quoted 8 lines
Quoted 2 lines
Then vendor driver should find pfns he has pinned from this range of iovas, then invalidate and unpin pfns. Right?
That seems like a valid choice. It's probably better than calling the notifier for each page of iova. Thanks,
Alex
Hi Kirti,
This version requires the *vendor driver* call
vfio_register_notifierfor an mdev device before any pinning operations. I guess all of the vendor drivers may have some alike code for notifier registration/unregistration.Quoted 9 lines
My question is, how about letting the mdev framework managing the notifier registration/unregistration process?
We could add a
notifier_fn_tcallback to "structparent_ops", then the mdev framework should make sure that the vendor driver assigned a value to this callback. The mdev core could initiate anotifier_blockfor each parent driver with its callback, and register/unregister it to vfio in the right time.
Quoted 8 lines
Module
mdev_coreis independent of VFIO so far and it should be independent of VFIO module.Its a good suggestion to have a notifier callback in
parent_ops, but we shouldn't call vfio_register_notifier()/vfio_unregister_notifier()from mdev core module.vfio_mdevmodule would take care to register/unregister notifier to vfio from its vfio_mdev_open()/vfio_mdev_release()call. That looks cleaner to me.
Nod.
Quoted 8 lines
Notifier callback in
parent_opsshould be optional since all vendor drivers might not pin/unpin pages, for example sample mtty driver. Notifier would be registered to vfio module only if the notifier callback is provided by vendor driver.If this looks reasonable, I'll have this patch in my next version of patch series.
Sounds reasonable for me. Thanks,
Quoted 4 lines
Thanks,
Kirti
Signature · 1 line
Dong Jia
Re: [PATCH v10 10/19] vfio iommu: Add blocking notifier to notify DMA_UNMAP
In reply to Kirti WankhedeQuoted 21 lines · Kirti Wankhede
Added blocking notifier to IOMMU TYPE1 driver to notify vendor drivers about
DMA_UNMAP.
Exported two APIsvfio_register_notifier()andvfio_unregister_notifier(). Vendor driver should register notifer using these APIs.
Vendor driver should useVFIO_IOMMU_NOTIFY_DMA_UNMAPaction to invalidate mappings.Signed-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id I5910d0024d6be87f3e8d3e0ca0eaeaaa0b17f271--- drivers/vfio/vfio.c | 73 +++++++++++++++++++++++++++++++++ drivers/vfio/vfio_iommu_type1.c | 89 ++++++++++++++++++++++++++++++++++++----- include/linux/vfio.h | 11 +++++ 3 files changed, 163 insertions(+), 10 deletions(-)drivers/vfio/vfio.c +0
-0
drivers/vfio/vfio.c err_unpin_pages: · line 1895
Quoted 5 lines
drivers/vfio/vfio.c +2
-0@@ -1891,6 +1891,79 @@ err_unpin_pages: }EXPORT_SYMBOL (vfio_unpin_pages); +int vfio_register_notifier (struct device *dev,struct notifier_block *nb) +{
Hi Kirti,
Given that below 4 methods are members of vfio_iommu_driver_ops:
pin_pages
unpin_pages
register_notifier
unregister_notifier
the names of exposed VFIO APIs could possibly be clearer:
vfio_iommu_pin_pages
vfio_iommu_unpin_pages
vfio_iommu_register_notifier
vfio_iommu_unreigster_nodier
--
Thanks,
Jike
Quoted 247 lines
drivers/vfio/vfio.c +71
-0+struct vfio_container *container; +struct vfio_group *group; +struct vfio_iommu_driver *driver; +ssize_t ret; + +if (!dev || !nb) +return -EINVAL ; + + group =vfio_group_get_from_dev (dev); +if (IS_ERR (group)) +return PTR_ERR (group); + + ret =vfio_group_add_container_user (group); +if (ret) + goto err_register_nb; + + container = group->container; +down_read (&container->group_lock); + + driver = container->iommu_driver; +if (likely (driver && driver->ops->register_notifier)) + ret = driver->ops->register_notifier (container->iommu_data, nb); +else + ret = -EINVAL ; + +up_read (&container->group_lock); +vfio_group_try_dissolve_container (group); + +err_register_nb: +vfio_group_put (group); +return ret; +} +EXPORT_SYMBOL (vfio_register_notifier); + +int vfio_unregister_notifier (struct device *dev,struct notifier_block *nb) +{ +struct vfio_container *container; +struct vfio_group *group; +struct vfio_iommu_driver *driver; +ssize_t ret; + +if (!dev || !nb) +return -EINVAL ; + + group =vfio_group_get_from_dev (dev); +if (IS_ERR (group)) +return PTR_ERR (group); + + ret =vfio_group_add_container_user (group); +if (ret) + goto err_unregister_nb; + + container = group->container; +down_read (&container->group_lock); + + driver = container->iommu_driver; +if (likely (driver && driver->ops->unregister_notifier)) + ret = driver->ops->unregister_notifier (container->iommu_data, + nb); +else + ret = -EINVAL ; + +up_read (&container->group_lock); +vfio_group_try_dissolve_container (group); + +err_unregister_nb: +vfio_group_put (group); +return ret; +} +EXPORT_SYMBOL (vfio_unregister_notifier); +/** * Module/class support */ drivers/vfio/vfio_iommu_type1.c +79
-10@@ -37,6 +37,7 @@#include <linux/vfio.h> #include <linux/workqueue.h> #include <linux/mdev.h> +#include <linux/notifier.h> #define DRIVER_VERSION "0.2"#define DRIVER_AUTHOR "Alex Williamson <[email protected]>"@@ -59,6 +60,7 @@ struct vfio_iommu {struct vfio_domain *external_domain;/* domain for external user */ struct mutex lock;struct rb_root dma_list; +struct blocking_notifier_head notifier;bool v2;bool nesting; };@@ -549,7 +551,8 @@ static long vfio_iommu_type1_pin_pages(void *iommu_data,mutex_lock (&iommu->lock); -if (!iommu->external_domain) { +/* Fail if notifier list is empty */ +if ((!iommu->external_domain) || (!iommu->notifier.head)) { ret = -EINVAL ; goto pin_done; }@@ -768,6 +771,50 @@ static unsigned long vfio_pgsize_bitmap(struct vfio_iommu *iommu)return bitmap; } +/* +* This function finds pfn in domain->external_addr_space->pfn_list for given +* iova range. If pfn exist, notify pfn to registered notifier list. On +* receiving notifier callback, vendor driver should invalidate the mapping and +* call vfio_unpin_pages() to unpin this pfn. With that vfio_pfn for this pfn +* gets removed from rb tree of pfn_list. That re-arranges rb tree, so while +* searching for next vfio_pfn in rb tree, start search from first node again. +* If any vendor driver doesn't unpin that pfn, vfio_pfn would not get removed +* from rb tree and so in next search vfio_pfn would be same as previous +* vfio_pfn. In that case, exit from loop. +*/ +static void vfio_notifier_call_chain (struct vfio_iommu *iommu, +struct vfio_iommu_type1_dma_unmap *unmap) +{ +struct vfio_domain *domain = iommu->external_domain; +struct rb_node *n; +struct vfio_pfn *vpfn =NULL , *prev_vpfn; + +do { + prev_vpfn = vpfn; +mutex_lock (&domain->external_addr_space->pfn_list_lock); + + n =rb_first (&domain->external_addr_space->pfn_list); + +for (; n; n =rb_next (n), vpfn =NULL ) { + vpfn = rb_entry(n, struct vfio_pfn, node); + +if ((vpfn->iova >= unmap->iova) && + (vpfn->iova < unmap->iova + unmap->size)) +break ; + } + +mutex_unlock (&domain->external_addr_space->pfn_list_lock); + +/* Notify any listeners about DMA_UNMAP */ +if (vpfn) +blocking_notifier_call_chain (&iommu->notifier, +VFIO_IOMMU_NOTIFY_DMA_UNMAP , + &vpfn->pfn); + }while (vpfn && (prev_vpfn != vpfn)); + +WARN_ON (vpfn); +} +static int vfio_dma_do_unmap (struct vfio_iommu *iommu,struct vfio_iommu_type1_dma_unmap *unmap) {@@ -844,6 +891,9 @@ unlock:/* Report how much was unmapped */ unmap->size = unmapped; +if (unmapped && iommu->external_domain) +vfio_notifier_call_chain (iommu, unmap); +return ret; }@@ -1418,6 +1468,7 @@ static void *vfio_iommu_type1_open(unsigned long arg)INIT_LIST_HEAD (&iommu->domain_list); iommu->dma_list =RB_ROOT ;mutex_init (&iommu->lock); +BLOCKING_INIT_NOTIFIER_HEAD (&iommu->notifier);return iommu; }@@ -1555,16 +1606,34 @@ static long vfio_iommu_type1_ioctl(void *iommu_data,return -ENOTTY ; } +static int vfio_iommu_type1_register_notifier (void *iommu_data, +struct notifier_block *nb) +{ +struct vfio_iommu *iommu = iommu_data; + +return blocking_notifier_chain_register (&iommu->notifier, nb); +} + +static int vfio_iommu_type1_unregister_notifier (void *iommu_data, +struct notifier_block *nb) +{ +struct vfio_iommu *iommu = iommu_data; + +return blocking_notifier_chain_unregister (&iommu->notifier, nb); +} +static const struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1 = { - .name ="vfio-iommu-type1" , - .owner =THIS_MODULE , - .open = vfio_iommu_type1_open, - .release = vfio_iommu_type1_release, - .ioctl = vfio_iommu_type1_ioctl, - .attach_group = vfio_iommu_type1_attach_group, - .detach_group = vfio_iommu_type1_detach_group, - .pin_pages = vfio_iommu_type1_pin_pages, - .unpin_pages = vfio_iommu_type1_unpin_pages, + .name ="vfio-iommu-type1" , + .owner =THIS_MODULE , + .open = vfio_iommu_type1_open, + .release = vfio_iommu_type1_release, + .ioctl = vfio_iommu_type1_ioctl, + .attach_group = vfio_iommu_type1_attach_group, + .detach_group = vfio_iommu_type1_detach_group, + .pin_pages = vfio_iommu_type1_pin_pages, + .unpin_pages = vfio_iommu_type1_unpin_pages, + .register_notifier = vfio_iommu_type1_register_notifier, + .unregister_notifier = vfio_iommu_type1_unregister_notifier, };static int __init vfio_iommu_type1_init (void )include/linux/vfio.h +11
-0@@ -80,6 +80,10 @@ struct vfio_iommu_driver_ops { unsigned long *phys_pfn);long (*unpin_pages)(void *iommu_data,unsigned long *pfn,long npage); +int (*register_notifier)(void *iommu_data, +struct notifier_block *nb); +int (*unregister_notifier)(void *iommu_data, +struct notifier_block *nb); };extern int vfio_register_iommu_driver (const struct vfio_iommu_driver_ops *ops);@@ -137,6 +141,13 @@ extern long vfio_pin_pages(struct device *dev, unsigned long *user_pfn,extern long vfio_unpin_pages (struct device *dev,unsigned long *pfn,long npage); +#define VFIO_IOMMU_NOTIFY_DMA_UNMAP 1 + +extern int vfio_register_notifier (struct device *dev, +struct notifier_block *nb); + +extern int vfio_unregister_notifier (struct device *dev, +struct notifier_block *nb);/* * IRQfd - generic */
Kirti Wankhede
maintainerRe: [PATCH v10 10/19] vfio iommu: Add blocking notifier to notify DMA_UNMAP
Quoted 37 lines · Jike Song
Quoted 21 lines · Kirti Wankhede
Added blocking notifier to IOMMU TYPE1 driver to notify vendor drivers about
DMA_UNMAP.
Exported two APIsvfio_register_notifier()andvfio_unregister_notifier(). Vendor driver should register notifer using these APIs.
Vendor driver should useVFIO_IOMMU_NOTIFY_DMA_UNMAPaction to invalidate mappings.Signed-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id I5910d0024d6be87f3e8d3e0ca0eaeaaa0b17f271--- drivers/vfio/vfio.c | 73 +++++++++++++++++++++++++++++++++ drivers/vfio/vfio_iommu_type1.c | 89 ++++++++++++++++++++++++++++++++++++----- include/linux/vfio.h | 11 +++++ 3 files changed, 163 insertions(+), 10 deletions(-)drivers/vfio/vfio.c +0
-0drivers/vfio/vfio.c err_unpin_pages: · line 1895
Quoted 5 lines
drivers/vfio/vfio.c +2
-0@@ -1891,6 +1891,79 @@ err_unpin_pages: }EXPORT_SYMBOL (vfio_unpin_pages); +int vfio_register_notifier (struct device *dev,struct notifier_block *nb) +{Hi Kirti,
Given that below 4 methods are members of
vfio_iommu_driver_ops:
pin_pages
unpin_pages
register_notifier
unregister_notifier
Quoted 7 lines
the names of exposed VFIO APIs could possibly be clearer:
vfio_iommu_pin_pages
vfio_iommu_unpin_pages
vfio_iommu_register_notifier
vfio_iommu_unreigster_nodier
Hey Jike,
I had followed the same style as other members in this structure:
attach_group
detach_group
Thanks,
Kirti
Re: [PATCH v10 10/19] vfio iommu: Add blocking notifier to notify DMA_UNMAP
Quoted 46 lines · Kirti Wankhede
Quoted 37 lines · Jike Song
Quoted 21 lines · Kirti Wankhede
Added blocking notifier to IOMMU TYPE1 driver to notify vendor drivers about
DMA_UNMAP.
Exported two APIsvfio_register_notifier()andvfio_unregister_notifier(). Vendor driver should register notifer using these APIs.
Vendor driver should useVFIO_IOMMU_NOTIFY_DMA_UNMAPaction to invalidate mappings.Signed-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id I5910d0024d6be87f3e8d3e0ca0eaeaaa0b17f271--- drivers/vfio/vfio.c | 73 +++++++++++++++++++++++++++++++++ drivers/vfio/vfio_iommu_type1.c | 89 ++++++++++++++++++++++++++++++++++++----- include/linux/vfio.h | 11 +++++ 3 files changed, 163 insertions(+), 10 deletions(-)drivers/vfio/vfio.c +0
-0drivers/vfio/vfio.c err_unpin_pages: · line 1895
Quoted 5 lines
drivers/vfio/vfio.c +2
-0@@ -1891,6 +1891,79 @@ err_unpin_pages: }EXPORT_SYMBOL (vfio_unpin_pages); +int vfio_register_notifier (struct device *dev,struct notifier_block *nb) +{Hi Kirti,
Given that below 4 methods are members of
vfio_iommu_driver_ops:
pin_pages
unpin_pages
register_notifier
unregister_notifierQuoted 7 lines
the names of exposed VFIO APIs could possibly be clearer:
vfio_iommu_pin_pages
vfio_iommu_unpin_pages
vfio_iommu_register_notifier
vfio_iommu_unreigster_nodier
Quoted 7 lines
Hey Jike,
I had followed the same style as other members in this structure:
attach_group
detach_group
I mean the APIs exposed. For example, vfio_register_notifier() is somehow by the name too generic to know what it is provided for.
--
Thanks,
Jike
Kirti Wankhede
maintainerPATCH v10 11/19 vfio: Introduce common function to add capabilities
In reply to Kirti WankhedeVendor driver using mediated device framework should use vfio_info_add_capability() to add capabilities.
Introduced this function to reduce code duplication in vendor drivers.
--- drivers/vfio/vfio.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++- include/linux/vfio.h | 3 +++ 2 files changed, 63 insertions(+), 1 deletion(-)
drivers/vfio/vfio.c +60 -1
@@ -1797,7 +1797,66 @@ void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset)for (tmp = caps->buf; tmp->next; tmp = (void *)tmp + tmp->next - offset) tmp->next += offset; } -EXPORT_SYMBOL_GPL (vfio_info_cap_shift); +EXPORT_SYMBOL (vfio_info_cap_shift); + +static int sparse_mmap_cap (struct vfio_info_cap *caps,void *cap_type) +{ +struct vfio_info_cap_header *header; +struct vfio_region_info_cap_sparse_mmap *sparse_cap, *sparse = cap_type; +size_t size; + + size =sizeof (*sparse) + sparse->nr_areas *sizeof (*sparse->areas); + header =vfio_info_cap_add (caps, size, +VFIO_REGION_INFO_CAP_SPARSE_MMAP ,1 ); +if (IS_ERR (header)) +return PTR_ERR (header); + + sparse_cap =container_of (header, + struct vfio_region_info_cap_sparse_mmap, header); + sparse_cap->nr_areas = sparse->nr_areas; +memcpy (sparse_cap->areas, sparse->areas, + sparse->nr_areas *sizeof (*sparse->areas)); +return 0 ; +} + +static int region_type_cap (struct vfio_info_cap *caps,void *cap_type) +{ +struct vfio_info_cap_header *header; +struct vfio_region_info_cap_type *type_cap, *cap = cap_type; + + header =vfio_info_cap_add (caps,sizeof (*cap), +VFIO_REGION_INFO_CAP_TYPE ,1 ); +if (IS_ERR (header)) +return PTR_ERR (header); + + type_cap = container_of(header, struct vfio_region_info_cap_type, + header); + type_cap->type = cap->type; + type_cap->subtype = cap->subtype; +return 0 ; +} + +int vfio_info_add_capability (struct vfio_info_cap *caps,int cap_type_id, +void *cap_type) +{ +int ret = -EINVAL ; + +if (!cap_type) +return 0 ; + +switch (cap_type_id) { +case VFIO_REGION_INFO_CAP_SPARSE_MMAP : + ret =sparse_mmap_cap (caps, cap_type); +break ; + +case VFIO_REGION_INFO_CAP_TYPE : + ret =region_type_cap (caps, cap_type); +break ; + } + +return ret; +} +EXPORT_SYMBOL (vfio_info_add_capability);/*
include/linux/vfio.h +3 -0
@@ -111,6 +111,9 @@ extern struct vfio_info_cap_header *vfio_info_cap_add( struct vfio_info_cap *caps, size_t size, u16 id, u16 version);extern void vfio_info_cap_shift (struct vfio_info_cap *caps,size_t offset); +extern int vfio_info_add_capability (struct vfio_info_cap *caps, +int cap_type_id,void *cap_type); +struct pci_dev ;#ifdef CONFIG_EEH extern void vfio_spapr_pci_eeh_open (struct pci_dev *pdev);
Signature · 1 line
2.7.0
Kirti Wankhede
maintainerPATCH v10 12/19 vfio_pci: Update vfio_pci to use vfio_info_add_capability()
In reply to Kirti WankhedeUpdate msix_sparse_mmap_cap() to use vfio_info_add_capability() Update region type capability to use vfio_info_add_capability()
--- drivers/vfio/pci/vfio_pci.c | 49 ++++++++++++++++++--------------------------- 1 file changed, 19 insertions(+), 30 deletions(-)
drivers/vfio/pci/vfio_pci.c +19 -30
@@ -558,10 +558,9 @@ static int vfio_pci_for_each_slot_or_bus(struct pci_dev *pdev,static int msix_sparse_mmap_cap (struct vfio_pci_device *vdev,struct vfio_info_cap *caps) { -struct vfio_info_cap_header *header;struct vfio_region_info_cap_sparse_mmap *sparse;size_t end, size; -int nr_areas =2 , i =0 ; +int nr_areas =2 , i =0 , ret; end =pci_resource_len (vdev->pdev, vdev->msix_bar);
@@ -572,13 +571,10 @@ static int msix_sparse_mmap_cap(struct vfio_pci_device *vdev, size =sizeof (*sparse) + (nr_areas *sizeof (*sparse->areas)); - header =vfio_info_cap_add (caps, size, -VFIO_REGION_INFO_CAP_SPARSE_MMAP ,1 ); -if (IS_ERR (header)) -return PTR_ERR (header); + sparse =kzalloc (size,GFP_KERNEL ); +if (!sparse) +return -ENOMEM ; - sparse =container_of (header, - struct vfio_region_info_cap_sparse_mmap, header); sparse->nr_areas = nr_areas;if (vdev->msix_offset &PAGE_MASK ) {
@@ -594,26 +590,11 @@ static int msix_sparse_mmap_cap(struct vfio_pci_device *vdev, i++; } -return 0 ; -} - -static int region_type_cap (struct vfio_pci_device *vdev, -struct vfio_info_cap *caps, -unsigned int type,unsigned int subtype) -{ -struct vfio_info_cap_header *header; -struct vfio_region_info_cap_type *cap; - - header =vfio_info_cap_add (caps,sizeof (*cap), -VFIO_REGION_INFO_CAP_TYPE ,1 ); -if (IS_ERR (header)) -return PTR_ERR (header); - - cap = container_of(header, struct vfio_region_info_cap_type, header); - cap->type = type; - cap->subtype = subtype; + ret =vfio_info_add_capability (caps,VFIO_REGION_INFO_CAP_SPARSE_MMAP , + sparse); +kfree (sparse); -return 0 ; +return ret; } int vfio_pci_register_dev_region(struct vfio_pci_device *vdev,
@@ -752,6 +733,9 @@ static long vfio_pci_ioctl(void *device_data,break ;default : + { +struct vfio_region_info_cap_type cap_type; +if (info.index >=VFIO_PCI_NUM_REGIONS + vdev->num_regions)return -EINVAL ;
@@ -762,11 +746,16 @@ static long vfio_pci_ioctl(void *device_data, info.size = vdev->region[i].size; info.flags = vdev->region[i].flags; - ret =region_type_cap (vdev, &caps, - vdev->region[i].type, - vdev->region[i].subtype); + cap_type.type = vdev->region[i].type; + cap_type.subtype = vdev->region[i].subtype; + + ret =vfio_info_add_capability (&caps, +VFIO_REGION_INFO_CAP_TYPE , + &cap_type);if (ret)return ret; + + } }if (caps.size) {
Signature · 1 line
2.7.0
Kirti Wankhede
maintainerPATCH v10 13/19 vfio: Introduce vfio_set_irqs_validate_and_prepare()
In reply to Kirti WankhedeVendor driver using mediated device framework would use same mechnism to validate and prepare IRQs. Introducing this function to reduce code replication in multiple drivers.
--- drivers/vfio/vfio.c | 39 +++++++++++++++++++++++++++++++++++++++ include/linux/vfio.h | 4 ++++ 2 files changed, 43 insertions(+)
drivers/vfio/vfio.c +39 -0
@@ -1858,6 +1858,45 @@ int vfio_info_add_capability(struct vfio_info_cap *caps, int cap_type_id, }EXPORT_SYMBOL (vfio_info_add_capability); +int vfio_set_irqs_validate_and_prepare (struct vfio_irq_set *hdr,int num_irqs, +int max_irq_type,size_t *data_size) +{ +unsigned long minsz; + + 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
@@ -114,6 +114,10 @@ extern void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);extern int vfio_info_add_capability (struct vfio_info_cap *caps,int cap_type_id,void *cap_type); +extern int vfio_set_irqs_validate_and_prepare (struct vfio_irq_set *hdr, +int num_irqs,int max_irq_type, +size_t *data_size); +struct pci_dev ;#ifdef CONFIG_EEH extern void vfio_spapr_pci_eeh_open (struct pci_dev *pdev);
Signature · 1 line
2.7.0
Kirti Wankhede
maintainerPATCH v10 14/19 vfio_pci: Updated to use vfio_set_irqs_validate_and_prepare()
In reply to Kirti WankhedeUpdated vfio_pci.c file to use vfio_set_irqs_validate_and_prepare()
--- drivers/vfio/pci/vfio_pci.c | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-)
drivers/vfio/pci/vfio_pci.c +9 -20
@@ -819,35 +819,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
Kirti Wankhede
maintainerPATCH v10 15/19 vfio_platform: Updated to use vfio_set_irqs_validate_and_prepare()
In reply to Kirti WankhedeUpdated vfio_platform_common.c file to usevfio_set_irqs_validate_and_prepare()
--- drivers/vfio/platform/vfio_platform_common.c | 31 +++++++--------------------- 1 file changed, 8 insertions(+), 23 deletions(-)
drivers/vfio/platform/vfio_platform_common.c +8 -23
@@ -364,36 +364,21 @@ static long vfio_platform_ioctl(void *device_data,struct vfio_irq_set hdr;u8 *data =NULL ;int ret =0 ; +size_t data_size =0 ; minsz = offsetofend(struct vfio_irq_set, count);if (copy_from_user (&hdr, (void __user *)arg, minsz))return -EFAULT ; -if (hdr.argsz < minsz) -return -EINVAL ; - -if (hdr.index >= vdev->num_irqs) -return -EINVAL ; - -if (hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK | -VFIO_IRQ_SET_ACTION_TYPE_MASK )) -return -EINVAL ; - -if (!(hdr.flags &VFIO_IRQ_SET_DATA_NONE )) { -size_t size; - -if (hdr.flags &VFIO_IRQ_SET_DATA_BOOL ) - size =sizeof (uint8_t ); -else if (hdr.flags &VFIO_IRQ_SET_DATA_EVENTFD ) - size =sizeof (int32_t ); -else -return -EINVAL ; - -if (hdr.argsz - minsz < size) -return -EINVAL ; + ret =vfio_set_irqs_validate_and_prepare (&hdr, vdev->num_irqs, + vdev->num_irqs, &data_size); +if (ret) +return ret; - data =memdup_user ((void __user *)(arg + minsz), size); +if (data_size) { + data =memdup_user ((void __user *)(arg + minsz), + data_size);if (IS_ERR (data))return PTR_ERR (data); }
Signature · 1 line
2.7.0
Kirti Wankhede
maintainerPATCH v10 16/19 vfio: Define device_api strings
In reply to Kirti WankhedeDefined device API strings. Vendor driver using mediated device framework should use corresponding string for device_api attribute.
--- include/uapi/linux/vfio.h | 10 ++++++++++ 1 file changed, 10 insertions(+)
include/uapi/linux/vfio.h +10 -0
@@ -203,6 +203,16 @@ struct vfio_device_info { };#define VFIO_DEVICE_GET_INFO _IO(VFIO_TYPE, VFIO_BASE + 7) +/* +* Vendor driver using Mediated device framework should provide device_api +* attribute in supported type attribute groups. Device API string should be one +* of the following corresponding to device flags in vfio_device_info structure. +*/ + +#define VFIO_DEVICE_API_PCI_STRING "vfio-pci" +#define VFIO_DEVICE_API_PLATFORM_STRING "vfio-platform" +#define VFIO_DEVICE_API_AMBA_STRING "vfio-amba" +/** * VFIO_DEVICE_GET_REGION_INFO - _IOWR(VFIO_TYPE, VFIO_BASE + 8, * struct vfio_region_info)
Signature · 1 line
2.7.0
Kirti Wankhede
maintainerPATCH v10 17/19 docs: Add Documentation for Mediated devices
In reply to Kirti WankhedeAdd file Documentation/vfio-mediated-device.txt that include details of mediated device framework.
--- Documentation/vfio-mdev/vfio-mediated-device.txt | 297 +++++++++++++++++++++++ 1 file changed, 297 insertions(+) create mode 100644 Documentation/vfio-mdev/vfio-mediated-device.txt
Documentation/vfio-mdev/vfio-mediated-device.txt new file +297 -0
@@ -0,0 +1,297 @@ +/* + * 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. + +* name + + This attribute should show human readable name. This is optional attribute. + +* description + + This attribute should show brief features/description of the type. This is + optional attribute. + +Directories and Files Under the sysfs for Each mdev Device +---------------------------------------------------------- + +|- [parent phy device] +|--- [$MDEV_UUID] + |--- remove + |--- mdev_type {link to its type} + |--- vendor-specific-attributes [optional] + +* remove (write only) +Writing '1' to the 'remove' file destroys the mdev device. The vendor driver can +fail the remove() callback if that device is active and the vendor driver +doesn't support hot unplug. + +Example: + # echo 1 > /sys/bus/mdev/devices/$mdev_UUID/remove + +Mediated device Hot plug: +------------------------ + +Mediated devices can be created and assigned at runtime. The procedure to hot +plug a mediated device is the same as the procedure to hot plug a PCI device. + +Translation APIs for Mediated Devices +===================================== + +The following APIs are provided for translating user pfn to host pfn in a VFIO +driver: + + extern long vfio_pin_pages(struct device *dev, unsigned long *user_pfn, + long npage, int prot, unsigned long *phys_pfn); + + extern long vfio_unpin_pages(struct device *dev, unsigned long *pfn, + long npage); + +These functions call back into the back-end IOMMU module by using the pin_pages +and unpin_pages callbacks of the struct vfio_iommu_driver_ops[4]. Currently +these callbacks are supported in the TYPE1 IOMMU module. To enable them for +other IOMMU backend modules, such as PPC64 sPAPR module, they need to provide +these two callback functions. + +References +---------- + +[1] See Documentation/vfio.txt for more information on VFIO. +[2] struct mdev_driver in include/linux/mdev.h +[3] struct parent_ops in include/linux/mdev.h +[4] struct vfio_iommu_driver_ops in include/linux/vfio.h
Signature · 1 line
2.7.0
Kirti Wankhede
maintainerPATCH v10 18/19 docs: Sysfs ABI for mediated device framework
In reply to Kirti WankhedeAdded details of sysfs ABI for mediated device framework
--- Documentation/ABI/testing/sysfs-bus-vfio-mdev | 111 ++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-bus-vfio-mdev
Documentation/ABI/testing/sysfs-bus-vfio-mdev new file +111 -0
@@ -0,0 +1,111 @@ +What: /sys/.../<device>/mdev_supported_types/ +Date: October 2016 +Contact: Kirti Wankhede <[email protected]> +Description: + This directory contains list of directories of currently + supported mediated device types and their details for + <device>. Supported type attributes are defined by the + vendor driver who registers with Mediated device framework. + Each supported type is a directory whose name is created + by adding the device driver string as a prefix to the + string provided by the vendor driver. + +What: /sys/.../<device>/mdev_supported_types/<type-id>/ +Date: October 2016 +Contact: Kirti Wankhede <[email protected]> +Description: + This directory gives details of supported type, like name, + description, available_instances, device_api etc. + 'device_api' and 'available_instances' are mandatory + attributes to be provided by vendor driver. 'name', + 'description' and other vendor driver specific attributes + are optional. + +What: /sys/.../mdev_supported_types/<type-id>/create +Date: October 2016 +Contact: Kirti Wankhede <[email protected]> +Description: + Writing UUID to this file will create mediated device of + type <type-id> for parent device <device>. This is a + write-only file. + For example: + # echo "83b8f4f2-509f-382f-3c1e-e6bfe0fa1001" > \ + /sys/devices/foo/mdev_supported_types/foo-1/create + +What: /sys/.../mdev_supported_types/<type-id>/devices/ +Date: October 2016 +Contact: Kirti Wankhede <[email protected]> +Description: + This directory contains symbolic links pointing to mdev + devices sysfs entries which are created of this <type-id>. + +What: /sys/.../mdev_supported_types/<type-id>/available_instances +Date: October 2016 +Contact: Kirti Wankhede <[email protected]> +Description: + Reading this attribute will show the number of mediated + devices of type <type-id> that can be created. This is a + readonly file. +Users: + Userspace applications interested in creating mediated + device of that type. Userspace application should check + the number of available instances could be created before + creating mediated device of this type. + +What: /sys/.../mdev_supported_types/<type-id>/device_api +Date: October 2016 +Contact: Kirti Wankhede <[email protected]> +Description: + Reading this attribute will show VFIO device API supported + by this type. For example, "vfio-pci" for a PCI device, + "vfio-platform" for platform device. + +What: /sys/.../mdev_supported_types/<type-id>/name +Date: October 2016 +Contact: Kirti Wankhede <[email protected]> +Description: + Reading this attribute will show human readable name of the + mediated device that will get created of type <type-id>. + This is optional attribute. For example: "Grid M60-0Q" +Users: + Userspace applications interested in knowing the name of + a particular <type-id> that can help in understanding the + type of mediated device. + +What: /sys/.../mdev_supported_types/<type-id>/description +Date: October 2016 +Contact: Kirti Wankhede <[email protected]> +Description: + Reading this attribute will show description of the type of + mediated device that will get created of type <type-id>. + This is optional attribute. For example: + "2 heads, 512M FB, 2560x1600 maximum resolution" +Users: + Userspace applications interested in knowing the details of + a particular <type-id> that can help in understanding the + features provided by that type of mediated device. + +What: /sys/.../<device>/<UUID>/ +Date: October 2016 +Contact: Kirti Wankhede <[email protected]> +Description: + This directory represents device directory of mediated + device. It contains all the attributes related to mediated + device. + +What: /sys/.../<device>/<UUID>/mdev_type +Date: October 2016 +Contact: Kirti Wankhede <[email protected]> +Description: + This is symbolic link pointing to supported type, <type-id> + directory of which this mediated device is created. + +What: /sys/.../<device>/<UUID>/remove +Date: October 2016 +Contact: Kirti Wankhede <[email protected]> +Description: + Writing '1' to this file destroys the mediated device. The + vendor driver can fail the remove() callback if that device + is active and the vendor driver doesn't support hot unplug. + Example: + # echo 1 > /sys/bus/mdev/devices/<UUID>/remove
Signature · 1 line
2.7.0
Re: [PATCH v10 18/19] docs: Sysfs ABI for mediated device framework
Quoted 29 lines · Kirti Wankhede
Added details of sysfs ABI for mediated device framework
Signed-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id Icb0fd4ed58a2fa793fbcb1c3d5009a4403c1f3ac--- Documentation/ABI/testing/sysfs-bus-vfio-mdev | 111 ++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-bus-vfio-mdevDocumentation/ABI/testing/sysfs-bus-vfio-mdev new file +13
-0@@ -0,0 +1,111 @@ +What: /sys/.../<device>/mdev_supported_types/ +Date: October 2016 +Contact: Kirti Wankhede <[email protected]> +Description: + This directory contains list of directories of currently + supported mediated device types and their details for + <device>. Supported type attributes are defined by the + vendor driver who registers with Mediated device framework. + Each supported type is a directory whose name is created + by adding the device driver string as a prefix to the + string provided by the vendor driver. + +What: /sys/.../<device>/mdev_supported_types/<type-id>/
Documentation/ABI/testing/sysfs-bus-vfio-mdev line 23
Quoted 10 lines
Documentation/ABI/testing/sysfs-bus-vfio-mdev new file +10
-0+Date: October 2016 +Contact: Kirti Wankhede <[email protected]> +Description: + This directory gives details of supported type, like name, + description, available_instances, device_api etc. + 'device_api' and 'available_instances' are mandatory + attributes to be provided by vendor driver. 'name', + 'description' and other vendor driver specific attributes + are optional. +
Hi Kirti,
Is there any checking in the mdev framework that mandatory attributes are actually provided?
--
Thanks,
Jike
Quoted 89 lines
Documentation/ABI/testing/sysfs-bus-vfio-mdev new file +88
-0+What: /sys/.../mdev_supported_types/<type-id>/create +Date: October 2016 +Contact: Kirti Wankhede <[email protected]> +Description: + Writing UUID to this file will create mediated device of + type <type-id> for parent device <device>. This is a + write-only file. + For example: + # echo "83b8f4f2-509f-382f-3c1e-e6bfe0fa1001" > \ + /sys/devices/foo/mdev_supported_types/foo-1/create + +What: /sys/.../mdev_supported_types/<type-id>/devices/ +Date: October 2016 +Contact: Kirti Wankhede <[email protected]> +Description: + This directory contains symbolic links pointing to mdev + devices sysfs entries which are created of this <type-id>. + +What: /sys/.../mdev_supported_types/<type-id>/available_instances +Date: October 2016 +Contact: Kirti Wankhede <[email protected]> +Description: + Reading this attribute will show the number of mediated + devices of type <type-id> that can be created. This is a + readonly file. +Users: + Userspace applications interested in creating mediated + device of that type. Userspace application should check + the number of available instances could be created before + creating mediated device of this type. + +What: /sys/.../mdev_supported_types/<type-id>/device_api +Date: October 2016 +Contact: Kirti Wankhede <[email protected]> +Description: + Reading this attribute will show VFIO device API supported + by this type. For example, "vfio-pci" for a PCI device, + "vfio-platform" for platform device. + +What: /sys/.../mdev_supported_types/<type-id>/name +Date: October 2016 +Contact: Kirti Wankhede <[email protected]> +Description: + Reading this attribute will show human readable name of the + mediated device that will get created of type <type-id>. + This is optional attribute. For example: "Grid M60-0Q" +Users: + Userspace applications interested in knowing the name of + a particular <type-id> that can help in understanding the + type of mediated device. + +What: /sys/.../mdev_supported_types/<type-id>/description +Date: October 2016 +Contact: Kirti Wankhede <[email protected]> +Description: + Reading this attribute will show description of the type of + mediated device that will get created of type <type-id>. + This is optional attribute. For example: + "2 heads, 512M FB, 2560x1600 maximum resolution" +Users: + Userspace applications interested in knowing the details of + a particular <type-id> that can help in understanding the + features provided by that type of mediated device. + +What: /sys/.../<device>/<UUID>/ +Date: October 2016 +Contact: Kirti Wankhede <[email protected]> +Description: + This directory represents device directory of mediated + device. It contains all the attributes related to mediated + device. + +What: /sys/.../<device>/<UUID>/mdev_type +Date: October 2016 +Contact: Kirti Wankhede <[email protected]> +Description: + This is symbolic link pointing to supported type, <type-id> + directory of which this mediated device is created. + +What: /sys/.../<device>/<UUID>/remove +Date: October 2016 +Contact: Kirti Wankhede <[email protected]> +Description: + Writing '1' to this file destroys the mediated device. The + vendor driver can fail the remove() callback if that device + is active and the vendor driver doesn't support hot unplug. + Example: + # echo 1 > /sys/bus/mdev/devices/<UUID>/remove
Kirti Wankhede
maintainerRe: [PATCH v10 18/19] docs: Sysfs ABI for mediated device framework
Quoted 43 lines · Jike Song
Quoted 29 lines · Kirti Wankhede
Added details of sysfs ABI for mediated device framework
Signed-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id Icb0fd4ed58a2fa793fbcb1c3d5009a4403c1f3ac--- Documentation/ABI/testing/sysfs-bus-vfio-mdev | 111 ++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-bus-vfio-mdevDocumentation/ABI/testing/sysfs-bus-vfio-mdev new file +13
-0@@ -0,0 +1,111 @@ +What: /sys/.../<device>/mdev_supported_types/ +Date: October 2016 +Contact: Kirti Wankhede <[email protected]> +Description: + This directory contains list of directories of currently + supported mediated device types and their details for + <device>. Supported type attributes are defined by the + vendor driver who registers with Mediated device framework. + Each supported type is a directory whose name is created + by adding the device driver string as a prefix to the + string provided by the vendor driver. + +What: /sys/.../<device>/mdev_supported_types/<type-id>/Documentation/ABI/testing/sysfs-bus-vfio-mdev line 23
Quoted 10 lines
Documentation/ABI/testing/sysfs-bus-vfio-mdev new file +10
-0+Date: October 2016 +Contact: Kirti Wankhede <[email protected]> +Description: + This directory gives details of supported type, like name, + description, available_instances, device_api etc. + 'device_api' and 'available_instances' are mandatory + attributes to be provided by vendor driver. 'name', + 'description' and other vendor driver specific attributes + are optional. +Hi Kirti,
Quoted 3 lines
Is there any checking in the mdev framework that mandatory attributes are actually provided?
No. These are exposed so that libvirt can use those to create devices. If those are not provided, libvirt would not work.
Kirti.
Kirti Wankhede
maintainerPATCH v10 19/19 docs: Sample driver to demonstrate how to use Mediated device framework.
In reply to Kirti WankhedeThe Sample driver creates mdev device that simulates serial port over PCI card.
--- Documentation/vfio-mdev/Makefile | 13 + Documentation/vfio-mdev/mtty.c | 1503 ++++++++++++++++++++++ Documentation/vfio-mdev/vfio-mediated-device.txt | 103 +- 3 files changed, 1618 insertions(+), 1 deletion(-) create mode 100644 Documentation/vfio-mdev/Makefile create mode 100644 Documentation/vfio-mdev/mtty.c
Documentation/vfio-mdev/Makefile new file +13 -0
@@ -0,0 +1,13 @@ +# +# Makefile for mtty.c file +# +KERNEL_DIR:=/lib/modules/$(shell uname -r)/build + +obj-m:=mtty.o + +modules clean modules_install: + $(MAKE) -C $(KERNEL_DIR) SUBDIRS=$(PWD) $@ + +default: modules + +module: modules
Documentation/vfio-mdev/mtty.c new file +1503 -0
1,503 lines not on this page · raw message
Documentation/vfio-mdev/vfio-mediated-device.txt +102 -1
110 lines not on this page · raw message
Signature · 1 line
2.7.0
Jonathan Corbet
maintainerRe: [PATCH v10 19/19] docs: Sample driver to demonstrate how to use Mediated device framework.
Quoted 10 lines · On Thu, 27 Oct 2016 02:59:26 +0530 Kirti Wankhede <[email protected]> wrote:
The Sample driver creates mdev device that simulates serial port over PCI card.
Signed-off-by Kirti Wankhede <[email protected]>Signed-off-by Neo Jia <[email protected]>Change-Id I857f8f12f8b275f2498dfe8c628a5cdc7193b1b2--- Documentation/vfio-mdev/Makefile | 13 + Documentation/vfio-mdev/mtty.c | 1503 ++++++++++++++++++++++ Documentation/vfio-mdev/vfio-mediated-device.txt | 103 +-
We've been working for a bit to get the buildable code *out* of the documentation tree. I think this should go into samples/ instead.
Thanks,
jon
Quoted 75 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 v10?
vfio:
- Split commits in multple individual commits.
- Removed the function added in v9 to getdevice_apistring.
- Defined constant strings ininclude/uapi/linux/vfio.hthat should be used by vendor driver fordevice_apiattribute.
vfio_iommu_type1:
- Fixed accounting when pass through device is unplugged while mdev device exist in a domain.
- Added blocking notifier to notifyDMA_UNMAPto vendor driver to invalidate mappings.
- Exported APIs to register notifier forDMA_UNMAPaction.Documentation:
- Added sysfs ABI for mediated device framework.
- UpdatedDocumentation/vfio-mdev/vfio-mediated-device.txt.
- Updatedmtty.cwith bug fixes.Kirti Wankhede (19):
vfio: Mediated device Core driver
vfio: VFIO based driver for Mediated devices
vfio: Rearrange functions to getvfio_groupfrom dev
vfio: Common function to incrementcontainer_users
vfio iommu: Added pin and unpin callback functions to
vfio_iommu_driver_ops
vfio iommu type1: Update arguments ofvfio_lock_acctvfio iommu type1: Update argument ofvaddr_get_pfn()vfio iommu type1: Addfind_iommu_group()function
vfio iommu type1: Add support for mediated devices
vfio iommu: Add blocking notifier to notifyDMA_UNMAP
vfio: Introduce common function to add capabilities
vfio_pci: Updatevfio_pcito usevfio_info_add_capability()
vfio: Introducevfio_set_irqs_validate_and_prepare()
vfio_pci: Updated to usevfio_set_irqs_validate_and_prepare()
vfio_platform: Updated to usevfio_set_irqs_validate_and_prepare()
vfio: Definedevice_apistrings
docs: Add Documentation for Mediated devices
docs: Sysfs ABI for mediated device framework
docs: Sample driver to demonstrate how to use Mediated device
framework.Documentation/ABI/testing/sysfs-bus-vfio-mdev | 111 ++ Documentation/vfio-mdev/Makefile | 13 + Documentation/vfio-mdev/mtty.c | 1503 ++++++++++++++++++++++ Documentation/vfio-mdev/vfio-mediated-device.txt | 398 ++++++ drivers/vfio/Kconfig | 1 + drivers/vfio/Makefile | 1 + drivers/vfio/mdev/Kconfig | 17 + drivers/vfio/mdev/Makefile | 5 + drivers/vfio/mdev/mdev_core.c | 384 ++++++ drivers/vfio/mdev/mdev_driver.c | 122 ++ drivers/vfio/mdev/mdev_private.h | 41 + drivers/vfio/mdev/mdev_sysfs.c | 286 ++++ drivers/vfio/mdev/vfio_mdev.c | 148 +++ drivers/vfio/pci/vfio_pci.c | 78 +- drivers/vfio/platform/vfio_platform_common.c | 31 +- drivers/vfio/vfio.c | 322 ++++- drivers/vfio/vfio_iommu_type1.c | 808 ++++++++++-- include/linux/mdev.h | 167 +++ include/linux/vfio.h | 30 +- include/uapi/linux/vfio.h | 10 + 20 files changed, 4270 insertions(+), 206 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-bus-vfio-mdev 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
Quoted 8 lines
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
A side note:
I rebased KVMGT upon v10, with 2 minor changes:
1, get_user_pages_remote has only 7 args
2, vfio iommu notifier calls vendor callback with iova instead of pfn
so far it works pretty well. Thanks!
--
Thanks,
Jike
Hi,
Quoted 3 lines
I rebased KVMGT upon v10, with 2 minor changes:
1,
get_user_pages_remotehas only 7 args
Appears to be a 4.9 merge window change. v10 as-is applies and builds fine against 4.8, after rebasing to 4.9-rc3 it stops building due to this.
Can you share the patch?
Quoted 1 line
2, vfio iommu notifier calls vendor callback with iova instead of pfn
And this one too?
Also: github seems to have the v9 kvmgt version still. Can you push the update?
The kvmgt branch apparently depends on alot of unmerged stuff, "git describe" says 1669 patches on top of 4.8-rc8.
Can you outline what this is? Mostly drm-next?
How much of this landed in the 4.9 merge window?
thanks,
Gerd
Quoted 8 lines · Gerd Hoffmann
Quoted 3 lines
I rebased KVMGT upon v10, with 2 minor changes:
1,
get_user_pages_remotehas only 7 argsAppears to be a 4.9 merge window change. v10 as-is applies and builds fine against 4.8, after rebasing to 4.9-rc3 it stops building due to this.
Quoted 8 lines
Can you share the patch?
Quoted 1 line
2, vfio iommu notifier calls vendor callback with iova instead of pfn
And this one too?
Also: github seems to have the v9 kvmgt version still. Can you push the update?
Zhenyu will help to push it to 01org/gvt-linux. Those 2 patches added by me are trivial, I guess Kirti will have her formal fixes in next version.
Quoted 5 lines
The kvmgt branch apparently depends on alot of unmerged stuff, "git describe" says 1669 patches on top of 4.8-rc8.
Can you outline what this is? Mostly drm-next?
How much of this landed in the 4.9 merge window?
Yes, mostly drm-next, targeting 4.10 merge window. The overwhelming part is the device-model under drivers/gpu/drm/i915/gvt/.
--
Thanks,
Jike