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