timer.c 41 KB

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