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