timer.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546
  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. #endif
  279. /**
  280. * init_timer - initialize a timer.
  281. * @timer: the timer to be initialized
  282. *
  283. * init_timer() must be done to a timer prior calling *any* of the
  284. * other timer functions.
  285. */
  286. void fastcall init_timer(struct timer_list *timer)
  287. {
  288. timer->entry.next = NULL;
  289. timer->base = __raw_get_cpu_var(tvec_bases);
  290. #ifdef CONFIG_TIMER_STATS
  291. timer->start_site = NULL;
  292. timer->start_pid = -1;
  293. memset(timer->start_comm, 0, TASK_COMM_LEN);
  294. #endif
  295. }
  296. EXPORT_SYMBOL(init_timer);
  297. void fastcall init_timer_deferrable(struct timer_list *timer)
  298. {
  299. init_timer(timer);
  300. timer_set_deferrable(timer);
  301. }
  302. EXPORT_SYMBOL(init_timer_deferrable);
  303. static inline void detach_timer(struct timer_list *timer,
  304. int clear_pending)
  305. {
  306. struct list_head *entry = &timer->entry;
  307. __list_del(entry->prev, entry->next);
  308. if (clear_pending)
  309. entry->next = NULL;
  310. entry->prev = LIST_POISON2;
  311. }
  312. /*
  313. * We are using hashed locking: holding per_cpu(tvec_bases).lock
  314. * means that all timers which are tied to this base via timer->base are
  315. * locked, and the base itself is locked too.
  316. *
  317. * So __run_timers/migrate_timers can safely modify all timers which could
  318. * be found on ->tvX lists.
  319. *
  320. * When the timer's base is locked, and the timer removed from list, it is
  321. * possible to set timer->base = NULL and drop the lock: the timer remains
  322. * locked.
  323. */
  324. static tvec_base_t *lock_timer_base(struct timer_list *timer,
  325. unsigned long *flags)
  326. __acquires(timer->base->lock)
  327. {
  328. tvec_base_t *base;
  329. for (;;) {
  330. tvec_base_t *prelock_base = timer->base;
  331. base = tbase_get_base(prelock_base);
  332. if (likely(base != NULL)) {
  333. spin_lock_irqsave(&base->lock, *flags);
  334. if (likely(prelock_base == timer->base))
  335. return base;
  336. /* The timer has migrated to another CPU */
  337. spin_unlock_irqrestore(&base->lock, *flags);
  338. }
  339. cpu_relax();
  340. }
  341. }
  342. int __mod_timer(struct timer_list *timer, unsigned long expires)
  343. {
  344. tvec_base_t *base, *new_base;
  345. unsigned long flags;
  346. int ret = 0;
  347. timer_stats_timer_set_start_info(timer);
  348. BUG_ON(!timer->function);
  349. base = lock_timer_base(timer, &flags);
  350. if (timer_pending(timer)) {
  351. detach_timer(timer, 0);
  352. ret = 1;
  353. }
  354. new_base = __get_cpu_var(tvec_bases);
  355. if (base != new_base) {
  356. /*
  357. * We are trying to schedule the timer on the local CPU.
  358. * However we can't change timer's base while it is running,
  359. * otherwise del_timer_sync() can't detect that the timer's
  360. * handler yet has not finished. This also guarantees that
  361. * the timer is serialized wrt itself.
  362. */
  363. if (likely(base->running_timer != timer)) {
  364. /* See the comment in lock_timer_base() */
  365. timer_set_base(timer, NULL);
  366. spin_unlock(&base->lock);
  367. base = new_base;
  368. spin_lock(&base->lock);
  369. timer_set_base(timer, base);
  370. }
  371. }
  372. timer->expires = expires;
  373. internal_add_timer(base, timer);
  374. spin_unlock_irqrestore(&base->lock, flags);
  375. return ret;
  376. }
  377. EXPORT_SYMBOL(__mod_timer);
  378. /**
  379. * add_timer_on - start a timer on a particular CPU
  380. * @timer: the timer to be added
  381. * @cpu: the CPU to start it on
  382. *
  383. * This is not very scalable on SMP. Double adds are not possible.
  384. */
  385. void add_timer_on(struct timer_list *timer, int cpu)
  386. {
  387. tvec_base_t *base = per_cpu(tvec_bases, cpu);
  388. unsigned long flags;
  389. timer_stats_timer_set_start_info(timer);
  390. BUG_ON(timer_pending(timer) || !timer->function);
  391. spin_lock_irqsave(&base->lock, flags);
  392. timer_set_base(timer, base);
  393. internal_add_timer(base, timer);
  394. spin_unlock_irqrestore(&base->lock, flags);
  395. }
  396. /**
  397. * mod_timer - modify a timer's timeout
  398. * @timer: the timer to be modified
  399. * @expires: new timeout in jiffies
  400. *
  401. * mod_timer() is a more efficient way to update the expire field of an
  402. * active timer (if the timer is inactive it will be activated)
  403. *
  404. * mod_timer(timer, expires) is equivalent to:
  405. *
  406. * del_timer(timer); timer->expires = expires; add_timer(timer);
  407. *
  408. * Note that if there are multiple unserialized concurrent users of the
  409. * same timer, then mod_timer() is the only safe way to modify the timeout,
  410. * since add_timer() cannot modify an already running timer.
  411. *
  412. * The function returns whether it has modified a pending timer or not.
  413. * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
  414. * active timer returns 1.)
  415. */
  416. int mod_timer(struct timer_list *timer, unsigned long expires)
  417. {
  418. BUG_ON(!timer->function);
  419. timer_stats_timer_set_start_info(timer);
  420. /*
  421. * This is a common optimization triggered by the
  422. * networking code - if the timer is re-modified
  423. * to be the same thing then just return:
  424. */
  425. if (timer->expires == expires && timer_pending(timer))
  426. return 1;
  427. return __mod_timer(timer, expires);
  428. }
  429. EXPORT_SYMBOL(mod_timer);
  430. /**
  431. * del_timer - deactive a timer.
  432. * @timer: the timer to be deactivated
  433. *
  434. * del_timer() deactivates a timer - this works on both active and inactive
  435. * timers.
  436. *
  437. * The function returns whether it has deactivated a pending timer or not.
  438. * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
  439. * active timer returns 1.)
  440. */
  441. int del_timer(struct timer_list *timer)
  442. {
  443. tvec_base_t *base;
  444. unsigned long flags;
  445. int ret = 0;
  446. timer_stats_timer_clear_start_info(timer);
  447. if (timer_pending(timer)) {
  448. base = lock_timer_base(timer, &flags);
  449. if (timer_pending(timer)) {
  450. detach_timer(timer, 1);
  451. ret = 1;
  452. }
  453. spin_unlock_irqrestore(&base->lock, flags);
  454. }
  455. return ret;
  456. }
  457. EXPORT_SYMBOL(del_timer);
  458. #ifdef CONFIG_SMP
  459. /**
  460. * try_to_del_timer_sync - Try to deactivate a timer
  461. * @timer: timer do del
  462. *
  463. * This function tries to deactivate a timer. Upon successful (ret >= 0)
  464. * exit the timer is not queued and the handler is not running on any CPU.
  465. *
  466. * It must not be called from interrupt contexts.
  467. */
  468. int try_to_del_timer_sync(struct timer_list *timer)
  469. {
  470. tvec_base_t *base;
  471. unsigned long flags;
  472. int ret = -1;
  473. base = lock_timer_base(timer, &flags);
  474. if (base->running_timer == timer)
  475. goto out;
  476. ret = 0;
  477. if (timer_pending(timer)) {
  478. detach_timer(timer, 1);
  479. ret = 1;
  480. }
  481. out:
  482. spin_unlock_irqrestore(&base->lock, flags);
  483. return ret;
  484. }
  485. EXPORT_SYMBOL(try_to_del_timer_sync);
  486. /**
  487. * del_timer_sync - deactivate a timer and wait for the handler to finish.
  488. * @timer: the timer to be deactivated
  489. *
  490. * This function only differs from del_timer() on SMP: besides deactivating
  491. * the timer it also makes sure the handler has finished executing on other
  492. * CPUs.
  493. *
  494. * Synchronization rules: Callers must prevent restarting of the timer,
  495. * otherwise this function is meaningless. It must not be called from
  496. * interrupt contexts. The caller must not hold locks which would prevent
  497. * completion of the timer's handler. The timer's handler must not call
  498. * add_timer_on(). Upon exit the timer is not queued and the handler is
  499. * not running on any CPU.
  500. *
  501. * The function returns whether it has deactivated a pending timer or not.
  502. */
  503. int del_timer_sync(struct timer_list *timer)
  504. {
  505. for (;;) {
  506. int ret = try_to_del_timer_sync(timer);
  507. if (ret >= 0)
  508. return ret;
  509. cpu_relax();
  510. }
  511. }
  512. EXPORT_SYMBOL(del_timer_sync);
  513. #endif
  514. static int cascade(tvec_base_t *base, tvec_t *tv, int index)
  515. {
  516. /* cascade all the timers from tv up one level */
  517. struct timer_list *timer, *tmp;
  518. struct list_head tv_list;
  519. list_replace_init(tv->vec + index, &tv_list);
  520. /*
  521. * We are removing _all_ timers from the list, so we
  522. * don't have to detach them individually.
  523. */
  524. list_for_each_entry_safe(timer, tmp, &tv_list, entry) {
  525. BUG_ON(tbase_get_base(timer->base) != base);
  526. internal_add_timer(base, timer);
  527. }
  528. return index;
  529. }
  530. #define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK)
  531. /**
  532. * __run_timers - run all expired timers (if any) on this CPU.
  533. * @base: the timer vector to be processed.
  534. *
  535. * This function cascades all vectors and executes all expired timer
  536. * vectors.
  537. */
  538. static inline void __run_timers(tvec_base_t *base)
  539. {
  540. struct timer_list *timer;
  541. spin_lock_irq(&base->lock);
  542. while (time_after_eq(jiffies, base->timer_jiffies)) {
  543. struct list_head work_list;
  544. struct list_head *head = &work_list;
  545. int index = base->timer_jiffies & TVR_MASK;
  546. /*
  547. * Cascade timers:
  548. */
  549. if (!index &&
  550. (!cascade(base, &base->tv2, INDEX(0))) &&
  551. (!cascade(base, &base->tv3, INDEX(1))) &&
  552. !cascade(base, &base->tv4, INDEX(2)))
  553. cascade(base, &base->tv5, INDEX(3));
  554. ++base->timer_jiffies;
  555. list_replace_init(base->tv1.vec + index, &work_list);
  556. while (!list_empty(head)) {
  557. void (*fn)(unsigned long);
  558. unsigned long data;
  559. timer = list_first_entry(head, struct timer_list,entry);
  560. fn = timer->function;
  561. data = timer->data;
  562. timer_stats_account_timer(timer);
  563. set_running_timer(base, timer);
  564. detach_timer(timer, 1);
  565. spin_unlock_irq(&base->lock);
  566. {
  567. int preempt_count = preempt_count();
  568. fn(data);
  569. if (preempt_count != preempt_count()) {
  570. printk(KERN_WARNING "huh, entered %p "
  571. "with preempt_count %08x, exited"
  572. " with %08x?\n",
  573. fn, preempt_count,
  574. preempt_count());
  575. BUG();
  576. }
  577. }
  578. spin_lock_irq(&base->lock);
  579. }
  580. }
  581. set_running_timer(base, NULL);
  582. spin_unlock_irq(&base->lock);
  583. }
  584. #if defined(CONFIG_NO_IDLE_HZ) || defined(CONFIG_NO_HZ)
  585. /*
  586. * Find out when the next timer event is due to happen. This
  587. * is used on S/390 to stop all activity when a cpus is idle.
  588. * This functions needs to be called disabled.
  589. */
  590. static unsigned long __next_timer_interrupt(tvec_base_t *base)
  591. {
  592. unsigned long timer_jiffies = base->timer_jiffies;
  593. unsigned long expires = timer_jiffies + (LONG_MAX >> 1);
  594. int index, slot, array, found = 0;
  595. struct timer_list *nte;
  596. tvec_t *varray[4];
  597. /* Look for timer events in tv1. */
  598. index = slot = timer_jiffies & TVR_MASK;
  599. do {
  600. list_for_each_entry(nte, base->tv1.vec + slot, entry) {
  601. if (tbase_get_deferrable(nte->base))
  602. continue;
  603. found = 1;
  604. expires = nte->expires;
  605. /* Look at the cascade bucket(s)? */
  606. if (!index || slot < index)
  607. goto cascade;
  608. return expires;
  609. }
  610. slot = (slot + 1) & TVR_MASK;
  611. } while (slot != index);
  612. cascade:
  613. /* Calculate the next cascade event */
  614. if (index)
  615. timer_jiffies += TVR_SIZE - index;
  616. timer_jiffies >>= TVR_BITS;
  617. /* Check tv2-tv5. */
  618. varray[0] = &base->tv2;
  619. varray[1] = &base->tv3;
  620. varray[2] = &base->tv4;
  621. varray[3] = &base->tv5;
  622. for (array = 0; array < 4; array++) {
  623. tvec_t *varp = varray[array];
  624. index = slot = timer_jiffies & TVN_MASK;
  625. do {
  626. list_for_each_entry(nte, varp->vec + slot, entry) {
  627. found = 1;
  628. if (time_before(nte->expires, expires))
  629. expires = nte->expires;
  630. }
  631. /*
  632. * Do we still search for the first timer or are
  633. * we looking up the cascade buckets ?
  634. */
  635. if (found) {
  636. /* Look at the cascade bucket(s)? */
  637. if (!index || slot < index)
  638. break;
  639. return expires;
  640. }
  641. slot = (slot + 1) & TVN_MASK;
  642. } while (slot != index);
  643. if (index)
  644. timer_jiffies += TVN_SIZE - index;
  645. timer_jiffies >>= TVN_BITS;
  646. }
  647. return expires;
  648. }
  649. /*
  650. * Check, if the next hrtimer event is before the next timer wheel
  651. * event:
  652. */
  653. static unsigned long cmp_next_hrtimer_event(unsigned long now,
  654. unsigned long expires)
  655. {
  656. ktime_t hr_delta = hrtimer_get_next_event();
  657. struct timespec tsdelta;
  658. unsigned long delta;
  659. if (hr_delta.tv64 == KTIME_MAX)
  660. return expires;
  661. /*
  662. * Expired timer available, let it expire in the next tick
  663. */
  664. if (hr_delta.tv64 <= 0)
  665. return now + 1;
  666. tsdelta = ktime_to_timespec(hr_delta);
  667. delta = timespec_to_jiffies(&tsdelta);
  668. /*
  669. * Take rounding errors in to account and make sure, that it
  670. * expires in the next tick. Otherwise we go into an endless
  671. * ping pong due to tick_nohz_stop_sched_tick() retriggering
  672. * the timer softirq
  673. */
  674. if (delta < 1)
  675. delta = 1;
  676. now += delta;
  677. if (time_before(now, expires))
  678. return now;
  679. return expires;
  680. }
  681. /**
  682. * next_timer_interrupt - return the jiffy of the next pending timer
  683. * @now: current time (in jiffies)
  684. */
  685. unsigned long get_next_timer_interrupt(unsigned long now)
  686. {
  687. tvec_base_t *base = __get_cpu_var(tvec_bases);
  688. unsigned long expires;
  689. spin_lock(&base->lock);
  690. expires = __next_timer_interrupt(base);
  691. spin_unlock(&base->lock);
  692. if (time_before_eq(expires, now))
  693. return now;
  694. return cmp_next_hrtimer_event(now, expires);
  695. }
  696. #ifdef CONFIG_NO_IDLE_HZ
  697. unsigned long next_timer_interrupt(void)
  698. {
  699. return get_next_timer_interrupt(jiffies);
  700. }
  701. #endif
  702. #endif
  703. /*
  704. * Called from the timer interrupt handler to charge one tick to the current
  705. * process. user_tick is 1 if the tick is user time, 0 for system.
  706. */
  707. void update_process_times(int user_tick)
  708. {
  709. struct task_struct *p = current;
  710. int cpu = smp_processor_id();
  711. /* Note: this timer irq context must be accounted for as well. */
  712. if (user_tick)
  713. account_user_time(p, jiffies_to_cputime(1));
  714. else
  715. account_system_time(p, HARDIRQ_OFFSET, jiffies_to_cputime(1));
  716. run_local_timers();
  717. if (rcu_pending(cpu))
  718. rcu_check_callbacks(cpu, user_tick);
  719. scheduler_tick();
  720. run_posix_cpu_timers(p);
  721. }
  722. /*
  723. * Nr of active tasks - counted in fixed-point numbers
  724. */
  725. static unsigned long count_active_tasks(void)
  726. {
  727. return nr_active() * FIXED_1;
  728. }
  729. /*
  730. * Hmm.. Changed this, as the GNU make sources (load.c) seems to
  731. * imply that avenrun[] is the standard name for this kind of thing.
  732. * Nothing else seems to be standardized: the fractional size etc
  733. * all seem to differ on different machines.
  734. *
  735. * Requires xtime_lock to access.
  736. */
  737. unsigned long avenrun[3];
  738. EXPORT_SYMBOL(avenrun);
  739. /*
  740. * calc_load - given tick count, update the avenrun load estimates.
  741. * This is called while holding a write_lock on xtime_lock.
  742. */
  743. static inline void calc_load(unsigned long ticks)
  744. {
  745. unsigned long active_tasks; /* fixed-point */
  746. static int count = LOAD_FREQ;
  747. count -= ticks;
  748. if (unlikely(count < 0)) {
  749. active_tasks = count_active_tasks();
  750. do {
  751. CALC_LOAD(avenrun[0], EXP_1, active_tasks);
  752. CALC_LOAD(avenrun[1], EXP_5, active_tasks);
  753. CALC_LOAD(avenrun[2], EXP_15, active_tasks);
  754. count += LOAD_FREQ;
  755. } while (count < 0);
  756. }
  757. }
  758. /*
  759. * This function runs timers and the timer-tq in bottom half context.
  760. */
  761. static void run_timer_softirq(struct softirq_action *h)
  762. {
  763. tvec_base_t *base = __get_cpu_var(tvec_bases);
  764. hrtimer_run_queues();
  765. if (time_after_eq(jiffies, base->timer_jiffies))
  766. __run_timers(base);
  767. }
  768. /*
  769. * Called by the local, per-CPU timer interrupt on SMP.
  770. */
  771. void run_local_timers(void)
  772. {
  773. raise_softirq(TIMER_SOFTIRQ);
  774. softlockup_tick();
  775. }
  776. /*
  777. * Called by the timer interrupt. xtime_lock must already be taken
  778. * by the timer IRQ!
  779. */
  780. static inline void update_times(unsigned long ticks)
  781. {
  782. update_wall_time();
  783. calc_load(ticks);
  784. }
  785. /*
  786. * The 64-bit jiffies value is not atomic - you MUST NOT read it
  787. * without sampling the sequence number in xtime_lock.
  788. * jiffies is defined in the linker script...
  789. */
  790. void do_timer(unsigned long ticks)
  791. {
  792. jiffies_64 += ticks;
  793. update_times(ticks);
  794. }
  795. #ifdef __ARCH_WANT_SYS_ALARM
  796. /*
  797. * For backwards compatibility? This can be done in libc so Alpha
  798. * and all newer ports shouldn't need it.
  799. */
  800. asmlinkage unsigned long sys_alarm(unsigned int seconds)
  801. {
  802. return alarm_setitimer(seconds);
  803. }
  804. #endif
  805. #ifndef __alpha__
  806. /*
  807. * The Alpha uses getxpid, getxuid, and getxgid instead. Maybe this
  808. * should be moved into arch/i386 instead?
  809. */
  810. /**
  811. * sys_getpid - return the thread group id of the current process
  812. *
  813. * Note, despite the name, this returns the tgid not the pid. The tgid and
  814. * the pid are identical unless CLONE_THREAD was specified on clone() in
  815. * which case the tgid is the same in all threads of the same group.
  816. *
  817. * This is SMP safe as current->tgid does not change.
  818. */
  819. asmlinkage long sys_getpid(void)
  820. {
  821. return current->tgid;
  822. }
  823. /*
  824. * Accessing ->real_parent is not SMP-safe, it could
  825. * change from under us. However, we can use a stale
  826. * value of ->real_parent under rcu_read_lock(), see
  827. * release_task()->call_rcu(delayed_put_task_struct).
  828. */
  829. asmlinkage long sys_getppid(void)
  830. {
  831. int pid;
  832. rcu_read_lock();
  833. pid = rcu_dereference(current->real_parent)->tgid;
  834. rcu_read_unlock();
  835. return pid;
  836. }
  837. asmlinkage long sys_getuid(void)
  838. {
  839. /* Only we change this so SMP safe */
  840. return current->uid;
  841. }
  842. asmlinkage long sys_geteuid(void)
  843. {
  844. /* Only we change this so SMP safe */
  845. return current->euid;
  846. }
  847. asmlinkage long sys_getgid(void)
  848. {
  849. /* Only we change this so SMP safe */
  850. return current->gid;
  851. }
  852. asmlinkage long sys_getegid(void)
  853. {
  854. /* Only we change this so SMP safe */
  855. return current->egid;
  856. }
  857. #endif
  858. static void process_timeout(unsigned long __data)
  859. {
  860. wake_up_process((struct task_struct *)__data);
  861. }
  862. /**
  863. * schedule_timeout - sleep until timeout
  864. * @timeout: timeout value in jiffies
  865. *
  866. * Make the current task sleep until @timeout jiffies have
  867. * elapsed. The routine will return immediately unless
  868. * the current task state has been set (see set_current_state()).
  869. *
  870. * You can set the task state as follows -
  871. *
  872. * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
  873. * pass before the routine returns. The routine will return 0
  874. *
  875. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  876. * delivered to the current task. In this case the remaining time
  877. * in jiffies will be returned, or 0 if the timer expired in time
  878. *
  879. * The current task state is guaranteed to be TASK_RUNNING when this
  880. * routine returns.
  881. *
  882. * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
  883. * the CPU away without a bound on the timeout. In this case the return
  884. * value will be %MAX_SCHEDULE_TIMEOUT.
  885. *
  886. * In all cases the return value is guaranteed to be non-negative.
  887. */
  888. fastcall signed long __sched schedule_timeout(signed long timeout)
  889. {
  890. struct timer_list timer;
  891. unsigned long expire;
  892. switch (timeout)
  893. {
  894. case MAX_SCHEDULE_TIMEOUT:
  895. /*
  896. * These two special cases are useful to be comfortable
  897. * in the caller. Nothing more. We could take
  898. * MAX_SCHEDULE_TIMEOUT from one of the negative value
  899. * but I' d like to return a valid offset (>=0) to allow
  900. * the caller to do everything it want with the retval.
  901. */
  902. schedule();
  903. goto out;
  904. default:
  905. /*
  906. * Another bit of PARANOID. Note that the retval will be
  907. * 0 since no piece of kernel is supposed to do a check
  908. * for a negative retval of schedule_timeout() (since it
  909. * should never happens anyway). You just have the printk()
  910. * that will tell you if something is gone wrong and where.
  911. */
  912. if (timeout < 0) {
  913. printk(KERN_ERR "schedule_timeout: wrong timeout "
  914. "value %lx\n", timeout);
  915. dump_stack();
  916. current->state = TASK_RUNNING;
  917. goto out;
  918. }
  919. }
  920. expire = timeout + jiffies;
  921. setup_timer(&timer, process_timeout, (unsigned long)current);
  922. __mod_timer(&timer, expire);
  923. schedule();
  924. del_singleshot_timer_sync(&timer);
  925. timeout = expire - jiffies;
  926. out:
  927. return timeout < 0 ? 0 : timeout;
  928. }
  929. EXPORT_SYMBOL(schedule_timeout);
  930. /*
  931. * We can use __set_current_state() here because schedule_timeout() calls
  932. * schedule() unconditionally.
  933. */
  934. signed long __sched schedule_timeout_interruptible(signed long timeout)
  935. {
  936. __set_current_state(TASK_INTERRUPTIBLE);
  937. return schedule_timeout(timeout);
  938. }
  939. EXPORT_SYMBOL(schedule_timeout_interruptible);
  940. signed long __sched schedule_timeout_uninterruptible(signed long timeout)
  941. {
  942. __set_current_state(TASK_UNINTERRUPTIBLE);
  943. return schedule_timeout(timeout);
  944. }
  945. EXPORT_SYMBOL(schedule_timeout_uninterruptible);
  946. /* Thread ID - the internal kernel "pid" */
  947. asmlinkage long sys_gettid(void)
  948. {
  949. return current->pid;
  950. }
  951. /**
  952. * do_sysinfo - fill in sysinfo struct
  953. * @info: pointer to buffer to fill
  954. */
  955. int do_sysinfo(struct sysinfo *info)
  956. {
  957. unsigned long mem_total, sav_total;
  958. unsigned int mem_unit, bitcount;
  959. unsigned long seq;
  960. memset(info, 0, sizeof(struct sysinfo));
  961. do {
  962. struct timespec tp;
  963. seq = read_seqbegin(&xtime_lock);
  964. /*
  965. * This is annoying. The below is the same thing
  966. * posix_get_clock_monotonic() does, but it wants to
  967. * take the lock which we want to cover the loads stuff
  968. * too.
  969. */
  970. getnstimeofday(&tp);
  971. tp.tv_sec += wall_to_monotonic.tv_sec;
  972. tp.tv_nsec += wall_to_monotonic.tv_nsec;
  973. if (tp.tv_nsec - NSEC_PER_SEC >= 0) {
  974. tp.tv_nsec = tp.tv_nsec - NSEC_PER_SEC;
  975. tp.tv_sec++;
  976. }
  977. info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
  978. info->loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT);
  979. info->loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT);
  980. info->loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT);
  981. info->procs = nr_threads;
  982. } while (read_seqretry(&xtime_lock, seq));
  983. si_meminfo(info);
  984. si_swapinfo(info);
  985. /*
  986. * If the sum of all the available memory (i.e. ram + swap)
  987. * is less than can be stored in a 32 bit unsigned long then
  988. * we can be binary compatible with 2.2.x kernels. If not,
  989. * well, in that case 2.2.x was broken anyways...
  990. *
  991. * -Erik Andersen <andersee@debian.org>
  992. */
  993. mem_total = info->totalram + info->totalswap;
  994. if (mem_total < info->totalram || mem_total < info->totalswap)
  995. goto out;
  996. bitcount = 0;
  997. mem_unit = info->mem_unit;
  998. while (mem_unit > 1) {
  999. bitcount++;
  1000. mem_unit >>= 1;
  1001. sav_total = mem_total;
  1002. mem_total <<= 1;
  1003. if (mem_total < sav_total)
  1004. goto out;
  1005. }
  1006. /*
  1007. * If mem_total did not overflow, multiply all memory values by
  1008. * info->mem_unit and set it to 1. This leaves things compatible
  1009. * with 2.2.x, and also retains compatibility with earlier 2.4.x
  1010. * kernels...
  1011. */
  1012. info->mem_unit = 1;
  1013. info->totalram <<= bitcount;
  1014. info->freeram <<= bitcount;
  1015. info->sharedram <<= bitcount;
  1016. info->bufferram <<= bitcount;
  1017. info->totalswap <<= bitcount;
  1018. info->freeswap <<= bitcount;
  1019. info->totalhigh <<= bitcount;
  1020. info->freehigh <<= bitcount;
  1021. out:
  1022. return 0;
  1023. }
  1024. asmlinkage long sys_sysinfo(struct sysinfo __user *info)
  1025. {
  1026. struct sysinfo val;
  1027. do_sysinfo(&val);
  1028. if (copy_to_user(info, &val, sizeof(struct sysinfo)))
  1029. return -EFAULT;
  1030. return 0;
  1031. }
  1032. /*
  1033. * lockdep: we want to track each per-CPU base as a separate lock-class,
  1034. * but timer-bases are kmalloc()-ed, so we need to attach separate
  1035. * keys to them:
  1036. */
  1037. static struct lock_class_key base_lock_keys[NR_CPUS];
  1038. static int __devinit init_timers_cpu(int cpu)
  1039. {
  1040. int j;
  1041. tvec_base_t *base;
  1042. static char __devinitdata tvec_base_done[NR_CPUS];
  1043. if (!tvec_base_done[cpu]) {
  1044. static char boot_done;
  1045. if (boot_done) {
  1046. /*
  1047. * The APs use this path later in boot
  1048. */
  1049. base = kmalloc_node(sizeof(*base), GFP_KERNEL,
  1050. cpu_to_node(cpu));
  1051. if (!base)
  1052. return -ENOMEM;
  1053. /* Make sure that tvec_base is 2 byte aligned */
  1054. if (tbase_get_deferrable(base)) {
  1055. WARN_ON(1);
  1056. kfree(base);
  1057. return -ENOMEM;
  1058. }
  1059. memset(base, 0, sizeof(*base));
  1060. per_cpu(tvec_bases, cpu) = base;
  1061. } else {
  1062. /*
  1063. * This is for the boot CPU - we use compile-time
  1064. * static initialisation because per-cpu memory isn't
  1065. * ready yet and because the memory allocators are not
  1066. * initialised either.
  1067. */
  1068. boot_done = 1;
  1069. base = &boot_tvec_bases;
  1070. }
  1071. tvec_base_done[cpu] = 1;
  1072. } else {
  1073. base = per_cpu(tvec_bases, cpu);
  1074. }
  1075. spin_lock_init(&base->lock);
  1076. lockdep_set_class(&base->lock, base_lock_keys + cpu);
  1077. for (j = 0; j < TVN_SIZE; j++) {
  1078. INIT_LIST_HEAD(base->tv5.vec + j);
  1079. INIT_LIST_HEAD(base->tv4.vec + j);
  1080. INIT_LIST_HEAD(base->tv3.vec + j);
  1081. INIT_LIST_HEAD(base->tv2.vec + j);
  1082. }
  1083. for (j = 0; j < TVR_SIZE; j++)
  1084. INIT_LIST_HEAD(base->tv1.vec + j);
  1085. base->timer_jiffies = jiffies;
  1086. return 0;
  1087. }
  1088. #ifdef CONFIG_HOTPLUG_CPU
  1089. static void migrate_timer_list(tvec_base_t *new_base, struct list_head *head)
  1090. {
  1091. struct timer_list *timer;
  1092. while (!list_empty(head)) {
  1093. timer = list_first_entry(head, struct timer_list, entry);
  1094. detach_timer(timer, 0);
  1095. timer_set_base(timer, new_base);
  1096. internal_add_timer(new_base, timer);
  1097. }
  1098. }
  1099. static void __devinit migrate_timers(int cpu)
  1100. {
  1101. tvec_base_t *old_base;
  1102. tvec_base_t *new_base;
  1103. int i;
  1104. BUG_ON(cpu_online(cpu));
  1105. old_base = per_cpu(tvec_bases, cpu);
  1106. new_base = get_cpu_var(tvec_bases);
  1107. local_irq_disable();
  1108. double_spin_lock(&new_base->lock, &old_base->lock,
  1109. smp_processor_id() < cpu);
  1110. BUG_ON(old_base->running_timer);
  1111. for (i = 0; i < TVR_SIZE; i++)
  1112. migrate_timer_list(new_base, old_base->tv1.vec + i);
  1113. for (i = 0; i < TVN_SIZE; i++) {
  1114. migrate_timer_list(new_base, old_base->tv2.vec + i);
  1115. migrate_timer_list(new_base, old_base->tv3.vec + i);
  1116. migrate_timer_list(new_base, old_base->tv4.vec + i);
  1117. migrate_timer_list(new_base, old_base->tv5.vec + i);
  1118. }
  1119. double_spin_unlock(&new_base->lock, &old_base->lock,
  1120. smp_processor_id() < cpu);
  1121. local_irq_enable();
  1122. put_cpu_var(tvec_bases);
  1123. }
  1124. #endif /* CONFIG_HOTPLUG_CPU */
  1125. static int __cpuinit timer_cpu_notify(struct notifier_block *self,
  1126. unsigned long action, void *hcpu)
  1127. {
  1128. long cpu = (long)hcpu;
  1129. switch(action) {
  1130. case CPU_UP_PREPARE:
  1131. case CPU_UP_PREPARE_FROZEN:
  1132. if (init_timers_cpu(cpu) < 0)
  1133. return NOTIFY_BAD;
  1134. break;
  1135. #ifdef CONFIG_HOTPLUG_CPU
  1136. case CPU_DEAD:
  1137. case CPU_DEAD_FROZEN:
  1138. migrate_timers(cpu);
  1139. break;
  1140. #endif
  1141. default:
  1142. break;
  1143. }
  1144. return NOTIFY_OK;
  1145. }
  1146. static struct notifier_block __cpuinitdata timers_nb = {
  1147. .notifier_call = timer_cpu_notify,
  1148. };
  1149. void __init init_timers(void)
  1150. {
  1151. int err = timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
  1152. (void *)(long)smp_processor_id());
  1153. init_timer_stats();
  1154. BUG_ON(err == NOTIFY_BAD);
  1155. register_cpu_notifier(&timers_nb);
  1156. open_softirq(TIMER_SOFTIRQ, run_timer_softirq, NULL);
  1157. }
  1158. #ifdef CONFIG_TIME_INTERPOLATION
  1159. struct time_interpolator *time_interpolator __read_mostly;
  1160. static struct time_interpolator *time_interpolator_list __read_mostly;
  1161. static DEFINE_SPINLOCK(time_interpolator_lock);
  1162. static inline cycles_t time_interpolator_get_cycles(unsigned int src)
  1163. {
  1164. unsigned long (*x)(void);
  1165. switch (src)
  1166. {
  1167. case TIME_SOURCE_FUNCTION:
  1168. x = time_interpolator->addr;
  1169. return x();
  1170. case TIME_SOURCE_MMIO64 :
  1171. return readq_relaxed((void __iomem *)time_interpolator->addr);
  1172. case TIME_SOURCE_MMIO32 :
  1173. return readl_relaxed((void __iomem *)time_interpolator->addr);
  1174. default: return get_cycles();
  1175. }
  1176. }
  1177. static inline u64 time_interpolator_get_counter(int writelock)
  1178. {
  1179. unsigned int src = time_interpolator->source;
  1180. if (time_interpolator->jitter)
  1181. {
  1182. cycles_t lcycle;
  1183. cycles_t now;
  1184. do {
  1185. lcycle = time_interpolator->last_cycle;
  1186. now = time_interpolator_get_cycles(src);
  1187. if (lcycle && time_after(lcycle, now))
  1188. return lcycle;
  1189. /* When holding the xtime write lock, there's no need
  1190. * to add the overhead of the cmpxchg. Readers are
  1191. * force to retry until the write lock is released.
  1192. */
  1193. if (writelock) {
  1194. time_interpolator->last_cycle = now;
  1195. return now;
  1196. }
  1197. /* Keep track of the last timer value returned. The use of cmpxchg here
  1198. * will cause contention in an SMP environment.
  1199. */
  1200. } while (unlikely(cmpxchg(&time_interpolator->last_cycle, lcycle, now) != lcycle));
  1201. return now;
  1202. }
  1203. else
  1204. return time_interpolator_get_cycles(src);
  1205. }
  1206. void time_interpolator_reset(void)
  1207. {
  1208. time_interpolator->offset = 0;
  1209. time_interpolator->last_counter = time_interpolator_get_counter(1);
  1210. }
  1211. #define GET_TI_NSECS(count,i) (((((count) - i->last_counter) & (i)->mask) * (i)->nsec_per_cyc) >> (i)->shift)
  1212. unsigned long time_interpolator_get_offset(void)
  1213. {
  1214. /* If we do not have a time interpolator set up then just return zero */
  1215. if (!time_interpolator)
  1216. return 0;
  1217. return time_interpolator->offset +
  1218. GET_TI_NSECS(time_interpolator_get_counter(0), time_interpolator);
  1219. }
  1220. #define INTERPOLATOR_ADJUST 65536
  1221. #define INTERPOLATOR_MAX_SKIP 10*INTERPOLATOR_ADJUST
  1222. void time_interpolator_update(long delta_nsec)
  1223. {
  1224. u64 counter;
  1225. unsigned long offset;
  1226. /* If there is no time interpolator set up then do nothing */
  1227. if (!time_interpolator)
  1228. return;
  1229. /*
  1230. * The interpolator compensates for late ticks by accumulating the late
  1231. * time in time_interpolator->offset. A tick earlier than expected will
  1232. * lead to a reset of the offset and a corresponding jump of the clock
  1233. * forward. Again this only works if the interpolator clock is running
  1234. * slightly slower than the regular clock and the tuning logic insures
  1235. * that.
  1236. */
  1237. counter = time_interpolator_get_counter(1);
  1238. offset = time_interpolator->offset +
  1239. GET_TI_NSECS(counter, time_interpolator);
  1240. if (delta_nsec < 0 || (unsigned long) delta_nsec < offset)
  1241. time_interpolator->offset = offset - delta_nsec;
  1242. else {
  1243. time_interpolator->skips++;
  1244. time_interpolator->ns_skipped += delta_nsec - offset;
  1245. time_interpolator->offset = 0;
  1246. }
  1247. time_interpolator->last_counter = counter;
  1248. /* Tuning logic for time interpolator invoked every minute or so.
  1249. * Decrease interpolator clock speed if no skips occurred and an offset is carried.
  1250. * Increase interpolator clock speed if we skip too much time.
  1251. */
  1252. if (jiffies % INTERPOLATOR_ADJUST == 0)
  1253. {
  1254. if (time_interpolator->skips == 0 && time_interpolator->offset > tick_nsec)
  1255. time_interpolator->nsec_per_cyc--;
  1256. if (time_interpolator->ns_skipped > INTERPOLATOR_MAX_SKIP && time_interpolator->offset == 0)
  1257. time_interpolator->nsec_per_cyc++;
  1258. time_interpolator->skips = 0;
  1259. time_interpolator->ns_skipped = 0;
  1260. }
  1261. }
  1262. static inline int
  1263. is_better_time_interpolator(struct time_interpolator *new)
  1264. {
  1265. if (!time_interpolator)
  1266. return 1;
  1267. return new->frequency > 2*time_interpolator->frequency ||
  1268. (unsigned long)new->drift < (unsigned long)time_interpolator->drift;
  1269. }
  1270. void
  1271. register_time_interpolator(struct time_interpolator *ti)
  1272. {
  1273. unsigned long flags;
  1274. /* Sanity check */
  1275. BUG_ON(ti->frequency == 0 || ti->mask == 0);
  1276. ti->nsec_per_cyc = ((u64)NSEC_PER_SEC << ti->shift) / ti->frequency;
  1277. spin_lock(&time_interpolator_lock);
  1278. write_seqlock_irqsave(&xtime_lock, flags);
  1279. if (is_better_time_interpolator(ti)) {
  1280. time_interpolator = ti;
  1281. time_interpolator_reset();
  1282. }
  1283. write_sequnlock_irqrestore(&xtime_lock, flags);
  1284. ti->next = time_interpolator_list;
  1285. time_interpolator_list = ti;
  1286. spin_unlock(&time_interpolator_lock);
  1287. }
  1288. void
  1289. unregister_time_interpolator(struct time_interpolator *ti)
  1290. {
  1291. struct time_interpolator *curr, **prev;
  1292. unsigned long flags;
  1293. spin_lock(&time_interpolator_lock);
  1294. prev = &time_interpolator_list;
  1295. for (curr = *prev; curr; curr = curr->next) {
  1296. if (curr == ti) {
  1297. *prev = curr->next;
  1298. break;
  1299. }
  1300. prev = &curr->next;
  1301. }
  1302. clocksource_resume();
  1303. write_seqlock_irqsave(&xtime_lock, flags);
  1304. if (ti == time_interpolator) {
  1305. /* we lost the best time-interpolator: */
  1306. time_interpolator = NULL;
  1307. /* find the next-best interpolator */
  1308. for (curr = time_interpolator_list; curr; curr = curr->next)
  1309. if (is_better_time_interpolator(curr))
  1310. time_interpolator = curr;
  1311. time_interpolator_reset();
  1312. }
  1313. write_sequnlock_irqrestore(&xtime_lock, flags);
  1314. spin_unlock(&time_interpolator_lock);
  1315. }
  1316. #endif /* CONFIG_TIME_INTERPOLATION */
  1317. /**
  1318. * msleep - sleep safely even with waitqueue interruptions
  1319. * @msecs: Time in milliseconds to sleep for
  1320. */
  1321. void msleep(unsigned int msecs)
  1322. {
  1323. unsigned long timeout = msecs_to_jiffies(msecs) + 1;
  1324. while (timeout)
  1325. timeout = schedule_timeout_uninterruptible(timeout);
  1326. }
  1327. EXPORT_SYMBOL(msleep);
  1328. /**
  1329. * msleep_interruptible - sleep waiting for signals
  1330. * @msecs: Time in milliseconds to sleep for
  1331. */
  1332. unsigned long msleep_interruptible(unsigned int msecs)
  1333. {
  1334. unsigned long timeout = msecs_to_jiffies(msecs) + 1;
  1335. while (timeout && !signal_pending(current))
  1336. timeout = schedule_timeout_interruptible(timeout);
  1337. return jiffies_to_msecs(timeout);
  1338. }
  1339. EXPORT_SYMBOL(msleep_interruptible);