timer.c 44 KB

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