tick-sched.c 29 KB

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