timer.c 43 KB

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