timer.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  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. * get_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. #ifndef CONFIG_VIRT_CPU_ACCOUNTING
  721. void account_process_tick(struct task_struct *p, int user_tick)
  722. {
  723. if (user_tick) {
  724. account_user_time(p, jiffies_to_cputime(1));
  725. account_user_time_scaled(p, jiffies_to_cputime(1));
  726. } else {
  727. account_system_time(p, HARDIRQ_OFFSET, jiffies_to_cputime(1));
  728. account_system_time_scaled(p, jiffies_to_cputime(1));
  729. }
  730. }
  731. #endif
  732. /*
  733. * Called from the timer interrupt handler to charge one tick to the current
  734. * process. user_tick is 1 if the tick is user time, 0 for system.
  735. */
  736. void update_process_times(int user_tick)
  737. {
  738. struct task_struct *p = current;
  739. int cpu = smp_processor_id();
  740. /* Note: this timer irq context must be accounted for as well. */
  741. account_process_tick(p, user_tick);
  742. run_local_timers();
  743. if (rcu_pending(cpu))
  744. rcu_check_callbacks(cpu, user_tick);
  745. scheduler_tick();
  746. run_posix_cpu_timers(p);
  747. }
  748. /*
  749. * Nr of active tasks - counted in fixed-point numbers
  750. */
  751. static unsigned long count_active_tasks(void)
  752. {
  753. return nr_active() * FIXED_1;
  754. }
  755. /*
  756. * Hmm.. Changed this, as the GNU make sources (load.c) seems to
  757. * imply that avenrun[] is the standard name for this kind of thing.
  758. * Nothing else seems to be standardized: the fractional size etc
  759. * all seem to differ on different machines.
  760. *
  761. * Requires xtime_lock to access.
  762. */
  763. unsigned long avenrun[3];
  764. EXPORT_SYMBOL(avenrun);
  765. /*
  766. * calc_load - given tick count, update the avenrun load estimates.
  767. * This is called while holding a write_lock on xtime_lock.
  768. */
  769. static inline void calc_load(unsigned long ticks)
  770. {
  771. unsigned long active_tasks; /* fixed-point */
  772. static int count = LOAD_FREQ;
  773. count -= ticks;
  774. if (unlikely(count < 0)) {
  775. active_tasks = count_active_tasks();
  776. do {
  777. CALC_LOAD(avenrun[0], EXP_1, active_tasks);
  778. CALC_LOAD(avenrun[1], EXP_5, active_tasks);
  779. CALC_LOAD(avenrun[2], EXP_15, active_tasks);
  780. count += LOAD_FREQ;
  781. } while (count < 0);
  782. }
  783. }
  784. /*
  785. * This function runs timers and the timer-tq in bottom half context.
  786. */
  787. static void run_timer_softirq(struct softirq_action *h)
  788. {
  789. tvec_base_t *base = __get_cpu_var(tvec_bases);
  790. hrtimer_run_queues();
  791. if (time_after_eq(jiffies, base->timer_jiffies))
  792. __run_timers(base);
  793. }
  794. /*
  795. * Called by the local, per-CPU timer interrupt on SMP.
  796. */
  797. void run_local_timers(void)
  798. {
  799. raise_softirq(TIMER_SOFTIRQ);
  800. softlockup_tick();
  801. }
  802. /*
  803. * Called by the timer interrupt. xtime_lock must already be taken
  804. * by the timer IRQ!
  805. */
  806. static inline void update_times(unsigned long ticks)
  807. {
  808. update_wall_time();
  809. calc_load(ticks);
  810. }
  811. /*
  812. * The 64-bit jiffies value is not atomic - you MUST NOT read it
  813. * without sampling the sequence number in xtime_lock.
  814. * jiffies is defined in the linker script...
  815. */
  816. void do_timer(unsigned long ticks)
  817. {
  818. jiffies_64 += ticks;
  819. update_times(ticks);
  820. }
  821. #ifdef __ARCH_WANT_SYS_ALARM
  822. /*
  823. * For backwards compatibility? This can be done in libc so Alpha
  824. * and all newer ports shouldn't need it.
  825. */
  826. asmlinkage unsigned long sys_alarm(unsigned int seconds)
  827. {
  828. return alarm_setitimer(seconds);
  829. }
  830. #endif
  831. #ifndef __alpha__
  832. /*
  833. * The Alpha uses getxpid, getxuid, and getxgid instead. Maybe this
  834. * should be moved into arch/i386 instead?
  835. */
  836. /**
  837. * sys_getpid - return the thread group id of the current process
  838. *
  839. * Note, despite the name, this returns the tgid not the pid. The tgid and
  840. * the pid are identical unless CLONE_THREAD was specified on clone() in
  841. * which case the tgid is the same in all threads of the same group.
  842. *
  843. * This is SMP safe as current->tgid does not change.
  844. */
  845. asmlinkage long sys_getpid(void)
  846. {
  847. return task_tgid_vnr(current);
  848. }
  849. /*
  850. * Accessing ->real_parent is not SMP-safe, it could
  851. * change from under us. However, we can use a stale
  852. * value of ->real_parent under rcu_read_lock(), see
  853. * release_task()->call_rcu(delayed_put_task_struct).
  854. */
  855. asmlinkage long sys_getppid(void)
  856. {
  857. int pid;
  858. rcu_read_lock();
  859. pid = task_ppid_nr_ns(current, current->nsproxy->pid_ns);
  860. rcu_read_unlock();
  861. return pid;
  862. }
  863. asmlinkage long sys_getuid(void)
  864. {
  865. /* Only we change this so SMP safe */
  866. return current->uid;
  867. }
  868. asmlinkage long sys_geteuid(void)
  869. {
  870. /* Only we change this so SMP safe */
  871. return current->euid;
  872. }
  873. asmlinkage long sys_getgid(void)
  874. {
  875. /* Only we change this so SMP safe */
  876. return current->gid;
  877. }
  878. asmlinkage long sys_getegid(void)
  879. {
  880. /* Only we change this so SMP safe */
  881. return current->egid;
  882. }
  883. #endif
  884. static void process_timeout(unsigned long __data)
  885. {
  886. wake_up_process((struct task_struct *)__data);
  887. }
  888. /**
  889. * schedule_timeout - sleep until timeout
  890. * @timeout: timeout value in jiffies
  891. *
  892. * Make the current task sleep until @timeout jiffies have
  893. * elapsed. The routine will return immediately unless
  894. * the current task state has been set (see set_current_state()).
  895. *
  896. * You can set the task state as follows -
  897. *
  898. * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
  899. * pass before the routine returns. The routine will return 0
  900. *
  901. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  902. * delivered to the current task. In this case the remaining time
  903. * in jiffies will be returned, or 0 if the timer expired in time
  904. *
  905. * The current task state is guaranteed to be TASK_RUNNING when this
  906. * routine returns.
  907. *
  908. * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
  909. * the CPU away without a bound on the timeout. In this case the return
  910. * value will be %MAX_SCHEDULE_TIMEOUT.
  911. *
  912. * In all cases the return value is guaranteed to be non-negative.
  913. */
  914. fastcall signed long __sched schedule_timeout(signed long timeout)
  915. {
  916. struct timer_list timer;
  917. unsigned long expire;
  918. switch (timeout)
  919. {
  920. case MAX_SCHEDULE_TIMEOUT:
  921. /*
  922. * These two special cases are useful to be comfortable
  923. * in the caller. Nothing more. We could take
  924. * MAX_SCHEDULE_TIMEOUT from one of the negative value
  925. * but I' d like to return a valid offset (>=0) to allow
  926. * the caller to do everything it want with the retval.
  927. */
  928. schedule();
  929. goto out;
  930. default:
  931. /*
  932. * Another bit of PARANOID. Note that the retval will be
  933. * 0 since no piece of kernel is supposed to do a check
  934. * for a negative retval of schedule_timeout() (since it
  935. * should never happens anyway). You just have the printk()
  936. * that will tell you if something is gone wrong and where.
  937. */
  938. if (timeout < 0) {
  939. printk(KERN_ERR "schedule_timeout: wrong timeout "
  940. "value %lx\n", timeout);
  941. dump_stack();
  942. current->state = TASK_RUNNING;
  943. goto out;
  944. }
  945. }
  946. expire = timeout + jiffies;
  947. setup_timer(&timer, process_timeout, (unsigned long)current);
  948. __mod_timer(&timer, expire);
  949. schedule();
  950. del_singleshot_timer_sync(&timer);
  951. timeout = expire - jiffies;
  952. out:
  953. return timeout < 0 ? 0 : timeout;
  954. }
  955. EXPORT_SYMBOL(schedule_timeout);
  956. /*
  957. * We can use __set_current_state() here because schedule_timeout() calls
  958. * schedule() unconditionally.
  959. */
  960. signed long __sched schedule_timeout_interruptible(signed long timeout)
  961. {
  962. __set_current_state(TASK_INTERRUPTIBLE);
  963. return schedule_timeout(timeout);
  964. }
  965. EXPORT_SYMBOL(schedule_timeout_interruptible);
  966. signed long __sched schedule_timeout_uninterruptible(signed long timeout)
  967. {
  968. __set_current_state(TASK_UNINTERRUPTIBLE);
  969. return schedule_timeout(timeout);
  970. }
  971. EXPORT_SYMBOL(schedule_timeout_uninterruptible);
  972. /* Thread ID - the internal kernel "pid" */
  973. asmlinkage long sys_gettid(void)
  974. {
  975. return task_pid_vnr(current);
  976. }
  977. /**
  978. * do_sysinfo - fill in sysinfo struct
  979. * @info: pointer to buffer to fill
  980. */
  981. int do_sysinfo(struct sysinfo *info)
  982. {
  983. unsigned long mem_total, sav_total;
  984. unsigned int mem_unit, bitcount;
  985. unsigned long seq;
  986. memset(info, 0, sizeof(struct sysinfo));
  987. do {
  988. struct timespec tp;
  989. seq = read_seqbegin(&xtime_lock);
  990. /*
  991. * This is annoying. The below is the same thing
  992. * posix_get_clock_monotonic() does, but it wants to
  993. * take the lock which we want to cover the loads stuff
  994. * too.
  995. */
  996. getnstimeofday(&tp);
  997. tp.tv_sec += wall_to_monotonic.tv_sec;
  998. tp.tv_nsec += wall_to_monotonic.tv_nsec;
  999. monotonic_to_bootbased(&tp);
  1000. if (tp.tv_nsec - NSEC_PER_SEC >= 0) {
  1001. tp.tv_nsec = tp.tv_nsec - NSEC_PER_SEC;
  1002. tp.tv_sec++;
  1003. }
  1004. info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
  1005. info->loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT);
  1006. info->loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT);
  1007. info->loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT);
  1008. info->procs = nr_threads;
  1009. } while (read_seqretry(&xtime_lock, seq));
  1010. si_meminfo(info);
  1011. si_swapinfo(info);
  1012. /*
  1013. * If the sum of all the available memory (i.e. ram + swap)
  1014. * is less than can be stored in a 32 bit unsigned long then
  1015. * we can be binary compatible with 2.2.x kernels. If not,
  1016. * well, in that case 2.2.x was broken anyways...
  1017. *
  1018. * -Erik Andersen <andersee@debian.org>
  1019. */
  1020. mem_total = info->totalram + info->totalswap;
  1021. if (mem_total < info->totalram || mem_total < info->totalswap)
  1022. goto out;
  1023. bitcount = 0;
  1024. mem_unit = info->mem_unit;
  1025. while (mem_unit > 1) {
  1026. bitcount++;
  1027. mem_unit >>= 1;
  1028. sav_total = mem_total;
  1029. mem_total <<= 1;
  1030. if (mem_total < sav_total)
  1031. goto out;
  1032. }
  1033. /*
  1034. * If mem_total did not overflow, multiply all memory values by
  1035. * info->mem_unit and set it to 1. This leaves things compatible
  1036. * with 2.2.x, and also retains compatibility with earlier 2.4.x
  1037. * kernels...
  1038. */
  1039. info->mem_unit = 1;
  1040. info->totalram <<= bitcount;
  1041. info->freeram <<= bitcount;
  1042. info->sharedram <<= bitcount;
  1043. info->bufferram <<= bitcount;
  1044. info->totalswap <<= bitcount;
  1045. info->freeswap <<= bitcount;
  1046. info->totalhigh <<= bitcount;
  1047. info->freehigh <<= bitcount;
  1048. out:
  1049. return 0;
  1050. }
  1051. asmlinkage long sys_sysinfo(struct sysinfo __user *info)
  1052. {
  1053. struct sysinfo val;
  1054. do_sysinfo(&val);
  1055. if (copy_to_user(info, &val, sizeof(struct sysinfo)))
  1056. return -EFAULT;
  1057. return 0;
  1058. }
  1059. /*
  1060. * lockdep: we want to track each per-CPU base as a separate lock-class,
  1061. * but timer-bases are kmalloc()-ed, so we need to attach separate
  1062. * keys to them:
  1063. */
  1064. static struct lock_class_key base_lock_keys[NR_CPUS];
  1065. static int __devinit init_timers_cpu(int cpu)
  1066. {
  1067. int j;
  1068. tvec_base_t *base;
  1069. static char __devinitdata tvec_base_done[NR_CPUS];
  1070. if (!tvec_base_done[cpu]) {
  1071. static char boot_done;
  1072. if (boot_done) {
  1073. /*
  1074. * The APs use this path later in boot
  1075. */
  1076. base = kmalloc_node(sizeof(*base),
  1077. GFP_KERNEL | __GFP_ZERO,
  1078. cpu_to_node(cpu));
  1079. if (!base)
  1080. return -ENOMEM;
  1081. /* Make sure that tvec_base is 2 byte aligned */
  1082. if (tbase_get_deferrable(base)) {
  1083. WARN_ON(1);
  1084. kfree(base);
  1085. return -ENOMEM;
  1086. }
  1087. per_cpu(tvec_bases, cpu) = base;
  1088. } else {
  1089. /*
  1090. * This is for the boot CPU - we use compile-time
  1091. * static initialisation because per-cpu memory isn't
  1092. * ready yet and because the memory allocators are not
  1093. * initialised either.
  1094. */
  1095. boot_done = 1;
  1096. base = &boot_tvec_bases;
  1097. }
  1098. tvec_base_done[cpu] = 1;
  1099. } else {
  1100. base = per_cpu(tvec_bases, cpu);
  1101. }
  1102. spin_lock_init(&base->lock);
  1103. lockdep_set_class(&base->lock, base_lock_keys + cpu);
  1104. for (j = 0; j < TVN_SIZE; j++) {
  1105. INIT_LIST_HEAD(base->tv5.vec + j);
  1106. INIT_LIST_HEAD(base->tv4.vec + j);
  1107. INIT_LIST_HEAD(base->tv3.vec + j);
  1108. INIT_LIST_HEAD(base->tv2.vec + j);
  1109. }
  1110. for (j = 0; j < TVR_SIZE; j++)
  1111. INIT_LIST_HEAD(base->tv1.vec + j);
  1112. base->timer_jiffies = jiffies;
  1113. return 0;
  1114. }
  1115. #ifdef CONFIG_HOTPLUG_CPU
  1116. static void migrate_timer_list(tvec_base_t *new_base, struct list_head *head)
  1117. {
  1118. struct timer_list *timer;
  1119. while (!list_empty(head)) {
  1120. timer = list_first_entry(head, struct timer_list, entry);
  1121. detach_timer(timer, 0);
  1122. timer_set_base(timer, new_base);
  1123. internal_add_timer(new_base, timer);
  1124. }
  1125. }
  1126. static void __devinit migrate_timers(int cpu)
  1127. {
  1128. tvec_base_t *old_base;
  1129. tvec_base_t *new_base;
  1130. int i;
  1131. BUG_ON(cpu_online(cpu));
  1132. old_base = per_cpu(tvec_bases, cpu);
  1133. new_base = get_cpu_var(tvec_bases);
  1134. local_irq_disable();
  1135. double_spin_lock(&new_base->lock, &old_base->lock,
  1136. smp_processor_id() < cpu);
  1137. BUG_ON(old_base->running_timer);
  1138. for (i = 0; i < TVR_SIZE; i++)
  1139. migrate_timer_list(new_base, old_base->tv1.vec + i);
  1140. for (i = 0; i < TVN_SIZE; i++) {
  1141. migrate_timer_list(new_base, old_base->tv2.vec + i);
  1142. migrate_timer_list(new_base, old_base->tv3.vec + i);
  1143. migrate_timer_list(new_base, old_base->tv4.vec + i);
  1144. migrate_timer_list(new_base, old_base->tv5.vec + i);
  1145. }
  1146. double_spin_unlock(&new_base->lock, &old_base->lock,
  1147. smp_processor_id() < cpu);
  1148. local_irq_enable();
  1149. put_cpu_var(tvec_bases);
  1150. }
  1151. #endif /* CONFIG_HOTPLUG_CPU */
  1152. static int __cpuinit timer_cpu_notify(struct notifier_block *self,
  1153. unsigned long action, void *hcpu)
  1154. {
  1155. long cpu = (long)hcpu;
  1156. switch(action) {
  1157. case CPU_UP_PREPARE:
  1158. case CPU_UP_PREPARE_FROZEN:
  1159. if (init_timers_cpu(cpu) < 0)
  1160. return NOTIFY_BAD;
  1161. break;
  1162. #ifdef CONFIG_HOTPLUG_CPU
  1163. case CPU_DEAD:
  1164. case CPU_DEAD_FROZEN:
  1165. migrate_timers(cpu);
  1166. break;
  1167. #endif
  1168. default:
  1169. break;
  1170. }
  1171. return NOTIFY_OK;
  1172. }
  1173. static struct notifier_block __cpuinitdata timers_nb = {
  1174. .notifier_call = timer_cpu_notify,
  1175. };
  1176. void __init init_timers(void)
  1177. {
  1178. int err = timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
  1179. (void *)(long)smp_processor_id());
  1180. init_timer_stats();
  1181. BUG_ON(err == NOTIFY_BAD);
  1182. register_cpu_notifier(&timers_nb);
  1183. open_softirq(TIMER_SOFTIRQ, run_timer_softirq, NULL);
  1184. }
  1185. /**
  1186. * msleep - sleep safely even with waitqueue interruptions
  1187. * @msecs: Time in milliseconds to sleep for
  1188. */
  1189. void msleep(unsigned int msecs)
  1190. {
  1191. unsigned long timeout = msecs_to_jiffies(msecs) + 1;
  1192. while (timeout)
  1193. timeout = schedule_timeout_uninterruptible(timeout);
  1194. }
  1195. EXPORT_SYMBOL(msleep);
  1196. /**
  1197. * msleep_interruptible - sleep waiting for signals
  1198. * @msecs: Time in milliseconds to sleep for
  1199. */
  1200. unsigned long msleep_interruptible(unsigned int msecs)
  1201. {
  1202. unsigned long timeout = msecs_to_jiffies(msecs) + 1;
  1203. while (timeout && !signal_pending(current))
  1204. timeout = schedule_timeout_interruptible(timeout);
  1205. return jiffies_to_msecs(timeout);
  1206. }
  1207. EXPORT_SYMBOL(msleep_interruptible);