Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp2520430imu; Thu, 29 Nov 2018 06:20:32 -0800 (PST) X-Google-Smtp-Source: AFSGD/X39M6wnWoWZAYxpl8PCb80IDsIwn4sQYGjJ02uYGJy6hBLsevRbYwnYXALL90+erAlrPgh X-Received: by 2002:a62:6a88:: with SMTP id f130mr1572111pfc.201.1543501232382; Thu, 29 Nov 2018 06:20:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543501232; cv=none; d=google.com; s=arc-20160816; b=PtZ3g00PvAcgQ2F9wsYNztw7ZOSuoZs1u303LEqNDHyauoxXKY5AU14UDs4490hPJA hDHIYQiKgE5jvhJxIq4BZDc0SPgXfCiF2vC4oEJCwwfmhs8ZaGMKhNjBMVbbzZuFTseV JUS8YDeHEshCmCZ7zOlIlqv7N4gQpjZd6pu3oqe5/HVwznQCQHHm/MG2MfLf9+9v6jZ8 ezB4aEp6mHgpC69xV1zLr2k4YMz9zgvIeDum7fjaD44BnsPnj/Lw/8eb/upcL1YRV5hJ gXKyEZkXnSXQXO/r0Y2H06AR3HVttBs2EsV7u8xp+C3Pa7OQ9k7DfJeXZLTwZu31YkyT XtqA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=6h6+MOc3u4aMRNjttS+qkW07uHsNb6IQ3xG4dS4e72U=; b=mu2Vf22Pjr1hzHZrYasxrKKWB1YMsZTyQ9KVmjVurVCXpLYpDDg5UXUxhMirImpZN0 D/Qbpb3X55iyWan0lVvGzFQJYQMwpicK3f8Ewx6HpA/YEPSrzLbArQVHqXLmZ6KE5izD kPWaOLlRvj4/j8lzPRui/9+krhZsUrGcQCXdeVxysclUmMRjkpadv7PHtfBybTphD+pt ZYrE1Xevls4AIW2c1GimpNk71QPxYQNx13fBjY0mRLkyjHB9xThKIKtjCmke+qIoz3C2 6WwnBIGpJM1xzEGrgzrQcasnx4rmSPVK7+xUhJLDOMkrWJQc4RwgbIQzHM6ZSCmpKUs+ JKOw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=oGG2yXff; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p5si2223802pls.338.2018.11.29.06.20.16; Thu, 29 Nov 2018 06:20:32 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=oGG2yXff; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730465AbeK3BXK (ORCPT + 99 others); Thu, 29 Nov 2018 20:23:10 -0500 Received: from mail.kernel.org ([198.145.29.99]:45148 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728439AbeK3BXJ (ORCPT ); Thu, 29 Nov 2018 20:23:09 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id E01F4208E7; Thu, 29 Nov 2018 14:17:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1543501058; bh=5pNSrrpD0fBeKfM5KBcwWR7JFH++1tiFo+49xtQb7+k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oGG2yXffFUqG+39GOIjy5/ZeUWqpHbFjJH6IQUUIGMXZATnqjog5xo3rGQ2DeyZ+w MPxjvuvWTqxdWO0JlWvbZQQWPKMegc9eOog+2+5AkomndC9wfNtan3EYExMiDjPqX+ taRYUOHTBtGLM9/zlGiyRMEJ5lGu9y+u7lc8ujAw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Johan Hovold , Rob Herring , Sasha Levin Subject: [PATCH 4.4 25/86] of: add helper to lookup compatible child node Date: Thu, 29 Nov 2018 15:11:50 +0100 Message-Id: <20181129140111.907831844@linuxfoundation.org> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181129140109.832117862@linuxfoundation.org> References: <20181129140109.832117862@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.4-stable review patch. If anyone has any objections, please let me know. ------------------ [ Upstream commit 36156f9241cb0f9e37d998052873ca7501ad4b36 ] 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 Signed-off-by: Rob Herring Signed-off-by: Sasha Levin --- 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 31341290cd91..c6e019208d17 100644 --- a/drivers/of/base.c +++ b/drivers/of/base.c @@ -712,6 +712,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 dd10626a615f..d9371c9cd88a 100644 --- a/include/linux/of.h +++ b/include/linux/of.h @@ -265,6 +265,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); @@ -462,6 +464,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.17.1