timer.c 47 KB

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