From 1dd07870e38fdd501aa150625fe8835f5e161ee6 Mon Sep 17 00:00:00 2001 From: yangtianyu 00383654 Date: Thu, 4 Apr 2019 09:45:14 +0800 Subject: [PATCH] bdev/null : Clean up module resources that failed to initialize. Currently, if the Bdev subsystem initialization fails, the initialization failed module does not call the fini function cleanup resource. However, null module use the 'spdk_io_device_register' and spdk_zmalloc to request resources. In failed path, it will cause resource leak when exiting the app. To avoid this, it will cleanup resources when module initialization fails. Change-Id: Ib1a89e5238252d69b6e64ea02a0bd826661349a5 Signed-off-by: Tianyu Yang Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/450105 Reviewed-by: GangCao Reviewed-by: Changpeng Liu Reviewed-by: Jim Harris Tested-by: SPDK CI Jenkins --- lib/bdev/null/bdev_null.c | 10 +++++++++- test/vhost/other/invalid.config | 18 ++++++++++++++++++ test/vhost/other/negative.sh | 5 +++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 test/vhost/other/invalid.config diff --git a/lib/bdev/null/bdev_null.c b/lib/bdev/null/bdev_null.c index e1b604420..eddb979a4 100644 --- a/lib/bdev/null/bdev_null.c +++ b/lib/bdev/null/bdev_null.c @@ -279,6 +279,12 @@ null_bdev_destroy_cb(void *io_device, void *ctx_buf) spdk_poller_unregister(&ch->poller); } +static void +_bdev_null_cleanup_cb(void *arg) +{ + spdk_free(g_null_read_buf); +} + static int bdev_null_initialize(void) { @@ -358,8 +364,10 @@ bdev_null_initialize(void) i++; } - end: + if (rc) { + spdk_io_device_unregister(&g_null_bdev_head, _bdev_null_cleanup_cb); + } return rc; } diff --git a/test/vhost/other/invalid.config b/test/vhost/other/invalid.config new file mode 100644 index 000000000..58b703068 --- /dev/null +++ b/test/vhost/other/invalid.config @@ -0,0 +1,18 @@ +# SPDK vhost configuration file +# +# Please write all parameters using ASCII. +# The parameter must be quoted if it includes whitespace. + +# Configuration syntax: +# Leading whitespace is ignored. +# Lines starting with '#' are comments. +# Lines ending with '\' are concatenated with the next line. +# Bracketed ([]) names define sections + +[Global] + # Instance ID for multi-process support + # Default: 0 + #InstanceID 0 + +[Null] + Dev null0 512 513 diff --git a/test/vhost/other/negative.sh b/test/vhost/other/negative.sh index 5728a2838..a34c462d4 100755 --- a/test/vhost/other/negative.sh +++ b/test/vhost/other/negative.sh @@ -49,6 +49,11 @@ if $VHOST_APP -c /path/to/non_existing_file/conf -f $SPDK_VHOST_SCSI_TEST_DIR/vh fail "vhost started when specifying invalid config file" fi +# Testing vhost start with invalid config. Vhost will exit with error as bdev module init failed +if $VHOST_APP -c $NEGATIVE_BASE_DIR/invalid.config; then + fail "vhost started when specifying invalid config file" +fi + # Expecting vhost to fail if an incorrect argument is given if $VHOST_APP -x -h; then fail "vhost started with invalid -x command line option"