[PATCH V4 4/5] virtio: introduce a vDPA based transport

2020-02-19 Thread Jason Wang
This patch introduces a vDPA transport for virtio. This is used to
use kernel virtio driver to drive the vDPA device that is capable
of populating virtqueue directly.

A new virtio-vdpa driver will be registered to the vDPA bus, when a
new virtio-vdpa device is probed, it will register the device with
vdpa based config ops. This means it is a software transport between
vDPA driver and vDPA device. The transport was implemented through
bus_ops of vDPA parent.

Signed-off-by: Jason Wang 
---
 drivers/virtio/Kconfig   |  13 ++
 drivers/virtio/Makefile  |   1 +
 drivers/virtio/virtio_vdpa.c | 392 +++
 3 files changed, 406 insertions(+)
 create mode 100644 drivers/virtio/virtio_vdpa.c

diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig
index 9c4fdb64d9ac..99e424570644 100644
--- a/drivers/virtio/Kconfig
+++ b/drivers/virtio/Kconfig
@@ -43,6 +43,19 @@ config VIRTIO_PCI_LEGACY
 
  If unsure, say Y.
 
+config VIRTIO_VDPA
+   tristate "vDPA driver for virtio devices"
+   select VDPA
+   select VIRTIO
+   help
+ This driver provides support for virtio based paravirtual
+ device driver over vDPA bus. For this to be useful, you need
+ an appropriate vDPA device implementation that operates on a
+ physical device to allow the datapath of virtio to be
+ offloaded to hardware.
+
+ If unsure, say M.
+
 config VIRTIO_PMEM
tristate "Support for virtio pmem driver"
depends on VIRTIO
diff --git a/drivers/virtio/Makefile b/drivers/virtio/Makefile
index fdf5eacd0d0a..3407ac03fe60 100644
--- a/drivers/virtio/Makefile
+++ b/drivers/virtio/Makefile
@@ -6,4 +6,5 @@ virtio_pci-y := virtio_pci_modern.o virtio_pci_common.o
 virtio_pci-$(CONFIG_VIRTIO_PCI_LEGACY) += virtio_pci_legacy.o
 obj-$(CONFIG_VIRTIO_BALLOON) += virtio_balloon.o
 obj-$(CONFIG_VIRTIO_INPUT) += virtio_input.o
+obj-$(CONFIG_VIRTIO_VDPA) += virtio_vdpa.o
 obj-$(CONFIG_VDPA) += vdpa/
diff --git a/drivers/virtio/virtio_vdpa.c b/drivers/virtio/virtio_vdpa.c
new file mode 100644
index ..077796087abf
--- /dev/null
+++ b/drivers/virtio/virtio_vdpa.c
@@ -0,0 +1,392 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * VIRTIO based driver for vDPA device
+ *
+ * Copyright (c) 2020, Red Hat. All rights reserved.
+ * Author: Jason Wang 
+ *
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define MOD_VERSION  "0.1"
+#define MOD_AUTHOR   "Jason Wang "
+#define MOD_DESC "vDPA bus driver for virtio devices"
+#define MOD_LICENSE  "GPL v2"
+
+struct virtio_vdpa_device {
+   struct virtio_device vdev;
+   struct vdpa_device *vdpa;
+   u64 features;
+
+   /* The lock to protect virtqueue list */
+   spinlock_t lock;
+   /* List of virtio_vdpa_vq_info */
+   struct list_head virtqueues;
+};
+
+struct virtio_vdpa_vq_info {
+   /* the actual virtqueue */
+   struct virtqueue *vq;
+
+   /* the list node for the virtqueues list */
+   struct list_head node;
+};
+
+static inline struct virtio_vdpa_device *
+to_virtio_vdpa_device(struct virtio_device *dev)
+{
+   return container_of(dev, struct virtio_vdpa_device, vdev);
+}
+
+static struct vdpa_device *vd_get_vdpa(struct virtio_device *vdev)
+{
+   return to_virtio_vdpa_device(vdev)->vdpa;
+}
+
+static void virtio_vdpa_get(struct virtio_device *vdev, unsigned offset,
+   void *buf, unsigned len)
+{
+   struct vdpa_device *vdpa = vd_get_vdpa(vdev);
+   const struct vdpa_config_ops *ops = vdpa->config;
+
+   ops->get_config(vdpa, offset, buf, len);
+}
+
+static void virtio_vdpa_set(struct virtio_device *vdev, unsigned offset,
+   const void *buf, unsigned len)
+{
+   struct vdpa_device *vdpa = vd_get_vdpa(vdev);
+   const struct vdpa_config_ops *ops = vdpa->config;
+
+   ops->set_config(vdpa, offset, buf, len);
+}
+
+static u32 virtio_vdpa_generation(struct virtio_device *vdev)
+{
+   struct vdpa_device *vdpa = vd_get_vdpa(vdev);
+   const struct vdpa_config_ops *ops = vdpa->config;
+
+   if (ops->get_generation)
+   return ops->get_generation(vdpa);
+
+   return 0;
+}
+
+static u8 virtio_vdpa_get_status(struct virtio_device *vdev)
+{
+   struct vdpa_device *vdpa = vd_get_vdpa(vdev);
+   const struct vdpa_config_ops *ops = vdpa->config;
+
+   return ops->get_status(vdpa);
+}
+
+static void virtio_vdpa_set_status(struct virtio_device *vdev, u8 status)
+{
+   struct vdpa_device *vdpa = vd_get_vdpa(vdev);
+   const struct vdpa_config_ops *ops = vdpa->config;
+
+   return ops->set_status(vdpa, status);
+}
+
+static void virtio_vdpa_reset(struct virtio_device *vdev)
+{
+   struct vdpa_device *vdpa = vd_get_vdpa(vdev);
+   const struct vdpa_config_ops *ops = vdpa->config;
+
+   return ops->set_status(vdpa, 0);
+}
+
+static bool virtio_vdpa

Re: [PATCH V4 4/5] virtio: introduce a vDPA based transport

2020-02-20 Thread Jason Gunthorpe
On Thu, Feb 20, 2020 at 02:11:40PM +0800, Jason Wang wrote:
> +static int virtio_vdpa_probe(struct vdpa_device *vdpa)
> +{
> + const struct vdpa_config_ops *ops = vdpa->config;
> + struct virtio_vdpa_device *vd_dev;
> + int ret = -EINVAL;
> +
> + vd_dev = kzalloc(sizeof(*vd_dev), GFP_KERNEL);
> + if (!vd_dev)
> + return -ENOMEM;
> +
> + vd_dev->vdev.dev.parent = vdpa_get_dma_dev(vdpa);
> + vd_dev->vdev.dev.release = virtio_vdpa_release_dev;
> + vd_dev->vdev.config = &virtio_vdpa_config_ops;
> + vd_dev->vdpa = vdpa;
> + INIT_LIST_HEAD(&vd_dev->virtqueues);
> + spin_lock_init(&vd_dev->lock);
> +
> + vd_dev->vdev.id.device = ops->get_device_id(vdpa);
> + if (vd_dev->vdev.id.device == 0)
> + goto err;
> +
> + vd_dev->vdev.id.vendor = ops->get_vendor_id(vdpa);
> + ret = register_virtio_device(&vd_dev->vdev);
> + if (ret)
> + goto err;

This error unwind is wrong. register_virtio_device() does
device_initialize() as it's first action. After that point error
unwind must be done with put_device() - particularly calling
kfree(vd_dev) after doing dev_set_name() leaks memory.

Looks like about half of the register_virtio_device() users did this
right, the others not. Perhaps you should fix them too...

Jason
___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization


Re: [PATCH V4 4/5] virtio: introduce a vDPA based transport

2020-02-21 Thread Jason Wang


On 2020/2/20 下午11:19, Jason Gunthorpe wrote:

On Thu, Feb 20, 2020 at 02:11:40PM +0800, Jason Wang wrote:

+static int virtio_vdpa_probe(struct vdpa_device *vdpa)
+{
+   const struct vdpa_config_ops *ops = vdpa->config;
+   struct virtio_vdpa_device *vd_dev;
+   int ret = -EINVAL;
+
+   vd_dev = kzalloc(sizeof(*vd_dev), GFP_KERNEL);
+   if (!vd_dev)
+   return -ENOMEM;
+
+   vd_dev->vdev.dev.parent = vdpa_get_dma_dev(vdpa);
+   vd_dev->vdev.dev.release = virtio_vdpa_release_dev;
+   vd_dev->vdev.config = &virtio_vdpa_config_ops;
+   vd_dev->vdpa = vdpa;
+   INIT_LIST_HEAD(&vd_dev->virtqueues);
+   spin_lock_init(&vd_dev->lock);
+
+   vd_dev->vdev.id.device = ops->get_device_id(vdpa);
+   if (vd_dev->vdev.id.device == 0)
+   goto err;
+
+   vd_dev->vdev.id.vendor = ops->get_vendor_id(vdpa);
+   ret = register_virtio_device(&vd_dev->vdev);
+   if (ret)
+   goto err;

This error unwind is wrong. register_virtio_device() does
device_initialize() as it's first action. After that point error
unwind must be done with put_device() - particularly calling
kfree(vd_dev) after doing dev_set_name() leaks memory.



Exactly.




Looks like about half of the register_virtio_device() users did this
right, the others not. Perhaps you should fix them too...

Jason



Will do.

Thanks






___
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization