tick-sched.c 25 KB

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