Skip to contentSkip to conversation

slub: fix a memory leak in get_partial_node()

PATCH Superseded Joonsoo Kim 4 participants 9 messages May 15, 2012 – May 18, 2012

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

PATCH 1/1 slub: fix a memory leak in get_partial_node()

In the case which is below,

1. acquire slab for cpu partial list
2. free object to it by remote cpu
3. page->freelist = t

then memory leak is occurred.

Change acquire_slab() not to zap freelist when it works for cpu partial list. I think it is a sufficient solution for fixing a memory leak.

Below is output of 'slabinfo -r kmalloc-256'
when './perf stat -r 30 hackbench 50 process 4000 > /dev/null' is done.

***Vanilla***
Sizes (bytes) Slabs Debug Memory
------------------------------------------------------------------------

Object :     256  Total  :     468   Sanity Checks : Off  Total: 3833856
SlabObj:     256  Full   :     111   Redzoning     : Off  Used : 2004992
SlabSiz:    8192  Partial:     302   Poisoning     : Off  Loss : 1828864
Loss   :       0  CpuSlab:      55   Tracking      : Off  Lalig:       0
Align  :       8  Objects:      32   Tracing       : Off  Lpadd:       0

***Patched***
Sizes (bytes) Slabs Debug Memory
------------------------------------------------------------------------

Object :     256  Total  :     300   Sanity Checks : Off  Total: 2457600
SlabObj:     256  Full   :     204   Redzoning     : Off  Used : 2348800
SlabSiz:    8192  Partial:      33   Poisoning     : Off  Loss :  108800
Loss   :       0  CpuSlab:      63   Tracking      : Off  Lalig:       0
Align  :       8  Objects:      32   Tracing       : Off  Lpadd:       0

Total and loss number is the impact of this patch.

mm/slub.c +6 -3
index ffe13fd..a7a766a 100644
@@ -1514,15 +1514,19 @@ static inline void *acquire_slab(struct kmem_cache *s,
 		freelist = page->freelist;
 		counters = page->counters;
 		new.counters = counters;
-		if (mode)
+		if (mode) {
 			new.inuse = page->objects;
+			new.freelist = NULL;
+		} else {
+			new.freelist = freelist;
+		}
 
 		VM_BUG_ON(new.frozen);
 		new.frozen = 1;
 
 	} while (!__cmpxchg_double_slab(s, page,
 			freelist, counters,
-			NULL, new.counters,
+			new.freelist, new.counters,
 			"lock and freeze"));
 
 	remove_partial(n, page);
@@ -1564,7 +1568,6 @@ static void *get_partial_node(struct kmem_cache *s,
 			object = t;
 			available =  page->objects - page->inuse;
 		} else {
-			page->freelist = t;
 			available = put_cpu_partial(s, page, 0);
 			stat(s, CPU_PARTIAL_NODE);
 		}
Signature · 1 line
1.7.9.5
#1 lore raw
Quoted 32 lines · Joonsoo Kim

In the case which is below,

1. acquire slab for cpu partial list
2. free object to it by remote cpu
3. page->freelist = t

then memory leak is occurred.

Change acquire_slab() not to zap freelist when it works for cpu partial list. I think it is a sufficient solution for fixing a memory leak.

Below is output of 'slabinfo -r kmalloc-256'
when './perf stat -r 30 hackbench 50 process 4000 > /dev/null' is done.

***Vanilla***
Sizes (bytes) Slabs Debug Memory
------------------------------------------------------------------------

Object :     256  Total  :     468   Sanity Checks : Off  Total: 3833856
SlabObj:     256  Full   :     111   Redzoning     : Off  Used : 2004992
SlabSiz:    8192  Partial:     302   Poisoning     : Off  Loss : 1828864
Loss   :       0  CpuSlab:      55   Tracking      : Off  Lalig:       0
Align  :       8  Objects:      32   Tracing       : Off  Lpadd:       0

***Patched***
Sizes (bytes) Slabs Debug Memory
------------------------------------------------------------------------

Object :     256  Total  :     300   Sanity Checks : Off  Total: 2457600
SlabObj:     256  Full   :     204   Redzoning     : Off  Used : 2348800
SlabSiz:    8192  Partial:      33   Poisoning     : Off  Loss :  108800
Loss   :       0  CpuSlab:      63   Tracking      : Off  Lalig:       0
Align  :       8  Objects:      32   Tracing       : Off  Lpadd:       0
Quoted 3 lines

Total and loss number is the impact of this patch.

<formletter>

This is not the correct way to submit patches for inclusion in the stable kernel tree. Please read Documentation/stable_kernel_rules.txt for how to do this properly.

</formletter>

Acked-by on v1 1/1
#2 lore raw
In reply to Joonsoo Kim
Quoted 7 lines · Joonsoo Kim

In the case which is below,

1. acquire slab for cpu partial list
2. free object to it by remote cpu
3. page->freelist = t

then memory leak is occurred.

Hmmm... Ok so we cannot assign page->freelist in get_partial_node() for the cpu partial slabs. It must be done in the cmpxchg transition.

#3 lore raw
Quoted 8 lines · Christoph Lameter
Quoted 6 lines · Joonsoo Kim

In the case which is below,

1. acquire slab for cpu partial list
2. free object to it by remote cpu
3. page->freelist = t

Quoted 6 lines
Quoted 1 line

then memory leak is occurred.

Hmmm... Ok so we cannot assign page->freelist in get_partial_node() for the cpu partial slabs. It must be done in the cmpxchg transition.

Joonsoo, can you please fix up the stable submission format, add Christoph's ACK and resend?

Pekka

#4 lore raw

2012/5/16 Pekka Enberg <[email protected]>:

Quoted 15 lines
Quoted 13 lines · Christoph Lameter
Quoted 7 lines · Joonsoo Kim

In the case which is below,

1. acquire slab for cpu partial list
2. free object to it by remote cpu
3. page->freelist = t

then memory leak is occurred.

Hmmm... Ok so we cannot assign page->freelist in get_partial_node() for the cpu partial slabs. It must be done in the cmpxchg transition.

Quoted 6 lines
Quoted 1 line

Joonsoo, can you please fix up the stable submission format, add Christoph's ACK and resend?

� � � � � � � � � � � �Pekka

Thanks for comment.
I'm a kernel newbie,
so could you please tell me how to fix up the stable submission format? I'm eager to fix it up, but I don't know how to.

I read stable_kernel_rules.txt, this article tells me I must note upstream commit ID.
Above patch is not included in upstream currently, so I can't find upstream commit ID.
Is 'Acked-by from MAINTAINER' sufficient for submitting to stable-kernel? Is below format right for stable submission format?

To: Linus Torvalds <[email protected]>, Greg Kroah-Hartman <[email protected]>, Pekka Enberg <[email protected]>

CC: Christoph Lameter <[email protected]>, [email protected], [email protected]

[ Upstream commit xxxxxxxxxxxxxxxxxxx ]

Comment is here

Acked-by:
Signed-off-by:

#5 lore raw
Quoted 28 lines · JoonSoo Kim

2012/5/16 Pekka Enberg <[email protected]>:

Quoted 15 lines
Quoted 13 lines · Christoph Lameter
Quoted 7 lines · Joonsoo Kim

In the case which is below,

1. acquire slab for cpu partial list
2. free object to it by remote cpu
3. page->freelist = t

then memory leak is occurred.

Hmmm... Ok so we cannot assign page->freelist in get_partial_node() for the cpu partial slabs. It must be done in the cmpxchg transition.

Quoted 6 lines
Quoted 1 line

Joonsoo, can you please fix up the stable submission format, add Christoph's ACK and resend?

� � � � � � � � � � � �Pekka

Thanks for comment.
I'm a kernel newbie,
so could you please tell me how to fix up the stable submission format? I'm eager to fix it up, but I don't know how to.

Quoted 6 lines

I read stable_kernel_rules.txt, this article tells me I must note upstream commit ID.
Above patch is not included in upstream currently, so I can't find upstream commit ID.
Is 'Acked-by from MAINTAINER' sufficient for submitting to stable-kernel? Is below format right for stable submission format?

No.

Please read the second item in the list that says: "Procedure for submitting patches to the -stable tree" in the file, Documentation/stable_kernel_rulest.txt. It states:

- To have the patch automatically included in the stable tree, add the tag
Cc: [email protected]
in the sign-off area. Once the patch is merged it will be applied to the stable tree without anything else needing to be done by the author or subsystem maintainer.

Does that help?

thanks,

greg k-h

#7 lore raw

PATCH RESEND 1/1 slub: fix a memory leak in get_partial_node()

In reply to Pekka Enberg

In the case which is below,

1. acquire slab for cpu partial list
2. free object to it by remote cpu
3. page->freelist = t

then memory leak is occurred.

Change acquire_slab() not to zap freelist when it works for cpu partial list. I think it is a sufficient solution for fixing a memory leak.

Below is output of 'slabinfo -r kmalloc-256'
when './perf stat -r 30 hackbench 50 process 4000 > /dev/null' is done.

***Vanilla***
Sizes (bytes) Slabs Debug Memory
------------------------------------------------------------------------

Object :     256  Total  :     468   Sanity Checks : Off  Total: 3833856
SlabObj:     256  Full   :     111   Redzoning     : Off  Used : 2004992
SlabSiz:    8192  Partial:     302   Poisoning     : Off  Loss : 1828864
Loss   :       0  CpuSlab:      55   Tracking      : Off  Lalig:       0
Align  :       8  Objects:      32   Tracing       : Off  Lpadd:       0

***Patched***
Sizes (bytes) Slabs Debug Memory
------------------------------------------------------------------------

Object :     256  Total  :     300   Sanity Checks : Off  Total: 2457600
SlabObj:     256  Full   :     204   Redzoning     : Off  Used : 2348800
SlabSiz:    8192  Partial:      33   Poisoning     : Off  Loss :  108800
Loss   :       0  CpuSlab:      63   Tracking      : Off  Lalig:       0
Align  :       8  Objects:      32   Tracing       : Off  Lpadd:       0

Total and loss number is the impact of this patch.

mm/slub.c +6 -3
index ffe13fd..a7a766a 100644
@@ -1514,15 +1514,19 @@ static inline void *acquire_slab(struct kmem_cache *s,
 		freelist = page->freelist;
 		counters = page->counters;
 		new.counters = counters;
-		if (mode)
+		if (mode) {
 			new.inuse = page->objects;
+			new.freelist = NULL;
+		} else {
+			new.freelist = freelist;
+		}
 
 		VM_BUG_ON(new.frozen);
 		new.frozen = 1;
 
 	} while (!__cmpxchg_double_slab(s, page,
 			freelist, counters,
-			NULL, new.counters,
+			new.freelist, new.counters,
 			"lock and freeze"));
 
 	remove_partial(n, page);
@@ -1564,7 +1568,6 @@ static void *get_partial_node(struct kmem_cache *s,
 			object = t;
 			available =  page->objects - page->inuse;
 		} else {
-			page->freelist = t;
 			available = put_cpu_partial(s, page, 0);
 			stat(s, CPU_PARTIAL_NODE);
 		}
Signature · 1 line
1.7.9.5
#6 lore raw
In reply to Greg Kroah-Hartman

2012/5/16 Greg Kroah-Hartman <[email protected]>:

Quoted 19 lines
Quoted 6 lines

I read stable_kernel_rules.txt, this article tells me I must note upstream commit ID.
Above patch is not included in upstream currently, so I can't find upstream commit ID.
Is 'Acked-by from MAINTAINER' sufficient for submitting to stable-kernel? Is below format right for stable submission format?

No.

Please read the second item in the list that says: "Procedure for submitting patches to the -stable tree" in the file, Documentation/stable_kernel_rulest.txt. �It states:

�- To have the patch automatically included in the stable tree, add the tag � � Cc: [email protected]
� in the sign-off area. Once the patch is merged it will be applied to � the stable tree without anything else needing to be done by the author � or subsystem maintainer.

Quoted 5 lines

Does that help?

thanks,

greg k-h

Thanks, very helpful.

Applied
#8 lore raw
In reply to Joonsoo Kim
Quoted 32 lines · Joonsoo Kim

In the case which is below,

1. acquire slab for cpu partial list
2. free object to it by remote cpu
3. page->freelist = t

then memory leak is occurred.

Change acquire_slab() not to zap freelist when it works for cpu partial list. I think it is a sufficient solution for fixing a memory leak.

Below is output of 'slabinfo -r kmalloc-256'
when './perf stat -r 30 hackbench 50 process 4000 > /dev/null' is done.

***Vanilla***
Sizes (bytes) Slabs Debug Memory
------------------------------------------------------------------------

Object :     256  Total  :     468   Sanity Checks : Off  Total: 3833856
SlabObj:     256  Full   :     111   Redzoning     : Off  Used : 2004992
SlabSiz:    8192  Partial:     302   Poisoning     : Off  Loss : 1828864
Loss   :       0  CpuSlab:      55   Tracking      : Off  Lalig:       0
Align  :       8  Objects:      32   Tracing       : Off  Lpadd:       0

***Patched***
Sizes (bytes) Slabs Debug Memory
------------------------------------------------------------------------

Object :     256  Total  :     300   Sanity Checks : Off  Total: 2457600
SlabObj:     256  Full   :     204   Redzoning     : Off  Used : 2348800
SlabSiz:    8192  Partial:      33   Poisoning     : Off  Loss :  108800
Loss   :       0  CpuSlab:      63   Tracking      : Off  Lalig:       0
Align  :       8  Objects:      32   Tracing       : Off  Lpadd:       0
Quoted 5 lines

Total and loss number is the impact of this patch.

Applied, thanks!