From 7b3f4110631edb5fb33c73082210949ec29d3faf Mon Sep 17 00:00:00 2001 From: Darek Stojaczyk Date: Thu, 25 Apr 2019 14:59:03 +0200 Subject: [PATCH] thread: fix deadlock in spdk_for_each_thread error handling We could return from spdk_for_each_thread() without unlocking the global threading mutex, rendering the entire SPDK application useless. Change-Id: I641c7b36db996f9a36310332279648014579de83 Signed-off-by: Darek Stojaczyk Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/452087 Tested-by: SPDK CI Jenkins Reviewed-by: Ben Walker Reviewed-by: Changpeng Liu Reviewed-by: Shuhei Matsumoto Reviewed-by: Jim Harris --- lib/thread/thread.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/thread/thread.c b/lib/thread/thread.c index c66281bd0..ba367cd65 100644 --- a/lib/thread/thread.c +++ b/lib/thread/thread.c @@ -786,7 +786,6 @@ spdk_for_each_thread(spdk_msg_fn fn, void *ctx, spdk_msg_fn cpl) ct->ctx = ctx; ct->cpl = cpl; - pthread_mutex_lock(&g_devlist_mutex); thread = _get_thread(); if (!thread) { SPDK_ERRLOG("No thread allocated\n"); @@ -795,6 +794,8 @@ spdk_for_each_thread(spdk_msg_fn fn, void *ctx, spdk_msg_fn cpl) return; } ct->orig_thread = thread; + + pthread_mutex_lock(&g_devlist_mutex); ct->cur_thread = TAILQ_FIRST(&g_threads); pthread_mutex_unlock(&g_devlist_mutex);