2016-03-10 15:10:48

by Richard Cochran

[permalink] [raw]
Subject: [PATCH] cpufreq: Make cpufreq_quick_get() safe to call.

The function, cpufreq_quick_get, accesses the global 'cpufreq_driver' and
its fields without taking the associated lock, cpufreq_driver_lock.

Without the locking, nothing guarantees that 'cpufreq_driver' remains
consistent during the call. This patch fixes the issue by taking the lock
before accessing the data structure.

Cc: Dirk Brandewie <[email protected]>
Cc: Rafael J. Wysocki <[email protected]>
Cc: Viresh Kumar <[email protected]>
Signed-off-by: Richard Cochran <[email protected]>
---
drivers/cpufreq/cpufreq.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
index e979ec7..ce02b2b 100644
--- a/drivers/cpufreq/cpufreq.c
+++ b/drivers/cpufreq/cpufreq.c
@@ -1457,9 +1457,17 @@ unsigned int cpufreq_quick_get(unsigned int cpu)
{
struct cpufreq_policy *policy;
unsigned int ret_freq = 0;
+ unsigned long flags;
+
+ read_lock_irqsave(&cpufreq_driver_lock, flags);

if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
- return cpufreq_driver->get(cpu);
+ ret_freq = cpufreq_driver->get(cpu);
+
+ read_unlock_irqrestore(&cpufreq_driver_lock, flags);
+
+ if (ret_freq)
+ return ret_freq;

policy = cpufreq_cpu_get(cpu);
if (policy) {
--
2.1.4


2016-03-10 22:18:56

by Rafael J. Wysocki

[permalink] [raw]
Subject: Re: [PATCH] cpufreq: Make cpufreq_quick_get() safe to call.

On Thursday, March 10, 2016 04:10:36 PM Richard Cochran wrote:
> The function, cpufreq_quick_get, accesses the global 'cpufreq_driver' and
> its fields without taking the associated lock, cpufreq_driver_lock.
>
> Without the locking, nothing guarantees that 'cpufreq_driver' remains
> consistent during the call. This patch fixes the issue by taking the lock
> before accessing the data structure.
>
> Cc: Dirk Brandewie <[email protected]>
> Cc: Rafael J. Wysocki <[email protected]>
> Cc: Viresh Kumar <[email protected]>
> Signed-off-by: Richard Cochran <[email protected]>

Can you please CC PM-related patches to [email protected]? They
are much easier to handle for me then.

> ---
> drivers/cpufreq/cpufreq.c | 10 +++++++++-
> 1 file changed, 9 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
> index e979ec7..ce02b2b 100644
> --- a/drivers/cpufreq/cpufreq.c
> +++ b/drivers/cpufreq/cpufreq.c
> @@ -1457,9 +1457,17 @@ unsigned int cpufreq_quick_get(unsigned int cpu)
> {
> struct cpufreq_policy *policy;
> unsigned int ret_freq = 0;
> + unsigned long flags;
> +
> + read_lock_irqsave(&cpufreq_driver_lock, flags);
>
> if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
> - return cpufreq_driver->get(cpu);
> + ret_freq = cpufreq_driver->get(cpu);
> +
> + read_unlock_irqrestore(&cpufreq_driver_lock, flags);
> +
> + if (ret_freq)
> + return ret_freq;
>
> policy = cpufreq_cpu_get(cpu);
> if (policy) {
>

I would prefer something like this:

read_lock_irqsave(&cpufreq_driver_lock, flags);

if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) {
unsigned int ret_freq = cpufreq_driver->get(cpu);

read_unlock_irqrestore(&cpufreq_driver_lock, flags);
return ret_freq;
}

read_unlock_irqrestore(&cpufreq_driver_lock, flags);

Thanks,
Rafael

2016-03-11 02:23:27

by Rafael J. Wysocki

[permalink] [raw]
Subject: Re: [PATCH] cpufreq: Make cpufreq_quick_get() safe to call.

On Thu, Mar 10, 2016 at 11:20 PM, Rafael J. Wysocki <[email protected]> wrote:
> On Thursday, March 10, 2016 04:10:36 PM Richard Cochran wrote:
>> The function, cpufreq_quick_get, accesses the global 'cpufreq_driver' and
>> its fields without taking the associated lock, cpufreq_driver_lock.
>>
>> Without the locking, nothing guarantees that 'cpufreq_driver' remains
>> consistent during the call. This patch fixes the issue by taking the lock
>> before accessing the data structure.
>>
>> Cc: Dirk Brandewie <[email protected]>
>> Cc: Rafael J. Wysocki <[email protected]>
>> Cc: Viresh Kumar <[email protected]>
>> Signed-off-by: Richard Cochran <[email protected]>
>
> Can you please CC PM-related patches to [email protected]? They
> are much easier to handle for me then.
>
>> ---
>> drivers/cpufreq/cpufreq.c | 10 +++++++++-
>> 1 file changed, 9 insertions(+), 1 deletion(-)
>>
>> diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
>> index e979ec7..ce02b2b 100644
>> --- a/drivers/cpufreq/cpufreq.c
>> +++ b/drivers/cpufreq/cpufreq.c
>> @@ -1457,9 +1457,17 @@ unsigned int cpufreq_quick_get(unsigned int cpu)
>> {
>> struct cpufreq_policy *policy;
>> unsigned int ret_freq = 0;
>> + unsigned long flags;
>> +
>> + read_lock_irqsave(&cpufreq_driver_lock, flags);
>>
>> if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
>> - return cpufreq_driver->get(cpu);
>> + ret_freq = cpufreq_driver->get(cpu);
>> +
>> + read_unlock_irqrestore(&cpufreq_driver_lock, flags);
>> +
>> + if (ret_freq)
>> + return ret_freq;
>>
>> policy = cpufreq_cpu_get(cpu);
>> if (policy) {
>>
>
> I would prefer something like this:
>
> read_lock_irqsave(&cpufreq_driver_lock, flags);
>
> if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) {
> unsigned int ret_freq = cpufreq_driver->get(cpu);

Sorry, ret_freq is needed outside of this block anyway, so that would be

ret_freq = cpufreq_driver->get(cpu);
>
> read_unlock_irqrestore(&cpufreq_driver_lock, flags);
> return ret_freq;
> }
>
> read_unlock_irqrestore(&cpufreq_driver_lock, flags);

2016-03-11 08:43:23

by Richard Cochran

[permalink] [raw]
Subject: [PATCH v2] cpufreq: Make cpufreq_quick_get() safe to call.

The function, cpufreq_quick_get, accesses the global 'cpufreq_driver' and
its fields without taking the associated lock, cpufreq_driver_lock.

Without the locking, nothing guarantees that 'cpufreq_driver' remains
consistent during the call. This patch fixes the issue by taking the lock
before accessing the data structure.

Cc: Dirk Brandewie <[email protected]>
Cc: Rafael J. Wysocki <[email protected]>
Cc: Viresh Kumar <[email protected]>
Cc: [email protected]
Signed-off-by: Richard Cochran <[email protected]>
---
drivers/cpufreq/cpufreq.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
index e979ec7..053aa1f 100644
--- a/drivers/cpufreq/cpufreq.c
+++ b/drivers/cpufreq/cpufreq.c
@@ -1457,9 +1457,17 @@ unsigned int cpufreq_quick_get(unsigned int cpu)
{
struct cpufreq_policy *policy;
unsigned int ret_freq = 0;
+ unsigned long flags;

- if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
- return cpufreq_driver->get(cpu);
+ read_lock_irqsave(&cpufreq_driver_lock, flags);
+
+ if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) {
+ ret_freq = cpufreq_driver->get(cpu);
+ read_unlock_irqrestore(&cpufreq_driver_lock, flags);
+ return ret_freq;
+ }
+
+ read_unlock_irqrestore(&cpufreq_driver_lock, flags);

policy = cpufreq_cpu_get(cpu);
if (policy) {
--
2.1.4

2016-03-11 23:52:42

by Viresh Kumar

[permalink] [raw]
Subject: Re: [PATCH v2] cpufreq: Make cpufreq_quick_get() safe to call.

On 11-03-16, 09:43, Richard Cochran wrote:
> The function, cpufreq_quick_get, accesses the global 'cpufreq_driver' and
> its fields without taking the associated lock, cpufreq_driver_lock.
>
> Without the locking, nothing guarantees that 'cpufreq_driver' remains
> consistent during the call. This patch fixes the issue by taking the lock
> before accessing the data structure.
>
> Cc: Dirk Brandewie <[email protected]>
> Cc: Rafael J. Wysocki <[email protected]>
> Cc: Viresh Kumar <[email protected]>
> Cc: [email protected]
> Signed-off-by: Richard Cochran <[email protected]>
> ---
> drivers/cpufreq/cpufreq.c | 12 ++++++++++--
> 1 file changed, 10 insertions(+), 2 deletions(-)

Acked-by: Viresh Kumar <[email protected]>

--
viresh

2016-03-18 23:32:48

by Rafael J. Wysocki

[permalink] [raw]
Subject: Re: [PATCH v2] cpufreq: Make cpufreq_quick_get() safe to call.

On Saturday, March 12, 2016 06:52:37 AM Viresh Kumar wrote:
> On 11-03-16, 09:43, Richard Cochran wrote:
> > The function, cpufreq_quick_get, accesses the global 'cpufreq_driver' and
> > its fields without taking the associated lock, cpufreq_driver_lock.
> >
> > Without the locking, nothing guarantees that 'cpufreq_driver' remains
> > consistent during the call. This patch fixes the issue by taking the lock
> > before accessing the data structure.
> >
> > Cc: Dirk Brandewie <[email protected]>
> > Cc: Rafael J. Wysocki <[email protected]>
> > Cc: Viresh Kumar <[email protected]>
> > Cc: [email protected]
> > Signed-off-by: Richard Cochran <[email protected]>
> > ---
> > drivers/cpufreq/cpufreq.c | 12 ++++++++++--
> > 1 file changed, 10 insertions(+), 2 deletions(-)
>
> Acked-by: Viresh Kumar <[email protected]>

Applied, thanks!