timer.c 44 KB

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