hrtimer.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859
  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. * Note: If we want to add new timer bases, we have to skip the two
  53. * clock ids captured by the cpu-timers. We do this by holding empty
  54. * entries rather than doing math adjustment of the clock ids.
  55. * This ensures that we capture erroneous accesses to these clock ids
  56. * rather than moving them into the range of valid clock id's.
  57. */
  58. DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
  59. {
  60. .clock_base =
  61. {
  62. {
  63. .index = CLOCK_REALTIME,
  64. .get_time = &ktime_get_real,
  65. .resolution = KTIME_LOW_RES,
  66. },
  67. {
  68. .index = CLOCK_MONOTONIC,
  69. .get_time = &ktime_get,
  70. .resolution = KTIME_LOW_RES,
  71. },
  72. }
  73. };
  74. /*
  75. * Get the coarse grained time at the softirq based on xtime and
  76. * wall_to_monotonic.
  77. */
  78. static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
  79. {
  80. ktime_t xtim, tomono;
  81. struct timespec xts, tom;
  82. unsigned long seq;
  83. do {
  84. seq = read_seqbegin(&xtime_lock);
  85. xts = __current_kernel_time();
  86. tom = __get_wall_to_monotonic();
  87. } while (read_seqretry(&xtime_lock, seq));
  88. xtim = timespec_to_ktime(xts);
  89. tomono = timespec_to_ktime(tom);
  90. base->clock_base[CLOCK_REALTIME].softirq_time = xtim;
  91. base->clock_base[CLOCK_MONOTONIC].softirq_time =
  92. ktime_add(xtim, tomono);
  93. }
  94. /*
  95. * Functions and macros which are different for UP/SMP systems are kept in a
  96. * single place
  97. */
  98. #ifdef CONFIG_SMP
  99. /*
  100. * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
  101. * means that all timers which are tied to this base via timer->base are
  102. * locked, and the base itself is locked too.
  103. *
  104. * So __run_timers/migrate_timers can safely modify all timers which could
  105. * be found on the lists/queues.
  106. *
  107. * When the timer's base is locked, and the timer removed from list, it is
  108. * possible to set timer->base = NULL and drop the lock: the timer remains
  109. * locked.
  110. */
  111. static
  112. struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
  113. unsigned long *flags)
  114. {
  115. struct hrtimer_clock_base *base;
  116. for (;;) {
  117. base = timer->base;
  118. if (likely(base != NULL)) {
  119. raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
  120. if (likely(base == timer->base))
  121. return base;
  122. /* The timer has migrated to another CPU: */
  123. raw_spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
  124. }
  125. cpu_relax();
  126. }
  127. }
  128. /*
  129. * Get the preferred target CPU for NOHZ
  130. */
  131. static int hrtimer_get_target(int this_cpu, int pinned)
  132. {
  133. #ifdef CONFIG_NO_HZ
  134. if (!pinned && get_sysctl_timer_migration() && idle_cpu(this_cpu))
  135. return get_nohz_timer_target();
  136. #endif
  137. return this_cpu;
  138. }
  139. /*
  140. * With HIGHRES=y we do not migrate the timer when it is expiring
  141. * before the next event on the target cpu because we cannot reprogram
  142. * the target cpu hardware and we would cause it to fire late.
  143. *
  144. * Called with cpu_base->lock of target cpu held.
  145. */
  146. static int
  147. hrtimer_check_target(struct hrtimer *timer, struct hrtimer_clock_base *new_base)
  148. {
  149. #ifdef CONFIG_HIGH_RES_TIMERS
  150. ktime_t expires;
  151. if (!new_base->cpu_base->hres_active)
  152. return 0;
  153. expires = ktime_sub(hrtimer_get_expires(timer), new_base->offset);
  154. return expires.tv64 <= new_base->cpu_base->expires_next.tv64;
  155. #else
  156. return 0;
  157. #endif
  158. }
  159. /*
  160. * Switch the timer base to the current CPU when possible.
  161. */
  162. static inline struct hrtimer_clock_base *
  163. switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
  164. int pinned)
  165. {
  166. struct hrtimer_clock_base *new_base;
  167. struct hrtimer_cpu_base *new_cpu_base;
  168. int this_cpu = smp_processor_id();
  169. int cpu = hrtimer_get_target(this_cpu, pinned);
  170. again:
  171. new_cpu_base = &per_cpu(hrtimer_bases, cpu);
  172. new_base = &new_cpu_base->clock_base[base->index];
  173. if (base != new_base) {
  174. /*
  175. * We are trying to move timer to new_base.
  176. * However we can't change timer's base while it is running,
  177. * so we keep it on the same CPU. No hassle vs. reprogramming
  178. * the event source in the high resolution case. The softirq
  179. * code will take care of this when the timer function has
  180. * completed. There is no conflict as we hold the lock until
  181. * the timer is enqueued.
  182. */
  183. if (unlikely(hrtimer_callback_running(timer)))
  184. return base;
  185. /* See the comment in lock_timer_base() */
  186. timer->base = NULL;
  187. raw_spin_unlock(&base->cpu_base->lock);
  188. raw_spin_lock(&new_base->cpu_base->lock);
  189. if (cpu != this_cpu && hrtimer_check_target(timer, new_base)) {
  190. cpu = this_cpu;
  191. raw_spin_unlock(&new_base->cpu_base->lock);
  192. raw_spin_lock(&base->cpu_base->lock);
  193. timer->base = base;
  194. goto again;
  195. }
  196. timer->base = new_base;
  197. }
  198. return new_base;
  199. }
  200. #else /* CONFIG_SMP */
  201. static inline struct hrtimer_clock_base *
  202. lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
  203. {
  204. struct hrtimer_clock_base *base = timer->base;
  205. raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
  206. return base;
  207. }
  208. # define switch_hrtimer_base(t, b, p) (b)
  209. #endif /* !CONFIG_SMP */
  210. /*
  211. * Functions for the union type storage format of ktime_t which are
  212. * too large for inlining:
  213. */
  214. #if BITS_PER_LONG < 64
  215. # ifndef CONFIG_KTIME_SCALAR
  216. /**
  217. * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
  218. * @kt: addend
  219. * @nsec: the scalar nsec value to add
  220. *
  221. * Returns the sum of kt and nsec in ktime_t format
  222. */
  223. ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
  224. {
  225. ktime_t tmp;
  226. if (likely(nsec < NSEC_PER_SEC)) {
  227. tmp.tv64 = nsec;
  228. } else {
  229. unsigned long rem = do_div(nsec, NSEC_PER_SEC);
  230. tmp = ktime_set((long)nsec, rem);
  231. }
  232. return ktime_add(kt, tmp);
  233. }
  234. EXPORT_SYMBOL_GPL(ktime_add_ns);
  235. /**
  236. * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
  237. * @kt: minuend
  238. * @nsec: the scalar nsec value to subtract
  239. *
  240. * Returns the subtraction of @nsec from @kt in ktime_t format
  241. */
  242. ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec)
  243. {
  244. ktime_t tmp;
  245. if (likely(nsec < NSEC_PER_SEC)) {
  246. tmp.tv64 = nsec;
  247. } else {
  248. unsigned long rem = do_div(nsec, NSEC_PER_SEC);
  249. tmp = ktime_set((long)nsec, rem);
  250. }
  251. return ktime_sub(kt, tmp);
  252. }
  253. EXPORT_SYMBOL_GPL(ktime_sub_ns);
  254. # endif /* !CONFIG_KTIME_SCALAR */
  255. /*
  256. * Divide a ktime value by a nanosecond value
  257. */
  258. u64 ktime_divns(const ktime_t kt, s64 div)
  259. {
  260. u64 dclc;
  261. int sft = 0;
  262. dclc = ktime_to_ns(kt);
  263. /* Make sure the divisor is less than 2^32: */
  264. while (div >> 32) {
  265. sft++;
  266. div >>= 1;
  267. }
  268. dclc >>= sft;
  269. do_div(dclc, (unsigned long) div);
  270. return dclc;
  271. }
  272. #endif /* BITS_PER_LONG >= 64 */
  273. /*
  274. * Add two ktime values and do a safety check for overflow:
  275. */
  276. ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
  277. {
  278. ktime_t res = ktime_add(lhs, rhs);
  279. /*
  280. * We use KTIME_SEC_MAX here, the maximum timeout which we can
  281. * return to user space in a timespec:
  282. */
  283. if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
  284. res = ktime_set(KTIME_SEC_MAX, 0);
  285. return res;
  286. }
  287. EXPORT_SYMBOL_GPL(ktime_add_safe);
  288. #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
  289. static struct debug_obj_descr hrtimer_debug_descr;
  290. /*
  291. * fixup_init is called when:
  292. * - an active object is initialized
  293. */
  294. static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
  295. {
  296. struct hrtimer *timer = addr;
  297. switch (state) {
  298. case ODEBUG_STATE_ACTIVE:
  299. hrtimer_cancel(timer);
  300. debug_object_init(timer, &hrtimer_debug_descr);
  301. return 1;
  302. default:
  303. return 0;
  304. }
  305. }
  306. /*
  307. * fixup_activate is called when:
  308. * - an active object is activated
  309. * - an unknown object is activated (might be a statically initialized object)
  310. */
  311. static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
  312. {
  313. switch (state) {
  314. case ODEBUG_STATE_NOTAVAILABLE:
  315. WARN_ON_ONCE(1);
  316. return 0;
  317. case ODEBUG_STATE_ACTIVE:
  318. WARN_ON(1);
  319. default:
  320. return 0;
  321. }
  322. }
  323. /*
  324. * fixup_free is called when:
  325. * - an active object is freed
  326. */
  327. static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
  328. {
  329. struct hrtimer *timer = addr;
  330. switch (state) {
  331. case ODEBUG_STATE_ACTIVE:
  332. hrtimer_cancel(timer);
  333. debug_object_free(timer, &hrtimer_debug_descr);
  334. return 1;
  335. default:
  336. return 0;
  337. }
  338. }
  339. static struct debug_obj_descr hrtimer_debug_descr = {
  340. .name = "hrtimer",
  341. .fixup_init = hrtimer_fixup_init,
  342. .fixup_activate = hrtimer_fixup_activate,
  343. .fixup_free = hrtimer_fixup_free,
  344. };
  345. static inline void debug_hrtimer_init(struct hrtimer *timer)
  346. {
  347. debug_object_init(timer, &hrtimer_debug_descr);
  348. }
  349. static inline void debug_hrtimer_activate(struct hrtimer *timer)
  350. {
  351. debug_object_activate(timer, &hrtimer_debug_descr);
  352. }
  353. static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
  354. {
  355. debug_object_deactivate(timer, &hrtimer_debug_descr);
  356. }
  357. static inline void debug_hrtimer_free(struct hrtimer *timer)
  358. {
  359. debug_object_free(timer, &hrtimer_debug_descr);
  360. }
  361. static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  362. enum hrtimer_mode mode);
  363. void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
  364. enum hrtimer_mode mode)
  365. {
  366. debug_object_init_on_stack(timer, &hrtimer_debug_descr);
  367. __hrtimer_init(timer, clock_id, mode);
  368. }
  369. EXPORT_SYMBOL_GPL(hrtimer_init_on_stack);
  370. void destroy_hrtimer_on_stack(struct hrtimer *timer)
  371. {
  372. debug_object_free(timer, &hrtimer_debug_descr);
  373. }
  374. #else
  375. static inline void debug_hrtimer_init(struct hrtimer *timer) { }
  376. static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
  377. static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
  378. #endif
  379. static inline void
  380. debug_init(struct hrtimer *timer, clockid_t clockid,
  381. enum hrtimer_mode mode)
  382. {
  383. debug_hrtimer_init(timer);
  384. trace_hrtimer_init(timer, clockid, mode);
  385. }
  386. static inline void debug_activate(struct hrtimer *timer)
  387. {
  388. debug_hrtimer_activate(timer);
  389. trace_hrtimer_start(timer);
  390. }
  391. static inline void debug_deactivate(struct hrtimer *timer)
  392. {
  393. debug_hrtimer_deactivate(timer);
  394. trace_hrtimer_cancel(timer);
  395. }
  396. /* High resolution timer related functions */
  397. #ifdef CONFIG_HIGH_RES_TIMERS
  398. /*
  399. * High resolution timer enabled ?
  400. */
  401. static int hrtimer_hres_enabled __read_mostly = 1;
  402. /*
  403. * Enable / Disable high resolution mode
  404. */
  405. static int __init setup_hrtimer_hres(char *str)
  406. {
  407. if (!strcmp(str, "off"))
  408. hrtimer_hres_enabled = 0;
  409. else if (!strcmp(str, "on"))
  410. hrtimer_hres_enabled = 1;
  411. else
  412. return 0;
  413. return 1;
  414. }
  415. __setup("highres=", setup_hrtimer_hres);
  416. /*
  417. * hrtimer_high_res_enabled - query, if the highres mode is enabled
  418. */
  419. static inline int hrtimer_is_hres_enabled(void)
  420. {
  421. return hrtimer_hres_enabled;
  422. }
  423. /*
  424. * Is the high resolution mode active ?
  425. */
  426. static inline int hrtimer_hres_active(void)
  427. {
  428. return __get_cpu_var(hrtimer_bases).hres_active;
  429. }
  430. /*
  431. * Reprogram the event source with checking both queues for the
  432. * next event
  433. * Called with interrupts disabled and base->lock held
  434. */
  435. static void
  436. hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
  437. {
  438. int i;
  439. struct hrtimer_clock_base *base = cpu_base->clock_base;
  440. ktime_t expires, expires_next;
  441. expires_next.tv64 = KTIME_MAX;
  442. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
  443. struct hrtimer *timer;
  444. if (!base->first)
  445. continue;
  446. timer = rb_entry(base->first, struct hrtimer, node);
  447. expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
  448. /*
  449. * clock_was_set() has changed base->offset so the
  450. * result might be negative. Fix it up to prevent a
  451. * false positive in clockevents_program_event()
  452. */
  453. if (expires.tv64 < 0)
  454. expires.tv64 = 0;
  455. if (expires.tv64 < expires_next.tv64)
  456. expires_next = expires;
  457. }
  458. if (skip_equal && expires_next.tv64 == cpu_base->expires_next.tv64)
  459. return;
  460. cpu_base->expires_next.tv64 = expires_next.tv64;
  461. if (cpu_base->expires_next.tv64 != KTIME_MAX)
  462. tick_program_event(cpu_base->expires_next, 1);
  463. }
  464. /*
  465. * Shared reprogramming for clock_realtime and clock_monotonic
  466. *
  467. * When a timer is enqueued and expires earlier than the already enqueued
  468. * timers, we have to check, whether it expires earlier than the timer for
  469. * which the clock event device was armed.
  470. *
  471. * Called with interrupts disabled and base->cpu_base.lock held
  472. */
  473. static int hrtimer_reprogram(struct hrtimer *timer,
  474. struct hrtimer_clock_base *base)
  475. {
  476. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  477. ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
  478. int res;
  479. WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
  480. /*
  481. * When the callback is running, we do not reprogram the clock event
  482. * device. The timer callback is either running on a different CPU or
  483. * the callback is executed in the hrtimer_interrupt context. The
  484. * reprogramming is handled either by the softirq, which called the
  485. * callback or at the end of the hrtimer_interrupt.
  486. */
  487. if (hrtimer_callback_running(timer))
  488. return 0;
  489. /*
  490. * CLOCK_REALTIME timer might be requested with an absolute
  491. * expiry time which is less than base->offset. Nothing wrong
  492. * about that, just avoid to call into the tick code, which
  493. * has now objections against negative expiry values.
  494. */
  495. if (expires.tv64 < 0)
  496. return -ETIME;
  497. if (expires.tv64 >= cpu_base->expires_next.tv64)
  498. return 0;
  499. /*
  500. * If a hang was detected in the last timer interrupt then we
  501. * do not schedule a timer which is earlier than the expiry
  502. * which we enforced in the hang detection. We want the system
  503. * to make progress.
  504. */
  505. if (cpu_base->hang_detected)
  506. return 0;
  507. /*
  508. * Clockevents returns -ETIME, when the event was in the past.
  509. */
  510. res = tick_program_event(expires, 0);
  511. if (!IS_ERR_VALUE(res))
  512. cpu_base->expires_next = expires;
  513. return res;
  514. }
  515. /*
  516. * Retrigger next event is called after clock was set
  517. *
  518. * Called with interrupts disabled via on_each_cpu()
  519. */
  520. static void retrigger_next_event(void *arg)
  521. {
  522. struct hrtimer_cpu_base *base;
  523. struct timespec realtime_offset, wtm;
  524. unsigned long seq;
  525. if (!hrtimer_hres_active())
  526. return;
  527. do {
  528. seq = read_seqbegin(&xtime_lock);
  529. wtm = __get_wall_to_monotonic();
  530. } while (read_seqretry(&xtime_lock, seq));
  531. set_normalized_timespec(&realtime_offset, -wtm.tv_sec, -wtm.tv_nsec);
  532. base = &__get_cpu_var(hrtimer_bases);
  533. /* Adjust CLOCK_REALTIME offset */
  534. raw_spin_lock(&base->lock);
  535. base->clock_base[CLOCK_REALTIME].offset =
  536. timespec_to_ktime(realtime_offset);
  537. hrtimer_force_reprogram(base, 0);
  538. raw_spin_unlock(&base->lock);
  539. }
  540. /*
  541. * Clock realtime was set
  542. *
  543. * Change the offset of the realtime clock vs. the monotonic
  544. * clock.
  545. *
  546. * We might have to reprogram the high resolution timer interrupt. On
  547. * SMP we call the architecture specific code to retrigger _all_ high
  548. * resolution timer interrupts. On UP we just disable interrupts and
  549. * call the high resolution interrupt code.
  550. */
  551. void clock_was_set(void)
  552. {
  553. /* Retrigger the CPU local events everywhere */
  554. on_each_cpu(retrigger_next_event, NULL, 1);
  555. }
  556. /*
  557. * During resume we might have to reprogram the high resolution timer
  558. * interrupt (on the local CPU):
  559. */
  560. void hres_timers_resume(void)
  561. {
  562. WARN_ONCE(!irqs_disabled(),
  563. KERN_INFO "hres_timers_resume() called with IRQs enabled!");
  564. retrigger_next_event(NULL);
  565. }
  566. /*
  567. * Initialize the high resolution related parts of cpu_base
  568. */
  569. static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
  570. {
  571. base->expires_next.tv64 = KTIME_MAX;
  572. base->hres_active = 0;
  573. }
  574. /*
  575. * Initialize the high resolution related parts of a hrtimer
  576. */
  577. static inline void hrtimer_init_timer_hres(struct hrtimer *timer)
  578. {
  579. }
  580. /*
  581. * When High resolution timers are active, try to reprogram. Note, that in case
  582. * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
  583. * check happens. The timer gets enqueued into the rbtree. The reprogramming
  584. * and expiry check is done in the hrtimer_interrupt or in the softirq.
  585. */
  586. static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
  587. struct hrtimer_clock_base *base,
  588. int wakeup)
  589. {
  590. if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
  591. if (wakeup) {
  592. raw_spin_unlock(&base->cpu_base->lock);
  593. raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  594. raw_spin_lock(&base->cpu_base->lock);
  595. } else
  596. __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  597. return 1;
  598. }
  599. return 0;
  600. }
  601. /*
  602. * Switch to high resolution mode
  603. */
  604. static int hrtimer_switch_to_hres(void)
  605. {
  606. int cpu = smp_processor_id();
  607. struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
  608. unsigned long flags;
  609. if (base->hres_active)
  610. return 1;
  611. local_irq_save(flags);
  612. if (tick_init_highres()) {
  613. local_irq_restore(flags);
  614. printk(KERN_WARNING "Could not switch to high resolution "
  615. "mode on CPU %d\n", cpu);
  616. return 0;
  617. }
  618. base->hres_active = 1;
  619. base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES;
  620. base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES;
  621. tick_setup_sched_timer();
  622. /* "Retrigger" the interrupt to get things going */
  623. retrigger_next_event(NULL);
  624. local_irq_restore(flags);
  625. return 1;
  626. }
  627. #else
  628. static inline int hrtimer_hres_active(void) { return 0; }
  629. static inline int hrtimer_is_hres_enabled(void) { return 0; }
  630. static inline int hrtimer_switch_to_hres(void) { return 0; }
  631. static inline void
  632. hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
  633. static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
  634. struct hrtimer_clock_base *base,
  635. int wakeup)
  636. {
  637. return 0;
  638. }
  639. static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
  640. static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { }
  641. #endif /* CONFIG_HIGH_RES_TIMERS */
  642. static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
  643. {
  644. #ifdef CONFIG_TIMER_STATS
  645. if (timer->start_site)
  646. return;
  647. timer->start_site = __builtin_return_address(0);
  648. memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
  649. timer->start_pid = current->pid;
  650. #endif
  651. }
  652. static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
  653. {
  654. #ifdef CONFIG_TIMER_STATS
  655. timer->start_site = NULL;
  656. #endif
  657. }
  658. static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
  659. {
  660. #ifdef CONFIG_TIMER_STATS
  661. if (likely(!timer_stats_active))
  662. return;
  663. timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
  664. timer->function, timer->start_comm, 0);
  665. #endif
  666. }
  667. /*
  668. * Counterpart to lock_hrtimer_base above:
  669. */
  670. static inline
  671. void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
  672. {
  673. raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
  674. }
  675. /**
  676. * hrtimer_forward - forward the timer expiry
  677. * @timer: hrtimer to forward
  678. * @now: forward past this time
  679. * @interval: the interval to forward
  680. *
  681. * Forward the timer expiry so it will expire in the future.
  682. * Returns the number of overruns.
  683. */
  684. u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
  685. {
  686. u64 orun = 1;
  687. ktime_t delta;
  688. delta = ktime_sub(now, hrtimer_get_expires(timer));
  689. if (delta.tv64 < 0)
  690. return 0;
  691. if (interval.tv64 < timer->base->resolution.tv64)
  692. interval.tv64 = timer->base->resolution.tv64;
  693. if (unlikely(delta.tv64 >= interval.tv64)) {
  694. s64 incr = ktime_to_ns(interval);
  695. orun = ktime_divns(delta, incr);
  696. hrtimer_add_expires_ns(timer, incr * orun);
  697. if (hrtimer_get_expires_tv64(timer) > now.tv64)
  698. return orun;
  699. /*
  700. * This (and the ktime_add() below) is the
  701. * correction for exact:
  702. */
  703. orun++;
  704. }
  705. hrtimer_add_expires(timer, interval);
  706. return orun;
  707. }
  708. EXPORT_SYMBOL_GPL(hrtimer_forward);
  709. /*
  710. * enqueue_hrtimer - internal function to (re)start a timer
  711. *
  712. * The timer is inserted in expiry order. Insertion into the
  713. * red black tree is O(log(n)). Must hold the base lock.
  714. *
  715. * Returns 1 when the new timer is the leftmost timer in the tree.
  716. */
  717. static int enqueue_hrtimer(struct hrtimer *timer,
  718. struct hrtimer_clock_base *base)
  719. {
  720. struct rb_node **link = &base->active.rb_node;
  721. struct rb_node *parent = NULL;
  722. struct hrtimer *entry;
  723. int leftmost = 1;
  724. debug_activate(timer);
  725. /*
  726. * Find the right place in the rbtree:
  727. */
  728. while (*link) {
  729. parent = *link;
  730. entry = rb_entry(parent, struct hrtimer, node);
  731. /*
  732. * We dont care about collisions. Nodes with
  733. * the same expiry time stay together.
  734. */
  735. if (hrtimer_get_expires_tv64(timer) <
  736. hrtimer_get_expires_tv64(entry)) {
  737. link = &(*link)->rb_left;
  738. } else {
  739. link = &(*link)->rb_right;
  740. leftmost = 0;
  741. }
  742. }
  743. /*
  744. * Insert the timer to the rbtree and check whether it
  745. * replaces the first pending timer
  746. */
  747. if (leftmost)
  748. base->first = &timer->node;
  749. rb_link_node(&timer->node, parent, link);
  750. rb_insert_color(&timer->node, &base->active);
  751. /*
  752. * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
  753. * state of a possibly running callback.
  754. */
  755. timer->state |= HRTIMER_STATE_ENQUEUED;
  756. return leftmost;
  757. }
  758. /*
  759. * __remove_hrtimer - internal function to remove a timer
  760. *
  761. * Caller must hold the base lock.
  762. *
  763. * High resolution timer mode reprograms the clock event device when the
  764. * timer is the one which expires next. The caller can disable this by setting
  765. * reprogram to zero. This is useful, when the context does a reprogramming
  766. * anyway (e.g. timer interrupt)
  767. */
  768. static void __remove_hrtimer(struct hrtimer *timer,
  769. struct hrtimer_clock_base *base,
  770. unsigned long newstate, int reprogram)
  771. {
  772. if (!(timer->state & HRTIMER_STATE_ENQUEUED))
  773. goto out;
  774. /*
  775. * Remove the timer from the rbtree and replace the first
  776. * entry pointer if necessary.
  777. */
  778. if (base->first == &timer->node) {
  779. base->first = rb_next(&timer->node);
  780. #ifdef CONFIG_HIGH_RES_TIMERS
  781. /* Reprogram the clock event device. if enabled */
  782. if (reprogram && hrtimer_hres_active()) {
  783. ktime_t expires;
  784. expires = ktime_sub(hrtimer_get_expires(timer),
  785. base->offset);
  786. if (base->cpu_base->expires_next.tv64 == expires.tv64)
  787. hrtimer_force_reprogram(base->cpu_base, 1);
  788. }
  789. #endif
  790. }
  791. rb_erase(&timer->node, &base->active);
  792. out:
  793. timer->state = newstate;
  794. }
  795. /*
  796. * remove hrtimer, called with base lock held
  797. */
  798. static inline int
  799. remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
  800. {
  801. if (hrtimer_is_queued(timer)) {
  802. int reprogram;
  803. /*
  804. * Remove the timer and force reprogramming when high
  805. * resolution mode is active and the timer is on the current
  806. * CPU. If we remove a timer on another CPU, reprogramming is
  807. * skipped. The interrupt event on this CPU is fired and
  808. * reprogramming happens in the interrupt handler. This is a
  809. * rare case and less expensive than a smp call.
  810. */
  811. debug_deactivate(timer);
  812. timer_stats_hrtimer_clear_start_info(timer);
  813. reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
  814. __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE,
  815. 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. /* Switch the timer base, if necessary: */
  831. new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
  832. if (mode & HRTIMER_MODE_REL) {
  833. tim = ktime_add_safe(tim, new_base->get_time());
  834. /*
  835. * CONFIG_TIME_LOW_RES is a temporary way for architectures
  836. * to signal that they simply return xtime in
  837. * do_gettimeoffset(). In this case we want to round up by
  838. * resolution when starting a relative timer, to avoid short
  839. * timeouts. This will go away with the GTOD framework.
  840. */
  841. #ifdef CONFIG_TIME_LOW_RES
  842. tim = ktime_add_safe(tim, base->resolution);
  843. #endif
  844. }
  845. hrtimer_set_expires_range_ns(timer, tim, delta_ns);
  846. timer_stats_hrtimer_set_start_info(timer);
  847. leftmost = enqueue_hrtimer(timer, new_base);
  848. /*
  849. * Only allow reprogramming if the new base is on this CPU.
  850. * (it might still be on another CPU if the timer was pending)
  851. *
  852. * XXX send_remote_softirq() ?
  853. */
  854. if (leftmost && new_base->cpu_base == &__get_cpu_var(hrtimer_bases))
  855. hrtimer_enqueue_reprogram(timer, new_base, wakeup);
  856. unlock_hrtimer_base(timer, &flags);
  857. return ret;
  858. }
  859. /**
  860. * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
  861. * @timer: the timer to be added
  862. * @tim: expiry time
  863. * @delta_ns: "slack" range for the timer
  864. * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
  865. *
  866. * Returns:
  867. * 0 on success
  868. * 1 when the timer was active
  869. */
  870. int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
  871. unsigned long delta_ns, const enum hrtimer_mode mode)
  872. {
  873. return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1);
  874. }
  875. EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
  876. /**
  877. * hrtimer_start - (re)start an hrtimer on the current CPU
  878. * @timer: the timer to be added
  879. * @tim: expiry time
  880. * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
  881. *
  882. * Returns:
  883. * 0 on success
  884. * 1 when the timer was active
  885. */
  886. int
  887. hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
  888. {
  889. return __hrtimer_start_range_ns(timer, tim, 0, mode, 1);
  890. }
  891. EXPORT_SYMBOL_GPL(hrtimer_start);
  892. /**
  893. * hrtimer_try_to_cancel - try to deactivate a timer
  894. * @timer: hrtimer to stop
  895. *
  896. * Returns:
  897. * 0 when the timer was not active
  898. * 1 when the timer was active
  899. * -1 when the timer is currently excuting the callback function and
  900. * cannot be stopped
  901. */
  902. int hrtimer_try_to_cancel(struct hrtimer *timer)
  903. {
  904. struct hrtimer_clock_base *base;
  905. unsigned long flags;
  906. int ret = -1;
  907. base = lock_hrtimer_base(timer, &flags);
  908. if (!hrtimer_callback_running(timer))
  909. ret = remove_hrtimer(timer, base);
  910. unlock_hrtimer_base(timer, &flags);
  911. return ret;
  912. }
  913. EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
  914. /**
  915. * hrtimer_cancel - cancel a timer and wait for the handler to finish.
  916. * @timer: the timer to be cancelled
  917. *
  918. * Returns:
  919. * 0 when the timer was not active
  920. * 1 when the timer was active
  921. */
  922. int hrtimer_cancel(struct hrtimer *timer)
  923. {
  924. for (;;) {
  925. int ret = hrtimer_try_to_cancel(timer);
  926. if (ret >= 0)
  927. return ret;
  928. cpu_relax();
  929. }
  930. }
  931. EXPORT_SYMBOL_GPL(hrtimer_cancel);
  932. /**
  933. * hrtimer_get_remaining - get remaining time for the timer
  934. * @timer: the timer to read
  935. */
  936. ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
  937. {
  938. unsigned long flags;
  939. ktime_t rem;
  940. lock_hrtimer_base(timer, &flags);
  941. rem = hrtimer_expires_remaining(timer);
  942. unlock_hrtimer_base(timer, &flags);
  943. return rem;
  944. }
  945. EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
  946. #ifdef CONFIG_NO_HZ
  947. /**
  948. * hrtimer_get_next_event - get the time until next expiry event
  949. *
  950. * Returns the delta to the next expiry event or KTIME_MAX if no timer
  951. * is pending.
  952. */
  953. ktime_t hrtimer_get_next_event(void)
  954. {
  955. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  956. struct hrtimer_clock_base *base = cpu_base->clock_base;
  957. ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
  958. unsigned long flags;
  959. int i;
  960. raw_spin_lock_irqsave(&cpu_base->lock, flags);
  961. if (!hrtimer_hres_active()) {
  962. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
  963. struct hrtimer *timer;
  964. if (!base->first)
  965. continue;
  966. timer = rb_entry(base->first, struct hrtimer, node);
  967. delta.tv64 = hrtimer_get_expires_tv64(timer);
  968. delta = ktime_sub(delta, base->get_time());
  969. if (delta.tv64 < mindelta.tv64)
  970. mindelta.tv64 = delta.tv64;
  971. }
  972. }
  973. raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
  974. if (mindelta.tv64 < 0)
  975. mindelta.tv64 = 0;
  976. return mindelta;
  977. }
  978. #endif
  979. static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  980. enum hrtimer_mode mode)
  981. {
  982. struct hrtimer_cpu_base *cpu_base;
  983. memset(timer, 0, sizeof(struct hrtimer));
  984. cpu_base = &__raw_get_cpu_var(hrtimer_bases);
  985. if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
  986. clock_id = CLOCK_MONOTONIC;
  987. timer->base = &cpu_base->clock_base[clock_id];
  988. hrtimer_init_timer_hres(timer);
  989. #ifdef CONFIG_TIMER_STATS
  990. timer->start_site = NULL;
  991. timer->start_pid = -1;
  992. memset(timer->start_comm, 0, TASK_COMM_LEN);
  993. #endif
  994. }
  995. /**
  996. * hrtimer_init - initialize a timer to the given clock
  997. * @timer: the timer to be initialized
  998. * @clock_id: the clock to be used
  999. * @mode: timer mode abs/rel
  1000. */
  1001. void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  1002. enum hrtimer_mode mode)
  1003. {
  1004. debug_init(timer, clock_id, mode);
  1005. __hrtimer_init(timer, clock_id, mode);
  1006. }
  1007. EXPORT_SYMBOL_GPL(hrtimer_init);
  1008. /**
  1009. * hrtimer_get_res - get the timer resolution for a clock
  1010. * @which_clock: which clock to query
  1011. * @tp: pointer to timespec variable to store the resolution
  1012. *
  1013. * Store the resolution of the clock selected by @which_clock in the
  1014. * variable pointed to by @tp.
  1015. */
  1016. int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
  1017. {
  1018. struct hrtimer_cpu_base *cpu_base;
  1019. cpu_base = &__raw_get_cpu_var(hrtimer_bases);
  1020. *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
  1021. return 0;
  1022. }
  1023. EXPORT_SYMBOL_GPL(hrtimer_get_res);
  1024. static void __run_hrtimer(struct hrtimer *timer, ktime_t *now)
  1025. {
  1026. struct hrtimer_clock_base *base = timer->base;
  1027. struct hrtimer_cpu_base *cpu_base = base->cpu_base;
  1028. enum hrtimer_restart (*fn)(struct hrtimer *);
  1029. int restart;
  1030. WARN_ON(!irqs_disabled());
  1031. debug_deactivate(timer);
  1032. __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
  1033. timer_stats_account_hrtimer(timer);
  1034. fn = timer->function;
  1035. /*
  1036. * Because we run timers from hardirq context, there is no chance
  1037. * they get migrated to another cpu, therefore its safe to unlock
  1038. * the timer base.
  1039. */
  1040. raw_spin_unlock(&cpu_base->lock);
  1041. trace_hrtimer_expire_entry(timer, now);
  1042. restart = fn(timer);
  1043. trace_hrtimer_expire_exit(timer);
  1044. raw_spin_lock(&cpu_base->lock);
  1045. /*
  1046. * Note: We clear the CALLBACK bit after enqueue_hrtimer and
  1047. * we do not reprogramm the event hardware. Happens either in
  1048. * hrtimer_start_range_ns() or in hrtimer_interrupt()
  1049. */
  1050. if (restart != HRTIMER_NORESTART) {
  1051. BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
  1052. enqueue_hrtimer(timer, base);
  1053. }
  1054. timer->state &= ~HRTIMER_STATE_CALLBACK;
  1055. }
  1056. #ifdef CONFIG_HIGH_RES_TIMERS
  1057. /*
  1058. * High resolution timer interrupt
  1059. * Called with interrupts disabled
  1060. */
  1061. void hrtimer_interrupt(struct clock_event_device *dev)
  1062. {
  1063. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  1064. struct hrtimer_clock_base *base;
  1065. ktime_t expires_next, now, entry_time, delta;
  1066. int i, retries = 0;
  1067. BUG_ON(!cpu_base->hres_active);
  1068. cpu_base->nr_events++;
  1069. dev->next_event.tv64 = KTIME_MAX;
  1070. entry_time = now = ktime_get();
  1071. retry:
  1072. expires_next.tv64 = KTIME_MAX;
  1073. raw_spin_lock(&cpu_base->lock);
  1074. /*
  1075. * We set expires_next to KTIME_MAX here with cpu_base->lock
  1076. * held to prevent that a timer is enqueued in our queue via
  1077. * the migration code. This does not affect enqueueing of
  1078. * timers which run their callback and need to be requeued on
  1079. * this CPU.
  1080. */
  1081. cpu_base->expires_next.tv64 = KTIME_MAX;
  1082. base = cpu_base->clock_base;
  1083. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  1084. ktime_t basenow;
  1085. struct rb_node *node;
  1086. basenow = ktime_add(now, base->offset);
  1087. while ((node = base->first)) {
  1088. struct hrtimer *timer;
  1089. timer = rb_entry(node, struct hrtimer, node);
  1090. /*
  1091. * The immediate goal for using the softexpires is
  1092. * minimizing wakeups, not running timers at the
  1093. * earliest interrupt after their soft expiration.
  1094. * This allows us to avoid using a Priority Search
  1095. * Tree, which can answer a stabbing querry for
  1096. * overlapping intervals and instead use the simple
  1097. * BST we already have.
  1098. * We don't add extra wakeups by delaying timers that
  1099. * are right-of a not yet expired timer, because that
  1100. * timer will have to trigger a wakeup anyway.
  1101. */
  1102. if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
  1103. ktime_t expires;
  1104. expires = ktime_sub(hrtimer_get_expires(timer),
  1105. base->offset);
  1106. if (expires.tv64 < expires_next.tv64)
  1107. expires_next = expires;
  1108. break;
  1109. }
  1110. __run_hrtimer(timer, &basenow);
  1111. }
  1112. base++;
  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 rb_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 (!base->first)
  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 = base->first)) {
  1242. struct hrtimer *timer;
  1243. timer = rb_entry(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.index,
  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.index = t.timer.base->index;
  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. hrtimer_init_hres(cpu_base);
  1376. }
  1377. #ifdef CONFIG_HOTPLUG_CPU
  1378. static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
  1379. struct hrtimer_clock_base *new_base)
  1380. {
  1381. struct hrtimer *timer;
  1382. struct rb_node *node;
  1383. while ((node = rb_first(&old_base->active))) {
  1384. timer = rb_entry(node, struct hrtimer, node);
  1385. BUG_ON(hrtimer_callback_running(timer));
  1386. debug_deactivate(timer);
  1387. /*
  1388. * Mark it as STATE_MIGRATE not INACTIVE otherwise the
  1389. * timer could be seen as !active and just vanish away
  1390. * under us on another CPU
  1391. */
  1392. __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
  1393. timer->base = new_base;
  1394. /*
  1395. * Enqueue the timers on the new cpu. This does not
  1396. * reprogram the event device in case the timer
  1397. * expires before the earliest on this CPU, but we run
  1398. * hrtimer_interrupt after we migrated everything to
  1399. * sort out already expired timers and reprogram the
  1400. * event device.
  1401. */
  1402. enqueue_hrtimer(timer, new_base);
  1403. /* Clear the migration state bit */
  1404. timer->state &= ~HRTIMER_STATE_MIGRATE;
  1405. }
  1406. }
  1407. static void migrate_hrtimers(int scpu)
  1408. {
  1409. struct hrtimer_cpu_base *old_base, *new_base;
  1410. int i;
  1411. BUG_ON(cpu_online(scpu));
  1412. tick_cancel_sched_timer(scpu);
  1413. local_irq_disable();
  1414. old_base = &per_cpu(hrtimer_bases, scpu);
  1415. new_base = &__get_cpu_var(hrtimer_bases);
  1416. /*
  1417. * The caller is globally serialized and nobody else
  1418. * takes two locks at once, deadlock is not possible.
  1419. */
  1420. raw_spin_lock(&new_base->lock);
  1421. raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
  1422. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  1423. migrate_hrtimer_list(&old_base->clock_base[i],
  1424. &new_base->clock_base[i]);
  1425. }
  1426. raw_spin_unlock(&old_base->lock);
  1427. raw_spin_unlock(&new_base->lock);
  1428. /* Check, if we got expired work to do */
  1429. __hrtimer_peek_ahead_timers();
  1430. local_irq_enable();
  1431. }
  1432. #endif /* CONFIG_HOTPLUG_CPU */
  1433. static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
  1434. unsigned long action, void *hcpu)
  1435. {
  1436. int scpu = (long)hcpu;
  1437. switch (action) {
  1438. case CPU_UP_PREPARE:
  1439. case CPU_UP_PREPARE_FROZEN:
  1440. init_hrtimers_cpu(scpu);
  1441. break;
  1442. #ifdef CONFIG_HOTPLUG_CPU
  1443. case CPU_DYING:
  1444. case CPU_DYING_FROZEN:
  1445. clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu);
  1446. break;
  1447. case CPU_DEAD:
  1448. case CPU_DEAD_FROZEN:
  1449. {
  1450. clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
  1451. migrate_hrtimers(scpu);
  1452. break;
  1453. }
  1454. #endif
  1455. default:
  1456. break;
  1457. }
  1458. return NOTIFY_OK;
  1459. }
  1460. static struct notifier_block __cpuinitdata hrtimers_nb = {
  1461. .notifier_call = hrtimer_cpu_notify,
  1462. };
  1463. void __init hrtimers_init(void)
  1464. {
  1465. hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
  1466. (void *)(long)smp_processor_id());
  1467. register_cpu_notifier(&hrtimers_nb);
  1468. #ifdef CONFIG_HIGH_RES_TIMERS
  1469. open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
  1470. #endif
  1471. }
  1472. /**
  1473. * schedule_hrtimeout_range_clock - sleep until timeout
  1474. * @expires: timeout value (ktime_t)
  1475. * @delta: slack in expires timeout (ktime_t)
  1476. * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
  1477. * @clock: timer clock, CLOCK_MONOTONIC or CLOCK_REALTIME
  1478. */
  1479. int __sched
  1480. schedule_hrtimeout_range_clock(ktime_t *expires, unsigned long delta,
  1481. const enum hrtimer_mode mode, int clock)
  1482. {
  1483. struct hrtimer_sleeper t;
  1484. /*
  1485. * Optimize when a zero timeout value is given. It does not
  1486. * matter whether this is an absolute or a relative time.
  1487. */
  1488. if (expires && !expires->tv64) {
  1489. __set_current_state(TASK_RUNNING);
  1490. return 0;
  1491. }
  1492. /*
  1493. * A NULL parameter means "inifinte"
  1494. */
  1495. if (!expires) {
  1496. schedule();
  1497. __set_current_state(TASK_RUNNING);
  1498. return -EINTR;
  1499. }
  1500. hrtimer_init_on_stack(&t.timer, clock, mode);
  1501. hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
  1502. hrtimer_init_sleeper(&t, current);
  1503. hrtimer_start_expires(&t.timer, mode);
  1504. if (!hrtimer_active(&t.timer))
  1505. t.task = NULL;
  1506. if (likely(t.task))
  1507. schedule();
  1508. hrtimer_cancel(&t.timer);
  1509. destroy_hrtimer_on_stack(&t.timer);
  1510. __set_current_state(TASK_RUNNING);
  1511. return !t.task ? 0 : -EINTR;
  1512. }
  1513. /**
  1514. * schedule_hrtimeout_range - sleep until timeout
  1515. * @expires: timeout value (ktime_t)
  1516. * @delta: slack in expires timeout (ktime_t)
  1517. * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
  1518. *
  1519. * Make the current task sleep until the given expiry time has
  1520. * elapsed. The routine will return immediately unless
  1521. * the current task state has been set (see set_current_state()).
  1522. *
  1523. * The @delta argument gives the kernel the freedom to schedule the
  1524. * actual wakeup to a time that is both power and performance friendly.
  1525. * The kernel give the normal best effort behavior for "@expires+@delta",
  1526. * but may decide to fire the timer earlier, but no earlier than @expires.
  1527. *
  1528. * You can set the task state as follows -
  1529. *
  1530. * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
  1531. * pass before the routine returns.
  1532. *
  1533. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  1534. * delivered to the current task.
  1535. *
  1536. * The current task state is guaranteed to be TASK_RUNNING when this
  1537. * routine returns.
  1538. *
  1539. * Returns 0 when the timer has expired otherwise -EINTR
  1540. */
  1541. int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
  1542. const enum hrtimer_mode mode)
  1543. {
  1544. return schedule_hrtimeout_range_clock(expires, delta, mode,
  1545. CLOCK_MONOTONIC);
  1546. }
  1547. EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
  1548. /**
  1549. * schedule_hrtimeout - sleep until timeout
  1550. * @expires: timeout value (ktime_t)
  1551. * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
  1552. *
  1553. * Make the current task sleep until the given expiry time has
  1554. * elapsed. The routine will return immediately unless
  1555. * the current task state has been set (see set_current_state()).
  1556. *
  1557. * You can set the task state as follows -
  1558. *
  1559. * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
  1560. * pass before the routine returns.
  1561. *
  1562. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  1563. * delivered to the current task.
  1564. *
  1565. * The current task state is guaranteed to be TASK_RUNNING when this
  1566. * routine returns.
  1567. *
  1568. * Returns 0 when the timer has expired otherwise -EINTR
  1569. */
  1570. int __sched schedule_hrtimeout(ktime_t *expires,
  1571. const enum hrtimer_mode mode)
  1572. {
  1573. return schedule_hrtimeout_range(expires, 0, mode);
  1574. }
  1575. EXPORT_SYMBOL_GPL(schedule_hrtimeout);