Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp5642825imm; Mon, 27 Aug 2018 01:24:39 -0700 (PDT) X-Google-Smtp-Source: ANB0VdbqAXIzSBWFsY89MZn1/qlyhrwlj92jg9Y16pIHvGIZ7HBlKks+l7sxG7h/UhH7rvwqzqRF X-Received: by 2002:a63:8c0b:: with SMTP id m11-v6mr11224055pgd.372.1535358279517; Mon, 27 Aug 2018 01:24:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535358279; cv=none; d=google.com; s=arc-20160816; b=W+qPm631lUIMcIUlL2UynxfambcCxfcXBqwwG7WROJQlTzDXLP4eNpmhBJ8w8ig1RY cf7spOY4Z1W7Lr6o8PqD1nUGcoHB9XHwV12B2KvwjgQeHlWyk3C8cA6DhVoVKn2WsGZ3 bIDShkm3NRYQIouIjGlzHZfpe9UpJ7/zwVZpsN4BZn16AnrbUeUxRluZUBxYlTcCZa5E sVb+7WPG7hSCfm0NpoJ2Ny0dv6TDnQozP+Fbg5Sechb24nCB4ceij0uQZj5yd2B9r1jJ sb2LHkfT1bHE2+UiXh9N+AWJkFmJjs/wEeO6nSqsTchCLmZdflJyjyQKZlGIqd+3PU4o G3/A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=h2/k3apiCocm0wblFIPa3bgtEpVthP9qn7ODBdhWFdY=; b=QhrwnLvvl2OqVh/SjdEaI+26vLj9xqd9c3iWMcZqf4ET0dkPmxG93p+18yjrlvf+Vi qtdspxYJnpEwh3/kAQ/TJ2D4beEl9B9DRFGCNR1JyPTEW+A7NdOfPK7UVQNB2bQyX8R6 hSjsydbhvRidZW2pa7cHHkZEYjptMBXCL2psPibXwNpfa15q/27t890re2vjKemZWIN7 TMD8Qc4dHuIungA3+xFlYZqIUCFjKXRHdcVCWzDYehbyvH6qq0L7tCF2xbhbQAEs0ig1 og2VmN21I8AhqOXyyOTbFuUjYUHaJ8GLHvlm8O8m+3qumT6Ucb/OmTMmFOaA+NJCszrf DEFg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=KprIHeMQ; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p6-v6si15145991pfh.266.2018.08.27.01.24.24; Mon, 27 Aug 2018 01:24:39 -0700 (PDT) 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=fail header.i=@gmail.com header.s=20161025 header.b=KprIHeMQ; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727192AbeH0MIO (ORCPT + 99 others); Mon, 27 Aug 2018 08:08:14 -0400 Received: from mail-lj1-f195.google.com ([209.85.208.195]:43955 "EHLO mail-lj1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727118AbeH0MIN (ORCPT ); Mon, 27 Aug 2018 08:08:13 -0400 Received: by mail-lj1-f195.google.com with SMTP id m84-v6so11587505lje.10; Mon, 27 Aug 2018 01:22:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=h2/k3apiCocm0wblFIPa3bgtEpVthP9qn7ODBdhWFdY=; b=KprIHeMQlLn91wZwsnMQIKblLR9KBX36ZiDo1ChO29XTQmoljzLys4LKoHYEqmmbmz cTWkXaWlSKdC64NlBzYd5JpGWvCUpGsZfs9/igvEccsfjC2PuYZYLvpScD/SLBTmykqW RxRyIa7Fu8DYWslKxniWyatJ03kfBUv19kqRNwUR4boBwzAVlBP6LAAncJUJ0Dj7HNkX jThMfHz7A8LWi4lhhp2PiRvJsKP7FjOH2ikMXFovKHsc3LbO7JT7ZySPo4A3O7vbI5NW dH0PNOIz40eRi2P7BaDERRGRKUGOPL/yj56I8nJS6WS3cKnFGkJ+VcWgLnU6TYT75+uf vl4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=h2/k3apiCocm0wblFIPa3bgtEpVthP9qn7ODBdhWFdY=; b=AkBvBzQjOQDlZkUW0BRQSBaV+U9PedA9R0aJFrj5sAZaHyTAz8mdyC9pXf7Hr/F/JA Q4TLaoazvYeWoQbCAjqG9BPThsO3QHCEXQJzTdUWT0LPI/hnndb2MACMeX/NW7LZDsqB alSFmh9hti5e9CWCqNoa3rq6CRmAhtbSGTObRsybMn/EMzhGAM5ZCcs+kbk2kZJVs2CC 99z5PEFaS7Xb+uC5K5DJC+GOj/p/x/Slm42elzu7/B1RVQeoHcU+aLLDPrBK9ndbR0dg kpNgo2NHevTfk8RjZvSk896Zx33rTnw57LyxFQ/TTWdWaf2ygwA+7SgXmlsz6WAnEEX7 Kgyw== X-Gm-Message-State: APzg51BKJY/TVSRiXTAuEiJKaEqEhaU5m84x365xnpATH/vUgTev4pm2 gBY4CDZ5W+CjksSzlCbIfH4UpSql X-Received: by 2002:a2e:8652:: with SMTP id i18-v6mr8287060ljj.43.1535358154923; Mon, 27 Aug 2018 01:22:34 -0700 (PDT) Received: from xi.terra ([85.230.190.116]) by smtp.gmail.com with ESMTPSA id x3-v6sm2640352ljb.25.2018.08.27.01.22.31 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 27 Aug 2018 01:22:31 -0700 (PDT) Received: from johan by xi.terra with local (Exim 4.91) (envelope-from ) id 1fuCn6-0005sS-KO; Mon, 27 Aug 2018 10:22:28 +0200 From: Johan Hovold To: Rob Herring , Greg Kroah-Hartman Cc: Frank Rowand , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Johan Hovold Subject: [PATCH v2 1/9] of: add helper to lookup compatible child node Date: Mon, 27 Aug 2018 10:21:45 +0200 Message-Id: <20180827082153.22537-2-johan@kernel.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180827082153.22537-1-johan@kernel.org> References: <20180827082153.22537-1-johan@kernel.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.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 --- 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