From mboxrd@z Thu Jan 1 00:00:00 1970 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=-7.1 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED 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 7908EC433E0 for ; Thu, 25 Jun 2020 10:52:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4BFB2207BB for ; Thu, 25 Jun 2020 10:52:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1593082379; bh=soNUNt6SN0NBZi4lpB0mUfayqf6qO/jv1+DvyYAoyEE=; h=References:In-Reply-To:From:Date:Subject:To:Cc:List-ID:From; b=FGxac0DMxPZwAuvoJmLl8qvqtlyhtdPbLmWJaMS5sRO1Wd9/QAfy72Qr6mYvlbDJC rUdnCS3Ry9vwSVFP/kjyhYKRxD/2uLiYA+p5JHbUcZD6kanR2XgZuEYaNxGiumjkSN MNsQPJDycx6vkaFP7bPuhFW9yJ75ZuX9iMN3eMBg= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2404089AbgFYKw6 (ORCPT ); Thu, 25 Jun 2020 06:52:58 -0400 Received: from mail-oi1-f193.google.com ([209.85.167.193]:41993 "EHLO mail-oi1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2403952AbgFYKw6 (ORCPT ); Thu, 25 Jun 2020 06:52:58 -0400 Received: by mail-oi1-f193.google.com with SMTP id s21so4541261oic.9; Thu, 25 Jun 2020 03:52:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=cwIdSXPUuU1uRqpvtJKtzstYQAUArlOMALUNp7co5jM=; b=tMNWIAwmLKevjNxSYczq7350FAkRplHgyVO2Ib5MBFXOQpk6NGLI53pG68xjXSIT9i 5ZM0SiUJN8V2V71kjxWJwQPdAuSeLhLogvhhaTLAY1D36O7MY1TGLsUWD8KYs/nkh0BL 7UxmMXjgd14veCtKITiI4q5VnMcKiBwNmsELzeuZX/L5bGXVPb4QzyH0gpmcnVioKFdd VOoceNKTqMAfSQAgNffVQaNntBwD55Fu0yvVSY7w5cJfdw6dFegkXDfv98GDqD0ZQfHw pTJfQW9C5XUve3ClM+BYxAjP1vse+k/rQAjCnJ2N0EU74fDiu6ReGIi3bW2vy+Q14/3V encA== X-Gm-Message-State: AOAM533sTjVlw5kxuos1rf5CRILy2HmzXRGQ8xqL9V0s57y5M0Z6Mwa5 XWajOJz6pBXLC3oV7qcQQRtutaWk5MxsP8nAE/0= X-Google-Smtp-Source: ABdhPJwRCQMnCB/bfuvmkARjDAhUHGU6ki0GfoTWamxo9Rq1xfFvWJi8TwqcHRaFJuXBeT1DMVCjbHvWJZL0Eu9jkFE= X-Received: by 2002:a54:4585:: with SMTP id z5mr1704626oib.110.1593082376670; Thu, 25 Jun 2020 03:52:56 -0700 (PDT) MIME-Version: 1.0 References: <20200623142138.209513-1-qperret@google.com> <20200623142138.209513-3-qperret@google.com> <20200624055023.xofefhohf7wifme5@vireshk-i7> <20200624153259.GA2844@google.com> <20200625085052.4ah4wbog3guj74v4@vireshk-i7> In-Reply-To: <20200625085052.4ah4wbog3guj74v4@vireshk-i7> From: "Rafael J. Wysocki" Date: Thu, 25 Jun 2020 12:52:45 +0200 Message-ID: Subject: Re: [PATCH v2 2/2] cpufreq: Specify default governor on command line To: Viresh Kumar Cc: Quentin Perret , "Rafael J. Wysocki" , "Rafael J. Wysocki" , Arnd Bergmann , Michael Ellerman , Benjamin Herrenschmidt , Paul Mackerras , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , linuxppc-dev , Linux Kernel Mailing List , Linux PM , "Cc: Android Kernel" , Todd Kjos , adharmap@codeaurora.org Content-Type: text/plain; charset="UTF-8" Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org On Thu, Jun 25, 2020 at 10:50 AM Viresh Kumar wrote: > > On 24-06-20, 16:32, Quentin Perret wrote: > > Right, but I must admit that, looking at this more, I'm getting a bit > > confused with the overall locking for governors :/ > > > > When in cpufreq_init_policy() we find a governor using > > find_governor(policy->last_governor), what guarantees this governor is > > not concurrently unregistered? That is, what guarantees this governor > > doesn't go away between that find_governor() call, and the subsequent > > call to try_module_get() in cpufreq_set_policy() down the line? > > > > Can we somewhat assume that whatever governor is referred to by > > policy->last_governor will have a non-null refcount? Or are the > > cpufreq_online() and cpufreq_unregister_governor() path mutually > > exclusive? Or is there something else? > > This should be sufficient to fix pending issues I believe. Based over your > patches. LGTM, but can you post it in a new thread to let Patchwork pick it up? > -------------------------8<------------------------- > From: Viresh Kumar > Date: Thu, 25 Jun 2020 13:15:23 +0530 > Subject: [PATCH] cpufreq: Fix locking issues with governors > > 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 | 59 ++++++++++++++++++++++++--------------- > 1 file changed, 36 insertions(+), 23 deletions(-) > > diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c > index 4b1a5c0173cf..dad6b85f4c89 100644 > --- a/drivers/cpufreq/cpufreq.c > +++ b/drivers/cpufreq/cpufreq.c > @@ -624,6 +624,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)) > @@ -643,28 +661,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); > } > > /** > @@ -818,12 +822,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; > @@ -1060,11 +1066,14 @@ static int cpufreq_init_policy(struct cpufreq_policy *policy) > { > struct cpufreq_governor *gov = NULL; > unsigned int pol = CPUFREQ_POLICY_UNKNOWN; > + bool put_governor = false; > + 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) { > + put_governor = true; > pr_debug("Restoring governor %s for cpu %d\n", > policy->governor->name, policy->cpu); > } else if (default_governor) { > @@ -1091,7 +1100,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 (put_governor) > + module_put(gov->owner); > + > + return ret; > } > > static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)