Commit 6bd83dd1 authored by Shuhei Matsumoto's avatar Shuhei Matsumoto Committed by Tomasz Zawadzki
Browse files

bdev/nvme: Factor out nvme_bdev_ctrlr destruction further



Factor out the common operation to destruct nvme_bdev_ctrlr further
into an new helper function _nvme_bdev_ctrlr_destruct() from
remove_cb(), bdev_nvme_delete(), and bdev_nvme_library_fini().

The next patch will use SPDK message to fix the race condition
by locking/unlocking during linked list parse.

Signed-off-by: default avatarShuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
Change-Id: If5392bebcf830fac6165aa424b06aec0c9274dc8
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/5320


Community-CI: Broadcom CI
Community-CI: Mellanox Build Bot
Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: default avatarAleksey Marchuk <alexeymar@mellanox.com>
Reviewed-by: default avatarChangpeng Liu <changpeng.liu@intel.com>
parent e5b23044
Loading
Loading
Loading
Loading
+20 −31
Original line number Diff line number Diff line
@@ -1594,6 +1594,23 @@ attach_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
	free(name);
}

static void
_nvme_bdev_ctrlr_destruct(struct nvme_bdev_ctrlr *nvme_bdev_ctrlr)
{
	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);
	}
}

static void
remove_cb(void *cb_ctx, struct spdk_nvme_ctrlr *ctrlr)
{
@@ -1610,17 +1627,7 @@ remove_cb(void *cb_ctx, struct spdk_nvme_ctrlr *ctrlr)
			nvme_bdev_ctrlr->destruct = true;
			pthread_mutex_unlock(&g_bdev_nvme_mutex);

			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);
			return;
		}
	}
@@ -2057,17 +2064,7 @@ bdev_nvme_delete(const char *name)
	nvme_bdev_ctrlr->destruct = true;
	pthread_mutex_unlock(&g_bdev_nvme_mutex);

	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);

	return 0;
}
@@ -2110,17 +2107,9 @@ bdev_nvme_library_fini(void)

		pthread_mutex_unlock(&g_bdev_nvme_mutex);

		nvme_ctrlr_depopulate_namespaces(nvme_bdev_ctrlr);
		_nvme_bdev_ctrlr_destruct(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);
			pthread_mutex_lock(&g_bdev_nvme_mutex);
		}
	}

	g_bdev_nvme_module_finish = true;