diff --git a/lib/virtio/virtio_pci.c b/lib/virtio/virtio_pci.c index 646f77c1a..1954043da 100644 --- a/lib/virtio/virtio_pci.c +++ b/lib/virtio/virtio_pci.c @@ -224,10 +224,11 @@ static void modern_destruct_dev(struct virtio_dev *vdev) { struct virtio_hw *hw = vdev->ctx; - struct spdk_pci_device *pci_dev = hw->pci_dev; - free_virtio_hw(hw); - spdk_pci_device_detach(pci_dev); + if (hw != NULL) { + free_virtio_hw(hw); + spdk_pci_device_detach(hw->pci_dev); + } } static uint8_t diff --git a/module/bdev/virtio/bdev_virtio_blk.c b/module/bdev/virtio/bdev_virtio_blk.c index 99653e238..8730f39db 100644 --- a/module/bdev/virtio/bdev_virtio_blk.c +++ b/module/bdev/virtio/bdev_virtio_blk.c @@ -554,9 +554,7 @@ virtio_pci_blk_dev_create(const char *name, struct virtio_pci_ctx *pci_ctx) rc = virtio_dev_reset(vdev, VIRTIO_BLK_DEV_SUPPORTED_FEATURES); if (rc != 0) { - virtio_dev_destruct(vdev); - free(bvdev); - return NULL; + goto fail; } /* TODO: add a way to limit usable virtqueues */ @@ -565,9 +563,7 @@ virtio_pci_blk_dev_create(const char *name, struct virtio_pci_ctx *pci_ctx) &num_queues, sizeof(num_queues)); if (rc) { SPDK_ERRLOG("%s: config read failed: %s\n", vdev->name, spdk_strerror(-rc)); - virtio_dev_destruct(vdev); - free(bvdev); - return NULL; + goto fail; } } else { num_queues = 1; @@ -575,12 +571,16 @@ virtio_pci_blk_dev_create(const char *name, struct virtio_pci_ctx *pci_ctx) rc = virtio_blk_dev_init(bvdev, num_queues); if (rc != 0) { - virtio_dev_destruct(vdev); - free(bvdev); - return NULL; + goto fail; } return bvdev; + +fail: + vdev->ctx = NULL; + virtio_dev_destruct(vdev); + free(bvdev); + return NULL; } static struct virtio_blk_dev *