Skip to content

Commit 25f0c65

Browse files
Amol Laddwmw2
authored andcommitted
ioremap balanced with iounmap for drivers/mtd subsystem
ioremap must be balanced by an iounmap and failing to do so can result in a memory leak. Tested (compilation only) with: - allmodconfig - Modifying drivers/mtd/maps/Kconfig and drivers/mtd/nand/Kconfig to make sure that the changed file is compiling without warning Signed-off-by: Amol Lad <amol@verismonetworks.com> Signed-off-by: David Woodhouse <dwmw2@infradead.org>
1 parent dd8e9ed commit 25f0c65

15 files changed

Lines changed: 119 additions & 15 deletions

File tree

‎drivers/mtd/maps/arctic-mtd.c‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ static struct mtd_partition arctic_partitions[PARTITIONS] = {
9696
static int __init
9797
init_arctic_mtd(void)
9898
{
99+
int err = 0;
100+
99101
printk("%s: 0x%08x at 0x%08x\n", NAME, SIZE, PADDR);
100102

101103
arctic_mtd_map.virt = ioremap(PADDR, SIZE);
@@ -109,12 +111,20 @@ init_arctic_mtd(void)
109111
printk("%s: probing %d-bit flash bus\n", NAME, BUSWIDTH * 8);
110112
arctic_mtd = do_map_probe("cfi_probe", &arctic_mtd_map);
111113

112-
if (!arctic_mtd)
114+
if (!arctic_mtd) {
115+
iounmap((void *) arctic_mtd_map.virt);
113116
return -ENXIO;
117+
}
114118

115119
arctic_mtd->owner = THIS_MODULE;
116120

117-
return add_mtd_partitions(arctic_mtd, arctic_partitions, PARTITIONS);
121+
err = add_mtd_partitions(arctic_mtd, arctic_partitions, PARTITIONS);
122+
if (err) {
123+
printk("%s: add_mtd_partitions failed\n", NAME);
124+
iounmap((void *) arctic_mtd_map.virt);
125+
}
126+
127+
return err;
118128
}
119129

120130
static void __exit

‎drivers/mtd/maps/beech-mtd.c‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ static struct mtd_partition beech_partitions[2] = {
7272
static int __init
7373
init_beech_mtd(void)
7474
{
75+
int err = 0;
76+
7577
printk("%s: 0x%08x at 0x%08x\n", NAME, SIZE, PADDR);
7678

7779
beech_mtd_map.virt = ioremap(PADDR, SIZE);
@@ -86,12 +88,20 @@ init_beech_mtd(void)
8688
printk("%s: probing %d-bit flash bus\n", NAME, BUSWIDTH * 8);
8789
beech_mtd = do_map_probe("cfi_probe", &beech_mtd_map);
8890

89-
if (!beech_mtd)
91+
if (!beech_mtd) {
92+
iounmap((void *) beech_mtd_map.virt);
9093
return -ENXIO;
94+
}
9195

9296
beech_mtd->owner = THIS_MODULE;
9397

94-
return add_mtd_partitions(beech_mtd, beech_partitions, 2);
98+
err = add_mtd_partitions(beech_mtd, beech_partitions, 2);
99+
if (err) {
100+
printk("%s: add_mtd_partitions failed\n", NAME);
101+
iounmap((void *) beech_mtd_map.virt);
102+
}
103+
104+
return err;
95105
}
96106

97107
static void __exit

‎drivers/mtd/maps/cstm_mips_ixx.c‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,14 @@ int __init init_cstm_mips_ixx(void)
171171
cstm_mips_ixx_map[i].phys = cstm_mips_ixx_board_desc[i].window_addr;
172172
cstm_mips_ixx_map[i].virt = ioremap(cstm_mips_ixx_board_desc[i].window_addr, cstm_mips_ixx_board_desc[i].window_size);
173173
if (!cstm_mips_ixx_map[i].virt) {
174+
int j = 0;
174175
printk(KERN_WARNING "Failed to ioremap\n");
176+
for (j = 0; j < i; j++) {
177+
if (cstm_mips_ixx_map[j].virt) {
178+
iounmap((void *)cstm_mips_ixx_map[j].virt);
179+
cstm_mips_ixx_map[j].virt = 0;
180+
}
181+
}
175182
return -EIO;
176183
}
177184
cstm_mips_ixx_map[i].name = cstm_mips_ixx_board_desc[i].name;
@@ -204,8 +211,15 @@ int __init init_cstm_mips_ixx(void)
204211
cstm_mips_ixx_map[i].map_priv_2 = (unsigned long)mymtd;
205212
add_mtd_partitions(mymtd, parts, cstm_mips_ixx_board_desc[i].num_partitions);
206213
}
207-
else
208-
return -ENXIO;
214+
else {
215+
for (i = 0; i < PHYSMAP_NUMBER; i++) {
216+
if (cstm_mips_ixx_map[i].virt) {
217+
iounmap((void *)cstm_mips_ixx_map[i].virt);
218+
cstm_mips_ixx_map[i].virt = 0;
219+
}
220+
}
221+
return -ENXIO;
222+
}
209223
}
210224
return 0;
211225
}

‎drivers/mtd/maps/ebony.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ int __init init_ebony(void)
108108
ARRAY_SIZE(ebony_small_partitions));
109109
} else {
110110
printk("map probe failed for flash\n");
111+
iounmap(ebony_small_map.virt);
111112
return -ENXIO;
112113
}
113114

@@ -117,6 +118,7 @@ int __init init_ebony(void)
117118

118119
if (!ebony_large_map.virt) {
119120
printk("Failed to ioremap flash\n");
121+
iounmap(ebony_small_map.virt);
120122
return -EIO;
121123
}
122124

@@ -129,6 +131,8 @@ int __init init_ebony(void)
129131
ARRAY_SIZE(ebony_large_partitions));
130132
} else {
131133
printk("map probe failed for flash\n");
134+
iounmap(ebony_small_map.virt);
135+
iounmap(ebony_large_map.virt);
132136
return -ENXIO;
133137
}
134138

‎drivers/mtd/maps/fortunet.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,11 @@ int __init init_fortunet(void)
218218
map_regions[ix].map_info.size);
219219
if(!map_regions[ix].map_info.virt)
220220
{
221+
int j = 0;
221222
printk(MTD_FORTUNET_PK "%s flash failed to ioremap!\n",
222223
map_regions[ix].map_info.name);
224+
for (j = 0 ; j < ix; j++)
225+
iounmap(map_regions[j].map_info.virt);
223226
return -ENXIO;
224227
}
225228
simple_map_init(&map_regions[ix].map_info);

‎drivers/mtd/maps/lasat.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ static int __init init_lasat(void)
7979
return 0;
8080
}
8181

82+
iounmap(lasat_map.virt);
8283
return -ENXIO;
8384
}
8485

@@ -89,6 +90,7 @@ static void __exit cleanup_lasat(void)
8990
map_destroy(lasat_mtd);
9091
}
9192
if (lasat_map.virt) {
93+
iounmap(lasat_map.virt);
9294
lasat_map.virt = 0;
9395
}
9496
}

‎drivers/mtd/maps/nettel.c‎

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ int __init nettel_init(void)
277277
nettel_amd_map.virt = ioremap_nocache(amdaddr, maxsize);
278278
if (!nettel_amd_map.virt) {
279279
printk("SNAPGEAR: failed to ioremap() BOOTCS\n");
280+
iounmap(nettel_mmcrp);
280281
return(-EIO);
281282
}
282283
simple_map_init(&nettel_amd_map);
@@ -337,7 +338,8 @@ int __init nettel_init(void)
337338
nettel_amd_map.virt = NULL;
338339
#else
339340
/* Only AMD flash supported */
340-
return(-ENXIO);
341+
rc = -ENXIO;
342+
goto out_unmap2;
341343
#endif
342344
}
343345

@@ -361,14 +363,15 @@ int __init nettel_init(void)
361363
nettel_intel_map.virt = ioremap_nocache(intel0addr, maxsize);
362364
if (!nettel_intel_map.virt) {
363365
printk("SNAPGEAR: failed to ioremap() ROMCS1\n");
364-
return(-EIO);
366+
rc = -EIO;
367+
goto out_unmap2;
365368
}
366369
simple_map_init(&nettel_intel_map);
367370

368371
intel_mtd = do_map_probe("cfi_probe", &nettel_intel_map);
369372
if (!intel_mtd) {
370-
iounmap(nettel_intel_map.virt);
371-
return(-ENXIO);
373+
rc = -ENXIO;
374+
goto out_unmap1;
372375
}
373376

374377
/* Set PAR to the detected size */
@@ -394,13 +397,14 @@ int __init nettel_init(void)
394397
nettel_intel_map.virt = ioremap_nocache(intel0addr, maxsize);
395398
if (!nettel_intel_map.virt) {
396399
printk("SNAPGEAR: failed to ioremap() ROMCS1/2\n");
397-
return(-EIO);
400+
rc = -EIO;
401+
goto out_unmap2;
398402
}
399403

400404
intel_mtd = do_map_probe("cfi_probe", &nettel_intel_map);
401405
if (! intel_mtd) {
402-
iounmap((void *) nettel_intel_map.virt);
403-
return(-ENXIO);
406+
rc = -ENXIO;
407+
goto out_unmap1;
404408
}
405409

406410
intel1size = intel_mtd->size - intel0size;
@@ -456,6 +460,18 @@ int __init nettel_init(void)
456460
#endif
457461

458462
return(rc);
463+
464+
#ifdef CONFIG_MTD_CFI_INTELEXT
465+
out_unmap1:
466+
iounmap((void *) nettel_intel_map.virt);
467+
#endif
468+
469+
out_unmap2:
470+
iounmap(nettel_mmcrp);
471+
iounmap(nettel_amd_map.virt);
472+
473+
return(rc);
474+
459475
}
460476

461477
/****************************************************************************/
@@ -469,6 +485,10 @@ void __exit nettel_cleanup(void)
469485
del_mtd_partitions(amd_mtd);
470486
map_destroy(amd_mtd);
471487
}
488+
if (nettel_mmcrp) {
489+
iounmap(nettel_mmcrp);
490+
nettel_mmcrp = NULL;
491+
}
472492
if (nettel_amd_map.virt) {
473493
iounmap(nettel_amd_map.virt);
474494
nettel_amd_map.virt = NULL;

‎drivers/mtd/maps/ocotea.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ int __init init_ocotea(void)
9797
ARRAY_SIZE(ocotea_small_partitions));
9898
} else {
9999
printk("map probe failed for flash\n");
100+
iounmap(ocotea_small_map.virt);
100101
return -ENXIO;
101102
}
102103

@@ -106,6 +107,7 @@ int __init init_ocotea(void)
106107

107108
if (!ocotea_large_map.virt) {
108109
printk("Failed to ioremap flash\n");
110+
iounmap(ocotea_small_map.virt);
109111
return -EIO;
110112
}
111113

@@ -118,6 +120,8 @@ int __init init_ocotea(void)
118120
ARRAY_SIZE(ocotea_large_partitions));
119121
} else {
120122
printk("map probe failed for flash\n");
123+
iounmap(ocotea_small_map.virt);
124+
iounmap(ocotea_large_map.virt);
121125
return -ENXIO;
122126
}
123127

‎drivers/mtd/maps/pcmciamtd.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,10 @@ static int pcmciamtd_config(struct pcmcia_device *link)
602602
ret = pcmcia_request_configuration(link, &link->conf);
603603
if(ret != CS_SUCCESS) {
604604
cs_error(link, RequestConfiguration, ret);
605+
if (dev->win_base) {
606+
iounmap(dev->win_base);
607+
dev->win_base = NULL;
608+
}
605609
return -ENODEV;
606610
}
607611

‎drivers/mtd/maps/redwood.c‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ static struct mtd_info *redwood_mtd;
126126

127127
int __init init_redwood_flash(void)
128128
{
129+
int err = 0;
130+
129131
printk(KERN_NOTICE "redwood: flash mapping: %x at %x\n",
130132
WINDOW_SIZE, WINDOW_ADDR);
131133

@@ -141,11 +143,18 @@ int __init init_redwood_flash(void)
141143

142144
if (redwood_mtd) {
143145
redwood_mtd->owner = THIS_MODULE;
144-
return add_mtd_partitions(redwood_mtd,
146+
err = add_mtd_partitions(redwood_mtd,
145147
redwood_flash_partitions,
146148
NUM_REDWOOD_FLASH_PARTITIONS);
149+
if (err) {
150+
printk("init_redwood_flash: add_mtd_partitions failed\n");
151+
iounmap(redwood_flash_map.virt);
152+
}
153+
return err;
154+
147155
}
148156

157+
iounmap(redwood_flash_map.virt);
149158
return -ENXIO;
150159
}
151160

0 commit comments

Comments
 (0)