timer.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629
  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 - initialize a timer.
  473. * @timer: the timer to be initialized
  474. *
  475. * init_timer() must be done to a timer prior calling *any* of the
  476. * other timer functions.
  477. */
  478. void init_timer_key(struct timer_list *timer,
  479. const char *name,
  480. struct lock_class_key *key)
  481. {
  482. debug_timer_init(timer);
  483. __init_timer(timer, name, key);
  484. }
  485. EXPORT_SYMBOL(init_timer_key);
  486. void init_timer_deferrable_key(struct timer_list *timer,
  487. const char *name,
  488. struct lock_class_key *key)
  489. {
  490. init_timer_key(timer, name, key);
  491. timer_set_deferrable(timer);
  492. }
  493. EXPORT_SYMBOL(init_timer_deferrable_key);
  494. static inline void detach_timer(struct timer_list *timer,
  495. int clear_pending)
  496. {
  497. struct list_head *entry = &timer->entry;
  498. debug_timer_deactivate(timer);
  499. __list_del(entry->prev, entry->next);
  500. if (clear_pending)
  501. entry->next = NULL;
  502. entry->prev = LIST_POISON2;
  503. }
  504. /*
  505. * We are using hashed locking: holding per_cpu(tvec_bases).lock
  506. * means that all timers which are tied to this base via timer->base are
  507. * locked, and the base itself is locked too.
  508. *
  509. * So __run_timers/migrate_timers can safely modify all timers which could
  510. * be found on ->tvX lists.
  511. *
  512. * When the timer's base is locked, and the timer removed from list, it is
  513. * possible to set timer->base = NULL and drop the lock: the timer remains
  514. * locked.
  515. */
  516. static struct tvec_base *lock_timer_base(struct timer_list *timer,
  517. unsigned long *flags)
  518. __acquires(timer->base->lock)
  519. {
  520. struct tvec_base *base;
  521. for (;;) {
  522. struct tvec_base *prelock_base = timer->base;
  523. base = tbase_get_base(prelock_base);
  524. if (likely(base != NULL)) {
  525. spin_lock_irqsave(&base->lock, *flags);
  526. if (likely(prelock_base == timer->base))
  527. return base;
  528. /* The timer has migrated to another CPU */
  529. spin_unlock_irqrestore(&base->lock, *flags);
  530. }
  531. cpu_relax();
  532. }
  533. }
  534. int __mod_timer(struct timer_list *timer, unsigned long expires)
  535. {
  536. struct tvec_base *base, *new_base;
  537. unsigned long flags;
  538. int ret = 0;
  539. timer_stats_timer_set_start_info(timer);
  540. BUG_ON(!timer->function);
  541. base = lock_timer_base(timer, &flags);
  542. if (timer_pending(timer)) {
  543. detach_timer(timer, 0);
  544. ret = 1;
  545. }
  546. debug_timer_activate(timer);
  547. new_base = __get_cpu_var(tvec_bases);
  548. if (base != new_base) {
  549. /*
  550. * We are trying to schedule the timer on the local CPU.
  551. * However we can't change timer's base while it is running,
  552. * otherwise del_timer_sync() can't detect that the timer's
  553. * handler yet has not finished. This also guarantees that
  554. * the timer is serialized wrt itself.
  555. */
  556. if (likely(base->running_timer != timer)) {
  557. /* See the comment in lock_timer_base() */
  558. timer_set_base(timer, NULL);
  559. spin_unlock(&base->lock);
  560. base = new_base;
  561. spin_lock(&base->lock);
  562. timer_set_base(timer, base);
  563. }
  564. }
  565. timer->expires = expires;
  566. internal_add_timer(base, timer);
  567. spin_unlock_irqrestore(&base->lock, flags);
  568. return ret;
  569. }
  570. EXPORT_SYMBOL(__mod_timer);
  571. /**
  572. * add_timer_on - start a timer on a particular CPU
  573. * @timer: the timer to be added
  574. * @cpu: the CPU to start it on
  575. *
  576. * This is not very scalable on SMP. Double adds are not possible.
  577. */
  578. void add_timer_on(struct timer_list *timer, int cpu)
  579. {
  580. struct tvec_base *base = per_cpu(tvec_bases, cpu);
  581. unsigned long flags;
  582. timer_stats_timer_set_start_info(timer);
  583. BUG_ON(timer_pending(timer) || !timer->function);
  584. spin_lock_irqsave(&base->lock, flags);
  585. timer_set_base(timer, base);
  586. debug_timer_activate(timer);
  587. internal_add_timer(base, timer);
  588. /*
  589. * Check whether the other CPU is idle and needs to be
  590. * triggered to reevaluate the timer wheel when nohz is
  591. * active. We are protected against the other CPU fiddling
  592. * with the timer by holding the timer base lock. This also
  593. * makes sure that a CPU on the way to idle can not evaluate
  594. * the timer wheel.
  595. */
  596. wake_up_idle_cpu(cpu);
  597. spin_unlock_irqrestore(&base->lock, flags);
  598. }
  599. /**
  600. * mod_timer - modify a timer's timeout
  601. * @timer: the timer to be modified
  602. * @expires: new timeout in jiffies
  603. *
  604. * mod_timer() is a more efficient way to update the expire field of an
  605. * active timer (if the timer is inactive it will be activated)
  606. *
  607. * mod_timer(timer, expires) is equivalent to:
  608. *
  609. * del_timer(timer); timer->expires = expires; add_timer(timer);
  610. *
  611. * Note that if there are multiple unserialized concurrent users of the
  612. * same timer, then mod_timer() is the only safe way to modify the timeout,
  613. * since add_timer() cannot modify an already running timer.
  614. *
  615. * The function returns whether it has modified a pending timer or not.
  616. * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
  617. * active timer returns 1.)
  618. */
  619. int mod_timer(struct timer_list *timer, unsigned long expires)
  620. {
  621. BUG_ON(!timer->function);
  622. timer_stats_timer_set_start_info(timer);
  623. /*
  624. * This is a common optimization triggered by the
  625. * networking code - if the timer is re-modified
  626. * to be the same thing then just return:
  627. */
  628. if (timer->expires == expires && timer_pending(timer))
  629. return 1;
  630. return __mod_timer(timer, expires);
  631. }
  632. EXPORT_SYMBOL(mod_timer);
  633. /**
  634. * del_timer - deactive a timer.
  635. * @timer: the timer to be deactivated
  636. *
  637. * del_timer() deactivates a timer - this works on both active and inactive
  638. * timers.
  639. *
  640. * The function returns whether it has deactivated a pending timer or not.
  641. * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
  642. * active timer returns 1.)
  643. */
  644. int del_timer(struct timer_list *timer)
  645. {
  646. struct tvec_base *base;
  647. unsigned long flags;
  648. int ret = 0;
  649. timer_stats_timer_clear_start_info(timer);
  650. if (timer_pending(timer)) {
  651. base = lock_timer_base(timer, &flags);
  652. if (timer_pending(timer)) {
  653. detach_timer(timer, 1);
  654. ret = 1;
  655. }
  656. spin_unlock_irqrestore(&base->lock, flags);
  657. }
  658. return ret;
  659. }
  660. EXPORT_SYMBOL(del_timer);
  661. #ifdef CONFIG_SMP
  662. /**
  663. * try_to_del_timer_sync - Try to deactivate a timer
  664. * @timer: timer do del
  665. *
  666. * This function tries to deactivate a timer. Upon successful (ret >= 0)
  667. * exit the timer is not queued and the handler is not running on any CPU.
  668. *
  669. * It must not be called from interrupt contexts.
  670. */
  671. int try_to_del_timer_sync(struct timer_list *timer)
  672. {
  673. struct tvec_base *base;
  674. unsigned long flags;
  675. int ret = -1;
  676. base = lock_timer_base(timer, &flags);
  677. if (base->running_timer == timer)
  678. goto out;
  679. ret = 0;
  680. if (timer_pending(timer)) {
  681. detach_timer(timer, 1);
  682. ret = 1;
  683. }
  684. out:
  685. spin_unlock_irqrestore(&base->lock, flags);
  686. return ret;
  687. }
  688. EXPORT_SYMBOL(try_to_del_timer_sync);
  689. /**
  690. * del_timer_sync - deactivate a timer and wait for the handler to finish.
  691. * @timer: the timer to be deactivated
  692. *
  693. * This function only differs from del_timer() on SMP: besides deactivating
  694. * the timer it also makes sure the handler has finished executing on other
  695. * CPUs.
  696. *
  697. * Synchronization rules: Callers must prevent restarting of the timer,
  698. * otherwise this function is meaningless. It must not be called from
  699. * interrupt contexts. The caller must not hold locks which would prevent
  700. * completion of the timer's handler. The timer's handler must not call
  701. * add_timer_on(). Upon exit the timer is not queued and the handler is
  702. * not running on any CPU.
  703. *
  704. * The function returns whether it has deactivated a pending timer or not.
  705. */
  706. int del_timer_sync(struct timer_list *timer)
  707. {
  708. #ifdef CONFIG_LOCKDEP
  709. unsigned long flags;
  710. local_irq_save(flags);
  711. lock_map_acquire(&timer->lockdep_map);
  712. lock_map_release(&timer->lockdep_map);
  713. local_irq_restore(flags);
  714. #endif
  715. for (;;) {
  716. int ret = try_to_del_timer_sync(timer);
  717. if (ret >= 0)
  718. return ret;
  719. cpu_relax();
  720. }
  721. }
  722. EXPORT_SYMBOL(del_timer_sync);
  723. #endif
  724. static int cascade(struct tvec_base *base, struct tvec *tv, int index)
  725. {
  726. /* cascade all the timers from tv up one level */
  727. struct timer_list *timer, *tmp;
  728. struct list_head tv_list;
  729. list_replace_init(tv->vec + index, &tv_list);
  730. /*
  731. * We are removing _all_ timers from the list, so we
  732. * don't have to detach them individually.
  733. */
  734. list_for_each_entry_safe(timer, tmp, &tv_list, entry) {
  735. BUG_ON(tbase_get_base(timer->base) != base);
  736. internal_add_timer(base, timer);
  737. }
  738. return index;
  739. }
  740. #define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK)
  741. /**
  742. * __run_timers - run all expired timers (if any) on this CPU.
  743. * @base: the timer vector to be processed.
  744. *
  745. * This function cascades all vectors and executes all expired timer
  746. * vectors.
  747. */
  748. static inline void __run_timers(struct tvec_base *base)
  749. {
  750. struct timer_list *timer;
  751. spin_lock_irq(&base->lock);
  752. while (time_after_eq(jiffies, base->timer_jiffies)) {
  753. struct list_head work_list;
  754. struct list_head *head = &work_list;
  755. int index = base->timer_jiffies & TVR_MASK;
  756. /*
  757. * Cascade timers:
  758. */
  759. if (!index &&
  760. (!cascade(base, &base->tv2, INDEX(0))) &&
  761. (!cascade(base, &base->tv3, INDEX(1))) &&
  762. !cascade(base, &base->tv4, INDEX(2)))
  763. cascade(base, &base->tv5, INDEX(3));
  764. ++base->timer_jiffies;
  765. list_replace_init(base->tv1.vec + index, &work_list);
  766. while (!list_empty(head)) {
  767. void (*fn)(unsigned long);
  768. unsigned long data;
  769. timer = list_first_entry(head, struct timer_list,entry);
  770. fn = timer->function;
  771. data = timer->data;
  772. timer_stats_account_timer(timer);
  773. set_running_timer(base, timer);
  774. detach_timer(timer, 1);
  775. spin_unlock_irq(&base->lock);
  776. {
  777. int preempt_count = preempt_count();
  778. #ifdef CONFIG_LOCKDEP
  779. /*
  780. * It is permissible to free the timer from
  781. * inside the function that is called from
  782. * it, this we need to take into account for
  783. * lockdep too. To avoid bogus "held lock
  784. * freed" warnings as well as problems when
  785. * looking into timer->lockdep_map, make a
  786. * copy and use that here.
  787. */
  788. struct lockdep_map lockdep_map =
  789. timer->lockdep_map;
  790. #endif
  791. /*
  792. * Couple the lock chain with the lock chain at
  793. * del_timer_sync() by acquiring the lock_map
  794. * around the fn() call here and in
  795. * del_timer_sync().
  796. */
  797. lock_map_acquire(&lockdep_map);
  798. fn(data);
  799. lock_map_release(&lockdep_map);
  800. if (preempt_count != preempt_count()) {
  801. printk(KERN_ERR "huh, entered %p "
  802. "with preempt_count %08x, exited"
  803. " with %08x?\n",
  804. fn, preempt_count,
  805. preempt_count());
  806. BUG();
  807. }
  808. }
  809. spin_lock_irq(&base->lock);
  810. }
  811. }
  812. set_running_timer(base, NULL);
  813. spin_unlock_irq(&base->lock);
  814. }
  815. #ifdef CONFIG_NO_HZ
  816. /*
  817. * Find out when the next timer event is due to happen. This
  818. * is used on S/390 to stop all activity when a cpus is idle.
  819. * This functions needs to be called disabled.
  820. */
  821. static unsigned long __next_timer_interrupt(struct tvec_base *base)
  822. {
  823. unsigned long timer_jiffies = base->timer_jiffies;
  824. unsigned long expires = timer_jiffies + NEXT_TIMER_MAX_DELTA;
  825. int index, slot, array, found = 0;
  826. struct timer_list *nte;
  827. struct tvec *varray[4];
  828. /* Look for timer events in tv1. */
  829. index = slot = timer_jiffies & TVR_MASK;
  830. do {
  831. list_for_each_entry(nte, base->tv1.vec + slot, entry) {
  832. if (tbase_get_deferrable(nte->base))
  833. continue;
  834. found = 1;
  835. expires = nte->expires;
  836. /* Look at the cascade bucket(s)? */
  837. if (!index || slot < index)
  838. goto cascade;
  839. return expires;
  840. }
  841. slot = (slot + 1) & TVR_MASK;
  842. } while (slot != index);
  843. cascade:
  844. /* Calculate the next cascade event */
  845. if (index)
  846. timer_jiffies += TVR_SIZE - index;
  847. timer_jiffies >>= TVR_BITS;
  848. /* Check tv2-tv5. */
  849. varray[0] = &base->tv2;
  850. varray[1] = &base->tv3;
  851. varray[2] = &base->tv4;
  852. varray[3] = &base->tv5;
  853. for (array = 0; array < 4; array++) {
  854. struct tvec *varp = varray[array];
  855. index = slot = timer_jiffies & TVN_MASK;
  856. do {
  857. list_for_each_entry(nte, varp->vec + slot, entry) {
  858. found = 1;
  859. if (time_before(nte->expires, expires))
  860. expires = nte->expires;
  861. }
  862. /*
  863. * Do we still search for the first timer or are
  864. * we looking up the cascade buckets ?
  865. */
  866. if (found) {
  867. /* Look at the cascade bucket(s)? */
  868. if (!index || slot < index)
  869. break;
  870. return expires;
  871. }
  872. slot = (slot + 1) & TVN_MASK;
  873. } while (slot != index);
  874. if (index)
  875. timer_jiffies += TVN_SIZE - index;
  876. timer_jiffies >>= TVN_BITS;
  877. }
  878. return expires;
  879. }
  880. /*
  881. * Check, if the next hrtimer event is before the next timer wheel
  882. * event:
  883. */
  884. static unsigned long cmp_next_hrtimer_event(unsigned long now,
  885. unsigned long expires)
  886. {
  887. ktime_t hr_delta = hrtimer_get_next_event();
  888. struct timespec tsdelta;
  889. unsigned long delta;
  890. if (hr_delta.tv64 == KTIME_MAX)
  891. return expires;
  892. /*
  893. * Expired timer available, let it expire in the next tick
  894. */
  895. if (hr_delta.tv64 <= 0)
  896. return now + 1;
  897. tsdelta = ktime_to_timespec(hr_delta);
  898. delta = timespec_to_jiffies(&tsdelta);
  899. /*
  900. * Limit the delta to the max value, which is checked in
  901. * tick_nohz_stop_sched_tick():
  902. */
  903. if (delta > NEXT_TIMER_MAX_DELTA)
  904. delta = NEXT_TIMER_MAX_DELTA;
  905. /*
  906. * Take rounding errors in to account and make sure, that it
  907. * expires in the next tick. Otherwise we go into an endless
  908. * ping pong due to tick_nohz_stop_sched_tick() retriggering
  909. * the timer softirq
  910. */
  911. if (delta < 1)
  912. delta = 1;
  913. now += delta;
  914. if (time_before(now, expires))
  915. return now;
  916. return expires;
  917. }
  918. /**
  919. * get_next_timer_interrupt - return the jiffy of the next pending timer
  920. * @now: current time (in jiffies)
  921. */
  922. unsigned long get_next_timer_interrupt(unsigned long now)
  923. {
  924. struct tvec_base *base = __get_cpu_var(tvec_bases);
  925. unsigned long expires;
  926. spin_lock(&base->lock);
  927. expires = __next_timer_interrupt(base);
  928. spin_unlock(&base->lock);
  929. if (time_before_eq(expires, now))
  930. return now;
  931. return cmp_next_hrtimer_event(now, expires);
  932. }
  933. #endif
  934. /*
  935. * Called from the timer interrupt handler to charge one tick to the current
  936. * process. user_tick is 1 if the tick is user time, 0 for system.
  937. */
  938. void update_process_times(int user_tick)
  939. {
  940. struct task_struct *p = current;
  941. int cpu = smp_processor_id();
  942. /* Note: this timer irq context must be accounted for as well. */
  943. account_process_tick(p, user_tick);
  944. run_local_timers();
  945. if (rcu_pending(cpu))
  946. rcu_check_callbacks(cpu, user_tick);
  947. printk_tick();
  948. scheduler_tick();
  949. run_posix_cpu_timers(p);
  950. }
  951. /*
  952. * Nr of active tasks - counted in fixed-point numbers
  953. */
  954. static unsigned long count_active_tasks(void)
  955. {
  956. return nr_active() * FIXED_1;
  957. }
  958. /*
  959. * Hmm.. Changed this, as the GNU make sources (load.c) seems to
  960. * imply that avenrun[] is the standard name for this kind of thing.
  961. * Nothing else seems to be standardized: the fractional size etc
  962. * all seem to differ on different machines.
  963. *
  964. * Requires xtime_lock to access.
  965. */
  966. unsigned long avenrun[3];
  967. EXPORT_SYMBOL(avenrun);
  968. /*
  969. * calc_load - given tick count, update the avenrun load estimates.
  970. * This is called while holding a write_lock on xtime_lock.
  971. */
  972. static inline void calc_load(unsigned long ticks)
  973. {
  974. unsigned long active_tasks; /* fixed-point */
  975. static int count = LOAD_FREQ;
  976. count -= ticks;
  977. if (unlikely(count < 0)) {
  978. active_tasks = count_active_tasks();
  979. do {
  980. CALC_LOAD(avenrun[0], EXP_1, active_tasks);
  981. CALC_LOAD(avenrun[1], EXP_5, active_tasks);
  982. CALC_LOAD(avenrun[2], EXP_15, active_tasks);
  983. count += LOAD_FREQ;
  984. } while (count < 0);
  985. }
  986. }
  987. /*
  988. * This function runs timers and the timer-tq in bottom half context.
  989. */
  990. static void run_timer_softirq(struct softirq_action *h)
  991. {
  992. struct tvec_base *base = __get_cpu_var(tvec_bases);
  993. hrtimer_run_pending();
  994. if (time_after_eq(jiffies, base->timer_jiffies))
  995. __run_timers(base);
  996. }
  997. /*
  998. * Called by the local, per-CPU timer interrupt on SMP.
  999. */
  1000. void run_local_timers(void)
  1001. {
  1002. hrtimer_run_queues();
  1003. raise_softirq(TIMER_SOFTIRQ);
  1004. softlockup_tick();
  1005. }
  1006. /*
  1007. * Called by the timer interrupt. xtime_lock must already be taken
  1008. * by the timer IRQ!
  1009. */
  1010. static inline void update_times(unsigned long ticks)
  1011. {
  1012. update_wall_time();
  1013. calc_load(ticks);
  1014. }
  1015. /*
  1016. * The 64-bit jiffies value is not atomic - you MUST NOT read it
  1017. * without sampling the sequence number in xtime_lock.
  1018. * jiffies is defined in the linker script...
  1019. */
  1020. void do_timer(unsigned long ticks)
  1021. {
  1022. jiffies_64 += ticks;
  1023. update_times(ticks);
  1024. }
  1025. #ifdef __ARCH_WANT_SYS_ALARM
  1026. /*
  1027. * For backwards compatibility? This can be done in libc so Alpha
  1028. * and all newer ports shouldn't need it.
  1029. */
  1030. SYSCALL_DEFINE1(alarm, unsigned int, seconds)
  1031. {
  1032. return alarm_setitimer(seconds);
  1033. }
  1034. #endif
  1035. #ifndef __alpha__
  1036. /*
  1037. * The Alpha uses getxpid, getxuid, and getxgid instead. Maybe this
  1038. * should be moved into arch/i386 instead?
  1039. */
  1040. /**
  1041. * sys_getpid - return the thread group id of the current process
  1042. *
  1043. * Note, despite the name, this returns the tgid not the pid. The tgid and
  1044. * the pid are identical unless CLONE_THREAD was specified on clone() in
  1045. * which case the tgid is the same in all threads of the same group.
  1046. *
  1047. * This is SMP safe as current->tgid does not change.
  1048. */
  1049. SYSCALL_DEFINE0(getpid)
  1050. {
  1051. return task_tgid_vnr(current);
  1052. }
  1053. /*
  1054. * Accessing ->real_parent is not SMP-safe, it could
  1055. * change from under us. However, we can use a stale
  1056. * value of ->real_parent under rcu_read_lock(), see
  1057. * release_task()->call_rcu(delayed_put_task_struct).
  1058. */
  1059. SYSCALL_DEFINE0(getppid)
  1060. {
  1061. int pid;
  1062. rcu_read_lock();
  1063. pid = task_tgid_vnr(current->real_parent);
  1064. rcu_read_unlock();
  1065. return pid;
  1066. }
  1067. SYSCALL_DEFINE0(getuid)
  1068. {
  1069. /* Only we change this so SMP safe */
  1070. return current_uid();
  1071. }
  1072. SYSCALL_DEFINE0(geteuid)
  1073. {
  1074. /* Only we change this so SMP safe */
  1075. return current_euid();
  1076. }
  1077. SYSCALL_DEFINE0(getgid)
  1078. {
  1079. /* Only we change this so SMP safe */
  1080. return current_gid();
  1081. }
  1082. SYSCALL_DEFINE0(getegid)
  1083. {
  1084. /* Only we change this so SMP safe */
  1085. return current_egid();
  1086. }
  1087. #endif
  1088. static void process_timeout(unsigned long __data)
  1089. {
  1090. wake_up_process((struct task_struct *)__data);
  1091. }
  1092. /**
  1093. * schedule_timeout - sleep until timeout
  1094. * @timeout: timeout value in jiffies
  1095. *
  1096. * Make the current task sleep until @timeout jiffies have
  1097. * elapsed. The routine will return immediately unless
  1098. * the current task state has been set (see set_current_state()).
  1099. *
  1100. * You can set the task state as follows -
  1101. *
  1102. * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
  1103. * pass before the routine returns. The routine will return 0
  1104. *
  1105. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  1106. * delivered to the current task. In this case the remaining time
  1107. * in jiffies will be returned, or 0 if the timer expired in time
  1108. *
  1109. * The current task state is guaranteed to be TASK_RUNNING when this
  1110. * routine returns.
  1111. *
  1112. * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
  1113. * the CPU away without a bound on the timeout. In this case the return
  1114. * value will be %MAX_SCHEDULE_TIMEOUT.
  1115. *
  1116. * In all cases the return value is guaranteed to be non-negative.
  1117. */
  1118. signed long __sched schedule_timeout(signed long timeout)
  1119. {
  1120. struct timer_list timer;
  1121. unsigned long expire;
  1122. switch (timeout)
  1123. {
  1124. case MAX_SCHEDULE_TIMEOUT:
  1125. /*
  1126. * These two special cases are useful to be comfortable
  1127. * in the caller. Nothing more. We could take
  1128. * MAX_SCHEDULE_TIMEOUT from one of the negative value
  1129. * but I' d like to return a valid offset (>=0) to allow
  1130. * the caller to do everything it want with the retval.
  1131. */
  1132. schedule();
  1133. goto out;
  1134. default:
  1135. /*
  1136. * Another bit of PARANOID. Note that the retval will be
  1137. * 0 since no piece of kernel is supposed to do a check
  1138. * for a negative retval of schedule_timeout() (since it
  1139. * should never happens anyway). You just have the printk()
  1140. * that will tell you if something is gone wrong and where.
  1141. */
  1142. if (timeout < 0) {
  1143. printk(KERN_ERR "schedule_timeout: wrong timeout "
  1144. "value %lx\n", timeout);
  1145. dump_stack();
  1146. current->state = TASK_RUNNING;
  1147. goto out;
  1148. }
  1149. }
  1150. expire = timeout + jiffies;
  1151. setup_timer_on_stack(&timer, process_timeout, (unsigned long)current);
  1152. __mod_timer(&timer, expire);
  1153. schedule();
  1154. del_singleshot_timer_sync(&timer);
  1155. /* Remove the timer from the object tracker */
  1156. destroy_timer_on_stack(&timer);
  1157. timeout = expire - jiffies;
  1158. out:
  1159. return timeout < 0 ? 0 : timeout;
  1160. }
  1161. EXPORT_SYMBOL(schedule_timeout);
  1162. /*
  1163. * We can use __set_current_state() here because schedule_timeout() calls
  1164. * schedule() unconditionally.
  1165. */
  1166. signed long __sched schedule_timeout_interruptible(signed long timeout)
  1167. {
  1168. __set_current_state(TASK_INTERRUPTIBLE);
  1169. return schedule_timeout(timeout);
  1170. }
  1171. EXPORT_SYMBOL(schedule_timeout_interruptible);
  1172. signed long __sched schedule_timeout_killable(signed long timeout)
  1173. {
  1174. __set_current_state(TASK_KILLABLE);
  1175. return schedule_timeout(timeout);
  1176. }
  1177. EXPORT_SYMBOL(schedule_timeout_killable);
  1178. signed long __sched schedule_timeout_uninterruptible(signed long timeout)
  1179. {
  1180. __set_current_state(TASK_UNINTERRUPTIBLE);
  1181. return schedule_timeout(timeout);
  1182. }
  1183. EXPORT_SYMBOL(schedule_timeout_uninterruptible);
  1184. /* Thread ID - the internal kernel "pid" */
  1185. SYSCALL_DEFINE0(gettid)
  1186. {
  1187. return task_pid_vnr(current);
  1188. }
  1189. /**
  1190. * do_sysinfo - fill in sysinfo struct
  1191. * @info: pointer to buffer to fill
  1192. */
  1193. int do_sysinfo(struct sysinfo *info)
  1194. {
  1195. unsigned long mem_total, sav_total;
  1196. unsigned int mem_unit, bitcount;
  1197. unsigned long seq;
  1198. memset(info, 0, sizeof(struct sysinfo));
  1199. do {
  1200. struct timespec tp;
  1201. seq = read_seqbegin(&xtime_lock);
  1202. /*
  1203. * This is annoying. The below is the same thing
  1204. * posix_get_clock_monotonic() does, but it wants to
  1205. * take the lock which we want to cover the loads stuff
  1206. * too.
  1207. */
  1208. getnstimeofday(&tp);
  1209. tp.tv_sec += wall_to_monotonic.tv_sec;
  1210. tp.tv_nsec += wall_to_monotonic.tv_nsec;
  1211. monotonic_to_bootbased(&tp);
  1212. if (tp.tv_nsec - NSEC_PER_SEC >= 0) {
  1213. tp.tv_nsec = tp.tv_nsec - NSEC_PER_SEC;
  1214. tp.tv_sec++;
  1215. }
  1216. info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
  1217. info->loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT);
  1218. info->loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT);
  1219. info->loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT);
  1220. info->procs = nr_threads;
  1221. } while (read_seqretry(&xtime_lock, seq));
  1222. si_meminfo(info);
  1223. si_swapinfo(info);
  1224. /*
  1225. * If the sum of all the available memory (i.e. ram + swap)
  1226. * is less than can be stored in a 32 bit unsigned long then
  1227. * we can be binary compatible with 2.2.x kernels. If not,
  1228. * well, in that case 2.2.x was broken anyways...
  1229. *
  1230. * -Erik Andersen <andersee@debian.org>
  1231. */
  1232. mem_total = info->totalram + info->totalswap;
  1233. if (mem_total < info->totalram || mem_total < info->totalswap)
  1234. goto out;
  1235. bitcount = 0;
  1236. mem_unit = info->mem_unit;
  1237. while (mem_unit > 1) {
  1238. bitcount++;
  1239. mem_unit >>= 1;
  1240. sav_total = mem_total;
  1241. mem_total <<= 1;
  1242. if (mem_total < sav_total)
  1243. goto out;
  1244. }
  1245. /*
  1246. * If mem_total did not overflow, multiply all memory values by
  1247. * info->mem_unit and set it to 1. This leaves things compatible
  1248. * with 2.2.x, and also retains compatibility with earlier 2.4.x
  1249. * kernels...
  1250. */
  1251. info->mem_unit = 1;
  1252. info->totalram <<= bitcount;
  1253. info->freeram <<= bitcount;
  1254. info->sharedram <<= bitcount;
  1255. info->bufferram <<= bitcount;
  1256. info->totalswap <<= bitcount;
  1257. info->freeswap <<= bitcount;
  1258. info->totalhigh <<= bitcount;
  1259. info->freehigh <<= bitcount;
  1260. out:
  1261. return 0;
  1262. }
  1263. SYSCALL_DEFINE1(sysinfo, struct sysinfo __user *, info)
  1264. {
  1265. struct sysinfo val;
  1266. do_sysinfo(&val);
  1267. if (copy_to_user(info, &val, sizeof(struct sysinfo)))
  1268. return -EFAULT;
  1269. return 0;
  1270. }
  1271. static int __cpuinit init_timers_cpu(int cpu)
  1272. {
  1273. int j;
  1274. struct tvec_base *base;
  1275. static char __cpuinitdata tvec_base_done[NR_CPUS];
  1276. if (!tvec_base_done[cpu]) {
  1277. static char boot_done;
  1278. if (boot_done) {
  1279. /*
  1280. * The APs use this path later in boot
  1281. */
  1282. base = kmalloc_node(sizeof(*base),
  1283. GFP_KERNEL | __GFP_ZERO,
  1284. cpu_to_node(cpu));
  1285. if (!base)
  1286. return -ENOMEM;
  1287. /* Make sure that tvec_base is 2 byte aligned */
  1288. if (tbase_get_deferrable(base)) {
  1289. WARN_ON(1);
  1290. kfree(base);
  1291. return -ENOMEM;
  1292. }
  1293. per_cpu(tvec_bases, cpu) = base;
  1294. } else {
  1295. /*
  1296. * This is for the boot CPU - we use compile-time
  1297. * static initialisation because per-cpu memory isn't
  1298. * ready yet and because the memory allocators are not
  1299. * initialised either.
  1300. */
  1301. boot_done = 1;
  1302. base = &boot_tvec_bases;
  1303. }
  1304. tvec_base_done[cpu] = 1;
  1305. } else {
  1306. base = per_cpu(tvec_bases, cpu);
  1307. }
  1308. spin_lock_init(&base->lock);
  1309. for (j = 0; j < TVN_SIZE; j++) {
  1310. INIT_LIST_HEAD(base->tv5.vec + j);
  1311. INIT_LIST_HEAD(base->tv4.vec + j);
  1312. INIT_LIST_HEAD(base->tv3.vec + j);
  1313. INIT_LIST_HEAD(base->tv2.vec + j);
  1314. }
  1315. for (j = 0; j < TVR_SIZE; j++)
  1316. INIT_LIST_HEAD(base->tv1.vec + j);
  1317. base->timer_jiffies = jiffies;
  1318. return 0;
  1319. }
  1320. #ifdef CONFIG_HOTPLUG_CPU
  1321. static void migrate_timer_list(struct tvec_base *new_base, struct list_head *head)
  1322. {
  1323. struct timer_list *timer;
  1324. while (!list_empty(head)) {
  1325. timer = list_first_entry(head, struct timer_list, entry);
  1326. detach_timer(timer, 0);
  1327. timer_set_base(timer, new_base);
  1328. internal_add_timer(new_base, timer);
  1329. }
  1330. }
  1331. static void __cpuinit migrate_timers(int cpu)
  1332. {
  1333. struct tvec_base *old_base;
  1334. struct tvec_base *new_base;
  1335. int i;
  1336. BUG_ON(cpu_online(cpu));
  1337. old_base = per_cpu(tvec_bases, cpu);
  1338. new_base = get_cpu_var(tvec_bases);
  1339. /*
  1340. * The caller is globally serialized and nobody else
  1341. * takes two locks at once, deadlock is not possible.
  1342. */
  1343. spin_lock_irq(&new_base->lock);
  1344. spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
  1345. BUG_ON(old_base->running_timer);
  1346. for (i = 0; i < TVR_SIZE; i++)
  1347. migrate_timer_list(new_base, old_base->tv1.vec + i);
  1348. for (i = 0; i < TVN_SIZE; i++) {
  1349. migrate_timer_list(new_base, old_base->tv2.vec + i);
  1350. migrate_timer_list(new_base, old_base->tv3.vec + i);
  1351. migrate_timer_list(new_base, old_base->tv4.vec + i);
  1352. migrate_timer_list(new_base, old_base->tv5.vec + i);
  1353. }
  1354. spin_unlock(&old_base->lock);
  1355. spin_unlock_irq(&new_base->lock);
  1356. put_cpu_var(tvec_bases);
  1357. }
  1358. #endif /* CONFIG_HOTPLUG_CPU */
  1359. static int __cpuinit timer_cpu_notify(struct notifier_block *self,
  1360. unsigned long action, void *hcpu)
  1361. {
  1362. long cpu = (long)hcpu;
  1363. switch(action) {
  1364. case CPU_UP_PREPARE:
  1365. case CPU_UP_PREPARE_FROZEN:
  1366. if (init_timers_cpu(cpu) < 0)
  1367. return NOTIFY_BAD;
  1368. break;
  1369. #ifdef CONFIG_HOTPLUG_CPU
  1370. case CPU_DEAD:
  1371. case CPU_DEAD_FROZEN:
  1372. migrate_timers(cpu);
  1373. break;
  1374. #endif
  1375. default:
  1376. break;
  1377. }
  1378. return NOTIFY_OK;
  1379. }
  1380. static struct notifier_block __cpuinitdata timers_nb = {
  1381. .notifier_call = timer_cpu_notify,
  1382. };
  1383. void __init init_timers(void)
  1384. {
  1385. int err = timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
  1386. (void *)(long)smp_processor_id());
  1387. init_timer_stats();
  1388. BUG_ON(err == NOTIFY_BAD);
  1389. register_cpu_notifier(&timers_nb);
  1390. open_softirq(TIMER_SOFTIRQ, run_timer_softirq);
  1391. }
  1392. /**
  1393. * msleep - sleep safely even with waitqueue interruptions
  1394. * @msecs: Time in milliseconds to sleep for
  1395. */
  1396. void msleep(unsigned int msecs)
  1397. {
  1398. unsigned long timeout = msecs_to_jiffies(msecs) + 1;
  1399. while (timeout)
  1400. timeout = schedule_timeout_uninterruptible(timeout);
  1401. }
  1402. EXPORT_SYMBOL(msleep);
  1403. /**
  1404. * msleep_interruptible - sleep waiting for signals
  1405. * @msecs: Time in milliseconds to sleep for
  1406. */
  1407. unsigned long msleep_interruptible(unsigned int msecs)
  1408. {
  1409. unsigned long timeout = msecs_to_jiffies(msecs) + 1;
  1410. while (timeout && !signal_pending(current))
  1411. timeout = schedule_timeout_interruptible(timeout);
  1412. return jiffies_to_msecs(timeout);
  1413. }
  1414. EXPORT_SYMBOL(msleep_interruptible);