Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp6386645ybe; Wed, 18 Sep 2019 02:36:42 -0700 (PDT) X-Google-Smtp-Source: APXvYqxDX+67L9iFlxytt3Vk75jmHjSiTez/MamxlBXkrr4b9Ul81nrZb+XKBmTNddlDs0gL4v0y X-Received: by 2002:a17:906:1348:: with SMTP id x8mr8563586ejb.179.1568799402388; Wed, 18 Sep 2019 02:36:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568799402; cv=none; d=google.com; s=arc-20160816; b=pOus7ovVcmyds+xvaSmYGfNBWVmG/BlWQHSyNhD9uQak9uKEW2GschxvOSVV/QkCcM 0Wp5J74geVXoY8emC92CN3PPtXgceTFE/icK8tu1eK/FzpnCQmVV51i/V6AYvzb2bmNE CL9jkMYddK7v5IYGjLi6/TuQtFr7wSqBC6uxId3ubnrWIBOO64SMWTn9uGkyfM2KMRQF I+SdbyFoQ+l95fpzjA9QNE1jC26ot0+hUW+S/caEMNJ2HLM3LjVKn8lsDDxwgovUFnYa pycIqVFRVPI/AX3XGdYestk9eOvsXVWGMUSWKb2BKG5a/WyV5Swbhjgo5r92cxlxi1+L +3gA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-transfer-encoding:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=Fmvva/0Le9pZr0SMqMD0GjpLXkSVd4/rl5p2nNmLL8U=; b=BUo9UmSXbZiDmmTGa2LvlOD5Rx4VV+vSBxVGKdRFs3gwnkoGHOr3IcvrWdL8N8of0s p1UHgQWd0RSZfiG182YWAi2Y3HYamKB1qU/OxUXtaDAUnfNQYRIw36ImQb+fwKwPTWil 2t1yR8X1r3ktNNsbMRYK7lbkgGp2RxLinPCerxUWrcs68Q0T9Pi6IQH95ArmLS3TDSGN FbfqNCdIB+C7sncZiJkk4cqeUcXVfO8T/CQMinRQ9PO8+iW4NICmVJ9QzHuV1TzEq8Ky tRQcIfLK1NvDtFOYS8JLXGHz+QK3pqgvGnZv8boC8koHMIkfFOoABq7R0nNISsADUbKm 6IMQ== ARC-Authentication-Results: i=1; mx.google.com; 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 y3si3168950ede.38.2019.09.18.02.36.19; Wed, 18 Sep 2019 02:36:42 -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; 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 S1729981AbfIRIrx (ORCPT + 99 others); Wed, 18 Sep 2019 04:47:53 -0400 Received: from metis.ext.pengutronix.de ([85.220.165.71]:40225 "EHLO metis.ext.pengutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729745AbfIRIrx (ORCPT ); Wed, 18 Sep 2019 04:47:53 -0400 Received: from pty.hi.pengutronix.de ([2001:67c:670:100:1d::c5]) by metis.ext.pengutronix.de with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1iAVcr-0001B8-T8; Wed, 18 Sep 2019 10:47:49 +0200 Received: from ukl by pty.hi.pengutronix.de with local (Exim 4.89) (envelope-from ) id 1iAVcq-0007mf-Bu; Wed, 18 Sep 2019 10:47:48 +0200 Date: Wed, 18 Sep 2019 10:47:48 +0200 From: Uwe =?iso-8859-1?Q?Kleine-K=F6nig?= To: Peter Rosin , Rob Herring , Frank Rowand Cc: "devicetree@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "linux-mediatek@lists.infradead.org" , "linux-arm-kernel@lists.infradead.org" , "kernel@pengutronix.de" , Will Deacon , Robin Murphy , Joerg Roedel , Matthias Brugger , Geert Uytterhoeven , Marek Szyprowski , Geert Uytterhoeven Subject: [PATCH v2] of: restore old handling of cells_name=NULL in of_*_phandle_with_args() Message-ID: <20190918084748.hnjkiq7wc5b35wjh@pengutronix.de> References: <20190918063837.8196-1-u.kleine-koenig@pengutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: NeoMutt/20170113 (1.7.2) X-SA-Exim-Connect-IP: 2001:67c:670:100:1d::c5 X-SA-Exim-Mail-From: ukl@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Before commit e42ee61017f5 ("of: Let of_for_each_phandle fallback to non-negative cell_count") the iterator functions calling of_for_each_phandle assumed a cell count of 0 if cells_name was NULL. This corner case was missed when implementing the fallback logic in e42ee61017f5 and resulted in an endless loop. Restore the old behaviour of of_count_phandle_with_args() and of_parse_phandle_with_args() and add a check to of_phandle_iterator_init() to prevent a similar failure as a safety precaution. of_parse_phandle_with_args_map() doesn't need a similar fix as cells_name isn't NULL there. Affected drivers are: - drivers/base/power/domain.c - drivers/base/power/domain.c - drivers/clk/ti/clk-dra7-atl.c - drivers/hwmon/ibmpowernv.c - drivers/i2c/muxes/i2c-demux-pinctrl.c - drivers/iommu/mtk_iommu.c - drivers/net/ethernet/freescale/fman/mac.c - drivers/opp/of.c - drivers/perf/arm_dsu_pmu.c - drivers/regulator/of_regulator.c - drivers/remoteproc/imx_rproc.c - drivers/soc/rockchip/pm_domains.c - sound/soc/fsl/imx-audmix.c - sound/soc/fsl/imx-audmix.c - sound/soc/meson/axg-card.c - sound/soc/samsung/tm2_wm5110.c - sound/soc/samsung/tm2_wm5110.c Thanks to Geert Uytterhoeven for reporting the issue, Peter Rosin for helping pinpoint the actual problem and the testers for confirming this fix. Fixes: e42ee61017f5 ("of: Let of_for_each_phandle fallback to non-negative cell_count") Tested-by: Marek Szyprowski Tested-by: Geert Uytterhoeven Signed-off-by: Uwe Kleine-K?nig --- On Wed, Sep 18, 2019 at 08:01:05AM +0000, Peter Rosin wrote: > On 2019-09-18 08:38, Uwe Kleine-K?nig wrote: > > EXPORT_SYMBOL(of_parse_phandle_with_args); > > > > @@ -1765,6 +1779,18 @@ int of_count_phandle_with_args(const struct device_node *np, const char *list_na > > struct of_phandle_iterator it; > > int rc, cur_index = 0; > > > > + /* If cells_name is NULL we assume a cell count of 0 */ > > + if (cells_name == NULL) { > > A couple of nits. > > I don't know if there are other considerations, but in the previous two > hunks you use !cells_name instead of comparing explicitly with NULL. > Personally, I find the shorter form more readable, and in the name of > consistency bla bla... Ack, changed to !cells_name here, too. > > Also, the comment explaining this NULL-check didn't really make sense > to me until I realized that knowing the cell count to be zero makes > counting trivial. Something along those lines should perhaps be in the > comment? You're right, I extended the comment a bit. > But as I said, these are nits. Feel free to ignore. I considered resending already anyhow as I fatfingerd my email address. this is fixed now, too. Additionally I fixed a typo in one of the comments. Thanks for your feedback. Best regards Uwe drivers/of/base.c | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/drivers/of/base.c b/drivers/of/base.c index 2f25d2dfecfa..1d667eb730e1 100644 --- a/drivers/of/base.c +++ b/drivers/of/base.c @@ -1286,6 +1286,13 @@ int of_phandle_iterator_init(struct of_phandle_iterator *it, memset(it, 0, sizeof(*it)); + /* + * one of cell_count or cells_name must be provided to determine the + * argument length. + */ + if (cell_count < 0 && !cells_name) + return -EINVAL; + list = of_get_property(np, list_name, &size); if (!list) return -ENOENT; @@ -1512,10 +1519,17 @@ int of_parse_phandle_with_args(const struct device_node *np, const char *list_na const char *cells_name, int index, struct of_phandle_args *out_args) { + int cell_count = -1; + if (index < 0) return -EINVAL; - return __of_parse_phandle_with_args(np, list_name, cells_name, -1, - index, out_args); + + /* If cells_name is NULL we assume a cell count of 0 */ + if (!cells_name) + cell_count = 0; + + return __of_parse_phandle_with_args(np, list_name, cells_name, + cell_count, index, out_args); } EXPORT_SYMBOL(of_parse_phandle_with_args); @@ -1765,6 +1779,23 @@ int of_count_phandle_with_args(const struct device_node *np, const char *list_na struct of_phandle_iterator it; int rc, cur_index = 0; + /* + * If cells_name is NULL we assume a cell count of 0. This makes + * counting the phandles trivial as each 32bit word in the list is a + * phandle and no arguments are to consider. So we don't iterate through + * the list but just use the length to determine the phandle count. + */ + if (!cells_name) { + const __be32 *list; + int size; + + list = of_get_property(np, list_name, &size); + if (!list) + return -ENOENT; + + return size / sizeof(*list); + } + rc = of_phandle_iterator_init(&it, np, list_name, cells_name, -1); if (rc) return rc; -- 2.23.0 -- Pengutronix e.K. | Uwe Kleine-K?nig | Industrial Linux Solutions | http://www.pengutronix.de/ |