timer.c 47 KB

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