[PATCH v3 2/3] cpufreq: Add a new function to get cpufreq policy without checking if the CPU is online

Lifeng Zheng posted 3 patches 2 months ago
There is a newer version of this series
[PATCH v3 2/3] cpufreq: Add a new function to get cpufreq policy without checking if the CPU is online
Posted by Lifeng Zheng 2 months ago
cpufreq_cpu_get_raw() gets cpufreq policy only if the CPU is in
policy->cpus mask, which means the CPU is already online. But in some
cases, the policy is needed before the CPU is added to cpus mask. Add a
function to get the policy in these cases.

Signed-off-by: Lifeng Zheng <zhenglifeng1@huawei.com>
---
 drivers/cpufreq/cpufreq.c | 11 +++++++++++
 include/linux/cpufreq.h   |  1 +
 2 files changed, 12 insertions(+)

diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
index fc7eace8b65b..2de76a072893 100644
--- a/drivers/cpufreq/cpufreq.c
+++ b/drivers/cpufreq/cpufreq.c
@@ -198,6 +198,17 @@ struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
 }
 EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw);
 
+struct cpufreq_policy *cpufreq_cpu_get_raw_no_check(unsigned int cpu)
+{
+	struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
+
+	if (policy)
+		return policy;
+
+	return NULL;
+}
+EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw_no_check);
+
 unsigned int cpufreq_generic_get(unsigned int cpu)
 {
 	struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
diff --git a/include/linux/cpufreq.h b/include/linux/cpufreq.h
index 95f3807c8c55..02ad8173dc10 100644
--- a/include/linux/cpufreq.h
+++ b/include/linux/cpufreq.h
@@ -205,6 +205,7 @@ struct cpufreq_freqs {
 
 #ifdef CONFIG_CPU_FREQ
 struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu);
+struct cpufreq_policy *cpufreq_cpu_get_raw_no_check(unsigned int cpu);
 struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu);
 void cpufreq_cpu_put(struct cpufreq_policy *policy);
 #else
-- 
2.33.0
Re: [PATCH v3 2/3] cpufreq: Add a new function to get cpufreq policy without checking if the CPU is online
Posted by Rafael J. Wysocki 2 months ago
On Tue, Aug 5, 2025 at 11:34 AM Lifeng Zheng <zhenglifeng1@huawei.com> wrote:
>
> cpufreq_cpu_get_raw() gets cpufreq policy only if the CPU is in
> policy->cpus mask, which means the CPU is already online. But in some
> cases, the policy is needed before the CPU is added to cpus mask. Add a
> function to get the policy in these cases.
>
> Signed-off-by: Lifeng Zheng <zhenglifeng1@huawei.com>
> ---
>  drivers/cpufreq/cpufreq.c | 11 +++++++++++
>  include/linux/cpufreq.h   |  1 +
>  2 files changed, 12 insertions(+)
>
> diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
> index fc7eace8b65b..2de76a072893 100644
> --- a/drivers/cpufreq/cpufreq.c
> +++ b/drivers/cpufreq/cpufreq.c
> @@ -198,6 +198,17 @@ struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
>  }
>  EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw);
>
> +struct cpufreq_policy *cpufreq_cpu_get_raw_no_check(unsigned int cpu)

This is not a particularly nice name.  Maybe call it cpufreq_cpu_policy()?

> +{
> +       struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
> +
> +       if (policy)
> +               return policy;
> +
> +       return NULL;

This could just be a one-liner with this statement in the function body:

  return per_cpu(cpufreq_cpu_data, cpu);

Can't it?

In which case it can be called in all places reading cpufreq_cpu_data
for a given CPU.

> +}
> +EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw_no_check);
> +
>  unsigned int cpufreq_generic_get(unsigned int cpu)
>  {
>         struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
> diff --git a/include/linux/cpufreq.h b/include/linux/cpufreq.h
> index 95f3807c8c55..02ad8173dc10 100644
> --- a/include/linux/cpufreq.h
> +++ b/include/linux/cpufreq.h
> @@ -205,6 +205,7 @@ struct cpufreq_freqs {
>
>  #ifdef CONFIG_CPU_FREQ
>  struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu);
> +struct cpufreq_policy *cpufreq_cpu_get_raw_no_check(unsigned int cpu);
>  struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu);
>  void cpufreq_cpu_put(struct cpufreq_policy *policy);
>  #else
> --
Re: [PATCH v3 2/3] cpufreq: Add a new function to get cpufreq policy without checking if the CPU is online
Posted by zhenglifeng (A) 2 months ago
On 2025/8/5 20:57, Rafael J. Wysocki wrote:

> On Tue, Aug 5, 2025 at 11:34 AM Lifeng Zheng <zhenglifeng1@huawei.com> wrote:
>>
>> cpufreq_cpu_get_raw() gets cpufreq policy only if the CPU is in
>> policy->cpus mask, which means the CPU is already online. But in some
>> cases, the policy is needed before the CPU is added to cpus mask. Add a
>> function to get the policy in these cases.
>>
>> Signed-off-by: Lifeng Zheng <zhenglifeng1@huawei.com>
>> ---
>>  drivers/cpufreq/cpufreq.c | 11 +++++++++++
>>  include/linux/cpufreq.h   |  1 +
>>  2 files changed, 12 insertions(+)
>>
>> diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
>> index fc7eace8b65b..2de76a072893 100644
>> --- a/drivers/cpufreq/cpufreq.c
>> +++ b/drivers/cpufreq/cpufreq.c
>> @@ -198,6 +198,17 @@ struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
>>  }
>>  EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw);
>>
>> +struct cpufreq_policy *cpufreq_cpu_get_raw_no_check(unsigned int cpu)
> 
> This is not a particularly nice name.  Maybe call it cpufreq_cpu_policy()?

Thanks for giving a better one.

> 
>> +{
>> +       struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
>> +
>> +       if (policy)
>> +               return policy;
>> +
>> +       return NULL;
> 
> This could just be a one-liner with this statement in the function body:
> 
>   return per_cpu(cpufreq_cpu_data, cpu);
> 
> Can't it?
> 
> In which case it can be called in all places reading cpufreq_cpu_data
> for a given CPU.

Yes, it can be done in one line. Thanks.

> 
>> +}
>> +EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw_no_check);
>> +
>>  unsigned int cpufreq_generic_get(unsigned int cpu)
>>  {
>>         struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
>> diff --git a/include/linux/cpufreq.h b/include/linux/cpufreq.h
>> index 95f3807c8c55..02ad8173dc10 100644
>> --- a/include/linux/cpufreq.h
>> +++ b/include/linux/cpufreq.h
>> @@ -205,6 +205,7 @@ struct cpufreq_freqs {
>>
>>  #ifdef CONFIG_CPU_FREQ
>>  struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu);
>> +struct cpufreq_policy *cpufreq_cpu_get_raw_no_check(unsigned int cpu);
>>  struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu);
>>  void cpufreq_cpu_put(struct cpufreq_policy *policy);
>>  #else
>> --
>