Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

PM / devfreq: Fix handling of min/max_freq == 0

Commit ab8f58ad72c4 ("PM / devfreq: Set min/max_freq when adding the
devfreq device") initializes df->min/max_freq with the min/max OPP when
the device is added. Later commit f1d981eaecf8 ("PM / devfreq: Use the
available min/max frequency") adds df->scaling_min/max_freq and the
following to the frequency adjustment code:

max_freq = MIN(devfreq->scaling_max_freq, devfreq->max_freq);

With the current handling of min/max_freq this is incorrect:

Even though df->max_freq is now initialized to a value != 0 user space
can still set it to 0, in this case max_freq would be 0 instead of
df->scaling_max_freq as intended. In consequence the frequency adjustment
is not performed:

if (max_freq && freq > max_freq) {
freq = max_freq;

To fix this set df->min/max freq to the min/max OPP in max/max_freq_store,
when the user passes a value of 0. This also prevents df->max_freq from
being set below the min OPP when df->min_freq is 0, and similar for
min_freq. Since it is now guaranteed that df->min/max_freq can't be 0 the
checks for this case can be removed.

Fixes: f1d981eaecf8 ("PM / devfreq: Use the available min/max frequency")
Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
Reviewed-by: Brian Norris <briannorris@chromium.org>
Reviewed-by: Chanwoo Choi <cw00.choi@samsung.com>
Signed-off-by: MyungJoo Ham <myungjoo.ham@samsung.com>

authored by

Matthias Kaehlcke and committed by
MyungJoo Ham
df5cf4a3 d0e46420

+30 -12
+30 -12
drivers/devfreq/devfreq.c
··· 324 324 max_freq = min(devfreq->scaling_max_freq, devfreq->max_freq); 325 325 min_freq = max(devfreq->scaling_min_freq, devfreq->min_freq); 326 326 327 - if (min_freq && freq < min_freq) { 327 + if (freq < min_freq) { 328 328 freq = min_freq; 329 329 flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */ 330 330 } 331 - if (max_freq && freq > max_freq) { 331 + if (freq > max_freq) { 332 332 freq = max_freq; 333 333 flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */ 334 334 } ··· 1168 1168 struct devfreq *df = to_devfreq(dev); 1169 1169 unsigned long value; 1170 1170 int ret; 1171 - unsigned long max; 1172 1171 1173 1172 ret = sscanf(buf, "%lu", &value); 1174 1173 if (ret != 1) 1175 1174 return -EINVAL; 1176 1175 1177 1176 mutex_lock(&df->lock); 1178 - max = df->max_freq; 1179 - if (value && max && value > max) { 1180 - ret = -EINVAL; 1181 - goto unlock; 1177 + 1178 + if (value) { 1179 + if (value > df->max_freq) { 1180 + ret = -EINVAL; 1181 + goto unlock; 1182 + } 1183 + } else { 1184 + unsigned long *freq_table = df->profile->freq_table; 1185 + 1186 + /* Get minimum frequency according to sorting order */ 1187 + if (freq_table[0] < freq_table[df->profile->max_state - 1]) 1188 + value = freq_table[0]; 1189 + else 1190 + value = freq_table[df->profile->max_state - 1]; 1182 1191 } 1183 1192 1184 1193 df->min_freq = value; ··· 1212 1203 struct devfreq *df = to_devfreq(dev); 1213 1204 unsigned long value; 1214 1205 int ret; 1215 - unsigned long min; 1216 1206 1217 1207 ret = sscanf(buf, "%lu", &value); 1218 1208 if (ret != 1) 1219 1209 return -EINVAL; 1220 1210 1221 1211 mutex_lock(&df->lock); 1222 - min = df->min_freq; 1223 - if (value && min && value < min) { 1224 - ret = -EINVAL; 1225 - goto unlock; 1212 + 1213 + if (value) { 1214 + if (value < df->min_freq) { 1215 + ret = -EINVAL; 1216 + goto unlock; 1217 + } 1218 + } else { 1219 + unsigned long *freq_table = df->profile->freq_table; 1220 + 1221 + /* Get maximum frequency according to sorting order */ 1222 + if (freq_table[0] < freq_table[df->profile->max_state - 1]) 1223 + value = freq_table[df->profile->max_state - 1]; 1224 + else 1225 + value = freq_table[0]; 1226 1226 } 1227 1227 1228 1228 df->max_freq = value;