timer.c 47 KB

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