Skip to contentSkip to conversation

sparc: kernel/sbus.c: fix memory leakage

PATCH Superseded Cong Ding 4 participants 12 messages Jan 14, 2013 – Jan 21, 2013 SPARC + UltraSPARC (sparc/sparc64)

lore
Share & tools
lore

Cong Ding

author
#0 lore raw

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
index 1271b3a..0b66e53 100644
@@ -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
#1 lore raw
Quoted 25 lines · Cong Ding

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 +2 -2
index 1271b3a..0b66e53 100644
@@ -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

author
#2 lore raw
Quoted 38 lines · Sam Ravnborg
Quoted 25 lines · Cong Ding

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 +2 -2
index 1271b3a..0b66e53 100644
@@ -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

#3 lore raw
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.

---
 arch/sparc/kernel/sbus.c |    8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
arch/sparc/kernel/sbus.c +2 -2
index 1271b3a..0b66e53 100644
@@ -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

author
#4 lore raw

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
index 1271b3a..78aa26b 100644
@@ -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
#5 lore raw
Quoted 12 lines · Cong Ding

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 +0 -0
index 1271b3a..78aa26b 100644

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");
  }
 
author
#6 lore raw
Quoted 17 lines · Richard Mortimer
Quoted 14 lines · Cong Ding

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 +0 -0
index 1271b3a..78aa26b 100644
@@ -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 -0
 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.

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");
 }
 
#7 lore raw
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.

---
  arch/sparc/kernel/sbus.c |    2 ++
  1 file changed, 2 insertions(+)
arch/sparc/kernel/sbus.c +0 -0
index 1271b3a..78aa26b 100644
@@ -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 -0
  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.

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");
  }
 
author
#8 lore raw
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.

---
 arch/sparc/kernel/sbus.c |    2 ++
 1 file changed, 2 insertions(+)
arch/sparc/kernel/sbus.c +0 -0
index 1271b3a..78aa26b 100644
@@ -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 -0
 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.

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_error label.

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

author
#9 lore raw

PATCH v3 1/1 sparc: kernel/sbus.c: fix memory leakage

In reply to Richard Mortimer

The 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
index 1271b3a..be5bdf9 100644
@@ -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
Reviewed-by on v3 1/1
#10 lore raw
Quoted 33 lines · Cong Ding

The 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
index 1271b3a..be5bdf9 100644
@@ -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");
  }