timer.c 54 KB

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