Skip to contentSkip to conversation

fix memory leak in mm/slab.c::alloc_kmemlist() (try #2)

PATCH In review Jesper Juhl 4 participants 9 messages Mar 18, 2006 – Mar 22, 2006 Memory management

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fix memory leak in mm/slab.c::alloc_kmemlist() (try #2)

The Coverity checker found that we may leak memory in mm/slab.c::alloc_kmemlist()
This should fix the leak and coverity bug #589

Currently the only caller of alloc_kmemlist() will BUG() if alloc_kmemlist() fails, but that doesn't mean we shouldn't clean up properly IMHO. Also, the caller (do_tune_cpucache()) could maybe be changed in the future to do something more clever than just BUG() and in that case we really shouldn't be leaking memory when we return -ENOMEM.

The patch introduces one more loop to the function in the failure path :-( But, since we are very unlikely to ever hit the failure path this shouldn't be too painfull.

The patch has been compile and boot tested on x86, but since I'm not very intimate with the slab code I'd appreciate it if someone would take a close look on the changes before merging them.
IMO this patch should not go into 2.6.16, but instead spend some time in -mm with the intention to merge it for 2.6.17 - although it does fix a real leak it's not a regression compared to 2.6.15.

---

 mm/slab.c |   35 +++++++++++++++++++++++++++++------
 1 files changed, 29 insertions(+), 6 deletions(-)
mm/slab.c +29 -6
@@ -3399,12 +3399,17 @@ EXPORT_SYMBOL_GPL(kmem_cache_name);
 static int alloc_kmemlist(struct kmem_cache *cachep)
 {
 	int node;
+	int count = -1;
 	struct kmem_list3 *l3;
-	int err = 0;
+	struct array_cache *new;
+	struct array_cache **new_alien;
 
 	for_each_online_node(node) {
-		struct array_cache *nc = NULL, *new;
-		struct array_cache **new_alien = NULL;
+		struct array_cache *nc = NULL;
+
+		new = NULL;
+		new_alien = NULL;
+		count++;
 #ifdef CONFIG_NUMA
 		new_alien = alloc_alien_cache(node, cachep->limit);
 		if (!new_alien)
@@ -3447,10 +3452,28 @@ static int alloc_kmemlist(struct kmem_ca
 					cachep->batchcount + cachep->num;
 		cachep->nodelists[node] = l3;
 	}
-	return err;
+	return 0;
+/*
+   If one or more allocations fail we need to undo all allocations done up to
+   this point.
+   Unfortunately this means yet another loop, but since this only happens on
+   failure and frees up memory in a memory-tight situation, it's not too bad.
+ */
 fail:
-	err = -ENOMEM;
-	return err;
+	kfree(new);
+	free_alien_cache(new_alien);
+	for_each_online_node(node) {
+		if (count <= 0)
+			break;
+		if (cachep->nodelists[node]) {
+			kfree(cachep->nodelists[node]->shared);
+			free_alien_cache(cachep->nodelists[node]->alien);
+			kfree(cachep->nodelists[node]);
+			cachep->nodelists[node] = NULL;
+		}
+		count--;
+	}
+	return -ENOMEM;
 }
 
 struct ccupdate_struct {
#1 lore raw
Quoted 5 lines · Jesper Juhl

Currently the only caller of alloc_kmemlist() will BUG() if alloc_kmemlist() fails, but that doesn't mean we shouldn't clean up properly IMHO. Also, the caller (do_tune_cpucache()) could maybe be changed in the future to do something more clever than just BUG() and in that case we really shouldn't be leaking memory when we return -ENOMEM.

Yeah, and BUG() can be no-op for embedded.

Quoted 3 lines · Jesper Juhl

The patch has been compile and boot tested on x86, but since I'm not very intimate with the slab code I'd appreciate it if someone would take a close look on the changes before merging them.

You probably didn't hit the error path on your x86 box. The patch looks good to me for -mm although there's few comments below.

mm/slab.c static int alloc_kmemlist(struct kmem_ca · line 3461

Quoted 6 lines
mm/slab.c +6 -0
+/*
+   If one or more allocations fail we need to undo all allocations done up to
+   this point.
+   Unfortunately this means yet another loop, but since this only happens on
+   failure and frees up memory in a memory-tight situation, it's not too bad.
+ */

The formatting of this comment looks strange.

mm/slab.c static int alloc_kmemlist(struct kmem_ca · line 3468

Quoted 4 lines
mm/slab.c +4 -0
+       for_each_online_node(node) {
+               if (count <= 0)
+                       break;
+               if (cachep->nodelists[node]) {

Would probably make sense to extract the above expression into local variable to reduce kernel text size.

Quoted 8 lines
mm/slab.c +8 -0
+                       kfree(cachep->nodelists[node]->shared);
+                       free_alien_cache(cachep->nodelists[node]->alien);
+                       kfree(cachep->nodelists[node]);
+                       cachep->nodelists[node] = NULL;
+               }
+               count--;
+       }
+       return -ENOMEM;
#2 lore raw

Hi Pekka,

Quoted 26 lines · Pekka Enberg
Quoted 5 lines · Jesper Juhl

Currently the only caller of alloc_kmemlist() will BUG() if alloc_kmemlist() fails, but that doesn't mean we shouldn't clean up properly IMHO. Also, the caller (do_tune_cpucache()) could maybe be changed in the future to do something more clever than just BUG() and in that case we really shouldn't be leaking memory when we return -ENOMEM.

Yeah, and BUG() can be no-op for embedded.

Quoted 3 lines · Jesper Juhl

The patch has been compile and boot tested on x86, but since I'm not very intimate with the slab code I'd appreciate it if someone would take a close look on the changes before merging them.

You probably didn't hit the error path on your x86 box. The patch looks good to me for -mm although there's few comments below.

mm/slab.c static int alloc_kmemlist(struct kmem_ca · line 3461

Quoted 6 lines
mm/slab.c +6 -0
+/*
+   If one or more allocations fail we need to undo all allocations done up to
+   this point.
+   Unfortunately this means yet another loop, but since this only happens on
+   failure and frees up memory in a memory-tight situation, it's not too bad.
+ */

The formatting of this comment looks strange.

Quoted 17 lines

mm/slab.c static int alloc_kmemlist(struct kmem_ca · line 3468

Quoted 4 lines
mm/slab.c +4 -0
+       for_each_online_node(node) {
+               if (count <= 0)
+                       break;
+               if (cachep->nodelists[node]) {

Would probably make sense to extract the above expression into local variable to reduce kernel text size.

Quoted 8 lines
mm/slab.c +8 -0
+                       kfree(cachep->nodelists[node]->shared);
+                       free_alien_cache(cachep->nodelists[node]->alien);
+                       kfree(cachep->nodelists[node]);
+                       cachep->nodelists[node] = NULL;
+               }
+               count--;
+       }
+       return -ENOMEM;

Thank you very much for your feedback.

I'll create an updated patch with the changes you suggest. They make perfect sense.

--
Jesper Juhl <[email protected]>
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please http://www.expita.com/nomime.html

#3 lore raw

Re: [PATCH] fix memory leak in mm/slab.c::alloc_kmemlist() (try #3)

Hi Pekka, Andrew, Christoph & everyone else,

Quoted 47 lines · Jesper Juhl

Hi Pekka,

Quoted 26 lines · Pekka Enberg
Quoted 5 lines · Jesper Juhl

Currently the only caller of alloc_kmemlist() will BUG() if alloc_kmemlist() fails, but that doesn't mean we shouldn't clean up properly IMHO. Also, the caller (do_tune_cpucache()) could maybe be changed in the future to do something more clever than just BUG() and in that case we really shouldn't be leaking memory when we return -ENOMEM.

Yeah, and BUG() can be no-op for embedded.

Quoted 3 lines · Jesper Juhl

The patch has been compile and boot tested on x86, but since I'm not very intimate with the slab code I'd appreciate it if someone would take a close look on the changes before merging them.

You probably didn't hit the error path on your x86 box. The patch looks good to me for -mm although there's few comments below.

Review

Quoted 6 lines
(quoted diff) +6 -0
+/*
+   If one or more allocations fail we need to undo all allocations done up to
+   this point.
+   Unfortunately this means yet another loop, but since this only happens on
+   failure and frees up memory in a memory-tight situation, it's not too bad.
+ */

The formatting of this comment looks strange.

Quoted 17 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+       for_each_online_node(node) {
+               if (count <= 0)
+                       break;
+               if (cachep->nodelists[node]) {

Would probably make sense to extract the above expression into local variable to reduce kernel text size.

Quoted 8 lines
(quoted diff) +8 -0
+                       kfree(cachep->nodelists[node]->shared);
+                       free_alien_cache(cachep->nodelists[node]->alien);
+                       kfree(cachep->nodelists[node]);
+                       cachep->nodelists[node] = NULL;
+               }
+               count--;
+       }
+       return -ENOMEM;
Quoted 5 lines

Thank you very much for your feedback.

I'll create an updated patch with the changes you suggest. They make perfect sense.

Here's the latest version of my patch to fix the mem leak in alloc_kmemlist(). It should address Pekkas's comments.

Andrew: Do you think this could go into -mm and get some field testing, so perhaps it has a chance of making 2.6.17?

Fix memory leak in mm/slab.c::alloc_kmemlist().
If one allocation fails we have to roll-back all allocations made up to the point of failure.

---

 mm/slab.c |   36 ++++++++++++++++++++++++++++++------
 1 files changed, 30 insertions(+), 6 deletions(-)
mm/slab.c +30 -6
@@ -3399,12 +3399,17 @@ EXPORT_SYMBOL_GPL(kmem_cache_name);
 static int alloc_kmemlist(struct kmem_cache *cachep)
 {
 	int node;
+	int count = -1;
 	struct kmem_list3 *l3;
-	int err = 0;
+	struct array_cache *new;
+	struct array_cache **new_alien;
 
 	for_each_online_node(node) {
-		struct array_cache *nc = NULL, *new;
-		struct array_cache **new_alien = NULL;
+		struct array_cache *nc = NULL;
+
+		new = NULL;
+		new_alien = NULL;
+		count++;
 #ifdef CONFIG_NUMA
 		new_alien = alloc_alien_cache(node, cachep->limit);
 		if (!new_alien)
@@ -3447,10 +3452,29 @@ static int alloc_kmemlist(struct kmem_ca
 					cachep->batchcount + cachep->num;
 		cachep->nodelists[node] = l3;
 	}
-	return err;
+	return 0;
+/**
+ * If one or more allocations fail we need to undo all allocations done up to
+ * this point. Unfortunately this means yet another loop, but since this only
+ * happens on failure and frees up memory in a memory-tight situation, it's
+ * not too bad.
+ */
 fail:
-	err = -ENOMEM;
-	return err;
+	kfree(new);
+	free_alien_cache(new_alien);
+	for_each_online_node(node) {
+		if (count <= 0)
+			break;
+		l3 = cachep->nodelists[node];
+		if (l3) {
+			kfree(l3->shared);
+			free_alien_cache(l3->alien);
+			kfree(l3);
+			cachep->nodelists[node] = NULL;
+		}
+		count--;
+	}
+	return -ENOMEM;
 }
 
 struct ccupdate_struct {
#4 lore raw

Re: [PATCH] fix memory leak in mm/slab.c::alloc_kmemlist() (try #3)

mm/slab.c EXPORT_SYMBOL_GPL(kmem_cache_name); · line 3402

Quoted 7 lines · Jesper Juhl
mm/slab.c +1 -0
@@ -3399,12 +3399,17 @@ EXPORT_SYMBOL_GPL(kmem_cache_name);
 static int alloc_kmemlist(struct kmem_cache *cachep)
 {
 	int node;
+	int count = -1;

Count? One could simply go backwards on the node and undo all allocations for present nodes. That may be much simpler.

while (node >= 0 && node_isset(node, node_online_map) {

....

	node--;
}

mm/slab.c EXPORT_SYMBOL_GPL(kmem_cache_name); · line 3403

Quoted 2 lines
mm/slab.c +0 -1
 	struct kmem_list3 *l3;
-	int err = 0;

Ok. err is removed sinc we never set it to a nonzero value.

mm/slab.c EXPORT_SYMBOL_GPL(kmem_cache_name); · line 3405

Quoted 2 lines
mm/slab.c +2 -0
+	struct array_cache *new;
+	struct array_cache **new_alien;

Ok. Also not checked.

#5 lore raw

Re: [PATCH] fix memory leak in mm/slab.c::alloc_kmemlist() (try #3)

In reply to Jesper Juhl
Quoted 3 lines · Jesper Juhl

Fix memory leak in mm/slab.c::alloc_kmemlist().
If one allocation fails we have to roll-back all allocations made up to the point of failure.

Sorry but you cannot roll back. alloc_kmemlist() could have been used for tuning the cpucache while accesses to the slab continue. "Rolling back" would partially destroy the slab for some nodes and likely cause the system to crash. We can only roll back if this is actually an initial allocation and we are assured that the whole thing is not yet in use.

#6 lore raw

Re: [PATCH] fix memory leak in mm/slab.c::alloc_kmemlist() (try #3)

Quoted 7 lines · Christoph Lameter
Quoted 3 lines · Jesper Juhl

Fix memory leak in mm/slab.c::alloc_kmemlist().
If one allocation fails we have to roll-back all allocations made up to the point of failure.

Quoted 5 lines

Sorry but you cannot roll back. alloc_kmemlist() could have been used for tuning the cpucache while accesses to the slab continue. "Rolling back" would partially destroy the slab for some nodes and likely cause the system to crash. We can only roll back if this is actually an initial allocation and we are assured that the whole thing is not yet in use.

Well that's a big pickle. How about allocating everything first, saving it locally then, if that all worked out, install it?

#7 lore raw

Re: [PATCH] fix memory leak in mm/slab.c::alloc_kmemlist() (try #3)

Quoted 2 lines · Andrew Morton

Well that's a big pickle. How about allocating everything first, saving it locally then, if that all worked out, install it?

That wont work for the tuning case. We need to know somehow if we are updating or creating a new kmemlist.

But there is so much messy stuff in there. Maybe we should start with a cleanup patch to increase our comprehension of the situation?

alloc_kmemlist: Some cleanup in preparation for a real memory leak fix

Inspired by Jesper Juhl's patch from today

1. Get rid of err
We do not set it to anything else but zero.

2. Drop the CONFIG_NUMA stuff.
There are definitions for alloc_alien_cache and free_alien_cache() that do the right thing for the non NUMA case.

3. Better naming of variables.

4. Remove redundant cachep->nodelists[node] expressions.

mm/slab.c +17 -17
Index: linux-2.6.16-rc6-mm2/mm/slab.c
===================================================================
@@ -3421,37 +3421,38 @@ static int alloc_kmemlist(struct kmem_ca
 {
 	int node;
 	struct kmem_list3 *l3;
-	int err = 0;
+	struct array_cache *new_shared;
+	struct array_cache **new_alien;
 
 	for_each_online_node(node) {
-		struct array_cache *nc = NULL, *new;
-		struct array_cache **new_alien = NULL;
-#ifdef CONFIG_NUMA
+
 		new_alien = alloc_alien_cache(node, cachep->limit);
 		if (!new_alien)
 			goto fail;
-#endif
-		new = alloc_arraycache(node, cachep->shared*cachep->batchcount,
+
+		new_shared = alloc_arraycache(node, cachep->shared*cachep->batchcount,
 					0xbaadf00d);
-		if (!new)
+		if (!new_shared)
 			goto fail;
+
 		l3 = cachep->nodelists[node];
 		if (l3) {
+			struct array_cache *shared = l3->shared;
+
 			spin_lock_irq(&l3->list_lock);
 
-			nc = cachep->nodelists[node]->shared;
-			if (nc)
-				free_block(cachep, nc->entry, nc->avail, node);
+			if (shared)
+				free_block(cachep, shared->entry, shared->avail, node);
 
-			l3->shared = new;
-			if (!cachep->nodelists[node]->alien) {
+			l3->shared = new_shared;
+			if (!l3->alien) {
 				l3->alien = new_alien;
 				new_alien = NULL;
 			}
 			l3->free_limit = (1 + nr_cpus_node(node)) *
 					cachep->batchcount + cachep->num;
 			spin_unlock_irq(&l3->list_lock);
-			kfree(nc);
+			kfree(shared);
 			free_alien_cache(new_alien);
 			continue;
 		}
@@ -3462,16 +3463,15 @@ static int alloc_kmemlist(struct kmem_ca
 		kmem_list3_init(l3);
 		l3->next_reap = jiffies + REAPTIMEOUT_LIST3 +
 				((unsigned long)cachep) % REAPTIMEOUT_LIST3;
-		l3->shared = new;
+		l3->shared = new_shared;
 		l3->alien = new_alien;
 		l3->free_limit = (1 + nr_cpus_node(node)) *
 					cachep->batchcount + cachep->num;
 		cachep->nodelists[node] = l3;
 	}
-	return err;
+	return 0;
 fail:
-	err = -ENOMEM;
-	return err;
+	return -ENOMEM;
 }
 
 struct ccupdate_struct {
#8 lore raw

Re: [PATCH] fix memory leak in mm/slab.c::alloc_kmemlist() (try #3)

In reply to Andrew Morton

Maybe it will work this way? (Testing may be difficult without proper failure scenarios, not tested yet).

Fix memory leak in alloc_kmemlist

We have had this memory leak for awhile now. The situation is complicated by the use of alloc_kmemlist() as a function to resize various caches by do_tune_cpucache().

What we do here is first of all make sure that we deallocate properly in the loop over all the nodes.

If we are just resizing caches then we can simply return with -ENOMEM if an allocation fails.

If the cache is new then we need to rollback and remove all earlier allocations.

We detect that a cache is new by checking if the link to the global cache chain has been setup. This is a bit hackish ....

(also fix up too overlong lines that I added in the last patch...)

mm/slab.c +26 -5
Index: linux-2.6.16-rc6-mm2/mm/slab.c
===================================================================
@@ -3415,7 +3415,7 @@ const char *kmem_cache_name(struct kmem_
 EXPORT_SYMBOL_GPL(kmem_cache_name);
 
 /*
- * This initializes kmem_list3 for all nodes.
+ * This initializes kmem_list3 or resizes varioius caches for all nodes.
  */
 static int alloc_kmemlist(struct kmem_cache *cachep)
 {
@@ -3430,10 +3430,13 @@ static int alloc_kmemlist(struct kmem_ca
 		if (!new_alien)
 			goto fail;
 
-		new_shared = alloc_arraycache(node, cachep->shared*cachep->batchcount,
+		new_shared = alloc_arraycache(node,
+				cachep->shared*cachep->batchcount,
 					0xbaadf00d);
-		if (!new_shared)
+		if (!new_shared) {
+			free_alien_cache(new_alien);
 			goto fail;
+		}
 
 		l3 = cachep->nodelists[node];
 		if (l3) {
@@ -3442,7 +3445,8 @@ static int alloc_kmemlist(struct kmem_ca
 			spin_lock_irq(&l3->list_lock);
 
 			if (shared)
-				free_block(cachep, shared->entry, shared->avail, node);
+				free_block(cachep, shared->entry,
+						shared->avail, node);
 
 			l3->shared = new_shared;
 			if (!l3->alien) {
@@ -3457,8 +3461,11 @@ static int alloc_kmemlist(struct kmem_ca
 			continue;
 		}
 		l3 = kmalloc_node(sizeof(struct kmem_list3), GFP_KERNEL, node);
-		if (!l3)
+		if (!l3) {
+			free_alien_cache(new_alien);
+			kfree(new_shared);
 			goto fail;
+		}
 
 		kmem_list3_init(l3);
 		l3->next_reap = jiffies + REAPTIMEOUT_LIST3 +
@@ -3470,7 +3477,21 @@ static int alloc_kmemlist(struct kmem_ca
 		cachep->nodelists[node] = l3;
 	}
 	return 0;
+
 fail:
+	if (!cachep->next.next) {
+		/* Cache is not active yet. Roll back what we did */
+		node--;
+		while (node >= 0 && cachep->nodelists[node]) {
+			l3 = cachep->nodelists[node];
+
+			kfree(l3->shared);
+			free_alien_cache(l3->alien);
+			kfree(l3);
+			cachep->nodelists[node] = NULL;
+			node--;
+		}
+	}
 	return -ENOMEM;
 }