timer.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999
  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 <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_entry(head->next,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. /*
  705. * The current time
  706. * wall_to_monotonic is what we need to add to xtime (or xtime corrected
  707. * for sub jiffie times) to get to monotonic time. Monotonic is pegged
  708. * at zero at system boot time, so wall_to_monotonic will be negative,
  709. * however, we will ALWAYS keep the tv_nsec part positive so we can use
  710. * the usual normalization.
  711. */
  712. struct timespec xtime __attribute__ ((aligned (16)));
  713. struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
  714. EXPORT_SYMBOL(xtime);
  715. /* XXX - all of this timekeeping code should be later moved to time.c */
  716. #include <linux/clocksource.h>
  717. static struct clocksource *clock; /* pointer to current clocksource */
  718. #ifdef CONFIG_GENERIC_TIME
  719. /**
  720. * __get_nsec_offset - Returns nanoseconds since last call to periodic_hook
  721. *
  722. * private function, must hold xtime_lock lock when being
  723. * called. Returns the number of nanoseconds since the
  724. * last call to update_wall_time() (adjusted by NTP scaling)
  725. */
  726. static inline s64 __get_nsec_offset(void)
  727. {
  728. cycle_t cycle_now, cycle_delta;
  729. s64 ns_offset;
  730. /* read clocksource: */
  731. cycle_now = clocksource_read(clock);
  732. /* calculate the delta since the last update_wall_time: */
  733. cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
  734. /* convert to nanoseconds: */
  735. ns_offset = cyc2ns(clock, cycle_delta);
  736. return ns_offset;
  737. }
  738. /**
  739. * __get_realtime_clock_ts - Returns the time of day in a timespec
  740. * @ts: pointer to the timespec to be set
  741. *
  742. * Returns the time of day in a timespec. Used by
  743. * do_gettimeofday() and get_realtime_clock_ts().
  744. */
  745. static inline void __get_realtime_clock_ts(struct timespec *ts)
  746. {
  747. unsigned long seq;
  748. s64 nsecs;
  749. do {
  750. seq = read_seqbegin(&xtime_lock);
  751. *ts = xtime;
  752. nsecs = __get_nsec_offset();
  753. } while (read_seqretry(&xtime_lock, seq));
  754. timespec_add_ns(ts, nsecs);
  755. }
  756. /**
  757. * getnstimeofday - Returns the time of day in a timespec
  758. * @ts: pointer to the timespec to be set
  759. *
  760. * Returns the time of day in a timespec.
  761. */
  762. void getnstimeofday(struct timespec *ts)
  763. {
  764. __get_realtime_clock_ts(ts);
  765. }
  766. EXPORT_SYMBOL(getnstimeofday);
  767. /**
  768. * do_gettimeofday - Returns the time of day in a timeval
  769. * @tv: pointer to the timeval to be set
  770. *
  771. * NOTE: Users should be converted to using get_realtime_clock_ts()
  772. */
  773. void do_gettimeofday(struct timeval *tv)
  774. {
  775. struct timespec now;
  776. __get_realtime_clock_ts(&now);
  777. tv->tv_sec = now.tv_sec;
  778. tv->tv_usec = now.tv_nsec/1000;
  779. }
  780. EXPORT_SYMBOL(do_gettimeofday);
  781. /**
  782. * do_settimeofday - Sets the time of day
  783. * @tv: pointer to the timespec variable containing the new time
  784. *
  785. * Sets the time of day to the new time and update NTP and notify hrtimers
  786. */
  787. int do_settimeofday(struct timespec *tv)
  788. {
  789. unsigned long flags;
  790. time_t wtm_sec, sec = tv->tv_sec;
  791. long wtm_nsec, nsec = tv->tv_nsec;
  792. if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
  793. return -EINVAL;
  794. write_seqlock_irqsave(&xtime_lock, flags);
  795. nsec -= __get_nsec_offset();
  796. wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
  797. wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
  798. set_normalized_timespec(&xtime, sec, nsec);
  799. set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
  800. clock->error = 0;
  801. ntp_clear();
  802. update_vsyscall(&xtime, clock);
  803. write_sequnlock_irqrestore(&xtime_lock, flags);
  804. /* signal hrtimers about time change */
  805. clock_was_set();
  806. return 0;
  807. }
  808. EXPORT_SYMBOL(do_settimeofday);
  809. /**
  810. * change_clocksource - Swaps clocksources if a new one is available
  811. *
  812. * Accumulates current time interval and initializes new clocksource
  813. */
  814. static void change_clocksource(void)
  815. {
  816. struct clocksource *new;
  817. cycle_t now;
  818. u64 nsec;
  819. new = clocksource_get_next();
  820. if (clock == new)
  821. return;
  822. now = clocksource_read(new);
  823. nsec = __get_nsec_offset();
  824. timespec_add_ns(&xtime, nsec);
  825. clock = new;
  826. clock->cycle_last = now;
  827. clock->error = 0;
  828. clock->xtime_nsec = 0;
  829. clocksource_calculate_interval(clock, NTP_INTERVAL_LENGTH);
  830. tick_clock_notify();
  831. printk(KERN_INFO "Time: %s clocksource has been installed.\n",
  832. clock->name);
  833. }
  834. #else
  835. static inline void change_clocksource(void) { }
  836. #endif
  837. /**
  838. * timekeeping_is_continuous - check to see if timekeeping is free running
  839. */
  840. int timekeeping_is_continuous(void)
  841. {
  842. unsigned long seq;
  843. int ret;
  844. do {
  845. seq = read_seqbegin(&xtime_lock);
  846. ret = clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
  847. } while (read_seqretry(&xtime_lock, seq));
  848. return ret;
  849. }
  850. /**
  851. * read_persistent_clock - Return time in seconds from the persistent clock.
  852. *
  853. * Weak dummy function for arches that do not yet support it.
  854. * Returns seconds from epoch using the battery backed persistent clock.
  855. * Returns zero if unsupported.
  856. *
  857. * XXX - Do be sure to remove it once all arches implement it.
  858. */
  859. unsigned long __attribute__((weak)) read_persistent_clock(void)
  860. {
  861. return 0;
  862. }
  863. /*
  864. * timekeeping_init - Initializes the clocksource and common timekeeping values
  865. */
  866. void __init timekeeping_init(void)
  867. {
  868. unsigned long flags;
  869. unsigned long sec = read_persistent_clock();
  870. write_seqlock_irqsave(&xtime_lock, flags);
  871. ntp_clear();
  872. clock = clocksource_get_next();
  873. clocksource_calculate_interval(clock, NTP_INTERVAL_LENGTH);
  874. clock->cycle_last = clocksource_read(clock);
  875. xtime.tv_sec = sec;
  876. xtime.tv_nsec = 0;
  877. set_normalized_timespec(&wall_to_monotonic,
  878. -xtime.tv_sec, -xtime.tv_nsec);
  879. write_sequnlock_irqrestore(&xtime_lock, flags);
  880. }
  881. /* flag for if timekeeping is suspended */
  882. static int timekeeping_suspended;
  883. /* time in seconds when suspend began */
  884. static unsigned long timekeeping_suspend_time;
  885. /**
  886. * timekeeping_resume - Resumes the generic timekeeping subsystem.
  887. * @dev: unused
  888. *
  889. * This is for the generic clocksource timekeeping.
  890. * xtime/wall_to_monotonic/jiffies/etc are
  891. * still managed by arch specific suspend/resume code.
  892. */
  893. static int timekeeping_resume(struct sys_device *dev)
  894. {
  895. unsigned long flags;
  896. unsigned long now = read_persistent_clock();
  897. write_seqlock_irqsave(&xtime_lock, flags);
  898. if (now && (now > timekeeping_suspend_time)) {
  899. unsigned long sleep_length = now - timekeeping_suspend_time;
  900. xtime.tv_sec += sleep_length;
  901. wall_to_monotonic.tv_sec -= sleep_length;
  902. }
  903. /* re-base the last cycle value */
  904. clock->cycle_last = clocksource_read(clock);
  905. clock->error = 0;
  906. timekeeping_suspended = 0;
  907. write_sequnlock_irqrestore(&xtime_lock, flags);
  908. touch_softlockup_watchdog();
  909. clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
  910. /* Resume hrtimers */
  911. hres_timers_resume();
  912. return 0;
  913. }
  914. static int timekeeping_suspend(struct sys_device *dev, pm_message_t state)
  915. {
  916. unsigned long flags;
  917. write_seqlock_irqsave(&xtime_lock, flags);
  918. timekeeping_suspended = 1;
  919. timekeeping_suspend_time = read_persistent_clock();
  920. write_sequnlock_irqrestore(&xtime_lock, flags);
  921. clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
  922. return 0;
  923. }
  924. /* sysfs resume/suspend bits for timekeeping */
  925. static struct sysdev_class timekeeping_sysclass = {
  926. .resume = timekeeping_resume,
  927. .suspend = timekeeping_suspend,
  928. set_kset_name("timekeeping"),
  929. };
  930. static struct sys_device device_timer = {
  931. .id = 0,
  932. .cls = &timekeeping_sysclass,
  933. };
  934. static int __init timekeeping_init_device(void)
  935. {
  936. int error = sysdev_class_register(&timekeeping_sysclass);
  937. if (!error)
  938. error = sysdev_register(&device_timer);
  939. return error;
  940. }
  941. device_initcall(timekeeping_init_device);
  942. /*
  943. * If the error is already larger, we look ahead even further
  944. * to compensate for late or lost adjustments.
  945. */
  946. static __always_inline int clocksource_bigadjust(s64 error, s64 *interval,
  947. s64 *offset)
  948. {
  949. s64 tick_error, i;
  950. u32 look_ahead, adj;
  951. s32 error2, mult;
  952. /*
  953. * Use the current error value to determine how much to look ahead.
  954. * The larger the error the slower we adjust for it to avoid problems
  955. * with losing too many ticks, otherwise we would overadjust and
  956. * produce an even larger error. The smaller the adjustment the
  957. * faster we try to adjust for it, as lost ticks can do less harm
  958. * here. This is tuned so that an error of about 1 msec is adusted
  959. * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
  960. */
  961. error2 = clock->error >> (TICK_LENGTH_SHIFT + 22 - 2 * SHIFT_HZ);
  962. error2 = abs(error2);
  963. for (look_ahead = 0; error2 > 0; look_ahead++)
  964. error2 >>= 2;
  965. /*
  966. * Now calculate the error in (1 << look_ahead) ticks, but first
  967. * remove the single look ahead already included in the error.
  968. */
  969. tick_error = current_tick_length() >>
  970. (TICK_LENGTH_SHIFT - clock->shift + 1);
  971. tick_error -= clock->xtime_interval >> 1;
  972. error = ((error - tick_error) >> look_ahead) + tick_error;
  973. /* Finally calculate the adjustment shift value. */
  974. i = *interval;
  975. mult = 1;
  976. if (error < 0) {
  977. error = -error;
  978. *interval = -*interval;
  979. *offset = -*offset;
  980. mult = -1;
  981. }
  982. for (adj = 0; error > i; adj++)
  983. error >>= 1;
  984. *interval <<= adj;
  985. *offset <<= adj;
  986. return mult << adj;
  987. }
  988. /*
  989. * Adjust the multiplier to reduce the error value,
  990. * this is optimized for the most common adjustments of -1,0,1,
  991. * for other values we can do a bit more work.
  992. */
  993. static void clocksource_adjust(struct clocksource *clock, s64 offset)
  994. {
  995. s64 error, interval = clock->cycle_interval;
  996. int adj;
  997. error = clock->error >> (TICK_LENGTH_SHIFT - clock->shift - 1);
  998. if (error > interval) {
  999. error >>= 2;
  1000. if (likely(error <= interval))
  1001. adj = 1;
  1002. else
  1003. adj = clocksource_bigadjust(error, &interval, &offset);
  1004. } else if (error < -interval) {
  1005. error >>= 2;
  1006. if (likely(error >= -interval)) {
  1007. adj = -1;
  1008. interval = -interval;
  1009. offset = -offset;
  1010. } else
  1011. adj = clocksource_bigadjust(error, &interval, &offset);
  1012. } else
  1013. return;
  1014. clock->mult += adj;
  1015. clock->xtime_interval += interval;
  1016. clock->xtime_nsec -= offset;
  1017. clock->error -= (interval - offset) <<
  1018. (TICK_LENGTH_SHIFT - clock->shift);
  1019. }
  1020. /**
  1021. * update_wall_time - Uses the current clocksource to increment the wall time
  1022. *
  1023. * Called from the timer interrupt, must hold a write on xtime_lock.
  1024. */
  1025. static void update_wall_time(void)
  1026. {
  1027. cycle_t offset;
  1028. /* Make sure we're fully resumed: */
  1029. if (unlikely(timekeeping_suspended))
  1030. return;
  1031. #ifdef CONFIG_GENERIC_TIME
  1032. offset = (clocksource_read(clock) - clock->cycle_last) & clock->mask;
  1033. #else
  1034. offset = clock->cycle_interval;
  1035. #endif
  1036. clock->xtime_nsec += (s64)xtime.tv_nsec << clock->shift;
  1037. /* normally this loop will run just once, however in the
  1038. * case of lost or late ticks, it will accumulate correctly.
  1039. */
  1040. while (offset >= clock->cycle_interval) {
  1041. /* accumulate one interval */
  1042. clock->xtime_nsec += clock->xtime_interval;
  1043. clock->cycle_last += clock->cycle_interval;
  1044. offset -= clock->cycle_interval;
  1045. if (clock->xtime_nsec >= (u64)NSEC_PER_SEC << clock->shift) {
  1046. clock->xtime_nsec -= (u64)NSEC_PER_SEC << clock->shift;
  1047. xtime.tv_sec++;
  1048. second_overflow();
  1049. }
  1050. /* interpolator bits */
  1051. time_interpolator_update(clock->xtime_interval
  1052. >> clock->shift);
  1053. /* accumulate error between NTP and clock interval */
  1054. clock->error += current_tick_length();
  1055. clock->error -= clock->xtime_interval << (TICK_LENGTH_SHIFT - clock->shift);
  1056. }
  1057. /* correct the clock when NTP error is too big */
  1058. clocksource_adjust(clock, offset);
  1059. /* store full nanoseconds into xtime */
  1060. xtime.tv_nsec = (s64)clock->xtime_nsec >> clock->shift;
  1061. clock->xtime_nsec -= (s64)xtime.tv_nsec << clock->shift;
  1062. /* check to see if there is a new clocksource to use */
  1063. change_clocksource();
  1064. update_vsyscall(&xtime, clock);
  1065. }
  1066. /*
  1067. * Called from the timer interrupt handler to charge one tick to the current
  1068. * process. user_tick is 1 if the tick is user time, 0 for system.
  1069. */
  1070. void update_process_times(int user_tick)
  1071. {
  1072. struct task_struct *p = current;
  1073. int cpu = smp_processor_id();
  1074. /* Note: this timer irq context must be accounted for as well. */
  1075. if (user_tick)
  1076. account_user_time(p, jiffies_to_cputime(1));
  1077. else
  1078. account_system_time(p, HARDIRQ_OFFSET, jiffies_to_cputime(1));
  1079. run_local_timers();
  1080. if (rcu_pending(cpu))
  1081. rcu_check_callbacks(cpu, user_tick);
  1082. scheduler_tick();
  1083. run_posix_cpu_timers(p);
  1084. }
  1085. /*
  1086. * Nr of active tasks - counted in fixed-point numbers
  1087. */
  1088. static unsigned long count_active_tasks(void)
  1089. {
  1090. return nr_active() * FIXED_1;
  1091. }
  1092. /*
  1093. * Hmm.. Changed this, as the GNU make sources (load.c) seems to
  1094. * imply that avenrun[] is the standard name for this kind of thing.
  1095. * Nothing else seems to be standardized: the fractional size etc
  1096. * all seem to differ on different machines.
  1097. *
  1098. * Requires xtime_lock to access.
  1099. */
  1100. unsigned long avenrun[3];
  1101. EXPORT_SYMBOL(avenrun);
  1102. /*
  1103. * calc_load - given tick count, update the avenrun load estimates.
  1104. * This is called while holding a write_lock on xtime_lock.
  1105. */
  1106. static inline void calc_load(unsigned long ticks)
  1107. {
  1108. unsigned long active_tasks; /* fixed-point */
  1109. static int count = LOAD_FREQ;
  1110. count -= ticks;
  1111. if (unlikely(count < 0)) {
  1112. active_tasks = count_active_tasks();
  1113. do {
  1114. CALC_LOAD(avenrun[0], EXP_1, active_tasks);
  1115. CALC_LOAD(avenrun[1], EXP_5, active_tasks);
  1116. CALC_LOAD(avenrun[2], EXP_15, active_tasks);
  1117. count += LOAD_FREQ;
  1118. } while (count < 0);
  1119. }
  1120. }
  1121. /*
  1122. * This read-write spinlock protects us from races in SMP while
  1123. * playing with xtime and avenrun.
  1124. */
  1125. __attribute__((weak)) __cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock);
  1126. EXPORT_SYMBOL(xtime_lock);
  1127. /*
  1128. * This function runs timers and the timer-tq in bottom half context.
  1129. */
  1130. static void run_timer_softirq(struct softirq_action *h)
  1131. {
  1132. tvec_base_t *base = __get_cpu_var(tvec_bases);
  1133. hrtimer_run_queues();
  1134. if (time_after_eq(jiffies, base->timer_jiffies))
  1135. __run_timers(base);
  1136. }
  1137. /*
  1138. * Called by the local, per-CPU timer interrupt on SMP.
  1139. */
  1140. void run_local_timers(void)
  1141. {
  1142. raise_softirq(TIMER_SOFTIRQ);
  1143. softlockup_tick();
  1144. }
  1145. /*
  1146. * Called by the timer interrupt. xtime_lock must already be taken
  1147. * by the timer IRQ!
  1148. */
  1149. static inline void update_times(unsigned long ticks)
  1150. {
  1151. update_wall_time();
  1152. calc_load(ticks);
  1153. }
  1154. /*
  1155. * The 64-bit jiffies value is not atomic - you MUST NOT read it
  1156. * without sampling the sequence number in xtime_lock.
  1157. * jiffies is defined in the linker script...
  1158. */
  1159. void do_timer(unsigned long ticks)
  1160. {
  1161. jiffies_64 += ticks;
  1162. update_times(ticks);
  1163. }
  1164. #ifdef __ARCH_WANT_SYS_ALARM
  1165. /*
  1166. * For backwards compatibility? This can be done in libc so Alpha
  1167. * and all newer ports shouldn't need it.
  1168. */
  1169. asmlinkage unsigned long sys_alarm(unsigned int seconds)
  1170. {
  1171. return alarm_setitimer(seconds);
  1172. }
  1173. #endif
  1174. #ifndef __alpha__
  1175. /*
  1176. * The Alpha uses getxpid, getxuid, and getxgid instead. Maybe this
  1177. * should be moved into arch/i386 instead?
  1178. */
  1179. /**
  1180. * sys_getpid - return the thread group id of the current process
  1181. *
  1182. * Note, despite the name, this returns the tgid not the pid. The tgid and
  1183. * the pid are identical unless CLONE_THREAD was specified on clone() in
  1184. * which case the tgid is the same in all threads of the same group.
  1185. *
  1186. * This is SMP safe as current->tgid does not change.
  1187. */
  1188. asmlinkage long sys_getpid(void)
  1189. {
  1190. return current->tgid;
  1191. }
  1192. /*
  1193. * Accessing ->real_parent is not SMP-safe, it could
  1194. * change from under us. However, we can use a stale
  1195. * value of ->real_parent under rcu_read_lock(), see
  1196. * release_task()->call_rcu(delayed_put_task_struct).
  1197. */
  1198. asmlinkage long sys_getppid(void)
  1199. {
  1200. int pid;
  1201. rcu_read_lock();
  1202. pid = rcu_dereference(current->real_parent)->tgid;
  1203. rcu_read_unlock();
  1204. return pid;
  1205. }
  1206. asmlinkage long sys_getuid(void)
  1207. {
  1208. /* Only we change this so SMP safe */
  1209. return current->uid;
  1210. }
  1211. asmlinkage long sys_geteuid(void)
  1212. {
  1213. /* Only we change this so SMP safe */
  1214. return current->euid;
  1215. }
  1216. asmlinkage long sys_getgid(void)
  1217. {
  1218. /* Only we change this so SMP safe */
  1219. return current->gid;
  1220. }
  1221. asmlinkage long sys_getegid(void)
  1222. {
  1223. /* Only we change this so SMP safe */
  1224. return current->egid;
  1225. }
  1226. #endif
  1227. static void process_timeout(unsigned long __data)
  1228. {
  1229. wake_up_process((struct task_struct *)__data);
  1230. }
  1231. /**
  1232. * schedule_timeout - sleep until timeout
  1233. * @timeout: timeout value in jiffies
  1234. *
  1235. * Make the current task sleep until @timeout jiffies have
  1236. * elapsed. The routine will return immediately unless
  1237. * the current task state has been set (see set_current_state()).
  1238. *
  1239. * You can set the task state as follows -
  1240. *
  1241. * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
  1242. * pass before the routine returns. The routine will return 0
  1243. *
  1244. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  1245. * delivered to the current task. In this case the remaining time
  1246. * in jiffies will be returned, or 0 if the timer expired in time
  1247. *
  1248. * The current task state is guaranteed to be TASK_RUNNING when this
  1249. * routine returns.
  1250. *
  1251. * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
  1252. * the CPU away without a bound on the timeout. In this case the return
  1253. * value will be %MAX_SCHEDULE_TIMEOUT.
  1254. *
  1255. * In all cases the return value is guaranteed to be non-negative.
  1256. */
  1257. fastcall signed long __sched schedule_timeout(signed long timeout)
  1258. {
  1259. struct timer_list timer;
  1260. unsigned long expire;
  1261. switch (timeout)
  1262. {
  1263. case MAX_SCHEDULE_TIMEOUT:
  1264. /*
  1265. * These two special cases are useful to be comfortable
  1266. * in the caller. Nothing more. We could take
  1267. * MAX_SCHEDULE_TIMEOUT from one of the negative value
  1268. * but I' d like to return a valid offset (>=0) to allow
  1269. * the caller to do everything it want with the retval.
  1270. */
  1271. schedule();
  1272. goto out;
  1273. default:
  1274. /*
  1275. * Another bit of PARANOID. Note that the retval will be
  1276. * 0 since no piece of kernel is supposed to do a check
  1277. * for a negative retval of schedule_timeout() (since it
  1278. * should never happens anyway). You just have the printk()
  1279. * that will tell you if something is gone wrong and where.
  1280. */
  1281. if (timeout < 0) {
  1282. printk(KERN_ERR "schedule_timeout: wrong timeout "
  1283. "value %lx\n", timeout);
  1284. dump_stack();
  1285. current->state = TASK_RUNNING;
  1286. goto out;
  1287. }
  1288. }
  1289. expire = timeout + jiffies;
  1290. setup_timer(&timer, process_timeout, (unsigned long)current);
  1291. __mod_timer(&timer, expire);
  1292. schedule();
  1293. del_singleshot_timer_sync(&timer);
  1294. timeout = expire - jiffies;
  1295. out:
  1296. return timeout < 0 ? 0 : timeout;
  1297. }
  1298. EXPORT_SYMBOL(schedule_timeout);
  1299. /*
  1300. * We can use __set_current_state() here because schedule_timeout() calls
  1301. * schedule() unconditionally.
  1302. */
  1303. signed long __sched schedule_timeout_interruptible(signed long timeout)
  1304. {
  1305. __set_current_state(TASK_INTERRUPTIBLE);
  1306. return schedule_timeout(timeout);
  1307. }
  1308. EXPORT_SYMBOL(schedule_timeout_interruptible);
  1309. signed long __sched schedule_timeout_uninterruptible(signed long timeout)
  1310. {
  1311. __set_current_state(TASK_UNINTERRUPTIBLE);
  1312. return schedule_timeout(timeout);
  1313. }
  1314. EXPORT_SYMBOL(schedule_timeout_uninterruptible);
  1315. /* Thread ID - the internal kernel "pid" */
  1316. asmlinkage long sys_gettid(void)
  1317. {
  1318. return current->pid;
  1319. }
  1320. /**
  1321. * do_sysinfo - fill in sysinfo struct
  1322. * @info: pointer to buffer to fill
  1323. */
  1324. int do_sysinfo(struct sysinfo *info)
  1325. {
  1326. unsigned long mem_total, sav_total;
  1327. unsigned int mem_unit, bitcount;
  1328. unsigned long seq;
  1329. memset(info, 0, sizeof(struct sysinfo));
  1330. do {
  1331. struct timespec tp;
  1332. seq = read_seqbegin(&xtime_lock);
  1333. /*
  1334. * This is annoying. The below is the same thing
  1335. * posix_get_clock_monotonic() does, but it wants to
  1336. * take the lock which we want to cover the loads stuff
  1337. * too.
  1338. */
  1339. getnstimeofday(&tp);
  1340. tp.tv_sec += wall_to_monotonic.tv_sec;
  1341. tp.tv_nsec += wall_to_monotonic.tv_nsec;
  1342. if (tp.tv_nsec - NSEC_PER_SEC >= 0) {
  1343. tp.tv_nsec = tp.tv_nsec - NSEC_PER_SEC;
  1344. tp.tv_sec++;
  1345. }
  1346. info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
  1347. info->loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT);
  1348. info->loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT);
  1349. info->loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT);
  1350. info->procs = nr_threads;
  1351. } while (read_seqretry(&xtime_lock, seq));
  1352. si_meminfo(info);
  1353. si_swapinfo(info);
  1354. /*
  1355. * If the sum of all the available memory (i.e. ram + swap)
  1356. * is less than can be stored in a 32 bit unsigned long then
  1357. * we can be binary compatible with 2.2.x kernels. If not,
  1358. * well, in that case 2.2.x was broken anyways...
  1359. *
  1360. * -Erik Andersen <andersee@debian.org>
  1361. */
  1362. mem_total = info->totalram + info->totalswap;
  1363. if (mem_total < info->totalram || mem_total < info->totalswap)
  1364. goto out;
  1365. bitcount = 0;
  1366. mem_unit = info->mem_unit;
  1367. while (mem_unit > 1) {
  1368. bitcount++;
  1369. mem_unit >>= 1;
  1370. sav_total = mem_total;
  1371. mem_total <<= 1;
  1372. if (mem_total < sav_total)
  1373. goto out;
  1374. }
  1375. /*
  1376. * If mem_total did not overflow, multiply all memory values by
  1377. * info->mem_unit and set it to 1. This leaves things compatible
  1378. * with 2.2.x, and also retains compatibility with earlier 2.4.x
  1379. * kernels...
  1380. */
  1381. info->mem_unit = 1;
  1382. info->totalram <<= bitcount;
  1383. info->freeram <<= bitcount;
  1384. info->sharedram <<= bitcount;
  1385. info->bufferram <<= bitcount;
  1386. info->totalswap <<= bitcount;
  1387. info->freeswap <<= bitcount;
  1388. info->totalhigh <<= bitcount;
  1389. info->freehigh <<= bitcount;
  1390. out:
  1391. return 0;
  1392. }
  1393. asmlinkage long sys_sysinfo(struct sysinfo __user *info)
  1394. {
  1395. struct sysinfo val;
  1396. do_sysinfo(&val);
  1397. if (copy_to_user(info, &val, sizeof(struct sysinfo)))
  1398. return -EFAULT;
  1399. return 0;
  1400. }
  1401. /*
  1402. * lockdep: we want to track each per-CPU base as a separate lock-class,
  1403. * but timer-bases are kmalloc()-ed, so we need to attach separate
  1404. * keys to them:
  1405. */
  1406. static struct lock_class_key base_lock_keys[NR_CPUS];
  1407. static int __devinit init_timers_cpu(int cpu)
  1408. {
  1409. int j;
  1410. tvec_base_t *base;
  1411. static char __devinitdata tvec_base_done[NR_CPUS];
  1412. if (!tvec_base_done[cpu]) {
  1413. static char boot_done;
  1414. if (boot_done) {
  1415. /*
  1416. * The APs use this path later in boot
  1417. */
  1418. base = kmalloc_node(sizeof(*base), GFP_KERNEL,
  1419. cpu_to_node(cpu));
  1420. if (!base)
  1421. return -ENOMEM;
  1422. /* Make sure that tvec_base is 2 byte aligned */
  1423. if (tbase_get_deferrable(base)) {
  1424. WARN_ON(1);
  1425. kfree(base);
  1426. return -ENOMEM;
  1427. }
  1428. memset(base, 0, sizeof(*base));
  1429. per_cpu(tvec_bases, cpu) = base;
  1430. } else {
  1431. /*
  1432. * This is for the boot CPU - we use compile-time
  1433. * static initialisation because per-cpu memory isn't
  1434. * ready yet and because the memory allocators are not
  1435. * initialised either.
  1436. */
  1437. boot_done = 1;
  1438. base = &boot_tvec_bases;
  1439. }
  1440. tvec_base_done[cpu] = 1;
  1441. } else {
  1442. base = per_cpu(tvec_bases, cpu);
  1443. }
  1444. spin_lock_init(&base->lock);
  1445. lockdep_set_class(&base->lock, base_lock_keys + cpu);
  1446. for (j = 0; j < TVN_SIZE; j++) {
  1447. INIT_LIST_HEAD(base->tv5.vec + j);
  1448. INIT_LIST_HEAD(base->tv4.vec + j);
  1449. INIT_LIST_HEAD(base->tv3.vec + j);
  1450. INIT_LIST_HEAD(base->tv2.vec + j);
  1451. }
  1452. for (j = 0; j < TVR_SIZE; j++)
  1453. INIT_LIST_HEAD(base->tv1.vec + j);
  1454. base->timer_jiffies = jiffies;
  1455. return 0;
  1456. }
  1457. #ifdef CONFIG_HOTPLUG_CPU
  1458. static void migrate_timer_list(tvec_base_t *new_base, struct list_head *head)
  1459. {
  1460. struct timer_list *timer;
  1461. while (!list_empty(head)) {
  1462. timer = list_entry(head->next, struct timer_list, entry);
  1463. detach_timer(timer, 0);
  1464. timer_set_base(timer, new_base);
  1465. internal_add_timer(new_base, timer);
  1466. }
  1467. }
  1468. static void __devinit migrate_timers(int cpu)
  1469. {
  1470. tvec_base_t *old_base;
  1471. tvec_base_t *new_base;
  1472. int i;
  1473. BUG_ON(cpu_online(cpu));
  1474. old_base = per_cpu(tvec_bases, cpu);
  1475. new_base = get_cpu_var(tvec_bases);
  1476. local_irq_disable();
  1477. double_spin_lock(&new_base->lock, &old_base->lock,
  1478. smp_processor_id() < cpu);
  1479. BUG_ON(old_base->running_timer);
  1480. for (i = 0; i < TVR_SIZE; i++)
  1481. migrate_timer_list(new_base, old_base->tv1.vec + i);
  1482. for (i = 0; i < TVN_SIZE; i++) {
  1483. migrate_timer_list(new_base, old_base->tv2.vec + i);
  1484. migrate_timer_list(new_base, old_base->tv3.vec + i);
  1485. migrate_timer_list(new_base, old_base->tv4.vec + i);
  1486. migrate_timer_list(new_base, old_base->tv5.vec + i);
  1487. }
  1488. double_spin_unlock(&new_base->lock, &old_base->lock,
  1489. smp_processor_id() < cpu);
  1490. local_irq_enable();
  1491. put_cpu_var(tvec_bases);
  1492. }
  1493. #endif /* CONFIG_HOTPLUG_CPU */
  1494. static int __cpuinit timer_cpu_notify(struct notifier_block *self,
  1495. unsigned long action, void *hcpu)
  1496. {
  1497. long cpu = (long)hcpu;
  1498. switch(action) {
  1499. case CPU_UP_PREPARE:
  1500. if (init_timers_cpu(cpu) < 0)
  1501. return NOTIFY_BAD;
  1502. break;
  1503. #ifdef CONFIG_HOTPLUG_CPU
  1504. case CPU_DEAD:
  1505. migrate_timers(cpu);
  1506. break;
  1507. #endif
  1508. default:
  1509. break;
  1510. }
  1511. return NOTIFY_OK;
  1512. }
  1513. static struct notifier_block __cpuinitdata timers_nb = {
  1514. .notifier_call = timer_cpu_notify,
  1515. };
  1516. void __init init_timers(void)
  1517. {
  1518. int err = timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
  1519. (void *)(long)smp_processor_id());
  1520. init_timer_stats();
  1521. BUG_ON(err == NOTIFY_BAD);
  1522. register_cpu_notifier(&timers_nb);
  1523. open_softirq(TIMER_SOFTIRQ, run_timer_softirq, NULL);
  1524. }
  1525. #ifdef CONFIG_TIME_INTERPOLATION
  1526. struct time_interpolator *time_interpolator __read_mostly;
  1527. static struct time_interpolator *time_interpolator_list __read_mostly;
  1528. static DEFINE_SPINLOCK(time_interpolator_lock);
  1529. static inline cycles_t time_interpolator_get_cycles(unsigned int src)
  1530. {
  1531. unsigned long (*x)(void);
  1532. switch (src)
  1533. {
  1534. case TIME_SOURCE_FUNCTION:
  1535. x = time_interpolator->addr;
  1536. return x();
  1537. case TIME_SOURCE_MMIO64 :
  1538. return readq_relaxed((void __iomem *)time_interpolator->addr);
  1539. case TIME_SOURCE_MMIO32 :
  1540. return readl_relaxed((void __iomem *)time_interpolator->addr);
  1541. default: return get_cycles();
  1542. }
  1543. }
  1544. static inline u64 time_interpolator_get_counter(int writelock)
  1545. {
  1546. unsigned int src = time_interpolator->source;
  1547. if (time_interpolator->jitter)
  1548. {
  1549. cycles_t lcycle;
  1550. cycles_t now;
  1551. do {
  1552. lcycle = time_interpolator->last_cycle;
  1553. now = time_interpolator_get_cycles(src);
  1554. if (lcycle && time_after(lcycle, now))
  1555. return lcycle;
  1556. /* When holding the xtime write lock, there's no need
  1557. * to add the overhead of the cmpxchg. Readers are
  1558. * force to retry until the write lock is released.
  1559. */
  1560. if (writelock) {
  1561. time_interpolator->last_cycle = now;
  1562. return now;
  1563. }
  1564. /* Keep track of the last timer value returned. The use of cmpxchg here
  1565. * will cause contention in an SMP environment.
  1566. */
  1567. } while (unlikely(cmpxchg(&time_interpolator->last_cycle, lcycle, now) != lcycle));
  1568. return now;
  1569. }
  1570. else
  1571. return time_interpolator_get_cycles(src);
  1572. }
  1573. void time_interpolator_reset(void)
  1574. {
  1575. time_interpolator->offset = 0;
  1576. time_interpolator->last_counter = time_interpolator_get_counter(1);
  1577. }
  1578. #define GET_TI_NSECS(count,i) (((((count) - i->last_counter) & (i)->mask) * (i)->nsec_per_cyc) >> (i)->shift)
  1579. unsigned long time_interpolator_get_offset(void)
  1580. {
  1581. /* If we do not have a time interpolator set up then just return zero */
  1582. if (!time_interpolator)
  1583. return 0;
  1584. return time_interpolator->offset +
  1585. GET_TI_NSECS(time_interpolator_get_counter(0), time_interpolator);
  1586. }
  1587. #define INTERPOLATOR_ADJUST 65536
  1588. #define INTERPOLATOR_MAX_SKIP 10*INTERPOLATOR_ADJUST
  1589. void time_interpolator_update(long delta_nsec)
  1590. {
  1591. u64 counter;
  1592. unsigned long offset;
  1593. /* If there is no time interpolator set up then do nothing */
  1594. if (!time_interpolator)
  1595. return;
  1596. /*
  1597. * The interpolator compensates for late ticks by accumulating the late
  1598. * time in time_interpolator->offset. A tick earlier than expected will
  1599. * lead to a reset of the offset and a corresponding jump of the clock
  1600. * forward. Again this only works if the interpolator clock is running
  1601. * slightly slower than the regular clock and the tuning logic insures
  1602. * that.
  1603. */
  1604. counter = time_interpolator_get_counter(1);
  1605. offset = time_interpolator->offset +
  1606. GET_TI_NSECS(counter, time_interpolator);
  1607. if (delta_nsec < 0 || (unsigned long) delta_nsec < offset)
  1608. time_interpolator->offset = offset - delta_nsec;
  1609. else {
  1610. time_interpolator->skips++;
  1611. time_interpolator->ns_skipped += delta_nsec - offset;
  1612. time_interpolator->offset = 0;
  1613. }
  1614. time_interpolator->last_counter = counter;
  1615. /* Tuning logic for time interpolator invoked every minute or so.
  1616. * Decrease interpolator clock speed if no skips occurred and an offset is carried.
  1617. * Increase interpolator clock speed if we skip too much time.
  1618. */
  1619. if (jiffies % INTERPOLATOR_ADJUST == 0)
  1620. {
  1621. if (time_interpolator->skips == 0 && time_interpolator->offset > tick_nsec)
  1622. time_interpolator->nsec_per_cyc--;
  1623. if (time_interpolator->ns_skipped > INTERPOLATOR_MAX_SKIP && time_interpolator->offset == 0)
  1624. time_interpolator->nsec_per_cyc++;
  1625. time_interpolator->skips = 0;
  1626. time_interpolator->ns_skipped = 0;
  1627. }
  1628. }
  1629. static inline int
  1630. is_better_time_interpolator(struct time_interpolator *new)
  1631. {
  1632. if (!time_interpolator)
  1633. return 1;
  1634. return new->frequency > 2*time_interpolator->frequency ||
  1635. (unsigned long)new->drift < (unsigned long)time_interpolator->drift;
  1636. }
  1637. void
  1638. register_time_interpolator(struct time_interpolator *ti)
  1639. {
  1640. unsigned long flags;
  1641. /* Sanity check */
  1642. BUG_ON(ti->frequency == 0 || ti->mask == 0);
  1643. ti->nsec_per_cyc = ((u64)NSEC_PER_SEC << ti->shift) / ti->frequency;
  1644. spin_lock(&time_interpolator_lock);
  1645. write_seqlock_irqsave(&xtime_lock, flags);
  1646. if (is_better_time_interpolator(ti)) {
  1647. time_interpolator = ti;
  1648. time_interpolator_reset();
  1649. }
  1650. write_sequnlock_irqrestore(&xtime_lock, flags);
  1651. ti->next = time_interpolator_list;
  1652. time_interpolator_list = ti;
  1653. spin_unlock(&time_interpolator_lock);
  1654. }
  1655. void
  1656. unregister_time_interpolator(struct time_interpolator *ti)
  1657. {
  1658. struct time_interpolator *curr, **prev;
  1659. unsigned long flags;
  1660. spin_lock(&time_interpolator_lock);
  1661. prev = &time_interpolator_list;
  1662. for (curr = *prev; curr; curr = curr->next) {
  1663. if (curr == ti) {
  1664. *prev = curr->next;
  1665. break;
  1666. }
  1667. prev = &curr->next;
  1668. }
  1669. write_seqlock_irqsave(&xtime_lock, flags);
  1670. if (ti == time_interpolator) {
  1671. /* we lost the best time-interpolator: */
  1672. time_interpolator = NULL;
  1673. /* find the next-best interpolator */
  1674. for (curr = time_interpolator_list; curr; curr = curr->next)
  1675. if (is_better_time_interpolator(curr))
  1676. time_interpolator = curr;
  1677. time_interpolator_reset();
  1678. }
  1679. write_sequnlock_irqrestore(&xtime_lock, flags);
  1680. spin_unlock(&time_interpolator_lock);
  1681. }
  1682. #endif /* CONFIG_TIME_INTERPOLATION */
  1683. /**
  1684. * msleep - sleep safely even with waitqueue interruptions
  1685. * @msecs: Time in milliseconds to sleep for
  1686. */
  1687. void msleep(unsigned int msecs)
  1688. {
  1689. unsigned long timeout = msecs_to_jiffies(msecs) + 1;
  1690. while (timeout)
  1691. timeout = schedule_timeout_uninterruptible(timeout);
  1692. }
  1693. EXPORT_SYMBOL(msleep);
  1694. /**
  1695. * msleep_interruptible - sleep waiting for signals
  1696. * @msecs: Time in milliseconds to sleep for
  1697. */
  1698. unsigned long msleep_interruptible(unsigned int msecs)
  1699. {
  1700. unsigned long timeout = msecs_to_jiffies(msecs) + 1;
  1701. while (timeout && !signal_pending(current))
  1702. timeout = schedule_timeout_interruptible(timeout);
  1703. return jiffies_to_msecs(timeout);
  1704. }
  1705. EXPORT_SYMBOL(msleep_interruptible);