timer.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579
  1. /*
  2. * linux/kernel/timer.c
  3. *
  4. * Kernel internal timers, kernel timekeeping, 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 <asm/uaccess.h>
  37. #include <asm/unistd.h>
  38. #include <asm/div64.h>
  39. #include <asm/timex.h>
  40. #include <asm/io.h>
  41. #ifdef CONFIG_TIME_INTERPOLATION
  42. static void time_interpolator_update(long delta_nsec);
  43. #else
  44. #define time_interpolator_update(x)
  45. #endif
  46. u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
  47. EXPORT_SYMBOL(jiffies_64);
  48. /*
  49. * per-CPU timer vector definitions:
  50. */
  51. #define TVN_BITS (CONFIG_BASE_SMALL ? 4 : 6)
  52. #define TVR_BITS (CONFIG_BASE_SMALL ? 6 : 8)
  53. #define TVN_SIZE (1 << TVN_BITS)
  54. #define TVR_SIZE (1 << TVR_BITS)
  55. #define TVN_MASK (TVN_SIZE - 1)
  56. #define TVR_MASK (TVR_SIZE - 1)
  57. struct timer_base_s {
  58. spinlock_t lock;
  59. struct timer_list *running_timer;
  60. };
  61. typedef struct tvec_s {
  62. struct list_head vec[TVN_SIZE];
  63. } tvec_t;
  64. typedef struct tvec_root_s {
  65. struct list_head vec[TVR_SIZE];
  66. } tvec_root_t;
  67. struct tvec_t_base_s {
  68. struct timer_base_s t_base;
  69. unsigned long timer_jiffies;
  70. tvec_root_t tv1;
  71. tvec_t tv2;
  72. tvec_t tv3;
  73. tvec_t tv4;
  74. tvec_t tv5;
  75. } ____cacheline_aligned_in_smp;
  76. typedef struct tvec_t_base_s tvec_base_t;
  77. static DEFINE_PER_CPU(tvec_base_t, tvec_bases);
  78. static inline void set_running_timer(tvec_base_t *base,
  79. struct timer_list *timer)
  80. {
  81. #ifdef CONFIG_SMP
  82. base->t_base.running_timer = timer;
  83. #endif
  84. }
  85. static void internal_add_timer(tvec_base_t *base, struct timer_list *timer)
  86. {
  87. unsigned long expires = timer->expires;
  88. unsigned long idx = expires - base->timer_jiffies;
  89. struct list_head *vec;
  90. if (idx < TVR_SIZE) {
  91. int i = expires & TVR_MASK;
  92. vec = base->tv1.vec + i;
  93. } else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
  94. int i = (expires >> TVR_BITS) & TVN_MASK;
  95. vec = base->tv2.vec + i;
  96. } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {
  97. int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
  98. vec = base->tv3.vec + i;
  99. } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {
  100. int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
  101. vec = base->tv4.vec + i;
  102. } else if ((signed long) idx < 0) {
  103. /*
  104. * Can happen if you add a timer with expires == jiffies,
  105. * or you set a timer to go off in the past
  106. */
  107. vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK);
  108. } else {
  109. int i;
  110. /* If the timeout is larger than 0xffffffff on 64-bit
  111. * architectures then we use the maximum timeout:
  112. */
  113. if (idx > 0xffffffffUL) {
  114. idx = 0xffffffffUL;
  115. expires = idx + base->timer_jiffies;
  116. }
  117. i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
  118. vec = base->tv5.vec + i;
  119. }
  120. /*
  121. * Timers are FIFO:
  122. */
  123. list_add_tail(&timer->entry, vec);
  124. }
  125. typedef struct timer_base_s timer_base_t;
  126. /*
  127. * Used by TIMER_INITIALIZER, we can't use per_cpu(tvec_bases)
  128. * at compile time, and we need timer->base to lock the timer.
  129. */
  130. timer_base_t __init_timer_base
  131. ____cacheline_aligned_in_smp = { .lock = SPIN_LOCK_UNLOCKED };
  132. EXPORT_SYMBOL(__init_timer_base);
  133. /***
  134. * init_timer - initialize a timer.
  135. * @timer: the timer to be initialized
  136. *
  137. * init_timer() must be done to a timer prior calling *any* of the
  138. * other timer functions.
  139. */
  140. void fastcall init_timer(struct timer_list *timer)
  141. {
  142. timer->entry.next = NULL;
  143. timer->base = &per_cpu(tvec_bases, raw_smp_processor_id()).t_base;
  144. }
  145. EXPORT_SYMBOL(init_timer);
  146. static inline void detach_timer(struct timer_list *timer,
  147. int clear_pending)
  148. {
  149. struct list_head *entry = &timer->entry;
  150. __list_del(entry->prev, entry->next);
  151. if (clear_pending)
  152. entry->next = NULL;
  153. entry->prev = LIST_POISON2;
  154. }
  155. /*
  156. * We are using hashed locking: holding per_cpu(tvec_bases).t_base.lock
  157. * means that all timers which are tied to this base via timer->base are
  158. * locked, and the base itself is locked too.
  159. *
  160. * So __run_timers/migrate_timers can safely modify all timers which could
  161. * be found on ->tvX lists.
  162. *
  163. * When the timer's base is locked, and the timer removed from list, it is
  164. * possible to set timer->base = NULL and drop the lock: the timer remains
  165. * locked.
  166. */
  167. static timer_base_t *lock_timer_base(struct timer_list *timer,
  168. unsigned long *flags)
  169. {
  170. timer_base_t *base;
  171. for (;;) {
  172. base = timer->base;
  173. if (likely(base != NULL)) {
  174. spin_lock_irqsave(&base->lock, *flags);
  175. if (likely(base == timer->base))
  176. return base;
  177. /* The timer has migrated to another CPU */
  178. spin_unlock_irqrestore(&base->lock, *flags);
  179. }
  180. cpu_relax();
  181. }
  182. }
  183. int __mod_timer(struct timer_list *timer, unsigned long expires)
  184. {
  185. timer_base_t *base;
  186. tvec_base_t *new_base;
  187. unsigned long flags;
  188. int ret = 0;
  189. BUG_ON(!timer->function);
  190. base = lock_timer_base(timer, &flags);
  191. if (timer_pending(timer)) {
  192. detach_timer(timer, 0);
  193. ret = 1;
  194. }
  195. new_base = &__get_cpu_var(tvec_bases);
  196. if (base != &new_base->t_base) {
  197. /*
  198. * We are trying to schedule the timer on the local CPU.
  199. * However we can't change timer's base while it is running,
  200. * otherwise del_timer_sync() can't detect that the timer's
  201. * handler yet has not finished. This also guarantees that
  202. * the timer is serialized wrt itself.
  203. */
  204. if (unlikely(base->running_timer == timer)) {
  205. /* The timer remains on a former base */
  206. new_base = container_of(base, tvec_base_t, t_base);
  207. } else {
  208. /* See the comment in lock_timer_base() */
  209. timer->base = NULL;
  210. spin_unlock(&base->lock);
  211. spin_lock(&new_base->t_base.lock);
  212. timer->base = &new_base->t_base;
  213. }
  214. }
  215. timer->expires = expires;
  216. internal_add_timer(new_base, timer);
  217. spin_unlock_irqrestore(&new_base->t_base.lock, flags);
  218. return ret;
  219. }
  220. EXPORT_SYMBOL(__mod_timer);
  221. /***
  222. * add_timer_on - start a timer on a particular CPU
  223. * @timer: the timer to be added
  224. * @cpu: the CPU to start it on
  225. *
  226. * This is not very scalable on SMP. Double adds are not possible.
  227. */
  228. void add_timer_on(struct timer_list *timer, int cpu)
  229. {
  230. tvec_base_t *base = &per_cpu(tvec_bases, cpu);
  231. unsigned long flags;
  232. BUG_ON(timer_pending(timer) || !timer->function);
  233. spin_lock_irqsave(&base->t_base.lock, flags);
  234. timer->base = &base->t_base;
  235. internal_add_timer(base, timer);
  236. spin_unlock_irqrestore(&base->t_base.lock, flags);
  237. }
  238. /***
  239. * mod_timer - modify a timer's timeout
  240. * @timer: the timer to be modified
  241. *
  242. * mod_timer is a more efficient way to update the expire field of an
  243. * active timer (if the timer is inactive it will be activated)
  244. *
  245. * mod_timer(timer, expires) is equivalent to:
  246. *
  247. * del_timer(timer); timer->expires = expires; add_timer(timer);
  248. *
  249. * Note that if there are multiple unserialized concurrent users of the
  250. * same timer, then mod_timer() is the only safe way to modify the timeout,
  251. * since add_timer() cannot modify an already running timer.
  252. *
  253. * The function returns whether it has modified a pending timer or not.
  254. * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
  255. * active timer returns 1.)
  256. */
  257. int mod_timer(struct timer_list *timer, unsigned long expires)
  258. {
  259. BUG_ON(!timer->function);
  260. /*
  261. * This is a common optimization triggered by the
  262. * networking code - if the timer is re-modified
  263. * to be the same thing then just return:
  264. */
  265. if (timer->expires == expires && timer_pending(timer))
  266. return 1;
  267. return __mod_timer(timer, expires);
  268. }
  269. EXPORT_SYMBOL(mod_timer);
  270. /***
  271. * del_timer - deactive a timer.
  272. * @timer: the timer to be deactivated
  273. *
  274. * del_timer() deactivates a timer - this works on both active and inactive
  275. * timers.
  276. *
  277. * The function returns whether it has deactivated a pending timer or not.
  278. * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
  279. * active timer returns 1.)
  280. */
  281. int del_timer(struct timer_list *timer)
  282. {
  283. timer_base_t *base;
  284. unsigned long flags;
  285. int ret = 0;
  286. if (timer_pending(timer)) {
  287. base = lock_timer_base(timer, &flags);
  288. if (timer_pending(timer)) {
  289. detach_timer(timer, 1);
  290. ret = 1;
  291. }
  292. spin_unlock_irqrestore(&base->lock, flags);
  293. }
  294. return ret;
  295. }
  296. EXPORT_SYMBOL(del_timer);
  297. #ifdef CONFIG_SMP
  298. /*
  299. * This function tries to deactivate a timer. Upon successful (ret >= 0)
  300. * exit the timer is not queued and the handler is not running on any CPU.
  301. *
  302. * It must not be called from interrupt contexts.
  303. */
  304. int try_to_del_timer_sync(struct timer_list *timer)
  305. {
  306. timer_base_t *base;
  307. unsigned long flags;
  308. int ret = -1;
  309. base = lock_timer_base(timer, &flags);
  310. if (base->running_timer == timer)
  311. goto out;
  312. ret = 0;
  313. if (timer_pending(timer)) {
  314. detach_timer(timer, 1);
  315. ret = 1;
  316. }
  317. out:
  318. spin_unlock_irqrestore(&base->lock, flags);
  319. return ret;
  320. }
  321. /***
  322. * del_timer_sync - deactivate a timer and wait for the handler to finish.
  323. * @timer: the timer to be deactivated
  324. *
  325. * This function only differs from del_timer() on SMP: besides deactivating
  326. * the timer it also makes sure the handler has finished executing on other
  327. * CPUs.
  328. *
  329. * Synchronization rules: callers must prevent restarting of the timer,
  330. * otherwise this function is meaningless. It must not be called from
  331. * interrupt contexts. The caller must not hold locks which would prevent
  332. * completion of the timer's handler. The timer's handler must not call
  333. * add_timer_on(). Upon exit the timer is not queued and the handler is
  334. * not running on any CPU.
  335. *
  336. * The function returns whether it has deactivated a pending timer or not.
  337. */
  338. int del_timer_sync(struct timer_list *timer)
  339. {
  340. for (;;) {
  341. int ret = try_to_del_timer_sync(timer);
  342. if (ret >= 0)
  343. return ret;
  344. }
  345. }
  346. EXPORT_SYMBOL(del_timer_sync);
  347. #endif
  348. static int cascade(tvec_base_t *base, tvec_t *tv, int index)
  349. {
  350. /* cascade all the timers from tv up one level */
  351. struct list_head *head, *curr;
  352. head = tv->vec + index;
  353. curr = head->next;
  354. /*
  355. * We are removing _all_ timers from the list, so we don't have to
  356. * detach them individually, just clear the list afterwards.
  357. */
  358. while (curr != head) {
  359. struct timer_list *tmp;
  360. tmp = list_entry(curr, struct timer_list, entry);
  361. BUG_ON(tmp->base != &base->t_base);
  362. curr = curr->next;
  363. internal_add_timer(base, tmp);
  364. }
  365. INIT_LIST_HEAD(head);
  366. return index;
  367. }
  368. /***
  369. * __run_timers - run all expired timers (if any) on this CPU.
  370. * @base: the timer vector to be processed.
  371. *
  372. * This function cascades all vectors and executes all expired timer
  373. * vectors.
  374. */
  375. #define INDEX(N) (base->timer_jiffies >> (TVR_BITS + N * TVN_BITS)) & TVN_MASK
  376. static inline void __run_timers(tvec_base_t *base)
  377. {
  378. struct timer_list *timer;
  379. spin_lock_irq(&base->t_base.lock);
  380. while (time_after_eq(jiffies, base->timer_jiffies)) {
  381. struct list_head work_list = LIST_HEAD_INIT(work_list);
  382. struct list_head *head = &work_list;
  383. int index = base->timer_jiffies & TVR_MASK;
  384. /*
  385. * Cascade timers:
  386. */
  387. if (!index &&
  388. (!cascade(base, &base->tv2, INDEX(0))) &&
  389. (!cascade(base, &base->tv3, INDEX(1))) &&
  390. !cascade(base, &base->tv4, INDEX(2)))
  391. cascade(base, &base->tv5, INDEX(3));
  392. ++base->timer_jiffies;
  393. list_splice_init(base->tv1.vec + index, &work_list);
  394. while (!list_empty(head)) {
  395. void (*fn)(unsigned long);
  396. unsigned long data;
  397. timer = list_entry(head->next,struct timer_list,entry);
  398. fn = timer->function;
  399. data = timer->data;
  400. set_running_timer(base, timer);
  401. detach_timer(timer, 1);
  402. spin_unlock_irq(&base->t_base.lock);
  403. {
  404. int preempt_count = preempt_count();
  405. fn(data);
  406. if (preempt_count != preempt_count()) {
  407. printk(KERN_WARNING "huh, entered %p "
  408. "with preempt_count %08x, exited"
  409. " with %08x?\n",
  410. fn, preempt_count,
  411. preempt_count());
  412. BUG();
  413. }
  414. }
  415. spin_lock_irq(&base->t_base.lock);
  416. }
  417. }
  418. set_running_timer(base, NULL);
  419. spin_unlock_irq(&base->t_base.lock);
  420. }
  421. #ifdef CONFIG_NO_IDLE_HZ
  422. /*
  423. * Find out when the next timer event is due to happen. This
  424. * is used on S/390 to stop all activity when a cpus is idle.
  425. * This functions needs to be called disabled.
  426. */
  427. unsigned long next_timer_interrupt(void)
  428. {
  429. tvec_base_t *base;
  430. struct list_head *list;
  431. struct timer_list *nte;
  432. unsigned long expires;
  433. tvec_t *varray[4];
  434. int i, j;
  435. base = &__get_cpu_var(tvec_bases);
  436. spin_lock(&base->t_base.lock);
  437. expires = base->timer_jiffies + (LONG_MAX >> 1);
  438. list = 0;
  439. /* Look for timer events in tv1. */
  440. j = base->timer_jiffies & TVR_MASK;
  441. do {
  442. list_for_each_entry(nte, base->tv1.vec + j, entry) {
  443. expires = nte->expires;
  444. if (j < (base->timer_jiffies & TVR_MASK))
  445. list = base->tv2.vec + (INDEX(0));
  446. goto found;
  447. }
  448. j = (j + 1) & TVR_MASK;
  449. } while (j != (base->timer_jiffies & TVR_MASK));
  450. /* Check tv2-tv5. */
  451. varray[0] = &base->tv2;
  452. varray[1] = &base->tv3;
  453. varray[2] = &base->tv4;
  454. varray[3] = &base->tv5;
  455. for (i = 0; i < 4; i++) {
  456. j = INDEX(i);
  457. do {
  458. if (list_empty(varray[i]->vec + j)) {
  459. j = (j + 1) & TVN_MASK;
  460. continue;
  461. }
  462. list_for_each_entry(nte, varray[i]->vec + j, entry)
  463. if (time_before(nte->expires, expires))
  464. expires = nte->expires;
  465. if (j < (INDEX(i)) && i < 3)
  466. list = varray[i + 1]->vec + (INDEX(i + 1));
  467. goto found;
  468. } while (j != (INDEX(i)));
  469. }
  470. found:
  471. if (list) {
  472. /*
  473. * The search wrapped. We need to look at the next list
  474. * from next tv element that would cascade into tv element
  475. * where we found the timer element.
  476. */
  477. list_for_each_entry(nte, list, entry) {
  478. if (time_before(nte->expires, expires))
  479. expires = nte->expires;
  480. }
  481. }
  482. spin_unlock(&base->t_base.lock);
  483. return expires;
  484. }
  485. #endif
  486. /******************************************************************/
  487. /*
  488. * Timekeeping variables
  489. */
  490. unsigned long tick_usec = TICK_USEC; /* USER_HZ period (usec) */
  491. unsigned long tick_nsec = TICK_NSEC; /* ACTHZ period (nsec) */
  492. /*
  493. * The current time
  494. * wall_to_monotonic is what we need to add to xtime (or xtime corrected
  495. * for sub jiffie times) to get to monotonic time. Monotonic is pegged
  496. * at zero at system boot time, so wall_to_monotonic will be negative,
  497. * however, we will ALWAYS keep the tv_nsec part positive so we can use
  498. * the usual normalization.
  499. */
  500. struct timespec xtime __attribute__ ((aligned (16)));
  501. struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
  502. EXPORT_SYMBOL(xtime);
  503. /* Don't completely fail for HZ > 500. */
  504. int tickadj = 500/HZ ? : 1; /* microsecs */
  505. /*
  506. * phase-lock loop variables
  507. */
  508. /* TIME_ERROR prevents overwriting the CMOS clock */
  509. int time_state = TIME_OK; /* clock synchronization status */
  510. int time_status = STA_UNSYNC; /* clock status bits */
  511. long time_offset; /* time adjustment (us) */
  512. long time_constant = 2; /* pll time constant */
  513. long time_tolerance = MAXFREQ; /* frequency tolerance (ppm) */
  514. long time_precision = 1; /* clock precision (us) */
  515. long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */
  516. long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */
  517. static long time_phase; /* phase offset (scaled us) */
  518. long time_freq = (((NSEC_PER_SEC + HZ/2) % HZ - HZ/2) << SHIFT_USEC) / NSEC_PER_USEC;
  519. /* frequency offset (scaled ppm)*/
  520. static long time_adj; /* tick adjust (scaled 1 / HZ) */
  521. long time_reftime; /* time at last adjustment (s) */
  522. long time_adjust;
  523. long time_next_adjust;
  524. /*
  525. * this routine handles the overflow of the microsecond field
  526. *
  527. * The tricky bits of code to handle the accurate clock support
  528. * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
  529. * They were originally developed for SUN and DEC kernels.
  530. * All the kudos should go to Dave for this stuff.
  531. *
  532. */
  533. static void second_overflow(void)
  534. {
  535. long ltemp;
  536. /* Bump the maxerror field */
  537. time_maxerror += time_tolerance >> SHIFT_USEC;
  538. if (time_maxerror > NTP_PHASE_LIMIT) {
  539. time_maxerror = NTP_PHASE_LIMIT;
  540. time_status |= STA_UNSYNC;
  541. }
  542. /*
  543. * Leap second processing. If in leap-insert state at the end of the
  544. * day, the system clock is set back one second; if in leap-delete
  545. * state, the system clock is set ahead one second. The microtime()
  546. * routine or external clock driver will insure that reported time is
  547. * always monotonic. The ugly divides should be replaced.
  548. */
  549. switch (time_state) {
  550. case TIME_OK:
  551. if (time_status & STA_INS)
  552. time_state = TIME_INS;
  553. else if (time_status & STA_DEL)
  554. time_state = TIME_DEL;
  555. break;
  556. case TIME_INS:
  557. if (xtime.tv_sec % 86400 == 0) {
  558. xtime.tv_sec--;
  559. wall_to_monotonic.tv_sec++;
  560. /*
  561. * The timer interpolator will make time change
  562. * gradually instead of an immediate jump by one second
  563. */
  564. time_interpolator_update(-NSEC_PER_SEC);
  565. time_state = TIME_OOP;
  566. clock_was_set();
  567. printk(KERN_NOTICE "Clock: inserting leap second "
  568. "23:59:60 UTC\n");
  569. }
  570. break;
  571. case TIME_DEL:
  572. if ((xtime.tv_sec + 1) % 86400 == 0) {
  573. xtime.tv_sec++;
  574. wall_to_monotonic.tv_sec--;
  575. /*
  576. * Use of time interpolator for a gradual change of
  577. * time
  578. */
  579. time_interpolator_update(NSEC_PER_SEC);
  580. time_state = TIME_WAIT;
  581. clock_was_set();
  582. printk(KERN_NOTICE "Clock: deleting leap second "
  583. "23:59:59 UTC\n");
  584. }
  585. break;
  586. case TIME_OOP:
  587. time_state = TIME_WAIT;
  588. break;
  589. case TIME_WAIT:
  590. if (!(time_status & (STA_INS | STA_DEL)))
  591. time_state = TIME_OK;
  592. }
  593. /*
  594. * Compute the phase adjustment for the next second. In PLL mode, the
  595. * offset is reduced by a fixed factor times the time constant. In FLL
  596. * mode the offset is used directly. In either mode, the maximum phase
  597. * adjustment for each second is clamped so as to spread the adjustment
  598. * over not more than the number of seconds between updates.
  599. */
  600. ltemp = time_offset;
  601. if (!(time_status & STA_FLL))
  602. ltemp = shift_right(ltemp, SHIFT_KG + time_constant);
  603. ltemp = min(ltemp, (MAXPHASE / MINSEC) << SHIFT_UPDATE);
  604. ltemp = max(ltemp, -(MAXPHASE / MINSEC) << SHIFT_UPDATE);
  605. time_offset -= ltemp;
  606. time_adj = ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE);
  607. /*
  608. * Compute the frequency estimate and additional phase adjustment due
  609. * to frequency error for the next second. When the PPS signal is
  610. * engaged, gnaw on the watchdog counter and update the frequency
  611. * computed by the pll and the PPS signal.
  612. */
  613. pps_valid++;
  614. if (pps_valid == PPS_VALID) { /* PPS signal lost */
  615. pps_jitter = MAXTIME;
  616. pps_stabil = MAXFREQ;
  617. time_status &= ~(STA_PPSSIGNAL | STA_PPSJITTER |
  618. STA_PPSWANDER | STA_PPSERROR);
  619. }
  620. ltemp = time_freq + pps_freq;
  621. time_adj += shift_right(ltemp,(SHIFT_USEC + SHIFT_HZ - SHIFT_SCALE));
  622. #if HZ == 100
  623. /*
  624. * Compensate for (HZ==100) != (1 << SHIFT_HZ). Add 25% and 3.125% to
  625. * get 128.125; => only 0.125% error (p. 14)
  626. */
  627. time_adj += shift_right(time_adj, 2) + shift_right(time_adj, 5);
  628. #endif
  629. #if HZ == 250
  630. /*
  631. * Compensate for (HZ==250) != (1 << SHIFT_HZ). Add 1.5625% and
  632. * 0.78125% to get 255.85938; => only 0.05% error (p. 14)
  633. */
  634. time_adj += shift_right(time_adj, 6) + shift_right(time_adj, 7);
  635. #endif
  636. #if HZ == 1000
  637. /*
  638. * Compensate for (HZ==1000) != (1 << SHIFT_HZ). Add 1.5625% and
  639. * 0.78125% to get 1023.4375; => only 0.05% error (p. 14)
  640. */
  641. time_adj += shift_right(time_adj, 6) + shift_right(time_adj, 7);
  642. #endif
  643. }
  644. /* in the NTP reference this is called "hardclock()" */
  645. static void update_wall_time_one_tick(void)
  646. {
  647. long time_adjust_step, delta_nsec;
  648. if ((time_adjust_step = time_adjust) != 0 ) {
  649. /*
  650. * We are doing an adjtime thing. Prepare time_adjust_step to
  651. * be within bounds. Note that a positive time_adjust means we
  652. * want the clock to run faster.
  653. *
  654. * Limit the amount of the step to be in the range
  655. * -tickadj .. +tickadj
  656. */
  657. time_adjust_step = min(time_adjust_step, (long)tickadj);
  658. time_adjust_step = max(time_adjust_step, (long)-tickadj);
  659. /* Reduce by this step the amount of time left */
  660. time_adjust -= time_adjust_step;
  661. }
  662. delta_nsec = tick_nsec + time_adjust_step * 1000;
  663. /*
  664. * Advance the phase, once it gets to one microsecond, then
  665. * advance the tick more.
  666. */
  667. time_phase += time_adj;
  668. if ((time_phase >= FINENSEC) || (time_phase <= -FINENSEC)) {
  669. long ltemp = shift_right(time_phase, (SHIFT_SCALE - 10));
  670. time_phase -= ltemp << (SHIFT_SCALE - 10);
  671. delta_nsec += ltemp;
  672. }
  673. xtime.tv_nsec += delta_nsec;
  674. time_interpolator_update(delta_nsec);
  675. /* Changes by adjtime() do not take effect till next tick. */
  676. if (time_next_adjust != 0) {
  677. time_adjust = time_next_adjust;
  678. time_next_adjust = 0;
  679. }
  680. }
  681. /*
  682. * Using a loop looks inefficient, but "ticks" is
  683. * usually just one (we shouldn't be losing ticks,
  684. * we're doing this this way mainly for interrupt
  685. * latency reasons, not because we think we'll
  686. * have lots of lost timer ticks
  687. */
  688. static void update_wall_time(unsigned long ticks)
  689. {
  690. do {
  691. ticks--;
  692. update_wall_time_one_tick();
  693. if (xtime.tv_nsec >= 1000000000) {
  694. xtime.tv_nsec -= 1000000000;
  695. xtime.tv_sec++;
  696. second_overflow();
  697. }
  698. } while (ticks);
  699. }
  700. /*
  701. * Called from the timer interrupt handler to charge one tick to the current
  702. * process. user_tick is 1 if the tick is user time, 0 for system.
  703. */
  704. void update_process_times(int user_tick)
  705. {
  706. struct task_struct *p = current;
  707. int cpu = smp_processor_id();
  708. /* Note: this timer irq context must be accounted for as well. */
  709. if (user_tick)
  710. account_user_time(p, jiffies_to_cputime(1));
  711. else
  712. account_system_time(p, HARDIRQ_OFFSET, jiffies_to_cputime(1));
  713. run_local_timers();
  714. if (rcu_pending(cpu))
  715. rcu_check_callbacks(cpu, user_tick);
  716. scheduler_tick();
  717. run_posix_cpu_timers(p);
  718. }
  719. /*
  720. * Nr of active tasks - counted in fixed-point numbers
  721. */
  722. static unsigned long count_active_tasks(void)
  723. {
  724. return (nr_running() + nr_uninterruptible()) * FIXED_1;
  725. }
  726. /*
  727. * Hmm.. Changed this, as the GNU make sources (load.c) seems to
  728. * imply that avenrun[] is the standard name for this kind of thing.
  729. * Nothing else seems to be standardized: the fractional size etc
  730. * all seem to differ on different machines.
  731. *
  732. * Requires xtime_lock to access.
  733. */
  734. unsigned long avenrun[3];
  735. EXPORT_SYMBOL(avenrun);
  736. /*
  737. * calc_load - given tick count, update the avenrun load estimates.
  738. * This is called while holding a write_lock on xtime_lock.
  739. */
  740. static inline void calc_load(unsigned long ticks)
  741. {
  742. unsigned long active_tasks; /* fixed-point */
  743. static int count = LOAD_FREQ;
  744. count -= ticks;
  745. if (count < 0) {
  746. count += LOAD_FREQ;
  747. active_tasks = count_active_tasks();
  748. CALC_LOAD(avenrun[0], EXP_1, active_tasks);
  749. CALC_LOAD(avenrun[1], EXP_5, active_tasks);
  750. CALC_LOAD(avenrun[2], EXP_15, active_tasks);
  751. }
  752. }
  753. /* jiffies at the most recent update of wall time */
  754. unsigned long wall_jiffies = INITIAL_JIFFIES;
  755. /*
  756. * This read-write spinlock protects us from races in SMP while
  757. * playing with xtime and avenrun.
  758. */
  759. #ifndef ARCH_HAVE_XTIME_LOCK
  760. seqlock_t xtime_lock __cacheline_aligned_in_smp = SEQLOCK_UNLOCKED;
  761. EXPORT_SYMBOL(xtime_lock);
  762. #endif
  763. /*
  764. * This function runs timers and the timer-tq in bottom half context.
  765. */
  766. static void run_timer_softirq(struct softirq_action *h)
  767. {
  768. tvec_base_t *base = &__get_cpu_var(tvec_bases);
  769. if (time_after_eq(jiffies, base->timer_jiffies))
  770. __run_timers(base);
  771. }
  772. /*
  773. * Called by the local, per-CPU timer interrupt on SMP.
  774. */
  775. void run_local_timers(void)
  776. {
  777. raise_softirq(TIMER_SOFTIRQ);
  778. }
  779. /*
  780. * Called by the timer interrupt. xtime_lock must already be taken
  781. * by the timer IRQ!
  782. */
  783. static inline void update_times(void)
  784. {
  785. unsigned long ticks;
  786. ticks = jiffies - wall_jiffies;
  787. if (ticks) {
  788. wall_jiffies += ticks;
  789. update_wall_time(ticks);
  790. }
  791. calc_load(ticks);
  792. }
  793. /*
  794. * The 64-bit jiffies value is not atomic - you MUST NOT read it
  795. * without sampling the sequence number in xtime_lock.
  796. * jiffies is defined in the linker script...
  797. */
  798. void do_timer(struct pt_regs *regs)
  799. {
  800. jiffies_64++;
  801. update_times();
  802. softlockup_tick(regs);
  803. }
  804. #ifdef __ARCH_WANT_SYS_ALARM
  805. /*
  806. * For backwards compatibility? This can be done in libc so Alpha
  807. * and all newer ports shouldn't need it.
  808. */
  809. asmlinkage unsigned long sys_alarm(unsigned int seconds)
  810. {
  811. struct itimerval it_new, it_old;
  812. unsigned int oldalarm;
  813. it_new.it_interval.tv_sec = it_new.it_interval.tv_usec = 0;
  814. it_new.it_value.tv_sec = seconds;
  815. it_new.it_value.tv_usec = 0;
  816. do_setitimer(ITIMER_REAL, &it_new, &it_old);
  817. oldalarm = it_old.it_value.tv_sec;
  818. /* ehhh.. We can't return 0 if we have an alarm pending.. */
  819. /* And we'd better return too much than too little anyway */
  820. if ((!oldalarm && it_old.it_value.tv_usec) || it_old.it_value.tv_usec >= 500000)
  821. oldalarm++;
  822. return oldalarm;
  823. }
  824. #endif
  825. #ifndef __alpha__
  826. /*
  827. * The Alpha uses getxpid, getxuid, and getxgid instead. Maybe this
  828. * should be moved into arch/i386 instead?
  829. */
  830. /**
  831. * sys_getpid - return the thread group id of the current process
  832. *
  833. * Note, despite the name, this returns the tgid not the pid. The tgid and
  834. * the pid are identical unless CLONE_THREAD was specified on clone() in
  835. * which case the tgid is the same in all threads of the same group.
  836. *
  837. * This is SMP safe as current->tgid does not change.
  838. */
  839. asmlinkage long sys_getpid(void)
  840. {
  841. return current->tgid;
  842. }
  843. /*
  844. * Accessing ->group_leader->real_parent is not SMP-safe, it could
  845. * change from under us. However, rather than getting any lock
  846. * we can use an optimistic algorithm: get the parent
  847. * pid, and go back and check that the parent is still
  848. * the same. If it has changed (which is extremely unlikely
  849. * indeed), we just try again..
  850. *
  851. * NOTE! This depends on the fact that even if we _do_
  852. * get an old value of "parent", we can happily dereference
  853. * the pointer (it was and remains a dereferencable kernel pointer
  854. * no matter what): we just can't necessarily trust the result
  855. * until we know that the parent pointer is valid.
  856. *
  857. * NOTE2: ->group_leader never changes from under us.
  858. */
  859. asmlinkage long sys_getppid(void)
  860. {
  861. int pid;
  862. struct task_struct *me = current;
  863. struct task_struct *parent;
  864. parent = me->group_leader->real_parent;
  865. for (;;) {
  866. pid = parent->tgid;
  867. #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
  868. {
  869. struct task_struct *old = parent;
  870. /*
  871. * Make sure we read the pid before re-reading the
  872. * parent pointer:
  873. */
  874. smp_rmb();
  875. parent = me->group_leader->real_parent;
  876. if (old != parent)
  877. continue;
  878. }
  879. #endif
  880. break;
  881. }
  882. return pid;
  883. }
  884. asmlinkage long sys_getuid(void)
  885. {
  886. /* Only we change this so SMP safe */
  887. return current->uid;
  888. }
  889. asmlinkage long sys_geteuid(void)
  890. {
  891. /* Only we change this so SMP safe */
  892. return current->euid;
  893. }
  894. asmlinkage long sys_getgid(void)
  895. {
  896. /* Only we change this so SMP safe */
  897. return current->gid;
  898. }
  899. asmlinkage long sys_getegid(void)
  900. {
  901. /* Only we change this so SMP safe */
  902. return current->egid;
  903. }
  904. #endif
  905. static void process_timeout(unsigned long __data)
  906. {
  907. wake_up_process((task_t *)__data);
  908. }
  909. /**
  910. * schedule_timeout - sleep until timeout
  911. * @timeout: timeout value in jiffies
  912. *
  913. * Make the current task sleep until @timeout jiffies have
  914. * elapsed. The routine will return immediately unless
  915. * the current task state has been set (see set_current_state()).
  916. *
  917. * You can set the task state as follows -
  918. *
  919. * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
  920. * pass before the routine returns. The routine will return 0
  921. *
  922. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  923. * delivered to the current task. In this case the remaining time
  924. * in jiffies will be returned, or 0 if the timer expired in time
  925. *
  926. * The current task state is guaranteed to be TASK_RUNNING when this
  927. * routine returns.
  928. *
  929. * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
  930. * the CPU away without a bound on the timeout. In this case the return
  931. * value will be %MAX_SCHEDULE_TIMEOUT.
  932. *
  933. * In all cases the return value is guaranteed to be non-negative.
  934. */
  935. fastcall signed long __sched schedule_timeout(signed long timeout)
  936. {
  937. struct timer_list timer;
  938. unsigned long expire;
  939. switch (timeout)
  940. {
  941. case MAX_SCHEDULE_TIMEOUT:
  942. /*
  943. * These two special cases are useful to be comfortable
  944. * in the caller. Nothing more. We could take
  945. * MAX_SCHEDULE_TIMEOUT from one of the negative value
  946. * but I' d like to return a valid offset (>=0) to allow
  947. * the caller to do everything it want with the retval.
  948. */
  949. schedule();
  950. goto out;
  951. default:
  952. /*
  953. * Another bit of PARANOID. Note that the retval will be
  954. * 0 since no piece of kernel is supposed to do a check
  955. * for a negative retval of schedule_timeout() (since it
  956. * should never happens anyway). You just have the printk()
  957. * that will tell you if something is gone wrong and where.
  958. */
  959. if (timeout < 0)
  960. {
  961. printk(KERN_ERR "schedule_timeout: wrong timeout "
  962. "value %lx from %p\n", timeout,
  963. __builtin_return_address(0));
  964. current->state = TASK_RUNNING;
  965. goto out;
  966. }
  967. }
  968. expire = timeout + jiffies;
  969. setup_timer(&timer, process_timeout, (unsigned long)current);
  970. __mod_timer(&timer, expire);
  971. schedule();
  972. del_singleshot_timer_sync(&timer);
  973. timeout = expire - jiffies;
  974. out:
  975. return timeout < 0 ? 0 : timeout;
  976. }
  977. EXPORT_SYMBOL(schedule_timeout);
  978. /*
  979. * We can use __set_current_state() here because schedule_timeout() calls
  980. * schedule() unconditionally.
  981. */
  982. signed long __sched schedule_timeout_interruptible(signed long timeout)
  983. {
  984. __set_current_state(TASK_INTERRUPTIBLE);
  985. return schedule_timeout(timeout);
  986. }
  987. EXPORT_SYMBOL(schedule_timeout_interruptible);
  988. signed long __sched schedule_timeout_uninterruptible(signed long timeout)
  989. {
  990. __set_current_state(TASK_UNINTERRUPTIBLE);
  991. return schedule_timeout(timeout);
  992. }
  993. EXPORT_SYMBOL(schedule_timeout_uninterruptible);
  994. /* Thread ID - the internal kernel "pid" */
  995. asmlinkage long sys_gettid(void)
  996. {
  997. return current->pid;
  998. }
  999. static long __sched nanosleep_restart(struct restart_block *restart)
  1000. {
  1001. unsigned long expire = restart->arg0, now = jiffies;
  1002. struct timespec __user *rmtp = (struct timespec __user *) restart->arg1;
  1003. long ret;
  1004. /* Did it expire while we handled signals? */
  1005. if (!time_after(expire, now))
  1006. return 0;
  1007. expire = schedule_timeout_interruptible(expire - now);
  1008. ret = 0;
  1009. if (expire) {
  1010. struct timespec t;
  1011. jiffies_to_timespec(expire, &t);
  1012. ret = -ERESTART_RESTARTBLOCK;
  1013. if (rmtp && copy_to_user(rmtp, &t, sizeof(t)))
  1014. ret = -EFAULT;
  1015. /* The 'restart' block is already filled in */
  1016. }
  1017. return ret;
  1018. }
  1019. asmlinkage long sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp)
  1020. {
  1021. struct timespec t;
  1022. unsigned long expire;
  1023. long ret;
  1024. if (copy_from_user(&t, rqtp, sizeof(t)))
  1025. return -EFAULT;
  1026. if ((t.tv_nsec >= 1000000000L) || (t.tv_nsec < 0) || (t.tv_sec < 0))
  1027. return -EINVAL;
  1028. expire = timespec_to_jiffies(&t) + (t.tv_sec || t.tv_nsec);
  1029. expire = schedule_timeout_interruptible(expire);
  1030. ret = 0;
  1031. if (expire) {
  1032. struct restart_block *restart;
  1033. jiffies_to_timespec(expire, &t);
  1034. if (rmtp && copy_to_user(rmtp, &t, sizeof(t)))
  1035. return -EFAULT;
  1036. restart = &current_thread_info()->restart_block;
  1037. restart->fn = nanosleep_restart;
  1038. restart->arg0 = jiffies + expire;
  1039. restart->arg1 = (unsigned long) rmtp;
  1040. ret = -ERESTART_RESTARTBLOCK;
  1041. }
  1042. return ret;
  1043. }
  1044. /*
  1045. * sys_sysinfo - fill in sysinfo struct
  1046. */
  1047. asmlinkage long sys_sysinfo(struct sysinfo __user *info)
  1048. {
  1049. struct sysinfo val;
  1050. unsigned long mem_total, sav_total;
  1051. unsigned int mem_unit, bitcount;
  1052. unsigned long seq;
  1053. memset((char *)&val, 0, sizeof(struct sysinfo));
  1054. do {
  1055. struct timespec tp;
  1056. seq = read_seqbegin(&xtime_lock);
  1057. /*
  1058. * This is annoying. The below is the same thing
  1059. * posix_get_clock_monotonic() does, but it wants to
  1060. * take the lock which we want to cover the loads stuff
  1061. * too.
  1062. */
  1063. getnstimeofday(&tp);
  1064. tp.tv_sec += wall_to_monotonic.tv_sec;
  1065. tp.tv_nsec += wall_to_monotonic.tv_nsec;
  1066. if (tp.tv_nsec - NSEC_PER_SEC >= 0) {
  1067. tp.tv_nsec = tp.tv_nsec - NSEC_PER_SEC;
  1068. tp.tv_sec++;
  1069. }
  1070. val.uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
  1071. val.loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT);
  1072. val.loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT);
  1073. val.loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT);
  1074. val.procs = nr_threads;
  1075. } while (read_seqretry(&xtime_lock, seq));
  1076. si_meminfo(&val);
  1077. si_swapinfo(&val);
  1078. /*
  1079. * If the sum of all the available memory (i.e. ram + swap)
  1080. * is less than can be stored in a 32 bit unsigned long then
  1081. * we can be binary compatible with 2.2.x kernels. If not,
  1082. * well, in that case 2.2.x was broken anyways...
  1083. *
  1084. * -Erik Andersen <andersee@debian.org>
  1085. */
  1086. mem_total = val.totalram + val.totalswap;
  1087. if (mem_total < val.totalram || mem_total < val.totalswap)
  1088. goto out;
  1089. bitcount = 0;
  1090. mem_unit = val.mem_unit;
  1091. while (mem_unit > 1) {
  1092. bitcount++;
  1093. mem_unit >>= 1;
  1094. sav_total = mem_total;
  1095. mem_total <<= 1;
  1096. if (mem_total < sav_total)
  1097. goto out;
  1098. }
  1099. /*
  1100. * If mem_total did not overflow, multiply all memory values by
  1101. * val.mem_unit and set it to 1. This leaves things compatible
  1102. * with 2.2.x, and also retains compatibility with earlier 2.4.x
  1103. * kernels...
  1104. */
  1105. val.mem_unit = 1;
  1106. val.totalram <<= bitcount;
  1107. val.freeram <<= bitcount;
  1108. val.sharedram <<= bitcount;
  1109. val.bufferram <<= bitcount;
  1110. val.totalswap <<= bitcount;
  1111. val.freeswap <<= bitcount;
  1112. val.totalhigh <<= bitcount;
  1113. val.freehigh <<= bitcount;
  1114. out:
  1115. if (copy_to_user(info, &val, sizeof(struct sysinfo)))
  1116. return -EFAULT;
  1117. return 0;
  1118. }
  1119. static void __devinit init_timers_cpu(int cpu)
  1120. {
  1121. int j;
  1122. tvec_base_t *base;
  1123. base = &per_cpu(tvec_bases, cpu);
  1124. spin_lock_init(&base->t_base.lock);
  1125. for (j = 0; j < TVN_SIZE; j++) {
  1126. INIT_LIST_HEAD(base->tv5.vec + j);
  1127. INIT_LIST_HEAD(base->tv4.vec + j);
  1128. INIT_LIST_HEAD(base->tv3.vec + j);
  1129. INIT_LIST_HEAD(base->tv2.vec + j);
  1130. }
  1131. for (j = 0; j < TVR_SIZE; j++)
  1132. INIT_LIST_HEAD(base->tv1.vec + j);
  1133. base->timer_jiffies = jiffies;
  1134. }
  1135. #ifdef CONFIG_HOTPLUG_CPU
  1136. static void migrate_timer_list(tvec_base_t *new_base, struct list_head *head)
  1137. {
  1138. struct timer_list *timer;
  1139. while (!list_empty(head)) {
  1140. timer = list_entry(head->next, struct timer_list, entry);
  1141. detach_timer(timer, 0);
  1142. timer->base = &new_base->t_base;
  1143. internal_add_timer(new_base, timer);
  1144. }
  1145. }
  1146. static void __devinit migrate_timers(int cpu)
  1147. {
  1148. tvec_base_t *old_base;
  1149. tvec_base_t *new_base;
  1150. int i;
  1151. BUG_ON(cpu_online(cpu));
  1152. old_base = &per_cpu(tvec_bases, cpu);
  1153. new_base = &get_cpu_var(tvec_bases);
  1154. local_irq_disable();
  1155. spin_lock(&new_base->t_base.lock);
  1156. spin_lock(&old_base->t_base.lock);
  1157. if (old_base->t_base.running_timer)
  1158. BUG();
  1159. for (i = 0; i < TVR_SIZE; i++)
  1160. migrate_timer_list(new_base, old_base->tv1.vec + i);
  1161. for (i = 0; i < TVN_SIZE; i++) {
  1162. migrate_timer_list(new_base, old_base->tv2.vec + i);
  1163. migrate_timer_list(new_base, old_base->tv3.vec + i);
  1164. migrate_timer_list(new_base, old_base->tv4.vec + i);
  1165. migrate_timer_list(new_base, old_base->tv5.vec + i);
  1166. }
  1167. spin_unlock(&old_base->t_base.lock);
  1168. spin_unlock(&new_base->t_base.lock);
  1169. local_irq_enable();
  1170. put_cpu_var(tvec_bases);
  1171. }
  1172. #endif /* CONFIG_HOTPLUG_CPU */
  1173. static int __devinit timer_cpu_notify(struct notifier_block *self,
  1174. unsigned long action, void *hcpu)
  1175. {
  1176. long cpu = (long)hcpu;
  1177. switch(action) {
  1178. case CPU_UP_PREPARE:
  1179. init_timers_cpu(cpu);
  1180. break;
  1181. #ifdef CONFIG_HOTPLUG_CPU
  1182. case CPU_DEAD:
  1183. migrate_timers(cpu);
  1184. break;
  1185. #endif
  1186. default:
  1187. break;
  1188. }
  1189. return NOTIFY_OK;
  1190. }
  1191. static struct notifier_block __devinitdata timers_nb = {
  1192. .notifier_call = timer_cpu_notify,
  1193. };
  1194. void __init init_timers(void)
  1195. {
  1196. timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
  1197. (void *)(long)smp_processor_id());
  1198. register_cpu_notifier(&timers_nb);
  1199. open_softirq(TIMER_SOFTIRQ, run_timer_softirq, NULL);
  1200. }
  1201. #ifdef CONFIG_TIME_INTERPOLATION
  1202. struct time_interpolator *time_interpolator;
  1203. static struct time_interpolator *time_interpolator_list;
  1204. static DEFINE_SPINLOCK(time_interpolator_lock);
  1205. static inline u64 time_interpolator_get_cycles(unsigned int src)
  1206. {
  1207. unsigned long (*x)(void);
  1208. switch (src)
  1209. {
  1210. case TIME_SOURCE_FUNCTION:
  1211. x = time_interpolator->addr;
  1212. return x();
  1213. case TIME_SOURCE_MMIO64 :
  1214. return readq((void __iomem *) time_interpolator->addr);
  1215. case TIME_SOURCE_MMIO32 :
  1216. return readl((void __iomem *) time_interpolator->addr);
  1217. default: return get_cycles();
  1218. }
  1219. }
  1220. static inline u64 time_interpolator_get_counter(int writelock)
  1221. {
  1222. unsigned int src = time_interpolator->source;
  1223. if (time_interpolator->jitter)
  1224. {
  1225. u64 lcycle;
  1226. u64 now;
  1227. do {
  1228. lcycle = time_interpolator->last_cycle;
  1229. now = time_interpolator_get_cycles(src);
  1230. if (lcycle && time_after(lcycle, now))
  1231. return lcycle;
  1232. /* When holding the xtime write lock, there's no need
  1233. * to add the overhead of the cmpxchg. Readers are
  1234. * force to retry until the write lock is released.
  1235. */
  1236. if (writelock) {
  1237. time_interpolator->last_cycle = now;
  1238. return now;
  1239. }
  1240. /* Keep track of the last timer value returned. The use of cmpxchg here
  1241. * will cause contention in an SMP environment.
  1242. */
  1243. } while (unlikely(cmpxchg(&time_interpolator->last_cycle, lcycle, now) != lcycle));
  1244. return now;
  1245. }
  1246. else
  1247. return time_interpolator_get_cycles(src);
  1248. }
  1249. void time_interpolator_reset(void)
  1250. {
  1251. time_interpolator->offset = 0;
  1252. time_interpolator->last_counter = time_interpolator_get_counter(1);
  1253. }
  1254. #define GET_TI_NSECS(count,i) (((((count) - i->last_counter) & (i)->mask) * (i)->nsec_per_cyc) >> (i)->shift)
  1255. unsigned long time_interpolator_get_offset(void)
  1256. {
  1257. /* If we do not have a time interpolator set up then just return zero */
  1258. if (!time_interpolator)
  1259. return 0;
  1260. return time_interpolator->offset +
  1261. GET_TI_NSECS(time_interpolator_get_counter(0), time_interpolator);
  1262. }
  1263. #define INTERPOLATOR_ADJUST 65536
  1264. #define INTERPOLATOR_MAX_SKIP 10*INTERPOLATOR_ADJUST
  1265. static void time_interpolator_update(long delta_nsec)
  1266. {
  1267. u64 counter;
  1268. unsigned long offset;
  1269. /* If there is no time interpolator set up then do nothing */
  1270. if (!time_interpolator)
  1271. return;
  1272. /*
  1273. * The interpolator compensates for late ticks by accumulating the late
  1274. * time in time_interpolator->offset. A tick earlier than expected will
  1275. * lead to a reset of the offset and a corresponding jump of the clock
  1276. * forward. Again this only works if the interpolator clock is running
  1277. * slightly slower than the regular clock and the tuning logic insures
  1278. * that.
  1279. */
  1280. counter = time_interpolator_get_counter(1);
  1281. offset = time_interpolator->offset +
  1282. GET_TI_NSECS(counter, time_interpolator);
  1283. if (delta_nsec < 0 || (unsigned long) delta_nsec < offset)
  1284. time_interpolator->offset = offset - delta_nsec;
  1285. else {
  1286. time_interpolator->skips++;
  1287. time_interpolator->ns_skipped += delta_nsec - offset;
  1288. time_interpolator->offset = 0;
  1289. }
  1290. time_interpolator->last_counter = counter;
  1291. /* Tuning logic for time interpolator invoked every minute or so.
  1292. * Decrease interpolator clock speed if no skips occurred and an offset is carried.
  1293. * Increase interpolator clock speed if we skip too much time.
  1294. */
  1295. if (jiffies % INTERPOLATOR_ADJUST == 0)
  1296. {
  1297. if (time_interpolator->skips == 0 && time_interpolator->offset > TICK_NSEC)
  1298. time_interpolator->nsec_per_cyc--;
  1299. if (time_interpolator->ns_skipped > INTERPOLATOR_MAX_SKIP && time_interpolator->offset == 0)
  1300. time_interpolator->nsec_per_cyc++;
  1301. time_interpolator->skips = 0;
  1302. time_interpolator->ns_skipped = 0;
  1303. }
  1304. }
  1305. static inline int
  1306. is_better_time_interpolator(struct time_interpolator *new)
  1307. {
  1308. if (!time_interpolator)
  1309. return 1;
  1310. return new->frequency > 2*time_interpolator->frequency ||
  1311. (unsigned long)new->drift < (unsigned long)time_interpolator->drift;
  1312. }
  1313. void
  1314. register_time_interpolator(struct time_interpolator *ti)
  1315. {
  1316. unsigned long flags;
  1317. /* Sanity check */
  1318. if (ti->frequency == 0 || ti->mask == 0)
  1319. BUG();
  1320. ti->nsec_per_cyc = ((u64)NSEC_PER_SEC << ti->shift) / ti->frequency;
  1321. spin_lock(&time_interpolator_lock);
  1322. write_seqlock_irqsave(&xtime_lock, flags);
  1323. if (is_better_time_interpolator(ti)) {
  1324. time_interpolator = ti;
  1325. time_interpolator_reset();
  1326. }
  1327. write_sequnlock_irqrestore(&xtime_lock, flags);
  1328. ti->next = time_interpolator_list;
  1329. time_interpolator_list = ti;
  1330. spin_unlock(&time_interpolator_lock);
  1331. }
  1332. void
  1333. unregister_time_interpolator(struct time_interpolator *ti)
  1334. {
  1335. struct time_interpolator *curr, **prev;
  1336. unsigned long flags;
  1337. spin_lock(&time_interpolator_lock);
  1338. prev = &time_interpolator_list;
  1339. for (curr = *prev; curr; curr = curr->next) {
  1340. if (curr == ti) {
  1341. *prev = curr->next;
  1342. break;
  1343. }
  1344. prev = &curr->next;
  1345. }
  1346. write_seqlock_irqsave(&xtime_lock, flags);
  1347. if (ti == time_interpolator) {
  1348. /* we lost the best time-interpolator: */
  1349. time_interpolator = NULL;
  1350. /* find the next-best interpolator */
  1351. for (curr = time_interpolator_list; curr; curr = curr->next)
  1352. if (is_better_time_interpolator(curr))
  1353. time_interpolator = curr;
  1354. time_interpolator_reset();
  1355. }
  1356. write_sequnlock_irqrestore(&xtime_lock, flags);
  1357. spin_unlock(&time_interpolator_lock);
  1358. }
  1359. #endif /* CONFIG_TIME_INTERPOLATION */
  1360. /**
  1361. * msleep - sleep safely even with waitqueue interruptions
  1362. * @msecs: Time in milliseconds to sleep for
  1363. */
  1364. void msleep(unsigned int msecs)
  1365. {
  1366. unsigned long timeout = msecs_to_jiffies(msecs) + 1;
  1367. while (timeout)
  1368. timeout = schedule_timeout_uninterruptible(timeout);
  1369. }
  1370. EXPORT_SYMBOL(msleep);
  1371. /**
  1372. * msleep_interruptible - sleep waiting for signals
  1373. * @msecs: Time in milliseconds to sleep for
  1374. */
  1375. unsigned long msleep_interruptible(unsigned int msecs)
  1376. {
  1377. unsigned long timeout = msecs_to_jiffies(msecs) + 1;
  1378. while (timeout && !signal_pending(current))
  1379. timeout = schedule_timeout_interruptible(timeout);
  1380. return jiffies_to_msecs(timeout);
  1381. }
  1382. EXPORT_SYMBOL(msleep_interruptible);