hrtimer.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  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 <tglx@timesys.com>
  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/interrupt.h>
  40. #include <linux/tick.h>
  41. #include <asm/uaccess.h>
  42. /**
  43. * ktime_get - get the monotonic time in ktime_t format
  44. *
  45. * returns the time in ktime_t format
  46. */
  47. ktime_t ktime_get(void)
  48. {
  49. struct timespec now;
  50. ktime_get_ts(&now);
  51. return timespec_to_ktime(now);
  52. }
  53. /**
  54. * ktime_get_real - get the real (wall-) time in ktime_t format
  55. *
  56. * returns the time in ktime_t format
  57. */
  58. ktime_t ktime_get_real(void)
  59. {
  60. struct timespec now;
  61. getnstimeofday(&now);
  62. return timespec_to_ktime(now);
  63. }
  64. EXPORT_SYMBOL_GPL(ktime_get_real);
  65. /*
  66. * The timer bases:
  67. *
  68. * Note: If we want to add new timer bases, we have to skip the two
  69. * clock ids captured by the cpu-timers. We do this by holding empty
  70. * entries rather than doing math adjustment of the clock ids.
  71. * This ensures that we capture erroneous accesses to these clock ids
  72. * rather than moving them into the range of valid clock id's.
  73. */
  74. static DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
  75. {
  76. .clock_base =
  77. {
  78. {
  79. .index = CLOCK_REALTIME,
  80. .get_time = &ktime_get_real,
  81. .resolution = KTIME_REALTIME_RES,
  82. },
  83. {
  84. .index = CLOCK_MONOTONIC,
  85. .get_time = &ktime_get,
  86. .resolution = KTIME_MONOTONIC_RES,
  87. },
  88. }
  89. };
  90. /**
  91. * ktime_get_ts - get the monotonic clock in timespec format
  92. * @ts: pointer to timespec variable
  93. *
  94. * The function calculates the monotonic clock from the realtime
  95. * clock and the wall_to_monotonic offset and stores the result
  96. * in normalized timespec format in the variable pointed to by @ts.
  97. */
  98. void ktime_get_ts(struct timespec *ts)
  99. {
  100. struct timespec tomono;
  101. unsigned long seq;
  102. do {
  103. seq = read_seqbegin(&xtime_lock);
  104. getnstimeofday(ts);
  105. tomono = wall_to_monotonic;
  106. } while (read_seqretry(&xtime_lock, seq));
  107. set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
  108. ts->tv_nsec + tomono.tv_nsec);
  109. }
  110. EXPORT_SYMBOL_GPL(ktime_get_ts);
  111. /*
  112. * Get the coarse grained time at the softirq based on xtime and
  113. * wall_to_monotonic.
  114. */
  115. static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
  116. {
  117. ktime_t xtim, tomono;
  118. struct timespec xts;
  119. unsigned long seq;
  120. do {
  121. seq = read_seqbegin(&xtime_lock);
  122. #ifdef CONFIG_NO_HZ
  123. getnstimeofday(&xts);
  124. #else
  125. xts = xtime;
  126. #endif
  127. } while (read_seqretry(&xtime_lock, seq));
  128. xtim = timespec_to_ktime(xts);
  129. tomono = timespec_to_ktime(wall_to_monotonic);
  130. base->clock_base[CLOCK_REALTIME].softirq_time = xtim;
  131. base->clock_base[CLOCK_MONOTONIC].softirq_time =
  132. ktime_add(xtim, tomono);
  133. }
  134. /*
  135. * Helper function to check, whether the timer is on one of the queues
  136. */
  137. static inline int hrtimer_is_queued(struct hrtimer *timer)
  138. {
  139. return timer->state & HRTIMER_STATE_ENQUEUED;
  140. }
  141. /*
  142. * Helper function to check, whether the timer is running the callback
  143. * function
  144. */
  145. static inline int hrtimer_callback_running(struct hrtimer *timer)
  146. {
  147. return timer->state & HRTIMER_STATE_CALLBACK;
  148. }
  149. /*
  150. * Functions and macros which are different for UP/SMP systems are kept in a
  151. * single place
  152. */
  153. #ifdef CONFIG_SMP
  154. /*
  155. * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
  156. * means that all timers which are tied to this base via timer->base are
  157. * locked, and the base itself is locked too.
  158. *
  159. * So __run_timers/migrate_timers can safely modify all timers which could
  160. * be found on the lists/queues.
  161. *
  162. * When the timer's base is locked, and the timer removed from list, it is
  163. * possible to set timer->base = NULL and drop the lock: the timer remains
  164. * locked.
  165. */
  166. static
  167. struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
  168. unsigned long *flags)
  169. {
  170. struct hrtimer_clock_base *base;
  171. for (;;) {
  172. base = timer->base;
  173. if (likely(base != NULL)) {
  174. spin_lock_irqsave(&base->cpu_base->lock, *flags);
  175. if (likely(base == timer->base))
  176. return base;
  177. /* The timer has migrated to another CPU: */
  178. spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
  179. }
  180. cpu_relax();
  181. }
  182. }
  183. /*
  184. * Switch the timer base to the current CPU when possible.
  185. */
  186. static inline struct hrtimer_clock_base *
  187. switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base)
  188. {
  189. struct hrtimer_clock_base *new_base;
  190. struct hrtimer_cpu_base *new_cpu_base;
  191. new_cpu_base = &__get_cpu_var(hrtimer_bases);
  192. new_base = &new_cpu_base->clock_base[base->index];
  193. if (base != new_base) {
  194. /*
  195. * We are trying to schedule the timer on the local CPU.
  196. * However we can't change timer's base while it is running,
  197. * so we keep it on the same CPU. No hassle vs. reprogramming
  198. * the event source in the high resolution case. The softirq
  199. * code will take care of this when the timer function has
  200. * completed. There is no conflict as we hold the lock until
  201. * the timer is enqueued.
  202. */
  203. if (unlikely(timer->state & HRTIMER_STATE_CALLBACK))
  204. return base;
  205. /* See the comment in lock_timer_base() */
  206. timer->base = NULL;
  207. spin_unlock(&base->cpu_base->lock);
  208. spin_lock(&new_base->cpu_base->lock);
  209. timer->base = new_base;
  210. }
  211. return new_base;
  212. }
  213. #else /* CONFIG_SMP */
  214. static inline struct hrtimer_clock_base *
  215. lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
  216. {
  217. struct hrtimer_clock_base *base = timer->base;
  218. spin_lock_irqsave(&base->cpu_base->lock, *flags);
  219. return base;
  220. }
  221. #define switch_hrtimer_base(t, b) (b)
  222. #endif /* !CONFIG_SMP */
  223. /*
  224. * Functions for the union type storage format of ktime_t which are
  225. * too large for inlining:
  226. */
  227. #if BITS_PER_LONG < 64
  228. # ifndef CONFIG_KTIME_SCALAR
  229. /**
  230. * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
  231. * @kt: addend
  232. * @nsec: the scalar nsec value to add
  233. *
  234. * Returns the sum of kt and nsec in ktime_t format
  235. */
  236. ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
  237. {
  238. ktime_t tmp;
  239. if (likely(nsec < NSEC_PER_SEC)) {
  240. tmp.tv64 = nsec;
  241. } else {
  242. unsigned long rem = do_div(nsec, NSEC_PER_SEC);
  243. tmp = ktime_set((long)nsec, rem);
  244. }
  245. return ktime_add(kt, tmp);
  246. }
  247. #else /* CONFIG_KTIME_SCALAR */
  248. # endif /* !CONFIG_KTIME_SCALAR */
  249. /*
  250. * Divide a ktime value by a nanosecond value
  251. */
  252. unsigned long ktime_divns(const ktime_t kt, s64 div)
  253. {
  254. u64 dclc, inc, dns;
  255. int sft = 0;
  256. dclc = dns = ktime_to_ns(kt);
  257. inc = div;
  258. /* Make sure the divisor is less than 2^32: */
  259. while (div >> 32) {
  260. sft++;
  261. div >>= 1;
  262. }
  263. dclc >>= sft;
  264. do_div(dclc, (unsigned long) div);
  265. return (unsigned long) dclc;
  266. }
  267. #endif /* BITS_PER_LONG >= 64 */
  268. /*
  269. * Counterpart to lock_timer_base above:
  270. */
  271. static inline
  272. void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
  273. {
  274. spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
  275. }
  276. /**
  277. * hrtimer_forward - forward the timer expiry
  278. * @timer: hrtimer to forward
  279. * @now: forward past this time
  280. * @interval: the interval to forward
  281. *
  282. * Forward the timer expiry so it will expire in the future.
  283. * Returns the number of overruns.
  284. */
  285. unsigned long
  286. hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
  287. {
  288. unsigned long orun = 1;
  289. ktime_t delta;
  290. delta = ktime_sub(now, timer->expires);
  291. if (delta.tv64 < 0)
  292. return 0;
  293. if (interval.tv64 < timer->base->resolution.tv64)
  294. interval.tv64 = timer->base->resolution.tv64;
  295. if (unlikely(delta.tv64 >= interval.tv64)) {
  296. s64 incr = ktime_to_ns(interval);
  297. orun = ktime_divns(delta, incr);
  298. timer->expires = ktime_add_ns(timer->expires, incr * orun);
  299. if (timer->expires.tv64 > now.tv64)
  300. return orun;
  301. /*
  302. * This (and the ktime_add() below) is the
  303. * correction for exact:
  304. */
  305. orun++;
  306. }
  307. timer->expires = ktime_add(timer->expires, interval);
  308. return orun;
  309. }
  310. /*
  311. * enqueue_hrtimer - internal function to (re)start a timer
  312. *
  313. * The timer is inserted in expiry order. Insertion into the
  314. * red black tree is O(log(n)). Must hold the base lock.
  315. */
  316. static void enqueue_hrtimer(struct hrtimer *timer,
  317. struct hrtimer_clock_base *base)
  318. {
  319. struct rb_node **link = &base->active.rb_node;
  320. struct rb_node *parent = NULL;
  321. struct hrtimer *entry;
  322. /*
  323. * Find the right place in the rbtree:
  324. */
  325. while (*link) {
  326. parent = *link;
  327. entry = rb_entry(parent, struct hrtimer, node);
  328. /*
  329. * We dont care about collisions. Nodes with
  330. * the same expiry time stay together.
  331. */
  332. if (timer->expires.tv64 < entry->expires.tv64)
  333. link = &(*link)->rb_left;
  334. else
  335. link = &(*link)->rb_right;
  336. }
  337. /*
  338. * Insert the timer to the rbtree and check whether it
  339. * replaces the first pending timer
  340. */
  341. rb_link_node(&timer->node, parent, link);
  342. rb_insert_color(&timer->node, &base->active);
  343. /*
  344. * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
  345. * state of a possibly running callback.
  346. */
  347. timer->state |= HRTIMER_STATE_ENQUEUED;
  348. if (!base->first || timer->expires.tv64 <
  349. rb_entry(base->first, struct hrtimer, node)->expires.tv64)
  350. base->first = &timer->node;
  351. }
  352. /*
  353. * __remove_hrtimer - internal function to remove a timer
  354. *
  355. * Caller must hold the base lock.
  356. */
  357. static void __remove_hrtimer(struct hrtimer *timer,
  358. struct hrtimer_clock_base *base,
  359. unsigned long newstate)
  360. {
  361. /*
  362. * Remove the timer from the rbtree and replace the
  363. * first entry pointer if necessary.
  364. */
  365. if (base->first == &timer->node)
  366. base->first = rb_next(&timer->node);
  367. rb_erase(&timer->node, &base->active);
  368. timer->state = newstate;
  369. }
  370. /*
  371. * remove hrtimer, called with base lock held
  372. */
  373. static inline int
  374. remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
  375. {
  376. if (hrtimer_is_queued(timer)) {
  377. __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE);
  378. return 1;
  379. }
  380. return 0;
  381. }
  382. /**
  383. * hrtimer_start - (re)start an relative timer on the current CPU
  384. * @timer: the timer to be added
  385. * @tim: expiry time
  386. * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
  387. *
  388. * Returns:
  389. * 0 on success
  390. * 1 when the timer was active
  391. */
  392. int
  393. hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
  394. {
  395. struct hrtimer_clock_base *base, *new_base;
  396. unsigned long flags;
  397. int ret;
  398. base = lock_hrtimer_base(timer, &flags);
  399. /* Remove an active timer from the queue: */
  400. ret = remove_hrtimer(timer, base);
  401. /* Switch the timer base, if necessary: */
  402. new_base = switch_hrtimer_base(timer, base);
  403. if (mode == HRTIMER_MODE_REL) {
  404. tim = ktime_add(tim, new_base->get_time());
  405. /*
  406. * CONFIG_TIME_LOW_RES is a temporary way for architectures
  407. * to signal that they simply return xtime in
  408. * do_gettimeoffset(). In this case we want to round up by
  409. * resolution when starting a relative timer, to avoid short
  410. * timeouts. This will go away with the GTOD framework.
  411. */
  412. #ifdef CONFIG_TIME_LOW_RES
  413. tim = ktime_add(tim, base->resolution);
  414. #endif
  415. }
  416. timer->expires = tim;
  417. enqueue_hrtimer(timer, new_base);
  418. unlock_hrtimer_base(timer, &flags);
  419. return ret;
  420. }
  421. EXPORT_SYMBOL_GPL(hrtimer_start);
  422. /**
  423. * hrtimer_try_to_cancel - try to deactivate a timer
  424. * @timer: hrtimer to stop
  425. *
  426. * Returns:
  427. * 0 when the timer was not active
  428. * 1 when the timer was active
  429. * -1 when the timer is currently excuting the callback function and
  430. * cannot be stopped
  431. */
  432. int hrtimer_try_to_cancel(struct hrtimer *timer)
  433. {
  434. struct hrtimer_clock_base *base;
  435. unsigned long flags;
  436. int ret = -1;
  437. base = lock_hrtimer_base(timer, &flags);
  438. if (!hrtimer_callback_running(timer))
  439. ret = remove_hrtimer(timer, base);
  440. unlock_hrtimer_base(timer, &flags);
  441. return ret;
  442. }
  443. EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
  444. /**
  445. * hrtimer_cancel - cancel a timer and wait for the handler to finish.
  446. * @timer: the timer to be cancelled
  447. *
  448. * Returns:
  449. * 0 when the timer was not active
  450. * 1 when the timer was active
  451. */
  452. int hrtimer_cancel(struct hrtimer *timer)
  453. {
  454. for (;;) {
  455. int ret = hrtimer_try_to_cancel(timer);
  456. if (ret >= 0)
  457. return ret;
  458. cpu_relax();
  459. }
  460. }
  461. EXPORT_SYMBOL_GPL(hrtimer_cancel);
  462. /**
  463. * hrtimer_get_remaining - get remaining time for the timer
  464. * @timer: the timer to read
  465. */
  466. ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
  467. {
  468. struct hrtimer_clock_base *base;
  469. unsigned long flags;
  470. ktime_t rem;
  471. base = lock_hrtimer_base(timer, &flags);
  472. rem = ktime_sub(timer->expires, base->get_time());
  473. unlock_hrtimer_base(timer, &flags);
  474. return rem;
  475. }
  476. EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
  477. #if defined(CONFIG_NO_IDLE_HZ) || defined(CONFIG_NO_HZ)
  478. /**
  479. * hrtimer_get_next_event - get the time until next expiry event
  480. *
  481. * Returns the delta to the next expiry event or KTIME_MAX if no timer
  482. * is pending.
  483. */
  484. ktime_t hrtimer_get_next_event(void)
  485. {
  486. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  487. struct hrtimer_clock_base *base = cpu_base->clock_base;
  488. ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
  489. unsigned long flags;
  490. int i;
  491. spin_lock_irqsave(&cpu_base->lock, flags);
  492. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
  493. struct hrtimer *timer;
  494. if (!base->first)
  495. continue;
  496. timer = rb_entry(base->first, struct hrtimer, node);
  497. delta.tv64 = timer->expires.tv64;
  498. delta = ktime_sub(delta, base->get_time());
  499. if (delta.tv64 < mindelta.tv64)
  500. mindelta.tv64 = delta.tv64;
  501. }
  502. spin_unlock_irqrestore(&cpu_base->lock, flags);
  503. if (mindelta.tv64 < 0)
  504. mindelta.tv64 = 0;
  505. return mindelta;
  506. }
  507. #endif
  508. /**
  509. * hrtimer_init - initialize a timer to the given clock
  510. * @timer: the timer to be initialized
  511. * @clock_id: the clock to be used
  512. * @mode: timer mode abs/rel
  513. */
  514. void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  515. enum hrtimer_mode mode)
  516. {
  517. struct hrtimer_cpu_base *cpu_base;
  518. memset(timer, 0, sizeof(struct hrtimer));
  519. cpu_base = &__raw_get_cpu_var(hrtimer_bases);
  520. if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
  521. clock_id = CLOCK_MONOTONIC;
  522. timer->base = &cpu_base->clock_base[clock_id];
  523. }
  524. EXPORT_SYMBOL_GPL(hrtimer_init);
  525. /**
  526. * hrtimer_get_res - get the timer resolution for a clock
  527. * @which_clock: which clock to query
  528. * @tp: pointer to timespec variable to store the resolution
  529. *
  530. * Store the resolution of the clock selected by @which_clock in the
  531. * variable pointed to by @tp.
  532. */
  533. int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
  534. {
  535. struct hrtimer_cpu_base *cpu_base;
  536. cpu_base = &__raw_get_cpu_var(hrtimer_bases);
  537. *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
  538. return 0;
  539. }
  540. EXPORT_SYMBOL_GPL(hrtimer_get_res);
  541. /*
  542. * Expire the per base hrtimer-queue:
  543. */
  544. static inline void run_hrtimer_queue(struct hrtimer_cpu_base *cpu_base,
  545. int index)
  546. {
  547. struct rb_node *node;
  548. struct hrtimer_clock_base *base = &cpu_base->clock_base[index];
  549. if (!base->first)
  550. return;
  551. if (base->get_softirq_time)
  552. base->softirq_time = base->get_softirq_time();
  553. spin_lock_irq(&cpu_base->lock);
  554. while ((node = base->first)) {
  555. struct hrtimer *timer;
  556. enum hrtimer_restart (*fn)(struct hrtimer *);
  557. int restart;
  558. timer = rb_entry(node, struct hrtimer, node);
  559. if (base->softirq_time.tv64 <= timer->expires.tv64)
  560. break;
  561. fn = timer->function;
  562. __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK);
  563. spin_unlock_irq(&cpu_base->lock);
  564. restart = fn(timer);
  565. spin_lock_irq(&cpu_base->lock);
  566. timer->state &= ~HRTIMER_STATE_CALLBACK;
  567. if (restart != HRTIMER_NORESTART) {
  568. BUG_ON(hrtimer_active(timer));
  569. enqueue_hrtimer(timer, base);
  570. }
  571. }
  572. spin_unlock_irq(&cpu_base->lock);
  573. }
  574. /*
  575. * Called from timer softirq every jiffy, expire hrtimers:
  576. */
  577. void hrtimer_run_queues(void)
  578. {
  579. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  580. int i;
  581. /*
  582. * This _is_ ugly: We have to check in the softirq context,
  583. * whether we can switch to highres and / or nohz mode. The
  584. * clocksource switch happens in the timer interrupt with
  585. * xtime_lock held. Notification from there only sets the
  586. * check bit in the tick_oneshot code, otherwise we might
  587. * deadlock vs. xtime_lock.
  588. */
  589. tick_check_oneshot_change(1);
  590. hrtimer_get_softirq_time(cpu_base);
  591. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
  592. run_hrtimer_queue(cpu_base, i);
  593. }
  594. /*
  595. * Sleep related functions:
  596. */
  597. static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
  598. {
  599. struct hrtimer_sleeper *t =
  600. container_of(timer, struct hrtimer_sleeper, timer);
  601. struct task_struct *task = t->task;
  602. t->task = NULL;
  603. if (task)
  604. wake_up_process(task);
  605. return HRTIMER_NORESTART;
  606. }
  607. void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
  608. {
  609. sl->timer.function = hrtimer_wakeup;
  610. sl->task = task;
  611. }
  612. static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
  613. {
  614. hrtimer_init_sleeper(t, current);
  615. do {
  616. set_current_state(TASK_INTERRUPTIBLE);
  617. hrtimer_start(&t->timer, t->timer.expires, mode);
  618. schedule();
  619. hrtimer_cancel(&t->timer);
  620. mode = HRTIMER_MODE_ABS;
  621. } while (t->task && !signal_pending(current));
  622. return t->task == NULL;
  623. }
  624. long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
  625. {
  626. struct hrtimer_sleeper t;
  627. struct timespec __user *rmtp;
  628. struct timespec tu;
  629. ktime_t time;
  630. restart->fn = do_no_restart_syscall;
  631. hrtimer_init(&t.timer, restart->arg0, HRTIMER_MODE_ABS);
  632. t.timer.expires.tv64 = ((u64)restart->arg3 << 32) | (u64) restart->arg2;
  633. if (do_nanosleep(&t, HRTIMER_MODE_ABS))
  634. return 0;
  635. rmtp = (struct timespec __user *) restart->arg1;
  636. if (rmtp) {
  637. time = ktime_sub(t.timer.expires, t.timer.base->get_time());
  638. if (time.tv64 <= 0)
  639. return 0;
  640. tu = ktime_to_timespec(time);
  641. if (copy_to_user(rmtp, &tu, sizeof(tu)))
  642. return -EFAULT;
  643. }
  644. restart->fn = hrtimer_nanosleep_restart;
  645. /* The other values in restart are already filled in */
  646. return -ERESTART_RESTARTBLOCK;
  647. }
  648. long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  649. const enum hrtimer_mode mode, const clockid_t clockid)
  650. {
  651. struct restart_block *restart;
  652. struct hrtimer_sleeper t;
  653. struct timespec tu;
  654. ktime_t rem;
  655. hrtimer_init(&t.timer, clockid, mode);
  656. t.timer.expires = timespec_to_ktime(*rqtp);
  657. if (do_nanosleep(&t, mode))
  658. return 0;
  659. /* Absolute timers do not update the rmtp value and restart: */
  660. if (mode == HRTIMER_MODE_ABS)
  661. return -ERESTARTNOHAND;
  662. if (rmtp) {
  663. rem = ktime_sub(t.timer.expires, t.timer.base->get_time());
  664. if (rem.tv64 <= 0)
  665. return 0;
  666. tu = ktime_to_timespec(rem);
  667. if (copy_to_user(rmtp, &tu, sizeof(tu)))
  668. return -EFAULT;
  669. }
  670. restart = &current_thread_info()->restart_block;
  671. restart->fn = hrtimer_nanosleep_restart;
  672. restart->arg0 = (unsigned long) t.timer.base->index;
  673. restart->arg1 = (unsigned long) rmtp;
  674. restart->arg2 = t.timer.expires.tv64 & 0xFFFFFFFF;
  675. restart->arg3 = t.timer.expires.tv64 >> 32;
  676. return -ERESTART_RESTARTBLOCK;
  677. }
  678. asmlinkage long
  679. sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp)
  680. {
  681. struct timespec tu;
  682. if (copy_from_user(&tu, rqtp, sizeof(tu)))
  683. return -EFAULT;
  684. if (!timespec_valid(&tu))
  685. return -EINVAL;
  686. return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
  687. }
  688. /*
  689. * Functions related to boot-time initialization:
  690. */
  691. static void __devinit init_hrtimers_cpu(int cpu)
  692. {
  693. struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
  694. int i;
  695. spin_lock_init(&cpu_base->lock);
  696. lockdep_set_class(&cpu_base->lock, &cpu_base->lock_key);
  697. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
  698. cpu_base->clock_base[i].cpu_base = cpu_base;
  699. }
  700. #ifdef CONFIG_HOTPLUG_CPU
  701. static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
  702. struct hrtimer_clock_base *new_base)
  703. {
  704. struct hrtimer *timer;
  705. struct rb_node *node;
  706. while ((node = rb_first(&old_base->active))) {
  707. timer = rb_entry(node, struct hrtimer, node);
  708. BUG_ON(timer->state & HRTIMER_STATE_CALLBACK);
  709. __remove_hrtimer(timer, old_base, HRTIMER_STATE_INACTIVE);
  710. timer->base = new_base;
  711. enqueue_hrtimer(timer, new_base);
  712. }
  713. }
  714. static void migrate_hrtimers(int cpu)
  715. {
  716. struct hrtimer_cpu_base *old_base, *new_base;
  717. int i;
  718. BUG_ON(cpu_online(cpu));
  719. old_base = &per_cpu(hrtimer_bases, cpu);
  720. new_base = &get_cpu_var(hrtimer_bases);
  721. local_irq_disable();
  722. spin_lock(&new_base->lock);
  723. spin_lock(&old_base->lock);
  724. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  725. migrate_hrtimer_list(&old_base->clock_base[i],
  726. &new_base->clock_base[i]);
  727. }
  728. spin_unlock(&old_base->lock);
  729. spin_unlock(&new_base->lock);
  730. local_irq_enable();
  731. put_cpu_var(hrtimer_bases);
  732. }
  733. #endif /* CONFIG_HOTPLUG_CPU */
  734. static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
  735. unsigned long action, void *hcpu)
  736. {
  737. long cpu = (long)hcpu;
  738. switch (action) {
  739. case CPU_UP_PREPARE:
  740. init_hrtimers_cpu(cpu);
  741. break;
  742. #ifdef CONFIG_HOTPLUG_CPU
  743. case CPU_DEAD:
  744. clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &cpu);
  745. migrate_hrtimers(cpu);
  746. break;
  747. #endif
  748. default:
  749. break;
  750. }
  751. return NOTIFY_OK;
  752. }
  753. static struct notifier_block __cpuinitdata hrtimers_nb = {
  754. .notifier_call = hrtimer_cpu_notify,
  755. };
  756. void __init hrtimers_init(void)
  757. {
  758. hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
  759. (void *)(long)smp_processor_id());
  760. register_cpu_notifier(&hrtimers_nb);
  761. }