timer.c 46 KB

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