hrtimer.c 47 KB

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