From patchwork Tue Oct 27 13:55:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 312336 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1779BC5DF9D for ; Tue, 27 Oct 2020 16:26:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BAB3E22202 for ; Tue, 27 Oct 2020 16:26:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603816017; bh=gUnPsjpz8bbn+vtIHxWGc2snR+x1L4HKGTSAMvog4+U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=muq1sHPGAX+jTYrNilgPJDuqBEgvu9U1hfTDmTPbcovIwrF/4CZZEyoFAkOkzdIg5 xs21VWD3vxFvVMcxDF3wz2yVdzJ8zi1XMw/10RcLVpUfln7j6XPHuE4KiVwpIiqN2D jmStIUJlNxZrZiUx92r20hhVPmxPXe+F7L6yK2cg= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1809610AbgJ0Q0z (ORCPT ); Tue, 27 Oct 2020 12:26:55 -0400 Received: from mail.kernel.org ([198.145.29.99]:53084 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1802814AbgJ0Pve (ORCPT ); Tue, 27 Oct 2020 11:51:34 -0400 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id A27CC2065C; Tue, 27 Oct 2020 15:51:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603813894; bh=gUnPsjpz8bbn+vtIHxWGc2snR+x1L4HKGTSAMvog4+U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hlqGCBJrAt/4+oXeqUCiods9WrZyeMIblXYjc+ZOKB2Z0YTGP3k7UXtZNXJH/okRY AQb7Ee954O04O2ByBtDsbsMRmPvOR89ISWmF8Czlmz9nNd2wxjgqHIqhVrlKmLcKAo t21DnPJ0Jll6UC2qMS3QAvEKenaK8eDD+gK3GfgQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Stephan Gerhold , Viresh Kumar , Sasha Levin Subject: [PATCH 5.9 705/757] opp: Prevent memory leak in dev_pm_opp_attach_genpd() Date: Tue, 27 Oct 2020 14:55:55 +0100 Message-Id: <20201027135523.573660843@linuxfoundation.org> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20201027135450.497324313@linuxfoundation.org> References: <20201027135450.497324313@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Viresh Kumar [ 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 Signed-off-by: Viresh Kumar Signed-off-by: Sasha Levin --- drivers/opp/core.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/drivers/opp/core.c b/drivers/opp/core.c index 3ca7543142bf3..1a95ad40795be 100644 --- a/drivers/opp/core.c +++ b/drivers/opp/core.c @@ -1949,6 +1949,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; @@ -1995,6 +1998,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