From patchwork Mon Jun 29 08:24:58 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viresh Kumar X-Patchwork-Id: 192012 Delivered-To: patch@linaro.org Received: by 2002:a92:d244:0:0:0:0:0 with SMTP id v4csp3224042ilg; Mon, 29 Jun 2020 14:03:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyzU23HfzSyz90gcBuRd3asQAgcLdrdbwr6i9oHBtruy9W98NQKbPNsXISNDRnKF/da43kW X-Received: by 2002:a17:906:fcb7:: with SMTP id qw23mr14716535ejb.229.1593464620867; Mon, 29 Jun 2020 14:03:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593464620; cv=none; d=google.com; s=arc-20160816; b=QyiwquUQWyx1CbKDW3I/B5p16O4ZqkBpCUj/BBz+LLjtKJTbWnxoG+RyUNZOlvogwR LiLqo4ufXqSyh8a1FdvPhNmMXIoBcuQU4+qJKBjxtp2fpbqVB01YP00DSJJUG/8jLyNn wOfsVoPdm8/plk0wiGGhT3CNdnqaJnzrby0cEIFU9hPHjE8QBGh39ChW0gbLaaBK90zm M/2IsiV30VxI6GELW10Tf0aBv4vjtKb3kXwjguYqDMD+wJP34RlU+p0gK2zXhZeqjwk+ //AK5q0i/AxPX93YYxZBDQNl+2QwZTx5ss9A3nSkFF2ND/blbiP4rzVhKoYfdcbX4pRn OwGw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=9cN6j0Ra4hTEQxVvKv02zIobt4Y/RvzU1LBCy2puJqU=; b=tv8ZgswvbaBTKLl13cUSYVQRjll4mmr90iZOVLSb8b36rw8WiwRJD/TJvoMpCLrriP bGFXsSU7TS+sbf3stdsVL2SCmviWTPHpqB7jbanvKijaGl6nJZMkQLXdYuhC5nAC+bX2 6bA+kvXm5Ntq9KPvXNUtj4wario26rzoqv+Uk5s8nkh+3ReliSXGu1ndj+Qb47FJtErn WkWfmLnSjQGcrYbdronD8TDruF/z5MSh9It/z4BqiF/Jo0TAs+sOrXF3ejlRyhSaZDsX VOZpdIZOWyHo5PgBWfVUOfWbb82knQLQgLMbd7J6Ie1VFr1HFnKE4DZllJ9iPwmh8Y4Q Eqcw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Qgec2BJk; spf=pass (google.com: domain of linux-pm-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-pm-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v2si406927edq.127.2020.06.29.14.03.40; Mon, 29 Jun 2020 14:03:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-pm-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=@linaro.org header.s=google header.b=Qgec2BJk; spf=pass (google.com: domain of linux-pm-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-pm-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390092AbgF2VDj (ORCPT + 10 others); Mon, 29 Jun 2020 17:03:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43382 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731130AbgF2TMq (ORCPT ); Mon, 29 Jun 2020 15:12:46 -0400 Received: from mail-pg1-x543.google.com (mail-pg1-x543.google.com [IPv6:2607:f8b0:4864:20::543]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8397FC0147C7 for ; Mon, 29 Jun 2020 01:25:11 -0700 (PDT) Received: by mail-pg1-x543.google.com with SMTP id w2so7161309pgg.10 for ; Mon, 29 Jun 2020 01:25:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=9cN6j0Ra4hTEQxVvKv02zIobt4Y/RvzU1LBCy2puJqU=; b=Qgec2BJkq7UooQ/OTJc/ye/hxVhyPLlqi9ITafcrZQh0NONrh9LallTFIsrjUAwteY Dxqj09oQUh23HK2po7xV3g5ppKJ6LdmlFXjvcXU9tLMADtodwCAVk7W6mbIm0Vjvkvhl VQFtUPTrEsrutiSPI2UK72jylfkeUtvToUOY0RAj/WMfkUE2yV2kcFCV9fMhckkh5F68 6cTRfyN+70n5zoPOCGBYnoKLNAwQbhXHcuWMjA8OOdvRBcFoUYOBeZHdiqMKIN/LHfK+ BXW8Rus3omR3mCmviN38JBCDpsatBJAKEKvUcJ1UYyy0L3xF7r6YEATZ+158GbnEuCLa UXPw== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=9cN6j0Ra4hTEQxVvKv02zIobt4Y/RvzU1LBCy2puJqU=; b=Dd/vANR5iaCqRU2Nd6GCOM8tX4zlwKC6Z/HEbmV/+5uDcjxk+9JukGhePrKdiMeydk urq0iex1fWcU8LkEEE0zuhJJ5QxefanJOxCJ1qxQLTaOUKiCDy3ZMh0rMOZr/KeIz8a+ XoouqmB4jQn0U8PzeyZOf8vlt8vTFl413VF9MKsrpCNf7eCYe0iAxJGbaOJsoQAxT/ik EEzsr/FRNDwx2SlnqQcvP7GGAiWvnu5yS0CdR+Av03UMUw2LxfEmZ5U0X2LH5A3XVU0W +r3EDIRgKHgLe+dglQNLv7XiLe+sKPBMfshkMa5HwmQQsd/smS0aUxf4h4vTAW1nuEx/ BVFA== X-Gm-Message-State: AOAM5309XQi9wpuqJEkk8pGhRyVPoek0gtPfi4U5SClR+eK9DIX0Vx07 ragRAioFXSdM0MqxZibSvWB+Ag== X-Received: by 2002:a62:1692:: with SMTP id 140mr13937131pfw.168.1593419111080; Mon, 29 Jun 2020 01:25:11 -0700 (PDT) Received: from localhost ([122.172.127.76]) by smtp.gmail.com with ESMTPSA id h7sm8404191pfq.15.2020.06.29.01.25.10 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 29 Jun 2020 01:25:10 -0700 (PDT) From: Viresh Kumar To: Rafael Wysocki , Viresh Kumar Cc: linux-pm@vger.kernel.org, Vincent Guittot , kernel-team@android.com, tkjos@google.com, adharmap@codeaurora.org, Quentin Perret , linux-kernel@vger.kernel.org Subject: [PATCH V4 1/3] cpufreq: Fix locking issues with governors Date: Mon, 29 Jun 2020 13:54:58 +0530 Message-Id: X-Mailer: git-send-email 2.25.0.rc1.19.g042ed3e048af In-Reply-To: References: MIME-Version: 1.0 Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org The locking around governors handling isn't adequate currently. The list of governors should never be traversed without locking in place. Also we must make sure the governor isn't removed while it is still referenced by code. Reported-by: Quentin Perret Signed-off-by: Viresh Kumar --- drivers/cpufreq/cpufreq.c | 58 +++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 23 deletions(-) -- 2.25.0.rc1.19.g042ed3e048af Reviewed-by: Quentin Perret diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c index 0128de3603df..e9e8200a0211 100644 --- a/drivers/cpufreq/cpufreq.c +++ b/drivers/cpufreq/cpufreq.c @@ -621,6 +621,24 @@ static struct cpufreq_governor *find_governor(const char *str_governor) 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_parse_governor(char *str_governor) { struct cpufreq_governor *t; - mutex_lock(&cpufreq_governor_mutex); - - t = find_governor(str_governor); - if (!t) { - int ret; - - mutex_unlock(&cpufreq_governor_mutex); - - ret = request_module("cpufreq_%s", str_governor); - if (ret) - return NULL; - - mutex_lock(&cpufreq_governor_mutex); + t = get_governor(str_governor); + if (t) + return t; - t = find_governor(str_governor); - } - if (t && !try_module_get(t->owner)) - t = NULL; - - mutex_unlock(&cpufreq_governor_mutex); + if (request_module("cpufreq_%s", str_governor)) + return NULL; - return t; + return get_governor(str_governor); } /** @@ -815,12 +819,14 @@ static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy, 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 cpufreq_policy *policy) 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 cpufreq_policy *policy) 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) From patchwork Mon Jun 29 08:24:59 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viresh Kumar X-Patchwork-Id: 192008 Delivered-To: patch@linaro.org Received: by 2002:a92:d244:0:0:0:0:0 with SMTP id v4csp3209965ilg; Mon, 29 Jun 2020 13:40:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzeNm1DsRFayYl2BON1L/1uLQohbBkLg8FReBlhKXoTsD76t1GC96yU5/CLzXUoyk58ieyb X-Received: by 2002:a50:c219:: with SMTP id n25mr19717200edf.306.1593463252314; Mon, 29 Jun 2020 13:40:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593463252; cv=none; d=google.com; s=arc-20160816; b=BLCtEJCiPFbeqqO0OYs2cTNh8YVagZSMLSPBo/Uf0gx1sUdHrHWntoBSywK2o+wtF0 6sMZC3vzopJF5SMpbciBwt+RsjPLcTxrbSmGSDqEGb1X8fs4qyNFGMlG2Q97/amxgjKd PfSjmMlRn0QNH24USfS2ta7gvYeqqj2Pxp2250G58aCUmtFR4kSo6P36hSTnTII8JJet oySr6Vghgt9/7jHkRfkbXQjvKqniHuYIg+FPQCeyvHToZmqIW2Mx0ipe0LSSr3pQGX5e FnGBCFPY1XAieSPL2nm9ugLTYElJin1QSbM7YE/Nxqtq6invlnn+gWT+F1zG43e1crhx Sdtw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=bDEuNi6VcWGVZVG3Ao7I2ar+mftbkIzW0ZQYBhMw7zM=; b=gnS/pY2OSePYMaa/GyegFqRo021RCbTAwcwSkYJ/7LXFJ2FyTbliBJ66R8muKtpsRS izzbKu7RTNINbsS++wZDNxjIGViJP+WLUJG7DQuJXNwpKNNPfXKq6zhB6HExRCkRDFQO 7ok6Ayk8ZslGl1nVWtbB7y+/HnGWDW9EAsZVlh4N4dJxaUiAh7PuyafR3yCvetIWp1vO fz+QNu1KV81U+7E56vPHIRKtnnuDNT5YSnZUJeTcH0eM7bmH5EuAQ00P6mhPdZ39c8MW sbwkhFNxDKH88/xMgFjXTmsMJLJvfwOFzUv4IC/AiwwyxnIlIj8VJ5FBs2COvOt/ri4q DuiQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=CH5VwQtB; spf=pass (google.com: domain of linux-pm-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-pm-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id eb14si501873edb.11.2020.06.29.13.40.47; Mon, 29 Jun 2020 13:40:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-pm-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=@linaro.org header.s=google header.b=CH5VwQtB; spf=pass (google.com: domain of linux-pm-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-pm-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389395AbgF2Ukj (ORCPT + 10 others); Mon, 29 Jun 2020 16:40:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43348 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731701AbgF2TOK (ORCPT ); Mon, 29 Jun 2020 15:14:10 -0400 Received: from mail-pg1-x544.google.com (mail-pg1-x544.google.com [IPv6:2607:f8b0:4864:20::544]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 12F80C0147CB for ; Mon, 29 Jun 2020 01:25:15 -0700 (PDT) Received: by mail-pg1-x544.google.com with SMTP id e18so7963291pgn.7 for ; Mon, 29 Jun 2020 01:25:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=bDEuNi6VcWGVZVG3Ao7I2ar+mftbkIzW0ZQYBhMw7zM=; b=CH5VwQtBwbK7psYBEhtv+ShpD0ybafiNCeyEP4XUtP5hi+Sqgi6uV4Jt6LVnSi7p9T NkHzvSd52VNpimgFoZZqHNLrx3CSEsuY5gsVUKGCli/5+XruiPVSQjO89BV7UEOglnxj eEcGuBBGYiCYGiiSaWdtMDrOP7mu+zLkQhj6HObnhwMa79GWwkwY1s3x3KFVcdDdcVoy NDpFYgZtPN1xcAoLtWIhp8wMaWszjZ6zxz0/yoQYIQoieY/GJ7CK8uIXbKCZjSmh3E/M yJM7aw8DDBt6SPHBK4AkaancxZzkQLOJucjfYxaUu3Gt0+YO8NS9J3JTuxmddkf1nBPQ un1A== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=bDEuNi6VcWGVZVG3Ao7I2ar+mftbkIzW0ZQYBhMw7zM=; b=FcNrqjoW7wfK8uk1Fmu9De7myDtRvaIGoyMA1A9y4VLeAUTlDzZRxAaRkyQoh5IE16 1uIXu5HWniEsabEsMs9Z/v6tkO486K21HqS7YnD1H2HGSbYvo8xtvJjoOCsPN06pTt/B 96vQjXBPXpCe/JKOsSVocpdV2U7jy9wJstUYOzzQS6mZpMtnfaL+aoY1PN/yftwvMXvs ckZktxb7svGGj/0S8xINqWvYutxjOrQ1SFs6cLxo9ifPkWxeLYd2UANT2L0OixIJuJoU eIHUqjH2aRxLG6J0t3XB+QTloep1WPvH78hPajWE4/TtzEQSuUHkO5bXuLsVyapU78An sBYw== X-Gm-Message-State: AOAM531wsuBGnF0LJcAqCXQouGcyEdqILl9czkNzPflMqNSFryiE8Zl5 JZEXORrCpMMuRVDp0p0I85jUmQ== X-Received: by 2002:a62:7847:: with SMTP id t68mr5093605pfc.112.1593419114535; Mon, 29 Jun 2020 01:25:14 -0700 (PDT) Received: from localhost ([122.172.127.76]) by smtp.gmail.com with ESMTPSA id 21sm13043683pfu.124.2020.06.29.01.25.13 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 29 Jun 2020 01:25:13 -0700 (PDT) From: Viresh Kumar To: Rafael Wysocki , Arnd Bergmann , Viresh Kumar , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman Cc: linux-pm@vger.kernel.org, kernel-team@android.com, tkjos@google.com, adharmap@codeaurora.org, Quentin Perret , linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org Subject: [PATCH V4 2/3] cpufreq: Register governors at core_initcall Date: Mon, 29 Jun 2020 13:54:59 +0530 Message-Id: <643132f6e7c8b0e94d03a4d4c121c738504b8aa3.1593418662.git.viresh.kumar@linaro.org> X-Mailer: git-send-email 2.25.0.rc1.19.g042ed3e048af In-Reply-To: References: MIME-Version: 1.0 Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org From: Quentin Perret Currently, most CPUFreq governors are registered at core_initcall time when used as default, and module_init otherwise. In preparation for letting users specify the default governor on the kernel command line, change all of them to use core_initcall unconditionally, as is already the case for schedutil and performance. This will enable us to assume builtin governors have been registered before the builtin CPUFreq drivers probe. And since all governors now have similar init/exit patterns, introduce two new macros cpufreq_governor_{init,exit}() to factorize the code. Acked-by: Viresh Kumar Signed-off-by: Quentin Perret Signed-off-by: Viresh Kumar --- .../platforms/cell/cpufreq_spudemand.c | 26 ++----------------- drivers/cpufreq/cpufreq_conservative.c | 22 ++++------------ drivers/cpufreq/cpufreq_ondemand.c | 24 +++++------------ drivers/cpufreq/cpufreq_performance.c | 14 ++-------- drivers/cpufreq/cpufreq_powersave.c | 18 +++---------- drivers/cpufreq/cpufreq_userspace.c | 18 +++---------- include/linux/cpufreq.h | 14 ++++++++++ kernel/sched/cpufreq_schedutil.c | 6 +---- 8 files changed, 36 insertions(+), 106 deletions(-) -- 2.25.0.rc1.19.g042ed3e048af diff --git a/arch/powerpc/platforms/cell/cpufreq_spudemand.c b/arch/powerpc/platforms/cell/cpufreq_spudemand.c index 55b31eadb3c8..ca7849e113d7 100644 --- a/arch/powerpc/platforms/cell/cpufreq_spudemand.c +++ b/arch/powerpc/platforms/cell/cpufreq_spudemand.c @@ -126,30 +126,8 @@ static struct cpufreq_governor spu_governor = { .stop = spu_gov_stop, .owner = THIS_MODULE, }; - -/* - * module init and destoy - */ - -static int __init spu_gov_init(void) -{ - int ret; - - ret = cpufreq_register_governor(&spu_governor); - if (ret) - printk(KERN_ERR "registration of governor failed\n"); - return ret; -} - -static void __exit spu_gov_exit(void) -{ - cpufreq_unregister_governor(&spu_governor); -} - - -module_init(spu_gov_init); -module_exit(spu_gov_exit); +cpufreq_governor_init(spu_governor); +cpufreq_governor_exit(spu_governor); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Christian Krafft "); - diff --git a/drivers/cpufreq/cpufreq_conservative.c b/drivers/cpufreq/cpufreq_conservative.c index 737ff3b9c2c0..aa39ff31ec9f 100644 --- a/drivers/cpufreq/cpufreq_conservative.c +++ b/drivers/cpufreq/cpufreq_conservative.c @@ -322,17 +322,7 @@ static struct dbs_governor cs_governor = { .start = cs_start, }; -#define CPU_FREQ_GOV_CONSERVATIVE (&cs_governor.gov) - -static int __init cpufreq_gov_dbs_init(void) -{ - return cpufreq_register_governor(CPU_FREQ_GOV_CONSERVATIVE); -} - -static void __exit cpufreq_gov_dbs_exit(void) -{ - cpufreq_unregister_governor(CPU_FREQ_GOV_CONSERVATIVE); -} +#define CPU_FREQ_GOV_CONSERVATIVE (cs_governor.gov) MODULE_AUTHOR("Alexander Clouter "); MODULE_DESCRIPTION("'cpufreq_conservative' - A dynamic cpufreq governor for " @@ -343,11 +333,9 @@ MODULE_LICENSE("GPL"); #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE struct cpufreq_governor *cpufreq_default_governor(void) { - return CPU_FREQ_GOV_CONSERVATIVE; + return &CPU_FREQ_GOV_CONSERVATIVE; } - -core_initcall(cpufreq_gov_dbs_init); -#else -module_init(cpufreq_gov_dbs_init); #endif -module_exit(cpufreq_gov_dbs_exit); + +cpufreq_governor_init(CPU_FREQ_GOV_CONSERVATIVE); +cpufreq_governor_exit(CPU_FREQ_GOV_CONSERVATIVE); diff --git a/drivers/cpufreq/cpufreq_ondemand.c b/drivers/cpufreq/cpufreq_ondemand.c index 82a4d37ddecb..ac361a8b1d3b 100644 --- a/drivers/cpufreq/cpufreq_ondemand.c +++ b/drivers/cpufreq/cpufreq_ondemand.c @@ -408,7 +408,7 @@ static struct dbs_governor od_dbs_gov = { .start = od_start, }; -#define CPU_FREQ_GOV_ONDEMAND (&od_dbs_gov.gov) +#define CPU_FREQ_GOV_ONDEMAND (od_dbs_gov.gov) static void od_set_powersave_bias(unsigned int powersave_bias) { @@ -429,7 +429,7 @@ static void od_set_powersave_bias(unsigned int powersave_bias) continue; policy = cpufreq_cpu_get_raw(cpu); - if (!policy || policy->governor != CPU_FREQ_GOV_ONDEMAND) + if (!policy || policy->governor != &CPU_FREQ_GOV_ONDEMAND) continue; policy_dbs = policy->governor_data; @@ -461,16 +461,6 @@ void od_unregister_powersave_bias_handler(void) } EXPORT_SYMBOL_GPL(od_unregister_powersave_bias_handler); -static int __init cpufreq_gov_dbs_init(void) -{ - return cpufreq_register_governor(CPU_FREQ_GOV_ONDEMAND); -} - -static void __exit cpufreq_gov_dbs_exit(void) -{ - cpufreq_unregister_governor(CPU_FREQ_GOV_ONDEMAND); -} - MODULE_AUTHOR("Venkatesh Pallipadi "); MODULE_AUTHOR("Alexey Starikovskiy "); MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for " @@ -480,11 +470,9 @@ MODULE_LICENSE("GPL"); #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND struct cpufreq_governor *cpufreq_default_governor(void) { - return CPU_FREQ_GOV_ONDEMAND; + return &CPU_FREQ_GOV_ONDEMAND; } - -core_initcall(cpufreq_gov_dbs_init); -#else -module_init(cpufreq_gov_dbs_init); #endif -module_exit(cpufreq_gov_dbs_exit); + +cpufreq_governor_init(CPU_FREQ_GOV_ONDEMAND); +cpufreq_governor_exit(CPU_FREQ_GOV_ONDEMAND); diff --git a/drivers/cpufreq/cpufreq_performance.c b/drivers/cpufreq/cpufreq_performance.c index def9afe0f5b8..71c1d9aba772 100644 --- a/drivers/cpufreq/cpufreq_performance.c +++ b/drivers/cpufreq/cpufreq_performance.c @@ -23,16 +23,6 @@ static struct cpufreq_governor cpufreq_gov_performance = { .limits = cpufreq_gov_performance_limits, }; -static int __init cpufreq_gov_performance_init(void) -{ - return cpufreq_register_governor(&cpufreq_gov_performance); -} - -static void __exit cpufreq_gov_performance_exit(void) -{ - cpufreq_unregister_governor(&cpufreq_gov_performance); -} - #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE struct cpufreq_governor *cpufreq_default_governor(void) { @@ -50,5 +40,5 @@ MODULE_AUTHOR("Dominik Brodowski "); MODULE_DESCRIPTION("CPUfreq policy governor 'performance'"); MODULE_LICENSE("GPL"); -core_initcall(cpufreq_gov_performance_init); -module_exit(cpufreq_gov_performance_exit); +cpufreq_governor_init(cpufreq_gov_performance); +cpufreq_governor_exit(cpufreq_gov_performance); diff --git a/drivers/cpufreq/cpufreq_powersave.c b/drivers/cpufreq/cpufreq_powersave.c index 1ae66019eb83..7749522355b5 100644 --- a/drivers/cpufreq/cpufreq_powersave.c +++ b/drivers/cpufreq/cpufreq_powersave.c @@ -23,16 +23,6 @@ static struct cpufreq_governor cpufreq_gov_powersave = { .owner = THIS_MODULE, }; -static int __init cpufreq_gov_powersave_init(void) -{ - return cpufreq_register_governor(&cpufreq_gov_powersave); -} - -static void __exit cpufreq_gov_powersave_exit(void) -{ - cpufreq_unregister_governor(&cpufreq_gov_powersave); -} - MODULE_AUTHOR("Dominik Brodowski "); MODULE_DESCRIPTION("CPUfreq policy governor 'powersave'"); MODULE_LICENSE("GPL"); @@ -42,9 +32,7 @@ struct cpufreq_governor *cpufreq_default_governor(void) { return &cpufreq_gov_powersave; } - -core_initcall(cpufreq_gov_powersave_init); -#else -module_init(cpufreq_gov_powersave_init); #endif -module_exit(cpufreq_gov_powersave_exit); + +cpufreq_governor_init(cpufreq_gov_powersave); +cpufreq_governor_exit(cpufreq_gov_powersave); diff --git a/drivers/cpufreq/cpufreq_userspace.c b/drivers/cpufreq/cpufreq_userspace.c index b43e7cd502c5..50a4d7846580 100644 --- a/drivers/cpufreq/cpufreq_userspace.c +++ b/drivers/cpufreq/cpufreq_userspace.c @@ -126,16 +126,6 @@ static struct cpufreq_governor cpufreq_gov_userspace = { .owner = THIS_MODULE, }; -static int __init cpufreq_gov_userspace_init(void) -{ - return cpufreq_register_governor(&cpufreq_gov_userspace); -} - -static void __exit cpufreq_gov_userspace_exit(void) -{ - cpufreq_unregister_governor(&cpufreq_gov_userspace); -} - MODULE_AUTHOR("Dominik Brodowski , " "Russell King "); MODULE_DESCRIPTION("CPUfreq policy governor 'userspace'"); @@ -146,9 +136,7 @@ struct cpufreq_governor *cpufreq_default_governor(void) { return &cpufreq_gov_userspace; } - -core_initcall(cpufreq_gov_userspace_init); -#else -module_init(cpufreq_gov_userspace_init); #endif -module_exit(cpufreq_gov_userspace_exit); + +cpufreq_governor_init(cpufreq_gov_userspace); +cpufreq_governor_exit(cpufreq_gov_userspace); diff --git a/include/linux/cpufreq.h b/include/linux/cpufreq.h index 3494f6763597..e62b022cb07e 100644 --- a/include/linux/cpufreq.h +++ b/include/linux/cpufreq.h @@ -577,6 +577,20 @@ unsigned int cpufreq_policy_transition_delay_us(struct cpufreq_policy *policy); int cpufreq_register_governor(struct cpufreq_governor *governor); void cpufreq_unregister_governor(struct cpufreq_governor *governor); +#define cpufreq_governor_init(__governor) \ +static int __init __governor##_init(void) \ +{ \ + return cpufreq_register_governor(&__governor); \ +} \ +core_initcall(__governor##_init) + +#define cpufreq_governor_exit(__governor) \ +static void __exit __governor##_exit(void) \ +{ \ + return cpufreq_unregister_governor(&__governor); \ +} \ +module_exit(__governor##_exit) + struct cpufreq_governor *cpufreq_default_governor(void); struct cpufreq_governor *cpufreq_fallback_governor(void); diff --git a/kernel/sched/cpufreq_schedutil.c b/kernel/sched/cpufreq_schedutil.c index 7fbaee24c824..402a09af9f43 100644 --- a/kernel/sched/cpufreq_schedutil.c +++ b/kernel/sched/cpufreq_schedutil.c @@ -909,11 +909,7 @@ struct cpufreq_governor *cpufreq_default_governor(void) } #endif -static int __init sugov_register(void) -{ - return cpufreq_register_governor(&schedutil_gov); -} -core_initcall(sugov_register); +cpufreq_governor_init(schedutil_gov); #ifdef CONFIG_ENERGY_MODEL extern bool sched_energy_update; From patchwork Mon Jun 29 08:25:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viresh Kumar X-Patchwork-Id: 192009 Delivered-To: patch@linaro.org Received: by 2002:a92:d244:0:0:0:0:0 with SMTP id v4csp3212162ilg; Mon, 29 Jun 2020 13:44:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz91IoVcPk48GM7W3VoAhWk0upOEJKW6aGhYZCzhKnw2gzmdReYMNQ6pCfdUfKWsxAZ/VS/ X-Received: by 2002:a17:906:3ac4:: with SMTP id z4mr13889639ejd.65.1593463482187; Mon, 29 Jun 2020 13:44:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593463482; cv=none; d=google.com; s=arc-20160816; b=OrdXE1ZIIVbR4vU50fmbN0X7X/rpQM3mFpE43iVBjXNm/GSGUOZqSOfO5NJ2jnwAAR 3lGJSQ6XYrsQaHsDOBQnE38z8m3eD4+qC9pFSlyju6v1s5dDomthHemJ+nWUrqpnIsy4 mjkh0HgGTAZUfW1Iakk+qJn87IaVLIYHtpsCDCWn3qQIz45OcRrmyeEoM9vUIkgF1+67 79RY6CBmb0HBGEWIn65RXlnAeN25Wa9pp4YHTPxyNCWnDwHBJnOJFOZzeHnKV2fw/028 DpFSEIFcTYEHoj+XtSjwdWXkvGsYnagyy1ikQaYUEDbfbnkkUjD2GOtYjfk9CmxazhAc cpEg== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=i9Nqhif6ApfXooGjeibxstbXojwGjO9SaXuxe+uQjDQ=; b=aXF+PCHCW6OAI/nMg6DLwfhVcry2qr5cgEGCmgFevmv5tOlteHTNQkUb2WXS8ZATne ASg1HFPsbwlYnfbbnSdc72/w8ap0P9fnU/z8C0zoKKqsHf/TeRD0eKA0OmN0Fq9C6yQM K/+5wOnnELpjOPMS9Qz+B8Gq83q2aD6ur1hnq0+8+ajfAPYElZbVvAKFv6mP/TNT4KXL VVHoWX+jTBRI9jeTo1s2WioTVmXePIXBmDc+zb2dRNnmMFXl18uYcgWH9kBCMbKHejPQ qqIE8a2pN5YArY0uGrSoJt0fgsfbVk7ahJJC1tgzUplqd1+96qVK356jEgjyOHTTSMvl tOYw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=jD0KhJNT; spf=pass (google.com: domain of linux-pm-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-pm-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e13si465888ejc.215.2020.06.29.13.44.42; Mon, 29 Jun 2020 13:44:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-pm-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=@linaro.org header.s=google header.b=jD0KhJNT; spf=pass (google.com: domain of linux-pm-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-pm-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730497AbgF2Uok (ORCPT + 10 others); Mon, 29 Jun 2020 16:44:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43382 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731600AbgF2TN7 (ORCPT ); Mon, 29 Jun 2020 15:13:59 -0400 Received: from mail-pj1-x1043.google.com (mail-pj1-x1043.google.com [IPv6:2607:f8b0:4864:20::1043]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7E134C0147FE for ; Mon, 29 Jun 2020 01:25:18 -0700 (PDT) Received: by mail-pj1-x1043.google.com with SMTP id f6so3688884pjq.5 for ; Mon, 29 Jun 2020 01:25:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=i9Nqhif6ApfXooGjeibxstbXojwGjO9SaXuxe+uQjDQ=; b=jD0KhJNTqUUeR6j8mvpYmsiEJ6YBf4Q+GOdLtNpRkaBosp2KwiuK9yUwZ8EHRsFuai KYlaB4Q7zIBzB2CxHTakt+snj6JFm0zvArqsimL3CTVwFzg9kFc/0uJX+4EJSMKl78Bc YCiIqNCe1diXqPg7c6Mx/WhrlkN/DkzOY8IABTkcpPU92AXSVbkPxz+vgwWZCUWrwHAy HHm4m+xOl4bLGpYOv4MdVIYM7y+wGaHXjZXNn0ntnKRUnenNT2W/SkCtEoTHirw+Bicz 6AptWVdJlrnSBh25JHVXnbv+eT7UEvVvS4o5HLKaQ0MHyTc5UnqrtFjtTGM2izAw55it Xx3w== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=i9Nqhif6ApfXooGjeibxstbXojwGjO9SaXuxe+uQjDQ=; b=QckinMMvincwOSEzXSyrdZ+2N8QyfxowDyiHMIkQ7+yM2t4oo2f8zEgWtlBgeGdBOh sfaJ1BPQYkp7EQ8V2TZq4xKGKdwqCX4BjaYakPMdR+3zIWqqCuD8SptJ2CR1SlDz6gVL T2A6W6xcL0d16qhnqeVS0eQ8GBE3ClluJ13OaH1ZevIhYRrkgPQ81L2Lp0syT0US74+e Z03iB0F9pznNtEnO/DG3ksa3BtBw43IcqvdIyW+qv9brcp2ezsxzE3EJuiQD6SGQTTUb SDr80k5BtVL6rJF9FvFzc3UT7d/8u7X0GHtP7AJJ4FPzH01qXpNXxR9LEzQLNgNeRTmS cBdg== X-Gm-Message-State: AOAM530oGl6paM14bz+14qjafxDp2Pen5obzsVr+2PR3MExOygzRmDz8 QBtqL3qFVM7Gro2/yzMf3Buq+A== X-Received: by 2002:a17:90b:1492:: with SMTP id js18mr8390978pjb.42.1593419118010; Mon, 29 Jun 2020 01:25:18 -0700 (PDT) Received: from localhost ([122.172.127.76]) by smtp.gmail.com with ESMTPSA id 8sm19048210pja.0.2020.06.29.01.25.17 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 29 Jun 2020 01:25:17 -0700 (PDT) From: Viresh Kumar To: Rafael Wysocki , Jonathan Corbet , Viresh Kumar Cc: linux-pm@vger.kernel.org, Vincent Guittot , kernel-team@android.com, tkjos@google.com, adharmap@codeaurora.org, Quentin Perret , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH V4 3/3] cpufreq: Specify default governor on command line Date: Mon, 29 Jun 2020 13:55:00 +0530 Message-Id: <96b6e6ca02b664194ff3e57e1ec768fbc597bf38.1593418662.git.viresh.kumar@linaro.org> X-Mailer: git-send-email 2.25.0.rc1.19.g042ed3e048af In-Reply-To: References: MIME-Version: 1.0 Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org From: Quentin Perret Currently, the only way to specify the default CPUfreq governor is via Kconfig options, which suits users who can build the kernel themselves perfectly. However, for those who use a distro-like kernel (such as Android, with the Generic Kernel Image project), the only way to use a different default is to boot to userspace, and to then switch using the sysfs interface. Being able to specify the default governor on the command line, like is the case for cpuidle, would enable those users to specify their governor of choice earlier on, and to simplify slighlty the userspace boot procedure. To support this use-case, add a kernel command line parameter enabling to specify a default governor for CPUfreq, which takes precedence over the builtin default. This implementation has one notable limitation: the default governor must be registered before the driver. This is solved for builtin governors and drivers using appropriate *_initcall() functions. And in the modular case, this must be reflected as a constraint on the module loading order. Signed-off-by: Quentin Perret [ Viresh: Converted 'default_governor' to a string and parsing it only at initcall level, and several updates to cpufreq_init_policy(). ] Signed-off-by: Viresh Kumar --- .../admin-guide/kernel-parameters.txt | 5 +++ Documentation/admin-guide/pm/cpufreq.rst | 6 ++-- drivers/cpufreq/cpufreq.c | 31 +++++++++++++------ 3 files changed, 30 insertions(+), 12 deletions(-) -- 2.25.0.rc1.19.g042ed3e048af diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt index fb95fad81c79..8deb5a89328a 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -703,6 +703,11 @@ cpufreq.off=1 [CPU_FREQ] disable the cpufreq sub-system + cpufreq.default_governor= + [CPU_FREQ] Name of the default cpufreq governor or + policy to use. This governor must be registered in the + kernel before the cpufreq driver probes. + cpu_init_udelay=N [X86] Delay for N microsec between assert and de-assert of APIC INIT to start processors. This delay occurs diff --git a/Documentation/admin-guide/pm/cpufreq.rst b/Documentation/admin-guide/pm/cpufreq.rst index 0c74a7784964..368e612145d2 100644 --- a/Documentation/admin-guide/pm/cpufreq.rst +++ b/Documentation/admin-guide/pm/cpufreq.rst @@ -147,9 +147,9 @@ CPUs in it. The next major initialization step for a new policy object is to attach a scaling governor to it (to begin with, that is the default scaling governor -determined by the kernel configuration, but it may be changed later -via ``sysfs``). First, a pointer to the new policy object is passed to the -governor's ``->init()`` callback which is expected to initialize all of the +determined by the kernel command line or configuration, but it may be changed +later via ``sysfs``). First, a pointer to the new policy object is passed to +the governor's ``->init()`` callback which is expected to initialize all of the data structures necessary to handle the given policy and, possibly, to add a governor ``sysfs`` interface to it. Next, the governor is started by invoking its ``->start()`` callback. diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c index e9e8200a0211..ad94b1d47ddb 100644 --- a/drivers/cpufreq/cpufreq.c +++ b/drivers/cpufreq/cpufreq.c @@ -50,6 +50,8 @@ static LIST_HEAD(cpufreq_governor_list); #define for_each_governor(__governor) \ list_for_each_entry(__governor, &cpufreq_governor_list, governor_list) +static char default_governor[CPUFREQ_NAME_LEN]; + /** * The "cpufreq driver" - the arch- or hardware-dependent low * level driver of CPUFreq support, and its spinlock. This lock @@ -1061,7 +1063,6 @@ __weak struct cpufreq_governor *cpufreq_default_governor(void) static int cpufreq_init_policy(struct cpufreq_policy *policy) { - struct cpufreq_governor *def_gov = cpufreq_default_governor(); struct cpufreq_governor *gov = NULL; unsigned int pol = CPUFREQ_POLICY_UNKNOWN; int ret; @@ -1071,21 +1072,27 @@ static int cpufreq_init_policy(struct cpufreq_policy *policy) 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); + gov->name, policy->cpu); } else { - return -ENODATA; + gov = get_governor(default_governor); + } + + if (!gov) { + gov = cpufreq_default_governor(); + if (!gov) + return -ENODATA; + __module_get(gov->owner); } + } else { + /* Use the default policy if there is no last_policy. */ if (policy->last_policy) { pol = policy->last_policy; - } else if (def_gov) { - pol = cpufreq_parse_policy(def_gov->name); + } else { + pol = cpufreq_parse_policy(default_governor); /* - * In case the default governor is neiter "performance" + * In case the default governor is neither "performance" * nor "powersave", fall back to the initial policy * value set by the driver. */ @@ -2795,13 +2802,19 @@ EXPORT_SYMBOL_GPL(cpufreq_unregister_driver); static int __init cpufreq_core_init(void) { + struct cpufreq_governor *gov = cpufreq_default_governor(); + if (cpufreq_disabled()) return -ENODEV; cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj); BUG_ON(!cpufreq_global_kobject); + if (!strlen(default_governor)) + strncpy(default_governor, gov->name, CPUFREQ_NAME_LEN); + return 0; } module_param(off, int, 0444); +module_param_string(default_governor, default_governor, CPUFREQ_NAME_LEN, 0444); core_initcall(cpufreq_core_init);