hrtimer.c 47 KB

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