Skip to content

Commit b790280

Browse files
committed
cpufreq: Fix setting policy limits when frequency tables are used
Commit 7491cdf ("cpufreq: Avoid using inconsistent policy->min and policy->max") overlooked the fact that policy->min and policy->max were accessed directly in cpufreq_frequency_table_target() and in the functions called by it. Consequently, the changes made by that commit led to problems with setting policy limits. Address this by passing the target frequency limits to __resolve_freq() and cpufreq_frequency_table_target() and propagating them to the functions called by the latter. Fixes: 7491cdf ("cpufreq: Avoid using inconsistent policy->min and policy->max") Cc: 5.16+ <stable@vger.kernel.org> # 5.16+ Closes: https://lore.kernel.org/linux-pm/aAplED3IA_J0eZN0@linaro.org/ Reported-by: Stephan Gerhold <stephan.gerhold@linaro.org> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com> Tested-by: Stephan Gerhold <stephan.gerhold@linaro.org> Reviewed-by: Lifeng Zheng <zhenglifeng1@huawei.com> Link: https://patch.msgid.link/5896780.DvuYhMxLoT@rjwysocki.net
1 parent 3d59224 commit b790280

File tree

4 files changed

+73
-41
lines changed

4 files changed

+73
-41
lines changed

drivers/cpufreq/cpufreq.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -536,14 +536,18 @@ void cpufreq_disable_fast_switch(struct cpufreq_policy *policy)
536536
EXPORT_SYMBOL_GPL(cpufreq_disable_fast_switch);
537537

538538
static unsigned int __resolve_freq(struct cpufreq_policy *policy,
539-
unsigned int target_freq, unsigned int relation)
539+
unsigned int target_freq,
540+
unsigned int min, unsigned int max,
541+
unsigned int relation)
540542
{
541543
unsigned int idx;
542544

545+
target_freq = clamp_val(target_freq, min, max);
546+
543547
if (!policy->freq_table)
544548
return target_freq;
545549

546-
idx = cpufreq_frequency_table_target(policy, target_freq, relation);
550+
idx = cpufreq_frequency_table_target(policy, target_freq, min, max, relation);
547551
policy->cached_resolved_idx = idx;
548552
policy->cached_target_freq = target_freq;
549553
return policy->freq_table[idx].frequency;
@@ -577,8 +581,7 @@ unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy,
577581
if (unlikely(min > max))
578582
min = max;
579583

580-
return __resolve_freq(policy, clamp_val(target_freq, min, max),
581-
CPUFREQ_RELATION_LE);
584+
return __resolve_freq(policy, target_freq, min, max, CPUFREQ_RELATION_LE);
582585
}
583586
EXPORT_SYMBOL_GPL(cpufreq_driver_resolve_freq);
584587

@@ -2397,8 +2400,8 @@ int __cpufreq_driver_target(struct cpufreq_policy *policy,
23972400
if (cpufreq_disabled())
23982401
return -ENODEV;
23992402

2400-
target_freq = clamp_val(target_freq, policy->min, policy->max);
2401-
target_freq = __resolve_freq(policy, target_freq, relation);
2403+
target_freq = __resolve_freq(policy, target_freq, policy->min,
2404+
policy->max, relation);
24022405

24032406
pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
24042407
policy->cpu, target_freq, relation, old_target_freq);
@@ -2727,8 +2730,11 @@ static int cpufreq_set_policy(struct cpufreq_policy *policy,
27272730
* compiler optimizations around them because they may be accessed
27282731
* concurrently by cpufreq_driver_resolve_freq() during the update.
27292732
*/
2730-
WRITE_ONCE(policy->max, __resolve_freq(policy, new_data.max, CPUFREQ_RELATION_H));
2731-
new_data.min = __resolve_freq(policy, new_data.min, CPUFREQ_RELATION_L);
2733+
WRITE_ONCE(policy->max, __resolve_freq(policy, new_data.max,
2734+
new_data.min, new_data.max,
2735+
CPUFREQ_RELATION_H));
2736+
new_data.min = __resolve_freq(policy, new_data.min, new_data.min,
2737+
new_data.max, CPUFREQ_RELATION_L);
27322738
WRITE_ONCE(policy->min, new_data.min > policy->max ? policy->max : new_data.min);
27332739

27342740
trace_cpu_frequency_limits(policy);

drivers/cpufreq/cpufreq_ondemand.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ static unsigned int generic_powersave_bias_target(struct cpufreq_policy *policy,
7676
return freq_next;
7777
}
7878

79-
index = cpufreq_frequency_table_target(policy, freq_next, relation);
79+
index = cpufreq_frequency_table_target(policy, freq_next, policy->min,
80+
policy->max, relation);
8081
freq_req = freq_table[index].frequency;
8182
freq_reduc = freq_req * od_tuners->powersave_bias / 1000;
8283
freq_avg = freq_req - freq_reduc;

drivers/cpufreq/freq_table.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ int cpufreq_generic_frequency_table_verify(struct cpufreq_policy_data *policy)
115115
EXPORT_SYMBOL_GPL(cpufreq_generic_frequency_table_verify);
116116

117117
int cpufreq_table_index_unsorted(struct cpufreq_policy *policy,
118-
unsigned int target_freq,
119-
unsigned int relation)
118+
unsigned int target_freq, unsigned int min,
119+
unsigned int max, unsigned int relation)
120120
{
121121
struct cpufreq_frequency_table optimal = {
122122
.driver_data = ~0,
@@ -147,7 +147,7 @@ int cpufreq_table_index_unsorted(struct cpufreq_policy *policy,
147147
cpufreq_for_each_valid_entry_idx(pos, table, i) {
148148
freq = pos->frequency;
149149

150-
if ((freq < policy->min) || (freq > policy->max))
150+
if (freq < min || freq > max)
151151
continue;
152152
if (freq == target_freq) {
153153
optimal.driver_data = i;

include/linux/cpufreq.h

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -776,8 +776,8 @@ int cpufreq_frequency_table_verify(struct cpufreq_policy_data *policy,
776776
int cpufreq_generic_frequency_table_verify(struct cpufreq_policy_data *policy);
777777

778778
int cpufreq_table_index_unsorted(struct cpufreq_policy *policy,
779-
unsigned int target_freq,
780-
unsigned int relation);
779+
unsigned int target_freq, unsigned int min,
780+
unsigned int max, unsigned int relation);
781781
int cpufreq_frequency_table_get_index(struct cpufreq_policy *policy,
782782
unsigned int freq);
783783

@@ -840,12 +840,12 @@ static inline int cpufreq_table_find_index_dl(struct cpufreq_policy *policy,
840840
return best;
841841
}
842842

843-
/* Works only on sorted freq-tables */
844-
static inline int cpufreq_table_find_index_l(struct cpufreq_policy *policy,
845-
unsigned int target_freq,
846-
bool efficiencies)
843+
static inline int find_index_l(struct cpufreq_policy *policy,
844+
unsigned int target_freq,
845+
unsigned int min, unsigned int max,
846+
bool efficiencies)
847847
{
848-
target_freq = clamp_val(target_freq, policy->min, policy->max);
848+
target_freq = clamp_val(target_freq, min, max);
849849

850850
if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING)
851851
return cpufreq_table_find_index_al(policy, target_freq,
@@ -855,6 +855,14 @@ static inline int cpufreq_table_find_index_l(struct cpufreq_policy *policy,
855855
efficiencies);
856856
}
857857

858+
/* Works only on sorted freq-tables */
859+
static inline int cpufreq_table_find_index_l(struct cpufreq_policy *policy,
860+
unsigned int target_freq,
861+
bool efficiencies)
862+
{
863+
return find_index_l(policy, target_freq, policy->min, policy->max, efficiencies);
864+
}
865+
858866
/* Find highest freq at or below target in a table in ascending order */
859867
static inline int cpufreq_table_find_index_ah(struct cpufreq_policy *policy,
860868
unsigned int target_freq,
@@ -908,12 +916,12 @@ static inline int cpufreq_table_find_index_dh(struct cpufreq_policy *policy,
908916
return best;
909917
}
910918

911-
/* Works only on sorted freq-tables */
912-
static inline int cpufreq_table_find_index_h(struct cpufreq_policy *policy,
913-
unsigned int target_freq,
914-
bool efficiencies)
919+
static inline int find_index_h(struct cpufreq_policy *policy,
920+
unsigned int target_freq,
921+
unsigned int min, unsigned int max,
922+
bool efficiencies)
915923
{
916-
target_freq = clamp_val(target_freq, policy->min, policy->max);
924+
target_freq = clamp_val(target_freq, min, max);
917925

918926
if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING)
919927
return cpufreq_table_find_index_ah(policy, target_freq,
@@ -923,6 +931,14 @@ static inline int cpufreq_table_find_index_h(struct cpufreq_policy *policy,
923931
efficiencies);
924932
}
925933

934+
/* Works only on sorted freq-tables */
935+
static inline int cpufreq_table_find_index_h(struct cpufreq_policy *policy,
936+
unsigned int target_freq,
937+
bool efficiencies)
938+
{
939+
return find_index_h(policy, target_freq, policy->min, policy->max, efficiencies);
940+
}
941+
926942
/* Find closest freq to target in a table in ascending order */
927943
static inline int cpufreq_table_find_index_ac(struct cpufreq_policy *policy,
928944
unsigned int target_freq,
@@ -993,12 +1009,12 @@ static inline int cpufreq_table_find_index_dc(struct cpufreq_policy *policy,
9931009
return best;
9941010
}
9951011

996-
/* Works only on sorted freq-tables */
997-
static inline int cpufreq_table_find_index_c(struct cpufreq_policy *policy,
998-
unsigned int target_freq,
999-
bool efficiencies)
1012+
static inline int find_index_c(struct cpufreq_policy *policy,
1013+
unsigned int target_freq,
1014+
unsigned int min, unsigned int max,
1015+
bool efficiencies)
10001016
{
1001-
target_freq = clamp_val(target_freq, policy->min, policy->max);
1017+
target_freq = clamp_val(target_freq, min, max);
10021018

10031019
if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING)
10041020
return cpufreq_table_find_index_ac(policy, target_freq,
@@ -1008,7 +1024,17 @@ static inline int cpufreq_table_find_index_c(struct cpufreq_policy *policy,
10081024
efficiencies);
10091025
}
10101026

1011-
static inline bool cpufreq_is_in_limits(struct cpufreq_policy *policy, int idx)
1027+
/* Works only on sorted freq-tables */
1028+
static inline int cpufreq_table_find_index_c(struct cpufreq_policy *policy,
1029+
unsigned int target_freq,
1030+
bool efficiencies)
1031+
{
1032+
return find_index_c(policy, target_freq, policy->min, policy->max, efficiencies);
1033+
}
1034+
1035+
static inline bool cpufreq_is_in_limits(struct cpufreq_policy *policy,
1036+
unsigned int min, unsigned int max,
1037+
int idx)
10121038
{
10131039
unsigned int freq;
10141040

@@ -1017,11 +1043,13 @@ static inline bool cpufreq_is_in_limits(struct cpufreq_policy *policy, int idx)
10171043

10181044
freq = policy->freq_table[idx].frequency;
10191045

1020-
return freq == clamp_val(freq, policy->min, policy->max);
1046+
return freq == clamp_val(freq, min, max);
10211047
}
10221048

10231049
static inline int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
10241050
unsigned int target_freq,
1051+
unsigned int min,
1052+
unsigned int max,
10251053
unsigned int relation)
10261054
{
10271055
bool efficiencies = policy->efficiencies_available &&
@@ -1032,29 +1060,26 @@ static inline int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
10321060
relation &= ~CPUFREQ_RELATION_E;
10331061

10341062
if (unlikely(policy->freq_table_sorted == CPUFREQ_TABLE_UNSORTED))
1035-
return cpufreq_table_index_unsorted(policy, target_freq,
1036-
relation);
1063+
return cpufreq_table_index_unsorted(policy, target_freq, min,
1064+
max, relation);
10371065
retry:
10381066
switch (relation) {
10391067
case CPUFREQ_RELATION_L:
1040-
idx = cpufreq_table_find_index_l(policy, target_freq,
1041-
efficiencies);
1068+
idx = find_index_l(policy, target_freq, min, max, efficiencies);
10421069
break;
10431070
case CPUFREQ_RELATION_H:
1044-
idx = cpufreq_table_find_index_h(policy, target_freq,
1045-
efficiencies);
1071+
idx = find_index_h(policy, target_freq, min, max, efficiencies);
10461072
break;
10471073
case CPUFREQ_RELATION_C:
1048-
idx = cpufreq_table_find_index_c(policy, target_freq,
1049-
efficiencies);
1074+
idx = find_index_c(policy, target_freq, min, max, efficiencies);
10501075
break;
10511076
default:
10521077
WARN_ON_ONCE(1);
10531078
return 0;
10541079
}
10551080

1056-
/* Limit frequency index to honor policy->min/max */
1057-
if (!cpufreq_is_in_limits(policy, idx) && efficiencies) {
1081+
/* Limit frequency index to honor min and max */
1082+
if (!cpufreq_is_in_limits(policy, min, max, idx) && efficiencies) {
10581083
efficiencies = false;
10591084
goto retry;
10601085
}

0 commit comments

Comments
 (0)