Skip to content

Commit 86f36ff

Browse files
olivier-matz-6windThomas Monjalon
authored andcommitted
mempool: fix leak when creation fails
Since commits ff909fe and 4e32101, 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>
1 parent ca67ed2 commit 86f36ff

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

lib/librte_mempool/rte_mempool.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,8 @@ rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
438438
char rg_name[RTE_RING_NAMESIZE];
439439
struct rte_mempool_list *mempool_list;
440440
struct rte_mempool *mp = NULL;
441-
struct rte_tailq_entry *te;
442-
struct rte_ring *r;
441+
struct rte_tailq_entry *te = NULL;
442+
struct rte_ring *r = NULL;
443443
const struct rte_memzone *mz;
444444
size_t mempool_size;
445445
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,
511511
snprintf(rg_name, sizeof(rg_name), RTE_MEMPOOL_MZ_FORMAT, name);
512512
r = rte_ring_create(rg_name, rte_align32pow2(n+1), socket_id, rg_flags);
513513
if (r == NULL)
514-
goto exit;
514+
goto exit_unlock;
515515

516516
/*
517517
* 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,
536536
te = rte_zmalloc("MEMPOOL_TAILQ_ENTRY", sizeof(*te), 0);
537537
if (te == NULL) {
538538
RTE_LOG(ERR, MEMPOOL, "Cannot allocate tailq entry!\n");
539-
goto exit;
539+
goto exit_unlock;
540540
}
541541

542542
/*
@@ -561,15 +561,8 @@ rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
561561
snprintf(mz_name, sizeof(mz_name), RTE_MEMPOOL_MZ_FORMAT, name);
562562

563563
mz = rte_memzone_reserve(mz_name, mempool_size, socket_id, mz_flags);
564-
565-
/*
566-
* no more memory: in this case we loose previously reserved
567-
* space for the ring as we cannot free it
568-
*/
569-
if (mz == NULL) {
570-
rte_free(te);
571-
goto exit;
572-
}
564+
if (mz == NULL)
565+
goto exit_unlock;
573566

574567
if (rte_eal_has_hugepages()) {
575568
startaddr = (void*)mz->addr;
@@ -633,11 +626,16 @@ rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
633626
rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
634627
TAILQ_INSERT_TAIL(mempool_list, te, next);
635628
rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
636-
637-
exit:
638629
rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
639630

640631
return mp;
632+
633+
exit_unlock:
634+
rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
635+
rte_ring_free(r);
636+
rte_free(te);
637+
638+
return NULL;
641639
}
642640

643641
/* Return the number of entries in the mempool */

0 commit comments

Comments
 (0)