timer.c 43 KB

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