hrtimer.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742
  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 <linux/debugobjects.h>
  46. #include <asm/uaccess.h>
  47. /**
  48. * ktime_get - get the monotonic time in ktime_t format
  49. *
  50. * returns the time in ktime_t format
  51. */
  52. ktime_t ktime_get(void)
  53. {
  54. struct timespec now;
  55. ktime_get_ts(&now);
  56. return timespec_to_ktime(now);
  57. }
  58. EXPORT_SYMBOL_GPL(ktime_get);
  59. /**
  60. * ktime_get_real - get the real (wall-) time in ktime_t format
  61. *
  62. * returns the time in ktime_t format
  63. */
  64. ktime_t ktime_get_real(void)
  65. {
  66. struct timespec now;
  67. getnstimeofday(&now);
  68. return timespec_to_ktime(now);
  69. }
  70. EXPORT_SYMBOL_GPL(ktime_get_real);
  71. /*
  72. * The timer bases:
  73. *
  74. * Note: If we want to add new timer bases, we have to skip the two
  75. * clock ids captured by the cpu-timers. We do this by holding empty
  76. * entries rather than doing math adjustment of the clock ids.
  77. * This ensures that we capture erroneous accesses to these clock ids
  78. * rather than moving them into the range of valid clock id's.
  79. */
  80. DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
  81. {
  82. .clock_base =
  83. {
  84. {
  85. .index = CLOCK_REALTIME,
  86. .get_time = &ktime_get_real,
  87. .resolution = KTIME_LOW_RES,
  88. },
  89. {
  90. .index = CLOCK_MONOTONIC,
  91. .get_time = &ktime_get,
  92. .resolution = KTIME_LOW_RES,
  93. },
  94. }
  95. };
  96. /**
  97. * ktime_get_ts - get the monotonic clock in timespec format
  98. * @ts: pointer to timespec variable
  99. *
  100. * The function calculates the monotonic clock from the realtime
  101. * clock and the wall_to_monotonic offset and stores the result
  102. * in normalized timespec format in the variable pointed to by @ts.
  103. */
  104. void ktime_get_ts(struct timespec *ts)
  105. {
  106. struct timespec tomono;
  107. unsigned long seq;
  108. do {
  109. seq = read_seqbegin(&xtime_lock);
  110. getnstimeofday(ts);
  111. tomono = wall_to_monotonic;
  112. } while (read_seqretry(&xtime_lock, seq));
  113. set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
  114. ts->tv_nsec + tomono.tv_nsec);
  115. }
  116. EXPORT_SYMBOL_GPL(ktime_get_ts);
  117. /*
  118. * Get the coarse grained time at the softirq based on xtime and
  119. * wall_to_monotonic.
  120. */
  121. static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
  122. {
  123. ktime_t xtim, tomono;
  124. struct timespec xts, tom;
  125. unsigned long seq;
  126. do {
  127. seq = read_seqbegin(&xtime_lock);
  128. xts = current_kernel_time();
  129. tom = wall_to_monotonic;
  130. } while (read_seqretry(&xtime_lock, seq));
  131. xtim = timespec_to_ktime(xts);
  132. tomono = timespec_to_ktime(tom);
  133. base->clock_base[CLOCK_REALTIME].softirq_time = xtim;
  134. base->clock_base[CLOCK_MONOTONIC].softirq_time =
  135. ktime_add(xtim, tomono);
  136. }
  137. /*
  138. * Functions and macros which are different for UP/SMP systems are kept in a
  139. * single place
  140. */
  141. #ifdef CONFIG_SMP
  142. /*
  143. * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
  144. * means that all timers which are tied to this base via timer->base are
  145. * locked, and the base itself is locked too.
  146. *
  147. * So __run_timers/migrate_timers can safely modify all timers which could
  148. * be found on the lists/queues.
  149. *
  150. * When the timer's base is locked, and the timer removed from list, it is
  151. * possible to set timer->base = NULL and drop the lock: the timer remains
  152. * locked.
  153. */
  154. static
  155. struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
  156. unsigned long *flags)
  157. {
  158. struct hrtimer_clock_base *base;
  159. for (;;) {
  160. base = timer->base;
  161. if (likely(base != NULL)) {
  162. spin_lock_irqsave(&base->cpu_base->lock, *flags);
  163. if (likely(base == timer->base))
  164. return base;
  165. /* The timer has migrated to another CPU: */
  166. spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
  167. }
  168. cpu_relax();
  169. }
  170. }
  171. /*
  172. * Switch the timer base to the current CPU when possible.
  173. */
  174. static inline struct hrtimer_clock_base *
  175. switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base)
  176. {
  177. struct hrtimer_clock_base *new_base;
  178. struct hrtimer_cpu_base *new_cpu_base;
  179. new_cpu_base = &__get_cpu_var(hrtimer_bases);
  180. new_base = &new_cpu_base->clock_base[base->index];
  181. if (base != new_base) {
  182. /*
  183. * We are trying to schedule the timer on the local CPU.
  184. * However we can't change timer's base while it is running,
  185. * so we keep it on the same CPU. No hassle vs. reprogramming
  186. * the event source in the high resolution case. The softirq
  187. * code will take care of this when the timer function has
  188. * completed. There is no conflict as we hold the lock until
  189. * the timer is enqueued.
  190. */
  191. if (unlikely(hrtimer_callback_running(timer)))
  192. return base;
  193. /* See the comment in lock_timer_base() */
  194. timer->base = NULL;
  195. spin_unlock(&base->cpu_base->lock);
  196. spin_lock(&new_base->cpu_base->lock);
  197. timer->base = new_base;
  198. }
  199. return new_base;
  200. }
  201. #else /* CONFIG_SMP */
  202. static inline struct hrtimer_clock_base *
  203. lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
  204. {
  205. struct hrtimer_clock_base *base = timer->base;
  206. spin_lock_irqsave(&base->cpu_base->lock, *flags);
  207. return base;
  208. }
  209. # define switch_hrtimer_base(t, b) (b)
  210. #endif /* !CONFIG_SMP */
  211. /*
  212. * Functions for the union type storage format of ktime_t which are
  213. * too large for inlining:
  214. */
  215. #if BITS_PER_LONG < 64
  216. # ifndef CONFIG_KTIME_SCALAR
  217. /**
  218. * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
  219. * @kt: addend
  220. * @nsec: the scalar nsec value to add
  221. *
  222. * Returns the sum of kt and nsec in ktime_t format
  223. */
  224. ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
  225. {
  226. ktime_t tmp;
  227. if (likely(nsec < NSEC_PER_SEC)) {
  228. tmp.tv64 = nsec;
  229. } else {
  230. unsigned long rem = do_div(nsec, NSEC_PER_SEC);
  231. tmp = ktime_set((long)nsec, rem);
  232. }
  233. return ktime_add(kt, tmp);
  234. }
  235. EXPORT_SYMBOL_GPL(ktime_add_ns);
  236. /**
  237. * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
  238. * @kt: minuend
  239. * @nsec: the scalar nsec value to subtract
  240. *
  241. * Returns the subtraction of @nsec from @kt in ktime_t format
  242. */
  243. ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec)
  244. {
  245. ktime_t tmp;
  246. if (likely(nsec < NSEC_PER_SEC)) {
  247. tmp.tv64 = nsec;
  248. } else {
  249. unsigned long rem = do_div(nsec, NSEC_PER_SEC);
  250. tmp = ktime_set((long)nsec, rem);
  251. }
  252. return ktime_sub(kt, tmp);
  253. }
  254. EXPORT_SYMBOL_GPL(ktime_sub_ns);
  255. # endif /* !CONFIG_KTIME_SCALAR */
  256. /*
  257. * Divide a ktime value by a nanosecond value
  258. */
  259. u64 ktime_divns(const ktime_t kt, s64 div)
  260. {
  261. u64 dclc;
  262. int sft = 0;
  263. dclc = ktime_to_ns(kt);
  264. /* Make sure the divisor is less than 2^32: */
  265. while (div >> 32) {
  266. sft++;
  267. div >>= 1;
  268. }
  269. dclc >>= sft;
  270. do_div(dclc, (unsigned long) div);
  271. return dclc;
  272. }
  273. #endif /* BITS_PER_LONG >= 64 */
  274. /*
  275. * Add two ktime values and do a safety check for overflow:
  276. */
  277. ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
  278. {
  279. ktime_t res = ktime_add(lhs, rhs);
  280. /*
  281. * We use KTIME_SEC_MAX here, the maximum timeout which we can
  282. * return to user space in a timespec:
  283. */
  284. if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
  285. res = ktime_set(KTIME_SEC_MAX, 0);
  286. return res;
  287. }
  288. #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
  289. static struct debug_obj_descr hrtimer_debug_descr;
  290. /*
  291. * fixup_init is called when:
  292. * - an active object is initialized
  293. */
  294. static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
  295. {
  296. struct hrtimer *timer = addr;
  297. switch (state) {
  298. case ODEBUG_STATE_ACTIVE:
  299. hrtimer_cancel(timer);
  300. debug_object_init(timer, &hrtimer_debug_descr);
  301. return 1;
  302. default:
  303. return 0;
  304. }
  305. }
  306. /*
  307. * fixup_activate is called when:
  308. * - an active object is activated
  309. * - an unknown object is activated (might be a statically initialized object)
  310. */
  311. static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
  312. {
  313. switch (state) {
  314. case ODEBUG_STATE_NOTAVAILABLE:
  315. WARN_ON_ONCE(1);
  316. return 0;
  317. case ODEBUG_STATE_ACTIVE:
  318. WARN_ON(1);
  319. default:
  320. return 0;
  321. }
  322. }
  323. /*
  324. * fixup_free is called when:
  325. * - an active object is freed
  326. */
  327. static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
  328. {
  329. struct hrtimer *timer = addr;
  330. switch (state) {
  331. case ODEBUG_STATE_ACTIVE:
  332. hrtimer_cancel(timer);
  333. debug_object_free(timer, &hrtimer_debug_descr);
  334. return 1;
  335. default:
  336. return 0;
  337. }
  338. }
  339. static struct debug_obj_descr hrtimer_debug_descr = {
  340. .name = "hrtimer",
  341. .fixup_init = hrtimer_fixup_init,
  342. .fixup_activate = hrtimer_fixup_activate,
  343. .fixup_free = hrtimer_fixup_free,
  344. };
  345. static inline void debug_hrtimer_init(struct hrtimer *timer)
  346. {
  347. debug_object_init(timer, &hrtimer_debug_descr);
  348. }
  349. static inline void debug_hrtimer_activate(struct hrtimer *timer)
  350. {
  351. debug_object_activate(timer, &hrtimer_debug_descr);
  352. }
  353. static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
  354. {
  355. debug_object_deactivate(timer, &hrtimer_debug_descr);
  356. }
  357. static inline void debug_hrtimer_free(struct hrtimer *timer)
  358. {
  359. debug_object_free(timer, &hrtimer_debug_descr);
  360. }
  361. static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  362. enum hrtimer_mode mode);
  363. void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
  364. enum hrtimer_mode mode)
  365. {
  366. debug_object_init_on_stack(timer, &hrtimer_debug_descr);
  367. __hrtimer_init(timer, clock_id, mode);
  368. }
  369. void destroy_hrtimer_on_stack(struct hrtimer *timer)
  370. {
  371. debug_object_free(timer, &hrtimer_debug_descr);
  372. }
  373. #else
  374. static inline void debug_hrtimer_init(struct hrtimer *timer) { }
  375. static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
  376. static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
  377. #endif
  378. /*
  379. * Check, whether the timer is on the callback pending list
  380. */
  381. static inline int hrtimer_cb_pending(const struct hrtimer *timer)
  382. {
  383. return timer->state & HRTIMER_STATE_PENDING;
  384. }
  385. /*
  386. * Remove a timer from the callback pending list
  387. */
  388. static inline void hrtimer_remove_cb_pending(struct hrtimer *timer)
  389. {
  390. list_del_init(&timer->cb_entry);
  391. }
  392. /* High resolution timer related functions */
  393. #ifdef CONFIG_HIGH_RES_TIMERS
  394. /*
  395. * High resolution timer enabled ?
  396. */
  397. static int hrtimer_hres_enabled __read_mostly = 1;
  398. /*
  399. * Enable / Disable high resolution mode
  400. */
  401. static int __init setup_hrtimer_hres(char *str)
  402. {
  403. if (!strcmp(str, "off"))
  404. hrtimer_hres_enabled = 0;
  405. else if (!strcmp(str, "on"))
  406. hrtimer_hres_enabled = 1;
  407. else
  408. return 0;
  409. return 1;
  410. }
  411. __setup("highres=", setup_hrtimer_hres);
  412. /*
  413. * hrtimer_high_res_enabled - query, if the highres mode is enabled
  414. */
  415. static inline int hrtimer_is_hres_enabled(void)
  416. {
  417. return hrtimer_hres_enabled;
  418. }
  419. /*
  420. * Is the high resolution mode active ?
  421. */
  422. static inline int hrtimer_hres_active(void)
  423. {
  424. return __get_cpu_var(hrtimer_bases).hres_active;
  425. }
  426. /*
  427. * Reprogram the event source with checking both queues for the
  428. * next event
  429. * Called with interrupts disabled and base->lock held
  430. */
  431. static void hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base)
  432. {
  433. int i;
  434. struct hrtimer_clock_base *base = cpu_base->clock_base;
  435. ktime_t expires;
  436. cpu_base->expires_next.tv64 = KTIME_MAX;
  437. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
  438. struct hrtimer *timer;
  439. if (!base->first)
  440. continue;
  441. timer = rb_entry(base->first, struct hrtimer, node);
  442. expires = ktime_sub(timer->expires, base->offset);
  443. if (expires.tv64 < cpu_base->expires_next.tv64)
  444. cpu_base->expires_next = expires;
  445. }
  446. if (cpu_base->expires_next.tv64 != KTIME_MAX)
  447. tick_program_event(cpu_base->expires_next, 1);
  448. }
  449. /*
  450. * Shared reprogramming for clock_realtime and clock_monotonic
  451. *
  452. * When a timer is enqueued and expires earlier than the already enqueued
  453. * timers, we have to check, whether it expires earlier than the timer for
  454. * which the clock event device was armed.
  455. *
  456. * Called with interrupts disabled and base->cpu_base.lock held
  457. */
  458. static int hrtimer_reprogram(struct hrtimer *timer,
  459. struct hrtimer_clock_base *base)
  460. {
  461. ktime_t *expires_next = &__get_cpu_var(hrtimer_bases).expires_next;
  462. ktime_t expires = ktime_sub(timer->expires, base->offset);
  463. int res;
  464. WARN_ON_ONCE(timer->expires.tv64 < 0);
  465. /*
  466. * When the callback is running, we do not reprogram the clock event
  467. * device. The timer callback is either running on a different CPU or
  468. * the callback is executed in the hrtimer_interrupt context. The
  469. * reprogramming is handled either by the softirq, which called the
  470. * callback or at the end of the hrtimer_interrupt.
  471. */
  472. if (hrtimer_callback_running(timer))
  473. return 0;
  474. /*
  475. * CLOCK_REALTIME timer might be requested with an absolute
  476. * expiry time which is less than base->offset. Nothing wrong
  477. * about that, just avoid to call into the tick code, which
  478. * has now objections against negative expiry values.
  479. */
  480. if (expires.tv64 < 0)
  481. return -ETIME;
  482. if (expires.tv64 >= expires_next->tv64)
  483. return 0;
  484. /*
  485. * Clockevents returns -ETIME, when the event was in the past.
  486. */
  487. res = tick_program_event(expires, 0);
  488. if (!IS_ERR_VALUE(res))
  489. *expires_next = expires;
  490. return res;
  491. }
  492. /*
  493. * Retrigger next event is called after clock was set
  494. *
  495. * Called with interrupts disabled via on_each_cpu()
  496. */
  497. static void retrigger_next_event(void *arg)
  498. {
  499. struct hrtimer_cpu_base *base;
  500. struct timespec realtime_offset;
  501. unsigned long seq;
  502. if (!hrtimer_hres_active())
  503. return;
  504. do {
  505. seq = read_seqbegin(&xtime_lock);
  506. set_normalized_timespec(&realtime_offset,
  507. -wall_to_monotonic.tv_sec,
  508. -wall_to_monotonic.tv_nsec);
  509. } while (read_seqretry(&xtime_lock, seq));
  510. base = &__get_cpu_var(hrtimer_bases);
  511. /* Adjust CLOCK_REALTIME offset */
  512. spin_lock(&base->lock);
  513. base->clock_base[CLOCK_REALTIME].offset =
  514. timespec_to_ktime(realtime_offset);
  515. hrtimer_force_reprogram(base);
  516. spin_unlock(&base->lock);
  517. }
  518. /*
  519. * Clock realtime was set
  520. *
  521. * Change the offset of the realtime clock vs. the monotonic
  522. * clock.
  523. *
  524. * We might have to reprogram the high resolution timer interrupt. On
  525. * SMP we call the architecture specific code to retrigger _all_ high
  526. * resolution timer interrupts. On UP we just disable interrupts and
  527. * call the high resolution interrupt code.
  528. */
  529. void clock_was_set(void)
  530. {
  531. /* Retrigger the CPU local events everywhere */
  532. on_each_cpu(retrigger_next_event, NULL, 1);
  533. }
  534. /*
  535. * During resume we might have to reprogram the high resolution timer
  536. * interrupt (on the local CPU):
  537. */
  538. void hres_timers_resume(void)
  539. {
  540. /* Retrigger the CPU local events: */
  541. retrigger_next_event(NULL);
  542. }
  543. /*
  544. * Initialize the high resolution related parts of cpu_base
  545. */
  546. static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
  547. {
  548. base->expires_next.tv64 = KTIME_MAX;
  549. base->hres_active = 0;
  550. }
  551. /*
  552. * Initialize the high resolution related parts of a hrtimer
  553. */
  554. static inline void hrtimer_init_timer_hres(struct hrtimer *timer)
  555. {
  556. }
  557. /*
  558. * When High resolution timers are active, try to reprogram. Note, that in case
  559. * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
  560. * check happens. The timer gets enqueued into the rbtree. The reprogramming
  561. * and expiry check is done in the hrtimer_interrupt or in the softirq.
  562. */
  563. static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
  564. struct hrtimer_clock_base *base)
  565. {
  566. if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
  567. /* Timer is expired, act upon the callback mode */
  568. switch(timer->cb_mode) {
  569. case HRTIMER_CB_IRQSAFE_NO_RESTART:
  570. debug_hrtimer_deactivate(timer);
  571. /*
  572. * We can call the callback from here. No restart
  573. * happens, so no danger of recursion
  574. */
  575. BUG_ON(timer->function(timer) != HRTIMER_NORESTART);
  576. return 1;
  577. case HRTIMER_CB_IRQSAFE_NO_SOFTIRQ:
  578. /*
  579. * This is solely for the sched tick emulation with
  580. * dynamic tick support to ensure that we do not
  581. * restart the tick right on the edge and end up with
  582. * the tick timer in the softirq ! The calling site
  583. * takes care of this.
  584. */
  585. debug_hrtimer_deactivate(timer);
  586. return 1;
  587. case HRTIMER_CB_IRQSAFE:
  588. case HRTIMER_CB_SOFTIRQ:
  589. /*
  590. * Move everything else into the softirq pending list !
  591. */
  592. list_add_tail(&timer->cb_entry,
  593. &base->cpu_base->cb_pending);
  594. timer->state = HRTIMER_STATE_PENDING;
  595. return 1;
  596. default:
  597. BUG();
  598. }
  599. }
  600. return 0;
  601. }
  602. /*
  603. * Switch to high resolution mode
  604. */
  605. static int hrtimer_switch_to_hres(void)
  606. {
  607. int cpu = smp_processor_id();
  608. struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
  609. unsigned long flags;
  610. if (base->hres_active)
  611. return 1;
  612. local_irq_save(flags);
  613. if (tick_init_highres()) {
  614. local_irq_restore(flags);
  615. printk(KERN_WARNING "Could not switch to high resolution "
  616. "mode on CPU %d\n", cpu);
  617. return 0;
  618. }
  619. base->hres_active = 1;
  620. base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES;
  621. base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES;
  622. tick_setup_sched_timer();
  623. /* "Retrigger" the interrupt to get things going */
  624. retrigger_next_event(NULL);
  625. local_irq_restore(flags);
  626. printk(KERN_DEBUG "Switched to high resolution mode on CPU %d\n",
  627. smp_processor_id());
  628. return 1;
  629. }
  630. static inline void hrtimer_raise_softirq(void)
  631. {
  632. raise_softirq(HRTIMER_SOFTIRQ);
  633. }
  634. #else
  635. static inline int hrtimer_hres_active(void) { return 0; }
  636. static inline int hrtimer_is_hres_enabled(void) { return 0; }
  637. static inline int hrtimer_switch_to_hres(void) { return 0; }
  638. static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base *base) { }
  639. static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
  640. struct hrtimer_clock_base *base)
  641. {
  642. return 0;
  643. }
  644. static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
  645. static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { }
  646. static inline int hrtimer_reprogram(struct hrtimer *timer,
  647. struct hrtimer_clock_base *base)
  648. {
  649. return 0;
  650. }
  651. static inline void hrtimer_raise_softirq(void) { }
  652. #endif /* CONFIG_HIGH_RES_TIMERS */
  653. #ifdef CONFIG_TIMER_STATS
  654. void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer, void *addr)
  655. {
  656. if (timer->start_site)
  657. return;
  658. timer->start_site = addr;
  659. memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
  660. timer->start_pid = current->pid;
  661. }
  662. #endif
  663. /*
  664. * Counterpart to lock_hrtimer_base above:
  665. */
  666. static inline
  667. void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
  668. {
  669. spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
  670. }
  671. /**
  672. * hrtimer_forward - forward the timer expiry
  673. * @timer: hrtimer to forward
  674. * @now: forward past this time
  675. * @interval: the interval to forward
  676. *
  677. * Forward the timer expiry so it will expire in the future.
  678. * Returns the number of overruns.
  679. */
  680. u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
  681. {
  682. u64 orun = 1;
  683. ktime_t delta;
  684. delta = ktime_sub(now, timer->expires);
  685. if (delta.tv64 < 0)
  686. return 0;
  687. if (interval.tv64 < timer->base->resolution.tv64)
  688. interval.tv64 = timer->base->resolution.tv64;
  689. if (unlikely(delta.tv64 >= interval.tv64)) {
  690. s64 incr = ktime_to_ns(interval);
  691. orun = ktime_divns(delta, incr);
  692. timer->expires = ktime_add_ns(timer->expires, incr * orun);
  693. if (timer->expires.tv64 > now.tv64)
  694. return orun;
  695. /*
  696. * This (and the ktime_add() below) is the
  697. * correction for exact:
  698. */
  699. orun++;
  700. }
  701. timer->expires = ktime_add_safe(timer->expires, interval);
  702. return orun;
  703. }
  704. EXPORT_SYMBOL_GPL(hrtimer_forward);
  705. /*
  706. * enqueue_hrtimer - internal function to (re)start a timer
  707. *
  708. * The timer is inserted in expiry order. Insertion into the
  709. * red black tree is O(log(n)). Must hold the base lock.
  710. */
  711. static void enqueue_hrtimer(struct hrtimer *timer,
  712. struct hrtimer_clock_base *base, int reprogram)
  713. {
  714. struct rb_node **link = &base->active.rb_node;
  715. struct rb_node *parent = NULL;
  716. struct hrtimer *entry;
  717. int leftmost = 1;
  718. debug_hrtimer_activate(timer);
  719. /*
  720. * Find the right place in the rbtree:
  721. */
  722. while (*link) {
  723. parent = *link;
  724. entry = rb_entry(parent, struct hrtimer, node);
  725. /*
  726. * We dont care about collisions. Nodes with
  727. * the same expiry time stay together.
  728. */
  729. if (timer->expires.tv64 < entry->expires.tv64) {
  730. link = &(*link)->rb_left;
  731. } else {
  732. link = &(*link)->rb_right;
  733. leftmost = 0;
  734. }
  735. }
  736. /*
  737. * Insert the timer to the rbtree and check whether it
  738. * replaces the first pending timer
  739. */
  740. if (leftmost) {
  741. /*
  742. * Reprogram the clock event device. When the timer is already
  743. * expired hrtimer_enqueue_reprogram has either called the
  744. * callback or added it to the pending list and raised the
  745. * softirq.
  746. *
  747. * This is a NOP for !HIGHRES
  748. */
  749. if (reprogram && hrtimer_enqueue_reprogram(timer, base))
  750. return;
  751. base->first = &timer->node;
  752. }
  753. rb_link_node(&timer->node, parent, link);
  754. rb_insert_color(&timer->node, &base->active);
  755. /*
  756. * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
  757. * state of a possibly running callback.
  758. */
  759. timer->state |= HRTIMER_STATE_ENQUEUED;
  760. }
  761. /*
  762. * __remove_hrtimer - internal function to remove a timer
  763. *
  764. * Caller must hold the base lock.
  765. *
  766. * High resolution timer mode reprograms the clock event device when the
  767. * timer is the one which expires next. The caller can disable this by setting
  768. * reprogram to zero. This is useful, when the context does a reprogramming
  769. * anyway (e.g. timer interrupt)
  770. */
  771. static void __remove_hrtimer(struct hrtimer *timer,
  772. struct hrtimer_clock_base *base,
  773. unsigned long newstate, int reprogram)
  774. {
  775. /* High res. callback list. NOP for !HIGHRES */
  776. if (hrtimer_cb_pending(timer))
  777. hrtimer_remove_cb_pending(timer);
  778. else {
  779. /*
  780. * Remove the timer from the rbtree and replace the
  781. * first entry pointer if necessary.
  782. */
  783. if (base->first == &timer->node) {
  784. base->first = rb_next(&timer->node);
  785. /* Reprogram the clock event device. if enabled */
  786. if (reprogram && hrtimer_hres_active())
  787. hrtimer_force_reprogram(base->cpu_base);
  788. }
  789. rb_erase(&timer->node, &base->active);
  790. }
  791. timer->state = newstate;
  792. }
  793. /*
  794. * remove hrtimer, called with base lock held
  795. */
  796. static inline int
  797. remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
  798. {
  799. if (hrtimer_is_queued(timer)) {
  800. int reprogram;
  801. /*
  802. * Remove the timer and force reprogramming when high
  803. * resolution mode is active and the timer is on the current
  804. * CPU. If we remove a timer on another CPU, reprogramming is
  805. * skipped. The interrupt event on this CPU is fired and
  806. * reprogramming happens in the interrupt handler. This is a
  807. * rare case and less expensive than a smp call.
  808. */
  809. debug_hrtimer_deactivate(timer);
  810. timer_stats_hrtimer_clear_start_info(timer);
  811. reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
  812. __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE,
  813. reprogram);
  814. return 1;
  815. }
  816. return 0;
  817. }
  818. /**
  819. * hrtimer_start - (re)start an relative timer on the current CPU
  820. * @timer: the timer to be added
  821. * @tim: expiry time
  822. * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
  823. *
  824. * Returns:
  825. * 0 on success
  826. * 1 when the timer was active
  827. */
  828. int
  829. hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
  830. {
  831. struct hrtimer_clock_base *base, *new_base;
  832. unsigned long flags;
  833. int ret, raise;
  834. base = lock_hrtimer_base(timer, &flags);
  835. /* Remove an active timer from the queue: */
  836. ret = remove_hrtimer(timer, base);
  837. /* Switch the timer base, if necessary: */
  838. new_base = switch_hrtimer_base(timer, base);
  839. if (mode == HRTIMER_MODE_REL) {
  840. tim = ktime_add_safe(tim, new_base->get_time());
  841. /*
  842. * CONFIG_TIME_LOW_RES is a temporary way for architectures
  843. * to signal that they simply return xtime in
  844. * do_gettimeoffset(). In this case we want to round up by
  845. * resolution when starting a relative timer, to avoid short
  846. * timeouts. This will go away with the GTOD framework.
  847. */
  848. #ifdef CONFIG_TIME_LOW_RES
  849. tim = ktime_add_safe(tim, base->resolution);
  850. #endif
  851. }
  852. timer->expires = tim;
  853. timer_stats_hrtimer_set_start_info(timer);
  854. /*
  855. * Only allow reprogramming if the new base is on this CPU.
  856. * (it might still be on another CPU if the timer was pending)
  857. */
  858. enqueue_hrtimer(timer, new_base,
  859. new_base->cpu_base == &__get_cpu_var(hrtimer_bases));
  860. /*
  861. * The timer may be expired and moved to the cb_pending
  862. * list. We can not raise the softirq with base lock held due
  863. * to a possible deadlock with runqueue lock.
  864. */
  865. raise = timer->state == HRTIMER_STATE_PENDING;
  866. /*
  867. * We use preempt_disable to prevent this task from migrating after
  868. * setting up the softirq and raising it. Otherwise, if me migrate
  869. * we will raise the softirq on the wrong CPU.
  870. */
  871. preempt_disable();
  872. unlock_hrtimer_base(timer, &flags);
  873. if (raise)
  874. hrtimer_raise_softirq();
  875. preempt_enable();
  876. return ret;
  877. }
  878. EXPORT_SYMBOL_GPL(hrtimer_start);
  879. /**
  880. * hrtimer_try_to_cancel - try to deactivate a timer
  881. * @timer: hrtimer to stop
  882. *
  883. * Returns:
  884. * 0 when the timer was not active
  885. * 1 when the timer was active
  886. * -1 when the timer is currently excuting the callback function and
  887. * cannot be stopped
  888. */
  889. int hrtimer_try_to_cancel(struct hrtimer *timer)
  890. {
  891. struct hrtimer_clock_base *base;
  892. unsigned long flags;
  893. int ret = -1;
  894. base = lock_hrtimer_base(timer, &flags);
  895. if (!hrtimer_callback_running(timer))
  896. ret = remove_hrtimer(timer, base);
  897. unlock_hrtimer_base(timer, &flags);
  898. return ret;
  899. }
  900. EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
  901. /**
  902. * hrtimer_cancel - cancel a timer and wait for the handler to finish.
  903. * @timer: the timer to be cancelled
  904. *
  905. * Returns:
  906. * 0 when the timer was not active
  907. * 1 when the timer was active
  908. */
  909. int hrtimer_cancel(struct hrtimer *timer)
  910. {
  911. for (;;) {
  912. int ret = hrtimer_try_to_cancel(timer);
  913. if (ret >= 0)
  914. return ret;
  915. cpu_relax();
  916. }
  917. }
  918. EXPORT_SYMBOL_GPL(hrtimer_cancel);
  919. /**
  920. * hrtimer_get_remaining - get remaining time for the timer
  921. * @timer: the timer to read
  922. */
  923. ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
  924. {
  925. struct hrtimer_clock_base *base;
  926. unsigned long flags;
  927. ktime_t rem;
  928. base = lock_hrtimer_base(timer, &flags);
  929. rem = ktime_sub(timer->expires, base->get_time());
  930. unlock_hrtimer_base(timer, &flags);
  931. return rem;
  932. }
  933. EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
  934. #ifdef CONFIG_NO_HZ
  935. /**
  936. * hrtimer_get_next_event - get the time until next expiry event
  937. *
  938. * Returns the delta to the next expiry event or KTIME_MAX if no timer
  939. * is pending.
  940. */
  941. ktime_t hrtimer_get_next_event(void)
  942. {
  943. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  944. struct hrtimer_clock_base *base = cpu_base->clock_base;
  945. ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
  946. unsigned long flags;
  947. int i;
  948. spin_lock_irqsave(&cpu_base->lock, flags);
  949. if (!hrtimer_hres_active()) {
  950. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
  951. struct hrtimer *timer;
  952. if (!base->first)
  953. continue;
  954. timer = rb_entry(base->first, struct hrtimer, node);
  955. delta.tv64 = timer->expires.tv64;
  956. delta = ktime_sub(delta, base->get_time());
  957. if (delta.tv64 < mindelta.tv64)
  958. mindelta.tv64 = delta.tv64;
  959. }
  960. }
  961. spin_unlock_irqrestore(&cpu_base->lock, flags);
  962. if (mindelta.tv64 < 0)
  963. mindelta.tv64 = 0;
  964. return mindelta;
  965. }
  966. #endif
  967. static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  968. enum hrtimer_mode mode)
  969. {
  970. struct hrtimer_cpu_base *cpu_base;
  971. memset(timer, 0, sizeof(struct hrtimer));
  972. cpu_base = &__raw_get_cpu_var(hrtimer_bases);
  973. if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
  974. clock_id = CLOCK_MONOTONIC;
  975. timer->base = &cpu_base->clock_base[clock_id];
  976. INIT_LIST_HEAD(&timer->cb_entry);
  977. hrtimer_init_timer_hres(timer);
  978. #ifdef CONFIG_TIMER_STATS
  979. timer->start_site = NULL;
  980. timer->start_pid = -1;
  981. memset(timer->start_comm, 0, TASK_COMM_LEN);
  982. #endif
  983. }
  984. /**
  985. * hrtimer_init - initialize a timer to the given clock
  986. * @timer: the timer to be initialized
  987. * @clock_id: the clock to be used
  988. * @mode: timer mode abs/rel
  989. */
  990. void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  991. enum hrtimer_mode mode)
  992. {
  993. debug_hrtimer_init(timer);
  994. __hrtimer_init(timer, clock_id, mode);
  995. }
  996. EXPORT_SYMBOL_GPL(hrtimer_init);
  997. /**
  998. * hrtimer_get_res - get the timer resolution for a clock
  999. * @which_clock: which clock to query
  1000. * @tp: pointer to timespec variable to store the resolution
  1001. *
  1002. * Store the resolution of the clock selected by @which_clock in the
  1003. * variable pointed to by @tp.
  1004. */
  1005. int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
  1006. {
  1007. struct hrtimer_cpu_base *cpu_base;
  1008. cpu_base = &__raw_get_cpu_var(hrtimer_bases);
  1009. *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
  1010. return 0;
  1011. }
  1012. EXPORT_SYMBOL_GPL(hrtimer_get_res);
  1013. static void run_hrtimer_pending(struct hrtimer_cpu_base *cpu_base)
  1014. {
  1015. spin_lock_irq(&cpu_base->lock);
  1016. while (!list_empty(&cpu_base->cb_pending)) {
  1017. enum hrtimer_restart (*fn)(struct hrtimer *);
  1018. struct hrtimer *timer;
  1019. int restart;
  1020. timer = list_entry(cpu_base->cb_pending.next,
  1021. struct hrtimer, cb_entry);
  1022. debug_hrtimer_deactivate(timer);
  1023. timer_stats_account_hrtimer(timer);
  1024. fn = timer->function;
  1025. __remove_hrtimer(timer, timer->base, HRTIMER_STATE_CALLBACK, 0);
  1026. spin_unlock_irq(&cpu_base->lock);
  1027. restart = fn(timer);
  1028. spin_lock_irq(&cpu_base->lock);
  1029. timer->state &= ~HRTIMER_STATE_CALLBACK;
  1030. if (restart == HRTIMER_RESTART) {
  1031. BUG_ON(hrtimer_active(timer));
  1032. /*
  1033. * Enqueue the timer, allow reprogramming of the event
  1034. * device
  1035. */
  1036. enqueue_hrtimer(timer, timer->base, 1);
  1037. } else if (hrtimer_active(timer)) {
  1038. /*
  1039. * If the timer was rearmed on another CPU, reprogram
  1040. * the event device.
  1041. */
  1042. struct hrtimer_clock_base *base = timer->base;
  1043. if (base->first == &timer->node &&
  1044. hrtimer_reprogram(timer, base)) {
  1045. /*
  1046. * Timer is expired. Thus move it from tree to
  1047. * pending list again.
  1048. */
  1049. __remove_hrtimer(timer, base,
  1050. HRTIMER_STATE_PENDING, 0);
  1051. list_add_tail(&timer->cb_entry,
  1052. &base->cpu_base->cb_pending);
  1053. }
  1054. }
  1055. }
  1056. spin_unlock_irq(&cpu_base->lock);
  1057. }
  1058. static void __run_hrtimer(struct hrtimer *timer)
  1059. {
  1060. struct hrtimer_clock_base *base = timer->base;
  1061. struct hrtimer_cpu_base *cpu_base = base->cpu_base;
  1062. enum hrtimer_restart (*fn)(struct hrtimer *);
  1063. int restart;
  1064. debug_hrtimer_deactivate(timer);
  1065. __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
  1066. timer_stats_account_hrtimer(timer);
  1067. fn = timer->function;
  1068. if (timer->cb_mode == HRTIMER_CB_IRQSAFE_NO_SOFTIRQ) {
  1069. /*
  1070. * Used for scheduler timers, avoid lock inversion with
  1071. * rq->lock and tasklist_lock.
  1072. *
  1073. * These timers are required to deal with enqueue expiry
  1074. * themselves and are not allowed to migrate.
  1075. */
  1076. spin_unlock(&cpu_base->lock);
  1077. restart = fn(timer);
  1078. spin_lock(&cpu_base->lock);
  1079. } else
  1080. restart = fn(timer);
  1081. /*
  1082. * Note: We clear the CALLBACK bit after enqueue_hrtimer to avoid
  1083. * reprogramming of the event hardware. This happens at the end of this
  1084. * function anyway.
  1085. */
  1086. if (restart != HRTIMER_NORESTART) {
  1087. BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
  1088. enqueue_hrtimer(timer, base, 0);
  1089. }
  1090. timer->state &= ~HRTIMER_STATE_CALLBACK;
  1091. }
  1092. #ifdef CONFIG_HIGH_RES_TIMERS
  1093. /*
  1094. * High resolution timer interrupt
  1095. * Called with interrupts disabled
  1096. */
  1097. void hrtimer_interrupt(struct clock_event_device *dev)
  1098. {
  1099. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  1100. struct hrtimer_clock_base *base;
  1101. ktime_t expires_next, now;
  1102. int i, raise = 0;
  1103. BUG_ON(!cpu_base->hres_active);
  1104. cpu_base->nr_events++;
  1105. dev->next_event.tv64 = KTIME_MAX;
  1106. retry:
  1107. now = ktime_get();
  1108. expires_next.tv64 = KTIME_MAX;
  1109. base = cpu_base->clock_base;
  1110. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  1111. ktime_t basenow;
  1112. struct rb_node *node;
  1113. spin_lock(&cpu_base->lock);
  1114. basenow = ktime_add(now, base->offset);
  1115. while ((node = base->first)) {
  1116. struct hrtimer *timer;
  1117. timer = rb_entry(node, struct hrtimer, node);
  1118. if (basenow.tv64 < timer->expires.tv64) {
  1119. ktime_t expires;
  1120. expires = ktime_sub(timer->expires,
  1121. base->offset);
  1122. if (expires.tv64 < expires_next.tv64)
  1123. expires_next = expires;
  1124. break;
  1125. }
  1126. /* Move softirq callbacks to the pending list */
  1127. if (timer->cb_mode == HRTIMER_CB_SOFTIRQ) {
  1128. __remove_hrtimer(timer, base,
  1129. HRTIMER_STATE_PENDING, 0);
  1130. list_add_tail(&timer->cb_entry,
  1131. &base->cpu_base->cb_pending);
  1132. raise = 1;
  1133. continue;
  1134. }
  1135. __run_hrtimer(timer);
  1136. }
  1137. spin_unlock(&cpu_base->lock);
  1138. base++;
  1139. }
  1140. cpu_base->expires_next = expires_next;
  1141. /* Reprogramming necessary ? */
  1142. if (expires_next.tv64 != KTIME_MAX) {
  1143. if (tick_program_event(expires_next, 0))
  1144. goto retry;
  1145. }
  1146. /* Raise softirq ? */
  1147. if (raise)
  1148. raise_softirq(HRTIMER_SOFTIRQ);
  1149. }
  1150. static void run_hrtimer_softirq(struct softirq_action *h)
  1151. {
  1152. run_hrtimer_pending(&__get_cpu_var(hrtimer_bases));
  1153. }
  1154. #endif /* CONFIG_HIGH_RES_TIMERS */
  1155. /*
  1156. * Called from timer softirq every jiffy, expire hrtimers:
  1157. *
  1158. * For HRT its the fall back code to run the softirq in the timer
  1159. * softirq context in case the hrtimer initialization failed or has
  1160. * not been done yet.
  1161. */
  1162. void hrtimer_run_pending(void)
  1163. {
  1164. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  1165. if (hrtimer_hres_active())
  1166. return;
  1167. /*
  1168. * This _is_ ugly: We have to check in the softirq context,
  1169. * whether we can switch to highres and / or nohz mode. The
  1170. * clocksource switch happens in the timer interrupt with
  1171. * xtime_lock held. Notification from there only sets the
  1172. * check bit in the tick_oneshot code, otherwise we might
  1173. * deadlock vs. xtime_lock.
  1174. */
  1175. if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
  1176. hrtimer_switch_to_hres();
  1177. run_hrtimer_pending(cpu_base);
  1178. }
  1179. /*
  1180. * Called from hardirq context every jiffy
  1181. */
  1182. void hrtimer_run_queues(void)
  1183. {
  1184. struct rb_node *node;
  1185. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  1186. struct hrtimer_clock_base *base;
  1187. int index, gettime = 1;
  1188. if (hrtimer_hres_active())
  1189. return;
  1190. for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
  1191. base = &cpu_base->clock_base[index];
  1192. if (!base->first)
  1193. continue;
  1194. if (base->get_softirq_time)
  1195. base->softirq_time = base->get_softirq_time();
  1196. else if (gettime) {
  1197. hrtimer_get_softirq_time(cpu_base);
  1198. gettime = 0;
  1199. }
  1200. spin_lock(&cpu_base->lock);
  1201. while ((node = base->first)) {
  1202. struct hrtimer *timer;
  1203. timer = rb_entry(node, struct hrtimer, node);
  1204. if (base->softirq_time.tv64 <= timer->expires.tv64)
  1205. break;
  1206. if (timer->cb_mode == HRTIMER_CB_SOFTIRQ) {
  1207. __remove_hrtimer(timer, base,
  1208. HRTIMER_STATE_PENDING, 0);
  1209. list_add_tail(&timer->cb_entry,
  1210. &base->cpu_base->cb_pending);
  1211. continue;
  1212. }
  1213. __run_hrtimer(timer);
  1214. }
  1215. spin_unlock(&cpu_base->lock);
  1216. }
  1217. }
  1218. /*
  1219. * Sleep related functions:
  1220. */
  1221. static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
  1222. {
  1223. struct hrtimer_sleeper *t =
  1224. container_of(timer, struct hrtimer_sleeper, timer);
  1225. struct task_struct *task = t->task;
  1226. t->task = NULL;
  1227. if (task)
  1228. wake_up_process(task);
  1229. return HRTIMER_NORESTART;
  1230. }
  1231. void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
  1232. {
  1233. sl->timer.function = hrtimer_wakeup;
  1234. sl->task = task;
  1235. #ifdef CONFIG_HIGH_RES_TIMERS
  1236. sl->timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
  1237. #endif
  1238. }
  1239. static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
  1240. {
  1241. hrtimer_init_sleeper(t, current);
  1242. do {
  1243. set_current_state(TASK_INTERRUPTIBLE);
  1244. hrtimer_start(&t->timer, t->timer.expires, mode);
  1245. if (!hrtimer_active(&t->timer))
  1246. t->task = NULL;
  1247. if (likely(t->task))
  1248. schedule();
  1249. hrtimer_cancel(&t->timer);
  1250. mode = HRTIMER_MODE_ABS;
  1251. } while (t->task && !signal_pending(current));
  1252. __set_current_state(TASK_RUNNING);
  1253. return t->task == NULL;
  1254. }
  1255. static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
  1256. {
  1257. struct timespec rmt;
  1258. ktime_t rem;
  1259. rem = ktime_sub(timer->expires, timer->base->get_time());
  1260. if (rem.tv64 <= 0)
  1261. return 0;
  1262. rmt = ktime_to_timespec(rem);
  1263. if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
  1264. return -EFAULT;
  1265. return 1;
  1266. }
  1267. long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
  1268. {
  1269. struct hrtimer_sleeper t;
  1270. struct timespec __user *rmtp;
  1271. int ret = 0;
  1272. hrtimer_init_on_stack(&t.timer, restart->nanosleep.index,
  1273. HRTIMER_MODE_ABS);
  1274. t.timer.expires.tv64 = restart->nanosleep.expires;
  1275. if (do_nanosleep(&t, HRTIMER_MODE_ABS))
  1276. goto out;
  1277. rmtp = restart->nanosleep.rmtp;
  1278. if (rmtp) {
  1279. ret = update_rmtp(&t.timer, rmtp);
  1280. if (ret <= 0)
  1281. goto out;
  1282. }
  1283. /* The other values in restart are already filled in */
  1284. ret = -ERESTART_RESTARTBLOCK;
  1285. out:
  1286. destroy_hrtimer_on_stack(&t.timer);
  1287. return ret;
  1288. }
  1289. long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  1290. const enum hrtimer_mode mode, const clockid_t clockid)
  1291. {
  1292. struct restart_block *restart;
  1293. struct hrtimer_sleeper t;
  1294. int ret = 0;
  1295. hrtimer_init_on_stack(&t.timer, clockid, mode);
  1296. t.timer.expires = timespec_to_ktime(*rqtp);
  1297. if (do_nanosleep(&t, mode))
  1298. goto out;
  1299. /* Absolute timers do not update the rmtp value and restart: */
  1300. if (mode == HRTIMER_MODE_ABS) {
  1301. ret = -ERESTARTNOHAND;
  1302. goto out;
  1303. }
  1304. if (rmtp) {
  1305. ret = update_rmtp(&t.timer, rmtp);
  1306. if (ret <= 0)
  1307. goto out;
  1308. }
  1309. restart = &current_thread_info()->restart_block;
  1310. restart->fn = hrtimer_nanosleep_restart;
  1311. restart->nanosleep.index = t.timer.base->index;
  1312. restart->nanosleep.rmtp = rmtp;
  1313. restart->nanosleep.expires = t.timer.expires.tv64;
  1314. ret = -ERESTART_RESTARTBLOCK;
  1315. out:
  1316. destroy_hrtimer_on_stack(&t.timer);
  1317. return ret;
  1318. }
  1319. asmlinkage long
  1320. sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp)
  1321. {
  1322. struct timespec tu;
  1323. if (copy_from_user(&tu, rqtp, sizeof(tu)))
  1324. return -EFAULT;
  1325. if (!timespec_valid(&tu))
  1326. return -EINVAL;
  1327. return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
  1328. }
  1329. /*
  1330. * Functions related to boot-time initialization:
  1331. */
  1332. static void __cpuinit init_hrtimers_cpu(int cpu)
  1333. {
  1334. struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
  1335. int i;
  1336. spin_lock_init(&cpu_base->lock);
  1337. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
  1338. cpu_base->clock_base[i].cpu_base = cpu_base;
  1339. INIT_LIST_HEAD(&cpu_base->cb_pending);
  1340. hrtimer_init_hres(cpu_base);
  1341. }
  1342. #ifdef CONFIG_HOTPLUG_CPU
  1343. static int migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
  1344. struct hrtimer_clock_base *new_base)
  1345. {
  1346. struct hrtimer *timer;
  1347. struct rb_node *node;
  1348. int raise = 0;
  1349. while ((node = rb_first(&old_base->active))) {
  1350. timer = rb_entry(node, struct hrtimer, node);
  1351. BUG_ON(hrtimer_callback_running(timer));
  1352. debug_hrtimer_deactivate(timer);
  1353. /*
  1354. * Mark it as STATE_MIGRATE not INACTIVE otherwise the
  1355. * timer could be seen as !active and just vanish away
  1356. * under us on another CPU
  1357. */
  1358. __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
  1359. timer->base = new_base;
  1360. /*
  1361. * Enqueue the timer. Allow reprogramming of the event device
  1362. */
  1363. enqueue_hrtimer(timer, new_base, 1);
  1364. #ifdef CONFIG_HIGH_RES_TIMERS
  1365. /*
  1366. * Happens with high res enabled when the timer was
  1367. * already expired and the callback mode is
  1368. * HRTIMER_CB_IRQSAFE_NO_SOFTIRQ
  1369. * (hrtimer_sleeper). The enqueue code does not move
  1370. * them to the soft irq pending list for
  1371. * performance/latency reasons, but in the migration
  1372. * state, we need to do that otherwise we end up with
  1373. * a stale timer.
  1374. */
  1375. if (timer->state == HRTIMER_STATE_MIGRATE) {
  1376. timer->state = HRTIMER_STATE_PENDING;
  1377. list_add_tail(&timer->cb_entry,
  1378. &new_base->cpu_base->cb_pending);
  1379. raise = 1;
  1380. }
  1381. #endif
  1382. /* Clear the migration state bit */
  1383. timer->state &= ~HRTIMER_STATE_MIGRATE;
  1384. }
  1385. return raise;
  1386. }
  1387. #ifdef CONFIG_HIGH_RES_TIMERS
  1388. static int migrate_hrtimer_pending(struct hrtimer_cpu_base *old_base,
  1389. struct hrtimer_cpu_base *new_base)
  1390. {
  1391. struct hrtimer *timer;
  1392. int raise = 0;
  1393. while (!list_empty(&old_base->cb_pending)) {
  1394. timer = list_entry(old_base->cb_pending.next,
  1395. struct hrtimer, cb_entry);
  1396. __remove_hrtimer(timer, timer->base, HRTIMER_STATE_PENDING, 0);
  1397. timer->base = &new_base->clock_base[timer->base->index];
  1398. list_add_tail(&timer->cb_entry, &new_base->cb_pending);
  1399. raise = 1;
  1400. }
  1401. return raise;
  1402. }
  1403. #else
  1404. static int migrate_hrtimer_pending(struct hrtimer_cpu_base *old_base,
  1405. struct hrtimer_cpu_base *new_base)
  1406. {
  1407. return 0;
  1408. }
  1409. #endif
  1410. static void migrate_hrtimers(int cpu)
  1411. {
  1412. struct hrtimer_cpu_base *old_base, *new_base;
  1413. int i, raise = 0;
  1414. BUG_ON(cpu_online(cpu));
  1415. old_base = &per_cpu(hrtimer_bases, cpu);
  1416. new_base = &get_cpu_var(hrtimer_bases);
  1417. tick_cancel_sched_timer(cpu);
  1418. local_irq_disable();
  1419. spin_lock(&new_base->lock);
  1420. spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
  1421. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  1422. if (migrate_hrtimer_list(&old_base->clock_base[i],
  1423. &new_base->clock_base[i]))
  1424. raise = 1;
  1425. }
  1426. if (migrate_hrtimer_pending(old_base, new_base))
  1427. raise = 1;
  1428. spin_unlock(&old_base->lock);
  1429. spin_unlock(&new_base->lock);
  1430. local_irq_enable();
  1431. put_cpu_var(hrtimer_bases);
  1432. if (raise)
  1433. hrtimer_raise_softirq();
  1434. }
  1435. #endif /* CONFIG_HOTPLUG_CPU */
  1436. static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
  1437. unsigned long action, void *hcpu)
  1438. {
  1439. unsigned int cpu = (long)hcpu;
  1440. switch (action) {
  1441. case CPU_UP_PREPARE:
  1442. case CPU_UP_PREPARE_FROZEN:
  1443. init_hrtimers_cpu(cpu);
  1444. break;
  1445. #ifdef CONFIG_HOTPLUG_CPU
  1446. case CPU_DEAD:
  1447. case CPU_DEAD_FROZEN:
  1448. clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &cpu);
  1449. migrate_hrtimers(cpu);
  1450. break;
  1451. #endif
  1452. default:
  1453. break;
  1454. }
  1455. return NOTIFY_OK;
  1456. }
  1457. static struct notifier_block __cpuinitdata hrtimers_nb = {
  1458. .notifier_call = hrtimer_cpu_notify,
  1459. };
  1460. void __init hrtimers_init(void)
  1461. {
  1462. hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
  1463. (void *)(long)smp_processor_id());
  1464. register_cpu_notifier(&hrtimers_nb);
  1465. #ifdef CONFIG_HIGH_RES_TIMERS
  1466. open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
  1467. #endif
  1468. }