timer.c 49 KB

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