timer.c 46 KB

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