timer.c 44 KB

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