timer.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971
  1. /*
  2. * linux/kernel/timer.c
  3. *
  4. * Kernel internal timers
  5. *
  6. * Copyright (C) 1991, 1992 Linus Torvalds
  7. *
  8. * 1997-01-28 Modified by Finn Arne Gangstad to make timers scale better.
  9. *
  10. * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
  11. * "A Kernel Model for Precision Timekeeping" by Dave Mills
  12. * 1998-12-24 Fixed a xtime SMP race (we need the xtime_lock rw spinlock to
  13. * serialize accesses to xtime/lost_ticks).
  14. * Copyright (C) 1998 Andrea Arcangeli
  15. * 1999-03-10 Improved NTP compatibility by Ulrich Windl
  16. * 2002-05-31 Move sys_sysinfo here and make its locking sane, Robert Love
  17. * 2000-10-05 Implemented scalable SMP per-CPU timer handling.
  18. * Copyright (C) 2000, 2001, 2002 Ingo Molnar
  19. * Designed by David S. Miller, Alexey Kuznetsov and Ingo Molnar
  20. */
  21. #include <linux/kernel_stat.h>
  22. #include <linux/export.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/percpu.h>
  25. #include <linux/init.h>
  26. #include <linux/mm.h>
  27. #include <linux/swap.h>
  28. #include <linux/pid_namespace.h>
  29. #include <linux/notifier.h>
  30. #include <linux/thread_info.h>
  31. #include <linux/time.h>
  32. #include <linux/jiffies.h>
  33. #include <linux/posix-timers.h>
  34. #include <linux/cpu.h>
  35. #include <linux/syscalls.h>
  36. #include <linux/delay.h>
  37. #include <linux/tick.h>
  38. #include <linux/kallsyms.h>
  39. #include <linux/irq_work.h>
  40. #include <linux/sched.h>
  41. #include <linux/sched/sysctl.h>
  42. #include <linux/slab.h>
  43. #include <linux/compat.h>
  44. #include <asm/uaccess.h>
  45. #include <asm/unistd.h>
  46. #include <asm/div64.h>
  47. #include <asm/timex.h>
  48. #include <asm/io.h>
  49. #include "tick-internal.h"
  50. #define CREATE_TRACE_POINTS
  51. #include <trace/events/timer.h>
  52. __visible u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
  53. EXPORT_SYMBOL(jiffies_64);
  54. /*
  55. * The timer wheel has LVL_DEPTH array levels. Each level provides an array of
  56. * LVL_SIZE buckets. Each level is driven by its own clock and therefor each
  57. * level has a different granularity.
  58. *
  59. * The level granularity is: LVL_CLK_DIV ^ lvl
  60. * The level clock frequency is: HZ / (LVL_CLK_DIV ^ level)
  61. *
  62. * The array level of a newly armed timer depends on the relative expiry
  63. * time. The farther the expiry time is away the higher the array level and
  64. * therefor the granularity becomes.
  65. *
  66. * Contrary to the original timer wheel implementation, which aims for 'exact'
  67. * expiry of the timers, this implementation removes the need for recascading
  68. * the timers into the lower array levels. The previous 'classic' timer wheel
  69. * implementation of the kernel already violated the 'exact' expiry by adding
  70. * slack to the expiry time to provide batched expiration. The granularity
  71. * levels provide implicit batching.
  72. *
  73. * This is an optimization of the original timer wheel implementation for the
  74. * majority of the timer wheel use cases: timeouts. The vast majority of
  75. * timeout timers (networking, disk I/O ...) are canceled before expiry. If
  76. * the timeout expires it indicates that normal operation is disturbed, so it
  77. * does not matter much whether the timeout comes with a slight delay.
  78. *
  79. * The only exception to this are networking timers with a small expiry
  80. * time. They rely on the granularity. Those fit into the first wheel level,
  81. * which has HZ granularity.
  82. *
  83. * We don't have cascading anymore. timers with a expiry time above the
  84. * capacity of the last wheel level are force expired at the maximum timeout
  85. * value of the last wheel level. From data sampling we know that the maximum
  86. * value observed is 5 days (network connection tracking), so this should not
  87. * be an issue.
  88. *
  89. * The currently chosen array constants values are a good compromise between
  90. * array size and granularity.
  91. *
  92. * This results in the following granularity and range levels:
  93. *
  94. * HZ 1000 steps
  95. * Level Offset Granularity Range
  96. * 0 0 1 ms 0 ms - 63 ms
  97. * 1 64 8 ms 64 ms - 511 ms
  98. * 2 128 64 ms 512 ms - 4095 ms (512ms - ~4s)
  99. * 3 192 512 ms 4096 ms - 32767 ms (~4s - ~32s)
  100. * 4 256 4096 ms (~4s) 32768 ms - 262143 ms (~32s - ~4m)
  101. * 5 320 32768 ms (~32s) 262144 ms - 2097151 ms (~4m - ~34m)
  102. * 6 384 262144 ms (~4m) 2097152 ms - 16777215 ms (~34m - ~4h)
  103. * 7 448 2097152 ms (~34m) 16777216 ms - 134217727 ms (~4h - ~1d)
  104. * 8 512 16777216 ms (~4h) 134217728 ms - 1073741822 ms (~1d - ~12d)
  105. *
  106. * HZ 300
  107. * Level Offset Granularity Range
  108. * 0 0 3 ms 0 ms - 210 ms
  109. * 1 64 26 ms 213 ms - 1703 ms (213ms - ~1s)
  110. * 2 128 213 ms 1706 ms - 13650 ms (~1s - ~13s)
  111. * 3 192 1706 ms (~1s) 13653 ms - 109223 ms (~13s - ~1m)
  112. * 4 256 13653 ms (~13s) 109226 ms - 873810 ms (~1m - ~14m)
  113. * 5 320 109226 ms (~1m) 873813 ms - 6990503 ms (~14m - ~1h)
  114. * 6 384 873813 ms (~14m) 6990506 ms - 55924050 ms (~1h - ~15h)
  115. * 7 448 6990506 ms (~1h) 55924053 ms - 447392423 ms (~15h - ~5d)
  116. * 8 512 55924053 ms (~15h) 447392426 ms - 3579139406 ms (~5d - ~41d)
  117. *
  118. * HZ 250
  119. * Level Offset Granularity Range
  120. * 0 0 4 ms 0 ms - 255 ms
  121. * 1 64 32 ms 256 ms - 2047 ms (256ms - ~2s)
  122. * 2 128 256 ms 2048 ms - 16383 ms (~2s - ~16s)
  123. * 3 192 2048 ms (~2s) 16384 ms - 131071 ms (~16s - ~2m)
  124. * 4 256 16384 ms (~16s) 131072 ms - 1048575 ms (~2m - ~17m)
  125. * 5 320 131072 ms (~2m) 1048576 ms - 8388607 ms (~17m - ~2h)
  126. * 6 384 1048576 ms (~17m) 8388608 ms - 67108863 ms (~2h - ~18h)
  127. * 7 448 8388608 ms (~2h) 67108864 ms - 536870911 ms (~18h - ~6d)
  128. * 8 512 67108864 ms (~18h) 536870912 ms - 4294967288 ms (~6d - ~49d)
  129. *
  130. * HZ 100
  131. * Level Offset Granularity Range
  132. * 0 0 10 ms 0 ms - 630 ms
  133. * 1 64 80 ms 640 ms - 5110 ms (640ms - ~5s)
  134. * 2 128 640 ms 5120 ms - 40950 ms (~5s - ~40s)
  135. * 3 192 5120 ms (~5s) 40960 ms - 327670 ms (~40s - ~5m)
  136. * 4 256 40960 ms (~40s) 327680 ms - 2621430 ms (~5m - ~43m)
  137. * 5 320 327680 ms (~5m) 2621440 ms - 20971510 ms (~43m - ~5h)
  138. * 6 384 2621440 ms (~43m) 20971520 ms - 167772150 ms (~5h - ~1d)
  139. * 7 448 20971520 ms (~5h) 167772160 ms - 1342177270 ms (~1d - ~15d)
  140. */
  141. /* Clock divisor for the next level */
  142. #define LVL_CLK_SHIFT 3
  143. #define LVL_CLK_DIV (1UL << LVL_CLK_SHIFT)
  144. #define LVL_CLK_MASK (LVL_CLK_DIV - 1)
  145. #define LVL_SHIFT(n) ((n) * LVL_CLK_SHIFT)
  146. #define LVL_GRAN(n) (1UL << LVL_SHIFT(n))
  147. /*
  148. * The time start value for each level to select the bucket at enqueue
  149. * time.
  150. */
  151. #define LVL_START(n) ((LVL_SIZE - 1) << (((n) - 1) * LVL_CLK_SHIFT))
  152. /* Size of each clock level */
  153. #define LVL_BITS 6
  154. #define LVL_SIZE (1UL << LVL_BITS)
  155. #define LVL_MASK (LVL_SIZE - 1)
  156. #define LVL_OFFS(n) ((n) * LVL_SIZE)
  157. /* Level depth */
  158. #if HZ > 100
  159. # define LVL_DEPTH 9
  160. # else
  161. # define LVL_DEPTH 8
  162. #endif
  163. /* The cutoff (max. capacity of the wheel) */
  164. #define WHEEL_TIMEOUT_CUTOFF (LVL_START(LVL_DEPTH))
  165. #define WHEEL_TIMEOUT_MAX (WHEEL_TIMEOUT_CUTOFF - LVL_GRAN(LVL_DEPTH - 1))
  166. /*
  167. * The resulting wheel size. If NOHZ is configured we allocate two
  168. * wheels so we have a separate storage for the deferrable timers.
  169. */
  170. #define WHEEL_SIZE (LVL_SIZE * LVL_DEPTH)
  171. #ifdef CONFIG_NO_HZ_COMMON
  172. # define NR_BASES 2
  173. # define BASE_STD 0
  174. # define BASE_DEF 1
  175. #else
  176. # define NR_BASES 1
  177. # define BASE_STD 0
  178. # define BASE_DEF 0
  179. #endif
  180. struct timer_base {
  181. spinlock_t lock;
  182. struct timer_list *running_timer;
  183. unsigned long clk;
  184. unsigned long next_expiry;
  185. unsigned int cpu;
  186. bool migration_enabled;
  187. bool nohz_active;
  188. bool is_idle;
  189. bool must_forward_clk;
  190. DECLARE_BITMAP(pending_map, WHEEL_SIZE);
  191. struct hlist_head vectors[WHEEL_SIZE];
  192. } ____cacheline_aligned;
  193. static DEFINE_PER_CPU(struct timer_base, timer_bases[NR_BASES]);
  194. #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
  195. unsigned int sysctl_timer_migration = 1;
  196. void timers_update_migration(bool update_nohz)
  197. {
  198. bool on = sysctl_timer_migration && tick_nohz_active;
  199. unsigned int cpu;
  200. /* Avoid the loop, if nothing to update */
  201. if (this_cpu_read(timer_bases[BASE_STD].migration_enabled) == on)
  202. return;
  203. for_each_possible_cpu(cpu) {
  204. per_cpu(timer_bases[BASE_STD].migration_enabled, cpu) = on;
  205. per_cpu(timer_bases[BASE_DEF].migration_enabled, cpu) = on;
  206. per_cpu(hrtimer_bases.migration_enabled, cpu) = on;
  207. if (!update_nohz)
  208. continue;
  209. per_cpu(timer_bases[BASE_STD].nohz_active, cpu) = true;
  210. per_cpu(timer_bases[BASE_DEF].nohz_active, cpu) = true;
  211. per_cpu(hrtimer_bases.nohz_active, cpu) = true;
  212. }
  213. }
  214. int timer_migration_handler(struct ctl_table *table, int write,
  215. void __user *buffer, size_t *lenp,
  216. loff_t *ppos)
  217. {
  218. static DEFINE_MUTEX(mutex);
  219. int ret;
  220. mutex_lock(&mutex);
  221. ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  222. if (!ret && write)
  223. timers_update_migration(false);
  224. mutex_unlock(&mutex);
  225. return ret;
  226. }
  227. #endif
  228. static unsigned long round_jiffies_common(unsigned long j, int cpu,
  229. bool force_up)
  230. {
  231. int rem;
  232. unsigned long original = j;
  233. /*
  234. * We don't want all cpus firing their timers at once hitting the
  235. * same lock or cachelines, so we skew each extra cpu with an extra
  236. * 3 jiffies. This 3 jiffies came originally from the mm/ code which
  237. * already did this.
  238. * The skew is done by adding 3*cpunr, then round, then subtract this
  239. * extra offset again.
  240. */
  241. j += cpu * 3;
  242. rem = j % HZ;
  243. /*
  244. * If the target jiffie is just after a whole second (which can happen
  245. * due to delays of the timer irq, long irq off times etc etc) then
  246. * we should round down to the whole second, not up. Use 1/4th second
  247. * as cutoff for this rounding as an extreme upper bound for this.
  248. * But never round down if @force_up is set.
  249. */
  250. if (rem < HZ/4 && !force_up) /* round down */
  251. j = j - rem;
  252. else /* round up */
  253. j = j - rem + HZ;
  254. /* now that we have rounded, subtract the extra skew again */
  255. j -= cpu * 3;
  256. /*
  257. * Make sure j is still in the future. Otherwise return the
  258. * unmodified value.
  259. */
  260. return time_is_after_jiffies(j) ? j : original;
  261. }
  262. /**
  263. * __round_jiffies - function to round jiffies to a full second
  264. * @j: the time in (absolute) jiffies that should be rounded
  265. * @cpu: the processor number on which the timeout will happen
  266. *
  267. * __round_jiffies() rounds an absolute time in the future (in jiffies)
  268. * up or down to (approximately) full seconds. This is useful for timers
  269. * for which the exact time they fire does not matter too much, as long as
  270. * they fire approximately every X seconds.
  271. *
  272. * By rounding these timers to whole seconds, all such timers will fire
  273. * at the same time, rather than at various times spread out. The goal
  274. * of this is to have the CPU wake up less, which saves power.
  275. *
  276. * The exact rounding is skewed for each processor to avoid all
  277. * processors firing at the exact same time, which could lead
  278. * to lock contention or spurious cache line bouncing.
  279. *
  280. * The return value is the rounded version of the @j parameter.
  281. */
  282. unsigned long __round_jiffies(unsigned long j, int cpu)
  283. {
  284. return round_jiffies_common(j, cpu, false);
  285. }
  286. EXPORT_SYMBOL_GPL(__round_jiffies);
  287. /**
  288. * __round_jiffies_relative - function to round jiffies to a full second
  289. * @j: the time in (relative) jiffies that should be rounded
  290. * @cpu: the processor number on which the timeout will happen
  291. *
  292. * __round_jiffies_relative() rounds a time delta in the future (in jiffies)
  293. * up or down to (approximately) full seconds. This is useful for timers
  294. * for which the exact time they fire does not matter too much, as long as
  295. * they fire approximately every X seconds.
  296. *
  297. * By rounding these timers to whole seconds, all such timers will fire
  298. * at the same time, rather than at various times spread out. The goal
  299. * of this is to have the CPU wake up less, which saves power.
  300. *
  301. * The exact rounding is skewed for each processor to avoid all
  302. * processors firing at the exact same time, which could lead
  303. * to lock contention or spurious cache line bouncing.
  304. *
  305. * The return value is the rounded version of the @j parameter.
  306. */
  307. unsigned long __round_jiffies_relative(unsigned long j, int cpu)
  308. {
  309. unsigned long j0 = jiffies;
  310. /* Use j0 because jiffies might change while we run */
  311. return round_jiffies_common(j + j0, cpu, false) - j0;
  312. }
  313. EXPORT_SYMBOL_GPL(__round_jiffies_relative);
  314. /**
  315. * round_jiffies - function to round jiffies to a full second
  316. * @j: the time in (absolute) jiffies that should be rounded
  317. *
  318. * round_jiffies() rounds an absolute time in the future (in jiffies)
  319. * up or down to (approximately) full seconds. This is useful for timers
  320. * for which the exact time they fire does not matter too much, as long as
  321. * they fire approximately every X seconds.
  322. *
  323. * By rounding these timers to whole seconds, all such timers will fire
  324. * at the same time, rather than at various times spread out. The goal
  325. * of this is to have the CPU wake up less, which saves power.
  326. *
  327. * The return value is the rounded version of the @j parameter.
  328. */
  329. unsigned long round_jiffies(unsigned long j)
  330. {
  331. return round_jiffies_common(j, raw_smp_processor_id(), false);
  332. }
  333. EXPORT_SYMBOL_GPL(round_jiffies);
  334. /**
  335. * round_jiffies_relative - function to round jiffies to a full second
  336. * @j: the time in (relative) jiffies that should be rounded
  337. *
  338. * round_jiffies_relative() rounds a time delta in the future (in jiffies)
  339. * up or down to (approximately) full seconds. This is useful for timers
  340. * for which the exact time they fire does not matter too much, as long as
  341. * they fire approximately every X seconds.
  342. *
  343. * By rounding these timers to whole seconds, all such timers will fire
  344. * at the same time, rather than at various times spread out. The goal
  345. * of this is to have the CPU wake up less, which saves power.
  346. *
  347. * The return value is the rounded version of the @j parameter.
  348. */
  349. unsigned long round_jiffies_relative(unsigned long j)
  350. {
  351. return __round_jiffies_relative(j, raw_smp_processor_id());
  352. }
  353. EXPORT_SYMBOL_GPL(round_jiffies_relative);
  354. /**
  355. * __round_jiffies_up - function to round jiffies up to a full second
  356. * @j: the time in (absolute) jiffies that should be rounded
  357. * @cpu: the processor number on which the timeout will happen
  358. *
  359. * This is the same as __round_jiffies() except that it will never
  360. * round down. This is useful for timeouts for which the exact time
  361. * of firing does not matter too much, as long as they don't fire too
  362. * early.
  363. */
  364. unsigned long __round_jiffies_up(unsigned long j, int cpu)
  365. {
  366. return round_jiffies_common(j, cpu, true);
  367. }
  368. EXPORT_SYMBOL_GPL(__round_jiffies_up);
  369. /**
  370. * __round_jiffies_up_relative - function to round jiffies up to a full second
  371. * @j: the time in (relative) jiffies that should be rounded
  372. * @cpu: the processor number on which the timeout will happen
  373. *
  374. * This is the same as __round_jiffies_relative() except that it will never
  375. * round down. This is useful for timeouts for which the exact time
  376. * of firing does not matter too much, as long as they don't fire too
  377. * early.
  378. */
  379. unsigned long __round_jiffies_up_relative(unsigned long j, int cpu)
  380. {
  381. unsigned long j0 = jiffies;
  382. /* Use j0 because jiffies might change while we run */
  383. return round_jiffies_common(j + j0, cpu, true) - j0;
  384. }
  385. EXPORT_SYMBOL_GPL(__round_jiffies_up_relative);
  386. /**
  387. * round_jiffies_up - function to round jiffies up to a full second
  388. * @j: the time in (absolute) jiffies that should be rounded
  389. *
  390. * This is the same as round_jiffies() except that it will never
  391. * round down. This is useful for timeouts for which the exact time
  392. * of firing does not matter too much, as long as they don't fire too
  393. * early.
  394. */
  395. unsigned long round_jiffies_up(unsigned long j)
  396. {
  397. return round_jiffies_common(j, raw_smp_processor_id(), true);
  398. }
  399. EXPORT_SYMBOL_GPL(round_jiffies_up);
  400. /**
  401. * round_jiffies_up_relative - function to round jiffies up to a full second
  402. * @j: the time in (relative) jiffies that should be rounded
  403. *
  404. * This is the same as round_jiffies_relative() except that it will never
  405. * round down. This is useful for timeouts for which the exact time
  406. * of firing does not matter too much, as long as they don't fire too
  407. * early.
  408. */
  409. unsigned long round_jiffies_up_relative(unsigned long j)
  410. {
  411. return __round_jiffies_up_relative(j, raw_smp_processor_id());
  412. }
  413. EXPORT_SYMBOL_GPL(round_jiffies_up_relative);
  414. static inline unsigned int timer_get_idx(struct timer_list *timer)
  415. {
  416. return (timer->flags & TIMER_ARRAYMASK) >> TIMER_ARRAYSHIFT;
  417. }
  418. static inline void timer_set_idx(struct timer_list *timer, unsigned int idx)
  419. {
  420. timer->flags = (timer->flags & ~TIMER_ARRAYMASK) |
  421. idx << TIMER_ARRAYSHIFT;
  422. }
  423. /*
  424. * Helper function to calculate the array index for a given expiry
  425. * time.
  426. */
  427. static inline unsigned calc_index(unsigned expires, unsigned lvl)
  428. {
  429. expires = (expires + LVL_GRAN(lvl)) >> LVL_SHIFT(lvl);
  430. return LVL_OFFS(lvl) + (expires & LVL_MASK);
  431. }
  432. static int calc_wheel_index(unsigned long expires, unsigned long clk)
  433. {
  434. unsigned long delta = expires - clk;
  435. unsigned int idx;
  436. if (delta < LVL_START(1)) {
  437. idx = calc_index(expires, 0);
  438. } else if (delta < LVL_START(2)) {
  439. idx = calc_index(expires, 1);
  440. } else if (delta < LVL_START(3)) {
  441. idx = calc_index(expires, 2);
  442. } else if (delta < LVL_START(4)) {
  443. idx = calc_index(expires, 3);
  444. } else if (delta < LVL_START(5)) {
  445. idx = calc_index(expires, 4);
  446. } else if (delta < LVL_START(6)) {
  447. idx = calc_index(expires, 5);
  448. } else if (delta < LVL_START(7)) {
  449. idx = calc_index(expires, 6);
  450. } else if (LVL_DEPTH > 8 && delta < LVL_START(8)) {
  451. idx = calc_index(expires, 7);
  452. } else if ((long) delta < 0) {
  453. idx = clk & LVL_MASK;
  454. } else {
  455. /*
  456. * Force expire obscene large timeouts to expire at the
  457. * capacity limit of the wheel.
  458. */
  459. if (expires >= WHEEL_TIMEOUT_CUTOFF)
  460. expires = WHEEL_TIMEOUT_MAX;
  461. idx = calc_index(expires, LVL_DEPTH - 1);
  462. }
  463. return idx;
  464. }
  465. /*
  466. * Enqueue the timer into the hash bucket, mark it pending in
  467. * the bitmap and store the index in the timer flags.
  468. */
  469. static void enqueue_timer(struct timer_base *base, struct timer_list *timer,
  470. unsigned int idx)
  471. {
  472. hlist_add_head(&timer->entry, base->vectors + idx);
  473. __set_bit(idx, base->pending_map);
  474. timer_set_idx(timer, idx);
  475. }
  476. static void
  477. __internal_add_timer(struct timer_base *base, struct timer_list *timer)
  478. {
  479. unsigned int idx;
  480. idx = calc_wheel_index(timer->expires, base->clk);
  481. enqueue_timer(base, timer, idx);
  482. }
  483. static void
  484. trigger_dyntick_cpu(struct timer_base *base, struct timer_list *timer)
  485. {
  486. if (!IS_ENABLED(CONFIG_NO_HZ_COMMON) || !base->nohz_active)
  487. return;
  488. /*
  489. * TODO: This wants some optimizing similar to the code below, but we
  490. * will do that when we switch from push to pull for deferrable timers.
  491. */
  492. if (timer->flags & TIMER_DEFERRABLE) {
  493. if (tick_nohz_full_cpu(base->cpu))
  494. wake_up_nohz_cpu(base->cpu);
  495. return;
  496. }
  497. /*
  498. * We might have to IPI the remote CPU if the base is idle and the
  499. * timer is not deferrable. If the other CPU is on the way to idle
  500. * then it can't set base->is_idle as we hold the base lock:
  501. */
  502. if (!base->is_idle)
  503. return;
  504. /* Check whether this is the new first expiring timer: */
  505. if (time_after_eq(timer->expires, base->next_expiry))
  506. return;
  507. /*
  508. * Set the next expiry time and kick the CPU so it can reevaluate the
  509. * wheel:
  510. */
  511. base->next_expiry = timer->expires;
  512. wake_up_nohz_cpu(base->cpu);
  513. }
  514. static void
  515. internal_add_timer(struct timer_base *base, struct timer_list *timer)
  516. {
  517. __internal_add_timer(base, timer);
  518. trigger_dyntick_cpu(base, timer);
  519. }
  520. #ifdef CONFIG_TIMER_STATS
  521. void __timer_stats_timer_set_start_info(struct timer_list *timer, void *addr)
  522. {
  523. if (timer->start_site)
  524. return;
  525. timer->start_site = addr;
  526. memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
  527. timer->start_pid = current->pid;
  528. }
  529. static void timer_stats_account_timer(struct timer_list *timer)
  530. {
  531. void *site;
  532. /*
  533. * start_site can be concurrently reset by
  534. * timer_stats_timer_clear_start_info()
  535. */
  536. site = READ_ONCE(timer->start_site);
  537. if (likely(!site))
  538. return;
  539. timer_stats_update_stats(timer, timer->start_pid, site,
  540. timer->function, timer->start_comm,
  541. timer->flags);
  542. }
  543. #else
  544. static void timer_stats_account_timer(struct timer_list *timer) {}
  545. #endif
  546. #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
  547. static struct debug_obj_descr timer_debug_descr;
  548. static void *timer_debug_hint(void *addr)
  549. {
  550. return ((struct timer_list *) addr)->function;
  551. }
  552. static bool timer_is_static_object(void *addr)
  553. {
  554. struct timer_list *timer = addr;
  555. return (timer->entry.pprev == NULL &&
  556. timer->entry.next == TIMER_ENTRY_STATIC);
  557. }
  558. /*
  559. * fixup_init is called when:
  560. * - an active object is initialized
  561. */
  562. static bool timer_fixup_init(void *addr, enum debug_obj_state state)
  563. {
  564. struct timer_list *timer = addr;
  565. switch (state) {
  566. case ODEBUG_STATE_ACTIVE:
  567. del_timer_sync(timer);
  568. debug_object_init(timer, &timer_debug_descr);
  569. return true;
  570. default:
  571. return false;
  572. }
  573. }
  574. /* Stub timer callback for improperly used timers. */
  575. static void stub_timer(unsigned long data)
  576. {
  577. WARN_ON(1);
  578. }
  579. /*
  580. * fixup_activate is called when:
  581. * - an active object is activated
  582. * - an unknown non-static object is activated
  583. */
  584. static bool timer_fixup_activate(void *addr, enum debug_obj_state state)
  585. {
  586. struct timer_list *timer = addr;
  587. switch (state) {
  588. case ODEBUG_STATE_NOTAVAILABLE:
  589. setup_timer(timer, stub_timer, 0);
  590. return true;
  591. case ODEBUG_STATE_ACTIVE:
  592. WARN_ON(1);
  593. default:
  594. return false;
  595. }
  596. }
  597. /*
  598. * fixup_free is called when:
  599. * - an active object is freed
  600. */
  601. static bool timer_fixup_free(void *addr, enum debug_obj_state state)
  602. {
  603. struct timer_list *timer = addr;
  604. switch (state) {
  605. case ODEBUG_STATE_ACTIVE:
  606. del_timer_sync(timer);
  607. debug_object_free(timer, &timer_debug_descr);
  608. return true;
  609. default:
  610. return false;
  611. }
  612. }
  613. /*
  614. * fixup_assert_init is called when:
  615. * - an untracked/uninit-ed object is found
  616. */
  617. static bool timer_fixup_assert_init(void *addr, enum debug_obj_state state)
  618. {
  619. struct timer_list *timer = addr;
  620. switch (state) {
  621. case ODEBUG_STATE_NOTAVAILABLE:
  622. setup_timer(timer, stub_timer, 0);
  623. return true;
  624. default:
  625. return false;
  626. }
  627. }
  628. static struct debug_obj_descr timer_debug_descr = {
  629. .name = "timer_list",
  630. .debug_hint = timer_debug_hint,
  631. .is_static_object = timer_is_static_object,
  632. .fixup_init = timer_fixup_init,
  633. .fixup_activate = timer_fixup_activate,
  634. .fixup_free = timer_fixup_free,
  635. .fixup_assert_init = timer_fixup_assert_init,
  636. };
  637. static inline void debug_timer_init(struct timer_list *timer)
  638. {
  639. debug_object_init(timer, &timer_debug_descr);
  640. }
  641. static inline void debug_timer_activate(struct timer_list *timer)
  642. {
  643. debug_object_activate(timer, &timer_debug_descr);
  644. }
  645. static inline void debug_timer_deactivate(struct timer_list *timer)
  646. {
  647. debug_object_deactivate(timer, &timer_debug_descr);
  648. }
  649. static inline void debug_timer_free(struct timer_list *timer)
  650. {
  651. debug_object_free(timer, &timer_debug_descr);
  652. }
  653. static inline void debug_timer_assert_init(struct timer_list *timer)
  654. {
  655. debug_object_assert_init(timer, &timer_debug_descr);
  656. }
  657. static void do_init_timer(struct timer_list *timer, unsigned int flags,
  658. const char *name, struct lock_class_key *key);
  659. void init_timer_on_stack_key(struct timer_list *timer, unsigned int flags,
  660. const char *name, struct lock_class_key *key)
  661. {
  662. debug_object_init_on_stack(timer, &timer_debug_descr);
  663. do_init_timer(timer, flags, name, key);
  664. }
  665. EXPORT_SYMBOL_GPL(init_timer_on_stack_key);
  666. void destroy_timer_on_stack(struct timer_list *timer)
  667. {
  668. debug_object_free(timer, &timer_debug_descr);
  669. }
  670. EXPORT_SYMBOL_GPL(destroy_timer_on_stack);
  671. #else
  672. static inline void debug_timer_init(struct timer_list *timer) { }
  673. static inline void debug_timer_activate(struct timer_list *timer) { }
  674. static inline void debug_timer_deactivate(struct timer_list *timer) { }
  675. static inline void debug_timer_assert_init(struct timer_list *timer) { }
  676. #endif
  677. static inline void debug_init(struct timer_list *timer)
  678. {
  679. debug_timer_init(timer);
  680. trace_timer_init(timer);
  681. }
  682. static inline void
  683. debug_activate(struct timer_list *timer, unsigned long expires)
  684. {
  685. debug_timer_activate(timer);
  686. trace_timer_start(timer, expires, timer->flags);
  687. }
  688. static inline void debug_deactivate(struct timer_list *timer)
  689. {
  690. debug_timer_deactivate(timer);
  691. trace_timer_cancel(timer);
  692. }
  693. static inline void debug_assert_init(struct timer_list *timer)
  694. {
  695. debug_timer_assert_init(timer);
  696. }
  697. static void do_init_timer(struct timer_list *timer, unsigned int flags,
  698. const char *name, struct lock_class_key *key)
  699. {
  700. timer->entry.pprev = NULL;
  701. timer->flags = flags | raw_smp_processor_id();
  702. #ifdef CONFIG_TIMER_STATS
  703. timer->start_site = NULL;
  704. timer->start_pid = -1;
  705. memset(timer->start_comm, 0, TASK_COMM_LEN);
  706. #endif
  707. lockdep_init_map(&timer->lockdep_map, name, key, 0);
  708. }
  709. /**
  710. * init_timer_key - initialize a timer
  711. * @timer: the timer to be initialized
  712. * @flags: timer flags
  713. * @name: name of the timer
  714. * @key: lockdep class key of the fake lock used for tracking timer
  715. * sync lock dependencies
  716. *
  717. * init_timer_key() must be done to a timer prior calling *any* of the
  718. * other timer functions.
  719. */
  720. void init_timer_key(struct timer_list *timer, unsigned int flags,
  721. const char *name, struct lock_class_key *key)
  722. {
  723. debug_init(timer);
  724. do_init_timer(timer, flags, name, key);
  725. }
  726. EXPORT_SYMBOL(init_timer_key);
  727. static inline void detach_timer(struct timer_list *timer, bool clear_pending)
  728. {
  729. struct hlist_node *entry = &timer->entry;
  730. debug_deactivate(timer);
  731. __hlist_del(entry);
  732. if (clear_pending)
  733. entry->pprev = NULL;
  734. entry->next = LIST_POISON2;
  735. }
  736. static int detach_if_pending(struct timer_list *timer, struct timer_base *base,
  737. bool clear_pending)
  738. {
  739. unsigned idx = timer_get_idx(timer);
  740. if (!timer_pending(timer))
  741. return 0;
  742. if (hlist_is_singular_node(&timer->entry, base->vectors + idx))
  743. __clear_bit(idx, base->pending_map);
  744. detach_timer(timer, clear_pending);
  745. return 1;
  746. }
  747. static inline struct timer_base *get_timer_cpu_base(u32 tflags, u32 cpu)
  748. {
  749. struct timer_base *base = per_cpu_ptr(&timer_bases[BASE_STD], cpu);
  750. /*
  751. * If the timer is deferrable and nohz is active then we need to use
  752. * the deferrable base.
  753. */
  754. if (IS_ENABLED(CONFIG_NO_HZ_COMMON) && base->nohz_active &&
  755. (tflags & TIMER_DEFERRABLE))
  756. base = per_cpu_ptr(&timer_bases[BASE_DEF], cpu);
  757. return base;
  758. }
  759. static inline struct timer_base *get_timer_this_cpu_base(u32 tflags)
  760. {
  761. struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
  762. /*
  763. * If the timer is deferrable and nohz is active then we need to use
  764. * the deferrable base.
  765. */
  766. if (IS_ENABLED(CONFIG_NO_HZ_COMMON) && base->nohz_active &&
  767. (tflags & TIMER_DEFERRABLE))
  768. base = this_cpu_ptr(&timer_bases[BASE_DEF]);
  769. return base;
  770. }
  771. static inline struct timer_base *get_timer_base(u32 tflags)
  772. {
  773. return get_timer_cpu_base(tflags, tflags & TIMER_CPUMASK);
  774. }
  775. #ifdef CONFIG_NO_HZ_COMMON
  776. static inline struct timer_base *
  777. get_target_base(struct timer_base *base, unsigned tflags)
  778. {
  779. #ifdef CONFIG_SMP
  780. if ((tflags & TIMER_PINNED) || !base->migration_enabled)
  781. return get_timer_this_cpu_base(tflags);
  782. return get_timer_cpu_base(tflags, get_nohz_timer_target());
  783. #else
  784. return get_timer_this_cpu_base(tflags);
  785. #endif
  786. }
  787. static inline void forward_timer_base(struct timer_base *base)
  788. {
  789. unsigned long jnow;
  790. /*
  791. * We only forward the base when we are idle or have just come out of
  792. * idle (must_forward_clk logic), and have a delta between base clock
  793. * and jiffies. In the common case, run_timers will take care of it.
  794. */
  795. if (likely(!base->must_forward_clk))
  796. return;
  797. jnow = READ_ONCE(jiffies);
  798. base->must_forward_clk = base->is_idle;
  799. if ((long)(jnow - base->clk) < 2)
  800. return;
  801. /*
  802. * If the next expiry value is > jiffies, then we fast forward to
  803. * jiffies otherwise we forward to the next expiry value.
  804. */
  805. if (time_after(base->next_expiry, jnow))
  806. base->clk = jnow;
  807. else
  808. base->clk = base->next_expiry;
  809. }
  810. #else
  811. static inline struct timer_base *
  812. get_target_base(struct timer_base *base, unsigned tflags)
  813. {
  814. return get_timer_this_cpu_base(tflags);
  815. }
  816. static inline void forward_timer_base(struct timer_base *base) { }
  817. #endif
  818. /*
  819. * We are using hashed locking: Holding per_cpu(timer_bases[x]).lock means
  820. * that all timers which are tied to this base are locked, and the base itself
  821. * is locked too.
  822. *
  823. * So __run_timers/migrate_timers can safely modify all timers which could
  824. * be found in the base->vectors array.
  825. *
  826. * When a timer is migrating then the TIMER_MIGRATING flag is set and we need
  827. * to wait until the migration is done.
  828. */
  829. static struct timer_base *lock_timer_base(struct timer_list *timer,
  830. unsigned long *flags)
  831. __acquires(timer->base->lock)
  832. {
  833. for (;;) {
  834. struct timer_base *base;
  835. u32 tf;
  836. /*
  837. * We need to use READ_ONCE() here, otherwise the compiler
  838. * might re-read @tf between the check for TIMER_MIGRATING
  839. * and spin_lock().
  840. */
  841. tf = READ_ONCE(timer->flags);
  842. if (!(tf & TIMER_MIGRATING)) {
  843. base = get_timer_base(tf);
  844. spin_lock_irqsave(&base->lock, *flags);
  845. if (timer->flags == tf)
  846. return base;
  847. spin_unlock_irqrestore(&base->lock, *flags);
  848. }
  849. cpu_relax();
  850. }
  851. }
  852. static inline int
  853. __mod_timer(struct timer_list *timer, unsigned long expires, bool pending_only)
  854. {
  855. struct timer_base *base, *new_base;
  856. unsigned int idx = UINT_MAX;
  857. unsigned long clk = 0, flags;
  858. int ret = 0;
  859. BUG_ON(!timer->function);
  860. /*
  861. * This is a common optimization triggered by the networking code - if
  862. * the timer is re-modified to have the same timeout or ends up in the
  863. * same array bucket then just return:
  864. */
  865. if (timer_pending(timer)) {
  866. /*
  867. * The downside of this optimization is that it can result in
  868. * larger granularity than you would get from adding a new
  869. * timer with this expiry.
  870. */
  871. if (timer->expires == expires)
  872. return 1;
  873. /*
  874. * We lock timer base and calculate the bucket index right
  875. * here. If the timer ends up in the same bucket, then we
  876. * just update the expiry time and avoid the whole
  877. * dequeue/enqueue dance.
  878. */
  879. base = lock_timer_base(timer, &flags);
  880. forward_timer_base(base);
  881. clk = base->clk;
  882. idx = calc_wheel_index(expires, clk);
  883. /*
  884. * Retrieve and compare the array index of the pending
  885. * timer. If it matches set the expiry to the new value so a
  886. * subsequent call will exit in the expires check above.
  887. */
  888. if (idx == timer_get_idx(timer)) {
  889. timer->expires = expires;
  890. ret = 1;
  891. goto out_unlock;
  892. }
  893. } else {
  894. base = lock_timer_base(timer, &flags);
  895. forward_timer_base(base);
  896. }
  897. timer_stats_timer_set_start_info(timer);
  898. ret = detach_if_pending(timer, base, false);
  899. if (!ret && pending_only)
  900. goto out_unlock;
  901. debug_activate(timer, expires);
  902. new_base = get_target_base(base, timer->flags);
  903. if (base != new_base) {
  904. /*
  905. * We are trying to schedule the timer on the new base.
  906. * However we can't change timer's base while it is running,
  907. * otherwise del_timer_sync() can't detect that the timer's
  908. * handler yet has not finished. This also guarantees that the
  909. * timer is serialized wrt itself.
  910. */
  911. if (likely(base->running_timer != timer)) {
  912. /* See the comment in lock_timer_base() */
  913. timer->flags |= TIMER_MIGRATING;
  914. spin_unlock(&base->lock);
  915. base = new_base;
  916. spin_lock(&base->lock);
  917. WRITE_ONCE(timer->flags,
  918. (timer->flags & ~TIMER_BASEMASK) | base->cpu);
  919. forward_timer_base(base);
  920. }
  921. }
  922. timer->expires = expires;
  923. /*
  924. * If 'idx' was calculated above and the base time did not advance
  925. * between calculating 'idx' and possibly switching the base, only
  926. * enqueue_timer() and trigger_dyntick_cpu() is required. Otherwise
  927. * we need to (re)calculate the wheel index via
  928. * internal_add_timer().
  929. */
  930. if (idx != UINT_MAX && clk == base->clk) {
  931. enqueue_timer(base, timer, idx);
  932. trigger_dyntick_cpu(base, timer);
  933. } else {
  934. internal_add_timer(base, timer);
  935. }
  936. out_unlock:
  937. spin_unlock_irqrestore(&base->lock, flags);
  938. return ret;
  939. }
  940. /**
  941. * mod_timer_pending - modify a pending timer's timeout
  942. * @timer: the pending timer to be modified
  943. * @expires: new timeout in jiffies
  944. *
  945. * mod_timer_pending() is the same for pending timers as mod_timer(),
  946. * but will not re-activate and modify already deleted timers.
  947. *
  948. * It is useful for unserialized use of timers.
  949. */
  950. int mod_timer_pending(struct timer_list *timer, unsigned long expires)
  951. {
  952. return __mod_timer(timer, expires, true);
  953. }
  954. EXPORT_SYMBOL(mod_timer_pending);
  955. /**
  956. * mod_timer - modify a timer's timeout
  957. * @timer: the timer to be modified
  958. * @expires: new timeout in jiffies
  959. *
  960. * mod_timer() is a more efficient way to update the expire field of an
  961. * active timer (if the timer is inactive it will be activated)
  962. *
  963. * mod_timer(timer, expires) is equivalent to:
  964. *
  965. * del_timer(timer); timer->expires = expires; add_timer(timer);
  966. *
  967. * Note that if there are multiple unserialized concurrent users of the
  968. * same timer, then mod_timer() is the only safe way to modify the timeout,
  969. * since add_timer() cannot modify an already running timer.
  970. *
  971. * The function returns whether it has modified a pending timer or not.
  972. * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
  973. * active timer returns 1.)
  974. */
  975. int mod_timer(struct timer_list *timer, unsigned long expires)
  976. {
  977. return __mod_timer(timer, expires, false);
  978. }
  979. EXPORT_SYMBOL(mod_timer);
  980. /**
  981. * add_timer - start a timer
  982. * @timer: the timer to be added
  983. *
  984. * The kernel will do a ->function(->data) callback from the
  985. * timer interrupt at the ->expires point in the future. The
  986. * current time is 'jiffies'.
  987. *
  988. * The timer's ->expires, ->function (and if the handler uses it, ->data)
  989. * fields must be set prior calling this function.
  990. *
  991. * Timers with an ->expires field in the past will be executed in the next
  992. * timer tick.
  993. */
  994. void add_timer(struct timer_list *timer)
  995. {
  996. BUG_ON(timer_pending(timer));
  997. mod_timer(timer, timer->expires);
  998. }
  999. EXPORT_SYMBOL(add_timer);
  1000. /**
  1001. * add_timer_on - start a timer on a particular CPU
  1002. * @timer: the timer to be added
  1003. * @cpu: the CPU to start it on
  1004. *
  1005. * This is not very scalable on SMP. Double adds are not possible.
  1006. */
  1007. void add_timer_on(struct timer_list *timer, int cpu)
  1008. {
  1009. struct timer_base *new_base, *base;
  1010. unsigned long flags;
  1011. timer_stats_timer_set_start_info(timer);
  1012. BUG_ON(timer_pending(timer) || !timer->function);
  1013. new_base = get_timer_cpu_base(timer->flags, cpu);
  1014. /*
  1015. * If @timer was on a different CPU, it should be migrated with the
  1016. * old base locked to prevent other operations proceeding with the
  1017. * wrong base locked. See lock_timer_base().
  1018. */
  1019. base = lock_timer_base(timer, &flags);
  1020. if (base != new_base) {
  1021. timer->flags |= TIMER_MIGRATING;
  1022. spin_unlock(&base->lock);
  1023. base = new_base;
  1024. spin_lock(&base->lock);
  1025. WRITE_ONCE(timer->flags,
  1026. (timer->flags & ~TIMER_BASEMASK) | cpu);
  1027. }
  1028. forward_timer_base(base);
  1029. debug_activate(timer, timer->expires);
  1030. internal_add_timer(base, timer);
  1031. spin_unlock_irqrestore(&base->lock, flags);
  1032. }
  1033. EXPORT_SYMBOL_GPL(add_timer_on);
  1034. /**
  1035. * del_timer - deactive a timer.
  1036. * @timer: the timer to be deactivated
  1037. *
  1038. * del_timer() deactivates a timer - this works on both active and inactive
  1039. * timers.
  1040. *
  1041. * The function returns whether it has deactivated a pending timer or not.
  1042. * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
  1043. * active timer returns 1.)
  1044. */
  1045. int del_timer(struct timer_list *timer)
  1046. {
  1047. struct timer_base *base;
  1048. unsigned long flags;
  1049. int ret = 0;
  1050. debug_assert_init(timer);
  1051. timer_stats_timer_clear_start_info(timer);
  1052. if (timer_pending(timer)) {
  1053. base = lock_timer_base(timer, &flags);
  1054. ret = detach_if_pending(timer, base, true);
  1055. spin_unlock_irqrestore(&base->lock, flags);
  1056. }
  1057. return ret;
  1058. }
  1059. EXPORT_SYMBOL(del_timer);
  1060. /**
  1061. * try_to_del_timer_sync - Try to deactivate a timer
  1062. * @timer: timer do del
  1063. *
  1064. * This function tries to deactivate a timer. Upon successful (ret >= 0)
  1065. * exit the timer is not queued and the handler is not running on any CPU.
  1066. */
  1067. int try_to_del_timer_sync(struct timer_list *timer)
  1068. {
  1069. struct timer_base *base;
  1070. unsigned long flags;
  1071. int ret = -1;
  1072. debug_assert_init(timer);
  1073. base = lock_timer_base(timer, &flags);
  1074. if (base->running_timer != timer) {
  1075. timer_stats_timer_clear_start_info(timer);
  1076. ret = detach_if_pending(timer, base, true);
  1077. }
  1078. spin_unlock_irqrestore(&base->lock, flags);
  1079. return ret;
  1080. }
  1081. EXPORT_SYMBOL(try_to_del_timer_sync);
  1082. #ifdef CONFIG_SMP
  1083. /**
  1084. * del_timer_sync - deactivate a timer and wait for the handler to finish.
  1085. * @timer: the timer to be deactivated
  1086. *
  1087. * This function only differs from del_timer() on SMP: besides deactivating
  1088. * the timer it also makes sure the handler has finished executing on other
  1089. * CPUs.
  1090. *
  1091. * Synchronization rules: Callers must prevent restarting of the timer,
  1092. * otherwise this function is meaningless. It must not be called from
  1093. * interrupt contexts unless the timer is an irqsafe one. The caller must
  1094. * not hold locks which would prevent completion of the timer's
  1095. * handler. The timer's handler must not call add_timer_on(). Upon exit the
  1096. * timer is not queued and the handler is not running on any CPU.
  1097. *
  1098. * Note: For !irqsafe timers, you must not hold locks that are held in
  1099. * interrupt context while calling this function. Even if the lock has
  1100. * nothing to do with the timer in question. Here's why:
  1101. *
  1102. * CPU0 CPU1
  1103. * ---- ----
  1104. * <SOFTIRQ>
  1105. * call_timer_fn();
  1106. * base->running_timer = mytimer;
  1107. * spin_lock_irq(somelock);
  1108. * <IRQ>
  1109. * spin_lock(somelock);
  1110. * del_timer_sync(mytimer);
  1111. * while (base->running_timer == mytimer);
  1112. *
  1113. * Now del_timer_sync() will never return and never release somelock.
  1114. * The interrupt on the other CPU is waiting to grab somelock but
  1115. * it has interrupted the softirq that CPU0 is waiting to finish.
  1116. *
  1117. * The function returns whether it has deactivated a pending timer or not.
  1118. */
  1119. int del_timer_sync(struct timer_list *timer)
  1120. {
  1121. #ifdef CONFIG_LOCKDEP
  1122. unsigned long flags;
  1123. /*
  1124. * If lockdep gives a backtrace here, please reference
  1125. * the synchronization rules above.
  1126. */
  1127. local_irq_save(flags);
  1128. lock_map_acquire(&timer->lockdep_map);
  1129. lock_map_release(&timer->lockdep_map);
  1130. local_irq_restore(flags);
  1131. #endif
  1132. /*
  1133. * don't use it in hardirq context, because it
  1134. * could lead to deadlock.
  1135. */
  1136. WARN_ON(in_irq() && !(timer->flags & TIMER_IRQSAFE));
  1137. for (;;) {
  1138. int ret = try_to_del_timer_sync(timer);
  1139. if (ret >= 0)
  1140. return ret;
  1141. cpu_relax();
  1142. }
  1143. }
  1144. EXPORT_SYMBOL(del_timer_sync);
  1145. #endif
  1146. static void call_timer_fn(struct timer_list *timer, void (*fn)(unsigned long),
  1147. unsigned long data)
  1148. {
  1149. int count = preempt_count();
  1150. #ifdef CONFIG_LOCKDEP
  1151. /*
  1152. * It is permissible to free the timer from inside the
  1153. * function that is called from it, this we need to take into
  1154. * account for lockdep too. To avoid bogus "held lock freed"
  1155. * warnings as well as problems when looking into
  1156. * timer->lockdep_map, make a copy and use that here.
  1157. */
  1158. struct lockdep_map lockdep_map;
  1159. lockdep_copy_map(&lockdep_map, &timer->lockdep_map);
  1160. #endif
  1161. /*
  1162. * Couple the lock chain with the lock chain at
  1163. * del_timer_sync() by acquiring the lock_map around the fn()
  1164. * call here and in del_timer_sync().
  1165. */
  1166. lock_map_acquire(&lockdep_map);
  1167. trace_timer_expire_entry(timer);
  1168. fn(data);
  1169. trace_timer_expire_exit(timer);
  1170. lock_map_release(&lockdep_map);
  1171. if (count != preempt_count()) {
  1172. WARN_ONCE(1, "timer: %pF preempt leak: %08x -> %08x\n",
  1173. fn, count, preempt_count());
  1174. /*
  1175. * Restore the preempt count. That gives us a decent
  1176. * chance to survive and extract information. If the
  1177. * callback kept a lock held, bad luck, but not worse
  1178. * than the BUG() we had.
  1179. */
  1180. preempt_count_set(count);
  1181. }
  1182. }
  1183. static void expire_timers(struct timer_base *base, struct hlist_head *head)
  1184. {
  1185. while (!hlist_empty(head)) {
  1186. struct timer_list *timer;
  1187. void (*fn)(unsigned long);
  1188. unsigned long data;
  1189. timer = hlist_entry(head->first, struct timer_list, entry);
  1190. timer_stats_account_timer(timer);
  1191. base->running_timer = timer;
  1192. detach_timer(timer, true);
  1193. fn = timer->function;
  1194. data = timer->data;
  1195. if (timer->flags & TIMER_IRQSAFE) {
  1196. spin_unlock(&base->lock);
  1197. call_timer_fn(timer, fn, data);
  1198. spin_lock(&base->lock);
  1199. } else {
  1200. spin_unlock_irq(&base->lock);
  1201. call_timer_fn(timer, fn, data);
  1202. spin_lock_irq(&base->lock);
  1203. }
  1204. }
  1205. }
  1206. static int __collect_expired_timers(struct timer_base *base,
  1207. struct hlist_head *heads)
  1208. {
  1209. unsigned long clk = base->clk;
  1210. struct hlist_head *vec;
  1211. int i, levels = 0;
  1212. unsigned int idx;
  1213. for (i = 0; i < LVL_DEPTH; i++) {
  1214. idx = (clk & LVL_MASK) + i * LVL_SIZE;
  1215. if (__test_and_clear_bit(idx, base->pending_map)) {
  1216. vec = base->vectors + idx;
  1217. hlist_move_list(vec, heads++);
  1218. levels++;
  1219. }
  1220. /* Is it time to look at the next level? */
  1221. if (clk & LVL_CLK_MASK)
  1222. break;
  1223. /* Shift clock for the next level granularity */
  1224. clk >>= LVL_CLK_SHIFT;
  1225. }
  1226. return levels;
  1227. }
  1228. #ifdef CONFIG_NO_HZ_COMMON
  1229. /*
  1230. * Find the next pending bucket of a level. Search from level start (@offset)
  1231. * + @clk upwards and if nothing there, search from start of the level
  1232. * (@offset) up to @offset + clk.
  1233. */
  1234. static int next_pending_bucket(struct timer_base *base, unsigned offset,
  1235. unsigned clk)
  1236. {
  1237. unsigned pos, start = offset + clk;
  1238. unsigned end = offset + LVL_SIZE;
  1239. pos = find_next_bit(base->pending_map, end, start);
  1240. if (pos < end)
  1241. return pos - start;
  1242. pos = find_next_bit(base->pending_map, start, offset);
  1243. return pos < start ? pos + LVL_SIZE - start : -1;
  1244. }
  1245. /*
  1246. * Search the first expiring timer in the various clock levels. Caller must
  1247. * hold base->lock.
  1248. */
  1249. static unsigned long __next_timer_interrupt(struct timer_base *base)
  1250. {
  1251. unsigned long clk, next, adj;
  1252. unsigned lvl, offset = 0;
  1253. next = base->clk + NEXT_TIMER_MAX_DELTA;
  1254. clk = base->clk;
  1255. for (lvl = 0; lvl < LVL_DEPTH; lvl++, offset += LVL_SIZE) {
  1256. int pos = next_pending_bucket(base, offset, clk & LVL_MASK);
  1257. if (pos >= 0) {
  1258. unsigned long tmp = clk + (unsigned long) pos;
  1259. tmp <<= LVL_SHIFT(lvl);
  1260. if (time_before(tmp, next))
  1261. next = tmp;
  1262. }
  1263. /*
  1264. * Clock for the next level. If the current level clock lower
  1265. * bits are zero, we look at the next level as is. If not we
  1266. * need to advance it by one because that's going to be the
  1267. * next expiring bucket in that level. base->clk is the next
  1268. * expiring jiffie. So in case of:
  1269. *
  1270. * LVL5 LVL4 LVL3 LVL2 LVL1 LVL0
  1271. * 0 0 0 0 0 0
  1272. *
  1273. * we have to look at all levels @index 0. With
  1274. *
  1275. * LVL5 LVL4 LVL3 LVL2 LVL1 LVL0
  1276. * 0 0 0 0 0 2
  1277. *
  1278. * LVL0 has the next expiring bucket @index 2. The upper
  1279. * levels have the next expiring bucket @index 1.
  1280. *
  1281. * In case that the propagation wraps the next level the same
  1282. * rules apply:
  1283. *
  1284. * LVL5 LVL4 LVL3 LVL2 LVL1 LVL0
  1285. * 0 0 0 0 F 2
  1286. *
  1287. * So after looking at LVL0 we get:
  1288. *
  1289. * LVL5 LVL4 LVL3 LVL2 LVL1
  1290. * 0 0 0 1 0
  1291. *
  1292. * So no propagation from LVL1 to LVL2 because that happened
  1293. * with the add already, but then we need to propagate further
  1294. * from LVL2 to LVL3.
  1295. *
  1296. * So the simple check whether the lower bits of the current
  1297. * level are 0 or not is sufficient for all cases.
  1298. */
  1299. adj = clk & LVL_CLK_MASK ? 1 : 0;
  1300. clk >>= LVL_CLK_SHIFT;
  1301. clk += adj;
  1302. }
  1303. return next;
  1304. }
  1305. /*
  1306. * Check, if the next hrtimer event is before the next timer wheel
  1307. * event:
  1308. */
  1309. static u64 cmp_next_hrtimer_event(u64 basem, u64 expires)
  1310. {
  1311. u64 nextevt = hrtimer_get_next_event();
  1312. /*
  1313. * If high resolution timers are enabled
  1314. * hrtimer_get_next_event() returns KTIME_MAX.
  1315. */
  1316. if (expires <= nextevt)
  1317. return expires;
  1318. /*
  1319. * If the next timer is already expired, return the tick base
  1320. * time so the tick is fired immediately.
  1321. */
  1322. if (nextevt <= basem)
  1323. return basem;
  1324. /*
  1325. * Round up to the next jiffie. High resolution timers are
  1326. * off, so the hrtimers are expired in the tick and we need to
  1327. * make sure that this tick really expires the timer to avoid
  1328. * a ping pong of the nohz stop code.
  1329. *
  1330. * Use DIV_ROUND_UP_ULL to prevent gcc calling __divdi3
  1331. */
  1332. return DIV_ROUND_UP_ULL(nextevt, TICK_NSEC) * TICK_NSEC;
  1333. }
  1334. /**
  1335. * get_next_timer_interrupt - return the time (clock mono) of the next timer
  1336. * @basej: base time jiffies
  1337. * @basem: base time clock monotonic
  1338. *
  1339. * Returns the tick aligned clock monotonic time of the next pending
  1340. * timer or KTIME_MAX if no timer is pending.
  1341. */
  1342. u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
  1343. {
  1344. struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
  1345. u64 expires = KTIME_MAX;
  1346. unsigned long nextevt;
  1347. bool is_max_delta;
  1348. /*
  1349. * Pretend that there is no timer pending if the cpu is offline.
  1350. * Possible pending timers will be migrated later to an active cpu.
  1351. */
  1352. if (cpu_is_offline(smp_processor_id()))
  1353. return expires;
  1354. spin_lock(&base->lock);
  1355. nextevt = __next_timer_interrupt(base);
  1356. is_max_delta = (nextevt == base->clk + NEXT_TIMER_MAX_DELTA);
  1357. base->next_expiry = nextevt;
  1358. /*
  1359. * We have a fresh next event. Check whether we can forward the
  1360. * base. We can only do that when @basej is past base->clk
  1361. * otherwise we might rewind base->clk.
  1362. */
  1363. if (time_after(basej, base->clk)) {
  1364. if (time_after(nextevt, basej))
  1365. base->clk = basej;
  1366. else if (time_after(nextevt, base->clk))
  1367. base->clk = nextevt;
  1368. }
  1369. if (time_before_eq(nextevt, basej)) {
  1370. expires = basem;
  1371. base->is_idle = false;
  1372. } else {
  1373. if (!is_max_delta)
  1374. expires = basem + (u64)(nextevt - basej) * TICK_NSEC;
  1375. /*
  1376. * If we expect to sleep more than a tick, mark the base idle.
  1377. * Also the tick is stopped so any added timer must forward
  1378. * the base clk itself to keep granularity small. This idle
  1379. * logic is only maintained for the BASE_STD base, deferrable
  1380. * timers may still see large granularity skew (by design).
  1381. */
  1382. if ((expires - basem) > TICK_NSEC) {
  1383. base->must_forward_clk = true;
  1384. base->is_idle = true;
  1385. }
  1386. }
  1387. spin_unlock(&base->lock);
  1388. return cmp_next_hrtimer_event(basem, expires);
  1389. }
  1390. /**
  1391. * timer_clear_idle - Clear the idle state of the timer base
  1392. *
  1393. * Called with interrupts disabled
  1394. */
  1395. void timer_clear_idle(void)
  1396. {
  1397. struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
  1398. /*
  1399. * We do this unlocked. The worst outcome is a remote enqueue sending
  1400. * a pointless IPI, but taking the lock would just make the window for
  1401. * sending the IPI a few instructions smaller for the cost of taking
  1402. * the lock in the exit from idle path.
  1403. */
  1404. base->is_idle = false;
  1405. }
  1406. static int collect_expired_timers(struct timer_base *base,
  1407. struct hlist_head *heads)
  1408. {
  1409. /*
  1410. * NOHZ optimization. After a long idle sleep we need to forward the
  1411. * base to current jiffies. Avoid a loop by searching the bitfield for
  1412. * the next expiring timer.
  1413. */
  1414. if ((long)(jiffies - base->clk) > 2) {
  1415. unsigned long next = __next_timer_interrupt(base);
  1416. /*
  1417. * If the next timer is ahead of time forward to current
  1418. * jiffies, otherwise forward to the next expiry time:
  1419. */
  1420. if (time_after(next, jiffies)) {
  1421. /* The call site will increment clock! */
  1422. base->clk = jiffies - 1;
  1423. return 0;
  1424. }
  1425. base->clk = next;
  1426. }
  1427. return __collect_expired_timers(base, heads);
  1428. }
  1429. #else
  1430. static inline int collect_expired_timers(struct timer_base *base,
  1431. struct hlist_head *heads)
  1432. {
  1433. return __collect_expired_timers(base, heads);
  1434. }
  1435. #endif
  1436. /*
  1437. * Called from the timer interrupt handler to charge one tick to the current
  1438. * process. user_tick is 1 if the tick is user time, 0 for system.
  1439. */
  1440. void update_process_times(int user_tick)
  1441. {
  1442. struct task_struct *p = current;
  1443. /* Note: this timer irq context must be accounted for as well. */
  1444. account_process_tick(p, user_tick);
  1445. run_local_timers();
  1446. rcu_check_callbacks(user_tick);
  1447. #ifdef CONFIG_IRQ_WORK
  1448. if (in_irq())
  1449. irq_work_tick();
  1450. #endif
  1451. scheduler_tick();
  1452. run_posix_cpu_timers(p);
  1453. }
  1454. /**
  1455. * __run_timers - run all expired timers (if any) on this CPU.
  1456. * @base: the timer vector to be processed.
  1457. */
  1458. static inline void __run_timers(struct timer_base *base)
  1459. {
  1460. struct hlist_head heads[LVL_DEPTH];
  1461. int levels;
  1462. if (!time_after_eq(jiffies, base->clk))
  1463. return;
  1464. spin_lock_irq(&base->lock);
  1465. while (time_after_eq(jiffies, base->clk)) {
  1466. levels = collect_expired_timers(base, heads);
  1467. base->clk++;
  1468. while (levels--)
  1469. expire_timers(base, heads + levels);
  1470. }
  1471. base->running_timer = NULL;
  1472. spin_unlock_irq(&base->lock);
  1473. }
  1474. /*
  1475. * This function runs timers and the timer-tq in bottom half context.
  1476. */
  1477. static __latent_entropy void run_timer_softirq(struct softirq_action *h)
  1478. {
  1479. struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
  1480. /*
  1481. * must_forward_clk must be cleared before running timers so that any
  1482. * timer functions that call mod_timer will not try to forward the
  1483. * base. idle trcking / clock forwarding logic is only used with
  1484. * BASE_STD timers.
  1485. *
  1486. * The deferrable base does not do idle tracking at all, so we do
  1487. * not forward it. This can result in very large variations in
  1488. * granularity for deferrable timers, but they can be deferred for
  1489. * long periods due to idle.
  1490. */
  1491. base->must_forward_clk = false;
  1492. __run_timers(base);
  1493. if (IS_ENABLED(CONFIG_NO_HZ_COMMON) && base->nohz_active)
  1494. __run_timers(this_cpu_ptr(&timer_bases[BASE_DEF]));
  1495. }
  1496. /*
  1497. * Called by the local, per-CPU timer interrupt on SMP.
  1498. */
  1499. void run_local_timers(void)
  1500. {
  1501. struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
  1502. hrtimer_run_queues();
  1503. /* Raise the softirq only if required. */
  1504. if (time_before(jiffies, base->clk)) {
  1505. if (!IS_ENABLED(CONFIG_NO_HZ_COMMON) || !base->nohz_active)
  1506. return;
  1507. /* CPU is awake, so check the deferrable base. */
  1508. base++;
  1509. if (time_before(jiffies, base->clk))
  1510. return;
  1511. }
  1512. raise_softirq(TIMER_SOFTIRQ);
  1513. }
  1514. #ifdef __ARCH_WANT_SYS_ALARM
  1515. /*
  1516. * For backwards compatibility? This can be done in libc so Alpha
  1517. * and all newer ports shouldn't need it.
  1518. */
  1519. SYSCALL_DEFINE1(alarm, unsigned int, seconds)
  1520. {
  1521. return alarm_setitimer(seconds);
  1522. }
  1523. #endif
  1524. static void process_timeout(unsigned long __data)
  1525. {
  1526. wake_up_process((struct task_struct *)__data);
  1527. }
  1528. /**
  1529. * schedule_timeout - sleep until timeout
  1530. * @timeout: timeout value in jiffies
  1531. *
  1532. * Make the current task sleep until @timeout jiffies have
  1533. * elapsed. The routine will return immediately unless
  1534. * the current task state has been set (see set_current_state()).
  1535. *
  1536. * You can set the task state as follows -
  1537. *
  1538. * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
  1539. * pass before the routine returns. The routine will return 0
  1540. *
  1541. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  1542. * delivered to the current task. In this case the remaining time
  1543. * in jiffies will be returned, or 0 if the timer expired in time
  1544. *
  1545. * The current task state is guaranteed to be TASK_RUNNING when this
  1546. * routine returns.
  1547. *
  1548. * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
  1549. * the CPU away without a bound on the timeout. In this case the return
  1550. * value will be %MAX_SCHEDULE_TIMEOUT.
  1551. *
  1552. * In all cases the return value is guaranteed to be non-negative.
  1553. */
  1554. signed long __sched schedule_timeout(signed long timeout)
  1555. {
  1556. struct timer_list timer;
  1557. unsigned long expire;
  1558. switch (timeout)
  1559. {
  1560. case MAX_SCHEDULE_TIMEOUT:
  1561. /*
  1562. * These two special cases are useful to be comfortable
  1563. * in the caller. Nothing more. We could take
  1564. * MAX_SCHEDULE_TIMEOUT from one of the negative value
  1565. * but I' d like to return a valid offset (>=0) to allow
  1566. * the caller to do everything it want with the retval.
  1567. */
  1568. schedule();
  1569. goto out;
  1570. default:
  1571. /*
  1572. * Another bit of PARANOID. Note that the retval will be
  1573. * 0 since no piece of kernel is supposed to do a check
  1574. * for a negative retval of schedule_timeout() (since it
  1575. * should never happens anyway). You just have the printk()
  1576. * that will tell you if something is gone wrong and where.
  1577. */
  1578. if (timeout < 0) {
  1579. printk(KERN_ERR "schedule_timeout: wrong timeout "
  1580. "value %lx\n", timeout);
  1581. dump_stack();
  1582. current->state = TASK_RUNNING;
  1583. goto out;
  1584. }
  1585. }
  1586. expire = timeout + jiffies;
  1587. setup_timer_on_stack(&timer, process_timeout, (unsigned long)current);
  1588. __mod_timer(&timer, expire, false);
  1589. schedule();
  1590. del_singleshot_timer_sync(&timer);
  1591. /* Remove the timer from the object tracker */
  1592. destroy_timer_on_stack(&timer);
  1593. timeout = expire - jiffies;
  1594. out:
  1595. return timeout < 0 ? 0 : timeout;
  1596. }
  1597. EXPORT_SYMBOL(schedule_timeout);
  1598. /*
  1599. * We can use __set_current_state() here because schedule_timeout() calls
  1600. * schedule() unconditionally.
  1601. */
  1602. signed long __sched schedule_timeout_interruptible(signed long timeout)
  1603. {
  1604. __set_current_state(TASK_INTERRUPTIBLE);
  1605. return schedule_timeout(timeout);
  1606. }
  1607. EXPORT_SYMBOL(schedule_timeout_interruptible);
  1608. signed long __sched schedule_timeout_killable(signed long timeout)
  1609. {
  1610. __set_current_state(TASK_KILLABLE);
  1611. return schedule_timeout(timeout);
  1612. }
  1613. EXPORT_SYMBOL(schedule_timeout_killable);
  1614. signed long __sched schedule_timeout_uninterruptible(signed long timeout)
  1615. {
  1616. __set_current_state(TASK_UNINTERRUPTIBLE);
  1617. return schedule_timeout(timeout);
  1618. }
  1619. EXPORT_SYMBOL(schedule_timeout_uninterruptible);
  1620. /*
  1621. * Like schedule_timeout_uninterruptible(), except this task will not contribute
  1622. * to load average.
  1623. */
  1624. signed long __sched schedule_timeout_idle(signed long timeout)
  1625. {
  1626. __set_current_state(TASK_IDLE);
  1627. return schedule_timeout(timeout);
  1628. }
  1629. EXPORT_SYMBOL(schedule_timeout_idle);
  1630. #ifdef CONFIG_HOTPLUG_CPU
  1631. static void migrate_timer_list(struct timer_base *new_base, struct hlist_head *head)
  1632. {
  1633. struct timer_list *timer;
  1634. int cpu = new_base->cpu;
  1635. while (!hlist_empty(head)) {
  1636. timer = hlist_entry(head->first, struct timer_list, entry);
  1637. detach_timer(timer, false);
  1638. timer->flags = (timer->flags & ~TIMER_BASEMASK) | cpu;
  1639. internal_add_timer(new_base, timer);
  1640. }
  1641. }
  1642. int timers_dead_cpu(unsigned int cpu)
  1643. {
  1644. struct timer_base *old_base;
  1645. struct timer_base *new_base;
  1646. int b, i;
  1647. BUG_ON(cpu_online(cpu));
  1648. for (b = 0; b < NR_BASES; b++) {
  1649. old_base = per_cpu_ptr(&timer_bases[b], cpu);
  1650. new_base = get_cpu_ptr(&timer_bases[b]);
  1651. /*
  1652. * The caller is globally serialized and nobody else
  1653. * takes two locks at once, deadlock is not possible.
  1654. */
  1655. spin_lock_irq(&new_base->lock);
  1656. spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
  1657. BUG_ON(old_base->running_timer);
  1658. for (i = 0; i < WHEEL_SIZE; i++)
  1659. migrate_timer_list(new_base, old_base->vectors + i);
  1660. spin_unlock(&old_base->lock);
  1661. spin_unlock_irq(&new_base->lock);
  1662. put_cpu_ptr(&timer_bases);
  1663. }
  1664. return 0;
  1665. }
  1666. #endif /* CONFIG_HOTPLUG_CPU */
  1667. static void __init init_timer_cpu(int cpu)
  1668. {
  1669. struct timer_base *base;
  1670. int i;
  1671. for (i = 0; i < NR_BASES; i++) {
  1672. base = per_cpu_ptr(&timer_bases[i], cpu);
  1673. base->cpu = cpu;
  1674. spin_lock_init(&base->lock);
  1675. base->clk = jiffies;
  1676. }
  1677. }
  1678. static void __init init_timer_cpus(void)
  1679. {
  1680. int cpu;
  1681. for_each_possible_cpu(cpu)
  1682. init_timer_cpu(cpu);
  1683. }
  1684. void __init init_timers(void)
  1685. {
  1686. init_timer_cpus();
  1687. init_timer_stats();
  1688. open_softirq(TIMER_SOFTIRQ, run_timer_softirq);
  1689. }
  1690. /**
  1691. * msleep - sleep safely even with waitqueue interruptions
  1692. * @msecs: Time in milliseconds to sleep for
  1693. */
  1694. void msleep(unsigned int msecs)
  1695. {
  1696. unsigned long timeout = msecs_to_jiffies(msecs) + 1;
  1697. while (timeout)
  1698. timeout = schedule_timeout_uninterruptible(timeout);
  1699. }
  1700. EXPORT_SYMBOL(msleep);
  1701. /**
  1702. * msleep_interruptible - sleep waiting for signals
  1703. * @msecs: Time in milliseconds to sleep for
  1704. */
  1705. unsigned long msleep_interruptible(unsigned int msecs)
  1706. {
  1707. unsigned long timeout = msecs_to_jiffies(msecs) + 1;
  1708. while (timeout && !signal_pending(current))
  1709. timeout = schedule_timeout_interruptible(timeout);
  1710. return jiffies_to_msecs(timeout);
  1711. }
  1712. EXPORT_SYMBOL(msleep_interruptible);
  1713. static void __sched do_usleep_range(unsigned long min, unsigned long max)
  1714. {
  1715. ktime_t kmin;
  1716. u64 delta;
  1717. kmin = ktime_set(0, min * NSEC_PER_USEC);
  1718. delta = (u64)(max - min) * NSEC_PER_USEC;
  1719. schedule_hrtimeout_range(&kmin, delta, HRTIMER_MODE_REL);
  1720. }
  1721. /**
  1722. * usleep_range - Sleep for an approximate time
  1723. * @min: Minimum time in usecs to sleep
  1724. * @max: Maximum time in usecs to sleep
  1725. *
  1726. * In non-atomic context where the exact wakeup time is flexible, use
  1727. * usleep_range() instead of udelay(). The sleep improves responsiveness
  1728. * by avoiding the CPU-hogging busy-wait of udelay(), and the range reduces
  1729. * power usage by allowing hrtimers to take advantage of an already-
  1730. * scheduled interrupt instead of scheduling a new one just for this sleep.
  1731. */
  1732. void __sched usleep_range(unsigned long min, unsigned long max)
  1733. {
  1734. __set_current_state(TASK_UNINTERRUPTIBLE);
  1735. do_usleep_range(min, max);
  1736. }
  1737. EXPORT_SYMBOL(usleep_range);