timer.c 49 KB

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