hrtimer.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817
  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. 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. 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. spin_unlock(&base->cpu_base->lock);
  191. spin_lock(&new_base->cpu_base->lock);
  192. if (cpu != this_cpu && hrtimer_check_target(timer, new_base)) {
  193. cpu = this_cpu;
  194. spin_unlock(&new_base->cpu_base->lock);
  195. 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. 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. ktime_t *expires_next = &__get_cpu_var(hrtimer_bases).expires_next;
  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 >= expires_next->tv64)
  501. return 0;
  502. /*
  503. * Clockevents returns -ETIME, when the event was in the past.
  504. */
  505. res = tick_program_event(expires, 0);
  506. if (!IS_ERR_VALUE(res))
  507. *expires_next = expires;
  508. return res;
  509. }
  510. /*
  511. * Retrigger next event is called after clock was set
  512. *
  513. * Called with interrupts disabled via on_each_cpu()
  514. */
  515. static void retrigger_next_event(void *arg)
  516. {
  517. struct hrtimer_cpu_base *base;
  518. struct timespec realtime_offset;
  519. unsigned long seq;
  520. if (!hrtimer_hres_active())
  521. return;
  522. do {
  523. seq = read_seqbegin(&xtime_lock);
  524. set_normalized_timespec(&realtime_offset,
  525. -wall_to_monotonic.tv_sec,
  526. -wall_to_monotonic.tv_nsec);
  527. } while (read_seqretry(&xtime_lock, seq));
  528. base = &__get_cpu_var(hrtimer_bases);
  529. /* Adjust CLOCK_REALTIME offset */
  530. spin_lock(&base->lock);
  531. base->clock_base[CLOCK_REALTIME].offset =
  532. timespec_to_ktime(realtime_offset);
  533. hrtimer_force_reprogram(base, 0);
  534. spin_unlock(&base->lock);
  535. }
  536. /*
  537. * Clock realtime was set
  538. *
  539. * Change the offset of the realtime clock vs. the monotonic
  540. * clock.
  541. *
  542. * We might have to reprogram the high resolution timer interrupt. On
  543. * SMP we call the architecture specific code to retrigger _all_ high
  544. * resolution timer interrupts. On UP we just disable interrupts and
  545. * call the high resolution interrupt code.
  546. */
  547. void clock_was_set(void)
  548. {
  549. /* Retrigger the CPU local events everywhere */
  550. on_each_cpu(retrigger_next_event, NULL, 1);
  551. }
  552. /*
  553. * During resume we might have to reprogram the high resolution timer
  554. * interrupt (on the local CPU):
  555. */
  556. void hres_timers_resume(void)
  557. {
  558. WARN_ONCE(!irqs_disabled(),
  559. KERN_INFO "hres_timers_resume() called with IRQs enabled!");
  560. retrigger_next_event(NULL);
  561. }
  562. /*
  563. * Initialize the high resolution related parts of cpu_base
  564. */
  565. static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
  566. {
  567. base->expires_next.tv64 = KTIME_MAX;
  568. base->hres_active = 0;
  569. }
  570. /*
  571. * Initialize the high resolution related parts of a hrtimer
  572. */
  573. static inline void hrtimer_init_timer_hres(struct hrtimer *timer)
  574. {
  575. }
  576. /*
  577. * When High resolution timers are active, try to reprogram. Note, that in case
  578. * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
  579. * check happens. The timer gets enqueued into the rbtree. The reprogramming
  580. * and expiry check is done in the hrtimer_interrupt or in the softirq.
  581. */
  582. static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
  583. struct hrtimer_clock_base *base,
  584. int wakeup)
  585. {
  586. if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
  587. if (wakeup) {
  588. spin_unlock(&base->cpu_base->lock);
  589. raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  590. spin_lock(&base->cpu_base->lock);
  591. } else
  592. __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  593. return 1;
  594. }
  595. return 0;
  596. }
  597. /*
  598. * Switch to high resolution mode
  599. */
  600. static int hrtimer_switch_to_hres(void)
  601. {
  602. int cpu = smp_processor_id();
  603. struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
  604. unsigned long flags;
  605. if (base->hres_active)
  606. return 1;
  607. local_irq_save(flags);
  608. if (tick_init_highres()) {
  609. local_irq_restore(flags);
  610. printk(KERN_WARNING "Could not switch to high resolution "
  611. "mode on CPU %d\n", cpu);
  612. return 0;
  613. }
  614. base->hres_active = 1;
  615. base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES;
  616. base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES;
  617. tick_setup_sched_timer();
  618. /* "Retrigger" the interrupt to get things going */
  619. retrigger_next_event(NULL);
  620. local_irq_restore(flags);
  621. return 1;
  622. }
  623. #else
  624. static inline int hrtimer_hres_active(void) { return 0; }
  625. static inline int hrtimer_is_hres_enabled(void) { return 0; }
  626. static inline int hrtimer_switch_to_hres(void) { return 0; }
  627. static inline void
  628. hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
  629. static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
  630. struct hrtimer_clock_base *base,
  631. int wakeup)
  632. {
  633. return 0;
  634. }
  635. static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
  636. static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { }
  637. #endif /* CONFIG_HIGH_RES_TIMERS */
  638. #ifdef CONFIG_TIMER_STATS
  639. void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer, void *addr)
  640. {
  641. if (timer->start_site)
  642. return;
  643. timer->start_site = addr;
  644. memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
  645. timer->start_pid = current->pid;
  646. }
  647. #endif
  648. /*
  649. * Counterpart to lock_hrtimer_base above:
  650. */
  651. static inline
  652. void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
  653. {
  654. spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
  655. }
  656. /**
  657. * hrtimer_forward - forward the timer expiry
  658. * @timer: hrtimer to forward
  659. * @now: forward past this time
  660. * @interval: the interval to forward
  661. *
  662. * Forward the timer expiry so it will expire in the future.
  663. * Returns the number of overruns.
  664. */
  665. u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
  666. {
  667. u64 orun = 1;
  668. ktime_t delta;
  669. delta = ktime_sub(now, hrtimer_get_expires(timer));
  670. if (delta.tv64 < 0)
  671. return 0;
  672. if (interval.tv64 < timer->base->resolution.tv64)
  673. interval.tv64 = timer->base->resolution.tv64;
  674. if (unlikely(delta.tv64 >= interval.tv64)) {
  675. s64 incr = ktime_to_ns(interval);
  676. orun = ktime_divns(delta, incr);
  677. hrtimer_add_expires_ns(timer, incr * orun);
  678. if (hrtimer_get_expires_tv64(timer) > now.tv64)
  679. return orun;
  680. /*
  681. * This (and the ktime_add() below) is the
  682. * correction for exact:
  683. */
  684. orun++;
  685. }
  686. hrtimer_add_expires(timer, interval);
  687. return orun;
  688. }
  689. EXPORT_SYMBOL_GPL(hrtimer_forward);
  690. /*
  691. * enqueue_hrtimer - internal function to (re)start a timer
  692. *
  693. * The timer is inserted in expiry order. Insertion into the
  694. * red black tree is O(log(n)). Must hold the base lock.
  695. *
  696. * Returns 1 when the new timer is the leftmost timer in the tree.
  697. */
  698. static int enqueue_hrtimer(struct hrtimer *timer,
  699. struct hrtimer_clock_base *base)
  700. {
  701. struct rb_node **link = &base->active.rb_node;
  702. struct rb_node *parent = NULL;
  703. struct hrtimer *entry;
  704. int leftmost = 1;
  705. debug_activate(timer);
  706. /*
  707. * Find the right place in the rbtree:
  708. */
  709. while (*link) {
  710. parent = *link;
  711. entry = rb_entry(parent, struct hrtimer, node);
  712. /*
  713. * We dont care about collisions. Nodes with
  714. * the same expiry time stay together.
  715. */
  716. if (hrtimer_get_expires_tv64(timer) <
  717. hrtimer_get_expires_tv64(entry)) {
  718. link = &(*link)->rb_left;
  719. } else {
  720. link = &(*link)->rb_right;
  721. leftmost = 0;
  722. }
  723. }
  724. /*
  725. * Insert the timer to the rbtree and check whether it
  726. * replaces the first pending timer
  727. */
  728. if (leftmost)
  729. base->first = &timer->node;
  730. rb_link_node(&timer->node, parent, link);
  731. rb_insert_color(&timer->node, &base->active);
  732. /*
  733. * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
  734. * state of a possibly running callback.
  735. */
  736. timer->state |= HRTIMER_STATE_ENQUEUED;
  737. return leftmost;
  738. }
  739. /*
  740. * __remove_hrtimer - internal function to remove a timer
  741. *
  742. * Caller must hold the base lock.
  743. *
  744. * High resolution timer mode reprograms the clock event device when the
  745. * timer is the one which expires next. The caller can disable this by setting
  746. * reprogram to zero. This is useful, when the context does a reprogramming
  747. * anyway (e.g. timer interrupt)
  748. */
  749. static void __remove_hrtimer(struct hrtimer *timer,
  750. struct hrtimer_clock_base *base,
  751. unsigned long newstate, int reprogram)
  752. {
  753. if (!(timer->state & HRTIMER_STATE_ENQUEUED))
  754. goto out;
  755. /*
  756. * Remove the timer from the rbtree and replace the first
  757. * entry pointer if necessary.
  758. */
  759. if (base->first == &timer->node) {
  760. base->first = rb_next(&timer->node);
  761. #ifdef CONFIG_HIGH_RES_TIMERS
  762. /* Reprogram the clock event device. if enabled */
  763. if (reprogram && hrtimer_hres_active()) {
  764. ktime_t expires;
  765. expires = ktime_sub(hrtimer_get_expires(timer),
  766. base->offset);
  767. if (base->cpu_base->expires_next.tv64 == expires.tv64)
  768. hrtimer_force_reprogram(base->cpu_base, 1);
  769. }
  770. #endif
  771. }
  772. rb_erase(&timer->node, &base->active);
  773. out:
  774. timer->state = newstate;
  775. }
  776. /*
  777. * remove hrtimer, called with base lock held
  778. */
  779. static inline int
  780. remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
  781. {
  782. if (hrtimer_is_queued(timer)) {
  783. int reprogram;
  784. /*
  785. * Remove the timer and force reprogramming when high
  786. * resolution mode is active and the timer is on the current
  787. * CPU. If we remove a timer on another CPU, reprogramming is
  788. * skipped. The interrupt event on this CPU is fired and
  789. * reprogramming happens in the interrupt handler. This is a
  790. * rare case and less expensive than a smp call.
  791. */
  792. debug_deactivate(timer);
  793. timer_stats_hrtimer_clear_start_info(timer);
  794. reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
  795. __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE,
  796. reprogram);
  797. return 1;
  798. }
  799. return 0;
  800. }
  801. int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
  802. unsigned long delta_ns, const enum hrtimer_mode mode,
  803. int wakeup)
  804. {
  805. struct hrtimer_clock_base *base, *new_base;
  806. unsigned long flags;
  807. int ret, leftmost;
  808. base = lock_hrtimer_base(timer, &flags);
  809. /* Remove an active timer from the queue: */
  810. ret = remove_hrtimer(timer, base);
  811. /* Switch the timer base, if necessary: */
  812. new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
  813. if (mode & HRTIMER_MODE_REL) {
  814. tim = ktime_add_safe(tim, new_base->get_time());
  815. /*
  816. * CONFIG_TIME_LOW_RES is a temporary way for architectures
  817. * to signal that they simply return xtime in
  818. * do_gettimeoffset(). In this case we want to round up by
  819. * resolution when starting a relative timer, to avoid short
  820. * timeouts. This will go away with the GTOD framework.
  821. */
  822. #ifdef CONFIG_TIME_LOW_RES
  823. tim = ktime_add_safe(tim, base->resolution);
  824. #endif
  825. }
  826. hrtimer_set_expires_range_ns(timer, tim, delta_ns);
  827. timer_stats_hrtimer_set_start_info(timer);
  828. leftmost = enqueue_hrtimer(timer, new_base);
  829. /*
  830. * Only allow reprogramming if the new base is on this CPU.
  831. * (it might still be on another CPU if the timer was pending)
  832. *
  833. * XXX send_remote_softirq() ?
  834. */
  835. if (leftmost && new_base->cpu_base == &__get_cpu_var(hrtimer_bases))
  836. hrtimer_enqueue_reprogram(timer, new_base, wakeup);
  837. unlock_hrtimer_base(timer, &flags);
  838. return ret;
  839. }
  840. /**
  841. * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
  842. * @timer: the timer to be added
  843. * @tim: expiry time
  844. * @delta_ns: "slack" range for the timer
  845. * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
  846. *
  847. * Returns:
  848. * 0 on success
  849. * 1 when the timer was active
  850. */
  851. int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
  852. unsigned long delta_ns, const enum hrtimer_mode mode)
  853. {
  854. return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1);
  855. }
  856. EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
  857. /**
  858. * hrtimer_start - (re)start an hrtimer on the current CPU
  859. * @timer: the timer to be added
  860. * @tim: expiry time
  861. * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
  862. *
  863. * Returns:
  864. * 0 on success
  865. * 1 when the timer was active
  866. */
  867. int
  868. hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
  869. {
  870. return __hrtimer_start_range_ns(timer, tim, 0, mode, 1);
  871. }
  872. EXPORT_SYMBOL_GPL(hrtimer_start);
  873. /**
  874. * hrtimer_try_to_cancel - try to deactivate a timer
  875. * @timer: hrtimer to stop
  876. *
  877. * Returns:
  878. * 0 when the timer was not active
  879. * 1 when the timer was active
  880. * -1 when the timer is currently excuting the callback function and
  881. * cannot be stopped
  882. */
  883. int hrtimer_try_to_cancel(struct hrtimer *timer)
  884. {
  885. struct hrtimer_clock_base *base;
  886. unsigned long flags;
  887. int ret = -1;
  888. base = lock_hrtimer_base(timer, &flags);
  889. if (!hrtimer_callback_running(timer))
  890. ret = remove_hrtimer(timer, base);
  891. unlock_hrtimer_base(timer, &flags);
  892. return ret;
  893. }
  894. EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
  895. /**
  896. * hrtimer_cancel - cancel a timer and wait for the handler to finish.
  897. * @timer: the timer to be cancelled
  898. *
  899. * Returns:
  900. * 0 when the timer was not active
  901. * 1 when the timer was active
  902. */
  903. int hrtimer_cancel(struct hrtimer *timer)
  904. {
  905. for (;;) {
  906. int ret = hrtimer_try_to_cancel(timer);
  907. if (ret >= 0)
  908. return ret;
  909. cpu_relax();
  910. }
  911. }
  912. EXPORT_SYMBOL_GPL(hrtimer_cancel);
  913. /**
  914. * hrtimer_get_remaining - get remaining time for the timer
  915. * @timer: the timer to read
  916. */
  917. ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
  918. {
  919. struct hrtimer_clock_base *base;
  920. unsigned long flags;
  921. ktime_t rem;
  922. base = lock_hrtimer_base(timer, &flags);
  923. rem = hrtimer_expires_remaining(timer);
  924. unlock_hrtimer_base(timer, &flags);
  925. return rem;
  926. }
  927. EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
  928. #ifdef CONFIG_NO_HZ
  929. /**
  930. * hrtimer_get_next_event - get the time until next expiry event
  931. *
  932. * Returns the delta to the next expiry event or KTIME_MAX if no timer
  933. * is pending.
  934. */
  935. ktime_t hrtimer_get_next_event(void)
  936. {
  937. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  938. struct hrtimer_clock_base *base = cpu_base->clock_base;
  939. ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
  940. unsigned long flags;
  941. int i;
  942. spin_lock_irqsave(&cpu_base->lock, flags);
  943. if (!hrtimer_hres_active()) {
  944. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
  945. struct hrtimer *timer;
  946. if (!base->first)
  947. continue;
  948. timer = rb_entry(base->first, struct hrtimer, node);
  949. delta.tv64 = hrtimer_get_expires_tv64(timer);
  950. delta = ktime_sub(delta, base->get_time());
  951. if (delta.tv64 < mindelta.tv64)
  952. mindelta.tv64 = delta.tv64;
  953. }
  954. }
  955. spin_unlock_irqrestore(&cpu_base->lock, flags);
  956. if (mindelta.tv64 < 0)
  957. mindelta.tv64 = 0;
  958. return mindelta;
  959. }
  960. #endif
  961. static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  962. enum hrtimer_mode mode)
  963. {
  964. struct hrtimer_cpu_base *cpu_base;
  965. memset(timer, 0, sizeof(struct hrtimer));
  966. cpu_base = &__raw_get_cpu_var(hrtimer_bases);
  967. if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
  968. clock_id = CLOCK_MONOTONIC;
  969. timer->base = &cpu_base->clock_base[clock_id];
  970. hrtimer_init_timer_hres(timer);
  971. #ifdef CONFIG_TIMER_STATS
  972. timer->start_site = NULL;
  973. timer->start_pid = -1;
  974. memset(timer->start_comm, 0, TASK_COMM_LEN);
  975. #endif
  976. }
  977. /**
  978. * hrtimer_init - initialize a timer to the given clock
  979. * @timer: the timer to be initialized
  980. * @clock_id: the clock to be used
  981. * @mode: timer mode abs/rel
  982. */
  983. void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  984. enum hrtimer_mode mode)
  985. {
  986. debug_init(timer, clock_id, mode);
  987. __hrtimer_init(timer, clock_id, mode);
  988. }
  989. EXPORT_SYMBOL_GPL(hrtimer_init);
  990. /**
  991. * hrtimer_get_res - get the timer resolution for a clock
  992. * @which_clock: which clock to query
  993. * @tp: pointer to timespec variable to store the resolution
  994. *
  995. * Store the resolution of the clock selected by @which_clock in the
  996. * variable pointed to by @tp.
  997. */
  998. int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
  999. {
  1000. struct hrtimer_cpu_base *cpu_base;
  1001. cpu_base = &__raw_get_cpu_var(hrtimer_bases);
  1002. *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
  1003. return 0;
  1004. }
  1005. EXPORT_SYMBOL_GPL(hrtimer_get_res);
  1006. static void __run_hrtimer(struct hrtimer *timer, ktime_t *now)
  1007. {
  1008. struct hrtimer_clock_base *base = timer->base;
  1009. struct hrtimer_cpu_base *cpu_base = base->cpu_base;
  1010. enum hrtimer_restart (*fn)(struct hrtimer *);
  1011. int restart;
  1012. WARN_ON(!irqs_disabled());
  1013. debug_deactivate(timer);
  1014. __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
  1015. timer_stats_account_hrtimer(timer);
  1016. fn = timer->function;
  1017. /*
  1018. * Because we run timers from hardirq context, there is no chance
  1019. * they get migrated to another cpu, therefore its safe to unlock
  1020. * the timer base.
  1021. */
  1022. spin_unlock(&cpu_base->lock);
  1023. trace_hrtimer_expire_entry(timer, now);
  1024. restart = fn(timer);
  1025. trace_hrtimer_expire_exit(timer);
  1026. spin_lock(&cpu_base->lock);
  1027. /*
  1028. * Note: We clear the CALLBACK bit after enqueue_hrtimer and
  1029. * we do not reprogramm the event hardware. Happens either in
  1030. * hrtimer_start_range_ns() or in hrtimer_interrupt()
  1031. */
  1032. if (restart != HRTIMER_NORESTART) {
  1033. BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
  1034. enqueue_hrtimer(timer, base);
  1035. }
  1036. timer->state &= ~HRTIMER_STATE_CALLBACK;
  1037. }
  1038. #ifdef CONFIG_HIGH_RES_TIMERS
  1039. static int force_clock_reprogram;
  1040. /*
  1041. * After 5 iteration's attempts, we consider that hrtimer_interrupt()
  1042. * is hanging, which could happen with something that slows the interrupt
  1043. * such as the tracing. Then we force the clock reprogramming for each future
  1044. * hrtimer interrupts to avoid infinite loops and use the min_delta_ns
  1045. * threshold that we will overwrite.
  1046. * The next tick event will be scheduled to 3 times we currently spend on
  1047. * hrtimer_interrupt(). This gives a good compromise, the cpus will spend
  1048. * 1/4 of their time to process the hrtimer interrupts. This is enough to
  1049. * let it running without serious starvation.
  1050. */
  1051. static inline void
  1052. hrtimer_interrupt_hanging(struct clock_event_device *dev,
  1053. ktime_t try_time)
  1054. {
  1055. force_clock_reprogram = 1;
  1056. dev->min_delta_ns = (unsigned long)try_time.tv64 * 3;
  1057. printk(KERN_WARNING "hrtimer: interrupt too slow, "
  1058. "forcing clock min delta to %llu ns\n",
  1059. (unsigned long long) dev->min_delta_ns);
  1060. }
  1061. /*
  1062. * High resolution timer interrupt
  1063. * Called with interrupts disabled
  1064. */
  1065. void hrtimer_interrupt(struct clock_event_device *dev)
  1066. {
  1067. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  1068. struct hrtimer_clock_base *base;
  1069. ktime_t expires_next, now;
  1070. int nr_retries = 0;
  1071. int i;
  1072. BUG_ON(!cpu_base->hres_active);
  1073. cpu_base->nr_events++;
  1074. dev->next_event.tv64 = KTIME_MAX;
  1075. retry:
  1076. /* 5 retries is enough to notice a hang */
  1077. if (!(++nr_retries % 5))
  1078. hrtimer_interrupt_hanging(dev, ktime_sub(ktime_get(), now));
  1079. now = ktime_get();
  1080. expires_next.tv64 = KTIME_MAX;
  1081. spin_lock(&cpu_base->lock);
  1082. /*
  1083. * We set expires_next to KTIME_MAX here with cpu_base->lock
  1084. * held to prevent that a timer is enqueued in our queue via
  1085. * the migration code. This does not affect enqueueing of
  1086. * timers which run their callback and need to be requeued on
  1087. * this CPU.
  1088. */
  1089. cpu_base->expires_next.tv64 = KTIME_MAX;
  1090. base = cpu_base->clock_base;
  1091. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  1092. ktime_t basenow;
  1093. struct rb_node *node;
  1094. basenow = ktime_add(now, base->offset);
  1095. while ((node = base->first)) {
  1096. struct hrtimer *timer;
  1097. timer = rb_entry(node, struct hrtimer, node);
  1098. /*
  1099. * The immediate goal for using the softexpires is
  1100. * minimizing wakeups, not running timers at the
  1101. * earliest interrupt after their soft expiration.
  1102. * This allows us to avoid using a Priority Search
  1103. * Tree, which can answer a stabbing querry for
  1104. * overlapping intervals and instead use the simple
  1105. * BST we already have.
  1106. * We don't add extra wakeups by delaying timers that
  1107. * are right-of a not yet expired timer, because that
  1108. * timer will have to trigger a wakeup anyway.
  1109. */
  1110. if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
  1111. ktime_t expires;
  1112. expires = ktime_sub(hrtimer_get_expires(timer),
  1113. base->offset);
  1114. if (expires.tv64 < expires_next.tv64)
  1115. expires_next = expires;
  1116. break;
  1117. }
  1118. __run_hrtimer(timer, &basenow);
  1119. }
  1120. base++;
  1121. }
  1122. /*
  1123. * Store the new expiry value so the migration code can verify
  1124. * against it.
  1125. */
  1126. cpu_base->expires_next = expires_next;
  1127. spin_unlock(&cpu_base->lock);
  1128. /* Reprogramming necessary ? */
  1129. if (expires_next.tv64 != KTIME_MAX) {
  1130. if (tick_program_event(expires_next, force_clock_reprogram))
  1131. goto retry;
  1132. }
  1133. }
  1134. /*
  1135. * local version of hrtimer_peek_ahead_timers() called with interrupts
  1136. * disabled.
  1137. */
  1138. static void __hrtimer_peek_ahead_timers(void)
  1139. {
  1140. struct tick_device *td;
  1141. if (!hrtimer_hres_active())
  1142. return;
  1143. td = &__get_cpu_var(tick_cpu_device);
  1144. if (td && td->evtdev)
  1145. hrtimer_interrupt(td->evtdev);
  1146. }
  1147. /**
  1148. * hrtimer_peek_ahead_timers -- run soft-expired timers now
  1149. *
  1150. * hrtimer_peek_ahead_timers will peek at the timer queue of
  1151. * the current cpu and check if there are any timers for which
  1152. * the soft expires time has passed. If any such timers exist,
  1153. * they are run immediately and then removed from the timer queue.
  1154. *
  1155. */
  1156. void hrtimer_peek_ahead_timers(void)
  1157. {
  1158. unsigned long flags;
  1159. local_irq_save(flags);
  1160. __hrtimer_peek_ahead_timers();
  1161. local_irq_restore(flags);
  1162. }
  1163. static void run_hrtimer_softirq(struct softirq_action *h)
  1164. {
  1165. hrtimer_peek_ahead_timers();
  1166. }
  1167. #else /* CONFIG_HIGH_RES_TIMERS */
  1168. static inline void __hrtimer_peek_ahead_timers(void) { }
  1169. #endif /* !CONFIG_HIGH_RES_TIMERS */
  1170. /*
  1171. * Called from timer softirq every jiffy, expire hrtimers:
  1172. *
  1173. * For HRT its the fall back code to run the softirq in the timer
  1174. * softirq context in case the hrtimer initialization failed or has
  1175. * not been done yet.
  1176. */
  1177. void hrtimer_run_pending(void)
  1178. {
  1179. if (hrtimer_hres_active())
  1180. return;
  1181. /*
  1182. * This _is_ ugly: We have to check in the softirq context,
  1183. * whether we can switch to highres and / or nohz mode. The
  1184. * clocksource switch happens in the timer interrupt with
  1185. * xtime_lock held. Notification from there only sets the
  1186. * check bit in the tick_oneshot code, otherwise we might
  1187. * deadlock vs. xtime_lock.
  1188. */
  1189. if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
  1190. hrtimer_switch_to_hres();
  1191. }
  1192. /*
  1193. * Called from hardirq context every jiffy
  1194. */
  1195. void hrtimer_run_queues(void)
  1196. {
  1197. struct rb_node *node;
  1198. struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
  1199. struct hrtimer_clock_base *base;
  1200. int index, gettime = 1;
  1201. if (hrtimer_hres_active())
  1202. return;
  1203. for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
  1204. base = &cpu_base->clock_base[index];
  1205. if (!base->first)
  1206. continue;
  1207. if (gettime) {
  1208. hrtimer_get_softirq_time(cpu_base);
  1209. gettime = 0;
  1210. }
  1211. spin_lock(&cpu_base->lock);
  1212. while ((node = base->first)) {
  1213. struct hrtimer *timer;
  1214. timer = rb_entry(node, struct hrtimer, node);
  1215. if (base->softirq_time.tv64 <=
  1216. hrtimer_get_expires_tv64(timer))
  1217. break;
  1218. __run_hrtimer(timer, &base->softirq_time);
  1219. }
  1220. spin_unlock(&cpu_base->lock);
  1221. }
  1222. }
  1223. /*
  1224. * Sleep related functions:
  1225. */
  1226. static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
  1227. {
  1228. struct hrtimer_sleeper *t =
  1229. container_of(timer, struct hrtimer_sleeper, timer);
  1230. struct task_struct *task = t->task;
  1231. t->task = NULL;
  1232. if (task)
  1233. wake_up_process(task);
  1234. return HRTIMER_NORESTART;
  1235. }
  1236. void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
  1237. {
  1238. sl->timer.function = hrtimer_wakeup;
  1239. sl->task = task;
  1240. }
  1241. EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
  1242. static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
  1243. {
  1244. hrtimer_init_sleeper(t, current);
  1245. do {
  1246. set_current_state(TASK_INTERRUPTIBLE);
  1247. hrtimer_start_expires(&t->timer, mode);
  1248. if (!hrtimer_active(&t->timer))
  1249. t->task = NULL;
  1250. if (likely(t->task))
  1251. schedule();
  1252. hrtimer_cancel(&t->timer);
  1253. mode = HRTIMER_MODE_ABS;
  1254. } while (t->task && !signal_pending(current));
  1255. __set_current_state(TASK_RUNNING);
  1256. return t->task == NULL;
  1257. }
  1258. static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
  1259. {
  1260. struct timespec rmt;
  1261. ktime_t rem;
  1262. rem = hrtimer_expires_remaining(timer);
  1263. if (rem.tv64 <= 0)
  1264. return 0;
  1265. rmt = ktime_to_timespec(rem);
  1266. if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
  1267. return -EFAULT;
  1268. return 1;
  1269. }
  1270. long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
  1271. {
  1272. struct hrtimer_sleeper t;
  1273. struct timespec __user *rmtp;
  1274. int ret = 0;
  1275. hrtimer_init_on_stack(&t.timer, restart->nanosleep.index,
  1276. HRTIMER_MODE_ABS);
  1277. hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
  1278. if (do_nanosleep(&t, HRTIMER_MODE_ABS))
  1279. goto out;
  1280. rmtp = restart->nanosleep.rmtp;
  1281. if (rmtp) {
  1282. ret = update_rmtp(&t.timer, rmtp);
  1283. if (ret <= 0)
  1284. goto out;
  1285. }
  1286. /* The other values in restart are already filled in */
  1287. ret = -ERESTART_RESTARTBLOCK;
  1288. out:
  1289. destroy_hrtimer_on_stack(&t.timer);
  1290. return ret;
  1291. }
  1292. long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  1293. const enum hrtimer_mode mode, const clockid_t clockid)
  1294. {
  1295. struct restart_block *restart;
  1296. struct hrtimer_sleeper t;
  1297. int ret = 0;
  1298. unsigned long slack;
  1299. slack = current->timer_slack_ns;
  1300. if (rt_task(current))
  1301. slack = 0;
  1302. hrtimer_init_on_stack(&t.timer, clockid, mode);
  1303. hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
  1304. if (do_nanosleep(&t, mode))
  1305. goto out;
  1306. /* Absolute timers do not update the rmtp value and restart: */
  1307. if (mode == HRTIMER_MODE_ABS) {
  1308. ret = -ERESTARTNOHAND;
  1309. goto out;
  1310. }
  1311. if (rmtp) {
  1312. ret = update_rmtp(&t.timer, rmtp);
  1313. if (ret <= 0)
  1314. goto out;
  1315. }
  1316. restart = &current_thread_info()->restart_block;
  1317. restart->fn = hrtimer_nanosleep_restart;
  1318. restart->nanosleep.index = t.timer.base->index;
  1319. restart->nanosleep.rmtp = rmtp;
  1320. restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
  1321. ret = -ERESTART_RESTARTBLOCK;
  1322. out:
  1323. destroy_hrtimer_on_stack(&t.timer);
  1324. return ret;
  1325. }
  1326. SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
  1327. struct timespec __user *, rmtp)
  1328. {
  1329. struct timespec tu;
  1330. if (copy_from_user(&tu, rqtp, sizeof(tu)))
  1331. return -EFAULT;
  1332. if (!timespec_valid(&tu))
  1333. return -EINVAL;
  1334. return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
  1335. }
  1336. /*
  1337. * Functions related to boot-time initialization:
  1338. */
  1339. static void __cpuinit init_hrtimers_cpu(int cpu)
  1340. {
  1341. struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
  1342. int i;
  1343. spin_lock_init(&cpu_base->lock);
  1344. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
  1345. cpu_base->clock_base[i].cpu_base = cpu_base;
  1346. hrtimer_init_hres(cpu_base);
  1347. }
  1348. #ifdef CONFIG_HOTPLUG_CPU
  1349. static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
  1350. struct hrtimer_clock_base *new_base)
  1351. {
  1352. struct hrtimer *timer;
  1353. struct rb_node *node;
  1354. while ((node = rb_first(&old_base->active))) {
  1355. timer = rb_entry(node, struct hrtimer, node);
  1356. BUG_ON(hrtimer_callback_running(timer));
  1357. debug_deactivate(timer);
  1358. /*
  1359. * Mark it as STATE_MIGRATE not INACTIVE otherwise the
  1360. * timer could be seen as !active and just vanish away
  1361. * under us on another CPU
  1362. */
  1363. __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
  1364. timer->base = new_base;
  1365. /*
  1366. * Enqueue the timers on the new cpu. This does not
  1367. * reprogram the event device in case the timer
  1368. * expires before the earliest on this CPU, but we run
  1369. * hrtimer_interrupt after we migrated everything to
  1370. * sort out already expired timers and reprogram the
  1371. * event device.
  1372. */
  1373. enqueue_hrtimer(timer, new_base);
  1374. /* Clear the migration state bit */
  1375. timer->state &= ~HRTIMER_STATE_MIGRATE;
  1376. }
  1377. }
  1378. static void migrate_hrtimers(int scpu)
  1379. {
  1380. struct hrtimer_cpu_base *old_base, *new_base;
  1381. int i;
  1382. BUG_ON(cpu_online(scpu));
  1383. tick_cancel_sched_timer(scpu);
  1384. local_irq_disable();
  1385. old_base = &per_cpu(hrtimer_bases, scpu);
  1386. new_base = &__get_cpu_var(hrtimer_bases);
  1387. /*
  1388. * The caller is globally serialized and nobody else
  1389. * takes two locks at once, deadlock is not possible.
  1390. */
  1391. spin_lock(&new_base->lock);
  1392. spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
  1393. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  1394. migrate_hrtimer_list(&old_base->clock_base[i],
  1395. &new_base->clock_base[i]);
  1396. }
  1397. spin_unlock(&old_base->lock);
  1398. spin_unlock(&new_base->lock);
  1399. /* Check, if we got expired work to do */
  1400. __hrtimer_peek_ahead_timers();
  1401. local_irq_enable();
  1402. }
  1403. #endif /* CONFIG_HOTPLUG_CPU */
  1404. static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
  1405. unsigned long action, void *hcpu)
  1406. {
  1407. int scpu = (long)hcpu;
  1408. switch (action) {
  1409. case CPU_UP_PREPARE:
  1410. case CPU_UP_PREPARE_FROZEN:
  1411. init_hrtimers_cpu(scpu);
  1412. break;
  1413. #ifdef CONFIG_HOTPLUG_CPU
  1414. case CPU_DYING:
  1415. case CPU_DYING_FROZEN:
  1416. clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu);
  1417. break;
  1418. case CPU_DEAD:
  1419. case CPU_DEAD_FROZEN:
  1420. {
  1421. clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
  1422. migrate_hrtimers(scpu);
  1423. break;
  1424. }
  1425. #endif
  1426. default:
  1427. break;
  1428. }
  1429. return NOTIFY_OK;
  1430. }
  1431. static struct notifier_block __cpuinitdata hrtimers_nb = {
  1432. .notifier_call = hrtimer_cpu_notify,
  1433. };
  1434. void __init hrtimers_init(void)
  1435. {
  1436. hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
  1437. (void *)(long)smp_processor_id());
  1438. register_cpu_notifier(&hrtimers_nb);
  1439. #ifdef CONFIG_HIGH_RES_TIMERS
  1440. open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
  1441. #endif
  1442. }
  1443. /**
  1444. * schedule_hrtimeout_range - sleep until timeout
  1445. * @expires: timeout value (ktime_t)
  1446. * @delta: slack in expires timeout (ktime_t)
  1447. * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
  1448. *
  1449. * Make the current task sleep until the given expiry time has
  1450. * elapsed. The routine will return immediately unless
  1451. * the current task state has been set (see set_current_state()).
  1452. *
  1453. * The @delta argument gives the kernel the freedom to schedule the
  1454. * actual wakeup to a time that is both power and performance friendly.
  1455. * The kernel give the normal best effort behavior for "@expires+@delta",
  1456. * but may decide to fire the timer earlier, but no earlier than @expires.
  1457. *
  1458. * You can set the task state as follows -
  1459. *
  1460. * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
  1461. * pass before the routine returns.
  1462. *
  1463. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  1464. * delivered to the current task.
  1465. *
  1466. * The current task state is guaranteed to be TASK_RUNNING when this
  1467. * routine returns.
  1468. *
  1469. * Returns 0 when the timer has expired otherwise -EINTR
  1470. */
  1471. int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
  1472. const enum hrtimer_mode mode)
  1473. {
  1474. struct hrtimer_sleeper t;
  1475. /*
  1476. * Optimize when a zero timeout value is given. It does not
  1477. * matter whether this is an absolute or a relative time.
  1478. */
  1479. if (expires && !expires->tv64) {
  1480. __set_current_state(TASK_RUNNING);
  1481. return 0;
  1482. }
  1483. /*
  1484. * A NULL parameter means "inifinte"
  1485. */
  1486. if (!expires) {
  1487. schedule();
  1488. __set_current_state(TASK_RUNNING);
  1489. return -EINTR;
  1490. }
  1491. hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, mode);
  1492. hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
  1493. hrtimer_init_sleeper(&t, current);
  1494. hrtimer_start_expires(&t.timer, mode);
  1495. if (!hrtimer_active(&t.timer))
  1496. t.task = NULL;
  1497. if (likely(t.task))
  1498. schedule();
  1499. hrtimer_cancel(&t.timer);
  1500. destroy_hrtimer_on_stack(&t.timer);
  1501. __set_current_state(TASK_RUNNING);
  1502. return !t.task ? 0 : -EINTR;
  1503. }
  1504. EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
  1505. /**
  1506. * schedule_hrtimeout - sleep until timeout
  1507. * @expires: timeout value (ktime_t)
  1508. * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
  1509. *
  1510. * Make the current task sleep until the given expiry time has
  1511. * elapsed. The routine will return immediately unless
  1512. * the current task state has been set (see set_current_state()).
  1513. *
  1514. * You can set the task state as follows -
  1515. *
  1516. * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
  1517. * pass before the routine returns.
  1518. *
  1519. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  1520. * delivered to the current task.
  1521. *
  1522. * The current task state is guaranteed to be TASK_RUNNING when this
  1523. * routine returns.
  1524. *
  1525. * Returns 0 when the timer has expired otherwise -EINTR
  1526. */
  1527. int __sched schedule_hrtimeout(ktime_t *expires,
  1528. const enum hrtimer_mode mode)
  1529. {
  1530. return schedule_hrtimeout_range(expires, 0, mode);
  1531. }
  1532. EXPORT_SYMBOL_GPL(schedule_hrtimeout);