]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
cpuidle: governors: menu: Refine stopped tick handling
authorRafael J. Wysocki <rafael.j.wysocki@intel.com>
Mon, 23 Feb 2026 15:38:55 +0000 (16:38 +0100)
committerRafael J. Wysocki <rafael.j.wysocki@intel.com>
Thu, 5 Mar 2026 14:23:16 +0000 (15:23 +0100)
This change is based on the observation that it is not in fact necessary
to select a deep idle state every time the scheduler tick has been
stopped before the idle state selection takes place.  Namely, if the
time till the closest timer (that is not the tick) is short enough,
a shallow idle state can be selected because the timer will kick the
CPU out of that state, so the damage from a possible overly optimistic
selection will be limited.

Update the menu governor in accordance with the above and use twice
the tick period length as the "safe timer range" for allowing the
original predicted_ns value to be used even if the tick has been
stopped.

Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Reviewed-by: Christian Loehle <christian.loehle@arm.com>
Link: https://patch.msgid.link/3341782.5fSG56mABF@rafael.j.wysocki
drivers/cpuidle/governors/gov.h
drivers/cpuidle/governors/menu.c

index 99e067d9668c1bb106389d019c8867b2cd32e915..cd06a2e7b5067ac93c689c272adf49316a7c3dc1 100644 (file)
  * check the time till the closest expected timer event.
  */
 #define RESIDENCY_THRESHOLD_NS (15 * NSEC_PER_USEC)
+/*
+ * If the closest timer is in this range, the governor idle state selection need
+ * not be adjusted after the scheduler tick has been stopped.
+ */
+#define SAFE_TIMER_RANGE_NS    (2 * TICK_NSEC)
 
 #endif /* __CPUIDLE_GOVERNOR_H */
index 899ff16ff1fe2c4ba918958fceb1801a842e1546..544a5d59300778675d77475f1d88e36e199e5092 100644 (file)
@@ -261,13 +261,16 @@ static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev,
                predicted_ns = min((u64)timer_us * NSEC_PER_USEC, predicted_ns);
                /*
                 * If the tick is already stopped, the cost of possible short
-                * idle duration misprediction is much higher, because the CPU
-                * may be stuck in a shallow idle state for a long time as a
-                * result of it.  In that case, say we might mispredict and use
-                * the known time till the closest timer event for the idle
-                * state selection.
+                * idle duration misprediction is higher because the CPU may get
+                * stuck in a shallow idle state then.  To avoid that, if
+                * predicted_ns is small enough, say it might be mispredicted
+                * and use the known time till the closest timer for idle state
+                * selection unless that timer is going to trigger within
+                * SAFE_TIMER_RANGE_NS in which case it can be regarded as a
+                * sufficient safety net.
                 */
-               if (tick_nohz_tick_stopped() && predicted_ns < TICK_NSEC)
+               if (tick_nohz_tick_stopped() && predicted_ns < TICK_NSEC &&
+                   data->next_timer_ns > SAFE_TIMER_RANGE_NS)
                        predicted_ns = data->next_timer_ns;
        } else {
                /*