tick-sched.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  1. /*
  2. * linux/kernel/time/tick-sched.c
  3. *
  4. * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
  5. * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
  6. * Copyright(C) 2006-2007 Timesys Corp., Thomas Gleixner
  7. *
  8. * No idle tick implementation for low and high resolution timers
  9. *
  10. * Started by: Thomas Gleixner and Ingo Molnar
  11. *
  12. * Distribute under GPLv2.
  13. */
  14. #include <linux/cpu.h>
  15. #include <linux/err.h>
  16. #include <linux/hrtimer.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/kernel_stat.h>
  19. #include <linux/percpu.h>
  20. #include <linux/profile.h>
  21. #include <linux/sched.h>
  22. #include <linux/module.h>
  23. #include <linux/irq_work.h>
  24. #include <linux/posix-timers.h>
  25. #include <linux/perf_event.h>
  26. #include <linux/context_tracking.h>
  27. #include <asm/irq_regs.h>
  28. #include "tick-internal.h"
  29. #include <trace/events/timer.h>
  30. /*
  31. * Per cpu nohz control structure
  32. */
  33. static DEFINE_PER_CPU(struct tick_sched, tick_cpu_sched);
  34. /*
  35. * The time, when the last jiffy update happened. Protected by jiffies_lock.
  36. */
  37. static ktime_t last_jiffies_update;
  38. struct tick_sched *tick_get_tick_sched(int cpu)
  39. {
  40. return &per_cpu(tick_cpu_sched, cpu);
  41. }
  42. /*
  43. * Must be called with interrupts disabled !
  44. */
  45. static void tick_do_update_jiffies64(ktime_t now)
  46. {
  47. unsigned long ticks = 0;
  48. ktime_t delta;
  49. /*
  50. * Do a quick check without holding jiffies_lock:
  51. */
  52. delta = ktime_sub(now, last_jiffies_update);
  53. if (delta.tv64 < tick_period.tv64)
  54. return;
  55. /* Reevalute with jiffies_lock held */
  56. write_seqlock(&jiffies_lock);
  57. delta = ktime_sub(now, last_jiffies_update);
  58. if (delta.tv64 >= tick_period.tv64) {
  59. delta = ktime_sub(delta, tick_period);
  60. last_jiffies_update = ktime_add(last_jiffies_update,
  61. tick_period);
  62. /* Slow path for long timeouts */
  63. if (unlikely(delta.tv64 >= tick_period.tv64)) {
  64. s64 incr = ktime_to_ns(tick_period);
  65. ticks = ktime_divns(delta, incr);
  66. last_jiffies_update = ktime_add_ns(last_jiffies_update,
  67. incr * ticks);
  68. }
  69. do_timer(++ticks);
  70. /* Keep the tick_next_period variable up to date */
  71. tick_next_period = ktime_add(last_jiffies_update, tick_period);
  72. } else {
  73. write_sequnlock(&jiffies_lock);
  74. return;
  75. }
  76. write_sequnlock(&jiffies_lock);
  77. update_wall_time();
  78. }
  79. /*
  80. * Initialize and return retrieve the jiffies update.
  81. */
  82. static ktime_t tick_init_jiffy_update(void)
  83. {
  84. ktime_t period;
  85. write_seqlock(&jiffies_lock);
  86. /* Did we start the jiffies update yet ? */
  87. if (last_jiffies_update.tv64 == 0)
  88. last_jiffies_update = tick_next_period;
  89. period = last_jiffies_update;
  90. write_sequnlock(&jiffies_lock);
  91. return period;
  92. }
  93. static void tick_sched_do_timer(ktime_t now)
  94. {
  95. int cpu = smp_processor_id();
  96. #ifdef CONFIG_NO_HZ_COMMON
  97. /*
  98. * Check if the do_timer duty was dropped. We don't care about
  99. * concurrency: This happens only when the cpu in charge went
  100. * into a long sleep. If two cpus happen to assign themself to
  101. * this duty, then the jiffies update is still serialized by
  102. * jiffies_lock.
  103. */
  104. if (unlikely(tick_do_timer_cpu == TICK_DO_TIMER_NONE)
  105. && !tick_nohz_full_cpu(cpu))
  106. tick_do_timer_cpu = cpu;
  107. #endif
  108. /* Check, if the jiffies need an update */
  109. if (tick_do_timer_cpu == cpu)
  110. tick_do_update_jiffies64(now);
  111. }
  112. static void tick_sched_handle(struct tick_sched *ts, struct pt_regs *regs)
  113. {
  114. #ifdef CONFIG_NO_HZ_COMMON
  115. /*
  116. * When we are idle and the tick is stopped, we have to touch
  117. * the watchdog as we might not schedule for a really long
  118. * time. This happens on complete idle SMP systems while
  119. * waiting on the login prompt. We also increment the "start of
  120. * idle" jiffy stamp so the idle accounting adjustment we do
  121. * when we go busy again does not account too much ticks.
  122. */
  123. if (ts->tick_stopped) {
  124. touch_softlockup_watchdog();
  125. if (is_idle_task(current))
  126. ts->idle_jiffies++;
  127. }
  128. #endif
  129. update_process_times(user_mode(regs));
  130. profile_tick(CPU_PROFILING);
  131. }
  132. #ifdef CONFIG_NO_HZ_FULL
  133. cpumask_var_t tick_nohz_full_mask;
  134. cpumask_var_t housekeeping_mask;
  135. bool tick_nohz_full_running;
  136. static bool can_stop_full_tick(void)
  137. {
  138. WARN_ON_ONCE(!irqs_disabled());
  139. if (!sched_can_stop_tick()) {
  140. trace_tick_stop(0, "more than 1 task in runqueue\n");
  141. return false;
  142. }
  143. if (!posix_cpu_timers_can_stop_tick(current)) {
  144. trace_tick_stop(0, "posix timers running\n");
  145. return false;
  146. }
  147. if (!perf_event_can_stop_tick()) {
  148. trace_tick_stop(0, "perf events running\n");
  149. return false;
  150. }
  151. /* sched_clock_tick() needs us? */
  152. #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
  153. /*
  154. * TODO: kick full dynticks CPUs when
  155. * sched_clock_stable is set.
  156. */
  157. if (!sched_clock_stable()) {
  158. trace_tick_stop(0, "unstable sched clock\n");
  159. /*
  160. * Don't allow the user to think they can get
  161. * full NO_HZ with this machine.
  162. */
  163. WARN_ONCE(tick_nohz_full_running,
  164. "NO_HZ FULL will not work with unstable sched clock");
  165. return false;
  166. }
  167. #endif
  168. return true;
  169. }
  170. static void nohz_full_kick_work_func(struct irq_work *work)
  171. {
  172. /* Empty, the tick restart happens on tick_nohz_irq_exit() */
  173. }
  174. static DEFINE_PER_CPU(struct irq_work, nohz_full_kick_work) = {
  175. .func = nohz_full_kick_work_func,
  176. };
  177. /*
  178. * Kick this CPU if it's full dynticks in order to force it to
  179. * re-evaluate its dependency on the tick and restart it if necessary.
  180. * This kick, unlike tick_nohz_full_kick_cpu() and tick_nohz_full_kick_all(),
  181. * is NMI safe.
  182. */
  183. void tick_nohz_full_kick(void)
  184. {
  185. if (!tick_nohz_full_cpu(smp_processor_id()))
  186. return;
  187. irq_work_queue(this_cpu_ptr(&nohz_full_kick_work));
  188. }
  189. /*
  190. * Kick the CPU if it's full dynticks in order to force it to
  191. * re-evaluate its dependency on the tick and restart it if necessary.
  192. */
  193. void tick_nohz_full_kick_cpu(int cpu)
  194. {
  195. if (!tick_nohz_full_cpu(cpu))
  196. return;
  197. irq_work_queue_on(&per_cpu(nohz_full_kick_work, cpu), cpu);
  198. }
  199. static void nohz_full_kick_ipi(void *info)
  200. {
  201. /* Empty, the tick restart happens on tick_nohz_irq_exit() */
  202. }
  203. /*
  204. * Kick all full dynticks CPUs in order to force these to re-evaluate
  205. * their dependency on the tick and restart it if necessary.
  206. */
  207. void tick_nohz_full_kick_all(void)
  208. {
  209. if (!tick_nohz_full_running)
  210. return;
  211. preempt_disable();
  212. smp_call_function_many(tick_nohz_full_mask,
  213. nohz_full_kick_ipi, NULL, false);
  214. tick_nohz_full_kick();
  215. preempt_enable();
  216. }
  217. /*
  218. * Re-evaluate the need for the tick as we switch the current task.
  219. * It might need the tick due to per task/process properties:
  220. * perf events, posix cpu timers, ...
  221. */
  222. void __tick_nohz_task_switch(void)
  223. {
  224. unsigned long flags;
  225. local_irq_save(flags);
  226. if (!tick_nohz_full_cpu(smp_processor_id()))
  227. goto out;
  228. if (tick_nohz_tick_stopped() && !can_stop_full_tick())
  229. tick_nohz_full_kick();
  230. out:
  231. local_irq_restore(flags);
  232. }
  233. /* Parse the boot-time nohz CPU list from the kernel parameters. */
  234. static int __init tick_nohz_full_setup(char *str)
  235. {
  236. alloc_bootmem_cpumask_var(&tick_nohz_full_mask);
  237. if (cpulist_parse(str, tick_nohz_full_mask) < 0) {
  238. pr_warning("NOHZ: Incorrect nohz_full cpumask\n");
  239. free_bootmem_cpumask_var(tick_nohz_full_mask);
  240. return 1;
  241. }
  242. tick_nohz_full_running = true;
  243. return 1;
  244. }
  245. __setup("nohz_full=", tick_nohz_full_setup);
  246. static int tick_nohz_cpu_down_callback(struct notifier_block *nfb,
  247. unsigned long action,
  248. void *hcpu)
  249. {
  250. unsigned int cpu = (unsigned long)hcpu;
  251. switch (action & ~CPU_TASKS_FROZEN) {
  252. case CPU_DOWN_PREPARE:
  253. /*
  254. * If we handle the timekeeping duty for full dynticks CPUs,
  255. * we can't safely shutdown that CPU.
  256. */
  257. if (tick_nohz_full_running && tick_do_timer_cpu == cpu)
  258. return NOTIFY_BAD;
  259. break;
  260. }
  261. return NOTIFY_OK;
  262. }
  263. static int tick_nohz_init_all(void)
  264. {
  265. int err = -1;
  266. #ifdef CONFIG_NO_HZ_FULL_ALL
  267. if (!alloc_cpumask_var(&tick_nohz_full_mask, GFP_KERNEL)) {
  268. WARN(1, "NO_HZ: Can't allocate full dynticks cpumask\n");
  269. return err;
  270. }
  271. err = 0;
  272. cpumask_setall(tick_nohz_full_mask);
  273. tick_nohz_full_running = true;
  274. #endif
  275. return err;
  276. }
  277. void __init tick_nohz_init(void)
  278. {
  279. int cpu;
  280. if (!tick_nohz_full_running) {
  281. if (tick_nohz_init_all() < 0)
  282. return;
  283. }
  284. if (!alloc_cpumask_var(&housekeeping_mask, GFP_KERNEL)) {
  285. WARN(1, "NO_HZ: Can't allocate not-full dynticks cpumask\n");
  286. cpumask_clear(tick_nohz_full_mask);
  287. tick_nohz_full_running = false;
  288. return;
  289. }
  290. /*
  291. * Full dynticks uses irq work to drive the tick rescheduling on safe
  292. * locking contexts. But then we need irq work to raise its own
  293. * interrupts to avoid circular dependency on the tick
  294. */
  295. if (!arch_irq_work_has_interrupt()) {
  296. pr_warning("NO_HZ: Can't run full dynticks because arch doesn't "
  297. "support irq work self-IPIs\n");
  298. cpumask_clear(tick_nohz_full_mask);
  299. cpumask_copy(housekeeping_mask, cpu_possible_mask);
  300. tick_nohz_full_running = false;
  301. return;
  302. }
  303. cpu = smp_processor_id();
  304. if (cpumask_test_cpu(cpu, tick_nohz_full_mask)) {
  305. pr_warning("NO_HZ: Clearing %d from nohz_full range for timekeeping\n", cpu);
  306. cpumask_clear_cpu(cpu, tick_nohz_full_mask);
  307. }
  308. cpumask_andnot(housekeeping_mask,
  309. cpu_possible_mask, tick_nohz_full_mask);
  310. for_each_cpu(cpu, tick_nohz_full_mask)
  311. context_tracking_cpu_set(cpu);
  312. cpu_notifier(tick_nohz_cpu_down_callback, 0);
  313. pr_info("NO_HZ: Full dynticks CPUs: %*pbl.\n",
  314. cpumask_pr_args(tick_nohz_full_mask));
  315. }
  316. #endif
  317. /*
  318. * NOHZ - aka dynamic tick functionality
  319. */
  320. #ifdef CONFIG_NO_HZ_COMMON
  321. /*
  322. * NO HZ enabled ?
  323. */
  324. static int tick_nohz_enabled __read_mostly = 1;
  325. unsigned long tick_nohz_active __read_mostly;
  326. /*
  327. * Enable / Disable tickless mode
  328. */
  329. static int __init setup_tick_nohz(char *str)
  330. {
  331. if (!strcmp(str, "off"))
  332. tick_nohz_enabled = 0;
  333. else if (!strcmp(str, "on"))
  334. tick_nohz_enabled = 1;
  335. else
  336. return 0;
  337. return 1;
  338. }
  339. __setup("nohz=", setup_tick_nohz);
  340. int tick_nohz_tick_stopped(void)
  341. {
  342. return __this_cpu_read(tick_cpu_sched.tick_stopped);
  343. }
  344. /**
  345. * tick_nohz_update_jiffies - update jiffies when idle was interrupted
  346. *
  347. * Called from interrupt entry when the CPU was idle
  348. *
  349. * In case the sched_tick was stopped on this CPU, we have to check if jiffies
  350. * must be updated. Otherwise an interrupt handler could use a stale jiffy
  351. * value. We do this unconditionally on any cpu, as we don't know whether the
  352. * cpu, which has the update task assigned is in a long sleep.
  353. */
  354. static void tick_nohz_update_jiffies(ktime_t now)
  355. {
  356. unsigned long flags;
  357. __this_cpu_write(tick_cpu_sched.idle_waketime, now);
  358. local_irq_save(flags);
  359. tick_do_update_jiffies64(now);
  360. local_irq_restore(flags);
  361. touch_softlockup_watchdog();
  362. }
  363. /*
  364. * Updates the per cpu time idle statistics counters
  365. */
  366. static void
  367. update_ts_time_stats(int cpu, struct tick_sched *ts, ktime_t now, u64 *last_update_time)
  368. {
  369. ktime_t delta;
  370. if (ts->idle_active) {
  371. delta = ktime_sub(now, ts->idle_entrytime);
  372. if (nr_iowait_cpu(cpu) > 0)
  373. ts->iowait_sleeptime = ktime_add(ts->iowait_sleeptime, delta);
  374. else
  375. ts->idle_sleeptime = ktime_add(ts->idle_sleeptime, delta);
  376. ts->idle_entrytime = now;
  377. }
  378. if (last_update_time)
  379. *last_update_time = ktime_to_us(now);
  380. }
  381. static void tick_nohz_stop_idle(struct tick_sched *ts, ktime_t now)
  382. {
  383. update_ts_time_stats(smp_processor_id(), ts, now, NULL);
  384. ts->idle_active = 0;
  385. sched_clock_idle_wakeup_event(0);
  386. }
  387. static ktime_t tick_nohz_start_idle(struct tick_sched *ts)
  388. {
  389. ktime_t now = ktime_get();
  390. ts->idle_entrytime = now;
  391. ts->idle_active = 1;
  392. sched_clock_idle_sleep_event();
  393. return now;
  394. }
  395. /**
  396. * get_cpu_idle_time_us - get the total idle time of a cpu
  397. * @cpu: CPU number to query
  398. * @last_update_time: variable to store update time in. Do not update
  399. * counters if NULL.
  400. *
  401. * Return the cummulative idle time (since boot) for a given
  402. * CPU, in microseconds.
  403. *
  404. * This time is measured via accounting rather than sampling,
  405. * and is as accurate as ktime_get() is.
  406. *
  407. * This function returns -1 if NOHZ is not enabled.
  408. */
  409. u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time)
  410. {
  411. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  412. ktime_t now, idle;
  413. if (!tick_nohz_active)
  414. return -1;
  415. now = ktime_get();
  416. if (last_update_time) {
  417. update_ts_time_stats(cpu, ts, now, last_update_time);
  418. idle = ts->idle_sleeptime;
  419. } else {
  420. if (ts->idle_active && !nr_iowait_cpu(cpu)) {
  421. ktime_t delta = ktime_sub(now, ts->idle_entrytime);
  422. idle = ktime_add(ts->idle_sleeptime, delta);
  423. } else {
  424. idle = ts->idle_sleeptime;
  425. }
  426. }
  427. return ktime_to_us(idle);
  428. }
  429. EXPORT_SYMBOL_GPL(get_cpu_idle_time_us);
  430. /**
  431. * get_cpu_iowait_time_us - get the total iowait time of a cpu
  432. * @cpu: CPU number to query
  433. * @last_update_time: variable to store update time in. Do not update
  434. * counters if NULL.
  435. *
  436. * Return the cummulative iowait time (since boot) for a given
  437. * CPU, in microseconds.
  438. *
  439. * This time is measured via accounting rather than sampling,
  440. * and is as accurate as ktime_get() is.
  441. *
  442. * This function returns -1 if NOHZ is not enabled.
  443. */
  444. u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time)
  445. {
  446. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  447. ktime_t now, iowait;
  448. if (!tick_nohz_active)
  449. return -1;
  450. now = ktime_get();
  451. if (last_update_time) {
  452. update_ts_time_stats(cpu, ts, now, last_update_time);
  453. iowait = ts->iowait_sleeptime;
  454. } else {
  455. if (ts->idle_active && nr_iowait_cpu(cpu) > 0) {
  456. ktime_t delta = ktime_sub(now, ts->idle_entrytime);
  457. iowait = ktime_add(ts->iowait_sleeptime, delta);
  458. } else {
  459. iowait = ts->iowait_sleeptime;
  460. }
  461. }
  462. return ktime_to_us(iowait);
  463. }
  464. EXPORT_SYMBOL_GPL(get_cpu_iowait_time_us);
  465. static void tick_nohz_restart(struct tick_sched *ts, ktime_t now)
  466. {
  467. hrtimer_cancel(&ts->sched_timer);
  468. hrtimer_set_expires(&ts->sched_timer, ts->last_tick);
  469. /* Forward the time to expire in the future */
  470. hrtimer_forward(&ts->sched_timer, now, tick_period);
  471. if (ts->nohz_mode == NOHZ_MODE_HIGHRES)
  472. hrtimer_start_expires(&ts->sched_timer, HRTIMER_MODE_ABS_PINNED);
  473. else
  474. tick_program_event(hrtimer_get_expires(&ts->sched_timer), 1);
  475. }
  476. static ktime_t tick_nohz_stop_sched_tick(struct tick_sched *ts,
  477. ktime_t now, int cpu)
  478. {
  479. struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
  480. u64 basemono, next_tick, next_tmr, next_rcu, delta, expires;
  481. unsigned long seq, basejiff;
  482. ktime_t tick;
  483. /* Read jiffies and the time when jiffies were updated last */
  484. do {
  485. seq = read_seqbegin(&jiffies_lock);
  486. basemono = last_jiffies_update.tv64;
  487. basejiff = jiffies;
  488. } while (read_seqretry(&jiffies_lock, seq));
  489. ts->last_jiffies = basejiff;
  490. if (rcu_needs_cpu(basemono, &next_rcu) ||
  491. arch_needs_cpu() || irq_work_needs_cpu()) {
  492. next_tick = basemono + TICK_NSEC;
  493. } else {
  494. /*
  495. * Get the next pending timer. If high resolution
  496. * timers are enabled this only takes the timer wheel
  497. * timers into account. If high resolution timers are
  498. * disabled this also looks at the next expiring
  499. * hrtimer.
  500. */
  501. next_tmr = get_next_timer_interrupt(basejiff, basemono);
  502. ts->next_timer = next_tmr;
  503. /* Take the next rcu event into account */
  504. next_tick = next_rcu < next_tmr ? next_rcu : next_tmr;
  505. }
  506. /*
  507. * If the tick is due in the next period, keep it ticking or
  508. * restart it proper.
  509. */
  510. delta = next_tick - basemono;
  511. if (delta <= (u64)TICK_NSEC) {
  512. tick.tv64 = 0;
  513. if (!ts->tick_stopped)
  514. goto out;
  515. if (delta == 0) {
  516. /* Tick is stopped, but required now. Enforce it */
  517. tick_nohz_restart(ts, now);
  518. goto out;
  519. }
  520. }
  521. /*
  522. * If this cpu is the one which updates jiffies, then give up
  523. * the assignment and let it be taken by the cpu which runs
  524. * the tick timer next, which might be this cpu as well. If we
  525. * don't drop this here the jiffies might be stale and
  526. * do_timer() never invoked. Keep track of the fact that it
  527. * was the one which had the do_timer() duty last. If this cpu
  528. * is the one which had the do_timer() duty last, we limit the
  529. * sleep time to the timekeeping max_deferement value.
  530. * Otherwise we can sleep as long as we want.
  531. */
  532. delta = timekeeping_max_deferment();
  533. if (cpu == tick_do_timer_cpu) {
  534. tick_do_timer_cpu = TICK_DO_TIMER_NONE;
  535. ts->do_timer_last = 1;
  536. } else if (tick_do_timer_cpu != TICK_DO_TIMER_NONE) {
  537. delta = KTIME_MAX;
  538. ts->do_timer_last = 0;
  539. } else if (!ts->do_timer_last) {
  540. delta = KTIME_MAX;
  541. }
  542. #ifdef CONFIG_NO_HZ_FULL
  543. /* Limit the tick delta to the maximum scheduler deferment */
  544. if (!ts->inidle)
  545. delta = min(delta, scheduler_tick_max_deferment());
  546. #endif
  547. /* Calculate the next expiry time */
  548. if (delta < (KTIME_MAX - basemono))
  549. expires = basemono + delta;
  550. else
  551. expires = KTIME_MAX;
  552. expires = min_t(u64, expires, next_tick);
  553. tick.tv64 = expires;
  554. /* Skip reprogram of event if its not changed */
  555. if (ts->tick_stopped && (expires == dev->next_event.tv64))
  556. goto out;
  557. /*
  558. * nohz_stop_sched_tick can be called several times before
  559. * the nohz_restart_sched_tick is called. This happens when
  560. * interrupts arrive which do not cause a reschedule. In the
  561. * first call we save the current tick time, so we can restart
  562. * the scheduler tick in nohz_restart_sched_tick.
  563. */
  564. if (!ts->tick_stopped) {
  565. nohz_balance_enter_idle(cpu);
  566. calc_load_enter_idle();
  567. ts->last_tick = hrtimer_get_expires(&ts->sched_timer);
  568. ts->tick_stopped = 1;
  569. trace_tick_stop(1, " ");
  570. }
  571. /*
  572. * If the expiration time == KTIME_MAX, then we simply stop
  573. * the tick timer.
  574. */
  575. if (unlikely(expires == KTIME_MAX)) {
  576. if (ts->nohz_mode == NOHZ_MODE_HIGHRES)
  577. hrtimer_cancel(&ts->sched_timer);
  578. goto out;
  579. }
  580. if (ts->nohz_mode == NOHZ_MODE_HIGHRES)
  581. hrtimer_start(&ts->sched_timer, tick, HRTIMER_MODE_ABS_PINNED);
  582. else
  583. tick_program_event(tick, 1);
  584. out:
  585. /* Update the estimated sleep length */
  586. ts->sleep_length = ktime_sub(dev->next_event, now);
  587. return tick;
  588. }
  589. static void tick_nohz_restart_sched_tick(struct tick_sched *ts, ktime_t now)
  590. {
  591. /* Update jiffies first */
  592. tick_do_update_jiffies64(now);
  593. update_cpu_load_nohz();
  594. calc_load_exit_idle();
  595. touch_softlockup_watchdog();
  596. /*
  597. * Cancel the scheduled timer and restore the tick
  598. */
  599. ts->tick_stopped = 0;
  600. ts->idle_exittime = now;
  601. tick_nohz_restart(ts, now);
  602. }
  603. static void tick_nohz_full_update_tick(struct tick_sched *ts)
  604. {
  605. #ifdef CONFIG_NO_HZ_FULL
  606. int cpu = smp_processor_id();
  607. if (!tick_nohz_full_cpu(cpu))
  608. return;
  609. if (!ts->tick_stopped && ts->nohz_mode == NOHZ_MODE_INACTIVE)
  610. return;
  611. if (can_stop_full_tick())
  612. tick_nohz_stop_sched_tick(ts, ktime_get(), cpu);
  613. else if (ts->tick_stopped)
  614. tick_nohz_restart_sched_tick(ts, ktime_get());
  615. #endif
  616. }
  617. static bool can_stop_idle_tick(int cpu, struct tick_sched *ts)
  618. {
  619. /*
  620. * If this cpu is offline and it is the one which updates
  621. * jiffies, then give up the assignment and let it be taken by
  622. * the cpu which runs the tick timer next. If we don't drop
  623. * this here the jiffies might be stale and do_timer() never
  624. * invoked.
  625. */
  626. if (unlikely(!cpu_online(cpu))) {
  627. if (cpu == tick_do_timer_cpu)
  628. tick_do_timer_cpu = TICK_DO_TIMER_NONE;
  629. return false;
  630. }
  631. if (unlikely(ts->nohz_mode == NOHZ_MODE_INACTIVE)) {
  632. ts->sleep_length = (ktime_t) { .tv64 = NSEC_PER_SEC/HZ };
  633. return false;
  634. }
  635. if (need_resched())
  636. return false;
  637. if (unlikely(local_softirq_pending() && cpu_online(cpu))) {
  638. static int ratelimit;
  639. if (ratelimit < 10 &&
  640. (local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK)) {
  641. pr_warn("NOHZ: local_softirq_pending %02x\n",
  642. (unsigned int) local_softirq_pending());
  643. ratelimit++;
  644. }
  645. return false;
  646. }
  647. if (tick_nohz_full_enabled()) {
  648. /*
  649. * Keep the tick alive to guarantee timekeeping progression
  650. * if there are full dynticks CPUs around
  651. */
  652. if (tick_do_timer_cpu == cpu)
  653. return false;
  654. /*
  655. * Boot safety: make sure the timekeeping duty has been
  656. * assigned before entering dyntick-idle mode,
  657. */
  658. if (tick_do_timer_cpu == TICK_DO_TIMER_NONE)
  659. return false;
  660. }
  661. return true;
  662. }
  663. static void __tick_nohz_idle_enter(struct tick_sched *ts)
  664. {
  665. ktime_t now, expires;
  666. int cpu = smp_processor_id();
  667. now = tick_nohz_start_idle(ts);
  668. if (can_stop_idle_tick(cpu, ts)) {
  669. int was_stopped = ts->tick_stopped;
  670. ts->idle_calls++;
  671. expires = tick_nohz_stop_sched_tick(ts, now, cpu);
  672. if (expires.tv64 > 0LL) {
  673. ts->idle_sleeps++;
  674. ts->idle_expires = expires;
  675. }
  676. if (!was_stopped && ts->tick_stopped)
  677. ts->idle_jiffies = ts->last_jiffies;
  678. }
  679. }
  680. /**
  681. * tick_nohz_idle_enter - stop the idle tick from the idle task
  682. *
  683. * When the next event is more than a tick into the future, stop the idle tick
  684. * Called when we start the idle loop.
  685. *
  686. * The arch is responsible of calling:
  687. *
  688. * - rcu_idle_enter() after its last use of RCU before the CPU is put
  689. * to sleep.
  690. * - rcu_idle_exit() before the first use of RCU after the CPU is woken up.
  691. */
  692. void tick_nohz_idle_enter(void)
  693. {
  694. struct tick_sched *ts;
  695. WARN_ON_ONCE(irqs_disabled());
  696. /*
  697. * Update the idle state in the scheduler domain hierarchy
  698. * when tick_nohz_stop_sched_tick() is called from the idle loop.
  699. * State will be updated to busy during the first busy tick after
  700. * exiting idle.
  701. */
  702. set_cpu_sd_state_idle();
  703. local_irq_disable();
  704. ts = this_cpu_ptr(&tick_cpu_sched);
  705. ts->inidle = 1;
  706. __tick_nohz_idle_enter(ts);
  707. local_irq_enable();
  708. }
  709. /**
  710. * tick_nohz_irq_exit - update next tick event from interrupt exit
  711. *
  712. * When an interrupt fires while we are idle and it doesn't cause
  713. * a reschedule, it may still add, modify or delete a timer, enqueue
  714. * an RCU callback, etc...
  715. * So we need to re-calculate and reprogram the next tick event.
  716. */
  717. void tick_nohz_irq_exit(void)
  718. {
  719. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  720. if (ts->inidle)
  721. __tick_nohz_idle_enter(ts);
  722. else
  723. tick_nohz_full_update_tick(ts);
  724. }
  725. /**
  726. * tick_nohz_get_sleep_length - return the length of the current sleep
  727. *
  728. * Called from power state control code with interrupts disabled
  729. */
  730. ktime_t tick_nohz_get_sleep_length(void)
  731. {
  732. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  733. return ts->sleep_length;
  734. }
  735. static void tick_nohz_account_idle_ticks(struct tick_sched *ts)
  736. {
  737. #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
  738. unsigned long ticks;
  739. if (vtime_accounting_enabled())
  740. return;
  741. /*
  742. * We stopped the tick in idle. Update process times would miss the
  743. * time we slept as update_process_times does only a 1 tick
  744. * accounting. Enforce that this is accounted to idle !
  745. */
  746. ticks = jiffies - ts->idle_jiffies;
  747. /*
  748. * We might be one off. Do not randomly account a huge number of ticks!
  749. */
  750. if (ticks && ticks < LONG_MAX)
  751. account_idle_ticks(ticks);
  752. #endif
  753. }
  754. /**
  755. * tick_nohz_idle_exit - restart the idle tick from the idle task
  756. *
  757. * Restart the idle tick when the CPU is woken up from idle
  758. * This also exit the RCU extended quiescent state. The CPU
  759. * can use RCU again after this function is called.
  760. */
  761. void tick_nohz_idle_exit(void)
  762. {
  763. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  764. ktime_t now;
  765. local_irq_disable();
  766. WARN_ON_ONCE(!ts->inidle);
  767. ts->inidle = 0;
  768. if (ts->idle_active || ts->tick_stopped)
  769. now = ktime_get();
  770. if (ts->idle_active)
  771. tick_nohz_stop_idle(ts, now);
  772. if (ts->tick_stopped) {
  773. tick_nohz_restart_sched_tick(ts, now);
  774. tick_nohz_account_idle_ticks(ts);
  775. }
  776. local_irq_enable();
  777. }
  778. /*
  779. * The nohz low res interrupt handler
  780. */
  781. static void tick_nohz_handler(struct clock_event_device *dev)
  782. {
  783. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  784. struct pt_regs *regs = get_irq_regs();
  785. ktime_t now = ktime_get();
  786. dev->next_event.tv64 = KTIME_MAX;
  787. tick_sched_do_timer(now);
  788. tick_sched_handle(ts, regs);
  789. /* No need to reprogram if we are running tickless */
  790. if (unlikely(ts->tick_stopped))
  791. return;
  792. hrtimer_forward(&ts->sched_timer, now, tick_period);
  793. tick_program_event(hrtimer_get_expires(&ts->sched_timer), 1);
  794. }
  795. static inline void tick_nohz_activate(struct tick_sched *ts, int mode)
  796. {
  797. if (!tick_nohz_enabled)
  798. return;
  799. ts->nohz_mode = mode;
  800. /* One update is enough */
  801. if (!test_and_set_bit(0, &tick_nohz_active))
  802. timers_update_migration(true);
  803. }
  804. /**
  805. * tick_nohz_switch_to_nohz - switch to nohz mode
  806. */
  807. static void tick_nohz_switch_to_nohz(void)
  808. {
  809. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  810. ktime_t next;
  811. if (!tick_nohz_enabled)
  812. return;
  813. if (tick_switch_to_oneshot(tick_nohz_handler))
  814. return;
  815. /*
  816. * Recycle the hrtimer in ts, so we can share the
  817. * hrtimer_forward with the highres code.
  818. */
  819. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  820. /* Get the next period */
  821. next = tick_init_jiffy_update();
  822. hrtimer_forward_now(&ts->sched_timer, tick_period);
  823. hrtimer_set_expires(&ts->sched_timer, next);
  824. tick_program_event(next, 1);
  825. tick_nohz_activate(ts, NOHZ_MODE_LOWRES);
  826. }
  827. /*
  828. * When NOHZ is enabled and the tick is stopped, we need to kick the
  829. * tick timer from irq_enter() so that the jiffies update is kept
  830. * alive during long running softirqs. That's ugly as hell, but
  831. * correctness is key even if we need to fix the offending softirq in
  832. * the first place.
  833. *
  834. * Note, this is different to tick_nohz_restart. We just kick the
  835. * timer and do not touch the other magic bits which need to be done
  836. * when idle is left.
  837. */
  838. static void tick_nohz_kick_tick(struct tick_sched *ts, ktime_t now)
  839. {
  840. #if 0
  841. /* Switch back to 2.6.27 behaviour */
  842. ktime_t delta;
  843. /*
  844. * Do not touch the tick device, when the next expiry is either
  845. * already reached or less/equal than the tick period.
  846. */
  847. delta = ktime_sub(hrtimer_get_expires(&ts->sched_timer), now);
  848. if (delta.tv64 <= tick_period.tv64)
  849. return;
  850. tick_nohz_restart(ts, now);
  851. #endif
  852. }
  853. static inline void tick_nohz_irq_enter(void)
  854. {
  855. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  856. ktime_t now;
  857. if (!ts->idle_active && !ts->tick_stopped)
  858. return;
  859. now = ktime_get();
  860. if (ts->idle_active)
  861. tick_nohz_stop_idle(ts, now);
  862. if (ts->tick_stopped) {
  863. tick_nohz_update_jiffies(now);
  864. tick_nohz_kick_tick(ts, now);
  865. }
  866. }
  867. #else
  868. static inline void tick_nohz_switch_to_nohz(void) { }
  869. static inline void tick_nohz_irq_enter(void) { }
  870. static inline void tick_nohz_activate(struct tick_sched *ts, int mode) { }
  871. #endif /* CONFIG_NO_HZ_COMMON */
  872. /*
  873. * Called from irq_enter to notify about the possible interruption of idle()
  874. */
  875. void tick_irq_enter(void)
  876. {
  877. tick_check_oneshot_broadcast_this_cpu();
  878. tick_nohz_irq_enter();
  879. }
  880. /*
  881. * High resolution timer specific code
  882. */
  883. #ifdef CONFIG_HIGH_RES_TIMERS
  884. /*
  885. * We rearm the timer until we get disabled by the idle code.
  886. * Called with interrupts disabled.
  887. */
  888. static enum hrtimer_restart tick_sched_timer(struct hrtimer *timer)
  889. {
  890. struct tick_sched *ts =
  891. container_of(timer, struct tick_sched, sched_timer);
  892. struct pt_regs *regs = get_irq_regs();
  893. ktime_t now = ktime_get();
  894. tick_sched_do_timer(now);
  895. /*
  896. * Do not call, when we are not in irq context and have
  897. * no valid regs pointer
  898. */
  899. if (regs)
  900. tick_sched_handle(ts, regs);
  901. /* No need to reprogram if we are in idle or full dynticks mode */
  902. if (unlikely(ts->tick_stopped))
  903. return HRTIMER_NORESTART;
  904. hrtimer_forward(timer, now, tick_period);
  905. return HRTIMER_RESTART;
  906. }
  907. static int sched_skew_tick;
  908. static int __init skew_tick(char *str)
  909. {
  910. get_option(&str, &sched_skew_tick);
  911. return 0;
  912. }
  913. early_param("skew_tick", skew_tick);
  914. /**
  915. * tick_setup_sched_timer - setup the tick emulation timer
  916. */
  917. void tick_setup_sched_timer(void)
  918. {
  919. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  920. ktime_t now = ktime_get();
  921. /*
  922. * Emulate tick processing via per-CPU hrtimers:
  923. */
  924. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  925. ts->sched_timer.function = tick_sched_timer;
  926. /* Get the next period (per cpu) */
  927. hrtimer_set_expires(&ts->sched_timer, tick_init_jiffy_update());
  928. /* Offset the tick to avert jiffies_lock contention. */
  929. if (sched_skew_tick) {
  930. u64 offset = ktime_to_ns(tick_period) >> 1;
  931. do_div(offset, num_possible_cpus());
  932. offset *= smp_processor_id();
  933. hrtimer_add_expires_ns(&ts->sched_timer, offset);
  934. }
  935. hrtimer_forward(&ts->sched_timer, now, tick_period);
  936. hrtimer_start_expires(&ts->sched_timer, HRTIMER_MODE_ABS_PINNED);
  937. tick_nohz_activate(ts, NOHZ_MODE_HIGHRES);
  938. }
  939. #endif /* HIGH_RES_TIMERS */
  940. #if defined CONFIG_NO_HZ_COMMON || defined CONFIG_HIGH_RES_TIMERS
  941. void tick_cancel_sched_timer(int cpu)
  942. {
  943. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  944. # ifdef CONFIG_HIGH_RES_TIMERS
  945. if (ts->sched_timer.base)
  946. hrtimer_cancel(&ts->sched_timer);
  947. # endif
  948. memset(ts, 0, sizeof(*ts));
  949. }
  950. #endif
  951. /**
  952. * Async notification about clocksource changes
  953. */
  954. void tick_clock_notify(void)
  955. {
  956. int cpu;
  957. for_each_possible_cpu(cpu)
  958. set_bit(0, &per_cpu(tick_cpu_sched, cpu).check_clocks);
  959. }
  960. /*
  961. * Async notification about clock event changes
  962. */
  963. void tick_oneshot_notify(void)
  964. {
  965. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  966. set_bit(0, &ts->check_clocks);
  967. }
  968. /**
  969. * Check, if a change happened, which makes oneshot possible.
  970. *
  971. * Called cyclic from the hrtimer softirq (driven by the timer
  972. * softirq) allow_nohz signals, that we can switch into low-res nohz
  973. * mode, because high resolution timers are disabled (either compile
  974. * or runtime). Called with interrupts disabled.
  975. */
  976. int tick_check_oneshot_change(int allow_nohz)
  977. {
  978. struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
  979. if (!test_and_clear_bit(0, &ts->check_clocks))
  980. return 0;
  981. if (ts->nohz_mode != NOHZ_MODE_INACTIVE)
  982. return 0;
  983. if (!timekeeping_valid_for_hres() || !tick_is_oneshot_available())
  984. return 0;
  985. if (!allow_nohz)
  986. return 1;
  987. tick_nohz_switch_to_nohz();
  988. return 0;
  989. }