hrtimer.c 43 KB

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