timer.c 40 KB

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