timer.c 42 KB

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