idle.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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. instrumentation_begin();
  46. trace_cpu_idle(0, smp_processor_id());
  47. stop_critical_timings();
  48. ct_cpuidle_enter();
  49. raw_local_irq_enable();
  50. while (!tif_need_resched() &&
  51. (cpu_idle_force_poll || tick_check_broadcast_expired()))
  52. cpu_relax();
  53. raw_local_irq_disable();
  54. ct_cpuidle_exit();
  55. start_critical_timings();
  56. trace_cpu_idle(PWR_EVENT_EXIT, smp_processor_id());
  57. local_irq_enable();
  58. instrumentation_end();
  59. return 1;
  60. }
  61. /* Weak implementations for optional arch specific functions */
  62. void __weak arch_cpu_idle_prepare(void) { }
  63. void __weak arch_cpu_idle_enter(void) { }
  64. void __weak arch_cpu_idle_exit(void) { }
  65. void __weak __noreturn arch_cpu_idle_dead(void) { while (1); }
  66. void __weak arch_cpu_idle(void)
  67. {
  68. cpu_idle_force_poll = 1;
  69. }
  70. #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST_IDLE
  71. DEFINE_STATIC_KEY_FALSE(arch_needs_tick_broadcast);
  72. static inline void cond_tick_broadcast_enter(void)
  73. {
  74. if (static_branch_unlikely(&arch_needs_tick_broadcast))
  75. tick_broadcast_enter();
  76. }
  77. static inline void cond_tick_broadcast_exit(void)
  78. {
  79. if (static_branch_unlikely(&arch_needs_tick_broadcast))
  80. tick_broadcast_exit();
  81. }
  82. #else
  83. static inline void cond_tick_broadcast_enter(void) { }
  84. static inline void cond_tick_broadcast_exit(void) { }
  85. #endif
  86. /**
  87. * default_idle_call - Default CPU idle routine.
  88. *
  89. * To use when the cpuidle framework cannot be used.
  90. */
  91. void __cpuidle default_idle_call(void)
  92. {
  93. instrumentation_begin();
  94. if (!current_clr_polling_and_test()) {
  95. cond_tick_broadcast_enter();
  96. trace_cpu_idle(1, smp_processor_id());
  97. stop_critical_timings();
  98. ct_cpuidle_enter();
  99. arch_cpu_idle();
  100. ct_cpuidle_exit();
  101. start_critical_timings();
  102. trace_cpu_idle(PWR_EVENT_EXIT, smp_processor_id());
  103. cond_tick_broadcast_exit();
  104. }
  105. local_irq_enable();
  106. instrumentation_end();
  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. if (cpuidle_not_available(drv, dev)) {
  157. tick_nohz_idle_stop_tick();
  158. default_idle_call();
  159. goto exit_idle;
  160. }
  161. /*
  162. * Suspend-to-idle ("s2idle") is a system state in which all user space
  163. * has been frozen, all I/O devices have been suspended and the only
  164. * activity happens here and in interrupts (if any). In that case bypass
  165. * the cpuidle governor and go straight for the deepest idle state
  166. * available. Possibly also suspend the local tick and the entire
  167. * timekeeping to prevent timer interrupts from kicking us out of idle
  168. * until a proper wakeup interrupt happens.
  169. */
  170. if (idle_should_enter_s2idle() || dev->forced_idle_latency_limit_ns) {
  171. u64 max_latency_ns;
  172. if (idle_should_enter_s2idle()) {
  173. entered_state = call_cpuidle_s2idle(drv, dev);
  174. if (entered_state > 0)
  175. goto exit_idle;
  176. max_latency_ns = U64_MAX;
  177. } else {
  178. max_latency_ns = dev->forced_idle_latency_limit_ns;
  179. }
  180. tick_nohz_idle_stop_tick();
  181. next_state = cpuidle_find_deepest_state(drv, dev, max_latency_ns);
  182. call_cpuidle(drv, dev, next_state);
  183. } else {
  184. bool stop_tick = true;
  185. /*
  186. * Ask the cpuidle framework to choose a convenient idle state.
  187. */
  188. next_state = cpuidle_select(drv, dev, &stop_tick);
  189. if (stop_tick || tick_nohz_tick_stopped())
  190. tick_nohz_idle_stop_tick();
  191. else
  192. tick_nohz_idle_retain_tick();
  193. entered_state = call_cpuidle(drv, dev, next_state);
  194. /*
  195. * Give the governor an opportunity to reflect on the outcome
  196. */
  197. cpuidle_reflect(dev, entered_state);
  198. }
  199. exit_idle:
  200. __current_set_polling();
  201. /*
  202. * It is up to the idle functions to re-enable local interrupts
  203. */
  204. if (WARN_ON_ONCE(irqs_disabled()))
  205. local_irq_enable();
  206. }
  207. /*
  208. * Generic idle loop implementation
  209. *
  210. * Called with polling cleared.
  211. */
  212. static void do_idle(void)
  213. {
  214. int cpu = smp_processor_id();
  215. /*
  216. * Check if we need to update blocked load
  217. */
  218. nohz_run_idle_balance(cpu);
  219. /*
  220. * If the arch has a polling bit, we maintain an invariant:
  221. *
  222. * Our polling bit is clear if we're not scheduled (i.e. if rq->curr !=
  223. * rq->idle). This means that, if rq->idle has the polling bit set,
  224. * then setting need_resched is guaranteed to cause the CPU to
  225. * reschedule.
  226. */
  227. __current_set_polling();
  228. tick_nohz_idle_enter();
  229. while (!need_resched()) {
  230. rmb();
  231. /*
  232. * Interrupts shouldn't be re-enabled from that point on until
  233. * the CPU sleeping instruction is reached. Otherwise an interrupt
  234. * may fire and queue a timer that would be ignored until the CPU
  235. * wakes from the sleeping instruction. And testing need_resched()
  236. * doesn't tell about pending needed timer reprogram.
  237. *
  238. * Several cases to consider:
  239. *
  240. * - SLEEP-UNTIL-PENDING-INTERRUPT based instructions such as
  241. * "wfi" or "mwait" are fine because they can be entered with
  242. * interrupt disabled.
  243. *
  244. * - sti;mwait() couple is fine because the interrupts are
  245. * re-enabled only upon the execution of mwait, leaving no gap
  246. * in-between.
  247. *
  248. * - ROLLBACK based idle handlers with the sleeping instruction
  249. * called with interrupts enabled are NOT fine. In this scheme
  250. * when the interrupt detects it has interrupted an idle handler,
  251. * it rolls back to its beginning which performs the
  252. * need_resched() check before re-executing the sleeping
  253. * instruction. This can leak a pending needed timer reprogram.
  254. * If such a scheme is really mandatory due to the lack of an
  255. * appropriate CPU sleeping instruction, then a FAST-FORWARD
  256. * must instead be applied: when the interrupt detects it has
  257. * interrupted an idle handler, it must resume to the end of
  258. * this idle handler so that the generic idle loop is iterated
  259. * again to reprogram the tick.
  260. */
  261. local_irq_disable();
  262. if (cpu_is_offline(cpu)) {
  263. cpuhp_report_idle_dead();
  264. arch_cpu_idle_dead();
  265. }
  266. arch_cpu_idle_enter();
  267. rcu_nocb_flush_deferred_wakeup();
  268. /*
  269. * In poll mode we re-enable interrupts and spin. Also if we
  270. * detected in the wakeup from idle path that the tick
  271. * broadcast device expired for us, we don't want to go deep
  272. * idle as we know that the IPI is going to arrive right away.
  273. */
  274. if (cpu_idle_force_poll || tick_check_broadcast_expired()) {
  275. tick_nohz_idle_restart_tick();
  276. cpu_idle_poll();
  277. } else {
  278. cpuidle_idle_call();
  279. }
  280. arch_cpu_idle_exit();
  281. }
  282. /*
  283. * Since we fell out of the loop above, we know TIF_NEED_RESCHED must
  284. * be set, propagate it into PREEMPT_NEED_RESCHED.
  285. *
  286. * This is required because for polling idle loops we will not have had
  287. * an IPI to fold the state for us.
  288. */
  289. preempt_set_need_resched();
  290. tick_nohz_idle_exit();
  291. __current_clr_polling();
  292. /*
  293. * We promise to call sched_ttwu_pending() and reschedule if
  294. * need_resched() is set while polling is set. That means that clearing
  295. * polling needs to be visible before doing these things.
  296. */
  297. smp_mb__after_atomic();
  298. /*
  299. * RCU relies on this call to be done outside of an RCU read-side
  300. * critical section.
  301. */
  302. flush_smp_call_function_queue();
  303. schedule_idle();
  304. if (unlikely(klp_patch_pending(current)))
  305. klp_update_patch_state(current);
  306. }
  307. bool cpu_in_idle(unsigned long pc)
  308. {
  309. return pc >= (unsigned long)__cpuidle_text_start &&
  310. pc < (unsigned long)__cpuidle_text_end;
  311. }
  312. struct idle_timer {
  313. struct hrtimer timer;
  314. int done;
  315. };
  316. static enum hrtimer_restart idle_inject_timer_fn(struct hrtimer *timer)
  317. {
  318. struct idle_timer *it = container_of(timer, struct idle_timer, timer);
  319. WRITE_ONCE(it->done, 1);
  320. set_tsk_need_resched(current);
  321. return HRTIMER_NORESTART;
  322. }
  323. void play_idle_precise(u64 duration_ns, u64 latency_ns)
  324. {
  325. struct idle_timer it;
  326. /*
  327. * Only FIFO tasks can disable the tick since they don't need the forced
  328. * preemption.
  329. */
  330. WARN_ON_ONCE(current->policy != SCHED_FIFO);
  331. WARN_ON_ONCE(current->nr_cpus_allowed != 1);
  332. WARN_ON_ONCE(!(current->flags & PF_KTHREAD));
  333. WARN_ON_ONCE(!(current->flags & PF_NO_SETAFFINITY));
  334. WARN_ON_ONCE(!duration_ns);
  335. WARN_ON_ONCE(current->mm);
  336. rcu_sleep_check();
  337. preempt_disable();
  338. current->flags |= PF_IDLE;
  339. cpuidle_use_deepest_state(latency_ns);
  340. it.done = 0;
  341. hrtimer_init_on_stack(&it.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
  342. it.timer.function = idle_inject_timer_fn;
  343. hrtimer_start(&it.timer, ns_to_ktime(duration_ns),
  344. HRTIMER_MODE_REL_PINNED_HARD);
  345. while (!READ_ONCE(it.done))
  346. do_idle();
  347. cpuidle_use_deepest_state(0);
  348. current->flags &= ~PF_IDLE;
  349. preempt_fold_need_resched();
  350. preempt_enable();
  351. }
  352. EXPORT_SYMBOL_GPL(play_idle_precise);
  353. void cpu_startup_entry(enum cpuhp_state state)
  354. {
  355. current->flags |= PF_IDLE;
  356. arch_cpu_idle_prepare();
  357. cpuhp_online_idle(state);
  358. while (1)
  359. do_idle();
  360. }
  361. /*
  362. * idle-task scheduling class.
  363. */
  364. #ifdef CONFIG_SMP
  365. static int
  366. select_task_rq_idle(struct task_struct *p, int cpu, int flags)
  367. {
  368. return task_cpu(p); /* IDLE tasks as never migrated */
  369. }
  370. static int
  371. balance_idle(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
  372. {
  373. return WARN_ON_ONCE(1);
  374. }
  375. #endif
  376. /*
  377. * Idle tasks are unconditionally rescheduled:
  378. */
  379. static void wakeup_preempt_idle(struct rq *rq, struct task_struct *p, int flags)
  380. {
  381. resched_curr(rq);
  382. }
  383. static void put_prev_task_idle(struct rq *rq, struct task_struct *prev, struct task_struct *next)
  384. {
  385. dl_server_update_idle_time(rq, prev);
  386. scx_update_idle(rq, false, true);
  387. }
  388. static void set_next_task_idle(struct rq *rq, struct task_struct *next, bool first)
  389. {
  390. update_idle_core(rq);
  391. scx_update_idle(rq, true, true);
  392. schedstat_inc(rq->sched_goidle);
  393. next->se.exec_start = rq_clock_task(rq);
  394. }
  395. struct task_struct *pick_task_idle(struct rq *rq)
  396. {
  397. scx_update_idle(rq, true, false);
  398. return rq->idle;
  399. }
  400. /*
  401. * It is not legal to sleep in the idle task - print a warning
  402. * message if some code attempts to do it:
  403. */
  404. static bool
  405. dequeue_task_idle(struct rq *rq, struct task_struct *p, int flags)
  406. {
  407. raw_spin_rq_unlock_irq(rq);
  408. printk(KERN_ERR "bad: scheduling from the idle thread!\n");
  409. dump_stack();
  410. raw_spin_rq_lock_irq(rq);
  411. return true;
  412. }
  413. /*
  414. * scheduler tick hitting a task of our scheduling class.
  415. *
  416. * NOTE: This function can be called remotely by the tick offload that
  417. * goes along full dynticks. Therefore no local assumption can be made
  418. * and everything must be accessed through the @rq and @curr passed in
  419. * parameters.
  420. */
  421. static void task_tick_idle(struct rq *rq, struct task_struct *curr, int queued)
  422. {
  423. }
  424. static void switched_to_idle(struct rq *rq, struct task_struct *p)
  425. {
  426. BUG();
  427. }
  428. static void
  429. prio_changed_idle(struct rq *rq, struct task_struct *p, int oldprio)
  430. {
  431. BUG();
  432. }
  433. static void update_curr_idle(struct rq *rq)
  434. {
  435. }
  436. /*
  437. * Simple, special scheduling class for the per-CPU idle tasks:
  438. */
  439. DEFINE_SCHED_CLASS(idle) = {
  440. /* no enqueue/yield_task for idle tasks */
  441. /* dequeue is not valid, we print a debug message there: */
  442. .dequeue_task = dequeue_task_idle,
  443. .wakeup_preempt = wakeup_preempt_idle,
  444. .pick_task = pick_task_idle,
  445. .put_prev_task = put_prev_task_idle,
  446. .set_next_task = set_next_task_idle,
  447. #ifdef CONFIG_SMP
  448. .balance = balance_idle,
  449. .select_task_rq = select_task_rq_idle,
  450. .set_cpus_allowed = set_cpus_allowed_common,
  451. #endif
  452. .task_tick = task_tick_idle,
  453. .prio_changed = prio_changed_idle,
  454. .switched_to = switched_to_idle,
  455. .update_curr = update_curr_idle,
  456. };