hrtimer.c 43 KB

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