hrtimer.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511
  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. u64 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 dclc;
  280. }
  281. #endif /* BITS_PER_LONG >= 64 */
  282. /*
  283. * Check, whether the timer is on the callback pending list
  284. */
  285. static inline int hrtimer_cb_pending(const struct hrtimer *timer)
  286. {
  287. return timer->state & HRTIMER_STATE_PENDING;
  288. }
  289. /*
  290. * Remove a timer from the callback pending list
  291. */
  292. static inline void hrtimer_remove_cb_pending(struct hrtimer *timer)
  293. {
  294. list_del_init(&timer->cb_entry);
  295. }
  296. /* High resolution timer related functions */
  297. #ifdef CONFIG_HIGH_RES_TIMERS
  298. /*
  299. * High resolution timer enabled ?
  300. */
  301. static int hrtimer_hres_enabled __read_mostly = 1;
  302. /*
  303. * Enable / Disable high resolution mode
  304. */
  305. static int __init setup_hrtimer_hres(char *str)
  306. {
  307. if (!strcmp(str, "off"))
  308. hrtimer_hres_enabled = 0;
  309. else if (!strcmp(str, "on"))
  310. hrtimer_hres_enabled = 1;
  311. else
  312. return 0;
  313. return 1;
  314. }
  315. __setup("highres=", setup_hrtimer_hres);
  316. /*
  317. * hrtimer_high_res_enabled - query, if the highres mode is enabled
  318. */
  319. static inline int hrtimer_is_hres_enabled(void)
  320. {
  321. return hrtimer_hres_enabled;
  322. }
  323. /*
  324. * Is the high resolution mode active ?
  325. */
  326. static inline int hrtimer_hres_active(void)
  327. {
  328. return __get_cpu_var(hrtimer_bases).hres_active;
  329. }
  330. /*
  331. * Reprogram the event source with checking both queues for the
  332. * next event
  333. * Called with interrupts disabled and base->lock held
  334. */
  335. static void hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base)
  336. {
  337. int i;
  338. struct hrtimer_clock_base *base = cpu_base->clock_base;
  339. ktime_t expires;
  340. cpu_base->expires_next.tv64 = KTIME_MAX;
  341. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
  342. struct hrtimer *timer;
  343. if (!base->first)
  344. continue;
  345. timer = rb_entry(base->first, struct hrtimer, node);
  346. expires = ktime_sub(timer->expires, base->offset);
  347. if (expires.tv64 < cpu_base->expires_next.tv64)
  348. cpu_base->expires_next = expires;
  349. }
  350. if (cpu_base->expires_next.tv64 != KTIME_MAX)
  351. tick_program_event(cpu_base->expires_next, 1);
  352. }
  353. /*
  354. * Shared reprogramming for clock_realtime and clock_monotonic
  355. *
  356. * When a timer is enqueued and expires earlier than the already enqueued
  357. * timers, we have to check, whether it expires earlier than the timer for
  358. * which the clock event device was armed.
  359. *
  360. * Called with interrupts disabled and base->cpu_base.lock held
  361. */
  362. static int hrtimer_reprogram(struct hrtimer *timer,
  363. struct hrtimer_clock_base *base)
  364. {
  365. ktime_t *expires_next = &__get_cpu_var(hrtimer_bases).expires_next;
  366. ktime_t expires = ktime_sub(timer->expires, base->offset);
  367. int res;
  368. /*
  369. * When the callback is running, we do not reprogram the clock event
  370. * device. The timer callback is either running on a different CPU or
  371. * the callback is executed in the hrtimer_interrupt context. The
  372. * reprogramming is handled either by the softirq, which called the
  373. * callback or at the end of the hrtimer_interrupt.
  374. */
  375. if (hrtimer_callback_running(timer))
  376. return 0;
  377. if (expires.tv64 >= expires_next->tv64)
  378. return 0;
  379. /*
  380. * Clockevents returns -ETIME, when the event was in the past.
  381. */
  382. res = tick_program_event(expires, 0);
  383. if (!IS_ERR_VALUE(res))
  384. *expires_next = expires;
  385. return res;
  386. }
  387. /*
  388. * Retrigger next event is called after clock was set
  389. *
  390. * Called with interrupts disabled via on_each_cpu()
  391. */
  392. static void retrigger_next_event(void *arg)
  393. {
  394. struct hrtimer_cpu_base *base;
  395. struct timespec realtime_offset;
  396. unsigned long seq;
  397. if (!hrtimer_hres_active())
  398. return;
  399. do {
  400. seq = read_seqbegin(&xtime_lock);
  401. set_normalized_timespec(&realtime_offset,
  402. -wall_to_monotonic.tv_sec,
  403. -wall_to_monotonic.tv_nsec);
  404. } while (read_seqretry(&xtime_lock, seq));
  405. base = &__get_cpu_var(hrtimer_bases);
  406. /* Adjust CLOCK_REALTIME offset */
  407. spin_lock(&base->lock);
  408. base->clock_base[CLOCK_REALTIME].offset =
  409. timespec_to_ktime(realtime_offset);
  410. hrtimer_force_reprogram(base);
  411. spin_unlock(&base->lock);
  412. }
  413. /*
  414. * Clock realtime was set
  415. *
  416. * Change the offset of the realtime clock vs. the monotonic
  417. * clock.
  418. *
  419. * We might have to reprogram the high resolution timer interrupt. On
  420. * SMP we call the architecture specific code to retrigger _all_ high
  421. * resolution timer interrupts. On UP we just disable interrupts and
  422. * call the high resolution interrupt code.
  423. */
  424. void clock_was_set(void)
  425. {
  426. /* Retrigger the CPU local events everywhere */
  427. on_each_cpu(retrigger_next_event, NULL, 0, 1);
  428. }
  429. /*
  430. * During resume we might have to reprogram the high resolution timer
  431. * interrupt (on the local CPU):
  432. */
  433. void hres_timers_resume(void)
  434. {
  435. WARN_ON_ONCE(num_online_cpus() > 1);
  436. /* Retrigger the CPU local events: */
  437. retrigger_next_event(NULL);
  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. }
  447. /*
  448. * Initialize the high resolution related parts of a hrtimer
  449. */
  450. static inline void hrtimer_init_timer_hres(struct hrtimer *timer)
  451. {
  452. }
  453. /*
  454. * When High resolution timers are active, try to reprogram. Note, that in case
  455. * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
  456. * check happens. The timer gets enqueued into the rbtree. The reprogramming
  457. * and expiry check is done in the hrtimer_interrupt or in the softirq.
  458. */
  459. static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
  460. struct hrtimer_clock_base *base)
  461. {
  462. if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
  463. /* Timer is expired, act upon the callback mode */
  464. switch(timer->cb_mode) {
  465. case HRTIMER_CB_IRQSAFE_NO_RESTART:
  466. /*
  467. * We can call the callback from here. No restart
  468. * happens, so no danger of recursion
  469. */
  470. BUG_ON(timer->function(timer) != HRTIMER_NORESTART);
  471. return 1;
  472. case HRTIMER_CB_IRQSAFE_NO_SOFTIRQ:
  473. /*
  474. * This is solely for the sched tick emulation with
  475. * dynamic tick support to ensure that we do not
  476. * restart the tick right on the edge and end up with
  477. * the tick timer in the softirq ! The calling site
  478. * takes care of this.
  479. */
  480. return 1;
  481. case HRTIMER_CB_IRQSAFE:
  482. case HRTIMER_CB_SOFTIRQ:
  483. /*
  484. * Move everything else into the softirq pending list !
  485. */
  486. list_add_tail(&timer->cb_entry,
  487. &base->cpu_base->cb_pending);
  488. timer->state = HRTIMER_STATE_PENDING;
  489. raise_softirq(HRTIMER_SOFTIRQ);
  490. return 1;
  491. default:
  492. BUG();
  493. }
  494. }
  495. return 0;
  496. }
  497. /*
  498. * Switch to high resolution mode
  499. */
  500. static int hrtimer_switch_to_hres(void)
  501. {
  502. int cpu = smp_processor_id();
  503. struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
  504. unsigned long flags;
  505. if (base->hres_active)
  506. return 1;
  507. local_irq_save(flags);
  508. if (tick_init_highres()) {
  509. local_irq_restore(flags);
  510. printk(KERN_WARNING "Could not switch to high resolution "
  511. "mode on CPU %d\n", cpu);
  512. return 0;
  513. }
  514. base->hres_active = 1;
  515. base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES;
  516. base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES;
  517. tick_setup_sched_timer();
  518. /* "Retrigger" the interrupt to get things going */
  519. retrigger_next_event(NULL);
  520. local_irq_restore(flags);
  521. printk(KERN_DEBUG "Switched to high resolution mode on CPU %d\n",
  522. smp_processor_id());
  523. return 1;
  524. }
  525. #else
  526. static inline int hrtimer_hres_active(void) { return 0; }
  527. static inline int hrtimer_is_hres_enabled(void) { return 0; }
  528. static inline int hrtimer_switch_to_hres(void) { return 0; }
  529. static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base *base) { }
  530. static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
  531. struct hrtimer_clock_base *base)
  532. {
  533. return 0;
  534. }
  535. static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
  536. static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { }
  537. static inline int hrtimer_reprogram(struct hrtimer *timer,
  538. struct hrtimer_clock_base *base)
  539. {
  540. return 0;
  541. }
  542. #endif /* CONFIG_HIGH_RES_TIMERS */
  543. #ifdef CONFIG_TIMER_STATS
  544. void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer, void *addr)
  545. {
  546. if (timer->start_site)
  547. return;
  548. timer->start_site = addr;
  549. memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
  550. timer->start_pid = current->pid;
  551. }
  552. #endif
  553. /*
  554. * Counterpart to lock_hrtimer_base above:
  555. */
  556. static inline
  557. void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
  558. {
  559. spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
  560. }
  561. /**
  562. * hrtimer_forward - forward the timer expiry
  563. * @timer: hrtimer to forward
  564. * @now: forward past this time
  565. * @interval: the interval to forward
  566. *
  567. * Forward the timer expiry so it will expire in the future.
  568. * Returns the number of overruns.
  569. */
  570. u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
  571. {
  572. u64 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. * Careful here: User space might have asked for a
  747. * very long sleep, so the add above might result in a
  748. * negative number, which enqueues the timer in front
  749. * of the queue.
  750. */
  751. if (tim.tv64 < 0)
  752. tim.tv64 = KTIME_MAX;
  753. }
  754. timer->expires = tim;
  755. timer_stats_hrtimer_set_start_info(timer);
  756. /*
  757. * Only allow reprogramming if the new base is on this CPU.
  758. * (it might still be on another CPU if the timer was pending)
  759. */
  760. enqueue_hrtimer(timer, new_base,
  761. new_base->cpu_base == &__get_cpu_var(hrtimer_bases));
  762. unlock_hrtimer_base(timer, &flags);
  763. return ret;
  764. }
  765. EXPORT_SYMBOL_GPL(hrtimer_start);
  766. /**
  767. * hrtimer_try_to_cancel - try to deactivate a timer
  768. * @timer: hrtimer to stop
  769. *
  770. * Returns:
  771. * 0 when the timer was not active
  772. * 1 when the timer was active
  773. * -1 when the timer is currently excuting the callback function and
  774. * cannot be stopped
  775. */
  776. int hrtimer_try_to_cancel(struct hrtimer *timer)
  777. {
  778. struct hrtimer_clock_base *base;
  779. unsigned long flags;
  780. int ret = -1;
  781. base = lock_hrtimer_base(timer, &flags);
  782. if (!hrtimer_callback_running(timer))
  783. ret = remove_hrtimer(timer, base);
  784. unlock_hrtimer_base(timer, &flags);
  785. return ret;
  786. }
  787. EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
  788. /**
  789. * hrtimer_cancel - cancel a timer and wait for the handler to finish.
  790. * @timer: the timer to be cancelled
  791. *
  792. * Returns:
  793. * 0 when the timer was not active
  794. * 1 when the timer was active
  795. */
  796. int hrtimer_cancel(struct hrtimer *timer)
  797. {
  798. for (;;) {
  799. int ret = hrtimer_try_to_cancel(timer);
  800. if (ret >= 0)
  801. return ret;
  802. cpu_relax();
  803. }
  804. }
  805. EXPORT_SYMBOL_GPL(hrtimer_cancel);
  806. /**
  807. * hrtimer_get_remaining - get remaining time for the timer
  808. * @timer: the timer to read
  809. */
  810. ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
  811. {
  812. struct hrtimer_clock_base *base;
  813. unsigned long flags;
  814. ktime_t rem;
  815. base = lock_hrtimer_base(timer, &flags);
  816. rem = ktime_sub(timer->expires, base->get_time());
  817. unlock_hrtimer_base(timer, &flags);
  818. return rem;
  819. }
  820. EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
  821. #if defined(CONFIG_NO_IDLE_HZ) || defined(CONFIG_NO_HZ)
  822. /**
  823. * hrtimer_get_next_event - get the time until next expiry event
  824. *
  825. * Returns the delta to the next expiry event or KTIME_MAX if no timer
  826. * is pending.
  827. */
  828. ktime_t hrtimer_get_next_event(void)
  829. {
  830. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  831. struct hrtimer_clock_base *base = cpu_base->clock_base;
  832. ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
  833. unsigned long flags;
  834. int i;
  835. spin_lock_irqsave(&cpu_base->lock, flags);
  836. if (!hrtimer_hres_active()) {
  837. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
  838. struct hrtimer *timer;
  839. if (!base->first)
  840. continue;
  841. timer = rb_entry(base->first, struct hrtimer, node);
  842. delta.tv64 = timer->expires.tv64;
  843. delta = ktime_sub(delta, base->get_time());
  844. if (delta.tv64 < mindelta.tv64)
  845. mindelta.tv64 = delta.tv64;
  846. }
  847. }
  848. spin_unlock_irqrestore(&cpu_base->lock, flags);
  849. if (mindelta.tv64 < 0)
  850. mindelta.tv64 = 0;
  851. return mindelta;
  852. }
  853. #endif
  854. /**
  855. * hrtimer_init - initialize a timer to the given clock
  856. * @timer: the timer to be initialized
  857. * @clock_id: the clock to be used
  858. * @mode: timer mode abs/rel
  859. */
  860. void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  861. enum hrtimer_mode mode)
  862. {
  863. struct hrtimer_cpu_base *cpu_base;
  864. memset(timer, 0, sizeof(struct hrtimer));
  865. cpu_base = &__raw_get_cpu_var(hrtimer_bases);
  866. if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
  867. clock_id = CLOCK_MONOTONIC;
  868. timer->base = &cpu_base->clock_base[clock_id];
  869. INIT_LIST_HEAD(&timer->cb_entry);
  870. hrtimer_init_timer_hres(timer);
  871. #ifdef CONFIG_TIMER_STATS
  872. timer->start_site = NULL;
  873. timer->start_pid = -1;
  874. memset(timer->start_comm, 0, TASK_COMM_LEN);
  875. #endif
  876. }
  877. EXPORT_SYMBOL_GPL(hrtimer_init);
  878. /**
  879. * hrtimer_get_res - get the timer resolution for a clock
  880. * @which_clock: which clock to query
  881. * @tp: pointer to timespec variable to store the resolution
  882. *
  883. * Store the resolution of the clock selected by @which_clock in the
  884. * variable pointed to by @tp.
  885. */
  886. int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
  887. {
  888. struct hrtimer_cpu_base *cpu_base;
  889. cpu_base = &__raw_get_cpu_var(hrtimer_bases);
  890. *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
  891. return 0;
  892. }
  893. EXPORT_SYMBOL_GPL(hrtimer_get_res);
  894. static void run_hrtimer_pending(struct hrtimer_cpu_base *cpu_base)
  895. {
  896. spin_lock_irq(&cpu_base->lock);
  897. while (!list_empty(&cpu_base->cb_pending)) {
  898. enum hrtimer_restart (*fn)(struct hrtimer *);
  899. struct hrtimer *timer;
  900. int restart;
  901. timer = list_entry(cpu_base->cb_pending.next,
  902. struct hrtimer, cb_entry);
  903. timer_stats_account_hrtimer(timer);
  904. fn = timer->function;
  905. __remove_hrtimer(timer, timer->base, HRTIMER_STATE_CALLBACK, 0);
  906. spin_unlock_irq(&cpu_base->lock);
  907. restart = fn(timer);
  908. spin_lock_irq(&cpu_base->lock);
  909. timer->state &= ~HRTIMER_STATE_CALLBACK;
  910. if (restart == HRTIMER_RESTART) {
  911. BUG_ON(hrtimer_active(timer));
  912. /*
  913. * Enqueue the timer, allow reprogramming of the event
  914. * device
  915. */
  916. enqueue_hrtimer(timer, timer->base, 1);
  917. } else if (hrtimer_active(timer)) {
  918. /*
  919. * If the timer was rearmed on another CPU, reprogram
  920. * the event device.
  921. */
  922. if (timer->base->first == &timer->node)
  923. hrtimer_reprogram(timer, timer->base);
  924. }
  925. }
  926. spin_unlock_irq(&cpu_base->lock);
  927. }
  928. static void __run_hrtimer(struct hrtimer *timer)
  929. {
  930. struct hrtimer_clock_base *base = timer->base;
  931. struct hrtimer_cpu_base *cpu_base = base->cpu_base;
  932. enum hrtimer_restart (*fn)(struct hrtimer *);
  933. int restart;
  934. __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
  935. timer_stats_account_hrtimer(timer);
  936. fn = timer->function;
  937. if (timer->cb_mode == HRTIMER_CB_IRQSAFE_NO_SOFTIRQ) {
  938. /*
  939. * Used for scheduler timers, avoid lock inversion with
  940. * rq->lock and tasklist_lock.
  941. *
  942. * These timers are required to deal with enqueue expiry
  943. * themselves and are not allowed to migrate.
  944. */
  945. spin_unlock(&cpu_base->lock);
  946. restart = fn(timer);
  947. spin_lock(&cpu_base->lock);
  948. } else
  949. restart = fn(timer);
  950. /*
  951. * Note: We clear the CALLBACK bit after enqueue_hrtimer to avoid
  952. * reprogramming of the event hardware. This happens at the end of this
  953. * function anyway.
  954. */
  955. if (restart != HRTIMER_NORESTART) {
  956. BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
  957. enqueue_hrtimer(timer, base, 0);
  958. }
  959. timer->state &= ~HRTIMER_STATE_CALLBACK;
  960. }
  961. #ifdef CONFIG_HIGH_RES_TIMERS
  962. /*
  963. * High resolution timer interrupt
  964. * Called with interrupts disabled
  965. */
  966. void hrtimer_interrupt(struct clock_event_device *dev)
  967. {
  968. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  969. struct hrtimer_clock_base *base;
  970. ktime_t expires_next, now;
  971. int i, raise = 0;
  972. BUG_ON(!cpu_base->hres_active);
  973. cpu_base->nr_events++;
  974. dev->next_event.tv64 = KTIME_MAX;
  975. retry:
  976. now = ktime_get();
  977. expires_next.tv64 = KTIME_MAX;
  978. base = cpu_base->clock_base;
  979. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  980. ktime_t basenow;
  981. struct rb_node *node;
  982. spin_lock(&cpu_base->lock);
  983. basenow = ktime_add(now, base->offset);
  984. while ((node = base->first)) {
  985. struct hrtimer *timer;
  986. timer = rb_entry(node, struct hrtimer, node);
  987. if (basenow.tv64 < timer->expires.tv64) {
  988. ktime_t expires;
  989. expires = ktime_sub(timer->expires,
  990. base->offset);
  991. if (expires.tv64 < expires_next.tv64)
  992. expires_next = expires;
  993. break;
  994. }
  995. /* Move softirq callbacks to the pending list */
  996. if (timer->cb_mode == HRTIMER_CB_SOFTIRQ) {
  997. __remove_hrtimer(timer, base,
  998. HRTIMER_STATE_PENDING, 0);
  999. list_add_tail(&timer->cb_entry,
  1000. &base->cpu_base->cb_pending);
  1001. raise = 1;
  1002. continue;
  1003. }
  1004. __run_hrtimer(timer);
  1005. }
  1006. spin_unlock(&cpu_base->lock);
  1007. base++;
  1008. }
  1009. cpu_base->expires_next = expires_next;
  1010. /* Reprogramming necessary ? */
  1011. if (expires_next.tv64 != KTIME_MAX) {
  1012. if (tick_program_event(expires_next, 0))
  1013. goto retry;
  1014. }
  1015. /* Raise softirq ? */
  1016. if (raise)
  1017. raise_softirq(HRTIMER_SOFTIRQ);
  1018. }
  1019. static void run_hrtimer_softirq(struct softirq_action *h)
  1020. {
  1021. run_hrtimer_pending(&__get_cpu_var(hrtimer_bases));
  1022. }
  1023. #endif /* CONFIG_HIGH_RES_TIMERS */
  1024. /*
  1025. * Called from timer softirq every jiffy, expire hrtimers:
  1026. *
  1027. * For HRT its the fall back code to run the softirq in the timer
  1028. * softirq context in case the hrtimer initialization failed or has
  1029. * not been done yet.
  1030. */
  1031. void hrtimer_run_pending(void)
  1032. {
  1033. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  1034. if (hrtimer_hres_active())
  1035. return;
  1036. /*
  1037. * This _is_ ugly: We have to check in the softirq context,
  1038. * whether we can switch to highres and / or nohz mode. The
  1039. * clocksource switch happens in the timer interrupt with
  1040. * xtime_lock held. Notification from there only sets the
  1041. * check bit in the tick_oneshot code, otherwise we might
  1042. * deadlock vs. xtime_lock.
  1043. */
  1044. if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
  1045. hrtimer_switch_to_hres();
  1046. run_hrtimer_pending(cpu_base);
  1047. }
  1048. /*
  1049. * Called from hardirq context every jiffy
  1050. */
  1051. static inline void run_hrtimer_queue(struct hrtimer_cpu_base *cpu_base,
  1052. int index)
  1053. {
  1054. struct rb_node *node;
  1055. struct hrtimer_clock_base *base = &cpu_base->clock_base[index];
  1056. if (!base->first)
  1057. return;
  1058. if (base->get_softirq_time)
  1059. base->softirq_time = base->get_softirq_time();
  1060. spin_lock(&cpu_base->lock);
  1061. while ((node = base->first)) {
  1062. struct hrtimer *timer;
  1063. timer = rb_entry(node, struct hrtimer, node);
  1064. if (base->softirq_time.tv64 <= timer->expires.tv64)
  1065. break;
  1066. if (timer->cb_mode == HRTIMER_CB_SOFTIRQ) {
  1067. __remove_hrtimer(timer, base, HRTIMER_STATE_PENDING, 0);
  1068. list_add_tail(&timer->cb_entry,
  1069. &base->cpu_base->cb_pending);
  1070. continue;
  1071. }
  1072. __run_hrtimer(timer);
  1073. }
  1074. spin_unlock(&cpu_base->lock);
  1075. }
  1076. void hrtimer_run_queues(void)
  1077. {
  1078. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  1079. int i;
  1080. if (hrtimer_hres_active())
  1081. return;
  1082. hrtimer_get_softirq_time(cpu_base);
  1083. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
  1084. run_hrtimer_queue(cpu_base, i);
  1085. }
  1086. /*
  1087. * Sleep related functions:
  1088. */
  1089. static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
  1090. {
  1091. struct hrtimer_sleeper *t =
  1092. container_of(timer, struct hrtimer_sleeper, timer);
  1093. struct task_struct *task = t->task;
  1094. t->task = NULL;
  1095. if (task)
  1096. wake_up_process(task);
  1097. return HRTIMER_NORESTART;
  1098. }
  1099. void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
  1100. {
  1101. sl->timer.function = hrtimer_wakeup;
  1102. sl->task = task;
  1103. #ifdef CONFIG_HIGH_RES_TIMERS
  1104. sl->timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
  1105. #endif
  1106. }
  1107. static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
  1108. {
  1109. hrtimer_init_sleeper(t, current);
  1110. do {
  1111. set_current_state(TASK_INTERRUPTIBLE);
  1112. hrtimer_start(&t->timer, t->timer.expires, mode);
  1113. if (!hrtimer_active(&t->timer))
  1114. t->task = NULL;
  1115. if (likely(t->task))
  1116. schedule();
  1117. hrtimer_cancel(&t->timer);
  1118. mode = HRTIMER_MODE_ABS;
  1119. } while (t->task && !signal_pending(current));
  1120. __set_current_state(TASK_RUNNING);
  1121. return t->task == NULL;
  1122. }
  1123. long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
  1124. {
  1125. struct hrtimer_sleeper t;
  1126. struct timespec *rmtp;
  1127. ktime_t time;
  1128. restart->fn = do_no_restart_syscall;
  1129. hrtimer_init(&t.timer, restart->arg0, HRTIMER_MODE_ABS);
  1130. t.timer.expires.tv64 = ((u64)restart->arg3 << 32) | (u64) restart->arg2;
  1131. if (do_nanosleep(&t, HRTIMER_MODE_ABS))
  1132. return 0;
  1133. rmtp = (struct timespec *)restart->arg1;
  1134. if (rmtp) {
  1135. time = ktime_sub(t.timer.expires, t.timer.base->get_time());
  1136. if (time.tv64 <= 0)
  1137. return 0;
  1138. *rmtp = ktime_to_timespec(time);
  1139. }
  1140. restart->fn = hrtimer_nanosleep_restart;
  1141. /* The other values in restart are already filled in */
  1142. return -ERESTART_RESTARTBLOCK;
  1143. }
  1144. long hrtimer_nanosleep(struct timespec *rqtp, struct timespec *rmtp,
  1145. const enum hrtimer_mode mode, const clockid_t clockid)
  1146. {
  1147. struct restart_block *restart;
  1148. struct hrtimer_sleeper t;
  1149. ktime_t rem;
  1150. hrtimer_init(&t.timer, clockid, mode);
  1151. t.timer.expires = timespec_to_ktime(*rqtp);
  1152. if (do_nanosleep(&t, mode))
  1153. return 0;
  1154. /* Absolute timers do not update the rmtp value and restart: */
  1155. if (mode == HRTIMER_MODE_ABS)
  1156. return -ERESTARTNOHAND;
  1157. if (rmtp) {
  1158. rem = ktime_sub(t.timer.expires, t.timer.base->get_time());
  1159. if (rem.tv64 <= 0)
  1160. return 0;
  1161. *rmtp = ktime_to_timespec(rem);
  1162. }
  1163. restart = &current_thread_info()->restart_block;
  1164. restart->fn = hrtimer_nanosleep_restart;
  1165. restart->arg0 = (unsigned long) t.timer.base->index;
  1166. restart->arg1 = (unsigned long) rmtp;
  1167. restart->arg2 = t.timer.expires.tv64 & 0xFFFFFFFF;
  1168. restart->arg3 = t.timer.expires.tv64 >> 32;
  1169. return -ERESTART_RESTARTBLOCK;
  1170. }
  1171. asmlinkage long
  1172. sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp)
  1173. {
  1174. struct timespec tu, rmt;
  1175. int ret;
  1176. if (copy_from_user(&tu, rqtp, sizeof(tu)))
  1177. return -EFAULT;
  1178. if (!timespec_valid(&tu))
  1179. return -EINVAL;
  1180. ret = hrtimer_nanosleep(&tu, rmtp ? &rmt : NULL, HRTIMER_MODE_REL,
  1181. CLOCK_MONOTONIC);
  1182. if (ret && rmtp) {
  1183. if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
  1184. return -EFAULT;
  1185. }
  1186. return ret;
  1187. }
  1188. /*
  1189. * Functions related to boot-time initialization:
  1190. */
  1191. static void __cpuinit init_hrtimers_cpu(int cpu)
  1192. {
  1193. struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
  1194. int i;
  1195. spin_lock_init(&cpu_base->lock);
  1196. lockdep_set_class(&cpu_base->lock, &cpu_base->lock_key);
  1197. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
  1198. cpu_base->clock_base[i].cpu_base = cpu_base;
  1199. INIT_LIST_HEAD(&cpu_base->cb_pending);
  1200. hrtimer_init_hres(cpu_base);
  1201. }
  1202. #ifdef CONFIG_HOTPLUG_CPU
  1203. static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
  1204. struct hrtimer_clock_base *new_base)
  1205. {
  1206. struct hrtimer *timer;
  1207. struct rb_node *node;
  1208. while ((node = rb_first(&old_base->active))) {
  1209. timer = rb_entry(node, struct hrtimer, node);
  1210. BUG_ON(hrtimer_callback_running(timer));
  1211. __remove_hrtimer(timer, old_base, HRTIMER_STATE_INACTIVE, 0);
  1212. timer->base = new_base;
  1213. /*
  1214. * Enqueue the timer. Allow reprogramming of the event device
  1215. */
  1216. enqueue_hrtimer(timer, new_base, 1);
  1217. }
  1218. }
  1219. static void migrate_hrtimers(int cpu)
  1220. {
  1221. struct hrtimer_cpu_base *old_base, *new_base;
  1222. int i;
  1223. BUG_ON(cpu_online(cpu));
  1224. old_base = &per_cpu(hrtimer_bases, cpu);
  1225. new_base = &get_cpu_var(hrtimer_bases);
  1226. tick_cancel_sched_timer(cpu);
  1227. local_irq_disable();
  1228. double_spin_lock(&new_base->lock, &old_base->lock,
  1229. smp_processor_id() < cpu);
  1230. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  1231. migrate_hrtimer_list(&old_base->clock_base[i],
  1232. &new_base->clock_base[i]);
  1233. }
  1234. double_spin_unlock(&new_base->lock, &old_base->lock,
  1235. smp_processor_id() < cpu);
  1236. local_irq_enable();
  1237. put_cpu_var(hrtimer_bases);
  1238. }
  1239. #endif /* CONFIG_HOTPLUG_CPU */
  1240. static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
  1241. unsigned long action, void *hcpu)
  1242. {
  1243. unsigned int cpu = (long)hcpu;
  1244. switch (action) {
  1245. case CPU_UP_PREPARE:
  1246. case CPU_UP_PREPARE_FROZEN:
  1247. init_hrtimers_cpu(cpu);
  1248. break;
  1249. #ifdef CONFIG_HOTPLUG_CPU
  1250. case CPU_DEAD:
  1251. case CPU_DEAD_FROZEN:
  1252. clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &cpu);
  1253. migrate_hrtimers(cpu);
  1254. break;
  1255. #endif
  1256. default:
  1257. break;
  1258. }
  1259. return NOTIFY_OK;
  1260. }
  1261. static struct notifier_block __cpuinitdata hrtimers_nb = {
  1262. .notifier_call = hrtimer_cpu_notify,
  1263. };
  1264. void __init hrtimers_init(void)
  1265. {
  1266. hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
  1267. (void *)(long)smp_processor_id());
  1268. register_cpu_notifier(&hrtimers_nb);
  1269. #ifdef CONFIG_HIGH_RES_TIMERS
  1270. open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq, NULL);
  1271. #endif
  1272. }