timer.c 42 KB

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