Skip to contentSkip to conversation

mtd: maps: add missing iounmap() in error path

PATCH Superseded Luis Henriques 3 participants 7 messages Nov 16, 2016 – Dec 1, 2016 Memory technology devices (MTD)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mtd: maps: add missing iounmap() in error path

This patch was triggered by the following Coccinelle error:

./drivers/mtd/maps/sc520cdp.c:246:3-9: \
	ERROR: missing iounmap; ioremap on line 242 \
	and execution via conditional on line 244
---
 drivers/mtd/maps/sc520cdp.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/mtd/maps/sc520cdp.c +4 -0
index 093edd51bdc7..7a27ed345d0d 100644
@@ -243,6 +243,10 @@ static int __init init_sc520cdp(void)
 
 		if (!sc520cdp_map[i].virt) {
 			printk("Failed to ioremap_nocache\n");
+			if (i) {
+				while (--i)
+					iounmap(sc520cdp_map[i].virt);
+			}
 			return -EIO;
 		}
 
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Luis Henriques

This patch was triggered by the following Coccinelle error:

./drivers/mtd/maps/sc520cdp.c:246:3-9: \
	ERROR: missing iounmap; ioremap on line 242 \
	and execution via conditional on line 244
Quoted 20 lines
---
 drivers/mtd/maps/sc520cdp.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/mtd/maps/sc520cdp.c +4 -0
index 093edd51bdc7..7a27ed345d0d 100644
@@ -243,6 +243,10 @@ static int __init init_sc520cdp(void)
 
 		if (!sc520cdp_map[i].virt) {
 			printk("Failed to ioremap_nocache\n");
+			if (i) {
+				while (--i)
+					iounmap(sc520cdp_map[i].virt);
+			}
 			return -EIO;
 		}
 
Signature · 2 lines
Best regards,
Marek Vasut
#2 lore raw
In reply to Luis Henriques
Quoted 16 lines · Luis Henriques

This patch was triggered by the following Coccinelle error:

./drivers/mtd/maps/sc520cdp.c:246:3-9: \
	ERROR: missing iounmap; ioremap on line 242 \
	and execution via conditional on line 244
---
 drivers/mtd/maps/sc520cdp.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/mtd/maps/sc520cdp.c +0 -0
index 093edd51bdc7..7a27ed345d0d 100644

drivers/mtd/maps/sc520cdp.c static int __init init_sc520cdp(void) · line 247

Quoted 5 lines
drivers/mtd/maps/sc520cdp.c +2 -0
@@ -243,6 +243,10 @@ static int __init init_sc520cdp(void)
 
 		if (!sc520cdp_map[i].virt) {
 			printk("Failed to ioremap_nocache\n");
+			if (i) {
+				while (--i)

Umm, so you never unmap from sc520cdp_map[0].virt? How about:

				while (--i >= 0)

?

You can also skip the 'if (i)' part in that case. Or maybe make it a for loop, to be even clearer.

drivers/mtd/maps/sc520cdp.c static int __init init_sc520cdp(void) · line 248

Quoted 1 line
drivers/mtd/maps/sc520cdp.c +1 -0
+					iounmap(sc520cdp_map[i].virt);

This may often be a double-iounmap. If you take a look later in the loop, many instances of the loop may not find a device, and so they'll unmap this memory and move on. You're just doing it a second time for them.

drivers/mtd/maps/sc520cdp.c static int __init init_sc520cdp(void) · line 252

Quoted 4 lines
drivers/mtd/maps/sc520cdp.c +1 -0
+			}
 			return -EIO;
 		}
 

Please put some more care into your patch, since I very much expect that you did not test it.

Brian

#3 lore raw
Quoted 47 lines · Brian Norris
Quoted 16 lines · Luis Henriques

This patch was triggered by the following Coccinelle error:

./drivers/mtd/maps/sc520cdp.c:246:3-9: \
	ERROR: missing iounmap; ioremap on line 242 \
	and execution via conditional on line 244
---
 drivers/mtd/maps/sc520cdp.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/mtd/maps/sc520cdp.c +0 -0
index 093edd51bdc7..7a27ed345d0d 100644

drivers/mtd/maps/sc520cdp.c static int __init init_sc520cdp(void) · line 247

Quoted 5 lines
drivers/mtd/maps/sc520cdp.c +2 -0
@@ -243,6 +243,10 @@ static int __init init_sc520cdp(void)
 
 		if (!sc520cdp_map[i].virt) {
 			printk("Failed to ioremap_nocache\n");
+			if (i) {
+				while (--i)

Umm, so you never unmap from sc520cdp_map[0].virt? How about:

				while (--i >= 0)

?

You can also skip the 'if (i)' part in that case. Or maybe make it a for loop, to be even clearer.

drivers/mtd/maps/sc520cdp.c static int __init init_sc520cdp(void) · line 248

Quoted 1 line
drivers/mtd/maps/sc520cdp.c +1 -0
+					iounmap(sc520cdp_map[i].virt);

This may often be a double-iounmap. If you take a look later in the loop, many instances of the loop may not find a device, and so they'll unmap this memory and move on. You're just doing it a second time for them.

drivers/mtd/maps/sc520cdp.c static int __init init_sc520cdp(void) · line 252

Quoted 4 lines
drivers/mtd/maps/sc520cdp.c +1 -0
+			}
 			return -EIO;
 		}
 

Please put some more care into your patch, since I very much expect that you did not test it.

Quoted 1 line

Brian

Thank you very much for your review. All very good points indeed! I'll try to send v2 soon to cover all the issues you've found.

Cheers,

Signature · 1 line
Luís
#4 lore raw

PATCH v2 1/1 mtd: maps: add missing iounmap() in error path

In reply to Brian Norris

This patch was triggered by the following Coccinelle error:

./drivers/mtd/maps/sc520cdp.c:246:3-9: \
	ERROR: missing iounmap; ioremap on line 242 \
	and execution via conditional on line 244

Since do_map_probe() is also invoked in this loop, it is also necessary to map_destroy() any initialised struct mtd_info.

---
Changes since v1:
* Changes according to Brian Norris review:
- Ensure sc520cdp_map[0].virt is unmaped when needed
- Use a for loop instead of an 'if (i)'
- Prevent double-iounmap
* Added map_destroy() in the cleanup

drivers/mtd/maps/sc520cdp.c | 8 +++++++-

 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/mtd/maps/sc520cdp.c +7 -1
index 093edd51bdc7..9b1c13aa9f20 100644
@@ -227,7 +227,7 @@ static void sc520cdp_setup_par(void)
 
 static int __init init_sc520cdp(void)
 {
-	int i, devices_found = 0;
+	int i, j, devices_found = 0;
 
 #ifdef REPROGRAM_PAR
 	/* reprogram PAR registers so flash appears at the desired addresses */
@@ -243,6 +243,12 @@ static int __init init_sc520cdp(void)
 
 		if (!sc520cdp_map[i].virt) {
 			printk("Failed to ioremap_nocache\n");
+			for (j = 0; j < i; j++) {
+				if (mymtd[j]) {
+					map_destroy(mymtd[j]);
+					iounmap(sc520cdp_map[j].virt);
+				}
+			}
 			return -EIO;
 		}
 
Reviewed-by on v2 1/1
#5 lore raw
Quoted 35 lines · Luis Henriques

This patch was triggered by the following Coccinelle error:

./drivers/mtd/maps/sc520cdp.c:246:3-9: \
	ERROR: missing iounmap; ioremap on line 242 \
	and execution via conditional on line 244

Since do_map_probe() is also invoked in this loop, it is also necessary to map_destroy() any initialised struct mtd_info.

---
Changes since v1:
* Changes according to Brian Norris review:
- Ensure sc520cdp_map[0].virt is unmaped when needed
- Use a for loop instead of an 'if (i)'
- Prevent double-iounmap
* Added map_destroy() in the cleanup

drivers/mtd/maps/sc520cdp.c | 8 +++++++-

 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/mtd/maps/sc520cdp.c +1 -1
index 093edd51bdc7..9b1c13aa9f20 100644
@@ -227,7 +227,7 @@ static void sc520cdp_setup_par(void)
 
 static int __init init_sc520cdp(void)
 {
-	int i, devices_found = 0;
+	int i, j, devices_found = 0;
 
 #ifdef REPROGRAM_PAR
 	/* reprogram PAR registers so flash appears at the desired addresses */

drivers/mtd/maps/sc520cdp.c static int __init init_sc520cdp(void) · line 251

Quoted 9 lines
drivers/mtd/maps/sc520cdp.c +6 -0
@@ -243,6 +243,12 @@ static int __init init_sc520cdp(void)
 
 		if (!sc520cdp_map[i].virt) {
 			printk("Failed to ioremap_nocache\n");
+			for (j = 0; j < i; j++) {
+				if (mymtd[j]) {
+					map_destroy(mymtd[j]);
+					iounmap(sc520cdp_map[j].virt);
+				}
+			}
Quoted 4 lines
drivers/mtd/maps/sc520cdp.c +0 -0
 			return -EIO;
 		}
 
Signature · 2 lines
Best regards,
Marek Vasut
Applied
#6 lore raw
Quoted 21 lines · Marek Vasut
Quoted 20 lines · Luis Henriques

This patch was triggered by the following Coccinelle error:

./drivers/mtd/maps/sc520cdp.c:246:3-9: \
	ERROR: missing iounmap; ioremap on line 242 \
	and execution via conditional on line 244

Since do_map_probe() is also invoked in this loop, it is also necessary to map_destroy() any initialised struct mtd_info.

---
Changes since v1:
* Changes according to Brian Norris review:
- Ensure sc520cdp_map[0].virt is unmaped when needed
- Use a for loop instead of an 'if (i)'
- Prevent double-iounmap
* Added map_destroy() in the cleanup

drivers/mtd/maps/sc520cdp.c | 8 +++++++-

 1 file changed, 7 insertions(+), 1 deletion(-)

...

Quoted 1 line

Applied to l2-mtd.git