* [PATCH] cpufreq: fix double unlock when cpufreq online
[not found] <20220506072146.GD4031@kadam>
@ 2022-05-06 17:00 ` Schspa Shi
2022-05-09 4:00 ` Viresh Kumar
0 siblings, 1 reply; 2+ messages in thread
From: Schspa Shi @ 2022-05-06 17:00 UTC (permalink / raw)
To: rafael, viresh.kumar, dan.carpenter; +Cc: linux-pm, linux-kernel, schspa
The patch f346e96267cd: ("cpufreq: Fix possible race in cpufreq online
error path") expand the critical region. But policy->rwsem is not held when
calling cpufreq_driver->online and cpufreq_driver->init calls, which lead to bad
unlock.
And it's well to hold this lock when calling cpufreq_driver->online, which
provide more protects without bad influence.
Fixes: f346e96267cd: ("cpufreq: Fix possible race in cpufreq online error path")
Link: https://lore.kernel.org/all/YnKZCGaig+EXSowf@kili/
Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
Signed-off-by: Schspa Shi <schspa@gmail.com>
---
drivers/cpufreq/cpufreq.c | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
index 0d58b0f8f3af..43dfaa8124e2 100644
--- a/drivers/cpufreq/cpufreq.c
+++ b/drivers/cpufreq/cpufreq.c
@@ -1337,12 +1337,12 @@ static int cpufreq_online(unsigned int cpu)
down_write(&policy->rwsem);
policy->cpu = cpu;
policy->governor = NULL;
- up_write(&policy->rwsem);
} else {
new_policy = true;
policy = cpufreq_policy_alloc(cpu);
if (!policy)
return -ENOMEM;
+ down_write(&policy->rwsem);
}
if (!new_policy && cpufreq_driver->online) {
@@ -1382,7 +1382,6 @@ static int cpufreq_online(unsigned int cpu)
cpumask_copy(policy->related_cpus, policy->cpus);
}
- down_write(&policy->rwsem);
/*
* affected cpus must always be the one, which are online. We aren't
* managing offline cpus here.
@@ -1542,9 +1541,9 @@ static int cpufreq_online(unsigned int cpu)
cpufreq_driver->exit(policy);
cpumask_clear(policy->cpus);
- up_write(&policy->rwsem);
out_free_policy:
+ up_write(&policy->rwsem);
cpufreq_policy_free(policy);
return ret;
}
--
2.24.3 (Apple Git-128)
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] cpufreq: fix double unlock when cpufreq online
2022-05-06 17:00 ` [PATCH] cpufreq: fix double unlock when cpufreq online Schspa Shi
@ 2022-05-09 4:00 ` Viresh Kumar
0 siblings, 0 replies; 2+ messages in thread
From: Viresh Kumar @ 2022-05-09 4:00 UTC (permalink / raw)
To: Schspa Shi; +Cc: rafael, dan.carpenter, linux-pm, linux-kernel
On 07-05-22, 01:00, Schspa Shi wrote:
> The patch f346e96267cd: ("cpufreq: Fix possible race in cpufreq online
> error path") expand the critical region. But policy->rwsem is not held when
> calling cpufreq_driver->online and cpufreq_driver->init calls, which lead to bad
> unlock.
>
> And it's well to hold this lock when calling cpufreq_driver->online, which
> provide more protects without bad influence.
>
> Fixes: f346e96267cd: ("cpufreq: Fix possible race in cpufreq online error path")
> Link: https://lore.kernel.org/all/YnKZCGaig+EXSowf@kili/
>
> Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
> Signed-off-by: Schspa Shi <schspa@gmail.com>
> ---
> drivers/cpufreq/cpufreq.c | 5 ++---
> 1 file changed, 2 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
> index 0d58b0f8f3af..43dfaa8124e2 100644
> --- a/drivers/cpufreq/cpufreq.c
> +++ b/drivers/cpufreq/cpufreq.c
> @@ -1337,12 +1337,12 @@ static int cpufreq_online(unsigned int cpu)
> down_write(&policy->rwsem);
> policy->cpu = cpu;
> policy->governor = NULL;
> - up_write(&policy->rwsem);
> } else {
> new_policy = true;
> policy = cpufreq_policy_alloc(cpu);
> if (!policy)
> return -ENOMEM;
> + down_write(&policy->rwsem);
> }
>
> if (!new_policy && cpufreq_driver->online) {
> @@ -1382,7 +1382,6 @@ static int cpufreq_online(unsigned int cpu)
> cpumask_copy(policy->related_cpus, policy->cpus);
> }
>
> - down_write(&policy->rwsem);
> /*
> * affected cpus must always be the one, which are online. We aren't
> * managing offline cpus here.
> @@ -1542,9 +1541,9 @@ static int cpufreq_online(unsigned int cpu)
> cpufreq_driver->exit(policy);
>
> cpumask_clear(policy->cpus);
> - up_write(&policy->rwsem);
>
> out_free_policy:
> + up_write(&policy->rwsem);
> cpufreq_policy_free(policy);
> return ret;
> }
Since this is a tricky piece of code, I suggest sending a fresh patch
to fix the original issue over the revert I have sent. I am not yet
sure both patches combined will fix it correctly.
--
viresh
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2022-05-09 4:09 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <20220506072146.GD4031@kadam>
2022-05-06 17:00 ` [PATCH] cpufreq: fix double unlock when cpufreq online Schspa Shi
2022-05-09 4:00 ` Viresh Kumar
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox