hrtimer.c 37 KB

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