timer.c 47 KB

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