timer.c 46 KB

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