timer.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567
  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. else
  731. account_system_time(p, HARDIRQ_OFFSET, jiffies_to_cputime(1));
  732. run_local_timers();
  733. if (rcu_pending(cpu))
  734. rcu_check_callbacks(cpu, user_tick);
  735. scheduler_tick();
  736. run_posix_cpu_timers(p);
  737. }
  738. /*
  739. * Nr of active tasks - counted in fixed-point numbers
  740. */
  741. static unsigned long count_active_tasks(void)
  742. {
  743. return nr_active() * FIXED_1;
  744. }
  745. /*
  746. * Hmm.. Changed this, as the GNU make sources (load.c) seems to
  747. * imply that avenrun[] is the standard name for this kind of thing.
  748. * Nothing else seems to be standardized: the fractional size etc
  749. * all seem to differ on different machines.
  750. *
  751. * Requires xtime_lock to access.
  752. */
  753. unsigned long avenrun[3];
  754. EXPORT_SYMBOL(avenrun);
  755. /*
  756. * calc_load - given tick count, update the avenrun load estimates.
  757. * This is called while holding a write_lock on xtime_lock.
  758. */
  759. static inline void calc_load(unsigned long ticks)
  760. {
  761. unsigned long active_tasks; /* fixed-point */
  762. static int count = LOAD_FREQ;
  763. count -= ticks;
  764. if (unlikely(count < 0)) {
  765. active_tasks = count_active_tasks();
  766. do {
  767. CALC_LOAD(avenrun[0], EXP_1, active_tasks);
  768. CALC_LOAD(avenrun[1], EXP_5, active_tasks);
  769. CALC_LOAD(avenrun[2], EXP_15, active_tasks);
  770. count += LOAD_FREQ;
  771. } while (count < 0);
  772. }
  773. }
  774. /*
  775. * This function runs timers and the timer-tq in bottom half context.
  776. */
  777. static void run_timer_softirq(struct softirq_action *h)
  778. {
  779. tvec_base_t *base = __get_cpu_var(tvec_bases);
  780. hrtimer_run_queues();
  781. if (time_after_eq(jiffies, base->timer_jiffies))
  782. __run_timers(base);
  783. }
  784. /*
  785. * Called by the local, per-CPU timer interrupt on SMP.
  786. */
  787. void run_local_timers(void)
  788. {
  789. raise_softirq(TIMER_SOFTIRQ);
  790. softlockup_tick();
  791. }
  792. /*
  793. * Called by the timer interrupt. xtime_lock must already be taken
  794. * by the timer IRQ!
  795. */
  796. static inline void update_times(unsigned long ticks)
  797. {
  798. update_wall_time();
  799. calc_load(ticks);
  800. }
  801. /*
  802. * The 64-bit jiffies value is not atomic - you MUST NOT read it
  803. * without sampling the sequence number in xtime_lock.
  804. * jiffies is defined in the linker script...
  805. */
  806. void do_timer(unsigned long ticks)
  807. {
  808. jiffies_64 += ticks;
  809. update_times(ticks);
  810. }
  811. #ifdef __ARCH_WANT_SYS_ALARM
  812. /*
  813. * For backwards compatibility? This can be done in libc so Alpha
  814. * and all newer ports shouldn't need it.
  815. */
  816. asmlinkage unsigned long sys_alarm(unsigned int seconds)
  817. {
  818. return alarm_setitimer(seconds);
  819. }
  820. #endif
  821. #ifndef __alpha__
  822. /*
  823. * The Alpha uses getxpid, getxuid, and getxgid instead. Maybe this
  824. * should be moved into arch/i386 instead?
  825. */
  826. /**
  827. * sys_getpid - return the thread group id of the current process
  828. *
  829. * Note, despite the name, this returns the tgid not the pid. The tgid and
  830. * the pid are identical unless CLONE_THREAD was specified on clone() in
  831. * which case the tgid is the same in all threads of the same group.
  832. *
  833. * This is SMP safe as current->tgid does not change.
  834. */
  835. asmlinkage long sys_getpid(void)
  836. {
  837. return current->tgid;
  838. }
  839. /*
  840. * Accessing ->real_parent is not SMP-safe, it could
  841. * change from under us. However, we can use a stale
  842. * value of ->real_parent under rcu_read_lock(), see
  843. * release_task()->call_rcu(delayed_put_task_struct).
  844. */
  845. asmlinkage long sys_getppid(void)
  846. {
  847. int pid;
  848. rcu_read_lock();
  849. pid = rcu_dereference(current->real_parent)->tgid;
  850. rcu_read_unlock();
  851. return pid;
  852. }
  853. asmlinkage long sys_getuid(void)
  854. {
  855. /* Only we change this so SMP safe */
  856. return current->uid;
  857. }
  858. asmlinkage long sys_geteuid(void)
  859. {
  860. /* Only we change this so SMP safe */
  861. return current->euid;
  862. }
  863. asmlinkage long sys_getgid(void)
  864. {
  865. /* Only we change this so SMP safe */
  866. return current->gid;
  867. }
  868. asmlinkage long sys_getegid(void)
  869. {
  870. /* Only we change this so SMP safe */
  871. return current->egid;
  872. }
  873. #endif
  874. static void process_timeout(unsigned long __data)
  875. {
  876. wake_up_process((struct task_struct *)__data);
  877. }
  878. /**
  879. * schedule_timeout - sleep until timeout
  880. * @timeout: timeout value in jiffies
  881. *
  882. * Make the current task sleep until @timeout jiffies have
  883. * elapsed. The routine will return immediately unless
  884. * the current task state has been set (see set_current_state()).
  885. *
  886. * You can set the task state as follows -
  887. *
  888. * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
  889. * pass before the routine returns. The routine will return 0
  890. *
  891. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  892. * delivered to the current task. In this case the remaining time
  893. * in jiffies will be returned, or 0 if the timer expired in time
  894. *
  895. * The current task state is guaranteed to be TASK_RUNNING when this
  896. * routine returns.
  897. *
  898. * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
  899. * the CPU away without a bound on the timeout. In this case the return
  900. * value will be %MAX_SCHEDULE_TIMEOUT.
  901. *
  902. * In all cases the return value is guaranteed to be non-negative.
  903. */
  904. fastcall signed long __sched schedule_timeout(signed long timeout)
  905. {
  906. struct timer_list timer;
  907. unsigned long expire;
  908. switch (timeout)
  909. {
  910. case MAX_SCHEDULE_TIMEOUT:
  911. /*
  912. * These two special cases are useful to be comfortable
  913. * in the caller. Nothing more. We could take
  914. * MAX_SCHEDULE_TIMEOUT from one of the negative value
  915. * but I' d like to return a valid offset (>=0) to allow
  916. * the caller to do everything it want with the retval.
  917. */
  918. schedule();
  919. goto out;
  920. default:
  921. /*
  922. * Another bit of PARANOID. Note that the retval will be
  923. * 0 since no piece of kernel is supposed to do a check
  924. * for a negative retval of schedule_timeout() (since it
  925. * should never happens anyway). You just have the printk()
  926. * that will tell you if something is gone wrong and where.
  927. */
  928. if (timeout < 0) {
  929. printk(KERN_ERR "schedule_timeout: wrong timeout "
  930. "value %lx\n", timeout);
  931. dump_stack();
  932. current->state = TASK_RUNNING;
  933. goto out;
  934. }
  935. }
  936. expire = timeout + jiffies;
  937. setup_timer(&timer, process_timeout, (unsigned long)current);
  938. __mod_timer(&timer, expire);
  939. schedule();
  940. del_singleshot_timer_sync(&timer);
  941. timeout = expire - jiffies;
  942. out:
  943. return timeout < 0 ? 0 : timeout;
  944. }
  945. EXPORT_SYMBOL(schedule_timeout);
  946. /*
  947. * We can use __set_current_state() here because schedule_timeout() calls
  948. * schedule() unconditionally.
  949. */
  950. signed long __sched schedule_timeout_interruptible(signed long timeout)
  951. {
  952. __set_current_state(TASK_INTERRUPTIBLE);
  953. return schedule_timeout(timeout);
  954. }
  955. EXPORT_SYMBOL(schedule_timeout_interruptible);
  956. signed long __sched schedule_timeout_uninterruptible(signed long timeout)
  957. {
  958. __set_current_state(TASK_UNINTERRUPTIBLE);
  959. return schedule_timeout(timeout);
  960. }
  961. EXPORT_SYMBOL(schedule_timeout_uninterruptible);
  962. /* Thread ID - the internal kernel "pid" */
  963. asmlinkage long sys_gettid(void)
  964. {
  965. return current->pid;
  966. }
  967. /**
  968. * do_sysinfo - fill in sysinfo struct
  969. * @info: pointer to buffer to fill
  970. */
  971. int do_sysinfo(struct sysinfo *info)
  972. {
  973. unsigned long mem_total, sav_total;
  974. unsigned int mem_unit, bitcount;
  975. unsigned long seq;
  976. memset(info, 0, sizeof(struct sysinfo));
  977. do {
  978. struct timespec tp;
  979. seq = read_seqbegin(&xtime_lock);
  980. /*
  981. * This is annoying. The below is the same thing
  982. * posix_get_clock_monotonic() does, but it wants to
  983. * take the lock which we want to cover the loads stuff
  984. * too.
  985. */
  986. getnstimeofday(&tp);
  987. tp.tv_sec += wall_to_monotonic.tv_sec;
  988. tp.tv_nsec += wall_to_monotonic.tv_nsec;
  989. monotonic_to_bootbased(&tp);
  990. if (tp.tv_nsec - NSEC_PER_SEC >= 0) {
  991. tp.tv_nsec = tp.tv_nsec - NSEC_PER_SEC;
  992. tp.tv_sec++;
  993. }
  994. info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
  995. info->loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT);
  996. info->loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT);
  997. info->loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT);
  998. info->procs = nr_threads;
  999. } while (read_seqretry(&xtime_lock, seq));
  1000. si_meminfo(info);
  1001. si_swapinfo(info);
  1002. /*
  1003. * If the sum of all the available memory (i.e. ram + swap)
  1004. * is less than can be stored in a 32 bit unsigned long then
  1005. * we can be binary compatible with 2.2.x kernels. If not,
  1006. * well, in that case 2.2.x was broken anyways...
  1007. *
  1008. * -Erik Andersen <andersee@debian.org>
  1009. */
  1010. mem_total = info->totalram + info->totalswap;
  1011. if (mem_total < info->totalram || mem_total < info->totalswap)
  1012. goto out;
  1013. bitcount = 0;
  1014. mem_unit = info->mem_unit;
  1015. while (mem_unit > 1) {
  1016. bitcount++;
  1017. mem_unit >>= 1;
  1018. sav_total = mem_total;
  1019. mem_total <<= 1;
  1020. if (mem_total < sav_total)
  1021. goto out;
  1022. }
  1023. /*
  1024. * If mem_total did not overflow, multiply all memory values by
  1025. * info->mem_unit and set it to 1. This leaves things compatible
  1026. * with 2.2.x, and also retains compatibility with earlier 2.4.x
  1027. * kernels...
  1028. */
  1029. info->mem_unit = 1;
  1030. info->totalram <<= bitcount;
  1031. info->freeram <<= bitcount;
  1032. info->sharedram <<= bitcount;
  1033. info->bufferram <<= bitcount;
  1034. info->totalswap <<= bitcount;
  1035. info->freeswap <<= bitcount;
  1036. info->totalhigh <<= bitcount;
  1037. info->freehigh <<= bitcount;
  1038. out:
  1039. return 0;
  1040. }
  1041. asmlinkage long sys_sysinfo(struct sysinfo __user *info)
  1042. {
  1043. struct sysinfo val;
  1044. do_sysinfo(&val);
  1045. if (copy_to_user(info, &val, sizeof(struct sysinfo)))
  1046. return -EFAULT;
  1047. return 0;
  1048. }
  1049. /*
  1050. * lockdep: we want to track each per-CPU base as a separate lock-class,
  1051. * but timer-bases are kmalloc()-ed, so we need to attach separate
  1052. * keys to them:
  1053. */
  1054. static struct lock_class_key base_lock_keys[NR_CPUS];
  1055. static int __devinit init_timers_cpu(int cpu)
  1056. {
  1057. int j;
  1058. tvec_base_t *base;
  1059. static char __devinitdata tvec_base_done[NR_CPUS];
  1060. if (!tvec_base_done[cpu]) {
  1061. static char boot_done;
  1062. if (boot_done) {
  1063. /*
  1064. * The APs use this path later in boot
  1065. */
  1066. base = kmalloc_node(sizeof(*base),
  1067. GFP_KERNEL | __GFP_ZERO,
  1068. cpu_to_node(cpu));
  1069. if (!base)
  1070. return -ENOMEM;
  1071. /* Make sure that tvec_base is 2 byte aligned */
  1072. if (tbase_get_deferrable(base)) {
  1073. WARN_ON(1);
  1074. kfree(base);
  1075. return -ENOMEM;
  1076. }
  1077. per_cpu(tvec_bases, cpu) = base;
  1078. } else {
  1079. /*
  1080. * This is for the boot CPU - we use compile-time
  1081. * static initialisation because per-cpu memory isn't
  1082. * ready yet and because the memory allocators are not
  1083. * initialised either.
  1084. */
  1085. boot_done = 1;
  1086. base = &boot_tvec_bases;
  1087. }
  1088. tvec_base_done[cpu] = 1;
  1089. } else {
  1090. base = per_cpu(tvec_bases, cpu);
  1091. }
  1092. spin_lock_init(&base->lock);
  1093. lockdep_set_class(&base->lock, base_lock_keys + cpu);
  1094. for (j = 0; j < TVN_SIZE; j++) {
  1095. INIT_LIST_HEAD(base->tv5.vec + j);
  1096. INIT_LIST_HEAD(base->tv4.vec + j);
  1097. INIT_LIST_HEAD(base->tv3.vec + j);
  1098. INIT_LIST_HEAD(base->tv2.vec + j);
  1099. }
  1100. for (j = 0; j < TVR_SIZE; j++)
  1101. INIT_LIST_HEAD(base->tv1.vec + j);
  1102. base->timer_jiffies = jiffies;
  1103. return 0;
  1104. }
  1105. #ifdef CONFIG_HOTPLUG_CPU
  1106. static void migrate_timer_list(tvec_base_t *new_base, struct list_head *head)
  1107. {
  1108. struct timer_list *timer;
  1109. while (!list_empty(head)) {
  1110. timer = list_first_entry(head, struct timer_list, entry);
  1111. detach_timer(timer, 0);
  1112. timer_set_base(timer, new_base);
  1113. internal_add_timer(new_base, timer);
  1114. }
  1115. }
  1116. static void __devinit migrate_timers(int cpu)
  1117. {
  1118. tvec_base_t *old_base;
  1119. tvec_base_t *new_base;
  1120. int i;
  1121. BUG_ON(cpu_online(cpu));
  1122. old_base = per_cpu(tvec_bases, cpu);
  1123. new_base = get_cpu_var(tvec_bases);
  1124. local_irq_disable();
  1125. double_spin_lock(&new_base->lock, &old_base->lock,
  1126. smp_processor_id() < cpu);
  1127. BUG_ON(old_base->running_timer);
  1128. for (i = 0; i < TVR_SIZE; i++)
  1129. migrate_timer_list(new_base, old_base->tv1.vec + i);
  1130. for (i = 0; i < TVN_SIZE; i++) {
  1131. migrate_timer_list(new_base, old_base->tv2.vec + i);
  1132. migrate_timer_list(new_base, old_base->tv3.vec + i);
  1133. migrate_timer_list(new_base, old_base->tv4.vec + i);
  1134. migrate_timer_list(new_base, old_base->tv5.vec + i);
  1135. }
  1136. double_spin_unlock(&new_base->lock, &old_base->lock,
  1137. smp_processor_id() < cpu);
  1138. local_irq_enable();
  1139. put_cpu_var(tvec_bases);
  1140. }
  1141. #endif /* CONFIG_HOTPLUG_CPU */
  1142. static int __cpuinit timer_cpu_notify(struct notifier_block *self,
  1143. unsigned long action, void *hcpu)
  1144. {
  1145. long cpu = (long)hcpu;
  1146. switch(action) {
  1147. case CPU_UP_PREPARE:
  1148. case CPU_UP_PREPARE_FROZEN:
  1149. if (init_timers_cpu(cpu) < 0)
  1150. return NOTIFY_BAD;
  1151. break;
  1152. #ifdef CONFIG_HOTPLUG_CPU
  1153. case CPU_DEAD:
  1154. case CPU_DEAD_FROZEN:
  1155. migrate_timers(cpu);
  1156. break;
  1157. #endif
  1158. default:
  1159. break;
  1160. }
  1161. return NOTIFY_OK;
  1162. }
  1163. static struct notifier_block __cpuinitdata timers_nb = {
  1164. .notifier_call = timer_cpu_notify,
  1165. };
  1166. void __init init_timers(void)
  1167. {
  1168. int err = timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
  1169. (void *)(long)smp_processor_id());
  1170. init_timer_stats();
  1171. BUG_ON(err == NOTIFY_BAD);
  1172. register_cpu_notifier(&timers_nb);
  1173. open_softirq(TIMER_SOFTIRQ, run_timer_softirq, NULL);
  1174. }
  1175. #ifdef CONFIG_TIME_INTERPOLATION
  1176. struct time_interpolator *time_interpolator __read_mostly;
  1177. static struct time_interpolator *time_interpolator_list __read_mostly;
  1178. static DEFINE_SPINLOCK(time_interpolator_lock);
  1179. static inline cycles_t time_interpolator_get_cycles(unsigned int src)
  1180. {
  1181. unsigned long (*x)(void);
  1182. switch (src)
  1183. {
  1184. case TIME_SOURCE_FUNCTION:
  1185. x = time_interpolator->addr;
  1186. return x();
  1187. case TIME_SOURCE_MMIO64 :
  1188. return readq_relaxed((void __iomem *)time_interpolator->addr);
  1189. case TIME_SOURCE_MMIO32 :
  1190. return readl_relaxed((void __iomem *)time_interpolator->addr);
  1191. default: return get_cycles();
  1192. }
  1193. }
  1194. static inline u64 time_interpolator_get_counter(int writelock)
  1195. {
  1196. unsigned int src = time_interpolator->source;
  1197. if (time_interpolator->jitter)
  1198. {
  1199. cycles_t lcycle;
  1200. cycles_t now;
  1201. do {
  1202. lcycle = time_interpolator->last_cycle;
  1203. now = time_interpolator_get_cycles(src);
  1204. if (lcycle && time_after(lcycle, now))
  1205. return lcycle;
  1206. /* When holding the xtime write lock, there's no need
  1207. * to add the overhead of the cmpxchg. Readers are
  1208. * force to retry until the write lock is released.
  1209. */
  1210. if (writelock) {
  1211. time_interpolator->last_cycle = now;
  1212. return now;
  1213. }
  1214. /* Keep track of the last timer value returned. The use of cmpxchg here
  1215. * will cause contention in an SMP environment.
  1216. */
  1217. } while (unlikely(cmpxchg(&time_interpolator->last_cycle, lcycle, now) != lcycle));
  1218. return now;
  1219. }
  1220. else
  1221. return time_interpolator_get_cycles(src);
  1222. }
  1223. void time_interpolator_reset(void)
  1224. {
  1225. time_interpolator->offset = 0;
  1226. time_interpolator->last_counter = time_interpolator_get_counter(1);
  1227. }
  1228. #define GET_TI_NSECS(count,i) (((((count) - i->last_counter) & (i)->mask) * (i)->nsec_per_cyc) >> (i)->shift)
  1229. unsigned long time_interpolator_get_offset(void)
  1230. {
  1231. /* If we do not have a time interpolator set up then just return zero */
  1232. if (!time_interpolator)
  1233. return 0;
  1234. return time_interpolator->offset +
  1235. GET_TI_NSECS(time_interpolator_get_counter(0), time_interpolator);
  1236. }
  1237. #define INTERPOLATOR_ADJUST 65536
  1238. #define INTERPOLATOR_MAX_SKIP 10*INTERPOLATOR_ADJUST
  1239. void time_interpolator_update(long delta_nsec)
  1240. {
  1241. u64 counter;
  1242. unsigned long offset;
  1243. /* If there is no time interpolator set up then do nothing */
  1244. if (!time_interpolator)
  1245. return;
  1246. /*
  1247. * The interpolator compensates for late ticks by accumulating the late
  1248. * time in time_interpolator->offset. A tick earlier than expected will
  1249. * lead to a reset of the offset and a corresponding jump of the clock
  1250. * forward. Again this only works if the interpolator clock is running
  1251. * slightly slower than the regular clock and the tuning logic insures
  1252. * that.
  1253. */
  1254. counter = time_interpolator_get_counter(1);
  1255. offset = time_interpolator->offset +
  1256. GET_TI_NSECS(counter, time_interpolator);
  1257. if (delta_nsec < 0 || (unsigned long) delta_nsec < offset)
  1258. time_interpolator->offset = offset - delta_nsec;
  1259. else {
  1260. time_interpolator->skips++;
  1261. time_interpolator->ns_skipped += delta_nsec - offset;
  1262. time_interpolator->offset = 0;
  1263. }
  1264. time_interpolator->last_counter = counter;
  1265. /* Tuning logic for time interpolator invoked every minute or so.
  1266. * Decrease interpolator clock speed if no skips occurred and an offset is carried.
  1267. * Increase interpolator clock speed if we skip too much time.
  1268. */
  1269. if (jiffies % INTERPOLATOR_ADJUST == 0)
  1270. {
  1271. if (time_interpolator->skips == 0 && time_interpolator->offset > tick_nsec)
  1272. time_interpolator->nsec_per_cyc--;
  1273. if (time_interpolator->ns_skipped > INTERPOLATOR_MAX_SKIP && time_interpolator->offset == 0)
  1274. time_interpolator->nsec_per_cyc++;
  1275. time_interpolator->skips = 0;
  1276. time_interpolator->ns_skipped = 0;
  1277. }
  1278. }
  1279. static inline int
  1280. is_better_time_interpolator(struct time_interpolator *new)
  1281. {
  1282. if (!time_interpolator)
  1283. return 1;
  1284. return new->frequency > 2*time_interpolator->frequency ||
  1285. (unsigned long)new->drift < (unsigned long)time_interpolator->drift;
  1286. }
  1287. void
  1288. register_time_interpolator(struct time_interpolator *ti)
  1289. {
  1290. unsigned long flags;
  1291. /* Sanity check */
  1292. BUG_ON(ti->frequency == 0 || ti->mask == 0);
  1293. ti->nsec_per_cyc = ((u64)NSEC_PER_SEC << ti->shift) / ti->frequency;
  1294. spin_lock(&time_interpolator_lock);
  1295. write_seqlock_irqsave(&xtime_lock, flags);
  1296. if (is_better_time_interpolator(ti)) {
  1297. time_interpolator = ti;
  1298. time_interpolator_reset();
  1299. }
  1300. write_sequnlock_irqrestore(&xtime_lock, flags);
  1301. ti->next = time_interpolator_list;
  1302. time_interpolator_list = ti;
  1303. spin_unlock(&time_interpolator_lock);
  1304. }
  1305. void
  1306. unregister_time_interpolator(struct time_interpolator *ti)
  1307. {
  1308. struct time_interpolator *curr, **prev;
  1309. unsigned long flags;
  1310. spin_lock(&time_interpolator_lock);
  1311. prev = &time_interpolator_list;
  1312. for (curr = *prev; curr; curr = curr->next) {
  1313. if (curr == ti) {
  1314. *prev = curr->next;
  1315. break;
  1316. }
  1317. prev = &curr->next;
  1318. }
  1319. write_seqlock_irqsave(&xtime_lock, flags);
  1320. if (ti == time_interpolator) {
  1321. /* we lost the best time-interpolator: */
  1322. time_interpolator = NULL;
  1323. /* find the next-best interpolator */
  1324. for (curr = time_interpolator_list; curr; curr = curr->next)
  1325. if (is_better_time_interpolator(curr))
  1326. time_interpolator = curr;
  1327. time_interpolator_reset();
  1328. }
  1329. write_sequnlock_irqrestore(&xtime_lock, flags);
  1330. spin_unlock(&time_interpolator_lock);
  1331. }
  1332. #endif /* CONFIG_TIME_INTERPOLATION */
  1333. /**
  1334. * msleep - sleep safely even with waitqueue interruptions
  1335. * @msecs: Time in milliseconds to sleep for
  1336. */
  1337. void msleep(unsigned int msecs)
  1338. {
  1339. unsigned long timeout = msecs_to_jiffies(msecs) + 1;
  1340. while (timeout)
  1341. timeout = schedule_timeout_uninterruptible(timeout);
  1342. }
  1343. EXPORT_SYMBOL(msleep);
  1344. /**
  1345. * msleep_interruptible - sleep waiting for signals
  1346. * @msecs: Time in milliseconds to sleep for
  1347. */
  1348. unsigned long msleep_interruptible(unsigned int msecs)
  1349. {
  1350. unsigned long timeout = msecs_to_jiffies(msecs) + 1;
  1351. while (timeout && !signal_pending(current))
  1352. timeout = schedule_timeout_interruptible(timeout);
  1353. return jiffies_to_msecs(timeout);
  1354. }
  1355. EXPORT_SYMBOL(msleep_interruptible);