hrtimer.c 47 KB

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