hrtimer.c 21 KB

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