1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Generic entry points for the idle threads and
4 * implementation of the idle task scheduling class.
5 *
6 * (NOTE: these are not related to SCHED_IDLE batch scheduled
7 * tasks which are handled in sched/fair.c )
8 */
9 #include "sched.h"
10
11 #include <trace/events/power.h>
12
13 #include <trace/hooks/sched.h>
14
15 /* Linker adds these: start and end of __cpuidle functions */
16 extern char __cpuidle_text_start[], __cpuidle_text_end[];
17
18 /**
19 * sched_idle_set_state - Record idle state for the current CPU.
20 * @idle_state: State to record.
21 */
sched_idle_set_state(struct cpuidle_state * idle_state)22 void sched_idle_set_state(struct cpuidle_state *idle_state)
23 {
24 idle_set_state(this_rq(), idle_state);
25 }
26
27 static int __read_mostly cpu_idle_force_poll;
28
cpu_idle_poll_ctrl(bool enable)29 void cpu_idle_poll_ctrl(bool enable)
30 {
31 if (enable) {
32 cpu_idle_force_poll++;
33 } else {
34 cpu_idle_force_poll--;
35 WARN_ON_ONCE(cpu_idle_force_poll < 0);
36 }
37 }
38
39 #ifdef CONFIG_GENERIC_IDLE_POLL_SETUP
cpu_idle_poll_setup(char * __unused)40 static int __init cpu_idle_poll_setup(char *__unused)
41 {
42 cpu_idle_force_poll = 1;
43
44 return 1;
45 }
46 __setup("nohlt", cpu_idle_poll_setup);
47
cpu_idle_nopoll_setup(char * __unused)48 static int __init cpu_idle_nopoll_setup(char *__unused)
49 {
50 cpu_idle_force_poll = 0;
51
52 return 1;
53 }
54 __setup("hlt", cpu_idle_nopoll_setup);
55 #endif
56
cpu_idle_poll(void)57 static noinline int __cpuidle cpu_idle_poll(void)
58 {
59 trace_cpu_idle(0, smp_processor_id());
60 stop_critical_timings();
61 rcu_idle_enter();
62 local_irq_enable();
63
64 while (!tif_need_resched() &&
65 (cpu_idle_force_poll || tick_check_broadcast_expired()))
66 cpu_relax();
67
68 rcu_idle_exit();
69 start_critical_timings();
70 trace_cpu_idle(PWR_EVENT_EXIT, smp_processor_id());
71
72 return 1;
73 }
74
75 /* Weak implementations for optional arch specific functions */
arch_cpu_idle_prepare(void)76 void __weak arch_cpu_idle_prepare(void) { }
arch_cpu_idle_enter(void)77 void __weak arch_cpu_idle_enter(void) { }
arch_cpu_idle_exit(void)78 void __weak arch_cpu_idle_exit(void) { }
arch_cpu_idle_dead(void)79 void __weak arch_cpu_idle_dead(void) { }
arch_cpu_idle(void)80 void __weak arch_cpu_idle(void)
81 {
82 cpu_idle_force_poll = 1;
83 raw_local_irq_enable();
84 }
85
86 /**
87 * default_idle_call - Default CPU idle routine.
88 *
89 * To use when the cpuidle framework cannot be used.
90 */
default_idle_call(void)91 void __cpuidle default_idle_call(void)
92 {
93 if (current_clr_polling_and_test()) {
94 local_irq_enable();
95 } else {
96
97 trace_cpu_idle(1, smp_processor_id());
98 stop_critical_timings();
99
100 /*
101 * arch_cpu_idle() is supposed to enable IRQs, however
102 * we can't do that because of RCU and tracing.
103 *
104 * Trace IRQs enable here, then switch off RCU, and have
105 * arch_cpu_idle() use raw_local_irq_enable(). Note that
106 * rcu_idle_enter() relies on lockdep IRQ state, so switch that
107 * last -- this is very similar to the entry code.
108 */
109 trace_hardirqs_on_prepare();
110 lockdep_hardirqs_on_prepare(_THIS_IP_);
111 rcu_idle_enter();
112 lockdep_hardirqs_on(_THIS_IP_);
113
114 arch_cpu_idle();
115
116 /*
117 * OK, so IRQs are enabled here, but RCU needs them disabled to
118 * turn itself back on.. funny thing is that disabling IRQs
119 * will cause tracing, which needs RCU. Jump through hoops to
120 * make it 'work'.
121 */
122 raw_local_irq_disable();
123 lockdep_hardirqs_off(_THIS_IP_);
124 rcu_idle_exit();
125 lockdep_hardirqs_on(_THIS_IP_);
126 raw_local_irq_enable();
127
128 start_critical_timings();
129 trace_cpu_idle(PWR_EVENT_EXIT, smp_processor_id());
130 }
131 }
132
call_cpuidle_s2idle(struct cpuidle_driver * drv,struct cpuidle_device * dev)133 static int call_cpuidle_s2idle(struct cpuidle_driver *drv,
134 struct cpuidle_device *dev)
135 {
136 if (current_clr_polling_and_test())
137 return -EBUSY;
138
139 return cpuidle_enter_s2idle(drv, dev);
140 }
141
call_cpuidle(struct cpuidle_driver * drv,struct cpuidle_device * dev,int next_state)142 static int call_cpuidle(struct cpuidle_driver *drv, struct cpuidle_device *dev,
143 int next_state)
144 {
145 /*
146 * The idle task must be scheduled, it is pointless to go to idle, just
147 * update no idle residency and return.
148 */
149 if (current_clr_polling_and_test()) {
150 dev->last_residency_ns = 0;
151 local_irq_enable();
152 return -EBUSY;
153 }
154
155 /*
156 * Enter the idle state previously returned by the governor decision.
157 * This function will block until an interrupt occurs and will take
158 * care of re-enabling the local interrupts
159 */
160 return cpuidle_enter(drv, dev, next_state);
161 }
162
163 /**
164 * cpuidle_idle_call - the main idle function
165 *
166 * NOTE: no locks or semaphores should be used here
167 *
168 * On archs that support TIF_POLLING_NRFLAG, is called with polling
169 * set, and it returns with polling set. If it ever stops polling, it
170 * must clear the polling bit.
171 */
cpuidle_idle_call(void)172 static void cpuidle_idle_call(void)
173 {
174 struct cpuidle_device *dev = cpuidle_get_device();
175 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
176 int next_state, entered_state;
177
178 /*
179 * Check if the idle task must be rescheduled. If it is the
180 * case, exit the function after re-enabling the local irq.
181 */
182 if (need_resched()) {
183 local_irq_enable();
184 return;
185 }
186
187 /*
188 * The RCU framework needs to be told that we are entering an idle
189 * section, so no more rcu read side critical sections and one more
190 * step to the grace period
191 */
192
193 if (cpuidle_not_available(drv, dev)) {
194 tick_nohz_idle_stop_tick();
195
196 default_idle_call();
197 goto exit_idle;
198 }
199
200 /*
201 * Suspend-to-idle ("s2idle") is a system state in which all user space
202 * has been frozen, all I/O devices have been suspended and the only
203 * activity happens here and in interrupts (if any). In that case bypass
204 * the cpuidle governor and go stratight for the deepest idle state
205 * available. Possibly also suspend the local tick and the entire
206 * timekeeping to prevent timer interrupts from kicking us out of idle
207 * until a proper wakeup interrupt happens.
208 */
209
210 if (idle_should_enter_s2idle() || dev->forced_idle_latency_limit_ns) {
211 u64 max_latency_ns;
212
213 if (idle_should_enter_s2idle()) {
214
215 entered_state = call_cpuidle_s2idle(drv, dev);
216 if (entered_state > 0)
217 goto exit_idle;
218
219 max_latency_ns = U64_MAX;
220 } else {
221 max_latency_ns = dev->forced_idle_latency_limit_ns;
222 }
223
224 tick_nohz_idle_stop_tick();
225
226 next_state = cpuidle_find_deepest_state(drv, dev, max_latency_ns);
227 call_cpuidle(drv, dev, next_state);
228 } else {
229 bool stop_tick = true;
230
231 /*
232 * Ask the cpuidle framework to choose a convenient idle state.
233 */
234 next_state = cpuidle_select(drv, dev, &stop_tick);
235
236 if (stop_tick || tick_nohz_tick_stopped())
237 tick_nohz_idle_stop_tick();
238 else
239 tick_nohz_idle_retain_tick();
240
241 entered_state = call_cpuidle(drv, dev, next_state);
242 /*
243 * Give the governor an opportunity to reflect on the outcome
244 */
245 cpuidle_reflect(dev, entered_state);
246 }
247
248 exit_idle:
249 __current_set_polling();
250
251 /*
252 * It is up to the idle functions to reenable local interrupts
253 */
254 if (WARN_ON_ONCE(irqs_disabled()))
255 local_irq_enable();
256 }
257
258 /*
259 * Generic idle loop implementation
260 *
261 * Called with polling cleared.
262 */
do_idle(void)263 static void do_idle(void)
264 {
265 int cpu = smp_processor_id();
266 /*
267 * If the arch has a polling bit, we maintain an invariant:
268 *
269 * Our polling bit is clear if we're not scheduled (i.e. if rq->curr !=
270 * rq->idle). This means that, if rq->idle has the polling bit set,
271 * then setting need_resched is guaranteed to cause the CPU to
272 * reschedule.
273 */
274
275 __current_set_polling();
276 tick_nohz_idle_enter();
277
278 while (!need_resched()) {
279 rmb();
280
281 local_irq_disable();
282
283 if (cpu_is_offline(cpu)) {
284 tick_nohz_idle_stop_tick();
285 cpuhp_report_idle_dead();
286 arch_cpu_idle_dead();
287 }
288
289 arch_cpu_idle_enter();
290 rcu_nocb_flush_deferred_wakeup();
291
292 /*
293 * In poll mode we reenable interrupts and spin. Also if we
294 * detected in the wakeup from idle path that the tick
295 * broadcast device expired for us, we don't want to go deep
296 * idle as we know that the IPI is going to arrive right away.
297 */
298 if (cpu_idle_force_poll || tick_check_broadcast_expired()) {
299 tick_nohz_idle_restart_tick();
300 cpu_idle_poll();
301 } else {
302 cpuidle_idle_call();
303 }
304 arch_cpu_idle_exit();
305 }
306
307 /*
308 * Since we fell out of the loop above, we know TIF_NEED_RESCHED must
309 * be set, propagate it into PREEMPT_NEED_RESCHED.
310 *
311 * This is required because for polling idle loops we will not have had
312 * an IPI to fold the state for us.
313 */
314 preempt_set_need_resched();
315 tick_nohz_idle_exit();
316 __current_clr_polling();
317
318 /*
319 * We promise to call sched_ttwu_pending() and reschedule if
320 * need_resched() is set while polling is set. That means that clearing
321 * polling needs to be visible before doing these things.
322 */
323 smp_mb__after_atomic();
324
325 /*
326 * RCU relies on this call to be done outside of an RCU read-side
327 * critical section.
328 */
329 flush_smp_call_function_from_idle();
330 schedule_idle();
331
332 if (unlikely(klp_patch_pending(current)))
333 klp_update_patch_state(current);
334 }
335
cpu_in_idle(unsigned long pc)336 bool cpu_in_idle(unsigned long pc)
337 {
338 return pc >= (unsigned long)__cpuidle_text_start &&
339 pc < (unsigned long)__cpuidle_text_end;
340 }
341
342 struct idle_timer {
343 struct hrtimer timer;
344 int done;
345 };
346
idle_inject_timer_fn(struct hrtimer * timer)347 static enum hrtimer_restart idle_inject_timer_fn(struct hrtimer *timer)
348 {
349 struct idle_timer *it = container_of(timer, struct idle_timer, timer);
350
351 WRITE_ONCE(it->done, 1);
352 set_tsk_need_resched(current);
353
354 return HRTIMER_NORESTART;
355 }
356
play_idle_precise(u64 duration_ns,u64 latency_ns)357 void play_idle_precise(u64 duration_ns, u64 latency_ns)
358 {
359 struct idle_timer it;
360
361 /*
362 * Only FIFO tasks can disable the tick since they don't need the forced
363 * preemption.
364 */
365 WARN_ON_ONCE(current->policy != SCHED_FIFO);
366 WARN_ON_ONCE(current->nr_cpus_allowed != 1);
367 WARN_ON_ONCE(!(current->flags & PF_KTHREAD));
368 WARN_ON_ONCE(!(current->flags & PF_NO_SETAFFINITY));
369 WARN_ON_ONCE(!duration_ns);
370
371 rcu_sleep_check();
372 preempt_disable();
373 current->flags |= PF_IDLE;
374 cpuidle_use_deepest_state(latency_ns);
375
376 it.done = 0;
377 hrtimer_init_on_stack(&it.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
378 it.timer.function = idle_inject_timer_fn;
379 hrtimer_start(&it.timer, ns_to_ktime(duration_ns),
380 HRTIMER_MODE_REL_PINNED_HARD);
381
382 while (!READ_ONCE(it.done))
383 do_idle();
384
385 cpuidle_use_deepest_state(0);
386 current->flags &= ~PF_IDLE;
387
388 preempt_fold_need_resched();
389 preempt_enable();
390 }
391 EXPORT_SYMBOL_GPL(play_idle_precise);
392
cpu_startup_entry(enum cpuhp_state state)393 void cpu_startup_entry(enum cpuhp_state state)
394 {
395 arch_cpu_idle_prepare();
396 cpuhp_online_idle(state);
397 while (1)
398 do_idle();
399 }
400
401 /*
402 * idle-task scheduling class.
403 */
404
405 #ifdef CONFIG_SMP
406 static int
select_task_rq_idle(struct task_struct * p,int cpu,int sd_flag,int flags)407 select_task_rq_idle(struct task_struct *p, int cpu, int sd_flag, int flags)
408 {
409 return task_cpu(p); /* IDLE tasks as never migrated */
410 }
411
412 static int
balance_idle(struct rq * rq,struct task_struct * prev,struct rq_flags * rf)413 balance_idle(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
414 {
415 return WARN_ON_ONCE(1);
416 }
417 #endif
418
419 /*
420 * Idle tasks are unconditionally rescheduled:
421 */
check_preempt_curr_idle(struct rq * rq,struct task_struct * p,int flags)422 static void check_preempt_curr_idle(struct rq *rq, struct task_struct *p, int flags)
423 {
424 resched_curr(rq);
425 }
426
put_prev_task_idle(struct rq * rq,struct task_struct * prev)427 static void put_prev_task_idle(struct rq *rq, struct task_struct *prev)
428 {
429 }
430
set_next_task_idle(struct rq * rq,struct task_struct * next,bool first)431 static void set_next_task_idle(struct rq *rq, struct task_struct *next, bool first)
432 {
433 update_idle_core(rq);
434 schedstat_inc(rq->sched_goidle);
435 }
436
pick_next_task_idle(struct rq * rq)437 struct task_struct *pick_next_task_idle(struct rq *rq)
438 {
439 struct task_struct *next = rq->idle;
440
441 set_next_task_idle(rq, next, true);
442
443 return next;
444 }
445
446 /*
447 * It is not legal to sleep in the idle task - print a warning
448 * message if some code attempts to do it:
449 */
450 static void
dequeue_task_idle(struct rq * rq,struct task_struct * p,int flags)451 dequeue_task_idle(struct rq *rq, struct task_struct *p, int flags)
452 {
453 raw_spin_unlock_irq(&rq->lock);
454 printk(KERN_ERR "bad: scheduling from the idle thread!\n");
455
456 trace_android_rvh_dequeue_task_idle(p);
457 dump_stack();
458 raw_spin_lock_irq(&rq->lock);
459 }
460
461 /*
462 * scheduler tick hitting a task of our scheduling class.
463 *
464 * NOTE: This function can be called remotely by the tick offload that
465 * goes along full dynticks. Therefore no local assumption can be made
466 * and everything must be accessed through the @rq and @curr passed in
467 * parameters.
468 */
task_tick_idle(struct rq * rq,struct task_struct * curr,int queued)469 static void task_tick_idle(struct rq *rq, struct task_struct *curr, int queued)
470 {
471 }
472
switched_to_idle(struct rq * rq,struct task_struct * p)473 static void switched_to_idle(struct rq *rq, struct task_struct *p)
474 {
475 BUG();
476 }
477
478 static void
prio_changed_idle(struct rq * rq,struct task_struct * p,int oldprio)479 prio_changed_idle(struct rq *rq, struct task_struct *p, int oldprio)
480 {
481 BUG();
482 }
483
update_curr_idle(struct rq * rq)484 static void update_curr_idle(struct rq *rq)
485 {
486 }
487
488 /*
489 * Simple, special scheduling class for the per-CPU idle tasks:
490 */
491 const struct sched_class idle_sched_class
492 __section("__idle_sched_class") = {
493 /* no enqueue/yield_task for idle tasks */
494
495 /* dequeue is not valid, we print a debug message there: */
496 .dequeue_task = dequeue_task_idle,
497
498 .check_preempt_curr = check_preempt_curr_idle,
499
500 .pick_next_task = pick_next_task_idle,
501 .put_prev_task = put_prev_task_idle,
502 .set_next_task = set_next_task_idle,
503
504 #ifdef CONFIG_SMP
505 .balance = balance_idle,
506 .select_task_rq = select_task_rq_idle,
507 .set_cpus_allowed = set_cpus_allowed_common,
508 #endif
509
510 .task_tick = task_tick_idle,
511
512 .prio_changed = prio_changed_idle,
513 .switched_to = switched_to_idle,
514 .update_curr = update_curr_idle,
515 };
516