hrtimer.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. /*
  2. * linux/kernel/hrtimer.c
  3. *
  4. * Copyright(C) 2005, Thomas Gleixner <tglx@linutronix.de>
  5. * Copyright(C) 2005, Red Hat, Inc., Ingo Molnar
  6. *
  7. * High-resolution kernel timers
  8. *
  9. * In contrast to the low-resolution timeout API implemented in
  10. * kernel/timer.c, hrtimers provide finer resolution and accuracy
  11. * depending on system configuration and capabilities.
  12. *
  13. * These timers are currently used for:
  14. * - itimers
  15. * - POSIX timers
  16. * - nanosleep
  17. * - precise in-kernel timing
  18. *
  19. * Started by: Thomas Gleixner and Ingo Molnar
  20. *
  21. * Credits:
  22. * based on kernel/timer.c
  23. *
  24. * For licencing details see kernel-base/COPYING
  25. */
  26. #include <linux/cpu.h>
  27. #include <linux/module.h>
  28. #include <linux/percpu.h>
  29. #include <linux/hrtimer.h>
  30. #include <linux/notifier.h>
  31. #include <linux/syscalls.h>
  32. #include <linux/interrupt.h>
  33. #include <asm/uaccess.h>
  34. /**
  35. * ktime_get - get the monotonic time in ktime_t format
  36. *
  37. * returns the time in ktime_t format
  38. */
  39. static ktime_t ktime_get(void)
  40. {
  41. struct timespec now;
  42. ktime_get_ts(&now);
  43. return timespec_to_ktime(now);
  44. }
  45. /**
  46. * ktime_get_real - get the real (wall-) time in ktime_t format
  47. *
  48. * returns the time in ktime_t format
  49. */
  50. static ktime_t ktime_get_real(void)
  51. {
  52. struct timespec now;
  53. getnstimeofday(&now);
  54. return timespec_to_ktime(now);
  55. }
  56. EXPORT_SYMBOL_GPL(ktime_get_real);
  57. /*
  58. * The timer bases:
  59. */
  60. #define MAX_HRTIMER_BASES 2
  61. static DEFINE_PER_CPU(struct hrtimer_base, hrtimer_bases[MAX_HRTIMER_BASES]) =
  62. {
  63. {
  64. .index = CLOCK_REALTIME,
  65. .get_time = &ktime_get_real,
  66. .resolution = KTIME_REALTIME_RES,
  67. },
  68. {
  69. .index = CLOCK_MONOTONIC,
  70. .get_time = &ktime_get,
  71. .resolution = KTIME_MONOTONIC_RES,
  72. },
  73. };
  74. /**
  75. * ktime_get_ts - get the monotonic clock in timespec format
  76. *
  77. * @ts: pointer to timespec variable
  78. *
  79. * The function calculates the monotonic clock from the realtime
  80. * clock and the wall_to_monotonic offset and stores the result
  81. * in normalized timespec format in the variable pointed to by ts.
  82. */
  83. void ktime_get_ts(struct timespec *ts)
  84. {
  85. struct timespec tomono;
  86. unsigned long seq;
  87. do {
  88. seq = read_seqbegin(&xtime_lock);
  89. getnstimeofday(ts);
  90. tomono = wall_to_monotonic;
  91. } while (read_seqretry(&xtime_lock, seq));
  92. set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
  93. ts->tv_nsec + tomono.tv_nsec);
  94. }
  95. /*
  96. * Functions and macros which are different for UP/SMP systems are kept in a
  97. * single place
  98. */
  99. #ifdef CONFIG_SMP
  100. #define set_curr_timer(b, t) do { (b)->curr_timer = (t); } while (0)
  101. /*
  102. * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
  103. * means that all timers which are tied to this base via timer->base are
  104. * locked, and the base itself is locked too.
  105. *
  106. * So __run_timers/migrate_timers can safely modify all timers which could
  107. * be found on the lists/queues.
  108. *
  109. * When the timer's base is locked, and the timer removed from list, it is
  110. * possible to set timer->base = NULL and drop the lock: the timer remains
  111. * locked.
  112. */
  113. static struct hrtimer_base *lock_hrtimer_base(const struct hrtimer *timer,
  114. unsigned long *flags)
  115. {
  116. struct hrtimer_base *base;
  117. for (;;) {
  118. base = timer->base;
  119. if (likely(base != NULL)) {
  120. spin_lock_irqsave(&base->lock, *flags);
  121. if (likely(base == timer->base))
  122. return base;
  123. /* The timer has migrated to another CPU: */
  124. spin_unlock_irqrestore(&base->lock, *flags);
  125. }
  126. cpu_relax();
  127. }
  128. }
  129. /*
  130. * Switch the timer base to the current CPU when possible.
  131. */
  132. static inline struct hrtimer_base *
  133. switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_base *base)
  134. {
  135. struct hrtimer_base *new_base;
  136. new_base = &__get_cpu_var(hrtimer_bases[base->index]);
  137. if (base != new_base) {
  138. /*
  139. * We are trying to schedule the timer on the local CPU.
  140. * However we can't change timer's base while it is running,
  141. * so we keep it on the same CPU. No hassle vs. reprogramming
  142. * the event source in the high resolution case. The softirq
  143. * code will take care of this when the timer function has
  144. * completed. There is no conflict as we hold the lock until
  145. * the timer is enqueued.
  146. */
  147. if (unlikely(base->curr_timer == timer))
  148. return base;
  149. /* See the comment in lock_timer_base() */
  150. timer->base = NULL;
  151. spin_unlock(&base->lock);
  152. spin_lock(&new_base->lock);
  153. timer->base = new_base;
  154. }
  155. return new_base;
  156. }
  157. #else /* CONFIG_SMP */
  158. #define set_curr_timer(b, t) do { } while (0)
  159. static inline struct hrtimer_base *
  160. lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
  161. {
  162. struct hrtimer_base *base = timer->base;
  163. spin_lock_irqsave(&base->lock, *flags);
  164. return base;
  165. }
  166. #define switch_hrtimer_base(t, b) (b)
  167. #endif /* !CONFIG_SMP */
  168. /*
  169. * Functions for the union type storage format of ktime_t which are
  170. * too large for inlining:
  171. */
  172. #if BITS_PER_LONG < 64
  173. # ifndef CONFIG_KTIME_SCALAR
  174. /**
  175. * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
  176. *
  177. * @kt: addend
  178. * @nsec: the scalar nsec value to add
  179. *
  180. * Returns the sum of kt and nsec in ktime_t format
  181. */
  182. ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
  183. {
  184. ktime_t tmp;
  185. if (likely(nsec < NSEC_PER_SEC)) {
  186. tmp.tv64 = nsec;
  187. } else {
  188. unsigned long rem = do_div(nsec, NSEC_PER_SEC);
  189. tmp = ktime_set((long)nsec, rem);
  190. }
  191. return ktime_add(kt, tmp);
  192. }
  193. #else /* CONFIG_KTIME_SCALAR */
  194. # endif /* !CONFIG_KTIME_SCALAR */
  195. /*
  196. * Divide a ktime value by a nanosecond value
  197. */
  198. static unsigned long ktime_divns(const ktime_t kt, nsec_t div)
  199. {
  200. u64 dclc, inc, dns;
  201. int sft = 0;
  202. dclc = dns = ktime_to_ns(kt);
  203. inc = div;
  204. /* Make sure the divisor is less than 2^32: */
  205. while (div >> 32) {
  206. sft++;
  207. div >>= 1;
  208. }
  209. dclc >>= sft;
  210. do_div(dclc, (unsigned long) div);
  211. return (unsigned long) dclc;
  212. }
  213. #else /* BITS_PER_LONG < 64 */
  214. # define ktime_divns(kt, div) (unsigned long)((kt).tv64 / (div))
  215. #endif /* BITS_PER_LONG >= 64 */
  216. /*
  217. * Counterpart to lock_timer_base above:
  218. */
  219. static inline
  220. void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
  221. {
  222. spin_unlock_irqrestore(&timer->base->lock, *flags);
  223. }
  224. /**
  225. * hrtimer_forward - forward the timer expiry
  226. *
  227. * @timer: hrtimer to forward
  228. * @interval: the interval to forward
  229. *
  230. * Forward the timer expiry so it will expire in the future.
  231. * The number of overruns is added to the overrun field.
  232. */
  233. unsigned long
  234. hrtimer_forward(struct hrtimer *timer, const ktime_t interval)
  235. {
  236. unsigned long orun = 1;
  237. ktime_t delta, now;
  238. now = timer->base->get_time();
  239. delta = ktime_sub(now, timer->expires);
  240. if (delta.tv64 < 0)
  241. return 0;
  242. if (unlikely(delta.tv64 >= interval.tv64)) {
  243. nsec_t incr = ktime_to_ns(interval);
  244. orun = ktime_divns(delta, incr);
  245. timer->expires = ktime_add_ns(timer->expires, incr * orun);
  246. if (timer->expires.tv64 > now.tv64)
  247. return orun;
  248. /*
  249. * This (and the ktime_add() below) is the
  250. * correction for exact:
  251. */
  252. orun++;
  253. }
  254. timer->expires = ktime_add(timer->expires, interval);
  255. return orun;
  256. }
  257. /*
  258. * enqueue_hrtimer - internal function to (re)start a timer
  259. *
  260. * The timer is inserted in expiry order. Insertion into the
  261. * red black tree is O(log(n)). Must hold the base lock.
  262. */
  263. static void enqueue_hrtimer(struct hrtimer *timer, struct hrtimer_base *base)
  264. {
  265. struct rb_node **link = &base->active.rb_node;
  266. struct list_head *prev = &base->pending;
  267. struct rb_node *parent = NULL;
  268. struct hrtimer *entry;
  269. /*
  270. * Find the right place in the rbtree:
  271. */
  272. while (*link) {
  273. parent = *link;
  274. entry = rb_entry(parent, struct hrtimer, node);
  275. /*
  276. * We dont care about collisions. Nodes with
  277. * the same expiry time stay together.
  278. */
  279. if (timer->expires.tv64 < entry->expires.tv64)
  280. link = &(*link)->rb_left;
  281. else {
  282. link = &(*link)->rb_right;
  283. prev = &entry->list;
  284. }
  285. }
  286. /*
  287. * Insert the timer to the rbtree and to the sorted list:
  288. */
  289. rb_link_node(&timer->node, parent, link);
  290. rb_insert_color(&timer->node, &base->active);
  291. list_add(&timer->list, prev);
  292. timer->state = HRTIMER_PENDING;
  293. }
  294. /*
  295. * __remove_hrtimer - internal function to remove a timer
  296. *
  297. * Caller must hold the base lock.
  298. */
  299. static void __remove_hrtimer(struct hrtimer *timer, struct hrtimer_base *base)
  300. {
  301. /*
  302. * Remove the timer from the sorted list and from the rbtree:
  303. */
  304. list_del(&timer->list);
  305. rb_erase(&timer->node, &base->active);
  306. }
  307. /*
  308. * remove hrtimer, called with base lock held
  309. */
  310. static inline int
  311. remove_hrtimer(struct hrtimer *timer, struct hrtimer_base *base)
  312. {
  313. if (hrtimer_active(timer)) {
  314. __remove_hrtimer(timer, base);
  315. timer->state = HRTIMER_INACTIVE;
  316. return 1;
  317. }
  318. return 0;
  319. }
  320. /**
  321. * hrtimer_start - (re)start an relative timer on the current CPU
  322. *
  323. * @timer: the timer to be added
  324. * @tim: expiry time
  325. * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
  326. *
  327. * Returns:
  328. * 0 on success
  329. * 1 when the timer was active
  330. */
  331. int
  332. hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
  333. {
  334. struct hrtimer_base *base, *new_base;
  335. unsigned long flags;
  336. int ret;
  337. base = lock_hrtimer_base(timer, &flags);
  338. /* Remove an active timer from the queue: */
  339. ret = remove_hrtimer(timer, base);
  340. /* Switch the timer base, if necessary: */
  341. new_base = switch_hrtimer_base(timer, base);
  342. if (mode == HRTIMER_REL)
  343. tim = ktime_add(tim, new_base->get_time());
  344. timer->expires = tim;
  345. enqueue_hrtimer(timer, new_base);
  346. unlock_hrtimer_base(timer, &flags);
  347. return ret;
  348. }
  349. /**
  350. * hrtimer_try_to_cancel - try to deactivate a timer
  351. *
  352. * @timer: hrtimer to stop
  353. *
  354. * Returns:
  355. * 0 when the timer was not active
  356. * 1 when the timer was active
  357. * -1 when the timer is currently excuting the callback function and
  358. * can not be stopped
  359. */
  360. int hrtimer_try_to_cancel(struct hrtimer *timer)
  361. {
  362. struct hrtimer_base *base;
  363. unsigned long flags;
  364. int ret = -1;
  365. base = lock_hrtimer_base(timer, &flags);
  366. if (base->curr_timer != timer)
  367. ret = remove_hrtimer(timer, base);
  368. unlock_hrtimer_base(timer, &flags);
  369. return ret;
  370. }
  371. /**
  372. * hrtimer_cancel - cancel a timer and wait for the handler to finish.
  373. *
  374. * @timer: the timer to be cancelled
  375. *
  376. * Returns:
  377. * 0 when the timer was not active
  378. * 1 when the timer was active
  379. */
  380. int hrtimer_cancel(struct hrtimer *timer)
  381. {
  382. for (;;) {
  383. int ret = hrtimer_try_to_cancel(timer);
  384. if (ret >= 0)
  385. return ret;
  386. }
  387. }
  388. /**
  389. * hrtimer_get_remaining - get remaining time for the timer
  390. *
  391. * @timer: the timer to read
  392. */
  393. ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
  394. {
  395. struct hrtimer_base *base;
  396. unsigned long flags;
  397. ktime_t rem;
  398. base = lock_hrtimer_base(timer, &flags);
  399. rem = ktime_sub(timer->expires, timer->base->get_time());
  400. unlock_hrtimer_base(timer, &flags);
  401. return rem;
  402. }
  403. /**
  404. * hrtimer_rebase - rebase an initialized hrtimer to a different base
  405. *
  406. * @timer: the timer to be rebased
  407. * @clock_id: the clock to be used
  408. */
  409. void hrtimer_rebase(struct hrtimer *timer, const clockid_t clock_id)
  410. {
  411. struct hrtimer_base *bases;
  412. bases = per_cpu(hrtimer_bases, raw_smp_processor_id());
  413. timer->base = &bases[clock_id];
  414. }
  415. /**
  416. * hrtimer_init - initialize a timer to the given clock
  417. *
  418. * @timer: the timer to be initialized
  419. * @clock_id: the clock to be used
  420. */
  421. void hrtimer_init(struct hrtimer *timer, const clockid_t clock_id)
  422. {
  423. memset(timer, 0, sizeof(struct hrtimer));
  424. hrtimer_rebase(timer, clock_id);
  425. }
  426. /**
  427. * hrtimer_get_res - get the timer resolution for a clock
  428. *
  429. * @which_clock: which clock to query
  430. * @tp: pointer to timespec variable to store the resolution
  431. *
  432. * Store the resolution of the clock selected by which_clock in the
  433. * variable pointed to by tp.
  434. */
  435. int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
  436. {
  437. struct hrtimer_base *bases;
  438. tp->tv_sec = 0;
  439. bases = per_cpu(hrtimer_bases, raw_smp_processor_id());
  440. tp->tv_nsec = bases[which_clock].resolution;
  441. return 0;
  442. }
  443. /*
  444. * Expire the per base hrtimer-queue:
  445. */
  446. static inline void run_hrtimer_queue(struct hrtimer_base *base)
  447. {
  448. ktime_t now = base->get_time();
  449. spin_lock_irq(&base->lock);
  450. while (!list_empty(&base->pending)) {
  451. struct hrtimer *timer;
  452. int (*fn)(void *);
  453. int restart;
  454. void *data;
  455. timer = list_entry(base->pending.next, struct hrtimer, list);
  456. if (now.tv64 <= timer->expires.tv64)
  457. break;
  458. fn = timer->function;
  459. data = timer->data;
  460. set_curr_timer(base, timer);
  461. __remove_hrtimer(timer, base);
  462. spin_unlock_irq(&base->lock);
  463. /*
  464. * fn == NULL is special case for the simplest timer
  465. * variant - wake up process and do not restart:
  466. */
  467. if (!fn) {
  468. wake_up_process(data);
  469. restart = HRTIMER_NORESTART;
  470. } else
  471. restart = fn(data);
  472. spin_lock_irq(&base->lock);
  473. if (restart == HRTIMER_RESTART)
  474. enqueue_hrtimer(timer, base);
  475. else
  476. timer->state = HRTIMER_EXPIRED;
  477. }
  478. set_curr_timer(base, NULL);
  479. spin_unlock_irq(&base->lock);
  480. }
  481. /*
  482. * Called from timer softirq every jiffy, expire hrtimers:
  483. */
  484. void hrtimer_run_queues(void)
  485. {
  486. struct hrtimer_base *base = __get_cpu_var(hrtimer_bases);
  487. int i;
  488. for (i = 0; i < MAX_HRTIMER_BASES; i++)
  489. run_hrtimer_queue(&base[i]);
  490. }
  491. /*
  492. * Functions related to boot-time initialization:
  493. */
  494. static void __devinit init_hrtimers_cpu(int cpu)
  495. {
  496. struct hrtimer_base *base = per_cpu(hrtimer_bases, cpu);
  497. int i;
  498. for (i = 0; i < MAX_HRTIMER_BASES; i++) {
  499. spin_lock_init(&base->lock);
  500. INIT_LIST_HEAD(&base->pending);
  501. base++;
  502. }
  503. }
  504. #ifdef CONFIG_HOTPLUG_CPU
  505. static void migrate_hrtimer_list(struct hrtimer_base *old_base,
  506. struct hrtimer_base *new_base)
  507. {
  508. struct hrtimer *timer;
  509. struct rb_node *node;
  510. while ((node = rb_first(&old_base->active))) {
  511. timer = rb_entry(node, struct hrtimer, node);
  512. __remove_hrtimer(timer, old_base);
  513. timer->base = new_base;
  514. enqueue_hrtimer(timer, new_base);
  515. }
  516. }
  517. static void migrate_hrtimers(int cpu)
  518. {
  519. struct hrtimer_base *old_base, *new_base;
  520. int i;
  521. BUG_ON(cpu_online(cpu));
  522. old_base = per_cpu(hrtimer_bases, cpu);
  523. new_base = get_cpu_var(hrtimer_bases);
  524. local_irq_disable();
  525. for (i = 0; i < MAX_HRTIMER_BASES; i++) {
  526. spin_lock(&new_base->lock);
  527. spin_lock(&old_base->lock);
  528. BUG_ON(old_base->curr_timer);
  529. migrate_hrtimer_list(old_base, new_base);
  530. spin_unlock(&old_base->lock);
  531. spin_unlock(&new_base->lock);
  532. old_base++;
  533. new_base++;
  534. }
  535. local_irq_enable();
  536. put_cpu_var(hrtimer_bases);
  537. }
  538. #endif /* CONFIG_HOTPLUG_CPU */
  539. static int __devinit hrtimer_cpu_notify(struct notifier_block *self,
  540. unsigned long action, void *hcpu)
  541. {
  542. long cpu = (long)hcpu;
  543. switch (action) {
  544. case CPU_UP_PREPARE:
  545. init_hrtimers_cpu(cpu);
  546. break;
  547. #ifdef CONFIG_HOTPLUG_CPU
  548. case CPU_DEAD:
  549. migrate_hrtimers(cpu);
  550. break;
  551. #endif
  552. default:
  553. break;
  554. }
  555. return NOTIFY_OK;
  556. }
  557. static struct notifier_block __devinitdata hrtimers_nb = {
  558. .notifier_call = hrtimer_cpu_notify,
  559. };
  560. void __init hrtimers_init(void)
  561. {
  562. hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
  563. (void *)(long)smp_processor_id());
  564. register_cpu_notifier(&hrtimers_nb);
  565. }