Skip to contentSkip to conversation

vhost: introduce mdev based hardware backend

PATCH Superseded Tiwei Bie 2 participants 5 messages Oct 31, 2019 – Nov 1, 2019

Subsystems (7)

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Tiwei Bie

author
#0 lore raw

PATCH v4 1/1 vhost: introduce mdev based hardware backend

This patch introduces a mdev based hardware vhost backend. This backend is built on top of the same abstraction used in virtio-mdev and provides a generic vhost interface for userspace to accelerate the virtio devices in guest.

This backend is implemented as a mdev device driver on top of the same mdev device ops used in virtio-mdev but using a different mdev class id, and it will register the device as a VFIO device for userspace to use. Userspace can setup the IOMMU with the existing VFIO container/group APIs and then get the device fd with the device name. After getting the device fd of this device, userspace can use vhost ioctls to setup the backend.

---
This patch depends on below series:
https://lkml.org/lkml/2019/10/30/62

v3 -> v4:
- Rebase on top of virtio-mdev series v6;
- Some minor tweaks and improvements;

v2 -> v3:
- Fix the return value (Jason);
- Don't cache unnecessary information in vhost-mdev (Jason);
- Get rid of the memset in open (Jason);
- Add comments for VHOST_SET_MEM_TABLE, ... (Jason);
- Filter out unsupported features in vhost-mdev (Jason);
- Add _GET_DEVICE_ID ioctl (Jason);
- Add _GET_CONFIG/_SET_CONFIG ioctls (Jason);
- Drop _GET_QUEUE_NUM ioctl (Jason);
- Fix the copy-paste errors in _IOW/_IOR usage;
- Some minor fixes and improvements;

v1 -> v2:
- Replace _SET_STATE with _SET_STATUS (MST);
- Check status bits at each step (MST);
- Report the max ring size and max number of queues (MST);
- Add missing MODULE_DEVICE_TABLE (Jason);
- Only support the network backend w/o multiqueue for now;
- Some minor fixes and improvements;
- Rebase on top of virtio-mdev series v4;

RFC v4 -> v1:
- Implement vhost-mdev as a mdev device driver directly and connect it to VFIO container/group. (Jason);
- Pass ring addresses as GPAs/IOVAs in vhost-mdev to avoid meaningless HVA->GPA translations (Jason);

RFC v3 -> RFC v4:
- Build vhost-mdev on top of the same abstraction used by virtio-mdev (Jason);
- Introduce vhost fd and pass VFIO fd via SET_BACKEND ioctl (MST);

RFC v2 -> RFC v3:
- Reuse vhost's ioctls instead of inventing a VFIO regions/irqs based vhost protocol on top of vfio-mdev (Jason);

RFC v1 -> RFC v2:
- Introduce a new VFIO device type to build a vhost protocol on top of vfio-mdev;

 drivers/vfio/mdev/mdev_core.c    |  20 ++
 drivers/vfio/mdev/mdev_private.h |   1 +
 drivers/vhost/Kconfig            |  12 +
 drivers/vhost/Makefile           |   3 +
 drivers/vhost/mdev.c             | 556 +++++++++++++++++++++++++++++++
 include/linux/mdev.h             |   5 +
 include/uapi/linux/vhost.h       |  18 +
 include/uapi/linux/vhost_types.h |   8 +
 8 files changed, 623 insertions(+)
 create mode 100644 drivers/vhost/mdev.c
drivers/vfio/mdev/mdev_core.c +20 -0
index 22ca589750d8..109dbac01a8f 100644
@@ -96,6 +96,26 @@ mdev_get_virtio_ops(struct mdev_device *mdev)
 }
 EXPORT_SYMBOL(mdev_get_virtio_ops);
 
+/* Specify the vhost device ops for the mdev device, this
+ * must be called during create() callback for vhost mdev device.
+ */
+void mdev_set_vhost_ops(struct mdev_device *mdev,
+			const struct virtio_mdev_device_ops *vhost_ops)
+{
+	mdev_set_class(mdev, MDEV_CLASS_ID_VHOST);
+	mdev->vhost_ops = vhost_ops;
+}
+EXPORT_SYMBOL(mdev_set_vhost_ops);
+
+/* Get the vhost device ops for the mdev device. */
+const struct virtio_mdev_device_ops *
+mdev_get_vhost_ops(struct mdev_device *mdev)
+{
+	WARN_ON(mdev->class_id != MDEV_CLASS_ID_VHOST);
+	return mdev->vhost_ops;
+}
+EXPORT_SYMBOL(mdev_get_vhost_ops);
+
 struct device *mdev_dev(struct mdev_device *mdev)
 {
 	return &mdev->dev;
drivers/vfio/mdev/mdev_private.h +1 -0
index 7b47890c34e7..5597c846e52f 100644
@@ -40,6 +40,7 @@ struct mdev_device {
 	union {
 		const struct vfio_mdev_device_ops *vfio_ops;
 		const struct virtio_mdev_device_ops *virtio_ops;
+		const struct virtio_mdev_device_ops *vhost_ops;
 	};
 };
 
drivers/vhost/Kconfig +12 -0
index 3d03ccbd1adc..062cada28f89 100644
@@ -34,6 +34,18 @@ config VHOST_VSOCK
 	To compile this driver as a module, choose M here: the module will be called
 	vhost_vsock.
 
+config VHOST_MDEV
+	tristate "Vhost driver for Mediated devices"
+	depends on EVENTFD && VFIO && VFIO_MDEV
+	select VHOST
+	default n
+	---help---
+	This kernel module can be loaded in host kernel to accelerate
+	guest virtio devices with the mediated device based backends.
+
+	To compile this driver as a module, choose M here: the module will
+	be called vhost_mdev.
+
 config VHOST
 	tristate
 	---help---
drivers/vhost/Makefile +3 -0
index 6c6df24f770c..ad9c0f8c6d8c 100644
@@ -10,4 +10,7 @@ vhost_vsock-y := vsock.o
 
 obj-$(CONFIG_VHOST_RING) += vringh.o
 
+obj-$(CONFIG_VHOST_MDEV) += vhost_mdev.o
+vhost_mdev-y := mdev.o
+
 obj-$(CONFIG_VHOST)	+= vhost.o
drivers/vhost/mdev.c new file +556 -0

556 lines not on this page · raw message

include/linux/mdev.h +5 -0
index 9b69b0bbebfd..3e1e03926355 100644
@@ -117,6 +117,10 @@ void mdev_set_virtio_ops(struct mdev_device *mdev,
 			 const struct virtio_mdev_device_ops *virtio_ops);
 const struct virtio_mdev_device_ops *
 mdev_get_virtio_ops(struct mdev_device *mdev);
+void mdev_set_vhost_ops(struct mdev_device *mdev,
+			const struct virtio_mdev_device_ops *vhost_ops);
+const struct virtio_mdev_device_ops *
+mdev_get_vhost_ops(struct mdev_device *mdev);
 
 extern struct bus_type mdev_bus_type;
 
@@ -133,6 +137,7 @@ struct mdev_device *mdev_from_dev(struct device *dev);
 enum {
 	MDEV_CLASS_ID_VFIO = 1,
 	MDEV_CLASS_ID_VIRTIO = 2,
+	MDEV_CLASS_ID_VHOST = 3,
 	/* New entries must be added here */
 };
 
include/uapi/linux/vhost.h +18 -0
index 40d028eed645..061a2824a1b3 100644
@@ -116,4 +116,22 @@
 #define VHOST_VSOCK_SET_GUEST_CID	_IOW(VHOST_VIRTIO, 0x60, __u64)
 #define VHOST_VSOCK_SET_RUNNING		_IOW(VHOST_VIRTIO, 0x61, int)
 
+/* VHOST_MDEV specific defines */
+
+/* Get the device id. The device ids follow the same definition of
+ * the device id defined in virtio-spec. */
+#define VHOST_MDEV_GET_DEVICE_ID	_IOR(VHOST_VIRTIO, 0x70, __u32)
+/* Get and set the status. The status bits follow the same definition
+ * of the device status defined in virtio-spec. */
+#define VHOST_MDEV_GET_STATUS		_IOR(VHOST_VIRTIO, 0x71, __u8)
+#define VHOST_MDEV_SET_STATUS		_IOW(VHOST_VIRTIO, 0x72, __u8)
+/* Get and set the device config. The device config follows the same
+ * definition of the device config defined in virtio-spec. */
+#define VHOST_MDEV_GET_CONFIG		_IOR(VHOST_VIRTIO, 0x73, struct vhost_mdev_config)
+#define VHOST_MDEV_SET_CONFIG		_IOW(VHOST_VIRTIO, 0x74, struct vhost_mdev_config)
+/* Enable/disable the ring. */
+#define VHOST_MDEV_SET_VRING_ENABLE	_IOW(VHOST_VIRTIO, 0x75, struct vhost_vring_state)
+/* Get the max ring size. */
+#define VHOST_MDEV_GET_VRING_NUM	_IOR(VHOST_VIRTIO, 0x76, __u16)
+
 #endif
include/uapi/linux/vhost_types.h +8 -0
index c907290ff065..7b105d0b2fb9 100644
@@ -119,6 +119,14 @@ struct vhost_scsi_target {
 	unsigned short reserved;
 };
 
+/* VHOST_MDEV specific definitions */
+
+struct vhost_mdev_config {
+	__u32 off;
+	__u32 len;
+	__u8 buf[0];
+};
+
 /* Feature bits */
 /* Log all write descriptors. Can be changed while device is active. */
 #define VHOST_F_LOG_ALL 26
Signature · 1 line
2.23.0
#1 lore raw
Quoted 110 lines · Tiwei Bie

This patch introduces a mdev based hardware vhost backend. This backend is built on top of the same abstraction used in virtio-mdev and provides a generic vhost interface for userspace to accelerate the virtio devices in guest.

This backend is implemented as a mdev device driver on top of the same mdev device ops used in virtio-mdev but using a different mdev class id, and it will register the device as a VFIO device for userspace to use. Userspace can setup the IOMMU with the existing VFIO container/group APIs and then get the device fd with the device name. After getting the device fd of this device, userspace can use vhost ioctls to setup the backend.

---
This patch depends on below series:
https://lkml.org/lkml/2019/10/30/62

v3 -> v4:
- Rebase on top of virtio-mdev series v6;
- Some minor tweaks and improvements;

v2 -> v3:
- Fix the return value (Jason);
- Don't cache unnecessary information in vhost-mdev (Jason);
- Get rid of the memset in open (Jason);
- Add comments for VHOST_SET_MEM_TABLE, ... (Jason);
- Filter out unsupported features in vhost-mdev (Jason);
- Add _GET_DEVICE_ID ioctl (Jason);
- Add _GET_CONFIG/_SET_CONFIG ioctls (Jason);
- Drop _GET_QUEUE_NUM ioctl (Jason);
- Fix the copy-paste errors in _IOW/_IOR usage;
- Some minor fixes and improvements;

v1 -> v2:
- Replace _SET_STATE with _SET_STATUS (MST);
- Check status bits at each step (MST);
- Report the max ring size and max number of queues (MST);
- Add missing MODULE_DEVICE_TABLE (Jason);
- Only support the network backend w/o multiqueue for now;
- Some minor fixes and improvements;
- Rebase on top of virtio-mdev series v4;

RFC v4 -> v1:
- Implement vhost-mdev as a mdev device driver directly and connect it to VFIO container/group. (Jason);
- Pass ring addresses as GPAs/IOVAs in vhost-mdev to avoid meaningless HVA->GPA translations (Jason);

RFC v3 -> RFC v4:
- Build vhost-mdev on top of the same abstraction used by virtio-mdev (Jason);
- Introduce vhost fd and pass VFIO fd via SET_BACKEND ioctl (MST);

RFC v2 -> RFC v3:
- Reuse vhost's ioctls instead of inventing a VFIO regions/irqs based vhost protocol on top of vfio-mdev (Jason);

RFC v1 -> RFC v2:
- Introduce a new VFIO device type to build a vhost protocol on top of vfio-mdev;

  drivers/vfio/mdev/mdev_core.c    |  20 ++
  drivers/vfio/mdev/mdev_private.h |   1 +
  drivers/vhost/Kconfig            |  12 +
  drivers/vhost/Makefile           |   3 +
  drivers/vhost/mdev.c             | 556 +++++++++++++++++++++++++++++++
  include/linux/mdev.h             |   5 +
  include/uapi/linux/vhost.h       |  18 +
  include/uapi/linux/vhost_types.h |   8 +
  8 files changed, 623 insertions(+)

create mode 100644 drivers/vhost/mdev.c

drivers/vfio/mdev/mdev_core.c +20 -0
index 22ca589750d8..109dbac01a8f 100644
@@ -96,6 +96,26 @@ mdev_get_virtio_ops(struct mdev_device *mdev)
  }
  EXPORT_SYMBOL(mdev_get_virtio_ops);
 
+/* Specify the vhost device ops for the mdev device, this
+ * must be called during create() callback for vhost mdev device.
+ */
+void mdev_set_vhost_ops(struct mdev_device *mdev,
+			const struct virtio_mdev_device_ops *vhost_ops)
+{
+	mdev_set_class(mdev, MDEV_CLASS_ID_VHOST);
+	mdev->vhost_ops = vhost_ops;
+}
+EXPORT_SYMBOL(mdev_set_vhost_ops);
+
+/* Get the vhost device ops for the mdev device. */
+const struct virtio_mdev_device_ops *
+mdev_get_vhost_ops(struct mdev_device *mdev)
+{
+	WARN_ON(mdev->class_id != MDEV_CLASS_ID_VHOST);
+	return mdev->vhost_ops;
+}
+EXPORT_SYMBOL(mdev_get_vhost_ops);
+
  struct device *mdev_dev(struct mdev_device *mdev)
  {
  	return &mdev->dev;
drivers/vfio/mdev/mdev_private.h +0 -0
index 7b47890c34e7..5597c846e52f 100644

drivers/vfio/mdev/mdev_private.h struct mdev_device { · line 43

Quoted 4 lines
drivers/vfio/mdev/mdev_private.h +1 -0
@@ -40,6 +40,7 @@ struct mdev_device {
  	union {
  		const struct vfio_mdev_device_ops *vfio_ops;
  		const struct virtio_mdev_device_ops *virtio_ops;
+		const struct virtio_mdev_device_ops *vhost_ops;

Any reason why virtio_ops is not used for vhost here?

Other looks good.

Thanks

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#2 lore raw
In reply to Tiwei Bie
Quoted 110 lines · Tiwei Bie

This patch introduces a mdev based hardware vhost backend. This backend is built on top of the same abstraction used in virtio-mdev and provides a generic vhost interface for userspace to accelerate the virtio devices in guest.

This backend is implemented as a mdev device driver on top of the same mdev device ops used in virtio-mdev but using a different mdev class id, and it will register the device as a VFIO device for userspace to use. Userspace can setup the IOMMU with the existing VFIO container/group APIs and then get the device fd with the device name. After getting the device fd of this device, userspace can use vhost ioctls to setup the backend.

---
This patch depends on below series:
https://lkml.org/lkml/2019/10/30/62

v3 -> v4:
- Rebase on top of virtio-mdev series v6;
- Some minor tweaks and improvements;

v2 -> v3:
- Fix the return value (Jason);
- Don't cache unnecessary information in vhost-mdev (Jason);
- Get rid of the memset in open (Jason);
- Add comments for VHOST_SET_MEM_TABLE, ... (Jason);
- Filter out unsupported features in vhost-mdev (Jason);
- Add _GET_DEVICE_ID ioctl (Jason);
- Add _GET_CONFIG/_SET_CONFIG ioctls (Jason);
- Drop _GET_QUEUE_NUM ioctl (Jason);
- Fix the copy-paste errors in _IOW/_IOR usage;
- Some minor fixes and improvements;

v1 -> v2:
- Replace _SET_STATE with _SET_STATUS (MST);
- Check status bits at each step (MST);
- Report the max ring size and max number of queues (MST);
- Add missing MODULE_DEVICE_TABLE (Jason);
- Only support the network backend w/o multiqueue for now;
- Some minor fixes and improvements;
- Rebase on top of virtio-mdev series v4;

RFC v4 -> v1:
- Implement vhost-mdev as a mdev device driver directly and connect it to VFIO container/group. (Jason);
- Pass ring addresses as GPAs/IOVAs in vhost-mdev to avoid meaningless HVA->GPA translations (Jason);

RFC v3 -> RFC v4:
- Build vhost-mdev on top of the same abstraction used by virtio-mdev (Jason);
- Introduce vhost fd and pass VFIO fd via SET_BACKEND ioctl (MST);

RFC v2 -> RFC v3:
- Reuse vhost's ioctls instead of inventing a VFIO regions/irqs based vhost protocol on top of vfio-mdev (Jason);

RFC v1 -> RFC v2:
- Introduce a new VFIO device type to build a vhost protocol on top of vfio-mdev;

  drivers/vfio/mdev/mdev_core.c    |  20 ++
  drivers/vfio/mdev/mdev_private.h |   1 +
  drivers/vhost/Kconfig            |  12 +
  drivers/vhost/Makefile           |   3 +
  drivers/vhost/mdev.c             | 556 +++++++++++++++++++++++++++++++
  include/linux/mdev.h             |   5 +
  include/uapi/linux/vhost.h       |  18 +
  include/uapi/linux/vhost_types.h |   8 +
  8 files changed, 623 insertions(+)

create mode 100644 drivers/vhost/mdev.c

drivers/vfio/mdev/mdev_core.c +20 -0
index 22ca589750d8..109dbac01a8f 100644
@@ -96,6 +96,26 @@ mdev_get_virtio_ops(struct mdev_device *mdev)
  }
  EXPORT_SYMBOL(mdev_get_virtio_ops);
 
+/* Specify the vhost device ops for the mdev device, this
+ * must be called during create() callback for vhost mdev device.
+ */
+void mdev_set_vhost_ops(struct mdev_device *mdev,
+			const struct virtio_mdev_device_ops *vhost_ops)
+{
+	mdev_set_class(mdev, MDEV_CLASS_ID_VHOST);
+	mdev->vhost_ops = vhost_ops;
+}
+EXPORT_SYMBOL(mdev_set_vhost_ops);
+
+/* Get the vhost device ops for the mdev device. */
+const struct virtio_mdev_device_ops *
+mdev_get_vhost_ops(struct mdev_device *mdev)
+{
+	WARN_ON(mdev->class_id != MDEV_CLASS_ID_VHOST);
+	return mdev->vhost_ops;
+}
+EXPORT_SYMBOL(mdev_get_vhost_ops);
+
  struct device *mdev_dev(struct mdev_device *mdev)
  {
  	return &mdev->dev;
drivers/vfio/mdev/mdev_private.h +0 -0
index 7b47890c34e7..5597c846e52f 100644

drivers/vfio/mdev/mdev_private.h struct mdev_device { · line 43

Quoted 4 lines
drivers/vfio/mdev/mdev_private.h +1 -0
@@ -40,6 +40,7 @@ struct mdev_device {
  	union {
  		const struct vfio_mdev_device_ops *vfio_ops;
  		const struct virtio_mdev_device_ops *virtio_ops;
+		const struct virtio_mdev_device_ops *vhost_ops;

Any reason why virtio_ops is not used for vhost here?

Other looks good.

Thanks

author
#3 lore raw
Quoted 109 lines · Jason Wang
Quoted 108 lines · Tiwei Bie

This patch introduces a mdev based hardware vhost backend. This backend is built on top of the same abstraction used in virtio-mdev and provides a generic vhost interface for userspace to accelerate the virtio devices in guest.

This backend is implemented as a mdev device driver on top of the same mdev device ops used in virtio-mdev but using a different mdev class id, and it will register the device as a VFIO device for userspace to use. Userspace can setup the IOMMU with the existing VFIO container/group APIs and then get the device fd with the device name. After getting the device fd of this device, userspace can use vhost ioctls to setup the backend.

---
This patch depends on below series:
https://lkml.org/lkml/2019/10/30/62

v3 -> v4:
- Rebase on top of virtio-mdev series v6;
- Some minor tweaks and improvements;

v2 -> v3:
- Fix the return value (Jason);
- Don't cache unnecessary information in vhost-mdev (Jason);
- Get rid of the memset in open (Jason);
- Add comments for VHOST_SET_MEM_TABLE, ... (Jason);
- Filter out unsupported features in vhost-mdev (Jason);
- Add _GET_DEVICE_ID ioctl (Jason);
- Add _GET_CONFIG/_SET_CONFIG ioctls (Jason);
- Drop _GET_QUEUE_NUM ioctl (Jason);
- Fix the copy-paste errors in _IOW/_IOR usage;
- Some minor fixes and improvements;

v1 -> v2:
- Replace _SET_STATE with _SET_STATUS (MST);
- Check status bits at each step (MST);
- Report the max ring size and max number of queues (MST);
- Add missing MODULE_DEVICE_TABLE (Jason);
- Only support the network backend w/o multiqueue for now;
- Some minor fixes and improvements;
- Rebase on top of virtio-mdev series v4;

RFC v4 -> v1:
- Implement vhost-mdev as a mdev device driver directly and connect it to VFIO container/group. (Jason);
- Pass ring addresses as GPAs/IOVAs in vhost-mdev to avoid meaningless HVA->GPA translations (Jason);

RFC v3 -> RFC v4:
- Build vhost-mdev on top of the same abstraction used by virtio-mdev (Jason);
- Introduce vhost fd and pass VFIO fd via SET_BACKEND ioctl (MST);

RFC v2 -> RFC v3:
- Reuse vhost's ioctls instead of inventing a VFIO regions/irqs based vhost protocol on top of vfio-mdev (Jason);

RFC v1 -> RFC v2:
- Introduce a new VFIO device type to build a vhost protocol on top of vfio-mdev;

  drivers/vfio/mdev/mdev_core.c    |  20 ++
  drivers/vfio/mdev/mdev_private.h |   1 +
  drivers/vhost/Kconfig            |  12 +
  drivers/vhost/Makefile           |   3 +
  drivers/vhost/mdev.c             | 556 +++++++++++++++++++++++++++++++
  include/linux/mdev.h             |   5 +
  include/uapi/linux/vhost.h       |  18 +
  include/uapi/linux/vhost_types.h |   8 +
  8 files changed, 623 insertions(+)

create mode 100644 drivers/vhost/mdev.c

drivers/vfio/mdev/mdev_core.c +20 -0
index 22ca589750d8..109dbac01a8f 100644
@@ -96,6 +96,26 @@ mdev_get_virtio_ops(struct mdev_device *mdev)
  }
  EXPORT_SYMBOL(mdev_get_virtio_ops);
+/* Specify the vhost device ops for the mdev device, this
+ * must be called during create() callback for vhost mdev device.
+ */
+void mdev_set_vhost_ops(struct mdev_device *mdev,
+			const struct virtio_mdev_device_ops *vhost_ops)
+{
+	mdev_set_class(mdev, MDEV_CLASS_ID_VHOST);
+	mdev->vhost_ops = vhost_ops;
+}
+EXPORT_SYMBOL(mdev_set_vhost_ops);
+
+/* Get the vhost device ops for the mdev device. */
+const struct virtio_mdev_device_ops *
+mdev_get_vhost_ops(struct mdev_device *mdev)
+{
+	WARN_ON(mdev->class_id != MDEV_CLASS_ID_VHOST);
+	return mdev->vhost_ops;
+}
+EXPORT_SYMBOL(mdev_get_vhost_ops);
+
  struct device *mdev_dev(struct mdev_device *mdev)
  {
  	return &mdev->dev;
drivers/vfio/mdev/mdev_private.h +0 -0
index 7b47890c34e7..5597c846e52f 100644
Quoted 8 lines

drivers/vfio/mdev/mdev_private.h struct mdev_device { · line 43

Quoted 5 lines
drivers/vfio/mdev/mdev_private.h +1 -0
@@ -40,6 +40,7 @@ struct mdev_device {
  	union {
  		const struct vfio_mdev_device_ops *vfio_ops;
  		const struct virtio_mdev_device_ops *virtio_ops;
+		const struct virtio_mdev_device_ops *vhost_ops;

Any reason why virtio_ops is not used for vhost here?

I don't have a strong opinion on this.
Will use virtio_ops directly.

Quoted 2 lines

Other looks good.

Thanks!

Quoted 4 lines

Thanks

author
#4 lore raw
In reply to Jason Wang
Quoted 109 lines · Jason Wang
Quoted 108 lines · Tiwei Bie

This patch introduces a mdev based hardware vhost backend. This backend is built on top of the same abstraction used in virtio-mdev and provides a generic vhost interface for userspace to accelerate the virtio devices in guest.

This backend is implemented as a mdev device driver on top of the same mdev device ops used in virtio-mdev but using a different mdev class id, and it will register the device as a VFIO device for userspace to use. Userspace can setup the IOMMU with the existing VFIO container/group APIs and then get the device fd with the device name. After getting the device fd of this device, userspace can use vhost ioctls to setup the backend.

---
This patch depends on below series:
https://lkml.org/lkml/2019/10/30/62

v3 -> v4:
- Rebase on top of virtio-mdev series v6;
- Some minor tweaks and improvements;

v2 -> v3:
- Fix the return value (Jason);
- Don't cache unnecessary information in vhost-mdev (Jason);
- Get rid of the memset in open (Jason);
- Add comments for VHOST_SET_MEM_TABLE, ... (Jason);
- Filter out unsupported features in vhost-mdev (Jason);
- Add _GET_DEVICE_ID ioctl (Jason);
- Add _GET_CONFIG/_SET_CONFIG ioctls (Jason);
- Drop _GET_QUEUE_NUM ioctl (Jason);
- Fix the copy-paste errors in _IOW/_IOR usage;
- Some minor fixes and improvements;

v1 -> v2:
- Replace _SET_STATE with _SET_STATUS (MST);
- Check status bits at each step (MST);
- Report the max ring size and max number of queues (MST);
- Add missing MODULE_DEVICE_TABLE (Jason);
- Only support the network backend w/o multiqueue for now;
- Some minor fixes and improvements;
- Rebase on top of virtio-mdev series v4;

RFC v4 -> v1:
- Implement vhost-mdev as a mdev device driver directly and connect it to VFIO container/group. (Jason);
- Pass ring addresses as GPAs/IOVAs in vhost-mdev to avoid meaningless HVA->GPA translations (Jason);

RFC v3 -> RFC v4:
- Build vhost-mdev on top of the same abstraction used by virtio-mdev (Jason);
- Introduce vhost fd and pass VFIO fd via SET_BACKEND ioctl (MST);

RFC v2 -> RFC v3:
- Reuse vhost's ioctls instead of inventing a VFIO regions/irqs based vhost protocol on top of vfio-mdev (Jason);

RFC v1 -> RFC v2:
- Introduce a new VFIO device type to build a vhost protocol on top of vfio-mdev;

  drivers/vfio/mdev/mdev_core.c    |  20 ++
  drivers/vfio/mdev/mdev_private.h |   1 +
  drivers/vhost/Kconfig            |  12 +
  drivers/vhost/Makefile           |   3 +
  drivers/vhost/mdev.c             | 556 +++++++++++++++++++++++++++++++
  include/linux/mdev.h             |   5 +
  include/uapi/linux/vhost.h       |  18 +
  include/uapi/linux/vhost_types.h |   8 +
  8 files changed, 623 insertions(+)

create mode 100644 drivers/vhost/mdev.c

drivers/vfio/mdev/mdev_core.c +20 -0
index 22ca589750d8..109dbac01a8f 100644
@@ -96,6 +96,26 @@ mdev_get_virtio_ops(struct mdev_device *mdev)
  }
  EXPORT_SYMBOL(mdev_get_virtio_ops);
+/* Specify the vhost device ops for the mdev device, this
+ * must be called during create() callback for vhost mdev device.
+ */
+void mdev_set_vhost_ops(struct mdev_device *mdev,
+			const struct virtio_mdev_device_ops *vhost_ops)
+{
+	mdev_set_class(mdev, MDEV_CLASS_ID_VHOST);
+	mdev->vhost_ops = vhost_ops;
+}
+EXPORT_SYMBOL(mdev_set_vhost_ops);
+
+/* Get the vhost device ops for the mdev device. */
+const struct virtio_mdev_device_ops *
+mdev_get_vhost_ops(struct mdev_device *mdev)
+{
+	WARN_ON(mdev->class_id != MDEV_CLASS_ID_VHOST);
+	return mdev->vhost_ops;
+}
+EXPORT_SYMBOL(mdev_get_vhost_ops);
+
  struct device *mdev_dev(struct mdev_device *mdev)
  {
  	return &mdev->dev;
drivers/vfio/mdev/mdev_private.h +0 -0
index 7b47890c34e7..5597c846e52f 100644
Quoted 8 lines

drivers/vfio/mdev/mdev_private.h struct mdev_device { · line 43

Quoted 5 lines
drivers/vfio/mdev/mdev_private.h +1 -0
@@ -40,6 +40,7 @@ struct mdev_device {
  	union {
  		const struct vfio_mdev_device_ops *vfio_ops;
  		const struct virtio_mdev_device_ops *virtio_ops;
+		const struct virtio_mdev_device_ops *vhost_ops;

Any reason why virtio_ops is not used for vhost here?

I don't have a strong opinion on this.
Will use virtio_ops directly.

Quoted 2 lines

Other looks good.

Thanks!

Quoted 4 lines

Thanks

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization