tick-sched.c 29 KB

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