sparc: kernel/sbus.c: fix memory leakage
PATCH 1/1 sparc: kernel/sbus.c: fix memory leakage
the variable iommu and strbuf are not freed if it goes to error.
--- arch/sparc/kernel/sbus.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
arch/sparc/kernel/sbus.c +6 -2
@@ -555,10 +555,10 @@ static void __init sbus_iommu_init(struct platform_device *op) iommu =kzalloc (sizeof (*iommu),GFP_ATOMIC );if (!iommu) - goto fatal_memory_error; + goto fatal_iommu_memory_error; strbuf =kzalloc (sizeof (*strbuf),GFP_ATOMIC );if (!strbuf) - goto fatal_memory_error; + goto fatal_strbuf_memory_error; op->dev.archdata.iommu = iommu; op->dev.archdata.stc = strbuf;
@@ -656,6 +656,10 @@ static void __init sbus_iommu_init(struct platform_device *op)return ; fatal_memory_error: +kfree (strbuf); +fatal_strbuf_memory_error: +kfree (iommu); +fatal_iommu_memory_error:prom_printf ("sbus_iommu_init: Fatal memory allocation error.\n" ); }
Signature · 1 line
1.7.9.5
Quoted 25 lines · Cong Ding
the variable iommu and strbuf are not freed if it goes to error.
Signed-off-by Cong Ding <[email protected]>--- arch/sparc/kernel/sbus.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)arch/sparc/kernel/sbus.c +2
-2@@ -555,10 +555,10 @@ static void __init sbus_iommu_init(struct platform_device *op) iommu =kzalloc (sizeof (*iommu),GFP_ATOMIC );if (!iommu) - goto fatal_memory_error; + goto fatal_iommu_memory_error; strbuf =kzalloc (sizeof (*strbuf),GFP_ATOMIC );if (!strbuf) - goto fatal_memory_error; + goto fatal_strbuf_memory_error; op->dev.archdata.iommu = iommu; op->dev.archdata.stc = strbuf;
arch/sparc/kernel/sbus.c static void __init sbus_iommu_init(struct platform_device *op) · line 664
Quoted 9 lines
arch/sparc/kernel/sbus.c +4
-0@@ -656,6 +656,10 @@ static void __init sbus_iommu_init(struct platform_device *op)return ; fatal_memory_error: +kfree (strbuf); +fatal_strbuf_memory_error: +kfree (iommu); +fatal_iommu_memory_error:prom_printf ("sbus_iommu_init: Fatal memory allocation error.\n" ); }
kfree is a noop if passed a null pointer.
So you can just add the missing kfree's to fix this.
Sam
Quoted 38 lines · Sam Ravnborg
Quoted 25 lines · Cong Ding
the variable iommu and strbuf are not freed if it goes to error.
Signed-off-by Cong Ding <[email protected]>--- arch/sparc/kernel/sbus.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)arch/sparc/kernel/sbus.c +2
-2@@ -555,10 +555,10 @@ static void __init sbus_iommu_init(struct platform_device *op) iommu =kzalloc (sizeof (*iommu),GFP_ATOMIC );if (!iommu) - goto fatal_memory_error; + goto fatal_iommu_memory_error; strbuf =kzalloc (sizeof (*strbuf),GFP_ATOMIC );if (!strbuf) - goto fatal_memory_error; + goto fatal_strbuf_memory_error; op->dev.archdata.iommu = iommu; op->dev.archdata.stc = strbuf;arch/sparc/kernel/sbus.c static void __init sbus_iommu_init(struct platform_device *op) · line 664
Quoted 9 lines
arch/sparc/kernel/sbus.c +4
-0@@ -656,6 +656,10 @@ static void __init sbus_iommu_init(struct platform_device *op)return ; fatal_memory_error: +kfree (strbuf); +fatal_strbuf_memory_error: +kfree (iommu); +fatal_iommu_memory_error:prom_printf ("sbus_iommu_init: Fatal memory allocation error.\n" ); }kfree is a noop if passed a null pointer.
So you can just add the missing kfree's to fix this.
Quoted 1 line
Sam
as far as I know, most codes in the kernel are in the same style as this patch, but if you really prefer to do in the way you described I can send a new version.
Let me know.
- cong
Quoted 39 lines · Cong Ding
Quoted 38 lines · Sam Ravnborg
Quoted 25 lines · Cong Ding
the variable iommu and strbuf are not freed if it goes to error.
Signed-off-by Cong Ding <[email protected]>--- arch/sparc/kernel/sbus.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)arch/sparc/kernel/sbus.c +2
-2@@ -555,10 +555,10 @@ static void __init sbus_iommu_init(struct platform_device *op) iommu =kzalloc (sizeof (*iommu),GFP_ATOMIC );if (!iommu) - goto fatal_memory_error; + goto fatal_iommu_memory_error; strbuf =kzalloc (sizeof (*strbuf),GFP_ATOMIC );if (!strbuf) - goto fatal_memory_error; + goto fatal_strbuf_memory_error; op->dev.archdata.iommu = iommu; op->dev.archdata.stc = strbuf;arch/sparc/kernel/sbus.c static void __init sbus_iommu_init(struct platform_device *op) · line 664
Quoted 9 lines
arch/sparc/kernel/sbus.c +4
-0@@ -656,6 +656,10 @@ static void __init sbus_iommu_init(struct platform_device *op)return ; fatal_memory_error: +kfree (strbuf); +fatal_strbuf_memory_error: +kfree (iommu); +fatal_iommu_memory_error:prom_printf ("sbus_iommu_init: Fatal memory allocation error.\n" ); }kfree is a noop if passed a null pointer.
So you can just add the missing kfree's to fix this.
Quoted 4 lines
Quoted 1 line
Sam
as far as I know, most codes in the kernel are in the same style as this patch, but if you really prefer to do in the way you described I can send a new version.
I prefer the simpler variant.
Please redo.
Sam
PATCH v2 1/1 sparc: kernel/sbus.c: fix memory leakage
the variable iommu and strbuf are not freed if it goes to error.
--- arch/sparc/kernel/sbus.c | 2 ++ 1 file changed, 2 insertions(+)
arch/sparc/kernel/sbus.c +2 -0
@@ -656,6 +656,8 @@ static void __init sbus_iommu_init(struct platform_device *op)return ; fatal_memory_error: +kfree (strbuf); +kfree (iommu);prom_printf ("sbus_iommu_init: Fatal memory allocation error.\n" ); }
Signature · 1 line
1.7.10.4
Quoted 12 lines · Cong Ding
the variable iommu and strbuf are not freed if it goes to error.
Signed-off-by Cong Ding <[email protected]>--- arch/sparc/kernel/sbus.c | 2 ++ 1 file changed, 2 insertions(+)arch/sparc/kernel/sbus.c +0
-0
arch/sparc/kernel/sbus.c static void __init sbus_iommu_init(struct platform_device *op) · line 659
Quoted 4 lines
arch/sparc/kernel/sbus.c +1
-0@@ -656,6 +656,8 @@ static void __init sbus_iommu_init(struct platform_device *op)return ; fatal_memory_error: +kfree (strbuf);
strbuf will be uninitialized if the iommu allocation fails. I don't have a particular preference for how to fix this but tend to dislike initial assignment with NULL because it hides other control flow issues.
Regards
Richard
Quoted 5 lines
arch/sparc/kernel/sbus.c +1
-0+kfree (iommu);prom_printf ("sbus_iommu_init: Fatal memory allocation error.\n" ); }
Quoted 17 lines · Richard Mortimer
Quoted 14 lines · Cong Ding
the variable iommu and strbuf are not freed if it goes to error.
Signed-off-by Cong Ding <[email protected]>--- arch/sparc/kernel/sbus.c | 2 ++ 1 file changed, 2 insertions(+)arch/sparc/kernel/sbus.c +0
-0@@ -656,6 +656,8 @@ static void __init sbus_iommu_init(struct platform_device *op)return ;
Quoted 7 lines
arch/sparc/kernel/sbus.c static void __init sbus_iommu_init(struct platform_device *op) · line 659
Quoted 2 lines
arch/sparc/kernel/sbus.c +1
-0fatal_memory_error: +kfree (strbuf);strbuf will be uninitialized if the iommu allocation fails. I don't have a particular preference for how to fix this but tend to dislike initial assignment with NULL because it hides other control flow issues.
Sorry I didn't notice strbuf will be uninitialized here. But if we don't initially assign a NULL value to strbuf, I cannot find a way to handle it besides the first version patch. Did you have any suggestions? For me, I like the first version.
- cong
Quoted 10 lines
Regards
Richard
Quoted 5 lines
arch/sparc/kernel/sbus.c +1
-0+kfree (iommu);prom_printf ("sbus_iommu_init: Fatal memory allocation error.\n" ); }
Quoted 25 lines · Cong Ding
Quoted 17 lines · Richard Mortimer
Quoted 14 lines · Cong Ding
the variable iommu and strbuf are not freed if it goes to error.
Signed-off-by Cong Ding <[email protected]>--- arch/sparc/kernel/sbus.c | 2 ++ 1 file changed, 2 insertions(+)arch/sparc/kernel/sbus.c +0
-0@@ -656,6 +656,8 @@ static void __init sbus_iommu_init(struct platform_device *op)return ;Quoted 7 lines
arch/sparc/kernel/sbus.c static void __init sbus_iommu_init(struct platform_device *op) · line 659
Quoted 2 lines
arch/sparc/kernel/sbus.c +1
-0fatal_memory_error: +kfree (strbuf);strbuf will be uninitialized if the iommu allocation fails. I don't have a particular preference for how to fix this but tend to dislike initial assignment with NULL because it hides other control flow issues.
Quoted 4 lines
Sorry I didn't notice strbuf will be uninitialized here. But if we don't initially assign a NULL value to strbuf, I cannot find a way to handle it besides the first version patch. Did you have any suggestions? For me, I like the first version.
Two thoughts...
1 - just use a goto target for the iommu allocation failure and make that skip the strbuf free call. The others use the existing fatal_memory_error label.
2 - Move the strbuf kzalloc up 2 lines so that it occurs before the test for iommu.
2b - In case (2) above the failure test could be changed to if (!iommu || !strbuf)
to remove duplication of goto.
I'd probably go for 2/2b to address Sam's initial comment.
Regards
Richard
Quoted 11 lines
- cong
Quoted 10 lines
Regards
Richard
Quoted 5 lines
arch/sparc/kernel/sbus.c +1
-0+kfree (iommu);prom_printf ("sbus_iommu_init: Fatal memory allocation error.\n" ); }
Quoted 33 lines · Richard Mortimer
Quoted 25 lines · Cong Ding
Quoted 17 lines · Richard Mortimer
Quoted 14 lines · Cong Ding
the variable iommu and strbuf are not freed if it goes to error.
Signed-off-by Cong Ding <[email protected]>--- arch/sparc/kernel/sbus.c | 2 ++ 1 file changed, 2 insertions(+)arch/sparc/kernel/sbus.c +0
-0@@ -656,6 +656,8 @@ static void __init sbus_iommu_init(struct platform_device *op)return ;Quoted 7 lines
arch/sparc/kernel/sbus.c static void __init sbus_iommu_init(struct platform_device *op) · line 659
Quoted 2 lines
arch/sparc/kernel/sbus.c +1
-0fatal_memory_error: +kfree (strbuf);strbuf will be uninitialized if the iommu allocation fails. I don't have a particular preference for how to fix this but tend to dislike initial assignment with NULL because it hides other control flow issues.
Quoted 4 lines
Sorry I didn't notice strbuf will be uninitialized here. But if we don't initially assign a NULL value to strbuf, I cannot find a way to handle it besides the first version patch. Did you have any suggestions? For me, I like the first version.
Quoted 5 lines
Two thoughts...
1 - just use a goto target for the iommu allocation failure and make that skip the strbuf free call. The others use the existing
fatal_memory_errorlabel.
this looks ugly. If we do in this way, why not version 1?
Quoted 7 lines
2 - Move the strbuf kzalloc up 2 lines so that it occurs before the test for iommu.
2b - In case (2) above the failure test could be changed to if (!iommu || !strbuf)
to remove duplication of goto.
I will send a new version by using this solution.
Thanks, - cong
PATCH v3 1/1 sparc: kernel/sbus.c: fix memory leakage
In reply to Richard MortimerThe variable iommu and strbuf are not freed properly if it goes to error.
--- arch/sparc/kernel/sbus.c | 6 +++--- 1 files changed, 3 insertions(+), 3 deletions(-)
arch/sparc/kernel/sbus.c +3 -3
@@ -554,10 +554,8 @@ static void __init sbus_iommu_init(struct platform_device *op) regs = pr->phys_addr; iommu =kzalloc (sizeof (*iommu),GFP_ATOMIC ); -if (!iommu) - goto fatal_memory_error; strbuf =kzalloc (sizeof (*strbuf),GFP_ATOMIC ); -if (!strbuf) +if (!iommu || !strbuf) goto fatal_memory_error; op->dev.archdata.iommu = iommu;
@@ -656,6 +654,8 @@ static void __init sbus_iommu_init(struct platform_device *op)return ; fatal_memory_error: +kfree (iommu); +kfree (strbuf);prom_printf ("sbus_iommu_init: Fatal memory allocation error.\n" ); }
Signature · 1 line
1.7.4.5
Quoted 33 lines · Cong Ding
The variable iommu and strbuf are not freed properly if it goes to error.
Signed-off-by Cong Ding <[email protected]>--- arch/sparc/kernel/sbus.c | 6 +++--- 1 files changed, 3 insertions(+), 3 deletions(-)arch/sparc/kernel/sbus.c +3
-3@@ -554,10 +554,8 @@ static void __init sbus_iommu_init(struct platform_device *op) regs = pr->phys_addr; iommu =kzalloc (sizeof (*iommu),GFP_ATOMIC ); -if (!iommu) - goto fatal_memory_error; strbuf =kzalloc (sizeof (*strbuf),GFP_ATOMIC ); -if (!strbuf) +if (!iommu || !strbuf) goto fatal_memory_error; op->dev.archdata.iommu = iommu;@@ -656,6 +654,8 @@ static void __init sbus_iommu_init(struct platform_device *op)return ; fatal_memory_error: +kfree (iommu); +kfree (strbuf);prom_printf ("sbus_iommu_init: Fatal memory allocation error.\n" ); }
From: Richard Mortimer <[email protected]>
Date: Mon, 21 Jan 2013 22:20:36 +0000
Quoted 7 lines
Reviewed-by Richard Mortimer <[email protected]>Quoted 4 lines · Cong Ding
The variable iommu and strbuf are not freed properly if it goes to error.
Signed-off-by Cong Ding <[email protected]>
Applied, thanks everyone.