timer.c 42 KB

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