timer.c 44 KB

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