hrtimer.c 45 KB

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