timer.c 49 KB

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