hrtimer.c 43 KB

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