From patchwork Tue Jan 23 10:23:10 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ulf Hansson X-Patchwork-Id: 125493 Delivered-To: patches@linaro.org Received: by 10.46.66.141 with SMTP id h13csp1666740ljf; Tue, 23 Jan 2018 02:23:18 -0800 (PST) X-Received: by 10.25.216.233 with SMTP id r102mr1022923lfi.139.1516702998459; Tue, 23 Jan 2018 02:23:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516702998; cv=none; d=google.com; s=arc-20160816; b=z6UqyDs6CJ0nnO0fT1+ihTK+KnI8RLMTJ9N2dg0MdegJXOzJn+JvMwb3kZCzYPoDZA 86ixi1ScVKveDYcgPaWZNmDSPq7HNUe3nrxEn3S5CbMIl1QUM0FGKPdGqeRZrbg9wFKE pValOe2SjbVSXEUan6bUhIbeKIoOiJ3FmHHMS/xxbFBfCZZxhhX0Cw+8AFfkE6LoLsJl 6j0WWnefwggVZVk/RlB7vbrT4lv/jozx5KgBxpW93zl5z/tBrfXnMsATuMaOy2womjav 2N55+tgjY8kbyK4+PPxVu8FR2vwWi0Yp3LUc2YpaYTOT8ddtZptVwnpmNMocLDOB+Bz7 21qA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=7dZ07SIF2p85/LkUAKUz1XlI6aA4LxmHplkFhrygfCg=; b=gmELXxBPdKySVjIKKbvqSuF2OTHk8ouh/VwAXmzPTDZ+kLr0AeJ+CEvhkU4QjYlFcz +tJtoJueEK4K4ljm6TwBhPAinWZKukOOIPUoPiYXz6JoP8rf8Lg+bzNV7/eJzZ9fFE4l yQGpkOyZAQM52HZuWHh36IKpbq7zcfJ5oF6Mhu1NJaVk3nuclIZtIUOfNDzdNuQurfpi 79Mx+pPswJj+M1JBCMJorSxY6ioT3XV5RS49GmTvLrpbn5RmsSaGyBSncu0PDVbaGalz 91Y+Io4l6EDFbewBMInMmfV2SI7+b/yWc8NiBxlQp5vhkScJGzInQlLGv1R8Ja2vXLT8 P0dQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=CyJyyizf; spf=pass (google.com: domain of ulf.hansson@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=ulf.hansson@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id q186sor3218782ljq.10.2018.01.23.02.23.18 for (Google Transport Security); Tue, 23 Jan 2018 02:23:18 -0800 (PST) Received-SPF: pass (google.com: domain of ulf.hansson@linaro.org designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=CyJyyizf; spf=pass (google.com: domain of ulf.hansson@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=ulf.hansson@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=7dZ07SIF2p85/LkUAKUz1XlI6aA4LxmHplkFhrygfCg=; b=CyJyyizfTcKIVpNyuvTDS50cMwMRbcp31An4FYzRZtHSgEIElRD/Pkzy/eGemI1NvC D6ABvd4t3XvCffi9MEbGlPlCFBeG+u9MIZkQRzJKA1cIs/BhQfk/5jabKeRbCce0pEBS 3+73VqxZ/x+wpXEEyw1ILA/yeOaqnXlvs7bT0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=7dZ07SIF2p85/LkUAKUz1XlI6aA4LxmHplkFhrygfCg=; b=Okai4HTaLE1r+OGEUQgDKH6kMJxo7z42gP8kSyAL3vvZ8u/H7PIenryx39fZOlHQ/e 1roWnHiBSiodd3ZEO7NthleCLTGLiXgUkWY4jC7KltgSMTN+HdijcUGyPneD6Dw5S1xd Bo4gmYajQNDuxeNu9CEZr02XwhgYAH9bR3Rv++7xRwOKBvxGH+SkzC8+6bzlZbbggh6z hRzIGArNmZDPaCYfd8LFitEN/y29zhIiKVEfhOGJ3Rsrx2EIEpCDPU48vONj6Mlj0lcw HFE1Hiue/C05IpLXFrA1bXydPTfKGIDxDNgvGiqpdT5PhHXej0oggrWIwIZ2btsAkcum oESA== X-Gm-Message-State: AKwxytfCiM8dfPaYdDcvJoTojKNsv0E6sRL/WqPfxma6xXn4KdTadvIG W0bU2Ol7tJzvR9696b6acLUg2mPI X-Google-Smtp-Source: AH8x226Eart2g1/Bx/l6KveStOLezz1uomdgVSpZgY1mx3pBoDs8IgVjGdctP5LzaFnzL2+agEkfNw== X-Received: by 10.46.71.211 with SMTP id u202mr949368lja.56.1516702998040; Tue, 23 Jan 2018 02:23:18 -0800 (PST) Return-Path: Received: from localhost.localdomain (h-158-174-22-67.NA.cust.bahnhof.se. [158.174.22.67]) by smtp.gmail.com with ESMTPSA id q1sm3000873ljc.61.2018.01.23.02.23.16 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 23 Jan 2018 02:23:17 -0800 (PST) From: Ulf Hansson To: "Rafael J . Wysocki" , Ulf Hansson , linux-pm@vger.kernel.org Cc: khilman@kernel.org, Lina Iyer , Viresh Kumar Subject: [PATCH] PM / Domains: Fixup domain-idle-states OF parsing Date: Tue, 23 Jan 2018 11:23:10 +0100 Message-Id: <1516702990-25564-1-git-send-email-ulf.hansson@linaro.org> X-Mailer: git-send-email 2.7.4 The commit b539cc82d493 ("PM / Domains: Ignore domain-idle-states that are not compatible"), made it possible to ignore non-compatible domain-idle-states OF nodes. However, in case that happens while doing the OF parsing, the number of elements in the allocated array would exceed the numbers actually needed. Moreover, the behaviour may not only cause a bigger allocation than needed, but also makes of_genpd_parse_idle_states() to return the wrong number of allocated elements to the caller. Fix this by pre-iterating the genpd OF node and counting the number of compatible domain-idle-states nodes, before doing the allocation. While doing this, it makes sense to rework the code a bit to avoid open coding, of parts responsible for OF node iteration. Let's also take the opportunity to clarify the function header for of_genpd_parse_idle_states(), about what is being returned in case of errors. Fixes: b539cc82d493 ("PM / Domains: Ignore domain-idle-states that are not compatible") Cc: Lina Iyer Signed-off-by: Ulf Hansson --- drivers/base/power/domain.c | 76 +++++++++++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 31 deletions(-) -- 2.7.4 diff --git a/drivers/base/power/domain.c b/drivers/base/power/domain.c index 528b241..1ea0e25 100644 --- a/drivers/base/power/domain.c +++ b/drivers/base/power/domain.c @@ -2290,6 +2290,38 @@ static int genpd_parse_state(struct genpd_power_state *genpd_state, return 0; } +static int genpd_iterate_idle_states(struct device_node *dn, + struct genpd_power_state *states) +{ + int ret; + struct of_phandle_iterator it; + struct device_node *np; + int i = 0; + + ret = of_count_phandle_with_args(dn, "domain-idle-states", NULL); + if (ret <= 0) + return ret; + + /* Loop over the phandles until all the requested entry is found */ + of_for_each_phandle(&it, ret, dn, "domain-idle-states", NULL, 0) { + np = it.node; + if (!of_match_node(idle_state_match, np)) + continue; + if (states) { + ret = genpd_parse_state(&states[i], np); + if (ret) { + pr_err("Parsing idle state node %pOF failed with err %d\n", + np, ret); + of_node_put(np); + return ret; + } + } + i++; + } + + return i; +} + /** * of_genpd_parse_idle_states: Return array of idle states for the genpd. * @@ -2299,49 +2331,31 @@ static int genpd_parse_state(struct genpd_power_state *genpd_state, * * Returns the device states parsed from the OF node. The memory for the states * is allocated by this function and is the responsibility of the caller to - * free the memory after use. + * free the memory after use. If no domain idle states is found it returns + * -EINVAL and in case of errors, a negative error code. */ int of_genpd_parse_idle_states(struct device_node *dn, struct genpd_power_state **states, int *n) { struct genpd_power_state *st; - struct device_node *np; - int i = 0; - int err, ret; - int count; - struct of_phandle_iterator it; - const struct of_device_id *match_id; + int ret; - count = of_count_phandle_with_args(dn, "domain-idle-states", NULL); - if (count <= 0) - return -EINVAL; + ret = genpd_iterate_idle_states(dn, NULL); + if (ret <= 0) + return ret < 0 ? ret : -EINVAL; - st = kcalloc(count, sizeof(*st), GFP_KERNEL); + st = kcalloc(ret, sizeof(*st), GFP_KERNEL); if (!st) return -ENOMEM; - /* Loop over the phandles until all the requested entry is found */ - of_for_each_phandle(&it, err, dn, "domain-idle-states", NULL, 0) { - np = it.node; - match_id = of_match_node(idle_state_match, np); - if (!match_id) - continue; - ret = genpd_parse_state(&st[i++], np); - if (ret) { - pr_err - ("Parsing idle state node %pOF failed with err %d\n", - np, ret); - of_node_put(np); - kfree(st); - return ret; - } + ret = genpd_iterate_idle_states(dn, st); + if (ret <= 0) { + kfree(st); + return ret < 0 ? ret : -EINVAL; } - *n = i; - if (!i) - kfree(st); - else - *states = st; + *states = st; + *n = ret; return 0; }