From f50ef7009c024c29a3da7ecdec866604df596c7f Mon Sep 17 00:00:00 2001 From: Nick Connolly Date: Wed, 26 Jan 2022 22:42:05 +0000 Subject: [PATCH] ut/nvme_ctrlr: initialize mutex for portability For correct behaviour, pthread_mutex must be initialized before use and destroyed afterwards. An already initialized mutex should not be re-initialized. Add calls to nvme_ctrlr_construct where nvme_ctrlr_destruct is called without a matching construct. Add missing calls to mutex_init and mutex_destroy as required. Signed-off-by: Nick Connolly Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/11298 (master) (cherry picked from commit 968371131eb4bd12679c08dc41d08660f158fe21) Change-Id: I9753fa7fbd77402f23a08a66f4b489a5c229487a Signed-off-by: Krzysztof Karas Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/11302 Reviewed-by: Nick Connolly Reviewed-by: Tomasz Zawadzki Reviewed-by: Konrad Sztyber Reviewed-by: Jim Harris Tested-by: SPDK CI Jenkins --- test/unit/lib/nvme/nvme_ctrlr.c/nvme_ctrlr_ut.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/unit/lib/nvme/nvme_ctrlr.c/nvme_ctrlr_ut.c b/test/unit/lib/nvme/nvme_ctrlr.c/nvme_ctrlr_ut.c index fa3665b2ff..138d714a7f 100644 --- a/test/unit/lib/nvme/nvme_ctrlr.c/nvme_ctrlr_ut.c +++ b/test/unit/lib/nvme/nvme_ctrlr.c/nvme_ctrlr_ut.c @@ -2601,6 +2601,8 @@ test_nvme_ctrlr_active_ns_list_v0(void) { DECLARE_AND_CONSTRUCT_CTRLR(); + SPDK_CU_ASSERT_FATAL(nvme_ctrlr_construct(&ctrlr) == 0); + ctrlr.vs.bits.mjr = 1; ctrlr.vs.bits.mnr = 0; ctrlr.vs.bits.ter = 0; @@ -2627,6 +2629,8 @@ test_nvme_ctrlr_active_ns_list_v2(void) uint32_t active_ns_list[1024]; DECLARE_AND_CONSTRUCT_CTRLR(); + SPDK_CU_ASSERT_FATAL(nvme_ctrlr_construct(&ctrlr) == 0); + ctrlr.vs.bits.mjr = 1; ctrlr.vs.bits.mnr = 2; ctrlr.vs.bits.ter = 0; @@ -2654,6 +2658,8 @@ test_nvme_ctrlr_active_ns_list_v2(void) active_ns_list[i] = i + 1; } + SPDK_CU_ASSERT_FATAL(nvme_ctrlr_construct(&ctrlr) == 0); + ctrlr.state = NVME_CTRLR_STATE_IDENTIFY_ACTIVE_NS; g_active_ns_list = active_ns_list; g_active_ns_list_length = SPDK_COUNTOF(active_ns_list); @@ -2675,6 +2681,8 @@ test_nvme_ctrlr_active_ns_list_v2(void) active_ns_list[i] = i + 1; } + SPDK_CU_ASSERT_FATAL(nvme_ctrlr_construct(&ctrlr) == 0); + ctrlr.state = NVME_CTRLR_STATE_IDENTIFY_ACTIVE_NS; SPDK_CU_ASSERT_FATAL(nvme_ctrlr_process_init(&ctrlr) == 0); SPDK_CU_ASSERT_FATAL(ctrlr.state == NVME_CTRLR_STATE_IDENTIFY_NS); @@ -2930,6 +2938,8 @@ test_nvme_ctrlr_identify_namespaces_iocs_specific_next(void) ns[i].active = true; } + CU_ASSERT(pthread_mutex_init(&ctrlr.ctrlr_lock, NULL) == 0); + ctrlr.cdata.nn = 5; /* case 1: No first/next active NS, move on to the next state, expect: pass */ prev_nsid = 0; @@ -2992,6 +3002,8 @@ test_nvme_ctrlr_identify_namespaces_iocs_specific_next(void) CU_ASSERT(rc == 1); CU_ASSERT(ctrlr.state == NVME_CTRLR_STATE_ERROR); CU_ASSERT(ctrlr.state_timeout_tsc == NVME_TIMEOUT_INFINITE); + + CU_ASSERT(pthread_mutex_destroy(&ctrlr.ctrlr_lock) == 0); } static void @@ -3069,6 +3081,8 @@ test_nvme_ctrlr_parse_ana_log_page(void) RB_INSERT(nvme_ns_tree, &ctrlr.ns, &ns[i]); } + CU_ASSERT(pthread_mutex_init(&ctrlr.ctrlr_lock, NULL) == 0); + ctrlr.cdata.nn = 3; ctrlr.cdata.nanagrpid = 3; ctrlr.active_ns_count = 3; @@ -3127,6 +3141,8 @@ test_nvme_ctrlr_parse_ana_log_page(void) CU_ASSERT(ns[2].ana_group_id == 1); CU_ASSERT(ns[2].ana_state == SPDK_NVME_ANA_OPTIMIZED_STATE); + CU_ASSERT(pthread_mutex_destroy(&ctrlr.ctrlr_lock) == 0); + free(ctrlr.ana_log_page); free(ctrlr.copied_ana_desc); }