hrtimer.c 36 KB

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