timer.c 43 KB

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