timer.c 50 KB

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