Skip to contentSkip to conversation

BUG: slub creates kmalloc slabs with refcount=0

PATCH In review Paul Hargrove 6 participants 11 messages Dec 25, 2012 – Jan 29, 2013

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

I have a 3.7.1 kernel on x86-86
It is configured with
CONFIG_SLUB=y
CONFIG_SLUB_DEBUG=y

I have an out-of-tree module calling KMEM_CACHE for an 8-byte struct:

        cr_pdata_cachep = KMEM_CACHE(cr_pdata_s,0);
        if (!cr_pdata_cachep) goto no_pdata_cachep;
        printk(KERN_ERR "@ refcount = %d name = '%s'\n",

cr_pdata_cachep->refcount, cr_pdata_cachep->name);

The output of the printk, below, shows that the request has been merged with the built-in 8-byte kmalloc pool, BUT the resulting refcount is 1, rather than 2 (or more):
@ refcount = 1 name = 'kmalloc-8'

This results in a very unhappy kernel when the module calls

    kmem_cache_destroy(cr_pdata_cachep);

at rmmod time, resulting is messages like
BUG kmalloc-8 (Tainted: G O): Objects remaining in kmalloc-96
on kmem_cache_close()

A quick look through mm/slub.c appears to confirm my suspicion that "s->refcount" is never incremented for the built-in kmalloc-* caches.
However, I leave it to the experts to determine where the increment
belongs.

FWIW: I am currently passing SLAB_POISON for the flags argument to KMEM_CACHE() as a work-around (it prevents merging and, if I understand correctly, has no overhead in a non-debug build).

-Paul

Signature · 4 lines
Paul H. Hargrove                          [email protected]
Future Technologies Group
Computer and Data Sciences Department     Tel: +1-510-495-2352
Lawrence Berkeley National Laboratory     Fax: +1-510-486-6900
#1 lore raw

Just to be clear the "BUG...." line I quoted contains an error of my own.

It should say "kmalloc-8" twice rather than having "kmalloc-96" toward the end. This is the result of a cut-and-paste error (and brain already on vacation). The module ALSO has a larger struct the cache for which gets merged with the kmalloc-96 cache. I grabbed the first BUG line from the dmesg output and replaced "96" with "8" when composing the email, but missed the second occurrence.

Merry Christmas,
-Paul

Quoted 40 lines · Paul Hargrove

I have a 3.7.1 kernel on x86-86
It is configured with
CONFIG_SLUB=y
CONFIG_SLUB_DEBUG=y

I have an out-of-tree module calling KMEM_CACHE for an 8-byte struct:

        cr_pdata_cachep = KMEM_CACHE(cr_pdata_s,0);
        if (!cr_pdata_cachep) goto no_pdata_cachep;
        printk(KERN_ERR "@ refcount = %d name = '%s'\n",

cr_pdata_cachep->refcount, cr_pdata_cachep->name);

The output of the printk, below, shows that the request has been merged with the built-in 8-byte kmalloc pool, BUT the resulting refcount is 1, rather than 2 (or more):
@ refcount = 1 name = 'kmalloc-8'

This results in a very unhappy kernel when the module calls

    kmem_cache_destroy(cr_pdata_cachep);

at rmmod time, resulting is messages like
BUG kmalloc-8 (Tainted: G O): Objects remaining in
kmalloc-96 on kmem_cache_close()

A quick look through mm/slub.c appears to confirm my suspicion that "s->refcount" is never incremented for the built-in kmalloc-* caches.
However, I leave it to the experts to determine where the increment
belongs.

FWIW: I am currently passing SLAB_POISON for the flags argument to KMEM_CACHE() as a work-around (it prevents merging and, if I understand correctly, has no overhead in a non-debug build).

-Paul

--
Paul H. Hargrove [email protected]
Future Technologies Group
Computer and Data Sciences Department Tel: +1-510-495-2352
Lawrence Berkeley National Laboratory Fax: +1-510-486-6900

Signature · 4 lines
Paul H. Hargrove                          [email protected]
Future Technologies Group
Computer and Data Sciences Department     Tel: +1-510-495-2352
Lawrence Berkeley National Laboratory     Fax: +1-510-486-6900
#2 lore raw

PATCH 1/1 slub: assign refcount for kmalloc_caches

In reply to Paul Hargrove

commit cce89f4f6911286500cf7be0363f46c9b0a12ce0('Move kmem_cache refcounting to common code') moves some refcount manipulation code to common code. Unfortunately, it also removed refcount assignment for kmalloc_caches. So, kmalloc_caches's refcount is initially 0. This makes errornous situation.

Paul Hargrove report that when he create a 8-byte kmem_cache and destory it, he encounter below message.
'Objects remaining in kmalloc-8 on kmem_cache_close()'

8-byte kmem_cache merge with 8-byte kmalloc cache and refcount is increased by one. So, resulting refcount is 1. When destory it, it hit refcount = 0, then kmem_cache_close() is executed and error message is printed.

This patch assign initial refcount 1 to kmalloc_caches, so fix this errornous situtation.

mm/slub.c +1 -0
index a0d6984..321afab 100644
@@ -3279,6 +3279,7 @@ static struct kmem_cache *__init create_kmalloc_cache(const char *name,
 	if (kmem_cache_open(s, flags))
 		goto panic;
 
+	s->refcount = 1;
 	list_add(&s->list, &slab_caches);
 	return s;
 
Signature · 7 lines
1.7.9.5

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#3 lore raw

Re: [PATCH] slub: assign refcount for kmalloc_caches

2012/12/26 Joonsoo Kim <[email protected]>:

Quoted 36 lines

commit cce89f4f6911286500cf7be0363f46c9b0a12ce0('Move kmem_cache refcounting to common code') moves some refcount manipulation code to common code. Unfortunately, it also removed refcount assignment for kmalloc_caches. So, kmalloc_caches's refcount is initially 0. This makes errornous situation.

Paul Hargrove report that when he create a 8-byte kmem_cache and destory it, he encounter below message.
'Objects remaining in kmalloc-8 on kmem_cache_close()'

8-byte kmem_cache merge with 8-byte kmalloc cache and refcount is increased by one. So, resulting refcount is 1. When destory it, it hit refcount = 0, then kmem_cache_close() is executed and error message is printed.

This patch assign initial refcount 1 to kmalloc_caches, so fix this errornous situtation.

mm/slub.c +1 -1
index a0d6984..321afab 100644
@@ -3279,6 +3279,7 @@ static struct kmem_cache *__init create_kmalloc_cache(const char *name,
        if (kmem_cache_open(s, flags))
                goto panic;
 
+       s->refcount = 1;
        list_add(&s->list, &slab_caches);
        return s;
 
--
Quoted 2 lines

1.7.9.5

I missed some explanation.
In v3.8-rc1, this problem is already solved.
See create_kmalloc_cache() in mm/slab_common.c.
So this patch is just for v3.7 stable.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#4 lore raw

Re: [PATCH] slub: assign refcount for kmalloc_caches

I just had a look at patch-3.7.2-rc1, and this change doesn't appear to have made it in yet.
Am I missing something?

-Paul

Quoted 46 lines · JoonSoo Kim

2012/12/26 Joonsoo Kim <[email protected]>:

mm/slub.c static struct kmem_cache *__init

Quoted 28 lines

commit cce89f4f6911286500cf7be0363f46c9b0a12ce0('Move kmem_cache refcounting to common code') moves some refcount manipulation code to common code. Unfortunately, it also removed refcount assignment for kmalloc_caches. So, kmalloc_caches's refcount is initially 0. This makes errornous situation.

Paul Hargrove report that when he create a 8-byte kmem_cache and destory it, he encounter below message.
'Objects remaining in kmalloc-8 on kmem_cache_close()'

8-byte kmem_cache merge with 8-byte kmalloc cache and refcount is increased by one. So, resulting refcount is 1. When destory it, it hit refcount = 0, then kmem_cache_close() is executed and error message is printed.

This patch assign initial refcount 1 to kmalloc_caches, so fix this errornous situtation.

mm/slub.c +0 -0
index a0d6984..321afab 100644
@@ -3279,6 +3279,7 @@ static struct kmem_cache *__init

create_kmalloc_cache(const char *name,

Quoted 10 lines
mm/slub.c +1 -1
        if (kmem_cache_open(s, flags))
                goto panic;
 
+       s->refcount = 1;
        list_add(&s->list, &slab_caches);
        return s;
 
--

1.7.9.5

I missed some explanation.
In v3.8-rc1, this problem is already solved. See create_kmalloc_cache() in mm/slab_common.c. So this patch is just for v3.7 stable.

Signature · 4 lines
Paul H. Hargrove                          [email protected]
Future Technologies Group
Computer and Data Sciences Department     Tel: +1-510-495-2352
Lawrence Berkeley National Laboratory     Fax: +1-510-486-6900
#5 lore raw

Re: [PATCH] slub: assign refcount for kmalloc_caches

Quoted 5 lines · Paul Hargrove

I just had a look at patch-3.7.2-rc1, and this change doesn't appear to have made it in yet.
Am I missing something?

-Paul

I try to check it.
Ccing to Greg.

Hello, Pekka and Greg.

v3.8-rcX has already fixed by another stuff, but it is not simple change. So I made a new patch and sent it.

How this kind of patch (only for stable v3.7) go into stable tree? through Pekka's slab tree? or send it to Greg, directly?

I don't know how to submit this kind of patch to stable tree exactly. Could anyone help me?

Thanks.

Quoted 43 lines · JoonSoo Kim
Quoted 42 lines

2012/12/26 Joonsoo Kim <[email protected]>:

mm/slub.c static struct kmem_cache *__init

Quoted 28 lines

commit cce89f4f6911286500cf7be0363f46c9b0a12ce0('Move kmem_cache refcounting to common code') moves some refcount manipulation code to common code. Unfortunately, it also removed refcount assignment for kmalloc_caches. So, kmalloc_caches's refcount is initially 0. This makes errornous situation.

Paul Hargrove report that when he create a 8-byte kmem_cache and destory it, he encounter below message.
'Objects remaining in kmalloc-8 on kmem_cache_close()'

8-byte kmem_cache merge with 8-byte kmalloc cache and refcount is increased by one. So, resulting refcount is 1. When destory it, it hit refcount = 0, then kmem_cache_close() is executed and error message is printed.

This patch assign initial refcount 1 to kmalloc_caches, so fix this errornous situtation.

mm/slub.c +0 -0
index a0d6984..321afab 100644
@@ -3279,6 +3279,7 @@ static struct kmem_cache *__init

create_kmalloc_cache(const char *name,

Quoted 10 lines
mm/slub.c +1 -1
        if (kmem_cache_open(s, flags))
                goto panic;
 
+       s->refcount = 1;
        list_add(&s->list, &slab_caches);
        return s;
 
--

1.7.9.5

Quoted 13 lines
Quoted 5 lines

I missed some explanation.
In v3.8-rc1, this problem is already solved. See create_kmalloc_cache() in mm/slab_common.c. So this patch is just for v3.7 stable.

Signature · 4 lines
Paul H. Hargrove                          [email protected]
Future Technologies Group
Computer and Data Sciences Department     Tel: +1-510-495-2352
Lawrence Berkeley National Laboratory     Fax: +1-510-486-6900

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#6 lore raw

Re: [PATCH] slub: assign refcount for kmalloc_caches

Quoted 12 lines · Joonsoo Kim
Quoted 5 lines · Paul Hargrove

I just had a look at patch-3.7.2-rc1, and this change doesn't appear to have made it in yet.
Am I missing something?

-Paul

I try to check it.
Ccing to Greg.

Hello, Pekka and Greg.

Quoted 8 lines

v3.8-rcX has already fixed by another stuff, but it is not simple change. So I made a new patch and sent it.

How this kind of patch (only for stable v3.7) go into stable tree? through Pekka's slab tree? or send it to Greg, directly?

I don't know how to submit this kind of patch to stable tree exactly. Could anyone help me?

Please redo it, and send it to [email protected], and say exactly why it isn't in Linus's tree, and that it should only be applied to 3.7-stable.

thanks,

greg k-h

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#7 lore raw

Re: [PATCH] slub: assign refcount for kmalloc_caches

----- Original Message -----

Quoted 28 lines

From: "Greg Kroah-Hartman" <[email protected]>
To: "Joonsoo Kim" <[email protected]>
Cc: "Paul Hargrove" <[email protected]>, "Pekka Enberg" <[email protected]>, [email protected], [email protected], "Christoph Lameter" <[email protected]>
Sent: Tuesday, January 15, 2013 3:23:36 AM
Subject: Re: [PATCH] slub: assign refcount for kmalloc_caches

Quoted 20 lines · Joonsoo Kim
Quoted 6 lines · Paul Hargrove

I just had a look at patch-3.7.2-rc1, and this change doesn't appear to
have made it in yet.
Am I missing something?

-Paul

I try to check it.
Ccing to Greg.

Hello, Pekka and Greg.

v3.8-rcX has already fixed by another stuff, but it is not simple change.
So I made a new patch and sent it.

How this kind of patch (only for stable v3.7) go into stable tree? through Pekka's slab tree? or send it to Greg, directly?

Quoted 8 lines
Quoted 3 lines

I don't know how to submit this kind of patch to stable tree exactly.
Could anyone help me?

Please redo it, and send it to [email protected], and say exactly
why it isn't in Linus's tree, and that it should only be applied to 3.7-stable.

I also met this during the testing, so I'll re-send it then.

Quoted 11 lines

thanks,

greg k-h

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#8 lore raw

Re: [PATCH] slub: assign refcount for kmalloc_caches

Quoted 35 lines · CAI Qian

----- Original Message -----

Quoted 32 lines

From: "Greg Kroah-Hartman" <[email protected]>
To: "Joonsoo Kim" <[email protected]>
Cc: "Paul Hargrove" <[email protected]>, "Pekka Enberg" <[email protected]>, [email protected], [email protected], "Christoph Lameter" <[email protected]>
Sent: Tuesday, January 15, 2013 3:23:36 AM
Subject: Re: [PATCH] slub: assign refcount for kmalloc_caches

Quoted 23 lines · Joonsoo Kim
Quoted 6 lines · Paul Hargrove

I just had a look at patch-3.7.2-rc1, and this change doesn't appear to
have made it in yet.
Am I missing something?

-Paul

I try to check it.
Ccing to Greg.

Hello, Pekka and Greg.

v3.8-rcX has already fixed by another stuff, but it is not simple change.
So I made a new patch and sent it.

How this kind of patch (only for stable v3.7) go into stable tree? through Pekka's slab tree? or send it to Greg, directly?

I don't know how to submit this kind of patch to stable tree exactly.
Could anyone help me?

Quoted 5 lines
Quoted 4 lines

Please redo it, and send it to [email protected], and say exactly
why it isn't in Linus's tree, and that it should only be applied to 3.7-stable.

I also met this during the testing, so I'll re-send it then.

Hello, CAI Qian.

I totally forget this.
Thanks for this work.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

Acked-by on v1 1/1
#9 lore raw

Re: [PATCH] slub: assign refcount for kmalloc_caches

In reply to Joonsoo Kim
Quoted 2 lines · Joonsoo Kim

This patch assign initial refcount 1 to kmalloc_caches, so fix this errornous situtation.

Ok Only for 3.7:

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#10 lore raw
In reply to Paul Hargrove

Hello, Paul.

2012/12/25 Paul Hargrove <[email protected]>:

Quoted 31 lines

I have a 3.7.1 kernel on x86-86
It is configured with
CONFIG_SLUB=y
CONFIG_SLUB_DEBUG=y

I have an out-of-tree module calling KMEM_CACHE for an 8-byte struct:

        cr_pdata_cachep = KMEM_CACHE(cr_pdata_s,0);
        if (!cr_pdata_cachep) goto no_pdata_cachep;
        printk(KERN_ERR "@ refcount = %d name = '%s'\n",

cr_pdata_cachep->refcount, cr_pdata_cachep->name);

The output of the printk, below, shows that the request has been merged with the built-in 8-byte kmalloc pool, BUT the resulting refcount is 1, rather than 2 (or more):
@ refcount = 1 name = 'kmalloc-8'

This results in a very unhappy kernel when the module calls

    kmem_cache_destroy(cr_pdata_cachep);

at rmmod time, resulting is messages like
BUG kmalloc-8 (Tainted: G O): Objects remaining in kmalloc-96
on kmem_cache_close()

A quick look through mm/slub.c appears to confirm my suspicion that "s->refcount" is never incremented for the built-in kmalloc-* caches. However, I leave it to the experts to determine where the increment belongs.

FWIW: I am currently passing SLAB_POISON for the flags argument to KMEM_CACHE() as a work-around (it prevents merging and, if I understand correctly, has no overhead in a non-debug build).

Quoted 7 lines

-Paul

--
Paul H. Hargrove [email protected]
Future Technologies Group
Computer and Data Sciences Department Tel: +1-510-495-2352
Lawrence Berkeley National Laboratory Fax: +1-510-486-6900

My e-mail client's 'Reply to message ID' is not working properly. I sent a patch('slub:assign refcount for kmalloc_caches') for fixing this and Cc'ed you.

Thanks.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>