From patchwork Tue Jan 23 20:43:08 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ulf Hansson X-Patchwork-Id: 125589 Delivered-To: patches@linaro.org Received: by 10.46.66.141 with SMTP id h13csp1993687ljf; Tue, 23 Jan 2018 12:43:15 -0800 (PST) X-Received: by 10.46.99.216 with SMTP id s85mr1867950lje.82.1516740195900; Tue, 23 Jan 2018 12:43:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516740195; cv=none; d=google.com; s=arc-20160816; b=FRrjoslQacuLEOVZMIqbJ/QQxZP/CYU8IFbySci7p127iZnFnW5/31Fkv1rn8Ib5ZI Vcbc24Fh6jZ2IPsUDzicVi3wxGPJPdGSIOu0rl67+VdMRpnj/acLR/j8ZCXLok+CIqRE dt18qiSxHZefbcvRyOJ8L2xvxXDcZtRswhO4A/V1y5AW5Yn+AUJTLjhxc7AJvb6BppHe 4zdKtWC1DpLrlOsF2H8B0mggblDwNnKDFUvq8c9cUps3GIcy6tU+UdEcuUylPIVV9W9C TuBSEjPqKezrDj4AZ5vGAsKr1wzkQXX7RJsnkl0y4/rL2YchnHN7FsH3dv4VJURIJshu PfqQ== 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=/UDkmCL8t+OUEvXAU2f7O5Kc3FBJR+BbIDejJKozl+8=; b=DzErFII/8aWKMomKFWg7x32Be0TFAD7LuLAnxl7RuKh4r0WN6t7qYLqXWbVfSBKsDv 1KY5x1/EjSYXWPGsUJIpZ4Fe5I/krZ3nKCOS4DZgtdhnz/UeGLdQBm6aX9h48AOPJMAN F8vV0bLio9ypapzV1mkfq0kdtZ0V/Kah1wkgCbsL/2pCNL534r/Dd0VTMA5NahEOWlVr 2eZIIKnZTVxCRYL7Vg/FtMhxJ+0kK+3iuZcUkhKxkPmY+lFcbVzqXbn0TTlFPSev7dFs r9gy62iGdCjT2OAUJXxdLIcHeV0UCqn2OQiv7c+cgUJwsATCtsXypVyTaEeFGQOAYEfo 3c6A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=WIzyJQzS; 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 j16sor290347lfg.31.2018.01.23.12.43.15 for (Google Transport Security); Tue, 23 Jan 2018 12:43:15 -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=WIzyJQzS; 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=/UDkmCL8t+OUEvXAU2f7O5Kc3FBJR+BbIDejJKozl+8=; b=WIzyJQzSZtiSFumpo/S9yBYCtNwAYMhfzOj/7tqSBz0KXujI8gWbjjGUuoCe3I1QCt 6Neg6gFNJdqEdCREJxdRBVKAzjgZGvhw3fSskd5W7ONhxvI4kdCDyDBEp1qsTw9kXtka TkydMNghiGp+qqNgs1wY9nFWDKDUtqkVNFq1U= 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=/UDkmCL8t+OUEvXAU2f7O5Kc3FBJR+BbIDejJKozl+8=; b=mwZafkv6GUukAbQICGH+J9VJh1oeRKDug5DEgNgnbtA3Rqwcq+0EuybqNYH4Gsvs7m l2nKJo2JRMY2ox3dYEpUZWXUFaSmRQYMF6fmj+BN9xdpgGitDVDNGgVhBYbiYQfFVNw6 YdsuBtqR762QfZueLH24p/zfkFQ1DSGdAo9x97/gcXVZauTYxwJ+hw0xErOe8KdJADh6 SnjFa0xGDXMTA4ZV+RrVNuTB8uJXjBIjkxB56X0kN3l+p/OwLWywGsbcQGVhyZGwahJT pmohfltpJMgdFDveYg1FqFYLLWpVT9IHy8WWvIiNdWgv4mhD0/KaPWBFqkrGRfKtDyaF l6GA== X-Gm-Message-State: AKwxyteaAokvwwcJv9sXVZD00MKiaH/uCaeQEyzdFAQ/SbAufyqCJbT5 fd9p8m7rzN4OgNR37FQzPeVOdW9Q X-Google-Smtp-Source: AH8x224uFA82yEggvIIbmkO6A/wUK/MoZBcSouV6lszaBDfvzBmREDOqtVRsjGLICg4rh7hd2Wjb2A== X-Received: by 10.25.75.132 with SMTP id y126mr1881190lfa.122.1516740195459; Tue, 23 Jan 2018 12:43:15 -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 i19sm193203lfk.37.2018.01.23.12.43.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 23 Jan 2018 12:43:14 -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 v2] PM / Domains: Fixup domain-idle-states OF parsing Date: Tue, 23 Jan 2018 21:43:08 +0100 Message-Id: <1516740188-3580-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, thus wasting memory. 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 the 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 --- Changes in v2: - Updated changelog to clarify the problem is really about avoiding to waste memory, according to comment from Lina. --- drivers/base/power/domain.c | 76 +++++++++++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 31 deletions(-) -- 2.7.4 Reviewed-by: Lina Iyer 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; }