timer.c 47 KB

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