From 7448f8953626a40db03d4c73d650bb1ea06a1089 Mon Sep 17 00:00:00 2001 From: Changpeng Liu Date: Thu, 8 Mar 2018 19:29:26 -0500 Subject: [PATCH] io_channel: protect critial variables under mutex Change-Id: If5e16c2ac148fd4818c942e8f57b02671e76ed62 Signed-off-by: Changpeng Liu Reviewed-on: https://review.gerrithub.io/403153 Tested-by: SPDK Automated Test System Reviewed-by: Jim Harris Reviewed-by: Daniel Verkamp --- lib/util/io_channel.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/util/io_channel.c b/lib/util/io_channel.c index d1ce7c543..4e0b2463f 100644 --- a/lib/util/io_channel.c +++ b/lib/util/io_channel.c @@ -334,6 +334,12 @@ _spdk_io_device_attempt_free(struct io_device *dev) struct spdk_io_channel *ch; pthread_mutex_lock(&g_devlist_mutex); + + if (!dev->unregistered) { + pthread_mutex_unlock(&g_devlist_mutex); + return; + } + TAILQ_FOREACH(thread, &g_threads, tailq) { TAILQ_FOREACH(ch, &thread->io_channels, tailq) { if (ch->dev == dev) { @@ -457,6 +463,8 @@ _spdk_put_io_channel(void *arg) { struct spdk_io_channel *ch = arg; + assert(ch->thread == spdk_get_thread()); + if (ch->ref == 0) { SPDK_ERRLOG("ref already zero\n"); return; @@ -474,9 +482,7 @@ _spdk_put_io_channel(void *arg) TAILQ_REMOVE(&ch->thread->io_channels, ch, tailq); pthread_mutex_unlock(&g_devlist_mutex); - if (ch->dev->unregistered) { - _spdk_io_device_attempt_free(ch->dev); - } + _spdk_io_device_attempt_free(ch->dev); free(ch); }