timer.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382
  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/notifier.h>
  29. #include <linux/thread_info.h>
  30. #include <linux/time.h>
  31. #include <linux/jiffies.h>
  32. #include <linux/posix-timers.h>
  33. #include <linux/cpu.h>
  34. #include <linux/syscalls.h>
  35. #include <linux/delay.h>
  36. #include <linux/tick.h>
  37. #include <linux/kallsyms.h>
  38. #include <asm/uaccess.h>
  39. #include <asm/unistd.h>
  40. #include <asm/div64.h>
  41. #include <asm/timex.h>
  42. #include <asm/io.h>
  43. u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
  44. EXPORT_SYMBOL(jiffies_64);
  45. /*
  46. * per-CPU timer vector definitions:
  47. */
  48. #define TVN_BITS (CONFIG_BASE_SMALL ? 4 : 6)
  49. #define TVR_BITS (CONFIG_BASE_SMALL ? 6 : 8)
  50. #define TVN_SIZE (1 << TVN_BITS)
  51. #define TVR_SIZE (1 << TVR_BITS)
  52. #define TVN_MASK (TVN_SIZE - 1)
  53. #define TVR_MASK (TVR_SIZE - 1)
  54. typedef struct tvec_s {
  55. struct list_head vec[TVN_SIZE];
  56. } tvec_t;
  57. typedef struct tvec_root_s {
  58. struct list_head vec[TVR_SIZE];
  59. } tvec_root_t;
  60. struct tvec_t_base_s {
  61. spinlock_t lock;
  62. struct timer_list *running_timer;
  63. unsigned long timer_jiffies;
  64. tvec_root_t tv1;
  65. tvec_t tv2;
  66. tvec_t tv3;
  67. tvec_t tv4;
  68. tvec_t tv5;
  69. } ____cacheline_aligned;
  70. typedef struct tvec_t_base_s tvec_base_t;
  71. tvec_base_t boot_tvec_bases;
  72. EXPORT_SYMBOL(boot_tvec_bases);
  73. static DEFINE_PER_CPU(tvec_base_t *, tvec_bases) = &boot_tvec_bases;
  74. /*
  75. * Note that all tvec_bases is 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(tvec_base_t *base)
  82. {
  83. return ((unsigned int)(unsigned long)base & TBASE_DEFERRABLE_FLAG);
  84. }
  85. static inline tvec_base_t *tbase_get_base(tvec_base_t *base)
  86. {
  87. return ((tvec_base_t *)((unsigned long)base & ~TBASE_DEFERRABLE_FLAG));
  88. }
  89. static inline void timer_set_deferrable(struct timer_list *timer)
  90. {
  91. timer->base = ((tvec_base_t *)((unsigned long)(timer->base) |
  92. TBASE_DEFERRABLE_FLAG));
  93. }
  94. static inline void
  95. timer_set_base(struct timer_list *timer, tvec_base_t *new_base)
  96. {
  97. timer->base = (tvec_base_t *)((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(tvec_base_t *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(tvec_base_t *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 fastcall 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 fastcall 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 tvec_base_t *lock_timer_base(struct timer_list *timer,
  335. unsigned long *flags)
  336. __acquires(timer->base->lock)
  337. {
  338. tvec_base_t *base;
  339. for (;;) {
  340. tvec_base_t *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. tvec_base_t *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. tvec_base_t *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. spin_unlock_irqrestore(&base->lock, flags);
  405. }
  406. /**
  407. * mod_timer - modify a timer's timeout
  408. * @timer: the timer to be modified
  409. * @expires: new timeout in jiffies
  410. *
  411. * mod_timer() is a more efficient way to update the expire field of an
  412. * active timer (if the timer is inactive it will be activated)
  413. *
  414. * mod_timer(timer, expires) is equivalent to:
  415. *
  416. * del_timer(timer); timer->expires = expires; add_timer(timer);
  417. *
  418. * Note that if there are multiple unserialized concurrent users of the
  419. * same timer, then mod_timer() is the only safe way to modify the timeout,
  420. * since add_timer() cannot modify an already running timer.
  421. *
  422. * The function returns whether it has modified a pending timer or not.
  423. * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
  424. * active timer returns 1.)
  425. */
  426. int mod_timer(struct timer_list *timer, unsigned long expires)
  427. {
  428. BUG_ON(!timer->function);
  429. timer_stats_timer_set_start_info(timer);
  430. /*
  431. * This is a common optimization triggered by the
  432. * networking code - if the timer is re-modified
  433. * to be the same thing then just return:
  434. */
  435. if (timer->expires == expires && timer_pending(timer))
  436. return 1;
  437. return __mod_timer(timer, expires);
  438. }
  439. EXPORT_SYMBOL(mod_timer);
  440. /**
  441. * del_timer - deactive a timer.
  442. * @timer: the timer to be deactivated
  443. *
  444. * del_timer() deactivates a timer - this works on both active and inactive
  445. * timers.
  446. *
  447. * The function returns whether it has deactivated a pending timer or not.
  448. * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
  449. * active timer returns 1.)
  450. */
  451. int del_timer(struct timer_list *timer)
  452. {
  453. tvec_base_t *base;
  454. unsigned long flags;
  455. int ret = 0;
  456. timer_stats_timer_clear_start_info(timer);
  457. if (timer_pending(timer)) {
  458. base = lock_timer_base(timer, &flags);
  459. if (timer_pending(timer)) {
  460. detach_timer(timer, 1);
  461. ret = 1;
  462. }
  463. spin_unlock_irqrestore(&base->lock, flags);
  464. }
  465. return ret;
  466. }
  467. EXPORT_SYMBOL(del_timer);
  468. #ifdef CONFIG_SMP
  469. /**
  470. * try_to_del_timer_sync - Try to deactivate a timer
  471. * @timer: timer do del
  472. *
  473. * This function tries to deactivate a timer. Upon successful (ret >= 0)
  474. * exit the timer is not queued and the handler is not running on any CPU.
  475. *
  476. * It must not be called from interrupt contexts.
  477. */
  478. int try_to_del_timer_sync(struct timer_list *timer)
  479. {
  480. tvec_base_t *base;
  481. unsigned long flags;
  482. int ret = -1;
  483. base = lock_timer_base(timer, &flags);
  484. if (base->running_timer == timer)
  485. goto out;
  486. ret = 0;
  487. if (timer_pending(timer)) {
  488. detach_timer(timer, 1);
  489. ret = 1;
  490. }
  491. out:
  492. spin_unlock_irqrestore(&base->lock, flags);
  493. return ret;
  494. }
  495. EXPORT_SYMBOL(try_to_del_timer_sync);
  496. /**
  497. * del_timer_sync - deactivate a timer and wait for the handler to finish.
  498. * @timer: the timer to be deactivated
  499. *
  500. * This function only differs from del_timer() on SMP: besides deactivating
  501. * the timer it also makes sure the handler has finished executing on other
  502. * CPUs.
  503. *
  504. * Synchronization rules: Callers must prevent restarting of the timer,
  505. * otherwise this function is meaningless. It must not be called from
  506. * interrupt contexts. The caller must not hold locks which would prevent
  507. * completion of the timer's handler. The timer's handler must not call
  508. * add_timer_on(). Upon exit the timer is not queued and the handler is
  509. * not running on any CPU.
  510. *
  511. * The function returns whether it has deactivated a pending timer or not.
  512. */
  513. int del_timer_sync(struct timer_list *timer)
  514. {
  515. for (;;) {
  516. int ret = try_to_del_timer_sync(timer);
  517. if (ret >= 0)
  518. return ret;
  519. cpu_relax();
  520. }
  521. }
  522. EXPORT_SYMBOL(del_timer_sync);
  523. #endif
  524. static int cascade(tvec_base_t *base, tvec_t *tv, int index)
  525. {
  526. /* cascade all the timers from tv up one level */
  527. struct timer_list *timer, *tmp;
  528. struct list_head tv_list;
  529. list_replace_init(tv->vec + index, &tv_list);
  530. /*
  531. * We are removing _all_ timers from the list, so we
  532. * don't have to detach them individually.
  533. */
  534. list_for_each_entry_safe(timer, tmp, &tv_list, entry) {
  535. BUG_ON(tbase_get_base(timer->base) != base);
  536. internal_add_timer(base, timer);
  537. }
  538. return index;
  539. }
  540. #define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK)
  541. /**
  542. * __run_timers - run all expired timers (if any) on this CPU.
  543. * @base: the timer vector to be processed.
  544. *
  545. * This function cascades all vectors and executes all expired timer
  546. * vectors.
  547. */
  548. static inline void __run_timers(tvec_base_t *base)
  549. {
  550. struct timer_list *timer;
  551. spin_lock_irq(&base->lock);
  552. while (time_after_eq(jiffies, base->timer_jiffies)) {
  553. struct list_head work_list;
  554. struct list_head *head = &work_list;
  555. int index = base->timer_jiffies & TVR_MASK;
  556. /*
  557. * Cascade timers:
  558. */
  559. if (!index &&
  560. (!cascade(base, &base->tv2, INDEX(0))) &&
  561. (!cascade(base, &base->tv3, INDEX(1))) &&
  562. !cascade(base, &base->tv4, INDEX(2)))
  563. cascade(base, &base->tv5, INDEX(3));
  564. ++base->timer_jiffies;
  565. list_replace_init(base->tv1.vec + index, &work_list);
  566. while (!list_empty(head)) {
  567. void (*fn)(unsigned long);
  568. unsigned long data;
  569. timer = list_first_entry(head, struct timer_list,entry);
  570. fn = timer->function;
  571. data = timer->data;
  572. timer_stats_account_timer(timer);
  573. set_running_timer(base, timer);
  574. detach_timer(timer, 1);
  575. spin_unlock_irq(&base->lock);
  576. {
  577. int preempt_count = preempt_count();
  578. fn(data);
  579. if (preempt_count != preempt_count()) {
  580. printk(KERN_WARNING "huh, entered %p "
  581. "with preempt_count %08x, exited"
  582. " with %08x?\n",
  583. fn, preempt_count,
  584. preempt_count());
  585. BUG();
  586. }
  587. }
  588. spin_lock_irq(&base->lock);
  589. }
  590. }
  591. set_running_timer(base, NULL);
  592. spin_unlock_irq(&base->lock);
  593. }
  594. #if defined(CONFIG_NO_IDLE_HZ) || defined(CONFIG_NO_HZ)
  595. /*
  596. * Find out when the next timer event is due to happen. This
  597. * is used on S/390 to stop all activity when a cpus is idle.
  598. * This functions needs to be called disabled.
  599. */
  600. static unsigned long __next_timer_interrupt(tvec_base_t *base)
  601. {
  602. unsigned long timer_jiffies = base->timer_jiffies;
  603. unsigned long expires = timer_jiffies + NEXT_TIMER_MAX_DELTA;
  604. int index, slot, array, found = 0;
  605. struct timer_list *nte;
  606. tvec_t *varray[4];
  607. /* Look for timer events in tv1. */
  608. index = slot = timer_jiffies & TVR_MASK;
  609. do {
  610. list_for_each_entry(nte, base->tv1.vec + slot, entry) {
  611. if (tbase_get_deferrable(nte->base))
  612. continue;
  613. found = 1;
  614. expires = nte->expires;
  615. /* Look at the cascade bucket(s)? */
  616. if (!index || slot < index)
  617. goto cascade;
  618. return expires;
  619. }
  620. slot = (slot + 1) & TVR_MASK;
  621. } while (slot != index);
  622. cascade:
  623. /* Calculate the next cascade event */
  624. if (index)
  625. timer_jiffies += TVR_SIZE - index;
  626. timer_jiffies >>= TVR_BITS;
  627. /* Check tv2-tv5. */
  628. varray[0] = &base->tv2;
  629. varray[1] = &base->tv3;
  630. varray[2] = &base->tv4;
  631. varray[3] = &base->tv5;
  632. for (array = 0; array < 4; array++) {
  633. tvec_t *varp = varray[array];
  634. index = slot = timer_jiffies & TVN_MASK;
  635. do {
  636. list_for_each_entry(nte, varp->vec + slot, entry) {
  637. found = 1;
  638. if (time_before(nte->expires, expires))
  639. expires = nte->expires;
  640. }
  641. /*
  642. * Do we still search for the first timer or are
  643. * we looking up the cascade buckets ?
  644. */
  645. if (found) {
  646. /* Look at the cascade bucket(s)? */
  647. if (!index || slot < index)
  648. break;
  649. return expires;
  650. }
  651. slot = (slot + 1) & TVN_MASK;
  652. } while (slot != index);
  653. if (index)
  654. timer_jiffies += TVN_SIZE - index;
  655. timer_jiffies >>= TVN_BITS;
  656. }
  657. return expires;
  658. }
  659. /*
  660. * Check, if the next hrtimer event is before the next timer wheel
  661. * event:
  662. */
  663. static unsigned long cmp_next_hrtimer_event(unsigned long now,
  664. unsigned long expires)
  665. {
  666. ktime_t hr_delta = hrtimer_get_next_event();
  667. struct timespec tsdelta;
  668. unsigned long delta;
  669. if (hr_delta.tv64 == KTIME_MAX)
  670. return expires;
  671. /*
  672. * Expired timer available, let it expire in the next tick
  673. */
  674. if (hr_delta.tv64 <= 0)
  675. return now + 1;
  676. tsdelta = ktime_to_timespec(hr_delta);
  677. delta = timespec_to_jiffies(&tsdelta);
  678. /*
  679. * Limit the delta to the max value, which is checked in
  680. * tick_nohz_stop_sched_tick():
  681. */
  682. if (delta > NEXT_TIMER_MAX_DELTA)
  683. delta = NEXT_TIMER_MAX_DELTA;
  684. /*
  685. * Take rounding errors in to account and make sure, that it
  686. * expires in the next tick. Otherwise we go into an endless
  687. * ping pong due to tick_nohz_stop_sched_tick() retriggering
  688. * the timer softirq
  689. */
  690. if (delta < 1)
  691. delta = 1;
  692. now += delta;
  693. if (time_before(now, expires))
  694. return now;
  695. return expires;
  696. }
  697. /**
  698. * next_timer_interrupt - return the jiffy of the next pending timer
  699. * @now: current time (in jiffies)
  700. */
  701. unsigned long get_next_timer_interrupt(unsigned long now)
  702. {
  703. tvec_base_t *base = __get_cpu_var(tvec_bases);
  704. unsigned long expires;
  705. spin_lock(&base->lock);
  706. expires = __next_timer_interrupt(base);
  707. spin_unlock(&base->lock);
  708. if (time_before_eq(expires, now))
  709. return now;
  710. return cmp_next_hrtimer_event(now, expires);
  711. }
  712. #ifdef CONFIG_NO_IDLE_HZ
  713. unsigned long next_timer_interrupt(void)
  714. {
  715. return get_next_timer_interrupt(jiffies);
  716. }
  717. #endif
  718. #endif
  719. /*
  720. * Called from the timer interrupt handler to charge one tick to the current
  721. * process. user_tick is 1 if the tick is user time, 0 for system.
  722. */
  723. void update_process_times(int user_tick)
  724. {
  725. struct task_struct *p = current;
  726. int cpu = smp_processor_id();
  727. /* Note: this timer irq context must be accounted for as well. */
  728. if (user_tick) {
  729. account_user_time(p, jiffies_to_cputime(1));
  730. account_user_time_scaled(p, jiffies_to_cputime(1));
  731. } else {
  732. account_system_time(p, HARDIRQ_OFFSET, jiffies_to_cputime(1));
  733. account_system_time_scaled(p, jiffies_to_cputime(1));
  734. }
  735. run_local_timers();
  736. if (rcu_pending(cpu))
  737. rcu_check_callbacks(cpu, user_tick);
  738. scheduler_tick();
  739. run_posix_cpu_timers(p);
  740. }
  741. /*
  742. * Nr of active tasks - counted in fixed-point numbers
  743. */
  744. static unsigned long count_active_tasks(void)
  745. {
  746. return nr_active() * FIXED_1;
  747. }
  748. /*
  749. * Hmm.. Changed this, as the GNU make sources (load.c) seems to
  750. * imply that avenrun[] is the standard name for this kind of thing.
  751. * Nothing else seems to be standardized: the fractional size etc
  752. * all seem to differ on different machines.
  753. *
  754. * Requires xtime_lock to access.
  755. */
  756. unsigned long avenrun[3];
  757. EXPORT_SYMBOL(avenrun);
  758. /*
  759. * calc_load - given tick count, update the avenrun load estimates.
  760. * This is called while holding a write_lock on xtime_lock.
  761. */
  762. static inline void calc_load(unsigned long ticks)
  763. {
  764. unsigned long active_tasks; /* fixed-point */
  765. static int count = LOAD_FREQ;
  766. count -= ticks;
  767. if (unlikely(count < 0)) {
  768. active_tasks = count_active_tasks();
  769. do {
  770. CALC_LOAD(avenrun[0], EXP_1, active_tasks);
  771. CALC_LOAD(avenrun[1], EXP_5, active_tasks);
  772. CALC_LOAD(avenrun[2], EXP_15, active_tasks);
  773. count += LOAD_FREQ;
  774. } while (count < 0);
  775. }
  776. }
  777. /*
  778. * This function runs timers and the timer-tq in bottom half context.
  779. */
  780. static void run_timer_softirq(struct softirq_action *h)
  781. {
  782. tvec_base_t *base = __get_cpu_var(tvec_bases);
  783. hrtimer_run_queues();
  784. if (time_after_eq(jiffies, base->timer_jiffies))
  785. __run_timers(base);
  786. }
  787. /*
  788. * Called by the local, per-CPU timer interrupt on SMP.
  789. */
  790. void run_local_timers(void)
  791. {
  792. raise_softirq(TIMER_SOFTIRQ);
  793. softlockup_tick();
  794. }
  795. /*
  796. * Called by the timer interrupt. xtime_lock must already be taken
  797. * by the timer IRQ!
  798. */
  799. static inline void update_times(unsigned long ticks)
  800. {
  801. update_wall_time();
  802. calc_load(ticks);
  803. }
  804. /*
  805. * The 64-bit jiffies value is not atomic - you MUST NOT read it
  806. * without sampling the sequence number in xtime_lock.
  807. * jiffies is defined in the linker script...
  808. */
  809. void do_timer(unsigned long ticks)
  810. {
  811. jiffies_64 += ticks;
  812. update_times(ticks);
  813. }
  814. #ifdef __ARCH_WANT_SYS_ALARM
  815. /*
  816. * For backwards compatibility? This can be done in libc so Alpha
  817. * and all newer ports shouldn't need it.
  818. */
  819. asmlinkage unsigned long sys_alarm(unsigned int seconds)
  820. {
  821. return alarm_setitimer(seconds);
  822. }
  823. #endif
  824. #ifndef __alpha__
  825. /*
  826. * The Alpha uses getxpid, getxuid, and getxgid instead. Maybe this
  827. * should be moved into arch/i386 instead?
  828. */
  829. /**
  830. * sys_getpid - return the thread group id of the current process
  831. *
  832. * Note, despite the name, this returns the tgid not the pid. The tgid and
  833. * the pid are identical unless CLONE_THREAD was specified on clone() in
  834. * which case the tgid is the same in all threads of the same group.
  835. *
  836. * This is SMP safe as current->tgid does not change.
  837. */
  838. asmlinkage long sys_getpid(void)
  839. {
  840. return current->tgid;
  841. }
  842. /*
  843. * Accessing ->real_parent is not SMP-safe, it could
  844. * change from under us. However, we can use a stale
  845. * value of ->real_parent under rcu_read_lock(), see
  846. * release_task()->call_rcu(delayed_put_task_struct).
  847. */
  848. asmlinkage long sys_getppid(void)
  849. {
  850. int pid;
  851. rcu_read_lock();
  852. pid = rcu_dereference(current->real_parent)->tgid;
  853. rcu_read_unlock();
  854. return pid;
  855. }
  856. asmlinkage long sys_getuid(void)
  857. {
  858. /* Only we change this so SMP safe */
  859. return current->uid;
  860. }
  861. asmlinkage long sys_geteuid(void)
  862. {
  863. /* Only we change this so SMP safe */
  864. return current->euid;
  865. }
  866. asmlinkage long sys_getgid(void)
  867. {
  868. /* Only we change this so SMP safe */
  869. return current->gid;
  870. }
  871. asmlinkage long sys_getegid(void)
  872. {
  873. /* Only we change this so SMP safe */
  874. return current->egid;
  875. }
  876. #endif
  877. static void process_timeout(unsigned long __data)
  878. {
  879. wake_up_process((struct task_struct *)__data);
  880. }
  881. /**
  882. * schedule_timeout - sleep until timeout
  883. * @timeout: timeout value in jiffies
  884. *
  885. * Make the current task sleep until @timeout jiffies have
  886. * elapsed. The routine will return immediately unless
  887. * the current task state has been set (see set_current_state()).
  888. *
  889. * You can set the task state as follows -
  890. *
  891. * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
  892. * pass before the routine returns. The routine will return 0
  893. *
  894. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  895. * delivered to the current task. In this case the remaining time
  896. * in jiffies will be returned, or 0 if the timer expired in time
  897. *
  898. * The current task state is guaranteed to be TASK_RUNNING when this
  899. * routine returns.
  900. *
  901. * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
  902. * the CPU away without a bound on the timeout. In this case the return
  903. * value will be %MAX_SCHEDULE_TIMEOUT.
  904. *
  905. * In all cases the return value is guaranteed to be non-negative.
  906. */
  907. fastcall signed long __sched schedule_timeout(signed long timeout)
  908. {
  909. struct timer_list timer;
  910. unsigned long expire;
  911. switch (timeout)
  912. {
  913. case MAX_SCHEDULE_TIMEOUT:
  914. /*
  915. * These two special cases are useful to be comfortable
  916. * in the caller. Nothing more. We could take
  917. * MAX_SCHEDULE_TIMEOUT from one of the negative value
  918. * but I' d like to return a valid offset (>=0) to allow
  919. * the caller to do everything it want with the retval.
  920. */
  921. schedule();
  922. goto out;
  923. default:
  924. /*
  925. * Another bit of PARANOID. Note that the retval will be
  926. * 0 since no piece of kernel is supposed to do a check
  927. * for a negative retval of schedule_timeout() (since it
  928. * should never happens anyway). You just have the printk()
  929. * that will tell you if something is gone wrong and where.
  930. */
  931. if (timeout < 0) {
  932. printk(KERN_ERR "schedule_timeout: wrong timeout "
  933. "value %lx\n", timeout);
  934. dump_stack();
  935. current->state = TASK_RUNNING;
  936. goto out;
  937. }
  938. }
  939. expire = timeout + jiffies;
  940. setup_timer(&timer, process_timeout, (unsigned long)current);
  941. __mod_timer(&timer, expire);
  942. schedule();
  943. del_singleshot_timer_sync(&timer);
  944. timeout = expire - jiffies;
  945. out:
  946. return timeout < 0 ? 0 : timeout;
  947. }
  948. EXPORT_SYMBOL(schedule_timeout);
  949. /*
  950. * We can use __set_current_state() here because schedule_timeout() calls
  951. * schedule() unconditionally.
  952. */
  953. signed long __sched schedule_timeout_interruptible(signed long timeout)
  954. {
  955. __set_current_state(TASK_INTERRUPTIBLE);
  956. return schedule_timeout(timeout);
  957. }
  958. EXPORT_SYMBOL(schedule_timeout_interruptible);
  959. signed long __sched schedule_timeout_uninterruptible(signed long timeout)
  960. {
  961. __set_current_state(TASK_UNINTERRUPTIBLE);
  962. return schedule_timeout(timeout);
  963. }
  964. EXPORT_SYMBOL(schedule_timeout_uninterruptible);
  965. /* Thread ID - the internal kernel "pid" */
  966. asmlinkage long sys_gettid(void)
  967. {
  968. return current->pid;
  969. }
  970. /**
  971. * do_sysinfo - fill in sysinfo struct
  972. * @info: pointer to buffer to fill
  973. */
  974. int do_sysinfo(struct sysinfo *info)
  975. {
  976. unsigned long mem_total, sav_total;
  977. unsigned int mem_unit, bitcount;
  978. unsigned long seq;
  979. memset(info, 0, sizeof(struct sysinfo));
  980. do {
  981. struct timespec tp;
  982. seq = read_seqbegin(&xtime_lock);
  983. /*
  984. * This is annoying. The below is the same thing
  985. * posix_get_clock_monotonic() does, but it wants to
  986. * take the lock which we want to cover the loads stuff
  987. * too.
  988. */
  989. getnstimeofday(&tp);
  990. tp.tv_sec += wall_to_monotonic.tv_sec;
  991. tp.tv_nsec += wall_to_monotonic.tv_nsec;
  992. monotonic_to_bootbased(&tp);
  993. if (tp.tv_nsec - NSEC_PER_SEC >= 0) {
  994. tp.tv_nsec = tp.tv_nsec - NSEC_PER_SEC;
  995. tp.tv_sec++;
  996. }
  997. info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
  998. info->loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT);
  999. info->loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT);
  1000. info->loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT);
  1001. info->procs = nr_threads;
  1002. } while (read_seqretry(&xtime_lock, seq));
  1003. si_meminfo(info);
  1004. si_swapinfo(info);
  1005. /*
  1006. * If the sum of all the available memory (i.e. ram + swap)
  1007. * is less than can be stored in a 32 bit unsigned long then
  1008. * we can be binary compatible with 2.2.x kernels. If not,
  1009. * well, in that case 2.2.x was broken anyways...
  1010. *
  1011. * -Erik Andersen <andersee@debian.org>
  1012. */
  1013. mem_total = info->totalram + info->totalswap;
  1014. if (mem_total < info->totalram || mem_total < info->totalswap)
  1015. goto out;
  1016. bitcount = 0;
  1017. mem_unit = info->mem_unit;
  1018. while (mem_unit > 1) {
  1019. bitcount++;
  1020. mem_unit >>= 1;
  1021. sav_total = mem_total;
  1022. mem_total <<= 1;
  1023. if (mem_total < sav_total)
  1024. goto out;
  1025. }
  1026. /*
  1027. * If mem_total did not overflow, multiply all memory values by
  1028. * info->mem_unit and set it to 1. This leaves things compatible
  1029. * with 2.2.x, and also retains compatibility with earlier 2.4.x
  1030. * kernels...
  1031. */
  1032. info->mem_unit = 1;
  1033. info->totalram <<= bitcount;
  1034. info->freeram <<= bitcount;
  1035. info->sharedram <<= bitcount;
  1036. info->bufferram <<= bitcount;
  1037. info->totalswap <<= bitcount;
  1038. info->freeswap <<= bitcount;
  1039. info->totalhigh <<= bitcount;
  1040. info->freehigh <<= bitcount;
  1041. out:
  1042. return 0;
  1043. }
  1044. asmlinkage long sys_sysinfo(struct sysinfo __user *info)
  1045. {
  1046. struct sysinfo val;
  1047. do_sysinfo(&val);
  1048. if (copy_to_user(info, &val, sizeof(struct sysinfo)))
  1049. return -EFAULT;
  1050. return 0;
  1051. }
  1052. /*
  1053. * lockdep: we want to track each per-CPU base as a separate lock-class,
  1054. * but timer-bases are kmalloc()-ed, so we need to attach separate
  1055. * keys to them:
  1056. */
  1057. static struct lock_class_key base_lock_keys[NR_CPUS];
  1058. static int __devinit init_timers_cpu(int cpu)
  1059. {
  1060. int j;
  1061. tvec_base_t *base;
  1062. static char __devinitdata tvec_base_done[NR_CPUS];
  1063. if (!tvec_base_done[cpu]) {
  1064. static char boot_done;
  1065. if (boot_done) {
  1066. /*
  1067. * The APs use this path later in boot
  1068. */
  1069. base = kmalloc_node(sizeof(*base),
  1070. GFP_KERNEL | __GFP_ZERO,
  1071. cpu_to_node(cpu));
  1072. if (!base)
  1073. return -ENOMEM;
  1074. /* Make sure that tvec_base is 2 byte aligned */
  1075. if (tbase_get_deferrable(base)) {
  1076. WARN_ON(1);
  1077. kfree(base);
  1078. return -ENOMEM;
  1079. }
  1080. per_cpu(tvec_bases, cpu) = base;
  1081. } else {
  1082. /*
  1083. * This is for the boot CPU - we use compile-time
  1084. * static initialisation because per-cpu memory isn't
  1085. * ready yet and because the memory allocators are not
  1086. * initialised either.
  1087. */
  1088. boot_done = 1;
  1089. base = &boot_tvec_bases;
  1090. }
  1091. tvec_base_done[cpu] = 1;
  1092. } else {
  1093. base = per_cpu(tvec_bases, cpu);
  1094. }
  1095. spin_lock_init(&base->lock);
  1096. lockdep_set_class(&base->lock, base_lock_keys + cpu);
  1097. for (j = 0; j < TVN_SIZE; j++) {
  1098. INIT_LIST_HEAD(base->tv5.vec + j);
  1099. INIT_LIST_HEAD(base->tv4.vec + j);
  1100. INIT_LIST_HEAD(base->tv3.vec + j);
  1101. INIT_LIST_HEAD(base->tv2.vec + j);
  1102. }
  1103. for (j = 0; j < TVR_SIZE; j++)
  1104. INIT_LIST_HEAD(base->tv1.vec + j);
  1105. base->timer_jiffies = jiffies;
  1106. return 0;
  1107. }
  1108. #ifdef CONFIG_HOTPLUG_CPU
  1109. static void migrate_timer_list(tvec_base_t *new_base, struct list_head *head)
  1110. {
  1111. struct timer_list *timer;
  1112. while (!list_empty(head)) {
  1113. timer = list_first_entry(head, struct timer_list, entry);
  1114. detach_timer(timer, 0);
  1115. timer_set_base(timer, new_base);
  1116. internal_add_timer(new_base, timer);
  1117. }
  1118. }
  1119. static void __devinit migrate_timers(int cpu)
  1120. {
  1121. tvec_base_t *old_base;
  1122. tvec_base_t *new_base;
  1123. int i;
  1124. BUG_ON(cpu_online(cpu));
  1125. old_base = per_cpu(tvec_bases, cpu);
  1126. new_base = get_cpu_var(tvec_bases);
  1127. local_irq_disable();
  1128. double_spin_lock(&new_base->lock, &old_base->lock,
  1129. smp_processor_id() < cpu);
  1130. BUG_ON(old_base->running_timer);
  1131. for (i = 0; i < TVR_SIZE; i++)
  1132. migrate_timer_list(new_base, old_base->tv1.vec + i);
  1133. for (i = 0; i < TVN_SIZE; i++) {
  1134. migrate_timer_list(new_base, old_base->tv2.vec + i);
  1135. migrate_timer_list(new_base, old_base->tv3.vec + i);
  1136. migrate_timer_list(new_base, old_base->tv4.vec + i);
  1137. migrate_timer_list(new_base, old_base->tv5.vec + i);
  1138. }
  1139. double_spin_unlock(&new_base->lock, &old_base->lock,
  1140. smp_processor_id() < cpu);
  1141. local_irq_enable();
  1142. put_cpu_var(tvec_bases);
  1143. }
  1144. #endif /* CONFIG_HOTPLUG_CPU */
  1145. static int __cpuinit timer_cpu_notify(struct notifier_block *self,
  1146. unsigned long action, void *hcpu)
  1147. {
  1148. long cpu = (long)hcpu;
  1149. switch(action) {
  1150. case CPU_UP_PREPARE:
  1151. case CPU_UP_PREPARE_FROZEN:
  1152. if (init_timers_cpu(cpu) < 0)
  1153. return NOTIFY_BAD;
  1154. break;
  1155. #ifdef CONFIG_HOTPLUG_CPU
  1156. case CPU_DEAD:
  1157. case CPU_DEAD_FROZEN:
  1158. migrate_timers(cpu);
  1159. break;
  1160. #endif
  1161. default:
  1162. break;
  1163. }
  1164. return NOTIFY_OK;
  1165. }
  1166. static struct notifier_block __cpuinitdata timers_nb = {
  1167. .notifier_call = timer_cpu_notify,
  1168. };
  1169. void __init init_timers(void)
  1170. {
  1171. int err = timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
  1172. (void *)(long)smp_processor_id());
  1173. init_timer_stats();
  1174. BUG_ON(err == NOTIFY_BAD);
  1175. register_cpu_notifier(&timers_nb);
  1176. open_softirq(TIMER_SOFTIRQ, run_timer_softirq, NULL);
  1177. }
  1178. /**
  1179. * msleep - sleep safely even with waitqueue interruptions
  1180. * @msecs: Time in milliseconds to sleep for
  1181. */
  1182. void msleep(unsigned int msecs)
  1183. {
  1184. unsigned long timeout = msecs_to_jiffies(msecs) + 1;
  1185. while (timeout)
  1186. timeout = schedule_timeout_uninterruptible(timeout);
  1187. }
  1188. EXPORT_SYMBOL(msleep);
  1189. /**
  1190. * msleep_interruptible - sleep waiting for signals
  1191. * @msecs: Time in milliseconds to sleep for
  1192. */
  1193. unsigned long msleep_interruptible(unsigned int msecs)
  1194. {
  1195. unsigned long timeout = msecs_to_jiffies(msecs) + 1;
  1196. while (timeout && !signal_pending(current))
  1197. timeout = schedule_timeout_interruptible(timeout);
  1198. return jiffies_to_msecs(timeout);
  1199. }
  1200. EXPORT_SYMBOL(msleep_interruptible);