cpufreq: Limit resolving a frequency to policy min/max
authorShivnandan Kumar <quic_kshivnan@quicinc.com>
Tue, 27 Feb 2024 09:13:51 +0000 (14:43 +0530)
committerRafael J. Wysocki <rafael.j.wysocki@intel.com>
Thu, 29 Feb 2024 19:21:44 +0000 (20:21 +0100)
Resolving a frequency to an efficient one should not transgress
policy->max (which can be set for thermal reason) and policy->min.

Currently, there is possibility where scaling_cur_freq can exceed
scaling_max_freq when scaling_max_freq is an inefficient frequency.

Add a check to ensure that resolving a frequency will respect
policy->min/max.

Cc: All applicable <stable@vger.kernel.org>
Fixes: 1f39fa0dccff ("cpufreq: Introducing CPUFREQ_RELATION_E")
Signed-off-by: Shivnandan Kumar <quic_kshivnan@quicinc.com>
[ rjw: Whitespace adjustment, changelog edits ]
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
include/linux/cpufreq.h

index 85908b3a2f24947abf4c9eb8d0ad90579fc8fec6..692ea6e5512985044f034cd85c8c924fbac05030 100644 (file)
@@ -1020,6 +1020,18 @@ static inline int cpufreq_table_find_index_c(struct cpufreq_policy *policy,
                                                   efficiencies);
 }
 
+static inline bool cpufreq_is_in_limits(struct cpufreq_policy *policy, int idx)
+{
+       unsigned int freq;
+
+       if (idx < 0)
+               return false;
+
+       freq = policy->freq_table[idx].frequency;
+
+       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 relation)
@@ -1053,7 +1065,8 @@ retry:
                return 0;
        }
 
-       if (idx < 0 && efficiencies) {
+       /* Limit frequency index to honor policy->min/max */
+       if (!cpufreq_is_in_limits(policy, idx) && efficiencies) {
                efficiencies = false;
                goto retry;
        }