devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Lizhi Hou <lizhi.hou@amd.com>
To: <linux-pci@vger.kernel.org>, <devicetree@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>, <robh@kernel.org>,
	<frowand.list@gmail.com>, <helgaas@kernel.org>
Cc: "Clément Léger" <clement.leger@bootlin.com>,
	max.zhen@amd.com, sonal.santan@amd.com, larry.liu@amd.com,
	brian.xu@amd.com, stefano.stabellini@xilinx.com, trix@redhat.com
Subject: [PATCH RFC 1/2] of: dynamic: add of_node_alloc()
Date: Mon, 29 Aug 2022 14:43:36 -0700	[thread overview]
Message-ID: <1661809417-11370-2-git-send-email-lizhi.hou@amd.com> (raw)
In-Reply-To: <1661809417-11370-1-git-send-email-lizhi.hou@amd.com>

From: Clément Léger <clement.leger@bootlin.com>

Add of_node_alloc() which allows to create nodes. The node full_name
field is allocated as part of the node allocation and the kfree() for
this field is checked at release time to allow users using their own
allocated name.

Signed-off-by: Clément Léger <clement.leger@bootlin.com>
---
 drivers/of/dynamic.c | 50 +++++++++++++++++++++++++++++++++++---------
 include/linux/of.h   |  7 +++++++
 2 files changed, 47 insertions(+), 10 deletions(-)

diff --git a/drivers/of/dynamic.c b/drivers/of/dynamic.c
index cd3821a6444f..0e08283fd4fd 100644
--- a/drivers/of/dynamic.c
+++ b/drivers/of/dynamic.c
@@ -362,10 +362,49 @@ void of_node_release(struct kobject *kobj)
 	fwnode_links_purge(of_fwnode_handle(node));
 
 	kfree(node->full_name);
+	if (node->full_name != (const char *)(node + 1))
+		kfree(node->full_name);
+
 	kfree(node->data);
 	kfree(node);
 }
 
+/**
+ * of_node_alloc - Allocate a node dynamically.
+ * @name:	Node name
+ *
+ * Create a node by dynamically allocating the memory of both the
+ * node structure and the node name & contents. The node's
+ * flags have the OF_DYNAMIC & OF_DETACHED bit set so that we can
+ * differentiate between dynamically allocated nodes and not.
+ *
+ * Return: The newly allocated node or NULL on out of memory error.
+ */
+struct device_node *of_node_alloc(const char *name)
+{
+	struct device_node *node;
+	int name_len = 0;
+
+	if (name)
+		name_len = strlen(name) + 1;
+
+	node = kzalloc(sizeof(*node) + name_len, GFP_KERNEL);
+	if (!node)
+		return NULL;
+
+	if (name) {
+		node->full_name = (const char *)(node + 1);
+		strcpy((char *)node->full_name, name);
+	}
+
+	of_node_set_flag(node, OF_DYNAMIC);
+	of_node_set_flag(node, OF_DETACHED);
+	of_node_init(node);
+
+	return node;
+}
+EXPORT_SYMBOL(of_node_alloc);
+
 /**
  * __of_prop_dup - Copy a property dynamically.
  * @prop:	Property to copy
@@ -426,18 +465,9 @@ struct device_node *__of_node_dup(const struct device_node *np,
 {
 	struct device_node *node;
 
-	node = kzalloc(sizeof(*node), GFP_KERNEL);
+	node = of_node_alloc(full_name);
 	if (!node)
 		return NULL;
-	node->full_name = kstrdup(full_name, GFP_KERNEL);
-	if (!node->full_name) {
-		kfree(node);
-		return NULL;
-	}
-
-	of_node_set_flag(node, OF_DYNAMIC);
-	of_node_set_flag(node, OF_DETACHED);
-	of_node_init(node);
 
 	/* Iterate over and duplicate all properties */
 	if (np) {
diff --git a/include/linux/of.h b/include/linux/of.h
index 766d002bddb9..fc71e0355f05 100644
--- a/include/linux/of.h
+++ b/include/linux/of.h
@@ -1462,6 +1462,8 @@ enum of_reconfig_change {
 };
 
 #ifdef CONFIG_OF_DYNAMIC
+struct device_node *of_node_alloc(const char *name);
+
 extern int of_reconfig_notifier_register(struct notifier_block *);
 extern int of_reconfig_notifier_unregister(struct notifier_block *);
 extern int of_reconfig_notify(unsigned long, struct of_reconfig_data *rd);
@@ -1506,6 +1508,11 @@ static inline int of_changeset_update_property(struct of_changeset *ocs,
 	return of_changeset_action(ocs, OF_RECONFIG_UPDATE_PROPERTY, np, prop);
 }
 #else /* CONFIG_OF_DYNAMIC */
+static inline struct device_node *of_node_alloc(const char *name)
+{
+	return NULL;
+}
+
 static inline int of_reconfig_notifier_register(struct notifier_block *nb)
 {
 	return -EINVAL;
-- 
2.27.0


  reply	other threads:[~2022-08-29 21:44 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-29 21:43 [PATCH RFC 0/2] Generate device tree node for pci devices Lizhi Hou
2022-08-29 21:43 ` Lizhi Hou [this message]
2022-09-16 23:15   ` [PATCH RFC 1/2] of: dynamic: add of_node_alloc() Frank Rowand
2022-08-29 21:43 ` [PATCH RFC 2/2] pci: create device tree node for selected devices Lizhi Hou
2022-09-02 18:54   ` Rob Herring
2022-09-12  6:33     ` Frank Rowand
2022-09-13  7:03       ` Frank Rowand
2022-09-16 23:20         ` Frank Rowand
2022-09-13  5:49     ` Lizhi Hou
2022-09-02 20:43 ` [PATCH RFC 0/2] Generate device tree node for pci devices Bjorn Helgaas
2022-09-09 23:06   ` Lizhi Hou
2022-09-13  7:00 ` Frank Rowand
2022-09-13 17:10   ` Lizhi Hou
2022-09-13 17:41     ` Frank Rowand
2022-09-13 21:02       ` Lizhi Hou
2022-09-17  2:23         ` Frank Rowand
2022-09-17 18:36           ` Tom Rix
2022-09-20  3:12             ` Frank Rowand
2022-09-26  3:03               ` Sonal Santan
2022-10-14 21:25                 ` Frank Rowand
2022-10-10  8:42       ` [PATCH RFC 0/2] Generate device tree node for pci devicesgain, Clément Léger
2022-10-13  6:05         ` Frank Rowand
2022-10-13  8:02           ` Clément Léger
2022-10-13 17:28             ` Frank Rowand
2022-10-14 17:33               ` Rob Herring
2022-10-14 18:52                 ` Frank Rowand
2022-10-17  7:18                   ` Clément Léger
2022-10-26 21:20                     ` Sonal Santan
2022-09-14 13:35 ` [PATCH RFC 0/2] Generate device tree node for pci devices Jeremi Piotrowski
2022-09-14 18:08   ` Rob Herring
2022-09-16 23:15 ` Frank Rowand
2022-09-26 22:44   ` Rob Herring
2022-09-30 19:29     ` Sonal Santan
2022-10-06 15:10       ` Rob Herring
2022-10-07 22:45         ` Sonal Santan
2022-10-10  8:58           ` Clément Léger
2022-10-13  6:08             ` Frank Rowand

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=1661809417-11370-2-git-send-email-lizhi.hou@amd.com \
    --to=lizhi.hou@amd.com \
    --cc=brian.xu@amd.com \
    --cc=clement.leger@bootlin.com \
    --cc=devicetree@vger.kernel.org \
    --cc=frowand.list@gmail.com \
    --cc=helgaas@kernel.org \
    --cc=larry.liu@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=max.zhen@amd.com \
    --cc=robh@kernel.org \
    --cc=sonal.santan@amd.com \
    --cc=stefano.stabellini@xilinx.com \
    --cc=trix@redhat.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).