hrtimer.c 48 KB

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