idle.c 9.1 KB

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