From 528cec83fb687edbf86382db38aeb4a6cdff9beb Mon Sep 17 00:00:00 2001 From: Shuhei Matsumoto Date: Mon, 4 Jan 2021 14:09:27 +0900 Subject: [PATCH] bdev/nvme: Merge decrementing ref. count into nvme_bdev_ctrlr_destruct() For further simplification, merge decrementing reference count of nvme_bdev_ctrlr into nvme_bdev_ctrlr_destruct(). Signed-off-by: Shuhei Matsumoto Change-Id: I604039c3f38a60b316ae465d4649e9eb11bfb6cc Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/5573 Community-CI: Broadcom CI Tested-by: SPDK CI Jenkins Reviewed-by: Jim Harris Reviewed-by: Changpeng Liu --- module/bdev/nvme/bdev_nvme.c | 24 ++----------------- module/bdev/nvme/common.c | 22 +++++++---------- .../lib/bdev/bdev_ocssd.c/bdev_ocssd_ut.c | 9 +------ 3 files changed, 12 insertions(+), 43 deletions(-) diff --git a/module/bdev/nvme/bdev_nvme.c b/module/bdev/nvme/bdev_nvme.c index 566c6ba79..8e971e6e3 100644 --- a/module/bdev/nvme/bdev_nvme.c +++ b/module/bdev/nvme/bdev_nvme.c @@ -1313,17 +1313,7 @@ nvme_ctrlr_depopulate_namespace_done(struct nvme_bdev_ns *nvme_ns) { struct nvme_bdev_ctrlr *nvme_bdev_ctrlr = nvme_ns->ctrlr; - pthread_mutex_lock(&g_bdev_nvme_mutex); - assert(nvme_bdev_ctrlr->ref > 0); - nvme_bdev_ctrlr->ref--; - - if (nvme_bdev_ctrlr->ref == 0 && nvme_bdev_ctrlr->destruct) { - pthread_mutex_unlock(&g_bdev_nvme_mutex); - nvme_bdev_ctrlr_destruct(nvme_bdev_ctrlr); - return; - } - - pthread_mutex_unlock(&g_bdev_nvme_mutex); + nvme_bdev_ctrlr_destruct(nvme_bdev_ctrlr); } static void @@ -1650,17 +1640,7 @@ _nvme_bdev_ctrlr_destruct(void *ctx) struct nvme_bdev_ctrlr *nvme_bdev_ctrlr = ctx; nvme_ctrlr_depopulate_namespaces(nvme_bdev_ctrlr); - - pthread_mutex_lock(&g_bdev_nvme_mutex); - assert(nvme_bdev_ctrlr->ref > 0); - nvme_bdev_ctrlr->ref--; - if (nvme_bdev_ctrlr->ref == 0) { - pthread_mutex_unlock(&g_bdev_nvme_mutex); - - nvme_bdev_ctrlr_destruct(nvme_bdev_ctrlr); - } else { - pthread_mutex_unlock(&g_bdev_nvme_mutex); - } + nvme_bdev_ctrlr_destruct(nvme_bdev_ctrlr); } static void diff --git a/module/bdev/nvme/common.c b/module/bdev/nvme/common.c index 3cefd2eaf..ac04b829a 100644 --- a/module/bdev/nvme/common.c +++ b/module/bdev/nvme/common.c @@ -168,13 +168,17 @@ nvme_bdev_ctrlr_do_destruct(void *ctx) void nvme_bdev_ctrlr_destruct(struct nvme_bdev_ctrlr *nvme_bdev_ctrlr) { - assert(nvme_bdev_ctrlr->destruct); - pthread_mutex_lock(&g_bdev_nvme_mutex); - if (nvme_bdev_ctrlr->resetting) { + + assert(nvme_bdev_ctrlr->ref > 0); + nvme_bdev_ctrlr->ref--; + + if (nvme_bdev_ctrlr->ref > 0 || !nvme_bdev_ctrlr->destruct || + nvme_bdev_ctrlr->resetting) { pthread_mutex_unlock(&g_bdev_nvme_mutex); return; } + pthread_mutex_unlock(&g_bdev_nvme_mutex); nvme_bdev_ctrlr_do_destruct(nvme_bdev_ctrlr); @@ -196,16 +200,8 @@ nvme_bdev_detach_bdev_from_ns(struct nvme_bdev *nvme_disk) struct nvme_bdev_ctrlr *ctrlr = nvme_disk->nvme_ns->ctrlr; pthread_mutex_lock(&g_bdev_nvme_mutex); - assert(ctrlr->ref > 0); - ctrlr->ref--; - TAILQ_REMOVE(&nvme_disk->nvme_ns->bdevs, nvme_disk, tailq); - - if (ctrlr->ref == 0 && ctrlr->destruct) { - pthread_mutex_unlock(&g_bdev_nvme_mutex); - nvme_bdev_ctrlr_destruct(ctrlr); - return; - } - pthread_mutex_unlock(&g_bdev_nvme_mutex); + + nvme_bdev_ctrlr_destruct(ctrlr); } diff --git a/test/unit/lib/bdev/bdev_ocssd.c/bdev_ocssd_ut.c b/test/unit/lib/bdev/bdev_ocssd.c/bdev_ocssd_ut.c index 1354b2b90..fb9208255 100644 --- a/test/unit/lib/bdev/bdev_ocssd.c/bdev_ocssd_ut.c +++ b/test/unit/lib/bdev/bdev_ocssd.c/bdev_ocssd_ut.c @@ -199,12 +199,7 @@ nvme_ctrlr_depopulate_namespace_done(struct nvme_bdev_ns *ns) { struct nvme_bdev_ctrlr *ctrlr = ns->ctrlr; - CU_ASSERT(ctrlr->ref > 0); - ctrlr->ref--; - - if (ctrlr->ref == 0 && ctrlr->destruct) { - nvme_bdev_ctrlr_destruct(ctrlr); - } + nvme_bdev_ctrlr_destruct(ctrlr); } static struct nvme_bdev_ctrlr * @@ -542,8 +537,6 @@ delete_nvme_bdev_controller(struct nvme_bdev_ctrlr *nvme_bdev_ctrlr) bdev_ocssd_depopulate_namespace(nvme_bdev_ctrlr->namespaces[nsid]); } - CU_ASSERT(nvme_bdev_ctrlr->ref == 1); - nvme_bdev_ctrlr->ref--; nvme_bdev_ctrlr_destruct(nvme_bdev_ctrlr); spdk_delay_us(1000);