Skip to contentSkip to conversation

mtd: mtdpart: break it as soon as we parse out the partitions

PATCH Applied Huang Shijie 2 participants 4 messages Aug 18, 2012 – Aug 24, 2012 Memory technology devices (MTD)

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mtd: mtdpart: break it as soon as we parse out the partitions

We may cause a memory leak when the @types has more then one parser.

Take the default_mtd_part_types for example. The default_mtd_part_types has two parsers now: cmdlinepart and ofpart.

Assume the following case:
The kernel command line sets the partitions like:

	#gpmi-nand:20m(boot),20m(kernel),1g(rootfs),-(user)

But the devicetree file(such as arch/arm/boot/dts/imx28-evk.dts) also sets the same partitions as the kernel command line does.

In the current code, the partitions parsed out by the ofpart will overwrite the @pparts which has already set by the cmdlinepart parser, and the the partitions parsed out by the cmdlinepart is missed. A memory leak occurs.

So we should break the code as soon as we parse out the partitions, In actually, this patch makes a priority order between the parsers. If one parser has already parsed out the partitions successfully, it's no need to use another parser anymore.

---
 drivers/mtd/mtdpart.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
drivers/mtd/mtdpart.c +4 -1
index d518e4d..5999856 100644
@@ -711,6 +711,8 @@ static const char *default_mtd_part_types[] = {
  * partition parsers, specified in @types. However, if @types is %NULL, then
  * the default list of parsers is used. The default list contains only the
  * "cmdlinepart" and "ofpart" parsers ATM.
+ * Note: If there are more one parsers in @types, the kernel only takes the
+ * partitions parsed out by the first parser.
  *
  * This function may return:
  * o a negative error code in case of failure
@@ -735,11 +737,12 @@ int parse_mtd_partitions(struct mtd_info *master, const char **types,
 		if (!parser)
 			continue;
 		ret = (*parser->parse_fn)(master, pparts, data);
+		put_partition_parser(parser);
 		if (ret > 0) {
 			printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
 			       ret, parser->name, master->name);
+			break;
 		}
-		put_partition_parser(parser);
 	}
 	return ret;
 }
Signature · 1 line
1.7.4.4
#1 lore raw
Quoted 16 lines · Huang Shijie

We may cause a memory leak when the @types has more then one parser.

Take the default_mtd_part_types for example. The default_mtd_part_types has two parsers now: cmdlinepart and ofpart.

Assume the following case:
The kernel command line sets the partitions like:

	#gpmi-nand:20m(boot),20m(kernel),1g(rootfs),-(user)

But the devicetree file(such as arch/arm/boot/dts/imx28-evk.dts) also sets the same partitions as the kernel command line does.

In the current code, the partitions parsed out by the ofpart will overwrite the @pparts which has already set by the cmdlinepart parser, and the the partitions parsed out by the cmdlinepart is missed. A memory leak occurs.

Quoted 6 lines

So we should break the code as soon as we parse out the partitions, In actually, this patch makes a priority order between the parsers. If one parser has already parsed out the partitions successfully, it's no need to use another parser anymore.

Pushed to l2-mtd.git with "Cc: [email protected]" - do you agree with that?

Thanks!

Signature · 2 lines
Best Regards,
Artem Bityutskiy
#2 lore raw
Quoted 2 lines · Artem Bityutskiy

Pushed to l2-mtd.git with "Cc: [email protected]" - do you agree with that?

Note, I've actually pushed v2 of this patch.

Signature · 2 lines
Best Regards,
Artem Bityutskiy
#3 lore raw
In reply to Artem Bityutskiy
Quoted 22 lines · Artem Bityutskiy
Quoted 21 lines · Huang Shijie

We may cause a memory leak when the @types has more then one parser.

Take the default_mtd_part_types for example. The default_mtd_part_types has two parsers now: cmdlinepart and ofpart.

Assume the following case:
The kernel command line sets the partitions like:

      #gpmi-nand:20m(boot),20m(kernel),1g(rootfs),-(user)

But the devicetree file(such as arch/arm/boot/dts/imx28-evk.dts) also sets the same partitions as the kernel command line does.

In the current code, the partitions parsed out by the ofpart will overwrite the @pparts which has already set by the cmdlinepart parser, and the the partitions parsed out by the cmdlinepart is missed. A memory leak occurs.

So we should break the code as soon as we parse out the partitions, In actually, this patch makes a priority order between the parsers. If one parser has already parsed out the partitions successfully, it's no need to use another parser anymore.

Quoted 5 lines
Quoted 1 line

Pushed to l2-mtd.git with "Cc: [email protected]" - do you agree with that?

thanks a lot. I do agree to cc to stable.

Huang Shijie