timer.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787
  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. void setup_deferrable_timer_on_stack_key(struct timer_list *timer,
  519. const char *name,
  520. struct lock_class_key *key,
  521. void (*function)(unsigned long),
  522. unsigned long data)
  523. {
  524. timer->function = function;
  525. timer->data = data;
  526. init_timer_on_stack_key(timer, name, key);
  527. timer_set_deferrable(timer);
  528. }
  529. EXPORT_SYMBOL_GPL(setup_deferrable_timer_on_stack_key);
  530. /**
  531. * init_timer_key - initialize a timer
  532. * @timer: the timer to be initialized
  533. * @name: name of the timer
  534. * @key: lockdep class key of the fake lock used for tracking timer
  535. * sync lock dependencies
  536. *
  537. * init_timer_key() must be done to a timer prior calling *any* of the
  538. * other timer functions.
  539. */
  540. void init_timer_key(struct timer_list *timer,
  541. const char *name,
  542. struct lock_class_key *key)
  543. {
  544. debug_init(timer);
  545. __init_timer(timer, name, key);
  546. }
  547. EXPORT_SYMBOL(init_timer_key);
  548. void init_timer_deferrable_key(struct timer_list *timer,
  549. const char *name,
  550. struct lock_class_key *key)
  551. {
  552. init_timer_key(timer, name, key);
  553. timer_set_deferrable(timer);
  554. }
  555. EXPORT_SYMBOL(init_timer_deferrable_key);
  556. static inline void detach_timer(struct timer_list *timer,
  557. int clear_pending)
  558. {
  559. struct list_head *entry = &timer->entry;
  560. debug_deactivate(timer);
  561. __list_del(entry->prev, entry->next);
  562. if (clear_pending)
  563. entry->next = NULL;
  564. entry->prev = LIST_POISON2;
  565. }
  566. /*
  567. * We are using hashed locking: holding per_cpu(tvec_bases).lock
  568. * means that all timers which are tied to this base via timer->base are
  569. * locked, and the base itself is locked too.
  570. *
  571. * So __run_timers/migrate_timers can safely modify all timers which could
  572. * be found on ->tvX lists.
  573. *
  574. * When the timer's base is locked, and the timer removed from list, it is
  575. * possible to set timer->base = NULL and drop the lock: the timer remains
  576. * locked.
  577. */
  578. static struct tvec_base *lock_timer_base(struct timer_list *timer,
  579. unsigned long *flags)
  580. __acquires(timer->base->lock)
  581. {
  582. struct tvec_base *base;
  583. for (;;) {
  584. struct tvec_base *prelock_base = timer->base;
  585. base = tbase_get_base(prelock_base);
  586. if (likely(base != NULL)) {
  587. spin_lock_irqsave(&base->lock, *flags);
  588. if (likely(prelock_base == timer->base))
  589. return base;
  590. /* The timer has migrated to another CPU */
  591. spin_unlock_irqrestore(&base->lock, *flags);
  592. }
  593. cpu_relax();
  594. }
  595. }
  596. static inline int
  597. __mod_timer(struct timer_list *timer, unsigned long expires,
  598. bool pending_only, int pinned)
  599. {
  600. struct tvec_base *base, *new_base;
  601. unsigned long flags;
  602. int ret = 0 , cpu;
  603. timer_stats_timer_set_start_info(timer);
  604. BUG_ON(!timer->function);
  605. base = lock_timer_base(timer, &flags);
  606. if (timer_pending(timer)) {
  607. detach_timer(timer, 0);
  608. if (timer->expires == base->next_timer &&
  609. !tbase_get_deferrable(timer->base))
  610. base->next_timer = base->timer_jiffies;
  611. ret = 1;
  612. } else {
  613. if (pending_only)
  614. goto out_unlock;
  615. }
  616. debug_activate(timer, expires);
  617. cpu = smp_processor_id();
  618. #if defined(CONFIG_NO_HZ) && defined(CONFIG_SMP)
  619. if (!pinned && get_sysctl_timer_migration() && idle_cpu(cpu))
  620. cpu = get_nohz_timer_target();
  621. #endif
  622. new_base = per_cpu(tvec_bases, cpu);
  623. if (base != new_base) {
  624. /*
  625. * We are trying to schedule the timer on the local CPU.
  626. * However we can't change timer's base while it is running,
  627. * otherwise del_timer_sync() can't detect that the timer's
  628. * handler yet has not finished. This also guarantees that
  629. * the timer is serialized wrt itself.
  630. */
  631. if (likely(base->running_timer != timer)) {
  632. /* See the comment in lock_timer_base() */
  633. timer_set_base(timer, NULL);
  634. spin_unlock(&base->lock);
  635. base = new_base;
  636. spin_lock(&base->lock);
  637. timer_set_base(timer, base);
  638. }
  639. }
  640. timer->expires = expires;
  641. if (time_before(timer->expires, base->next_timer) &&
  642. !tbase_get_deferrable(timer->base))
  643. base->next_timer = timer->expires;
  644. internal_add_timer(base, timer);
  645. out_unlock:
  646. spin_unlock_irqrestore(&base->lock, flags);
  647. return ret;
  648. }
  649. /**
  650. * mod_timer_pending - modify a pending timer's timeout
  651. * @timer: the pending timer to be modified
  652. * @expires: new timeout in jiffies
  653. *
  654. * mod_timer_pending() is the same for pending timers as mod_timer(),
  655. * but will not re-activate and modify already deleted timers.
  656. *
  657. * It is useful for unserialized use of timers.
  658. */
  659. int mod_timer_pending(struct timer_list *timer, unsigned long expires)
  660. {
  661. return __mod_timer(timer, expires, true, TIMER_NOT_PINNED);
  662. }
  663. EXPORT_SYMBOL(mod_timer_pending);
  664. /*
  665. * Decide where to put the timer while taking the slack into account
  666. *
  667. * Algorithm:
  668. * 1) calculate the maximum (absolute) time
  669. * 2) calculate the highest bit where the expires and new max are different
  670. * 3) use this bit to make a mask
  671. * 4) use the bitmask to round down the maximum time, so that all last
  672. * bits are zeros
  673. */
  674. static inline
  675. unsigned long apply_slack(struct timer_list *timer, unsigned long expires)
  676. {
  677. unsigned long expires_limit, mask;
  678. int bit;
  679. expires_limit = expires;
  680. if (timer->slack >= 0) {
  681. expires_limit = expires + timer->slack;
  682. } else {
  683. unsigned long now = jiffies;
  684. /* No slack, if already expired else auto slack 0.4% */
  685. if (time_after(expires, now))
  686. expires_limit = expires + (expires - now)/256;
  687. }
  688. mask = expires ^ expires_limit;
  689. if (mask == 0)
  690. return expires;
  691. bit = find_last_bit(&mask, BITS_PER_LONG);
  692. mask = (1 << bit) - 1;
  693. expires_limit = expires_limit & ~(mask);
  694. return expires_limit;
  695. }
  696. /**
  697. * mod_timer - modify a timer's timeout
  698. * @timer: the timer to be modified
  699. * @expires: new timeout in jiffies
  700. *
  701. * mod_timer() is a more efficient way to update the expire field of an
  702. * active timer (if the timer is inactive it will be activated)
  703. *
  704. * mod_timer(timer, expires) is equivalent to:
  705. *
  706. * del_timer(timer); timer->expires = expires; add_timer(timer);
  707. *
  708. * Note that if there are multiple unserialized concurrent users of the
  709. * same timer, then mod_timer() is the only safe way to modify the timeout,
  710. * since add_timer() cannot modify an already running timer.
  711. *
  712. * The function returns whether it has modified a pending timer or not.
  713. * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
  714. * active timer returns 1.)
  715. */
  716. int mod_timer(struct timer_list *timer, unsigned long expires)
  717. {
  718. /*
  719. * This is a common optimization triggered by the
  720. * networking code - if the timer is re-modified
  721. * to be the same thing then just return:
  722. */
  723. if (timer_pending(timer) && timer->expires == expires)
  724. return 1;
  725. expires = apply_slack(timer, expires);
  726. return __mod_timer(timer, expires, false, TIMER_NOT_PINNED);
  727. }
  728. EXPORT_SYMBOL(mod_timer);
  729. /**
  730. * mod_timer_pinned - modify a timer's timeout
  731. * @timer: the timer to be modified
  732. * @expires: new timeout in jiffies
  733. *
  734. * mod_timer_pinned() is a way to update the expire field of an
  735. * active timer (if the timer is inactive it will be activated)
  736. * and not allow the timer to be migrated to a different CPU.
  737. *
  738. * mod_timer_pinned(timer, expires) is equivalent to:
  739. *
  740. * del_timer(timer); timer->expires = expires; add_timer(timer);
  741. */
  742. int mod_timer_pinned(struct timer_list *timer, unsigned long expires)
  743. {
  744. if (timer->expires == expires && timer_pending(timer))
  745. return 1;
  746. return __mod_timer(timer, expires, false, TIMER_PINNED);
  747. }
  748. EXPORT_SYMBOL(mod_timer_pinned);
  749. /**
  750. * add_timer - start a timer
  751. * @timer: the timer to be added
  752. *
  753. * The kernel will do a ->function(->data) callback from the
  754. * timer interrupt at the ->expires point in the future. The
  755. * current time is 'jiffies'.
  756. *
  757. * The timer's ->expires, ->function (and if the handler uses it, ->data)
  758. * fields must be set prior calling this function.
  759. *
  760. * Timers with an ->expires field in the past will be executed in the next
  761. * timer tick.
  762. */
  763. void add_timer(struct timer_list *timer)
  764. {
  765. BUG_ON(timer_pending(timer));
  766. mod_timer(timer, timer->expires);
  767. }
  768. EXPORT_SYMBOL(add_timer);
  769. /**
  770. * add_timer_on - start a timer on a particular CPU
  771. * @timer: the timer to be added
  772. * @cpu: the CPU to start it on
  773. *
  774. * This is not very scalable on SMP. Double adds are not possible.
  775. */
  776. void add_timer_on(struct timer_list *timer, int cpu)
  777. {
  778. struct tvec_base *base = per_cpu(tvec_bases, cpu);
  779. unsigned long flags;
  780. timer_stats_timer_set_start_info(timer);
  781. BUG_ON(timer_pending(timer) || !timer->function);
  782. spin_lock_irqsave(&base->lock, flags);
  783. timer_set_base(timer, base);
  784. debug_activate(timer, timer->expires);
  785. if (time_before(timer->expires, base->next_timer) &&
  786. !tbase_get_deferrable(timer->base))
  787. base->next_timer = timer->expires;
  788. internal_add_timer(base, timer);
  789. /*
  790. * Check whether the other CPU is idle and needs to be
  791. * triggered to reevaluate the timer wheel when nohz is
  792. * active. We are protected against the other CPU fiddling
  793. * with the timer by holding the timer base lock. This also
  794. * makes sure that a CPU on the way to idle can not evaluate
  795. * the timer wheel.
  796. */
  797. wake_up_idle_cpu(cpu);
  798. spin_unlock_irqrestore(&base->lock, flags);
  799. }
  800. EXPORT_SYMBOL_GPL(add_timer_on);
  801. /**
  802. * del_timer - deactive a timer.
  803. * @timer: the timer to be deactivated
  804. *
  805. * del_timer() deactivates a timer - this works on both active and inactive
  806. * timers.
  807. *
  808. * The function returns whether it has deactivated a pending timer or not.
  809. * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
  810. * active timer returns 1.)
  811. */
  812. int del_timer(struct timer_list *timer)
  813. {
  814. struct tvec_base *base;
  815. unsigned long flags;
  816. int ret = 0;
  817. timer_stats_timer_clear_start_info(timer);
  818. if (timer_pending(timer)) {
  819. base = lock_timer_base(timer, &flags);
  820. if (timer_pending(timer)) {
  821. detach_timer(timer, 1);
  822. if (timer->expires == base->next_timer &&
  823. !tbase_get_deferrable(timer->base))
  824. base->next_timer = base->timer_jiffies;
  825. ret = 1;
  826. }
  827. spin_unlock_irqrestore(&base->lock, flags);
  828. }
  829. return ret;
  830. }
  831. EXPORT_SYMBOL(del_timer);
  832. #ifdef CONFIG_SMP
  833. /**
  834. * try_to_del_timer_sync - Try to deactivate a timer
  835. * @timer: timer do del
  836. *
  837. * This function tries to deactivate a timer. Upon successful (ret >= 0)
  838. * exit the timer is not queued and the handler is not running on any CPU.
  839. *
  840. * It must not be called from interrupt contexts.
  841. */
  842. int try_to_del_timer_sync(struct timer_list *timer)
  843. {
  844. struct tvec_base *base;
  845. unsigned long flags;
  846. int ret = -1;
  847. base = lock_timer_base(timer, &flags);
  848. if (base->running_timer == timer)
  849. goto out;
  850. timer_stats_timer_clear_start_info(timer);
  851. ret = 0;
  852. if (timer_pending(timer)) {
  853. detach_timer(timer, 1);
  854. if (timer->expires == base->next_timer &&
  855. !tbase_get_deferrable(timer->base))
  856. base->next_timer = base->timer_jiffies;
  857. ret = 1;
  858. }
  859. out:
  860. spin_unlock_irqrestore(&base->lock, flags);
  861. return ret;
  862. }
  863. EXPORT_SYMBOL(try_to_del_timer_sync);
  864. /**
  865. * del_timer_sync - deactivate a timer and wait for the handler to finish.
  866. * @timer: the timer to be deactivated
  867. *
  868. * This function only differs from del_timer() on SMP: besides deactivating
  869. * the timer it also makes sure the handler has finished executing on other
  870. * CPUs.
  871. *
  872. * Synchronization rules: Callers must prevent restarting of the timer,
  873. * otherwise this function is meaningless. It must not be called from
  874. * interrupt contexts. The caller must not hold locks which would prevent
  875. * completion of the timer's handler. The timer's handler must not call
  876. * add_timer_on(). Upon exit the timer is not queued and the handler is
  877. * not running on any CPU.
  878. *
  879. * The function returns whether it has deactivated a pending timer or not.
  880. */
  881. int del_timer_sync(struct timer_list *timer)
  882. {
  883. #ifdef CONFIG_LOCKDEP
  884. unsigned long flags;
  885. local_irq_save(flags);
  886. lock_map_acquire(&timer->lockdep_map);
  887. lock_map_release(&timer->lockdep_map);
  888. local_irq_restore(flags);
  889. #endif
  890. for (;;) {
  891. int ret = try_to_del_timer_sync(timer);
  892. if (ret >= 0)
  893. return ret;
  894. cpu_relax();
  895. }
  896. }
  897. EXPORT_SYMBOL(del_timer_sync);
  898. #endif
  899. static int cascade(struct tvec_base *base, struct tvec *tv, int index)
  900. {
  901. /* cascade all the timers from tv up one level */
  902. struct timer_list *timer, *tmp;
  903. struct list_head tv_list;
  904. list_replace_init(tv->vec + index, &tv_list);
  905. /*
  906. * We are removing _all_ timers from the list, so we
  907. * don't have to detach them individually.
  908. */
  909. list_for_each_entry_safe(timer, tmp, &tv_list, entry) {
  910. BUG_ON(tbase_get_base(timer->base) != base);
  911. internal_add_timer(base, timer);
  912. }
  913. return index;
  914. }
  915. static void call_timer_fn(struct timer_list *timer, void (*fn)(unsigned long),
  916. unsigned long data)
  917. {
  918. int preempt_count = preempt_count();
  919. #ifdef CONFIG_LOCKDEP
  920. /*
  921. * It is permissible to free the timer from inside the
  922. * function that is called from it, this we need to take into
  923. * account for lockdep too. To avoid bogus "held lock freed"
  924. * warnings as well as problems when looking into
  925. * timer->lockdep_map, make a copy and use that here.
  926. */
  927. struct lockdep_map lockdep_map = timer->lockdep_map;
  928. #endif
  929. /*
  930. * Couple the lock chain with the lock chain at
  931. * del_timer_sync() by acquiring the lock_map around the fn()
  932. * call here and in del_timer_sync().
  933. */
  934. lock_map_acquire(&lockdep_map);
  935. trace_timer_expire_entry(timer);
  936. fn(data);
  937. trace_timer_expire_exit(timer);
  938. lock_map_release(&lockdep_map);
  939. if (preempt_count != preempt_count()) {
  940. WARN_ONCE(1, "timer: %pF preempt leak: %08x -> %08x\n",
  941. fn, preempt_count, preempt_count());
  942. /*
  943. * Restore the preempt count. That gives us a decent
  944. * chance to survive and extract information. If the
  945. * callback kept a lock held, bad luck, but not worse
  946. * than the BUG() we had.
  947. */
  948. preempt_count() = preempt_count;
  949. }
  950. }
  951. #define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK)
  952. /**
  953. * __run_timers - run all expired timers (if any) on this CPU.
  954. * @base: the timer vector to be processed.
  955. *
  956. * This function cascades all vectors and executes all expired timer
  957. * vectors.
  958. */
  959. static inline void __run_timers(struct tvec_base *base)
  960. {
  961. struct timer_list *timer;
  962. spin_lock_irq(&base->lock);
  963. while (time_after_eq(jiffies, base->timer_jiffies)) {
  964. struct list_head work_list;
  965. struct list_head *head = &work_list;
  966. int index = base->timer_jiffies & TVR_MASK;
  967. /*
  968. * Cascade timers:
  969. */
  970. if (!index &&
  971. (!cascade(base, &base->tv2, INDEX(0))) &&
  972. (!cascade(base, &base->tv3, INDEX(1))) &&
  973. !cascade(base, &base->tv4, INDEX(2)))
  974. cascade(base, &base->tv5, INDEX(3));
  975. ++base->timer_jiffies;
  976. list_replace_init(base->tv1.vec + index, &work_list);
  977. while (!list_empty(head)) {
  978. void (*fn)(unsigned long);
  979. unsigned long data;
  980. timer = list_first_entry(head, struct timer_list,entry);
  981. fn = timer->function;
  982. data = timer->data;
  983. timer_stats_account_timer(timer);
  984. set_running_timer(base, timer);
  985. detach_timer(timer, 1);
  986. spin_unlock_irq(&base->lock);
  987. call_timer_fn(timer, fn, data);
  988. spin_lock_irq(&base->lock);
  989. }
  990. }
  991. set_running_timer(base, NULL);
  992. spin_unlock_irq(&base->lock);
  993. }
  994. #ifdef CONFIG_NO_HZ
  995. /*
  996. * Find out when the next timer event is due to happen. This
  997. * is used on S/390 to stop all activity when a CPU is idle.
  998. * This function needs to be called with interrupts disabled.
  999. */
  1000. static unsigned long __next_timer_interrupt(struct tvec_base *base)
  1001. {
  1002. unsigned long timer_jiffies = base->timer_jiffies;
  1003. unsigned long expires = timer_jiffies + NEXT_TIMER_MAX_DELTA;
  1004. int index, slot, array, found = 0;
  1005. struct timer_list *nte;
  1006. struct tvec *varray[4];
  1007. /* Look for timer events in tv1. */
  1008. index = slot = timer_jiffies & TVR_MASK;
  1009. do {
  1010. list_for_each_entry(nte, base->tv1.vec + slot, entry) {
  1011. if (tbase_get_deferrable(nte->base))
  1012. continue;
  1013. found = 1;
  1014. expires = nte->expires;
  1015. /* Look at the cascade bucket(s)? */
  1016. if (!index || slot < index)
  1017. goto cascade;
  1018. return expires;
  1019. }
  1020. slot = (slot + 1) & TVR_MASK;
  1021. } while (slot != index);
  1022. cascade:
  1023. /* Calculate the next cascade event */
  1024. if (index)
  1025. timer_jiffies += TVR_SIZE - index;
  1026. timer_jiffies >>= TVR_BITS;
  1027. /* Check tv2-tv5. */
  1028. varray[0] = &base->tv2;
  1029. varray[1] = &base->tv3;
  1030. varray[2] = &base->tv4;
  1031. varray[3] = &base->tv5;
  1032. for (array = 0; array < 4; array++) {
  1033. struct tvec *varp = varray[array];
  1034. index = slot = timer_jiffies & TVN_MASK;
  1035. do {
  1036. list_for_each_entry(nte, varp->vec + slot, entry) {
  1037. if (tbase_get_deferrable(nte->base))
  1038. continue;
  1039. found = 1;
  1040. if (time_before(nte->expires, expires))
  1041. expires = nte->expires;
  1042. }
  1043. /*
  1044. * Do we still search for the first timer or are
  1045. * we looking up the cascade buckets ?
  1046. */
  1047. if (found) {
  1048. /* Look at the cascade bucket(s)? */
  1049. if (!index || slot < index)
  1050. break;
  1051. return expires;
  1052. }
  1053. slot = (slot + 1) & TVN_MASK;
  1054. } while (slot != index);
  1055. if (index)
  1056. timer_jiffies += TVN_SIZE - index;
  1057. timer_jiffies >>= TVN_BITS;
  1058. }
  1059. return expires;
  1060. }
  1061. /*
  1062. * Check, if the next hrtimer event is before the next timer wheel
  1063. * event:
  1064. */
  1065. static unsigned long cmp_next_hrtimer_event(unsigned long now,
  1066. unsigned long expires)
  1067. {
  1068. ktime_t hr_delta = hrtimer_get_next_event();
  1069. struct timespec tsdelta;
  1070. unsigned long delta;
  1071. if (hr_delta.tv64 == KTIME_MAX)
  1072. return expires;
  1073. /*
  1074. * Expired timer available, let it expire in the next tick
  1075. */
  1076. if (hr_delta.tv64 <= 0)
  1077. return now + 1;
  1078. tsdelta = ktime_to_timespec(hr_delta);
  1079. delta = timespec_to_jiffies(&tsdelta);
  1080. /*
  1081. * Limit the delta to the max value, which is checked in
  1082. * tick_nohz_stop_sched_tick():
  1083. */
  1084. if (delta > NEXT_TIMER_MAX_DELTA)
  1085. delta = NEXT_TIMER_MAX_DELTA;
  1086. /*
  1087. * Take rounding errors in to account and make sure, that it
  1088. * expires in the next tick. Otherwise we go into an endless
  1089. * ping pong due to tick_nohz_stop_sched_tick() retriggering
  1090. * the timer softirq
  1091. */
  1092. if (delta < 1)
  1093. delta = 1;
  1094. now += delta;
  1095. if (time_before(now, expires))
  1096. return now;
  1097. return expires;
  1098. }
  1099. /**
  1100. * get_next_timer_interrupt - return the jiffy of the next pending timer
  1101. * @now: current time (in jiffies)
  1102. */
  1103. unsigned long get_next_timer_interrupt(unsigned long now)
  1104. {
  1105. struct tvec_base *base = __get_cpu_var(tvec_bases);
  1106. unsigned long expires;
  1107. spin_lock(&base->lock);
  1108. if (time_before_eq(base->next_timer, base->timer_jiffies))
  1109. base->next_timer = __next_timer_interrupt(base);
  1110. expires = base->next_timer;
  1111. spin_unlock(&base->lock);
  1112. if (time_before_eq(expires, now))
  1113. return now;
  1114. return cmp_next_hrtimer_event(now, expires);
  1115. }
  1116. #endif
  1117. /*
  1118. * Called from the timer interrupt handler to charge one tick to the current
  1119. * process. user_tick is 1 if the tick is user time, 0 for system.
  1120. */
  1121. void update_process_times(int user_tick)
  1122. {
  1123. struct task_struct *p = current;
  1124. int cpu = smp_processor_id();
  1125. /* Note: this timer irq context must be accounted for as well. */
  1126. account_process_tick(p, user_tick);
  1127. run_local_timers();
  1128. rcu_check_callbacks(cpu, user_tick);
  1129. printk_tick();
  1130. perf_event_do_pending();
  1131. scheduler_tick();
  1132. run_posix_cpu_timers(p);
  1133. }
  1134. /*
  1135. * This function runs timers and the timer-tq in bottom half context.
  1136. */
  1137. static void run_timer_softirq(struct softirq_action *h)
  1138. {
  1139. struct tvec_base *base = __get_cpu_var(tvec_bases);
  1140. hrtimer_run_pending();
  1141. if (time_after_eq(jiffies, base->timer_jiffies))
  1142. __run_timers(base);
  1143. }
  1144. /*
  1145. * Called by the local, per-CPU timer interrupt on SMP.
  1146. */
  1147. void run_local_timers(void)
  1148. {
  1149. hrtimer_run_queues();
  1150. raise_softirq(TIMER_SOFTIRQ);
  1151. }
  1152. /*
  1153. * The 64-bit jiffies value is not atomic - you MUST NOT read it
  1154. * without sampling the sequence number in xtime_lock.
  1155. * jiffies is defined in the linker script...
  1156. */
  1157. void do_timer(unsigned long ticks)
  1158. {
  1159. jiffies_64 += ticks;
  1160. update_wall_time();
  1161. calc_global_load();
  1162. }
  1163. #ifdef __ARCH_WANT_SYS_ALARM
  1164. /*
  1165. * For backwards compatibility? This can be done in libc so Alpha
  1166. * and all newer ports shouldn't need it.
  1167. */
  1168. SYSCALL_DEFINE1(alarm, unsigned int, seconds)
  1169. {
  1170. return alarm_setitimer(seconds);
  1171. }
  1172. #endif
  1173. #ifndef __alpha__
  1174. /*
  1175. * The Alpha uses getxpid, getxuid, and getxgid instead. Maybe this
  1176. * should be moved into arch/i386 instead?
  1177. */
  1178. /**
  1179. * sys_getpid - return the thread group id of the current process
  1180. *
  1181. * Note, despite the name, this returns the tgid not the pid. The tgid and
  1182. * the pid are identical unless CLONE_THREAD was specified on clone() in
  1183. * which case the tgid is the same in all threads of the same group.
  1184. *
  1185. * This is SMP safe as current->tgid does not change.
  1186. */
  1187. SYSCALL_DEFINE0(getpid)
  1188. {
  1189. return task_tgid_vnr(current);
  1190. }
  1191. /*
  1192. * Accessing ->real_parent is not SMP-safe, it could
  1193. * change from under us. However, we can use a stale
  1194. * value of ->real_parent under rcu_read_lock(), see
  1195. * release_task()->call_rcu(delayed_put_task_struct).
  1196. */
  1197. SYSCALL_DEFINE0(getppid)
  1198. {
  1199. int pid;
  1200. rcu_read_lock();
  1201. pid = task_tgid_vnr(current->real_parent);
  1202. rcu_read_unlock();
  1203. return pid;
  1204. }
  1205. SYSCALL_DEFINE0(getuid)
  1206. {
  1207. /* Only we change this so SMP safe */
  1208. return current_uid();
  1209. }
  1210. SYSCALL_DEFINE0(geteuid)
  1211. {
  1212. /* Only we change this so SMP safe */
  1213. return current_euid();
  1214. }
  1215. SYSCALL_DEFINE0(getgid)
  1216. {
  1217. /* Only we change this so SMP safe */
  1218. return current_gid();
  1219. }
  1220. SYSCALL_DEFINE0(getegid)
  1221. {
  1222. /* Only we change this so SMP safe */
  1223. return current_egid();
  1224. }
  1225. #endif
  1226. static void process_timeout(unsigned long __data)
  1227. {
  1228. wake_up_process((struct task_struct *)__data);
  1229. }
  1230. /**
  1231. * schedule_timeout - sleep until timeout
  1232. * @timeout: timeout value in jiffies
  1233. *
  1234. * Make the current task sleep until @timeout jiffies have
  1235. * elapsed. The routine will return immediately unless
  1236. * the current task state has been set (see set_current_state()).
  1237. *
  1238. * You can set the task state as follows -
  1239. *
  1240. * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
  1241. * pass before the routine returns. The routine will return 0
  1242. *
  1243. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  1244. * delivered to the current task. In this case the remaining time
  1245. * in jiffies will be returned, or 0 if the timer expired in time
  1246. *
  1247. * The current task state is guaranteed to be TASK_RUNNING when this
  1248. * routine returns.
  1249. *
  1250. * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
  1251. * the CPU away without a bound on the timeout. In this case the return
  1252. * value will be %MAX_SCHEDULE_TIMEOUT.
  1253. *
  1254. * In all cases the return value is guaranteed to be non-negative.
  1255. */
  1256. signed long __sched schedule_timeout(signed long timeout)
  1257. {
  1258. struct timer_list timer;
  1259. unsigned long expire;
  1260. switch (timeout)
  1261. {
  1262. case MAX_SCHEDULE_TIMEOUT:
  1263. /*
  1264. * These two special cases are useful to be comfortable
  1265. * in the caller. Nothing more. We could take
  1266. * MAX_SCHEDULE_TIMEOUT from one of the negative value
  1267. * but I' d like to return a valid offset (>=0) to allow
  1268. * the caller to do everything it want with the retval.
  1269. */
  1270. schedule();
  1271. goto out;
  1272. default:
  1273. /*
  1274. * Another bit of PARANOID. Note that the retval will be
  1275. * 0 since no piece of kernel is supposed to do a check
  1276. * for a negative retval of schedule_timeout() (since it
  1277. * should never happens anyway). You just have the printk()
  1278. * that will tell you if something is gone wrong and where.
  1279. */
  1280. if (timeout < 0) {
  1281. printk(KERN_ERR "schedule_timeout: wrong timeout "
  1282. "value %lx\n", timeout);
  1283. dump_stack();
  1284. current->state = TASK_RUNNING;
  1285. goto out;
  1286. }
  1287. }
  1288. expire = timeout + jiffies;
  1289. setup_timer_on_stack(&timer, process_timeout, (unsigned long)current);
  1290. __mod_timer(&timer, expire, false, TIMER_NOT_PINNED);
  1291. schedule();
  1292. del_singleshot_timer_sync(&timer);
  1293. /* Remove the timer from the object tracker */
  1294. destroy_timer_on_stack(&timer);
  1295. timeout = expire - jiffies;
  1296. out:
  1297. return timeout < 0 ? 0 : timeout;
  1298. }
  1299. EXPORT_SYMBOL(schedule_timeout);
  1300. /*
  1301. * We can use __set_current_state() here because schedule_timeout() calls
  1302. * schedule() unconditionally.
  1303. */
  1304. signed long __sched schedule_timeout_interruptible(signed long timeout)
  1305. {
  1306. __set_current_state(TASK_INTERRUPTIBLE);
  1307. return schedule_timeout(timeout);
  1308. }
  1309. EXPORT_SYMBOL(schedule_timeout_interruptible);
  1310. signed long __sched schedule_timeout_killable(signed long timeout)
  1311. {
  1312. __set_current_state(TASK_KILLABLE);
  1313. return schedule_timeout(timeout);
  1314. }
  1315. EXPORT_SYMBOL(schedule_timeout_killable);
  1316. signed long __sched schedule_timeout_uninterruptible(signed long timeout)
  1317. {
  1318. __set_current_state(TASK_UNINTERRUPTIBLE);
  1319. return schedule_timeout(timeout);
  1320. }
  1321. EXPORT_SYMBOL(schedule_timeout_uninterruptible);
  1322. /* Thread ID - the internal kernel "pid" */
  1323. SYSCALL_DEFINE0(gettid)
  1324. {
  1325. return task_pid_vnr(current);
  1326. }
  1327. /**
  1328. * do_sysinfo - fill in sysinfo struct
  1329. * @info: pointer to buffer to fill
  1330. */
  1331. int do_sysinfo(struct sysinfo *info)
  1332. {
  1333. unsigned long mem_total, sav_total;
  1334. unsigned int mem_unit, bitcount;
  1335. struct timespec tp;
  1336. memset(info, 0, sizeof(struct sysinfo));
  1337. ktime_get_ts(&tp);
  1338. monotonic_to_bootbased(&tp);
  1339. info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
  1340. get_avenrun(info->loads, 0, SI_LOAD_SHIFT - FSHIFT);
  1341. info->procs = nr_threads;
  1342. si_meminfo(info);
  1343. si_swapinfo(info);
  1344. /*
  1345. * If the sum of all the available memory (i.e. ram + swap)
  1346. * is less than can be stored in a 32 bit unsigned long then
  1347. * we can be binary compatible with 2.2.x kernels. If not,
  1348. * well, in that case 2.2.x was broken anyways...
  1349. *
  1350. * -Erik Andersen <andersee@debian.org>
  1351. */
  1352. mem_total = info->totalram + info->totalswap;
  1353. if (mem_total < info->totalram || mem_total < info->totalswap)
  1354. goto out;
  1355. bitcount = 0;
  1356. mem_unit = info->mem_unit;
  1357. while (mem_unit > 1) {
  1358. bitcount++;
  1359. mem_unit >>= 1;
  1360. sav_total = mem_total;
  1361. mem_total <<= 1;
  1362. if (mem_total < sav_total)
  1363. goto out;
  1364. }
  1365. /*
  1366. * If mem_total did not overflow, multiply all memory values by
  1367. * info->mem_unit and set it to 1. This leaves things compatible
  1368. * with 2.2.x, and also retains compatibility with earlier 2.4.x
  1369. * kernels...
  1370. */
  1371. info->mem_unit = 1;
  1372. info->totalram <<= bitcount;
  1373. info->freeram <<= bitcount;
  1374. info->sharedram <<= bitcount;
  1375. info->bufferram <<= bitcount;
  1376. info->totalswap <<= bitcount;
  1377. info->freeswap <<= bitcount;
  1378. info->totalhigh <<= bitcount;
  1379. info->freehigh <<= bitcount;
  1380. out:
  1381. return 0;
  1382. }
  1383. SYSCALL_DEFINE1(sysinfo, struct sysinfo __user *, info)
  1384. {
  1385. struct sysinfo val;
  1386. do_sysinfo(&val);
  1387. if (copy_to_user(info, &val, sizeof(struct sysinfo)))
  1388. return -EFAULT;
  1389. return 0;
  1390. }
  1391. static int __cpuinit init_timers_cpu(int cpu)
  1392. {
  1393. int j;
  1394. struct tvec_base *base;
  1395. static char __cpuinitdata tvec_base_done[NR_CPUS];
  1396. if (!tvec_base_done[cpu]) {
  1397. static char boot_done;
  1398. if (boot_done) {
  1399. /*
  1400. * The APs use this path later in boot
  1401. */
  1402. base = kmalloc_node(sizeof(*base),
  1403. GFP_KERNEL | __GFP_ZERO,
  1404. cpu_to_node(cpu));
  1405. if (!base)
  1406. return -ENOMEM;
  1407. /* Make sure that tvec_base is 2 byte aligned */
  1408. if (tbase_get_deferrable(base)) {
  1409. WARN_ON(1);
  1410. kfree(base);
  1411. return -ENOMEM;
  1412. }
  1413. per_cpu(tvec_bases, cpu) = base;
  1414. } else {
  1415. /*
  1416. * This is for the boot CPU - we use compile-time
  1417. * static initialisation because per-cpu memory isn't
  1418. * ready yet and because the memory allocators are not
  1419. * initialised either.
  1420. */
  1421. boot_done = 1;
  1422. base = &boot_tvec_bases;
  1423. }
  1424. tvec_base_done[cpu] = 1;
  1425. } else {
  1426. base = per_cpu(tvec_bases, cpu);
  1427. }
  1428. spin_lock_init(&base->lock);
  1429. for (j = 0; j < TVN_SIZE; j++) {
  1430. INIT_LIST_HEAD(base->tv5.vec + j);
  1431. INIT_LIST_HEAD(base->tv4.vec + j);
  1432. INIT_LIST_HEAD(base->tv3.vec + j);
  1433. INIT_LIST_HEAD(base->tv2.vec + j);
  1434. }
  1435. for (j = 0; j < TVR_SIZE; j++)
  1436. INIT_LIST_HEAD(base->tv1.vec + j);
  1437. base->timer_jiffies = jiffies;
  1438. base->next_timer = base->timer_jiffies;
  1439. return 0;
  1440. }
  1441. #ifdef CONFIG_HOTPLUG_CPU
  1442. static void migrate_timer_list(struct tvec_base *new_base, struct list_head *head)
  1443. {
  1444. struct timer_list *timer;
  1445. while (!list_empty(head)) {
  1446. timer = list_first_entry(head, struct timer_list, entry);
  1447. detach_timer(timer, 0);
  1448. timer_set_base(timer, new_base);
  1449. if (time_before(timer->expires, new_base->next_timer) &&
  1450. !tbase_get_deferrable(timer->base))
  1451. new_base->next_timer = timer->expires;
  1452. internal_add_timer(new_base, timer);
  1453. }
  1454. }
  1455. static void __cpuinit migrate_timers(int cpu)
  1456. {
  1457. struct tvec_base *old_base;
  1458. struct tvec_base *new_base;
  1459. int i;
  1460. BUG_ON(cpu_online(cpu));
  1461. old_base = per_cpu(tvec_bases, cpu);
  1462. new_base = get_cpu_var(tvec_bases);
  1463. /*
  1464. * The caller is globally serialized and nobody else
  1465. * takes two locks at once, deadlock is not possible.
  1466. */
  1467. spin_lock_irq(&new_base->lock);
  1468. spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
  1469. BUG_ON(old_base->running_timer);
  1470. for (i = 0; i < TVR_SIZE; i++)
  1471. migrate_timer_list(new_base, old_base->tv1.vec + i);
  1472. for (i = 0; i < TVN_SIZE; i++) {
  1473. migrate_timer_list(new_base, old_base->tv2.vec + i);
  1474. migrate_timer_list(new_base, old_base->tv3.vec + i);
  1475. migrate_timer_list(new_base, old_base->tv4.vec + i);
  1476. migrate_timer_list(new_base, old_base->tv5.vec + i);
  1477. }
  1478. spin_unlock(&old_base->lock);
  1479. spin_unlock_irq(&new_base->lock);
  1480. put_cpu_var(tvec_bases);
  1481. }
  1482. #endif /* CONFIG_HOTPLUG_CPU */
  1483. static int __cpuinit timer_cpu_notify(struct notifier_block *self,
  1484. unsigned long action, void *hcpu)
  1485. {
  1486. long cpu = (long)hcpu;
  1487. int err;
  1488. switch(action) {
  1489. case CPU_UP_PREPARE:
  1490. case CPU_UP_PREPARE_FROZEN:
  1491. err = init_timers_cpu(cpu);
  1492. if (err < 0)
  1493. return notifier_from_errno(err);
  1494. break;
  1495. #ifdef CONFIG_HOTPLUG_CPU
  1496. case CPU_DEAD:
  1497. case CPU_DEAD_FROZEN:
  1498. migrate_timers(cpu);
  1499. break;
  1500. #endif
  1501. default:
  1502. break;
  1503. }
  1504. return NOTIFY_OK;
  1505. }
  1506. static struct notifier_block __cpuinitdata timers_nb = {
  1507. .notifier_call = timer_cpu_notify,
  1508. };
  1509. void __init init_timers(void)
  1510. {
  1511. int err = timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
  1512. (void *)(long)smp_processor_id());
  1513. init_timer_stats();
  1514. BUG_ON(err != NOTIFY_OK);
  1515. register_cpu_notifier(&timers_nb);
  1516. open_softirq(TIMER_SOFTIRQ, run_timer_softirq);
  1517. }
  1518. /**
  1519. * msleep - sleep safely even with waitqueue interruptions
  1520. * @msecs: Time in milliseconds to sleep for
  1521. */
  1522. void msleep(unsigned int msecs)
  1523. {
  1524. unsigned long timeout = msecs_to_jiffies(msecs) + 1;
  1525. while (timeout)
  1526. timeout = schedule_timeout_uninterruptible(timeout);
  1527. }
  1528. EXPORT_SYMBOL(msleep);
  1529. /**
  1530. * msleep_interruptible - sleep waiting for signals
  1531. * @msecs: Time in milliseconds to sleep for
  1532. */
  1533. unsigned long msleep_interruptible(unsigned int msecs)
  1534. {
  1535. unsigned long timeout = msecs_to_jiffies(msecs) + 1;
  1536. while (timeout && !signal_pending(current))
  1537. timeout = schedule_timeout_interruptible(timeout);
  1538. return jiffies_to_msecs(timeout);
  1539. }
  1540. EXPORT_SYMBOL(msleep_interruptible);
  1541. static int __sched do_usleep_range(unsigned long min, unsigned long max)
  1542. {
  1543. ktime_t kmin;
  1544. unsigned long delta;
  1545. kmin = ktime_set(0, min * NSEC_PER_USEC);
  1546. delta = (max - min) * NSEC_PER_USEC;
  1547. return schedule_hrtimeout_range(&kmin, delta, HRTIMER_MODE_REL);
  1548. }
  1549. /**
  1550. * usleep_range - Drop in replacement for udelay where wakeup is flexible
  1551. * @min: Minimum time in usecs to sleep
  1552. * @max: Maximum time in usecs to sleep
  1553. */
  1554. void usleep_range(unsigned long min, unsigned long max)
  1555. {
  1556. __set_current_state(TASK_UNINTERRUPTIBLE);
  1557. do_usleep_range(min, max);
  1558. }
  1559. EXPORT_SYMBOL(usleep_range);