hrtimer.c 46 KB

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