hrtimer.c 45 KB

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