timer.c 47 KB

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