hrtimer.c 48 KB

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