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