From patchwork Mon Aug 17 15:17:21 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: 247821 Delivered-To: patch@linaro.org Received: by 2002:a92:cc90:0:0:0:0:0 with SMTP id x16csp2762278ilo; Mon, 17 Aug 2020 09:09:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzg9qbpkxeF5jh03TJ0GfbDKTw/GoaDpE2xVo7oyf7Pv4IyNbaWpzs3RxuJ2ejuV48+196w X-Received: by 2002:a17:906:b294:: with SMTP id q20mr15379129ejz.223.1597680588809; Mon, 17 Aug 2020 09:09:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597680588; cv=none; d=google.com; s=arc-20160816; b=d7wsewxZuASp93KNCu8I/wBnpjOTQBWtGsrkN+OFHaTyeQS7/8t7X0Xx/EGs38c3e0 J974HM6LCg7MkbvmZ2x7yFZyD3yY8bJ7F5/ysRBKL68Cq3JHVeGbyXbHUaq0WgMs7S3e GdLoGyDDEZy9eL150x4Oq0b/nWat8AsbXvo2VTMOr7nyKt31teINPB45hM5h/d9lIo9e cRV9mSt82rOeZ82ifRW1Jvjleqjvi3Jtl9XEmvQ6vh/+4PfalgeQ7XWWlQbq2vYkiXrL JEAR6mtjR02qwWsoOd9AALBf/btSNpO3VccpF3hPpR3v2IRPmaM+QOjYnVcvNQX51TBG j+YQ== 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=0zzQGwMaNTfPONmwwAYm0vHltitAFn0mcoowXe8ioBQ=; b=LbEBPN1N2d1jLvEqyzH7Mi8D+QV/TUXi1HZh4xEPbtH/cxyFh3h0umt5UZlnTXCAld 2bL2kHveoJMoenrk0HQvZt4Vey8sxpKjTX85lVaO70DWTGQPfMDhR3BGpn/NDjZupchI 7o3vn/KtMBYBJ9Ay398ig3mdR4n9MqNYSrdHqDDa5F9prekw0/7MqeBBm9D11LckEIAe WKXLbs/Y+eNJec8mokL/ytbJnx920pWj7acZwO37tlMWZL87lgNPuiGSMQr5fjWJPanI +QOaa0Ua8MoVS78zvUDjrF9tgnMLADIuY7KiSD7a4Z9ifb42mU70sKFffcOQcqKhvKnC PhfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=krxqFnR2; 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 e17si11229191ejx.211.2020.08.17.09.09.48; Mon, 17 Aug 2020 09:09:48 -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=krxqFnR2; 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 S1730885AbgHQQJe (ORCPT + 15 others); Mon, 17 Aug 2020 12:09:34 -0400 Received: from mail.kernel.org ([198.145.29.99]:59836 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388386AbgHQQJb (ORCPT ); Mon, 17 Aug 2020 12:09:31 -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 B320D20729; Mon, 17 Aug 2020 16:09:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1597680570; bh=s9Uz8xel/PTVtq7+hr3B9WgsiuOjZDmxIlXkQ49B1oU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=krxqFnR2NUE02DoK7i8c0sF5RBwkENv6QquJY6LcJTWCh5UiYrcbg5/TWsLaabJpm F2vFCnU9WyX4C++BFS1lfww3kAgDMj8Rki1fXSfuvssqtW+booko1t4oVrBmlcMZi0 ZaaG0QXvszI6Dze5UZY6YfDT3os2T4DBIzxi2Urg= 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.4 240/270] cpufreq: Fix locking issues with governors Date: Mon, 17 Aug 2020 17:17:21 +0200 Message-Id: <20200817143807.770401962@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200817143755.807583758@linuxfoundation.org> References: <20200817143755.807583758@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 @@ -616,6 +616,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)) @@ -635,28 +653,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); } /** @@ -810,12 +814,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; @@ -1053,15 +1059,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; } @@ -1084,7 +1092,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)