hrtimer.c 46 KB

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