timer.c 45 KB

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