hrtimer.c 46 KB

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