All of lore.kernel.org
 help / color / mirror / Atom feed
From: Pantelis Antoniou <pantelis.antoniou@konsulko.com>
To: Grant Likely <grant.likely@secretlab.ca>
Cc: Rob Herring <robherring2@gmail.com>,
	Stephen Warren <swarren@wwwdotorg.org>,
	Matt Porter <matt.porter@linaro.org>,
	Koen Kooi <koen@dominion.thruhere.net>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Alison Chaiken <Alison_Chaiken@mentor.com>,
	Dinh Nguyen <dinh.linux@gmail.com>, Jan Lubbe <jluebbe@lasnet.de>,
	Alexander Sverdlin <alexander.sverdlin@nsn.com>,
	Michael Stickel <ms@mycable.de>,
	Guenter Roeck <linux@roeck-us.net>,
	Dirk Behme <dirk.behme@gmail.com>,
	Alan Tull <delicious.quinoa@gmail.com>,
	Sascha Hauer <s.hauer@pengutronix.de>,
	Michael Bohan <mbohan@codeaurora.org>,
	Ionut Nicu <ioan.nicu.ext@nsn.com>,
	Michal Simek <monstr@monstr.eu>,
	Matt Ranostay <mranostay@gmail.com>,
	Joel Becker <jlbec@evilplan.org>,
	devicetree@vger.kernel.org, Wolfram Sang <wsa@the-dreams.de>,
	linux-i2c@vger.kernel.org, Mark Brown <broonie@kernel.org>,
	linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org,
	Pete Popov <pete.popov@konsulko.com>,
	Dan Malek <dan.malek@konsulko.com>,
	Georgi Vlaev <georgi.vlaev@konsulko.com>
Subject: Re: [PATCH 1/6] of: Do not free memory at of_node_release
Date: Tue, 24 Jun 2014 17:23:35 +0300	[thread overview]
Message-ID: <0E39F7E8-1141-4D6D-A251-3F0DDEA9AA29@konsulko.com> (raw)
In-Reply-To: <20140624141004.9E989C40B84@trevor.secretlab.ca>

Hi Grant,

On Jun 24, 2014, at 5:10 PM, Grant Likely wrote:

> On Sun, 22 Jun 2014 12:40:34 +0300, Pantelis Antoniou <pantelis.antoniou@konsulko.com> wrote:
>> The life-cycle of nodes and properties does not allow us to release
>> the memory taken by a device_node. Pointer to properties and nodes
>> might still be in use in drivers, so any memory free'ing is dangerous.
>> 
>> Simply move all the properties to the deadprops list, and the node
>> itself to of_alldeadnodes until the life-cycles issues are resolved.
> 
> Ummm. this looks wrong. The release function is supposed to be the place
> to do the freeing, and with our discussion the other day about moving to
> rcu, but keeping of_node_get/put() for anything that needs to hold a
> long term reference, that means the lifecycle issues are pretty much
> resolved. I don't think this patch is necessary.
> 

Well, I thought about it too. This is the culmination of that process :)

The problem is not the node life-cycle, it's the properties. We can't
tell for sure who and where has a pointer to the properties of the node,
and when we free the node, the current code iterates over the properties
list and frees them. This works only because in my tests, of_node_release
only gets called when using overlays created nodes.

It is your call, but this patch makes me sleep easier at nights :)

> g.
> 

Regards

-- Pantelis

>> 
>> Signed-off-by: Pantelis Antoniou <pantelis.antoniou@konsulko.com>
>> ---
>> drivers/of/base.c | 43 ++++++++++++++++++++++++++++---------------
>> 1 file changed, 28 insertions(+), 15 deletions(-)
>> 
>> diff --git a/drivers/of/base.c b/drivers/of/base.c
>> index b986480..d3493e1 100644
>> --- a/drivers/of/base.c
>> +++ b/drivers/of/base.c
>> @@ -110,17 +110,27 @@ static inline struct device_node *kobj_to_device_node(struct kobject *kobj)
>> 	return container_of(kobj, struct device_node, kobj);
>> }
>> 
>> +static struct device_node *of_alldeadnodes;
>> +static DEFINE_RAW_SPINLOCK(deadtree_lock);
>> +
>> /**
>>  *	of_node_release - release a dynamically allocated node
>>  *	@kref:  kref element of the node to be released
>>  *
>>  *	In of_node_put() this function is passed to kref_put()
>>  *	as the destructor.
>> + *
>> + *	Note that due to the way that node and property life-cycles
>> + *	are not completely managed, we can't free the memory of
>> + *	a node at will. Instead we move the node to the dead nodes
>> + *	list where it will remain until the life-cycle issues are
>> + *	resolved.
>>  */
>> static void of_node_release(struct kobject *kobj)
>> {
>> 	struct device_node *node = kobj_to_device_node(kobj);
>> -	struct property *prop = node->properties;
>> +	struct property *prop;
>> +	unsigned long flags;
>> 
>> 	/* We should never be releasing nodes that haven't been detached. */
>> 	if (!of_node_check_flag(node, OF_DETACHED)) {
>> @@ -129,24 +139,27 @@ static void of_node_release(struct kobject *kobj)
>> 		return;
>> 	}
>> 
>> -	if (!of_node_check_flag(node, OF_DYNAMIC))
>> +	pr_info("%s: dead node \"%s\"\n", __func__, node->full_name);
>> +
>> +	/* we should not be trying to release the root */
>> +	if (WARN_ON(node == of_allnodes))
>> 		return;
>> 
>> -	while (prop) {
>> -		struct property *next = prop->next;
>> -		kfree(prop->name);
>> -		kfree(prop->value);
>> -		kfree(prop);
>> -		prop = next;
>> +	/* can't use devtree lock; at of_node_put caller might be holding it */
>> +	raw_spin_lock_irqsave(&deadtree_lock, flags);
>> 
>> -		if (!prop) {
>> -			prop = node->deadprops;
>> -			node->deadprops = NULL;
>> -		}
>> +	/* move all properties to dead properties */
>> +	while ((prop = node->properties) != NULL) {
>> +		node->properties = prop->next;
>> +		prop->next = node->deadprops;
>> +		node->deadprops = prop;
>> 	}
>> -	kfree(node->full_name);
>> -	kfree(node->data);
>> -	kfree(node);
>> +
>> +	/* move node to alldeadnodes */
>> +	node->allnext = of_alldeadnodes;
>> +	of_alldeadnodes = node;
>> +
>> +	raw_spin_unlock_irqrestore(&deadtree_lock, flags);
>> }
>> 
>> /**
>> -- 
>> 1.7.12
>> 
> 

  reply	other threads:[~2014-06-24 14:23 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-22  9:40 [PATCH 0/6] OF: Fixes preparing transactions/overlays Pantelis Antoniou
2014-06-22  9:40 ` [PATCH 1/6] of: Do not free memory at of_node_release Pantelis Antoniou
2014-06-24 14:10   ` Grant Likely
2014-06-24 14:23     ` Pantelis Antoniou [this message]
2014-06-24 20:21       ` Grant Likely
2014-06-24 20:23         ` Pantelis Antoniou
2014-06-24 20:33           ` Guenter Roeck
2014-06-24 21:02             ` Pantelis Antoniou
2014-06-24 23:20               ` Guenter Roeck
2014-06-25 19:24           ` Grant Likely
2014-06-24 14:53     ` Guenter Roeck
2014-06-22  9:40 ` [PATCH 2/6] OF: Add [__]of_find_node_by_full_name Pantelis Antoniou
2014-06-23 17:58   ` Guenter Roeck
2014-06-23 18:00     ` Pantelis Antoniou
2014-06-24 13:55       ` Grant Likely
2014-06-24 13:55         ` Grant Likely
2014-06-24 13:55         ` Grant Likely
2014-06-24 14:12   ` Grant Likely
2014-06-24 14:23     ` Pantelis Antoniou
2014-06-22  9:40 ` [PATCH 3/6] of: platform: Fix and export of_platform_device_destroy Pantelis Antoniou
2014-06-24 14:23   ` Grant Likely
2014-06-24 15:13   ` Grant Likely
2014-06-22  9:40 ` [PATCH 4/6] OF: Export a number of __of_* methods Pantelis Antoniou
2014-06-24 19:27   ` Grant Likely
2014-06-24 19:38     ` Pantelis Antoniou
2014-06-24 20:19       ` Grant Likely
2014-06-22  9:40 ` [PATCH 5/6] OF: Utility helper functions for dynamic nodes Pantelis Antoniou
2014-06-23 16:26   ` Alexander Sverdlin
2014-06-23 16:26     ` Alexander Sverdlin
2014-06-23 16:26     ` Alexander Sverdlin
2014-06-23 16:57     ` Pantelis Antoniou
2014-06-23 18:33       ` Ioan Nicu
2014-06-23 19:13         ` Pantelis Antoniou
2014-06-23 19:48           ` Guenter Roeck
2014-06-23 19:48             ` Guenter Roeck
2014-06-23 19:48             ` Guenter Roeck
2014-06-23 20:39             ` Ioan Nicu
2014-06-24  9:08             ` Pantelis Antoniou
2014-06-24 13:46               ` Rob Herring
2014-06-24 13:53                 ` Alexander Sverdlin
2014-06-24 14:49                   ` Rob Herring
2014-06-24 15:43                     ` Alexander Sverdlin
2014-06-24 15:59                       ` Pantelis Antoniou
2014-06-24 18:23                         ` Ioan Nicu
2014-06-24 18:31                           ` Ioan Nicu
2014-06-24 18:43                           ` Pantelis Antoniou
2014-06-24  8:12           ` Alexander Sverdlin
2014-06-24  8:12             ` Alexander Sverdlin
2014-06-24  8:12             ` Alexander Sverdlin
2014-06-24  8:19             ` Pantelis Antoniou
2014-06-24  8:38               ` Alexander Sverdlin
2014-06-24  8:54                 ` Pantelis Antoniou
2014-06-24  9:00                   ` Alexander Sverdlin
2014-06-24  9:09                     ` Pantelis Antoniou
2014-06-24  8:10         ` Alexander Sverdlin
2014-06-24  8:10           ` Alexander Sverdlin
2014-06-24  8:10           ` Alexander Sverdlin
2014-06-25  9:09           ` Grant Likely
2014-06-25  9:09             ` Grant Likely
2014-06-25  9:09             ` Grant Likely
2014-06-25 11:14   ` Grant Likely
2014-06-25 11:22     ` Pantelis Antoniou
2014-06-22  9:40 ` [PATCH 6/6] of: Introduce tree change __foo_post methods Pantelis Antoniou

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=0E39F7E8-1141-4D6D-A251-3F0DDEA9AA29@konsulko.com \
    --to=pantelis.antoniou@konsulko.com \
    --cc=Alison_Chaiken@mentor.com \
    --cc=alexander.sverdlin@nsn.com \
    --cc=broonie@kernel.org \
    --cc=dan.malek@konsulko.com \
    --cc=delicious.quinoa@gmail.com \
    --cc=devicetree@vger.kernel.org \
    --cc=dinh.linux@gmail.com \
    --cc=dirk.behme@gmail.com \
    --cc=georgi.vlaev@konsulko.com \
    --cc=grant.likely@secretlab.ca \
    --cc=gregkh@linuxfoundation.org \
    --cc=ioan.nicu.ext@nsn.com \
    --cc=jlbec@evilplan.org \
    --cc=jluebbe@lasnet.de \
    --cc=koen@dominion.thruhere.net \
    --cc=linux-i2c@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-spi@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --cc=matt.porter@linaro.org \
    --cc=mbohan@codeaurora.org \
    --cc=monstr@monstr.eu \
    --cc=mranostay@gmail.com \
    --cc=ms@mycable.de \
    --cc=pete.popov@konsulko.com \
    --cc=robherring2@gmail.com \
    --cc=s.hauer@pengutronix.de \
    --cc=swarren@wwwdotorg.org \
    --cc=wsa@the-dreams.de \
    /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.