timer.c 46 KB

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