tick-sched.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. * For licencing details see kernel-base/COPYING
  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/tick.h>
  23. #include "tick-internal.h"
  24. /*
  25. * Per cpu nohz control structure
  26. */
  27. static DEFINE_PER_CPU(struct tick_sched, tick_cpu_sched);
  28. /*
  29. * The time, when the last jiffy update happened. Protected by xtime_lock.
  30. */
  31. static ktime_t last_jiffies_update;
  32. struct tick_sched *tick_get_tick_sched(int cpu)
  33. {
  34. return &per_cpu(tick_cpu_sched, cpu);
  35. }
  36. /*
  37. * Must be called with interrupts disabled !
  38. */
  39. static void tick_do_update_jiffies64(ktime_t now)
  40. {
  41. unsigned long ticks = 0;
  42. ktime_t delta;
  43. /* Reevalute with xtime_lock held */
  44. write_seqlock(&xtime_lock);
  45. delta = ktime_sub(now, last_jiffies_update);
  46. if (delta.tv64 >= tick_period.tv64) {
  47. delta = ktime_sub(delta, tick_period);
  48. last_jiffies_update = ktime_add(last_jiffies_update,
  49. tick_period);
  50. /* Slow path for long timeouts */
  51. if (unlikely(delta.tv64 >= tick_period.tv64)) {
  52. s64 incr = ktime_to_ns(tick_period);
  53. ticks = ktime_divns(delta, incr);
  54. last_jiffies_update = ktime_add_ns(last_jiffies_update,
  55. incr * ticks);
  56. }
  57. do_timer(++ticks);
  58. }
  59. write_sequnlock(&xtime_lock);
  60. }
  61. /*
  62. * Initialize and return retrieve the jiffies update.
  63. */
  64. static ktime_t tick_init_jiffy_update(void)
  65. {
  66. ktime_t period;
  67. write_seqlock(&xtime_lock);
  68. /* Did we start the jiffies update yet ? */
  69. if (last_jiffies_update.tv64 == 0)
  70. last_jiffies_update = tick_next_period;
  71. period = last_jiffies_update;
  72. write_sequnlock(&xtime_lock);
  73. return period;
  74. }
  75. /*
  76. * NOHZ - aka dynamic tick functionality
  77. */
  78. #ifdef CONFIG_NO_HZ
  79. /*
  80. * NO HZ enabled ?
  81. */
  82. static int tick_nohz_enabled __read_mostly = 1;
  83. /*
  84. * Enable / Disable tickless mode
  85. */
  86. static int __init setup_tick_nohz(char *str)
  87. {
  88. if (!strcmp(str, "off"))
  89. tick_nohz_enabled = 0;
  90. else if (!strcmp(str, "on"))
  91. tick_nohz_enabled = 1;
  92. else
  93. return 0;
  94. return 1;
  95. }
  96. __setup("nohz=", setup_tick_nohz);
  97. /**
  98. * tick_nohz_update_jiffies - update jiffies when idle was interrupted
  99. *
  100. * Called from interrupt entry when the CPU was idle
  101. *
  102. * In case the sched_tick was stopped on this CPU, we have to check if jiffies
  103. * must be updated. Otherwise an interrupt handler could use a stale jiffy
  104. * value. We do this unconditionally on any cpu, as we don't know whether the
  105. * cpu, which has the update task assigned is in a long sleep.
  106. */
  107. void tick_nohz_update_jiffies(void)
  108. {
  109. int cpu = smp_processor_id();
  110. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  111. unsigned long flags;
  112. ktime_t now;
  113. if (!ts->tick_stopped)
  114. return;
  115. cpu_clear(cpu, nohz_cpu_mask);
  116. now = ktime_get();
  117. local_irq_save(flags);
  118. tick_do_update_jiffies64(now);
  119. local_irq_restore(flags);
  120. }
  121. /**
  122. * tick_nohz_stop_sched_tick - stop the idle tick from the idle task
  123. *
  124. * When the next event is more than a tick into the future, stop the idle tick
  125. * Called either from the idle loop or from irq_exit() when an idle period was
  126. * just interrupted by an interrupt which did not cause a reschedule.
  127. */
  128. void tick_nohz_stop_sched_tick(void)
  129. {
  130. unsigned long seq, last_jiffies, next_jiffies, delta_jiffies, flags;
  131. struct tick_sched *ts;
  132. ktime_t last_update, expires, now, delta;
  133. int cpu;
  134. local_irq_save(flags);
  135. cpu = smp_processor_id();
  136. ts = &per_cpu(tick_cpu_sched, cpu);
  137. if (unlikely(ts->nohz_mode == NOHZ_MODE_INACTIVE))
  138. goto end;
  139. if (need_resched())
  140. goto end;
  141. cpu = smp_processor_id();
  142. if (unlikely(local_softirq_pending()))
  143. printk(KERN_ERR "NOHZ: local_softirq_pending %02x\n",
  144. local_softirq_pending());
  145. now = ktime_get();
  146. /*
  147. * When called from irq_exit we need to account the idle sleep time
  148. * correctly.
  149. */
  150. if (ts->tick_stopped) {
  151. delta = ktime_sub(now, ts->idle_entrytime);
  152. ts->idle_sleeptime = ktime_add(ts->idle_sleeptime, delta);
  153. }
  154. ts->idle_entrytime = now;
  155. ts->idle_calls++;
  156. /* Read jiffies and the time when jiffies were updated last */
  157. do {
  158. seq = read_seqbegin(&xtime_lock);
  159. last_update = last_jiffies_update;
  160. last_jiffies = jiffies;
  161. } while (read_seqretry(&xtime_lock, seq));
  162. /* Get the next timer wheel timer */
  163. next_jiffies = get_next_timer_interrupt(last_jiffies);
  164. delta_jiffies = next_jiffies - last_jiffies;
  165. if (rcu_needs_cpu(cpu))
  166. delta_jiffies = 1;
  167. /*
  168. * Do not stop the tick, if we are only one off
  169. * or if the cpu is required for rcu
  170. */
  171. if (!ts->tick_stopped && delta_jiffies == 1)
  172. goto out;
  173. /* Schedule the tick, if we are at least one jiffie off */
  174. if ((long)delta_jiffies >= 1) {
  175. if (delta_jiffies > 1)
  176. cpu_set(cpu, nohz_cpu_mask);
  177. /*
  178. * nohz_stop_sched_tick can be called several times before
  179. * the nohz_restart_sched_tick is called. This happens when
  180. * interrupts arrive which do not cause a reschedule. In the
  181. * first call we save the current tick time, so we can restart
  182. * the scheduler tick in nohz_restart_sched_tick.
  183. */
  184. if (!ts->tick_stopped) {
  185. ts->idle_tick = ts->sched_timer.expires;
  186. ts->tick_stopped = 1;
  187. ts->idle_jiffies = last_jiffies;
  188. }
  189. /*
  190. * calculate the expiry time for the next timer wheel
  191. * timer
  192. */
  193. expires = ktime_add_ns(last_update, tick_period.tv64 *
  194. delta_jiffies);
  195. ts->idle_expires = expires;
  196. ts->idle_sleeps++;
  197. if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
  198. hrtimer_start(&ts->sched_timer, expires,
  199. HRTIMER_MODE_ABS);
  200. /* Check, if the timer was already in the past */
  201. if (hrtimer_active(&ts->sched_timer))
  202. goto out;
  203. } else if(!tick_program_event(expires, 0))
  204. goto out;
  205. /*
  206. * We are past the event already. So we crossed a
  207. * jiffie boundary. Update jiffies and raise the
  208. * softirq.
  209. */
  210. tick_do_update_jiffies64(ktime_get());
  211. cpu_clear(cpu, nohz_cpu_mask);
  212. }
  213. raise_softirq_irqoff(TIMER_SOFTIRQ);
  214. out:
  215. ts->next_jiffies = next_jiffies;
  216. ts->last_jiffies = last_jiffies;
  217. end:
  218. local_irq_restore(flags);
  219. }
  220. /**
  221. * nohz_restart_sched_tick - restart the idle tick from the idle task
  222. *
  223. * Restart the idle tick when the CPU is woken up from idle
  224. */
  225. void tick_nohz_restart_sched_tick(void)
  226. {
  227. int cpu = smp_processor_id();
  228. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  229. unsigned long ticks;
  230. ktime_t now, delta;
  231. if (!ts->tick_stopped)
  232. return;
  233. /* Update jiffies first */
  234. now = ktime_get();
  235. local_irq_disable();
  236. tick_do_update_jiffies64(now);
  237. cpu_clear(cpu, nohz_cpu_mask);
  238. /* Account the idle time */
  239. delta = ktime_sub(now, ts->idle_entrytime);
  240. ts->idle_sleeptime = ktime_add(ts->idle_sleeptime, delta);
  241. /*
  242. * We stopped the tick in idle. Update process times would miss the
  243. * time we slept as update_process_times does only a 1 tick
  244. * accounting. Enforce that this is accounted to idle !
  245. */
  246. ticks = jiffies - ts->idle_jiffies;
  247. /*
  248. * We might be one off. Do not randomly account a huge number of ticks!
  249. */
  250. if (ticks && ticks < LONG_MAX) {
  251. add_preempt_count(HARDIRQ_OFFSET);
  252. account_system_time(current, HARDIRQ_OFFSET,
  253. jiffies_to_cputime(ticks));
  254. sub_preempt_count(HARDIRQ_OFFSET);
  255. }
  256. /*
  257. * Cancel the scheduled timer and restore the tick
  258. */
  259. ts->tick_stopped = 0;
  260. hrtimer_cancel(&ts->sched_timer);
  261. ts->sched_timer.expires = ts->idle_tick;
  262. while (1) {
  263. /* Forward the time to expire in the future */
  264. hrtimer_forward(&ts->sched_timer, now, tick_period);
  265. if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
  266. hrtimer_start(&ts->sched_timer,
  267. ts->sched_timer.expires,
  268. HRTIMER_MODE_ABS);
  269. /* Check, if the timer was already in the past */
  270. if (hrtimer_active(&ts->sched_timer))
  271. break;
  272. } else {
  273. if (!tick_program_event(ts->sched_timer.expires, 0))
  274. break;
  275. }
  276. /* Update jiffies and reread time */
  277. tick_do_update_jiffies64(now);
  278. now = ktime_get();
  279. }
  280. local_irq_enable();
  281. }
  282. static int tick_nohz_reprogram(struct tick_sched *ts, ktime_t now)
  283. {
  284. hrtimer_forward(&ts->sched_timer, now, tick_period);
  285. return tick_program_event(ts->sched_timer.expires, 0);
  286. }
  287. /*
  288. * The nohz low res interrupt handler
  289. */
  290. static void tick_nohz_handler(struct clock_event_device *dev)
  291. {
  292. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  293. struct pt_regs *regs = get_irq_regs();
  294. ktime_t now = ktime_get();
  295. dev->next_event.tv64 = KTIME_MAX;
  296. /* Check, if the jiffies need an update */
  297. tick_do_update_jiffies64(now);
  298. /*
  299. * When we are idle and the tick is stopped, we have to touch
  300. * the watchdog as we might not schedule for a really long
  301. * time. This happens on complete idle SMP systems while
  302. * waiting on the login prompt. We also increment the "start
  303. * of idle" jiffy stamp so the idle accounting adjustment we
  304. * do when we go busy again does not account too much ticks.
  305. */
  306. if (ts->tick_stopped) {
  307. touch_softlockup_watchdog();
  308. ts->idle_jiffies++;
  309. }
  310. update_process_times(user_mode(regs));
  311. profile_tick(CPU_PROFILING);
  312. /* Do not restart, when we are in the idle loop */
  313. if (ts->tick_stopped)
  314. return;
  315. while (tick_nohz_reprogram(ts, now)) {
  316. now = ktime_get();
  317. tick_do_update_jiffies64(now);
  318. }
  319. }
  320. /**
  321. * tick_nohz_switch_to_nohz - switch to nohz mode
  322. */
  323. static void tick_nohz_switch_to_nohz(void)
  324. {
  325. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  326. ktime_t next;
  327. if (!tick_nohz_enabled)
  328. return;
  329. local_irq_disable();
  330. if (tick_switch_to_oneshot(tick_nohz_handler)) {
  331. local_irq_enable();
  332. return;
  333. }
  334. ts->nohz_mode = NOHZ_MODE_LOWRES;
  335. /*
  336. * Recycle the hrtimer in ts, so we can share the
  337. * hrtimer_forward with the highres code.
  338. */
  339. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  340. /* Get the next period */
  341. next = tick_init_jiffy_update();
  342. for (;;) {
  343. ts->sched_timer.expires = next;
  344. if (!tick_program_event(next, 0))
  345. break;
  346. next = ktime_add(next, tick_period);
  347. }
  348. local_irq_enable();
  349. printk(KERN_INFO "Switched to NOHz mode on CPU #%d\n",
  350. smp_processor_id());
  351. }
  352. #else
  353. static inline void tick_nohz_switch_to_nohz(void) { }
  354. #endif /* NO_HZ */
  355. /*
  356. * High resolution timer specific code
  357. */
  358. #ifdef CONFIG_HIGH_RES_TIMERS
  359. /*
  360. * We rearm the timer until we get disabled by the idle code
  361. * Called with interrupts disabled and timer->base->cpu_base->lock held.
  362. */
  363. static enum hrtimer_restart tick_sched_timer(struct hrtimer *timer)
  364. {
  365. struct tick_sched *ts =
  366. container_of(timer, struct tick_sched, sched_timer);
  367. struct hrtimer_cpu_base *base = timer->base->cpu_base;
  368. struct pt_regs *regs = get_irq_regs();
  369. ktime_t now = ktime_get();
  370. /* Check, if the jiffies need an update */
  371. tick_do_update_jiffies64(now);
  372. /*
  373. * Do not call, when we are not in irq context and have
  374. * no valid regs pointer
  375. */
  376. if (regs) {
  377. /*
  378. * When we are idle and the tick is stopped, we have to touch
  379. * the watchdog as we might not schedule for a really long
  380. * time. This happens on complete idle SMP systems while
  381. * waiting on the login prompt. We also increment the "start of
  382. * idle" jiffy stamp so the idle accounting adjustment we do
  383. * when we go busy again does not account too much ticks.
  384. */
  385. if (ts->tick_stopped) {
  386. touch_softlockup_watchdog();
  387. ts->idle_jiffies++;
  388. }
  389. /*
  390. * update_process_times() might take tasklist_lock, hence
  391. * drop the base lock. sched-tick hrtimers are per-CPU and
  392. * never accessible by userspace APIs, so this is safe to do.
  393. */
  394. spin_unlock(&base->lock);
  395. update_process_times(user_mode(regs));
  396. profile_tick(CPU_PROFILING);
  397. spin_lock(&base->lock);
  398. }
  399. /* Do not restart, when we are in the idle loop */
  400. if (ts->tick_stopped)
  401. return HRTIMER_NORESTART;
  402. hrtimer_forward(timer, now, tick_period);
  403. return HRTIMER_RESTART;
  404. }
  405. /**
  406. * tick_setup_sched_timer - setup the tick emulation timer
  407. */
  408. void tick_setup_sched_timer(void)
  409. {
  410. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  411. ktime_t now = ktime_get();
  412. /*
  413. * Emulate tick processing via per-CPU hrtimers:
  414. */
  415. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  416. ts->sched_timer.function = tick_sched_timer;
  417. ts->sched_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
  418. /* Get the next period */
  419. ts->sched_timer.expires = tick_init_jiffy_update();
  420. for (;;) {
  421. hrtimer_forward(&ts->sched_timer, now, tick_period);
  422. hrtimer_start(&ts->sched_timer, ts->sched_timer.expires,
  423. HRTIMER_MODE_ABS);
  424. /* Check, if the timer was already in the past */
  425. if (hrtimer_active(&ts->sched_timer))
  426. break;
  427. now = ktime_get();
  428. }
  429. #ifdef CONFIG_NO_HZ
  430. if (tick_nohz_enabled)
  431. ts->nohz_mode = NOHZ_MODE_HIGHRES;
  432. #endif
  433. }
  434. void tick_cancel_sched_timer(int cpu)
  435. {
  436. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  437. if (ts->sched_timer.base)
  438. hrtimer_cancel(&ts->sched_timer);
  439. ts->tick_stopped = 0;
  440. ts->nohz_mode = NOHZ_MODE_INACTIVE;
  441. }
  442. #endif /* HIGH_RES_TIMERS */
  443. /**
  444. * Async notification about clocksource changes
  445. */
  446. void tick_clock_notify(void)
  447. {
  448. int cpu;
  449. for_each_possible_cpu(cpu)
  450. set_bit(0, &per_cpu(tick_cpu_sched, cpu).check_clocks);
  451. }
  452. /*
  453. * Async notification about clock event changes
  454. */
  455. void tick_oneshot_notify(void)
  456. {
  457. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  458. set_bit(0, &ts->check_clocks);
  459. }
  460. /**
  461. * Check, if a change happened, which makes oneshot possible.
  462. *
  463. * Called cyclic from the hrtimer softirq (driven by the timer
  464. * softirq) allow_nohz signals, that we can switch into low-res nohz
  465. * mode, because high resolution timers are disabled (either compile
  466. * or runtime).
  467. */
  468. int tick_check_oneshot_change(int allow_nohz)
  469. {
  470. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  471. if (!test_and_clear_bit(0, &ts->check_clocks))
  472. return 0;
  473. if (ts->nohz_mode != NOHZ_MODE_INACTIVE)
  474. return 0;
  475. if (!timekeeping_is_continuous() || !tick_is_oneshot_available())
  476. return 0;
  477. if (!allow_nohz)
  478. return 1;
  479. tick_nohz_switch_to_nohz();
  480. return 0;
  481. }