summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUlf Hansson <ulf.hansson@linaro.org>2019-03-27 10:35:47 -0400
committerRafael J. Wysocki <rafael.j.wysocki@intel.com>2019-04-09 18:32:34 -0400
commit6f9b83ac877fb5558d76b9f78590f3afd1bdf421 (patch)
tree905e5cf3ff9671e792bf3c473c706b413e3cce58
parenteb594b7325f61835555140922a4cb715264a325c (diff)
cpuidle: Export the next timer expiration for CPUs
To be able to predict the sleep duration for a CPU entering idle, it is essential to know the expiration time of the next timer. Both the teo and the menu cpuidle governors already use this information for CPU idle state selection. Moving forward, a similar prediction needs to be made for a group of idle CPUs rather than for a single one and the following changes implement a new genpd governor for that purpose. In order to support that feature, add a new function called tick_nohz_get_next_hrtimer() that will return the next hrtimer expiration time of a given CPU to be invoked after deciding whether or not to stop the scheduler tick on that CPU. Make the cpuidle core call tick_nohz_get_next_hrtimer() right before invoking the ->enter() callback provided by the cpuidle driver for the given state and store its return value in the per-CPU struct cpuidle_device, so as to make it available to code outside of cpuidle. Note that at the point when cpuidle calls tick_nohz_get_next_hrtimer(), the governor's ->select() callback has already returned and indicated whether or not the tick should be stopped, so in fact the value returned by tick_nohz_get_next_hrtimer() always is the next hrtimer expiration time for the given CPU, possibly including the tick (if it hasn't been stopped). Co-developed-by: Lina Iyer <lina.iyer@linaro.org> Co-developed-by: Daniel Lezcano <daniel.lezcano@linaro.org> Acked-by: Daniel Lezcano <daniel.lezcano@linaro.org> Signed-off-by: Ulf Hansson <ulf.hansson@linaro.org> [ rjw: Subject & changelog ] Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
-rw-r--r--drivers/cpuidle/cpuidle.c19
-rw-r--r--include/linux/cpuidle.h1
-rw-r--r--include/linux/tick.h7
-rw-r--r--kernel/time/tick-sched.c12
4 files changed, 36 insertions, 3 deletions
diff --git a/drivers/cpuidle/cpuidle.c b/drivers/cpuidle/cpuidle.c
index 7f108309e871..0f4b7c45df3e 100644
--- a/drivers/cpuidle/cpuidle.c
+++ b/drivers/cpuidle/cpuidle.c
@@ -328,9 +328,23 @@ int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev,
328int cpuidle_enter(struct cpuidle_driver *drv, struct cpuidle_device *dev, 328int cpuidle_enter(struct cpuidle_driver *drv, struct cpuidle_device *dev,
329 int index) 329 int index)
330{ 330{
331 int ret = 0;
332
333 /*
334 * Store the next hrtimer, which becomes either next tick or the next
335 * timer event, whatever expires first. Additionally, to make this data
336 * useful for consumers outside cpuidle, we rely on that the governor's
337 * ->select() callback have decided, whether to stop the tick or not.
338 */
339 WRITE_ONCE(dev->next_hrtimer, tick_nohz_get_next_hrtimer());
340
331 if (cpuidle_state_is_coupled(drv, index)) 341 if (cpuidle_state_is_coupled(drv, index))
332 return cpuidle_enter_state_coupled(dev, drv, index); 342 ret = cpuidle_enter_state_coupled(dev, drv, index);
333 return cpuidle_enter_state(dev, drv, index); 343 else
344 ret = cpuidle_enter_state(dev, drv, index);
345
346 WRITE_ONCE(dev->next_hrtimer, 0);
347 return ret;
334} 348}
335 349
336/** 350/**
@@ -511,6 +525,7 @@ static void __cpuidle_device_init(struct cpuidle_device *dev)
511{ 525{
512 memset(dev->states_usage, 0, sizeof(dev->states_usage)); 526 memset(dev->states_usage, 0, sizeof(dev->states_usage));
513 dev->last_residency = 0; 527 dev->last_residency = 0;
528 dev->next_hrtimer = 0;
514} 529}
515 530
516/** 531/**
diff --git a/include/linux/cpuidle.h b/include/linux/cpuidle.h
index 3b39472324a3..bb9a0db89f1a 100644
--- a/include/linux/cpuidle.h
+++ b/include/linux/cpuidle.h
@@ -83,6 +83,7 @@ struct cpuidle_device {
83 unsigned int use_deepest_state:1; 83 unsigned int use_deepest_state:1;
84 unsigned int poll_time_limit:1; 84 unsigned int poll_time_limit:1;
85 unsigned int cpu; 85 unsigned int cpu;
86 ktime_t next_hrtimer;
86 87
87 int last_residency; 88 int last_residency;
88 struct cpuidle_state_usage states_usage[CPUIDLE_STATE_MAX]; 89 struct cpuidle_state_usage states_usage[CPUIDLE_STATE_MAX];
diff --git a/include/linux/tick.h b/include/linux/tick.h
index 55388ab45fd4..8891b5ac3e40 100644
--- a/include/linux/tick.h
+++ b/include/linux/tick.h
@@ -122,6 +122,7 @@ extern void tick_nohz_idle_enter(void);
122extern void tick_nohz_idle_exit(void); 122extern void tick_nohz_idle_exit(void);
123extern void tick_nohz_irq_exit(void); 123extern void tick_nohz_irq_exit(void);
124extern bool tick_nohz_idle_got_tick(void); 124extern bool tick_nohz_idle_got_tick(void);
125extern ktime_t tick_nohz_get_next_hrtimer(void);
125extern ktime_t tick_nohz_get_sleep_length(ktime_t *delta_next); 126extern ktime_t tick_nohz_get_sleep_length(ktime_t *delta_next);
126extern unsigned long tick_nohz_get_idle_calls(void); 127extern unsigned long tick_nohz_get_idle_calls(void);
127extern unsigned long tick_nohz_get_idle_calls_cpu(int cpu); 128extern unsigned long tick_nohz_get_idle_calls_cpu(int cpu);
@@ -145,7 +146,11 @@ static inline void tick_nohz_idle_restart_tick(void) { }
145static inline void tick_nohz_idle_enter(void) { } 146static inline void tick_nohz_idle_enter(void) { }
146static inline void tick_nohz_idle_exit(void) { } 147static inline void tick_nohz_idle_exit(void) { }
147static inline bool tick_nohz_idle_got_tick(void) { return false; } 148static inline bool tick_nohz_idle_got_tick(void) { return false; }
148 149static inline ktime_t tick_nohz_get_next_hrtimer(void)
150{
151 /* Next wake up is the tick period, assume it starts now */
152 return ktime_add(ktime_get(), TICK_NSEC);
153}
149static inline ktime_t tick_nohz_get_sleep_length(ktime_t *delta_next) 154static inline ktime_t tick_nohz_get_sleep_length(ktime_t *delta_next)
150{ 155{
151 *delta_next = TICK_NSEC; 156 *delta_next = TICK_NSEC;
diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c
index 6fa52cd6df0b..8d18e03124ff 100644
--- a/kernel/time/tick-sched.c
+++ b/kernel/time/tick-sched.c
@@ -1023,6 +1023,18 @@ bool tick_nohz_idle_got_tick(void)
1023} 1023}
1024 1024
1025/** 1025/**
1026 * tick_nohz_get_next_hrtimer - return the next expiration time for the hrtimer
1027 * or the tick, whatever that expires first. Note that, if the tick has been
1028 * stopped, it returns the next hrtimer.
1029 *
1030 * Called from power state control code with interrupts disabled
1031 */
1032ktime_t tick_nohz_get_next_hrtimer(void)
1033{
1034 return __this_cpu_read(tick_cpu_device.evtdev)->next_event;
1035}
1036
1037/**
1026 * tick_nohz_get_sleep_length - return the expected length of the current sleep 1038 * tick_nohz_get_sleep_length - return the expected length of the current sleep
1027 * @delta_next: duration until the next event if the tick cannot be stopped 1039 * @delta_next: duration until the next event if the tick cannot be stopped
1028 * 1040 *