hrtimer.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852
  1. /*
  2. * linux/kernel/hrtimer.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. * High-resolution kernel timers
  9. *
  10. * In contrast to the low-resolution timeout API implemented in
  11. * kernel/timer.c, hrtimers provide finer resolution and accuracy
  12. * depending on system configuration and capabilities.
  13. *
  14. * These timers are currently used for:
  15. * - itimers
  16. * - POSIX timers
  17. * - nanosleep
  18. * - precise in-kernel timing
  19. *
  20. * Started by: Thomas Gleixner and Ingo Molnar
  21. *
  22. * Credits:
  23. * based on kernel/timer.c
  24. *
  25. * Help, testing, suggestions, bugfixes, improvements were
  26. * provided by:
  27. *
  28. * George Anzinger, Andrew Morton, Steven Rostedt, Roman Zippel
  29. * et. al.
  30. *
  31. * For licencing details see kernel-base/COPYING
  32. */
  33. #include <linux/cpu.h>
  34. #include <linux/export.h>
  35. #include <linux/percpu.h>
  36. #include <linux/hrtimer.h>
  37. #include <linux/notifier.h>
  38. #include <linux/syscalls.h>
  39. #include <linux/kallsyms.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/tick.h>
  42. #include <linux/seq_file.h>
  43. #include <linux/err.h>
  44. #include <linux/debugobjects.h>
  45. #include <linux/sched.h>
  46. #include <linux/sched/sysctl.h>
  47. #include <linux/sched/rt.h>
  48. #include <linux/sched/deadline.h>
  49. #include <linux/timer.h>
  50. #include <linux/freezer.h>
  51. #include <asm/uaccess.h>
  52. #include <trace/events/timer.h>
  53. #include "tick-internal.h"
  54. /*
  55. * The timer bases:
  56. *
  57. * There are more clockids then hrtimer bases. Thus, we index
  58. * into the timer bases by the hrtimer_base_type enum. When trying
  59. * to reach a base using a clockid, hrtimer_clockid_to_base()
  60. * is used to convert from clockid to the proper hrtimer_base_type.
  61. */
  62. DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
  63. {
  64. .lock = __RAW_SPIN_LOCK_UNLOCKED(hrtimer_bases.lock),
  65. .clock_base =
  66. {
  67. {
  68. .index = HRTIMER_BASE_MONOTONIC,
  69. .clockid = CLOCK_MONOTONIC,
  70. .get_time = &ktime_get,
  71. .resolution = KTIME_LOW_RES,
  72. },
  73. {
  74. .index = HRTIMER_BASE_REALTIME,
  75. .clockid = CLOCK_REALTIME,
  76. .get_time = &ktime_get_real,
  77. .resolution = KTIME_LOW_RES,
  78. },
  79. {
  80. .index = HRTIMER_BASE_BOOTTIME,
  81. .clockid = CLOCK_BOOTTIME,
  82. .get_time = &ktime_get_boottime,
  83. .resolution = KTIME_LOW_RES,
  84. },
  85. {
  86. .index = HRTIMER_BASE_TAI,
  87. .clockid = CLOCK_TAI,
  88. .get_time = &ktime_get_clocktai,
  89. .resolution = KTIME_LOW_RES,
  90. },
  91. }
  92. };
  93. static const int hrtimer_clock_to_base_table[MAX_CLOCKS] = {
  94. [CLOCK_REALTIME] = HRTIMER_BASE_REALTIME,
  95. [CLOCK_MONOTONIC] = HRTIMER_BASE_MONOTONIC,
  96. [CLOCK_BOOTTIME] = HRTIMER_BASE_BOOTTIME,
  97. [CLOCK_TAI] = HRTIMER_BASE_TAI,
  98. };
  99. static inline int hrtimer_clockid_to_base(clockid_t clock_id)
  100. {
  101. return hrtimer_clock_to_base_table[clock_id];
  102. }
  103. /*
  104. * Get the coarse grained time at the softirq based on xtime and
  105. * wall_to_monotonic.
  106. */
  107. static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
  108. {
  109. ktime_t xtim, mono, boot, tai;
  110. ktime_t off_real, off_boot, off_tai;
  111. mono = ktime_get_update_offsets_tick(&off_real, &off_boot, &off_tai);
  112. boot = ktime_add(mono, off_boot);
  113. xtim = ktime_add(mono, off_real);
  114. tai = ktime_add(mono, off_tai);
  115. base->clock_base[HRTIMER_BASE_REALTIME].softirq_time = xtim;
  116. base->clock_base[HRTIMER_BASE_MONOTONIC].softirq_time = mono;
  117. base->clock_base[HRTIMER_BASE_BOOTTIME].softirq_time = boot;
  118. base->clock_base[HRTIMER_BASE_TAI].softirq_time = tai;
  119. }
  120. /*
  121. * Functions and macros which are different for UP/SMP systems are kept in a
  122. * single place
  123. */
  124. #ifdef CONFIG_SMP
  125. /*
  126. * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
  127. * means that all timers which are tied to this base via timer->base are
  128. * locked, and the base itself is locked too.
  129. *
  130. * So __run_timers/migrate_timers can safely modify all timers which could
  131. * be found on the lists/queues.
  132. *
  133. * When the timer's base is locked, and the timer removed from list, it is
  134. * possible to set timer->base = NULL and drop the lock: the timer remains
  135. * locked.
  136. */
  137. static
  138. struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
  139. unsigned long *flags)
  140. {
  141. struct hrtimer_clock_base *base;
  142. for (;;) {
  143. base = timer->base;
  144. if (likely(base != NULL)) {
  145. raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
  146. if (likely(base == timer->base))
  147. return base;
  148. /* The timer has migrated to another CPU: */
  149. raw_spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
  150. }
  151. cpu_relax();
  152. }
  153. }
  154. /*
  155. * With HIGHRES=y we do not migrate the timer when it is expiring
  156. * before the next event on the target cpu because we cannot reprogram
  157. * the target cpu hardware and we would cause it to fire late.
  158. *
  159. * Called with cpu_base->lock of target cpu held.
  160. */
  161. static int
  162. hrtimer_check_target(struct hrtimer *timer, struct hrtimer_clock_base *new_base)
  163. {
  164. #ifdef CONFIG_HIGH_RES_TIMERS
  165. ktime_t expires;
  166. if (!new_base->cpu_base->hres_active)
  167. return 0;
  168. expires = ktime_sub(hrtimer_get_expires(timer), new_base->offset);
  169. return expires.tv64 <= new_base->cpu_base->expires_next.tv64;
  170. #else
  171. return 0;
  172. #endif
  173. }
  174. /*
  175. * Switch the timer base to the current CPU when possible.
  176. */
  177. static inline struct hrtimer_clock_base *
  178. switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
  179. int pinned)
  180. {
  181. struct hrtimer_clock_base *new_base;
  182. struct hrtimer_cpu_base *new_cpu_base;
  183. int this_cpu = smp_processor_id();
  184. int cpu = get_nohz_timer_target(pinned);
  185. int basenum = base->index;
  186. again:
  187. new_cpu_base = &per_cpu(hrtimer_bases, cpu);
  188. new_base = &new_cpu_base->clock_base[basenum];
  189. if (base != new_base) {
  190. /*
  191. * We are trying to move timer to new_base.
  192. * However we can't change timer's base while it is running,
  193. * so we keep it on the same CPU. No hassle vs. reprogramming
  194. * the event source in the high resolution case. The softirq
  195. * code will take care of this when the timer function has
  196. * completed. There is no conflict as we hold the lock until
  197. * the timer is enqueued.
  198. */
  199. if (unlikely(hrtimer_callback_running(timer)))
  200. return base;
  201. /* See the comment in lock_timer_base() */
  202. timer->base = NULL;
  203. raw_spin_unlock(&base->cpu_base->lock);
  204. raw_spin_lock(&new_base->cpu_base->lock);
  205. if (cpu != this_cpu && hrtimer_check_target(timer, new_base)) {
  206. cpu = this_cpu;
  207. raw_spin_unlock(&new_base->cpu_base->lock);
  208. raw_spin_lock(&base->cpu_base->lock);
  209. timer->base = base;
  210. goto again;
  211. }
  212. timer->base = new_base;
  213. } else {
  214. if (cpu != this_cpu && hrtimer_check_target(timer, new_base)) {
  215. cpu = this_cpu;
  216. goto again;
  217. }
  218. }
  219. return new_base;
  220. }
  221. #else /* CONFIG_SMP */
  222. static inline struct hrtimer_clock_base *
  223. lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
  224. {
  225. struct hrtimer_clock_base *base = timer->base;
  226. raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
  227. return base;
  228. }
  229. # define switch_hrtimer_base(t, b, p) (b)
  230. #endif /* !CONFIG_SMP */
  231. /*
  232. * Functions for the union type storage format of ktime_t which are
  233. * too large for inlining:
  234. */
  235. #if BITS_PER_LONG < 64
  236. /*
  237. * Divide a ktime value by a nanosecond value
  238. */
  239. s64 __ktime_divns(const ktime_t kt, s64 div)
  240. {
  241. int sft = 0;
  242. s64 dclc;
  243. u64 tmp;
  244. dclc = ktime_to_ns(kt);
  245. tmp = dclc < 0 ? -dclc : dclc;
  246. /* Make sure the divisor is less than 2^32: */
  247. while (div >> 32) {
  248. sft++;
  249. div >>= 1;
  250. }
  251. tmp >>= sft;
  252. do_div(tmp, (unsigned long) div);
  253. return dclc < 0 ? -tmp : tmp;
  254. }
  255. EXPORT_SYMBOL_GPL(__ktime_divns);
  256. #endif /* BITS_PER_LONG >= 64 */
  257. /*
  258. * Add two ktime values and do a safety check for overflow:
  259. */
  260. ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
  261. {
  262. ktime_t res = ktime_add(lhs, rhs);
  263. /*
  264. * We use KTIME_SEC_MAX here, the maximum timeout which we can
  265. * return to user space in a timespec:
  266. */
  267. if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
  268. res = ktime_set(KTIME_SEC_MAX, 0);
  269. return res;
  270. }
  271. EXPORT_SYMBOL_GPL(ktime_add_safe);
  272. #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
  273. static struct debug_obj_descr hrtimer_debug_descr;
  274. static void *hrtimer_debug_hint(void *addr)
  275. {
  276. return ((struct hrtimer *) addr)->function;
  277. }
  278. /*
  279. * fixup_init is called when:
  280. * - an active object is initialized
  281. */
  282. static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
  283. {
  284. struct hrtimer *timer = addr;
  285. switch (state) {
  286. case ODEBUG_STATE_ACTIVE:
  287. hrtimer_cancel(timer);
  288. debug_object_init(timer, &hrtimer_debug_descr);
  289. return 1;
  290. default:
  291. return 0;
  292. }
  293. }
  294. /*
  295. * fixup_activate is called when:
  296. * - an active object is activated
  297. * - an unknown object is activated (might be a statically initialized object)
  298. */
  299. static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
  300. {
  301. switch (state) {
  302. case ODEBUG_STATE_NOTAVAILABLE:
  303. WARN_ON_ONCE(1);
  304. return 0;
  305. case ODEBUG_STATE_ACTIVE:
  306. WARN_ON(1);
  307. default:
  308. return 0;
  309. }
  310. }
  311. /*
  312. * fixup_free is called when:
  313. * - an active object is freed
  314. */
  315. static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
  316. {
  317. struct hrtimer *timer = addr;
  318. switch (state) {
  319. case ODEBUG_STATE_ACTIVE:
  320. hrtimer_cancel(timer);
  321. debug_object_free(timer, &hrtimer_debug_descr);
  322. return 1;
  323. default:
  324. return 0;
  325. }
  326. }
  327. static struct debug_obj_descr hrtimer_debug_descr = {
  328. .name = "hrtimer",
  329. .debug_hint = hrtimer_debug_hint,
  330. .fixup_init = hrtimer_fixup_init,
  331. .fixup_activate = hrtimer_fixup_activate,
  332. .fixup_free = hrtimer_fixup_free,
  333. };
  334. static inline void debug_hrtimer_init(struct hrtimer *timer)
  335. {
  336. debug_object_init(timer, &hrtimer_debug_descr);
  337. }
  338. static inline void debug_hrtimer_activate(struct hrtimer *timer)
  339. {
  340. debug_object_activate(timer, &hrtimer_debug_descr);
  341. }
  342. static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
  343. {
  344. debug_object_deactivate(timer, &hrtimer_debug_descr);
  345. }
  346. static inline void debug_hrtimer_free(struct hrtimer *timer)
  347. {
  348. debug_object_free(timer, &hrtimer_debug_descr);
  349. }
  350. static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  351. enum hrtimer_mode mode);
  352. void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
  353. enum hrtimer_mode mode)
  354. {
  355. debug_object_init_on_stack(timer, &hrtimer_debug_descr);
  356. __hrtimer_init(timer, clock_id, mode);
  357. }
  358. EXPORT_SYMBOL_GPL(hrtimer_init_on_stack);
  359. void destroy_hrtimer_on_stack(struct hrtimer *timer)
  360. {
  361. debug_object_free(timer, &hrtimer_debug_descr);
  362. }
  363. #else
  364. static inline void debug_hrtimer_init(struct hrtimer *timer) { }
  365. static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
  366. static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
  367. #endif
  368. static inline void
  369. debug_init(struct hrtimer *timer, clockid_t clockid,
  370. enum hrtimer_mode mode)
  371. {
  372. debug_hrtimer_init(timer);
  373. trace_hrtimer_init(timer, clockid, mode);
  374. }
  375. static inline void debug_activate(struct hrtimer *timer)
  376. {
  377. debug_hrtimer_activate(timer);
  378. trace_hrtimer_start(timer);
  379. }
  380. static inline void debug_deactivate(struct hrtimer *timer)
  381. {
  382. debug_hrtimer_deactivate(timer);
  383. trace_hrtimer_cancel(timer);
  384. }
  385. #if defined(CONFIG_NO_HZ_COMMON) || defined(CONFIG_HIGH_RES_TIMERS)
  386. static ktime_t __hrtimer_get_next_event(struct hrtimer_cpu_base *cpu_base)
  387. {
  388. struct hrtimer_clock_base *base = cpu_base->clock_base;
  389. ktime_t expires, expires_next = { .tv64 = KTIME_MAX };
  390. int i;
  391. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
  392. struct timerqueue_node *next;
  393. struct hrtimer *timer;
  394. next = timerqueue_getnext(&base->active);
  395. if (!next)
  396. continue;
  397. timer = container_of(next, struct hrtimer, node);
  398. expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
  399. if (expires.tv64 < expires_next.tv64)
  400. expires_next = expires;
  401. }
  402. /*
  403. * clock_was_set() might have changed base->offset of any of
  404. * the clock bases so the result might be negative. Fix it up
  405. * to prevent a false positive in clockevents_program_event().
  406. */
  407. if (expires_next.tv64 < 0)
  408. expires_next.tv64 = 0;
  409. return expires_next;
  410. }
  411. #endif
  412. /* High resolution timer related functions */
  413. #ifdef CONFIG_HIGH_RES_TIMERS
  414. /*
  415. * High resolution timer enabled ?
  416. */
  417. static int hrtimer_hres_enabled __read_mostly = 1;
  418. /*
  419. * Enable / Disable high resolution mode
  420. */
  421. static int __init setup_hrtimer_hres(char *str)
  422. {
  423. if (!strcmp(str, "off"))
  424. hrtimer_hres_enabled = 0;
  425. else if (!strcmp(str, "on"))
  426. hrtimer_hres_enabled = 1;
  427. else
  428. return 0;
  429. return 1;
  430. }
  431. __setup("highres=", setup_hrtimer_hres);
  432. /*
  433. * hrtimer_high_res_enabled - query, if the highres mode is enabled
  434. */
  435. static inline int hrtimer_is_hres_enabled(void)
  436. {
  437. return hrtimer_hres_enabled;
  438. }
  439. /*
  440. * Is the high resolution mode active ?
  441. */
  442. static inline int hrtimer_hres_active(void)
  443. {
  444. return __this_cpu_read(hrtimer_bases.hres_active);
  445. }
  446. /*
  447. * Reprogram the event source with checking both queues for the
  448. * next event
  449. * Called with interrupts disabled and base->lock held
  450. */
  451. static void
  452. hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
  453. {
  454. ktime_t expires_next = __hrtimer_get_next_event(cpu_base);
  455. if (skip_equal && expires_next.tv64 == cpu_base->expires_next.tv64)
  456. return;
  457. cpu_base->expires_next.tv64 = expires_next.tv64;
  458. /*
  459. * If a hang was detected in the last timer interrupt then we
  460. * leave the hang delay active in the hardware. We want the
  461. * system to make progress. That also prevents the following
  462. * scenario:
  463. * T1 expires 50ms from now
  464. * T2 expires 5s from now
  465. *
  466. * T1 is removed, so this code is called and would reprogram
  467. * the hardware to 5s from now. Any hrtimer_start after that
  468. * will not reprogram the hardware due to hang_detected being
  469. * set. So we'd effectivly block all timers until the T2 event
  470. * fires.
  471. */
  472. if (cpu_base->hang_detected)
  473. return;
  474. if (cpu_base->expires_next.tv64 != KTIME_MAX)
  475. tick_program_event(cpu_base->expires_next, 1);
  476. }
  477. /*
  478. * Shared reprogramming for clock_realtime and clock_monotonic
  479. *
  480. * When a timer is enqueued and expires earlier than the already enqueued
  481. * timers, we have to check, whether it expires earlier than the timer for
  482. * which the clock event device was armed.
  483. *
  484. * Note, that in case the state has HRTIMER_STATE_CALLBACK set, no reprogramming
  485. * and no expiry check happens. The timer gets enqueued into the rbtree. The
  486. * reprogramming and expiry check is done in the hrtimer_interrupt or in the
  487. * softirq.
  488. *
  489. * Called with interrupts disabled and base->cpu_base.lock held
  490. */
  491. static int hrtimer_reprogram(struct hrtimer *timer,
  492. struct hrtimer_clock_base *base)
  493. {
  494. struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
  495. ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
  496. int res;
  497. WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
  498. /*
  499. * When the callback is running, we do not reprogram the clock event
  500. * device. The timer callback is either running on a different CPU or
  501. * the callback is executed in the hrtimer_interrupt context. The
  502. * reprogramming is handled either by the softirq, which called the
  503. * callback or at the end of the hrtimer_interrupt.
  504. */
  505. if (hrtimer_callback_running(timer))
  506. return 0;
  507. /*
  508. * CLOCK_REALTIME timer might be requested with an absolute
  509. * expiry time which is less than base->offset. Nothing wrong
  510. * about that, just avoid to call into the tick code, which
  511. * has now objections against negative expiry values.
  512. */
  513. if (expires.tv64 < 0)
  514. return -ETIME;
  515. if (expires.tv64 >= cpu_base->expires_next.tv64)
  516. return 0;
  517. /*
  518. * When the target cpu of the timer is currently executing
  519. * hrtimer_interrupt(), then we do not touch the clock event
  520. * device. hrtimer_interrupt() will reevaluate all clock bases
  521. * before reprogramming the device.
  522. */
  523. if (cpu_base->in_hrtirq)
  524. return 0;
  525. /*
  526. * If a hang was detected in the last timer interrupt then we
  527. * do not schedule a timer which is earlier than the expiry
  528. * which we enforced in the hang detection. We want the system
  529. * to make progress.
  530. */
  531. if (cpu_base->hang_detected)
  532. return 0;
  533. /*
  534. * Clockevents returns -ETIME, when the event was in the past.
  535. */
  536. res = tick_program_event(expires, 0);
  537. if (!IS_ERR_VALUE(res))
  538. cpu_base->expires_next = expires;
  539. return res;
  540. }
  541. /*
  542. * Initialize the high resolution related parts of cpu_base
  543. */
  544. static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
  545. {
  546. base->expires_next.tv64 = KTIME_MAX;
  547. base->hres_active = 0;
  548. }
  549. static inline ktime_t hrtimer_update_base(struct hrtimer_cpu_base *base)
  550. {
  551. ktime_t *offs_real = &base->clock_base[HRTIMER_BASE_REALTIME].offset;
  552. ktime_t *offs_boot = &base->clock_base[HRTIMER_BASE_BOOTTIME].offset;
  553. ktime_t *offs_tai = &base->clock_base[HRTIMER_BASE_TAI].offset;
  554. return ktime_get_update_offsets_now(offs_real, offs_boot, offs_tai);
  555. }
  556. /*
  557. * Retrigger next event is called after clock was set
  558. *
  559. * Called with interrupts disabled via on_each_cpu()
  560. */
  561. static void retrigger_next_event(void *arg)
  562. {
  563. struct hrtimer_cpu_base *base = this_cpu_ptr(&hrtimer_bases);
  564. if (!hrtimer_hres_active())
  565. return;
  566. raw_spin_lock(&base->lock);
  567. hrtimer_update_base(base);
  568. hrtimer_force_reprogram(base, 0);
  569. raw_spin_unlock(&base->lock);
  570. }
  571. /*
  572. * Switch to high resolution mode
  573. */
  574. static int hrtimer_switch_to_hres(void)
  575. {
  576. int i, cpu = smp_processor_id();
  577. struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
  578. unsigned long flags;
  579. if (base->hres_active)
  580. return 1;
  581. local_irq_save(flags);
  582. if (tick_init_highres()) {
  583. local_irq_restore(flags);
  584. printk(KERN_WARNING "Could not switch to high resolution "
  585. "mode on CPU %d\n", cpu);
  586. return 0;
  587. }
  588. base->hres_active = 1;
  589. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
  590. base->clock_base[i].resolution = KTIME_HIGH_RES;
  591. tick_setup_sched_timer();
  592. /* "Retrigger" the interrupt to get things going */
  593. retrigger_next_event(NULL);
  594. local_irq_restore(flags);
  595. return 1;
  596. }
  597. static void clock_was_set_work(struct work_struct *work)
  598. {
  599. clock_was_set();
  600. }
  601. static DECLARE_WORK(hrtimer_work, clock_was_set_work);
  602. /*
  603. * Called from timekeeping and resume code to reprogramm the hrtimer
  604. * interrupt device on all cpus.
  605. */
  606. void clock_was_set_delayed(void)
  607. {
  608. schedule_work(&hrtimer_work);
  609. }
  610. #else
  611. static inline int hrtimer_hres_active(void) { return 0; }
  612. static inline int hrtimer_is_hres_enabled(void) { return 0; }
  613. static inline int hrtimer_switch_to_hres(void) { return 0; }
  614. static inline void
  615. hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
  616. static inline int hrtimer_reprogram(struct hrtimer *timer,
  617. struct hrtimer_clock_base *base)
  618. {
  619. return 0;
  620. }
  621. static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
  622. static inline void retrigger_next_event(void *arg) { }
  623. #endif /* CONFIG_HIGH_RES_TIMERS */
  624. /*
  625. * Clock realtime was set
  626. *
  627. * Change the offset of the realtime clock vs. the monotonic
  628. * clock.
  629. *
  630. * We might have to reprogram the high resolution timer interrupt. On
  631. * SMP we call the architecture specific code to retrigger _all_ high
  632. * resolution timer interrupts. On UP we just disable interrupts and
  633. * call the high resolution interrupt code.
  634. */
  635. void clock_was_set(void)
  636. {
  637. #ifdef CONFIG_HIGH_RES_TIMERS
  638. /* Retrigger the CPU local events everywhere */
  639. on_each_cpu(retrigger_next_event, NULL, 1);
  640. #endif
  641. timerfd_clock_was_set();
  642. }
  643. /*
  644. * During resume we might have to reprogram the high resolution timer
  645. * interrupt on all online CPUs. However, all other CPUs will be
  646. * stopped with IRQs interrupts disabled so the clock_was_set() call
  647. * must be deferred.
  648. */
  649. void hrtimers_resume(void)
  650. {
  651. WARN_ONCE(!irqs_disabled(),
  652. KERN_INFO "hrtimers_resume() called with IRQs enabled!");
  653. /* Retrigger on the local CPU */
  654. retrigger_next_event(NULL);
  655. /* And schedule a retrigger for all others */
  656. clock_was_set_delayed();
  657. }
  658. static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
  659. {
  660. #ifdef CONFIG_TIMER_STATS
  661. if (timer->start_site)
  662. return;
  663. timer->start_site = __builtin_return_address(0);
  664. memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
  665. timer->start_pid = current->pid;
  666. #endif
  667. }
  668. static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
  669. {
  670. #ifdef CONFIG_TIMER_STATS
  671. timer->start_site = NULL;
  672. #endif
  673. }
  674. static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
  675. {
  676. #ifdef CONFIG_TIMER_STATS
  677. if (likely(!timer_stats_active))
  678. return;
  679. timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
  680. timer->function, timer->start_comm, 0);
  681. #endif
  682. }
  683. /*
  684. * Counterpart to lock_hrtimer_base above:
  685. */
  686. static inline
  687. void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
  688. {
  689. raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
  690. }
  691. /**
  692. * hrtimer_forward - forward the timer expiry
  693. * @timer: hrtimer to forward
  694. * @now: forward past this time
  695. * @interval: the interval to forward
  696. *
  697. * Forward the timer expiry so it will expire in the future.
  698. * Returns the number of overruns.
  699. */
  700. u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
  701. {
  702. u64 orun = 1;
  703. ktime_t delta;
  704. delta = ktime_sub(now, hrtimer_get_expires(timer));
  705. if (delta.tv64 < 0)
  706. return 0;
  707. if (interval.tv64 < timer->base->resolution.tv64)
  708. interval.tv64 = timer->base->resolution.tv64;
  709. if (unlikely(delta.tv64 >= interval.tv64)) {
  710. s64 incr = ktime_to_ns(interval);
  711. orun = ktime_divns(delta, incr);
  712. hrtimer_add_expires_ns(timer, incr * orun);
  713. if (hrtimer_get_expires_tv64(timer) > now.tv64)
  714. return orun;
  715. /*
  716. * This (and the ktime_add() below) is the
  717. * correction for exact:
  718. */
  719. orun++;
  720. }
  721. hrtimer_add_expires(timer, interval);
  722. return orun;
  723. }
  724. EXPORT_SYMBOL_GPL(hrtimer_forward);
  725. /*
  726. * enqueue_hrtimer - internal function to (re)start a timer
  727. *
  728. * The timer is inserted in expiry order. Insertion into the
  729. * red black tree is O(log(n)). Must hold the base lock.
  730. *
  731. * Returns 1 when the new timer is the leftmost timer in the tree.
  732. */
  733. static int enqueue_hrtimer(struct hrtimer *timer,
  734. struct hrtimer_clock_base *base)
  735. {
  736. debug_activate(timer);
  737. timerqueue_add(&base->active, &timer->node);
  738. base->cpu_base->active_bases |= 1 << base->index;
  739. /*
  740. * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
  741. * state of a possibly running callback.
  742. */
  743. timer->state |= HRTIMER_STATE_ENQUEUED;
  744. return (&timer->node == base->active.next);
  745. }
  746. /*
  747. * __remove_hrtimer - internal function to remove a timer
  748. *
  749. * Caller must hold the base lock.
  750. *
  751. * High resolution timer mode reprograms the clock event device when the
  752. * timer is the one which expires next. The caller can disable this by setting
  753. * reprogram to zero. This is useful, when the context does a reprogramming
  754. * anyway (e.g. timer interrupt)
  755. */
  756. static void __remove_hrtimer(struct hrtimer *timer,
  757. struct hrtimer_clock_base *base,
  758. unsigned long newstate, int reprogram)
  759. {
  760. struct timerqueue_node *next_timer;
  761. if (!(timer->state & HRTIMER_STATE_ENQUEUED))
  762. goto out;
  763. next_timer = timerqueue_getnext(&base->active);
  764. timerqueue_del(&base->active, &timer->node);
  765. if (&timer->node == next_timer) {
  766. #ifdef CONFIG_HIGH_RES_TIMERS
  767. /* Reprogram the clock event device. if enabled */
  768. if (reprogram && hrtimer_hres_active()) {
  769. ktime_t expires;
  770. expires = ktime_sub(hrtimer_get_expires(timer),
  771. base->offset);
  772. if (base->cpu_base->expires_next.tv64 == expires.tv64)
  773. hrtimer_force_reprogram(base->cpu_base, 1);
  774. }
  775. #endif
  776. }
  777. if (!timerqueue_getnext(&base->active))
  778. base->cpu_base->active_bases &= ~(1 << base->index);
  779. out:
  780. timer->state = newstate;
  781. }
  782. /*
  783. * remove hrtimer, called with base lock held
  784. */
  785. static inline int
  786. remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
  787. {
  788. if (hrtimer_is_queued(timer)) {
  789. unsigned long state;
  790. int reprogram;
  791. /*
  792. * Remove the timer and force reprogramming when high
  793. * resolution mode is active and the timer is on the current
  794. * CPU. If we remove a timer on another CPU, reprogramming is
  795. * skipped. The interrupt event on this CPU is fired and
  796. * reprogramming happens in the interrupt handler. This is a
  797. * rare case and less expensive than a smp call.
  798. */
  799. debug_deactivate(timer);
  800. timer_stats_hrtimer_clear_start_info(timer);
  801. reprogram = base->cpu_base == this_cpu_ptr(&hrtimer_bases);
  802. /*
  803. * We must preserve the CALLBACK state flag here,
  804. * otherwise we could move the timer base in
  805. * switch_hrtimer_base.
  806. */
  807. state = timer->state & HRTIMER_STATE_CALLBACK;
  808. __remove_hrtimer(timer, base, state, reprogram);
  809. return 1;
  810. }
  811. return 0;
  812. }
  813. int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
  814. unsigned long delta_ns, const enum hrtimer_mode mode,
  815. int wakeup)
  816. {
  817. struct hrtimer_clock_base *base, *new_base;
  818. unsigned long flags;
  819. int ret, leftmost;
  820. base = lock_hrtimer_base(timer, &flags);
  821. /* Remove an active timer from the queue: */
  822. ret = remove_hrtimer(timer, base);
  823. if (mode & HRTIMER_MODE_REL) {
  824. tim = ktime_add_safe(tim, base->get_time());
  825. /*
  826. * CONFIG_TIME_LOW_RES is a temporary way for architectures
  827. * to signal that they simply return xtime in
  828. * do_gettimeoffset(). In this case we want to round up by
  829. * resolution when starting a relative timer, to avoid short
  830. * timeouts. This will go away with the GTOD framework.
  831. */
  832. #ifdef CONFIG_TIME_LOW_RES
  833. tim = ktime_add_safe(tim, base->resolution);
  834. #endif
  835. }
  836. hrtimer_set_expires_range_ns(timer, tim, delta_ns);
  837. /* Switch the timer base, if necessary: */
  838. new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
  839. timer_stats_hrtimer_set_start_info(timer);
  840. leftmost = enqueue_hrtimer(timer, new_base);
  841. if (!leftmost) {
  842. unlock_hrtimer_base(timer, &flags);
  843. return ret;
  844. }
  845. if (!hrtimer_is_hres_active(timer)) {
  846. /*
  847. * Kick to reschedule the next tick to handle the new timer
  848. * on dynticks target.
  849. */
  850. wake_up_nohz_cpu(new_base->cpu_base->cpu);
  851. } else if (new_base->cpu_base == this_cpu_ptr(&hrtimer_bases) &&
  852. hrtimer_reprogram(timer, new_base)) {
  853. /*
  854. * Only allow reprogramming if the new base is on this CPU.
  855. * (it might still be on another CPU if the timer was pending)
  856. *
  857. * XXX send_remote_softirq() ?
  858. */
  859. if (wakeup) {
  860. /*
  861. * We need to drop cpu_base->lock to avoid a
  862. * lock ordering issue vs. rq->lock.
  863. */
  864. raw_spin_unlock(&new_base->cpu_base->lock);
  865. raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  866. local_irq_restore(flags);
  867. return ret;
  868. } else {
  869. __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  870. }
  871. }
  872. unlock_hrtimer_base(timer, &flags);
  873. return ret;
  874. }
  875. EXPORT_SYMBOL_GPL(__hrtimer_start_range_ns);
  876. /**
  877. * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
  878. * @timer: the timer to be added
  879. * @tim: expiry time
  880. * @delta_ns: "slack" range for the timer
  881. * @mode: expiry mode: absolute (HRTIMER_MODE_ABS) or
  882. * relative (HRTIMER_MODE_REL)
  883. *
  884. * Returns:
  885. * 0 on success
  886. * 1 when the timer was active
  887. */
  888. int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
  889. unsigned long delta_ns, const enum hrtimer_mode mode)
  890. {
  891. return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1);
  892. }
  893. EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
  894. /**
  895. * hrtimer_start - (re)start an hrtimer on the current CPU
  896. * @timer: the timer to be added
  897. * @tim: expiry time
  898. * @mode: expiry mode: absolute (HRTIMER_MODE_ABS) or
  899. * relative (HRTIMER_MODE_REL)
  900. *
  901. * Returns:
  902. * 0 on success
  903. * 1 when the timer was active
  904. */
  905. int
  906. hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
  907. {
  908. return __hrtimer_start_range_ns(timer, tim, 0, mode, 1);
  909. }
  910. EXPORT_SYMBOL_GPL(hrtimer_start);
  911. /**
  912. * hrtimer_try_to_cancel - try to deactivate a timer
  913. * @timer: hrtimer to stop
  914. *
  915. * Returns:
  916. * 0 when the timer was not active
  917. * 1 when the timer was active
  918. * -1 when the timer is currently excuting the callback function and
  919. * cannot be stopped
  920. */
  921. int hrtimer_try_to_cancel(struct hrtimer *timer)
  922. {
  923. struct hrtimer_clock_base *base;
  924. unsigned long flags;
  925. int ret = -1;
  926. base = lock_hrtimer_base(timer, &flags);
  927. if (!hrtimer_callback_running(timer))
  928. ret = remove_hrtimer(timer, base);
  929. unlock_hrtimer_base(timer, &flags);
  930. return ret;
  931. }
  932. EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
  933. /**
  934. * hrtimer_cancel - cancel a timer and wait for the handler to finish.
  935. * @timer: the timer to be cancelled
  936. *
  937. * Returns:
  938. * 0 when the timer was not active
  939. * 1 when the timer was active
  940. */
  941. int hrtimer_cancel(struct hrtimer *timer)
  942. {
  943. for (;;) {
  944. int ret = hrtimer_try_to_cancel(timer);
  945. if (ret >= 0)
  946. return ret;
  947. cpu_relax();
  948. }
  949. }
  950. EXPORT_SYMBOL_GPL(hrtimer_cancel);
  951. /**
  952. * hrtimer_get_remaining - get remaining time for the timer
  953. * @timer: the timer to read
  954. */
  955. ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
  956. {
  957. unsigned long flags;
  958. ktime_t rem;
  959. lock_hrtimer_base(timer, &flags);
  960. rem = hrtimer_expires_remaining(timer);
  961. unlock_hrtimer_base(timer, &flags);
  962. return rem;
  963. }
  964. EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
  965. #ifdef CONFIG_NO_HZ_COMMON
  966. /**
  967. * hrtimer_get_next_event - get the time until next expiry event
  968. *
  969. * Returns the delta to the next expiry event or KTIME_MAX if no timer
  970. * is pending.
  971. */
  972. ktime_t hrtimer_get_next_event(void)
  973. {
  974. struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
  975. ktime_t mindelta = { .tv64 = KTIME_MAX };
  976. unsigned long flags;
  977. raw_spin_lock_irqsave(&cpu_base->lock, flags);
  978. if (!hrtimer_hres_active())
  979. mindelta = ktime_sub(__hrtimer_get_next_event(cpu_base),
  980. ktime_get());
  981. raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
  982. if (mindelta.tv64 < 0)
  983. mindelta.tv64 = 0;
  984. return mindelta;
  985. }
  986. #endif
  987. static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  988. enum hrtimer_mode mode)
  989. {
  990. struct hrtimer_cpu_base *cpu_base;
  991. int base;
  992. memset(timer, 0, sizeof(struct hrtimer));
  993. cpu_base = raw_cpu_ptr(&hrtimer_bases);
  994. if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
  995. clock_id = CLOCK_MONOTONIC;
  996. base = hrtimer_clockid_to_base(clock_id);
  997. timer->base = &cpu_base->clock_base[base];
  998. timerqueue_init(&timer->node);
  999. #ifdef CONFIG_TIMER_STATS
  1000. timer->start_site = NULL;
  1001. timer->start_pid = -1;
  1002. memset(timer->start_comm, 0, TASK_COMM_LEN);
  1003. #endif
  1004. }
  1005. /**
  1006. * hrtimer_init - initialize a timer to the given clock
  1007. * @timer: the timer to be initialized
  1008. * @clock_id: the clock to be used
  1009. * @mode: timer mode abs/rel
  1010. */
  1011. void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  1012. enum hrtimer_mode mode)
  1013. {
  1014. debug_init(timer, clock_id, mode);
  1015. __hrtimer_init(timer, clock_id, mode);
  1016. }
  1017. EXPORT_SYMBOL_GPL(hrtimer_init);
  1018. /**
  1019. * hrtimer_get_res - get the timer resolution for a clock
  1020. * @which_clock: which clock to query
  1021. * @tp: pointer to timespec variable to store the resolution
  1022. *
  1023. * Store the resolution of the clock selected by @which_clock in the
  1024. * variable pointed to by @tp.
  1025. */
  1026. int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
  1027. {
  1028. struct hrtimer_cpu_base *cpu_base;
  1029. int base = hrtimer_clockid_to_base(which_clock);
  1030. cpu_base = raw_cpu_ptr(&hrtimer_bases);
  1031. *tp = ktime_to_timespec(cpu_base->clock_base[base].resolution);
  1032. return 0;
  1033. }
  1034. EXPORT_SYMBOL_GPL(hrtimer_get_res);
  1035. static void __run_hrtimer(struct hrtimer *timer, ktime_t *now)
  1036. {
  1037. struct hrtimer_clock_base *base = timer->base;
  1038. struct hrtimer_cpu_base *cpu_base = base->cpu_base;
  1039. enum hrtimer_restart (*fn)(struct hrtimer *);
  1040. int restart;
  1041. WARN_ON(!irqs_disabled());
  1042. debug_deactivate(timer);
  1043. __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
  1044. timer_stats_account_hrtimer(timer);
  1045. fn = timer->function;
  1046. /*
  1047. * Because we run timers from hardirq context, there is no chance
  1048. * they get migrated to another cpu, therefore its safe to unlock
  1049. * the timer base.
  1050. */
  1051. raw_spin_unlock(&cpu_base->lock);
  1052. trace_hrtimer_expire_entry(timer, now);
  1053. restart = fn(timer);
  1054. trace_hrtimer_expire_exit(timer);
  1055. raw_spin_lock(&cpu_base->lock);
  1056. /*
  1057. * Note: We clear the CALLBACK bit after enqueue_hrtimer and
  1058. * we do not reprogramm the event hardware. Happens either in
  1059. * hrtimer_start_range_ns() or in hrtimer_interrupt()
  1060. */
  1061. if (restart != HRTIMER_NORESTART) {
  1062. BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
  1063. enqueue_hrtimer(timer, base);
  1064. }
  1065. WARN_ON_ONCE(!(timer->state & HRTIMER_STATE_CALLBACK));
  1066. timer->state &= ~HRTIMER_STATE_CALLBACK;
  1067. }
  1068. #ifdef CONFIG_HIGH_RES_TIMERS
  1069. /*
  1070. * High resolution timer interrupt
  1071. * Called with interrupts disabled
  1072. */
  1073. void hrtimer_interrupt(struct clock_event_device *dev)
  1074. {
  1075. struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
  1076. ktime_t expires_next, now, entry_time, delta;
  1077. int i, retries = 0;
  1078. BUG_ON(!cpu_base->hres_active);
  1079. cpu_base->nr_events++;
  1080. dev->next_event.tv64 = KTIME_MAX;
  1081. raw_spin_lock(&cpu_base->lock);
  1082. entry_time = now = hrtimer_update_base(cpu_base);
  1083. retry:
  1084. cpu_base->in_hrtirq = 1;
  1085. /*
  1086. * We set expires_next to KTIME_MAX here with cpu_base->lock
  1087. * held to prevent that a timer is enqueued in our queue via
  1088. * the migration code. This does not affect enqueueing of
  1089. * timers which run their callback and need to be requeued on
  1090. * this CPU.
  1091. */
  1092. cpu_base->expires_next.tv64 = KTIME_MAX;
  1093. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  1094. struct hrtimer_clock_base *base;
  1095. struct timerqueue_node *node;
  1096. ktime_t basenow;
  1097. if (!(cpu_base->active_bases & (1 << i)))
  1098. continue;
  1099. base = cpu_base->clock_base + i;
  1100. basenow = ktime_add(now, base->offset);
  1101. while ((node = timerqueue_getnext(&base->active))) {
  1102. struct hrtimer *timer;
  1103. timer = container_of(node, struct hrtimer, node);
  1104. /*
  1105. * The immediate goal for using the softexpires is
  1106. * minimizing wakeups, not running timers at the
  1107. * earliest interrupt after their soft expiration.
  1108. * This allows us to avoid using a Priority Search
  1109. * Tree, which can answer a stabbing querry for
  1110. * overlapping intervals and instead use the simple
  1111. * BST we already have.
  1112. * We don't add extra wakeups by delaying timers that
  1113. * are right-of a not yet expired timer, because that
  1114. * timer will have to trigger a wakeup anyway.
  1115. */
  1116. if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer))
  1117. break;
  1118. __run_hrtimer(timer, &basenow);
  1119. }
  1120. }
  1121. /* Reevaluate the clock bases for the next expiry */
  1122. expires_next = __hrtimer_get_next_event(cpu_base);
  1123. /*
  1124. * Store the new expiry value so the migration code can verify
  1125. * against it.
  1126. */
  1127. cpu_base->expires_next = expires_next;
  1128. cpu_base->in_hrtirq = 0;
  1129. raw_spin_unlock(&cpu_base->lock);
  1130. /* Reprogramming necessary ? */
  1131. if (expires_next.tv64 == KTIME_MAX ||
  1132. !tick_program_event(expires_next, 0)) {
  1133. cpu_base->hang_detected = 0;
  1134. return;
  1135. }
  1136. /*
  1137. * The next timer was already expired due to:
  1138. * - tracing
  1139. * - long lasting callbacks
  1140. * - being scheduled away when running in a VM
  1141. *
  1142. * We need to prevent that we loop forever in the hrtimer
  1143. * interrupt routine. We give it 3 attempts to avoid
  1144. * overreacting on some spurious event.
  1145. *
  1146. * Acquire base lock for updating the offsets and retrieving
  1147. * the current time.
  1148. */
  1149. raw_spin_lock(&cpu_base->lock);
  1150. now = hrtimer_update_base(cpu_base);
  1151. cpu_base->nr_retries++;
  1152. if (++retries < 3)
  1153. goto retry;
  1154. /*
  1155. * Give the system a chance to do something else than looping
  1156. * here. We stored the entry time, so we know exactly how long
  1157. * we spent here. We schedule the next event this amount of
  1158. * time away.
  1159. */
  1160. cpu_base->nr_hangs++;
  1161. cpu_base->hang_detected = 1;
  1162. raw_spin_unlock(&cpu_base->lock);
  1163. delta = ktime_sub(now, entry_time);
  1164. if (delta.tv64 > cpu_base->max_hang_time.tv64)
  1165. cpu_base->max_hang_time = delta;
  1166. /*
  1167. * Limit it to a sensible value as we enforce a longer
  1168. * delay. Give the CPU at least 100ms to catch up.
  1169. */
  1170. if (delta.tv64 > 100 * NSEC_PER_MSEC)
  1171. expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC);
  1172. else
  1173. expires_next = ktime_add(now, delta);
  1174. tick_program_event(expires_next, 1);
  1175. printk_once(KERN_WARNING "hrtimer: interrupt took %llu ns\n",
  1176. ktime_to_ns(delta));
  1177. }
  1178. /*
  1179. * local version of hrtimer_peek_ahead_timers() called with interrupts
  1180. * disabled.
  1181. */
  1182. static void __hrtimer_peek_ahead_timers(void)
  1183. {
  1184. struct tick_device *td;
  1185. if (!hrtimer_hres_active())
  1186. return;
  1187. td = this_cpu_ptr(&tick_cpu_device);
  1188. if (td && td->evtdev)
  1189. hrtimer_interrupt(td->evtdev);
  1190. }
  1191. /**
  1192. * hrtimer_peek_ahead_timers -- run soft-expired timers now
  1193. *
  1194. * hrtimer_peek_ahead_timers will peek at the timer queue of
  1195. * the current cpu and check if there are any timers for which
  1196. * the soft expires time has passed. If any such timers exist,
  1197. * they are run immediately and then removed from the timer queue.
  1198. *
  1199. */
  1200. void hrtimer_peek_ahead_timers(void)
  1201. {
  1202. unsigned long flags;
  1203. local_irq_save(flags);
  1204. __hrtimer_peek_ahead_timers();
  1205. local_irq_restore(flags);
  1206. }
  1207. static void run_hrtimer_softirq(struct softirq_action *h)
  1208. {
  1209. hrtimer_peek_ahead_timers();
  1210. }
  1211. #else /* CONFIG_HIGH_RES_TIMERS */
  1212. static inline void __hrtimer_peek_ahead_timers(void) { }
  1213. #endif /* !CONFIG_HIGH_RES_TIMERS */
  1214. /*
  1215. * Called from timer softirq every jiffy, expire hrtimers:
  1216. *
  1217. * For HRT its the fall back code to run the softirq in the timer
  1218. * softirq context in case the hrtimer initialization failed or has
  1219. * not been done yet.
  1220. */
  1221. void hrtimer_run_pending(void)
  1222. {
  1223. if (hrtimer_hres_active())
  1224. return;
  1225. /*
  1226. * This _is_ ugly: We have to check in the softirq context,
  1227. * whether we can switch to highres and / or nohz mode. The
  1228. * clocksource switch happens in the timer interrupt with
  1229. * xtime_lock held. Notification from there only sets the
  1230. * check bit in the tick_oneshot code, otherwise we might
  1231. * deadlock vs. xtime_lock.
  1232. */
  1233. if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
  1234. hrtimer_switch_to_hres();
  1235. }
  1236. /*
  1237. * Called from hardirq context every jiffy
  1238. */
  1239. void hrtimer_run_queues(void)
  1240. {
  1241. struct timerqueue_node *node;
  1242. struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
  1243. struct hrtimer_clock_base *base;
  1244. int index, gettime = 1;
  1245. if (hrtimer_hres_active())
  1246. return;
  1247. for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
  1248. base = &cpu_base->clock_base[index];
  1249. if (!timerqueue_getnext(&base->active))
  1250. continue;
  1251. if (gettime) {
  1252. hrtimer_get_softirq_time(cpu_base);
  1253. gettime = 0;
  1254. }
  1255. raw_spin_lock(&cpu_base->lock);
  1256. while ((node = timerqueue_getnext(&base->active))) {
  1257. struct hrtimer *timer;
  1258. timer = container_of(node, struct hrtimer, node);
  1259. if (base->softirq_time.tv64 <=
  1260. hrtimer_get_expires_tv64(timer))
  1261. break;
  1262. __run_hrtimer(timer, &base->softirq_time);
  1263. }
  1264. raw_spin_unlock(&cpu_base->lock);
  1265. }
  1266. }
  1267. /*
  1268. * Sleep related functions:
  1269. */
  1270. static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
  1271. {
  1272. struct hrtimer_sleeper *t =
  1273. container_of(timer, struct hrtimer_sleeper, timer);
  1274. struct task_struct *task = t->task;
  1275. t->task = NULL;
  1276. if (task)
  1277. wake_up_process(task);
  1278. return HRTIMER_NORESTART;
  1279. }
  1280. void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
  1281. {
  1282. sl->timer.function = hrtimer_wakeup;
  1283. sl->task = task;
  1284. }
  1285. EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
  1286. static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
  1287. {
  1288. hrtimer_init_sleeper(t, current);
  1289. do {
  1290. set_current_state(TASK_INTERRUPTIBLE);
  1291. hrtimer_start_expires(&t->timer, mode);
  1292. if (!hrtimer_active(&t->timer))
  1293. t->task = NULL;
  1294. if (likely(t->task))
  1295. freezable_schedule();
  1296. hrtimer_cancel(&t->timer);
  1297. mode = HRTIMER_MODE_ABS;
  1298. } while (t->task && !signal_pending(current));
  1299. __set_current_state(TASK_RUNNING);
  1300. return t->task == NULL;
  1301. }
  1302. static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
  1303. {
  1304. struct timespec rmt;
  1305. ktime_t rem;
  1306. rem = hrtimer_expires_remaining(timer);
  1307. if (rem.tv64 <= 0)
  1308. return 0;
  1309. rmt = ktime_to_timespec(rem);
  1310. if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
  1311. return -EFAULT;
  1312. return 1;
  1313. }
  1314. long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
  1315. {
  1316. struct hrtimer_sleeper t;
  1317. struct timespec __user *rmtp;
  1318. int ret = 0;
  1319. hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid,
  1320. HRTIMER_MODE_ABS);
  1321. hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
  1322. if (do_nanosleep(&t, HRTIMER_MODE_ABS))
  1323. goto out;
  1324. rmtp = restart->nanosleep.rmtp;
  1325. if (rmtp) {
  1326. ret = update_rmtp(&t.timer, rmtp);
  1327. if (ret <= 0)
  1328. goto out;
  1329. }
  1330. /* The other values in restart are already filled in */
  1331. ret = -ERESTART_RESTARTBLOCK;
  1332. out:
  1333. destroy_hrtimer_on_stack(&t.timer);
  1334. return ret;
  1335. }
  1336. long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  1337. const enum hrtimer_mode mode, const clockid_t clockid)
  1338. {
  1339. struct restart_block *restart;
  1340. struct hrtimer_sleeper t;
  1341. int ret = 0;
  1342. unsigned long slack;
  1343. slack = current->timer_slack_ns;
  1344. if (dl_task(current) || rt_task(current))
  1345. slack = 0;
  1346. hrtimer_init_on_stack(&t.timer, clockid, mode);
  1347. hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
  1348. if (do_nanosleep(&t, mode))
  1349. goto out;
  1350. /* Absolute timers do not update the rmtp value and restart: */
  1351. if (mode == HRTIMER_MODE_ABS) {
  1352. ret = -ERESTARTNOHAND;
  1353. goto out;
  1354. }
  1355. if (rmtp) {
  1356. ret = update_rmtp(&t.timer, rmtp);
  1357. if (ret <= 0)
  1358. goto out;
  1359. }
  1360. restart = &current->restart_block;
  1361. restart->fn = hrtimer_nanosleep_restart;
  1362. restart->nanosleep.clockid = t.timer.base->clockid;
  1363. restart->nanosleep.rmtp = rmtp;
  1364. restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
  1365. ret = -ERESTART_RESTARTBLOCK;
  1366. out:
  1367. destroy_hrtimer_on_stack(&t.timer);
  1368. return ret;
  1369. }
  1370. SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
  1371. struct timespec __user *, rmtp)
  1372. {
  1373. struct timespec tu;
  1374. if (copy_from_user(&tu, rqtp, sizeof(tu)))
  1375. return -EFAULT;
  1376. if (!timespec_valid(&tu))
  1377. return -EINVAL;
  1378. return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
  1379. }
  1380. /*
  1381. * Functions related to boot-time initialization:
  1382. */
  1383. static void init_hrtimers_cpu(int cpu)
  1384. {
  1385. struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
  1386. int i;
  1387. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  1388. cpu_base->clock_base[i].cpu_base = cpu_base;
  1389. timerqueue_init_head(&cpu_base->clock_base[i].active);
  1390. }
  1391. cpu_base->cpu = cpu;
  1392. hrtimer_init_hres(cpu_base);
  1393. }
  1394. #ifdef CONFIG_HOTPLUG_CPU
  1395. static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
  1396. struct hrtimer_clock_base *new_base)
  1397. {
  1398. struct hrtimer *timer;
  1399. struct timerqueue_node *node;
  1400. while ((node = timerqueue_getnext(&old_base->active))) {
  1401. timer = container_of(node, struct hrtimer, node);
  1402. BUG_ON(hrtimer_callback_running(timer));
  1403. debug_deactivate(timer);
  1404. /*
  1405. * Mark it as STATE_MIGRATE not INACTIVE otherwise the
  1406. * timer could be seen as !active and just vanish away
  1407. * under us on another CPU
  1408. */
  1409. __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
  1410. timer->base = new_base;
  1411. /*
  1412. * Enqueue the timers on the new cpu. This does not
  1413. * reprogram the event device in case the timer
  1414. * expires before the earliest on this CPU, but we run
  1415. * hrtimer_interrupt after we migrated everything to
  1416. * sort out already expired timers and reprogram the
  1417. * event device.
  1418. */
  1419. enqueue_hrtimer(timer, new_base);
  1420. /* Clear the migration state bit */
  1421. timer->state &= ~HRTIMER_STATE_MIGRATE;
  1422. }
  1423. }
  1424. static void migrate_hrtimers(int scpu)
  1425. {
  1426. struct hrtimer_cpu_base *old_base, *new_base;
  1427. int i;
  1428. BUG_ON(cpu_online(scpu));
  1429. tick_cancel_sched_timer(scpu);
  1430. local_irq_disable();
  1431. old_base = &per_cpu(hrtimer_bases, scpu);
  1432. new_base = this_cpu_ptr(&hrtimer_bases);
  1433. /*
  1434. * The caller is globally serialized and nobody else
  1435. * takes two locks at once, deadlock is not possible.
  1436. */
  1437. raw_spin_lock(&new_base->lock);
  1438. raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
  1439. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  1440. migrate_hrtimer_list(&old_base->clock_base[i],
  1441. &new_base->clock_base[i]);
  1442. }
  1443. raw_spin_unlock(&old_base->lock);
  1444. raw_spin_unlock(&new_base->lock);
  1445. /* Check, if we got expired work to do */
  1446. __hrtimer_peek_ahead_timers();
  1447. local_irq_enable();
  1448. }
  1449. #endif /* CONFIG_HOTPLUG_CPU */
  1450. static int hrtimer_cpu_notify(struct notifier_block *self,
  1451. unsigned long action, void *hcpu)
  1452. {
  1453. int scpu = (long)hcpu;
  1454. switch (action) {
  1455. case CPU_UP_PREPARE:
  1456. case CPU_UP_PREPARE_FROZEN:
  1457. init_hrtimers_cpu(scpu);
  1458. break;
  1459. #ifdef CONFIG_HOTPLUG_CPU
  1460. case CPU_DEAD:
  1461. case CPU_DEAD_FROZEN:
  1462. migrate_hrtimers(scpu);
  1463. break;
  1464. #endif
  1465. default:
  1466. break;
  1467. }
  1468. return NOTIFY_OK;
  1469. }
  1470. static struct notifier_block hrtimers_nb = {
  1471. .notifier_call = hrtimer_cpu_notify,
  1472. };
  1473. void __init hrtimers_init(void)
  1474. {
  1475. hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
  1476. (void *)(long)smp_processor_id());
  1477. register_cpu_notifier(&hrtimers_nb);
  1478. #ifdef CONFIG_HIGH_RES_TIMERS
  1479. open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
  1480. #endif
  1481. }
  1482. /**
  1483. * schedule_hrtimeout_range_clock - sleep until timeout
  1484. * @expires: timeout value (ktime_t)
  1485. * @delta: slack in expires timeout (ktime_t)
  1486. * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
  1487. * @clock: timer clock, CLOCK_MONOTONIC or CLOCK_REALTIME
  1488. */
  1489. int __sched
  1490. schedule_hrtimeout_range_clock(ktime_t *expires, unsigned long delta,
  1491. const enum hrtimer_mode mode, int clock)
  1492. {
  1493. struct hrtimer_sleeper t;
  1494. /*
  1495. * Optimize when a zero timeout value is given. It does not
  1496. * matter whether this is an absolute or a relative time.
  1497. */
  1498. if (expires && !expires->tv64) {
  1499. __set_current_state(TASK_RUNNING);
  1500. return 0;
  1501. }
  1502. /*
  1503. * A NULL parameter means "infinite"
  1504. */
  1505. if (!expires) {
  1506. schedule();
  1507. return -EINTR;
  1508. }
  1509. hrtimer_init_on_stack(&t.timer, clock, mode);
  1510. hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
  1511. hrtimer_init_sleeper(&t, current);
  1512. hrtimer_start_expires(&t.timer, mode);
  1513. if (!hrtimer_active(&t.timer))
  1514. t.task = NULL;
  1515. if (likely(t.task))
  1516. schedule();
  1517. hrtimer_cancel(&t.timer);
  1518. destroy_hrtimer_on_stack(&t.timer);
  1519. __set_current_state(TASK_RUNNING);
  1520. return !t.task ? 0 : -EINTR;
  1521. }
  1522. /**
  1523. * schedule_hrtimeout_range - sleep until timeout
  1524. * @expires: timeout value (ktime_t)
  1525. * @delta: slack in expires timeout (ktime_t)
  1526. * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
  1527. *
  1528. * Make the current task sleep until the given expiry time has
  1529. * elapsed. The routine will return immediately unless
  1530. * the current task state has been set (see set_current_state()).
  1531. *
  1532. * The @delta argument gives the kernel the freedom to schedule the
  1533. * actual wakeup to a time that is both power and performance friendly.
  1534. * The kernel give the normal best effort behavior for "@expires+@delta",
  1535. * but may decide to fire the timer earlier, but no earlier than @expires.
  1536. *
  1537. * You can set the task state as follows -
  1538. *
  1539. * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
  1540. * pass before the routine returns.
  1541. *
  1542. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  1543. * delivered to the current task.
  1544. *
  1545. * The current task state is guaranteed to be TASK_RUNNING when this
  1546. * routine returns.
  1547. *
  1548. * Returns 0 when the timer has expired otherwise -EINTR
  1549. */
  1550. int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
  1551. const enum hrtimer_mode mode)
  1552. {
  1553. return schedule_hrtimeout_range_clock(expires, delta, mode,
  1554. CLOCK_MONOTONIC);
  1555. }
  1556. EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
  1557. /**
  1558. * schedule_hrtimeout - sleep until timeout
  1559. * @expires: timeout value (ktime_t)
  1560. * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
  1561. *
  1562. * Make the current task sleep until the given expiry time has
  1563. * elapsed. The routine will return immediately unless
  1564. * the current task state has been set (see set_current_state()).
  1565. *
  1566. * You can set the task state as follows -
  1567. *
  1568. * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
  1569. * pass before the routine returns.
  1570. *
  1571. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  1572. * delivered to the current task.
  1573. *
  1574. * The current task state is guaranteed to be TASK_RUNNING when this
  1575. * routine returns.
  1576. *
  1577. * Returns 0 when the timer has expired otherwise -EINTR
  1578. */
  1579. int __sched schedule_hrtimeout(ktime_t *expires,
  1580. const enum hrtimer_mode mode)
  1581. {
  1582. return schedule_hrtimeout_range(expires, 0, mode);
  1583. }
  1584. EXPORT_SYMBOL_GPL(schedule_hrtimeout);