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. int wakeup)
  552. {
  553. if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
  554. if (wakeup) {
  555. raw_spin_unlock(&base->cpu_base->lock);
  556. raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  557. raw_spin_lock(&base->cpu_base->lock);
  558. } else
  559. __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  560. return 1;
  561. }
  562. return 0;
  563. }
  564. static inline ktime_t hrtimer_update_base(struct hrtimer_cpu_base *base)
  565. {
  566. ktime_t *offs_real = &base->clock_base[HRTIMER_BASE_REALTIME].offset;
  567. ktime_t *offs_boot = &base->clock_base[HRTIMER_BASE_BOOTTIME].offset;
  568. return ktime_get_update_offsets(offs_real, offs_boot);
  569. }
  570. /*
  571. * Retrigger next event is called after clock was set
  572. *
  573. * Called with interrupts disabled via on_each_cpu()
  574. */
  575. static void retrigger_next_event(void *arg)
  576. {
  577. struct hrtimer_cpu_base *base = &__get_cpu_var(hrtimer_bases);
  578. if (!hrtimer_hres_active())
  579. return;
  580. raw_spin_lock(&base->lock);
  581. hrtimer_update_base(base);
  582. hrtimer_force_reprogram(base, 0);
  583. raw_spin_unlock(&base->lock);
  584. }
  585. /*
  586. * Switch to high resolution mode
  587. */
  588. static int hrtimer_switch_to_hres(void)
  589. {
  590. int i, cpu = smp_processor_id();
  591. struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
  592. unsigned long flags;
  593. if (base->hres_active)
  594. return 1;
  595. local_irq_save(flags);
  596. if (tick_init_highres()) {
  597. local_irq_restore(flags);
  598. printk(KERN_WARNING "Could not switch to high resolution "
  599. "mode on CPU %d\n", cpu);
  600. return 0;
  601. }
  602. base->hres_active = 1;
  603. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
  604. base->clock_base[i].resolution = KTIME_HIGH_RES;
  605. tick_setup_sched_timer();
  606. /* "Retrigger" the interrupt to get things going */
  607. retrigger_next_event(NULL);
  608. local_irq_restore(flags);
  609. return 1;
  610. }
  611. /*
  612. * Called from timekeeping code to reprogramm the hrtimer interrupt
  613. * device. If called from the timer interrupt context we defer it to
  614. * softirq context.
  615. */
  616. void clock_was_set_delayed(void)
  617. {
  618. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  619. cpu_base->clock_was_set = 1;
  620. __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  621. }
  622. #else
  623. static inline int hrtimer_hres_active(void) { return 0; }
  624. static inline int hrtimer_is_hres_enabled(void) { return 0; }
  625. static inline int hrtimer_switch_to_hres(void) { return 0; }
  626. static inline void
  627. hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
  628. static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
  629. struct hrtimer_clock_base *base,
  630. int wakeup)
  631. {
  632. return 0;
  633. }
  634. static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
  635. static inline void retrigger_next_event(void *arg) { }
  636. #endif /* CONFIG_HIGH_RES_TIMERS */
  637. /*
  638. * Clock realtime was set
  639. *
  640. * Change the offset of the realtime clock vs. the monotonic
  641. * clock.
  642. *
  643. * We might have to reprogram the high resolution timer interrupt. On
  644. * SMP we call the architecture specific code to retrigger _all_ high
  645. * resolution timer interrupts. On UP we just disable interrupts and
  646. * call the high resolution interrupt code.
  647. */
  648. void clock_was_set(void)
  649. {
  650. #ifdef CONFIG_HIGH_RES_TIMERS
  651. /* Retrigger the CPU local events everywhere */
  652. on_each_cpu(retrigger_next_event, NULL, 1);
  653. #endif
  654. timerfd_clock_was_set();
  655. }
  656. /*
  657. * During resume we might have to reprogram the high resolution timer
  658. * interrupt (on the local CPU):
  659. */
  660. void hrtimers_resume(void)
  661. {
  662. WARN_ONCE(!irqs_disabled(),
  663. KERN_INFO "hrtimers_resume() called with IRQs enabled!");
  664. retrigger_next_event(NULL);
  665. timerfd_clock_was_set();
  666. }
  667. static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
  668. {
  669. #ifdef CONFIG_TIMER_STATS
  670. if (timer->start_site)
  671. return;
  672. timer->start_site = __builtin_return_address(0);
  673. memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
  674. timer->start_pid = current->pid;
  675. #endif
  676. }
  677. static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
  678. {
  679. #ifdef CONFIG_TIMER_STATS
  680. timer->start_site = NULL;
  681. #endif
  682. }
  683. static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
  684. {
  685. #ifdef CONFIG_TIMER_STATS
  686. if (likely(!timer_stats_active))
  687. return;
  688. timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
  689. timer->function, timer->start_comm, 0);
  690. #endif
  691. }
  692. /*
  693. * Counterpart to lock_hrtimer_base above:
  694. */
  695. static inline
  696. void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
  697. {
  698. raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
  699. }
  700. /**
  701. * hrtimer_forward - forward the timer expiry
  702. * @timer: hrtimer to forward
  703. * @now: forward past this time
  704. * @interval: the interval to forward
  705. *
  706. * Forward the timer expiry so it will expire in the future.
  707. * Returns the number of overruns.
  708. */
  709. u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
  710. {
  711. u64 orun = 1;
  712. ktime_t delta;
  713. delta = ktime_sub(now, hrtimer_get_expires(timer));
  714. if (delta.tv64 < 0)
  715. return 0;
  716. if (interval.tv64 < timer->base->resolution.tv64)
  717. interval.tv64 = timer->base->resolution.tv64;
  718. if (unlikely(delta.tv64 >= interval.tv64)) {
  719. s64 incr = ktime_to_ns(interval);
  720. orun = ktime_divns(delta, incr);
  721. hrtimer_add_expires_ns(timer, incr * orun);
  722. if (hrtimer_get_expires_tv64(timer) > now.tv64)
  723. return orun;
  724. /*
  725. * This (and the ktime_add() below) is the
  726. * correction for exact:
  727. */
  728. orun++;
  729. }
  730. hrtimer_add_expires(timer, interval);
  731. return orun;
  732. }
  733. EXPORT_SYMBOL_GPL(hrtimer_forward);
  734. /*
  735. * enqueue_hrtimer - internal function to (re)start a timer
  736. *
  737. * The timer is inserted in expiry order. Insertion into the
  738. * red black tree is O(log(n)). Must hold the base lock.
  739. *
  740. * Returns 1 when the new timer is the leftmost timer in the tree.
  741. */
  742. static int enqueue_hrtimer(struct hrtimer *timer,
  743. struct hrtimer_clock_base *base)
  744. {
  745. debug_activate(timer);
  746. timerqueue_add(&base->active, &timer->node);
  747. base->cpu_base->active_bases |= 1 << base->index;
  748. /*
  749. * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
  750. * state of a possibly running callback.
  751. */
  752. timer->state |= HRTIMER_STATE_ENQUEUED;
  753. return (&timer->node == base->active.next);
  754. }
  755. /*
  756. * __remove_hrtimer - internal function to remove a timer
  757. *
  758. * Caller must hold the base lock.
  759. *
  760. * High resolution timer mode reprograms the clock event device when the
  761. * timer is the one which expires next. The caller can disable this by setting
  762. * reprogram to zero. This is useful, when the context does a reprogramming
  763. * anyway (e.g. timer interrupt)
  764. */
  765. static void __remove_hrtimer(struct hrtimer *timer,
  766. struct hrtimer_clock_base *base,
  767. unsigned long newstate, int reprogram)
  768. {
  769. struct timerqueue_node *next_timer;
  770. if (!(timer->state & HRTIMER_STATE_ENQUEUED))
  771. goto out;
  772. next_timer = timerqueue_getnext(&base->active);
  773. timerqueue_del(&base->active, &timer->node);
  774. if (&timer->node == next_timer) {
  775. #ifdef CONFIG_HIGH_RES_TIMERS
  776. /* Reprogram the clock event device. if enabled */
  777. if (reprogram && hrtimer_hres_active()) {
  778. ktime_t expires;
  779. expires = ktime_sub(hrtimer_get_expires(timer),
  780. base->offset);
  781. if (base->cpu_base->expires_next.tv64 == expires.tv64)
  782. hrtimer_force_reprogram(base->cpu_base, 1);
  783. }
  784. #endif
  785. }
  786. if (!timerqueue_getnext(&base->active))
  787. base->cpu_base->active_bases &= ~(1 << base->index);
  788. out:
  789. timer->state = newstate;
  790. }
  791. /*
  792. * remove hrtimer, called with base lock held
  793. */
  794. static inline int
  795. remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
  796. {
  797. if (hrtimer_is_queued(timer)) {
  798. unsigned long state;
  799. int reprogram;
  800. /*
  801. * Remove the timer and force reprogramming when high
  802. * resolution mode is active and the timer is on the current
  803. * CPU. If we remove a timer on another CPU, reprogramming is
  804. * skipped. The interrupt event on this CPU is fired and
  805. * reprogramming happens in the interrupt handler. This is a
  806. * rare case and less expensive than a smp call.
  807. */
  808. debug_deactivate(timer);
  809. timer_stats_hrtimer_clear_start_info(timer);
  810. reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
  811. /*
  812. * We must preserve the CALLBACK state flag here,
  813. * otherwise we could move the timer base in
  814. * switch_hrtimer_base.
  815. */
  816. state = timer->state & HRTIMER_STATE_CALLBACK;
  817. __remove_hrtimer(timer, base, state, reprogram);
  818. return 1;
  819. }
  820. return 0;
  821. }
  822. int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
  823. unsigned long delta_ns, const enum hrtimer_mode mode,
  824. int wakeup)
  825. {
  826. struct hrtimer_clock_base *base, *new_base;
  827. unsigned long flags;
  828. int ret, leftmost;
  829. base = lock_hrtimer_base(timer, &flags);
  830. /* Remove an active timer from the queue: */
  831. ret = remove_hrtimer(timer, base);
  832. /* Switch the timer base, if necessary: */
  833. new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
  834. if (mode & HRTIMER_MODE_REL) {
  835. tim = ktime_add_safe(tim, new_base->get_time());
  836. /*
  837. * CONFIG_TIME_LOW_RES is a temporary way for architectures
  838. * to signal that they simply return xtime in
  839. * do_gettimeoffset(). In this case we want to round up by
  840. * resolution when starting a relative timer, to avoid short
  841. * timeouts. This will go away with the GTOD framework.
  842. */
  843. #ifdef CONFIG_TIME_LOW_RES
  844. tim = ktime_add_safe(tim, base->resolution);
  845. #endif
  846. }
  847. hrtimer_set_expires_range_ns(timer, tim, delta_ns);
  848. timer_stats_hrtimer_set_start_info(timer);
  849. leftmost = enqueue_hrtimer(timer, new_base);
  850. /*
  851. * Only allow reprogramming if the new base is on this CPU.
  852. * (it might still be on another CPU if the timer was pending)
  853. *
  854. * XXX send_remote_softirq() ?
  855. */
  856. if (leftmost && new_base->cpu_base == &__get_cpu_var(hrtimer_bases))
  857. hrtimer_enqueue_reprogram(timer, new_base, wakeup);
  858. unlock_hrtimer_base(timer, &flags);
  859. return ret;
  860. }
  861. /**
  862. * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
  863. * @timer: the timer to be added
  864. * @tim: expiry time
  865. * @delta_ns: "slack" range for the timer
  866. * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
  867. *
  868. * Returns:
  869. * 0 on success
  870. * 1 when the timer was active
  871. */
  872. int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
  873. unsigned long delta_ns, const enum hrtimer_mode mode)
  874. {
  875. return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1);
  876. }
  877. EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
  878. /**
  879. * hrtimer_start - (re)start an hrtimer on the current CPU
  880. * @timer: the timer to be added
  881. * @tim: expiry time
  882. * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
  883. *
  884. * Returns:
  885. * 0 on success
  886. * 1 when the timer was active
  887. */
  888. int
  889. hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
  890. {
  891. return __hrtimer_start_range_ns(timer, tim, 0, mode, 1);
  892. }
  893. EXPORT_SYMBOL_GPL(hrtimer_start);
  894. /**
  895. * hrtimer_try_to_cancel - try to deactivate a timer
  896. * @timer: hrtimer to stop
  897. *
  898. * Returns:
  899. * 0 when the timer was not active
  900. * 1 when the timer was active
  901. * -1 when the timer is currently excuting the callback function and
  902. * cannot be stopped
  903. */
  904. int hrtimer_try_to_cancel(struct hrtimer *timer)
  905. {
  906. struct hrtimer_clock_base *base;
  907. unsigned long flags;
  908. int ret = -1;
  909. base = lock_hrtimer_base(timer, &flags);
  910. if (!hrtimer_callback_running(timer))
  911. ret = remove_hrtimer(timer, base);
  912. unlock_hrtimer_base(timer, &flags);
  913. return ret;
  914. }
  915. EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
  916. /**
  917. * hrtimer_cancel - cancel a timer and wait for the handler to finish.
  918. * @timer: the timer to be cancelled
  919. *
  920. * Returns:
  921. * 0 when the timer was not active
  922. * 1 when the timer was active
  923. */
  924. int hrtimer_cancel(struct hrtimer *timer)
  925. {
  926. for (;;) {
  927. int ret = hrtimer_try_to_cancel(timer);
  928. if (ret >= 0)
  929. return ret;
  930. cpu_relax();
  931. }
  932. }
  933. EXPORT_SYMBOL_GPL(hrtimer_cancel);
  934. /**
  935. * hrtimer_get_remaining - get remaining time for the timer
  936. * @timer: the timer to read
  937. */
  938. ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
  939. {
  940. unsigned long flags;
  941. ktime_t rem;
  942. lock_hrtimer_base(timer, &flags);
  943. rem = hrtimer_expires_remaining(timer);
  944. unlock_hrtimer_base(timer, &flags);
  945. return rem;
  946. }
  947. EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
  948. #ifdef CONFIG_NO_HZ
  949. /**
  950. * hrtimer_get_next_event - get the time until next expiry event
  951. *
  952. * Returns the delta to the next expiry event or KTIME_MAX if no timer
  953. * is pending.
  954. */
  955. ktime_t hrtimer_get_next_event(void)
  956. {
  957. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  958. struct hrtimer_clock_base *base = cpu_base->clock_base;
  959. ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
  960. unsigned long flags;
  961. int i;
  962. raw_spin_lock_irqsave(&cpu_base->lock, flags);
  963. if (!hrtimer_hres_active()) {
  964. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
  965. struct hrtimer *timer;
  966. struct timerqueue_node *next;
  967. next = timerqueue_getnext(&base->active);
  968. if (!next)
  969. continue;
  970. timer = container_of(next, struct hrtimer, node);
  971. delta.tv64 = hrtimer_get_expires_tv64(timer);
  972. delta = ktime_sub(delta, base->get_time());
  973. if (delta.tv64 < mindelta.tv64)
  974. mindelta.tv64 = delta.tv64;
  975. }
  976. }
  977. raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
  978. if (mindelta.tv64 < 0)
  979. mindelta.tv64 = 0;
  980. return mindelta;
  981. }
  982. #endif
  983. static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  984. enum hrtimer_mode mode)
  985. {
  986. struct hrtimer_cpu_base *cpu_base;
  987. int base;
  988. memset(timer, 0, sizeof(struct hrtimer));
  989. cpu_base = &__raw_get_cpu_var(hrtimer_bases);
  990. if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
  991. clock_id = CLOCK_MONOTONIC;
  992. base = hrtimer_clockid_to_base(clock_id);
  993. timer->base = &cpu_base->clock_base[base];
  994. timerqueue_init(&timer->node);
  995. #ifdef CONFIG_TIMER_STATS
  996. timer->start_site = NULL;
  997. timer->start_pid = -1;
  998. memset(timer->start_comm, 0, TASK_COMM_LEN);
  999. #endif
  1000. }
  1001. /**
  1002. * hrtimer_init - initialize a timer to the given clock
  1003. * @timer: the timer to be initialized
  1004. * @clock_id: the clock to be used
  1005. * @mode: timer mode abs/rel
  1006. */
  1007. void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  1008. enum hrtimer_mode mode)
  1009. {
  1010. debug_init(timer, clock_id, mode);
  1011. __hrtimer_init(timer, clock_id, mode);
  1012. }
  1013. EXPORT_SYMBOL_GPL(hrtimer_init);
  1014. /**
  1015. * hrtimer_get_res - get the timer resolution for a clock
  1016. * @which_clock: which clock to query
  1017. * @tp: pointer to timespec variable to store the resolution
  1018. *
  1019. * Store the resolution of the clock selected by @which_clock in the
  1020. * variable pointed to by @tp.
  1021. */
  1022. int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
  1023. {
  1024. struct hrtimer_cpu_base *cpu_base;
  1025. int base = hrtimer_clockid_to_base(which_clock);
  1026. cpu_base = &__raw_get_cpu_var(hrtimer_bases);
  1027. *tp = ktime_to_timespec(cpu_base->clock_base[base].resolution);
  1028. return 0;
  1029. }
  1030. EXPORT_SYMBOL_GPL(hrtimer_get_res);
  1031. static void __run_hrtimer(struct hrtimer *timer, ktime_t *now)
  1032. {
  1033. struct hrtimer_clock_base *base = timer->base;
  1034. struct hrtimer_cpu_base *cpu_base = base->cpu_base;
  1035. enum hrtimer_restart (*fn)(struct hrtimer *);
  1036. int restart;
  1037. WARN_ON(!irqs_disabled());
  1038. debug_deactivate(timer);
  1039. __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
  1040. timer_stats_account_hrtimer(timer);
  1041. fn = timer->function;
  1042. /*
  1043. * Because we run timers from hardirq context, there is no chance
  1044. * they get migrated to another cpu, therefore its safe to unlock
  1045. * the timer base.
  1046. */
  1047. raw_spin_unlock(&cpu_base->lock);
  1048. trace_hrtimer_expire_entry(timer, now);
  1049. restart = fn(timer);
  1050. trace_hrtimer_expire_exit(timer);
  1051. raw_spin_lock(&cpu_base->lock);
  1052. /*
  1053. * Note: We clear the CALLBACK bit after enqueue_hrtimer and
  1054. * we do not reprogramm the event hardware. Happens either in
  1055. * hrtimer_start_range_ns() or in hrtimer_interrupt()
  1056. */
  1057. if (restart != HRTIMER_NORESTART) {
  1058. BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
  1059. enqueue_hrtimer(timer, base);
  1060. }
  1061. WARN_ON_ONCE(!(timer->state & HRTIMER_STATE_CALLBACK));
  1062. timer->state &= ~HRTIMER_STATE_CALLBACK;
  1063. }
  1064. #ifdef CONFIG_HIGH_RES_TIMERS
  1065. /*
  1066. * High resolution timer interrupt
  1067. * Called with interrupts disabled
  1068. */
  1069. void hrtimer_interrupt(struct clock_event_device *dev)
  1070. {
  1071. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  1072. ktime_t expires_next, now, entry_time, delta;
  1073. int i, retries = 0;
  1074. BUG_ON(!cpu_base->hres_active);
  1075. cpu_base->nr_events++;
  1076. dev->next_event.tv64 = KTIME_MAX;
  1077. raw_spin_lock(&cpu_base->lock);
  1078. entry_time = now = hrtimer_update_base(cpu_base);
  1079. retry:
  1080. expires_next.tv64 = KTIME_MAX;
  1081. /*
  1082. * We set expires_next to KTIME_MAX here with cpu_base->lock
  1083. * held to prevent that a timer is enqueued in our queue via
  1084. * the migration code. This does not affect enqueueing of
  1085. * timers which run their callback and need to be requeued on
  1086. * this CPU.
  1087. */
  1088. cpu_base->expires_next.tv64 = KTIME_MAX;
  1089. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  1090. struct hrtimer_clock_base *base;
  1091. struct timerqueue_node *node;
  1092. ktime_t basenow;
  1093. if (!(cpu_base->active_bases & (1 << i)))
  1094. continue;
  1095. base = cpu_base->clock_base + i;
  1096. basenow = ktime_add(now, base->offset);
  1097. while ((node = timerqueue_getnext(&base->active))) {
  1098. struct hrtimer *timer;
  1099. timer = container_of(node, struct hrtimer, node);
  1100. /*
  1101. * The immediate goal for using the softexpires is
  1102. * minimizing wakeups, not running timers at the
  1103. * earliest interrupt after their soft expiration.
  1104. * This allows us to avoid using a Priority Search
  1105. * Tree, which can answer a stabbing querry for
  1106. * overlapping intervals and instead use the simple
  1107. * BST we already have.
  1108. * We don't add extra wakeups by delaying timers that
  1109. * are right-of a not yet expired timer, because that
  1110. * timer will have to trigger a wakeup anyway.
  1111. */
  1112. if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
  1113. ktime_t expires;
  1114. expires = ktime_sub(hrtimer_get_expires(timer),
  1115. base->offset);
  1116. if (expires.tv64 < expires_next.tv64)
  1117. expires_next = expires;
  1118. break;
  1119. }
  1120. __run_hrtimer(timer, &basenow);
  1121. }
  1122. }
  1123. /*
  1124. * Store the new expiry value so the migration code can verify
  1125. * against it.
  1126. */
  1127. cpu_base->expires_next = expires_next;
  1128. raw_spin_unlock(&cpu_base->lock);
  1129. /* Reprogramming necessary ? */
  1130. if (expires_next.tv64 == KTIME_MAX ||
  1131. !tick_program_event(expires_next, 0)) {
  1132. cpu_base->hang_detected = 0;
  1133. return;
  1134. }
  1135. /*
  1136. * The next timer was already expired due to:
  1137. * - tracing
  1138. * - long lasting callbacks
  1139. * - being scheduled away when running in a VM
  1140. *
  1141. * We need to prevent that we loop forever in the hrtimer
  1142. * interrupt routine. We give it 3 attempts to avoid
  1143. * overreacting on some spurious event.
  1144. *
  1145. * Acquire base lock for updating the offsets and retrieving
  1146. * the current time.
  1147. */
  1148. raw_spin_lock(&cpu_base->lock);
  1149. now = hrtimer_update_base(cpu_base);
  1150. cpu_base->nr_retries++;
  1151. if (++retries < 3)
  1152. goto retry;
  1153. /*
  1154. * Give the system a chance to do something else than looping
  1155. * here. We stored the entry time, so we know exactly how long
  1156. * we spent here. We schedule the next event this amount of
  1157. * time away.
  1158. */
  1159. cpu_base->nr_hangs++;
  1160. cpu_base->hang_detected = 1;
  1161. raw_spin_unlock(&cpu_base->lock);
  1162. delta = ktime_sub(now, entry_time);
  1163. if (delta.tv64 > cpu_base->max_hang_time.tv64)
  1164. cpu_base->max_hang_time = delta;
  1165. /*
  1166. * Limit it to a sensible value as we enforce a longer
  1167. * delay. Give the CPU at least 100ms to catch up.
  1168. */
  1169. if (delta.tv64 > 100 * NSEC_PER_MSEC)
  1170. expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC);
  1171. else
  1172. expires_next = ktime_add(now, delta);
  1173. tick_program_event(expires_next, 1);
  1174. printk_once(KERN_WARNING "hrtimer: interrupt took %llu ns\n",
  1175. ktime_to_ns(delta));
  1176. }
  1177. /*
  1178. * local version of hrtimer_peek_ahead_timers() called with interrupts
  1179. * disabled.
  1180. */
  1181. static void __hrtimer_peek_ahead_timers(void)
  1182. {
  1183. struct tick_device *td;
  1184. if (!hrtimer_hres_active())
  1185. return;
  1186. td = &__get_cpu_var(tick_cpu_device);
  1187. if (td && td->evtdev)
  1188. hrtimer_interrupt(td->evtdev);
  1189. }
  1190. /**
  1191. * hrtimer_peek_ahead_timers -- run soft-expired timers now
  1192. *
  1193. * hrtimer_peek_ahead_timers will peek at the timer queue of
  1194. * the current cpu and check if there are any timers for which
  1195. * the soft expires time has passed. If any such timers exist,
  1196. * they are run immediately and then removed from the timer queue.
  1197. *
  1198. */
  1199. void hrtimer_peek_ahead_timers(void)
  1200. {
  1201. unsigned long flags;
  1202. local_irq_save(flags);
  1203. __hrtimer_peek_ahead_timers();
  1204. local_irq_restore(flags);
  1205. }
  1206. static void run_hrtimer_softirq(struct softirq_action *h)
  1207. {
  1208. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  1209. if (cpu_base->clock_was_set) {
  1210. cpu_base->clock_was_set = 0;
  1211. clock_was_set();
  1212. }
  1213. hrtimer_peek_ahead_timers();
  1214. }
  1215. #else /* CONFIG_HIGH_RES_TIMERS */
  1216. static inline void __hrtimer_peek_ahead_timers(void) { }
  1217. #endif /* !CONFIG_HIGH_RES_TIMERS */
  1218. /*
  1219. * Called from timer softirq every jiffy, expire hrtimers:
  1220. *
  1221. * For HRT its the fall back code to run the softirq in the timer
  1222. * softirq context in case the hrtimer initialization failed or has
  1223. * not been done yet.
  1224. */
  1225. void hrtimer_run_pending(void)
  1226. {
  1227. if (hrtimer_hres_active())
  1228. return;
  1229. /*
  1230. * This _is_ ugly: We have to check in the softirq context,
  1231. * whether we can switch to highres and / or nohz mode. The
  1232. * clocksource switch happens in the timer interrupt with
  1233. * xtime_lock held. Notification from there only sets the
  1234. * check bit in the tick_oneshot code, otherwise we might
  1235. * deadlock vs. xtime_lock.
  1236. */
  1237. if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
  1238. hrtimer_switch_to_hres();
  1239. }
  1240. /*
  1241. * Called from hardirq context every jiffy
  1242. */
  1243. void hrtimer_run_queues(void)
  1244. {
  1245. struct timerqueue_node *node;
  1246. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  1247. struct hrtimer_clock_base *base;
  1248. int index, gettime = 1;
  1249. if (hrtimer_hres_active())
  1250. return;
  1251. for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
  1252. base = &cpu_base->clock_base[index];
  1253. if (!timerqueue_getnext(&base->active))
  1254. continue;
  1255. if (gettime) {
  1256. hrtimer_get_softirq_time(cpu_base);
  1257. gettime = 0;
  1258. }
  1259. raw_spin_lock(&cpu_base->lock);
  1260. while ((node = timerqueue_getnext(&base->active))) {
  1261. struct hrtimer *timer;
  1262. timer = container_of(node, struct hrtimer, node);
  1263. if (base->softirq_time.tv64 <=
  1264. hrtimer_get_expires_tv64(timer))
  1265. break;
  1266. __run_hrtimer(timer, &base->softirq_time);
  1267. }
  1268. raw_spin_unlock(&cpu_base->lock);
  1269. }
  1270. }
  1271. /*
  1272. * Sleep related functions:
  1273. */
  1274. static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
  1275. {
  1276. struct hrtimer_sleeper *t =
  1277. container_of(timer, struct hrtimer_sleeper, timer);
  1278. struct task_struct *task = t->task;
  1279. t->task = NULL;
  1280. if (task)
  1281. wake_up_process(task);
  1282. return HRTIMER_NORESTART;
  1283. }
  1284. void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
  1285. {
  1286. sl->timer.function = hrtimer_wakeup;
  1287. sl->task = task;
  1288. }
  1289. EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
  1290. static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
  1291. {
  1292. hrtimer_init_sleeper(t, current);
  1293. do {
  1294. set_current_state(TASK_INTERRUPTIBLE);
  1295. hrtimer_start_expires(&t->timer, mode);
  1296. if (!hrtimer_active(&t->timer))
  1297. t->task = NULL;
  1298. if (likely(t->task))
  1299. schedule();
  1300. hrtimer_cancel(&t->timer);
  1301. mode = HRTIMER_MODE_ABS;
  1302. } while (t->task && !signal_pending(current));
  1303. __set_current_state(TASK_RUNNING);
  1304. return t->task == NULL;
  1305. }
  1306. static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
  1307. {
  1308. struct timespec rmt;
  1309. ktime_t rem;
  1310. rem = hrtimer_expires_remaining(timer);
  1311. if (rem.tv64 <= 0)
  1312. return 0;
  1313. rmt = ktime_to_timespec(rem);
  1314. if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
  1315. return -EFAULT;
  1316. return 1;
  1317. }
  1318. long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
  1319. {
  1320. struct hrtimer_sleeper t;
  1321. struct timespec __user *rmtp;
  1322. int ret = 0;
  1323. hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid,
  1324. HRTIMER_MODE_ABS);
  1325. hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
  1326. if (do_nanosleep(&t, HRTIMER_MODE_ABS))
  1327. goto out;
  1328. rmtp = restart->nanosleep.rmtp;
  1329. if (rmtp) {
  1330. ret = update_rmtp(&t.timer, rmtp);
  1331. if (ret <= 0)
  1332. goto out;
  1333. }
  1334. /* The other values in restart are already filled in */
  1335. ret = -ERESTART_RESTARTBLOCK;
  1336. out:
  1337. destroy_hrtimer_on_stack(&t.timer);
  1338. return ret;
  1339. }
  1340. long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  1341. const enum hrtimer_mode mode, const clockid_t clockid)
  1342. {
  1343. struct restart_block *restart;
  1344. struct hrtimer_sleeper t;
  1345. int ret = 0;
  1346. unsigned long slack;
  1347. slack = current->timer_slack_ns;
  1348. if (rt_task(current))
  1349. slack = 0;
  1350. hrtimer_init_on_stack(&t.timer, clockid, mode);
  1351. hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
  1352. if (do_nanosleep(&t, mode))
  1353. goto out;
  1354. /* Absolute timers do not update the rmtp value and restart: */
  1355. if (mode == HRTIMER_MODE_ABS) {
  1356. ret = -ERESTARTNOHAND;
  1357. goto out;
  1358. }
  1359. if (rmtp) {
  1360. ret = update_rmtp(&t.timer, rmtp);
  1361. if (ret <= 0)
  1362. goto out;
  1363. }
  1364. restart = &current_thread_info()->restart_block;
  1365. restart->fn = hrtimer_nanosleep_restart;
  1366. restart->nanosleep.clockid = t.timer.base->clockid;
  1367. restart->nanosleep.rmtp = rmtp;
  1368. restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
  1369. ret = -ERESTART_RESTARTBLOCK;
  1370. out:
  1371. destroy_hrtimer_on_stack(&t.timer);
  1372. return ret;
  1373. }
  1374. SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
  1375. struct timespec __user *, rmtp)
  1376. {
  1377. struct timespec tu;
  1378. if (copy_from_user(&tu, rqtp, sizeof(tu)))
  1379. return -EFAULT;
  1380. if (!timespec_valid(&tu))
  1381. return -EINVAL;
  1382. return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
  1383. }
  1384. /*
  1385. * Functions related to boot-time initialization:
  1386. */
  1387. static void __cpuinit init_hrtimers_cpu(int cpu)
  1388. {
  1389. struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
  1390. int i;
  1391. raw_spin_lock_init(&cpu_base->lock);
  1392. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  1393. cpu_base->clock_base[i].cpu_base = cpu_base;
  1394. timerqueue_init_head(&cpu_base->clock_base[i].active);
  1395. }
  1396. hrtimer_init_hres(cpu_base);
  1397. }
  1398. #ifdef CONFIG_HOTPLUG_CPU
  1399. static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
  1400. struct hrtimer_clock_base *new_base)
  1401. {
  1402. struct hrtimer *timer;
  1403. struct timerqueue_node *node;
  1404. while ((node = timerqueue_getnext(&old_base->active))) {
  1405. timer = container_of(node, struct hrtimer, node);
  1406. BUG_ON(hrtimer_callback_running(timer));
  1407. debug_deactivate(timer);
  1408. /*
  1409. * Mark it as STATE_MIGRATE not INACTIVE otherwise the
  1410. * timer could be seen as !active and just vanish away
  1411. * under us on another CPU
  1412. */
  1413. __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
  1414. timer->base = new_base;
  1415. /*
  1416. * Enqueue the timers on the new cpu. This does not
  1417. * reprogram the event device in case the timer
  1418. * expires before the earliest on this CPU, but we run
  1419. * hrtimer_interrupt after we migrated everything to
  1420. * sort out already expired timers and reprogram the
  1421. * event device.
  1422. */
  1423. enqueue_hrtimer(timer, new_base);
  1424. /* Clear the migration state bit */
  1425. timer->state &= ~HRTIMER_STATE_MIGRATE;
  1426. }
  1427. }
  1428. static void migrate_hrtimers(int scpu)
  1429. {
  1430. struct hrtimer_cpu_base *old_base, *new_base;
  1431. int i;
  1432. BUG_ON(cpu_online(scpu));
  1433. tick_cancel_sched_timer(scpu);
  1434. local_irq_disable();
  1435. old_base = &per_cpu(hrtimer_bases, scpu);
  1436. new_base = &__get_cpu_var(hrtimer_bases);
  1437. /*
  1438. * The caller is globally serialized and nobody else
  1439. * takes two locks at once, deadlock is not possible.
  1440. */
  1441. raw_spin_lock(&new_base->lock);
  1442. raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
  1443. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  1444. migrate_hrtimer_list(&old_base->clock_base[i],
  1445. &new_base->clock_base[i]);
  1446. }
  1447. raw_spin_unlock(&old_base->lock);
  1448. raw_spin_unlock(&new_base->lock);
  1449. /* Check, if we got expired work to do */
  1450. __hrtimer_peek_ahead_timers();
  1451. local_irq_enable();
  1452. }
  1453. #endif /* CONFIG_HOTPLUG_CPU */
  1454. static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
  1455. unsigned long action, void *hcpu)
  1456. {
  1457. int scpu = (long)hcpu;
  1458. switch (action) {
  1459. case CPU_UP_PREPARE:
  1460. case CPU_UP_PREPARE_FROZEN:
  1461. init_hrtimers_cpu(scpu);
  1462. break;
  1463. #ifdef CONFIG_HOTPLUG_CPU
  1464. case CPU_DYING:
  1465. case CPU_DYING_FROZEN:
  1466. clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu);
  1467. break;
  1468. case CPU_DEAD:
  1469. case CPU_DEAD_FROZEN:
  1470. {
  1471. clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
  1472. migrate_hrtimers(scpu);
  1473. break;
  1474. }
  1475. #endif
  1476. default:
  1477. break;
  1478. }
  1479. return NOTIFY_OK;
  1480. }
  1481. static struct notifier_block __cpuinitdata hrtimers_nb = {
  1482. .notifier_call = hrtimer_cpu_notify,
  1483. };
  1484. void __init hrtimers_init(void)
  1485. {
  1486. hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
  1487. (void *)(long)smp_processor_id());
  1488. register_cpu_notifier(&hrtimers_nb);
  1489. #ifdef CONFIG_HIGH_RES_TIMERS
  1490. open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
  1491. #endif
  1492. }
  1493. /**
  1494. * schedule_hrtimeout_range_clock - sleep until timeout
  1495. * @expires: timeout value (ktime_t)
  1496. * @delta: slack in expires timeout (ktime_t)
  1497. * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
  1498. * @clock: timer clock, CLOCK_MONOTONIC or CLOCK_REALTIME
  1499. */
  1500. int __sched
  1501. schedule_hrtimeout_range_clock(ktime_t *expires, unsigned long delta,
  1502. const enum hrtimer_mode mode, int clock)
  1503. {
  1504. struct hrtimer_sleeper t;
  1505. /*
  1506. * Optimize when a zero timeout value is given. It does not
  1507. * matter whether this is an absolute or a relative time.
  1508. */
  1509. if (expires && !expires->tv64) {
  1510. __set_current_state(TASK_RUNNING);
  1511. return 0;
  1512. }
  1513. /*
  1514. * A NULL parameter means "infinite"
  1515. */
  1516. if (!expires) {
  1517. schedule();
  1518. __set_current_state(TASK_RUNNING);
  1519. return -EINTR;
  1520. }
  1521. hrtimer_init_on_stack(&t.timer, clock, mode);
  1522. hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
  1523. hrtimer_init_sleeper(&t, current);
  1524. hrtimer_start_expires(&t.timer, mode);
  1525. if (!hrtimer_active(&t.timer))
  1526. t.task = NULL;
  1527. if (likely(t.task))
  1528. schedule();
  1529. hrtimer_cancel(&t.timer);
  1530. destroy_hrtimer_on_stack(&t.timer);
  1531. __set_current_state(TASK_RUNNING);
  1532. return !t.task ? 0 : -EINTR;
  1533. }
  1534. /**
  1535. * schedule_hrtimeout_range - sleep until timeout
  1536. * @expires: timeout value (ktime_t)
  1537. * @delta: slack in expires timeout (ktime_t)
  1538. * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
  1539. *
  1540. * Make the current task sleep until the given expiry time has
  1541. * elapsed. The routine will return immediately unless
  1542. * the current task state has been set (see set_current_state()).
  1543. *
  1544. * The @delta argument gives the kernel the freedom to schedule the
  1545. * actual wakeup to a time that is both power and performance friendly.
  1546. * The kernel give the normal best effort behavior for "@expires+@delta",
  1547. * but may decide to fire the timer earlier, but no earlier than @expires.
  1548. *
  1549. * You can set the task state as follows -
  1550. *
  1551. * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
  1552. * pass before the routine returns.
  1553. *
  1554. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  1555. * delivered to the current task.
  1556. *
  1557. * The current task state is guaranteed to be TASK_RUNNING when this
  1558. * routine returns.
  1559. *
  1560. * Returns 0 when the timer has expired otherwise -EINTR
  1561. */
  1562. int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
  1563. const enum hrtimer_mode mode)
  1564. {
  1565. return schedule_hrtimeout_range_clock(expires, delta, mode,
  1566. CLOCK_MONOTONIC);
  1567. }
  1568. EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
  1569. /**
  1570. * schedule_hrtimeout - sleep until timeout
  1571. * @expires: timeout value (ktime_t)
  1572. * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
  1573. *
  1574. * Make the current task sleep until the given expiry time has
  1575. * elapsed. The routine will return immediately unless
  1576. * the current task state has been set (see set_current_state()).
  1577. *
  1578. * You can set the task state as follows -
  1579. *
  1580. * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
  1581. * pass before the routine returns.
  1582. *
  1583. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  1584. * delivered to the current task.
  1585. *
  1586. * The current task state is guaranteed to be TASK_RUNNING when this
  1587. * routine returns.
  1588. *
  1589. * Returns 0 when the timer has expired otherwise -EINTR
  1590. */
  1591. int __sched schedule_hrtimeout(ktime_t *expires,
  1592. const enum hrtimer_mode mode)
  1593. {
  1594. return schedule_hrtimeout_range(expires, 0, mode);
  1595. }
  1596. EXPORT_SYMBOL_GPL(schedule_hrtimeout);