123123struct menu_device {
124124 int last_state_idx ;
125125 int needs_update ;
126+ int tick_wakeup ;
126127
127128 unsigned int next_timer_us ;
128129 unsigned int predicted_us ;
@@ -279,8 +280,10 @@ static unsigned int get_typical_interval(struct menu_device *data)
279280 * menu_select - selects the next idle state to enter
280281 * @drv: cpuidle driver containing state data
281282 * @dev: the CPU
283+ * @stop_tick: indication on whether or not to stop the tick
282284 */
283- static int menu_select (struct cpuidle_driver * drv , struct cpuidle_device * dev )
285+ static int menu_select (struct cpuidle_driver * drv , struct cpuidle_device * dev ,
286+ bool * stop_tick )
284287{
285288 struct menu_device * data = this_cpu_ptr (& menu_devices );
286289 struct device * device = get_cpu_device (dev -> cpu );
@@ -292,6 +295,7 @@ static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev)
292295 unsigned int expected_interval ;
293296 unsigned long nr_iowaiters , cpu_load ;
294297 int resume_latency = dev_pm_qos_raw_read_value (device );
298+ ktime_t delta_next ;
295299
296300 if (data -> needs_update ) {
297301 menu_update (drv , dev );
@@ -303,11 +307,13 @@ static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev)
303307 latency_req = resume_latency ;
304308
305309 /* Special case when user has set very strict latency requirement */
306- if (unlikely (latency_req == 0 ))
310+ if (unlikely (latency_req == 0 )) {
311+ * stop_tick = false;
307312 return 0 ;
313+ }
308314
309315 /* determine the expected residency time, round up */
310- data -> next_timer_us = ktime_to_us (tick_nohz_get_sleep_length ());
316+ data -> next_timer_us = ktime_to_us (tick_nohz_get_sleep_length (& delta_next ));
311317
312318 get_iowait_load (& nr_iowaiters , & cpu_load );
313319 data -> bucket = which_bucket (data -> next_timer_us , nr_iowaiters );
@@ -346,14 +352,30 @@ static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev)
346352 */
347353 data -> predicted_us = min (data -> predicted_us , expected_interval );
348354
349- /*
350- * Use the performance multiplier and the user-configurable
351- * latency_req to determine the maximum exit latency.
352- */
353- interactivity_req = data -> predicted_us / performance_multiplier (nr_iowaiters , cpu_load );
354- if (latency_req > interactivity_req )
355- 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+ }
356377
378+ expected_interval = data -> predicted_us ;
357379 /*
358380 * Find the idle state with the lowest power while satisfying
359381 * our constraints.
@@ -369,15 +391,52 @@ static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev)
369391 idx = i ; /* first enabled state */
370392 if (s -> target_residency > data -> predicted_us )
371393 break ;
372- if (s -> exit_latency > latency_req )
394+ if (s -> exit_latency > latency_req ) {
395+ /*
396+ * If we break out of the loop for latency reasons, use
397+ * the target residency of the selected state as the
398+ * expected idle duration so that the tick is retained
399+ * as long as that target residency is low enough.
400+ */
401+ expected_interval = drv -> states [idx ].target_residency ;
373402 break ;
374-
403+ }
375404 idx = i ;
376405 }
377406
378407 if (idx == -1 )
379408 idx = 0 ; /* No states enabled. Must use 0. */
380409
410+ /*
411+ * Don't stop the tick if the selected state is a polling one or if the
412+ * expected idle duration is shorter than the tick period length.
413+ */
414+ if ((drv -> states [idx ].flags & CPUIDLE_FLAG_POLLING ) ||
415+ expected_interval < TICK_USEC ) {
416+ unsigned int delta_next_us = ktime_to_us (delta_next );
417+
418+ * stop_tick = false;
419+
420+ if (!tick_nohz_tick_stopped () && idx > 0 &&
421+ drv -> states [idx ].target_residency > delta_next_us ) {
422+ /*
423+ * The tick is not going to be stopped and the target
424+ * residency of the state to be returned is not within
425+ * the time until the next timer event including the
426+ * tick, so try to correct that.
427+ */
428+ for (i = idx - 1 ; i >= 0 ; i -- ) {
429+ if (drv -> states [i ].disabled ||
430+ dev -> states_usage [i ].disable )
431+ continue ;
432+
433+ idx = i ;
434+ if (drv -> states [i ].target_residency <= delta_next_us )
435+ break ;
436+ }
437+ }
438+ }
439+
381440 data -> last_state_idx = idx ;
382441
383442 return data -> last_state_idx ;
@@ -397,6 +456,7 @@ static void menu_reflect(struct cpuidle_device *dev, int index)
397456
398457 data -> last_state_idx = index ;
399458 data -> needs_update = 1 ;
459+ data -> tick_wakeup = tick_nohz_idle_got_tick ();
400460}
401461
402462/**
@@ -427,14 +487,27 @@ static void menu_update(struct cpuidle_driver *drv, struct cpuidle_device *dev)
427487 * assume the state was never reached and the exit latency is 0.
428488 */
429489
430- /* measured value */
431- measured_us = cpuidle_get_last_residency (dev );
432-
433- /* Deduct exit latency */
434- if (measured_us > 2 * target -> exit_latency )
435- measured_us -= target -> exit_latency ;
436- else
437- measured_us /= 2 ;
490+ if (data -> tick_wakeup && data -> next_timer_us > TICK_USEC ) {
491+ /*
492+ * The nohz code said that there wouldn't be any events within
493+ * the tick boundary (if the tick was stopped), but the idle
494+ * duration predictor had a differing opinion. Since the CPU
495+ * was woken up by a tick (that wasn't stopped after all), the
496+ * predictor was not quite right, so assume that the CPU could
497+ * have been idle long (but not forever) to help the idle
498+ * duration predictor do a better job next time.
499+ */
500+ measured_us = 9 * MAX_INTERESTING / 10 ;
501+ } else {
502+ /* measured value */
503+ measured_us = cpuidle_get_last_residency (dev );
504+
505+ /* Deduct exit latency */
506+ if (measured_us > 2 * target -> exit_latency )
507+ measured_us -= target -> exit_latency ;
508+ else
509+ measured_us /= 2 ;
510+ }
438511
439512 /* Make sure our coefficients do not exceed unity */
440513 if (measured_us > data -> next_timer_us )
0 commit comments