timer.c 36 KB

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