[PATCH RFC 1/2] hrtimer: support hrtimer suspend when CPU suspend
From: Wang Qing
Date: Tue Jun 22 2021 - 05:07:30 EST
When the CPU suspend, in order to achieve a more power-saving effect,
it is hoped that the CPU sleeps as long as possible, but the timer is an
important reason for the CPU to wake up.
In some cases, when the CPU suspend, the timer doesn’t have to work.
Here provides a hrtimer mechanism that supports suspend.
Signed-off-by: Wang Qing <wangqing@xxxxxxxx>
---
include/linux/hrtimer.h | 3 +++
kernel/time/hrtimer.c | 4 +++-
kernel/time/tick-sched.c | 7 +++++++
3 files changed, 13 insertions(+), 1 deletion(-)
diff --git a/include/linux/hrtimer.h b/include/linux/hrtimer.h
index bb5e7b0..bd0a4e6
--- a/include/linux/hrtimer.h
+++ b/include/linux/hrtimer.h
@@ -42,6 +42,7 @@ enum hrtimer_mode {
HRTIMER_MODE_PINNED = 0x02,
HRTIMER_MODE_SOFT = 0x04,
HRTIMER_MODE_HARD = 0x08,
+ HRTIMER_MODE_SUSPEND = 0x08,
HRTIMER_MODE_ABS_PINNED = HRTIMER_MODE_ABS | HRTIMER_MODE_PINNED,
HRTIMER_MODE_REL_PINNED = HRTIMER_MODE_REL | HRTIMER_MODE_PINNED,
@@ -112,6 +113,7 @@ enum hrtimer_restart {
* @is_soft: Set if hrtimer will be expired in soft interrupt context.
* @is_hard: Set if hrtimer will be expired in hard interrupt context
* even on RT.
+ * @is_suspend: Set if hrtimer will be suspend when CPU suspend
*
* The hrtimer structure must be initialized by hrtimer_init()
*/
@@ -124,6 +126,7 @@ struct hrtimer {
u8 is_rel;
u8 is_soft;
u8 is_hard;
+ u8 is_suspend;
};
/**
diff --git a/kernel/time/hrtimer.c b/kernel/time/hrtimer.c
index 4a66725..db34c9d
--- a/kernel/time/hrtimer.c
+++ b/kernel/time/hrtimer.c
@@ -513,7 +513,8 @@ static ktime_t __hrtimer_next_event_base(struct hrtimer_cpu_base *cpu_base,
next = timerqueue_getnext(&base->active);
timer = container_of(next, struct hrtimer, node);
- if (timer == exclude) {
+ if ((timer == exclude) ||
+ (tick_nohz_tick_inidle() && timer->is_suspend)) {
/* Get to the next timer in the queue. */
next = timerqueue_iterate_next(next);
if (!next)
@@ -1422,6 +1423,7 @@ static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
base += hrtimer_clockid_to_base(clock_id);
timer->is_soft = softtimer;
timer->is_hard = !!(mode & HRTIMER_MODE_HARD);
+ timer->is_suspend = !!(mode & HRTIMER_MODE_SUSPEND);
timer->base = &cpu_base->clock_base[base];
timerqueue_init(&timer->node);
}
diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c
index 828b091..c886758
--- a/kernel/time/tick-sched.c
+++ b/kernel/time/tick-sched.c
@@ -562,6 +562,13 @@ bool tick_nohz_tick_stopped_cpu(int cpu)
return ts->tick_stopped;
}
+bool tick_nohz_tick_inidle(void)
+{
+ struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
+
+ return ts->inidle;
+}
+
/**
* tick_nohz_update_jiffies - update jiffies when idle was interrupted
*
--
2.7.4