]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
opp: Prevent memory leak in dev_pm_opp_attach_genpd()
authorViresh Kumar <viresh.kumar@linaro.org>
Mon, 31 Aug 2020 05:52:37 +0000 (11:22 +0530)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 29 Oct 2020 08:58:06 +0000 (09:58 +0100)
[ Upstream commit cb60e9602cce1593eb1e9cdc8ee562815078a354 ]

If dev_pm_opp_attach_genpd() is called multiple times (once for each CPU
sharing the table), then it would result in unwanted behavior like
memory leak, attaching the domain multiple times, etc.

Handle that by checking and returning earlier if the domains are already
attached. Now that dev_pm_opp_detach_genpd() can get called multiple
times as well, we need to protect that too.

Note that the virtual device pointers aren't returned in this case, as
they may become unavailable to some callers during the middle of the
operation.

Reported-by: Stephan Gerhold <stephan@gerhold.net>
Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/opp/core.c

index 29dfaa591f8b088cc2c15b2c99ec2c4ebf3759c5..8867bab72e1715c1156921abc532e03cf9ec9e04 100644 (file)
@@ -1796,6 +1796,9 @@ static void _opp_detach_genpd(struct opp_table *opp_table)
 {
        int index;
 
+       if (!opp_table->genpd_virt_devs)
+               return;
+
        for (index = 0; index < opp_table->required_opp_count; index++) {
                if (!opp_table->genpd_virt_devs[index])
                        continue;
@@ -1842,6 +1845,9 @@ struct opp_table *dev_pm_opp_attach_genpd(struct device *dev,
        if (!opp_table)
                return ERR_PTR(-ENOMEM);
 
+       if (opp_table->genpd_virt_devs)
+               return opp_table;
+
        /*
         * If the genpd's OPP table isn't already initialized, parsing of the
         * required-opps fail for dev. We should retry this after genpd's OPP