blob: 2ea4b7b23044497eac151b84a4b4042697a2bedb [file] [log] [blame]
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -05001/*
2 * Generic entry point for the idle threads
3 */
4#include <linux/sched.h>
Ingo Molnar4c822692017-02-01 16:36:40 +01005#include <linux/sched/idle.h>
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -05006#include <linux/cpu.h>
7#include <linux/cpuidle.h>
Thomas Gleixner8df3e072016-02-26 18:43:41 +00008#include <linux/cpuhotplug.h>
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -05009#include <linux/tick.h>
10#include <linux/mm.h>
11#include <linux/stackprotector.h>
Rafael J. Wysocki38106312015-02-12 23:33:15 +010012#include <linux/suspend.h>
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -060013#include <linux/livepatch.h>
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -050014
15#include <asm/tlb.h>
16
17#include <trace/events/power.h>
18
Peter Zijlstrae3baac42014-06-04 10:31:18 -070019#include "sched.h"
20
Chris Metcalf6727ad92016-10-07 17:02:55 -070021/* Linker adds these: start and end of __cpuidle functions */
22extern char __cpuidle_text_start[], __cpuidle_text_end[];
23
Rafael J. Wysockifaad3842015-05-10 01:18:03 +020024/**
25 * sched_idle_set_state - Record idle state for the current CPU.
26 * @idle_state: State to record.
27 */
28void sched_idle_set_state(struct cpuidle_state *idle_state)
29{
30 idle_set_state(this_rq(), idle_state);
31}
32
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -050033static int __read_mostly cpu_idle_force_poll;
34
35void cpu_idle_poll_ctrl(bool enable)
36{
37 if (enable) {
38 cpu_idle_force_poll++;
39 } else {
40 cpu_idle_force_poll--;
41 WARN_ON_ONCE(cpu_idle_force_poll < 0);
42 }
43}
44
45#ifdef CONFIG_GENERIC_IDLE_POLL_SETUP
46static int __init cpu_idle_poll_setup(char *__unused)
47{
48 cpu_idle_force_poll = 1;
49 return 1;
50}
51__setup("nohlt", cpu_idle_poll_setup);
52
53static int __init cpu_idle_nopoll_setup(char *__unused)
54{
55 cpu_idle_force_poll = 0;
56 return 1;
57}
58__setup("hlt", cpu_idle_nopoll_setup);
59#endif
60
Chris Metcalf6727ad92016-10-07 17:02:55 -070061static noinline int __cpuidle cpu_idle_poll(void)
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -050062{
63 rcu_idle_enter();
64 trace_cpu_idle_rcuidle(0, smp_processor_id());
65 local_irq_enable();
Daniel Bristot de Oliveira9babcd72015-10-08 15:36:06 -030066 stop_critical_timings();
Preeti U Murthyff6f2d22015-01-21 16:27:25 +053067 while (!tif_need_resched() &&
68 (cpu_idle_force_poll || tick_check_broadcast_expired()))
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -050069 cpu_relax();
Daniel Bristot de Oliveira9babcd72015-10-08 15:36:06 -030070 start_critical_timings();
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -050071 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
72 rcu_idle_exit();
73 return 1;
74}
75
76/* Weak implementations for optional arch specific functions */
77void __weak arch_cpu_idle_prepare(void) { }
78void __weak arch_cpu_idle_enter(void) { }
79void __weak arch_cpu_idle_exit(void) { }
80void __weak arch_cpu_idle_dead(void) { }
81void __weak arch_cpu_idle(void)
82{
83 cpu_idle_force_poll = 1;
84 local_irq_enable();
85}
86
Rafael J. Wysocki827a5ae2015-05-10 01:18:46 +020087/**
88 * default_idle_call - Default CPU idle routine.
89 *
90 * To use when the cpuidle framework cannot be used.
91 */
Chris Metcalf6727ad92016-10-07 17:02:55 -070092void __cpuidle default_idle_call(void)
Rafael J. Wysocki82f66322015-05-04 22:53:22 +020093{
Lucas Stach63caae82015-07-20 18:34:50 +020094 if (current_clr_polling_and_test()) {
Rafael J. Wysocki82f66322015-05-04 22:53:22 +020095 local_irq_enable();
Lucas Stach63caae82015-07-20 18:34:50 +020096 } else {
97 stop_critical_timings();
Rafael J. Wysocki82f66322015-05-04 22:53:22 +020098 arch_cpu_idle();
Lucas Stach63caae82015-07-20 18:34:50 +020099 start_critical_timings();
100 }
Rafael J. Wysocki82f66322015-05-04 22:53:22 +0200101}
102
Rafael J. Wysockibcf6ad82015-05-04 22:53:35 +0200103static int call_cpuidle(struct cpuidle_driver *drv, struct cpuidle_device *dev,
104 int next_state)
105{
Rafael J. Wysockibcf6ad82015-05-04 22:53:35 +0200106 /*
107 * The idle task must be scheduled, it is pointless to go to idle, just
108 * update no idle residency and return.
109 */
110 if (current_clr_polling_and_test()) {
111 dev->last_residency = 0;
112 local_irq_enable();
113 return -EBUSY;
114 }
115
Rafael J. Wysockibcf6ad82015-05-04 22:53:35 +0200116 /*
117 * Enter the idle state previously returned by the governor decision.
118 * This function will block until an interrupt occurs and will take
119 * care of re-enabling the local interrupts
120 */
Rafael J. Wysocki827a5ae2015-05-10 01:18:46 +0200121 return cpuidle_enter(drv, dev, next_state);
Rafael J. Wysockibcf6ad82015-05-04 22:53:35 +0200122}
123
Daniel Lezcano30cdd692014-03-03 08:48:51 +0100124/**
125 * cpuidle_idle_call - the main idle function
126 *
127 * NOTE: no locks or semaphores should be used here
Andy Lutomirski82c65d62014-06-04 10:31:16 -0700128 *
129 * On archs that support TIF_POLLING_NRFLAG, is called with polling
130 * set, and it returns with polling set. If it ever stops polling, it
131 * must clear the polling bit.
Daniel Lezcano30cdd692014-03-03 08:48:51 +0100132 */
Rafael J. Wysocki08c373e2014-04-21 01:26:58 +0200133static void cpuidle_idle_call(void)
Daniel Lezcano30cdd692014-03-03 08:48:51 +0100134{
Catalin Marinas9bd616e2016-06-01 18:52:16 +0100135 struct cpuidle_device *dev = cpuidle_get_device();
Daniel Lezcano30cdd692014-03-03 08:48:51 +0100136 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
Peter Zijlstra37352272014-04-11 13:55:48 +0200137 int next_state, entered_state;
Daniel Lezcano30cdd692014-03-03 08:48:51 +0100138
Daniel Lezcanoa1d028b2014-03-03 08:48:54 +0100139 /*
140 * Check if the idle task must be rescheduled. If it is the
Peter Zijlstrac4441172014-04-11 13:47:16 +0200141 * case, exit the function after re-enabling the local irq.
Daniel Lezcanoa1d028b2014-03-03 08:48:54 +0100142 */
Peter Zijlstrac4441172014-04-11 13:47:16 +0200143 if (need_resched()) {
Daniel Lezcano8ca3c642014-03-03 08:48:53 +0100144 local_irq_enable();
Rafael J. Wysocki08c373e2014-04-21 01:26:58 +0200145 return;
Daniel Lezcano8ca3c642014-03-03 08:48:53 +0100146 }
147
Daniel Lezcanoa1d028b2014-03-03 08:48:54 +0100148 /*
Daniel Lezcanoa1d028b2014-03-03 08:48:54 +0100149 * Tell the RCU framework we are entering an idle section,
150 * so no more rcu read side critical sections and one more
151 * step to the grace period
152 */
Daniel Lezcanoc8cc7d42014-03-03 08:48:52 +0100153 rcu_idle_enter();
154
Rafael J. Wysocki82f66322015-05-04 22:53:22 +0200155 if (cpuidle_not_available(drv, dev)) {
156 default_idle_call();
157 goto exit_idle;
158 }
Rafael J. Wysockief2b22a2015-03-02 22:26:55 +0100159
Daniel Lezcanoa1d028b2014-03-03 08:48:54 +0100160 /*
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +0200161 * Suspend-to-idle ("s2idle") is a system state in which all user space
Rafael J. Wysocki38106312015-02-12 23:33:15 +0100162 * has been frozen, all I/O devices have been suspended and the only
163 * activity happens here and in iterrupts (if any). In that case bypass
164 * the cpuidle governor and go stratight for the deepest idle state
165 * available. Possibly also suspend the local tick and the entire
166 * timekeeping to prevent timer interrupts from kicking us out of idle
167 * until a proper wakeup interrupt happens.
168 */
Jacob Panbb8313b2016-11-28 23:03:04 -0800169
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +0200170 if (idle_should_enter_s2idle() || dev->use_deepest_state) {
171 if (idle_should_enter_s2idle()) {
Jacob Panbb8313b2016-11-28 23:03:04 -0800172 entered_state = cpuidle_enter_freeze(drv, dev);
173 if (entered_state > 0) {
174 local_irq_enable();
175 goto exit_idle;
176 }
Rafael J. Wysockief2b22a2015-03-02 22:26:55 +0100177 }
Rafael J. Wysocki38106312015-02-12 23:33:15 +0100178
Rafael J. Wysockief2b22a2015-03-02 22:26:55 +0100179 next_state = cpuidle_find_deepest_state(drv, dev);
Rafael J. Wysockibcf6ad82015-05-04 22:53:35 +0200180 call_cpuidle(drv, dev, next_state);
Rafael J. Wysockief2b22a2015-03-02 22:26:55 +0100181 } else {
Rafael J. Wysockief2b22a2015-03-02 22:26:55 +0100182 /*
183 * Ask the cpuidle framework to choose a convenient idle state.
184 */
185 next_state = cpuidle_select(drv, dev);
Rafael J. Wysockibcf6ad82015-05-04 22:53:35 +0200186 entered_state = call_cpuidle(drv, dev, next_state);
187 /*
188 * Give the governor an opportunity to reflect on the outcome
189 */
Rafael J. Wysockief2b22a2015-03-02 22:26:55 +0100190 cpuidle_reflect(dev, entered_state);
Rafael J. Wysockibcf6ad82015-05-04 22:53:35 +0200191 }
Peter Zijlstra37352272014-04-11 13:55:48 +0200192
193exit_idle:
Daniel Lezcano8ca3c642014-03-03 08:48:53 +0100194 __current_set_polling();
Daniel Lezcano30cdd692014-03-03 08:48:51 +0100195
Daniel Lezcanoa1d028b2014-03-03 08:48:54 +0100196 /*
Peter Zijlstra37352272014-04-11 13:55:48 +0200197 * It is up to the idle functions to reenable local interrupts
Daniel Lezcanoa1d028b2014-03-03 08:48:54 +0100198 */
Daniel Lezcanoc8cc7d42014-03-03 08:48:52 +0100199 if (WARN_ON_ONCE(irqs_disabled()))
200 local_irq_enable();
201
202 rcu_idle_exit();
Daniel Lezcano30cdd692014-03-03 08:48:51 +0100203}
Daniel Lezcano30cdd692014-03-03 08:48:51 +0100204
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500205/*
206 * Generic idle loop implementation
Andy Lutomirski82c65d62014-06-04 10:31:16 -0700207 *
208 * Called with polling cleared.
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500209 */
Peter Zijlstrac1de45c2016-11-28 23:03:05 -0800210static void do_idle(void)
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500211{
Peter Zijlstrac1de45c2016-11-28 23:03:05 -0800212 /*
213 * If the arch has a polling bit, we maintain an invariant:
214 *
215 * Our polling bit is clear if we're not scheduled (i.e. if rq->curr !=
216 * rq->idle). This means that, if rq->idle has the polling bit set,
217 * then setting need_resched is guaranteed to cause the CPU to
218 * reschedule.
219 */
Gaurav Jindal (Gaurav Jindal)df55f462016-05-12 10:13:33 +0000220
Peter Zijlstrac1de45c2016-11-28 23:03:05 -0800221 __current_set_polling();
Aubrey Liebfa4c02017-06-07 10:49:02 +0800222 quiet_vmstat();
Peter Zijlstrac1de45c2016-11-28 23:03:05 -0800223 tick_nohz_idle_enter();
Andy Lutomirski82c65d62014-06-04 10:31:16 -0700224
Peter Zijlstrac1de45c2016-11-28 23:03:05 -0800225 while (!need_resched()) {
226 check_pgt_cache();
227 rmb();
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500228
Peter Zijlstrac1de45c2016-11-28 23:03:05 -0800229 if (cpu_is_offline(smp_processor_id())) {
230 cpuhp_report_idle_dead();
231 arch_cpu_idle_dead();
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500232 }
Peter Zijlstra06d50c62014-02-24 18:22:07 +0100233
Peter Zijlstrac1de45c2016-11-28 23:03:05 -0800234 local_irq_disable();
235 arch_cpu_idle_enter();
Andy Lutomirski82c65d62014-06-04 10:31:16 -0700236
237 /*
Peter Zijlstrac1de45c2016-11-28 23:03:05 -0800238 * In poll mode we reenable interrupts and spin. Also if we
239 * detected in the wakeup from idle path that the tick
240 * broadcast device expired for us, we don't want to go deep
241 * idle as we know that the IPI is going to arrive right away.
Andy Lutomirski82c65d62014-06-04 10:31:16 -0700242 */
Peter Zijlstrac1de45c2016-11-28 23:03:05 -0800243 if (cpu_idle_force_poll || tick_check_broadcast_expired())
244 cpu_idle_poll();
245 else
246 cpuidle_idle_call();
247 arch_cpu_idle_exit();
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500248 }
Peter Zijlstrac1de45c2016-11-28 23:03:05 -0800249
250 /*
251 * Since we fell out of the loop above, we know TIF_NEED_RESCHED must
252 * be set, propagate it into PREEMPT_NEED_RESCHED.
253 *
254 * This is required because for polling idle loops we will not have had
255 * an IPI to fold the state for us.
256 */
257 preempt_set_need_resched();
258 tick_nohz_idle_exit();
259 __current_clr_polling();
260
261 /*
262 * We promise to call sched_ttwu_pending() and reschedule if
263 * need_resched() is set while polling is set. That means that clearing
264 * polling needs to be visible before doing these things.
265 */
266 smp_mb__after_atomic();
267
268 sched_ttwu_pending();
Steven Rostedt (VMware)8663eff2017-04-14 08:48:09 -0400269 schedule_idle();
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -0600270
271 if (unlikely(klp_patch_pending(current)))
272 klp_update_patch_state(current);
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500273}
274
Chris Metcalf6727ad92016-10-07 17:02:55 -0700275bool cpu_in_idle(unsigned long pc)
276{
277 return pc >= (unsigned long)__cpuidle_text_start &&
278 pc < (unsigned long)__cpuidle_text_end;
279}
280
Peter Zijlstrac1de45c2016-11-28 23:03:05 -0800281struct idle_timer {
282 struct hrtimer timer;
283 int done;
284};
285
286static enum hrtimer_restart idle_inject_timer_fn(struct hrtimer *timer)
287{
288 struct idle_timer *it = container_of(timer, struct idle_timer, timer);
289
290 WRITE_ONCE(it->done, 1);
291 set_tsk_need_resched(current);
292
293 return HRTIMER_NORESTART;
294}
295
296void play_idle(unsigned long duration_ms)
297{
298 struct idle_timer it;
299
300 /*
301 * Only FIFO tasks can disable the tick since they don't need the forced
302 * preemption.
303 */
304 WARN_ON_ONCE(current->policy != SCHED_FIFO);
305 WARN_ON_ONCE(current->nr_cpus_allowed != 1);
306 WARN_ON_ONCE(!(current->flags & PF_KTHREAD));
307 WARN_ON_ONCE(!(current->flags & PF_NO_SETAFFINITY));
308 WARN_ON_ONCE(!duration_ms);
309
310 rcu_sleep_check();
311 preempt_disable();
312 current->flags |= PF_IDLE;
313 cpuidle_use_deepest_state(true);
314
315 it.done = 0;
316 hrtimer_init_on_stack(&it.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
317 it.timer.function = idle_inject_timer_fn;
318 hrtimer_start(&it.timer, ms_to_ktime(duration_ms), HRTIMER_MODE_REL_PINNED);
319
320 while (!READ_ONCE(it.done))
321 do_idle();
322
323 cpuidle_use_deepest_state(false);
324 current->flags &= ~PF_IDLE;
325
326 preempt_fold_need_resched();
327 preempt_enable();
328}
329EXPORT_SYMBOL_GPL(play_idle);
330
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500331void cpu_startup_entry(enum cpuhp_state state)
332{
333 /*
334 * This #ifdef needs to die, but it's too late in the cycle to
335 * make this generic (arm and sh have never invoked the canary
336 * init for the non boot cpus!). Will be fixed in 3.11
337 */
338#ifdef CONFIG_X86
339 /*
340 * If we're the non-boot CPU, nothing set the stack canary up
341 * for us. The boot CPU already has it initialized but no harm
342 * in doing it again. This is a good place for updating it, as
343 * we wont ever return from this function (so the invalid
344 * canaries already on the stack wont ever trigger).
345 */
346 boot_init_stack_canary();
347#endif
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500348 arch_cpu_idle_prepare();
Thomas Gleixner8df3e072016-02-26 18:43:41 +0000349 cpuhp_online_idle(state);
Peter Zijlstrac1de45c2016-11-28 23:03:05 -0800350 while (1)
351 do_idle();
Nicolas Pitrecf37b6b2014-01-26 23:42:01 -0500352}