hrtimer.c 47 KB

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