hrtimer.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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. 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. 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. * Counterpart to lock_timer_base above:
  271. */
  272. static inline
  273. void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
  274. {
  275. spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
  276. }
  277. /**
  278. * hrtimer_forward - forward the timer expiry
  279. * @timer: hrtimer to forward
  280. * @now: forward past this time
  281. * @interval: the interval to forward
  282. *
  283. * Forward the timer expiry so it will expire in the future.
  284. * Returns the number of overruns.
  285. */
  286. unsigned long
  287. hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
  288. {
  289. unsigned long orun = 1;
  290. ktime_t delta;
  291. delta = ktime_sub(now, timer->expires);
  292. if (delta.tv64 < 0)
  293. return 0;
  294. if (interval.tv64 < timer->base->resolution.tv64)
  295. interval.tv64 = timer->base->resolution.tv64;
  296. if (unlikely(delta.tv64 >= interval.tv64)) {
  297. s64 incr = ktime_to_ns(interval);
  298. orun = ktime_divns(delta, incr);
  299. timer->expires = ktime_add_ns(timer->expires, incr * orun);
  300. if (timer->expires.tv64 > now.tv64)
  301. return orun;
  302. /*
  303. * This (and the ktime_add() below) is the
  304. * correction for exact:
  305. */
  306. orun++;
  307. }
  308. timer->expires = ktime_add(timer->expires, interval);
  309. return orun;
  310. }
  311. /*
  312. * enqueue_hrtimer - internal function to (re)start a timer
  313. *
  314. * The timer is inserted in expiry order. Insertion into the
  315. * red black tree is O(log(n)). Must hold the base lock.
  316. */
  317. static void enqueue_hrtimer(struct hrtimer *timer,
  318. struct hrtimer_clock_base *base)
  319. {
  320. struct rb_node **link = &base->active.rb_node;
  321. struct rb_node *parent = NULL;
  322. struct hrtimer *entry;
  323. /*
  324. * Find the right place in the rbtree:
  325. */
  326. while (*link) {
  327. parent = *link;
  328. entry = rb_entry(parent, struct hrtimer, node);
  329. /*
  330. * We dont care about collisions. Nodes with
  331. * the same expiry time stay together.
  332. */
  333. if (timer->expires.tv64 < entry->expires.tv64)
  334. link = &(*link)->rb_left;
  335. else
  336. link = &(*link)->rb_right;
  337. }
  338. /*
  339. * Insert the timer to the rbtree and check whether it
  340. * replaces the first pending timer
  341. */
  342. rb_link_node(&timer->node, parent, link);
  343. rb_insert_color(&timer->node, &base->active);
  344. /*
  345. * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
  346. * state of a possibly running callback.
  347. */
  348. timer->state |= HRTIMER_STATE_ENQUEUED;
  349. if (!base->first || timer->expires.tv64 <
  350. rb_entry(base->first, struct hrtimer, node)->expires.tv64)
  351. base->first = &timer->node;
  352. }
  353. /*
  354. * __remove_hrtimer - internal function to remove a timer
  355. *
  356. * Caller must hold the base lock.
  357. */
  358. static void __remove_hrtimer(struct hrtimer *timer,
  359. struct hrtimer_clock_base *base,
  360. unsigned long newstate)
  361. {
  362. /*
  363. * Remove the timer from the rbtree and replace the
  364. * first entry pointer if necessary.
  365. */
  366. if (base->first == &timer->node)
  367. base->first = rb_next(&timer->node);
  368. rb_erase(&timer->node, &base->active);
  369. timer->state = newstate;
  370. }
  371. /*
  372. * remove hrtimer, called with base lock held
  373. */
  374. static inline int
  375. remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
  376. {
  377. if (hrtimer_is_queued(timer)) {
  378. __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE);
  379. return 1;
  380. }
  381. return 0;
  382. }
  383. /**
  384. * hrtimer_start - (re)start an relative timer on the current CPU
  385. * @timer: the timer to be added
  386. * @tim: expiry time
  387. * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
  388. *
  389. * Returns:
  390. * 0 on success
  391. * 1 when the timer was active
  392. */
  393. int
  394. hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
  395. {
  396. struct hrtimer_clock_base *base, *new_base;
  397. unsigned long flags;
  398. int ret;
  399. base = lock_hrtimer_base(timer, &flags);
  400. /* Remove an active timer from the queue: */
  401. ret = remove_hrtimer(timer, base);
  402. /* Switch the timer base, if necessary: */
  403. new_base = switch_hrtimer_base(timer, base);
  404. if (mode == HRTIMER_MODE_REL) {
  405. tim = ktime_add(tim, new_base->get_time());
  406. /*
  407. * CONFIG_TIME_LOW_RES is a temporary way for architectures
  408. * to signal that they simply return xtime in
  409. * do_gettimeoffset(). In this case we want to round up by
  410. * resolution when starting a relative timer, to avoid short
  411. * timeouts. This will go away with the GTOD framework.
  412. */
  413. #ifdef CONFIG_TIME_LOW_RES
  414. tim = ktime_add(tim, base->resolution);
  415. #endif
  416. }
  417. timer->expires = tim;
  418. enqueue_hrtimer(timer, new_base);
  419. unlock_hrtimer_base(timer, &flags);
  420. return ret;
  421. }
  422. EXPORT_SYMBOL_GPL(hrtimer_start);
  423. /**
  424. * hrtimer_try_to_cancel - try to deactivate a timer
  425. * @timer: hrtimer to stop
  426. *
  427. * Returns:
  428. * 0 when the timer was not active
  429. * 1 when the timer was active
  430. * -1 when the timer is currently excuting the callback function and
  431. * cannot be stopped
  432. */
  433. int hrtimer_try_to_cancel(struct hrtimer *timer)
  434. {
  435. struct hrtimer_clock_base *base;
  436. unsigned long flags;
  437. int ret = -1;
  438. base = lock_hrtimer_base(timer, &flags);
  439. if (!hrtimer_callback_running(timer))
  440. ret = remove_hrtimer(timer, base);
  441. unlock_hrtimer_base(timer, &flags);
  442. return ret;
  443. }
  444. EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
  445. /**
  446. * hrtimer_cancel - cancel a timer and wait for the handler to finish.
  447. * @timer: the timer to be cancelled
  448. *
  449. * Returns:
  450. * 0 when the timer was not active
  451. * 1 when the timer was active
  452. */
  453. int hrtimer_cancel(struct hrtimer *timer)
  454. {
  455. for (;;) {
  456. int ret = hrtimer_try_to_cancel(timer);
  457. if (ret >= 0)
  458. return ret;
  459. cpu_relax();
  460. }
  461. }
  462. EXPORT_SYMBOL_GPL(hrtimer_cancel);
  463. /**
  464. * hrtimer_get_remaining - get remaining time for the timer
  465. * @timer: the timer to read
  466. */
  467. ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
  468. {
  469. struct hrtimer_clock_base *base;
  470. unsigned long flags;
  471. ktime_t rem;
  472. base = lock_hrtimer_base(timer, &flags);
  473. rem = ktime_sub(timer->expires, base->get_time());
  474. unlock_hrtimer_base(timer, &flags);
  475. return rem;
  476. }
  477. EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
  478. #if defined(CONFIG_NO_IDLE_HZ) || defined(CONFIG_NO_HZ)
  479. /**
  480. * hrtimer_get_next_event - get the time until next expiry event
  481. *
  482. * Returns the delta to the next expiry event or KTIME_MAX if no timer
  483. * is pending.
  484. */
  485. ktime_t hrtimer_get_next_event(void)
  486. {
  487. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  488. struct hrtimer_clock_base *base = cpu_base->clock_base;
  489. ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
  490. unsigned long flags;
  491. int i;
  492. spin_lock_irqsave(&cpu_base->lock, flags);
  493. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
  494. struct hrtimer *timer;
  495. if (!base->first)
  496. continue;
  497. timer = rb_entry(base->first, struct hrtimer, node);
  498. delta.tv64 = timer->expires.tv64;
  499. delta = ktime_sub(delta, base->get_time());
  500. if (delta.tv64 < mindelta.tv64)
  501. mindelta.tv64 = delta.tv64;
  502. }
  503. spin_unlock_irqrestore(&cpu_base->lock, flags);
  504. if (mindelta.tv64 < 0)
  505. mindelta.tv64 = 0;
  506. return mindelta;
  507. }
  508. #endif
  509. /**
  510. * hrtimer_init - initialize a timer to the given clock
  511. * @timer: the timer to be initialized
  512. * @clock_id: the clock to be used
  513. * @mode: timer mode abs/rel
  514. */
  515. void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  516. enum hrtimer_mode mode)
  517. {
  518. struct hrtimer_cpu_base *cpu_base;
  519. memset(timer, 0, sizeof(struct hrtimer));
  520. cpu_base = &__raw_get_cpu_var(hrtimer_bases);
  521. if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
  522. clock_id = CLOCK_MONOTONIC;
  523. timer->base = &cpu_base->clock_base[clock_id];
  524. }
  525. EXPORT_SYMBOL_GPL(hrtimer_init);
  526. /**
  527. * hrtimer_get_res - get the timer resolution for a clock
  528. * @which_clock: which clock to query
  529. * @tp: pointer to timespec variable to store the resolution
  530. *
  531. * Store the resolution of the clock selected by @which_clock in the
  532. * variable pointed to by @tp.
  533. */
  534. int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
  535. {
  536. struct hrtimer_cpu_base *cpu_base;
  537. cpu_base = &__raw_get_cpu_var(hrtimer_bases);
  538. *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
  539. return 0;
  540. }
  541. EXPORT_SYMBOL_GPL(hrtimer_get_res);
  542. /*
  543. * Expire the per base hrtimer-queue:
  544. */
  545. static inline void run_hrtimer_queue(struct hrtimer_cpu_base *cpu_base,
  546. int index)
  547. {
  548. struct rb_node *node;
  549. struct hrtimer_clock_base *base = &cpu_base->clock_base[index];
  550. if (!base->first)
  551. return;
  552. if (base->get_softirq_time)
  553. base->softirq_time = base->get_softirq_time();
  554. spin_lock_irq(&cpu_base->lock);
  555. while ((node = base->first)) {
  556. struct hrtimer *timer;
  557. enum hrtimer_restart (*fn)(struct hrtimer *);
  558. int restart;
  559. timer = rb_entry(node, struct hrtimer, node);
  560. if (base->softirq_time.tv64 <= timer->expires.tv64)
  561. break;
  562. fn = timer->function;
  563. __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK);
  564. spin_unlock_irq(&cpu_base->lock);
  565. restart = fn(timer);
  566. spin_lock_irq(&cpu_base->lock);
  567. timer->state &= ~HRTIMER_STATE_CALLBACK;
  568. if (restart != HRTIMER_NORESTART) {
  569. BUG_ON(hrtimer_active(timer));
  570. enqueue_hrtimer(timer, base);
  571. }
  572. }
  573. spin_unlock_irq(&cpu_base->lock);
  574. }
  575. /*
  576. * Called from timer softirq every jiffy, expire hrtimers:
  577. */
  578. void hrtimer_run_queues(void)
  579. {
  580. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  581. int i;
  582. hrtimer_get_softirq_time(cpu_base);
  583. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
  584. run_hrtimer_queue(cpu_base, i);
  585. }
  586. /*
  587. * Sleep related functions:
  588. */
  589. static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
  590. {
  591. struct hrtimer_sleeper *t =
  592. container_of(timer, struct hrtimer_sleeper, timer);
  593. struct task_struct *task = t->task;
  594. t->task = NULL;
  595. if (task)
  596. wake_up_process(task);
  597. return HRTIMER_NORESTART;
  598. }
  599. void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
  600. {
  601. sl->timer.function = hrtimer_wakeup;
  602. sl->task = task;
  603. }
  604. static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
  605. {
  606. hrtimer_init_sleeper(t, current);
  607. do {
  608. set_current_state(TASK_INTERRUPTIBLE);
  609. hrtimer_start(&t->timer, t->timer.expires, mode);
  610. schedule();
  611. hrtimer_cancel(&t->timer);
  612. mode = HRTIMER_MODE_ABS;
  613. } while (t->task && !signal_pending(current));
  614. return t->task == NULL;
  615. }
  616. long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
  617. {
  618. struct hrtimer_sleeper t;
  619. struct timespec __user *rmtp;
  620. struct timespec tu;
  621. ktime_t time;
  622. restart->fn = do_no_restart_syscall;
  623. hrtimer_init(&t.timer, restart->arg0, HRTIMER_MODE_ABS);
  624. t.timer.expires.tv64 = ((u64)restart->arg3 << 32) | (u64) restart->arg2;
  625. if (do_nanosleep(&t, HRTIMER_MODE_ABS))
  626. return 0;
  627. rmtp = (struct timespec __user *) restart->arg1;
  628. if (rmtp) {
  629. time = ktime_sub(t.timer.expires, t.timer.base->get_time());
  630. if (time.tv64 <= 0)
  631. return 0;
  632. tu = ktime_to_timespec(time);
  633. if (copy_to_user(rmtp, &tu, sizeof(tu)))
  634. return -EFAULT;
  635. }
  636. restart->fn = hrtimer_nanosleep_restart;
  637. /* The other values in restart are already filled in */
  638. return -ERESTART_RESTARTBLOCK;
  639. }
  640. long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  641. const enum hrtimer_mode mode, const clockid_t clockid)
  642. {
  643. struct restart_block *restart;
  644. struct hrtimer_sleeper t;
  645. struct timespec tu;
  646. ktime_t rem;
  647. hrtimer_init(&t.timer, clockid, mode);
  648. t.timer.expires = timespec_to_ktime(*rqtp);
  649. if (do_nanosleep(&t, mode))
  650. return 0;
  651. /* Absolute timers do not update the rmtp value and restart: */
  652. if (mode == HRTIMER_MODE_ABS)
  653. return -ERESTARTNOHAND;
  654. if (rmtp) {
  655. rem = ktime_sub(t.timer.expires, t.timer.base->get_time());
  656. if (rem.tv64 <= 0)
  657. return 0;
  658. tu = ktime_to_timespec(rem);
  659. if (copy_to_user(rmtp, &tu, sizeof(tu)))
  660. return -EFAULT;
  661. }
  662. restart = &current_thread_info()->restart_block;
  663. restart->fn = hrtimer_nanosleep_restart;
  664. restart->arg0 = (unsigned long) t.timer.base->index;
  665. restart->arg1 = (unsigned long) rmtp;
  666. restart->arg2 = t.timer.expires.tv64 & 0xFFFFFFFF;
  667. restart->arg3 = t.timer.expires.tv64 >> 32;
  668. return -ERESTART_RESTARTBLOCK;
  669. }
  670. asmlinkage long
  671. sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp)
  672. {
  673. struct timespec tu;
  674. if (copy_from_user(&tu, rqtp, sizeof(tu)))
  675. return -EFAULT;
  676. if (!timespec_valid(&tu))
  677. return -EINVAL;
  678. return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
  679. }
  680. /*
  681. * Functions related to boot-time initialization:
  682. */
  683. static void __devinit init_hrtimers_cpu(int cpu)
  684. {
  685. struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
  686. int i;
  687. spin_lock_init(&cpu_base->lock);
  688. lockdep_set_class(&cpu_base->lock, &cpu_base->lock_key);
  689. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
  690. cpu_base->clock_base[i].cpu_base = cpu_base;
  691. }
  692. #ifdef CONFIG_HOTPLUG_CPU
  693. static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
  694. struct hrtimer_clock_base *new_base)
  695. {
  696. struct hrtimer *timer;
  697. struct rb_node *node;
  698. while ((node = rb_first(&old_base->active))) {
  699. timer = rb_entry(node, struct hrtimer, node);
  700. BUG_ON(timer->state & HRTIMER_STATE_CALLBACK);
  701. __remove_hrtimer(timer, old_base, HRTIMER_STATE_INACTIVE);
  702. timer->base = new_base;
  703. enqueue_hrtimer(timer, new_base);
  704. }
  705. }
  706. static void migrate_hrtimers(int cpu)
  707. {
  708. struct hrtimer_cpu_base *old_base, *new_base;
  709. int i;
  710. BUG_ON(cpu_online(cpu));
  711. old_base = &per_cpu(hrtimer_bases, cpu);
  712. new_base = &get_cpu_var(hrtimer_bases);
  713. local_irq_disable();
  714. spin_lock(&new_base->lock);
  715. spin_lock(&old_base->lock);
  716. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  717. migrate_hrtimer_list(&old_base->clock_base[i],
  718. &new_base->clock_base[i]);
  719. }
  720. spin_unlock(&old_base->lock);
  721. spin_unlock(&new_base->lock);
  722. local_irq_enable();
  723. put_cpu_var(hrtimer_bases);
  724. }
  725. #endif /* CONFIG_HOTPLUG_CPU */
  726. static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
  727. unsigned long action, void *hcpu)
  728. {
  729. long cpu = (long)hcpu;
  730. switch (action) {
  731. case CPU_UP_PREPARE:
  732. init_hrtimers_cpu(cpu);
  733. break;
  734. #ifdef CONFIG_HOTPLUG_CPU
  735. case CPU_DEAD:
  736. clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &cpu);
  737. migrate_hrtimers(cpu);
  738. break;
  739. #endif
  740. default:
  741. break;
  742. }
  743. return NOTIFY_OK;
  744. }
  745. static struct notifier_block __cpuinitdata hrtimers_nb = {
  746. .notifier_call = hrtimer_cpu_notify,
  747. };
  748. void __init hrtimers_init(void)
  749. {
  750. hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
  751. (void *)(long)smp_processor_id());
  752. register_cpu_notifier(&hrtimers_nb);
  753. }