timer.c 50 KB

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