Skip to contentSkip to conversation

vhost: introduce mdev based hardware backend

PATCH New Tiwei Bie 1 participant 1 message Nov 19, 2019

Subsystems (5)
lore
Share & tools
lore

Tiwei Bie

author
#0 lore raw

PATCH v7 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 ops used in virtio-mdev but using a different class id. 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. With the device fd, userspace can use vhost ioctls to setup the backend.

---
This patch depends on below series:
https://lkml.org/lkml/2019/11/18/261

v6 -> v7:
- Rebase on top of virtio-mdev series v13;

v5 -> v6:
- Filter out VHOST_SET_LOG_BASE/VHOST_SET_LOG_FD (Jason);
- Simplify len/off check (Jason);
- Address checkpatch warnings, some of them are ignored to keep the coding style consistent with existing ones;

v4 -> v5:
- Rebase on top of virtio-mdev series v8;
- Use the virtio_ops of mdev_device in vhost-mdev (Jason);
- Some minor improvements on commit log;

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/vfio_iommu_type1.c  |   9 +
 drivers/vhost/Kconfig            |  12 +
 drivers/vhost/Makefile           |   3 +
 drivers/vhost/mdev.c             | 561 +++++++++++++++++++++++++++++++
 include/linux/mdev_virtio.h      |   1 +
 include/uapi/linux/vhost.h       |  21 ++
 include/uapi/linux/vhost_types.h |   8 +
 7 files changed, 615 insertions(+)
 create mode 100644 drivers/vhost/mdev.c
drivers/vfio/vfio_iommu_type1.c +9 -0
index f35523f822eb..f27b92b8fdd4 100644
@@ -35,6 +35,7 @@
 #include <linux/vfio.h>
 #include <linux/workqueue.h>
 #include <linux/mdev_vfio.h>
+#include <linux/mdev_virtio.h>
 #include <linux/notifier.h>
 #include <linux/dma-iommu.h>
 #include <linux/irqdomain.h>
@@ -1411,6 +1412,14 @@ static bool vfio_bus_is_mdev(struct bus_type *bus)
 		symbol_put(mdev_vfio_bus_type);
 	}
 
+	if (!ret) {
+		mdev_bus = symbol_get(mdev_virtio_bus_type);
+		if (mdev_bus) {
+			ret = (bus == mdev_bus);
+			symbol_put(mdev_virtio_bus_type);
+		}
+	}
+
 	return ret;
 }
 
drivers/vhost/Kconfig +12 -0
index 3d03ccbd1adc..a4cf67a3aaa4 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 && MDEV_VIRTIO && VFIO
+	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 +561 -0

561 lines not on this page · raw message

include/linux/mdev_virtio.h +1 -0
index 5f75f3cf59e1..6669540fff62 100644
@@ -27,6 +27,7 @@ struct virtio_mdev_callback {
 
 enum {
 	MDEV_VIRTIO_CLASS_ID_VIRTIO = 1,
+	MDEV_VIRTIO_CLASS_ID_VHOST = 2,
 	/* New entries must be added here */
 };
 
include/uapi/linux/vhost.h +21 -0
index 40d028eed645..a7e862245bb4 100644
@@ -116,4 +116,25 @@
 #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