All of lore.kernel.org
 help / color / mirror / Atom feed
From: Brian Norris <computersforpeace@gmail.com>
To: Boris Brezillon <boris.brezillon@free-electrons.com>
Cc: David Woodhouse <dwmw2@infradead.org>,
	linux-mtd@lists.infradead.org,
	Ezequiel Garcia <ezequiel@vanguardiasur.com.ar>,
	Marek Vasut <marex@denx.de>, Scott Wood <scottwood@freescale.com>,
	Josh Wu <josh.wu@atmel.com>,
	Robert Jarzmik <robert.jarzmik@free.fr>,
	Kyungmin Park <kyungmin.park@samsung.com>,
	Han Xu <han.xu@freescale.com>, Stefan Agner <stefan@agner.ch>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 13/11] mtd: assign mtd->dev.of_node when creating partition devices
Date: Wed, 11 Nov 2015 16:15:50 -0800	[thread overview]
Message-ID: <20151112001550.GD96199@google.com> (raw)
In-Reply-To: <1446424721-23471-1-git-send-email-boris.brezillon@free-electrons.com>

On Mon, Nov 02, 2015 at 01:38:41AM +0100, Boris Brezillon wrote:
> MTD partitions may have been created from a DT definition, and in this case
> the ->of_node of the struct device embedded in mtd_info should point to
> the DT node that was used to create the partition.
> 
> Signed-off-by: Boris Brezillon <boris.brezillon@free-electrons.com>

I like the idea of this patch, but I'm not sure about all of its
details.

> ---
> Hi Brian,
> 
> Yet another patch that IMO should go into your "mtd: migrate 'of_node'
> handling to core, not in mtd_part_parser_data" series.
> 
> Best Regards,
> 
> Boris
> 
>  drivers/mtd/mtdcore.c          | 17 +++++++++++++----
>  drivers/mtd/mtdpart.c          |  3 +++
>  drivers/mtd/ofpart.c           |  1 +
>  include/linux/mtd/partitions.h |  1 +
>  4 files changed, 18 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
> index b1eea48..6101288 100644
> --- a/drivers/mtd/mtdcore.c
> +++ b/drivers/mtd/mtdcore.c
> @@ -32,6 +32,7 @@
>  #include <linux/err.h>
>  #include <linux/ioctl.h>
>  #include <linux/init.h>
> +#include <linux/of.h>
>  #include <linux/proc_fs.h>
>  #include <linux/idr.h>
>  #include <linux/backing-dev.h>
> @@ -584,11 +585,13 @@ int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types,
>  			      const struct mtd_partition *parts,
>  			      int nr_parts)
>  {
> -	int ret;
> +	int ret, i;
>  	struct mtd_partition *real_parts = NULL;
>  
>  	ret = parse_mtd_partitions(mtd, types, &real_parts, parser_data);
> -	if (ret <= 0 && nr_parts && parts) {
> +	if (ret > 0) {
> +		nr_parts = ret;
> +	} else if (nr_parts && parts) {
>  		real_parts = kmemdup(parts, sizeof(*parts) * nr_parts,
>  				     GFP_KERNEL);
>  		if (!real_parts)
> @@ -604,7 +607,7 @@ int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types,
>  		ret = 0;
>  	}
>  
> -	ret = mtd_add_device_partitions(mtd, real_parts, ret);
> +	ret = mtd_add_device_partitions(mtd, real_parts, nr_parts);
>  	if (ret)
>  		goto out;
>  
> @@ -623,7 +626,13 @@ int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types,
>  	}
>  
>  out:
> -	kfree(real_parts);
> +	if (real_parts) {
> +		for (i = 0; i < nr_parts; i++)
> +			of_node_put(real_parts[i].of_node);
> +
> +		kfree(real_parts);
> +	}
> +
>  	return ret;
>  }
>  EXPORT_SYMBOL_GPL(mtd_device_parse_register);
> diff --git a/drivers/mtd/mtdpart.c b/drivers/mtd/mtdpart.c
> index f8ba153..95f3a0d 100644
> --- a/drivers/mtd/mtdpart.c
> +++ b/drivers/mtd/mtdpart.c
> @@ -29,6 +29,7 @@
>  #include <linux/kmod.h>
>  #include <linux/mtd/mtd.h>
>  #include <linux/mtd/partitions.h>
> +#include <linux/of.h>
>  #include <linux/err.h>
>  #include <linux/kconfig.h>
>  
> @@ -319,6 +320,7 @@ static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
>  
>  static inline void free_partition(struct mtd_part *p)
>  {
> +	of_node_put(mtd_get_of_node(&p->mtd));
>  	kfree(p->mtd.name);
>  	kfree(p);
>  }
> @@ -391,6 +393,7 @@ static struct mtd_part *allocate_partition(struct mtd_info *master,
>  	slave->mtd.dev.parent = IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) ?
>  				&master->dev :
>  				master->dev.parent;
> +	mtd_set_of_node(&slave->mtd, of_node_get(part->of_node));
>  
>  	slave->mtd._read = part_read;
>  	slave->mtd._write = part_write;
> diff --git a/drivers/mtd/ofpart.c b/drivers/mtd/ofpart.c
> index f78d2ae..5c64e04 100644
> --- a/drivers/mtd/ofpart.c
> +++ b/drivers/mtd/ofpart.c
> @@ -111,6 +111,7 @@ static int parse_ofpart_partitions(struct mtd_info *master,
>  		if (of_get_property(pp, "lock", &len))
>  			(*pparts)[i].mask_flags |= MTD_POWERUP_LOCK;
>  
> +		(*pparts)[i].of_node = of_node_get(pp);

I don't think we should mix up too much of the error handling between
ofpart.c and mtdcore.c (right now, you do of_node_get() here but the
of_node_put() is forced into mtdcore.c). I think the problem here is the
same as the problem with Linus' patch here:

http://lists.infradead.org/pipermail/linux-mtd/2015-November/063219.html

It's just too easy to leak resources when MTD parsers don't have their
own cleanup functions (like C++ destructors, or the driver model's
remove() function, or so many other resource models). If we had a
cleanup function, then we could just put the release handling there.

But actually, I don't think we need to 'get' the property here; we only
need to 'get' it when we're putting it somewhere that will actually use
it long term. i.e., when it actually gets assigned to the
mtd.dev.of_node field and is headed for sysfs.

IOW, I think we can grab the reference in add_mtd_device() and drop it
in del_mtd_device(). This would handle both the partition and
non-partition case the same.

I have a patch to do that (for the non-partition case) queued up. I'll
push it out soon, then I expect you can make this patch a lot simpler.

Brian

>  		i++;
>  	}
>  
> diff --git a/include/linux/mtd/partitions.h b/include/linux/mtd/partitions.h
> index 773975a..282644c 100644
> --- a/include/linux/mtd/partitions.h
> +++ b/include/linux/mtd/partitions.h
> @@ -42,6 +42,7 @@ struct mtd_partition {
>  	uint64_t offset;		/* offset within the master MTD space */
>  	uint32_t mask_flags;		/* master MTD flags to mask out for this partition */
>  	struct nand_ecclayout *ecclayout;	/* out of band layout for this partition (NAND only) */
> +	struct device_node *of_node;	/* OF node attached to the partition */
>  };
>  
>  #define MTDPART_OFS_RETAIN	(-3)
> -- 
> 2.1.4
> 

  reply	other threads:[~2015-11-12  0:16 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-31  3:33 [PATCH v2 00/11] mtd: migrate 'of_node' handling to core, not in mtd_part_parser_data Brian Norris
2015-10-31  3:33 ` [PATCH v2 01/11] mtd: add get/set of_node/flash_node helpers Brian Norris
2015-11-01 23:27   ` Boris Brezillon
2015-11-02 21:12     ` Brian Norris
2015-11-11 21:46   ` Brian Norris
2015-10-31  3:33 ` [PATCH v2 02/11] mtd: ofpart: grab device tree node directly from master device node Brian Norris
2015-10-31  3:33 ` [PATCH v2 03/11] mtd: {nand,spi-nor}: assign MTD of_node Brian Norris
2015-10-31  3:33 ` [PATCH v2 04/11] mtd: nand: convert to nand_set_flash_node() Brian Norris
2015-10-31 15:17   ` Marek Vasut
2015-10-31  3:33 ` [PATCH v2 05/11] mtd: spi-nor: convert to spi_nor_{get, set}_flash_node() Brian Norris
2015-10-31  3:33   ` [PATCH v2 05/11] mtd: spi-nor: convert to spi_nor_{get,set}_flash_node() Brian Norris
2015-10-31  3:33 ` [PATCH v2 06/11] mtd: nand: drop unnecessary partition parser data Brian Norris
2015-11-01 22:32   ` Boris Brezillon
2015-11-02 21:00     ` Brian Norris
2015-11-11 23:46       ` Brian Norris
2015-10-31  3:33 ` [PATCH v2 07/11] mtd: spi-nor: " Brian Norris
2015-10-31  3:33 ` [PATCH v2 08/11] mtd: spi-nor: drop flash_node field Brian Norris
2015-10-31  3:33 ` [PATCH v2 09/11] mtd: drop unnecessary partition parser data Brian Norris
2015-10-31 15:26   ` Marek Vasut
2015-11-01  0:11     ` Brian Norris
2015-11-05  8:49   ` Boris Brezillon
2015-11-11 23:47     ` Brian Norris
2015-10-31  3:33 ` [PATCH v2 10/11] mtd: ofpart: drop 'of_node' " Brian Norris
2015-10-31  3:33 ` [PATCH v2 11/11] mtd: physmap_of: assign parent for the concatenated MTD Brian Norris
2015-11-01  7:59 ` [PATCH v2 00/11] mtd: migrate 'of_node' handling to core, not in mtd_part_parser_data Boris Brezillon
2015-11-01 23:03 ` [PATCH v2 12/11] mtd: nand: convert to nand_get_flash_node() Boris Brezillon
2015-11-11 23:55   ` Brian Norris
2015-11-02  0:38 ` [PATCH v2 13/11] mtd: assign mtd->dev.of_node when creating partition devices Boris Brezillon
2015-11-12  0:15   ` Brian Norris [this message]
2015-11-12 13:22     ` Boris Brezillon
2015-11-20  2:58       ` Brian Norris

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=20151112001550.GD96199@google.com \
    --to=computersforpeace@gmail.com \
    --cc=boris.brezillon@free-electrons.com \
    --cc=dwmw2@infradead.org \
    --cc=ezequiel@vanguardiasur.com.ar \
    --cc=han.xu@freescale.com \
    --cc=josh.wu@atmel.com \
    --cc=kyungmin.park@samsung.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mtd@lists.infradead.org \
    --cc=marex@denx.de \
    --cc=robert.jarzmik@free.fr \
    --cc=scottwood@freescale.com \
    --cc=stefan@agner.ch \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.