timer.c 53 KB

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