[PATCH v1] cpufreq: Replace pointer subtraction with iteration macros

Zihuan Zhang posted 1 patch 2 weeks, 3 days ago
There is a newer version of this series
drivers/cpufreq/freq_table.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
[PATCH v1] cpufreq: Replace pointer subtraction with iteration macros
Posted by Zihuan Zhang 2 weeks, 3 days ago
The cpufreq documentation suggests avoiding direct pointer
subtraction when working with entries in driver_freq_table, as
it is relatively costly. Instead, the recommended approach is
to use the provided iteration macros:

  - cpufreq_for_each_valid_entry_idx()

Update freq_table.c accordingly to replace pointer difference
calculations with the proper macros. This improves code clarity
and follows the established cpufreq coding style.

No functional change intended.

Signed-off-by: Zihuan Zhang <zhangzihuan@kylinos.cn>
---
 drivers/cpufreq/freq_table.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/cpufreq/freq_table.c b/drivers/cpufreq/freq_table.c
index d5111ee56e38..ca06a0236e70 100644
--- a/drivers/cpufreq/freq_table.c
+++ b/drivers/cpufreq/freq_table.c
@@ -33,16 +33,17 @@ int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy)
 	struct cpufreq_frequency_table *pos, *table = policy->freq_table;
 	unsigned int min_freq = ~0;
 	unsigned int max_freq = 0;
+	unsigned int i = 0;
 	unsigned int freq;
 
-	cpufreq_for_each_valid_entry(pos, table) {
+	cpufreq_for_each_valid_entry_idx(pos, table, i) {
 		freq = pos->frequency;
 
 		if ((!cpufreq_boost_enabled() || !policy->boost_enabled)
 		    && (pos->flags & CPUFREQ_BOOST_FREQ))
 			continue;
 
-		pr_debug("table entry %u: %u kHz\n", (int)(pos - table), freq);
+		pr_debug("table entry %u: %u kHz\n", i, freq);
 		if (freq < min_freq)
 			min_freq = freq;
 		if (freq > max_freq)
-- 
2.25.1
Re: [PATCH v1] cpufreq: Replace pointer subtraction with iteration macros
Posted by Rafael J. Wysocki 1 week, 5 days ago
In the subject, this is just one macro, not multiple macros.

On Mon, Sep 15, 2025 at 9:03 AM Zihuan Zhang <zhangzihuan@kylinos.cn> wrote:
>
> The cpufreq documentation suggests avoiding direct pointer
> subtraction when working with entries in driver_freq_table, as
> it is relatively costly. Instead, the recommended approach is
> to use the provided iteration macros:
>
>   - cpufreq_for_each_valid_entry_idx()
>
> Update freq_table.c accordingly to replace pointer difference
> calculations with the proper macros.

And here too.

> This improves code clarity
> and follows the established cpufreq coding style.
>
> No functional change intended.
>
> Signed-off-by: Zihuan Zhang <zhangzihuan@kylinos.cn>
> ---
>  drivers/cpufreq/freq_table.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/cpufreq/freq_table.c b/drivers/cpufreq/freq_table.c
> index d5111ee56e38..ca06a0236e70 100644
> --- a/drivers/cpufreq/freq_table.c
> +++ b/drivers/cpufreq/freq_table.c
> @@ -33,16 +33,17 @@ int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy)
>         struct cpufreq_frequency_table *pos, *table = policy->freq_table;
>         unsigned int min_freq = ~0;
>         unsigned int max_freq = 0;
> +       unsigned int i = 0;

This initialization isn't necessary because
cpufreq_for_each_valid_entry_idx() will initialize i to 0 to start
with AFAICS.

>         unsigned int freq;
>
> -       cpufreq_for_each_valid_entry(pos, table) {
> +       cpufreq_for_each_valid_entry_idx(pos, table, i) {
>                 freq = pos->frequency;
>
>                 if ((!cpufreq_boost_enabled() || !policy->boost_enabled)
>                     && (pos->flags & CPUFREQ_BOOST_FREQ))
>                         continue;
>
> -               pr_debug("table entry %u: %u kHz\n", (int)(pos - table), freq);
> +               pr_debug("table entry %u: %u kHz\n", i, freq);
>                 if (freq < min_freq)
>                         min_freq = freq;
>                 if (freq > max_freq)
> --
> 2.25.1
>