tick-sched.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  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. 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 tick_nohz_restart_sched_tick(struct tick_sched *ts, ktime_t now);
  171. /*
  172. * Re-evaluate the need for the tick on the current CPU
  173. * and restart it if necessary.
  174. */
  175. void __tick_nohz_full_check(void)
  176. {
  177. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  178. if (tick_nohz_full_cpu(smp_processor_id())) {
  179. if (ts->tick_stopped && !is_idle_task(current)) {
  180. if (!can_stop_full_tick())
  181. tick_nohz_restart_sched_tick(ts, ktime_get());
  182. }
  183. }
  184. }
  185. static void nohz_full_kick_work_func(struct irq_work *work)
  186. {
  187. __tick_nohz_full_check();
  188. }
  189. static DEFINE_PER_CPU(struct irq_work, nohz_full_kick_work) = {
  190. .func = nohz_full_kick_work_func,
  191. };
  192. /*
  193. * Kick the CPU if it's full dynticks in order to force it to
  194. * re-evaluate its dependency on the tick and restart it if necessary.
  195. */
  196. void tick_nohz_full_kick_cpu(int cpu)
  197. {
  198. if (!tick_nohz_full_cpu(cpu))
  199. return;
  200. irq_work_queue_on(&per_cpu(nohz_full_kick_work, cpu), cpu);
  201. }
  202. static void nohz_full_kick_ipi(void *info)
  203. {
  204. __tick_nohz_full_check();
  205. }
  206. /*
  207. * Kick all full dynticks CPUs in order to force these to re-evaluate
  208. * their dependency on the tick and restart it if necessary.
  209. */
  210. void tick_nohz_full_kick_all(void)
  211. {
  212. if (!tick_nohz_full_running)
  213. return;
  214. preempt_disable();
  215. smp_call_function_many(tick_nohz_full_mask,
  216. nohz_full_kick_ipi, NULL, false);
  217. tick_nohz_full_kick();
  218. preempt_enable();
  219. }
  220. /*
  221. * Re-evaluate the need for the tick as we switch the current task.
  222. * It might need the tick due to per task/process properties:
  223. * perf events, posix cpu timers, ...
  224. */
  225. void __tick_nohz_task_switch(struct task_struct *tsk)
  226. {
  227. unsigned long flags;
  228. local_irq_save(flags);
  229. if (!tick_nohz_full_cpu(smp_processor_id()))
  230. goto out;
  231. if (tick_nohz_tick_stopped() && !can_stop_full_tick())
  232. tick_nohz_full_kick();
  233. out:
  234. local_irq_restore(flags);
  235. }
  236. /* Parse the boot-time nohz CPU list from the kernel parameters. */
  237. static int __init tick_nohz_full_setup(char *str)
  238. {
  239. int cpu;
  240. alloc_bootmem_cpumask_var(&tick_nohz_full_mask);
  241. alloc_bootmem_cpumask_var(&housekeeping_mask);
  242. if (cpulist_parse(str, tick_nohz_full_mask) < 0) {
  243. pr_warning("NOHZ: Incorrect nohz_full cpumask\n");
  244. return 1;
  245. }
  246. cpu = smp_processor_id();
  247. if (cpumask_test_cpu(cpu, tick_nohz_full_mask)) {
  248. pr_warning("NO_HZ: Clearing %d from nohz_full range for timekeeping\n", cpu);
  249. cpumask_clear_cpu(cpu, tick_nohz_full_mask);
  250. }
  251. cpumask_andnot(housekeeping_mask,
  252. cpu_possible_mask, tick_nohz_full_mask);
  253. tick_nohz_full_running = true;
  254. return 1;
  255. }
  256. __setup("nohz_full=", tick_nohz_full_setup);
  257. static int tick_nohz_cpu_down_callback(struct notifier_block *nfb,
  258. unsigned long action,
  259. void *hcpu)
  260. {
  261. unsigned int cpu = (unsigned long)hcpu;
  262. switch (action & ~CPU_TASKS_FROZEN) {
  263. case CPU_DOWN_PREPARE:
  264. /*
  265. * If we handle the timekeeping duty for full dynticks CPUs,
  266. * we can't safely shutdown that CPU.
  267. */
  268. if (tick_nohz_full_running && tick_do_timer_cpu == cpu)
  269. return NOTIFY_BAD;
  270. break;
  271. }
  272. return NOTIFY_OK;
  273. }
  274. /*
  275. * Worst case string length in chunks of CPU range seems 2 steps
  276. * separations: 0,2,4,6,...
  277. * This is NR_CPUS + sizeof('\0')
  278. */
  279. static char __initdata nohz_full_buf[NR_CPUS + 1];
  280. static int tick_nohz_init_all(void)
  281. {
  282. int err = -1;
  283. #ifdef CONFIG_NO_HZ_FULL_ALL
  284. if (!alloc_cpumask_var(&tick_nohz_full_mask, GFP_KERNEL)) {
  285. pr_err("NO_HZ: Can't allocate full dynticks cpumask\n");
  286. return err;
  287. }
  288. if (!alloc_cpumask_var(&housekeeping_mask, GFP_KERNEL)) {
  289. pr_err("NO_HZ: Can't allocate not-full dynticks cpumask\n");
  290. return err;
  291. }
  292. err = 0;
  293. cpumask_setall(tick_nohz_full_mask);
  294. cpumask_clear_cpu(smp_processor_id(), tick_nohz_full_mask);
  295. cpumask_clear(housekeeping_mask);
  296. cpumask_set_cpu(smp_processor_id(), housekeeping_mask);
  297. tick_nohz_full_running = true;
  298. #endif
  299. return err;
  300. }
  301. void __init tick_nohz_init(void)
  302. {
  303. int cpu;
  304. if (!tick_nohz_full_running) {
  305. if (tick_nohz_init_all() < 0)
  306. return;
  307. }
  308. for_each_cpu(cpu, tick_nohz_full_mask)
  309. context_tracking_cpu_set(cpu);
  310. cpu_notifier(tick_nohz_cpu_down_callback, 0);
  311. cpulist_scnprintf(nohz_full_buf, sizeof(nohz_full_buf), tick_nohz_full_mask);
  312. pr_info("NO_HZ: Full dynticks CPUs: %s.\n", nohz_full_buf);
  313. }
  314. #endif
  315. /*
  316. * NOHZ - aka dynamic tick functionality
  317. */
  318. #ifdef CONFIG_NO_HZ_COMMON
  319. /*
  320. * NO HZ enabled ?
  321. */
  322. static int tick_nohz_enabled __read_mostly = 1;
  323. int tick_nohz_active __read_mostly;
  324. /*
  325. * Enable / Disable tickless mode
  326. */
  327. static int __init setup_tick_nohz(char *str)
  328. {
  329. if (!strcmp(str, "off"))
  330. tick_nohz_enabled = 0;
  331. else if (!strcmp(str, "on"))
  332. tick_nohz_enabled = 1;
  333. else
  334. return 0;
  335. return 1;
  336. }
  337. __setup("nohz=", setup_tick_nohz);
  338. /**
  339. * tick_nohz_update_jiffies - update jiffies when idle was interrupted
  340. *
  341. * Called from interrupt entry when the CPU was idle
  342. *
  343. * In case the sched_tick was stopped on this CPU, we have to check if jiffies
  344. * must be updated. Otherwise an interrupt handler could use a stale jiffy
  345. * value. We do this unconditionally on any cpu, as we don't know whether the
  346. * cpu, which has the update task assigned is in a long sleep.
  347. */
  348. static void tick_nohz_update_jiffies(ktime_t now)
  349. {
  350. unsigned long flags;
  351. __this_cpu_write(tick_cpu_sched.idle_waketime, now);
  352. local_irq_save(flags);
  353. tick_do_update_jiffies64(now);
  354. local_irq_restore(flags);
  355. touch_softlockup_watchdog();
  356. }
  357. /*
  358. * Updates the per cpu time idle statistics counters
  359. */
  360. static void
  361. update_ts_time_stats(int cpu, struct tick_sched *ts, ktime_t now, u64 *last_update_time)
  362. {
  363. ktime_t delta;
  364. if (ts->idle_active) {
  365. delta = ktime_sub(now, ts->idle_entrytime);
  366. if (nr_iowait_cpu(cpu) > 0)
  367. ts->iowait_sleeptime = ktime_add(ts->iowait_sleeptime, delta);
  368. else
  369. ts->idle_sleeptime = ktime_add(ts->idle_sleeptime, delta);
  370. ts->idle_entrytime = now;
  371. }
  372. if (last_update_time)
  373. *last_update_time = ktime_to_us(now);
  374. }
  375. static void tick_nohz_stop_idle(struct tick_sched *ts, ktime_t now)
  376. {
  377. update_ts_time_stats(smp_processor_id(), ts, now, NULL);
  378. ts->idle_active = 0;
  379. sched_clock_idle_wakeup_event(0);
  380. }
  381. static ktime_t tick_nohz_start_idle(struct tick_sched *ts)
  382. {
  383. ktime_t now = ktime_get();
  384. ts->idle_entrytime = now;
  385. ts->idle_active = 1;
  386. sched_clock_idle_sleep_event();
  387. return now;
  388. }
  389. /**
  390. * get_cpu_idle_time_us - get the total idle time of a cpu
  391. * @cpu: CPU number to query
  392. * @last_update_time: variable to store update time in. Do not update
  393. * counters if NULL.
  394. *
  395. * Return the cummulative idle time (since boot) for a given
  396. * CPU, in microseconds.
  397. *
  398. * This time is measured via accounting rather than sampling,
  399. * and is as accurate as ktime_get() is.
  400. *
  401. * This function returns -1 if NOHZ is not enabled.
  402. */
  403. u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time)
  404. {
  405. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  406. ktime_t now, idle;
  407. if (!tick_nohz_active)
  408. return -1;
  409. now = ktime_get();
  410. if (last_update_time) {
  411. update_ts_time_stats(cpu, ts, now, last_update_time);
  412. idle = ts->idle_sleeptime;
  413. } else {
  414. if (ts->idle_active && !nr_iowait_cpu(cpu)) {
  415. ktime_t delta = ktime_sub(now, ts->idle_entrytime);
  416. idle = ktime_add(ts->idle_sleeptime, delta);
  417. } else {
  418. idle = ts->idle_sleeptime;
  419. }
  420. }
  421. return ktime_to_us(idle);
  422. }
  423. EXPORT_SYMBOL_GPL(get_cpu_idle_time_us);
  424. /**
  425. * get_cpu_iowait_time_us - get the total iowait time of a cpu
  426. * @cpu: CPU number to query
  427. * @last_update_time: variable to store update time in. Do not update
  428. * counters if NULL.
  429. *
  430. * Return the cummulative iowait time (since boot) for a given
  431. * CPU, in microseconds.
  432. *
  433. * This time is measured via accounting rather than sampling,
  434. * and is as accurate as ktime_get() is.
  435. *
  436. * This function returns -1 if NOHZ is not enabled.
  437. */
  438. u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time)
  439. {
  440. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  441. ktime_t now, iowait;
  442. if (!tick_nohz_active)
  443. return -1;
  444. now = ktime_get();
  445. if (last_update_time) {
  446. update_ts_time_stats(cpu, ts, now, last_update_time);
  447. iowait = ts->iowait_sleeptime;
  448. } else {
  449. if (ts->idle_active && nr_iowait_cpu(cpu) > 0) {
  450. ktime_t delta = ktime_sub(now, ts->idle_entrytime);
  451. iowait = ktime_add(ts->iowait_sleeptime, delta);
  452. } else {
  453. iowait = ts->iowait_sleeptime;
  454. }
  455. }
  456. return ktime_to_us(iowait);
  457. }
  458. EXPORT_SYMBOL_GPL(get_cpu_iowait_time_us);
  459. static ktime_t tick_nohz_stop_sched_tick(struct tick_sched *ts,
  460. ktime_t now, int cpu)
  461. {
  462. unsigned long seq, last_jiffies, next_jiffies, delta_jiffies;
  463. ktime_t last_update, expires, ret = { .tv64 = 0 };
  464. unsigned long rcu_delta_jiffies;
  465. struct clock_event_device *dev = __get_cpu_var(tick_cpu_device).evtdev;
  466. u64 time_delta;
  467. time_delta = timekeeping_max_deferment();
  468. /* Read jiffies and the time when jiffies were updated last */
  469. do {
  470. seq = read_seqbegin(&jiffies_lock);
  471. last_update = last_jiffies_update;
  472. last_jiffies = jiffies;
  473. } while (read_seqretry(&jiffies_lock, seq));
  474. if (rcu_needs_cpu(cpu, &rcu_delta_jiffies) ||
  475. arch_needs_cpu(cpu) || irq_work_needs_cpu()) {
  476. next_jiffies = last_jiffies + 1;
  477. delta_jiffies = 1;
  478. } else {
  479. /* Get the next timer wheel timer */
  480. next_jiffies = get_next_timer_interrupt(last_jiffies);
  481. delta_jiffies = next_jiffies - last_jiffies;
  482. if (rcu_delta_jiffies < delta_jiffies) {
  483. next_jiffies = last_jiffies + rcu_delta_jiffies;
  484. delta_jiffies = rcu_delta_jiffies;
  485. }
  486. }
  487. /*
  488. * Do not stop the tick, if we are only one off (or less)
  489. * or if the cpu is required for RCU:
  490. */
  491. if (!ts->tick_stopped && delta_jiffies <= 1)
  492. goto out;
  493. /* Schedule the tick, if we are at least one jiffie off */
  494. if ((long)delta_jiffies >= 1) {
  495. /*
  496. * If this cpu is the one which updates jiffies, then
  497. * give up the assignment and let it be taken by the
  498. * cpu which runs the tick timer next, which might be
  499. * this cpu as well. If we don't drop this here the
  500. * jiffies might be stale and do_timer() never
  501. * invoked. Keep track of the fact that it was the one
  502. * which had the do_timer() duty last. If this cpu is
  503. * the one which had the do_timer() duty last, we
  504. * limit the sleep time to the timekeeping
  505. * max_deferement value which we retrieved
  506. * above. Otherwise we can sleep as long as we want.
  507. */
  508. if (cpu == tick_do_timer_cpu) {
  509. tick_do_timer_cpu = TICK_DO_TIMER_NONE;
  510. ts->do_timer_last = 1;
  511. } else if (tick_do_timer_cpu != TICK_DO_TIMER_NONE) {
  512. time_delta = KTIME_MAX;
  513. ts->do_timer_last = 0;
  514. } else if (!ts->do_timer_last) {
  515. time_delta = KTIME_MAX;
  516. }
  517. #ifdef CONFIG_NO_HZ_FULL
  518. if (!ts->inidle) {
  519. time_delta = min(time_delta,
  520. scheduler_tick_max_deferment());
  521. }
  522. #endif
  523. /*
  524. * calculate the expiry time for the next timer wheel
  525. * timer. delta_jiffies >= NEXT_TIMER_MAX_DELTA signals
  526. * that there is no timer pending or at least extremely
  527. * far into the future (12 days for HZ=1000). In this
  528. * case we set the expiry to the end of time.
  529. */
  530. if (likely(delta_jiffies < NEXT_TIMER_MAX_DELTA)) {
  531. /*
  532. * Calculate the time delta for the next timer event.
  533. * If the time delta exceeds the maximum time delta
  534. * permitted by the current clocksource then adjust
  535. * the time delta accordingly to ensure the
  536. * clocksource does not wrap.
  537. */
  538. time_delta = min_t(u64, time_delta,
  539. tick_period.tv64 * delta_jiffies);
  540. }
  541. if (time_delta < KTIME_MAX)
  542. expires = ktime_add_ns(last_update, time_delta);
  543. else
  544. expires.tv64 = KTIME_MAX;
  545. /* Skip reprogram of event if its not changed */
  546. if (ts->tick_stopped && ktime_equal(expires, dev->next_event))
  547. goto out;
  548. ret = expires;
  549. /*
  550. * nohz_stop_sched_tick can be called several times before
  551. * the nohz_restart_sched_tick is called. This happens when
  552. * interrupts arrive which do not cause a reschedule. In the
  553. * first call we save the current tick time, so we can restart
  554. * the scheduler tick in nohz_restart_sched_tick.
  555. */
  556. if (!ts->tick_stopped) {
  557. nohz_balance_enter_idle(cpu);
  558. calc_load_enter_idle();
  559. ts->last_tick = hrtimer_get_expires(&ts->sched_timer);
  560. ts->tick_stopped = 1;
  561. trace_tick_stop(1, " ");
  562. }
  563. /*
  564. * If the expiration time == KTIME_MAX, then
  565. * in this case we simply stop the tick timer.
  566. */
  567. if (unlikely(expires.tv64 == KTIME_MAX)) {
  568. if (ts->nohz_mode == NOHZ_MODE_HIGHRES)
  569. hrtimer_cancel(&ts->sched_timer);
  570. goto out;
  571. }
  572. if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
  573. hrtimer_start(&ts->sched_timer, expires,
  574. HRTIMER_MODE_ABS_PINNED);
  575. /* Check, if the timer was already in the past */
  576. if (hrtimer_active(&ts->sched_timer))
  577. goto out;
  578. } else if (!tick_program_event(expires, 0))
  579. goto out;
  580. /*
  581. * We are past the event already. So we crossed a
  582. * jiffie boundary. Update jiffies and raise the
  583. * softirq.
  584. */
  585. tick_do_update_jiffies64(ktime_get());
  586. }
  587. raise_softirq_irqoff(TIMER_SOFTIRQ);
  588. out:
  589. ts->next_jiffies = next_jiffies;
  590. ts->last_jiffies = last_jiffies;
  591. ts->sleep_length = ktime_sub(dev->next_event, now);
  592. return ret;
  593. }
  594. static void tick_nohz_full_stop_tick(struct tick_sched *ts)
  595. {
  596. #ifdef CONFIG_NO_HZ_FULL
  597. int cpu = smp_processor_id();
  598. if (!tick_nohz_full_cpu(cpu) || is_idle_task(current))
  599. return;
  600. if (!ts->tick_stopped && ts->nohz_mode == NOHZ_MODE_INACTIVE)
  601. return;
  602. if (!can_stop_full_tick())
  603. return;
  604. tick_nohz_stop_sched_tick(ts, ktime_get(), cpu);
  605. #endif
  606. }
  607. static bool can_stop_idle_tick(int cpu, struct tick_sched *ts)
  608. {
  609. /*
  610. * If this cpu is offline and it is the one which updates
  611. * jiffies, then give up the assignment and let it be taken by
  612. * the cpu which runs the tick timer next. If we don't drop
  613. * this here the jiffies might be stale and do_timer() never
  614. * invoked.
  615. */
  616. if (unlikely(!cpu_online(cpu))) {
  617. if (cpu == tick_do_timer_cpu)
  618. tick_do_timer_cpu = TICK_DO_TIMER_NONE;
  619. return false;
  620. }
  621. if (unlikely(ts->nohz_mode == NOHZ_MODE_INACTIVE)) {
  622. ts->sleep_length = (ktime_t) { .tv64 = NSEC_PER_SEC/HZ };
  623. return false;
  624. }
  625. if (need_resched())
  626. return false;
  627. if (unlikely(local_softirq_pending() && cpu_online(cpu))) {
  628. static int ratelimit;
  629. if (ratelimit < 10 &&
  630. (local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK)) {
  631. pr_warn("NOHZ: local_softirq_pending %02x\n",
  632. (unsigned int) local_softirq_pending());
  633. ratelimit++;
  634. }
  635. return false;
  636. }
  637. if (tick_nohz_full_enabled()) {
  638. /*
  639. * Keep the tick alive to guarantee timekeeping progression
  640. * if there are full dynticks CPUs around
  641. */
  642. if (tick_do_timer_cpu == cpu)
  643. return false;
  644. /*
  645. * Boot safety: make sure the timekeeping duty has been
  646. * assigned before entering dyntick-idle mode,
  647. */
  648. if (tick_do_timer_cpu == TICK_DO_TIMER_NONE)
  649. return false;
  650. }
  651. return true;
  652. }
  653. static void __tick_nohz_idle_enter(struct tick_sched *ts)
  654. {
  655. ktime_t now, expires;
  656. int cpu = smp_processor_id();
  657. now = tick_nohz_start_idle(ts);
  658. if (can_stop_idle_tick(cpu, ts)) {
  659. int was_stopped = ts->tick_stopped;
  660. ts->idle_calls++;
  661. expires = tick_nohz_stop_sched_tick(ts, now, cpu);
  662. if (expires.tv64 > 0LL) {
  663. ts->idle_sleeps++;
  664. ts->idle_expires = expires;
  665. }
  666. if (!was_stopped && ts->tick_stopped)
  667. ts->idle_jiffies = ts->last_jiffies;
  668. }
  669. }
  670. /**
  671. * tick_nohz_idle_enter - stop the idle tick from the idle task
  672. *
  673. * When the next event is more than a tick into the future, stop the idle tick
  674. * Called when we start the idle loop.
  675. *
  676. * The arch is responsible of calling:
  677. *
  678. * - rcu_idle_enter() after its last use of RCU before the CPU is put
  679. * to sleep.
  680. * - rcu_idle_exit() before the first use of RCU after the CPU is woken up.
  681. */
  682. void tick_nohz_idle_enter(void)
  683. {
  684. struct tick_sched *ts;
  685. WARN_ON_ONCE(irqs_disabled());
  686. /*
  687. * Update the idle state in the scheduler domain hierarchy
  688. * when tick_nohz_stop_sched_tick() is called from the idle loop.
  689. * State will be updated to busy during the first busy tick after
  690. * exiting idle.
  691. */
  692. set_cpu_sd_state_idle();
  693. local_irq_disable();
  694. ts = &__get_cpu_var(tick_cpu_sched);
  695. ts->inidle = 1;
  696. __tick_nohz_idle_enter(ts);
  697. local_irq_enable();
  698. }
  699. EXPORT_SYMBOL_GPL(tick_nohz_idle_enter);
  700. /**
  701. * tick_nohz_irq_exit - update next tick event from interrupt exit
  702. *
  703. * When an interrupt fires while we are idle and it doesn't cause
  704. * a reschedule, it may still add, modify or delete a timer, enqueue
  705. * an RCU callback, etc...
  706. * So we need to re-calculate and reprogram the next tick event.
  707. */
  708. void tick_nohz_irq_exit(void)
  709. {
  710. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  711. if (ts->inidle)
  712. __tick_nohz_idle_enter(ts);
  713. else
  714. tick_nohz_full_stop_tick(ts);
  715. }
  716. /**
  717. * tick_nohz_get_sleep_length - return the length of the current sleep
  718. *
  719. * Called from power state control code with interrupts disabled
  720. */
  721. ktime_t tick_nohz_get_sleep_length(void)
  722. {
  723. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  724. return ts->sleep_length;
  725. }
  726. static void tick_nohz_restart(struct tick_sched *ts, ktime_t now)
  727. {
  728. hrtimer_cancel(&ts->sched_timer);
  729. hrtimer_set_expires(&ts->sched_timer, ts->last_tick);
  730. while (1) {
  731. /* Forward the time to expire in the future */
  732. hrtimer_forward(&ts->sched_timer, now, tick_period);
  733. if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
  734. hrtimer_start_expires(&ts->sched_timer,
  735. HRTIMER_MODE_ABS_PINNED);
  736. /* Check, if the timer was already in the past */
  737. if (hrtimer_active(&ts->sched_timer))
  738. break;
  739. } else {
  740. if (!tick_program_event(
  741. hrtimer_get_expires(&ts->sched_timer), 0))
  742. break;
  743. }
  744. /* Reread time and update jiffies */
  745. now = ktime_get();
  746. tick_do_update_jiffies64(now);
  747. }
  748. }
  749. static void tick_nohz_restart_sched_tick(struct tick_sched *ts, ktime_t now)
  750. {
  751. /* Update jiffies first */
  752. tick_do_update_jiffies64(now);
  753. update_cpu_load_nohz();
  754. calc_load_exit_idle();
  755. touch_softlockup_watchdog();
  756. /*
  757. * Cancel the scheduled timer and restore the tick
  758. */
  759. ts->tick_stopped = 0;
  760. ts->idle_exittime = now;
  761. tick_nohz_restart(ts, now);
  762. }
  763. static void tick_nohz_account_idle_ticks(struct tick_sched *ts)
  764. {
  765. #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
  766. unsigned long ticks;
  767. if (vtime_accounting_enabled())
  768. return;
  769. /*
  770. * We stopped the tick in idle. Update process times would miss the
  771. * time we slept as update_process_times does only a 1 tick
  772. * accounting. Enforce that this is accounted to idle !
  773. */
  774. ticks = jiffies - ts->idle_jiffies;
  775. /*
  776. * We might be one off. Do not randomly account a huge number of ticks!
  777. */
  778. if (ticks && ticks < LONG_MAX)
  779. account_idle_ticks(ticks);
  780. #endif
  781. }
  782. /**
  783. * tick_nohz_idle_exit - restart the idle tick from the idle task
  784. *
  785. * Restart the idle tick when the CPU is woken up from idle
  786. * This also exit the RCU extended quiescent state. The CPU
  787. * can use RCU again after this function is called.
  788. */
  789. void tick_nohz_idle_exit(void)
  790. {
  791. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  792. ktime_t now;
  793. local_irq_disable();
  794. WARN_ON_ONCE(!ts->inidle);
  795. ts->inidle = 0;
  796. if (ts->idle_active || ts->tick_stopped)
  797. now = ktime_get();
  798. if (ts->idle_active)
  799. tick_nohz_stop_idle(ts, now);
  800. if (ts->tick_stopped) {
  801. tick_nohz_restart_sched_tick(ts, now);
  802. tick_nohz_account_idle_ticks(ts);
  803. }
  804. local_irq_enable();
  805. }
  806. EXPORT_SYMBOL_GPL(tick_nohz_idle_exit);
  807. static int tick_nohz_reprogram(struct tick_sched *ts, ktime_t now)
  808. {
  809. hrtimer_forward(&ts->sched_timer, now, tick_period);
  810. return tick_program_event(hrtimer_get_expires(&ts->sched_timer), 0);
  811. }
  812. /*
  813. * The nohz low res interrupt handler
  814. */
  815. static void tick_nohz_handler(struct clock_event_device *dev)
  816. {
  817. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  818. struct pt_regs *regs = get_irq_regs();
  819. ktime_t now = ktime_get();
  820. dev->next_event.tv64 = KTIME_MAX;
  821. tick_sched_do_timer(now);
  822. tick_sched_handle(ts, regs);
  823. while (tick_nohz_reprogram(ts, now)) {
  824. now = ktime_get();
  825. tick_do_update_jiffies64(now);
  826. }
  827. }
  828. /**
  829. * tick_nohz_switch_to_nohz - switch to nohz mode
  830. */
  831. static void tick_nohz_switch_to_nohz(void)
  832. {
  833. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  834. ktime_t next;
  835. if (!tick_nohz_enabled)
  836. return;
  837. local_irq_disable();
  838. if (tick_switch_to_oneshot(tick_nohz_handler)) {
  839. local_irq_enable();
  840. return;
  841. }
  842. tick_nohz_active = 1;
  843. ts->nohz_mode = NOHZ_MODE_LOWRES;
  844. /*
  845. * Recycle the hrtimer in ts, so we can share the
  846. * hrtimer_forward with the highres code.
  847. */
  848. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  849. /* Get the next period */
  850. next = tick_init_jiffy_update();
  851. for (;;) {
  852. hrtimer_set_expires(&ts->sched_timer, next);
  853. if (!tick_program_event(next, 0))
  854. break;
  855. next = ktime_add(next, tick_period);
  856. }
  857. local_irq_enable();
  858. }
  859. /*
  860. * When NOHZ is enabled and the tick is stopped, we need to kick the
  861. * tick timer from irq_enter() so that the jiffies update is kept
  862. * alive during long running softirqs. That's ugly as hell, but
  863. * correctness is key even if we need to fix the offending softirq in
  864. * the first place.
  865. *
  866. * Note, this is different to tick_nohz_restart. We just kick the
  867. * timer and do not touch the other magic bits which need to be done
  868. * when idle is left.
  869. */
  870. static void tick_nohz_kick_tick(struct tick_sched *ts, ktime_t now)
  871. {
  872. #if 0
  873. /* Switch back to 2.6.27 behaviour */
  874. ktime_t delta;
  875. /*
  876. * Do not touch the tick device, when the next expiry is either
  877. * already reached or less/equal than the tick period.
  878. */
  879. delta = ktime_sub(hrtimer_get_expires(&ts->sched_timer), now);
  880. if (delta.tv64 <= tick_period.tv64)
  881. return;
  882. tick_nohz_restart(ts, now);
  883. #endif
  884. }
  885. static inline void tick_nohz_irq_enter(void)
  886. {
  887. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  888. ktime_t now;
  889. if (!ts->idle_active && !ts->tick_stopped)
  890. return;
  891. now = ktime_get();
  892. if (ts->idle_active)
  893. tick_nohz_stop_idle(ts, now);
  894. if (ts->tick_stopped) {
  895. tick_nohz_update_jiffies(now);
  896. tick_nohz_kick_tick(ts, now);
  897. }
  898. }
  899. #else
  900. static inline void tick_nohz_switch_to_nohz(void) { }
  901. static inline void tick_nohz_irq_enter(void) { }
  902. #endif /* CONFIG_NO_HZ_COMMON */
  903. /*
  904. * Called from irq_enter to notify about the possible interruption of idle()
  905. */
  906. void tick_irq_enter(void)
  907. {
  908. tick_check_oneshot_broadcast_this_cpu();
  909. tick_nohz_irq_enter();
  910. }
  911. /*
  912. * High resolution timer specific code
  913. */
  914. #ifdef CONFIG_HIGH_RES_TIMERS
  915. /*
  916. * We rearm the timer until we get disabled by the idle code.
  917. * Called with interrupts disabled.
  918. */
  919. static enum hrtimer_restart tick_sched_timer(struct hrtimer *timer)
  920. {
  921. struct tick_sched *ts =
  922. container_of(timer, struct tick_sched, sched_timer);
  923. struct pt_regs *regs = get_irq_regs();
  924. ktime_t now = ktime_get();
  925. tick_sched_do_timer(now);
  926. /*
  927. * Do not call, when we are not in irq context and have
  928. * no valid regs pointer
  929. */
  930. if (regs)
  931. tick_sched_handle(ts, regs);
  932. hrtimer_forward(timer, now, tick_period);
  933. return HRTIMER_RESTART;
  934. }
  935. static int sched_skew_tick;
  936. static int __init skew_tick(char *str)
  937. {
  938. get_option(&str, &sched_skew_tick);
  939. return 0;
  940. }
  941. early_param("skew_tick", skew_tick);
  942. /**
  943. * tick_setup_sched_timer - setup the tick emulation timer
  944. */
  945. void tick_setup_sched_timer(void)
  946. {
  947. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  948. ktime_t now = ktime_get();
  949. /*
  950. * Emulate tick processing via per-CPU hrtimers:
  951. */
  952. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  953. ts->sched_timer.function = tick_sched_timer;
  954. /* Get the next period (per cpu) */
  955. hrtimer_set_expires(&ts->sched_timer, tick_init_jiffy_update());
  956. /* Offset the tick to avert jiffies_lock contention. */
  957. if (sched_skew_tick) {
  958. u64 offset = ktime_to_ns(tick_period) >> 1;
  959. do_div(offset, num_possible_cpus());
  960. offset *= smp_processor_id();
  961. hrtimer_add_expires_ns(&ts->sched_timer, offset);
  962. }
  963. for (;;) {
  964. hrtimer_forward(&ts->sched_timer, now, tick_period);
  965. hrtimer_start_expires(&ts->sched_timer,
  966. HRTIMER_MODE_ABS_PINNED);
  967. /* Check, if the timer was already in the past */
  968. if (hrtimer_active(&ts->sched_timer))
  969. break;
  970. now = ktime_get();
  971. }
  972. #ifdef CONFIG_NO_HZ_COMMON
  973. if (tick_nohz_enabled) {
  974. ts->nohz_mode = NOHZ_MODE_HIGHRES;
  975. tick_nohz_active = 1;
  976. }
  977. #endif
  978. }
  979. #endif /* HIGH_RES_TIMERS */
  980. #if defined CONFIG_NO_HZ_COMMON || defined CONFIG_HIGH_RES_TIMERS
  981. void tick_cancel_sched_timer(int cpu)
  982. {
  983. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  984. # ifdef CONFIG_HIGH_RES_TIMERS
  985. if (ts->sched_timer.base)
  986. hrtimer_cancel(&ts->sched_timer);
  987. # endif
  988. memset(ts, 0, sizeof(*ts));
  989. }
  990. #endif
  991. /**
  992. * Async notification about clocksource changes
  993. */
  994. void tick_clock_notify(void)
  995. {
  996. int cpu;
  997. for_each_possible_cpu(cpu)
  998. set_bit(0, &per_cpu(tick_cpu_sched, cpu).check_clocks);
  999. }
  1000. /*
  1001. * Async notification about clock event changes
  1002. */
  1003. void tick_oneshot_notify(void)
  1004. {
  1005. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  1006. set_bit(0, &ts->check_clocks);
  1007. }
  1008. /**
  1009. * Check, if a change happened, which makes oneshot possible.
  1010. *
  1011. * Called cyclic from the hrtimer softirq (driven by the timer
  1012. * softirq) allow_nohz signals, that we can switch into low-res nohz
  1013. * mode, because high resolution timers are disabled (either compile
  1014. * or runtime).
  1015. */
  1016. int tick_check_oneshot_change(int allow_nohz)
  1017. {
  1018. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  1019. if (!test_and_clear_bit(0, &ts->check_clocks))
  1020. return 0;
  1021. if (ts->nohz_mode != NOHZ_MODE_INACTIVE)
  1022. return 0;
  1023. if (!timekeeping_valid_for_hres() || !tick_is_oneshot_available())
  1024. return 0;
  1025. if (!allow_nohz)
  1026. return 1;
  1027. tick_nohz_switch_to_nohz();
  1028. return 0;
  1029. }