timer.c 49 KB

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