linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Richard Weinberger <richard@nod.at>
To: miklos@szeredi.hu
Cc: miquel.raynal@bootlin.com, vigneshr@ti.com,
	boris.brezillon@collabora.com, rminnich@google.com,
	sven@narfation.org, linux-kernel@vger.kernel.org,
	linux-mtd@lists.infradead.org, fuse-devel@lists.sourceforge.net,
	Richard Weinberger <richard@nod.at>
Subject: [PATCH 5/8] mtd: Allow passing a custom cmdline to cmdline line parser
Date: Mon, 25 Jan 2021 00:20:04 +0100	[thread overview]
Message-ID: <20210124232007.21639-6-richard@nod.at> (raw)
In-Reply-To: <20210124232007.21639-1-richard@nod.at>

The cmdline parser uses usually the mtdparts string from the kernel cmdline.
For special purpose MTDs like MUSE it is useful to pass a custom
mtdparts string to the parser. This allows the MTD simulator in
userspace directly passing a mtdparts string and the new MTD
partitioned.

To achieve this, struct mtd_part_parser_data now has a mtdparts pointer
where a custom mtdparts string can be provided, it overrules the kernel
cmdline.
Since the cmdline parser stays for ever in the kernel, the memory lifecycle
had to be changed a little such that custom mtdparts string don't result
in memory leaks.

Signed-off-by: Richard Weinberger <richard@nod.at>
---
 drivers/mtd/parsers/cmdlinepart.c | 73 ++++++++++++++++++++++++-------
 include/linux/mtd/partitions.h    |  2 +
 2 files changed, 58 insertions(+), 17 deletions(-)

diff --git a/drivers/mtd/parsers/cmdlinepart.c b/drivers/mtd/parsers/cmdlinepart.c
index 0ddff1a4b51f..f0fe87267380 100644
--- a/drivers/mtd/parsers/cmdlinepart.c
+++ b/drivers/mtd/parsers/cmdlinepart.c
@@ -64,7 +64,7 @@ struct cmdline_mtd_partition {
 };
 
 /* mtdpart_setup() parses into here */
-static struct cmdline_mtd_partition *partitions;
+static struct cmdline_mtd_partition *cmdline_partitions;
 
 /* the command line passed to mtdpart_setup() */
 static char *mtdparts;
@@ -138,9 +138,6 @@ static struct mtd_partition * newpart(char *s,
 		name_len = 13; /* Partition_000 */
 	}
 
-	/* record name length for memory allocation later */
-	extra_mem_size += name_len + 1;
-
 	/* test for options */
 	if (strncmp(s, "ro", 2) == 0) {
 		mask_flags |= MTD_WRITEABLE;
@@ -192,12 +189,17 @@ static struct mtd_partition * newpart(char *s,
 	parts[this_part].offset = offset;
 	parts[this_part].mask_flags = mask_flags;
 	parts[this_part].add_flags = add_flags;
+
+	/*
+	 * Will get free()'ed in ->cleanup()
+	 */
 	if (name)
-		strlcpy(extra_mem, name, name_len + 1);
+		parts[this_part].name = kmemdup_nul(name, name_len, GFP_KERNEL);
 	else
-		sprintf(extra_mem, "Partition_%03d", this_part);
-	parts[this_part].name = extra_mem;
-	extra_mem += name_len + 1;
+		parts[this_part].name = kasprintf(GFP_KERNEL, "Partition_%03d", this_part);
+
+	if (!parts[this_part].name)
+		return ERR_PTR(-ENOMEM);
 
 	dbg(("partition %d: name <%s>, offset %llx, size %llx, mask flags %x\n",
 	     this_part, parts[this_part].name, parts[this_part].offset,
@@ -217,7 +219,7 @@ static struct mtd_partition * newpart(char *s,
 /*
  * Parse the command line.
  */
-static int mtdpart_setup_real(char *s)
+static int mtdpart_setup_real(char *s, struct cmdline_mtd_partition **partitions)
 {
 	cmdline_parsed = 1;
 
@@ -301,8 +303,8 @@ static int mtdpart_setup_real(char *s)
 		strlcpy(this_mtd->mtd_id, mtd_id, mtd_id_len + 1);
 
 		/* link into chain */
-		this_mtd->next = partitions;
-		partitions = this_mtd;
+		this_mtd->next = *partitions;
+		*partitions = this_mtd;
 
 		dbg(("mtdid=<%s> num_parts=<%d>\n",
 		     this_mtd->mtd_id, this_mtd->num_parts));
@@ -335,13 +337,23 @@ static int parse_cmdline_partitions(struct mtd_info *master,
 				    struct mtd_part_parser_data *data)
 {
 	unsigned long long offset;
-	int i, err;
+	int i, err, num_parts;
 	struct cmdline_mtd_partition *part;
 	const char *mtd_id = master->name;
+	struct cmdline_mtd_partition *parsed_parts = cmdline_partitions;
+	bool free_parts = false;
+
+	if (data && data->mtdparts) {
+		parsed_parts = NULL;
 
-	/* parse command line */
-	if (!cmdline_parsed) {
-		err = mtdpart_setup_real(cmdline);
+		err = mtdpart_setup_real(data->mtdparts, &parsed_parts);
+		if (err)
+			return err;
+
+		free_parts = true;
+	} else if (!cmdline_parsed) {
+		/* parse command line */
+		err = mtdpart_setup_real(cmdline, &cmdline_partitions);
 		if (err)
 			return err;
 	}
@@ -350,7 +362,7 @@ static int parse_cmdline_partitions(struct mtd_info *master,
 	 * Search for the partition definition matching master->name.
 	 * If master->name is not set, stop at first partition definition.
 	 */
-	for (part = partitions; part; part = part->next) {
+	for (part = parsed_parts; part; part = part->next) {
 		if ((!mtd_id) || (!strcmp(part->mtd_id, mtd_id)))
 			break;
 	}
@@ -384,12 +396,38 @@ static int parse_cmdline_partitions(struct mtd_info *master,
 		}
 	}
 
+	/*
+	 * Will get free()'ed in ->cleanup()
+	 */
 	*pparts = kmemdup(part->parts, sizeof(*part->parts) * part->num_parts,
 			  GFP_KERNEL);
 	if (!*pparts)
 		return -ENOMEM;
 
-	return part->num_parts;
+	num_parts = part->num_parts;
+
+	if (free_parts == true) {
+		part = parsed_parts;
+		while (part) {
+			struct cmdline_mtd_partition *next = part->next;
+
+			kfree(part->parts);
+			part = next;
+		}
+	}
+
+	return num_parts;
+}
+
+static void cmdline_partitions_cleanup(const struct mtd_partition *pparts,
+				       int nr_parts)
+{
+	int i;
+
+	for (i = 0; i < nr_parts; i++)
+		kfree(pparts[i].name);
+
+	kfree(pparts);
 }
 
 
@@ -410,6 +448,7 @@ __setup("mtdparts=", mtdpart_setup);
 
 static struct mtd_part_parser cmdline_parser = {
 	.parse_fn = parse_cmdline_partitions,
+	.cleanup = cmdline_partitions_cleanup,
 	.name = "cmdlinepart",
 };
 
diff --git a/include/linux/mtd/partitions.h b/include/linux/mtd/partitions.h
index b74a539ec581..6c8b3399143d 100644
--- a/include/linux/mtd/partitions.h
+++ b/include/linux/mtd/partitions.h
@@ -65,9 +65,11 @@ struct device_node;
 /**
  * struct mtd_part_parser_data - used to pass data to MTD partition parsers.
  * @origin: for RedBoot, start address of MTD device
+ * @mtdparts: for cmdline parser, use this string instead of mtdparts= from cmdline
  */
 struct mtd_part_parser_data {
 	unsigned long origin;
+	char *mtdparts;
 };
 
 
-- 
2.26.2


  parent reply	other threads:[~2021-01-24 23:23 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-24 23:19 [PATCH 0/8] MUSE: Userspace backed MTD v3 Richard Weinberger
2021-01-24 23:20 ` [PATCH 1/8] fuse: Export fuse_simple_request Richard Weinberger
2021-01-24 23:20 ` [PATCH 2/8] fuse: Export IO helpers Richard Weinberger
2021-01-24 23:20 ` [PATCH 3/8] fuse: Make cuse_parse_one a common helper Richard Weinberger
2021-01-24 23:20 ` [PATCH 4/8] mtd: Add MTD_MUSE flag Richard Weinberger
2021-01-24 23:20 ` Richard Weinberger [this message]
2021-01-24 23:20 ` [PATCH 6/8] fuse: Add MUSE specific defines FUSE interface Richard Weinberger
2021-01-24 23:20 ` [PATCH 7/8] fuse: Implement MUSE - MTD in userspace Richard Weinberger
2021-01-24 23:20 ` [PATCH 8/8] MAINTAINERS: Add entry for MUSE Richard Weinberger
2021-01-28 10:30 ` [PATCH 0/8] MUSE: Userspace backed MTD v3 Miquel Raynal
2021-02-01 13:14 ` Richard Weinberger
2021-02-01 13:55   ` Miklos Szeredi
2021-02-09 14:26 ` Miklos Szeredi
2021-02-09 14:35   ` Richard Weinberger
2021-02-09 15:10     ` [fuse-devel] " Luca Risolia
2021-02-09 15:22       ` Miklos Szeredi
2021-02-09 15:41       ` Richard Weinberger
2021-02-09 15:56         ` Luca Risolia
2021-02-09 16:04           ` Richard Weinberger
2021-02-09 16:28             ` Luca Risolia
2021-02-09 16:29               ` Richard Weinberger
2021-02-09 16:42                 ` Luca Risolia
2021-02-09 16:50                   ` Richard Weinberger
2021-02-09 17:46                     ` Luca Risolia
2021-02-09 19:42                       ` Miklos Szeredi
2021-02-09 20:06     ` Richard Weinberger
2021-02-10 10:12       ` Miklos Szeredi
2021-02-10 11:12         ` Richard Weinberger
2021-02-10 11:16         ` Miklos Szeredi
2021-02-11 18:09           ` Miklos Szeredi
2021-04-13 12:59             ` Miklos Szeredi
2021-02-09 21:39   ` Richard Weinberger
2021-02-10 10:16     ` Miklos Szeredi
2021-02-10 11:00       ` Richard Weinberger
2021-02-10 11:14       ` Miquel Raynal
2021-02-10 11:23         ` Richard Weinberger
2021-02-10 20:55           ` Miquel Raynal
2021-02-10 21:11             ` Richard Weinberger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210124232007.21639-6-richard@nod.at \
    --to=richard@nod.at \
    --cc=boris.brezillon@collabora.com \
    --cc=fuse-devel@lists.sourceforge.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mtd@lists.infradead.org \
    --cc=miklos@szeredi.hu \
    --cc=miquel.raynal@bootlin.com \
    --cc=rminnich@google.com \
    --cc=sven@narfation.org \
    --cc=vigneshr@ti.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).