hrtimer.c 35 KB

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