From: "Clément Léger" <clement.leger@bootlin.com>
To: Michael Ellerman <mpe@ellerman.id.au>,
Benjamin Herrenschmidt <benh@kernel.crashing.org>,
Paul Mackerras <paulus@samba.org>,
Rob Herring <robh+dt@kernel.org>,
Frank Rowand <frowand.list@gmail.com>,
Nathan Lynch <nathanl@linux.ibm.com>,
Laurent Dufour <ldufour@linux.ibm.com>,
Daniel Henrique Barboza <danielhb413@gmail.com>,
David Gibson <david@gibson.dropbear.id.au>,
Andrew Morton <akpm@linux-foundation.org>,
David Hildenbrand <david@redhat.com>,
Ohhoon Kwon <ohoono.kwon@samsung.com>,
"Aneesh Kumar K.V" <aneesh.kumar@linux.ibm.com>,
YueHaibing <yuehaibing@huawei.com>
Cc: devicetree@vger.kernel.org,
"Clément Léger" <clement.leger@bootlin.com>,
"Steen Hegelund" <steen.hegelund@microchip.com>,
linux-kernel@vger.kernel.org, "Lizhi Hou" <lizhi.hou@xilinx.com>,
"Allan Nielsen" <allan.nielsen@microchip.com>,
"Thomas Petazzoni" <thomas.petazzoni@bootlin.com>,
"Bjorn Helgaas" <helgaas@kernel.org>,
linuxppc-dev@lists.ozlabs.org,
"Horatiu Vultur" <horatiu.vultur@microchip.com>
Subject: [PATCH v2 3/4] of: dynamic: add of_node_alloc()
Date: Wed, 1 Jun 2022 10:18:00 +0200 [thread overview]
Message-ID: <20220601081801.348571-4-clement.leger@bootlin.com> (raw)
In-Reply-To: <20220601081801.348571-1-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 | 52 ++++++++++++++++++++++++++++++++++----------
include/linux/of.h | 6 +++++
2 files changed, 47 insertions(+), 11 deletions(-)
diff --git a/drivers/of/dynamic.c b/drivers/of/dynamic.c
index c0dcbea31d28..67636aafe810 100644
--- a/drivers/of/dynamic.c
+++ b/drivers/of/dynamic.c
@@ -359,7 +359,9 @@ void of_node_release(struct kobject *kobj)
property_list_free(node->deadprops);
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);
}
@@ -426,6 +428,43 @@ struct property *of_property_alloc(const char *name, const void *value,
}
EXPORT_SYMBOL(of_property_alloc);
+/**
+ * of_node_alloc - Allocate a node dynamically.
+ * @name: Node name
+ * @allocflags: Allocation flags (typically pass GFP_KERNEL)
+ *
+ * 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, gfp_t allocflags)
+{
+ struct device_node *node;
+ int name_len = 0;
+
+ if (name)
+ name_len = strlen(name) + 1;
+
+ node = kzalloc(sizeof(*node) + name_len, allocflags);
+ 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_node_dup() - Duplicate or create an empty device node dynamically.
* @np: if not NULL, contains properties to be duplicated in new node
@@ -442,18 +481,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, GFP_KERNEL);
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 f1966f3c3847..c18da685ade1 100644
--- a/include/linux/of.h
+++ b/include/linux/of.h
@@ -1464,6 +1464,7 @@ enum of_reconfig_change {
};
#ifdef CONFIG_OF_DYNAMIC
+struct device_node *of_node_alloc(const char *name, gfp_t allocflags);
struct property *of_property_alloc(const char *name, const void *value,
size_t len, gfp_t allocflags);
void of_property_free(const struct property *prop);
@@ -1512,6 +1513,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,
+ gfp_t allocflags)
+{
+ return NULL;
+}
static inline
struct property *of_property_alloc(const char *name, const void *value,
--
2.36.0
WARNING: multiple messages have this Message-ID (diff)
From: "Clément Léger" <clement.leger@bootlin.com>
To: Michael Ellerman <mpe@ellerman.id.au>,
Benjamin Herrenschmidt <benh@kernel.crashing.org>,
Paul Mackerras <paulus@samba.org>,
Rob Herring <robh+dt@kernel.org>,
Frank Rowand <frowand.list@gmail.com>,
Nathan Lynch <nathanl@linux.ibm.com>,
Laurent Dufour <ldufour@linux.ibm.com>,
Daniel Henrique Barboza <danielhb413@gmail.com>,
David Gibson <david@gibson.dropbear.id.au>,
Andrew Morton <akpm@linux-foundation.org>,
David Hildenbrand <david@redhat.com>,
Ohhoon Kwon <ohoono.kwon@samsung.com>,
"Aneesh Kumar K.V" <aneesh.kumar@linux.ibm.com>,
YueHaibing <yuehaibing@huawei.com>
Cc: "Clément Léger" <clement.leger@bootlin.com>,
linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org,
devicetree@vger.kernel.org,
"Allan Nielsen" <allan.nielsen@microchip.com>,
"Horatiu Vultur" <horatiu.vultur@microchip.com>,
"Steen Hegelund" <steen.hegelund@microchip.com>,
"Thomas Petazzoni" <thomas.petazzoni@bootlin.com>,
"Bjorn Helgaas" <helgaas@kernel.org>,
"Lizhi Hou" <lizhi.hou@xilinx.com>
Subject: [PATCH v2 3/4] of: dynamic: add of_node_alloc()
Date: Wed, 1 Jun 2022 10:18:00 +0200 [thread overview]
Message-ID: <20220601081801.348571-4-clement.leger@bootlin.com> (raw)
In-Reply-To: <20220601081801.348571-1-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 | 52 ++++++++++++++++++++++++++++++++++----------
include/linux/of.h | 6 +++++
2 files changed, 47 insertions(+), 11 deletions(-)
diff --git a/drivers/of/dynamic.c b/drivers/of/dynamic.c
index c0dcbea31d28..67636aafe810 100644
--- a/drivers/of/dynamic.c
+++ b/drivers/of/dynamic.c
@@ -359,7 +359,9 @@ void of_node_release(struct kobject *kobj)
property_list_free(node->deadprops);
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);
}
@@ -426,6 +428,43 @@ struct property *of_property_alloc(const char *name, const void *value,
}
EXPORT_SYMBOL(of_property_alloc);
+/**
+ * of_node_alloc - Allocate a node dynamically.
+ * @name: Node name
+ * @allocflags: Allocation flags (typically pass GFP_KERNEL)
+ *
+ * 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, gfp_t allocflags)
+{
+ struct device_node *node;
+ int name_len = 0;
+
+ if (name)
+ name_len = strlen(name) + 1;
+
+ node = kzalloc(sizeof(*node) + name_len, allocflags);
+ 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_node_dup() - Duplicate or create an empty device node dynamically.
* @np: if not NULL, contains properties to be duplicated in new node
@@ -442,18 +481,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, GFP_KERNEL);
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 f1966f3c3847..c18da685ade1 100644
--- a/include/linux/of.h
+++ b/include/linux/of.h
@@ -1464,6 +1464,7 @@ enum of_reconfig_change {
};
#ifdef CONFIG_OF_DYNAMIC
+struct device_node *of_node_alloc(const char *name, gfp_t allocflags);
struct property *of_property_alloc(const char *name, const void *value,
size_t len, gfp_t allocflags);
void of_property_free(const struct property *prop);
@@ -1512,6 +1513,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,
+ gfp_t allocflags)
+{
+ return NULL;
+}
static inline
struct property *of_property_alloc(const char *name, const void *value,
--
2.36.0
next prev parent reply other threads:[~2022-06-01 8:21 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-06-01 8:17 [PATCH v2 0/4] of: add of_property_alloc/free() and of_node_alloc() Clément Léger
2022-06-01 8:17 ` Clément Léger
2022-06-01 8:17 ` [PATCH v2 1/4] of: constify of_property_check_flags() prop argument Clément Léger
2022-06-01 8:17 ` Clément Léger
2022-06-03 20:24 ` Rob Herring
2022-06-03 20:24 ` Rob Herring
2022-06-01 8:17 ` [PATCH v2 2/4] of: dynamic: add of_property_alloc() and of_property_free() Clément Léger
2022-06-01 8:17 ` Clément Léger
2022-06-01 22:32 ` Tyrel Datwyler
2022-06-01 22:32 ` Tyrel Datwyler
2022-06-02 6:58 ` Clément Léger
2022-06-02 6:58 ` Clément Léger
2022-06-02 18:10 ` Tyrel Datwyler
2022-06-02 18:10 ` Tyrel Datwyler
2022-06-01 8:18 ` Clément Léger [this message]
2022-06-01 8:18 ` [PATCH v2 3/4] of: dynamic: add of_node_alloc() Clément Léger
2022-06-01 8:18 ` [PATCH v2 4/4] powerpc/pseries: use of_property_alloc/free() and of_node_alloc() Clément Léger
2022-06-01 8:18 ` Clément Léger
2022-06-03 20:14 ` Rob Herring
2022-06-03 20:14 ` Rob Herring
2022-06-06 8:45 ` Clément Léger
2022-06-06 8:45 ` Clément Léger
-- strict thread matches above, loose matches on Subject: below --
2022-06-02 19:29 [PATCH v2 3/4] of: dynamic: add of_node_alloc() kernel test robot
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=20220601081801.348571-4-clement.leger@bootlin.com \
--to=clement.leger@bootlin.com \
--cc=akpm@linux-foundation.org \
--cc=allan.nielsen@microchip.com \
--cc=aneesh.kumar@linux.ibm.com \
--cc=benh@kernel.crashing.org \
--cc=danielhb413@gmail.com \
--cc=david@gibson.dropbear.id.au \
--cc=david@redhat.com \
--cc=devicetree@vger.kernel.org \
--cc=frowand.list@gmail.com \
--cc=helgaas@kernel.org \
--cc=horatiu.vultur@microchip.com \
--cc=ldufour@linux.ibm.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linuxppc-dev@lists.ozlabs.org \
--cc=lizhi.hou@xilinx.com \
--cc=mpe@ellerman.id.au \
--cc=nathanl@linux.ibm.com \
--cc=ohoono.kwon@samsung.com \
--cc=paulus@samba.org \
--cc=robh+dt@kernel.org \
--cc=steen.hegelund@microchip.com \
--cc=thomas.petazzoni@bootlin.com \
--cc=yuehaibing@huawei.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.