All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] cpufreq: Avoid using inactive policies
@ 2016-11-17 15:08 Rafael J. Wysocki
  2016-11-18  3:17 ` Viresh Kumar
  0 siblings, 1 reply; 4+ messages in thread
From: Rafael J. Wysocki @ 2016-11-17 15:08 UTC (permalink / raw)
  To: Linux PM list
  Cc: Linux Kernel Mailing List, Viresh Kumar, Srinivas Pandruvada

From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>

There are two places in the cpufreq core in which low-level driver
callbacks may be invoked for an inactive cpufreq policy, which isn't
guaranteed to work in general.  Both are due to possible races with
CPU offline.

First, in cpufreq_get(), the policy may become inactive after
the check against policy->cpus in cpufreq_cpu_get() and before
policy->rwsem is acquired, in which case using the policy going
forward may not be correct.

Second, an analogous situation is possible in cpufreq_update_policy().

Avoid using inactive policies by adding policy_is_inactive() checks
to the code in the above places.

Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
---
 drivers/cpufreq/cpufreq.c |    8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

Index: linux-pm/drivers/cpufreq/cpufreq.c
===================================================================
--- linux-pm.orig/drivers/cpufreq/cpufreq.c
+++ linux-pm/drivers/cpufreq/cpufreq.c
@@ -1526,7 +1526,10 @@ unsigned int cpufreq_get(unsigned int cp
 
 	if (policy) {
 		down_read(&policy->rwsem);
-		ret_freq = __cpufreq_get(policy);
+
+		if (!policy_is_inactive(policy))
+			ret_freq = __cpufreq_get(policy);
+
 		up_read(&policy->rwsem);
 
 		cpufreq_cpu_put(policy);
@@ -2265,6 +2268,9 @@ int cpufreq_update_policy(unsigned int c
 
 	down_write(&policy->rwsem);
 
+	if (policy_is_inactive(policy))
+		goto unlock;
+
 	pr_debug("updating policy for CPU %u\n", cpu);
 	memcpy(&new_policy, policy, sizeof(*policy));
 	new_policy.min = policy->user_policy.min;

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] cpufreq: Avoid using inactive policies
  2016-11-17 15:08 [PATCH] cpufreq: Avoid using inactive policies Rafael J. Wysocki
@ 2016-11-18  3:17 ` Viresh Kumar
  2016-11-18 12:20   ` Rafael J. Wysocki
  0 siblings, 1 reply; 4+ messages in thread
From: Viresh Kumar @ 2016-11-18  3:17 UTC (permalink / raw)
  To: Rafael J. Wysocki
  Cc: Linux PM list, Linux Kernel Mailing List, Srinivas Pandruvada

On 17-11-16, 16:08, Rafael J. Wysocki wrote:
> From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> 
> There are two places in the cpufreq core in which low-level driver
> callbacks may be invoked for an inactive cpufreq policy, which isn't
> guaranteed to work in general.  Both are due to possible races with
> CPU offline.
> 
> First, in cpufreq_get(), the policy may become inactive after
> the check against policy->cpus in cpufreq_cpu_get() and before
> policy->rwsem is acquired, in which case using the policy going
> forward may not be correct.
> 
> Second, an analogous situation is possible in cpufreq_update_policy().
> 
> Avoid using inactive policies by adding policy_is_inactive() checks
> to the code in the above places.
> 
> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> ---
>  drivers/cpufreq/cpufreq.c |    8 +++++++-
>  1 file changed, 7 insertions(+), 1 deletion(-)
> 
> Index: linux-pm/drivers/cpufreq/cpufreq.c
> ===================================================================
> --- linux-pm.orig/drivers/cpufreq/cpufreq.c
> +++ linux-pm/drivers/cpufreq/cpufreq.c
> @@ -1526,7 +1526,10 @@ unsigned int cpufreq_get(unsigned int cp
>  
>  	if (policy) {
>  		down_read(&policy->rwsem);
> -		ret_freq = __cpufreq_get(policy);
> +
> +		if (!policy_is_inactive(policy))
> +			ret_freq = __cpufreq_get(policy);
> +
>  		up_read(&policy->rwsem);
>  
>  		cpufreq_cpu_put(policy);
> @@ -2265,6 +2268,9 @@ int cpufreq_update_policy(unsigned int c
>  
>  	down_write(&policy->rwsem);
>  
> +	if (policy_is_inactive(policy))

You also need to set some value to 'ret' as it is uninitialized right now.

> +		goto unlock;
> +
>  	pr_debug("updating policy for CPU %u\n", cpu);
>  	memcpy(&new_policy, policy, sizeof(*policy));
>  	new_policy.min = policy->user_policy.min;

-- 
viresh

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] cpufreq: Avoid using inactive policies
  2016-11-18  3:17 ` Viresh Kumar
@ 2016-11-18 12:20   ` Rafael J. Wysocki
  2016-11-18 12:25     ` Rafael J. Wysocki
  0 siblings, 1 reply; 4+ messages in thread
From: Rafael J. Wysocki @ 2016-11-18 12:20 UTC (permalink / raw)
  To: Viresh Kumar
  Cc: Rafael J. Wysocki, Linux PM list, Linux Kernel Mailing List,
	Srinivas Pandruvada

On Fri, Nov 18, 2016 at 4:17 AM, Viresh Kumar <viresh.kumar@linaro.org> wrote:
> On 17-11-16, 16:08, Rafael J. Wysocki wrote:
>> From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
>>
>> There are two places in the cpufreq core in which low-level driver
>> callbacks may be invoked for an inactive cpufreq policy, which isn't
>> guaranteed to work in general.  Both are due to possible races with
>> CPU offline.
>>
>> First, in cpufreq_get(), the policy may become inactive after
>> the check against policy->cpus in cpufreq_cpu_get() and before
>> policy->rwsem is acquired, in which case using the policy going
>> forward may not be correct.
>>
>> Second, an analogous situation is possible in cpufreq_update_policy().
>>
>> Avoid using inactive policies by adding policy_is_inactive() checks
>> to the code in the above places.
>>
>> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
>> ---
>>  drivers/cpufreq/cpufreq.c |    8 +++++++-
>>  1 file changed, 7 insertions(+), 1 deletion(-)
>>
>> Index: linux-pm/drivers/cpufreq/cpufreq.c
>> ===================================================================
>> --- linux-pm.orig/drivers/cpufreq/cpufreq.c
>> +++ linux-pm/drivers/cpufreq/cpufreq.c
>> @@ -1526,7 +1526,10 @@ unsigned int cpufreq_get(unsigned int cp
>>
>>       if (policy) {
>>               down_read(&policy->rwsem);
>> -             ret_freq = __cpufreq_get(policy);
>> +
>> +             if (!policy_is_inactive(policy))
>> +                     ret_freq = __cpufreq_get(policy);
>> +
>>               up_read(&policy->rwsem);
>>
>>               cpufreq_cpu_put(policy);
>> @@ -2265,6 +2268,9 @@ int cpufreq_update_policy(unsigned int c
>>
>>       down_write(&policy->rwsem);
>>
>> +     if (policy_is_inactive(policy))
>
> You also need to set some value to 'ret' as it is uninitialized right now.

Right, thanks!

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] cpufreq: Avoid using inactive policies
  2016-11-18 12:20   ` Rafael J. Wysocki
@ 2016-11-18 12:25     ` Rafael J. Wysocki
  0 siblings, 0 replies; 4+ messages in thread
From: Rafael J. Wysocki @ 2016-11-18 12:25 UTC (permalink / raw)
  To: Rafael J. Wysocki
  Cc: Viresh Kumar, Rafael J. Wysocki, Linux PM list,
	Linux Kernel Mailing List, Srinivas Pandruvada

On Fri, Nov 18, 2016 at 1:20 PM, Rafael J. Wysocki <rafael@kernel.org> wrote:
> On Fri, Nov 18, 2016 at 4:17 AM, Viresh Kumar <viresh.kumar@linaro.org> wrote:
>> On 17-11-16, 16:08, Rafael J. Wysocki wrote:
>>> From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
>>>
>>> There are two places in the cpufreq core in which low-level driver
>>> callbacks may be invoked for an inactive cpufreq policy, which isn't
>>> guaranteed to work in general.  Both are due to possible races with
>>> CPU offline.
>>>
>>> First, in cpufreq_get(), the policy may become inactive after
>>> the check against policy->cpus in cpufreq_cpu_get() and before
>>> policy->rwsem is acquired, in which case using the policy going
>>> forward may not be correct.
>>>
>>> Second, an analogous situation is possible in cpufreq_update_policy().
>>>
>>> Avoid using inactive policies by adding policy_is_inactive() checks
>>> to the code in the above places.
>>>
>>> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
>>> ---
>>>  drivers/cpufreq/cpufreq.c |    8 +++++++-
>>>  1 file changed, 7 insertions(+), 1 deletion(-)
>>>
>>> Index: linux-pm/drivers/cpufreq/cpufreq.c
>>> ===================================================================
>>> --- linux-pm.orig/drivers/cpufreq/cpufreq.c
>>> +++ linux-pm/drivers/cpufreq/cpufreq.c
>>> @@ -1526,7 +1526,10 @@ unsigned int cpufreq_get(unsigned int cp
>>>
>>>       if (policy) {
>>>               down_read(&policy->rwsem);
>>> -             ret_freq = __cpufreq_get(policy);
>>> +
>>> +             if (!policy_is_inactive(policy))
>>> +                     ret_freq = __cpufreq_get(policy);
>>> +
>>>               up_read(&policy->rwsem);
>>>
>>>               cpufreq_cpu_put(policy);
>>> @@ -2265,6 +2268,9 @@ int cpufreq_update_policy(unsigned int c
>>>
>>>       down_write(&policy->rwsem);
>>>
>>> +     if (policy_is_inactive(policy))
>>
>> You also need to set some value to 'ret' as it is uninitialized right now.
>
> Right, thanks!

Which doesn't matter too much, though, because none of the callers
actually checks the return value. :-)

acpi_processor_ppc_has_changed() returns it further, but none of the
callers of that checks the value returned by it.

I guess I'll post a cleanup on top of this ...

Thanks,
Rafael

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2016-11-18 12:25 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-11-17 15:08 [PATCH] cpufreq: Avoid using inactive policies Rafael J. Wysocki
2016-11-18  3:17 ` Viresh Kumar
2016-11-18 12:20   ` Rafael J. Wysocki
2016-11-18 12:25     ` Rafael J. Wysocki

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.