Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp2529024imu; Thu, 29 Nov 2018 06:27:34 -0800 (PST) X-Google-Smtp-Source: AFSGD/XJ8EGSRLTl+yqrzgEJONGK1I8Etu5jUl1axgqck3cEiPYoD2PSFeq/Yz75dX+5Wh7axklm X-Received: by 2002:a65:4049:: with SMTP id h9mr1411913pgp.304.1543501654538; Thu, 29 Nov 2018 06:27:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543501654; cv=none; d=google.com; s=arc-20160816; b=0oqoEdimRyNo32L7W3QtaBWSbCGPW5JSJCwkfnxnLhX+sgN+dWazkd5M7YmHoDeWn7 1Ye8HMUBrmIbqbFV2S3uILvSkNqNjjNt3NX5g0V2UBQJF3as/VCe/4dGLTw/xMjpy95K Gs5xX/Ep4/UlmWwtgVs8k1TRU6l6lvCf7Ss8vicoITNRWxfnspZdPh+GqQU5NQZn6JRQ oAfN/T9Pn0Gxm1jAbYhm+cd0LYuuxQjQLwSQYCeECoXVQsVUPeFu/RxHkI49ijolZOVm /9kkCm3/ge7KBS8T7MaZLYG+8ORasfkg7eOcgztG8Y03CbQh05R8gXLrzY6AXFbOGfRl qvAA== 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=fIxBCmBX6/ZiazL8WVx1Sd6vUeHt7RZK0O2ws8NiyRY=; b=drh18wfL91+b4BsbN55Wg1S395ddvm63Vc+999QF4QXSqpnLdT9jxIjyE/7RXRgvpb YYb5yBQ2nZg0mto7vNSBJIoavz0uMZ5XV/VvITO3eaVbs7oscDQeo7w9G4v8JlRz9Ith SjYwMva/clofWDaV6Z1VJaa3k9KfgxYJdEPH0KTe4dA2RfsJTQQusZyC1mXzP2bIDZws +GDVMF1C+zFVOU5Ql4WMZZDwPUMLw2Du0lg32el1TgGjqptkXzDvwU2yvvrWhoOR1ns+ w4xTxiQiDc9m2DyYNRxRtVTXIcIh2dmUjitNJqb3A1BXao2NQTAYkvy+IB0D903LIoKN 70yA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=loaHkGND; 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 cf16si2376950plb.227.2018.11.29.06.27.19; Thu, 29 Nov 2018 06:27:34 -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=loaHkGND; 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 S2387452AbeK3BcO (ORCPT + 99 others); Thu, 29 Nov 2018 20:32:14 -0500 Received: from mail.kernel.org ([198.145.29.99]:60246 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732836AbeK3BcN (ORCPT ); Thu, 29 Nov 2018 20:32:13 -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 F10F72146D; Thu, 29 Nov 2018 14:26:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1543501601; bh=WtpKq3kZ9e0Ee1UOw8mtNqj7yhKMMY/E4HJltHafkuc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=loaHkGND+k771e+EgpBMu57XBzZcyFQqtuub5yp1Z/Mp1fFsTdQnnX+5RTOdIy2JA KvHvYWzvw/UlSe0ENt2HdP/zYhFZHSmTSlwGPtK6y1rzP1wcJEv2B/Qy4T4ULW6ovl USZYDQhd51ostfAaYhBrBFyw3ZPSqobLjxXXVgb4= 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.14 065/100] of: add helper to lookup compatible child node Date: Thu, 29 Nov 2018 15:12:35 +0100 Message-Id: <20181129140104.563698702@linuxfoundation.org> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181129140058.768942700@linuxfoundation.org> References: <20181129140058.768942700@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.14-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 63897531cd75..ce8a6e0c9b6a 100644 --- a/drivers/of/base.c +++ b/drivers/of/base.c @@ -737,6 +737,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 b240ed69dc96..70b7dacf9238 100644 --- a/include/linux/of.h +++ b/include/linux/of.h @@ -288,6 +288,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); @@ -625,6 +627,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