timer.c 47 KB

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