timer.c 37 KB

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