idle.c 11 KB

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