timer.c 44 KB

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