timer.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535
  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. #if defined(CONFIG_NO_IDLE_HZ) || defined(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. #ifdef CONFIG_NO_IDLE_HZ
  833. unsigned long next_timer_interrupt(void)
  834. {
  835. return get_next_timer_interrupt(jiffies);
  836. }
  837. #endif
  838. #endif
  839. #ifndef CONFIG_VIRT_CPU_ACCOUNTING
  840. void account_process_tick(struct task_struct *p, int user_tick)
  841. {
  842. cputime_t one_jiffy = jiffies_to_cputime(1);
  843. if (user_tick) {
  844. account_user_time(p, one_jiffy);
  845. account_user_time_scaled(p, cputime_to_scaled(one_jiffy));
  846. } else {
  847. account_system_time(p, HARDIRQ_OFFSET, one_jiffy);
  848. account_system_time_scaled(p, cputime_to_scaled(one_jiffy));
  849. }
  850. }
  851. #endif
  852. /*
  853. * Called from the timer interrupt handler to charge one tick to the current
  854. * process. user_tick is 1 if the tick is user time, 0 for system.
  855. */
  856. void update_process_times(int user_tick)
  857. {
  858. struct task_struct *p = current;
  859. int cpu = smp_processor_id();
  860. /* Note: this timer irq context must be accounted for as well. */
  861. account_process_tick(p, user_tick);
  862. run_local_timers();
  863. if (rcu_pending(cpu))
  864. rcu_check_callbacks(cpu, user_tick);
  865. scheduler_tick();
  866. run_posix_cpu_timers(p);
  867. }
  868. /*
  869. * Nr of active tasks - counted in fixed-point numbers
  870. */
  871. static unsigned long count_active_tasks(void)
  872. {
  873. return nr_active() * FIXED_1;
  874. }
  875. /*
  876. * Hmm.. Changed this, as the GNU make sources (load.c) seems to
  877. * imply that avenrun[] is the standard name for this kind of thing.
  878. * Nothing else seems to be standardized: the fractional size etc
  879. * all seem to differ on different machines.
  880. *
  881. * Requires xtime_lock to access.
  882. */
  883. unsigned long avenrun[3];
  884. EXPORT_SYMBOL(avenrun);
  885. /*
  886. * calc_load - given tick count, update the avenrun load estimates.
  887. * This is called while holding a write_lock on xtime_lock.
  888. */
  889. static inline void calc_load(unsigned long ticks)
  890. {
  891. unsigned long active_tasks; /* fixed-point */
  892. static int count = LOAD_FREQ;
  893. count -= ticks;
  894. if (unlikely(count < 0)) {
  895. active_tasks = count_active_tasks();
  896. do {
  897. CALC_LOAD(avenrun[0], EXP_1, active_tasks);
  898. CALC_LOAD(avenrun[1], EXP_5, active_tasks);
  899. CALC_LOAD(avenrun[2], EXP_15, active_tasks);
  900. count += LOAD_FREQ;
  901. } while (count < 0);
  902. }
  903. }
  904. /*
  905. * This function runs timers and the timer-tq in bottom half context.
  906. */
  907. static void run_timer_softirq(struct softirq_action *h)
  908. {
  909. struct tvec_base *base = __get_cpu_var(tvec_bases);
  910. hrtimer_run_pending();
  911. if (time_after_eq(jiffies, base->timer_jiffies))
  912. __run_timers(base);
  913. }
  914. /*
  915. * Called by the local, per-CPU timer interrupt on SMP.
  916. */
  917. void run_local_timers(void)
  918. {
  919. hrtimer_run_queues();
  920. raise_softirq(TIMER_SOFTIRQ);
  921. softlockup_tick();
  922. }
  923. /*
  924. * Called by the timer interrupt. xtime_lock must already be taken
  925. * by the timer IRQ!
  926. */
  927. static inline void update_times(unsigned long ticks)
  928. {
  929. update_wall_time();
  930. calc_load(ticks);
  931. }
  932. /*
  933. * The 64-bit jiffies value is not atomic - you MUST NOT read it
  934. * without sampling the sequence number in xtime_lock.
  935. * jiffies is defined in the linker script...
  936. */
  937. void do_timer(unsigned long ticks)
  938. {
  939. jiffies_64 += ticks;
  940. update_times(ticks);
  941. }
  942. #ifdef __ARCH_WANT_SYS_ALARM
  943. /*
  944. * For backwards compatibility? This can be done in libc so Alpha
  945. * and all newer ports shouldn't need it.
  946. */
  947. asmlinkage unsigned long sys_alarm(unsigned int seconds)
  948. {
  949. return alarm_setitimer(seconds);
  950. }
  951. #endif
  952. #ifndef __alpha__
  953. /*
  954. * The Alpha uses getxpid, getxuid, and getxgid instead. Maybe this
  955. * should be moved into arch/i386 instead?
  956. */
  957. /**
  958. * sys_getpid - return the thread group id of the current process
  959. *
  960. * Note, despite the name, this returns the tgid not the pid. The tgid and
  961. * the pid are identical unless CLONE_THREAD was specified on clone() in
  962. * which case the tgid is the same in all threads of the same group.
  963. *
  964. * This is SMP safe as current->tgid does not change.
  965. */
  966. asmlinkage long sys_getpid(void)
  967. {
  968. return task_tgid_vnr(current);
  969. }
  970. /*
  971. * Accessing ->real_parent is not SMP-safe, it could
  972. * change from under us. However, we can use a stale
  973. * value of ->real_parent under rcu_read_lock(), see
  974. * release_task()->call_rcu(delayed_put_task_struct).
  975. */
  976. asmlinkage long sys_getppid(void)
  977. {
  978. int pid;
  979. rcu_read_lock();
  980. pid = task_tgid_vnr(current->real_parent);
  981. rcu_read_unlock();
  982. return pid;
  983. }
  984. asmlinkage long sys_getuid(void)
  985. {
  986. /* Only we change this so SMP safe */
  987. return current->uid;
  988. }
  989. asmlinkage long sys_geteuid(void)
  990. {
  991. /* Only we change this so SMP safe */
  992. return current->euid;
  993. }
  994. asmlinkage long sys_getgid(void)
  995. {
  996. /* Only we change this so SMP safe */
  997. return current->gid;
  998. }
  999. asmlinkage long sys_getegid(void)
  1000. {
  1001. /* Only we change this so SMP safe */
  1002. return current->egid;
  1003. }
  1004. #endif
  1005. static void process_timeout(unsigned long __data)
  1006. {
  1007. wake_up_process((struct task_struct *)__data);
  1008. }
  1009. /**
  1010. * schedule_timeout - sleep until timeout
  1011. * @timeout: timeout value in jiffies
  1012. *
  1013. * Make the current task sleep until @timeout jiffies have
  1014. * elapsed. The routine will return immediately unless
  1015. * the current task state has been set (see set_current_state()).
  1016. *
  1017. * You can set the task state as follows -
  1018. *
  1019. * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
  1020. * pass before the routine returns. The routine will return 0
  1021. *
  1022. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  1023. * delivered to the current task. In this case the remaining time
  1024. * in jiffies will be returned, or 0 if the timer expired in time
  1025. *
  1026. * The current task state is guaranteed to be TASK_RUNNING when this
  1027. * routine returns.
  1028. *
  1029. * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
  1030. * the CPU away without a bound on the timeout. In this case the return
  1031. * value will be %MAX_SCHEDULE_TIMEOUT.
  1032. *
  1033. * In all cases the return value is guaranteed to be non-negative.
  1034. */
  1035. signed long __sched schedule_timeout(signed long timeout)
  1036. {
  1037. struct timer_list timer;
  1038. unsigned long expire;
  1039. switch (timeout)
  1040. {
  1041. case MAX_SCHEDULE_TIMEOUT:
  1042. /*
  1043. * These two special cases are useful to be comfortable
  1044. * in the caller. Nothing more. We could take
  1045. * MAX_SCHEDULE_TIMEOUT from one of the negative value
  1046. * but I' d like to return a valid offset (>=0) to allow
  1047. * the caller to do everything it want with the retval.
  1048. */
  1049. schedule();
  1050. goto out;
  1051. default:
  1052. /*
  1053. * Another bit of PARANOID. Note that the retval will be
  1054. * 0 since no piece of kernel is supposed to do a check
  1055. * for a negative retval of schedule_timeout() (since it
  1056. * should never happens anyway). You just have the printk()
  1057. * that will tell you if something is gone wrong and where.
  1058. */
  1059. if (timeout < 0) {
  1060. printk(KERN_ERR "schedule_timeout: wrong timeout "
  1061. "value %lx\n", timeout);
  1062. dump_stack();
  1063. current->state = TASK_RUNNING;
  1064. goto out;
  1065. }
  1066. }
  1067. expire = timeout + jiffies;
  1068. setup_timer_on_stack(&timer, process_timeout, (unsigned long)current);
  1069. __mod_timer(&timer, expire);
  1070. schedule();
  1071. del_singleshot_timer_sync(&timer);
  1072. /* Remove the timer from the object tracker */
  1073. destroy_timer_on_stack(&timer);
  1074. timeout = expire - jiffies;
  1075. out:
  1076. return timeout < 0 ? 0 : timeout;
  1077. }
  1078. EXPORT_SYMBOL(schedule_timeout);
  1079. /*
  1080. * We can use __set_current_state() here because schedule_timeout() calls
  1081. * schedule() unconditionally.
  1082. */
  1083. signed long __sched schedule_timeout_interruptible(signed long timeout)
  1084. {
  1085. __set_current_state(TASK_INTERRUPTIBLE);
  1086. return schedule_timeout(timeout);
  1087. }
  1088. EXPORT_SYMBOL(schedule_timeout_interruptible);
  1089. signed long __sched schedule_timeout_killable(signed long timeout)
  1090. {
  1091. __set_current_state(TASK_KILLABLE);
  1092. return schedule_timeout(timeout);
  1093. }
  1094. EXPORT_SYMBOL(schedule_timeout_killable);
  1095. signed long __sched schedule_timeout_uninterruptible(signed long timeout)
  1096. {
  1097. __set_current_state(TASK_UNINTERRUPTIBLE);
  1098. return schedule_timeout(timeout);
  1099. }
  1100. EXPORT_SYMBOL(schedule_timeout_uninterruptible);
  1101. /* Thread ID - the internal kernel "pid" */
  1102. asmlinkage long sys_gettid(void)
  1103. {
  1104. return task_pid_vnr(current);
  1105. }
  1106. /**
  1107. * do_sysinfo - fill in sysinfo struct
  1108. * @info: pointer to buffer to fill
  1109. */
  1110. int do_sysinfo(struct sysinfo *info)
  1111. {
  1112. unsigned long mem_total, sav_total;
  1113. unsigned int mem_unit, bitcount;
  1114. unsigned long seq;
  1115. memset(info, 0, sizeof(struct sysinfo));
  1116. do {
  1117. struct timespec tp;
  1118. seq = read_seqbegin(&xtime_lock);
  1119. /*
  1120. * This is annoying. The below is the same thing
  1121. * posix_get_clock_monotonic() does, but it wants to
  1122. * take the lock which we want to cover the loads stuff
  1123. * too.
  1124. */
  1125. getnstimeofday(&tp);
  1126. tp.tv_sec += wall_to_monotonic.tv_sec;
  1127. tp.tv_nsec += wall_to_monotonic.tv_nsec;
  1128. monotonic_to_bootbased(&tp);
  1129. if (tp.tv_nsec - NSEC_PER_SEC >= 0) {
  1130. tp.tv_nsec = tp.tv_nsec - NSEC_PER_SEC;
  1131. tp.tv_sec++;
  1132. }
  1133. info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
  1134. info->loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT);
  1135. info->loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT);
  1136. info->loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT);
  1137. info->procs = nr_threads;
  1138. } while (read_seqretry(&xtime_lock, seq));
  1139. si_meminfo(info);
  1140. si_swapinfo(info);
  1141. /*
  1142. * If the sum of all the available memory (i.e. ram + swap)
  1143. * is less than can be stored in a 32 bit unsigned long then
  1144. * we can be binary compatible with 2.2.x kernels. If not,
  1145. * well, in that case 2.2.x was broken anyways...
  1146. *
  1147. * -Erik Andersen <andersee@debian.org>
  1148. */
  1149. mem_total = info->totalram + info->totalswap;
  1150. if (mem_total < info->totalram || mem_total < info->totalswap)
  1151. goto out;
  1152. bitcount = 0;
  1153. mem_unit = info->mem_unit;
  1154. while (mem_unit > 1) {
  1155. bitcount++;
  1156. mem_unit >>= 1;
  1157. sav_total = mem_total;
  1158. mem_total <<= 1;
  1159. if (mem_total < sav_total)
  1160. goto out;
  1161. }
  1162. /*
  1163. * If mem_total did not overflow, multiply all memory values by
  1164. * info->mem_unit and set it to 1. This leaves things compatible
  1165. * with 2.2.x, and also retains compatibility with earlier 2.4.x
  1166. * kernels...
  1167. */
  1168. info->mem_unit = 1;
  1169. info->totalram <<= bitcount;
  1170. info->freeram <<= bitcount;
  1171. info->sharedram <<= bitcount;
  1172. info->bufferram <<= bitcount;
  1173. info->totalswap <<= bitcount;
  1174. info->freeswap <<= bitcount;
  1175. info->totalhigh <<= bitcount;
  1176. info->freehigh <<= bitcount;
  1177. out:
  1178. return 0;
  1179. }
  1180. asmlinkage long sys_sysinfo(struct sysinfo __user *info)
  1181. {
  1182. struct sysinfo val;
  1183. do_sysinfo(&val);
  1184. if (copy_to_user(info, &val, sizeof(struct sysinfo)))
  1185. return -EFAULT;
  1186. return 0;
  1187. }
  1188. static int __cpuinit init_timers_cpu(int cpu)
  1189. {
  1190. int j;
  1191. struct tvec_base *base;
  1192. static char __cpuinitdata tvec_base_done[NR_CPUS];
  1193. if (!tvec_base_done[cpu]) {
  1194. static char boot_done;
  1195. if (boot_done) {
  1196. /*
  1197. * The APs use this path later in boot
  1198. */
  1199. base = kmalloc_node(sizeof(*base),
  1200. GFP_KERNEL | __GFP_ZERO,
  1201. cpu_to_node(cpu));
  1202. if (!base)
  1203. return -ENOMEM;
  1204. /* Make sure that tvec_base is 2 byte aligned */
  1205. if (tbase_get_deferrable(base)) {
  1206. WARN_ON(1);
  1207. kfree(base);
  1208. return -ENOMEM;
  1209. }
  1210. per_cpu(tvec_bases, cpu) = base;
  1211. } else {
  1212. /*
  1213. * This is for the boot CPU - we use compile-time
  1214. * static initialisation because per-cpu memory isn't
  1215. * ready yet and because the memory allocators are not
  1216. * initialised either.
  1217. */
  1218. boot_done = 1;
  1219. base = &boot_tvec_bases;
  1220. }
  1221. tvec_base_done[cpu] = 1;
  1222. } else {
  1223. base = per_cpu(tvec_bases, cpu);
  1224. }
  1225. spin_lock_init(&base->lock);
  1226. for (j = 0; j < TVN_SIZE; j++) {
  1227. INIT_LIST_HEAD(base->tv5.vec + j);
  1228. INIT_LIST_HEAD(base->tv4.vec + j);
  1229. INIT_LIST_HEAD(base->tv3.vec + j);
  1230. INIT_LIST_HEAD(base->tv2.vec + j);
  1231. }
  1232. for (j = 0; j < TVR_SIZE; j++)
  1233. INIT_LIST_HEAD(base->tv1.vec + j);
  1234. base->timer_jiffies = jiffies;
  1235. return 0;
  1236. }
  1237. #ifdef CONFIG_HOTPLUG_CPU
  1238. static void migrate_timer_list(struct tvec_base *new_base, struct list_head *head)
  1239. {
  1240. struct timer_list *timer;
  1241. while (!list_empty(head)) {
  1242. timer = list_first_entry(head, struct timer_list, entry);
  1243. detach_timer(timer, 0);
  1244. timer_set_base(timer, new_base);
  1245. internal_add_timer(new_base, timer);
  1246. }
  1247. }
  1248. static void __cpuinit migrate_timers(int cpu)
  1249. {
  1250. struct tvec_base *old_base;
  1251. struct tvec_base *new_base;
  1252. int i;
  1253. BUG_ON(cpu_online(cpu));
  1254. old_base = per_cpu(tvec_bases, cpu);
  1255. new_base = get_cpu_var(tvec_bases);
  1256. local_irq_disable();
  1257. spin_lock(&new_base->lock);
  1258. spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
  1259. BUG_ON(old_base->running_timer);
  1260. for (i = 0; i < TVR_SIZE; i++)
  1261. migrate_timer_list(new_base, old_base->tv1.vec + i);
  1262. for (i = 0; i < TVN_SIZE; i++) {
  1263. migrate_timer_list(new_base, old_base->tv2.vec + i);
  1264. migrate_timer_list(new_base, old_base->tv3.vec + i);
  1265. migrate_timer_list(new_base, old_base->tv4.vec + i);
  1266. migrate_timer_list(new_base, old_base->tv5.vec + i);
  1267. }
  1268. spin_unlock(&old_base->lock);
  1269. spin_unlock(&new_base->lock);
  1270. local_irq_enable();
  1271. put_cpu_var(tvec_bases);
  1272. }
  1273. #endif /* CONFIG_HOTPLUG_CPU */
  1274. static int __cpuinit timer_cpu_notify(struct notifier_block *self,
  1275. unsigned long action, void *hcpu)
  1276. {
  1277. long cpu = (long)hcpu;
  1278. switch(action) {
  1279. case CPU_UP_PREPARE:
  1280. case CPU_UP_PREPARE_FROZEN:
  1281. if (init_timers_cpu(cpu) < 0)
  1282. return NOTIFY_BAD;
  1283. break;
  1284. #ifdef CONFIG_HOTPLUG_CPU
  1285. case CPU_DEAD:
  1286. case CPU_DEAD_FROZEN:
  1287. migrate_timers(cpu);
  1288. break;
  1289. #endif
  1290. default:
  1291. break;
  1292. }
  1293. return NOTIFY_OK;
  1294. }
  1295. static struct notifier_block __cpuinitdata timers_nb = {
  1296. .notifier_call = timer_cpu_notify,
  1297. };
  1298. void __init init_timers(void)
  1299. {
  1300. int err = timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
  1301. (void *)(long)smp_processor_id());
  1302. init_timer_stats();
  1303. BUG_ON(err == NOTIFY_BAD);
  1304. register_cpu_notifier(&timers_nb);
  1305. open_softirq(TIMER_SOFTIRQ, run_timer_softirq, NULL);
  1306. }
  1307. /**
  1308. * msleep - sleep safely even with waitqueue interruptions
  1309. * @msecs: Time in milliseconds to sleep for
  1310. */
  1311. void msleep(unsigned int msecs)
  1312. {
  1313. unsigned long timeout = msecs_to_jiffies(msecs) + 1;
  1314. while (timeout)
  1315. timeout = schedule_timeout_uninterruptible(timeout);
  1316. }
  1317. EXPORT_SYMBOL(msleep);
  1318. /**
  1319. * msleep_interruptible - sleep waiting for signals
  1320. * @msecs: Time in milliseconds to sleep for
  1321. */
  1322. unsigned long msleep_interruptible(unsigned int msecs)
  1323. {
  1324. unsigned long timeout = msecs_to_jiffies(msecs) + 1;
  1325. while (timeout && !signal_pending(current))
  1326. timeout = schedule_timeout_interruptible(timeout);
  1327. return jiffies_to_msecs(timeout);
  1328. }
  1329. EXPORT_SYMBOL(msleep_interruptible);