hrtimer.c 44 KB

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