hrtimer.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709
  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. /* High resolution timer related functions */
  379. #ifdef CONFIG_HIGH_RES_TIMERS
  380. /*
  381. * High resolution timer enabled ?
  382. */
  383. static int hrtimer_hres_enabled __read_mostly = 1;
  384. /*
  385. * Enable / Disable high resolution mode
  386. */
  387. static int __init setup_hrtimer_hres(char *str)
  388. {
  389. if (!strcmp(str, "off"))
  390. hrtimer_hres_enabled = 0;
  391. else if (!strcmp(str, "on"))
  392. hrtimer_hres_enabled = 1;
  393. else
  394. return 0;
  395. return 1;
  396. }
  397. __setup("highres=", setup_hrtimer_hres);
  398. /*
  399. * hrtimer_high_res_enabled - query, if the highres mode is enabled
  400. */
  401. static inline int hrtimer_is_hres_enabled(void)
  402. {
  403. return hrtimer_hres_enabled;
  404. }
  405. /*
  406. * Is the high resolution mode active ?
  407. */
  408. static inline int hrtimer_hres_active(void)
  409. {
  410. return __get_cpu_var(hrtimer_bases).hres_active;
  411. }
  412. /*
  413. * Reprogram the event source with checking both queues for the
  414. * next event
  415. * Called with interrupts disabled and base->lock held
  416. */
  417. static void hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base)
  418. {
  419. int i;
  420. struct hrtimer_clock_base *base = cpu_base->clock_base;
  421. ktime_t expires;
  422. cpu_base->expires_next.tv64 = KTIME_MAX;
  423. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
  424. struct hrtimer *timer;
  425. if (!base->first)
  426. continue;
  427. timer = rb_entry(base->first, struct hrtimer, node);
  428. expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
  429. if (expires.tv64 < cpu_base->expires_next.tv64)
  430. cpu_base->expires_next = expires;
  431. }
  432. if (cpu_base->expires_next.tv64 != KTIME_MAX)
  433. tick_program_event(cpu_base->expires_next, 1);
  434. }
  435. /*
  436. * Shared reprogramming for clock_realtime and clock_monotonic
  437. *
  438. * When a timer is enqueued and expires earlier than the already enqueued
  439. * timers, we have to check, whether it expires earlier than the timer for
  440. * which the clock event device was armed.
  441. *
  442. * Called with interrupts disabled and base->cpu_base.lock held
  443. */
  444. static int hrtimer_reprogram(struct hrtimer *timer,
  445. struct hrtimer_clock_base *base)
  446. {
  447. ktime_t *expires_next = &__get_cpu_var(hrtimer_bases).expires_next;
  448. ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
  449. int res;
  450. WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
  451. /*
  452. * When the callback is running, we do not reprogram the clock event
  453. * device. The timer callback is either running on a different CPU or
  454. * the callback is executed in the hrtimer_interrupt context. The
  455. * reprogramming is handled either by the softirq, which called the
  456. * callback or at the end of the hrtimer_interrupt.
  457. */
  458. if (hrtimer_callback_running(timer))
  459. return 0;
  460. /*
  461. * CLOCK_REALTIME timer might be requested with an absolute
  462. * expiry time which is less than base->offset. Nothing wrong
  463. * about that, just avoid to call into the tick code, which
  464. * has now objections against negative expiry values.
  465. */
  466. if (expires.tv64 < 0)
  467. return -ETIME;
  468. if (expires.tv64 >= expires_next->tv64)
  469. return 0;
  470. /*
  471. * Clockevents returns -ETIME, when the event was in the past.
  472. */
  473. res = tick_program_event(expires, 0);
  474. if (!IS_ERR_VALUE(res))
  475. *expires_next = expires;
  476. return res;
  477. }
  478. /*
  479. * Retrigger next event is called after clock was set
  480. *
  481. * Called with interrupts disabled via on_each_cpu()
  482. */
  483. static void retrigger_next_event(void *arg)
  484. {
  485. struct hrtimer_cpu_base *base;
  486. struct timespec realtime_offset;
  487. unsigned long seq;
  488. if (!hrtimer_hres_active())
  489. return;
  490. do {
  491. seq = read_seqbegin(&xtime_lock);
  492. set_normalized_timespec(&realtime_offset,
  493. -wall_to_monotonic.tv_sec,
  494. -wall_to_monotonic.tv_nsec);
  495. } while (read_seqretry(&xtime_lock, seq));
  496. base = &__get_cpu_var(hrtimer_bases);
  497. /* Adjust CLOCK_REALTIME offset */
  498. spin_lock(&base->lock);
  499. base->clock_base[CLOCK_REALTIME].offset =
  500. timespec_to_ktime(realtime_offset);
  501. hrtimer_force_reprogram(base);
  502. spin_unlock(&base->lock);
  503. }
  504. /*
  505. * Clock realtime was set
  506. *
  507. * Change the offset of the realtime clock vs. the monotonic
  508. * clock.
  509. *
  510. * We might have to reprogram the high resolution timer interrupt. On
  511. * SMP we call the architecture specific code to retrigger _all_ high
  512. * resolution timer interrupts. On UP we just disable interrupts and
  513. * call the high resolution interrupt code.
  514. */
  515. void clock_was_set(void)
  516. {
  517. /* Retrigger the CPU local events everywhere */
  518. on_each_cpu(retrigger_next_event, NULL, 1);
  519. }
  520. /*
  521. * During resume we might have to reprogram the high resolution timer
  522. * interrupt (on the local CPU):
  523. */
  524. void hres_timers_resume(void)
  525. {
  526. /* Retrigger the CPU local events: */
  527. retrigger_next_event(NULL);
  528. }
  529. /*
  530. * Initialize the high resolution related parts of cpu_base
  531. */
  532. static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
  533. {
  534. base->expires_next.tv64 = KTIME_MAX;
  535. base->hres_active = 0;
  536. }
  537. /*
  538. * Initialize the high resolution related parts of a hrtimer
  539. */
  540. static inline void hrtimer_init_timer_hres(struct hrtimer *timer)
  541. {
  542. }
  543. static void __run_hrtimer(struct hrtimer *timer);
  544. /*
  545. * When High resolution timers are active, try to reprogram. Note, that in case
  546. * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
  547. * check happens. The timer gets enqueued into the rbtree. The reprogramming
  548. * and expiry check is done in the hrtimer_interrupt or in the softirq.
  549. */
  550. static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
  551. struct hrtimer_clock_base *base)
  552. {
  553. if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
  554. /*
  555. * XXX: recursion check?
  556. * hrtimer_forward() should round up with timer granularity
  557. * so that we never get into inf recursion here,
  558. * it doesn't do that though
  559. */
  560. __run_hrtimer(timer);
  561. return 1;
  562. }
  563. return 0;
  564. }
  565. /*
  566. * Switch to high resolution mode
  567. */
  568. static int hrtimer_switch_to_hres(void)
  569. {
  570. int cpu = smp_processor_id();
  571. struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
  572. unsigned long flags;
  573. if (base->hres_active)
  574. return 1;
  575. local_irq_save(flags);
  576. if (tick_init_highres()) {
  577. local_irq_restore(flags);
  578. printk(KERN_WARNING "Could not switch to high resolution "
  579. "mode on CPU %d\n", cpu);
  580. return 0;
  581. }
  582. base->hres_active = 1;
  583. base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES;
  584. base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES;
  585. tick_setup_sched_timer();
  586. /* "Retrigger" the interrupt to get things going */
  587. retrigger_next_event(NULL);
  588. local_irq_restore(flags);
  589. printk(KERN_DEBUG "Switched to high resolution mode on CPU %d\n",
  590. smp_processor_id());
  591. return 1;
  592. }
  593. #else
  594. static inline int hrtimer_hres_active(void) { return 0; }
  595. static inline int hrtimer_is_hres_enabled(void) { return 0; }
  596. static inline int hrtimer_switch_to_hres(void) { return 0; }
  597. static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base *base) { }
  598. static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
  599. struct hrtimer_clock_base *base)
  600. {
  601. return 0;
  602. }
  603. static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
  604. static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { }
  605. static inline int hrtimer_reprogram(struct hrtimer *timer,
  606. struct hrtimer_clock_base *base)
  607. {
  608. return 0;
  609. }
  610. #endif /* CONFIG_HIGH_RES_TIMERS */
  611. #ifdef CONFIG_TIMER_STATS
  612. void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer, void *addr)
  613. {
  614. if (timer->start_site)
  615. return;
  616. timer->start_site = addr;
  617. memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
  618. timer->start_pid = current->pid;
  619. }
  620. #endif
  621. /*
  622. * Counterpart to lock_hrtimer_base above:
  623. */
  624. static inline
  625. void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
  626. {
  627. spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
  628. }
  629. /**
  630. * hrtimer_forward - forward the timer expiry
  631. * @timer: hrtimer to forward
  632. * @now: forward past this time
  633. * @interval: the interval to forward
  634. *
  635. * Forward the timer expiry so it will expire in the future.
  636. * Returns the number of overruns.
  637. */
  638. u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
  639. {
  640. u64 orun = 1;
  641. ktime_t delta;
  642. delta = ktime_sub(now, hrtimer_get_expires(timer));
  643. if (delta.tv64 < 0)
  644. return 0;
  645. if (interval.tv64 < timer->base->resolution.tv64)
  646. interval.tv64 = timer->base->resolution.tv64;
  647. if (unlikely(delta.tv64 >= interval.tv64)) {
  648. s64 incr = ktime_to_ns(interval);
  649. orun = ktime_divns(delta, incr);
  650. hrtimer_add_expires_ns(timer, incr * orun);
  651. if (hrtimer_get_expires_tv64(timer) > now.tv64)
  652. return orun;
  653. /*
  654. * This (and the ktime_add() below) is the
  655. * correction for exact:
  656. */
  657. orun++;
  658. }
  659. hrtimer_add_expires(timer, interval);
  660. return orun;
  661. }
  662. EXPORT_SYMBOL_GPL(hrtimer_forward);
  663. /*
  664. * enqueue_hrtimer - internal function to (re)start a timer
  665. *
  666. * The timer is inserted in expiry order. Insertion into the
  667. * red black tree is O(log(n)). Must hold the base lock.
  668. */
  669. static void enqueue_hrtimer(struct hrtimer *timer,
  670. struct hrtimer_clock_base *base, int reprogram)
  671. {
  672. struct rb_node **link = &base->active.rb_node;
  673. struct rb_node *parent = NULL;
  674. struct hrtimer *entry;
  675. int leftmost = 1;
  676. debug_hrtimer_activate(timer);
  677. /*
  678. * Find the right place in the rbtree:
  679. */
  680. while (*link) {
  681. parent = *link;
  682. entry = rb_entry(parent, struct hrtimer, node);
  683. /*
  684. * We dont care about collisions. Nodes with
  685. * the same expiry time stay together.
  686. */
  687. if (hrtimer_get_expires_tv64(timer) <
  688. hrtimer_get_expires_tv64(entry)) {
  689. link = &(*link)->rb_left;
  690. } else {
  691. link = &(*link)->rb_right;
  692. leftmost = 0;
  693. }
  694. }
  695. /*
  696. * Insert the timer to the rbtree and check whether it
  697. * replaces the first pending timer
  698. */
  699. if (leftmost) {
  700. /*
  701. * Reprogram the clock event device. When the timer is already
  702. * expired hrtimer_enqueue_reprogram has either called the
  703. * callback or added it to the pending list and raised the
  704. * softirq.
  705. *
  706. * This is a NOP for !HIGHRES
  707. */
  708. if (reprogram && hrtimer_enqueue_reprogram(timer, base))
  709. return;
  710. base->first = &timer->node;
  711. }
  712. rb_link_node(&timer->node, parent, link);
  713. rb_insert_color(&timer->node, &base->active);
  714. /*
  715. * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
  716. * state of a possibly running callback.
  717. */
  718. timer->state |= HRTIMER_STATE_ENQUEUED;
  719. }
  720. /*
  721. * __remove_hrtimer - internal function to remove a timer
  722. *
  723. * Caller must hold the base lock.
  724. *
  725. * High resolution timer mode reprograms the clock event device when the
  726. * timer is the one which expires next. The caller can disable this by setting
  727. * reprogram to zero. This is useful, when the context does a reprogramming
  728. * anyway (e.g. timer interrupt)
  729. */
  730. static void __remove_hrtimer(struct hrtimer *timer,
  731. struct hrtimer_clock_base *base,
  732. unsigned long newstate, int reprogram)
  733. {
  734. if (timer->state & HRTIMER_STATE_ENQUEUED) {
  735. /*
  736. * Remove the timer from the rbtree and replace the
  737. * first entry pointer if necessary.
  738. */
  739. if (base->first == &timer->node) {
  740. base->first = rb_next(&timer->node);
  741. /* Reprogram the clock event device. if enabled */
  742. if (reprogram && hrtimer_hres_active())
  743. hrtimer_force_reprogram(base->cpu_base);
  744. }
  745. rb_erase(&timer->node, &base->active);
  746. }
  747. timer->state = newstate;
  748. }
  749. /*
  750. * remove hrtimer, called with base lock held
  751. */
  752. static inline int
  753. remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
  754. {
  755. if (hrtimer_is_queued(timer)) {
  756. int reprogram;
  757. /*
  758. * Remove the timer and force reprogramming when high
  759. * resolution mode is active and the timer is on the current
  760. * CPU. If we remove a timer on another CPU, reprogramming is
  761. * skipped. The interrupt event on this CPU is fired and
  762. * reprogramming happens in the interrupt handler. This is a
  763. * rare case and less expensive than a smp call.
  764. */
  765. debug_hrtimer_deactivate(timer);
  766. timer_stats_hrtimer_clear_start_info(timer);
  767. reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
  768. __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE,
  769. reprogram);
  770. return 1;
  771. }
  772. return 0;
  773. }
  774. /**
  775. * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
  776. * @timer: the timer to be added
  777. * @tim: expiry time
  778. * @delta_ns: "slack" range for the timer
  779. * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
  780. *
  781. * Returns:
  782. * 0 on success
  783. * 1 when the timer was active
  784. */
  785. int
  786. hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim, unsigned long delta_ns,
  787. const enum hrtimer_mode mode)
  788. {
  789. struct hrtimer_clock_base *base, *new_base;
  790. unsigned long flags;
  791. int ret;
  792. base = lock_hrtimer_base(timer, &flags);
  793. /* Remove an active timer from the queue: */
  794. ret = remove_hrtimer(timer, base);
  795. /* Switch the timer base, if necessary: */
  796. new_base = switch_hrtimer_base(timer, base);
  797. if (mode == HRTIMER_MODE_REL) {
  798. tim = ktime_add_safe(tim, new_base->get_time());
  799. /*
  800. * CONFIG_TIME_LOW_RES is a temporary way for architectures
  801. * to signal that they simply return xtime in
  802. * do_gettimeoffset(). In this case we want to round up by
  803. * resolution when starting a relative timer, to avoid short
  804. * timeouts. This will go away with the GTOD framework.
  805. */
  806. #ifdef CONFIG_TIME_LOW_RES
  807. tim = ktime_add_safe(tim, base->resolution);
  808. #endif
  809. }
  810. hrtimer_set_expires_range_ns(timer, tim, delta_ns);
  811. timer_stats_hrtimer_set_start_info(timer);
  812. /*
  813. * Only allow reprogramming if the new base is on this CPU.
  814. * (it might still be on another CPU if the timer was pending)
  815. */
  816. enqueue_hrtimer(timer, new_base,
  817. new_base->cpu_base == &__get_cpu_var(hrtimer_bases));
  818. unlock_hrtimer_base(timer, &flags);
  819. return ret;
  820. }
  821. EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
  822. /**
  823. * hrtimer_start - (re)start an hrtimer on the current CPU
  824. * @timer: the timer to be added
  825. * @tim: expiry time
  826. * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
  827. *
  828. * Returns:
  829. * 0 on success
  830. * 1 when the timer was active
  831. */
  832. int
  833. hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
  834. {
  835. return hrtimer_start_range_ns(timer, tim, 0, mode);
  836. }
  837. EXPORT_SYMBOL_GPL(hrtimer_start);
  838. /**
  839. * hrtimer_try_to_cancel - try to deactivate a timer
  840. * @timer: hrtimer to stop
  841. *
  842. * Returns:
  843. * 0 when the timer was not active
  844. * 1 when the timer was active
  845. * -1 when the timer is currently excuting the callback function and
  846. * cannot be stopped
  847. */
  848. int hrtimer_try_to_cancel(struct hrtimer *timer)
  849. {
  850. struct hrtimer_clock_base *base;
  851. unsigned long flags;
  852. int ret = -1;
  853. base = lock_hrtimer_base(timer, &flags);
  854. if (!hrtimer_callback_running(timer))
  855. ret = remove_hrtimer(timer, base);
  856. unlock_hrtimer_base(timer, &flags);
  857. return ret;
  858. }
  859. EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
  860. /**
  861. * hrtimer_cancel - cancel a timer and wait for the handler to finish.
  862. * @timer: the timer to be cancelled
  863. *
  864. * Returns:
  865. * 0 when the timer was not active
  866. * 1 when the timer was active
  867. */
  868. int hrtimer_cancel(struct hrtimer *timer)
  869. {
  870. for (;;) {
  871. int ret = hrtimer_try_to_cancel(timer);
  872. if (ret >= 0)
  873. return ret;
  874. cpu_relax();
  875. }
  876. }
  877. EXPORT_SYMBOL_GPL(hrtimer_cancel);
  878. /**
  879. * hrtimer_get_remaining - get remaining time for the timer
  880. * @timer: the timer to read
  881. */
  882. ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
  883. {
  884. struct hrtimer_clock_base *base;
  885. unsigned long flags;
  886. ktime_t rem;
  887. base = lock_hrtimer_base(timer, &flags);
  888. rem = hrtimer_expires_remaining(timer);
  889. unlock_hrtimer_base(timer, &flags);
  890. return rem;
  891. }
  892. EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
  893. #ifdef CONFIG_NO_HZ
  894. /**
  895. * hrtimer_get_next_event - get the time until next expiry event
  896. *
  897. * Returns the delta to the next expiry event or KTIME_MAX if no timer
  898. * is pending.
  899. */
  900. ktime_t hrtimer_get_next_event(void)
  901. {
  902. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  903. struct hrtimer_clock_base *base = cpu_base->clock_base;
  904. ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
  905. unsigned long flags;
  906. int i;
  907. spin_lock_irqsave(&cpu_base->lock, flags);
  908. if (!hrtimer_hres_active()) {
  909. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
  910. struct hrtimer *timer;
  911. if (!base->first)
  912. continue;
  913. timer = rb_entry(base->first, struct hrtimer, node);
  914. delta.tv64 = hrtimer_get_expires_tv64(timer);
  915. delta = ktime_sub(delta, base->get_time());
  916. if (delta.tv64 < mindelta.tv64)
  917. mindelta.tv64 = delta.tv64;
  918. }
  919. }
  920. spin_unlock_irqrestore(&cpu_base->lock, flags);
  921. if (mindelta.tv64 < 0)
  922. mindelta.tv64 = 0;
  923. return mindelta;
  924. }
  925. #endif
  926. static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  927. enum hrtimer_mode mode)
  928. {
  929. struct hrtimer_cpu_base *cpu_base;
  930. memset(timer, 0, sizeof(struct hrtimer));
  931. cpu_base = &__raw_get_cpu_var(hrtimer_bases);
  932. if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
  933. clock_id = CLOCK_MONOTONIC;
  934. timer->base = &cpu_base->clock_base[clock_id];
  935. INIT_LIST_HEAD(&timer->cb_entry);
  936. hrtimer_init_timer_hres(timer);
  937. #ifdef CONFIG_TIMER_STATS
  938. timer->start_site = NULL;
  939. timer->start_pid = -1;
  940. memset(timer->start_comm, 0, TASK_COMM_LEN);
  941. #endif
  942. }
  943. /**
  944. * hrtimer_init - initialize a timer to the given clock
  945. * @timer: the timer to be initialized
  946. * @clock_id: the clock to be used
  947. * @mode: timer mode abs/rel
  948. */
  949. void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  950. enum hrtimer_mode mode)
  951. {
  952. debug_hrtimer_init(timer);
  953. __hrtimer_init(timer, clock_id, mode);
  954. }
  955. EXPORT_SYMBOL_GPL(hrtimer_init);
  956. /**
  957. * hrtimer_get_res - get the timer resolution for a clock
  958. * @which_clock: which clock to query
  959. * @tp: pointer to timespec variable to store the resolution
  960. *
  961. * Store the resolution of the clock selected by @which_clock in the
  962. * variable pointed to by @tp.
  963. */
  964. int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
  965. {
  966. struct hrtimer_cpu_base *cpu_base;
  967. cpu_base = &__raw_get_cpu_var(hrtimer_bases);
  968. *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
  969. return 0;
  970. }
  971. EXPORT_SYMBOL_GPL(hrtimer_get_res);
  972. static void __run_hrtimer(struct hrtimer *timer)
  973. {
  974. struct hrtimer_clock_base *base = timer->base;
  975. struct hrtimer_cpu_base *cpu_base = base->cpu_base;
  976. enum hrtimer_restart (*fn)(struct hrtimer *);
  977. int restart;
  978. WARN_ON(!irqs_disabled());
  979. debug_hrtimer_deactivate(timer);
  980. __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
  981. timer_stats_account_hrtimer(timer);
  982. fn = timer->function;
  983. /*
  984. * Because we run timers from hardirq context, there is no chance
  985. * they get migrated to another cpu, therefore its safe to unlock
  986. * the timer base.
  987. */
  988. spin_unlock(&cpu_base->lock);
  989. restart = fn(timer);
  990. spin_lock(&cpu_base->lock);
  991. /*
  992. * Note: We clear the CALLBACK bit after enqueue_hrtimer to avoid
  993. * reprogramming of the event hardware. This happens at the end of this
  994. * function anyway.
  995. */
  996. if (restart != HRTIMER_NORESTART) {
  997. BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
  998. enqueue_hrtimer(timer, base, 0);
  999. }
  1000. timer->state &= ~HRTIMER_STATE_CALLBACK;
  1001. }
  1002. #ifdef CONFIG_HIGH_RES_TIMERS
  1003. /*
  1004. * High resolution timer interrupt
  1005. * Called with interrupts disabled
  1006. */
  1007. void hrtimer_interrupt(struct clock_event_device *dev)
  1008. {
  1009. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  1010. struct hrtimer_clock_base *base;
  1011. ktime_t expires_next, now;
  1012. int i;
  1013. BUG_ON(!cpu_base->hres_active);
  1014. cpu_base->nr_events++;
  1015. dev->next_event.tv64 = KTIME_MAX;
  1016. retry:
  1017. now = ktime_get();
  1018. expires_next.tv64 = KTIME_MAX;
  1019. base = cpu_base->clock_base;
  1020. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  1021. ktime_t basenow;
  1022. struct rb_node *node;
  1023. spin_lock(&cpu_base->lock);
  1024. basenow = ktime_add(now, base->offset);
  1025. while ((node = base->first)) {
  1026. struct hrtimer *timer;
  1027. timer = rb_entry(node, struct hrtimer, node);
  1028. /*
  1029. * The immediate goal for using the softexpires is
  1030. * minimizing wakeups, not running timers at the
  1031. * earliest interrupt after their soft expiration.
  1032. * This allows us to avoid using a Priority Search
  1033. * Tree, which can answer a stabbing querry for
  1034. * overlapping intervals and instead use the simple
  1035. * BST we already have.
  1036. * We don't add extra wakeups by delaying timers that
  1037. * are right-of a not yet expired timer, because that
  1038. * timer will have to trigger a wakeup anyway.
  1039. */
  1040. if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
  1041. ktime_t expires;
  1042. expires = ktime_sub(hrtimer_get_expires(timer),
  1043. base->offset);
  1044. if (expires.tv64 < expires_next.tv64)
  1045. expires_next = expires;
  1046. break;
  1047. }
  1048. __run_hrtimer(timer);
  1049. }
  1050. spin_unlock(&cpu_base->lock);
  1051. base++;
  1052. }
  1053. cpu_base->expires_next = expires_next;
  1054. /* Reprogramming necessary ? */
  1055. if (expires_next.tv64 != KTIME_MAX) {
  1056. if (tick_program_event(expires_next, 0))
  1057. goto retry;
  1058. }
  1059. }
  1060. /**
  1061. * hrtimer_peek_ahead_timers -- run soft-expired timers now
  1062. *
  1063. * hrtimer_peek_ahead_timers will peek at the timer queue of
  1064. * the current cpu and check if there are any timers for which
  1065. * the soft expires time has passed. If any such timers exist,
  1066. * they are run immediately and then removed from the timer queue.
  1067. *
  1068. */
  1069. void hrtimer_peek_ahead_timers(void)
  1070. {
  1071. struct tick_device *td;
  1072. unsigned long flags;
  1073. if (!hrtimer_hres_active())
  1074. return;
  1075. local_irq_save(flags);
  1076. td = &__get_cpu_var(tick_cpu_device);
  1077. if (td && td->evtdev)
  1078. hrtimer_interrupt(td->evtdev);
  1079. local_irq_restore(flags);
  1080. }
  1081. #endif /* CONFIG_HIGH_RES_TIMERS */
  1082. /*
  1083. * Called from timer softirq every jiffy, expire hrtimers:
  1084. *
  1085. * For HRT its the fall back code to run the softirq in the timer
  1086. * softirq context in case the hrtimer initialization failed or has
  1087. * not been done yet.
  1088. */
  1089. void hrtimer_run_pending(void)
  1090. {
  1091. if (hrtimer_hres_active())
  1092. return;
  1093. /*
  1094. * This _is_ ugly: We have to check in the softirq context,
  1095. * whether we can switch to highres and / or nohz mode. The
  1096. * clocksource switch happens in the timer interrupt with
  1097. * xtime_lock held. Notification from there only sets the
  1098. * check bit in the tick_oneshot code, otherwise we might
  1099. * deadlock vs. xtime_lock.
  1100. */
  1101. if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
  1102. hrtimer_switch_to_hres();
  1103. }
  1104. /*
  1105. * Called from hardirq context every jiffy
  1106. */
  1107. void hrtimer_run_queues(void)
  1108. {
  1109. struct rb_node *node;
  1110. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  1111. struct hrtimer_clock_base *base;
  1112. int index, gettime = 1;
  1113. if (hrtimer_hres_active())
  1114. return;
  1115. for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
  1116. base = &cpu_base->clock_base[index];
  1117. if (!base->first)
  1118. continue;
  1119. if (gettime) {
  1120. hrtimer_get_softirq_time(cpu_base);
  1121. gettime = 0;
  1122. }
  1123. spin_lock(&cpu_base->lock);
  1124. while ((node = base->first)) {
  1125. struct hrtimer *timer;
  1126. timer = rb_entry(node, struct hrtimer, node);
  1127. if (base->softirq_time.tv64 <=
  1128. hrtimer_get_expires_tv64(timer))
  1129. break;
  1130. __run_hrtimer(timer);
  1131. }
  1132. spin_unlock(&cpu_base->lock);
  1133. }
  1134. }
  1135. /*
  1136. * Sleep related functions:
  1137. */
  1138. static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
  1139. {
  1140. struct hrtimer_sleeper *t =
  1141. container_of(timer, struct hrtimer_sleeper, timer);
  1142. struct task_struct *task = t->task;
  1143. t->task = NULL;
  1144. if (task)
  1145. wake_up_process(task);
  1146. return HRTIMER_NORESTART;
  1147. }
  1148. void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
  1149. {
  1150. sl->timer.function = hrtimer_wakeup;
  1151. sl->task = task;
  1152. }
  1153. static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
  1154. {
  1155. hrtimer_init_sleeper(t, current);
  1156. do {
  1157. set_current_state(TASK_INTERRUPTIBLE);
  1158. hrtimer_start_expires(&t->timer, mode);
  1159. if (!hrtimer_active(&t->timer))
  1160. t->task = NULL;
  1161. if (likely(t->task))
  1162. schedule();
  1163. hrtimer_cancel(&t->timer);
  1164. mode = HRTIMER_MODE_ABS;
  1165. } while (t->task && !signal_pending(current));
  1166. __set_current_state(TASK_RUNNING);
  1167. return t->task == NULL;
  1168. }
  1169. static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
  1170. {
  1171. struct timespec rmt;
  1172. ktime_t rem;
  1173. rem = hrtimer_expires_remaining(timer);
  1174. if (rem.tv64 <= 0)
  1175. return 0;
  1176. rmt = ktime_to_timespec(rem);
  1177. if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
  1178. return -EFAULT;
  1179. return 1;
  1180. }
  1181. long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
  1182. {
  1183. struct hrtimer_sleeper t;
  1184. struct timespec __user *rmtp;
  1185. int ret = 0;
  1186. hrtimer_init_on_stack(&t.timer, restart->nanosleep.index,
  1187. HRTIMER_MODE_ABS);
  1188. hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
  1189. if (do_nanosleep(&t, HRTIMER_MODE_ABS))
  1190. goto out;
  1191. rmtp = restart->nanosleep.rmtp;
  1192. if (rmtp) {
  1193. ret = update_rmtp(&t.timer, rmtp);
  1194. if (ret <= 0)
  1195. goto out;
  1196. }
  1197. /* The other values in restart are already filled in */
  1198. ret = -ERESTART_RESTARTBLOCK;
  1199. out:
  1200. destroy_hrtimer_on_stack(&t.timer);
  1201. return ret;
  1202. }
  1203. long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  1204. const enum hrtimer_mode mode, const clockid_t clockid)
  1205. {
  1206. struct restart_block *restart;
  1207. struct hrtimer_sleeper t;
  1208. int ret = 0;
  1209. unsigned long slack;
  1210. slack = current->timer_slack_ns;
  1211. if (rt_task(current))
  1212. slack = 0;
  1213. hrtimer_init_on_stack(&t.timer, clockid, mode);
  1214. hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
  1215. if (do_nanosleep(&t, mode))
  1216. goto out;
  1217. /* Absolute timers do not update the rmtp value and restart: */
  1218. if (mode == HRTIMER_MODE_ABS) {
  1219. ret = -ERESTARTNOHAND;
  1220. goto out;
  1221. }
  1222. if (rmtp) {
  1223. ret = update_rmtp(&t.timer, rmtp);
  1224. if (ret <= 0)
  1225. goto out;
  1226. }
  1227. restart = &current_thread_info()->restart_block;
  1228. restart->fn = hrtimer_nanosleep_restart;
  1229. restart->nanosleep.index = t.timer.base->index;
  1230. restart->nanosleep.rmtp = rmtp;
  1231. restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
  1232. ret = -ERESTART_RESTARTBLOCK;
  1233. out:
  1234. destroy_hrtimer_on_stack(&t.timer);
  1235. return ret;
  1236. }
  1237. asmlinkage long
  1238. sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp)
  1239. {
  1240. struct timespec tu;
  1241. if (copy_from_user(&tu, rqtp, sizeof(tu)))
  1242. return -EFAULT;
  1243. if (!timespec_valid(&tu))
  1244. return -EINVAL;
  1245. return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
  1246. }
  1247. /*
  1248. * Functions related to boot-time initialization:
  1249. */
  1250. static void __cpuinit init_hrtimers_cpu(int cpu)
  1251. {
  1252. struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
  1253. int i;
  1254. spin_lock_init(&cpu_base->lock);
  1255. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
  1256. cpu_base->clock_base[i].cpu_base = cpu_base;
  1257. hrtimer_init_hres(cpu_base);
  1258. }
  1259. #ifdef CONFIG_HOTPLUG_CPU
  1260. static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
  1261. struct hrtimer_clock_base *new_base)
  1262. {
  1263. struct hrtimer *timer;
  1264. struct rb_node *node;
  1265. while ((node = rb_first(&old_base->active))) {
  1266. timer = rb_entry(node, struct hrtimer, node);
  1267. BUG_ON(hrtimer_callback_running(timer));
  1268. debug_hrtimer_deactivate(timer);
  1269. /*
  1270. * Mark it as STATE_MIGRATE not INACTIVE otherwise the
  1271. * timer could be seen as !active and just vanish away
  1272. * under us on another CPU
  1273. */
  1274. __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
  1275. timer->base = new_base;
  1276. /*
  1277. * Enqueue the timers on the new cpu, but do not reprogram
  1278. * the timer as that would enable a deadlock between
  1279. * hrtimer_enqueue_reprogramm() running the timer and us still
  1280. * holding a nested base lock.
  1281. *
  1282. * Instead we tickle the hrtimer interrupt after the migration
  1283. * is done, which will run all expired timers and re-programm
  1284. * the timer device.
  1285. */
  1286. enqueue_hrtimer(timer, new_base, 0);
  1287. /* Clear the migration state bit */
  1288. timer->state &= ~HRTIMER_STATE_MIGRATE;
  1289. }
  1290. }
  1291. static int migrate_hrtimers(int scpu)
  1292. {
  1293. struct hrtimer_cpu_base *old_base, *new_base;
  1294. int dcpu, i;
  1295. BUG_ON(cpu_online(scpu));
  1296. old_base = &per_cpu(hrtimer_bases, scpu);
  1297. new_base = &get_cpu_var(hrtimer_bases);
  1298. dcpu = smp_processor_id();
  1299. tick_cancel_sched_timer(scpu);
  1300. /*
  1301. * The caller is globally serialized and nobody else
  1302. * takes two locks at once, deadlock is not possible.
  1303. */
  1304. spin_lock_irq(&new_base->lock);
  1305. spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
  1306. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  1307. migrate_hrtimer_list(&old_base->clock_base[i],
  1308. &new_base->clock_base[i]);
  1309. }
  1310. spin_unlock(&old_base->lock);
  1311. spin_unlock_irq(&new_base->lock);
  1312. put_cpu_var(hrtimer_bases);
  1313. return dcpu;
  1314. }
  1315. static void tickle_timers(void *arg)
  1316. {
  1317. hrtimer_peek_ahead_timers();
  1318. }
  1319. #endif /* CONFIG_HOTPLUG_CPU */
  1320. static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
  1321. unsigned long action, void *hcpu)
  1322. {
  1323. int dcpu, scpu = (long)hcpu;
  1324. switch (action) {
  1325. case CPU_UP_PREPARE:
  1326. case CPU_UP_PREPARE_FROZEN:
  1327. init_hrtimers_cpu(scpu);
  1328. break;
  1329. #ifdef CONFIG_HOTPLUG_CPU
  1330. case CPU_DEAD:
  1331. case CPU_DEAD_FROZEN:
  1332. clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
  1333. dcpu = migrate_hrtimers(scpu);
  1334. smp_call_function_single(dcpu, tickle_timers, NULL, 0);
  1335. break;
  1336. #endif
  1337. default:
  1338. break;
  1339. }
  1340. return NOTIFY_OK;
  1341. }
  1342. static struct notifier_block __cpuinitdata hrtimers_nb = {
  1343. .notifier_call = hrtimer_cpu_notify,
  1344. };
  1345. void __init hrtimers_init(void)
  1346. {
  1347. hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
  1348. (void *)(long)smp_processor_id());
  1349. register_cpu_notifier(&hrtimers_nb);
  1350. }
  1351. /**
  1352. * schedule_hrtimeout_range - sleep until timeout
  1353. * @expires: timeout value (ktime_t)
  1354. * @delta: slack in expires timeout (ktime_t)
  1355. * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
  1356. *
  1357. * Make the current task sleep until the given expiry time has
  1358. * elapsed. The routine will return immediately unless
  1359. * the current task state has been set (see set_current_state()).
  1360. *
  1361. * The @delta argument gives the kernel the freedom to schedule the
  1362. * actual wakeup to a time that is both power and performance friendly.
  1363. * The kernel give the normal best effort behavior for "@expires+@delta",
  1364. * but may decide to fire the timer earlier, but no earlier than @expires.
  1365. *
  1366. * You can set the task state as follows -
  1367. *
  1368. * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
  1369. * pass before the routine returns.
  1370. *
  1371. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  1372. * delivered to the current task.
  1373. *
  1374. * The current task state is guaranteed to be TASK_RUNNING when this
  1375. * routine returns.
  1376. *
  1377. * Returns 0 when the timer has expired otherwise -EINTR
  1378. */
  1379. int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
  1380. const enum hrtimer_mode mode)
  1381. {
  1382. struct hrtimer_sleeper t;
  1383. /*
  1384. * Optimize when a zero timeout value is given. It does not
  1385. * matter whether this is an absolute or a relative time.
  1386. */
  1387. if (expires && !expires->tv64) {
  1388. __set_current_state(TASK_RUNNING);
  1389. return 0;
  1390. }
  1391. /*
  1392. * A NULL parameter means "inifinte"
  1393. */
  1394. if (!expires) {
  1395. schedule();
  1396. __set_current_state(TASK_RUNNING);
  1397. return -EINTR;
  1398. }
  1399. hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, mode);
  1400. hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
  1401. hrtimer_init_sleeper(&t, current);
  1402. hrtimer_start_expires(&t.timer, mode);
  1403. if (!hrtimer_active(&t.timer))
  1404. t.task = NULL;
  1405. if (likely(t.task))
  1406. schedule();
  1407. hrtimer_cancel(&t.timer);
  1408. destroy_hrtimer_on_stack(&t.timer);
  1409. __set_current_state(TASK_RUNNING);
  1410. return !t.task ? 0 : -EINTR;
  1411. }
  1412. EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
  1413. /**
  1414. * schedule_hrtimeout - sleep until timeout
  1415. * @expires: timeout value (ktime_t)
  1416. * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
  1417. *
  1418. * Make the current task sleep until the given expiry time has
  1419. * elapsed. The routine will return immediately unless
  1420. * the current task state has been set (see set_current_state()).
  1421. *
  1422. * You can set the task state as follows -
  1423. *
  1424. * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
  1425. * pass before the routine returns.
  1426. *
  1427. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  1428. * delivered to the current task.
  1429. *
  1430. * The current task state is guaranteed to be TASK_RUNNING when this
  1431. * routine returns.
  1432. *
  1433. * Returns 0 when the timer has expired otherwise -EINTR
  1434. */
  1435. int __sched schedule_hrtimeout(ktime_t *expires,
  1436. const enum hrtimer_mode mode)
  1437. {
  1438. return schedule_hrtimeout_range(expires, 0, mode);
  1439. }
  1440. EXPORT_SYMBOL_GPL(schedule_hrtimeout);