timer.c 43 KB

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