mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Grant Likely <grant.likely@secretlab.ca>
To: Pantelis Antoniou <panto@antoniou-consulting.com>
Cc: Rob Herring <robherring2@gmail.com>,
	Stephen Warren <swarren@wwwdotorg.org>,
	Matt Porter <matt.porter@linaro.org>,
	Koen Kooi <koen@dominion.thruhere.net>,
	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>,
	devicetree@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 5/5] OF: Introduce utility helper functions
Date: Wed, 13 Nov 2013 10:34:59 +0900	[thread overview]
Message-ID: <20131113013459.D3886C40F49@trevor.secretlab.ca> (raw)
In-Reply-To: <89491720-464B-47EC-A888-9CDE29B035F5@antoniou-consulting.com>

On Tue, 12 Nov 2013 11:39:08 +0100, Pantelis Antoniou <panto@antoniou-consulting.com> wrote:
> Hi Grant,
> 
> On Nov 11, 2013, at 5:37 PM, Grant Likely wrote:
> 
> > On Tue,  5 Nov 2013 19:50:16 +0200, Pantelis Antoniou <panto@antoniou-consulting.com> wrote:
> >> Introduce helper functions for working with the live DT tree.
> >> 
> >> __of_free_property() frees a dynamically created property
> >> __of_free_tree() recursively frees a device node tree
> >> __of_copy_property() copies a property dynamically
> >> __of_create_empty_node() creates an empty node
> >> __of_find_node_by_full_name() finds the node with the full name
> >> and
> >> of_multi_prop_cmp() performs a multi property compare but without
> >> having to take locks.
> >> 
> >> Signed-off-by: Pantelis Antoniou <panto@antoniou-consulting.com>
> > 
> > So, this all looks like private stuff, or stuff that belongs in
> > drivers/of/base.c. Can you move stuff around. I've made more comments
> > below.
> > 
> 
> Placement is no big issue;
> 
> > g.
> > 
> 
> [snip]
> 
> >> +	} else {
> >> +		pr_warn("%s: node %p cannot be freed; memory is gone\n",
> >> +				__func__, node);
> >> +	}
> >> +}
> > 
> > All of the above is potentially dangerous. There is no way to determine
> > if anything still holds a reference to a node. The proper way to handle
> > removal of properties is to have a release method when the last
> > of_node_put is called.
> > 
> 
> This is safe, and expected to be called only on a dynamically created tree,
> that's what all the checks against OF_DYNAMIC guard against.
> 
> It is not ever meant to be called on an arbitrary tree, created by unflattening
> a blob.

I am talking about when being used on a dynamic tree. The problem is
when a driver or other code holds a reference to a dynamic nodes, but
doesn't release it correctly. The memory must not be freed until all of
the references are relased. OF_DYNAMIC doesn't actually help in that
case, and it is the reason for of_node_get()/of_node_put()

> Perhaps we could have a switch to control whether an unflattened tree is 
> created dynamically and then freeing/releasing will work.
> 
> kobject-ifcation will require it anyway, don't you agree? 

Yes. Kobjectifcation will also take care of the release method.

> 
> >> +
> >> +/**
> >> + * __of_copy_property - Copy a property dynamically.
> >> + * @prop:	Property to copy
> >> + * @flags:	Allocation flags (typically pass GFP_KERNEL)
> >> + *
> >> + * Copy a property by dynamically allocating the memory of both the
> >> + * property stucture and the property name & contents. The property's
> >> + * flags have the OF_DYNAMIC bit set so that we can differentiate between
> >> + * dynamically allocated properties and not.
> >> + * Returns the newly allocated property or NULL on out of memory error.
> >> + */
> > 
> > What do you intend the use-case to be for this function? Will the
> > duplicated property be immediately modified? If so, what happens if the
> > property needs to be grown in size?
> > 
> 
> No, the property will no be modified. If it needs to grow it will be moved to 
> deadprops (since we don't track refs to props) and a new one will be allocated.
> 
> >> +struct property *__of_copy_property(const struct property *prop, gfp_t flags)
> >> +{
> >> +	struct property *propn;
> >> +
> >> +	propn = kzalloc(sizeof(*prop), flags);
> >> +	if (propn == NULL)
> >> +		return NULL;
> >> +
> >> +	propn->name = kstrdup(prop->name, flags);
> >> +	if (propn->name == NULL)
> >> +		goto err_fail_name;
> >> +
> >> +	if (prop->length > 0) {
> >> +		propn->value = kmalloc(prop->length, flags);
> >> +		if (propn->value == NULL)
> >> +			goto err_fail_value;
> >> +		memcpy(propn->value, prop->value, prop->length);
> >> +		propn->length = prop->length;
> >> +	}
> >> +
> >> +	/* mark the property as dynamic */
> >> +	of_property_set_flag(propn, OF_DYNAMIC);
> >> +
> >> +	return propn;
> >> +
> >> +err_fail_value:
> >> +	kfree(propn->name);
> >> +err_fail_name:
> >> +	kfree(propn);
> >> +	return NULL;
> >> +}
> >> +
> >> +/**
> >> + * __of_create_empty_node - Create an empty device node dynamically.
> >> + * @name:	Name of the new device node
> >> + * @type:	Type of the new device node
> >> + * @full_name:	Full name of the new device node
> >> + * @phandle:	Phandle of the new device node
> >> + * @flags:	Allocation flags (typically pass GFP_KERNEL)
> >> + *
> >> + * Create an empty device tree node, suitable for further modification.
> >> + * The node data are dynamically allocated and all the node flags
> >> + * have the OF_DYNAMIC & OF_DETACHED bits set.
> >> + * Returns the newly allocated node or NULL on out of memory error.
> >> + */
> >> +struct device_node *__of_create_empty_node(
> >> +		const char *name, const char *type, const char *full_name,
> >> +		phandle phandle, gfp_t flags)
> > 
> > I would like to see a user of this function in the core DT paths that
> > allocate nodes. It will make for less chance of breakage if the fdt and
> > pdt paths change something, but this function isn't updated.
> > 
> 
> Hmm, where do you think it can be used? Perhaps the unflatten parts?

Yes. In unflattening the tree, fdt.c and in extracting the trea from
real OFW (pdt.c).

> 
> > g.
> > 
> >> +{
> >> +	struct device_node *node;
> >> +
> >> +	node = kzalloc(sizeof(*node), flags);
> >> +	if (node == NULL)
> >> +		return NULL;
> >> +
> >> +	node->name = kstrdup(name, flags);
> >> +	if (node->name == NULL)
> >> +		goto err_return;
> >> +
> >> +	node->type = kstrdup(type, flags);
> >> +	if (node->type == NULL)
> >> +		goto err_return;
> >> +
> >> +	node->full_name = kstrdup(full_name, flags);
> >> +	if (node->type == NULL)
> >> +		goto err_return;
> > 
> > Again, who do you expect the user of this function to be? If it is part
> > of unflattening an overlay tree, is there a reason that the passed in
> > names cannot be used directly instead of kmallocing them?
> > 
> 
> I want to be able to get rid of the blob eventually; I don't need to keep
> dragging it around.

Why? It really doesn't hurt and it means data does not need to be
copied.

> 
> >> +
> >> +	node->phandle = phandle;
> >> +	kref_init(&node->kref);
> >> +	of_node_set_flag(node, OF_DYNAMIC);
> >> +	of_node_set_flag(node, OF_DETACHED);
> >> +
> >> +	return node;
> >> +
> >> +err_return:
> >> +	__of_free_tree(node);
> >> +	return NULL;
> >> +}
> >> +
> >> +/**
> >> + * __of_find_node_by_full_name - Find a node with the full name recursively
> >> + * @node:	Root of the tree to perform the search
> >> + * @full_name:	Full name of the node to find.
> >> + *
> >> + * Find a node with the give full name by recursively following any of 
> >> + * the child node links.
> >> + * Returns the matching node, or NULL if not found.
> >> + * Note that the devtree lock is not taken, so this function is only
> >> + * safe to call on either detached trees, or when devtree lock is already
> >> + * taken.
> >> + */
> >> +struct device_node *__of_find_node_by_full_name(struct device_node *node,
> >> +		const char *full_name)
> > 
> > Sounds like something that should be in drivers/of/base.c
> > 
> 
> Yes.
> 
> >> +{
> >> +	struct device_node *child, *found;
> >> +
> >> +	if (node == NULL)
> >> +		return NULL;
> >> +
> >> +	/* check */
> >> +	if (of_node_cmp(node->full_name, full_name) == 0)
> >> +		return node;
> >> +
> >> +	__for_each_child_of_node(node, child) {
> >> +		found = __of_find_node_by_full_name(child, full_name);
> >> +		if (found != NULL)
> >> +			return found;
> >> +	}
> > 
> > I'm not a huge fan of recursive calls. Why doesn't a slightly modified
> > of_fund_node_by_name() work here?
> > 
> > I agree that of_find_node_by_name is not particularly elegant and it
> > would be good to have something more efficient, but it works and
> > following the same method would be consistent.
> > 
> 
> of_find_node_by_name is not iterating on the passed tree and it's subnodes,
> it is a global search starting from:
> 
> 	np = from ? from->allnext : of_allnodes;
> 
> This is just broken, since it depends on unflattening creating nodes in the
> allnodes list in sequence. I.e. that child nodes are after the parent node.
> This assumption breaks down when doing dynamic insertions of nodes.
> A detached tree in not linked on of_allnodes anyway.

It would be good to have a root-of-tree structure for both the core tree
and the overlay trees so that a common iterator can be implemented.

g.


  reply	other threads:[~2013-11-13  3:55 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-11-05 17:50 [PATCH 0/5] OF: Fixes in preperation of DT overlays Pantelis Antoniou
2013-11-05 17:50 ` [PATCH 1/5] OF: Clear detach flag on attach Pantelis Antoniou
2013-11-05 19:43   ` Gerhard Sittig
2013-11-05 20:03     ` Pantelis Antoniou
2013-11-06  8:46       ` Alexander Sverdlin
2013-11-06  8:49         ` Pantelis Antoniou
2013-11-06 10:05           ` Alexander Sverdlin
2013-11-11 16:05           ` Grant Likely
2013-11-05 17:50 ` [PATCH 2/5] OF: Introduce device tree node flag helpers Pantelis Antoniou
2013-11-11 16:05   ` Grant Likely
2013-11-05 17:50 ` [PATCH 3/5] OF: export of_property_notify Pantelis Antoniou
2013-11-05 18:12   ` Matt Porter
2013-11-05 18:15     ` Pantelis Antoniou
2013-11-11 16:06   ` Grant Likely
2013-11-12  9:31     ` Pantelis Antoniou
2013-11-05 17:50 ` [PATCH 4/5] OF: Export all DT proc update functions Pantelis Antoniou
2013-11-11 16:09   ` Grant Likely
2013-11-12 10:21     ` Pantelis Antoniou
2013-11-05 17:50 ` [PATCH 5/5] OF: Introduce utility helper functions Pantelis Antoniou
     [not found]   ` < 20131111163743.D136CC42330@trevor.secretlab.ca>
     [not found]     ` < 89491720-464B-47EC-A888-9CDE29B035F5@antoniou-consulting.com>
     [not found]       ` < 20131113013459.D3886C40F49@trevor.secretlab.ca>
     [not found]         ` < F88335D9-6515-4E48-A21F-25039F16754A@antoniou-consulting.com>
     [not found]           ` < 20131114014420.E871CC4054D@trevor.secretlab.ca>
2013-11-06  9:21   ` Ionut Nicu
2013-11-06  9:34     ` Pantelis Antoniou
2013-11-06 14:53       ` Guenter Roeck
2013-11-06 15:08         ` Pantelis Antoniou
2013-11-11 16:12         ` Grant Likely
2013-11-11 16:37   ` Grant Likely
2013-11-12 10:39     ` Pantelis Antoniou
2013-11-13  1:34       ` Grant Likely [this message]
2013-11-13  9:03         ` Pantelis Antoniou
2013-11-14  1:44           ` Grant Likely
2013-11-14  9:51             ` Pantelis Antoniou
2013-11-15  6:27               ` Grant Likely
2013-11-16 12:23                 ` Pantelis Antoniou
2013-11-06 15:41 ` [PATCH 0/5] OF: Fixes in preperation of DT overlays Alexander Sverdlin

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=20131113013459.D3886C40F49@trevor.secretlab.ca \
    --to=grant.likely@secretlab.ca \
    --cc=Alison_Chaiken@mentor.com \
    --cc=alexander.sverdlin@nsn.com \
    --cc=delicious.quinoa@gmail.com \
    --cc=devicetree@vger.kernel.org \
    --cc=dinh.linux@gmail.com \
    --cc=dirk.behme@gmail.com \
    --cc=ioan.nicu.ext@nsn.com \
    --cc=jluebbe@lasnet.de \
    --cc=koen@dominion.thruhere.net \
    --cc=linux-kernel@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=panto@antoniou-consulting.com \
    --cc=robherring2@gmail.com \
    --cc=s.hauer@pengutronix.de \
    --cc=swarren@wwwdotorg.org \
    /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

all inboxes | Powered by JetHome®