[PATCH v2 2/3] cpuidle: teo: Avoid stopping the tick unnecessarily when bailing out

From: Rafael J. Wysocki
Date: Fri Jul 28 2023 - 06:03:35 EST


From: Rafael J. Wysocki <rafael.j.wysocki@xxxxxxxxx>

When teo_select() is going to return early in some special cases, make
it avoid stopping the tick if the idle state to be returned is shallow.

Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@xxxxxxxxx>
---
drivers/cpuidle/governors/teo.c | 50 +++++++++++++++++++++++++---------------
1 file changed, 32 insertions(+), 18 deletions(-)

Index: linux-pm/drivers/cpuidle/governors/teo.c
===================================================================
--- linux-pm.orig/drivers/cpuidle/governors/teo.c
+++ linux-pm/drivers/cpuidle/governors/teo.c
@@ -462,9 +462,9 @@ static int teo_select(struct cpuidle_dri
/* Avoid unnecessary overhead. */
if (idx < 0) {
idx = 0; /* No states enabled, must use 0. */
- goto end;
+ goto bail_out;
} else if (idx == idx0) {
- goto end;
+ goto bail_out;
}

/*
@@ -547,8 +547,10 @@ static int teo_select(struct cpuidle_dri
* If there is a latency constraint, it may be necessary to select an
* idle state shallower than the current candidate one.
*/
- if (idx > constraint_idx)
+ if (idx > constraint_idx) {
idx = constraint_idx;
+ goto bail_out;
+ }

/*
* If the CPU is being utilized over the threshold, choose a shallower
@@ -569,23 +571,35 @@ static int teo_select(struct cpuidle_dri

end:
/*
- * Don't stop the tick if the selected state is a polling one or if the
- * expected idle duration is shorter than the tick period length.
+ * Allow the tick to be stopped unless the selected state is a polling
+ * one or the expected idle duration is shorter than the tick period
+ * length.
*/
- if (((drv->states[idx].flags & CPUIDLE_FLAG_POLLING) ||
- duration_ns < TICK_NSEC) && !tick_nohz_tick_stopped()) {
- *stop_tick = false;
+ if ((!(drv->states[idx].flags & CPUIDLE_FLAG_POLLING) &&
+ duration_ns >= TICK_NSEC) || tick_nohz_tick_stopped())
+ return idx;

- /*
- * The tick is not going to be stopped, so if the target
- * residency of the state to be returned is not within the time
- * till the closest timer including the tick, try to correct
- * that.
- */
- if (idx > idx0 &&
- drv->states[idx].target_residency_ns > delta_tick)
- idx = teo_find_shallower_state(drv, dev, idx, delta_tick, false);
- }
+retain_tick:
+ *stop_tick = false;
+
+ /*
+ * The tick is not going to be stopped, so if the target residency of
+ * the state to be returned is not within the time till the closest
+ * timer including the tick, try to correct that.
+ */
+ if (idx > idx0 &&
+ drv->states[idx].target_residency_ns > delta_tick)
+ idx = teo_find_shallower_state(drv, dev, idx, delta_tick, false);
+
+ return idx;
+
+bail_out:
+ /*
+ * Do not allow the tick to be stopped if the selected state is shallow
+ * enough.
+ */
+ if (drv->states[idx].target_residency_ns < TICK_NSEC)
+ goto retain_tick;

return idx;
}