timer.c 47 KB

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