diff --git a/lib/virtio/virtio.c b/lib/virtio/virtio.c index 1a9709a901..1e2991a1a3 100644 --- a/lib/virtio/virtio.c +++ b/lib/virtio/virtio.c @@ -165,6 +165,8 @@ virtio_init_queue(struct virtio_dev *dev, uint16_t vtpci_queue_idx) if (virtio_dev_backend_ops(dev)->setup_queue(dev, vq) < 0) { SPDK_ERRLOG("setup_queue failed\n"); + spdk_dma_free(vq); + dev->vqs[vtpci_queue_idx] = NULL; return -EINVAL; } diff --git a/lib/virtio/virtio_pci.c b/lib/virtio/virtio_pci.c index 24f9b35725..2dc1e6f6b7 100644 --- a/lib/virtio/virtio_pci.c +++ b/lib/virtio/virtio_pci.c @@ -281,6 +281,7 @@ modern_setup_queue(struct virtio_dev *dev, struct virtqueue *vq) vq->vq_ring_virt_mem = queue_mem; if (!check_vq_phys_addr_ok(vq)) { + spdk_dma_free(queue_mem); return -1; } diff --git a/lib/virtio/virtio_user.c b/lib/virtio/virtio_user.c index 6e30bcef06..a40b9f7919 100644 --- a/lib/virtio/virtio_user.c +++ b/lib/virtio/virtio_user.c @@ -366,14 +366,6 @@ virtio_user_setup_queue(struct virtio_dev *vdev, struct virtqueue *vq) return -1; } - queue_mem = spdk_dma_zmalloc(vq->vq_ring_size, VIRTIO_PCI_VRING_ALIGN, NULL); - if (queue_mem == NULL) { - return -ENOMEM; - } - - vq->vq_ring_mem = SPDK_VTOPHYS_ERROR; - vq->vq_ring_virt_mem = queue_mem; - /* May use invalid flag, but some backend uses kickfd and * callfd as criteria to judge if dev is alive. so finally we * use real event_fd. @@ -391,6 +383,16 @@ virtio_user_setup_queue(struct virtio_dev *vdev, struct virtqueue *vq) return -1; } + queue_mem = spdk_dma_zmalloc(vq->vq_ring_size, VIRTIO_PCI_VRING_ALIGN, NULL); + if (queue_mem == NULL) { + close(kickfd); + close(callfd); + return -ENOMEM; + } + + vq->vq_ring_mem = SPDK_VTOPHYS_ERROR; + vq->vq_ring_virt_mem = queue_mem; + state.index = vq->vq_queue_index; state.num = 0; @@ -398,6 +400,7 @@ virtio_user_setup_queue(struct virtio_dev *vdev, struct virtqueue *vq) dev->ops->send_request(dev, VHOST_USER_SET_VRING_ENABLE, &state) < 0) { SPDK_ERRLOG("failed to send VHOST_USER_SET_VRING_ENABLE: %s\n", spdk_strerror(errno)); + spdk_dma_free(queue_mem); return -1; }