timer.c 41 KB

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