hrtimer.c 43 KB

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