Skip to contentSkip to conversation

slab: clean up kmem_cache_create_memcg() error handling

DISCUSSION Dave Jones 2 participants 2 messages Jan 24, 2014 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

Dave Jones

author
#1 lore raw
In reply to a message not in the archive
Quoted 17 lines · Linux Kernel
Gitweb:     http://git.kernel.org/linus/;a=commit;h=3965fc3652244651006ebb31c8c45318ce84818f
Commit:     3965fc3652244651006ebb31c8c45318ce84818f
Parent:     309381feaee564281c3d9e90fbca8963bb7428ad
Author:     Vladimir Davydov <[email protected]>
AuthorDate: Thu Jan 23 15:52:55 2014 -0800
Committer:  Linus Torvalds <[email protected]>
CommitDate: Thu Jan 23 16:36:50 2014 -0800

slab: clean up kmem_cache_create_memcg() error handling

Currently kmem_cache_create_memcg() backoffs on failure inside conditionals, without using gotos. This results in the rollback code duplication, which makes the function look cumbersome even though on error we should only free the allocated cache. Since in the next patch I am going to add yet another rollback function call on error path there, let's employ labels instead of conditionals for undoing any changes on failure to keep things clean.

...

Quoted 14 lines
(quoted diff) +1 -2
+out_unlock:
 	mutex_unlock(&slab_mutex);
 	put_online_cpus();
 
 	if (err) {
-
 		if (flags & SLAB_PANIC)
 			panic("kmem_cache_create: Failed to create slab '%s'. Error %d\n",
 				name, err);
@@ -236,11 +230,14 @@ out_locked:
 				name, err);
 			dump_stack();
 		}
-

Review out_locked: · line 241

Quoted 10 lines
(quoted diff) +5 -1
 		return NULL;
 	}
-
 	return s;
+
+out_free_cache:
+	kfree(s->name);
+	kmem_cache_free(kmem_cache, s);
+	goto out_unlock;
 }

This is now returning a freed pointer as 's' if an error occurs. Perhaps the patch below ?

Dave

mm/slab_common.c +1 -0
index 8e40321da091..2c62294cee23 100644
@@ -257,6 +257,7 @@ out_free_cache:
 	memcg_free_cache_params(s);
 	kfree(s->name);
 	kmem_cache_free(kmem_cache, s);
+	s = NULL;
 	goto out_unlock;
 }
 
#2 lore raw
Quoted 39 lines · Dave Jones
Quoted 17 lines · Linux Kernel
Gitweb:     http://git.kernel.org/linus/;a=commit;h=3965fc3652244651006ebb31c8c45318ce84818f
Commit:     3965fc3652244651006ebb31c8c45318ce84818f
Parent:     309381feaee564281c3d9e90fbca8963bb7428ad
Author:     Vladimir Davydov <[email protected]>
AuthorDate: Thu Jan 23 15:52:55 2014 -0800
Committer:  Linus Torvalds <[email protected]>
CommitDate: Thu Jan 23 16:36:50 2014 -0800

slab: clean up kmem_cache_create_memcg() error handling

Currently kmem_cache_create_memcg() backoffs on failure inside conditionals, without using gotos. This results in the rollback code duplication, which makes the function look cumbersome even though on error we should only free the allocated cache. Since in the next patch I am going to add yet another rollback function call on error path there, let's employ labels instead of conditionals for undoing any changes on failure to keep things clean.

...

Quoted 18 lines
(quoted diff) +1 -3
+out_unlock:
 	mutex_unlock(&slab_mutex);
 	put_online_cpus();
 
 	if (err) {
-
 		if (flags & SLAB_PANIC)
 			panic("kmem_cache_create: Failed to create slab '%s'. Error %d\n",
 				name, err);
@@ -236,11 +230,14 @@ out_locked:
 				name, err);
 			dump_stack();
 		}
-
 		return NULL;
 	}
-
 	return s;
Quoted 8 lines

Review out_locked: · line 241

Quoted 6 lines
(quoted diff) +5 -0
+
+out_free_cache:
+	kfree(s->name);
+	kmem_cache_free(kmem_cache, s);
+	goto out_unlock;
 }

This is now returning a freed pointer as 's' if an error occurs.

If we go to out_free_cache, we set err, and since under out_unlock we have:

Quoted 1 line

if (err) {

...

Quoted 2 lines

return NULL
}

we will return NULL, which is right.

However this behavior was broken by another my 'fix' :-(

commit f717eb3abb5ea38f60e671dbfdbf512c2c93d22e
slab: do not panic if we fail to create memcg cache

Quoted 12 lines
(quoted diff) +8 -1
-    if (err) {
+    /*
+     * There is no point in flooding logs with warnings or especially
+     * crashing the system if we fail to create a cache for a memcg. In
+     * this case we will be accounting the memcg allocation to the root
+     * cgroup until we succeed to create its own cache, but it isn't that
+     * critical.
+     */
+    if (err && !memcg) {
         if (flags & SLAB_PANIC)
             panic("kmem_cache_create: Failed to create slab '%s'.

Error %d\n",

Quoted 1 line
                 name, err);

In case memcg != NULL we can return crap on error. So you are right in the end. Thank you for catching this!

mm/slab_common.c out_free_cache: · line 262

Quoted 13 lines

Perhaps the patch below ?

mm/slab_common.c +1 -0
index 8e40321da091..2c62294cee23 100644
@@ -257,6 +257,7 @@ out_free_cache:
 	memcg_free_cache_params(s);
 	kfree(s->name);
 	kmem_cache_free(kmem_cache, s);
+	s = NULL;
 	goto out_unlock;
 }

This one looks correct to me. I'll send it on behalf of you.

Thanks!