From c49786722a150ab47e5eaf5935518c6f31e730ff Mon Sep 17 00:00:00 2001 From: Tomasz Zawadzki Date: Mon, 19 Nov 2018 05:29:18 -0500 Subject: [PATCH] lib/thread: check for NULL return on _get_thread() In addition UT were changed to set_thread() before registering io device. Change-Id: I959dbc800db9c1f50564274a73d71e05e843d8c9 Signed-off-by: Tomasz Zawadzki Reviewed-on: https://review.gerrithub.io/433611 Tested-by: SPDK CI Jenkins Reviewed-by: Ben Walker Reviewed-by: Jim Harris Reviewed-by: Shuhei Matsumoto --- lib/thread/thread.c | 37 ++++++++++++++++++++--- test/unit/lib/thread/thread.c/thread_ut.c | 4 +-- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/lib/thread/thread.c b/lib/thread/thread.c index 42b0fcbd2..d4028400f 100644 --- a/lib/thread/thread.c +++ b/lib/thread/thread.c @@ -630,6 +630,7 @@ void spdk_for_each_thread(spdk_thread_fn fn, void *ctx, spdk_thread_fn cpl) { struct call_thread *ct; + struct spdk_thread *thread; ct = calloc(1, sizeof(*ct)); if (!ct) { @@ -643,7 +644,14 @@ spdk_for_each_thread(spdk_thread_fn fn, void *ctx, spdk_thread_fn cpl) ct->cpl = cpl; pthread_mutex_lock(&g_devlist_mutex); - ct->orig_thread = _get_thread(); + thread = _get_thread(); + if (!thread) { + SPDK_ERRLOG("No thread allocated\n"); + free(ct); + cpl(ctx); + return; + } + ct->orig_thread = thread; ct->cur_thread = TAILQ_FIRST(&g_threads); pthread_mutex_unlock(&g_devlist_mutex); @@ -659,11 +667,19 @@ spdk_io_device_register(void *io_device, spdk_io_channel_create_cb create_cb, const char *name) { struct io_device *dev, *tmp; + struct spdk_thread *thread; assert(io_device != NULL); assert(create_cb != NULL); assert(destroy_cb != NULL); + thread = spdk_get_thread(); + if (!thread) { + SPDK_ERRLOG("%s called from non-SPDK thread\n", __func__); + assert(false); + return; + } + dev = calloc(1, sizeof(struct io_device)); if (dev == NULL) { SPDK_ERRLOG("could not allocate io_device\n"); @@ -685,7 +701,7 @@ spdk_io_device_register(void *io_device, spdk_io_channel_create_cb create_cb, dev->refcnt = 0; SPDK_DEBUGLOG(SPDK_LOG_THREAD, "Registering io_device %s (%p) on thread %s\n", - dev->name, dev->io_device, spdk_get_thread()->name); + dev->name, dev->io_device, thread->name); pthread_mutex_lock(&g_devlist_mutex); TAILQ_FOREACH(tmp, &g_io_devices, tailq) { @@ -737,6 +753,11 @@ spdk_io_device_unregister(void *io_device, spdk_io_device_unregister_cb unregist struct spdk_thread *thread; thread = spdk_get_thread(); + if (!thread) { + SPDK_ERRLOG("%s called from non-SPDK thread\n", __func__); + assert(false); + return; + } pthread_mutex_lock(&g_devlist_mutex); TAILQ_FOREACH(dev, &g_io_devices, tailq) { @@ -859,12 +880,20 @@ _spdk_put_io_channel(void *arg) { struct spdk_io_channel *ch = arg; bool do_remove_dev = true; + struct spdk_thread *thread; + + thread = spdk_get_thread(); + if (!thread) { + SPDK_ERRLOG("%s called from non-SPDK thread\n", __func__); + assert(false); + return; + } SPDK_DEBUGLOG(SPDK_LOG_THREAD, "Releasing io_channel %p for io_device %s (%p). Channel thread %p. Current thread %s\n", - ch, ch->dev->name, ch->dev->io_device, ch->thread, spdk_get_thread()->name); + ch, ch->dev->name, ch->dev->io_device, ch->thread, thread->name); - assert(ch->thread == spdk_get_thread()); + assert(ch->thread == thread); ch->destroy_ref--; diff --git a/test/unit/lib/thread/thread.c/thread_ut.c b/test/unit/lib/thread/thread.c/thread_ut.c index e00e762fa..b4a42bcf3 100644 --- a/test/unit/lib/thread/thread.c/thread_ut.c +++ b/test/unit/lib/thread/thread.c/thread_ut.c @@ -219,8 +219,8 @@ for_each_channel_remove(void) int count = 0; allocate_threads(3); - spdk_io_device_register(&io_target, channel_create, channel_destroy, sizeof(int), NULL); set_thread(0); + spdk_io_device_register(&io_target, channel_create, channel_destroy, sizeof(int), NULL); ch0 = spdk_get_io_channel(&io_target); set_thread(1); ch1 = spdk_get_io_channel(&io_target); @@ -294,13 +294,13 @@ for_each_channel_unreg(void) int io_target; allocate_threads(1); + set_thread(0); CU_ASSERT(TAILQ_EMPTY(&g_io_devices)); spdk_io_device_register(&io_target, channel_create, channel_destroy, sizeof(int), NULL); CU_ASSERT(!TAILQ_EMPTY(&g_io_devices)); dev = TAILQ_FIRST(&g_io_devices); SPDK_CU_ASSERT_FATAL(dev != NULL); CU_ASSERT(TAILQ_NEXT(dev, tailq) == NULL); - set_thread(0); ch0 = spdk_get_io_channel(&io_target); spdk_for_each_channel(&io_target, unreg_ch_done, &ctx, unreg_foreach_done);