timer.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715
  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. * per-CPU timer vector definitions:
  56. */
  57. #define TVN_BITS (CONFIG_BASE_SMALL ? 4 : 6)
  58. #define TVR_BITS (CONFIG_BASE_SMALL ? 6 : 8)
  59. #define TVN_SIZE (1 << TVN_BITS)
  60. #define TVR_SIZE (1 << TVR_BITS)
  61. #define TVN_MASK (TVN_SIZE - 1)
  62. #define TVR_MASK (TVR_SIZE - 1)
  63. #define MAX_TVAL ((unsigned long)((1ULL << (TVR_BITS + 4*TVN_BITS)) - 1))
  64. struct tvec {
  65. struct list_head vec[TVN_SIZE];
  66. };
  67. struct tvec_root {
  68. struct list_head vec[TVR_SIZE];
  69. };
  70. struct tvec_base {
  71. spinlock_t lock;
  72. struct timer_list *running_timer;
  73. unsigned long timer_jiffies;
  74. unsigned long next_timer;
  75. unsigned long active_timers;
  76. unsigned long all_timers;
  77. int cpu;
  78. struct tvec_root tv1;
  79. struct tvec tv2;
  80. struct tvec tv3;
  81. struct tvec tv4;
  82. struct tvec tv5;
  83. } ____cacheline_aligned;
  84. /*
  85. * __TIMER_INITIALIZER() needs to set ->base to a valid pointer (because we've
  86. * made NULL special, hint: lock_timer_base()) and we cannot get a compile time
  87. * pointer to per-cpu entries because we don't know where we'll map the section,
  88. * even for the boot cpu.
  89. *
  90. * And so we use boot_tvec_bases for boot CPU and per-cpu __tvec_bases for the
  91. * rest of them.
  92. */
  93. struct tvec_base boot_tvec_bases;
  94. EXPORT_SYMBOL(boot_tvec_bases);
  95. static DEFINE_PER_CPU(struct tvec_base *, tvec_bases) = &boot_tvec_bases;
  96. /* Functions below help us manage 'deferrable' flag */
  97. static inline unsigned int tbase_get_deferrable(struct tvec_base *base)
  98. {
  99. return ((unsigned int)(unsigned long)base & TIMER_DEFERRABLE);
  100. }
  101. static inline unsigned int tbase_get_irqsafe(struct tvec_base *base)
  102. {
  103. return ((unsigned int)(unsigned long)base & TIMER_IRQSAFE);
  104. }
  105. static inline struct tvec_base *tbase_get_base(struct tvec_base *base)
  106. {
  107. return ((struct tvec_base *)((unsigned long)base & ~TIMER_FLAG_MASK));
  108. }
  109. static inline void
  110. timer_set_base(struct timer_list *timer, struct tvec_base *new_base)
  111. {
  112. unsigned long flags = (unsigned long)timer->base & TIMER_FLAG_MASK;
  113. timer->base = (struct tvec_base *)((unsigned long)(new_base) | flags);
  114. }
  115. static unsigned long round_jiffies_common(unsigned long j, int cpu,
  116. bool force_up)
  117. {
  118. int rem;
  119. unsigned long original = j;
  120. /*
  121. * We don't want all cpus firing their timers at once hitting the
  122. * same lock or cachelines, so we skew each extra cpu with an extra
  123. * 3 jiffies. This 3 jiffies came originally from the mm/ code which
  124. * already did this.
  125. * The skew is done by adding 3*cpunr, then round, then subtract this
  126. * extra offset again.
  127. */
  128. j += cpu * 3;
  129. rem = j % HZ;
  130. /*
  131. * If the target jiffie is just after a whole second (which can happen
  132. * due to delays of the timer irq, long irq off times etc etc) then
  133. * we should round down to the whole second, not up. Use 1/4th second
  134. * as cutoff for this rounding as an extreme upper bound for this.
  135. * But never round down if @force_up is set.
  136. */
  137. if (rem < HZ/4 && !force_up) /* round down */
  138. j = j - rem;
  139. else /* round up */
  140. j = j - rem + HZ;
  141. /* now that we have rounded, subtract the extra skew again */
  142. j -= cpu * 3;
  143. /*
  144. * Make sure j is still in the future. Otherwise return the
  145. * unmodified value.
  146. */
  147. return time_is_after_jiffies(j) ? j : original;
  148. }
  149. /**
  150. * __round_jiffies - function to round jiffies to a full second
  151. * @j: the time in (absolute) jiffies that should be rounded
  152. * @cpu: the processor number on which the timeout will happen
  153. *
  154. * __round_jiffies() rounds an absolute time in the future (in jiffies)
  155. * up or down to (approximately) full seconds. This is useful for timers
  156. * for which the exact time they fire does not matter too much, as long as
  157. * they fire approximately every X seconds.
  158. *
  159. * By rounding these timers to whole seconds, all such timers will fire
  160. * at the same time, rather than at various times spread out. The goal
  161. * of this is to have the CPU wake up less, which saves power.
  162. *
  163. * The exact rounding is skewed for each processor to avoid all
  164. * processors firing at the exact same time, which could lead
  165. * to lock contention or spurious cache line bouncing.
  166. *
  167. * The return value is the rounded version of the @j parameter.
  168. */
  169. unsigned long __round_jiffies(unsigned long j, int cpu)
  170. {
  171. return round_jiffies_common(j, cpu, false);
  172. }
  173. EXPORT_SYMBOL_GPL(__round_jiffies);
  174. /**
  175. * __round_jiffies_relative - function to round jiffies to a full second
  176. * @j: the time in (relative) jiffies that should be rounded
  177. * @cpu: the processor number on which the timeout will happen
  178. *
  179. * __round_jiffies_relative() rounds a time delta in the future (in jiffies)
  180. * up or down to (approximately) full seconds. This is useful for timers
  181. * for which the exact time they fire does not matter too much, as long as
  182. * they fire approximately every X seconds.
  183. *
  184. * By rounding these timers to whole seconds, all such timers will fire
  185. * at the same time, rather than at various times spread out. The goal
  186. * of this is to have the CPU wake up less, which saves power.
  187. *
  188. * The exact rounding is skewed for each processor to avoid all
  189. * processors firing at the exact same time, which could lead
  190. * to lock contention or spurious cache line bouncing.
  191. *
  192. * The return value is the rounded version of the @j parameter.
  193. */
  194. unsigned long __round_jiffies_relative(unsigned long j, int cpu)
  195. {
  196. unsigned long j0 = jiffies;
  197. /* Use j0 because jiffies might change while we run */
  198. return round_jiffies_common(j + j0, cpu, false) - j0;
  199. }
  200. EXPORT_SYMBOL_GPL(__round_jiffies_relative);
  201. /**
  202. * round_jiffies - function to round jiffies to a full second
  203. * @j: the time in (absolute) jiffies that should be rounded
  204. *
  205. * round_jiffies() rounds an absolute time in the future (in jiffies)
  206. * up or down to (approximately) full seconds. This is useful for timers
  207. * for which the exact time they fire does not matter too much, as long as
  208. * they fire approximately every X seconds.
  209. *
  210. * By rounding these timers to whole seconds, all such timers will fire
  211. * at the same time, rather than at various times spread out. The goal
  212. * of this is to have the CPU wake up less, which saves power.
  213. *
  214. * The return value is the rounded version of the @j parameter.
  215. */
  216. unsigned long round_jiffies(unsigned long j)
  217. {
  218. return round_jiffies_common(j, raw_smp_processor_id(), false);
  219. }
  220. EXPORT_SYMBOL_GPL(round_jiffies);
  221. /**
  222. * round_jiffies_relative - function to round jiffies to a full second
  223. * @j: the time in (relative) jiffies that should be rounded
  224. *
  225. * round_jiffies_relative() rounds a time delta in the future (in jiffies)
  226. * up or down to (approximately) full seconds. This is useful for timers
  227. * for which the exact time they fire does not matter too much, as long as
  228. * they fire approximately every X seconds.
  229. *
  230. * By rounding these timers to whole seconds, all such timers will fire
  231. * at the same time, rather than at various times spread out. The goal
  232. * of this is to have the CPU wake up less, which saves power.
  233. *
  234. * The return value is the rounded version of the @j parameter.
  235. */
  236. unsigned long round_jiffies_relative(unsigned long j)
  237. {
  238. return __round_jiffies_relative(j, raw_smp_processor_id());
  239. }
  240. EXPORT_SYMBOL_GPL(round_jiffies_relative);
  241. /**
  242. * __round_jiffies_up - function to round jiffies up to a full second
  243. * @j: the time in (absolute) jiffies that should be rounded
  244. * @cpu: the processor number on which the timeout will happen
  245. *
  246. * This is the same as __round_jiffies() except that it will never
  247. * round down. This is useful for timeouts for which the exact time
  248. * of firing does not matter too much, as long as they don't fire too
  249. * early.
  250. */
  251. unsigned long __round_jiffies_up(unsigned long j, int cpu)
  252. {
  253. return round_jiffies_common(j, cpu, true);
  254. }
  255. EXPORT_SYMBOL_GPL(__round_jiffies_up);
  256. /**
  257. * __round_jiffies_up_relative - function to round jiffies up to a full second
  258. * @j: the time in (relative) jiffies that should be rounded
  259. * @cpu: the processor number on which the timeout will happen
  260. *
  261. * This is the same as __round_jiffies_relative() except that it will never
  262. * round down. This is useful for timeouts for which the exact time
  263. * of firing does not matter too much, as long as they don't fire too
  264. * early.
  265. */
  266. unsigned long __round_jiffies_up_relative(unsigned long j, int cpu)
  267. {
  268. unsigned long j0 = jiffies;
  269. /* Use j0 because jiffies might change while we run */
  270. return round_jiffies_common(j + j0, cpu, true) - j0;
  271. }
  272. EXPORT_SYMBOL_GPL(__round_jiffies_up_relative);
  273. /**
  274. * round_jiffies_up - function to round jiffies up to a full second
  275. * @j: the time in (absolute) jiffies that should be rounded
  276. *
  277. * This is the same as round_jiffies() except that it will never
  278. * round down. This is useful for timeouts for which the exact time
  279. * of firing does not matter too much, as long as they don't fire too
  280. * early.
  281. */
  282. unsigned long round_jiffies_up(unsigned long j)
  283. {
  284. return round_jiffies_common(j, raw_smp_processor_id(), true);
  285. }
  286. EXPORT_SYMBOL_GPL(round_jiffies_up);
  287. /**
  288. * round_jiffies_up_relative - function to round jiffies up to a full second
  289. * @j: the time in (relative) jiffies that should be rounded
  290. *
  291. * This is the same as round_jiffies_relative() except that it will never
  292. * round down. This is useful for timeouts for which the exact time
  293. * of firing does not matter too much, as long as they don't fire too
  294. * early.
  295. */
  296. unsigned long round_jiffies_up_relative(unsigned long j)
  297. {
  298. return __round_jiffies_up_relative(j, raw_smp_processor_id());
  299. }
  300. EXPORT_SYMBOL_GPL(round_jiffies_up_relative);
  301. /**
  302. * set_timer_slack - set the allowed slack for a timer
  303. * @timer: the timer to be modified
  304. * @slack_hz: the amount of time (in jiffies) allowed for rounding
  305. *
  306. * Set the amount of time, in jiffies, that a certain timer has
  307. * in terms of slack. By setting this value, the timer subsystem
  308. * will schedule the actual timer somewhere between
  309. * the time mod_timer() asks for, and that time plus the slack.
  310. *
  311. * By setting the slack to -1, a percentage of the delay is used
  312. * instead.
  313. */
  314. void set_timer_slack(struct timer_list *timer, int slack_hz)
  315. {
  316. timer->slack = slack_hz;
  317. }
  318. EXPORT_SYMBOL_GPL(set_timer_slack);
  319. /*
  320. * If the list is empty, catch up ->timer_jiffies to the current time.
  321. * The caller must hold the tvec_base lock. Returns true if the list
  322. * was empty and therefore ->timer_jiffies was updated.
  323. */
  324. static bool catchup_timer_jiffies(struct tvec_base *base)
  325. {
  326. if (!base->all_timers) {
  327. base->timer_jiffies = jiffies;
  328. return true;
  329. }
  330. return false;
  331. }
  332. static void
  333. __internal_add_timer(struct tvec_base *base, struct timer_list *timer)
  334. {
  335. unsigned long expires = timer->expires;
  336. unsigned long idx = expires - base->timer_jiffies;
  337. struct list_head *vec;
  338. if (idx < TVR_SIZE) {
  339. int i = expires & TVR_MASK;
  340. vec = base->tv1.vec + i;
  341. } else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
  342. int i = (expires >> TVR_BITS) & TVN_MASK;
  343. vec = base->tv2.vec + i;
  344. } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {
  345. int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
  346. vec = base->tv3.vec + i;
  347. } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {
  348. int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
  349. vec = base->tv4.vec + i;
  350. } else if ((signed long) idx < 0) {
  351. /*
  352. * Can happen if you add a timer with expires == jiffies,
  353. * or you set a timer to go off in the past
  354. */
  355. vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK);
  356. } else {
  357. int i;
  358. /* If the timeout is larger than MAX_TVAL (on 64-bit
  359. * architectures or with CONFIG_BASE_SMALL=1) then we
  360. * use the maximum timeout.
  361. */
  362. if (idx > MAX_TVAL) {
  363. idx = MAX_TVAL;
  364. expires = idx + base->timer_jiffies;
  365. }
  366. i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
  367. vec = base->tv5.vec + i;
  368. }
  369. /*
  370. * Timers are FIFO:
  371. */
  372. list_add_tail(&timer->entry, vec);
  373. }
  374. static void internal_add_timer(struct tvec_base *base, struct timer_list *timer)
  375. {
  376. (void)catchup_timer_jiffies(base);
  377. __internal_add_timer(base, timer);
  378. /*
  379. * Update base->active_timers and base->next_timer
  380. */
  381. if (!tbase_get_deferrable(timer->base)) {
  382. if (!base->active_timers++ ||
  383. time_before(timer->expires, base->next_timer))
  384. base->next_timer = timer->expires;
  385. }
  386. base->all_timers++;
  387. /*
  388. * Check whether the other CPU is in dynticks mode and needs
  389. * to be triggered to reevaluate the timer wheel.
  390. * We are protected against the other CPU fiddling
  391. * with the timer by holding the timer base lock. This also
  392. * makes sure that a CPU on the way to stop its tick can not
  393. * evaluate the timer wheel.
  394. *
  395. * Spare the IPI for deferrable timers on idle targets though.
  396. * The next busy ticks will take care of it. Except full dynticks
  397. * require special care against races with idle_cpu(), lets deal
  398. * with that later.
  399. */
  400. if (!tbase_get_deferrable(timer->base) || tick_nohz_full_cpu(base->cpu))
  401. wake_up_nohz_cpu(base->cpu);
  402. }
  403. #ifdef CONFIG_TIMER_STATS
  404. void __timer_stats_timer_set_start_info(struct timer_list *timer, void *addr)
  405. {
  406. if (timer->start_site)
  407. return;
  408. timer->start_site = addr;
  409. memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
  410. timer->start_pid = current->pid;
  411. }
  412. static void timer_stats_account_timer(struct timer_list *timer)
  413. {
  414. unsigned int flag = 0;
  415. if (likely(!timer->start_site))
  416. return;
  417. if (unlikely(tbase_get_deferrable(timer->base)))
  418. flag |= TIMER_STATS_FLAG_DEFERRABLE;
  419. timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
  420. timer->function, timer->start_comm, flag);
  421. }
  422. #else
  423. static void timer_stats_account_timer(struct timer_list *timer) {}
  424. #endif
  425. #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
  426. static struct debug_obj_descr timer_debug_descr;
  427. static void *timer_debug_hint(void *addr)
  428. {
  429. return ((struct timer_list *) addr)->function;
  430. }
  431. /*
  432. * fixup_init is called when:
  433. * - an active object is initialized
  434. */
  435. static int timer_fixup_init(void *addr, enum debug_obj_state state)
  436. {
  437. struct timer_list *timer = addr;
  438. switch (state) {
  439. case ODEBUG_STATE_ACTIVE:
  440. del_timer_sync(timer);
  441. debug_object_init(timer, &timer_debug_descr);
  442. return 1;
  443. default:
  444. return 0;
  445. }
  446. }
  447. /* Stub timer callback for improperly used timers. */
  448. static void stub_timer(unsigned long data)
  449. {
  450. WARN_ON(1);
  451. }
  452. /*
  453. * fixup_activate is called when:
  454. * - an active object is activated
  455. * - an unknown object is activated (might be a statically initialized object)
  456. */
  457. static int timer_fixup_activate(void *addr, enum debug_obj_state state)
  458. {
  459. struct timer_list *timer = addr;
  460. switch (state) {
  461. case ODEBUG_STATE_NOTAVAILABLE:
  462. /*
  463. * This is not really a fixup. The timer was
  464. * statically initialized. We just make sure that it
  465. * is tracked in the object tracker.
  466. */
  467. if (timer->entry.next == NULL &&
  468. timer->entry.prev == TIMER_ENTRY_STATIC) {
  469. debug_object_init(timer, &timer_debug_descr);
  470. debug_object_activate(timer, &timer_debug_descr);
  471. return 0;
  472. } else {
  473. setup_timer(timer, stub_timer, 0);
  474. return 1;
  475. }
  476. return 0;
  477. case ODEBUG_STATE_ACTIVE:
  478. WARN_ON(1);
  479. default:
  480. return 0;
  481. }
  482. }
  483. /*
  484. * fixup_free is called when:
  485. * - an active object is freed
  486. */
  487. static int timer_fixup_free(void *addr, enum debug_obj_state state)
  488. {
  489. struct timer_list *timer = addr;
  490. switch (state) {
  491. case ODEBUG_STATE_ACTIVE:
  492. del_timer_sync(timer);
  493. debug_object_free(timer, &timer_debug_descr);
  494. return 1;
  495. default:
  496. return 0;
  497. }
  498. }
  499. /*
  500. * fixup_assert_init is called when:
  501. * - an untracked/uninit-ed object is found
  502. */
  503. static int timer_fixup_assert_init(void *addr, enum debug_obj_state state)
  504. {
  505. struct timer_list *timer = addr;
  506. switch (state) {
  507. case ODEBUG_STATE_NOTAVAILABLE:
  508. if (timer->entry.prev == TIMER_ENTRY_STATIC) {
  509. /*
  510. * This is not really a fixup. The timer was
  511. * statically initialized. We just make sure that it
  512. * is tracked in the object tracker.
  513. */
  514. debug_object_init(timer, &timer_debug_descr);
  515. return 0;
  516. } else {
  517. setup_timer(timer, stub_timer, 0);
  518. return 1;
  519. }
  520. default:
  521. return 0;
  522. }
  523. }
  524. static struct debug_obj_descr timer_debug_descr = {
  525. .name = "timer_list",
  526. .debug_hint = timer_debug_hint,
  527. .fixup_init = timer_fixup_init,
  528. .fixup_activate = timer_fixup_activate,
  529. .fixup_free = timer_fixup_free,
  530. .fixup_assert_init = timer_fixup_assert_init,
  531. };
  532. static inline void debug_timer_init(struct timer_list *timer)
  533. {
  534. debug_object_init(timer, &timer_debug_descr);
  535. }
  536. static inline void debug_timer_activate(struct timer_list *timer)
  537. {
  538. debug_object_activate(timer, &timer_debug_descr);
  539. }
  540. static inline void debug_timer_deactivate(struct timer_list *timer)
  541. {
  542. debug_object_deactivate(timer, &timer_debug_descr);
  543. }
  544. static inline void debug_timer_free(struct timer_list *timer)
  545. {
  546. debug_object_free(timer, &timer_debug_descr);
  547. }
  548. static inline void debug_timer_assert_init(struct timer_list *timer)
  549. {
  550. debug_object_assert_init(timer, &timer_debug_descr);
  551. }
  552. static void do_init_timer(struct timer_list *timer, unsigned int flags,
  553. const char *name, struct lock_class_key *key);
  554. void init_timer_on_stack_key(struct timer_list *timer, unsigned int flags,
  555. const char *name, struct lock_class_key *key)
  556. {
  557. debug_object_init_on_stack(timer, &timer_debug_descr);
  558. do_init_timer(timer, flags, name, key);
  559. }
  560. EXPORT_SYMBOL_GPL(init_timer_on_stack_key);
  561. void destroy_timer_on_stack(struct timer_list *timer)
  562. {
  563. debug_object_free(timer, &timer_debug_descr);
  564. }
  565. EXPORT_SYMBOL_GPL(destroy_timer_on_stack);
  566. #else
  567. static inline void debug_timer_init(struct timer_list *timer) { }
  568. static inline void debug_timer_activate(struct timer_list *timer) { }
  569. static inline void debug_timer_deactivate(struct timer_list *timer) { }
  570. static inline void debug_timer_assert_init(struct timer_list *timer) { }
  571. #endif
  572. static inline void debug_init(struct timer_list *timer)
  573. {
  574. debug_timer_init(timer);
  575. trace_timer_init(timer);
  576. }
  577. static inline void
  578. debug_activate(struct timer_list *timer, unsigned long expires)
  579. {
  580. debug_timer_activate(timer);
  581. trace_timer_start(timer, expires, tbase_get_deferrable(timer->base));
  582. }
  583. static inline void debug_deactivate(struct timer_list *timer)
  584. {
  585. debug_timer_deactivate(timer);
  586. trace_timer_cancel(timer);
  587. }
  588. static inline void debug_assert_init(struct timer_list *timer)
  589. {
  590. debug_timer_assert_init(timer);
  591. }
  592. static void do_init_timer(struct timer_list *timer, unsigned int flags,
  593. const char *name, struct lock_class_key *key)
  594. {
  595. struct tvec_base *base = raw_cpu_read(tvec_bases);
  596. timer->entry.next = NULL;
  597. timer->base = (void *)((unsigned long)base | flags);
  598. timer->slack = -1;
  599. #ifdef CONFIG_TIMER_STATS
  600. timer->start_site = NULL;
  601. timer->start_pid = -1;
  602. memset(timer->start_comm, 0, TASK_COMM_LEN);
  603. #endif
  604. lockdep_init_map(&timer->lockdep_map, name, key, 0);
  605. }
  606. /**
  607. * init_timer_key - initialize a timer
  608. * @timer: the timer to be initialized
  609. * @flags: timer flags
  610. * @name: name of the timer
  611. * @key: lockdep class key of the fake lock used for tracking timer
  612. * sync lock dependencies
  613. *
  614. * init_timer_key() must be done to a timer prior calling *any* of the
  615. * other timer functions.
  616. */
  617. void init_timer_key(struct timer_list *timer, unsigned int flags,
  618. const char *name, struct lock_class_key *key)
  619. {
  620. debug_init(timer);
  621. do_init_timer(timer, flags, name, key);
  622. }
  623. EXPORT_SYMBOL(init_timer_key);
  624. static inline void detach_timer(struct timer_list *timer, bool clear_pending)
  625. {
  626. struct list_head *entry = &timer->entry;
  627. debug_deactivate(timer);
  628. __list_del(entry->prev, entry->next);
  629. if (clear_pending)
  630. entry->next = NULL;
  631. entry->prev = LIST_POISON2;
  632. }
  633. static inline void
  634. detach_expired_timer(struct timer_list *timer, struct tvec_base *base)
  635. {
  636. detach_timer(timer, true);
  637. if (!tbase_get_deferrable(timer->base))
  638. base->active_timers--;
  639. base->all_timers--;
  640. (void)catchup_timer_jiffies(base);
  641. }
  642. static int detach_if_pending(struct timer_list *timer, struct tvec_base *base,
  643. bool clear_pending)
  644. {
  645. if (!timer_pending(timer))
  646. return 0;
  647. detach_timer(timer, clear_pending);
  648. if (!tbase_get_deferrable(timer->base)) {
  649. base->active_timers--;
  650. if (timer->expires == base->next_timer)
  651. base->next_timer = base->timer_jiffies;
  652. }
  653. base->all_timers--;
  654. (void)catchup_timer_jiffies(base);
  655. return 1;
  656. }
  657. /*
  658. * We are using hashed locking: holding per_cpu(tvec_bases).lock
  659. * means that all timers which are tied to this base via timer->base are
  660. * locked, and the base itself is locked too.
  661. *
  662. * So __run_timers/migrate_timers can safely modify all timers which could
  663. * be found on ->tvX lists.
  664. *
  665. * When the timer's base is locked, and the timer removed from list, it is
  666. * possible to set timer->base = NULL and drop the lock: the timer remains
  667. * locked.
  668. */
  669. static struct tvec_base *lock_timer_base(struct timer_list *timer,
  670. unsigned long *flags)
  671. __acquires(timer->base->lock)
  672. {
  673. struct tvec_base *base;
  674. for (;;) {
  675. struct tvec_base *prelock_base = timer->base;
  676. base = tbase_get_base(prelock_base);
  677. if (likely(base != NULL)) {
  678. spin_lock_irqsave(&base->lock, *flags);
  679. if (likely(prelock_base == timer->base))
  680. return base;
  681. /* The timer has migrated to another CPU */
  682. spin_unlock_irqrestore(&base->lock, *flags);
  683. }
  684. cpu_relax();
  685. }
  686. }
  687. static inline int
  688. __mod_timer(struct timer_list *timer, unsigned long expires,
  689. bool pending_only, int pinned)
  690. {
  691. struct tvec_base *base, *new_base;
  692. unsigned long flags;
  693. int ret = 0 , cpu;
  694. timer_stats_timer_set_start_info(timer);
  695. BUG_ON(!timer->function);
  696. base = lock_timer_base(timer, &flags);
  697. ret = detach_if_pending(timer, base, false);
  698. if (!ret && pending_only)
  699. goto out_unlock;
  700. debug_activate(timer, expires);
  701. cpu = get_nohz_timer_target(pinned);
  702. new_base = per_cpu(tvec_bases, cpu);
  703. if (base != new_base) {
  704. /*
  705. * We are trying to schedule the timer on the local CPU.
  706. * However we can't change timer's base while it is running,
  707. * otherwise del_timer_sync() can't detect that the timer's
  708. * handler yet has not finished. This also guarantees that
  709. * the timer is serialized wrt itself.
  710. */
  711. if (likely(base->running_timer != timer)) {
  712. /* See the comment in lock_timer_base() */
  713. timer_set_base(timer, NULL);
  714. spin_unlock(&base->lock);
  715. base = new_base;
  716. spin_lock(&base->lock);
  717. timer_set_base(timer, base);
  718. }
  719. }
  720. timer->expires = expires;
  721. internal_add_timer(base, timer);
  722. out_unlock:
  723. spin_unlock_irqrestore(&base->lock, flags);
  724. return ret;
  725. }
  726. /**
  727. * mod_timer_pending - modify a pending timer's timeout
  728. * @timer: the pending timer to be modified
  729. * @expires: new timeout in jiffies
  730. *
  731. * mod_timer_pending() is the same for pending timers as mod_timer(),
  732. * but will not re-activate and modify already deleted timers.
  733. *
  734. * It is useful for unserialized use of timers.
  735. */
  736. int mod_timer_pending(struct timer_list *timer, unsigned long expires)
  737. {
  738. return __mod_timer(timer, expires, true, TIMER_NOT_PINNED);
  739. }
  740. EXPORT_SYMBOL(mod_timer_pending);
  741. /*
  742. * Decide where to put the timer while taking the slack into account
  743. *
  744. * Algorithm:
  745. * 1) calculate the maximum (absolute) time
  746. * 2) calculate the highest bit where the expires and new max are different
  747. * 3) use this bit to make a mask
  748. * 4) use the bitmask to round down the maximum time, so that all last
  749. * bits are zeros
  750. */
  751. static inline
  752. unsigned long apply_slack(struct timer_list *timer, unsigned long expires)
  753. {
  754. unsigned long expires_limit, mask;
  755. int bit;
  756. if (timer->slack >= 0) {
  757. expires_limit = expires + timer->slack;
  758. } else {
  759. long delta = expires - jiffies;
  760. if (delta < 256)
  761. return expires;
  762. expires_limit = expires + delta / 256;
  763. }
  764. mask = expires ^ expires_limit;
  765. if (mask == 0)
  766. return expires;
  767. bit = find_last_bit(&mask, BITS_PER_LONG);
  768. mask = (1UL << bit) - 1;
  769. expires_limit = expires_limit & ~(mask);
  770. return expires_limit;
  771. }
  772. /**
  773. * mod_timer - modify a timer's timeout
  774. * @timer: the timer to be modified
  775. * @expires: new timeout in jiffies
  776. *
  777. * mod_timer() is a more efficient way to update the expire field of an
  778. * active timer (if the timer is inactive it will be activated)
  779. *
  780. * mod_timer(timer, expires) is equivalent to:
  781. *
  782. * del_timer(timer); timer->expires = expires; add_timer(timer);
  783. *
  784. * Note that if there are multiple unserialized concurrent users of the
  785. * same timer, then mod_timer() is the only safe way to modify the timeout,
  786. * since add_timer() cannot modify an already running timer.
  787. *
  788. * The function returns whether it has modified a pending timer or not.
  789. * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
  790. * active timer returns 1.)
  791. */
  792. int mod_timer(struct timer_list *timer, unsigned long expires)
  793. {
  794. expires = apply_slack(timer, expires);
  795. /*
  796. * This is a common optimization triggered by the
  797. * networking code - if the timer is re-modified
  798. * to be the same thing then just return:
  799. */
  800. if (timer_pending(timer) && timer->expires == expires)
  801. return 1;
  802. return __mod_timer(timer, expires, false, TIMER_NOT_PINNED);
  803. }
  804. EXPORT_SYMBOL(mod_timer);
  805. /**
  806. * mod_timer_pinned - modify a timer's timeout
  807. * @timer: the timer to be modified
  808. * @expires: new timeout in jiffies
  809. *
  810. * mod_timer_pinned() is a way to update the expire field of an
  811. * active timer (if the timer is inactive it will be activated)
  812. * and to ensure that the timer is scheduled on the current CPU.
  813. *
  814. * Note that this does not prevent the timer from being migrated
  815. * when the current CPU goes offline. If this is a problem for
  816. * you, use CPU-hotplug notifiers to handle it correctly, for
  817. * example, cancelling the timer when the corresponding CPU goes
  818. * offline.
  819. *
  820. * mod_timer_pinned(timer, expires) is equivalent to:
  821. *
  822. * del_timer(timer); timer->expires = expires; add_timer(timer);
  823. */
  824. int mod_timer_pinned(struct timer_list *timer, unsigned long expires)
  825. {
  826. if (timer->expires == expires && timer_pending(timer))
  827. return 1;
  828. return __mod_timer(timer, expires, false, TIMER_PINNED);
  829. }
  830. EXPORT_SYMBOL(mod_timer_pinned);
  831. /**
  832. * add_timer - start a timer
  833. * @timer: the timer to be added
  834. *
  835. * The kernel will do a ->function(->data) callback from the
  836. * timer interrupt at the ->expires point in the future. The
  837. * current time is 'jiffies'.
  838. *
  839. * The timer's ->expires, ->function (and if the handler uses it, ->data)
  840. * fields must be set prior calling this function.
  841. *
  842. * Timers with an ->expires field in the past will be executed in the next
  843. * timer tick.
  844. */
  845. void add_timer(struct timer_list *timer)
  846. {
  847. BUG_ON(timer_pending(timer));
  848. mod_timer(timer, timer->expires);
  849. }
  850. EXPORT_SYMBOL(add_timer);
  851. /**
  852. * add_timer_on - start a timer on a particular CPU
  853. * @timer: the timer to be added
  854. * @cpu: the CPU to start it on
  855. *
  856. * This is not very scalable on SMP. Double adds are not possible.
  857. */
  858. void add_timer_on(struct timer_list *timer, int cpu)
  859. {
  860. struct tvec_base *base = per_cpu(tvec_bases, cpu);
  861. unsigned long flags;
  862. timer_stats_timer_set_start_info(timer);
  863. BUG_ON(timer_pending(timer) || !timer->function);
  864. spin_lock_irqsave(&base->lock, flags);
  865. timer_set_base(timer, base);
  866. debug_activate(timer, timer->expires);
  867. internal_add_timer(base, timer);
  868. spin_unlock_irqrestore(&base->lock, flags);
  869. }
  870. EXPORT_SYMBOL_GPL(add_timer_on);
  871. /**
  872. * del_timer - deactive a timer.
  873. * @timer: the timer to be deactivated
  874. *
  875. * del_timer() deactivates a timer - this works on both active and inactive
  876. * timers.
  877. *
  878. * The function returns whether it has deactivated a pending timer or not.
  879. * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
  880. * active timer returns 1.)
  881. */
  882. int del_timer(struct timer_list *timer)
  883. {
  884. struct tvec_base *base;
  885. unsigned long flags;
  886. int ret = 0;
  887. debug_assert_init(timer);
  888. timer_stats_timer_clear_start_info(timer);
  889. if (timer_pending(timer)) {
  890. base = lock_timer_base(timer, &flags);
  891. ret = detach_if_pending(timer, base, true);
  892. spin_unlock_irqrestore(&base->lock, flags);
  893. }
  894. return ret;
  895. }
  896. EXPORT_SYMBOL(del_timer);
  897. /**
  898. * try_to_del_timer_sync - Try to deactivate a timer
  899. * @timer: timer do del
  900. *
  901. * This function tries to deactivate a timer. Upon successful (ret >= 0)
  902. * exit the timer is not queued and the handler is not running on any CPU.
  903. */
  904. int try_to_del_timer_sync(struct timer_list *timer)
  905. {
  906. struct tvec_base *base;
  907. unsigned long flags;
  908. int ret = -1;
  909. debug_assert_init(timer);
  910. base = lock_timer_base(timer, &flags);
  911. if (base->running_timer != timer) {
  912. timer_stats_timer_clear_start_info(timer);
  913. ret = detach_if_pending(timer, base, true);
  914. }
  915. spin_unlock_irqrestore(&base->lock, flags);
  916. return ret;
  917. }
  918. EXPORT_SYMBOL(try_to_del_timer_sync);
  919. #ifdef CONFIG_SMP
  920. static DEFINE_PER_CPU(struct tvec_base, __tvec_bases);
  921. /**
  922. * del_timer_sync - deactivate a timer and wait for the handler to finish.
  923. * @timer: the timer to be deactivated
  924. *
  925. * This function only differs from del_timer() on SMP: besides deactivating
  926. * the timer it also makes sure the handler has finished executing on other
  927. * CPUs.
  928. *
  929. * Synchronization rules: Callers must prevent restarting of the timer,
  930. * otherwise this function is meaningless. It must not be called from
  931. * interrupt contexts unless the timer is an irqsafe one. The caller must
  932. * not hold locks which would prevent completion of the timer's
  933. * handler. The timer's handler must not call add_timer_on(). Upon exit the
  934. * timer is not queued and the handler is not running on any CPU.
  935. *
  936. * Note: For !irqsafe timers, you must not hold locks that are held in
  937. * interrupt context while calling this function. Even if the lock has
  938. * nothing to do with the timer in question. Here's why:
  939. *
  940. * CPU0 CPU1
  941. * ---- ----
  942. * <SOFTIRQ>
  943. * call_timer_fn();
  944. * base->running_timer = mytimer;
  945. * spin_lock_irq(somelock);
  946. * <IRQ>
  947. * spin_lock(somelock);
  948. * del_timer_sync(mytimer);
  949. * while (base->running_timer == mytimer);
  950. *
  951. * Now del_timer_sync() will never return and never release somelock.
  952. * The interrupt on the other CPU is waiting to grab somelock but
  953. * it has interrupted the softirq that CPU0 is waiting to finish.
  954. *
  955. * The function returns whether it has deactivated a pending timer or not.
  956. */
  957. int del_timer_sync(struct timer_list *timer)
  958. {
  959. #ifdef CONFIG_LOCKDEP
  960. unsigned long flags;
  961. /*
  962. * If lockdep gives a backtrace here, please reference
  963. * the synchronization rules above.
  964. */
  965. local_irq_save(flags);
  966. lock_map_acquire(&timer->lockdep_map);
  967. lock_map_release(&timer->lockdep_map);
  968. local_irq_restore(flags);
  969. #endif
  970. /*
  971. * don't use it in hardirq context, because it
  972. * could lead to deadlock.
  973. */
  974. WARN_ON(in_irq() && !tbase_get_irqsafe(timer->base));
  975. for (;;) {
  976. int ret = try_to_del_timer_sync(timer);
  977. if (ret >= 0)
  978. return ret;
  979. cpu_relax();
  980. }
  981. }
  982. EXPORT_SYMBOL(del_timer_sync);
  983. #endif
  984. static int cascade(struct tvec_base *base, struct tvec *tv, int index)
  985. {
  986. /* cascade all the timers from tv up one level */
  987. struct timer_list *timer, *tmp;
  988. struct list_head tv_list;
  989. list_replace_init(tv->vec + index, &tv_list);
  990. /*
  991. * We are removing _all_ timers from the list, so we
  992. * don't have to detach them individually.
  993. */
  994. list_for_each_entry_safe(timer, tmp, &tv_list, entry) {
  995. BUG_ON(tbase_get_base(timer->base) != base);
  996. /* No accounting, while moving them */
  997. __internal_add_timer(base, timer);
  998. }
  999. return index;
  1000. }
  1001. static void call_timer_fn(struct timer_list *timer, void (*fn)(unsigned long),
  1002. unsigned long data)
  1003. {
  1004. int count = preempt_count();
  1005. #ifdef CONFIG_LOCKDEP
  1006. /*
  1007. * It is permissible to free the timer from inside the
  1008. * function that is called from it, this we need to take into
  1009. * account for lockdep too. To avoid bogus "held lock freed"
  1010. * warnings as well as problems when looking into
  1011. * timer->lockdep_map, make a copy and use that here.
  1012. */
  1013. struct lockdep_map lockdep_map;
  1014. lockdep_copy_map(&lockdep_map, &timer->lockdep_map);
  1015. #endif
  1016. /*
  1017. * Couple the lock chain with the lock chain at
  1018. * del_timer_sync() by acquiring the lock_map around the fn()
  1019. * call here and in del_timer_sync().
  1020. */
  1021. lock_map_acquire(&lockdep_map);
  1022. trace_timer_expire_entry(timer);
  1023. fn(data);
  1024. trace_timer_expire_exit(timer);
  1025. lock_map_release(&lockdep_map);
  1026. if (count != preempt_count()) {
  1027. WARN_ONCE(1, "timer: %pF preempt leak: %08x -> %08x\n",
  1028. fn, count, preempt_count());
  1029. /*
  1030. * Restore the preempt count. That gives us a decent
  1031. * chance to survive and extract information. If the
  1032. * callback kept a lock held, bad luck, but not worse
  1033. * than the BUG() we had.
  1034. */
  1035. preempt_count_set(count);
  1036. }
  1037. }
  1038. #define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK)
  1039. /**
  1040. * __run_timers - run all expired timers (if any) on this CPU.
  1041. * @base: the timer vector to be processed.
  1042. *
  1043. * This function cascades all vectors and executes all expired timer
  1044. * vectors.
  1045. */
  1046. static inline void __run_timers(struct tvec_base *base)
  1047. {
  1048. struct timer_list *timer;
  1049. spin_lock_irq(&base->lock);
  1050. if (catchup_timer_jiffies(base)) {
  1051. spin_unlock_irq(&base->lock);
  1052. return;
  1053. }
  1054. while (time_after_eq(jiffies, base->timer_jiffies)) {
  1055. struct list_head work_list;
  1056. struct list_head *head = &work_list;
  1057. int index = base->timer_jiffies & TVR_MASK;
  1058. /*
  1059. * Cascade timers:
  1060. */
  1061. if (!index &&
  1062. (!cascade(base, &base->tv2, INDEX(0))) &&
  1063. (!cascade(base, &base->tv3, INDEX(1))) &&
  1064. !cascade(base, &base->tv4, INDEX(2)))
  1065. cascade(base, &base->tv5, INDEX(3));
  1066. ++base->timer_jiffies;
  1067. list_replace_init(base->tv1.vec + index, head);
  1068. while (!list_empty(head)) {
  1069. void (*fn)(unsigned long);
  1070. unsigned long data;
  1071. bool irqsafe;
  1072. timer = list_first_entry(head, struct timer_list,entry);
  1073. fn = timer->function;
  1074. data = timer->data;
  1075. irqsafe = tbase_get_irqsafe(timer->base);
  1076. timer_stats_account_timer(timer);
  1077. base->running_timer = timer;
  1078. detach_expired_timer(timer, base);
  1079. if (irqsafe) {
  1080. spin_unlock(&base->lock);
  1081. call_timer_fn(timer, fn, data);
  1082. spin_lock(&base->lock);
  1083. } else {
  1084. spin_unlock_irq(&base->lock);
  1085. call_timer_fn(timer, fn, data);
  1086. spin_lock_irq(&base->lock);
  1087. }
  1088. }
  1089. }
  1090. base->running_timer = NULL;
  1091. spin_unlock_irq(&base->lock);
  1092. }
  1093. #ifdef CONFIG_NO_HZ_COMMON
  1094. /*
  1095. * Find out when the next timer event is due to happen. This
  1096. * is used on S/390 to stop all activity when a CPU is idle.
  1097. * This function needs to be called with interrupts disabled.
  1098. */
  1099. static unsigned long __next_timer_interrupt(struct tvec_base *base)
  1100. {
  1101. unsigned long timer_jiffies = base->timer_jiffies;
  1102. unsigned long expires = timer_jiffies + NEXT_TIMER_MAX_DELTA;
  1103. int index, slot, array, found = 0;
  1104. struct timer_list *nte;
  1105. struct tvec *varray[4];
  1106. /* Look for timer events in tv1. */
  1107. index = slot = timer_jiffies & TVR_MASK;
  1108. do {
  1109. list_for_each_entry(nte, base->tv1.vec + slot, entry) {
  1110. if (tbase_get_deferrable(nte->base))
  1111. continue;
  1112. found = 1;
  1113. expires = nte->expires;
  1114. /* Look at the cascade bucket(s)? */
  1115. if (!index || slot < index)
  1116. goto cascade;
  1117. return expires;
  1118. }
  1119. slot = (slot + 1) & TVR_MASK;
  1120. } while (slot != index);
  1121. cascade:
  1122. /* Calculate the next cascade event */
  1123. if (index)
  1124. timer_jiffies += TVR_SIZE - index;
  1125. timer_jiffies >>= TVR_BITS;
  1126. /* Check tv2-tv5. */
  1127. varray[0] = &base->tv2;
  1128. varray[1] = &base->tv3;
  1129. varray[2] = &base->tv4;
  1130. varray[3] = &base->tv5;
  1131. for (array = 0; array < 4; array++) {
  1132. struct tvec *varp = varray[array];
  1133. index = slot = timer_jiffies & TVN_MASK;
  1134. do {
  1135. list_for_each_entry(nte, varp->vec + slot, entry) {
  1136. if (tbase_get_deferrable(nte->base))
  1137. continue;
  1138. found = 1;
  1139. if (time_before(nte->expires, expires))
  1140. expires = nte->expires;
  1141. }
  1142. /*
  1143. * Do we still search for the first timer or are
  1144. * we looking up the cascade buckets ?
  1145. */
  1146. if (found) {
  1147. /* Look at the cascade bucket(s)? */
  1148. if (!index || slot < index)
  1149. break;
  1150. return expires;
  1151. }
  1152. slot = (slot + 1) & TVN_MASK;
  1153. } while (slot != index);
  1154. if (index)
  1155. timer_jiffies += TVN_SIZE - index;
  1156. timer_jiffies >>= TVN_BITS;
  1157. }
  1158. return expires;
  1159. }
  1160. /*
  1161. * Check, if the next hrtimer event is before the next timer wheel
  1162. * event:
  1163. */
  1164. static u64 cmp_next_hrtimer_event(u64 basem, u64 expires)
  1165. {
  1166. u64 nextevt = hrtimer_get_next_event();
  1167. /*
  1168. * If high resolution timers are enabled
  1169. * hrtimer_get_next_event() returns KTIME_MAX.
  1170. */
  1171. if (expires <= nextevt)
  1172. return expires;
  1173. /*
  1174. * If the next timer is already expired, return the tick base
  1175. * time so the tick is fired immediately.
  1176. */
  1177. if (nextevt <= basem)
  1178. return basem;
  1179. /*
  1180. * Round up to the next jiffie. High resolution timers are
  1181. * off, so the hrtimers are expired in the tick and we need to
  1182. * make sure that this tick really expires the timer to avoid
  1183. * a ping pong of the nohz stop code.
  1184. *
  1185. * Use DIV_ROUND_UP_ULL to prevent gcc calling __divdi3
  1186. */
  1187. return DIV_ROUND_UP_ULL(nextevt, TICK_NSEC) * TICK_NSEC;
  1188. }
  1189. /**
  1190. * get_next_timer_interrupt - return the time (clock mono) of the next timer
  1191. * @basej: base time jiffies
  1192. * @basem: base time clock monotonic
  1193. *
  1194. * Returns the tick aligned clock monotonic time of the next pending
  1195. * timer or KTIME_MAX if no timer is pending.
  1196. */
  1197. u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
  1198. {
  1199. struct tvec_base *base = __this_cpu_read(tvec_bases);
  1200. u64 expires = KTIME_MAX;
  1201. unsigned long nextevt;
  1202. /*
  1203. * Pretend that there is no timer pending if the cpu is offline.
  1204. * Possible pending timers will be migrated later to an active cpu.
  1205. */
  1206. if (cpu_is_offline(smp_processor_id()))
  1207. return expires;
  1208. spin_lock(&base->lock);
  1209. if (base->active_timers) {
  1210. if (time_before_eq(base->next_timer, base->timer_jiffies))
  1211. base->next_timer = __next_timer_interrupt(base);
  1212. nextevt = base->next_timer;
  1213. if (time_before_eq(nextevt, basej))
  1214. expires = basem;
  1215. else
  1216. expires = basem + (nextevt - basej) * TICK_NSEC;
  1217. }
  1218. spin_unlock(&base->lock);
  1219. return cmp_next_hrtimer_event(basem, expires);
  1220. }
  1221. #endif
  1222. /*
  1223. * Called from the timer interrupt handler to charge one tick to the current
  1224. * process. user_tick is 1 if the tick is user time, 0 for system.
  1225. */
  1226. void update_process_times(int user_tick)
  1227. {
  1228. struct task_struct *p = current;
  1229. /* Note: this timer irq context must be accounted for as well. */
  1230. account_process_tick(p, user_tick);
  1231. run_local_timers();
  1232. rcu_check_callbacks(user_tick);
  1233. #ifdef CONFIG_IRQ_WORK
  1234. if (in_irq())
  1235. irq_work_tick();
  1236. #endif
  1237. scheduler_tick();
  1238. run_posix_cpu_timers(p);
  1239. }
  1240. /*
  1241. * This function runs timers and the timer-tq in bottom half context.
  1242. */
  1243. static void run_timer_softirq(struct softirq_action *h)
  1244. {
  1245. struct tvec_base *base = __this_cpu_read(tvec_bases);
  1246. if (time_after_eq(jiffies, base->timer_jiffies))
  1247. __run_timers(base);
  1248. }
  1249. /*
  1250. * Called by the local, per-CPU timer interrupt on SMP.
  1251. */
  1252. void run_local_timers(void)
  1253. {
  1254. hrtimer_run_queues();
  1255. raise_softirq(TIMER_SOFTIRQ);
  1256. }
  1257. #ifdef __ARCH_WANT_SYS_ALARM
  1258. /*
  1259. * For backwards compatibility? This can be done in libc so Alpha
  1260. * and all newer ports shouldn't need it.
  1261. */
  1262. SYSCALL_DEFINE1(alarm, unsigned int, seconds)
  1263. {
  1264. return alarm_setitimer(seconds);
  1265. }
  1266. #endif
  1267. static void process_timeout(unsigned long __data)
  1268. {
  1269. wake_up_process((struct task_struct *)__data);
  1270. }
  1271. /**
  1272. * schedule_timeout - sleep until timeout
  1273. * @timeout: timeout value in jiffies
  1274. *
  1275. * Make the current task sleep until @timeout jiffies have
  1276. * elapsed. The routine will return immediately unless
  1277. * the current task state has been set (see set_current_state()).
  1278. *
  1279. * You can set the task state as follows -
  1280. *
  1281. * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
  1282. * pass before the routine returns. The routine will return 0
  1283. *
  1284. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  1285. * delivered to the current task. In this case the remaining time
  1286. * in jiffies will be returned, or 0 if the timer expired in time
  1287. *
  1288. * The current task state is guaranteed to be TASK_RUNNING when this
  1289. * routine returns.
  1290. *
  1291. * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
  1292. * the CPU away without a bound on the timeout. In this case the return
  1293. * value will be %MAX_SCHEDULE_TIMEOUT.
  1294. *
  1295. * In all cases the return value is guaranteed to be non-negative.
  1296. */
  1297. signed long __sched schedule_timeout(signed long timeout)
  1298. {
  1299. struct timer_list timer;
  1300. unsigned long expire;
  1301. switch (timeout)
  1302. {
  1303. case MAX_SCHEDULE_TIMEOUT:
  1304. /*
  1305. * These two special cases are useful to be comfortable
  1306. * in the caller. Nothing more. We could take
  1307. * MAX_SCHEDULE_TIMEOUT from one of the negative value
  1308. * but I' d like to return a valid offset (>=0) to allow
  1309. * the caller to do everything it want with the retval.
  1310. */
  1311. schedule();
  1312. goto out;
  1313. default:
  1314. /*
  1315. * Another bit of PARANOID. Note that the retval will be
  1316. * 0 since no piece of kernel is supposed to do a check
  1317. * for a negative retval of schedule_timeout() (since it
  1318. * should never happens anyway). You just have the printk()
  1319. * that will tell you if something is gone wrong and where.
  1320. */
  1321. if (timeout < 0) {
  1322. printk(KERN_ERR "schedule_timeout: wrong timeout "
  1323. "value %lx\n", timeout);
  1324. dump_stack();
  1325. current->state = TASK_RUNNING;
  1326. goto out;
  1327. }
  1328. }
  1329. expire = timeout + jiffies;
  1330. setup_timer_on_stack(&timer, process_timeout, (unsigned long)current);
  1331. __mod_timer(&timer, expire, false, TIMER_NOT_PINNED);
  1332. schedule();
  1333. del_singleshot_timer_sync(&timer);
  1334. /* Remove the timer from the object tracker */
  1335. destroy_timer_on_stack(&timer);
  1336. timeout = expire - jiffies;
  1337. out:
  1338. return timeout < 0 ? 0 : timeout;
  1339. }
  1340. EXPORT_SYMBOL(schedule_timeout);
  1341. /*
  1342. * We can use __set_current_state() here because schedule_timeout() calls
  1343. * schedule() unconditionally.
  1344. */
  1345. signed long __sched schedule_timeout_interruptible(signed long timeout)
  1346. {
  1347. __set_current_state(TASK_INTERRUPTIBLE);
  1348. return schedule_timeout(timeout);
  1349. }
  1350. EXPORT_SYMBOL(schedule_timeout_interruptible);
  1351. signed long __sched schedule_timeout_killable(signed long timeout)
  1352. {
  1353. __set_current_state(TASK_KILLABLE);
  1354. return schedule_timeout(timeout);
  1355. }
  1356. EXPORT_SYMBOL(schedule_timeout_killable);
  1357. signed long __sched schedule_timeout_uninterruptible(signed long timeout)
  1358. {
  1359. __set_current_state(TASK_UNINTERRUPTIBLE);
  1360. return schedule_timeout(timeout);
  1361. }
  1362. EXPORT_SYMBOL(schedule_timeout_uninterruptible);
  1363. #ifdef CONFIG_HOTPLUG_CPU
  1364. static void migrate_timer_list(struct tvec_base *new_base, struct list_head *head)
  1365. {
  1366. struct timer_list *timer;
  1367. while (!list_empty(head)) {
  1368. timer = list_first_entry(head, struct timer_list, entry);
  1369. /* We ignore the accounting on the dying cpu */
  1370. detach_timer(timer, false);
  1371. timer_set_base(timer, new_base);
  1372. internal_add_timer(new_base, timer);
  1373. }
  1374. }
  1375. static void migrate_timers(int cpu)
  1376. {
  1377. struct tvec_base *old_base;
  1378. struct tvec_base *new_base;
  1379. int i;
  1380. BUG_ON(cpu_online(cpu));
  1381. old_base = per_cpu(tvec_bases, cpu);
  1382. new_base = get_cpu_var(tvec_bases);
  1383. /*
  1384. * The caller is globally serialized and nobody else
  1385. * takes two locks at once, deadlock is not possible.
  1386. */
  1387. spin_lock_irq(&new_base->lock);
  1388. spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
  1389. BUG_ON(old_base->running_timer);
  1390. for (i = 0; i < TVR_SIZE; i++)
  1391. migrate_timer_list(new_base, old_base->tv1.vec + i);
  1392. for (i = 0; i < TVN_SIZE; i++) {
  1393. migrate_timer_list(new_base, old_base->tv2.vec + i);
  1394. migrate_timer_list(new_base, old_base->tv3.vec + i);
  1395. migrate_timer_list(new_base, old_base->tv4.vec + i);
  1396. migrate_timer_list(new_base, old_base->tv5.vec + i);
  1397. }
  1398. old_base->active_timers = 0;
  1399. old_base->all_timers = 0;
  1400. spin_unlock(&old_base->lock);
  1401. spin_unlock_irq(&new_base->lock);
  1402. put_cpu_var(tvec_bases);
  1403. }
  1404. static int timer_cpu_notify(struct notifier_block *self,
  1405. unsigned long action, void *hcpu)
  1406. {
  1407. switch (action) {
  1408. case CPU_DEAD:
  1409. case CPU_DEAD_FROZEN:
  1410. migrate_timers((long)hcpu);
  1411. break;
  1412. default:
  1413. break;
  1414. }
  1415. return NOTIFY_OK;
  1416. }
  1417. static inline void timer_register_cpu_notifier(void)
  1418. {
  1419. cpu_notifier(timer_cpu_notify, 0);
  1420. }
  1421. #else
  1422. static inline void timer_register_cpu_notifier(void) { }
  1423. #endif /* CONFIG_HOTPLUG_CPU */
  1424. static void __init init_timer_cpu(struct tvec_base *base, int cpu)
  1425. {
  1426. int j;
  1427. BUG_ON(base != tbase_get_base(base));
  1428. base->cpu = cpu;
  1429. per_cpu(tvec_bases, cpu) = base;
  1430. spin_lock_init(&base->lock);
  1431. for (j = 0; j < TVN_SIZE; j++) {
  1432. INIT_LIST_HEAD(base->tv5.vec + j);
  1433. INIT_LIST_HEAD(base->tv4.vec + j);
  1434. INIT_LIST_HEAD(base->tv3.vec + j);
  1435. INIT_LIST_HEAD(base->tv2.vec + j);
  1436. }
  1437. for (j = 0; j < TVR_SIZE; j++)
  1438. INIT_LIST_HEAD(base->tv1.vec + j);
  1439. base->timer_jiffies = jiffies;
  1440. base->next_timer = base->timer_jiffies;
  1441. }
  1442. static void __init init_timer_cpus(void)
  1443. {
  1444. struct tvec_base *base;
  1445. int local_cpu = smp_processor_id();
  1446. int cpu;
  1447. for_each_possible_cpu(cpu) {
  1448. if (cpu == local_cpu)
  1449. base = &boot_tvec_bases;
  1450. #ifdef CONFIG_SMP
  1451. else
  1452. base = per_cpu_ptr(&__tvec_bases, cpu);
  1453. #endif
  1454. init_timer_cpu(base, cpu);
  1455. }
  1456. }
  1457. void __init init_timers(void)
  1458. {
  1459. /* ensure there are enough low bits for flags in timer->base pointer */
  1460. BUILD_BUG_ON(__alignof__(struct tvec_base) & TIMER_FLAG_MASK);
  1461. init_timer_cpus();
  1462. init_timer_stats();
  1463. timer_register_cpu_notifier();
  1464. open_softirq(TIMER_SOFTIRQ, run_timer_softirq);
  1465. }
  1466. /**
  1467. * msleep - sleep safely even with waitqueue interruptions
  1468. * @msecs: Time in milliseconds to sleep for
  1469. */
  1470. void msleep(unsigned int msecs)
  1471. {
  1472. unsigned long timeout = msecs_to_jiffies(msecs) + 1;
  1473. while (timeout)
  1474. timeout = schedule_timeout_uninterruptible(timeout);
  1475. }
  1476. EXPORT_SYMBOL(msleep);
  1477. /**
  1478. * msleep_interruptible - sleep waiting for signals
  1479. * @msecs: Time in milliseconds to sleep for
  1480. */
  1481. unsigned long msleep_interruptible(unsigned int msecs)
  1482. {
  1483. unsigned long timeout = msecs_to_jiffies(msecs) + 1;
  1484. while (timeout && !signal_pending(current))
  1485. timeout = schedule_timeout_interruptible(timeout);
  1486. return jiffies_to_msecs(timeout);
  1487. }
  1488. EXPORT_SYMBOL(msleep_interruptible);
  1489. static void __sched do_usleep_range(unsigned long min, unsigned long max)
  1490. {
  1491. ktime_t kmin;
  1492. unsigned long delta;
  1493. kmin = ktime_set(0, min * NSEC_PER_USEC);
  1494. delta = (max - min) * NSEC_PER_USEC;
  1495. schedule_hrtimeout_range(&kmin, delta, HRTIMER_MODE_REL);
  1496. }
  1497. /**
  1498. * usleep_range - Drop in replacement for udelay where wakeup is flexible
  1499. * @min: Minimum time in usecs to sleep
  1500. * @max: Maximum time in usecs to sleep
  1501. */
  1502. void __sched usleep_range(unsigned long min, unsigned long max)
  1503. {
  1504. __set_current_state(TASK_UNINTERRUPTIBLE);
  1505. do_usleep_range(min, max);
  1506. }
  1507. EXPORT_SYMBOL(usleep_range);