Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp2560932imu; Thu, 29 Nov 2018 06:56:07 -0800 (PST) X-Google-Smtp-Source: AFSGD/VVacceuYc+QOLj4BSHaoJCsuz+aYzeZJgZzQkbOrEXk6n68ouy9gFOv3oXaHB8p0bnv8zZ X-Received: by 2002:a62:7086:: with SMTP id l128mr1693075pfc.68.1543503367838; Thu, 29 Nov 2018 06:56:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543503367; cv=none; d=google.com; s=arc-20160816; b=oKlXMzK4JeIRSDwBkpMRNV34pYZZYlHmvr4uUdM1Izl8nkKEefgC2GsVls3RxJnNqB Sm3xJHUPMNju3wPO23CmKsf6QvAeUmdNpWv6AHI0bC0Ija2klJNWGoSRvDrAyb7MCeYy l4rw3ZRjw8zgjH9oKCPCFyS4CiyN4SAcUJn6NDejBeAxsozr+/AYpI9DhFi92zXP692n GUtlviPy+tEZIF/XCauEEhjX5Fc1XDuZ8vi0sWIt4vqA+Rj74EJJiAhlACshjva6Zdsh lOc8wRCqsNXf3c9rUIG9vpiCwC28P5aEEZwBSxCJ9jCjF9ZfsCLui770BBX0G7ZkIAya rRLQ== 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=74ehLHzfHSGT94yEVK/lPlQ1bb+wvGAh5ywM1jRdSrY=; b=IOEACYgi5foRcfat39vwotEB5tvFYN+5EmBGvrPzKum5m/ETE8V6EV0lYtIrw1UwzA Zbb/XrE7QiU2Ccd9P3ABw2cPTAr9cRl7nu8i7y+uSVdpmXGFo2z4iW2OoVekaRyN+upr dLlSC/xikTdjm/cN4pyHLJ9EPgDj53uqr4jHW3oCP2/pH5+70jShJGqxL66gSpl8rILX NadQ/Qv7QIeKO1bsxFbogiYS0Pw3pTZIryck4yF1ODPOfSHYVQoEEFRvKB/fjhUKXu+c XuwV5rw85R9Y/BZilUpOOAl0X+nLzpTmpHvgHhlETeXzBuvf2oOqbNy2gLhAeApysnS+ ZfJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=ll5eGenQ; 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 h33si2419751plh.228.2018.11.29.06.55.52; Thu, 29 Nov 2018 06:56:07 -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=ll5eGenQ; 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 S1731836AbeK3B1b (ORCPT + 99 others); Thu, 29 Nov 2018 20:27:31 -0500 Received: from mail.kernel.org ([198.145.29.99]:52484 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731142AbeK3B1b (ORCPT ); Thu, 29 Nov 2018 20:27:31 -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 6D4A120868; Thu, 29 Nov 2018 14:21:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1543501318; bh=GKWQHRYnbfiJlN0I0BEGjXBLG1gQTK3lIsM2IBS+10I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ll5eGenQVBCTxgCMmNr3yW0yNsYZZDwEJMUOOSfOQX+fWiOdGHwvmBtVVMzMsGcUx jIi8B2n/JPhPR1RR6DSYOGRA5IilNlpDb1AMsr4YbZ8m6Z9Di7rAcsglvUqCWdeV/P D8OWoLGzQF3DKKg8rOhBy3ckWZEf6MO+/F/uCNUU= 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.9 41/92] of: add helper to lookup compatible child node Date: Thu, 29 Nov 2018 15:12:10 +0100 Message-Id: <20181129140109.564497936@linuxfoundation.org> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181129140106.520639693@linuxfoundation.org> References: <20181129140106.520639693@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.9-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 466b285cef3e..f366af135d5b 100644 --- a/drivers/of/base.c +++ b/drivers/of/base.c @@ -738,6 +738,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 299aeb192727..a19cc85b9373 100644 --- a/include/linux/of.h +++ b/include/linux/of.h @@ -275,6 +275,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); @@ -606,6 +608,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