• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Generic entry point for the idle threads
3  */
4 #include <linux/sched.h>
5 #include <linux/sched/idle.h>
6 #include <linux/cpu.h>
7 #include <linux/cpuidle.h>
8 #include <linux/cpuhotplug.h>
9 #include <linux/tick.h>
10 #include <linux/mm.h>
11 #include <linux/stackprotector.h>
12 #include <linux/suspend.h>
13 #include <linux/livepatch.h>
14 
15 #include <asm/tlb.h>
16 
17 #include <trace/events/power.h>
18 
19 #include "sched.h"
20 
21 /* Linker adds these: start and end of __cpuidle functions */
22 extern char __cpuidle_text_start[], __cpuidle_text_end[];
23 
24 /**
25  * sched_idle_set_state - Record idle state for the current CPU.
26  * @idle_state: State to record.
27  */
sched_idle_set_state(struct cpuidle_state * idle_state,int index)28 void sched_idle_set_state(struct cpuidle_state *idle_state, int index)
29 {
30 	idle_set_state(this_rq(), idle_state);
31 	idle_set_state_idx(this_rq(), index);
32 }
33 
34 static int __read_mostly cpu_idle_force_poll;
35 
cpu_idle_poll_ctrl(bool enable)36 void cpu_idle_poll_ctrl(bool enable)
37 {
38 	if (enable) {
39 		cpu_idle_force_poll++;
40 	} else {
41 		cpu_idle_force_poll--;
42 		WARN_ON_ONCE(cpu_idle_force_poll < 0);
43 	}
44 }
45 
46 #ifdef CONFIG_GENERIC_IDLE_POLL_SETUP
cpu_idle_poll_setup(char * __unused)47 static int __init cpu_idle_poll_setup(char *__unused)
48 {
49 	cpu_idle_force_poll = 1;
50 	return 1;
51 }
52 __setup("nohlt", cpu_idle_poll_setup);
53 
cpu_idle_nopoll_setup(char * __unused)54 static int __init cpu_idle_nopoll_setup(char *__unused)
55 {
56 	cpu_idle_force_poll = 0;
57 	return 1;
58 }
59 __setup("hlt", cpu_idle_nopoll_setup);
60 #endif
61 
cpu_idle_poll(void)62 static noinline int __cpuidle cpu_idle_poll(void)
63 {
64 	rcu_idle_enter();
65 	trace_cpu_idle_rcuidle(0, smp_processor_id());
66 	local_irq_enable();
67 	stop_critical_timings();
68 	while (!tif_need_resched() &&
69 		(cpu_idle_force_poll || tick_check_broadcast_expired()))
70 		cpu_relax();
71 	start_critical_timings();
72 	trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
73 	rcu_idle_exit();
74 	return 1;
75 }
76 
77 /* Weak implementations for optional arch specific functions */
arch_cpu_idle_prepare(void)78 void __weak arch_cpu_idle_prepare(void) { }
arch_cpu_idle_enter(void)79 void __weak arch_cpu_idle_enter(void) { }
arch_cpu_idle_exit(void)80 void __weak arch_cpu_idle_exit(void) { }
arch_cpu_idle_dead(void)81 void __weak arch_cpu_idle_dead(void) { }
arch_cpu_idle(void)82 void __weak arch_cpu_idle(void)
83 {
84 	cpu_idle_force_poll = 1;
85 	local_irq_enable();
86 }
87 
88 /**
89  * default_idle_call - Default CPU idle routine.
90  *
91  * To use when the cpuidle framework cannot be used.
92  */
default_idle_call(void)93 void __cpuidle default_idle_call(void)
94 {
95 	if (current_clr_polling_and_test()) {
96 		local_irq_enable();
97 	} else {
98 		stop_critical_timings();
99 		arch_cpu_idle();
100 		start_critical_timings();
101 	}
102 }
103 
call_cpuidle(struct cpuidle_driver * drv,struct cpuidle_device * dev,int next_state)104 static int call_cpuidle(struct cpuidle_driver *drv, struct cpuidle_device *dev,
105 		      int next_state)
106 {
107 	/*
108 	 * The idle task must be scheduled, it is pointless to go to idle, just
109 	 * update no idle residency and return.
110 	 */
111 	if (current_clr_polling_and_test()) {
112 		dev->last_residency = 0;
113 		local_irq_enable();
114 		return -EBUSY;
115 	}
116 
117 	/*
118 	 * Enter the idle state previously returned by the governor decision.
119 	 * This function will block until an interrupt occurs and will take
120 	 * care of re-enabling the local interrupts
121 	 */
122 	return cpuidle_enter(drv, dev, next_state);
123 }
124 
125 /**
126  * cpuidle_idle_call - the main idle function
127  *
128  * NOTE: no locks or semaphores should be used here
129  *
130  * On archs that support TIF_POLLING_NRFLAG, is called with polling
131  * set, and it returns with polling set.  If it ever stops polling, it
132  * must clear the polling bit.
133  */
cpuidle_idle_call(void)134 static void cpuidle_idle_call(void)
135 {
136 	struct cpuidle_device *dev = cpuidle_get_device();
137 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
138 	int next_state, entered_state;
139 
140 	/*
141 	 * Check if the idle task must be rescheduled. If it is the
142 	 * case, exit the function after re-enabling the local irq.
143 	 */
144 	if (need_resched()) {
145 		local_irq_enable();
146 		return;
147 	}
148 
149 	/*
150 	 * The RCU framework needs to be told that we are entering an idle
151 	 * section, so no more rcu read side critical sections and one more
152 	 * step to the grace period
153 	 */
154 
155 	if (cpuidle_not_available(drv, dev)) {
156 		tick_nohz_idle_stop_tick();
157 		rcu_idle_enter();
158 
159 		default_idle_call();
160 		goto exit_idle;
161 	}
162 
163 	/*
164 	 * Suspend-to-idle ("s2idle") is a system state in which all user space
165 	 * has been frozen, all I/O devices have been suspended and the only
166 	 * activity happens here and in iterrupts (if any).  In that case bypass
167 	 * the cpuidle governor and go stratight for the deepest idle state
168 	 * available.  Possibly also suspend the local tick and the entire
169 	 * timekeeping to prevent timer interrupts from kicking us out of idle
170 	 * until a proper wakeup interrupt happens.
171 	 */
172 
173 	if (idle_should_enter_s2idle() || dev->use_deepest_state) {
174 		if (idle_should_enter_s2idle()) {
175 			rcu_idle_enter();
176 
177 			entered_state = cpuidle_enter_s2idle(drv, dev);
178 			if (entered_state > 0) {
179 				local_irq_enable();
180 				goto exit_idle;
181 			}
182 
183 			rcu_idle_exit();
184 		}
185 
186 		tick_nohz_idle_stop_tick();
187 		rcu_idle_enter();
188 
189 		next_state = cpuidle_find_deepest_state(drv, dev);
190 		call_cpuidle(drv, dev, next_state);
191 	} else {
192 		bool stop_tick = true;
193 
194 		/*
195 		 * Ask the cpuidle framework to choose a convenient idle state.
196 		 */
197 		next_state = cpuidle_select(drv, dev, &stop_tick);
198 
199 		if (stop_tick)
200 			tick_nohz_idle_stop_tick();
201 		else
202 			tick_nohz_idle_retain_tick();
203 
204 		rcu_idle_enter();
205 
206 		entered_state = call_cpuidle(drv, dev, next_state);
207 		/*
208 		 * Give the governor an opportunity to reflect on the outcome
209 		 */
210 		cpuidle_reflect(dev, entered_state);
211 	}
212 
213 exit_idle:
214 	__current_set_polling();
215 
216 	/*
217 	 * It is up to the idle functions to reenable local interrupts
218 	 */
219 	if (WARN_ON_ONCE(irqs_disabled()))
220 		local_irq_enable();
221 
222 	rcu_idle_exit();
223 }
224 
225 /*
226  * Generic idle loop implementation
227  *
228  * Called with polling cleared.
229  */
do_idle(void)230 static void do_idle(void)
231 {
232 	/*
233 	 * If the arch has a polling bit, we maintain an invariant:
234 	 *
235 	 * Our polling bit is clear if we're not scheduled (i.e. if rq->curr !=
236 	 * rq->idle). This means that, if rq->idle has the polling bit set,
237 	 * then setting need_resched is guaranteed to cause the CPU to
238 	 * reschedule.
239 	 */
240 
241 	__current_set_polling();
242 	quiet_vmstat();
243 	tick_nohz_idle_enter();
244 
245 	while (!need_resched()) {
246 		check_pgt_cache();
247 		rmb();
248 
249 		if (cpu_is_offline(smp_processor_id())) {
250 			tick_nohz_idle_stop_tick_protected();
251 			cpuhp_report_idle_dead();
252 			arch_cpu_idle_dead();
253 		}
254 
255 		local_irq_disable();
256 		arch_cpu_idle_enter();
257 
258 		/*
259 		 * In poll mode we reenable interrupts and spin. Also if we
260 		 * detected in the wakeup from idle path that the tick
261 		 * broadcast device expired for us, we don't want to go deep
262 		 * idle as we know that the IPI is going to arrive right away.
263 		 */
264 		if (cpu_idle_force_poll || tick_check_broadcast_expired()) {
265 			tick_nohz_idle_restart_tick();
266 			cpu_idle_poll();
267 		} else {
268 			cpuidle_idle_call();
269 		}
270 		arch_cpu_idle_exit();
271 	}
272 
273 	/*
274 	 * Since we fell out of the loop above, we know TIF_NEED_RESCHED must
275 	 * be set, propagate it into PREEMPT_NEED_RESCHED.
276 	 *
277 	 * This is required because for polling idle loops we will not have had
278 	 * an IPI to fold the state for us.
279 	 */
280 	preempt_set_need_resched();
281 	tick_nohz_idle_exit();
282 	__current_clr_polling();
283 
284 	/*
285 	 * We promise to call sched_ttwu_pending() and reschedule if
286 	 * need_resched() is set while polling is set. That means that clearing
287 	 * polling needs to be visible before doing these things.
288 	 */
289 	smp_mb__after_atomic();
290 
291 	sched_ttwu_pending();
292 	schedule_idle();
293 
294 	if (unlikely(klp_patch_pending(current)))
295 		klp_update_patch_state(current);
296 }
297 
cpu_in_idle(unsigned long pc)298 bool cpu_in_idle(unsigned long pc)
299 {
300 	return pc >= (unsigned long)__cpuidle_text_start &&
301 		pc < (unsigned long)__cpuidle_text_end;
302 }
303 
304 struct idle_timer {
305 	struct hrtimer timer;
306 	int done;
307 };
308 
idle_inject_timer_fn(struct hrtimer * timer)309 static enum hrtimer_restart idle_inject_timer_fn(struct hrtimer *timer)
310 {
311 	struct idle_timer *it = container_of(timer, struct idle_timer, timer);
312 
313 	WRITE_ONCE(it->done, 1);
314 	set_tsk_need_resched(current);
315 
316 	return HRTIMER_NORESTART;
317 }
318 
play_idle(unsigned long duration_ms)319 void play_idle(unsigned long duration_ms)
320 {
321 	struct idle_timer it;
322 
323 	/*
324 	 * Only FIFO tasks can disable the tick since they don't need the forced
325 	 * preemption.
326 	 */
327 	WARN_ON_ONCE(current->policy != SCHED_FIFO);
328 	WARN_ON_ONCE(current->nr_cpus_allowed != 1);
329 	WARN_ON_ONCE(!(current->flags & PF_KTHREAD));
330 	WARN_ON_ONCE(!(current->flags & PF_NO_SETAFFINITY));
331 	WARN_ON_ONCE(!duration_ms);
332 
333 	rcu_sleep_check();
334 	preempt_disable();
335 	current->flags |= PF_IDLE;
336 	cpuidle_use_deepest_state(true);
337 
338 	it.done = 0;
339 	hrtimer_init_on_stack(&it.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
340 	it.timer.function = idle_inject_timer_fn;
341 	hrtimer_start(&it.timer, ms_to_ktime(duration_ms), HRTIMER_MODE_REL_PINNED);
342 
343 	while (!READ_ONCE(it.done))
344 		do_idle();
345 
346 	cpuidle_use_deepest_state(false);
347 	current->flags &= ~PF_IDLE;
348 
349 	preempt_fold_need_resched();
350 	preempt_enable();
351 }
352 EXPORT_SYMBOL_GPL(play_idle);
353 
cpu_startup_entry(enum cpuhp_state state)354 void cpu_startup_entry(enum cpuhp_state state)
355 {
356 	/*
357 	 * This #ifdef needs to die, but it's too late in the cycle to
358 	 * make this generic (arm and sh have never invoked the canary
359 	 * init for the non boot cpus!). Will be fixed in 3.11
360 	 */
361 #ifdef CONFIG_X86
362 	/*
363 	 * If we're the non-boot CPU, nothing set the stack canary up
364 	 * for us. The boot CPU already has it initialized but no harm
365 	 * in doing it again. This is a good place for updating it, as
366 	 * we wont ever return from this function (so the invalid
367 	 * canaries already on the stack wont ever trigger).
368 	 */
369 	boot_init_stack_canary();
370 #endif
371 	arch_cpu_idle_prepare();
372 	cpuhp_online_idle(state);
373 	while (1)
374 		do_idle();
375 }
376