From 175e17671b6c03725fed89ab81cd97cba90cbfa7 Mon Sep 17 00:00:00 2001 From: Ben Walker Date: Wed, 15 Dec 2021 10:29:13 -0700 Subject: [PATCH] nvme: Don't free and allocate the entire ns array in nvme_ctrlr_construct_namespaces We can just reallocate here to be more efficient. Signed-off-by: Ben Walker Change-Id: I8cfc87da23aee6c05ff83aea2165683dddba1dbd Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/10688 Community-CI: Mellanox Build Bot Reviewed-by: Changpeng Liu Reviewed-by: Aleksey Marchuk Tested-by: SPDK CI Jenkins --- lib/nvme/nvme_ctrlr.c | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/lib/nvme/nvme_ctrlr.c b/lib/nvme/nvme_ctrlr.c index 8f2bc723e..d8c13beaf 100644 --- a/lib/nvme/nvme_ctrlr.c +++ b/lib/nvme/nvme_ctrlr.c @@ -2991,34 +2991,32 @@ nvme_ctrlr_construct_namespaces(struct spdk_nvme_ctrlr *ctrlr) { int rc = 0; uint32_t i, nn = ctrlr->cdata.nn; + struct spdk_nvme_ns *tmp; /* ctrlr->num_ns may be 0 (startup) or a different number of namespaces (reset), * so check if we need to reallocate. */ if (nn != ctrlr->num_ns) { - nvme_ctrlr_destruct_namespaces(ctrlr); - - if (nn == 0) { - NVME_CTRLR_WARNLOG(ctrlr, "controller has 0 namespaces\n"); - return 0; - } - - ctrlr->ns = spdk_zmalloc(nn * sizeof(struct spdk_nvme_ns), 64, NULL, - SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_SHARE); - if (ctrlr->ns == NULL) { + tmp = spdk_realloc(ctrlr->ns, nn * sizeof(struct spdk_nvme_ns), 64); + if (tmp == NULL) { rc = -ENOMEM; goto fail; } - ctrlr->num_ns = nn; - } else { - /* - * The controller could have been reset with the same number of namespaces. - * If so, we still need to free the iocs specific data, to get a clean slate. - */ - for (i = 0; i < ctrlr->num_ns; i++) { - nvme_ns_free_iocs_specific_data(&ctrlr->ns[i]); + if (nn > ctrlr->num_ns) { + memset(tmp + ctrlr->num_ns, 0, (nn - ctrlr->num_ns) * sizeof(struct spdk_nvme_ns)); } + + ctrlr->ns = tmp; + ctrlr->num_ns = nn; + } + + /* + * The controller could have been reset with the same number of namespaces. + * If so, we still need to free the iocs specific data, to get a clean slate. + */ + for (i = 0; i < ctrlr->num_ns; i++) { + nvme_ns_free_iocs_specific_data(&ctrlr->ns[i]); } return 0;