timer.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528
  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/pid_namespace.h>
  29. #include <linux/notifier.h>
  30. #include <linux/thread_info.h>
  31. #include <linux/time.h>
  32. #include <linux/jiffies.h>
  33. #include <linux/posix-timers.h>
  34. #include <linux/cpu.h>
  35. #include <linux/syscalls.h>
  36. #include <linux/delay.h>
  37. #include <linux/tick.h>
  38. #include <linux/kallsyms.h>
  39. #include <asm/uaccess.h>
  40. #include <asm/unistd.h>
  41. #include <asm/div64.h>
  42. #include <asm/timex.h>
  43. #include <asm/io.h>
  44. u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
  45. EXPORT_SYMBOL(jiffies_64);
  46. /*
  47. * per-CPU timer vector definitions:
  48. */
  49. #define TVN_BITS (CONFIG_BASE_SMALL ? 4 : 6)
  50. #define TVR_BITS (CONFIG_BASE_SMALL ? 6 : 8)
  51. #define TVN_SIZE (1 << TVN_BITS)
  52. #define TVR_SIZE (1 << TVR_BITS)
  53. #define TVN_MASK (TVN_SIZE - 1)
  54. #define TVR_MASK (TVR_SIZE - 1)
  55. struct tvec {
  56. struct list_head vec[TVN_SIZE];
  57. };
  58. struct tvec_root {
  59. struct list_head vec[TVR_SIZE];
  60. };
  61. struct tvec_base {
  62. spinlock_t lock;
  63. struct timer_list *running_timer;
  64. unsigned long timer_jiffies;
  65. struct tvec_root tv1;
  66. struct tvec tv2;
  67. struct tvec tv3;
  68. struct tvec tv4;
  69. struct tvec tv5;
  70. } ____cacheline_aligned;
  71. struct tvec_base boot_tvec_bases;
  72. EXPORT_SYMBOL(boot_tvec_bases);
  73. static DEFINE_PER_CPU(struct tvec_base *, tvec_bases) = &boot_tvec_bases;
  74. /*
  75. * Note that all tvec_bases are 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(struct tvec_base *base)
  82. {
  83. return ((unsigned int)(unsigned long)base & TBASE_DEFERRABLE_FLAG);
  84. }
  85. static inline struct tvec_base *tbase_get_base(struct tvec_base *base)
  86. {
  87. return ((struct tvec_base *)((unsigned long)base & ~TBASE_DEFERRABLE_FLAG));
  88. }
  89. static inline void timer_set_deferrable(struct timer_list *timer)
  90. {
  91. timer->base = ((struct tvec_base *)((unsigned long)(timer->base) |
  92. TBASE_DEFERRABLE_FLAG));
  93. }
  94. static inline void
  95. timer_set_base(struct timer_list *timer, struct tvec_base *new_base)
  96. {
  97. timer->base = (struct tvec_base *)((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(struct tvec_base *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(struct tvec_base *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. static void timer_stats_account_timer(struct timer_list *timer)
  279. {
  280. unsigned int flag = 0;
  281. if (unlikely(tbase_get_deferrable(timer->base)))
  282. flag |= TIMER_STATS_FLAG_DEFERRABLE;
  283. timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
  284. timer->function, timer->start_comm, flag);
  285. }
  286. #else
  287. static void timer_stats_account_timer(struct timer_list *timer) {}
  288. #endif
  289. #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
  290. static struct debug_obj_descr timer_debug_descr;
  291. /*
  292. * fixup_init is called when:
  293. * - an active object is initialized
  294. */
  295. static int timer_fixup_init(void *addr, enum debug_obj_state state)
  296. {
  297. struct timer_list *timer = addr;
  298. switch (state) {
  299. case ODEBUG_STATE_ACTIVE:
  300. del_timer_sync(timer);
  301. debug_object_init(timer, &timer_debug_descr);
  302. return 1;
  303. default:
  304. return 0;
  305. }
  306. }
  307. /*
  308. * fixup_activate is called when:
  309. * - an active object is activated
  310. * - an unknown object is activated (might be a statically initialized object)
  311. */
  312. static int timer_fixup_activate(void *addr, enum debug_obj_state state)
  313. {
  314. struct timer_list *timer = addr;
  315. switch (state) {
  316. case ODEBUG_STATE_NOTAVAILABLE:
  317. /*
  318. * This is not really a fixup. The timer was
  319. * statically initialized. We just make sure that it
  320. * is tracked in the object tracker.
  321. */
  322. if (timer->entry.next == NULL &&
  323. timer->entry.prev == TIMER_ENTRY_STATIC) {
  324. debug_object_init(timer, &timer_debug_descr);
  325. debug_object_activate(timer, &timer_debug_descr);
  326. return 0;
  327. } else {
  328. WARN_ON_ONCE(1);
  329. }
  330. return 0;
  331. case ODEBUG_STATE_ACTIVE:
  332. WARN_ON(1);
  333. default:
  334. return 0;
  335. }
  336. }
  337. /*
  338. * fixup_free is called when:
  339. * - an active object is freed
  340. */
  341. static int timer_fixup_free(void *addr, enum debug_obj_state state)
  342. {
  343. struct timer_list *timer = addr;
  344. switch (state) {
  345. case ODEBUG_STATE_ACTIVE:
  346. del_timer_sync(timer);
  347. debug_object_free(timer, &timer_debug_descr);
  348. return 1;
  349. default:
  350. return 0;
  351. }
  352. }
  353. static struct debug_obj_descr timer_debug_descr = {
  354. .name = "timer_list",
  355. .fixup_init = timer_fixup_init,
  356. .fixup_activate = timer_fixup_activate,
  357. .fixup_free = timer_fixup_free,
  358. };
  359. static inline void debug_timer_init(struct timer_list *timer)
  360. {
  361. debug_object_init(timer, &timer_debug_descr);
  362. }
  363. static inline void debug_timer_activate(struct timer_list *timer)
  364. {
  365. debug_object_activate(timer, &timer_debug_descr);
  366. }
  367. static inline void debug_timer_deactivate(struct timer_list *timer)
  368. {
  369. debug_object_deactivate(timer, &timer_debug_descr);
  370. }
  371. static inline void debug_timer_free(struct timer_list *timer)
  372. {
  373. debug_object_free(timer, &timer_debug_descr);
  374. }
  375. static void __init_timer(struct timer_list *timer);
  376. void init_timer_on_stack(struct timer_list *timer)
  377. {
  378. debug_object_init_on_stack(timer, &timer_debug_descr);
  379. __init_timer(timer);
  380. }
  381. EXPORT_SYMBOL_GPL(init_timer_on_stack);
  382. void destroy_timer_on_stack(struct timer_list *timer)
  383. {
  384. debug_object_free(timer, &timer_debug_descr);
  385. }
  386. EXPORT_SYMBOL_GPL(destroy_timer_on_stack);
  387. #else
  388. static inline void debug_timer_init(struct timer_list *timer) { }
  389. static inline void debug_timer_activate(struct timer_list *timer) { }
  390. static inline void debug_timer_deactivate(struct timer_list *timer) { }
  391. #endif
  392. static void __init_timer(struct timer_list *timer)
  393. {
  394. timer->entry.next = NULL;
  395. timer->base = __raw_get_cpu_var(tvec_bases);
  396. #ifdef CONFIG_TIMER_STATS
  397. timer->start_site = NULL;
  398. timer->start_pid = -1;
  399. memset(timer->start_comm, 0, TASK_COMM_LEN);
  400. #endif
  401. }
  402. /**
  403. * init_timer - initialize a timer.
  404. * @timer: the timer to be initialized
  405. *
  406. * init_timer() must be done to a timer prior calling *any* of the
  407. * other timer functions.
  408. */
  409. void init_timer(struct timer_list *timer)
  410. {
  411. debug_timer_init(timer);
  412. __init_timer(timer);
  413. }
  414. EXPORT_SYMBOL(init_timer);
  415. void init_timer_deferrable(struct timer_list *timer)
  416. {
  417. init_timer(timer);
  418. timer_set_deferrable(timer);
  419. }
  420. EXPORT_SYMBOL(init_timer_deferrable);
  421. static inline void detach_timer(struct timer_list *timer,
  422. int clear_pending)
  423. {
  424. struct list_head *entry = &timer->entry;
  425. debug_timer_deactivate(timer);
  426. __list_del(entry->prev, entry->next);
  427. if (clear_pending)
  428. entry->next = NULL;
  429. entry->prev = LIST_POISON2;
  430. }
  431. /*
  432. * We are using hashed locking: holding per_cpu(tvec_bases).lock
  433. * means that all timers which are tied to this base via timer->base are
  434. * locked, and the base itself is locked too.
  435. *
  436. * So __run_timers/migrate_timers can safely modify all timers which could
  437. * be found on ->tvX lists.
  438. *
  439. * When the timer's base is locked, and the timer removed from list, it is
  440. * possible to set timer->base = NULL and drop the lock: the timer remains
  441. * locked.
  442. */
  443. static struct tvec_base *lock_timer_base(struct timer_list *timer,
  444. unsigned long *flags)
  445. __acquires(timer->base->lock)
  446. {
  447. struct tvec_base *base;
  448. for (;;) {
  449. struct tvec_base *prelock_base = timer->base;
  450. base = tbase_get_base(prelock_base);
  451. if (likely(base != NULL)) {
  452. spin_lock_irqsave(&base->lock, *flags);
  453. if (likely(prelock_base == timer->base))
  454. return base;
  455. /* The timer has migrated to another CPU */
  456. spin_unlock_irqrestore(&base->lock, *flags);
  457. }
  458. cpu_relax();
  459. }
  460. }
  461. int __mod_timer(struct timer_list *timer, unsigned long expires)
  462. {
  463. struct tvec_base *base, *new_base;
  464. unsigned long flags;
  465. int ret = 0;
  466. timer_stats_timer_set_start_info(timer);
  467. BUG_ON(!timer->function);
  468. base = lock_timer_base(timer, &flags);
  469. if (timer_pending(timer)) {
  470. detach_timer(timer, 0);
  471. ret = 1;
  472. }
  473. debug_timer_activate(timer);
  474. new_base = __get_cpu_var(tvec_bases);
  475. if (base != new_base) {
  476. /*
  477. * We are trying to schedule the timer on the local CPU.
  478. * However we can't change timer's base while it is running,
  479. * otherwise del_timer_sync() can't detect that the timer's
  480. * handler yet has not finished. This also guarantees that
  481. * the timer is serialized wrt itself.
  482. */
  483. if (likely(base->running_timer != timer)) {
  484. /* See the comment in lock_timer_base() */
  485. timer_set_base(timer, NULL);
  486. spin_unlock(&base->lock);
  487. base = new_base;
  488. spin_lock(&base->lock);
  489. timer_set_base(timer, base);
  490. }
  491. }
  492. timer->expires = expires;
  493. internal_add_timer(base, timer);
  494. spin_unlock_irqrestore(&base->lock, flags);
  495. return ret;
  496. }
  497. EXPORT_SYMBOL(__mod_timer);
  498. /**
  499. * add_timer_on - start a timer on a particular CPU
  500. * @timer: the timer to be added
  501. * @cpu: the CPU to start it on
  502. *
  503. * This is not very scalable on SMP. Double adds are not possible.
  504. */
  505. void add_timer_on(struct timer_list *timer, int cpu)
  506. {
  507. struct tvec_base *base = per_cpu(tvec_bases, cpu);
  508. unsigned long flags;
  509. timer_stats_timer_set_start_info(timer);
  510. BUG_ON(timer_pending(timer) || !timer->function);
  511. spin_lock_irqsave(&base->lock, flags);
  512. timer_set_base(timer, base);
  513. debug_timer_activate(timer);
  514. internal_add_timer(base, timer);
  515. /*
  516. * Check whether the other CPU is idle and needs to be
  517. * triggered to reevaluate the timer wheel when nohz is
  518. * active. We are protected against the other CPU fiddling
  519. * with the timer by holding the timer base lock. This also
  520. * makes sure that a CPU on the way to idle can not evaluate
  521. * the timer wheel.
  522. */
  523. wake_up_idle_cpu(cpu);
  524. spin_unlock_irqrestore(&base->lock, flags);
  525. }
  526. /**
  527. * mod_timer - modify a timer's timeout
  528. * @timer: the timer to be modified
  529. * @expires: new timeout in jiffies
  530. *
  531. * mod_timer() is a more efficient way to update the expire field of an
  532. * active timer (if the timer is inactive it will be activated)
  533. *
  534. * mod_timer(timer, expires) is equivalent to:
  535. *
  536. * del_timer(timer); timer->expires = expires; add_timer(timer);
  537. *
  538. * Note that if there are multiple unserialized concurrent users of the
  539. * same timer, then mod_timer() is the only safe way to modify the timeout,
  540. * since add_timer() cannot modify an already running timer.
  541. *
  542. * The function returns whether it has modified a pending timer or not.
  543. * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
  544. * active timer returns 1.)
  545. */
  546. int mod_timer(struct timer_list *timer, unsigned long expires)
  547. {
  548. BUG_ON(!timer->function);
  549. timer_stats_timer_set_start_info(timer);
  550. /*
  551. * This is a common optimization triggered by the
  552. * networking code - if the timer is re-modified
  553. * to be the same thing then just return:
  554. */
  555. if (timer->expires == expires && timer_pending(timer))
  556. return 1;
  557. return __mod_timer(timer, expires);
  558. }
  559. EXPORT_SYMBOL(mod_timer);
  560. /**
  561. * del_timer - deactive a timer.
  562. * @timer: the timer to be deactivated
  563. *
  564. * del_timer() deactivates a timer - this works on both active and inactive
  565. * timers.
  566. *
  567. * The function returns whether it has deactivated a pending timer or not.
  568. * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
  569. * active timer returns 1.)
  570. */
  571. int del_timer(struct timer_list *timer)
  572. {
  573. struct tvec_base *base;
  574. unsigned long flags;
  575. int ret = 0;
  576. timer_stats_timer_clear_start_info(timer);
  577. if (timer_pending(timer)) {
  578. base = lock_timer_base(timer, &flags);
  579. if (timer_pending(timer)) {
  580. detach_timer(timer, 1);
  581. ret = 1;
  582. }
  583. spin_unlock_irqrestore(&base->lock, flags);
  584. }
  585. return ret;
  586. }
  587. EXPORT_SYMBOL(del_timer);
  588. #ifdef CONFIG_SMP
  589. /**
  590. * try_to_del_timer_sync - Try to deactivate a timer
  591. * @timer: timer do del
  592. *
  593. * This function tries to deactivate a timer. Upon successful (ret >= 0)
  594. * exit the timer is not queued and the handler is not running on any CPU.
  595. *
  596. * It must not be called from interrupt contexts.
  597. */
  598. int try_to_del_timer_sync(struct timer_list *timer)
  599. {
  600. struct tvec_base *base;
  601. unsigned long flags;
  602. int ret = -1;
  603. base = lock_timer_base(timer, &flags);
  604. if (base->running_timer == timer)
  605. goto out;
  606. ret = 0;
  607. if (timer_pending(timer)) {
  608. detach_timer(timer, 1);
  609. ret = 1;
  610. }
  611. out:
  612. spin_unlock_irqrestore(&base->lock, flags);
  613. return ret;
  614. }
  615. EXPORT_SYMBOL(try_to_del_timer_sync);
  616. /**
  617. * del_timer_sync - deactivate a timer and wait for the handler to finish.
  618. * @timer: the timer to be deactivated
  619. *
  620. * This function only differs from del_timer() on SMP: besides deactivating
  621. * the timer it also makes sure the handler has finished executing on other
  622. * CPUs.
  623. *
  624. * Synchronization rules: Callers must prevent restarting of the timer,
  625. * otherwise this function is meaningless. It must not be called from
  626. * interrupt contexts. The caller must not hold locks which would prevent
  627. * completion of the timer's handler. The timer's handler must not call
  628. * add_timer_on(). Upon exit the timer is not queued and the handler is
  629. * not running on any CPU.
  630. *
  631. * The function returns whether it has deactivated a pending timer or not.
  632. */
  633. int del_timer_sync(struct timer_list *timer)
  634. {
  635. for (;;) {
  636. int ret = try_to_del_timer_sync(timer);
  637. if (ret >= 0)
  638. return ret;
  639. cpu_relax();
  640. }
  641. }
  642. EXPORT_SYMBOL(del_timer_sync);
  643. #endif
  644. static int cascade(struct tvec_base *base, struct tvec *tv, int index)
  645. {
  646. /* cascade all the timers from tv up one level */
  647. struct timer_list *timer, *tmp;
  648. struct list_head tv_list;
  649. list_replace_init(tv->vec + index, &tv_list);
  650. /*
  651. * We are removing _all_ timers from the list, so we
  652. * don't have to detach them individually.
  653. */
  654. list_for_each_entry_safe(timer, tmp, &tv_list, entry) {
  655. BUG_ON(tbase_get_base(timer->base) != base);
  656. internal_add_timer(base, timer);
  657. }
  658. return index;
  659. }
  660. #define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK)
  661. /**
  662. * __run_timers - run all expired timers (if any) on this CPU.
  663. * @base: the timer vector to be processed.
  664. *
  665. * This function cascades all vectors and executes all expired timer
  666. * vectors.
  667. */
  668. static inline void __run_timers(struct tvec_base *base)
  669. {
  670. struct timer_list *timer;
  671. spin_lock_irq(&base->lock);
  672. while (time_after_eq(jiffies, base->timer_jiffies)) {
  673. struct list_head work_list;
  674. struct list_head *head = &work_list;
  675. int index = base->timer_jiffies & TVR_MASK;
  676. /*
  677. * Cascade timers:
  678. */
  679. if (!index &&
  680. (!cascade(base, &base->tv2, INDEX(0))) &&
  681. (!cascade(base, &base->tv3, INDEX(1))) &&
  682. !cascade(base, &base->tv4, INDEX(2)))
  683. cascade(base, &base->tv5, INDEX(3));
  684. ++base->timer_jiffies;
  685. list_replace_init(base->tv1.vec + index, &work_list);
  686. while (!list_empty(head)) {
  687. void (*fn)(unsigned long);
  688. unsigned long data;
  689. timer = list_first_entry(head, struct timer_list,entry);
  690. fn = timer->function;
  691. data = timer->data;
  692. timer_stats_account_timer(timer);
  693. set_running_timer(base, timer);
  694. detach_timer(timer, 1);
  695. spin_unlock_irq(&base->lock);
  696. {
  697. int preempt_count = preempt_count();
  698. fn(data);
  699. if (preempt_count != preempt_count()) {
  700. printk(KERN_ERR "huh, entered %p "
  701. "with preempt_count %08x, exited"
  702. " with %08x?\n",
  703. fn, preempt_count,
  704. preempt_count());
  705. BUG();
  706. }
  707. }
  708. spin_lock_irq(&base->lock);
  709. }
  710. }
  711. set_running_timer(base, NULL);
  712. spin_unlock_irq(&base->lock);
  713. }
  714. #ifdef CONFIG_NO_HZ
  715. /*
  716. * Find out when the next timer event is due to happen. This
  717. * is used on S/390 to stop all activity when a cpus is idle.
  718. * This functions needs to be called disabled.
  719. */
  720. static unsigned long __next_timer_interrupt(struct tvec_base *base)
  721. {
  722. unsigned long timer_jiffies = base->timer_jiffies;
  723. unsigned long expires = timer_jiffies + NEXT_TIMER_MAX_DELTA;
  724. int index, slot, array, found = 0;
  725. struct timer_list *nte;
  726. struct tvec *varray[4];
  727. /* Look for timer events in tv1. */
  728. index = slot = timer_jiffies & TVR_MASK;
  729. do {
  730. list_for_each_entry(nte, base->tv1.vec + slot, entry) {
  731. if (tbase_get_deferrable(nte->base))
  732. continue;
  733. found = 1;
  734. expires = nte->expires;
  735. /* Look at the cascade bucket(s)? */
  736. if (!index || slot < index)
  737. goto cascade;
  738. return expires;
  739. }
  740. slot = (slot + 1) & TVR_MASK;
  741. } while (slot != index);
  742. cascade:
  743. /* Calculate the next cascade event */
  744. if (index)
  745. timer_jiffies += TVR_SIZE - index;
  746. timer_jiffies >>= TVR_BITS;
  747. /* Check tv2-tv5. */
  748. varray[0] = &base->tv2;
  749. varray[1] = &base->tv3;
  750. varray[2] = &base->tv4;
  751. varray[3] = &base->tv5;
  752. for (array = 0; array < 4; array++) {
  753. struct tvec *varp = varray[array];
  754. index = slot = timer_jiffies & TVN_MASK;
  755. do {
  756. list_for_each_entry(nte, varp->vec + slot, entry) {
  757. found = 1;
  758. if (time_before(nte->expires, expires))
  759. expires = nte->expires;
  760. }
  761. /*
  762. * Do we still search for the first timer or are
  763. * we looking up the cascade buckets ?
  764. */
  765. if (found) {
  766. /* Look at the cascade bucket(s)? */
  767. if (!index || slot < index)
  768. break;
  769. return expires;
  770. }
  771. slot = (slot + 1) & TVN_MASK;
  772. } while (slot != index);
  773. if (index)
  774. timer_jiffies += TVN_SIZE - index;
  775. timer_jiffies >>= TVN_BITS;
  776. }
  777. return expires;
  778. }
  779. /*
  780. * Check, if the next hrtimer event is before the next timer wheel
  781. * event:
  782. */
  783. static unsigned long cmp_next_hrtimer_event(unsigned long now,
  784. unsigned long expires)
  785. {
  786. ktime_t hr_delta = hrtimer_get_next_event();
  787. struct timespec tsdelta;
  788. unsigned long delta;
  789. if (hr_delta.tv64 == KTIME_MAX)
  790. return expires;
  791. /*
  792. * Expired timer available, let it expire in the next tick
  793. */
  794. if (hr_delta.tv64 <= 0)
  795. return now + 1;
  796. tsdelta = ktime_to_timespec(hr_delta);
  797. delta = timespec_to_jiffies(&tsdelta);
  798. /*
  799. * Limit the delta to the max value, which is checked in
  800. * tick_nohz_stop_sched_tick():
  801. */
  802. if (delta > NEXT_TIMER_MAX_DELTA)
  803. delta = NEXT_TIMER_MAX_DELTA;
  804. /*
  805. * Take rounding errors in to account and make sure, that it
  806. * expires in the next tick. Otherwise we go into an endless
  807. * ping pong due to tick_nohz_stop_sched_tick() retriggering
  808. * the timer softirq
  809. */
  810. if (delta < 1)
  811. delta = 1;
  812. now += delta;
  813. if (time_before(now, expires))
  814. return now;
  815. return expires;
  816. }
  817. /**
  818. * get_next_timer_interrupt - return the jiffy of the next pending timer
  819. * @now: current time (in jiffies)
  820. */
  821. unsigned long get_next_timer_interrupt(unsigned long now)
  822. {
  823. struct tvec_base *base = __get_cpu_var(tvec_bases);
  824. unsigned long expires;
  825. spin_lock(&base->lock);
  826. expires = __next_timer_interrupt(base);
  827. spin_unlock(&base->lock);
  828. if (time_before_eq(expires, now))
  829. return now;
  830. return cmp_next_hrtimer_event(now, expires);
  831. }
  832. #endif
  833. #ifndef CONFIG_VIRT_CPU_ACCOUNTING
  834. void account_process_tick(struct task_struct *p, int user_tick)
  835. {
  836. cputime_t one_jiffy = jiffies_to_cputime(1);
  837. if (user_tick) {
  838. account_user_time(p, one_jiffy);
  839. account_user_time_scaled(p, cputime_to_scaled(one_jiffy));
  840. } else {
  841. account_system_time(p, HARDIRQ_OFFSET, one_jiffy);
  842. account_system_time_scaled(p, cputime_to_scaled(one_jiffy));
  843. }
  844. }
  845. #endif
  846. /*
  847. * Called from the timer interrupt handler to charge one tick to the current
  848. * process. user_tick is 1 if the tick is user time, 0 for system.
  849. */
  850. void update_process_times(int user_tick)
  851. {
  852. struct task_struct *p = current;
  853. int cpu = smp_processor_id();
  854. /* Note: this timer irq context must be accounted for as well. */
  855. account_process_tick(p, user_tick);
  856. run_local_timers();
  857. if (rcu_pending(cpu))
  858. rcu_check_callbacks(cpu, user_tick);
  859. printk_tick();
  860. scheduler_tick();
  861. run_posix_cpu_timers(p);
  862. }
  863. /*
  864. * Nr of active tasks - counted in fixed-point numbers
  865. */
  866. static unsigned long count_active_tasks(void)
  867. {
  868. return nr_active() * FIXED_1;
  869. }
  870. /*
  871. * Hmm.. Changed this, as the GNU make sources (load.c) seems to
  872. * imply that avenrun[] is the standard name for this kind of thing.
  873. * Nothing else seems to be standardized: the fractional size etc
  874. * all seem to differ on different machines.
  875. *
  876. * Requires xtime_lock to access.
  877. */
  878. unsigned long avenrun[3];
  879. EXPORT_SYMBOL(avenrun);
  880. /*
  881. * calc_load - given tick count, update the avenrun load estimates.
  882. * This is called while holding a write_lock on xtime_lock.
  883. */
  884. static inline void calc_load(unsigned long ticks)
  885. {
  886. unsigned long active_tasks; /* fixed-point */
  887. static int count = LOAD_FREQ;
  888. count -= ticks;
  889. if (unlikely(count < 0)) {
  890. active_tasks = count_active_tasks();
  891. do {
  892. CALC_LOAD(avenrun[0], EXP_1, active_tasks);
  893. CALC_LOAD(avenrun[1], EXP_5, active_tasks);
  894. CALC_LOAD(avenrun[2], EXP_15, active_tasks);
  895. count += LOAD_FREQ;
  896. } while (count < 0);
  897. }
  898. }
  899. /*
  900. * This function runs timers and the timer-tq in bottom half context.
  901. */
  902. static void run_timer_softirq(struct softirq_action *h)
  903. {
  904. struct tvec_base *base = __get_cpu_var(tvec_bases);
  905. hrtimer_run_pending();
  906. if (time_after_eq(jiffies, base->timer_jiffies))
  907. __run_timers(base);
  908. }
  909. /*
  910. * Called by the local, per-CPU timer interrupt on SMP.
  911. */
  912. void run_local_timers(void)
  913. {
  914. hrtimer_run_queues();
  915. raise_softirq(TIMER_SOFTIRQ);
  916. softlockup_tick();
  917. }
  918. /*
  919. * Called by the timer interrupt. xtime_lock must already be taken
  920. * by the timer IRQ!
  921. */
  922. static inline void update_times(unsigned long ticks)
  923. {
  924. update_wall_time();
  925. calc_load(ticks);
  926. }
  927. /*
  928. * The 64-bit jiffies value is not atomic - you MUST NOT read it
  929. * without sampling the sequence number in xtime_lock.
  930. * jiffies is defined in the linker script...
  931. */
  932. void do_timer(unsigned long ticks)
  933. {
  934. jiffies_64 += ticks;
  935. update_times(ticks);
  936. }
  937. #ifdef __ARCH_WANT_SYS_ALARM
  938. /*
  939. * For backwards compatibility? This can be done in libc so Alpha
  940. * and all newer ports shouldn't need it.
  941. */
  942. asmlinkage unsigned long sys_alarm(unsigned int seconds)
  943. {
  944. return alarm_setitimer(seconds);
  945. }
  946. #endif
  947. #ifndef __alpha__
  948. /*
  949. * The Alpha uses getxpid, getxuid, and getxgid instead. Maybe this
  950. * should be moved into arch/i386 instead?
  951. */
  952. /**
  953. * sys_getpid - return the thread group id of the current process
  954. *
  955. * Note, despite the name, this returns the tgid not the pid. The tgid and
  956. * the pid are identical unless CLONE_THREAD was specified on clone() in
  957. * which case the tgid is the same in all threads of the same group.
  958. *
  959. * This is SMP safe as current->tgid does not change.
  960. */
  961. asmlinkage long sys_getpid(void)
  962. {
  963. return task_tgid_vnr(current);
  964. }
  965. /*
  966. * Accessing ->real_parent is not SMP-safe, it could
  967. * change from under us. However, we can use a stale
  968. * value of ->real_parent under rcu_read_lock(), see
  969. * release_task()->call_rcu(delayed_put_task_struct).
  970. */
  971. asmlinkage long sys_getppid(void)
  972. {
  973. int pid;
  974. rcu_read_lock();
  975. pid = task_tgid_vnr(current->real_parent);
  976. rcu_read_unlock();
  977. return pid;
  978. }
  979. asmlinkage long sys_getuid(void)
  980. {
  981. /* Only we change this so SMP safe */
  982. return current->uid;
  983. }
  984. asmlinkage long sys_geteuid(void)
  985. {
  986. /* Only we change this so SMP safe */
  987. return current->euid;
  988. }
  989. asmlinkage long sys_getgid(void)
  990. {
  991. /* Only we change this so SMP safe */
  992. return current->gid;
  993. }
  994. asmlinkage long sys_getegid(void)
  995. {
  996. /* Only we change this so SMP safe */
  997. return current->egid;
  998. }
  999. #endif
  1000. static void process_timeout(unsigned long __data)
  1001. {
  1002. wake_up_process((struct task_struct *)__data);
  1003. }
  1004. /**
  1005. * schedule_timeout - sleep until timeout
  1006. * @timeout: timeout value in jiffies
  1007. *
  1008. * Make the current task sleep until @timeout jiffies have
  1009. * elapsed. The routine will return immediately unless
  1010. * the current task state has been set (see set_current_state()).
  1011. *
  1012. * You can set the task state as follows -
  1013. *
  1014. * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
  1015. * pass before the routine returns. The routine will return 0
  1016. *
  1017. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  1018. * delivered to the current task. In this case the remaining time
  1019. * in jiffies will be returned, or 0 if the timer expired in time
  1020. *
  1021. * The current task state is guaranteed to be TASK_RUNNING when this
  1022. * routine returns.
  1023. *
  1024. * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
  1025. * the CPU away without a bound on the timeout. In this case the return
  1026. * value will be %MAX_SCHEDULE_TIMEOUT.
  1027. *
  1028. * In all cases the return value is guaranteed to be non-negative.
  1029. */
  1030. signed long __sched schedule_timeout(signed long timeout)
  1031. {
  1032. struct timer_list timer;
  1033. unsigned long expire;
  1034. switch (timeout)
  1035. {
  1036. case MAX_SCHEDULE_TIMEOUT:
  1037. /*
  1038. * These two special cases are useful to be comfortable
  1039. * in the caller. Nothing more. We could take
  1040. * MAX_SCHEDULE_TIMEOUT from one of the negative value
  1041. * but I' d like to return a valid offset (>=0) to allow
  1042. * the caller to do everything it want with the retval.
  1043. */
  1044. schedule();
  1045. goto out;
  1046. default:
  1047. /*
  1048. * Another bit of PARANOID. Note that the retval will be
  1049. * 0 since no piece of kernel is supposed to do a check
  1050. * for a negative retval of schedule_timeout() (since it
  1051. * should never happens anyway). You just have the printk()
  1052. * that will tell you if something is gone wrong and where.
  1053. */
  1054. if (timeout < 0) {
  1055. printk(KERN_ERR "schedule_timeout: wrong timeout "
  1056. "value %lx\n", timeout);
  1057. dump_stack();
  1058. current->state = TASK_RUNNING;
  1059. goto out;
  1060. }
  1061. }
  1062. expire = timeout + jiffies;
  1063. setup_timer_on_stack(&timer, process_timeout, (unsigned long)current);
  1064. __mod_timer(&timer, expire);
  1065. schedule();
  1066. del_singleshot_timer_sync(&timer);
  1067. /* Remove the timer from the object tracker */
  1068. destroy_timer_on_stack(&timer);
  1069. timeout = expire - jiffies;
  1070. out:
  1071. return timeout < 0 ? 0 : timeout;
  1072. }
  1073. EXPORT_SYMBOL(schedule_timeout);
  1074. /*
  1075. * We can use __set_current_state() here because schedule_timeout() calls
  1076. * schedule() unconditionally.
  1077. */
  1078. signed long __sched schedule_timeout_interruptible(signed long timeout)
  1079. {
  1080. __set_current_state(TASK_INTERRUPTIBLE);
  1081. return schedule_timeout(timeout);
  1082. }
  1083. EXPORT_SYMBOL(schedule_timeout_interruptible);
  1084. signed long __sched schedule_timeout_killable(signed long timeout)
  1085. {
  1086. __set_current_state(TASK_KILLABLE);
  1087. return schedule_timeout(timeout);
  1088. }
  1089. EXPORT_SYMBOL(schedule_timeout_killable);
  1090. signed long __sched schedule_timeout_uninterruptible(signed long timeout)
  1091. {
  1092. __set_current_state(TASK_UNINTERRUPTIBLE);
  1093. return schedule_timeout(timeout);
  1094. }
  1095. EXPORT_SYMBOL(schedule_timeout_uninterruptible);
  1096. /* Thread ID - the internal kernel "pid" */
  1097. asmlinkage long sys_gettid(void)
  1098. {
  1099. return task_pid_vnr(current);
  1100. }
  1101. /**
  1102. * do_sysinfo - fill in sysinfo struct
  1103. * @info: pointer to buffer to fill
  1104. */
  1105. int do_sysinfo(struct sysinfo *info)
  1106. {
  1107. unsigned long mem_total, sav_total;
  1108. unsigned int mem_unit, bitcount;
  1109. unsigned long seq;
  1110. memset(info, 0, sizeof(struct sysinfo));
  1111. do {
  1112. struct timespec tp;
  1113. seq = read_seqbegin(&xtime_lock);
  1114. /*
  1115. * This is annoying. The below is the same thing
  1116. * posix_get_clock_monotonic() does, but it wants to
  1117. * take the lock which we want to cover the loads stuff
  1118. * too.
  1119. */
  1120. getnstimeofday(&tp);
  1121. tp.tv_sec += wall_to_monotonic.tv_sec;
  1122. tp.tv_nsec += wall_to_monotonic.tv_nsec;
  1123. monotonic_to_bootbased(&tp);
  1124. if (tp.tv_nsec - NSEC_PER_SEC >= 0) {
  1125. tp.tv_nsec = tp.tv_nsec - NSEC_PER_SEC;
  1126. tp.tv_sec++;
  1127. }
  1128. info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
  1129. info->loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT);
  1130. info->loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT);
  1131. info->loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT);
  1132. info->procs = nr_threads;
  1133. } while (read_seqretry(&xtime_lock, seq));
  1134. si_meminfo(info);
  1135. si_swapinfo(info);
  1136. /*
  1137. * If the sum of all the available memory (i.e. ram + swap)
  1138. * is less than can be stored in a 32 bit unsigned long then
  1139. * we can be binary compatible with 2.2.x kernels. If not,
  1140. * well, in that case 2.2.x was broken anyways...
  1141. *
  1142. * -Erik Andersen <andersee@debian.org>
  1143. */
  1144. mem_total = info->totalram + info->totalswap;
  1145. if (mem_total < info->totalram || mem_total < info->totalswap)
  1146. goto out;
  1147. bitcount = 0;
  1148. mem_unit = info->mem_unit;
  1149. while (mem_unit > 1) {
  1150. bitcount++;
  1151. mem_unit >>= 1;
  1152. sav_total = mem_total;
  1153. mem_total <<= 1;
  1154. if (mem_total < sav_total)
  1155. goto out;
  1156. }
  1157. /*
  1158. * If mem_total did not overflow, multiply all memory values by
  1159. * info->mem_unit and set it to 1. This leaves things compatible
  1160. * with 2.2.x, and also retains compatibility with earlier 2.4.x
  1161. * kernels...
  1162. */
  1163. info->mem_unit = 1;
  1164. info->totalram <<= bitcount;
  1165. info->freeram <<= bitcount;
  1166. info->sharedram <<= bitcount;
  1167. info->bufferram <<= bitcount;
  1168. info->totalswap <<= bitcount;
  1169. info->freeswap <<= bitcount;
  1170. info->totalhigh <<= bitcount;
  1171. info->freehigh <<= bitcount;
  1172. out:
  1173. return 0;
  1174. }
  1175. asmlinkage long sys_sysinfo(struct sysinfo __user *info)
  1176. {
  1177. struct sysinfo val;
  1178. do_sysinfo(&val);
  1179. if (copy_to_user(info, &val, sizeof(struct sysinfo)))
  1180. return -EFAULT;
  1181. return 0;
  1182. }
  1183. static int __cpuinit init_timers_cpu(int cpu)
  1184. {
  1185. int j;
  1186. struct tvec_base *base;
  1187. static char __cpuinitdata tvec_base_done[NR_CPUS];
  1188. if (!tvec_base_done[cpu]) {
  1189. static char boot_done;
  1190. if (boot_done) {
  1191. /*
  1192. * The APs use this path later in boot
  1193. */
  1194. base = kmalloc_node(sizeof(*base),
  1195. GFP_KERNEL | __GFP_ZERO,
  1196. cpu_to_node(cpu));
  1197. if (!base)
  1198. return -ENOMEM;
  1199. /* Make sure that tvec_base is 2 byte aligned */
  1200. if (tbase_get_deferrable(base)) {
  1201. WARN_ON(1);
  1202. kfree(base);
  1203. return -ENOMEM;
  1204. }
  1205. per_cpu(tvec_bases, cpu) = base;
  1206. } else {
  1207. /*
  1208. * This is for the boot CPU - we use compile-time
  1209. * static initialisation because per-cpu memory isn't
  1210. * ready yet and because the memory allocators are not
  1211. * initialised either.
  1212. */
  1213. boot_done = 1;
  1214. base = &boot_tvec_bases;
  1215. }
  1216. tvec_base_done[cpu] = 1;
  1217. } else {
  1218. base = per_cpu(tvec_bases, cpu);
  1219. }
  1220. spin_lock_init(&base->lock);
  1221. for (j = 0; j < TVN_SIZE; j++) {
  1222. INIT_LIST_HEAD(base->tv5.vec + j);
  1223. INIT_LIST_HEAD(base->tv4.vec + j);
  1224. INIT_LIST_HEAD(base->tv3.vec + j);
  1225. INIT_LIST_HEAD(base->tv2.vec + j);
  1226. }
  1227. for (j = 0; j < TVR_SIZE; j++)
  1228. INIT_LIST_HEAD(base->tv1.vec + j);
  1229. base->timer_jiffies = jiffies;
  1230. return 0;
  1231. }
  1232. #ifdef CONFIG_HOTPLUG_CPU
  1233. static void migrate_timer_list(struct tvec_base *new_base, struct list_head *head)
  1234. {
  1235. struct timer_list *timer;
  1236. while (!list_empty(head)) {
  1237. timer = list_first_entry(head, struct timer_list, entry);
  1238. detach_timer(timer, 0);
  1239. timer_set_base(timer, new_base);
  1240. internal_add_timer(new_base, timer);
  1241. }
  1242. }
  1243. static void __cpuinit migrate_timers(int cpu)
  1244. {
  1245. struct tvec_base *old_base;
  1246. struct tvec_base *new_base;
  1247. int i;
  1248. BUG_ON(cpu_online(cpu));
  1249. old_base = per_cpu(tvec_bases, cpu);
  1250. new_base = get_cpu_var(tvec_bases);
  1251. local_irq_disable();
  1252. spin_lock(&new_base->lock);
  1253. spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
  1254. BUG_ON(old_base->running_timer);
  1255. for (i = 0; i < TVR_SIZE; i++)
  1256. migrate_timer_list(new_base, old_base->tv1.vec + i);
  1257. for (i = 0; i < TVN_SIZE; i++) {
  1258. migrate_timer_list(new_base, old_base->tv2.vec + i);
  1259. migrate_timer_list(new_base, old_base->tv3.vec + i);
  1260. migrate_timer_list(new_base, old_base->tv4.vec + i);
  1261. migrate_timer_list(new_base, old_base->tv5.vec + i);
  1262. }
  1263. spin_unlock(&old_base->lock);
  1264. spin_unlock(&new_base->lock);
  1265. local_irq_enable();
  1266. put_cpu_var(tvec_bases);
  1267. }
  1268. #endif /* CONFIG_HOTPLUG_CPU */
  1269. static int __cpuinit timer_cpu_notify(struct notifier_block *self,
  1270. unsigned long action, void *hcpu)
  1271. {
  1272. long cpu = (long)hcpu;
  1273. switch(action) {
  1274. case CPU_UP_PREPARE:
  1275. case CPU_UP_PREPARE_FROZEN:
  1276. if (init_timers_cpu(cpu) < 0)
  1277. return NOTIFY_BAD;
  1278. break;
  1279. #ifdef CONFIG_HOTPLUG_CPU
  1280. case CPU_DEAD:
  1281. case CPU_DEAD_FROZEN:
  1282. migrate_timers(cpu);
  1283. break;
  1284. #endif
  1285. default:
  1286. break;
  1287. }
  1288. return NOTIFY_OK;
  1289. }
  1290. static struct notifier_block __cpuinitdata timers_nb = {
  1291. .notifier_call = timer_cpu_notify,
  1292. };
  1293. void __init init_timers(void)
  1294. {
  1295. int err = timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
  1296. (void *)(long)smp_processor_id());
  1297. init_timer_stats();
  1298. BUG_ON(err == NOTIFY_BAD);
  1299. register_cpu_notifier(&timers_nb);
  1300. open_softirq(TIMER_SOFTIRQ, run_timer_softirq);
  1301. }
  1302. /**
  1303. * msleep - sleep safely even with waitqueue interruptions
  1304. * @msecs: Time in milliseconds to sleep for
  1305. */
  1306. void msleep(unsigned int msecs)
  1307. {
  1308. unsigned long timeout = msecs_to_jiffies(msecs) + 1;
  1309. while (timeout)
  1310. timeout = schedule_timeout_uninterruptible(timeout);
  1311. }
  1312. EXPORT_SYMBOL(msleep);
  1313. /**
  1314. * msleep_interruptible - sleep waiting for signals
  1315. * @msecs: Time in milliseconds to sleep for
  1316. */
  1317. unsigned long msleep_interruptible(unsigned int msecs)
  1318. {
  1319. unsigned long timeout = msecs_to_jiffies(msecs) + 1;
  1320. while (timeout && !signal_pending(current))
  1321. timeout = schedule_timeout_interruptible(timeout);
  1322. return jiffies_to_msecs(timeout);
  1323. }
  1324. EXPORT_SYMBOL(msleep_interruptible);