timer.c 47 KB

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