vhost: remove all devices on shutdown

Fixes a memory leak on shutdown.

Change-Id: I1fb922374b98771858757c536ab45202283150f4
Signed-off-by: Dariusz Stojaczyk <dariuszx.stojaczyk@intel.com>
Reviewed-on: https://review.gerrithub.io/396576
Tested-by: SPDK Automated Test System <sys_sgsw@intel.com>
Reviewed-by: <shuhei.matsumoto.xt@hitachi.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
This commit is contained in:
Dariusz Stojaczyk 2018-01-26 14:46:26 +01:00 committed by Jim Harris
parent 483567b1ae
commit 2307738334
5 changed files with 33 additions and 5 deletions

View File

@ -707,6 +707,7 @@ spdk_vhost_dev_register(struct spdk_vhost_dev *vdev, const char *name, const cha
vdev->vid = -1; vdev->vid = -1;
vdev->lcore = -1; vdev->lcore = -1;
vdev->cpumask = cpumask; vdev->cpumask = cpumask;
vdev->registered = true;
vdev->type = type; vdev->type = type;
vdev->backend = backend; vdev->backend = backend;
@ -754,7 +755,7 @@ spdk_vhost_dev_unregister(struct spdk_vhost_dev *vdev)
return -ENOSPC; return -ENOSPC;
} }
if (rte_vhost_driver_unregister(vdev->path) != 0) { if (vdev->registered && rte_vhost_driver_unregister(vdev->path) != 0) {
SPDK_ERRLOG("Could not unregister controller %s with vhost library\n" SPDK_ERRLOG("Could not unregister controller %s with vhost library\n"
"Check if domain socket %s still exists\n", "Check if domain socket %s still exists\n",
vdev->name, vdev->path); vdev->name, vdev->path);
@ -1181,6 +1182,7 @@ session_shutdown(void *arg)
} }
rte_vhost_driver_unregister(vdev->path); rte_vhost_driver_unregister(vdev->path);
vdev->registered = false;
} }
SPDK_NOTICELOG("Exiting\n"); SPDK_NOTICELOG("Exiting\n");
@ -1337,13 +1339,28 @@ spdk_vhost_init(void)
return 0; return 0;
} }
static int
_spdk_vhost_fini_remove_vdev_cb(struct spdk_vhost_dev *vdev, void *arg)
{
spdk_vhost_fini_cb fini_cb = arg;
if (vdev != NULL) {
spdk_vhost_dev_remove(vdev);
return 0;
}
/* All devices are removed now. */
free(g_num_ctrlrs);
fini_cb();
return 0;
}
static void static void
_spdk_vhost_fini(void *arg1, void *arg2) _spdk_vhost_fini(void *arg1, void *arg2)
{ {
spdk_vhost_fini_cb fini_cb = arg1; spdk_vhost_fini_cb fini_cb = arg1;
free(g_num_ctrlrs); spdk_vhost_call_external_event_foreach(_spdk_vhost_fini_remove_vdev_cb, fini_cb);
fini_cb();
} }
void void

View File

@ -146,6 +146,7 @@ struct spdk_vhost_dev {
int task_cnt; int task_cnt;
int32_t lcore; int32_t lcore;
struct spdk_cpuset *cpumask; struct spdk_cpuset *cpumask;
bool registered;
enum spdk_vhost_dev_type type; enum spdk_vhost_dev_type type;
const struct spdk_vhost_dev_backend *backend; const struct spdk_vhost_dev_backend *backend;

View File

@ -721,9 +721,17 @@ spdk_vhost_scsi_dev_remove(struct spdk_vhost_dev *vdev)
for (i = 0; i < SPDK_VHOST_SCSI_CTRLR_MAX_DEVS; ++i) { for (i = 0; i < SPDK_VHOST_SCSI_CTRLR_MAX_DEVS; ++i) {
if (svdev->scsi_dev[i]) { if (svdev->scsi_dev[i]) {
if (vdev->registered) {
SPDK_ERRLOG("Trying to remove non-empty controller: %s.\n", vdev->name); SPDK_ERRLOG("Trying to remove non-empty controller: %s.\n", vdev->name);
return -EBUSY; return -EBUSY;
} }
rc = spdk_vhost_scsi_dev_remove_tgt(vdev, i, NULL, NULL);
if (rc != 0) {
SPDK_ERRLOG("%s: failed to force-remove target %d\n", vdev->name, i);
return rc;
}
}
} }
rc = spdk_vhost_dev_unregister(vdev); rc = spdk_vhost_dev_unregister(vdev);

View File

@ -115,6 +115,7 @@ spdk_vhost_dev_register(struct spdk_vhost_dev *vdev, const char *name, const cha
} }
g_spdk_vhost_device = vdev; g_spdk_vhost_device = vdev;
vdev->registered = true;
return 0; return 0;
} }

View File

@ -104,6 +104,7 @@ alloc_svdev(void)
SPDK_CACHE_LINE_SIZE, NULL); SPDK_CACHE_LINE_SIZE, NULL);
SPDK_CU_ASSERT_FATAL(svdev != NULL); SPDK_CU_ASSERT_FATAL(svdev != NULL);
svdev->vdev.registered = true;
return svdev; return svdev;
} }