hrtimer.c 40 KB

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