idle.c 12 KB

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