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

cpuidle: menu: Avoid selecting shallow states with stopped tick

If the scheduler tick has been stopped already and the governor
selects a shallow idle state, the CPU can spend a long time in that
state if the selection is based on an inaccurate prediction of idle
time. That effect turns out to be relevant, so it needs to be
mitigated.

To that end, modify the menu governor to discard the result of the
idle time prediction if the tick is stopped and the predicted idle
time is less than the tick period length, unless the tick timer is
going to expire soon.

Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>

+22 -7
+22 -7
drivers/cpuidle/governors/menu.c
··· 352 352 */ 353 353 data->predicted_us = min(data->predicted_us, expected_interval); 354 354 355 - /* 356 - * Use the performance multiplier and the user-configurable 357 - * latency_req to determine the maximum exit latency. 358 - */ 359 - interactivity_req = data->predicted_us / performance_multiplier(nr_iowaiters, cpu_load); 360 - if (latency_req > interactivity_req) 361 - latency_req = interactivity_req; 355 + if (tick_nohz_tick_stopped()) { 356 + /* 357 + * If the tick is already stopped, the cost of possible short 358 + * idle duration misprediction is much higher, because the CPU 359 + * may be stuck in a shallow idle state for a long time as a 360 + * result of it. In that case say we might mispredict and try 361 + * to force the CPU into a state for which we would have stopped 362 + * the tick, unless a timer is going to expire really soon 363 + * anyway. 364 + */ 365 + if (data->predicted_us < TICK_USEC) 366 + data->predicted_us = min_t(unsigned int, TICK_USEC, 367 + ktime_to_us(delta_next)); 368 + } else { 369 + /* 370 + * Use the performance multiplier and the user-configurable 371 + * latency_req to determine the maximum exit latency. 372 + */ 373 + interactivity_req = data->predicted_us / performance_multiplier(nr_iowaiters, cpu_load); 374 + if (latency_req > interactivity_req) 375 + latency_req = interactivity_req; 376 + } 362 377 363 378 expected_interval = data->predicted_us; 364 379 /*