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: 10181043 X-Patchwork-Delegate: rjw@sisk.pl Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 9440C6037F for ; Tue, 23 Jan 2018 20:43:20 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 79297287E1 for ; Tue, 23 Jan 2018 20:43:20 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6DB60287E6; Tue, 23 Jan 2018 20:43:20 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 87D70287E1 for ; Tue, 23 Jan 2018 20:43:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752601AbeAWUnR (ORCPT ); Tue, 23 Jan 2018 15:43:17 -0500 Received: from mail-lf0-f67.google.com ([209.85.215.67]:45790 "EHLO mail-lf0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752568AbeAWUnR (ORCPT ); Tue, 23 Jan 2018 15:43:17 -0500 Received: by mail-lf0-f67.google.com with SMTP id x196so2288658lfd.12 for ; Tue, 23 Jan 2018 12:43:16 -0800 (PST) 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=D05c/1RqgS1dw7Ydo1dETRxyZyjyFLXyD/iQCiuvrcWbex3YcAGaKBHTtFkK+hXUs5 1GBcppZbmsfEu/Fo6ODvXbgOmEhZYM7BW3AaCNeX8i0eg5QoHVcOBZaXauvnEweTcx9f fZL6pOd2OPhe+RybxQN01ndmyr5m5jkuC5XeBuafGLw28UKoKUQZ3+b2uAugy5bznDoX gbANKVsva8pp4vOq5o0w73nWR20OqXFgxRn8RDCjoekG86tMMoNAOTNe0xrqTeDMBSv6 XnluzUK235p28fr/7+cTgfJBU0sjvl0d6qdUfiFZAkSeRlNDVOw01zHZLKgryBMvMVjI Y3HQ== X-Gm-Message-State: AKwxytfdvBFj7vvm7nM+UQu4kk0TjD9OYZvCSZQdb1jzWlNAwA1gXGtM FwlPX7MFoAYCSbfjXLW6LrEnSg== 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) 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 Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 Reviewed-by: Lina Iyer --- 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(-) 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; }