[PATCH 7/7] cpufreq: Exit governor when failed to start old governor

Lifeng Zheng posted 7 patches 3 months, 2 weeks ago
[PATCH 7/7] cpufreq: Exit governor when failed to start old governor
Posted by Lifeng Zheng 3 months, 2 weeks ago
Detect the result of starting old governor in cpufreq_set_policy(). If it
fails, exit the governor and clear policy->governor.

Signed-off-by: Lifeng Zheng <zhenglifeng1@huawei.com>
---
 drivers/cpufreq/cpufreq.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
index 7b82ffb50283..2b431845a8a3 100644
--- a/drivers/cpufreq/cpufreq.c
+++ b/drivers/cpufreq/cpufreq.c
@@ -2715,10 +2715,12 @@ static int cpufreq_set_policy(struct cpufreq_policy *policy,
 	pr_debug("starting governor %s failed\n", policy->governor->name);
 	if (old_gov) {
 		policy->governor = old_gov;
-		if (cpufreq_init_governor(policy))
+		if (cpufreq_init_governor(policy)) {
 			policy->governor = NULL;
-		else
-			cpufreq_start_governor(policy);
+		} else if (cpufreq_start_governor(policy)) {
+			cpufreq_exit_governor(policy);
+			policy->governor = NULL;
+		}
 	}
 
 	return ret;
-- 
2.33.0
Re: [PATCH 7/7] cpufreq: Exit governor when failed to start old governor
Posted by Rafael J. Wysocki 3 months, 2 weeks ago
On Mon, Jun 23, 2025 at 3:34 PM Lifeng Zheng <zhenglifeng1@huawei.com> wrote:
>
> Detect the result of starting old governor in cpufreq_set_policy(). If it
> fails, exit the governor and clear policy->governor.
>
> Signed-off-by: Lifeng Zheng <zhenglifeng1@huawei.com>
> ---
>  drivers/cpufreq/cpufreq.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
> index 7b82ffb50283..2b431845a8a3 100644
> --- a/drivers/cpufreq/cpufreq.c
> +++ b/drivers/cpufreq/cpufreq.c
> @@ -2715,10 +2715,12 @@ static int cpufreq_set_policy(struct cpufreq_policy *policy,
>         pr_debug("starting governor %s failed\n", policy->governor->name);
>         if (old_gov) {
>                 policy->governor = old_gov;
> -               if (cpufreq_init_governor(policy))
> +               if (cpufreq_init_governor(policy)) {
>                         policy->governor = NULL;
> -               else
> -                       cpufreq_start_governor(policy);
> +               } else if (cpufreq_start_governor(policy)) {
> +                       cpufreq_exit_governor(policy);

This may introduce a governor module reference imbalance AFAICS.

> +                       policy->governor = NULL;
> +               }
>         }
>
>         return ret;
> --
Re: [PATCH 7/7] cpufreq: Exit governor when failed to start old governor
Posted by zhenglifeng (A) 3 months ago
On 2025/6/23 23:12, Rafael J. Wysocki wrote:

> On Mon, Jun 23, 2025 at 3:34 PM Lifeng Zheng <zhenglifeng1@huawei.com> wrote:
>>
>> Detect the result of starting old governor in cpufreq_set_policy(). If it
>> fails, exit the governor and clear policy->governor.
>>
>> Signed-off-by: Lifeng Zheng <zhenglifeng1@huawei.com>
>> ---
>>  drivers/cpufreq/cpufreq.c | 8 +++++---
>>  1 file changed, 5 insertions(+), 3 deletions(-)
>>
>> diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
>> index 7b82ffb50283..2b431845a8a3 100644
>> --- a/drivers/cpufreq/cpufreq.c
>> +++ b/drivers/cpufreq/cpufreq.c
>> @@ -2715,10 +2715,12 @@ static int cpufreq_set_policy(struct cpufreq_policy *policy,
>>         pr_debug("starting governor %s failed\n", policy->governor->name);
>>         if (old_gov) {
>>                 policy->governor = old_gov;
>> -               if (cpufreq_init_governor(policy))
>> +               if (cpufreq_init_governor(policy)) {
>>                         policy->governor = NULL;
>> -               else
>> -                       cpufreq_start_governor(policy);
>> +               } else if (cpufreq_start_governor(policy)) {
>> +                       cpufreq_exit_governor(policy);
> 
> This may introduce a governor module reference imbalance AFAICS.

Sorry, I don't really understand this. Could you explain more? Thanks.

> 
>> +                       policy->governor = NULL;
>> +               }
>>         }
>>
>>         return ret;
>> --
> 

Re: [PATCH 7/7] cpufreq: Exit governor when failed to start old governor
Posted by Rafael J. Wysocki 3 months ago
On Fri, Jul 4, 2025 at 11:38 AM zhenglifeng (A) <zhenglifeng1@huawei.com> wrote:
>
> On 2025/6/23 23:12, Rafael J. Wysocki wrote:
>
> > On Mon, Jun 23, 2025 at 3:34 PM Lifeng Zheng <zhenglifeng1@huawei.com> wrote:
> >>
> >> Detect the result of starting old governor in cpufreq_set_policy(). If it
> >> fails, exit the governor and clear policy->governor.
> >>
> >> Signed-off-by: Lifeng Zheng <zhenglifeng1@huawei.com>
> >> ---
> >>  drivers/cpufreq/cpufreq.c | 8 +++++---
> >>  1 file changed, 5 insertions(+), 3 deletions(-)
> >>
> >> diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
> >> index 7b82ffb50283..2b431845a8a3 100644
> >> --- a/drivers/cpufreq/cpufreq.c
> >> +++ b/drivers/cpufreq/cpufreq.c
> >> @@ -2715,10 +2715,12 @@ static int cpufreq_set_policy(struct cpufreq_policy *policy,
> >>         pr_debug("starting governor %s failed\n", policy->governor->name);
> >>         if (old_gov) {
> >>                 policy->governor = old_gov;
> >> -               if (cpufreq_init_governor(policy))
> >> +               if (cpufreq_init_governor(policy)) {
> >>                         policy->governor = NULL;
> >> -               else
> >> -                       cpufreq_start_governor(policy);
> >> +               } else if (cpufreq_start_governor(policy)) {
> >> +                       cpufreq_exit_governor(policy);
> >
> > This may introduce a governor module reference imbalance AFAICS.
>
> Sorry, I don't really understand this. Could you explain more? Thanks.

It looks like I've confused cpufreq_start_governor() with
cpufreq_init_governor(), sorry for the noise.