hrtimer.c 46 KB

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