timer.c 36 KB

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