DPDK patches and discussions
 help / color / mirror / Atom feed
From: Olivier Matz <olivier.matz@6wind.com>
To: dev@dpdk.org
Subject: [dpdk-dev] [PATCH] mempool: fix leak when mempool creation fails
Date: Tue, 16 Feb 2016 15:40:10 +0100	[thread overview]
Message-ID: <1455633610-5179-1-git-send-email-olivier.matz@6wind.com> (raw)

Since commits ff909fe21f and 4e32101f9b, it is now possible to free
memzones and rings.

The rte_mempool_create() should be modified to take advantage of this
and not leak memory when an allocation fails.

Signed-off-by: Olivier Matz <olivier.matz@6wind.com>
---
 lib/librte_mempool/rte_mempool.c | 28 +++++++++++++---------------
 1 file changed, 13 insertions(+), 15 deletions(-)

diff --git a/lib/librte_mempool/rte_mempool.c b/lib/librte_mempool/rte_mempool.c
index aff5f6d..f8781e1 100644
--- a/lib/librte_mempool/rte_mempool.c
+++ b/lib/librte_mempool/rte_mempool.c
@@ -438,8 +438,8 @@ rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
 	char rg_name[RTE_RING_NAMESIZE];
 	struct rte_mempool_list *mempool_list;
 	struct rte_mempool *mp = NULL;
-	struct rte_tailq_entry *te;
-	struct rte_ring *r;
+	struct rte_tailq_entry *te = NULL;
+	struct rte_ring *r = NULL;
 	const struct rte_memzone *mz;
 	size_t mempool_size;
 	int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
@@ -511,7 +511,7 @@ rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
 	snprintf(rg_name, sizeof(rg_name), RTE_MEMPOOL_MZ_FORMAT, name);
 	r = rte_ring_create(rg_name, rte_align32pow2(n+1), socket_id, rg_flags);
 	if (r == NULL)
-		goto exit;
+		goto exit_unlock;
 
 	/*
 	 * reserve a memory zone for this mempool: private data is
@@ -536,7 +536,7 @@ rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
 	te = rte_zmalloc("MEMPOOL_TAILQ_ENTRY", sizeof(*te), 0);
 	if (te == NULL) {
 		RTE_LOG(ERR, MEMPOOL, "Cannot allocate tailq entry!\n");
-		goto exit;
+		goto exit_unlock;
 	}
 
 	/*
@@ -561,15 +561,8 @@ rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
 	snprintf(mz_name, sizeof(mz_name), RTE_MEMPOOL_MZ_FORMAT, name);
 
 	mz = rte_memzone_reserve(mz_name, mempool_size, socket_id, mz_flags);
-
-	/*
-	 * no more memory: in this case we loose previously reserved
-	 * space for the ring as we cannot free it
-	 */
-	if (mz == NULL) {
-		rte_free(te);
-		goto exit;
-	}
+	if (mz == NULL)
+		goto exit_unlock;
 
 	if (rte_eal_has_hugepages()) {
 		startaddr = (void*)mz->addr;
@@ -633,11 +626,16 @@ rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
 	rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
 	TAILQ_INSERT_TAIL(mempool_list, te, next);
 	rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
-
-exit:
 	rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
 
 	return mp;
+
+exit_unlock:
+	rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
+	rte_ring_free(r);
+	rte_free(te);
+
+	return NULL;
 }
 
 /* Return the number of entries in the mempool */
-- 
2.1.4

             reply	other threads:[~2016-02-16 14:40 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-16 14:40 Olivier Matz [this message]
2016-02-19 15:18 ` Thomas Monjalon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1455633610-5179-1-git-send-email-olivier.matz@6wind.com \
    --to=olivier.matz@6wind.com \
    --cc=dev@dpdk.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).