tick-sched.c 30 KB

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