From fb12bbecd239b09450f52e72a602dea0f3832861 Mon Sep 17 00:00:00 2001 From: Dariusz Stojaczyk Date: Mon, 15 Jan 2018 13:06:58 +0100 Subject: [PATCH] virtio: move vdev->name allocation to generic virtio Previously vdev->name was being allocated/freed separately in virtio_pci and virtio_user backends. Now it's all done in generic virtio library and cleans up some code. Change-Id: I810e976d09781c0c9b25c6f7fd957a83aad6c7b8 Signed-off-by: Dariusz Stojaczyk Reviewed-on: https://review.gerrithub.io/394704 Reviewed-by: Reviewed-by: Daniel Verkamp Tested-by: SPDK Automated Test System Reviewed-by: Jim Harris --- include/spdk_internal/virtio.h | 8 +++++--- lib/virtio/virtio.c | 18 ++++++++++++++++-- lib/virtio/virtio_pci.c | 11 +---------- lib/virtio/virtio_user.c | 8 +------- 4 files changed, 23 insertions(+), 22 deletions(-) diff --git a/include/spdk_internal/virtio.h b/include/spdk_internal/virtio.h index e1fadcbb8..399095b3f 100644 --- a/include/spdk_internal/virtio.h +++ b/include/spdk_internal/virtio.h @@ -256,11 +256,13 @@ void virtqueue_req_add_iovs(struct virtqueue *vq, struct iovec *iovs, uint16_t i * Before doing any I/O, it has to be manually started via \c virtio_dev_restart. * * \param vdev memory for virtio device, must be zeroed + * \param name name for the virtio device * \param ops backend callbacks - * \param ctx argument for the backend callbacks + * \param ops_ctx argument for the backend callbacks + * \return zero on success, or negative error code otherwise */ -int virtio_dev_construct(struct virtio_dev *vdev, const struct virtio_dev_ops *ops, - void *ctx); +int virtio_dev_construct(struct virtio_dev *vdev, const char *name, + const struct virtio_dev_ops *ops, void *ops_ctx); /** * Reset the device and prepare it to be `virtio_dev_start`ed. This call diff --git a/lib/virtio/virtio.c b/lib/virtio/virtio.c index a4f0fc6d1..813264764 100644 --- a/lib/virtio/virtio.c +++ b/lib/virtio/virtio.c @@ -325,9 +325,22 @@ virtio_negotiate_features(struct virtio_dev *dev, uint64_t req_features) } int -virtio_dev_construct(struct virtio_dev *vdev, const struct virtio_dev_ops *ops, void *ctx) +virtio_dev_construct(struct virtio_dev *vdev, const char *name, + const struct virtio_dev_ops *ops, void *ctx) { - pthread_mutex_init(&vdev->mutex, NULL); + int rc; + + vdev->name = strdup(name); + if (vdev->name == NULL) { + return -ENOMEM; + } + + rc = pthread_mutex_init(&vdev->mutex, NULL); + if (rc != 0) { + free(vdev->name); + return -rc; + } + vdev->backend_ops = ops; vdev->ctx = ctx; @@ -370,6 +383,7 @@ virtio_dev_destruct(struct virtio_dev *dev) { virtio_dev_backend_ops(dev)->destruct_dev(dev); pthread_mutex_destroy(&dev->mutex); + free(dev->name); } static void diff --git a/lib/virtio/virtio_pci.c b/lib/virtio/virtio_pci.c index 3e8ec9295..bf8bcdbcb 100644 --- a/lib/virtio/virtio_pci.c +++ b/lib/virtio/virtio_pci.c @@ -210,7 +210,6 @@ modern_destruct_dev(struct virtio_dev *vdev) struct spdk_pci_device *pci_dev = hw->pci_dev; free_virtio_hw(hw); - free(vdev->name); spdk_pci_device_detach(pci_dev); } @@ -533,20 +532,12 @@ virtio_pci_dev_init(struct virtio_dev *vdev, const char *name, struct virtio_pci_ctx *pci_ctx) { int rc; - char *name_dup; - name_dup = strdup(name); - if (name_dup == NULL) { - return -1; - } - - rc = virtio_dev_construct(vdev, &modern_ops, pci_ctx); + rc = virtio_dev_construct(vdev, name, &modern_ops, pci_ctx); if (rc != 0) { - free(name_dup); return -1; } - vdev->name = name_dup; vdev->is_hw = 1; vdev->modern = 1; diff --git a/lib/virtio/virtio_user.c b/lib/virtio/virtio_user.c index 0d7adf774..415ecfc63 100644 --- a/lib/virtio/virtio_user.c +++ b/lib/virtio/virtio_user.c @@ -362,7 +362,6 @@ virtio_user_destroy(struct virtio_dev *vdev) struct virtio_user_dev *dev = vdev->ctx; close(dev->vhostfd); - free(vdev->name); free(dev); } @@ -410,7 +409,7 @@ virtio_user_dev_init(struct virtio_dev *vdev, const char *name, const char *path return -1; } - rc = virtio_dev_construct(vdev, &virtio_user_ops, dev); + rc = virtio_dev_construct(vdev, name, &virtio_user_ops, dev); if (rc != 0) { SPDK_ERRLOG("Failed to init device: %s\n", path); free(dev); @@ -418,11 +417,6 @@ virtio_user_dev_init(struct virtio_dev *vdev, const char *name, const char *path } vdev->is_hw = 0; - vdev->name = strdup(name); - if (!vdev->name) { - SPDK_ERRLOG("Failed to reserve memory for controller name: %s\n", path); - goto err; - } snprintf(dev->path, PATH_MAX, "%s", path); dev->queue_size = queue_size;