linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Johan Hovold <johan@kernel.org>
To: Rob Herring <robh+dt@kernel.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Frank Rowand <frowand.list@gmail.com>,
	devicetree@vger.kernel.org, linux-kernel@vger.kernel.org,
	Johan Hovold <johan@kernel.org>
Subject: [PATCH v2 1/9] of: add helper to lookup compatible child node
Date: Mon, 27 Aug 2018 10:21:45 +0200	[thread overview]
Message-ID: <20180827082153.22537-2-johan@kernel.org> (raw)
In-Reply-To: <20180827082153.22537-1-johan@kernel.org>

Add of_get_compatible_child() helper that can be used to lookup
compatible child nodes.

Several drivers currently use of_find_compatible_node() to lookup child
nodes while failing to notice that the of_find_ functions search the
entire tree depth-first (from a given start node) and therefore can
match unrelated nodes. The fact that these functions also drop a
reference to the node they start searching from (e.g. the parent node)
is typically also overlooked, something which can lead to use-after-free
bugs.

Signed-off-by: Johan Hovold <johan@kernel.org>
---
 drivers/of/base.c  | 25 +++++++++++++++++++++++++
 include/linux/of.h |  8 ++++++++
 2 files changed, 33 insertions(+)

diff --git a/drivers/of/base.c b/drivers/of/base.c
index 466e3c8582f0..bc420d2aa5f5 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -719,6 +719,31 @@ struct device_node *of_get_next_available_child(const struct device_node *node,
 }
 EXPORT_SYMBOL(of_get_next_available_child);
 
+/**
+ * of_get_compatible_child - Find compatible child node
+ * @parent:	parent node
+ * @compatible:	compatible string
+ *
+ * Lookup child node whose compatible property contains the given compatible
+ * string.
+ *
+ * Returns a node pointer with refcount incremented, use of_node_put() on it
+ * when done; or NULL if not found.
+ */
+struct device_node *of_get_compatible_child(const struct device_node *parent,
+				const char *compatible)
+{
+	struct device_node *child;
+
+	for_each_child_of_node(parent, child) {
+		if (of_device_is_compatible(child, compatible))
+			break;
+	}
+
+	return child;
+}
+EXPORT_SYMBOL(of_get_compatible_child);
+
 /**
  *	of_get_child_by_name - Find the child node by name for a given parent
  *	@node:	parent node
diff --git a/include/linux/of.h b/include/linux/of.h
index 4d25e4f952d9..b99a1a8c2952 100644
--- a/include/linux/of.h
+++ b/include/linux/of.h
@@ -290,6 +290,8 @@ extern struct device_node *of_get_next_child(const struct device_node *node,
 extern struct device_node *of_get_next_available_child(
 	const struct device_node *node, struct device_node *prev);
 
+extern struct device_node *of_get_compatible_child(const struct device_node *parent,
+					const char *compatible);
 extern struct device_node *of_get_child_by_name(const struct device_node *node,
 					const char *name);
 
@@ -632,6 +634,12 @@ static inline bool of_have_populated_dt(void)
 	return false;
 }
 
+static inline struct device_node *of_get_compatible_child(const struct device_node *parent,
+					const char *compatible)
+{
+	return NULL;
+}
+
 static inline struct device_node *of_get_child_by_name(
 					const struct device_node *node,
 					const char *name)
-- 
2.18.0


  reply	other threads:[~2018-08-27  8:22 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-08-27  8:21 [PATCH v2 0/9] of: fix compatible-child-node lookups Johan Hovold
2018-08-27  8:21 ` Johan Hovold [this message]
2018-08-30 15:51   ` [PATCH v2 1/9] of: add helper to lookup compatible child node Rob Herring
2018-08-27  8:21 ` [PATCH v2 2/9] drm/mediatek: fix OF sibling-node lookup Johan Hovold
2018-08-27  8:21 ` [PATCH v2 3/9] drm/msm: fix OF child-node lookup Johan Hovold
2018-08-27  8:21 ` [PATCH v2 4/9] mmc: meson-mx-sdio: " Johan Hovold
2018-08-27 14:44   ` Ulf Hansson
2018-09-04 12:54     ` Johan Hovold
2018-09-05  6:30       ` Ulf Hansson
2018-08-27  8:21 ` [PATCH v2 5/9] mtd: nand: atmel: " Johan Hovold
2018-08-27  8:28   ` Boris Brezillon
2018-08-27  8:44     ` Johan Hovold
2018-08-27  8:48       ` Boris Brezillon
2018-08-27  9:44         ` Johan Hovold
2018-10-23 18:28           ` Rob Herring
2018-10-23 18:51             ` Boris Brezillon
2018-11-15 14:26               ` Johan Hovold
2018-11-18 10:45                 ` Boris Brezillon
2018-08-27  8:21 ` [PATCH v2 6/9] net: bcmgenet: " Johan Hovold
2018-08-31  0:47   ` Florian Fainelli
2018-09-04 12:56     ` Johan Hovold
2018-08-27  8:21 ` [PATCH v2 7/9] net: stmmac: dwmac-sun8i: " Johan Hovold
2018-08-28  8:06   ` Corentin Labbe
2018-08-29  7:54     ` Johan Hovold
2018-09-06 20:03   ` Corentin Labbe
2018-09-07  7:48     ` Johan Hovold
2018-08-27  8:21 ` [PATCH v2 8/9] NFC: nfcmrvl_uart: " Johan Hovold
2018-08-27  8:21 ` [PATCH v2 9/9] power: supply: twl4030-charger: fix OF sibling-node lookup Johan Hovold
2018-09-04 13:05 ` [PATCH v2 0/9] of: fix compatible-child-node lookups Johan Hovold
2018-10-23  9:19   ` Johan Hovold
2018-10-23 18:32     ` Rob Herring
2018-10-24  7:32       ` Johan Hovold

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=20180827082153.22537-2-johan@kernel.org \
    --to=johan@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=frowand.list@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=robh+dt@kernel.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;
as well as URLs for NNTP newsgroup(s).