All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Rafał Miłecki" <zajec5@gmail.com>
To: "han.xu" <han.xu@nxp.com>, Ansuel Smith <ansuelsmth@gmail.com>
Cc: "Miquel Raynal" <miquel.raynal@bootlin.com>,
	"Richard Weinberger" <richard@nod.at>,
	"Vignesh Raghavendra" <vigneshr@ti.com>,
	"Rob Herring" <robh+dt@kernel.org>,
	"Krzysztof Kozlowski" <krzysztof.kozlowski+dt@linaro.org>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Jens Axboe" <axboe@kernel.dk>, �ecki <rafal@milecki.pl>,
	"Manivannan Sadhasivam" <mani@kernel.org>,
	linux-mtd@lists.infradead.org, devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v5 3/3] mtd: core: introduce of support for dynamic partitions
Date: Mon, 17 Oct 2022 23:52:34 +0200	[thread overview]
Message-ID: <d98dd483-70b9-4bd1-0463-9e3343576955@gmail.com> (raw)
In-Reply-To: <20221017195820.ve6c3zj2civkswm6@umbrella>

On 17.10.2022 21:59, han.xu wrote:
> On 22/06/06 05:14PM, Ansuel Smith wrote:
>> We have many parser that register mtd partitions at runtime. One example
>> is the cmdlinepart or the smem-part parser where the compatible is defined
>> in the dts and the partitions gets detected and registered by the
>> parser. This is problematic for the NVMEM subsystem that requires an OF node
>> to detect NVMEM cells.
>>
>> To fix this problem, introduce an additional logic that will try to
>> assign an OF node to the MTD if declared.
>>
>> On MTD addition, it will be checked if the MTD has an OF node and if
>> not declared will check if a partition with the same label is
>> declared in DTS. If an exact match is found, the partition dynamically
>> allocated by the parser will have a connected OF node.
>>
>> The NVMEM subsystem will detect the OF node and register any NVMEM cells
>> declared statically in the DTS.
>>
>> Signed-off-by: Ansuel Smith <ansuelsmth@gmail.com>
>> ---
>>   drivers/mtd/mtdcore.c | 49 +++++++++++++++++++++++++++++++++++++++++++
>>   1 file changed, 49 insertions(+)
>>
>> diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
>> index 7731796024e0..807194efb580 100644
>> --- a/drivers/mtd/mtdcore.c
>> +++ b/drivers/mtd/mtdcore.c
>> @@ -546,6 +546,54 @@ static int mtd_nvmem_add(struct mtd_info *mtd)
>>   	return 0;
>>   }
>>   
>> +static void mtd_check_of_node(struct mtd_info *mtd)
>> +{
>> +	struct device_node *partitions, *parent_dn, *mtd_dn = NULL;
>> +	struct mtd_info *parent;
>> +	const char *mtd_name;
>> +	bool found = false;
>> +	int plen;
>> +
>> +	/* Check if MTD already has a device node */
>> +	if (dev_of_node(&mtd->dev))
>> +		return;
>> +
>> +	/* Check if a partitions node exist */
>> +	parent = mtd->parent;
>> +	parent_dn = dev_of_node(&parent->dev);
>> +	if (!parent_dn)
>> +		return;
>> +
>> +	partitions = of_get_child_by_name(parent_dn, "partitions");
>> +	if (!partitions)
>> +		goto exit_parent;
>> +
>> +	/* Search if a partition is defined with the same name */
>> +	for_each_child_of_node(partitions, mtd_dn) {
>> +		/* Skip partition with no label */
>> +		mtd_name = of_get_property(mtd_dn, "label", &plen);
>> +		if (!mtd_name)
>> +			continue;
>> +
>> +		if (!strncmp(mtd->name, mtd_name, plen)) {
>> +			found = true;
>> +			break;
>> +		}
>> +	}
>> +
>> +	if (!found)
>> +		goto exit_partitions;
>> +
>> +	/* Set of_node only for nvmem */
>> +	if (of_device_is_compatible(mtd_dn, "nvmem-cells"))
>> +		mtd_set_of_node(mtd, mtd_dn);
>> +
>> +exit_partitions:
>> +	of_node_put(partitions);
>> +exit_parent:
>> +	of_node_put(parent_dn);
>> +}
>> +
>>   /**
>>    *	add_mtd_device - register an MTD device
>>    *	@mtd: pointer to new MTD device info structure
>> @@ -651,6 +699,7 @@ int add_mtd_device(struct mtd_info *mtd)
>>   	mtd->dev.devt = MTD_DEVT(i);
>>   	dev_set_name(&mtd->dev, "mtd%d", i);
>>   	dev_set_drvdata(&mtd->dev, mtd);
>> +	mtd_check_of_node(mtd);
>>   	of_node_get(mtd_get_of_node(mtd));
>>   	error = device_register(&mtd->dev);
>>   	if (error)
> 
> NXP GPMI NAND controller with 6 cmdline partitions meets refcount underflow
> with this patch. The of_node_put(parent_dn) doesn't work with cmdline parser and
> causes the issue.

Can you try:

diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
index 18aa54460d36..0b4ca0aa4132 100644
--- a/drivers/mtd/mtdcore.c
+++ b/drivers/mtd/mtdcore.c
@@ -562,7 +562,7 @@ static void mtd_check_of_node(struct mtd_info *mtd)
  	if (!mtd_is_partition(mtd))
  		return;
  	parent = mtd->parent;
-	parent_dn = dev_of_node(&parent->dev);
+	parent_dn = of_node_get(dev_of_node(&parent->dev));
  	if (!parent_dn)
  		return;



WARNING: multiple messages have this Message-ID (diff)
From: "Rafał Miłecki" <zajec5@gmail.com>
To: "han.xu" <han.xu@nxp.com>, Ansuel Smith <ansuelsmth@gmail.com>
Cc: "Miquel Raynal" <miquel.raynal@bootlin.com>,
	"Richard Weinberger" <richard@nod.at>,
	"Vignesh Raghavendra" <vigneshr@ti.com>,
	"Rob Herring" <robh+dt@kernel.org>,
	"Krzysztof Kozlowski" <krzysztof.kozlowski+dt@linaro.org>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Jens Axboe" <axboe@kernel.dk>, �ecki <rafal@milecki.pl>,
	"Manivannan Sadhasivam" <mani@kernel.org>,
	linux-mtd@lists.infradead.org, devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v5 3/3] mtd: core: introduce of support for dynamic partitions
Date: Mon, 17 Oct 2022 23:52:34 +0200	[thread overview]
Message-ID: <d98dd483-70b9-4bd1-0463-9e3343576955@gmail.com> (raw)
In-Reply-To: <20221017195820.ve6c3zj2civkswm6@umbrella>

On 17.10.2022 21:59, han.xu wrote:
> On 22/06/06 05:14PM, Ansuel Smith wrote:
>> We have many parser that register mtd partitions at runtime. One example
>> is the cmdlinepart or the smem-part parser where the compatible is defined
>> in the dts and the partitions gets detected and registered by the
>> parser. This is problematic for the NVMEM subsystem that requires an OF node
>> to detect NVMEM cells.
>>
>> To fix this problem, introduce an additional logic that will try to
>> assign an OF node to the MTD if declared.
>>
>> On MTD addition, it will be checked if the MTD has an OF node and if
>> not declared will check if a partition with the same label is
>> declared in DTS. If an exact match is found, the partition dynamically
>> allocated by the parser will have a connected OF node.
>>
>> The NVMEM subsystem will detect the OF node and register any NVMEM cells
>> declared statically in the DTS.
>>
>> Signed-off-by: Ansuel Smith <ansuelsmth@gmail.com>
>> ---
>>   drivers/mtd/mtdcore.c | 49 +++++++++++++++++++++++++++++++++++++++++++
>>   1 file changed, 49 insertions(+)
>>
>> diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
>> index 7731796024e0..807194efb580 100644
>> --- a/drivers/mtd/mtdcore.c
>> +++ b/drivers/mtd/mtdcore.c
>> @@ -546,6 +546,54 @@ static int mtd_nvmem_add(struct mtd_info *mtd)
>>   	return 0;
>>   }
>>   
>> +static void mtd_check_of_node(struct mtd_info *mtd)
>> +{
>> +	struct device_node *partitions, *parent_dn, *mtd_dn = NULL;
>> +	struct mtd_info *parent;
>> +	const char *mtd_name;
>> +	bool found = false;
>> +	int plen;
>> +
>> +	/* Check if MTD already has a device node */
>> +	if (dev_of_node(&mtd->dev))
>> +		return;
>> +
>> +	/* Check if a partitions node exist */
>> +	parent = mtd->parent;
>> +	parent_dn = dev_of_node(&parent->dev);
>> +	if (!parent_dn)
>> +		return;
>> +
>> +	partitions = of_get_child_by_name(parent_dn, "partitions");
>> +	if (!partitions)
>> +		goto exit_parent;
>> +
>> +	/* Search if a partition is defined with the same name */
>> +	for_each_child_of_node(partitions, mtd_dn) {
>> +		/* Skip partition with no label */
>> +		mtd_name = of_get_property(mtd_dn, "label", &plen);
>> +		if (!mtd_name)
>> +			continue;
>> +
>> +		if (!strncmp(mtd->name, mtd_name, plen)) {
>> +			found = true;
>> +			break;
>> +		}
>> +	}
>> +
>> +	if (!found)
>> +		goto exit_partitions;
>> +
>> +	/* Set of_node only for nvmem */
>> +	if (of_device_is_compatible(mtd_dn, "nvmem-cells"))
>> +		mtd_set_of_node(mtd, mtd_dn);
>> +
>> +exit_partitions:
>> +	of_node_put(partitions);
>> +exit_parent:
>> +	of_node_put(parent_dn);
>> +}
>> +
>>   /**
>>    *	add_mtd_device - register an MTD device
>>    *	@mtd: pointer to new MTD device info structure
>> @@ -651,6 +699,7 @@ int add_mtd_device(struct mtd_info *mtd)
>>   	mtd->dev.devt = MTD_DEVT(i);
>>   	dev_set_name(&mtd->dev, "mtd%d", i);
>>   	dev_set_drvdata(&mtd->dev, mtd);
>> +	mtd_check_of_node(mtd);
>>   	of_node_get(mtd_get_of_node(mtd));
>>   	error = device_register(&mtd->dev);
>>   	if (error)
> 
> NXP GPMI NAND controller with 6 cmdline partitions meets refcount underflow
> with this patch. The of_node_put(parent_dn) doesn't work with cmdline parser and
> causes the issue.

Can you try:

diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
index 18aa54460d36..0b4ca0aa4132 100644
--- a/drivers/mtd/mtdcore.c
+++ b/drivers/mtd/mtdcore.c
@@ -562,7 +562,7 @@ static void mtd_check_of_node(struct mtd_info *mtd)
  	if (!mtd_is_partition(mtd))
  		return;
  	parent = mtd->parent;
-	parent_dn = dev_of_node(&parent->dev);
+	parent_dn = of_node_get(dev_of_node(&parent->dev));
  	if (!parent_dn)
  		return;



______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

  reply	other threads:[~2022-10-17 21:52 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-06 15:14 [PATCH v5 0/3] Add nvmem support for dynamic partitions Ansuel Smith
2022-06-06 15:14 ` Ansuel Smith
2022-06-06 15:14 ` [PATCH v5 1/3] dt-bindings: mtd: partitions: Support label only partition Ansuel Smith
2022-06-06 15:14   ` Ansuel Smith
2022-06-09 18:32   ` Rob Herring
2022-06-09 18:32     ` Rob Herring
2022-06-09 19:57     ` Ansuel Smith
2022-06-09 19:57       ` Ansuel Smith
2022-06-10 17:02       ` Rob Herring
2022-06-10 17:02         ` Rob Herring
2022-06-11 12:36         ` Ansuel Smith
2022-06-11 12:36           ` Ansuel Smith
2022-06-13 19:25         ` Ansuel Smith
2022-06-13 19:25           ` Ansuel Smith
2022-06-06 15:14 ` [PATCH v5 2/3] dt-bindings: mtd: partitions: add additional example for qcom,smem-part Ansuel Smith
2022-06-06 15:14   ` Ansuel Smith
2022-06-09 18:33   ` Rob Herring
2022-06-09 18:33     ` Rob Herring
2022-06-06 15:14 ` [PATCH v5 3/3] mtd: core: introduce of support for dynamic partitions Ansuel Smith
2022-06-06 15:14   ` Ansuel Smith
2022-06-08 15:44   ` [mtd] a2af0cae87: BUG:kernel_NULL_pointer_dereference,address kernel test robot
2022-06-08 15:44     ` kernel test robot
2022-06-08 15:44     ` kernel test robot
2022-06-09 12:35     ` Miquel Raynal
2022-06-09 12:35       ` [mtd] a2af0cae87: BUG:kernel_NULL_pointer_dereference, address Miquel Raynal
2022-06-09 12:35       ` [mtd] a2af0cae87: BUG:kernel_NULL_pointer_dereference,address Miquel Raynal
2022-10-17 19:59   ` [PATCH v5 3/3] mtd: core: introduce of support for dynamic partitions han.xu
2022-10-17 19:59     ` han.xu
2022-10-17 21:52     ` Rafał Miłecki [this message]
2022-10-17 21:52       ` Rafał Miłecki
2022-10-18  2:48       ` han.xu
2022-10-18  2:48         ` han.xu

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=d98dd483-70b9-4bd1-0463-9e3343576955@gmail.com \
    --to=zajec5@gmail.com \
    --cc=ansuelsmth@gmail.com \
    --cc=axboe@kernel.dk \
    --cc=devicetree@vger.kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=han.xu@nxp.com \
    --cc=krzysztof.kozlowski+dt@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mtd@lists.infradead.org \
    --cc=mani@kernel.org \
    --cc=miquel.raynal@bootlin.com \
    --cc=rafal@milecki.pl \
    --cc=richard@nod.at \
    --cc=robh+dt@kernel.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 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.