SLUB: Fix merged slab cache names
Subsystems (2)
PATCH 1/2 SLUB: Fix merged slab cache names
As explained by Linus "I'm Proud to be an American" Torvalds:
Looking at the merging code, I actually think it's totally buggy. If you have something like this:
- load module A: create slab cache A
- load module B: create slab cache B that can merge with A
- unload module A
- "cat /proc/slabinfo": BOOM. Oops.
exactly because the name is not handled correctly, and you'll have module B holding open a slab cache that has a name pointer that points to module A that no longer exists.
This patch fixes the problem by introducing a SLAB_DYNAMIC_NAME flag and using kstrdup() to allocate memory when reference count is bumped up.
--- include/linux/slab.h | 2 ++ mm/slub.c | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 0 deletions(-)
include/linux/slab.h +2 -0
@@ -76,6 +76,8 @@# define SLAB_FAILSLAB 0x00000000UL#endif +#define SLAB_DYNAMIC_NAME 0x04000000UL/* s->name is kmalloc()'d */ +/* The following flags affect the page allocator grouping pages by mobility */ #define SLAB_RECLAIM_ACCOUNT 0x00020000UL/* Objects are reclaimable */ #define SLAB_TEMPORARY SLAB_RECLAIM_ACCOUNT/* Objects are short-lived */
mm/slub.c +15 -0
@@ -210,6 +210,9 @@ static inline int sysfs_slab_alias(struct kmem_cache *s, const char *p) {return 0 ; }static inline void sysfs_slab_remove (struct kmem_cache *s) { +if (s->flags &SLAB_DYNAMIC_NAME ) +kfree (s->name); +kfree (s); }
@@ -3218,6 +3221,18 @@ struct kmem_cache *kmem_cache_create(const char *name, size_t size,down_write (&slub_lock); s =find_mergeable (size, align, flags, name, ctor);if (s) { +if (!(s->flags &SLAB_DYNAMIC_NAME )) { +const char *new_name; + + new_name =kstrdup (s->name,GFP_KERNEL ); +if (!new_name) + goto err; + + s->name = new_name; + + s->flags |=SLAB_DYNAMIC_NAME ; + } + s->refcount++;/* * Adjust the object sizes so that we clear
Signature · 1 line
1.6.3.3
PATCH 2/2 SLUB: Mark merged slab caches in /proc/slabinfo
SLUB uses the name of the first slab cache for all merged slab caches. To make the output of /proc/slabinfo more obvious, append the name of each merged slab cache to s->name.
An example output looks like this:
bip-16,ip_dst_cache,kioctx 42 42 [snip]
--- mm/slub.c | 41 ++++++++++++++++++++++++++++++----------- 1 files changed, 30 insertions(+), 11 deletions(-)
mm/slub.c +30 -11
@@ -3149,6 +3149,34 @@ void __init kmem_cache_init_late(void) { } +static int kmem_merge_names (struct kmem_cache *s,const char *name) +{ +size_t new_size; +char *new_name; + +/* Don't append name to merged name more than once */ +if (strstr (s->name, name)) +return 0 ; + +/* Comma separated and NULL terminated. */ + new_size =strlen (s->name) +strlen (name) +2 ; + + new_name =kmalloc (new_size,GFP_KERNEL ); +if (!new_name) +return -ENOMEM ; + +snprintf (new_name, new_size,"%s,%s" , s->name, name); + +if (s->flags &SLAB_DYNAMIC_NAME ) +kfree (s->name); + + s->name = new_name; + + s->flags |=SLAB_DYNAMIC_NAME ; + +return 0 ; +} +/* * Find a mergeable slab cache */
@@ -3221,17 +3249,8 @@ struct kmem_cache *kmem_cache_create(const char *name, size_t size,down_write (&slub_lock); s =find_mergeable (size, align, flags, name, ctor);if (s) { -if (!(s->flags &SLAB_DYNAMIC_NAME )) { -const char *new_name; - - new_name =kstrdup (s->name,GFP_KERNEL ); -if (!new_name) - goto err; - - s->name = new_name; - - s->flags |=SLAB_DYNAMIC_NAME ; - } +if (kmem_merge_names (s, name)) + goto err; s->refcount++;/*
Signature · 1 line
1.6.3.3
Re: [PATCH 2/2] SLUB: Mark merged slab caches in /proc/slabinfo
Quoted 4 lines · Pekka Enberg
SLUB uses the name of the first slab cache for all merged slab caches. To make the output of
/proc/slabinfomore obvious, append the name of each merged slab cache tos->name.
Quoted 8 lines
An example output looks like this:
bip-16,ip_dst_cache,kioctx 42 42 [snip]
Cc Christoph Lameter <[email protected]>Cc David Rientjes <[email protected]>Reported-by Linus Torvalds <[email protected]>Signed-off-by Pekka Enberg <[email protected]>
Unfortunately this breaks "slabtop". I guess we could try some other delimiter?
Quoted 72 lines
---
mm/slub.c | 41 ++++++++++++++++++++++++++++++-----------1 files changed, 30 insertions(+), 11 deletions(-)mm/slub.c +30
-12@@ -3149,6 +3149,34 @@ void __init kmem_cache_init_late(void) { } +static int kmem_merge_names (struct kmem_cache *s,const char *name) +{ + size_t new_size; + char *new_name; + + /* Don't append name to merged name more than once */ + if (strstr(s->name, name)) + return 0; + + /* Comma separated and NULL terminated. */ + new_size = strlen(s->name) + strlen(name) + 2; + + new_name = kmalloc(new_size, GFP_KERNEL); + if (!new_name) + return -ENOMEM; + + snprintf(new_name, new_size, "%s,%s", s->name, name); + + if (s->flags & SLAB_DYNAMIC_NAME) + kfree(s->name); + + s->name = new_name; + + s->flags |= SLAB_DYNAMIC_NAME; + + return 0; +} + /** Find a mergeable slab cache */ @@ -3221,17 +3249,8 @@ struct kmem_cache *kmem_cache_create(const char *name, size_t size, down_write(&slub_lock); s = find_mergeable(size, align, flags, name, ctor); if (s) { - if (!(s->flags & SLAB_DYNAMIC_NAME)) { - const char *new_name; - - new_name = kstrdup(s->name, GFP_KERNEL); - if (!new_name) - goto err; - - s->name = new_name; - - s->flags |= SLAB_DYNAMIC_NAME; - } + if (kmem_merge_names(s, name)) + goto err; s->refcount++; /* -- 1.6.3.3
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Re: [PATCH 2/2] SLUB: Mark merged slab caches in /proc/slabinfo
Quoted 1 line · Pekka Enberg
Unfortunately this breaks "slabtop". I guess we could try some other delimiter?
+ ?
Re: [PATCH 2/2] SLUB: Mark merged slab caches in /proc/slabinfo
Quoted 1 line · Pekka Enberg
Quoted 1 line
Unfortunately this breaks "slabtop". I guess we could try some other delimiter?
Quoted 1 line · Christoph Lameter
+ ?
Looking at procps sources, slabtop and other utilities seem to think slab cache names are limited to 64 bytes. Linus, are you OK with adding a "+" character prefix for merged caches like you suggested in private?
Pekka
Re: [PATCH 2/2] SLUB: Mark merged slab caches in /proc/slabinfo
I didn't see this before I replied to the 1/2 patch..
Quoted 10 lines · Pekka Enberg
Quoted 1 line · Pekka Enberg
Quoted 1 line
Unfortunately this breaks "slabtop". I guess we could try some other delimiter?
Quoted 1 line · Christoph Lameter
+ ?
Looking at procps sources, slabtop and other utilities seem to think slab cache names are limited to 64 bytes. Linus, are you OK with adding a "+" character prefix for merged caches like you suggested in private?
I'd much rather see "x+y+z" and actually see what the slab entries are.
But we can certainly also say "limit it to 64 bytes" and truncate things past that.
Linus
Re: [PATCH 2/2] SLUB: Mark merged slab caches in /proc/slabinfo
Quoted 9 lines · Linus Torvalds
Quoted 4 lines
Looking at procps sources, slabtop and other utilities seem to think slab cache names are limited to 64 bytes. Linus, are you OK with adding a "+" character prefix for merged caches like you suggested in private?
I'd much rather see "x+y+z" and actually see what the slab entries are.
But we can certainly also say "limit it to 64 bytes" and truncate things past that.
It's actually 63 bytes due to off-by-one error in procps. There never has been such limit on the kernel side but I worry about breaking other applications that are prepared to read even less characters. Oh well, I'll send the reworked patches and let you decide.
Pekka
include/linux/slab.h line 80
Quoted 10 lines · Pekka Enberg
include/linux/slab.h +2
-0@@ -76,6 +76,8 @@# define SLAB_FAILSLAB 0x00000000UL#endif +#define SLAB_DYNAMIC_NAME 0x04000000UL/* s->name is kmalloc()'d */ +
Put this into mm/slub.c as slub only flag? What is the difference from refcount == 1?
include/linux/slab.h struct kmem_cache *kmem_cache_create(const char *name, size_t size, · line 3227
Quoted 8 lines
include/linux/slab.h +4
-0@@ -3218,6 +3221,18 @@ struct kmem_cache *kmem_cache_create(const char *name, size_t size,down_write (&slub_lock); s =find_mergeable (size, align, flags, name, ctor);if (s) { +if (!(s->flags &SLAB_DYNAMIC_NAME )) { +const char *new_name; + + new_name =kstrdup (s->name,GFP_KERNEL );
Ok. Keeping the original name. Why dont we do strdup by default and always do a kfree(s->name) on close?
Quoted 10 lines · Christoph Lameter
Quoted 8 lines · Pekka Enberg
include/linux/slab.h +0
-0@@ -76,6 +76,8 @@# define SLAB_FAILSLAB 0x00000000UL#endif
Quoted 4 lines
include/linux/slab.h line 80
Quoted 2 lines
include/linux/slab.h +2
-0+#define SLAB_DYNAMIC_NAME 0x04000000UL/* s->name is kmalloc()'d */ +Put this into
mm/slub.cas slub only flag? What is the difference from refcount == 1?
I can put it in mm/slub.c but I was worried about someone reusing the bit for something else.
Do you mean refcount == 2? You don't know during kmem cache release time if someone was merged to the cache or not.
Quoted 11 lines
include/linux/slab.h struct kmem_cache *kmem_cache_create(const char *name, size_t size, · line 3227
Quoted 8 lines
include/linux/slab.h +4
-0@@ -3218,6 +3221,18 @@ struct kmem_cache *kmem_cache_create(const char *name, size_t size,down_write (&slub_lock); s =find_mergeable (size, align, flags, name, ctor);if (s) { +if (!(s->flags&SLAB_DYNAMIC_NAME )) { +const char *new_name; + + new_name =kstrdup (s->name,GFP_KERNEL );Ok. Keeping the original name. Why dont we do strdup by default and always do a
kfree(s->name)on close?
I tried that. It gets very nasty during bootstrap.
Quoted 4 lines · Pekka Enberg
Quoted 2 lines
Put this into
mm/slub.cas slub only flag? What is the difference from refcount == 1?I can put it in
mm/slub.cbut I was worried about someone reusing the bit for something else.
Allocate from the other end like __OBJECT_POISON.
Quoted 7 lines
Do you mean refcount == 2? You don't know during kmem cache release time if someone was merged to the cache or not.
Quoted 3 lines
Ok. Keeping the original name. Why dont we do strdup by default and always do a
kfree(s->name)on close?I tried that. It gets very nasty during bootstrap.
Add it only to kmem_cache_create() not to kmem_cache_open. That is not used during bootstrap. The bootstrap caches do not matter since they are never freed.
Quoted 7 lines · Christoph Lameter
Quoted 4 lines · Pekka Enberg
Quoted 2 lines
Put this into
mm/slub.cas slub only flag? What is the difference from refcount == 1?I can put it in
mm/slub.cbut I was worried about someone reusing the bit for something else.Allocate from the other end like
__OBJECT_POISON.
I'll do that. Thanks!
Quoted 9 lines
Quoted 6 lines
Do you mean refcount == 2? You don't know during kmem cache release time if someone was merged to the cache or not.
Quoted 3 lines
Ok. Keeping the original name. Why dont we do strdup by default and always do a
kfree(s->name)on close?I tried that. It gets very nasty during bootstrap.
Add it only to
kmem_cache_create()not tokmem_cache_open. That is not used during bootstrap. The bootstrap caches do not matter since they are never freed
I tried that too. It doesn't work because we get merged to kmalloc caches and can't do kfree() on them.
Pekka
Quoted 7 lines · Pekka Enberg
Quoted 7 lines
Quoted 7 lines
Do you mean refcount == 2? You don't know during kmem cache release time if
someone was merged to the cache or not.Quoted 4 lines
Ok. Keeping the original name. Why dont we do strdup by default and always
do akfree(s->name)on close?
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
I tried that. It gets very nasty during bootstrap.
Add it only to
kmem_cache_create()not tokmem_cache_open. That is not used during bootstrap. The bootstrap caches do not matter since they are never freedI tried that too. It doesn't work because we get merged to kmalloc caches and can't do
kfree()on them.
For most of the kmalloc caches we are already doing a kasprintf. See the last for loop in kmem_cache_init(). There are a couple of cases where we are not doing it right now. kmalloc-96 kmalloc-192 and kmem_cache_node.
mm/slub.c struct kmem_cache *kmem_cache_create(const char *name, size_t size, · line 3231
Quoted 13 lines · Pekka Enberg
mm/slub.c +8
-0@@ -3218,6 +3221,18 @@ struct kmem_cache *kmem_cache_create(const char *name, size_t size, down_write(&slub_lock); s = find_mergeable(size, align, flags, name, ctor); if (s) { + if (!(s->flags & SLAB_DYNAMIC_NAME)) { + const char *new_name; + + new_name = kstrdup(s->name, GFP_KERNEL); + if (!new_name) + goto err; + + s->name = new_name;
Once you do this, please actually update the name to list the first few different names.
Since the SLAB_DYNAMIC_NAME test mean that this will only happen once, I would suggest something like just doing
size = strlen(s->name) + strlen(name) + sizeof(", ...");
new_name = kmalloc(size, GFP_KERNEL);
if (!new_name)
return -ENOMEM;
snprintf(new_name, "%s, %s...", s->name, name);so that you get the two first names listed. Sure, it will be wrong/incomplete if there's lots of merging, but it will be visually very obvious in /proc/slabinfo and in the debug messages.
Linus