hrtimer.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844
  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 <linux/sched.h>
  46. #include <linux/timer.h>
  47. #include <asm/uaccess.h>
  48. #include <trace/events/timer.h>
  49. /*
  50. * The timer bases:
  51. *
  52. * There are more clockids then hrtimer bases. Thus, we index
  53. * into the timer bases by the hrtimer_base_type enum. When trying
  54. * to reach a base using a clockid, hrtimer_clockid_to_base()
  55. * is used to convert from clockid to the proper hrtimer_base_type.
  56. */
  57. DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
  58. {
  59. .clock_base =
  60. {
  61. {
  62. .index = CLOCK_REALTIME,
  63. .get_time = &ktime_get_real,
  64. .resolution = KTIME_LOW_RES,
  65. },
  66. {
  67. .index = CLOCK_MONOTONIC,
  68. .get_time = &ktime_get,
  69. .resolution = KTIME_LOW_RES,
  70. },
  71. }
  72. };
  73. static int hrtimer_clock_to_base_table[MAX_CLOCKS];
  74. static inline int hrtimer_clockid_to_base(clockid_t clock_id)
  75. {
  76. return hrtimer_clock_to_base_table[clock_id];
  77. }
  78. /*
  79. * Get the coarse grained time at the softirq based on xtime and
  80. * wall_to_monotonic.
  81. */
  82. static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
  83. {
  84. ktime_t xtim, tomono;
  85. struct timespec xts, tom;
  86. get_xtime_and_monotonic_offset(&xts, &tom);
  87. xtim = timespec_to_ktime(xts);
  88. tomono = timespec_to_ktime(tom);
  89. base->clock_base[HRTIMER_BASE_REALTIME].softirq_time = xtim;
  90. base->clock_base[HRTIMER_BASE_MONOTONIC].softirq_time =
  91. ktime_add(xtim, tomono);
  92. }
  93. /*
  94. * Functions and macros which are different for UP/SMP systems are kept in a
  95. * single place
  96. */
  97. #ifdef CONFIG_SMP
  98. /*
  99. * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
  100. * means that all timers which are tied to this base via timer->base are
  101. * locked, and the base itself is locked too.
  102. *
  103. * So __run_timers/migrate_timers can safely modify all timers which could
  104. * be found on the lists/queues.
  105. *
  106. * When the timer's base is locked, and the timer removed from list, it is
  107. * possible to set timer->base = NULL and drop the lock: the timer remains
  108. * locked.
  109. */
  110. static
  111. struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
  112. unsigned long *flags)
  113. {
  114. struct hrtimer_clock_base *base;
  115. for (;;) {
  116. base = timer->base;
  117. if (likely(base != NULL)) {
  118. raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
  119. if (likely(base == timer->base))
  120. return base;
  121. /* The timer has migrated to another CPU: */
  122. raw_spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
  123. }
  124. cpu_relax();
  125. }
  126. }
  127. /*
  128. * Get the preferred target CPU for NOHZ
  129. */
  130. static int hrtimer_get_target(int this_cpu, int pinned)
  131. {
  132. #ifdef CONFIG_NO_HZ
  133. if (!pinned && get_sysctl_timer_migration() && idle_cpu(this_cpu))
  134. return get_nohz_timer_target();
  135. #endif
  136. return this_cpu;
  137. }
  138. /*
  139. * With HIGHRES=y we do not migrate the timer when it is expiring
  140. * before the next event on the target cpu because we cannot reprogram
  141. * the target cpu hardware and we would cause it to fire late.
  142. *
  143. * Called with cpu_base->lock of target cpu held.
  144. */
  145. static int
  146. hrtimer_check_target(struct hrtimer *timer, struct hrtimer_clock_base *new_base)
  147. {
  148. #ifdef CONFIG_HIGH_RES_TIMERS
  149. ktime_t expires;
  150. if (!new_base->cpu_base->hres_active)
  151. return 0;
  152. expires = ktime_sub(hrtimer_get_expires(timer), new_base->offset);
  153. return expires.tv64 <= new_base->cpu_base->expires_next.tv64;
  154. #else
  155. return 0;
  156. #endif
  157. }
  158. /*
  159. * Switch the timer base to the current CPU when possible.
  160. */
  161. static inline struct hrtimer_clock_base *
  162. switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
  163. int pinned)
  164. {
  165. struct hrtimer_clock_base *new_base;
  166. struct hrtimer_cpu_base *new_cpu_base;
  167. int this_cpu = smp_processor_id();
  168. int cpu = hrtimer_get_target(this_cpu, pinned);
  169. int basenum = hrtimer_clockid_to_base(base->index);
  170. again:
  171. new_cpu_base = &per_cpu(hrtimer_bases, cpu);
  172. new_base = &new_cpu_base->clock_base[basenum];
  173. if (base != new_base) {
  174. /*
  175. * We are trying to move timer to new_base.
  176. * However we can't change timer's base while it is running,
  177. * so we keep it on the same CPU. No hassle vs. reprogramming
  178. * the event source in the high resolution case. The softirq
  179. * code will take care of this when the timer function has
  180. * completed. There is no conflict as we hold the lock until
  181. * the timer is enqueued.
  182. */
  183. if (unlikely(hrtimer_callback_running(timer)))
  184. return base;
  185. /* See the comment in lock_timer_base() */
  186. timer->base = NULL;
  187. raw_spin_unlock(&base->cpu_base->lock);
  188. raw_spin_lock(&new_base->cpu_base->lock);
  189. if (cpu != this_cpu && hrtimer_check_target(timer, new_base)) {
  190. cpu = this_cpu;
  191. raw_spin_unlock(&new_base->cpu_base->lock);
  192. raw_spin_lock(&base->cpu_base->lock);
  193. timer->base = base;
  194. goto again;
  195. }
  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. raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
  206. return base;
  207. }
  208. # define switch_hrtimer_base(t, b, p) (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. EXPORT_SYMBOL_GPL(ktime_add_safe);
  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. EXPORT_SYMBOL_GPL(hrtimer_init_on_stack);
  370. void destroy_hrtimer_on_stack(struct hrtimer *timer)
  371. {
  372. debug_object_free(timer, &hrtimer_debug_descr);
  373. }
  374. #else
  375. static inline void debug_hrtimer_init(struct hrtimer *timer) { }
  376. static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
  377. static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
  378. #endif
  379. static inline void
  380. debug_init(struct hrtimer *timer, clockid_t clockid,
  381. enum hrtimer_mode mode)
  382. {
  383. debug_hrtimer_init(timer);
  384. trace_hrtimer_init(timer, clockid, mode);
  385. }
  386. static inline void debug_activate(struct hrtimer *timer)
  387. {
  388. debug_hrtimer_activate(timer);
  389. trace_hrtimer_start(timer);
  390. }
  391. static inline void debug_deactivate(struct hrtimer *timer)
  392. {
  393. debug_hrtimer_deactivate(timer);
  394. trace_hrtimer_cancel(timer);
  395. }
  396. /* High resolution timer related functions */
  397. #ifdef CONFIG_HIGH_RES_TIMERS
  398. /*
  399. * High resolution timer enabled ?
  400. */
  401. static int hrtimer_hres_enabled __read_mostly = 1;
  402. /*
  403. * Enable / Disable high resolution mode
  404. */
  405. static int __init setup_hrtimer_hres(char *str)
  406. {
  407. if (!strcmp(str, "off"))
  408. hrtimer_hres_enabled = 0;
  409. else if (!strcmp(str, "on"))
  410. hrtimer_hres_enabled = 1;
  411. else
  412. return 0;
  413. return 1;
  414. }
  415. __setup("highres=", setup_hrtimer_hres);
  416. /*
  417. * hrtimer_high_res_enabled - query, if the highres mode is enabled
  418. */
  419. static inline int hrtimer_is_hres_enabled(void)
  420. {
  421. return hrtimer_hres_enabled;
  422. }
  423. /*
  424. * Is the high resolution mode active ?
  425. */
  426. static inline int hrtimer_hres_active(void)
  427. {
  428. return __this_cpu_read(hrtimer_bases.hres_active);
  429. }
  430. /*
  431. * Reprogram the event source with checking both queues for the
  432. * next event
  433. * Called with interrupts disabled and base->lock held
  434. */
  435. static void
  436. hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
  437. {
  438. int i;
  439. struct hrtimer_clock_base *base = cpu_base->clock_base;
  440. ktime_t expires, expires_next;
  441. expires_next.tv64 = KTIME_MAX;
  442. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
  443. struct hrtimer *timer;
  444. struct timerqueue_node *next;
  445. next = timerqueue_getnext(&base->active);
  446. if (!next)
  447. continue;
  448. timer = container_of(next, struct hrtimer, node);
  449. expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
  450. /*
  451. * clock_was_set() has changed base->offset so the
  452. * result might be negative. Fix it up to prevent a
  453. * false positive in clockevents_program_event()
  454. */
  455. if (expires.tv64 < 0)
  456. expires.tv64 = 0;
  457. if (expires.tv64 < expires_next.tv64)
  458. expires_next = expires;
  459. }
  460. if (skip_equal && expires_next.tv64 == cpu_base->expires_next.tv64)
  461. return;
  462. cpu_base->expires_next.tv64 = expires_next.tv64;
  463. if (cpu_base->expires_next.tv64 != KTIME_MAX)
  464. tick_program_event(cpu_base->expires_next, 1);
  465. }
  466. /*
  467. * Shared reprogramming for clock_realtime and clock_monotonic
  468. *
  469. * When a timer is enqueued and expires earlier than the already enqueued
  470. * timers, we have to check, whether it expires earlier than the timer for
  471. * which the clock event device was armed.
  472. *
  473. * Called with interrupts disabled and base->cpu_base.lock held
  474. */
  475. static int hrtimer_reprogram(struct hrtimer *timer,
  476. struct hrtimer_clock_base *base)
  477. {
  478. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  479. ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
  480. int res;
  481. WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
  482. /*
  483. * When the callback is running, we do not reprogram the clock event
  484. * device. The timer callback is either running on a different CPU or
  485. * the callback is executed in the hrtimer_interrupt context. The
  486. * reprogramming is handled either by the softirq, which called the
  487. * callback or at the end of the hrtimer_interrupt.
  488. */
  489. if (hrtimer_callback_running(timer))
  490. return 0;
  491. /*
  492. * CLOCK_REALTIME timer might be requested with an absolute
  493. * expiry time which is less than base->offset. Nothing wrong
  494. * about that, just avoid to call into the tick code, which
  495. * has now objections against negative expiry values.
  496. */
  497. if (expires.tv64 < 0)
  498. return -ETIME;
  499. if (expires.tv64 >= cpu_base->expires_next.tv64)
  500. return 0;
  501. /*
  502. * If a hang was detected in the last timer interrupt then we
  503. * do not schedule a timer which is earlier than the expiry
  504. * which we enforced in the hang detection. We want the system
  505. * to make progress.
  506. */
  507. if (cpu_base->hang_detected)
  508. return 0;
  509. /*
  510. * Clockevents returns -ETIME, when the event was in the past.
  511. */
  512. res = tick_program_event(expires, 0);
  513. if (!IS_ERR_VALUE(res))
  514. cpu_base->expires_next = expires;
  515. return res;
  516. }
  517. /*
  518. * Retrigger next event is called after clock was set
  519. *
  520. * Called with interrupts disabled via on_each_cpu()
  521. */
  522. static void retrigger_next_event(void *arg)
  523. {
  524. struct hrtimer_cpu_base *base;
  525. struct timespec realtime_offset, wtm;
  526. if (!hrtimer_hres_active())
  527. return;
  528. get_xtime_and_monotonic_offset(&realtime_offset, &wtm);
  529. set_normalized_timespec(&realtime_offset, -wtm.tv_sec, -wtm.tv_nsec);
  530. base = &__get_cpu_var(hrtimer_bases);
  531. /* Adjust CLOCK_REALTIME offset */
  532. raw_spin_lock(&base->lock);
  533. base->clock_base[HRTIMER_BASE_REALTIME].offset =
  534. timespec_to_ktime(realtime_offset);
  535. hrtimer_force_reprogram(base, 0);
  536. raw_spin_unlock(&base->lock);
  537. }
  538. /*
  539. * Clock realtime was set
  540. *
  541. * Change the offset of the realtime clock vs. the monotonic
  542. * clock.
  543. *
  544. * We might have to reprogram the high resolution timer interrupt. On
  545. * SMP we call the architecture specific code to retrigger _all_ high
  546. * resolution timer interrupts. On UP we just disable interrupts and
  547. * call the high resolution interrupt code.
  548. */
  549. void clock_was_set(void)
  550. {
  551. /* Retrigger the CPU local events everywhere */
  552. on_each_cpu(retrigger_next_event, NULL, 1);
  553. }
  554. /*
  555. * During resume we might have to reprogram the high resolution timer
  556. * interrupt (on the local CPU):
  557. */
  558. void hres_timers_resume(void)
  559. {
  560. WARN_ONCE(!irqs_disabled(),
  561. KERN_INFO "hres_timers_resume() called with IRQs enabled!");
  562. retrigger_next_event(NULL);
  563. }
  564. /*
  565. * Initialize the high resolution related parts of cpu_base
  566. */
  567. static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
  568. {
  569. base->expires_next.tv64 = KTIME_MAX;
  570. base->hres_active = 0;
  571. }
  572. /*
  573. * Initialize the high resolution related parts of a hrtimer
  574. */
  575. static inline void hrtimer_init_timer_hres(struct hrtimer *timer)
  576. {
  577. }
  578. /*
  579. * When High resolution timers are active, try to reprogram. Note, that in case
  580. * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
  581. * check happens. The timer gets enqueued into the rbtree. The reprogramming
  582. * and expiry check is done in the hrtimer_interrupt or in the softirq.
  583. */
  584. static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
  585. struct hrtimer_clock_base *base,
  586. int wakeup)
  587. {
  588. if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
  589. if (wakeup) {
  590. raw_spin_unlock(&base->cpu_base->lock);
  591. raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  592. raw_spin_lock(&base->cpu_base->lock);
  593. } else
  594. __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  595. return 1;
  596. }
  597. return 0;
  598. }
  599. /*
  600. * Switch to high resolution mode
  601. */
  602. static int hrtimer_switch_to_hres(void)
  603. {
  604. int cpu = smp_processor_id();
  605. struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
  606. unsigned long flags;
  607. if (base->hres_active)
  608. return 1;
  609. local_irq_save(flags);
  610. if (tick_init_highres()) {
  611. local_irq_restore(flags);
  612. printk(KERN_WARNING "Could not switch to high resolution "
  613. "mode on CPU %d\n", cpu);
  614. return 0;
  615. }
  616. base->hres_active = 1;
  617. base->clock_base[HRTIMER_BASE_REALTIME].resolution = KTIME_HIGH_RES;
  618. base->clock_base[HRTIMER_BASE_MONOTONIC].resolution = KTIME_HIGH_RES;
  619. tick_setup_sched_timer();
  620. /* "Retrigger" the interrupt to get things going */
  621. retrigger_next_event(NULL);
  622. local_irq_restore(flags);
  623. return 1;
  624. }
  625. #else
  626. static inline int hrtimer_hres_active(void) { return 0; }
  627. static inline int hrtimer_is_hres_enabled(void) { return 0; }
  628. static inline int hrtimer_switch_to_hres(void) { return 0; }
  629. static inline void
  630. hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
  631. static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
  632. struct hrtimer_clock_base *base,
  633. int wakeup)
  634. {
  635. return 0;
  636. }
  637. static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
  638. static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { }
  639. #endif /* CONFIG_HIGH_RES_TIMERS */
  640. static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
  641. {
  642. #ifdef CONFIG_TIMER_STATS
  643. if (timer->start_site)
  644. return;
  645. timer->start_site = __builtin_return_address(0);
  646. memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
  647. timer->start_pid = current->pid;
  648. #endif
  649. }
  650. static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
  651. {
  652. #ifdef CONFIG_TIMER_STATS
  653. timer->start_site = NULL;
  654. #endif
  655. }
  656. static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
  657. {
  658. #ifdef CONFIG_TIMER_STATS
  659. if (likely(!timer_stats_active))
  660. return;
  661. timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
  662. timer->function, timer->start_comm, 0);
  663. #endif
  664. }
  665. /*
  666. * Counterpart to lock_hrtimer_base above:
  667. */
  668. static inline
  669. void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
  670. {
  671. raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
  672. }
  673. /**
  674. * hrtimer_forward - forward the timer expiry
  675. * @timer: hrtimer to forward
  676. * @now: forward past this time
  677. * @interval: the interval to forward
  678. *
  679. * Forward the timer expiry so it will expire in the future.
  680. * Returns the number of overruns.
  681. */
  682. u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
  683. {
  684. u64 orun = 1;
  685. ktime_t delta;
  686. delta = ktime_sub(now, hrtimer_get_expires(timer));
  687. if (delta.tv64 < 0)
  688. return 0;
  689. if (interval.tv64 < timer->base->resolution.tv64)
  690. interval.tv64 = timer->base->resolution.tv64;
  691. if (unlikely(delta.tv64 >= interval.tv64)) {
  692. s64 incr = ktime_to_ns(interval);
  693. orun = ktime_divns(delta, incr);
  694. hrtimer_add_expires_ns(timer, incr * orun);
  695. if (hrtimer_get_expires_tv64(timer) > now.tv64)
  696. return orun;
  697. /*
  698. * This (and the ktime_add() below) is the
  699. * correction for exact:
  700. */
  701. orun++;
  702. }
  703. hrtimer_add_expires(timer, interval);
  704. return orun;
  705. }
  706. EXPORT_SYMBOL_GPL(hrtimer_forward);
  707. /*
  708. * enqueue_hrtimer - internal function to (re)start a timer
  709. *
  710. * The timer is inserted in expiry order. Insertion into the
  711. * red black tree is O(log(n)). Must hold the base lock.
  712. *
  713. * Returns 1 when the new timer is the leftmost timer in the tree.
  714. */
  715. static int enqueue_hrtimer(struct hrtimer *timer,
  716. struct hrtimer_clock_base *base)
  717. {
  718. debug_activate(timer);
  719. timerqueue_add(&base->active, &timer->node);
  720. /*
  721. * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
  722. * state of a possibly running callback.
  723. */
  724. timer->state |= HRTIMER_STATE_ENQUEUED;
  725. return (&timer->node == base->active.next);
  726. }
  727. /*
  728. * __remove_hrtimer - internal function to remove a timer
  729. *
  730. * Caller must hold the base lock.
  731. *
  732. * High resolution timer mode reprograms the clock event device when the
  733. * timer is the one which expires next. The caller can disable this by setting
  734. * reprogram to zero. This is useful, when the context does a reprogramming
  735. * anyway (e.g. timer interrupt)
  736. */
  737. static void __remove_hrtimer(struct hrtimer *timer,
  738. struct hrtimer_clock_base *base,
  739. unsigned long newstate, int reprogram)
  740. {
  741. if (!(timer->state & HRTIMER_STATE_ENQUEUED))
  742. goto out;
  743. if (&timer->node == timerqueue_getnext(&base->active)) {
  744. #ifdef CONFIG_HIGH_RES_TIMERS
  745. /* Reprogram the clock event device. if enabled */
  746. if (reprogram && hrtimer_hres_active()) {
  747. ktime_t expires;
  748. expires = ktime_sub(hrtimer_get_expires(timer),
  749. base->offset);
  750. if (base->cpu_base->expires_next.tv64 == expires.tv64)
  751. hrtimer_force_reprogram(base->cpu_base, 1);
  752. }
  753. #endif
  754. }
  755. timerqueue_del(&base->active, &timer->node);
  756. out:
  757. timer->state = newstate;
  758. }
  759. /*
  760. * remove hrtimer, called with base lock held
  761. */
  762. static inline int
  763. remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
  764. {
  765. if (hrtimer_is_queued(timer)) {
  766. unsigned long state;
  767. int reprogram;
  768. /*
  769. * Remove the timer and force reprogramming when high
  770. * resolution mode is active and the timer is on the current
  771. * CPU. If we remove a timer on another CPU, reprogramming is
  772. * skipped. The interrupt event on this CPU is fired and
  773. * reprogramming happens in the interrupt handler. This is a
  774. * rare case and less expensive than a smp call.
  775. */
  776. debug_deactivate(timer);
  777. timer_stats_hrtimer_clear_start_info(timer);
  778. reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
  779. /*
  780. * We must preserve the CALLBACK state flag here,
  781. * otherwise we could move the timer base in
  782. * switch_hrtimer_base.
  783. */
  784. state = timer->state & HRTIMER_STATE_CALLBACK;
  785. __remove_hrtimer(timer, base, state, reprogram);
  786. return 1;
  787. }
  788. return 0;
  789. }
  790. int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
  791. unsigned long delta_ns, const enum hrtimer_mode mode,
  792. int wakeup)
  793. {
  794. struct hrtimer_clock_base *base, *new_base;
  795. unsigned long flags;
  796. int ret, leftmost;
  797. base = lock_hrtimer_base(timer, &flags);
  798. /* Remove an active timer from the queue: */
  799. ret = remove_hrtimer(timer, base);
  800. /* Switch the timer base, if necessary: */
  801. new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
  802. if (mode & HRTIMER_MODE_REL) {
  803. tim = ktime_add_safe(tim, new_base->get_time());
  804. /*
  805. * CONFIG_TIME_LOW_RES is a temporary way for architectures
  806. * to signal that they simply return xtime in
  807. * do_gettimeoffset(). In this case we want to round up by
  808. * resolution when starting a relative timer, to avoid short
  809. * timeouts. This will go away with the GTOD framework.
  810. */
  811. #ifdef CONFIG_TIME_LOW_RES
  812. tim = ktime_add_safe(tim, base->resolution);
  813. #endif
  814. }
  815. hrtimer_set_expires_range_ns(timer, tim, delta_ns);
  816. timer_stats_hrtimer_set_start_info(timer);
  817. leftmost = enqueue_hrtimer(timer, new_base);
  818. /*
  819. * Only allow reprogramming if the new base is on this CPU.
  820. * (it might still be on another CPU if the timer was pending)
  821. *
  822. * XXX send_remote_softirq() ?
  823. */
  824. if (leftmost && new_base->cpu_base == &__get_cpu_var(hrtimer_bases))
  825. hrtimer_enqueue_reprogram(timer, new_base, wakeup);
  826. unlock_hrtimer_base(timer, &flags);
  827. return ret;
  828. }
  829. /**
  830. * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
  831. * @timer: the timer to be added
  832. * @tim: expiry time
  833. * @delta_ns: "slack" range for the timer
  834. * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
  835. *
  836. * Returns:
  837. * 0 on success
  838. * 1 when the timer was active
  839. */
  840. int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
  841. unsigned long delta_ns, const enum hrtimer_mode mode)
  842. {
  843. return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1);
  844. }
  845. EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
  846. /**
  847. * hrtimer_start - (re)start an hrtimer on the current CPU
  848. * @timer: the timer to be added
  849. * @tim: expiry time
  850. * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
  851. *
  852. * Returns:
  853. * 0 on success
  854. * 1 when the timer was active
  855. */
  856. int
  857. hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
  858. {
  859. return __hrtimer_start_range_ns(timer, tim, 0, mode, 1);
  860. }
  861. EXPORT_SYMBOL_GPL(hrtimer_start);
  862. /**
  863. * hrtimer_try_to_cancel - try to deactivate a timer
  864. * @timer: hrtimer to stop
  865. *
  866. * Returns:
  867. * 0 when the timer was not active
  868. * 1 when the timer was active
  869. * -1 when the timer is currently excuting the callback function and
  870. * cannot be stopped
  871. */
  872. int hrtimer_try_to_cancel(struct hrtimer *timer)
  873. {
  874. struct hrtimer_clock_base *base;
  875. unsigned long flags;
  876. int ret = -1;
  877. base = lock_hrtimer_base(timer, &flags);
  878. if (!hrtimer_callback_running(timer))
  879. ret = remove_hrtimer(timer, base);
  880. unlock_hrtimer_base(timer, &flags);
  881. return ret;
  882. }
  883. EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
  884. /**
  885. * hrtimer_cancel - cancel a timer and wait for the handler to finish.
  886. * @timer: the timer to be cancelled
  887. *
  888. * Returns:
  889. * 0 when the timer was not active
  890. * 1 when the timer was active
  891. */
  892. int hrtimer_cancel(struct hrtimer *timer)
  893. {
  894. for (;;) {
  895. int ret = hrtimer_try_to_cancel(timer);
  896. if (ret >= 0)
  897. return ret;
  898. cpu_relax();
  899. }
  900. }
  901. EXPORT_SYMBOL_GPL(hrtimer_cancel);
  902. /**
  903. * hrtimer_get_remaining - get remaining time for the timer
  904. * @timer: the timer to read
  905. */
  906. ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
  907. {
  908. unsigned long flags;
  909. ktime_t rem;
  910. lock_hrtimer_base(timer, &flags);
  911. rem = hrtimer_expires_remaining(timer);
  912. unlock_hrtimer_base(timer, &flags);
  913. return rem;
  914. }
  915. EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
  916. #ifdef CONFIG_NO_HZ
  917. /**
  918. * hrtimer_get_next_event - get the time until next expiry event
  919. *
  920. * Returns the delta to the next expiry event or KTIME_MAX if no timer
  921. * is pending.
  922. */
  923. ktime_t hrtimer_get_next_event(void)
  924. {
  925. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  926. struct hrtimer_clock_base *base = cpu_base->clock_base;
  927. ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
  928. unsigned long flags;
  929. int i;
  930. raw_spin_lock_irqsave(&cpu_base->lock, flags);
  931. if (!hrtimer_hres_active()) {
  932. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
  933. struct hrtimer *timer;
  934. struct timerqueue_node *next;
  935. next = timerqueue_getnext(&base->active);
  936. if (!next)
  937. continue;
  938. timer = container_of(next, struct hrtimer, node);
  939. delta.tv64 = hrtimer_get_expires_tv64(timer);
  940. delta = ktime_sub(delta, base->get_time());
  941. if (delta.tv64 < mindelta.tv64)
  942. mindelta.tv64 = delta.tv64;
  943. }
  944. }
  945. raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
  946. if (mindelta.tv64 < 0)
  947. mindelta.tv64 = 0;
  948. return mindelta;
  949. }
  950. #endif
  951. static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  952. enum hrtimer_mode mode)
  953. {
  954. struct hrtimer_cpu_base *cpu_base;
  955. int base;
  956. memset(timer, 0, sizeof(struct hrtimer));
  957. cpu_base = &__raw_get_cpu_var(hrtimer_bases);
  958. if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
  959. clock_id = CLOCK_MONOTONIC;
  960. base = hrtimer_clockid_to_base(clock_id);
  961. timer->base = &cpu_base->clock_base[base];
  962. hrtimer_init_timer_hres(timer);
  963. timerqueue_init(&timer->node);
  964. #ifdef CONFIG_TIMER_STATS
  965. timer->start_site = NULL;
  966. timer->start_pid = -1;
  967. memset(timer->start_comm, 0, TASK_COMM_LEN);
  968. #endif
  969. }
  970. /**
  971. * hrtimer_init - initialize a timer to the given clock
  972. * @timer: the timer to be initialized
  973. * @clock_id: the clock to be used
  974. * @mode: timer mode abs/rel
  975. */
  976. void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  977. enum hrtimer_mode mode)
  978. {
  979. debug_init(timer, clock_id, mode);
  980. __hrtimer_init(timer, clock_id, mode);
  981. }
  982. EXPORT_SYMBOL_GPL(hrtimer_init);
  983. /**
  984. * hrtimer_get_res - get the timer resolution for a clock
  985. * @which_clock: which clock to query
  986. * @tp: pointer to timespec variable to store the resolution
  987. *
  988. * Store the resolution of the clock selected by @which_clock in the
  989. * variable pointed to by @tp.
  990. */
  991. int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
  992. {
  993. struct hrtimer_cpu_base *cpu_base;
  994. int base = hrtimer_clockid_to_base(which_clock);
  995. cpu_base = &__raw_get_cpu_var(hrtimer_bases);
  996. *tp = ktime_to_timespec(cpu_base->clock_base[base].resolution);
  997. return 0;
  998. }
  999. EXPORT_SYMBOL_GPL(hrtimer_get_res);
  1000. static void __run_hrtimer(struct hrtimer *timer, ktime_t *now)
  1001. {
  1002. struct hrtimer_clock_base *base = timer->base;
  1003. struct hrtimer_cpu_base *cpu_base = base->cpu_base;
  1004. enum hrtimer_restart (*fn)(struct hrtimer *);
  1005. int restart;
  1006. WARN_ON(!irqs_disabled());
  1007. debug_deactivate(timer);
  1008. __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
  1009. timer_stats_account_hrtimer(timer);
  1010. fn = timer->function;
  1011. /*
  1012. * Because we run timers from hardirq context, there is no chance
  1013. * they get migrated to another cpu, therefore its safe to unlock
  1014. * the timer base.
  1015. */
  1016. raw_spin_unlock(&cpu_base->lock);
  1017. trace_hrtimer_expire_entry(timer, now);
  1018. restart = fn(timer);
  1019. trace_hrtimer_expire_exit(timer);
  1020. raw_spin_lock(&cpu_base->lock);
  1021. /*
  1022. * Note: We clear the CALLBACK bit after enqueue_hrtimer and
  1023. * we do not reprogramm the event hardware. Happens either in
  1024. * hrtimer_start_range_ns() or in hrtimer_interrupt()
  1025. */
  1026. if (restart != HRTIMER_NORESTART) {
  1027. BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
  1028. enqueue_hrtimer(timer, base);
  1029. }
  1030. WARN_ON_ONCE(!(timer->state & HRTIMER_STATE_CALLBACK));
  1031. timer->state &= ~HRTIMER_STATE_CALLBACK;
  1032. }
  1033. #ifdef CONFIG_HIGH_RES_TIMERS
  1034. /*
  1035. * High resolution timer interrupt
  1036. * Called with interrupts disabled
  1037. */
  1038. void hrtimer_interrupt(struct clock_event_device *dev)
  1039. {
  1040. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  1041. struct hrtimer_clock_base *base;
  1042. ktime_t expires_next, now, entry_time, delta;
  1043. int i, retries = 0;
  1044. BUG_ON(!cpu_base->hres_active);
  1045. cpu_base->nr_events++;
  1046. dev->next_event.tv64 = KTIME_MAX;
  1047. entry_time = now = ktime_get();
  1048. retry:
  1049. expires_next.tv64 = KTIME_MAX;
  1050. raw_spin_lock(&cpu_base->lock);
  1051. /*
  1052. * We set expires_next to KTIME_MAX here with cpu_base->lock
  1053. * held to prevent that a timer is enqueued in our queue via
  1054. * the migration code. This does not affect enqueueing of
  1055. * timers which run their callback and need to be requeued on
  1056. * this CPU.
  1057. */
  1058. cpu_base->expires_next.tv64 = KTIME_MAX;
  1059. base = cpu_base->clock_base;
  1060. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  1061. ktime_t basenow;
  1062. struct timerqueue_node *node;
  1063. basenow = ktime_add(now, base->offset);
  1064. while ((node = timerqueue_getnext(&base->active))) {
  1065. struct hrtimer *timer;
  1066. timer = container_of(node, struct hrtimer, node);
  1067. /*
  1068. * The immediate goal for using the softexpires is
  1069. * minimizing wakeups, not running timers at the
  1070. * earliest interrupt after their soft expiration.
  1071. * This allows us to avoid using a Priority Search
  1072. * Tree, which can answer a stabbing querry for
  1073. * overlapping intervals and instead use the simple
  1074. * BST we already have.
  1075. * We don't add extra wakeups by delaying timers that
  1076. * are right-of a not yet expired timer, because that
  1077. * timer will have to trigger a wakeup anyway.
  1078. */
  1079. if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
  1080. ktime_t expires;
  1081. expires = ktime_sub(hrtimer_get_expires(timer),
  1082. base->offset);
  1083. if (expires.tv64 < expires_next.tv64)
  1084. expires_next = expires;
  1085. break;
  1086. }
  1087. __run_hrtimer(timer, &basenow);
  1088. }
  1089. base++;
  1090. }
  1091. /*
  1092. * Store the new expiry value so the migration code can verify
  1093. * against it.
  1094. */
  1095. cpu_base->expires_next = expires_next;
  1096. raw_spin_unlock(&cpu_base->lock);
  1097. /* Reprogramming necessary ? */
  1098. if (expires_next.tv64 == KTIME_MAX ||
  1099. !tick_program_event(expires_next, 0)) {
  1100. cpu_base->hang_detected = 0;
  1101. return;
  1102. }
  1103. /*
  1104. * The next timer was already expired due to:
  1105. * - tracing
  1106. * - long lasting callbacks
  1107. * - being scheduled away when running in a VM
  1108. *
  1109. * We need to prevent that we loop forever in the hrtimer
  1110. * interrupt routine. We give it 3 attempts to avoid
  1111. * overreacting on some spurious event.
  1112. */
  1113. now = ktime_get();
  1114. cpu_base->nr_retries++;
  1115. if (++retries < 3)
  1116. goto retry;
  1117. /*
  1118. * Give the system a chance to do something else than looping
  1119. * here. We stored the entry time, so we know exactly how long
  1120. * we spent here. We schedule the next event this amount of
  1121. * time away.
  1122. */
  1123. cpu_base->nr_hangs++;
  1124. cpu_base->hang_detected = 1;
  1125. delta = ktime_sub(now, entry_time);
  1126. if (delta.tv64 > cpu_base->max_hang_time.tv64)
  1127. cpu_base->max_hang_time = delta;
  1128. /*
  1129. * Limit it to a sensible value as we enforce a longer
  1130. * delay. Give the CPU at least 100ms to catch up.
  1131. */
  1132. if (delta.tv64 > 100 * NSEC_PER_MSEC)
  1133. expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC);
  1134. else
  1135. expires_next = ktime_add(now, delta);
  1136. tick_program_event(expires_next, 1);
  1137. printk_once(KERN_WARNING "hrtimer: interrupt took %llu ns\n",
  1138. ktime_to_ns(delta));
  1139. }
  1140. /*
  1141. * local version of hrtimer_peek_ahead_timers() called with interrupts
  1142. * disabled.
  1143. */
  1144. static void __hrtimer_peek_ahead_timers(void)
  1145. {
  1146. struct tick_device *td;
  1147. if (!hrtimer_hres_active())
  1148. return;
  1149. td = &__get_cpu_var(tick_cpu_device);
  1150. if (td && td->evtdev)
  1151. hrtimer_interrupt(td->evtdev);
  1152. }
  1153. /**
  1154. * hrtimer_peek_ahead_timers -- run soft-expired timers now
  1155. *
  1156. * hrtimer_peek_ahead_timers will peek at the timer queue of
  1157. * the current cpu and check if there are any timers for which
  1158. * the soft expires time has passed. If any such timers exist,
  1159. * they are run immediately and then removed from the timer queue.
  1160. *
  1161. */
  1162. void hrtimer_peek_ahead_timers(void)
  1163. {
  1164. unsigned long flags;
  1165. local_irq_save(flags);
  1166. __hrtimer_peek_ahead_timers();
  1167. local_irq_restore(flags);
  1168. }
  1169. static void run_hrtimer_softirq(struct softirq_action *h)
  1170. {
  1171. hrtimer_peek_ahead_timers();
  1172. }
  1173. #else /* CONFIG_HIGH_RES_TIMERS */
  1174. static inline void __hrtimer_peek_ahead_timers(void) { }
  1175. #endif /* !CONFIG_HIGH_RES_TIMERS */
  1176. /*
  1177. * Called from timer softirq every jiffy, expire hrtimers:
  1178. *
  1179. * For HRT its the fall back code to run the softirq in the timer
  1180. * softirq context in case the hrtimer initialization failed or has
  1181. * not been done yet.
  1182. */
  1183. void hrtimer_run_pending(void)
  1184. {
  1185. if (hrtimer_hres_active())
  1186. return;
  1187. /*
  1188. * This _is_ ugly: We have to check in the softirq context,
  1189. * whether we can switch to highres and / or nohz mode. The
  1190. * clocksource switch happens in the timer interrupt with
  1191. * xtime_lock held. Notification from there only sets the
  1192. * check bit in the tick_oneshot code, otherwise we might
  1193. * deadlock vs. xtime_lock.
  1194. */
  1195. if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
  1196. hrtimer_switch_to_hres();
  1197. }
  1198. /*
  1199. * Called from hardirq context every jiffy
  1200. */
  1201. void hrtimer_run_queues(void)
  1202. {
  1203. struct timerqueue_node *node;
  1204. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  1205. struct hrtimer_clock_base *base;
  1206. int index, gettime = 1;
  1207. if (hrtimer_hres_active())
  1208. return;
  1209. for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
  1210. base = &cpu_base->clock_base[index];
  1211. if (!timerqueue_getnext(&base->active))
  1212. continue;
  1213. if (gettime) {
  1214. hrtimer_get_softirq_time(cpu_base);
  1215. gettime = 0;
  1216. }
  1217. raw_spin_lock(&cpu_base->lock);
  1218. while ((node = timerqueue_getnext(&base->active))) {
  1219. struct hrtimer *timer;
  1220. timer = container_of(node, struct hrtimer, node);
  1221. if (base->softirq_time.tv64 <=
  1222. hrtimer_get_expires_tv64(timer))
  1223. break;
  1224. __run_hrtimer(timer, &base->softirq_time);
  1225. }
  1226. raw_spin_unlock(&cpu_base->lock);
  1227. }
  1228. }
  1229. /*
  1230. * Sleep related functions:
  1231. */
  1232. static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
  1233. {
  1234. struct hrtimer_sleeper *t =
  1235. container_of(timer, struct hrtimer_sleeper, timer);
  1236. struct task_struct *task = t->task;
  1237. t->task = NULL;
  1238. if (task)
  1239. wake_up_process(task);
  1240. return HRTIMER_NORESTART;
  1241. }
  1242. void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
  1243. {
  1244. sl->timer.function = hrtimer_wakeup;
  1245. sl->task = task;
  1246. }
  1247. EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
  1248. static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
  1249. {
  1250. hrtimer_init_sleeper(t, current);
  1251. do {
  1252. set_current_state(TASK_INTERRUPTIBLE);
  1253. hrtimer_start_expires(&t->timer, mode);
  1254. if (!hrtimer_active(&t->timer))
  1255. t->task = NULL;
  1256. if (likely(t->task))
  1257. schedule();
  1258. hrtimer_cancel(&t->timer);
  1259. mode = HRTIMER_MODE_ABS;
  1260. } while (t->task && !signal_pending(current));
  1261. __set_current_state(TASK_RUNNING);
  1262. return t->task == NULL;
  1263. }
  1264. static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
  1265. {
  1266. struct timespec rmt;
  1267. ktime_t rem;
  1268. rem = hrtimer_expires_remaining(timer);
  1269. if (rem.tv64 <= 0)
  1270. return 0;
  1271. rmt = ktime_to_timespec(rem);
  1272. if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
  1273. return -EFAULT;
  1274. return 1;
  1275. }
  1276. long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
  1277. {
  1278. struct hrtimer_sleeper t;
  1279. struct timespec __user *rmtp;
  1280. int ret = 0;
  1281. hrtimer_init_on_stack(&t.timer, restart->nanosleep.index,
  1282. HRTIMER_MODE_ABS);
  1283. hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
  1284. if (do_nanosleep(&t, HRTIMER_MODE_ABS))
  1285. goto out;
  1286. rmtp = restart->nanosleep.rmtp;
  1287. if (rmtp) {
  1288. ret = update_rmtp(&t.timer, rmtp);
  1289. if (ret <= 0)
  1290. goto out;
  1291. }
  1292. /* The other values in restart are already filled in */
  1293. ret = -ERESTART_RESTARTBLOCK;
  1294. out:
  1295. destroy_hrtimer_on_stack(&t.timer);
  1296. return ret;
  1297. }
  1298. long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  1299. const enum hrtimer_mode mode, const clockid_t clockid)
  1300. {
  1301. struct restart_block *restart;
  1302. struct hrtimer_sleeper t;
  1303. int ret = 0;
  1304. unsigned long slack;
  1305. slack = current->timer_slack_ns;
  1306. if (rt_task(current))
  1307. slack = 0;
  1308. hrtimer_init_on_stack(&t.timer, clockid, mode);
  1309. hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
  1310. if (do_nanosleep(&t, mode))
  1311. goto out;
  1312. /* Absolute timers do not update the rmtp value and restart: */
  1313. if (mode == HRTIMER_MODE_ABS) {
  1314. ret = -ERESTARTNOHAND;
  1315. goto out;
  1316. }
  1317. if (rmtp) {
  1318. ret = update_rmtp(&t.timer, rmtp);
  1319. if (ret <= 0)
  1320. goto out;
  1321. }
  1322. restart = &current_thread_info()->restart_block;
  1323. restart->fn = hrtimer_nanosleep_restart;
  1324. restart->nanosleep.index = t.timer.base->index;
  1325. restart->nanosleep.rmtp = rmtp;
  1326. restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
  1327. ret = -ERESTART_RESTARTBLOCK;
  1328. out:
  1329. destroy_hrtimer_on_stack(&t.timer);
  1330. return ret;
  1331. }
  1332. SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
  1333. struct timespec __user *, rmtp)
  1334. {
  1335. struct timespec tu;
  1336. if (copy_from_user(&tu, rqtp, sizeof(tu)))
  1337. return -EFAULT;
  1338. if (!timespec_valid(&tu))
  1339. return -EINVAL;
  1340. return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
  1341. }
  1342. /*
  1343. * Functions related to boot-time initialization:
  1344. */
  1345. static void __cpuinit init_hrtimers_cpu(int cpu)
  1346. {
  1347. struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
  1348. int i;
  1349. raw_spin_lock_init(&cpu_base->lock);
  1350. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  1351. cpu_base->clock_base[i].cpu_base = cpu_base;
  1352. timerqueue_init_head(&cpu_base->clock_base[i].active);
  1353. }
  1354. hrtimer_init_hres(cpu_base);
  1355. }
  1356. #ifdef CONFIG_HOTPLUG_CPU
  1357. static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
  1358. struct hrtimer_clock_base *new_base)
  1359. {
  1360. struct hrtimer *timer;
  1361. struct timerqueue_node *node;
  1362. while ((node = timerqueue_getnext(&old_base->active))) {
  1363. timer = container_of(node, struct hrtimer, node);
  1364. BUG_ON(hrtimer_callback_running(timer));
  1365. debug_deactivate(timer);
  1366. /*
  1367. * Mark it as STATE_MIGRATE not INACTIVE otherwise the
  1368. * timer could be seen as !active and just vanish away
  1369. * under us on another CPU
  1370. */
  1371. __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
  1372. timer->base = new_base;
  1373. /*
  1374. * Enqueue the timers on the new cpu. This does not
  1375. * reprogram the event device in case the timer
  1376. * expires before the earliest on this CPU, but we run
  1377. * hrtimer_interrupt after we migrated everything to
  1378. * sort out already expired timers and reprogram the
  1379. * event device.
  1380. */
  1381. enqueue_hrtimer(timer, new_base);
  1382. /* Clear the migration state bit */
  1383. timer->state &= ~HRTIMER_STATE_MIGRATE;
  1384. }
  1385. }
  1386. static void migrate_hrtimers(int scpu)
  1387. {
  1388. struct hrtimer_cpu_base *old_base, *new_base;
  1389. int i;
  1390. BUG_ON(cpu_online(scpu));
  1391. tick_cancel_sched_timer(scpu);
  1392. local_irq_disable();
  1393. old_base = &per_cpu(hrtimer_bases, scpu);
  1394. new_base = &__get_cpu_var(hrtimer_bases);
  1395. /*
  1396. * The caller is globally serialized and nobody else
  1397. * takes two locks at once, deadlock is not possible.
  1398. */
  1399. raw_spin_lock(&new_base->lock);
  1400. raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
  1401. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  1402. migrate_hrtimer_list(&old_base->clock_base[i],
  1403. &new_base->clock_base[i]);
  1404. }
  1405. raw_spin_unlock(&old_base->lock);
  1406. raw_spin_unlock(&new_base->lock);
  1407. /* Check, if we got expired work to do */
  1408. __hrtimer_peek_ahead_timers();
  1409. local_irq_enable();
  1410. }
  1411. #endif /* CONFIG_HOTPLUG_CPU */
  1412. static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
  1413. unsigned long action, void *hcpu)
  1414. {
  1415. int scpu = (long)hcpu;
  1416. switch (action) {
  1417. case CPU_UP_PREPARE:
  1418. case CPU_UP_PREPARE_FROZEN:
  1419. init_hrtimers_cpu(scpu);
  1420. break;
  1421. #ifdef CONFIG_HOTPLUG_CPU
  1422. case CPU_DYING:
  1423. case CPU_DYING_FROZEN:
  1424. clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu);
  1425. break;
  1426. case CPU_DEAD:
  1427. case CPU_DEAD_FROZEN:
  1428. {
  1429. clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
  1430. migrate_hrtimers(scpu);
  1431. break;
  1432. }
  1433. #endif
  1434. default:
  1435. break;
  1436. }
  1437. return NOTIFY_OK;
  1438. }
  1439. static struct notifier_block __cpuinitdata hrtimers_nb = {
  1440. .notifier_call = hrtimer_cpu_notify,
  1441. };
  1442. void __init hrtimers_init(void)
  1443. {
  1444. hrtimer_clock_to_base_table[CLOCK_REALTIME] = HRTIMER_BASE_REALTIME;
  1445. hrtimer_clock_to_base_table[CLOCK_MONOTONIC] = HRTIMER_BASE_MONOTONIC;
  1446. hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
  1447. (void *)(long)smp_processor_id());
  1448. register_cpu_notifier(&hrtimers_nb);
  1449. #ifdef CONFIG_HIGH_RES_TIMERS
  1450. open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
  1451. #endif
  1452. }
  1453. /**
  1454. * schedule_hrtimeout_range_clock - sleep until timeout
  1455. * @expires: timeout value (ktime_t)
  1456. * @delta: slack in expires timeout (ktime_t)
  1457. * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
  1458. * @clock: timer clock, CLOCK_MONOTONIC or CLOCK_REALTIME
  1459. */
  1460. int __sched
  1461. schedule_hrtimeout_range_clock(ktime_t *expires, unsigned long delta,
  1462. const enum hrtimer_mode mode, int clock)
  1463. {
  1464. struct hrtimer_sleeper t;
  1465. /*
  1466. * Optimize when a zero timeout value is given. It does not
  1467. * matter whether this is an absolute or a relative time.
  1468. */
  1469. if (expires && !expires->tv64) {
  1470. __set_current_state(TASK_RUNNING);
  1471. return 0;
  1472. }
  1473. /*
  1474. * A NULL parameter means "infinite"
  1475. */
  1476. if (!expires) {
  1477. schedule();
  1478. __set_current_state(TASK_RUNNING);
  1479. return -EINTR;
  1480. }
  1481. hrtimer_init_on_stack(&t.timer, clock, mode);
  1482. hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
  1483. hrtimer_init_sleeper(&t, current);
  1484. hrtimer_start_expires(&t.timer, mode);
  1485. if (!hrtimer_active(&t.timer))
  1486. t.task = NULL;
  1487. if (likely(t.task))
  1488. schedule();
  1489. hrtimer_cancel(&t.timer);
  1490. destroy_hrtimer_on_stack(&t.timer);
  1491. __set_current_state(TASK_RUNNING);
  1492. return !t.task ? 0 : -EINTR;
  1493. }
  1494. /**
  1495. * schedule_hrtimeout_range - sleep until timeout
  1496. * @expires: timeout value (ktime_t)
  1497. * @delta: slack in expires timeout (ktime_t)
  1498. * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
  1499. *
  1500. * Make the current task sleep until the given expiry time has
  1501. * elapsed. The routine will return immediately unless
  1502. * the current task state has been set (see set_current_state()).
  1503. *
  1504. * The @delta argument gives the kernel the freedom to schedule the
  1505. * actual wakeup to a time that is both power and performance friendly.
  1506. * The kernel give the normal best effort behavior for "@expires+@delta",
  1507. * but may decide to fire the timer earlier, but no earlier than @expires.
  1508. *
  1509. * You can set the task state as follows -
  1510. *
  1511. * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
  1512. * pass before the routine returns.
  1513. *
  1514. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  1515. * delivered to the current task.
  1516. *
  1517. * The current task state is guaranteed to be TASK_RUNNING when this
  1518. * routine returns.
  1519. *
  1520. * Returns 0 when the timer has expired otherwise -EINTR
  1521. */
  1522. int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
  1523. const enum hrtimer_mode mode)
  1524. {
  1525. return schedule_hrtimeout_range_clock(expires, delta, mode,
  1526. CLOCK_MONOTONIC);
  1527. }
  1528. EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
  1529. /**
  1530. * schedule_hrtimeout - sleep until timeout
  1531. * @expires: timeout value (ktime_t)
  1532. * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
  1533. *
  1534. * Make the current task sleep until the given expiry time has
  1535. * elapsed. The routine will return immediately unless
  1536. * the current task state has been set (see set_current_state()).
  1537. *
  1538. * You can set the task state as follows -
  1539. *
  1540. * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
  1541. * pass before the routine returns.
  1542. *
  1543. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  1544. * delivered to the current task.
  1545. *
  1546. * The current task state is guaranteed to be TASK_RUNNING when this
  1547. * routine returns.
  1548. *
  1549. * Returns 0 when the timer has expired otherwise -EINTR
  1550. */
  1551. int __sched schedule_hrtimeout(ktime_t *expires,
  1552. const enum hrtimer_mode mode)
  1553. {
  1554. return schedule_hrtimeout_range(expires, 0, mode);
  1555. }
  1556. EXPORT_SYMBOL_GPL(schedule_hrtimeout);