linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Rafael J. Wysocki" <rafael@kernel.org>
To: Viresh Kumar <viresh.kumar@linaro.org>
Cc: Quentin Perret <qperret@google.com>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	"Rafael J. Wysocki" <rjw@rjwysocki.net>,
	Arnd Bergmann <arnd@arndb.de>,
	Michael Ellerman <mpe@ellerman.id.au>,
	Benjamin Herrenschmidt <benh@kernel.crashing.org>,
	Paul Mackerras <paulus@samba.org>, Ingo Molnar <mingo@redhat.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Juri Lelli <juri.lelli@redhat.com>,
	Vincent Guittot <vincent.guittot@linaro.org>,
	linuxppc-dev <linuxppc-dev@lists.ozlabs.org>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	Linux PM <linux-pm@vger.kernel.org>,
	"Cc: Android Kernel" <kernel-team@android.com>,
	Todd Kjos <tkjos@google.com>,
	adharmap@codeaurora.org
Subject: Re: [PATCH v2 2/2] cpufreq: Specify default governor on command line
Date: Thu, 25 Jun 2020 12:52:45 +0200	[thread overview]
Message-ID: <CAJZ5v0huMCZcqEkDw2KKp9RMBAU=rpex0zSAw_kK3CqjMyaibw@mail.gmail.com> (raw)
In-Reply-To: <20200625085052.4ah4wbog3guj74v4@vireshk-i7>

On Thu, Jun 25, 2020 at 10:50 AM Viresh Kumar <viresh.kumar@linaro.org> 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 <viresh.kumar@linaro.org>
> 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 <qperret@google.com>
> Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
> ---
>  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)

  reply	other threads:[~2020-06-25 10:53 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-23 14:21 [PATCH v2 0/2] cpufreq: Specify the default governor on command line Quentin Perret
2020-06-23 14:21 ` [PATCH v2 1/2] cpufreq: Register governors at core_initcall Quentin Perret
2020-06-23 14:21 ` [PATCH v2 2/2] cpufreq: Specify default governor on command line Quentin Perret
2020-06-24  5:50   ` Viresh Kumar
2020-06-24 12:51     ` Rafael J. Wysocki
2020-06-24 15:32       ` Quentin Perret
2020-06-25  8:50         ` Viresh Kumar
2020-06-25 10:52           ` Rafael J. Wysocki [this message]
2020-06-25 11:36   ` Viresh Kumar
2020-06-25 11:44     ` Rafael J. Wysocki
2020-06-25 11:53       ` Quentin Perret
2020-06-25 13:28         ` Rafael J. Wysocki
2020-06-25 13:49           ` Quentin Perret
2020-06-25 14:08             ` Rafael J. Wysocki
2020-06-26  2:53   ` Viresh Kumar
2020-06-26  8:09     ` Quentin Perret
2020-06-23 17:54 ` [PATCH v2 0/2] cpufreq: Specify the " Doug Smythies
2020-06-23 18:04   ` Quentin Perret
2020-06-24  0:07     ` Doug Smythies

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAJZ5v0huMCZcqEkDw2KKp9RMBAU=rpex0zSAw_kK3CqjMyaibw@mail.gmail.com' \
    --to=rafael@kernel.org \
    --cc=adharmap@codeaurora.org \
    --cc=arnd@arndb.de \
    --cc=benh@kernel.crashing.org \
    --cc=juri.lelli@redhat.com \
    --cc=kernel-team@android.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=mingo@redhat.com \
    --cc=mpe@ellerman.id.au \
    --cc=paulus@samba.org \
    --cc=peterz@infradead.org \
    --cc=qperret@google.com \
    --cc=rjw@rjwysocki.net \
    --cc=tkjos@google.com \
    --cc=vincent.guittot@linaro.org \
    --cc=viresh.kumar@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).