hrtimer.c 35 KB

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