From patchwork Mon Aug 17 15:16:50 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg Kroah-Hartman X-Patchwork-Id: 247849 Delivered-To: patch@linaro.org Received: by 2002:a92:cc90:0:0:0:0:0 with SMTP id x16csp2868665ilo; Mon, 17 Aug 2020 11:35:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzY6iHowaqC1ulhcpgTkU8h0LTWAUnZCoIE82r8ZX6yDhKll0KKgUByt9OH4YlsdjMkIIuA X-Received: by 2002:a17:906:36c2:: with SMTP id b2mr16011463ejc.238.1597689349312; Mon, 17 Aug 2020 11:35:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597689349; cv=none; d=google.com; s=arc-20160816; b=fDDhRt4OpzMSBlebifWHOU6MkeP4M6lKBwK1X0+QxVmE8giTZZGYxt+8lmVDFxwi1p Ty85dcRtb5UlaGnBwm4scCkL5Q+0acaSpr18c2Tfy9MoU75atVDB+AXa73b5uC6ZOECr FLLf+RRaYS/QO4fPJxczu5xqvOFq3os6Lo2xr0/tMcpCM/t3wOKbyM4Sx6QSHrU6RaVC 8BzMZXMGjUAlXS2qIsKxMXZxUiGyw7hBMuLBmQ7VBNDzolcWvIlJwFqQuYS+YtMY4pPg aSPMGMuqraJ6tP1UiSvZCcYqCGzFguUYZgc6R+9tntjs1KaKqv/vRhdRiyhVDLtnUs3R iZYA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=UqUZ/vYaecK48b9Q1wXQd6dpYinGu2pRWe2XxBk1TfI=; b=08Ruqrac/8Z+zr0DEwSGtc3UwZubsw/9jKXEqL2wsEf2ufFnZXxMu9Z8UuPdBFIouZ a9B+16Ntir4+QVwh2wos14QTPiXbak0JArPHUgdpqyfVcpqe+c1jR4hZYy922bK7QCX7 oTdEHnDsQXsctxpPnUSNWx5DDsytaQu/Rm5LGAyTCs0ZoN5HvHYNiZcDmPhkLB6x1lMg TQqpIwX4VV0iQR0hfL2hXOyx5lfmpqKCnsUUyHafetF4BiYFe67mKrOEuEdCmDwn6t+4 qaHbYkOEiH6/tADVm8xukRZQLDf/Irw4OSOeGse+YTJMAZU+ftHQ3n5XPArztkhjVTRp X+fQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=gB0mKpFS; spf=pass (google.com: domain of stable-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=stable-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g8si12719774ejm.100.2020.08.17.11.35.49; Mon, 17 Aug 2020 11:35:49 -0700 (PDT) Received-SPF: pass (google.com: domain of stable-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=gB0mKpFS; spf=pass (google.com: domain of stable-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=stable-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730974AbgHQP7g (ORCPT + 15 others); Mon, 17 Aug 2020 11:59:36 -0400 Received: from mail.kernel.org ([198.145.29.99]:46462 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730954AbgHQP7N (ORCPT ); Mon, 17 Aug 2020 11:59:13 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (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 DD06920729; Mon, 17 Aug 2020 15:59:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1597679952; bh=Awa9szfVnyfHhZXV0EKIVyiunFy+LWKFXGUkDfuCRVk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gB0mKpFSuvlUHhx0KTi2rVElOuXcEZ4D0NVpcoc0tUb3NM4Ar14of0CKuF6PasKeC bh75QSvc1EAZIhxjZ9pILv8hGdyCFxdRkwzlHK3D8e7B5bl6Ubgil+eI3QGZ6I1pBn c+dQxXgudZr2LNAL4Tf5P6ibMeeKW/gvWDzSzMxo= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Quentin Perret , Viresh Kumar , "Rafael J. Wysocki" Subject: [PATCH 5.7 360/393] cpufreq: Fix locking issues with governors Date: Mon, 17 Aug 2020 17:16:50 +0200 Message-Id: <20200817143837.065164987@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200817143819.579311991@linuxfoundation.org> References: <20200817143819.579311991@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Viresh Kumar commit 8cc46ae565c393f77417cb9530b1265eb50f5d2e upstream. The locking around governors handling isn't adequate currently. The list of governors should never be traversed without the locking in place. Also governor modules must not be removed while the code in them is still in use. Reported-by: Quentin Perret Signed-off-by: Viresh Kumar Cc: All applicable [ rjw: Changelog ] Signed-off-by: Rafael J. Wysocki Signed-off-by: Greg Kroah-Hartman --- drivers/cpufreq/cpufreq.c | 58 +++++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 23 deletions(-) --- a/drivers/cpufreq/cpufreq.c +++ b/drivers/cpufreq/cpufreq.c @@ -621,6 +621,24 @@ static struct cpufreq_governor *find_gov return NULL; } +static struct cpufreq_governor *get_governor(const char *str_governor) +{ + struct cpufreq_governor *t; + + mutex_lock(&cpufreq_governor_mutex); + t = find_governor(str_governor); + if (!t) + goto unlock; + + if (!try_module_get(t->owner)) + t = NULL; + +unlock: + mutex_unlock(&cpufreq_governor_mutex); + + return t; +} + static unsigned int cpufreq_parse_policy(char *str_governor) { if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) @@ -640,28 +658,14 @@ static struct cpufreq_governor *cpufreq_ { struct cpufreq_governor *t; - mutex_lock(&cpufreq_governor_mutex); - - t = find_governor(str_governor); - if (!t) { - int ret; + t = get_governor(str_governor); + if (t) + return t; - mutex_unlock(&cpufreq_governor_mutex); + if (request_module("cpufreq_%s", str_governor)) + return NULL; - ret = request_module("cpufreq_%s", str_governor); - if (ret) - return NULL; - - mutex_lock(&cpufreq_governor_mutex); - - t = find_governor(str_governor); - } - if (t && !try_module_get(t->owner)) - t = NULL; - - mutex_unlock(&cpufreq_governor_mutex); - - return t; + return get_governor(str_governor); } /** @@ -815,12 +819,14 @@ static ssize_t show_scaling_available_go goto out; } + mutex_lock(&cpufreq_governor_mutex); for_each_governor(t) { if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2))) - goto out; + break; i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name); } + mutex_unlock(&cpufreq_governor_mutex); out: i += sprintf(&buf[i], "\n"); return i; @@ -1058,15 +1064,17 @@ static int cpufreq_init_policy(struct cp struct cpufreq_governor *def_gov = cpufreq_default_governor(); struct cpufreq_governor *gov = NULL; unsigned int pol = CPUFREQ_POLICY_UNKNOWN; + int ret; if (has_target()) { /* Update policy governor to the one used before hotplug. */ - gov = find_governor(policy->last_governor); + gov = get_governor(policy->last_governor); if (gov) { pr_debug("Restoring governor %s for cpu %d\n", policy->governor->name, policy->cpu); } else if (def_gov) { gov = def_gov; + __module_get(gov->owner); } else { return -ENODATA; } @@ -1089,7 +1097,11 @@ static int cpufreq_init_policy(struct cp return -ENODATA; } - return cpufreq_set_policy(policy, gov, pol); + ret = cpufreq_set_policy(policy, gov, pol); + if (gov) + module_put(gov->owner); + + return ret; } static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)