Skip to content

Commit 159e485

Browse files
committed
cpuidle: teo: Fix "early hits" handling for disabled idle states
The TEO governor uses idle duration "bins" defined in accordance with the CPU idle states table provided by the driver, so that each "bin" covers the idle duration range between the target residency of the idle state corresponding to it and the target residency of the closest deeper idle state. The governor collects statistics for each bin regardless of whether or not the idle state corresponding to it is currently enabled. In particular, the "early hits" metric measures the likelihood of a situation in which the idle duration measured after wakeup falls into to given bin, but the time till the next timer (sleep length) falls into a bin corresponding to one of the deeper idle states. It is used when the "hits" and "misses" metrics indicate that the state "matching" the sleep length should not be selected, so that the state with the maximum "early hits" value is selected instead of it. If the idle state corresponding to the given bin is disabled, it cannot be selected and if it turns out to be the one that should be selected, a shallower idle state needs to be used instead of it. Nevertheless, the metrics collected for the bin corresponding to it are still valid and need to be taken into account as though that state had not been disabled. As far as the "early hits" metric is concerned, teo_select() tries to take disabled states into account, but the state index corresponding to the maximum "early hits" value computed by it may be incorrect. Namely, it always uses the index of the previous maximum "early hits" state then, but there may be enabled idle states closer to the disabled one in question. In particular, if the current candidate state (whose index is the idx value) is closer to the disabled one and the "early hits" value of the disabled state is greater than the current maximum, the index of the current candidate state (idx) should replace the "maximum early hits state" index. Modify the code to handle that case correctly. Fixes: b26bf6a ("cpuidle: New timer events oriented governor for tickless systems") Reported-by: Doug Smythies <[email protected]> Signed-off-by: Rafael J. Wysocki <[email protected]> Cc: 5.1+ <[email protected]> # 5.1+
1 parent e43dcf2 commit 159e485

File tree

1 file changed

+26
-9
lines changed
  • drivers/cpuidle/governors

1 file changed

+26
-9
lines changed

drivers/cpuidle/governors/teo.c

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -277,18 +277,35 @@ static int teo_select(struct cpuidle_driver *drv, struct cpuidle_device *dev,
277277
hits = cpu_data->states[i].hits;
278278
misses = cpu_data->states[i].misses;
279279

280+
if (early_hits >= cpu_data->states[i].early_hits ||
281+
idx < 0)
282+
continue;
283+
280284
/*
281-
* If the "early hits" metric of a disabled state is
282-
* greater than the current maximum, it should be taken
283-
* into account, because it would be a mistake to select
284-
* a deeper state with lower "early hits" metric. The
285-
* index cannot be changed to point to it, however, so
286-
* just increase the "early hits" count alone and let
287-
* the index still point to a shallower idle state.
285+
* If the current candidate state has been the one with
286+
* the maximum "early hits" metric so far, the "early
287+
* hits" metric of the disabled state replaces the
288+
* current "early hits" count to avoid selecting a
289+
* deeper state with lower "early hits" metric.
288290
*/
289-
if (max_early_idx >= 0 &&
290-
early_hits < cpu_data->states[i].early_hits)
291+
if (max_early_idx == idx) {
291292
early_hits = cpu_data->states[i].early_hits;
293+
continue;
294+
}
295+
296+
/*
297+
* The current candidate state is closer to the disabled
298+
* one than the current maximum "early hits" state, so
299+
* replace the latter with it, but in case the maximum
300+
* "early hits" state index has not been set so far,
301+
* check if the current candidate state is not too
302+
* shallow for that role.
303+
*/
304+
if (!(tick_nohz_tick_stopped() &&
305+
drv->states[idx].target_residency < TICK_USEC)) {
306+
early_hits = cpu_data->states[i].early_hits;
307+
max_early_idx = idx;
308+
}
292309

293310
continue;
294311
}

0 commit comments

Comments
 (0)