Skip to contentSkip to conversation

mempool: don't leak ring on failure

PATCH In review Stephen Hemminger 4 participants 7 messages Jun 24, 2014 – Jun 25, 2014 4 messages not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mempool: don't leak ring on failure

If mempool can not be created because of insufficient memory it returns an error but has already created a ring (and leaves it behind). This prevents code from trying one mempool size and then retrying with a smaller size if the bigger size fails.

Reordering to do ring creation after getting memory fixes the problem.

lib/librte_mempool/rte_mempool.c +9 -9
@@ -473,15 +473,6 @@ rte_mempool_xmem_create(const char *name
 
 	rte_rwlock_write_lock(RTE_EAL_MEMPOOL_RWLOCK);
 
-	/* allocate the ring that will be used to store objects */
-	/* Ring functions will return appropriate errors if we are
-	 * running as a secondary process etc., so no checks made
-	 * in this function for that condition */
-	rte_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;
-
 	/*
 	 * reserve a memory zone for this mempool: private data is
 	 * cache-aligned
@@ -542,6 +533,15 @@ rte_mempool_xmem_create(const char *name
 		startaddr = (void*)addr;
 	}
 
+	/* allocate the ring that will be used to store objects */
+	/* Ring functions will return appropriate errors if we are
+	 * running as a secondary process etc., so no checks made
+	 * in this function for that condition */
+	rte_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;
+
 	/* init the mempool structure */
 	mp = startaddr;
 	memset(mp, 0, sizeof(*mp));
#2 lore raw
In reply to a message not in the archive

Hi Stephen,

Quoted 9 lines

If mempool can not be created because of insufficient memory it returns an error but has already created a ring (and leaves it behind). This prevents code from trying one mempool size and then retrying with a smaller size if the bigger size fails.

Reordering to do ring creation after getting memory fixes the problem.

But now, memzone created for the actual mempool could get leaked instead?

Quoted 37 lines
lib/librte_mempool/rte_mempool.c +9 -9
@@ -473,15 +473,6 @@ rte_mempool_xmem_create(const char *name
 
 	rte_rwlock_write_lock(RTE_EAL_MEMPOOL_RWLOCK);
 
-	/* allocate the ring that will be used to store objects */
-	/* Ring functions will return appropriate errors if we are
-	 * running as a secondary process etc., so no checks made
-	 * in this function for that condition */
-	rte_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;
-
 	/*
 	 * reserve a memory zone for this mempool: private data is
 	 * cache-aligned
@@ -542,6 +533,15 @@ rte_mempool_xmem_create(const char *name
 		startaddr = (void*)addr;
 	}
 
+	/* allocate the ring that will be used to store objects */
+	/* Ring functions will return appropriate errors if we are
+	 * running as a secondary process etc., so no checks made
+	 * in this function for that condition */
+	rte_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;
+
 	/* init the mempool structure */
 	mp = startaddr;
 	memset(mp, 0, sizeof(*mp));
#7 lore raw
In reply to a message not in the archive
Quoted 14 lines

-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Ananyev, Konstantin
Sent: Tuesday, June 24, 2014 9:16 AM
To: Stephen Hemminger; [email protected]
Subject: Re: [dpdk-dev] [PATCH] mempool: don't leak ring on failure

Hi Stephen,

Quoted 6 lines

If mempool can not be created because of insufficient memory it returns an error but has already created a ring (and leaves it behind). This prevents code from trying one mempool size and then retrying with a smaller size if the bigger size fails.

Quoted 6 lines
Quoted 3 lines

Reordering to do ring creation after getting memory fixes the problem.

But now, memzone created for the actual mempool could get leaked instead?

Is either of these a problem that really needs to be fixed? If there is not enough memory to create the mempool what is the app likely to do, other than just quit with an error message?

#6 lore raw
In reply to a message not in the archive
Quoted 8 lines
Quoted 1 line · Konstantin

But now, memzone created for the actual mempool could get leaked instead?

Since memzone's can't be destroyed, then only solution would be if checked if memzone with same name already exists.

Actually, wouldn't it be the better way to create one memzone for both mempool and its ring? Now we have rte_ring_init().

Konstantin

Acked-by
#8 lore raw
In reply to a message not in the archive

Hello Stephen,

Quoted 7 lines · Stephen Hemminger

If mempool can not be created because of insufficient memory it returns an error but has already created a ring (and leaves it behind). This prevents code from trying one mempool size and then retrying with a smaller size if the bigger size fails.

Reordering to do ring creation after getting memory fixes the problem.

Your patch moves the creation of the ring after the call to rte_memzone_reserve(), so now it tries to create the memory for the object pool before the ring. The problem disappears because the object pool is usually much bigger than the ring, so once the first allocation is done, the second is unlikely to fail.

I think this explanation could be added in the commit log.

#10 lore raw
In reply to a message not in the archive
Quoted 10 lines · Olivier MATZ

Your patch moves the creation of the ring after the call to rte_memzone_reserve(), so now it tries to create the memory for the object pool before the ring. The problem disappears because the object pool is usually much bigger than the ring, so once the first allocation is done, the second is unlikely to fail.

I think this explanation could be added in the commit log.

Sorry, I didn't see that Konstantin and Bruce already answered to this.