hrtimer.c 46 KB

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