timer.c 47 KB

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