From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by dpdk.org (Postfix) with ESMTP id 322C72986 for ; Sat, 4 May 2019 00:56:13 +0200 (CEST) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga106.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 03 May 2019 15:56:12 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.60,427,1549958400"; d="scan'208";a="139718049" Received: from txasoft-yocto.an.intel.com ([10.123.72.192]) by orsmga008.jf.intel.com with ESMTP; 03 May 2019 15:56:11 -0700 From: Erik Gabriel Carrillo To: rsanford@akamai.com, thomas@monjalon.net Cc: anatoly.burakov@intel.com, dev@dpdk.org Date: Fri, 3 May 2019 17:54:42 -0500 Message-Id: <1556924082-22535-1-git-send-email-erik.g.carrillo@intel.com> X-Mailer: git-send-email 1.7.10 In-Reply-To: <1556737217-24338-1-git-send-email-erik.g.carrillo@intel.com> References: <1556737217-24338-1-git-send-email-erik.g.carrillo@intel.com> Subject: [dpdk-dev] [PATCH v2] timer: fix resource leak in finalize X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 03 May 2019 22:56:14 -0000 The finalize function should free the memzone created in the init function, rather than freeing the allocation the memzone references, otherwise a memzone descriptor can be leaked. Fixes: c0749f7096c7 ("timer: allow management in shared memory") Signed-off-by: Erik Gabriel Carrillo --- changes in v2: - Handle scenario where primary process exits before secondaries such that memzone is not freed early (Anatoly) lib/librte_timer/rte_timer.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/librte_timer/rte_timer.c b/lib/librte_timer/rte_timer.c index eb46009..4771287 100644 --- a/lib/librte_timer/rte_timer.c +++ b/lib/librte_timer/rte_timer.c @@ -60,6 +60,8 @@ struct rte_timer_data { }; #define RTE_MAX_DATA_ELS 64 +static const struct rte_memzone *rte_timer_data_mz; +static rte_atomic16_t *rte_timer_mz_refcnt; static struct rte_timer_data *rte_timer_data_arr; static const uint32_t default_data_id; static uint32_t rte_timer_subsystem_initialized; @@ -155,6 +157,7 @@ rte_timer_subsystem_init_v1905(void) struct rte_timer_data *data; int i, lcore_id; static const char *mz_name = "rte_timer_mz"; + size_t data_arr_size = RTE_MAX_DATA_ELS * sizeof(*rte_timer_data_arr); if (rte_timer_subsystem_initialized) return -EALREADY; @@ -164,10 +167,14 @@ rte_timer_subsystem_init_v1905(void) if (mz == NULL) return -EEXIST; + rte_timer_data_mz = mz; rte_timer_data_arr = mz->addr; + rte_timer_mz_refcnt = + (void *)((char *)mz->addr + data_arr_size); rte_timer_data_arr[default_data_id].internal_flags |= FL_ALLOCATED; + rte_atomic16_inc(rte_timer_mz_refcnt); rte_timer_subsystem_initialized = 1; @@ -175,12 +182,15 @@ rte_timer_subsystem_init_v1905(void) } mz = rte_memzone_reserve_aligned(mz_name, - RTE_MAX_DATA_ELS * sizeof(*rte_timer_data_arr), + data_arr_size + sizeof(*rte_timer_mz_refcnt), SOCKET_ID_ANY, 0, RTE_CACHE_LINE_SIZE); if (mz == NULL) return -ENOMEM; + rte_timer_data_mz = mz; rte_timer_data_arr = mz->addr; + rte_timer_mz_refcnt = (void *)((char *)mz->addr + data_arr_size); + rte_atomic16_init(rte_timer_mz_refcnt); for (i = 0; i < RTE_MAX_DATA_ELS; i++) { data = &rte_timer_data_arr[i]; @@ -193,6 +203,7 @@ rte_timer_subsystem_init_v1905(void) } rte_timer_data_arr[default_data_id].internal_flags |= FL_ALLOCATED; + rte_atomic16_inc(rte_timer_mz_refcnt); rte_timer_subsystem_initialized = 1; @@ -205,8 +216,11 @@ BIND_DEFAULT_SYMBOL(rte_timer_subsystem_init, _v1905, 19.05); void __rte_experimental rte_timer_subsystem_finalize(void) { - if (rte_timer_data_arr) - rte_free(rte_timer_data_arr); + if (!rte_timer_subsystem_initialized) + return; + + if (rte_atomic16_dec_and_test(rte_timer_mz_refcnt)) + rte_memzone_free(rte_timer_data_mz); rte_timer_subsystem_initialized = 0; } -- 2.6.4 From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dpdk.org (dpdk.org [92.243.14.124]) by dpdk.space (Postfix) with ESMTP id A9184A0AC5 for ; Sat, 4 May 2019 00:56:17 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 3750729AC; Sat, 4 May 2019 00:56:16 +0200 (CEST) Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by dpdk.org (Postfix) with ESMTP id 322C72986 for ; Sat, 4 May 2019 00:56:13 +0200 (CEST) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga106.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 03 May 2019 15:56:12 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.60,427,1549958400"; d="scan'208";a="139718049" Received: from txasoft-yocto.an.intel.com ([10.123.72.192]) by orsmga008.jf.intel.com with ESMTP; 03 May 2019 15:56:11 -0700 From: Erik Gabriel Carrillo To: rsanford@akamai.com, thomas@monjalon.net Cc: anatoly.burakov@intel.com, dev@dpdk.org Date: Fri, 3 May 2019 17:54:42 -0500 Message-Id: <1556924082-22535-1-git-send-email-erik.g.carrillo@intel.com> X-Mailer: git-send-email 1.7.10 In-Reply-To: <1556737217-24338-1-git-send-email-erik.g.carrillo@intel.com> References: <1556737217-24338-1-git-send-email-erik.g.carrillo@intel.com> Subject: [dpdk-dev] [PATCH v2] timer: fix resource leak in finalize X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Content-Type: text/plain; charset="UTF-8" Message-ID: <20190503225442.mE7aPexQCuPXZyEoCy0bnUJDs93j2fEu_j1X5Y62XME@z> The finalize function should free the memzone created in the init function, rather than freeing the allocation the memzone references, otherwise a memzone descriptor can be leaked. Fixes: c0749f7096c7 ("timer: allow management in shared memory") Signed-off-by: Erik Gabriel Carrillo --- changes in v2: - Handle scenario where primary process exits before secondaries such that memzone is not freed early (Anatoly) lib/librte_timer/rte_timer.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/librte_timer/rte_timer.c b/lib/librte_timer/rte_timer.c index eb46009..4771287 100644 --- a/lib/librte_timer/rte_timer.c +++ b/lib/librte_timer/rte_timer.c @@ -60,6 +60,8 @@ struct rte_timer_data { }; #define RTE_MAX_DATA_ELS 64 +static const struct rte_memzone *rte_timer_data_mz; +static rte_atomic16_t *rte_timer_mz_refcnt; static struct rte_timer_data *rte_timer_data_arr; static const uint32_t default_data_id; static uint32_t rte_timer_subsystem_initialized; @@ -155,6 +157,7 @@ rte_timer_subsystem_init_v1905(void) struct rte_timer_data *data; int i, lcore_id; static const char *mz_name = "rte_timer_mz"; + size_t data_arr_size = RTE_MAX_DATA_ELS * sizeof(*rte_timer_data_arr); if (rte_timer_subsystem_initialized) return -EALREADY; @@ -164,10 +167,14 @@ rte_timer_subsystem_init_v1905(void) if (mz == NULL) return -EEXIST; + rte_timer_data_mz = mz; rte_timer_data_arr = mz->addr; + rte_timer_mz_refcnt = + (void *)((char *)mz->addr + data_arr_size); rte_timer_data_arr[default_data_id].internal_flags |= FL_ALLOCATED; + rte_atomic16_inc(rte_timer_mz_refcnt); rte_timer_subsystem_initialized = 1; @@ -175,12 +182,15 @@ rte_timer_subsystem_init_v1905(void) } mz = rte_memzone_reserve_aligned(mz_name, - RTE_MAX_DATA_ELS * sizeof(*rte_timer_data_arr), + data_arr_size + sizeof(*rte_timer_mz_refcnt), SOCKET_ID_ANY, 0, RTE_CACHE_LINE_SIZE); if (mz == NULL) return -ENOMEM; + rte_timer_data_mz = mz; rte_timer_data_arr = mz->addr; + rte_timer_mz_refcnt = (void *)((char *)mz->addr + data_arr_size); + rte_atomic16_init(rte_timer_mz_refcnt); for (i = 0; i < RTE_MAX_DATA_ELS; i++) { data = &rte_timer_data_arr[i]; @@ -193,6 +203,7 @@ rte_timer_subsystem_init_v1905(void) } rte_timer_data_arr[default_data_id].internal_flags |= FL_ALLOCATED; + rte_atomic16_inc(rte_timer_mz_refcnt); rte_timer_subsystem_initialized = 1; @@ -205,8 +216,11 @@ BIND_DEFAULT_SYMBOL(rte_timer_subsystem_init, _v1905, 19.05); void __rte_experimental rte_timer_subsystem_finalize(void) { - if (rte_timer_data_arr) - rte_free(rte_timer_data_arr); + if (!rte_timer_subsystem_initialized) + return; + + if (rte_atomic16_dec_and_test(rte_timer_mz_refcnt)) + rte_memzone_free(rte_timer_data_mz); rte_timer_subsystem_initialized = 0; } -- 2.6.4