hrtimer.c 35 KB

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