hrtimer.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032
  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/interrupt.h>
  40. #include <linux/tick.h>
  41. #include <linux/seq_file.h>
  42. #include <linux/err.h>
  43. #include <linux/debugobjects.h>
  44. #include <linux/sched/signal.h>
  45. #include <linux/sched/sysctl.h>
  46. #include <linux/sched/rt.h>
  47. #include <linux/sched/deadline.h>
  48. #include <linux/sched/nohz.h>
  49. #include <linux/sched/debug.h>
  50. #include <linux/timer.h>
  51. #include <linux/freezer.h>
  52. #include <linux/compat.h>
  53. #include <linux/uaccess.h>
  54. #include <trace/events/timer.h>
  55. #include "tick-internal.h"
  56. /*
  57. * Masks for selecting the soft and hard context timers from
  58. * cpu_base->active
  59. */
  60. #define MASK_SHIFT (HRTIMER_BASE_MONOTONIC_SOFT)
  61. #define HRTIMER_ACTIVE_HARD ((1U << MASK_SHIFT) - 1)
  62. #define HRTIMER_ACTIVE_SOFT (HRTIMER_ACTIVE_HARD << MASK_SHIFT)
  63. #define HRTIMER_ACTIVE_ALL (HRTIMER_ACTIVE_SOFT | HRTIMER_ACTIVE_HARD)
  64. /*
  65. * The timer bases:
  66. *
  67. * There are more clockids than hrtimer bases. Thus, we index
  68. * into the timer bases by the hrtimer_base_type enum. When trying
  69. * to reach a base using a clockid, hrtimer_clockid_to_base()
  70. * is used to convert from clockid to the proper hrtimer_base_type.
  71. */
  72. DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
  73. {
  74. .lock = __RAW_SPIN_LOCK_UNLOCKED(hrtimer_bases.lock),
  75. .clock_base =
  76. {
  77. {
  78. .index = HRTIMER_BASE_MONOTONIC,
  79. .clockid = CLOCK_MONOTONIC,
  80. .get_time = &ktime_get,
  81. },
  82. {
  83. .index = HRTIMER_BASE_REALTIME,
  84. .clockid = CLOCK_REALTIME,
  85. .get_time = &ktime_get_real,
  86. },
  87. {
  88. .index = HRTIMER_BASE_BOOTTIME,
  89. .clockid = CLOCK_BOOTTIME,
  90. .get_time = &ktime_get_boottime,
  91. },
  92. {
  93. .index = HRTIMER_BASE_TAI,
  94. .clockid = CLOCK_TAI,
  95. .get_time = &ktime_get_clocktai,
  96. },
  97. {
  98. .index = HRTIMER_BASE_MONOTONIC_SOFT,
  99. .clockid = CLOCK_MONOTONIC,
  100. .get_time = &ktime_get,
  101. },
  102. {
  103. .index = HRTIMER_BASE_REALTIME_SOFT,
  104. .clockid = CLOCK_REALTIME,
  105. .get_time = &ktime_get_real,
  106. },
  107. {
  108. .index = HRTIMER_BASE_BOOTTIME_SOFT,
  109. .clockid = CLOCK_BOOTTIME,
  110. .get_time = &ktime_get_boottime,
  111. },
  112. {
  113. .index = HRTIMER_BASE_TAI_SOFT,
  114. .clockid = CLOCK_TAI,
  115. .get_time = &ktime_get_clocktai,
  116. },
  117. }
  118. };
  119. static const int hrtimer_clock_to_base_table[MAX_CLOCKS] = {
  120. /* Make sure we catch unsupported clockids */
  121. [0 ... MAX_CLOCKS - 1] = HRTIMER_MAX_CLOCK_BASES,
  122. [CLOCK_REALTIME] = HRTIMER_BASE_REALTIME,
  123. [CLOCK_MONOTONIC] = HRTIMER_BASE_MONOTONIC,
  124. [CLOCK_BOOTTIME] = HRTIMER_BASE_BOOTTIME,
  125. [CLOCK_TAI] = HRTIMER_BASE_TAI,
  126. };
  127. /*
  128. * Functions and macros which are different for UP/SMP systems are kept in a
  129. * single place
  130. */
  131. #ifdef CONFIG_SMP
  132. /*
  133. * We require the migration_base for lock_hrtimer_base()/switch_hrtimer_base()
  134. * such that hrtimer_callback_running() can unconditionally dereference
  135. * timer->base->cpu_base
  136. */
  137. static struct hrtimer_cpu_base migration_cpu_base = {
  138. .clock_base = { { .cpu_base = &migration_cpu_base, }, },
  139. };
  140. #define migration_base migration_cpu_base.clock_base[0]
  141. /*
  142. * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
  143. * means that all timers which are tied to this base via timer->base are
  144. * locked, and the base itself is locked too.
  145. *
  146. * So __run_timers/migrate_timers can safely modify all timers which could
  147. * be found on the lists/queues.
  148. *
  149. * When the timer's base is locked, and the timer removed from list, it is
  150. * possible to set timer->base = &migration_base and drop the lock: the timer
  151. * remains locked.
  152. */
  153. static
  154. struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
  155. unsigned long *flags)
  156. {
  157. struct hrtimer_clock_base *base;
  158. for (;;) {
  159. base = timer->base;
  160. if (likely(base != &migration_base)) {
  161. raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
  162. if (likely(base == timer->base))
  163. return base;
  164. /* The timer has migrated to another CPU: */
  165. raw_spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
  166. }
  167. cpu_relax();
  168. }
  169. }
  170. /*
  171. * We do not migrate the timer when it is expiring before the next
  172. * event on the target cpu. When high resolution is enabled, we cannot
  173. * reprogram the target cpu hardware and we would cause it to fire
  174. * late. To keep it simple, we handle the high resolution enabled and
  175. * disabled case similar.
  176. *
  177. * Called with cpu_base->lock of target cpu held.
  178. */
  179. static int
  180. hrtimer_check_target(struct hrtimer *timer, struct hrtimer_clock_base *new_base)
  181. {
  182. ktime_t expires;
  183. expires = ktime_sub(hrtimer_get_expires(timer), new_base->offset);
  184. return expires < new_base->cpu_base->expires_next;
  185. }
  186. static inline
  187. struct hrtimer_cpu_base *get_target_base(struct hrtimer_cpu_base *base,
  188. int pinned)
  189. {
  190. #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
  191. if (static_branch_likely(&timers_migration_enabled) && !pinned)
  192. return &per_cpu(hrtimer_bases, get_nohz_timer_target());
  193. #endif
  194. return base;
  195. }
  196. /*
  197. * We switch the timer base to a power-optimized selected CPU target,
  198. * if:
  199. * - NO_HZ_COMMON is enabled
  200. * - timer migration is enabled
  201. * - the timer callback is not running
  202. * - the timer is not the first expiring timer on the new target
  203. *
  204. * If one of the above requirements is not fulfilled we move the timer
  205. * to the current CPU or leave it on the previously assigned CPU if
  206. * the timer callback is currently running.
  207. */
  208. static inline struct hrtimer_clock_base *
  209. switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
  210. int pinned)
  211. {
  212. struct hrtimer_cpu_base *new_cpu_base, *this_cpu_base;
  213. struct hrtimer_clock_base *new_base;
  214. int basenum = base->index;
  215. this_cpu_base = this_cpu_ptr(&hrtimer_bases);
  216. new_cpu_base = get_target_base(this_cpu_base, pinned);
  217. again:
  218. new_base = &new_cpu_base->clock_base[basenum];
  219. if (base != new_base) {
  220. /*
  221. * We are trying to move timer to new_base.
  222. * However we can't change timer's base while it is running,
  223. * so we keep it on the same CPU. No hassle vs. reprogramming
  224. * the event source in the high resolution case. The softirq
  225. * code will take care of this when the timer function has
  226. * completed. There is no conflict as we hold the lock until
  227. * the timer is enqueued.
  228. */
  229. if (unlikely(hrtimer_callback_running(timer)))
  230. return base;
  231. /* See the comment in lock_hrtimer_base() */
  232. timer->base = &migration_base;
  233. raw_spin_unlock(&base->cpu_base->lock);
  234. raw_spin_lock(&new_base->cpu_base->lock);
  235. if (new_cpu_base != this_cpu_base &&
  236. hrtimer_check_target(timer, new_base)) {
  237. raw_spin_unlock(&new_base->cpu_base->lock);
  238. raw_spin_lock(&base->cpu_base->lock);
  239. new_cpu_base = this_cpu_base;
  240. timer->base = base;
  241. goto again;
  242. }
  243. timer->base = new_base;
  244. } else {
  245. if (new_cpu_base != this_cpu_base &&
  246. hrtimer_check_target(timer, new_base)) {
  247. new_cpu_base = this_cpu_base;
  248. goto again;
  249. }
  250. }
  251. return new_base;
  252. }
  253. #else /* CONFIG_SMP */
  254. static inline struct hrtimer_clock_base *
  255. lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
  256. {
  257. struct hrtimer_clock_base *base = timer->base;
  258. raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
  259. return base;
  260. }
  261. # define switch_hrtimer_base(t, b, p) (b)
  262. #endif /* !CONFIG_SMP */
  263. /*
  264. * Functions for the union type storage format of ktime_t which are
  265. * too large for inlining:
  266. */
  267. #if BITS_PER_LONG < 64
  268. /*
  269. * Divide a ktime value by a nanosecond value
  270. */
  271. s64 __ktime_divns(const ktime_t kt, s64 div)
  272. {
  273. int sft = 0;
  274. s64 dclc;
  275. u64 tmp;
  276. dclc = ktime_to_ns(kt);
  277. tmp = dclc < 0 ? -dclc : dclc;
  278. /* Make sure the divisor is less than 2^32: */
  279. while (div >> 32) {
  280. sft++;
  281. div >>= 1;
  282. }
  283. tmp >>= sft;
  284. do_div(tmp, (unsigned long) div);
  285. return dclc < 0 ? -tmp : tmp;
  286. }
  287. EXPORT_SYMBOL_GPL(__ktime_divns);
  288. #endif /* BITS_PER_LONG >= 64 */
  289. /*
  290. * Add two ktime values and do a safety check for overflow:
  291. */
  292. ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
  293. {
  294. ktime_t res = ktime_add_unsafe(lhs, rhs);
  295. /*
  296. * We use KTIME_SEC_MAX here, the maximum timeout which we can
  297. * return to user space in a timespec:
  298. */
  299. if (res < 0 || res < lhs || res < rhs)
  300. res = ktime_set(KTIME_SEC_MAX, 0);
  301. return res;
  302. }
  303. EXPORT_SYMBOL_GPL(ktime_add_safe);
  304. #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
  305. static struct debug_obj_descr hrtimer_debug_descr;
  306. static void *hrtimer_debug_hint(void *addr)
  307. {
  308. return ((struct hrtimer *) addr)->function;
  309. }
  310. /*
  311. * fixup_init is called when:
  312. * - an active object is initialized
  313. */
  314. static bool hrtimer_fixup_init(void *addr, enum debug_obj_state state)
  315. {
  316. struct hrtimer *timer = addr;
  317. switch (state) {
  318. case ODEBUG_STATE_ACTIVE:
  319. hrtimer_cancel(timer);
  320. debug_object_init(timer, &hrtimer_debug_descr);
  321. return true;
  322. default:
  323. return false;
  324. }
  325. }
  326. /*
  327. * fixup_activate is called when:
  328. * - an active object is activated
  329. * - an unknown non-static object is activated
  330. */
  331. static bool hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
  332. {
  333. switch (state) {
  334. case ODEBUG_STATE_ACTIVE:
  335. WARN_ON(1);
  336. default:
  337. return false;
  338. }
  339. }
  340. /*
  341. * fixup_free is called when:
  342. * - an active object is freed
  343. */
  344. static bool hrtimer_fixup_free(void *addr, enum debug_obj_state state)
  345. {
  346. struct hrtimer *timer = addr;
  347. switch (state) {
  348. case ODEBUG_STATE_ACTIVE:
  349. hrtimer_cancel(timer);
  350. debug_object_free(timer, &hrtimer_debug_descr);
  351. return true;
  352. default:
  353. return false;
  354. }
  355. }
  356. static struct debug_obj_descr hrtimer_debug_descr = {
  357. .name = "hrtimer",
  358. .debug_hint = hrtimer_debug_hint,
  359. .fixup_init = hrtimer_fixup_init,
  360. .fixup_activate = hrtimer_fixup_activate,
  361. .fixup_free = hrtimer_fixup_free,
  362. };
  363. static inline void debug_hrtimer_init(struct hrtimer *timer)
  364. {
  365. debug_object_init(timer, &hrtimer_debug_descr);
  366. }
  367. static inline void debug_hrtimer_activate(struct hrtimer *timer,
  368. enum hrtimer_mode mode)
  369. {
  370. debug_object_activate(timer, &hrtimer_debug_descr);
  371. }
  372. static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
  373. {
  374. debug_object_deactivate(timer, &hrtimer_debug_descr);
  375. }
  376. static inline void debug_hrtimer_free(struct hrtimer *timer)
  377. {
  378. debug_object_free(timer, &hrtimer_debug_descr);
  379. }
  380. static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  381. enum hrtimer_mode mode);
  382. void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
  383. enum hrtimer_mode mode)
  384. {
  385. debug_object_init_on_stack(timer, &hrtimer_debug_descr);
  386. __hrtimer_init(timer, clock_id, mode);
  387. }
  388. EXPORT_SYMBOL_GPL(hrtimer_init_on_stack);
  389. void destroy_hrtimer_on_stack(struct hrtimer *timer)
  390. {
  391. debug_object_free(timer, &hrtimer_debug_descr);
  392. }
  393. EXPORT_SYMBOL_GPL(destroy_hrtimer_on_stack);
  394. #else
  395. static inline void debug_hrtimer_init(struct hrtimer *timer) { }
  396. static inline void debug_hrtimer_activate(struct hrtimer *timer,
  397. enum hrtimer_mode mode) { }
  398. static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
  399. #endif
  400. static inline void
  401. debug_init(struct hrtimer *timer, clockid_t clockid,
  402. enum hrtimer_mode mode)
  403. {
  404. debug_hrtimer_init(timer);
  405. trace_hrtimer_init(timer, clockid, mode);
  406. }
  407. static inline void debug_activate(struct hrtimer *timer,
  408. enum hrtimer_mode mode)
  409. {
  410. debug_hrtimer_activate(timer, mode);
  411. trace_hrtimer_start(timer, mode);
  412. }
  413. static inline void debug_deactivate(struct hrtimer *timer)
  414. {
  415. debug_hrtimer_deactivate(timer);
  416. trace_hrtimer_cancel(timer);
  417. }
  418. static struct hrtimer_clock_base *
  419. __next_base(struct hrtimer_cpu_base *cpu_base, unsigned int *active)
  420. {
  421. unsigned int idx;
  422. if (!*active)
  423. return NULL;
  424. idx = __ffs(*active);
  425. *active &= ~(1U << idx);
  426. return &cpu_base->clock_base[idx];
  427. }
  428. #define for_each_active_base(base, cpu_base, active) \
  429. while ((base = __next_base((cpu_base), &(active))))
  430. static ktime_t __hrtimer_next_event_base(struct hrtimer_cpu_base *cpu_base,
  431. const struct hrtimer *exclude,
  432. unsigned int active,
  433. ktime_t expires_next)
  434. {
  435. struct hrtimer_clock_base *base;
  436. ktime_t expires;
  437. for_each_active_base(base, cpu_base, active) {
  438. struct timerqueue_node *next;
  439. struct hrtimer *timer;
  440. next = timerqueue_getnext(&base->active);
  441. timer = container_of(next, struct hrtimer, node);
  442. if (timer == exclude) {
  443. /* Get to the next timer in the queue. */
  444. next = timerqueue_iterate_next(next);
  445. if (!next)
  446. continue;
  447. timer = container_of(next, struct hrtimer, node);
  448. }
  449. expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
  450. if (expires < expires_next) {
  451. expires_next = expires;
  452. /* Skip cpu_base update if a timer is being excluded. */
  453. if (exclude)
  454. continue;
  455. if (timer->is_soft)
  456. cpu_base->softirq_next_timer = timer;
  457. else
  458. cpu_base->next_timer = timer;
  459. }
  460. }
  461. /*
  462. * clock_was_set() might have changed base->offset of any of
  463. * the clock bases so the result might be negative. Fix it up
  464. * to prevent a false positive in clockevents_program_event().
  465. */
  466. if (expires_next < 0)
  467. expires_next = 0;
  468. return expires_next;
  469. }
  470. /*
  471. * Recomputes cpu_base::*next_timer and returns the earliest expires_next but
  472. * does not set cpu_base::*expires_next, that is done by hrtimer_reprogram.
  473. *
  474. * When a softirq is pending, we can ignore the HRTIMER_ACTIVE_SOFT bases,
  475. * those timers will get run whenever the softirq gets handled, at the end of
  476. * hrtimer_run_softirq(), hrtimer_update_softirq_timer() will re-add these bases.
  477. *
  478. * Therefore softirq values are those from the HRTIMER_ACTIVE_SOFT clock bases.
  479. * The !softirq values are the minima across HRTIMER_ACTIVE_ALL, unless an actual
  480. * softirq is pending, in which case they're the minima of HRTIMER_ACTIVE_HARD.
  481. *
  482. * @active_mask must be one of:
  483. * - HRTIMER_ACTIVE_ALL,
  484. * - HRTIMER_ACTIVE_SOFT, or
  485. * - HRTIMER_ACTIVE_HARD.
  486. */
  487. static ktime_t
  488. __hrtimer_get_next_event(struct hrtimer_cpu_base *cpu_base, unsigned int active_mask)
  489. {
  490. unsigned int active;
  491. struct hrtimer *next_timer = NULL;
  492. ktime_t expires_next = KTIME_MAX;
  493. if (!cpu_base->softirq_activated && (active_mask & HRTIMER_ACTIVE_SOFT)) {
  494. active = cpu_base->active_bases & HRTIMER_ACTIVE_SOFT;
  495. cpu_base->softirq_next_timer = NULL;
  496. expires_next = __hrtimer_next_event_base(cpu_base, NULL,
  497. active, KTIME_MAX);
  498. next_timer = cpu_base->softirq_next_timer;
  499. }
  500. if (active_mask & HRTIMER_ACTIVE_HARD) {
  501. active = cpu_base->active_bases & HRTIMER_ACTIVE_HARD;
  502. cpu_base->next_timer = next_timer;
  503. expires_next = __hrtimer_next_event_base(cpu_base, NULL, active,
  504. expires_next);
  505. }
  506. return expires_next;
  507. }
  508. static inline ktime_t hrtimer_update_base(struct hrtimer_cpu_base *base)
  509. {
  510. ktime_t *offs_real = &base->clock_base[HRTIMER_BASE_REALTIME].offset;
  511. ktime_t *offs_boot = &base->clock_base[HRTIMER_BASE_BOOTTIME].offset;
  512. ktime_t *offs_tai = &base->clock_base[HRTIMER_BASE_TAI].offset;
  513. ktime_t now = ktime_get_update_offsets_now(&base->clock_was_set_seq,
  514. offs_real, offs_boot, offs_tai);
  515. base->clock_base[HRTIMER_BASE_REALTIME_SOFT].offset = *offs_real;
  516. base->clock_base[HRTIMER_BASE_BOOTTIME_SOFT].offset = *offs_boot;
  517. base->clock_base[HRTIMER_BASE_TAI_SOFT].offset = *offs_tai;
  518. return now;
  519. }
  520. /*
  521. * Is the high resolution mode active ?
  522. */
  523. static inline int __hrtimer_hres_active(struct hrtimer_cpu_base *cpu_base)
  524. {
  525. return IS_ENABLED(CONFIG_HIGH_RES_TIMERS) ?
  526. cpu_base->hres_active : 0;
  527. }
  528. static inline int hrtimer_hres_active(void)
  529. {
  530. return __hrtimer_hres_active(this_cpu_ptr(&hrtimer_bases));
  531. }
  532. /*
  533. * Reprogram the event source with checking both queues for the
  534. * next event
  535. * Called with interrupts disabled and base->lock held
  536. */
  537. static void
  538. hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
  539. {
  540. ktime_t expires_next;
  541. /*
  542. * Find the current next expiration time.
  543. */
  544. expires_next = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_ALL);
  545. if (cpu_base->next_timer && cpu_base->next_timer->is_soft) {
  546. /*
  547. * When the softirq is activated, hrtimer has to be
  548. * programmed with the first hard hrtimer because soft
  549. * timer interrupt could occur too late.
  550. */
  551. if (cpu_base->softirq_activated)
  552. expires_next = __hrtimer_get_next_event(cpu_base,
  553. HRTIMER_ACTIVE_HARD);
  554. else
  555. cpu_base->softirq_expires_next = expires_next;
  556. }
  557. if (skip_equal && expires_next == cpu_base->expires_next)
  558. return;
  559. cpu_base->expires_next = expires_next;
  560. /*
  561. * If hres is not active, hardware does not have to be
  562. * reprogrammed yet.
  563. *
  564. * If a hang was detected in the last timer interrupt then we
  565. * leave the hang delay active in the hardware. We want the
  566. * system to make progress. That also prevents the following
  567. * scenario:
  568. * T1 expires 50ms from now
  569. * T2 expires 5s from now
  570. *
  571. * T1 is removed, so this code is called and would reprogram
  572. * the hardware to 5s from now. Any hrtimer_start after that
  573. * will not reprogram the hardware due to hang_detected being
  574. * set. So we'd effectivly block all timers until the T2 event
  575. * fires.
  576. */
  577. if (!__hrtimer_hres_active(cpu_base) || cpu_base->hang_detected)
  578. return;
  579. tick_program_event(cpu_base->expires_next, 1);
  580. }
  581. /* High resolution timer related functions */
  582. #ifdef CONFIG_HIGH_RES_TIMERS
  583. /*
  584. * High resolution timer enabled ?
  585. */
  586. static bool hrtimer_hres_enabled __read_mostly = true;
  587. unsigned int hrtimer_resolution __read_mostly = LOW_RES_NSEC;
  588. EXPORT_SYMBOL_GPL(hrtimer_resolution);
  589. /*
  590. * Enable / Disable high resolution mode
  591. */
  592. static int __init setup_hrtimer_hres(char *str)
  593. {
  594. return (kstrtobool(str, &hrtimer_hres_enabled) == 0);
  595. }
  596. __setup("highres=", setup_hrtimer_hres);
  597. /*
  598. * hrtimer_high_res_enabled - query, if the highres mode is enabled
  599. */
  600. static inline int hrtimer_is_hres_enabled(void)
  601. {
  602. return hrtimer_hres_enabled;
  603. }
  604. /*
  605. * Retrigger next event is called after clock was set
  606. *
  607. * Called with interrupts disabled via on_each_cpu()
  608. */
  609. static void retrigger_next_event(void *arg)
  610. {
  611. struct hrtimer_cpu_base *base = this_cpu_ptr(&hrtimer_bases);
  612. if (!__hrtimer_hres_active(base))
  613. return;
  614. raw_spin_lock(&base->lock);
  615. hrtimer_update_base(base);
  616. hrtimer_force_reprogram(base, 0);
  617. raw_spin_unlock(&base->lock);
  618. }
  619. /*
  620. * Switch to high resolution mode
  621. */
  622. static void hrtimer_switch_to_hres(void)
  623. {
  624. struct hrtimer_cpu_base *base = this_cpu_ptr(&hrtimer_bases);
  625. if (tick_init_highres()) {
  626. pr_warn("Could not switch to high resolution mode on CPU %u\n",
  627. base->cpu);
  628. return;
  629. }
  630. base->hres_active = 1;
  631. hrtimer_resolution = HIGH_RES_NSEC;
  632. tick_setup_sched_timer();
  633. /* "Retrigger" the interrupt to get things going */
  634. retrigger_next_event(NULL);
  635. }
  636. static void clock_was_set_work(struct work_struct *work)
  637. {
  638. clock_was_set();
  639. }
  640. static DECLARE_WORK(hrtimer_work, clock_was_set_work);
  641. /*
  642. * Called from timekeeping and resume code to reprogram the hrtimer
  643. * interrupt device on all cpus.
  644. */
  645. void clock_was_set_delayed(void)
  646. {
  647. schedule_work(&hrtimer_work);
  648. }
  649. #else
  650. static inline int hrtimer_is_hres_enabled(void) { return 0; }
  651. static inline void hrtimer_switch_to_hres(void) { }
  652. static inline void retrigger_next_event(void *arg) { }
  653. #endif /* CONFIG_HIGH_RES_TIMERS */
  654. /*
  655. * When a timer is enqueued and expires earlier than the already enqueued
  656. * timers, we have to check, whether it expires earlier than the timer for
  657. * which the clock event device was armed.
  658. *
  659. * Called with interrupts disabled and base->cpu_base.lock held
  660. */
  661. static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram)
  662. {
  663. struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
  664. struct hrtimer_clock_base *base = timer->base;
  665. ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
  666. WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
  667. /*
  668. * CLOCK_REALTIME timer might be requested with an absolute
  669. * expiry time which is less than base->offset. Set it to 0.
  670. */
  671. if (expires < 0)
  672. expires = 0;
  673. if (timer->is_soft) {
  674. /*
  675. * soft hrtimer could be started on a remote CPU. In this
  676. * case softirq_expires_next needs to be updated on the
  677. * remote CPU. The soft hrtimer will not expire before the
  678. * first hard hrtimer on the remote CPU -
  679. * hrtimer_check_target() prevents this case.
  680. */
  681. struct hrtimer_cpu_base *timer_cpu_base = base->cpu_base;
  682. if (timer_cpu_base->softirq_activated)
  683. return;
  684. if (!ktime_before(expires, timer_cpu_base->softirq_expires_next))
  685. return;
  686. timer_cpu_base->softirq_next_timer = timer;
  687. timer_cpu_base->softirq_expires_next = expires;
  688. if (!ktime_before(expires, timer_cpu_base->expires_next) ||
  689. !reprogram)
  690. return;
  691. }
  692. /*
  693. * If the timer is not on the current cpu, we cannot reprogram
  694. * the other cpus clock event device.
  695. */
  696. if (base->cpu_base != cpu_base)
  697. return;
  698. /*
  699. * If the hrtimer interrupt is running, then it will
  700. * reevaluate the clock bases and reprogram the clock event
  701. * device. The callbacks are always executed in hard interrupt
  702. * context so we don't need an extra check for a running
  703. * callback.
  704. */
  705. if (cpu_base->in_hrtirq)
  706. return;
  707. if (expires >= cpu_base->expires_next)
  708. return;
  709. /* Update the pointer to the next expiring timer */
  710. cpu_base->next_timer = timer;
  711. cpu_base->expires_next = expires;
  712. /*
  713. * If hres is not active, hardware does not have to be
  714. * programmed yet.
  715. *
  716. * If a hang was detected in the last timer interrupt then we
  717. * do not schedule a timer which is earlier than the expiry
  718. * which we enforced in the hang detection. We want the system
  719. * to make progress.
  720. */
  721. if (!__hrtimer_hres_active(cpu_base) || cpu_base->hang_detected)
  722. return;
  723. /*
  724. * Program the timer hardware. We enforce the expiry for
  725. * events which are already in the past.
  726. */
  727. tick_program_event(expires, 1);
  728. }
  729. /*
  730. * Clock realtime was set
  731. *
  732. * Change the offset of the realtime clock vs. the monotonic
  733. * clock.
  734. *
  735. * We might have to reprogram the high resolution timer interrupt. On
  736. * SMP we call the architecture specific code to retrigger _all_ high
  737. * resolution timer interrupts. On UP we just disable interrupts and
  738. * call the high resolution interrupt code.
  739. */
  740. void clock_was_set(void)
  741. {
  742. #ifdef CONFIG_HIGH_RES_TIMERS
  743. /* Retrigger the CPU local events everywhere */
  744. on_each_cpu(retrigger_next_event, NULL, 1);
  745. #endif
  746. timerfd_clock_was_set();
  747. }
  748. /*
  749. * During resume we might have to reprogram the high resolution timer
  750. * interrupt on all online CPUs. However, all other CPUs will be
  751. * stopped with IRQs interrupts disabled so the clock_was_set() call
  752. * must be deferred.
  753. */
  754. void hrtimers_resume(void)
  755. {
  756. lockdep_assert_irqs_disabled();
  757. /* Retrigger on the local CPU */
  758. retrigger_next_event(NULL);
  759. /* And schedule a retrigger for all others */
  760. clock_was_set_delayed();
  761. }
  762. /*
  763. * Counterpart to lock_hrtimer_base above:
  764. */
  765. static inline
  766. void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
  767. {
  768. raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
  769. }
  770. /**
  771. * hrtimer_forward - forward the timer expiry
  772. * @timer: hrtimer to forward
  773. * @now: forward past this time
  774. * @interval: the interval to forward
  775. *
  776. * Forward the timer expiry so it will expire in the future.
  777. * Returns the number of overruns.
  778. *
  779. * Can be safely called from the callback function of @timer. If
  780. * called from other contexts @timer must neither be enqueued nor
  781. * running the callback and the caller needs to take care of
  782. * serialization.
  783. *
  784. * Note: This only updates the timer expiry value and does not requeue
  785. * the timer.
  786. */
  787. u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
  788. {
  789. u64 orun = 1;
  790. ktime_t delta;
  791. delta = ktime_sub(now, hrtimer_get_expires(timer));
  792. if (delta < 0)
  793. return 0;
  794. if (WARN_ON(timer->state & HRTIMER_STATE_ENQUEUED))
  795. return 0;
  796. if (interval < hrtimer_resolution)
  797. interval = hrtimer_resolution;
  798. if (unlikely(delta >= interval)) {
  799. s64 incr = ktime_to_ns(interval);
  800. orun = ktime_divns(delta, incr);
  801. hrtimer_add_expires_ns(timer, incr * orun);
  802. if (hrtimer_get_expires_tv64(timer) > now)
  803. return orun;
  804. /*
  805. * This (and the ktime_add() below) is the
  806. * correction for exact:
  807. */
  808. orun++;
  809. }
  810. hrtimer_add_expires(timer, interval);
  811. return orun;
  812. }
  813. EXPORT_SYMBOL_GPL(hrtimer_forward);
  814. /*
  815. * enqueue_hrtimer - internal function to (re)start a timer
  816. *
  817. * The timer is inserted in expiry order. Insertion into the
  818. * red black tree is O(log(n)). Must hold the base lock.
  819. *
  820. * Returns 1 when the new timer is the leftmost timer in the tree.
  821. */
  822. static int enqueue_hrtimer(struct hrtimer *timer,
  823. struct hrtimer_clock_base *base,
  824. enum hrtimer_mode mode)
  825. {
  826. debug_activate(timer, mode);
  827. base->cpu_base->active_bases |= 1 << base->index;
  828. /* Pairs with the lockless read in hrtimer_is_queued() */
  829. WRITE_ONCE(timer->state, HRTIMER_STATE_ENQUEUED);
  830. return timerqueue_add(&base->active, &timer->node);
  831. }
  832. /*
  833. * __remove_hrtimer - internal function to remove a timer
  834. *
  835. * Caller must hold the base lock.
  836. *
  837. * High resolution timer mode reprograms the clock event device when the
  838. * timer is the one which expires next. The caller can disable this by setting
  839. * reprogram to zero. This is useful, when the context does a reprogramming
  840. * anyway (e.g. timer interrupt)
  841. */
  842. static void __remove_hrtimer(struct hrtimer *timer,
  843. struct hrtimer_clock_base *base,
  844. u8 newstate, int reprogram)
  845. {
  846. struct hrtimer_cpu_base *cpu_base = base->cpu_base;
  847. u8 state = timer->state;
  848. /* Pairs with the lockless read in hrtimer_is_queued() */
  849. WRITE_ONCE(timer->state, newstate);
  850. if (!(state & HRTIMER_STATE_ENQUEUED))
  851. return;
  852. if (!timerqueue_del(&base->active, &timer->node))
  853. cpu_base->active_bases &= ~(1 << base->index);
  854. /*
  855. * Note: If reprogram is false we do not update
  856. * cpu_base->next_timer. This happens when we remove the first
  857. * timer on a remote cpu. No harm as we never dereference
  858. * cpu_base->next_timer. So the worst thing what can happen is
  859. * an superflous call to hrtimer_force_reprogram() on the
  860. * remote cpu later on if the same timer gets enqueued again.
  861. */
  862. if (reprogram && timer == cpu_base->next_timer)
  863. hrtimer_force_reprogram(cpu_base, 1);
  864. }
  865. /*
  866. * remove hrtimer, called with base lock held
  867. */
  868. static inline int
  869. remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base, bool restart)
  870. {
  871. u8 state = timer->state;
  872. if (state & HRTIMER_STATE_ENQUEUED) {
  873. int reprogram;
  874. /*
  875. * Remove the timer and force reprogramming when high
  876. * resolution mode is active and the timer is on the current
  877. * CPU. If we remove a timer on another CPU, reprogramming is
  878. * skipped. The interrupt event on this CPU is fired and
  879. * reprogramming happens in the interrupt handler. This is a
  880. * rare case and less expensive than a smp call.
  881. */
  882. debug_deactivate(timer);
  883. reprogram = base->cpu_base == this_cpu_ptr(&hrtimer_bases);
  884. if (!restart)
  885. state = HRTIMER_STATE_INACTIVE;
  886. __remove_hrtimer(timer, base, state, reprogram);
  887. return 1;
  888. }
  889. return 0;
  890. }
  891. static inline ktime_t hrtimer_update_lowres(struct hrtimer *timer, ktime_t tim,
  892. const enum hrtimer_mode mode)
  893. {
  894. #ifdef CONFIG_TIME_LOW_RES
  895. /*
  896. * CONFIG_TIME_LOW_RES indicates that the system has no way to return
  897. * granular time values. For relative timers we add hrtimer_resolution
  898. * (i.e. one jiffie) to prevent short timeouts.
  899. */
  900. timer->is_rel = mode & HRTIMER_MODE_REL;
  901. if (timer->is_rel)
  902. tim = ktime_add_safe(tim, hrtimer_resolution);
  903. #endif
  904. return tim;
  905. }
  906. static void
  907. hrtimer_update_softirq_timer(struct hrtimer_cpu_base *cpu_base, bool reprogram)
  908. {
  909. ktime_t expires;
  910. /*
  911. * Find the next SOFT expiration.
  912. */
  913. expires = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_SOFT);
  914. /*
  915. * reprogramming needs to be triggered, even if the next soft
  916. * hrtimer expires at the same time than the next hard
  917. * hrtimer. cpu_base->softirq_expires_next needs to be updated!
  918. */
  919. if (expires == KTIME_MAX)
  920. return;
  921. /*
  922. * cpu_base->*next_timer is recomputed by __hrtimer_get_next_event()
  923. * cpu_base->*expires_next is only set by hrtimer_reprogram()
  924. */
  925. hrtimer_reprogram(cpu_base->softirq_next_timer, reprogram);
  926. }
  927. static int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
  928. u64 delta_ns, const enum hrtimer_mode mode,
  929. struct hrtimer_clock_base *base)
  930. {
  931. struct hrtimer_clock_base *new_base;
  932. /* Remove an active timer from the queue: */
  933. remove_hrtimer(timer, base, true);
  934. if (mode & HRTIMER_MODE_REL)
  935. tim = ktime_add_safe(tim, base->get_time());
  936. tim = hrtimer_update_lowres(timer, tim, mode);
  937. hrtimer_set_expires_range_ns(timer, tim, delta_ns);
  938. /* Switch the timer base, if necessary: */
  939. new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
  940. return enqueue_hrtimer(timer, new_base, mode);
  941. }
  942. /**
  943. * hrtimer_start_range_ns - (re)start an hrtimer
  944. * @timer: the timer to be added
  945. * @tim: expiry time
  946. * @delta_ns: "slack" range for the timer
  947. * @mode: timer mode: absolute (HRTIMER_MODE_ABS) or
  948. * relative (HRTIMER_MODE_REL), and pinned (HRTIMER_MODE_PINNED);
  949. * softirq based mode is considered for debug purpose only!
  950. */
  951. void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
  952. u64 delta_ns, const enum hrtimer_mode mode)
  953. {
  954. struct hrtimer_clock_base *base;
  955. unsigned long flags;
  956. /*
  957. * Check whether the HRTIMER_MODE_SOFT bit and hrtimer.is_soft
  958. * match.
  959. */
  960. WARN_ON_ONCE(!(mode & HRTIMER_MODE_SOFT) ^ !timer->is_soft);
  961. base = lock_hrtimer_base(timer, &flags);
  962. if (__hrtimer_start_range_ns(timer, tim, delta_ns, mode, base))
  963. hrtimer_reprogram(timer, true);
  964. unlock_hrtimer_base(timer, &flags);
  965. }
  966. EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
  967. /**
  968. * hrtimer_try_to_cancel - try to deactivate a timer
  969. * @timer: hrtimer to stop
  970. *
  971. * Returns:
  972. * 0 when the timer was not active
  973. * 1 when the timer was active
  974. * -1 when the timer is currently executing the callback function and
  975. * cannot be stopped
  976. */
  977. int hrtimer_try_to_cancel(struct hrtimer *timer)
  978. {
  979. struct hrtimer_clock_base *base;
  980. unsigned long flags;
  981. int ret = -1;
  982. /*
  983. * Check lockless first. If the timer is not active (neither
  984. * enqueued nor running the callback, nothing to do here. The
  985. * base lock does not serialize against a concurrent enqueue,
  986. * so we can avoid taking it.
  987. */
  988. if (!hrtimer_active(timer))
  989. return 0;
  990. base = lock_hrtimer_base(timer, &flags);
  991. if (!hrtimer_callback_running(timer))
  992. ret = remove_hrtimer(timer, base, false);
  993. unlock_hrtimer_base(timer, &flags);
  994. return ret;
  995. }
  996. EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
  997. /**
  998. * hrtimer_cancel - cancel a timer and wait for the handler to finish.
  999. * @timer: the timer to be cancelled
  1000. *
  1001. * Returns:
  1002. * 0 when the timer was not active
  1003. * 1 when the timer was active
  1004. */
  1005. int hrtimer_cancel(struct hrtimer *timer)
  1006. {
  1007. for (;;) {
  1008. int ret = hrtimer_try_to_cancel(timer);
  1009. if (ret >= 0)
  1010. return ret;
  1011. cpu_relax();
  1012. }
  1013. }
  1014. EXPORT_SYMBOL_GPL(hrtimer_cancel);
  1015. /**
  1016. * hrtimer_get_remaining - get remaining time for the timer
  1017. * @timer: the timer to read
  1018. * @adjust: adjust relative timers when CONFIG_TIME_LOW_RES=y
  1019. */
  1020. ktime_t __hrtimer_get_remaining(const struct hrtimer *timer, bool adjust)
  1021. {
  1022. unsigned long flags;
  1023. ktime_t rem;
  1024. lock_hrtimer_base(timer, &flags);
  1025. if (IS_ENABLED(CONFIG_TIME_LOW_RES) && adjust)
  1026. rem = hrtimer_expires_remaining_adjusted(timer);
  1027. else
  1028. rem = hrtimer_expires_remaining(timer);
  1029. unlock_hrtimer_base(timer, &flags);
  1030. return rem;
  1031. }
  1032. EXPORT_SYMBOL_GPL(__hrtimer_get_remaining);
  1033. #ifdef CONFIG_NO_HZ_COMMON
  1034. /**
  1035. * hrtimer_get_next_event - get the time until next expiry event
  1036. *
  1037. * Returns the next expiry time or KTIME_MAX if no timer is pending.
  1038. */
  1039. u64 hrtimer_get_next_event(void)
  1040. {
  1041. struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
  1042. u64 expires = KTIME_MAX;
  1043. unsigned long flags;
  1044. raw_spin_lock_irqsave(&cpu_base->lock, flags);
  1045. if (!__hrtimer_hres_active(cpu_base))
  1046. expires = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_ALL);
  1047. raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
  1048. return expires;
  1049. }
  1050. /**
  1051. * hrtimer_next_event_without - time until next expiry event w/o one timer
  1052. * @exclude: timer to exclude
  1053. *
  1054. * Returns the next expiry time over all timers except for the @exclude one or
  1055. * KTIME_MAX if none of them is pending.
  1056. */
  1057. u64 hrtimer_next_event_without(const struct hrtimer *exclude)
  1058. {
  1059. struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
  1060. u64 expires = KTIME_MAX;
  1061. unsigned long flags;
  1062. raw_spin_lock_irqsave(&cpu_base->lock, flags);
  1063. if (__hrtimer_hres_active(cpu_base)) {
  1064. unsigned int active;
  1065. if (!cpu_base->softirq_activated) {
  1066. active = cpu_base->active_bases & HRTIMER_ACTIVE_SOFT;
  1067. expires = __hrtimer_next_event_base(cpu_base, exclude,
  1068. active, KTIME_MAX);
  1069. }
  1070. active = cpu_base->active_bases & HRTIMER_ACTIVE_HARD;
  1071. expires = __hrtimer_next_event_base(cpu_base, exclude, active,
  1072. expires);
  1073. }
  1074. raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
  1075. return expires;
  1076. }
  1077. #endif
  1078. static inline int hrtimer_clockid_to_base(clockid_t clock_id)
  1079. {
  1080. if (likely(clock_id < MAX_CLOCKS)) {
  1081. int base = hrtimer_clock_to_base_table[clock_id];
  1082. if (likely(base != HRTIMER_MAX_CLOCK_BASES))
  1083. return base;
  1084. }
  1085. WARN(1, "Invalid clockid %d. Using MONOTONIC\n", clock_id);
  1086. return HRTIMER_BASE_MONOTONIC;
  1087. }
  1088. static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  1089. enum hrtimer_mode mode)
  1090. {
  1091. bool softtimer = !!(mode & HRTIMER_MODE_SOFT);
  1092. int base = softtimer ? HRTIMER_MAX_CLOCK_BASES / 2 : 0;
  1093. struct hrtimer_cpu_base *cpu_base;
  1094. memset(timer, 0, sizeof(struct hrtimer));
  1095. cpu_base = raw_cpu_ptr(&hrtimer_bases);
  1096. /*
  1097. * POSIX magic: Relative CLOCK_REALTIME timers are not affected by
  1098. * clock modifications, so they needs to become CLOCK_MONOTONIC to
  1099. * ensure POSIX compliance.
  1100. */
  1101. if (clock_id == CLOCK_REALTIME && mode & HRTIMER_MODE_REL)
  1102. clock_id = CLOCK_MONOTONIC;
  1103. base += hrtimer_clockid_to_base(clock_id);
  1104. timer->is_soft = softtimer;
  1105. timer->base = &cpu_base->clock_base[base];
  1106. timerqueue_init(&timer->node);
  1107. }
  1108. /**
  1109. * hrtimer_init - initialize a timer to the given clock
  1110. * @timer: the timer to be initialized
  1111. * @clock_id: the clock to be used
  1112. * @mode: The modes which are relevant for intitialization:
  1113. * HRTIMER_MODE_ABS, HRTIMER_MODE_REL, HRTIMER_MODE_ABS_SOFT,
  1114. * HRTIMER_MODE_REL_SOFT
  1115. *
  1116. * The PINNED variants of the above can be handed in,
  1117. * but the PINNED bit is ignored as pinning happens
  1118. * when the hrtimer is started
  1119. */
  1120. void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  1121. enum hrtimer_mode mode)
  1122. {
  1123. debug_init(timer, clock_id, mode);
  1124. __hrtimer_init(timer, clock_id, mode);
  1125. }
  1126. EXPORT_SYMBOL_GPL(hrtimer_init);
  1127. /*
  1128. * A timer is active, when it is enqueued into the rbtree or the
  1129. * callback function is running or it's in the state of being migrated
  1130. * to another cpu.
  1131. *
  1132. * It is important for this function to not return a false negative.
  1133. */
  1134. bool hrtimer_active(const struct hrtimer *timer)
  1135. {
  1136. struct hrtimer_clock_base *base;
  1137. unsigned int seq;
  1138. do {
  1139. base = READ_ONCE(timer->base);
  1140. seq = raw_read_seqcount_begin(&base->seq);
  1141. if (timer->state != HRTIMER_STATE_INACTIVE ||
  1142. base->running == timer)
  1143. return true;
  1144. } while (read_seqcount_retry(&base->seq, seq) ||
  1145. base != READ_ONCE(timer->base));
  1146. return false;
  1147. }
  1148. EXPORT_SYMBOL_GPL(hrtimer_active);
  1149. /*
  1150. * The write_seqcount_barrier()s in __run_hrtimer() split the thing into 3
  1151. * distinct sections:
  1152. *
  1153. * - queued: the timer is queued
  1154. * - callback: the timer is being ran
  1155. * - post: the timer is inactive or (re)queued
  1156. *
  1157. * On the read side we ensure we observe timer->state and cpu_base->running
  1158. * from the same section, if anything changed while we looked at it, we retry.
  1159. * This includes timer->base changing because sequence numbers alone are
  1160. * insufficient for that.
  1161. *
  1162. * The sequence numbers are required because otherwise we could still observe
  1163. * a false negative if the read side got smeared over multiple consequtive
  1164. * __run_hrtimer() invocations.
  1165. */
  1166. static void __run_hrtimer(struct hrtimer_cpu_base *cpu_base,
  1167. struct hrtimer_clock_base *base,
  1168. struct hrtimer *timer, ktime_t *now,
  1169. unsigned long flags)
  1170. {
  1171. enum hrtimer_restart (*fn)(struct hrtimer *);
  1172. int restart;
  1173. lockdep_assert_held(&cpu_base->lock);
  1174. debug_deactivate(timer);
  1175. base->running = timer;
  1176. /*
  1177. * Separate the ->running assignment from the ->state assignment.
  1178. *
  1179. * As with a regular write barrier, this ensures the read side in
  1180. * hrtimer_active() cannot observe base->running == NULL &&
  1181. * timer->state == INACTIVE.
  1182. */
  1183. raw_write_seqcount_barrier(&base->seq);
  1184. __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE, 0);
  1185. fn = timer->function;
  1186. /*
  1187. * Clear the 'is relative' flag for the TIME_LOW_RES case. If the
  1188. * timer is restarted with a period then it becomes an absolute
  1189. * timer. If its not restarted it does not matter.
  1190. */
  1191. if (IS_ENABLED(CONFIG_TIME_LOW_RES))
  1192. timer->is_rel = false;
  1193. /*
  1194. * The timer is marked as running in the CPU base, so it is
  1195. * protected against migration to a different CPU even if the lock
  1196. * is dropped.
  1197. */
  1198. raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
  1199. trace_hrtimer_expire_entry(timer, now);
  1200. restart = fn(timer);
  1201. trace_hrtimer_expire_exit(timer);
  1202. raw_spin_lock_irq(&cpu_base->lock);
  1203. /*
  1204. * Note: We clear the running state after enqueue_hrtimer and
  1205. * we do not reprogram the event hardware. Happens either in
  1206. * hrtimer_start_range_ns() or in hrtimer_interrupt()
  1207. *
  1208. * Note: Because we dropped the cpu_base->lock above,
  1209. * hrtimer_start_range_ns() can have popped in and enqueued the timer
  1210. * for us already.
  1211. */
  1212. if (restart != HRTIMER_NORESTART &&
  1213. !(timer->state & HRTIMER_STATE_ENQUEUED))
  1214. enqueue_hrtimer(timer, base, HRTIMER_MODE_ABS);
  1215. /*
  1216. * Separate the ->running assignment from the ->state assignment.
  1217. *
  1218. * As with a regular write barrier, this ensures the read side in
  1219. * hrtimer_active() cannot observe base->running.timer == NULL &&
  1220. * timer->state == INACTIVE.
  1221. */
  1222. raw_write_seqcount_barrier(&base->seq);
  1223. WARN_ON_ONCE(base->running != timer);
  1224. base->running = NULL;
  1225. }
  1226. static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now,
  1227. unsigned long flags, unsigned int active_mask)
  1228. {
  1229. struct hrtimer_clock_base *base;
  1230. unsigned int active = cpu_base->active_bases & active_mask;
  1231. for_each_active_base(base, cpu_base, active) {
  1232. struct timerqueue_node *node;
  1233. ktime_t basenow;
  1234. basenow = ktime_add(now, base->offset);
  1235. while ((node = timerqueue_getnext(&base->active))) {
  1236. struct hrtimer *timer;
  1237. timer = container_of(node, struct hrtimer, node);
  1238. /*
  1239. * The immediate goal for using the softexpires is
  1240. * minimizing wakeups, not running timers at the
  1241. * earliest interrupt after their soft expiration.
  1242. * This allows us to avoid using a Priority Search
  1243. * Tree, which can answer a stabbing querry for
  1244. * overlapping intervals and instead use the simple
  1245. * BST we already have.
  1246. * We don't add extra wakeups by delaying timers that
  1247. * are right-of a not yet expired timer, because that
  1248. * timer will have to trigger a wakeup anyway.
  1249. */
  1250. if (basenow < hrtimer_get_softexpires_tv64(timer))
  1251. break;
  1252. __run_hrtimer(cpu_base, base, timer, &basenow, flags);
  1253. }
  1254. }
  1255. }
  1256. static __latent_entropy void hrtimer_run_softirq(struct softirq_action *h)
  1257. {
  1258. struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
  1259. unsigned long flags;
  1260. ktime_t now;
  1261. raw_spin_lock_irqsave(&cpu_base->lock, flags);
  1262. now = hrtimer_update_base(cpu_base);
  1263. __hrtimer_run_queues(cpu_base, now, flags, HRTIMER_ACTIVE_SOFT);
  1264. cpu_base->softirq_activated = 0;
  1265. hrtimer_update_softirq_timer(cpu_base, true);
  1266. raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
  1267. }
  1268. #ifdef CONFIG_HIGH_RES_TIMERS
  1269. /*
  1270. * High resolution timer interrupt
  1271. * Called with interrupts disabled
  1272. */
  1273. void hrtimer_interrupt(struct clock_event_device *dev)
  1274. {
  1275. struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
  1276. ktime_t expires_next, now, entry_time, delta;
  1277. unsigned long flags;
  1278. int retries = 0;
  1279. BUG_ON(!cpu_base->hres_active);
  1280. cpu_base->nr_events++;
  1281. dev->next_event = KTIME_MAX;
  1282. raw_spin_lock_irqsave(&cpu_base->lock, flags);
  1283. entry_time = now = hrtimer_update_base(cpu_base);
  1284. retry:
  1285. cpu_base->in_hrtirq = 1;
  1286. /*
  1287. * We set expires_next to KTIME_MAX here with cpu_base->lock
  1288. * held to prevent that a timer is enqueued in our queue via
  1289. * the migration code. This does not affect enqueueing of
  1290. * timers which run their callback and need to be requeued on
  1291. * this CPU.
  1292. */
  1293. cpu_base->expires_next = KTIME_MAX;
  1294. if (!ktime_before(now, cpu_base->softirq_expires_next)) {
  1295. cpu_base->softirq_expires_next = KTIME_MAX;
  1296. cpu_base->softirq_activated = 1;
  1297. raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  1298. }
  1299. __hrtimer_run_queues(cpu_base, now, flags, HRTIMER_ACTIVE_HARD);
  1300. /* Reevaluate the clock bases for the next expiry */
  1301. expires_next = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_ALL);
  1302. /*
  1303. * Store the new expiry value so the migration code can verify
  1304. * against it.
  1305. */
  1306. cpu_base->expires_next = expires_next;
  1307. cpu_base->in_hrtirq = 0;
  1308. raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
  1309. /* Reprogramming necessary ? */
  1310. if (!tick_program_event(expires_next, 0)) {
  1311. cpu_base->hang_detected = 0;
  1312. return;
  1313. }
  1314. /*
  1315. * The next timer was already expired due to:
  1316. * - tracing
  1317. * - long lasting callbacks
  1318. * - being scheduled away when running in a VM
  1319. *
  1320. * We need to prevent that we loop forever in the hrtimer
  1321. * interrupt routine. We give it 3 attempts to avoid
  1322. * overreacting on some spurious event.
  1323. *
  1324. * Acquire base lock for updating the offsets and retrieving
  1325. * the current time.
  1326. */
  1327. raw_spin_lock_irqsave(&cpu_base->lock, flags);
  1328. now = hrtimer_update_base(cpu_base);
  1329. cpu_base->nr_retries++;
  1330. if (++retries < 3)
  1331. goto retry;
  1332. /*
  1333. * Give the system a chance to do something else than looping
  1334. * here. We stored the entry time, so we know exactly how long
  1335. * we spent here. We schedule the next event this amount of
  1336. * time away.
  1337. */
  1338. cpu_base->nr_hangs++;
  1339. cpu_base->hang_detected = 1;
  1340. raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
  1341. delta = ktime_sub(now, entry_time);
  1342. if ((unsigned int)delta > cpu_base->max_hang_time)
  1343. cpu_base->max_hang_time = (unsigned int) delta;
  1344. /*
  1345. * Limit it to a sensible value as we enforce a longer
  1346. * delay. Give the CPU at least 100ms to catch up.
  1347. */
  1348. if (delta > 100 * NSEC_PER_MSEC)
  1349. expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC);
  1350. else
  1351. expires_next = ktime_add(now, delta);
  1352. tick_program_event(expires_next, 1);
  1353. pr_warn_once("hrtimer: interrupt took %llu ns\n", ktime_to_ns(delta));
  1354. }
  1355. /* called with interrupts disabled */
  1356. static inline void __hrtimer_peek_ahead_timers(void)
  1357. {
  1358. struct tick_device *td;
  1359. if (!hrtimer_hres_active())
  1360. return;
  1361. td = this_cpu_ptr(&tick_cpu_device);
  1362. if (td && td->evtdev)
  1363. hrtimer_interrupt(td->evtdev);
  1364. }
  1365. #else /* CONFIG_HIGH_RES_TIMERS */
  1366. static inline void __hrtimer_peek_ahead_timers(void) { }
  1367. #endif /* !CONFIG_HIGH_RES_TIMERS */
  1368. /*
  1369. * Called from run_local_timers in hardirq context every jiffy
  1370. */
  1371. void hrtimer_run_queues(void)
  1372. {
  1373. struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
  1374. unsigned long flags;
  1375. ktime_t now;
  1376. if (__hrtimer_hres_active(cpu_base))
  1377. return;
  1378. /*
  1379. * This _is_ ugly: We have to check periodically, whether we
  1380. * can switch to highres and / or nohz mode. The clocksource
  1381. * switch happens with xtime_lock held. Notification from
  1382. * there only sets the check bit in the tick_oneshot code,
  1383. * otherwise we might deadlock vs. xtime_lock.
  1384. */
  1385. if (tick_check_oneshot_change(!hrtimer_is_hres_enabled())) {
  1386. hrtimer_switch_to_hres();
  1387. return;
  1388. }
  1389. raw_spin_lock_irqsave(&cpu_base->lock, flags);
  1390. now = hrtimer_update_base(cpu_base);
  1391. if (!ktime_before(now, cpu_base->softirq_expires_next)) {
  1392. cpu_base->softirq_expires_next = KTIME_MAX;
  1393. cpu_base->softirq_activated = 1;
  1394. raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  1395. }
  1396. __hrtimer_run_queues(cpu_base, now, flags, HRTIMER_ACTIVE_HARD);
  1397. raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
  1398. }
  1399. /*
  1400. * Sleep related functions:
  1401. */
  1402. static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
  1403. {
  1404. struct hrtimer_sleeper *t =
  1405. container_of(timer, struct hrtimer_sleeper, timer);
  1406. struct task_struct *task = t->task;
  1407. t->task = NULL;
  1408. if (task)
  1409. wake_up_process(task);
  1410. return HRTIMER_NORESTART;
  1411. }
  1412. void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
  1413. {
  1414. sl->timer.function = hrtimer_wakeup;
  1415. sl->task = task;
  1416. }
  1417. EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
  1418. int nanosleep_copyout(struct restart_block *restart, struct timespec64 *ts)
  1419. {
  1420. switch(restart->nanosleep.type) {
  1421. #ifdef CONFIG_COMPAT_32BIT_TIME
  1422. case TT_COMPAT:
  1423. if (compat_put_timespec64(ts, restart->nanosleep.compat_rmtp))
  1424. return -EFAULT;
  1425. break;
  1426. #endif
  1427. case TT_NATIVE:
  1428. if (put_timespec64(ts, restart->nanosleep.rmtp))
  1429. return -EFAULT;
  1430. break;
  1431. default:
  1432. BUG();
  1433. }
  1434. return -ERESTART_RESTARTBLOCK;
  1435. }
  1436. static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
  1437. {
  1438. struct restart_block *restart;
  1439. hrtimer_init_sleeper(t, current);
  1440. do {
  1441. set_current_state(TASK_INTERRUPTIBLE);
  1442. hrtimer_start_expires(&t->timer, mode);
  1443. if (likely(t->task))
  1444. freezable_schedule();
  1445. hrtimer_cancel(&t->timer);
  1446. mode = HRTIMER_MODE_ABS;
  1447. } while (t->task && !signal_pending(current));
  1448. __set_current_state(TASK_RUNNING);
  1449. if (!t->task)
  1450. return 0;
  1451. restart = &current->restart_block;
  1452. if (restart->nanosleep.type != TT_NONE) {
  1453. ktime_t rem = hrtimer_expires_remaining(&t->timer);
  1454. struct timespec64 rmt;
  1455. if (rem <= 0)
  1456. return 0;
  1457. rmt = ktime_to_timespec64(rem);
  1458. return nanosleep_copyout(restart, &rmt);
  1459. }
  1460. return -ERESTART_RESTARTBLOCK;
  1461. }
  1462. static long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
  1463. {
  1464. struct hrtimer_sleeper t;
  1465. int ret;
  1466. hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid,
  1467. HRTIMER_MODE_ABS);
  1468. hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
  1469. ret = do_nanosleep(&t, HRTIMER_MODE_ABS);
  1470. destroy_hrtimer_on_stack(&t.timer);
  1471. return ret;
  1472. }
  1473. long hrtimer_nanosleep(const struct timespec64 *rqtp,
  1474. const enum hrtimer_mode mode, const clockid_t clockid)
  1475. {
  1476. struct restart_block *restart;
  1477. struct hrtimer_sleeper t;
  1478. int ret = 0;
  1479. u64 slack;
  1480. slack = current->timer_slack_ns;
  1481. if (dl_task(current) || rt_task(current))
  1482. slack = 0;
  1483. hrtimer_init_on_stack(&t.timer, clockid, mode);
  1484. hrtimer_set_expires_range_ns(&t.timer, timespec64_to_ktime(*rqtp), slack);
  1485. ret = do_nanosleep(&t, mode);
  1486. if (ret != -ERESTART_RESTARTBLOCK)
  1487. goto out;
  1488. /* Absolute timers do not update the rmtp value and restart: */
  1489. if (mode == HRTIMER_MODE_ABS) {
  1490. ret = -ERESTARTNOHAND;
  1491. goto out;
  1492. }
  1493. restart = &current->restart_block;
  1494. restart->fn = hrtimer_nanosleep_restart;
  1495. restart->nanosleep.clockid = t.timer.base->clockid;
  1496. restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
  1497. out:
  1498. destroy_hrtimer_on_stack(&t.timer);
  1499. return ret;
  1500. }
  1501. #if !defined(CONFIG_64BIT_TIME) || defined(CONFIG_64BIT)
  1502. SYSCALL_DEFINE2(nanosleep, struct __kernel_timespec __user *, rqtp,
  1503. struct __kernel_timespec __user *, rmtp)
  1504. {
  1505. struct timespec64 tu;
  1506. if (get_timespec64(&tu, rqtp))
  1507. return -EFAULT;
  1508. if (!timespec64_valid(&tu))
  1509. return -EINVAL;
  1510. current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
  1511. current->restart_block.nanosleep.rmtp = rmtp;
  1512. return hrtimer_nanosleep(&tu, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
  1513. }
  1514. #endif
  1515. #ifdef CONFIG_COMPAT_32BIT_TIME
  1516. COMPAT_SYSCALL_DEFINE2(nanosleep, struct compat_timespec __user *, rqtp,
  1517. struct compat_timespec __user *, rmtp)
  1518. {
  1519. struct timespec64 tu;
  1520. if (compat_get_timespec64(&tu, rqtp))
  1521. return -EFAULT;
  1522. if (!timespec64_valid(&tu))
  1523. return -EINVAL;
  1524. current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
  1525. current->restart_block.nanosleep.compat_rmtp = rmtp;
  1526. return hrtimer_nanosleep(&tu, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
  1527. }
  1528. #endif
  1529. /*
  1530. * Functions related to boot-time initialization:
  1531. */
  1532. int hrtimers_prepare_cpu(unsigned int cpu)
  1533. {
  1534. struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
  1535. int i;
  1536. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  1537. cpu_base->clock_base[i].cpu_base = cpu_base;
  1538. timerqueue_init_head(&cpu_base->clock_base[i].active);
  1539. }
  1540. cpu_base->cpu = cpu;
  1541. cpu_base->active_bases = 0;
  1542. cpu_base->hres_active = 0;
  1543. cpu_base->hang_detected = 0;
  1544. cpu_base->next_timer = NULL;
  1545. cpu_base->softirq_next_timer = NULL;
  1546. cpu_base->expires_next = KTIME_MAX;
  1547. cpu_base->softirq_expires_next = KTIME_MAX;
  1548. return 0;
  1549. }
  1550. #ifdef CONFIG_HOTPLUG_CPU
  1551. static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
  1552. struct hrtimer_clock_base *new_base)
  1553. {
  1554. struct hrtimer *timer;
  1555. struct timerqueue_node *node;
  1556. while ((node = timerqueue_getnext(&old_base->active))) {
  1557. timer = container_of(node, struct hrtimer, node);
  1558. BUG_ON(hrtimer_callback_running(timer));
  1559. debug_deactivate(timer);
  1560. /*
  1561. * Mark it as ENQUEUED not INACTIVE otherwise the
  1562. * timer could be seen as !active and just vanish away
  1563. * under us on another CPU
  1564. */
  1565. __remove_hrtimer(timer, old_base, HRTIMER_STATE_ENQUEUED, 0);
  1566. timer->base = new_base;
  1567. /*
  1568. * Enqueue the timers on the new cpu. This does not
  1569. * reprogram the event device in case the timer
  1570. * expires before the earliest on this CPU, but we run
  1571. * hrtimer_interrupt after we migrated everything to
  1572. * sort out already expired timers and reprogram the
  1573. * event device.
  1574. */
  1575. enqueue_hrtimer(timer, new_base, HRTIMER_MODE_ABS);
  1576. }
  1577. }
  1578. int hrtimers_dead_cpu(unsigned int scpu)
  1579. {
  1580. struct hrtimer_cpu_base *old_base, *new_base;
  1581. int i;
  1582. BUG_ON(cpu_online(scpu));
  1583. tick_cancel_sched_timer(scpu);
  1584. /*
  1585. * this BH disable ensures that raise_softirq_irqoff() does
  1586. * not wakeup ksoftirqd (and acquire the pi-lock) while
  1587. * holding the cpu_base lock
  1588. */
  1589. local_bh_disable();
  1590. local_irq_disable();
  1591. old_base = &per_cpu(hrtimer_bases, scpu);
  1592. new_base = this_cpu_ptr(&hrtimer_bases);
  1593. /*
  1594. * The caller is globally serialized and nobody else
  1595. * takes two locks at once, deadlock is not possible.
  1596. */
  1597. raw_spin_lock(&new_base->lock);
  1598. raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
  1599. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  1600. migrate_hrtimer_list(&old_base->clock_base[i],
  1601. &new_base->clock_base[i]);
  1602. }
  1603. /*
  1604. * The migration might have changed the first expiring softirq
  1605. * timer on this CPU. Update it.
  1606. */
  1607. hrtimer_update_softirq_timer(new_base, false);
  1608. raw_spin_unlock(&old_base->lock);
  1609. raw_spin_unlock(&new_base->lock);
  1610. /* Check, if we got expired work to do */
  1611. __hrtimer_peek_ahead_timers();
  1612. local_irq_enable();
  1613. local_bh_enable();
  1614. return 0;
  1615. }
  1616. #endif /* CONFIG_HOTPLUG_CPU */
  1617. void __init hrtimers_init(void)
  1618. {
  1619. hrtimers_prepare_cpu(smp_processor_id());
  1620. open_softirq(HRTIMER_SOFTIRQ, hrtimer_run_softirq);
  1621. }
  1622. /**
  1623. * schedule_hrtimeout_range_clock - sleep until timeout
  1624. * @expires: timeout value (ktime_t)
  1625. * @delta: slack in expires timeout (ktime_t)
  1626. * @mode: timer mode
  1627. * @clock_id: timer clock to be used
  1628. */
  1629. int __sched
  1630. schedule_hrtimeout_range_clock(ktime_t *expires, u64 delta,
  1631. const enum hrtimer_mode mode, clockid_t clock_id)
  1632. {
  1633. struct hrtimer_sleeper t;
  1634. /*
  1635. * Optimize when a zero timeout value is given. It does not
  1636. * matter whether this is an absolute or a relative time.
  1637. */
  1638. if (expires && *expires == 0) {
  1639. __set_current_state(TASK_RUNNING);
  1640. return 0;
  1641. }
  1642. /*
  1643. * A NULL parameter means "infinite"
  1644. */
  1645. if (!expires) {
  1646. schedule();
  1647. return -EINTR;
  1648. }
  1649. hrtimer_init_on_stack(&t.timer, clock_id, mode);
  1650. hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
  1651. hrtimer_init_sleeper(&t, current);
  1652. hrtimer_start_expires(&t.timer, mode);
  1653. if (likely(t.task))
  1654. schedule();
  1655. hrtimer_cancel(&t.timer);
  1656. destroy_hrtimer_on_stack(&t.timer);
  1657. __set_current_state(TASK_RUNNING);
  1658. return !t.task ? 0 : -EINTR;
  1659. }
  1660. /**
  1661. * schedule_hrtimeout_range - sleep until timeout
  1662. * @expires: timeout value (ktime_t)
  1663. * @delta: slack in expires timeout (ktime_t)
  1664. * @mode: timer mode
  1665. *
  1666. * Make the current task sleep until the given expiry time has
  1667. * elapsed. The routine will return immediately unless
  1668. * the current task state has been set (see set_current_state()).
  1669. *
  1670. * The @delta argument gives the kernel the freedom to schedule the
  1671. * actual wakeup to a time that is both power and performance friendly.
  1672. * The kernel give the normal best effort behavior for "@expires+@delta",
  1673. * but may decide to fire the timer earlier, but no earlier than @expires.
  1674. *
  1675. * You can set the task state as follows -
  1676. *
  1677. * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
  1678. * pass before the routine returns unless the current task is explicitly
  1679. * woken up, (e.g. by wake_up_process()).
  1680. *
  1681. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  1682. * delivered to the current task or the current task is explicitly woken
  1683. * up.
  1684. *
  1685. * The current task state is guaranteed to be TASK_RUNNING when this
  1686. * routine returns.
  1687. *
  1688. * Returns 0 when the timer has expired. If the task was woken before the
  1689. * timer expired by a signal (only possible in state TASK_INTERRUPTIBLE) or
  1690. * by an explicit wakeup, it returns -EINTR.
  1691. */
  1692. int __sched schedule_hrtimeout_range(ktime_t *expires, u64 delta,
  1693. const enum hrtimer_mode mode)
  1694. {
  1695. return schedule_hrtimeout_range_clock(expires, delta, mode,
  1696. CLOCK_MONOTONIC);
  1697. }
  1698. EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
  1699. /**
  1700. * schedule_hrtimeout - sleep until timeout
  1701. * @expires: timeout value (ktime_t)
  1702. * @mode: timer mode
  1703. *
  1704. * Make the current task sleep until the given expiry time has
  1705. * elapsed. The routine will return immediately unless
  1706. * the current task state has been set (see set_current_state()).
  1707. *
  1708. * You can set the task state as follows -
  1709. *
  1710. * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
  1711. * pass before the routine returns unless the current task is explicitly
  1712. * woken up, (e.g. by wake_up_process()).
  1713. *
  1714. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  1715. * delivered to the current task or the current task is explicitly woken
  1716. * up.
  1717. *
  1718. * The current task state is guaranteed to be TASK_RUNNING when this
  1719. * routine returns.
  1720. *
  1721. * Returns 0 when the timer has expired. If the task was woken before the
  1722. * timer expired by a signal (only possible in state TASK_INTERRUPTIBLE) or
  1723. * by an explicit wakeup, it returns -EINTR.
  1724. */
  1725. int __sched schedule_hrtimeout(ktime_t *expires,
  1726. const enum hrtimer_mode mode)
  1727. {
  1728. return schedule_hrtimeout_range(expires, 0, mode);
  1729. }
  1730. EXPORT_SYMBOL_GPL(schedule_hrtimeout);