timer.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622
  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. static unsigned long round_jiffies_common(unsigned long j, int cpu,
  101. bool force_up)
  102. {
  103. int rem;
  104. unsigned long original = j;
  105. /*
  106. * We don't want all cpus firing their timers at once hitting the
  107. * same lock or cachelines, so we skew each extra cpu with an extra
  108. * 3 jiffies. This 3 jiffies came originally from the mm/ code which
  109. * already did this.
  110. * The skew is done by adding 3*cpunr, then round, then subtract this
  111. * extra offset again.
  112. */
  113. j += cpu * 3;
  114. rem = j % HZ;
  115. /*
  116. * If the target jiffie is just after a whole second (which can happen
  117. * due to delays of the timer irq, long irq off times etc etc) then
  118. * we should round down to the whole second, not up. Use 1/4th second
  119. * as cutoff for this rounding as an extreme upper bound for this.
  120. * But never round down if @force_up is set.
  121. */
  122. if (rem < HZ/4 && !force_up) /* round down */
  123. j = j - rem;
  124. else /* round up */
  125. j = j - rem + HZ;
  126. /* now that we have rounded, subtract the extra skew again */
  127. j -= cpu * 3;
  128. if (j <= jiffies) /* rounding ate our timeout entirely; */
  129. return original;
  130. return j;
  131. }
  132. /**
  133. * __round_jiffies - function to round jiffies to a full second
  134. * @j: the time in (absolute) jiffies that should be rounded
  135. * @cpu: the processor number on which the timeout will happen
  136. *
  137. * __round_jiffies() rounds an absolute time in the future (in jiffies)
  138. * up or down to (approximately) full seconds. This is useful for timers
  139. * for which the exact time they fire does not matter too much, as long as
  140. * they fire approximately every X seconds.
  141. *
  142. * By rounding these timers to whole seconds, all such timers will fire
  143. * at the same time, rather than at various times spread out. The goal
  144. * of this is to have the CPU wake up less, which saves power.
  145. *
  146. * The exact rounding is skewed for each processor to avoid all
  147. * processors firing at the exact same time, which could lead
  148. * to lock contention or spurious cache line bouncing.
  149. *
  150. * The return value is the rounded version of the @j parameter.
  151. */
  152. unsigned long __round_jiffies(unsigned long j, int cpu)
  153. {
  154. return round_jiffies_common(j, cpu, false);
  155. }
  156. EXPORT_SYMBOL_GPL(__round_jiffies);
  157. /**
  158. * __round_jiffies_relative - function to round jiffies to a full second
  159. * @j: the time in (relative) jiffies that should be rounded
  160. * @cpu: the processor number on which the timeout will happen
  161. *
  162. * __round_jiffies_relative() rounds a time delta in the future (in jiffies)
  163. * up or down to (approximately) full seconds. This is useful for timers
  164. * for which the exact time they fire does not matter too much, as long as
  165. * they fire approximately every X seconds.
  166. *
  167. * By rounding these timers to whole seconds, all such timers will fire
  168. * at the same time, rather than at various times spread out. The goal
  169. * of this is to have the CPU wake up less, which saves power.
  170. *
  171. * The exact rounding is skewed for each processor to avoid all
  172. * processors firing at the exact same time, which could lead
  173. * to lock contention or spurious cache line bouncing.
  174. *
  175. * The return value is the rounded version of the @j parameter.
  176. */
  177. unsigned long __round_jiffies_relative(unsigned long j, int cpu)
  178. {
  179. unsigned long j0 = jiffies;
  180. /* Use j0 because jiffies might change while we run */
  181. return round_jiffies_common(j + j0, cpu, false) - j0;
  182. }
  183. EXPORT_SYMBOL_GPL(__round_jiffies_relative);
  184. /**
  185. * round_jiffies - function to round jiffies to a full second
  186. * @j: the time in (absolute) jiffies that should be rounded
  187. *
  188. * round_jiffies() rounds an absolute time in the future (in jiffies)
  189. * up or down to (approximately) full seconds. This is useful for timers
  190. * for which the exact time they fire does not matter too much, as long as
  191. * they fire approximately every X seconds.
  192. *
  193. * By rounding these timers to whole seconds, all such timers will fire
  194. * at the same time, rather than at various times spread out. The goal
  195. * of this is to have the CPU wake up less, which saves power.
  196. *
  197. * The return value is the rounded version of the @j parameter.
  198. */
  199. unsigned long round_jiffies(unsigned long j)
  200. {
  201. return round_jiffies_common(j, raw_smp_processor_id(), false);
  202. }
  203. EXPORT_SYMBOL_GPL(round_jiffies);
  204. /**
  205. * round_jiffies_relative - function to round jiffies to a full second
  206. * @j: the time in (relative) jiffies that should be rounded
  207. *
  208. * round_jiffies_relative() rounds a time delta in the future (in jiffies)
  209. * up or down to (approximately) full seconds. This is useful for timers
  210. * for which the exact time they fire does not matter too much, as long as
  211. * they fire approximately every X seconds.
  212. *
  213. * By rounding these timers to whole seconds, all such timers will fire
  214. * at the same time, rather than at various times spread out. The goal
  215. * of this is to have the CPU wake up less, which saves power.
  216. *
  217. * The return value is the rounded version of the @j parameter.
  218. */
  219. unsigned long round_jiffies_relative(unsigned long j)
  220. {
  221. return __round_jiffies_relative(j, raw_smp_processor_id());
  222. }
  223. EXPORT_SYMBOL_GPL(round_jiffies_relative);
  224. /**
  225. * __round_jiffies_up - function to round jiffies up to a full second
  226. * @j: the time in (absolute) jiffies that should be rounded
  227. * @cpu: the processor number on which the timeout will happen
  228. *
  229. * This is the same as __round_jiffies() except that it will never
  230. * round down. This is useful for timeouts for which the exact time
  231. * of firing does not matter too much, as long as they don't fire too
  232. * early.
  233. */
  234. unsigned long __round_jiffies_up(unsigned long j, int cpu)
  235. {
  236. return round_jiffies_common(j, cpu, true);
  237. }
  238. EXPORT_SYMBOL_GPL(__round_jiffies_up);
  239. /**
  240. * __round_jiffies_up_relative - function to round jiffies up to a full second
  241. * @j: the time in (relative) jiffies that should be rounded
  242. * @cpu: the processor number on which the timeout will happen
  243. *
  244. * This is the same as __round_jiffies_relative() except that it will never
  245. * round down. This is useful for timeouts for which the exact time
  246. * of firing does not matter too much, as long as they don't fire too
  247. * early.
  248. */
  249. unsigned long __round_jiffies_up_relative(unsigned long j, int cpu)
  250. {
  251. unsigned long j0 = jiffies;
  252. /* Use j0 because jiffies might change while we run */
  253. return round_jiffies_common(j + j0, cpu, true) - j0;
  254. }
  255. EXPORT_SYMBOL_GPL(__round_jiffies_up_relative);
  256. /**
  257. * round_jiffies_up - function to round jiffies up to a full second
  258. * @j: the time in (absolute) jiffies that should be rounded
  259. *
  260. * This is the same as round_jiffies() except that it will never
  261. * round down. This is useful for timeouts for which the exact time
  262. * of firing does not matter too much, as long as they don't fire too
  263. * early.
  264. */
  265. unsigned long round_jiffies_up(unsigned long j)
  266. {
  267. return round_jiffies_common(j, raw_smp_processor_id(), true);
  268. }
  269. EXPORT_SYMBOL_GPL(round_jiffies_up);
  270. /**
  271. * round_jiffies_up_relative - function to round jiffies up to a full second
  272. * @j: the time in (relative) jiffies that should be rounded
  273. *
  274. * This is the same as round_jiffies_relative() except that it will never
  275. * round down. This is useful for timeouts for which the exact time
  276. * of firing does not matter too much, as long as they don't fire too
  277. * early.
  278. */
  279. unsigned long round_jiffies_up_relative(unsigned long j)
  280. {
  281. return __round_jiffies_up_relative(j, raw_smp_processor_id());
  282. }
  283. EXPORT_SYMBOL_GPL(round_jiffies_up_relative);
  284. static inline void set_running_timer(struct tvec_base *base,
  285. struct timer_list *timer)
  286. {
  287. #ifdef CONFIG_SMP
  288. base->running_timer = timer;
  289. #endif
  290. }
  291. static void internal_add_timer(struct tvec_base *base, struct timer_list *timer)
  292. {
  293. unsigned long expires = timer->expires;
  294. unsigned long idx = expires - base->timer_jiffies;
  295. struct list_head *vec;
  296. if (idx < TVR_SIZE) {
  297. int i = expires & TVR_MASK;
  298. vec = base->tv1.vec + i;
  299. } else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
  300. int i = (expires >> TVR_BITS) & TVN_MASK;
  301. vec = base->tv2.vec + i;
  302. } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {
  303. int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
  304. vec = base->tv3.vec + i;
  305. } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {
  306. int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
  307. vec = base->tv4.vec + i;
  308. } else if ((signed long) idx < 0) {
  309. /*
  310. * Can happen if you add a timer with expires == jiffies,
  311. * or you set a timer to go off in the past
  312. */
  313. vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK);
  314. } else {
  315. int i;
  316. /* If the timeout is larger than 0xffffffff on 64-bit
  317. * architectures then we use the maximum timeout:
  318. */
  319. if (idx > 0xffffffffUL) {
  320. idx = 0xffffffffUL;
  321. expires = idx + base->timer_jiffies;
  322. }
  323. i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
  324. vec = base->tv5.vec + i;
  325. }
  326. /*
  327. * Timers are FIFO:
  328. */
  329. list_add_tail(&timer->entry, vec);
  330. }
  331. #ifdef CONFIG_TIMER_STATS
  332. void __timer_stats_timer_set_start_info(struct timer_list *timer, void *addr)
  333. {
  334. if (timer->start_site)
  335. return;
  336. timer->start_site = addr;
  337. memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
  338. timer->start_pid = current->pid;
  339. }
  340. static void timer_stats_account_timer(struct timer_list *timer)
  341. {
  342. unsigned int flag = 0;
  343. if (unlikely(tbase_get_deferrable(timer->base)))
  344. flag |= TIMER_STATS_FLAG_DEFERRABLE;
  345. timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
  346. timer->function, timer->start_comm, flag);
  347. }
  348. #else
  349. static void timer_stats_account_timer(struct timer_list *timer) {}
  350. #endif
  351. #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
  352. static struct debug_obj_descr timer_debug_descr;
  353. /*
  354. * fixup_init is called when:
  355. * - an active object is initialized
  356. */
  357. static int timer_fixup_init(void *addr, enum debug_obj_state state)
  358. {
  359. struct timer_list *timer = addr;
  360. switch (state) {
  361. case ODEBUG_STATE_ACTIVE:
  362. del_timer_sync(timer);
  363. debug_object_init(timer, &timer_debug_descr);
  364. return 1;
  365. default:
  366. return 0;
  367. }
  368. }
  369. /*
  370. * fixup_activate is called when:
  371. * - an active object is activated
  372. * - an unknown object is activated (might be a statically initialized object)
  373. */
  374. static int timer_fixup_activate(void *addr, enum debug_obj_state state)
  375. {
  376. struct timer_list *timer = addr;
  377. switch (state) {
  378. case ODEBUG_STATE_NOTAVAILABLE:
  379. /*
  380. * This is not really a fixup. The timer was
  381. * statically initialized. We just make sure that it
  382. * is tracked in the object tracker.
  383. */
  384. if (timer->entry.next == NULL &&
  385. timer->entry.prev == TIMER_ENTRY_STATIC) {
  386. debug_object_init(timer, &timer_debug_descr);
  387. debug_object_activate(timer, &timer_debug_descr);
  388. return 0;
  389. } else {
  390. WARN_ON_ONCE(1);
  391. }
  392. return 0;
  393. case ODEBUG_STATE_ACTIVE:
  394. WARN_ON(1);
  395. default:
  396. return 0;
  397. }
  398. }
  399. /*
  400. * fixup_free is called when:
  401. * - an active object is freed
  402. */
  403. static int timer_fixup_free(void *addr, enum debug_obj_state state)
  404. {
  405. struct timer_list *timer = addr;
  406. switch (state) {
  407. case ODEBUG_STATE_ACTIVE:
  408. del_timer_sync(timer);
  409. debug_object_free(timer, &timer_debug_descr);
  410. return 1;
  411. default:
  412. return 0;
  413. }
  414. }
  415. static struct debug_obj_descr timer_debug_descr = {
  416. .name = "timer_list",
  417. .fixup_init = timer_fixup_init,
  418. .fixup_activate = timer_fixup_activate,
  419. .fixup_free = timer_fixup_free,
  420. };
  421. static inline void debug_timer_init(struct timer_list *timer)
  422. {
  423. debug_object_init(timer, &timer_debug_descr);
  424. }
  425. static inline void debug_timer_activate(struct timer_list *timer)
  426. {
  427. debug_object_activate(timer, &timer_debug_descr);
  428. }
  429. static inline void debug_timer_deactivate(struct timer_list *timer)
  430. {
  431. debug_object_deactivate(timer, &timer_debug_descr);
  432. }
  433. static inline void debug_timer_free(struct timer_list *timer)
  434. {
  435. debug_object_free(timer, &timer_debug_descr);
  436. }
  437. static void __init_timer(struct timer_list *timer);
  438. void init_timer_on_stack(struct timer_list *timer)
  439. {
  440. debug_object_init_on_stack(timer, &timer_debug_descr);
  441. __init_timer(timer);
  442. }
  443. EXPORT_SYMBOL_GPL(init_timer_on_stack);
  444. void destroy_timer_on_stack(struct timer_list *timer)
  445. {
  446. debug_object_free(timer, &timer_debug_descr);
  447. }
  448. EXPORT_SYMBOL_GPL(destroy_timer_on_stack);
  449. #else
  450. static inline void debug_timer_init(struct timer_list *timer) { }
  451. static inline void debug_timer_activate(struct timer_list *timer) { }
  452. static inline void debug_timer_deactivate(struct timer_list *timer) { }
  453. #endif
  454. static void __init_timer(struct timer_list *timer)
  455. {
  456. timer->entry.next = NULL;
  457. timer->base = __raw_get_cpu_var(tvec_bases);
  458. #ifdef CONFIG_TIMER_STATS
  459. timer->start_site = NULL;
  460. timer->start_pid = -1;
  461. memset(timer->start_comm, 0, TASK_COMM_LEN);
  462. #endif
  463. }
  464. /**
  465. * init_timer_key - initialize a timer
  466. * @timer: the timer to be initialized
  467. * @name: name of the timer
  468. * @key: lockdep class key of the fake lock used for tracking timer
  469. * sync lock dependencies
  470. *
  471. * init_timer_key() must be done to a timer prior calling *any* of the
  472. * other timer functions.
  473. */
  474. void init_timer(struct timer_list *timer)
  475. {
  476. debug_timer_init(timer);
  477. __init_timer(timer);
  478. }
  479. EXPORT_SYMBOL(init_timer);
  480. void init_timer_deferrable(struct timer_list *timer)
  481. {
  482. init_timer(timer);
  483. timer_set_deferrable(timer);
  484. }
  485. EXPORT_SYMBOL(init_timer_deferrable);
  486. static inline void detach_timer(struct timer_list *timer,
  487. int clear_pending)
  488. {
  489. struct list_head *entry = &timer->entry;
  490. debug_timer_deactivate(timer);
  491. __list_del(entry->prev, entry->next);
  492. if (clear_pending)
  493. entry->next = NULL;
  494. entry->prev = LIST_POISON2;
  495. }
  496. /*
  497. * We are using hashed locking: holding per_cpu(tvec_bases).lock
  498. * means that all timers which are tied to this base via timer->base are
  499. * locked, and the base itself is locked too.
  500. *
  501. * So __run_timers/migrate_timers can safely modify all timers which could
  502. * be found on ->tvX lists.
  503. *
  504. * When the timer's base is locked, and the timer removed from list, it is
  505. * possible to set timer->base = NULL and drop the lock: the timer remains
  506. * locked.
  507. */
  508. static struct tvec_base *lock_timer_base(struct timer_list *timer,
  509. unsigned long *flags)
  510. __acquires(timer->base->lock)
  511. {
  512. struct tvec_base *base;
  513. for (;;) {
  514. struct tvec_base *prelock_base = timer->base;
  515. base = tbase_get_base(prelock_base);
  516. if (likely(base != NULL)) {
  517. spin_lock_irqsave(&base->lock, *flags);
  518. if (likely(prelock_base == timer->base))
  519. return base;
  520. /* The timer has migrated to another CPU */
  521. spin_unlock_irqrestore(&base->lock, *flags);
  522. }
  523. cpu_relax();
  524. }
  525. }
  526. static inline int
  527. __mod_timer(struct timer_list *timer, unsigned long expires, bool pending_only)
  528. {
  529. struct tvec_base *base, *new_base;
  530. unsigned long flags;
  531. int ret;
  532. ret = 0;
  533. timer_stats_timer_set_start_info(timer);
  534. BUG_ON(!timer->function);
  535. base = lock_timer_base(timer, &flags);
  536. if (timer_pending(timer)) {
  537. detach_timer(timer, 0);
  538. ret = 1;
  539. } else {
  540. if (pending_only)
  541. goto out_unlock;
  542. }
  543. debug_timer_activate(timer);
  544. new_base = __get_cpu_var(tvec_bases);
  545. if (base != new_base) {
  546. /*
  547. * We are trying to schedule the timer on the local CPU.
  548. * However we can't change timer's base while it is running,
  549. * otherwise del_timer_sync() can't detect that the timer's
  550. * handler yet has not finished. This also guarantees that
  551. * the timer is serialized wrt itself.
  552. */
  553. if (likely(base->running_timer != timer)) {
  554. /* See the comment in lock_timer_base() */
  555. timer_set_base(timer, NULL);
  556. spin_unlock(&base->lock);
  557. base = new_base;
  558. spin_lock(&base->lock);
  559. timer_set_base(timer, base);
  560. }
  561. }
  562. timer->expires = expires;
  563. internal_add_timer(base, timer);
  564. out_unlock:
  565. spin_unlock_irqrestore(&base->lock, flags);
  566. return ret;
  567. }
  568. /**
  569. * mod_timer_pending - modify a pending timer's timeout
  570. * @timer: the pending timer to be modified
  571. * @expires: new timeout in jiffies
  572. *
  573. * mod_timer_pending() is the same for pending timers as mod_timer(),
  574. * but will not re-activate and modify already deleted timers.
  575. *
  576. * It is useful for unserialized use of timers.
  577. */
  578. int mod_timer_pending(struct timer_list *timer, unsigned long expires)
  579. {
  580. return __mod_timer(timer, expires, true);
  581. }
  582. EXPORT_SYMBOL(mod_timer_pending);
  583. /**
  584. * mod_timer - modify a timer's timeout
  585. * @timer: the timer to be modified
  586. * @expires: new timeout in jiffies
  587. *
  588. * mod_timer() is a more efficient way to update the expire field of an
  589. * active timer (if the timer is inactive it will be activated)
  590. *
  591. * mod_timer(timer, expires) is equivalent to:
  592. *
  593. * del_timer(timer); timer->expires = expires; add_timer(timer);
  594. *
  595. * Note that if there are multiple unserialized concurrent users of the
  596. * same timer, then mod_timer() is the only safe way to modify the timeout,
  597. * since add_timer() cannot modify an already running timer.
  598. *
  599. * The function returns whether it has modified a pending timer or not.
  600. * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
  601. * active timer returns 1.)
  602. */
  603. int mod_timer(struct timer_list *timer, unsigned long expires)
  604. {
  605. /*
  606. * This is a common optimization triggered by the
  607. * networking code - if the timer is re-modified
  608. * to be the same thing then just return:
  609. */
  610. if (timer->expires == expires && timer_pending(timer))
  611. return 1;
  612. return __mod_timer(timer, expires, false);
  613. }
  614. EXPORT_SYMBOL(mod_timer);
  615. /**
  616. * add_timer - start a timer
  617. * @timer: the timer to be added
  618. *
  619. * The kernel will do a ->function(->data) callback from the
  620. * timer interrupt at the ->expires point in the future. The
  621. * current time is 'jiffies'.
  622. *
  623. * The timer's ->expires, ->function (and if the handler uses it, ->data)
  624. * fields must be set prior calling this function.
  625. *
  626. * Timers with an ->expires field in the past will be executed in the next
  627. * timer tick.
  628. */
  629. void add_timer(struct timer_list *timer)
  630. {
  631. BUG_ON(timer_pending(timer));
  632. mod_timer(timer, timer->expires);
  633. }
  634. EXPORT_SYMBOL(add_timer);
  635. /**
  636. * add_timer_on - start a timer on a particular CPU
  637. * @timer: the timer to be added
  638. * @cpu: the CPU to start it on
  639. *
  640. * This is not very scalable on SMP. Double adds are not possible.
  641. */
  642. void add_timer_on(struct timer_list *timer, int cpu)
  643. {
  644. struct tvec_base *base = per_cpu(tvec_bases, cpu);
  645. unsigned long flags;
  646. timer_stats_timer_set_start_info(timer);
  647. BUG_ON(timer_pending(timer) || !timer->function);
  648. spin_lock_irqsave(&base->lock, flags);
  649. timer_set_base(timer, base);
  650. debug_timer_activate(timer);
  651. internal_add_timer(base, timer);
  652. /*
  653. * Check whether the other CPU is idle and needs to be
  654. * triggered to reevaluate the timer wheel when nohz is
  655. * active. We are protected against the other CPU fiddling
  656. * with the timer by holding the timer base lock. This also
  657. * makes sure that a CPU on the way to idle can not evaluate
  658. * the timer wheel.
  659. */
  660. wake_up_idle_cpu(cpu);
  661. spin_unlock_irqrestore(&base->lock, flags);
  662. }
  663. /**
  664. * del_timer - deactive a timer.
  665. * @timer: the timer to be deactivated
  666. *
  667. * del_timer() deactivates a timer - this works on both active and inactive
  668. * timers.
  669. *
  670. * The function returns whether it has deactivated a pending timer or not.
  671. * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
  672. * active timer returns 1.)
  673. */
  674. int del_timer(struct timer_list *timer)
  675. {
  676. struct tvec_base *base;
  677. unsigned long flags;
  678. int ret = 0;
  679. timer_stats_timer_clear_start_info(timer);
  680. if (timer_pending(timer)) {
  681. base = lock_timer_base(timer, &flags);
  682. if (timer_pending(timer)) {
  683. detach_timer(timer, 1);
  684. ret = 1;
  685. }
  686. spin_unlock_irqrestore(&base->lock, flags);
  687. }
  688. return ret;
  689. }
  690. EXPORT_SYMBOL(del_timer);
  691. #ifdef CONFIG_SMP
  692. /**
  693. * try_to_del_timer_sync - Try to deactivate a timer
  694. * @timer: timer do del
  695. *
  696. * This function tries to deactivate a timer. Upon successful (ret >= 0)
  697. * exit the timer is not queued and the handler is not running on any CPU.
  698. *
  699. * It must not be called from interrupt contexts.
  700. */
  701. int try_to_del_timer_sync(struct timer_list *timer)
  702. {
  703. struct tvec_base *base;
  704. unsigned long flags;
  705. int ret = -1;
  706. base = lock_timer_base(timer, &flags);
  707. if (base->running_timer == timer)
  708. goto out;
  709. ret = 0;
  710. if (timer_pending(timer)) {
  711. detach_timer(timer, 1);
  712. ret = 1;
  713. }
  714. out:
  715. spin_unlock_irqrestore(&base->lock, flags);
  716. return ret;
  717. }
  718. EXPORT_SYMBOL(try_to_del_timer_sync);
  719. /**
  720. * del_timer_sync - deactivate a timer and wait for the handler to finish.
  721. * @timer: the timer to be deactivated
  722. *
  723. * This function only differs from del_timer() on SMP: besides deactivating
  724. * the timer it also makes sure the handler has finished executing on other
  725. * CPUs.
  726. *
  727. * Synchronization rules: Callers must prevent restarting of the timer,
  728. * otherwise this function is meaningless. It must not be called from
  729. * interrupt contexts. The caller must not hold locks which would prevent
  730. * completion of the timer's handler. The timer's handler must not call
  731. * add_timer_on(). Upon exit the timer is not queued and the handler is
  732. * not running on any CPU.
  733. *
  734. * The function returns whether it has deactivated a pending timer or not.
  735. */
  736. int del_timer_sync(struct timer_list *timer)
  737. {
  738. for (;;) {
  739. int ret = try_to_del_timer_sync(timer);
  740. if (ret >= 0)
  741. return ret;
  742. cpu_relax();
  743. }
  744. }
  745. EXPORT_SYMBOL(del_timer_sync);
  746. #endif
  747. static int cascade(struct tvec_base *base, struct tvec *tv, int index)
  748. {
  749. /* cascade all the timers from tv up one level */
  750. struct timer_list *timer, *tmp;
  751. struct list_head tv_list;
  752. list_replace_init(tv->vec + index, &tv_list);
  753. /*
  754. * We are removing _all_ timers from the list, so we
  755. * don't have to detach them individually.
  756. */
  757. list_for_each_entry_safe(timer, tmp, &tv_list, entry) {
  758. BUG_ON(tbase_get_base(timer->base) != base);
  759. internal_add_timer(base, timer);
  760. }
  761. return index;
  762. }
  763. #define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK)
  764. /**
  765. * __run_timers - run all expired timers (if any) on this CPU.
  766. * @base: the timer vector to be processed.
  767. *
  768. * This function cascades all vectors and executes all expired timer
  769. * vectors.
  770. */
  771. static inline void __run_timers(struct tvec_base *base)
  772. {
  773. struct timer_list *timer;
  774. spin_lock_irq(&base->lock);
  775. while (time_after_eq(jiffies, base->timer_jiffies)) {
  776. struct list_head work_list;
  777. struct list_head *head = &work_list;
  778. int index = base->timer_jiffies & TVR_MASK;
  779. /*
  780. * Cascade timers:
  781. */
  782. if (!index &&
  783. (!cascade(base, &base->tv2, INDEX(0))) &&
  784. (!cascade(base, &base->tv3, INDEX(1))) &&
  785. !cascade(base, &base->tv4, INDEX(2)))
  786. cascade(base, &base->tv5, INDEX(3));
  787. ++base->timer_jiffies;
  788. list_replace_init(base->tv1.vec + index, &work_list);
  789. while (!list_empty(head)) {
  790. void (*fn)(unsigned long);
  791. unsigned long data;
  792. timer = list_first_entry(head, struct timer_list,entry);
  793. fn = timer->function;
  794. data = timer->data;
  795. timer_stats_account_timer(timer);
  796. set_running_timer(base, timer);
  797. detach_timer(timer, 1);
  798. spin_unlock_irq(&base->lock);
  799. {
  800. int preempt_count = preempt_count();
  801. fn(data);
  802. if (preempt_count != preempt_count()) {
  803. printk(KERN_ERR "huh, entered %p "
  804. "with preempt_count %08x, exited"
  805. " with %08x?\n",
  806. fn, preempt_count,
  807. preempt_count());
  808. BUG();
  809. }
  810. }
  811. spin_lock_irq(&base->lock);
  812. }
  813. }
  814. set_running_timer(base, NULL);
  815. spin_unlock_irq(&base->lock);
  816. }
  817. #ifdef CONFIG_NO_HZ
  818. /*
  819. * Find out when the next timer event is due to happen. This
  820. * is used on S/390 to stop all activity when a cpus is idle.
  821. * This functions needs to be called disabled.
  822. */
  823. static unsigned long __next_timer_interrupt(struct tvec_base *base)
  824. {
  825. unsigned long timer_jiffies = base->timer_jiffies;
  826. unsigned long expires = timer_jiffies + NEXT_TIMER_MAX_DELTA;
  827. int index, slot, array, found = 0;
  828. struct timer_list *nte;
  829. struct tvec *varray[4];
  830. /* Look for timer events in tv1. */
  831. index = slot = timer_jiffies & TVR_MASK;
  832. do {
  833. list_for_each_entry(nte, base->tv1.vec + slot, entry) {
  834. if (tbase_get_deferrable(nte->base))
  835. continue;
  836. found = 1;
  837. expires = nte->expires;
  838. /* Look at the cascade bucket(s)? */
  839. if (!index || slot < index)
  840. goto cascade;
  841. return expires;
  842. }
  843. slot = (slot + 1) & TVR_MASK;
  844. } while (slot != index);
  845. cascade:
  846. /* Calculate the next cascade event */
  847. if (index)
  848. timer_jiffies += TVR_SIZE - index;
  849. timer_jiffies >>= TVR_BITS;
  850. /* Check tv2-tv5. */
  851. varray[0] = &base->tv2;
  852. varray[1] = &base->tv3;
  853. varray[2] = &base->tv4;
  854. varray[3] = &base->tv5;
  855. for (array = 0; array < 4; array++) {
  856. struct tvec *varp = varray[array];
  857. index = slot = timer_jiffies & TVN_MASK;
  858. do {
  859. list_for_each_entry(nte, varp->vec + slot, entry) {
  860. found = 1;
  861. if (time_before(nte->expires, expires))
  862. expires = nte->expires;
  863. }
  864. /*
  865. * Do we still search for the first timer or are
  866. * we looking up the cascade buckets ?
  867. */
  868. if (found) {
  869. /* Look at the cascade bucket(s)? */
  870. if (!index || slot < index)
  871. break;
  872. return expires;
  873. }
  874. slot = (slot + 1) & TVN_MASK;
  875. } while (slot != index);
  876. if (index)
  877. timer_jiffies += TVN_SIZE - index;
  878. timer_jiffies >>= TVN_BITS;
  879. }
  880. return expires;
  881. }
  882. /*
  883. * Check, if the next hrtimer event is before the next timer wheel
  884. * event:
  885. */
  886. static unsigned long cmp_next_hrtimer_event(unsigned long now,
  887. unsigned long expires)
  888. {
  889. ktime_t hr_delta = hrtimer_get_next_event();
  890. struct timespec tsdelta;
  891. unsigned long delta;
  892. if (hr_delta.tv64 == KTIME_MAX)
  893. return expires;
  894. /*
  895. * Expired timer available, let it expire in the next tick
  896. */
  897. if (hr_delta.tv64 <= 0)
  898. return now + 1;
  899. tsdelta = ktime_to_timespec(hr_delta);
  900. delta = timespec_to_jiffies(&tsdelta);
  901. /*
  902. * Limit the delta to the max value, which is checked in
  903. * tick_nohz_stop_sched_tick():
  904. */
  905. if (delta > NEXT_TIMER_MAX_DELTA)
  906. delta = NEXT_TIMER_MAX_DELTA;
  907. /*
  908. * Take rounding errors in to account and make sure, that it
  909. * expires in the next tick. Otherwise we go into an endless
  910. * ping pong due to tick_nohz_stop_sched_tick() retriggering
  911. * the timer softirq
  912. */
  913. if (delta < 1)
  914. delta = 1;
  915. now += delta;
  916. if (time_before(now, expires))
  917. return now;
  918. return expires;
  919. }
  920. /**
  921. * get_next_timer_interrupt - return the jiffy of the next pending timer
  922. * @now: current time (in jiffies)
  923. */
  924. unsigned long get_next_timer_interrupt(unsigned long now)
  925. {
  926. struct tvec_base *base = __get_cpu_var(tvec_bases);
  927. unsigned long expires;
  928. spin_lock(&base->lock);
  929. expires = __next_timer_interrupt(base);
  930. spin_unlock(&base->lock);
  931. if (time_before_eq(expires, now))
  932. return now;
  933. return cmp_next_hrtimer_event(now, expires);
  934. }
  935. #endif
  936. /*
  937. * Called from the timer interrupt handler to charge one tick to the current
  938. * process. user_tick is 1 if the tick is user time, 0 for system.
  939. */
  940. void update_process_times(int user_tick)
  941. {
  942. struct task_struct *p = current;
  943. int cpu = smp_processor_id();
  944. /* Note: this timer irq context must be accounted for as well. */
  945. account_process_tick(p, user_tick);
  946. run_local_timers();
  947. if (rcu_pending(cpu))
  948. rcu_check_callbacks(cpu, user_tick);
  949. printk_tick();
  950. scheduler_tick();
  951. run_posix_cpu_timers(p);
  952. }
  953. /*
  954. * Nr of active tasks - counted in fixed-point numbers
  955. */
  956. static unsigned long count_active_tasks(void)
  957. {
  958. return nr_active() * FIXED_1;
  959. }
  960. /*
  961. * Hmm.. Changed this, as the GNU make sources (load.c) seems to
  962. * imply that avenrun[] is the standard name for this kind of thing.
  963. * Nothing else seems to be standardized: the fractional size etc
  964. * all seem to differ on different machines.
  965. *
  966. * Requires xtime_lock to access.
  967. */
  968. unsigned long avenrun[3];
  969. EXPORT_SYMBOL(avenrun);
  970. /*
  971. * calc_load - given tick count, update the avenrun load estimates.
  972. * This is called while holding a write_lock on xtime_lock.
  973. */
  974. static inline void calc_load(unsigned long ticks)
  975. {
  976. unsigned long active_tasks; /* fixed-point */
  977. static int count = LOAD_FREQ;
  978. count -= ticks;
  979. if (unlikely(count < 0)) {
  980. active_tasks = count_active_tasks();
  981. do {
  982. CALC_LOAD(avenrun[0], EXP_1, active_tasks);
  983. CALC_LOAD(avenrun[1], EXP_5, active_tasks);
  984. CALC_LOAD(avenrun[2], EXP_15, active_tasks);
  985. count += LOAD_FREQ;
  986. } while (count < 0);
  987. }
  988. }
  989. /*
  990. * This function runs timers and the timer-tq in bottom half context.
  991. */
  992. static void run_timer_softirq(struct softirq_action *h)
  993. {
  994. struct tvec_base *base = __get_cpu_var(tvec_bases);
  995. hrtimer_run_pending();
  996. if (time_after_eq(jiffies, base->timer_jiffies))
  997. __run_timers(base);
  998. }
  999. /*
  1000. * Called by the local, per-CPU timer interrupt on SMP.
  1001. */
  1002. void run_local_timers(void)
  1003. {
  1004. hrtimer_run_queues();
  1005. raise_softirq(TIMER_SOFTIRQ);
  1006. softlockup_tick();
  1007. }
  1008. /*
  1009. * Called by the timer interrupt. xtime_lock must already be taken
  1010. * by the timer IRQ!
  1011. */
  1012. static inline void update_times(unsigned long ticks)
  1013. {
  1014. update_wall_time();
  1015. calc_load(ticks);
  1016. }
  1017. /*
  1018. * The 64-bit jiffies value is not atomic - you MUST NOT read it
  1019. * without sampling the sequence number in xtime_lock.
  1020. * jiffies is defined in the linker script...
  1021. */
  1022. void do_timer(unsigned long ticks)
  1023. {
  1024. jiffies_64 += ticks;
  1025. update_times(ticks);
  1026. }
  1027. #ifdef __ARCH_WANT_SYS_ALARM
  1028. /*
  1029. * For backwards compatibility? This can be done in libc so Alpha
  1030. * and all newer ports shouldn't need it.
  1031. */
  1032. SYSCALL_DEFINE1(alarm, unsigned int, seconds)
  1033. {
  1034. return alarm_setitimer(seconds);
  1035. }
  1036. #endif
  1037. #ifndef __alpha__
  1038. /*
  1039. * The Alpha uses getxpid, getxuid, and getxgid instead. Maybe this
  1040. * should be moved into arch/i386 instead?
  1041. */
  1042. /**
  1043. * sys_getpid - return the thread group id of the current process
  1044. *
  1045. * Note, despite the name, this returns the tgid not the pid. The tgid and
  1046. * the pid are identical unless CLONE_THREAD was specified on clone() in
  1047. * which case the tgid is the same in all threads of the same group.
  1048. *
  1049. * This is SMP safe as current->tgid does not change.
  1050. */
  1051. SYSCALL_DEFINE0(getpid)
  1052. {
  1053. return task_tgid_vnr(current);
  1054. }
  1055. /*
  1056. * Accessing ->real_parent is not SMP-safe, it could
  1057. * change from under us. However, we can use a stale
  1058. * value of ->real_parent under rcu_read_lock(), see
  1059. * release_task()->call_rcu(delayed_put_task_struct).
  1060. */
  1061. SYSCALL_DEFINE0(getppid)
  1062. {
  1063. int pid;
  1064. rcu_read_lock();
  1065. pid = task_tgid_vnr(current->real_parent);
  1066. rcu_read_unlock();
  1067. return pid;
  1068. }
  1069. SYSCALL_DEFINE0(getuid)
  1070. {
  1071. /* Only we change this so SMP safe */
  1072. return current_uid();
  1073. }
  1074. SYSCALL_DEFINE0(geteuid)
  1075. {
  1076. /* Only we change this so SMP safe */
  1077. return current_euid();
  1078. }
  1079. SYSCALL_DEFINE0(getgid)
  1080. {
  1081. /* Only we change this so SMP safe */
  1082. return current_gid();
  1083. }
  1084. SYSCALL_DEFINE0(getegid)
  1085. {
  1086. /* Only we change this so SMP safe */
  1087. return current_egid();
  1088. }
  1089. #endif
  1090. static void process_timeout(unsigned long __data)
  1091. {
  1092. wake_up_process((struct task_struct *)__data);
  1093. }
  1094. /**
  1095. * schedule_timeout - sleep until timeout
  1096. * @timeout: timeout value in jiffies
  1097. *
  1098. * Make the current task sleep until @timeout jiffies have
  1099. * elapsed. The routine will return immediately unless
  1100. * the current task state has been set (see set_current_state()).
  1101. *
  1102. * You can set the task state as follows -
  1103. *
  1104. * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
  1105. * pass before the routine returns. The routine will return 0
  1106. *
  1107. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  1108. * delivered to the current task. In this case the remaining time
  1109. * in jiffies will be returned, or 0 if the timer expired in time
  1110. *
  1111. * The current task state is guaranteed to be TASK_RUNNING when this
  1112. * routine returns.
  1113. *
  1114. * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
  1115. * the CPU away without a bound on the timeout. In this case the return
  1116. * value will be %MAX_SCHEDULE_TIMEOUT.
  1117. *
  1118. * In all cases the return value is guaranteed to be non-negative.
  1119. */
  1120. signed long __sched schedule_timeout(signed long timeout)
  1121. {
  1122. struct timer_list timer;
  1123. unsigned long expire;
  1124. switch (timeout)
  1125. {
  1126. case MAX_SCHEDULE_TIMEOUT:
  1127. /*
  1128. * These two special cases are useful to be comfortable
  1129. * in the caller. Nothing more. We could take
  1130. * MAX_SCHEDULE_TIMEOUT from one of the negative value
  1131. * but I' d like to return a valid offset (>=0) to allow
  1132. * the caller to do everything it want with the retval.
  1133. */
  1134. schedule();
  1135. goto out;
  1136. default:
  1137. /*
  1138. * Another bit of PARANOID. Note that the retval will be
  1139. * 0 since no piece of kernel is supposed to do a check
  1140. * for a negative retval of schedule_timeout() (since it
  1141. * should never happens anyway). You just have the printk()
  1142. * that will tell you if something is gone wrong and where.
  1143. */
  1144. if (timeout < 0) {
  1145. printk(KERN_ERR "schedule_timeout: wrong timeout "
  1146. "value %lx\n", timeout);
  1147. dump_stack();
  1148. current->state = TASK_RUNNING;
  1149. goto out;
  1150. }
  1151. }
  1152. expire = timeout + jiffies;
  1153. setup_timer_on_stack(&timer, process_timeout, (unsigned long)current);
  1154. __mod_timer(&timer, expire, false);
  1155. schedule();
  1156. del_singleshot_timer_sync(&timer);
  1157. /* Remove the timer from the object tracker */
  1158. destroy_timer_on_stack(&timer);
  1159. timeout = expire - jiffies;
  1160. out:
  1161. return timeout < 0 ? 0 : timeout;
  1162. }
  1163. EXPORT_SYMBOL(schedule_timeout);
  1164. /*
  1165. * We can use __set_current_state() here because schedule_timeout() calls
  1166. * schedule() unconditionally.
  1167. */
  1168. signed long __sched schedule_timeout_interruptible(signed long timeout)
  1169. {
  1170. __set_current_state(TASK_INTERRUPTIBLE);
  1171. return schedule_timeout(timeout);
  1172. }
  1173. EXPORT_SYMBOL(schedule_timeout_interruptible);
  1174. signed long __sched schedule_timeout_killable(signed long timeout)
  1175. {
  1176. __set_current_state(TASK_KILLABLE);
  1177. return schedule_timeout(timeout);
  1178. }
  1179. EXPORT_SYMBOL(schedule_timeout_killable);
  1180. signed long __sched schedule_timeout_uninterruptible(signed long timeout)
  1181. {
  1182. __set_current_state(TASK_UNINTERRUPTIBLE);
  1183. return schedule_timeout(timeout);
  1184. }
  1185. EXPORT_SYMBOL(schedule_timeout_uninterruptible);
  1186. /* Thread ID - the internal kernel "pid" */
  1187. SYSCALL_DEFINE0(gettid)
  1188. {
  1189. return task_pid_vnr(current);
  1190. }
  1191. /**
  1192. * do_sysinfo - fill in sysinfo struct
  1193. * @info: pointer to buffer to fill
  1194. */
  1195. int do_sysinfo(struct sysinfo *info)
  1196. {
  1197. unsigned long mem_total, sav_total;
  1198. unsigned int mem_unit, bitcount;
  1199. unsigned long seq;
  1200. memset(info, 0, sizeof(struct sysinfo));
  1201. do {
  1202. struct timespec tp;
  1203. seq = read_seqbegin(&xtime_lock);
  1204. /*
  1205. * This is annoying. The below is the same thing
  1206. * posix_get_clock_monotonic() does, but it wants to
  1207. * take the lock which we want to cover the loads stuff
  1208. * too.
  1209. */
  1210. getnstimeofday(&tp);
  1211. tp.tv_sec += wall_to_monotonic.tv_sec;
  1212. tp.tv_nsec += wall_to_monotonic.tv_nsec;
  1213. monotonic_to_bootbased(&tp);
  1214. if (tp.tv_nsec - NSEC_PER_SEC >= 0) {
  1215. tp.tv_nsec = tp.tv_nsec - NSEC_PER_SEC;
  1216. tp.tv_sec++;
  1217. }
  1218. info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
  1219. info->loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT);
  1220. info->loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT);
  1221. info->loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT);
  1222. info->procs = nr_threads;
  1223. } while (read_seqretry(&xtime_lock, seq));
  1224. si_meminfo(info);
  1225. si_swapinfo(info);
  1226. /*
  1227. * If the sum of all the available memory (i.e. ram + swap)
  1228. * is less than can be stored in a 32 bit unsigned long then
  1229. * we can be binary compatible with 2.2.x kernels. If not,
  1230. * well, in that case 2.2.x was broken anyways...
  1231. *
  1232. * -Erik Andersen <andersee@debian.org>
  1233. */
  1234. mem_total = info->totalram + info->totalswap;
  1235. if (mem_total < info->totalram || mem_total < info->totalswap)
  1236. goto out;
  1237. bitcount = 0;
  1238. mem_unit = info->mem_unit;
  1239. while (mem_unit > 1) {
  1240. bitcount++;
  1241. mem_unit >>= 1;
  1242. sav_total = mem_total;
  1243. mem_total <<= 1;
  1244. if (mem_total < sav_total)
  1245. goto out;
  1246. }
  1247. /*
  1248. * If mem_total did not overflow, multiply all memory values by
  1249. * info->mem_unit and set it to 1. This leaves things compatible
  1250. * with 2.2.x, and also retains compatibility with earlier 2.4.x
  1251. * kernels...
  1252. */
  1253. info->mem_unit = 1;
  1254. info->totalram <<= bitcount;
  1255. info->freeram <<= bitcount;
  1256. info->sharedram <<= bitcount;
  1257. info->bufferram <<= bitcount;
  1258. info->totalswap <<= bitcount;
  1259. info->freeswap <<= bitcount;
  1260. info->totalhigh <<= bitcount;
  1261. info->freehigh <<= bitcount;
  1262. out:
  1263. return 0;
  1264. }
  1265. SYSCALL_DEFINE1(sysinfo, struct sysinfo __user *, info)
  1266. {
  1267. struct sysinfo val;
  1268. do_sysinfo(&val);
  1269. if (copy_to_user(info, &val, sizeof(struct sysinfo)))
  1270. return -EFAULT;
  1271. return 0;
  1272. }
  1273. static int __cpuinit init_timers_cpu(int cpu)
  1274. {
  1275. int j;
  1276. struct tvec_base *base;
  1277. static char __cpuinitdata tvec_base_done[NR_CPUS];
  1278. if (!tvec_base_done[cpu]) {
  1279. static char boot_done;
  1280. if (boot_done) {
  1281. /*
  1282. * The APs use this path later in boot
  1283. */
  1284. base = kmalloc_node(sizeof(*base),
  1285. GFP_KERNEL | __GFP_ZERO,
  1286. cpu_to_node(cpu));
  1287. if (!base)
  1288. return -ENOMEM;
  1289. /* Make sure that tvec_base is 2 byte aligned */
  1290. if (tbase_get_deferrable(base)) {
  1291. WARN_ON(1);
  1292. kfree(base);
  1293. return -ENOMEM;
  1294. }
  1295. per_cpu(tvec_bases, cpu) = base;
  1296. } else {
  1297. /*
  1298. * This is for the boot CPU - we use compile-time
  1299. * static initialisation because per-cpu memory isn't
  1300. * ready yet and because the memory allocators are not
  1301. * initialised either.
  1302. */
  1303. boot_done = 1;
  1304. base = &boot_tvec_bases;
  1305. }
  1306. tvec_base_done[cpu] = 1;
  1307. } else {
  1308. base = per_cpu(tvec_bases, cpu);
  1309. }
  1310. spin_lock_init(&base->lock);
  1311. for (j = 0; j < TVN_SIZE; j++) {
  1312. INIT_LIST_HEAD(base->tv5.vec + j);
  1313. INIT_LIST_HEAD(base->tv4.vec + j);
  1314. INIT_LIST_HEAD(base->tv3.vec + j);
  1315. INIT_LIST_HEAD(base->tv2.vec + j);
  1316. }
  1317. for (j = 0; j < TVR_SIZE; j++)
  1318. INIT_LIST_HEAD(base->tv1.vec + j);
  1319. base->timer_jiffies = jiffies;
  1320. return 0;
  1321. }
  1322. #ifdef CONFIG_HOTPLUG_CPU
  1323. static void migrate_timer_list(struct tvec_base *new_base, struct list_head *head)
  1324. {
  1325. struct timer_list *timer;
  1326. while (!list_empty(head)) {
  1327. timer = list_first_entry(head, struct timer_list, entry);
  1328. detach_timer(timer, 0);
  1329. timer_set_base(timer, new_base);
  1330. internal_add_timer(new_base, timer);
  1331. }
  1332. }
  1333. static void __cpuinit migrate_timers(int cpu)
  1334. {
  1335. struct tvec_base *old_base;
  1336. struct tvec_base *new_base;
  1337. int i;
  1338. BUG_ON(cpu_online(cpu));
  1339. old_base = per_cpu(tvec_bases, cpu);
  1340. new_base = get_cpu_var(tvec_bases);
  1341. /*
  1342. * The caller is globally serialized and nobody else
  1343. * takes two locks at once, deadlock is not possible.
  1344. */
  1345. spin_lock_irq(&new_base->lock);
  1346. spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
  1347. BUG_ON(old_base->running_timer);
  1348. for (i = 0; i < TVR_SIZE; i++)
  1349. migrate_timer_list(new_base, old_base->tv1.vec + i);
  1350. for (i = 0; i < TVN_SIZE; i++) {
  1351. migrate_timer_list(new_base, old_base->tv2.vec + i);
  1352. migrate_timer_list(new_base, old_base->tv3.vec + i);
  1353. migrate_timer_list(new_base, old_base->tv4.vec + i);
  1354. migrate_timer_list(new_base, old_base->tv5.vec + i);
  1355. }
  1356. spin_unlock(&old_base->lock);
  1357. spin_unlock_irq(&new_base->lock);
  1358. put_cpu_var(tvec_bases);
  1359. }
  1360. #endif /* CONFIG_HOTPLUG_CPU */
  1361. static int __cpuinit timer_cpu_notify(struct notifier_block *self,
  1362. unsigned long action, void *hcpu)
  1363. {
  1364. long cpu = (long)hcpu;
  1365. switch(action) {
  1366. case CPU_UP_PREPARE:
  1367. case CPU_UP_PREPARE_FROZEN:
  1368. if (init_timers_cpu(cpu) < 0)
  1369. return NOTIFY_BAD;
  1370. break;
  1371. #ifdef CONFIG_HOTPLUG_CPU
  1372. case CPU_DEAD:
  1373. case CPU_DEAD_FROZEN:
  1374. migrate_timers(cpu);
  1375. break;
  1376. #endif
  1377. default:
  1378. break;
  1379. }
  1380. return NOTIFY_OK;
  1381. }
  1382. static struct notifier_block __cpuinitdata timers_nb = {
  1383. .notifier_call = timer_cpu_notify,
  1384. };
  1385. void __init init_timers(void)
  1386. {
  1387. int err = timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
  1388. (void *)(long)smp_processor_id());
  1389. init_timer_stats();
  1390. BUG_ON(err == NOTIFY_BAD);
  1391. register_cpu_notifier(&timers_nb);
  1392. open_softirq(TIMER_SOFTIRQ, run_timer_softirq);
  1393. }
  1394. /**
  1395. * msleep - sleep safely even with waitqueue interruptions
  1396. * @msecs: Time in milliseconds to sleep for
  1397. */
  1398. void msleep(unsigned int msecs)
  1399. {
  1400. unsigned long timeout = msecs_to_jiffies(msecs) + 1;
  1401. while (timeout)
  1402. timeout = schedule_timeout_uninterruptible(timeout);
  1403. }
  1404. EXPORT_SYMBOL(msleep);
  1405. /**
  1406. * msleep_interruptible - sleep waiting for signals
  1407. * @msecs: Time in milliseconds to sleep for
  1408. */
  1409. unsigned long msleep_interruptible(unsigned int msecs)
  1410. {
  1411. unsigned long timeout = msecs_to_jiffies(msecs) + 1;
  1412. while (timeout && !signal_pending(current))
  1413. timeout = schedule_timeout_interruptible(timeout);
  1414. return jiffies_to_msecs(timeout);
  1415. }
  1416. EXPORT_SYMBOL(msleep_interruptible);