hrtimer.c 47 KB

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