timer.c 39 KB

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