hrtimer.c 46 KB

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