From 9840922a8702a5d4c5091569a100abaf1053b092 Mon Sep 17 00:00:00 2001 From: Greg Kroah-Hartman Date: Sun, 11 May 2025 05:36:04 +0000 Subject: [PATCH] Revert "cpufreq: Fix setting policy limits when frequency tables are used" This reverts commit 15888cd413f8ffdba42396a764c33eff4ef2ec3c which is commit b79028039f440e7d2c4df6ab243060c4e3803e84 upstream. It breaks the Android kernel abi and can be brought back in the future in an abi-safe way if it is really needed. Bug: 161946584 Change-Id: I82289f4148265e8e3264bf9b372b638a59a70d47 Signed-off-by: Greg Kroah-Hartman --- drivers/cpufreq/cpufreq.c | 21 +++----- drivers/cpufreq/cpufreq_ondemand.c | 3 +- drivers/cpufreq/freq_table.c | 6 +-- include/linux/cpufreq.h | 83 +++++++++++------------------- 4 files changed, 41 insertions(+), 72 deletions(-) diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c index 6f2c0be3ec0f..b6ef1e7ea00b 100644 --- a/drivers/cpufreq/cpufreq.c +++ b/drivers/cpufreq/cpufreq.c @@ -533,20 +533,17 @@ void cpufreq_disable_fast_switch(struct cpufreq_policy *policy) EXPORT_SYMBOL_GPL(cpufreq_disable_fast_switch); static unsigned int __resolve_freq(struct cpufreq_policy *policy, - unsigned int target_freq, - unsigned int min, unsigned int max, - unsigned int relation) + unsigned int target_freq, unsigned int relation) { unsigned int idx; unsigned int old_target_freq = target_freq; - target_freq = clamp_val(target_freq, min, max); trace_android_vh_cpufreq_resolve_freq(policy, &target_freq, old_target_freq); if (!policy->freq_table) return target_freq; - idx = cpufreq_frequency_table_target(policy, target_freq, min, max, relation); + idx = cpufreq_frequency_table_target(policy, target_freq, relation); policy->cached_resolved_idx = idx; policy->cached_target_freq = target_freq; return policy->freq_table[idx].frequency; @@ -580,7 +577,8 @@ unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy, if (unlikely(min > max)) min = max; - return __resolve_freq(policy, target_freq, min, max, CPUFREQ_RELATION_LE); + return __resolve_freq(policy, clamp_val(target_freq, min, max), + CPUFREQ_RELATION_LE); } EXPORT_SYMBOL_GPL(cpufreq_driver_resolve_freq); @@ -2322,8 +2320,8 @@ int __cpufreq_driver_target(struct cpufreq_policy *policy, if (cpufreq_disabled()) return -ENODEV; - target_freq = __resolve_freq(policy, target_freq, policy->min, - policy->max, relation); + target_freq = clamp_val(target_freq, policy->min, policy->max); + target_freq = __resolve_freq(policy, target_freq, relation); trace_android_vh_cpufreq_target(policy, &target_freq, old_target_freq); @@ -2620,11 +2618,8 @@ static int cpufreq_set_policy(struct cpufreq_policy *policy, * compiler optimizations around them because they may be accessed * concurrently by cpufreq_driver_resolve_freq() during the update. */ - WRITE_ONCE(policy->max, __resolve_freq(policy, new_data.max, - new_data.min, new_data.max, - CPUFREQ_RELATION_H)); - new_data.min = __resolve_freq(policy, new_data.min, new_data.min, - new_data.max, CPUFREQ_RELATION_L); + WRITE_ONCE(policy->max, __resolve_freq(policy, new_data.max, CPUFREQ_RELATION_H)); + new_data.min = __resolve_freq(policy, new_data.min, CPUFREQ_RELATION_L); WRITE_ONCE(policy->min, new_data.min > policy->max ? policy->max : new_data.min); trace_cpu_frequency_limits(policy); diff --git a/drivers/cpufreq/cpufreq_ondemand.c b/drivers/cpufreq/cpufreq_ondemand.c index 65cbd5ecbaf8..c52d19d67557 100644 --- a/drivers/cpufreq/cpufreq_ondemand.c +++ b/drivers/cpufreq/cpufreq_ondemand.c @@ -77,8 +77,7 @@ static unsigned int generic_powersave_bias_target(struct cpufreq_policy *policy, return freq_next; } - index = cpufreq_frequency_table_target(policy, freq_next, policy->min, - policy->max, relation); + index = cpufreq_frequency_table_target(policy, freq_next, relation); freq_req = freq_table[index].frequency; freq_reduc = freq_req * od_tuners->powersave_bias / 1000; freq_avg = freq_req - freq_reduc; diff --git a/drivers/cpufreq/freq_table.c b/drivers/cpufreq/freq_table.c index 5fbc16e144e6..24d3115c8350 100644 --- a/drivers/cpufreq/freq_table.c +++ b/drivers/cpufreq/freq_table.c @@ -118,8 +118,8 @@ int cpufreq_generic_frequency_table_verify(struct cpufreq_policy_data *policy) EXPORT_SYMBOL_GPL(cpufreq_generic_frequency_table_verify); int cpufreq_table_index_unsorted(struct cpufreq_policy *policy, - unsigned int target_freq, unsigned int min, - unsigned int max, unsigned int relation) + unsigned int target_freq, + unsigned int relation) { struct cpufreq_frequency_table optimal = { .driver_data = ~0, @@ -150,7 +150,7 @@ int cpufreq_table_index_unsorted(struct cpufreq_policy *policy, cpufreq_for_each_valid_entry_idx(pos, table, i) { freq = pos->frequency; - if (freq < min || freq > max) + if ((freq < policy->min) || (freq > policy->max)) continue; if (freq == target_freq) { optimal.driver_data = i; diff --git a/include/linux/cpufreq.h b/include/linux/cpufreq.h index 136325bff069..14d307b2a8e3 100644 --- a/include/linux/cpufreq.h +++ b/include/linux/cpufreq.h @@ -789,8 +789,8 @@ int cpufreq_frequency_table_verify(struct cpufreq_policy_data *policy, int cpufreq_generic_frequency_table_verify(struct cpufreq_policy_data *policy); int cpufreq_table_index_unsorted(struct cpufreq_policy *policy, - unsigned int target_freq, unsigned int min, - unsigned int max, unsigned int relation); + unsigned int target_freq, + unsigned int relation); int cpufreq_frequency_table_get_index(struct cpufreq_policy *policy, unsigned int freq); @@ -855,12 +855,12 @@ static inline int cpufreq_table_find_index_dl(struct cpufreq_policy *policy, return best; } -static inline int find_index_l(struct cpufreq_policy *policy, - unsigned int target_freq, - unsigned int min, unsigned int max, - bool efficiencies) +/* Works only on sorted freq-tables */ +static inline int cpufreq_table_find_index_l(struct cpufreq_policy *policy, + unsigned int target_freq, + bool efficiencies) { - target_freq = clamp_val(target_freq, min, max); + target_freq = clamp_val(target_freq, policy->min, policy->max); if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING) return cpufreq_table_find_index_al(policy, target_freq, @@ -870,14 +870,6 @@ static inline int find_index_l(struct cpufreq_policy *policy, efficiencies); } -/* Works only on sorted freq-tables */ -static inline int cpufreq_table_find_index_l(struct cpufreq_policy *policy, - unsigned int target_freq, - bool efficiencies) -{ - return find_index_l(policy, target_freq, policy->min, policy->max, efficiencies); -} - /* Find highest freq at or below target in a table in ascending order */ static inline int cpufreq_table_find_index_ah(struct cpufreq_policy *policy, unsigned int target_freq, @@ -931,12 +923,12 @@ static inline int cpufreq_table_find_index_dh(struct cpufreq_policy *policy, return best; } -static inline int find_index_h(struct cpufreq_policy *policy, - unsigned int target_freq, - unsigned int min, unsigned int max, - bool efficiencies) +/* Works only on sorted freq-tables */ +static inline int cpufreq_table_find_index_h(struct cpufreq_policy *policy, + unsigned int target_freq, + bool efficiencies) { - target_freq = clamp_val(target_freq, min, max); + target_freq = clamp_val(target_freq, policy->min, policy->max); if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING) return cpufreq_table_find_index_ah(policy, target_freq, @@ -946,14 +938,6 @@ static inline int find_index_h(struct cpufreq_policy *policy, efficiencies); } -/* Works only on sorted freq-tables */ -static inline int cpufreq_table_find_index_h(struct cpufreq_policy *policy, - unsigned int target_freq, - bool efficiencies) -{ - return find_index_h(policy, target_freq, policy->min, policy->max, efficiencies); -} - /* Find closest freq to target in a table in ascending order */ static inline int cpufreq_table_find_index_ac(struct cpufreq_policy *policy, unsigned int target_freq, @@ -1024,12 +1008,12 @@ static inline int cpufreq_table_find_index_dc(struct cpufreq_policy *policy, return best; } -static inline int find_index_c(struct cpufreq_policy *policy, - unsigned int target_freq, - unsigned int min, unsigned int max, - bool efficiencies) +/* Works only on sorted freq-tables */ +static inline int cpufreq_table_find_index_c(struct cpufreq_policy *policy, + unsigned int target_freq, + bool efficiencies) { - target_freq = clamp_val(target_freq, min, max); + target_freq = clamp_val(target_freq, policy->min, policy->max); if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING) return cpufreq_table_find_index_ac(policy, target_freq, @@ -1039,17 +1023,7 @@ static inline int find_index_c(struct cpufreq_policy *policy, efficiencies); } -/* Works only on sorted freq-tables */ -static inline int cpufreq_table_find_index_c(struct cpufreq_policy *policy, - unsigned int target_freq, - bool efficiencies) -{ - return find_index_c(policy, target_freq, policy->min, policy->max, efficiencies); -} - -static inline bool cpufreq_is_in_limits(struct cpufreq_policy *policy, - unsigned int min, unsigned int max, - int idx) +static inline bool cpufreq_is_in_limits(struct cpufreq_policy *policy, int idx) { unsigned int freq; @@ -1058,13 +1032,11 @@ static inline bool cpufreq_is_in_limits(struct cpufreq_policy *policy, freq = policy->freq_table[idx].frequency; - return freq == clamp_val(freq, min, max); + return freq == clamp_val(freq, policy->min, policy->max); } static inline int cpufreq_frequency_table_target(struct cpufreq_policy *policy, unsigned int target_freq, - unsigned int min, - unsigned int max, unsigned int relation) { bool efficiencies = policy->efficiencies_available && @@ -1075,26 +1047,29 @@ static inline int cpufreq_frequency_table_target(struct cpufreq_policy *policy, relation &= ~CPUFREQ_RELATION_E; if (unlikely(policy->freq_table_sorted == CPUFREQ_TABLE_UNSORTED)) - return cpufreq_table_index_unsorted(policy, target_freq, min, - max, relation); + return cpufreq_table_index_unsorted(policy, target_freq, + relation); retry: switch (relation) { case CPUFREQ_RELATION_L: - idx = find_index_l(policy, target_freq, min, max, efficiencies); + idx = cpufreq_table_find_index_l(policy, target_freq, + efficiencies); break; case CPUFREQ_RELATION_H: - idx = find_index_h(policy, target_freq, min, max, efficiencies); + idx = cpufreq_table_find_index_h(policy, target_freq, + efficiencies); break; case CPUFREQ_RELATION_C: - idx = find_index_c(policy, target_freq, min, max, efficiencies); + idx = cpufreq_table_find_index_c(policy, target_freq, + efficiencies); break; default: WARN_ON_ONCE(1); return 0; } - /* Limit frequency index to honor min and max */ - if (!cpufreq_is_in_limits(policy, min, max, idx) && efficiencies) { + /* Limit frequency index to honor policy->min/max */ + if (!cpufreq_is_in_limits(policy, idx) && efficiencies) { efficiencies = false; goto retry; }