Merge tag 'nfs-for-3.20-1' of git://git.linux-nfs.org/projects/trondmy/linux-nfs
[linux-2.6-block.git] / kernel / sched / idle.c
CommitLineData
cf37b6b4
NP
1/*
2 * Generic entry point for the idle threads
3 */
4#include <linux/sched.h>
5#include <linux/cpu.h>
6#include <linux/cpuidle.h>
7#include <linux/tick.h>
8#include <linux/mm.h>
9#include <linux/stackprotector.h>
10
11#include <asm/tlb.h>
12
13#include <trace/events/power.h>
14
e3baac47
PZ
15#include "sched.h"
16
cf37b6b4
NP
17static int __read_mostly cpu_idle_force_poll;
18
19void cpu_idle_poll_ctrl(bool enable)
20{
21 if (enable) {
22 cpu_idle_force_poll++;
23 } else {
24 cpu_idle_force_poll--;
25 WARN_ON_ONCE(cpu_idle_force_poll < 0);
26 }
27}
28
29#ifdef CONFIG_GENERIC_IDLE_POLL_SETUP
30static int __init cpu_idle_poll_setup(char *__unused)
31{
32 cpu_idle_force_poll = 1;
33 return 1;
34}
35__setup("nohlt", cpu_idle_poll_setup);
36
37static int __init cpu_idle_nopoll_setup(char *__unused)
38{
39 cpu_idle_force_poll = 0;
40 return 1;
41}
42__setup("hlt", cpu_idle_nopoll_setup);
43#endif
44
45static inline int cpu_idle_poll(void)
46{
47 rcu_idle_enter();
48 trace_cpu_idle_rcuidle(0, smp_processor_id());
49 local_irq_enable();
ff6f2d29
PM
50 while (!tif_need_resched() &&
51 (cpu_idle_force_poll || tick_check_broadcast_expired()))
cf37b6b4
NP
52 cpu_relax();
53 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
54 rcu_idle_exit();
55 return 1;
56}
57
58/* Weak implementations for optional arch specific functions */
59void __weak arch_cpu_idle_prepare(void) { }
60void __weak arch_cpu_idle_enter(void) { }
61void __weak arch_cpu_idle_exit(void) { }
62void __weak arch_cpu_idle_dead(void) { }
63void __weak arch_cpu_idle(void)
64{
65 cpu_idle_force_poll = 1;
66 local_irq_enable();
67}
68
30cdd69e
DL
69/**
70 * cpuidle_idle_call - the main idle function
71 *
72 * NOTE: no locks or semaphores should be used here
82c65d60
AL
73 *
74 * On archs that support TIF_POLLING_NRFLAG, is called with polling
75 * set, and it returns with polling set. If it ever stops polling, it
76 * must clear the polling bit.
30cdd69e 77 */
08c373e5 78static void cpuidle_idle_call(void)
30cdd69e
DL
79{
80 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
81 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
37352273 82 int next_state, entered_state;
89abb5ad 83 unsigned int broadcast;
30cdd69e 84
a1d028bd
DL
85 /*
86 * Check if the idle task must be rescheduled. If it is the
c444117f 87 * case, exit the function after re-enabling the local irq.
a1d028bd 88 */
c444117f 89 if (need_resched()) {
8ca3c642 90 local_irq_enable();
08c373e5 91 return;
8ca3c642
DL
92 }
93
a1d028bd
DL
94 /*
95 * During the idle period, stop measuring the disabled irqs
96 * critical sections latencies
97 */
c8cc7d4d 98 stop_critical_timings();
a1d028bd
DL
99
100 /*
101 * Tell the RCU framework we are entering an idle section,
102 * so no more rcu read side critical sections and one more
103 * step to the grace period
104 */
c8cc7d4d
DL
105 rcu_idle_enter();
106
a1d028bd 107 /*
52c324f8 108 * Ask the cpuidle framework to choose a convenient idle state.
ec6e7f40 109 * Fall back to the default arch idle method on errors.
a1d028bd 110 */
52c324f8 111 next_state = cpuidle_select(drv, dev);
ec6e7f40 112 if (next_state < 0) {
37352273 113use_default:
a1d028bd 114 /*
37352273
PZ
115 * We can't use the cpuidle framework, let's use the default
116 * idle routine.
a1d028bd 117 */
37352273 118 if (current_clr_polling_and_test())
8ca3c642 119 local_irq_enable();
37352273
PZ
120 else
121 arch_cpu_idle();
122
123 goto exit_idle;
8ca3c642
DL
124 }
125
37352273
PZ
126
127 /*
128 * The idle task must be scheduled, it is pointless to
129 * go to idle, just update no idle residency and get
130 * out of this function
131 */
132 if (current_clr_polling_and_test()) {
133 dev->last_residency = 0;
134 entered_state = next_state;
135 local_irq_enable();
136 goto exit_idle;
c444117f 137 }
8ca3c642 138
89abb5ad 139 broadcast = drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP;
37352273
PZ
140
141 /*
142 * Tell the time framework to switch to a broadcast timer
143 * because our local timer will be shutdown. If a local timer
144 * is used from another cpu as a broadcast timer, this call may
145 * fail if it is not available
146 */
147 if (broadcast &&
148 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &dev->cpu))
149 goto use_default;
150
442bf3aa
DL
151 /* Take note of the planned idle state. */
152 idle_set_state(this_rq(), &drv->states[next_state]);
153
37352273
PZ
154 /*
155 * Enter the idle state previously returned by the governor decision.
156 * This function will block until an interrupt occurs and will take
157 * care of re-enabling the local interrupts
158 */
159 entered_state = cpuidle_enter(drv, dev, next_state);
160
442bf3aa
DL
161 /* The cpu is no longer idle or about to enter idle. */
162 idle_set_state(this_rq(), NULL);
163
37352273
PZ
164 if (broadcast)
165 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &dev->cpu);
166
167 /*
168 * Give the governor an opportunity to reflect on the outcome
169 */
170 cpuidle_reflect(dev, entered_state);
171
172exit_idle:
8ca3c642 173 __current_set_polling();
30cdd69e 174
a1d028bd 175 /*
37352273 176 * It is up to the idle functions to reenable local interrupts
a1d028bd 177 */
c8cc7d4d
DL
178 if (WARN_ON_ONCE(irqs_disabled()))
179 local_irq_enable();
180
181 rcu_idle_exit();
182 start_critical_timings();
30cdd69e 183}
30cdd69e 184
cf37b6b4
NP
185/*
186 * Generic idle loop implementation
82c65d60
AL
187 *
188 * Called with polling cleared.
cf37b6b4
NP
189 */
190static void cpu_idle_loop(void)
191{
192 while (1) {
82c65d60
AL
193 /*
194 * If the arch has a polling bit, we maintain an invariant:
195 *
196 * Our polling bit is clear if we're not scheduled (i.e. if
197 * rq->curr != rq->idle). This means that, if rq->idle has
198 * the polling bit set, then setting need_resched is
199 * guaranteed to cause the cpu to reschedule.
200 */
201
202 __current_set_polling();
cf37b6b4
NP
203 tick_nohz_idle_enter();
204
205 while (!need_resched()) {
206 check_pgt_cache();
207 rmb();
208
209 if (cpu_is_offline(smp_processor_id()))
210 arch_cpu_idle_dead();
211
212 local_irq_disable();
213 arch_cpu_idle_enter();
214
215 /*
216 * In poll mode we reenable interrupts and spin.
217 *
218 * Also if we detected in the wakeup from idle
219 * path that the tick broadcast device expired
220 * for us, we don't want to go deep idle as we
221 * know that the IPI is going to arrive right
222 * away
223 */
8ca3c642 224 if (cpu_idle_force_poll || tick_check_broadcast_expired())
cf37b6b4 225 cpu_idle_poll();
8ca3c642
DL
226 else
227 cpuidle_idle_call();
228
cf37b6b4 229 arch_cpu_idle_exit();
cf37b6b4 230 }
06d50c65
PZ
231
232 /*
233 * Since we fell out of the loop above, we know
234 * TIF_NEED_RESCHED must be set, propagate it into
235 * PREEMPT_NEED_RESCHED.
236 *
237 * This is required because for polling idle loops we will
238 * not have had an IPI to fold the state for us.
239 */
240 preempt_set_need_resched();
cf37b6b4 241 tick_nohz_idle_exit();
82c65d60
AL
242 __current_clr_polling();
243
244 /*
e3baac47
PZ
245 * We promise to call sched_ttwu_pending and reschedule
246 * if need_resched is set while polling is set. That
247 * means that clearing polling needs to be visible
248 * before doing these things.
82c65d60
AL
249 */
250 smp_mb__after_atomic();
251
e3baac47 252 sched_ttwu_pending();
cf37b6b4
NP
253 schedule_preempt_disabled();
254 }
255}
256
257void cpu_startup_entry(enum cpuhp_state state)
258{
259 /*
260 * This #ifdef needs to die, but it's too late in the cycle to
261 * make this generic (arm and sh have never invoked the canary
262 * init for the non boot cpus!). Will be fixed in 3.11
263 */
264#ifdef CONFIG_X86
265 /*
266 * If we're the non-boot CPU, nothing set the stack canary up
267 * for us. The boot CPU already has it initialized but no harm
268 * in doing it again. This is a good place for updating it, as
269 * we wont ever return from this function (so the invalid
270 * canaries already on the stack wont ever trigger).
271 */
272 boot_init_stack_canary();
273#endif
cf37b6b4
NP
274 arch_cpu_idle_prepare();
275 cpu_idle_loop();
276}