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