itimer.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * linux/kernel/itimer.c
  3. *
  4. * Copyright (C) 1992 Darren Senn
  5. */
  6. /* These are all the functions necessary to implement itimers */
  7. #include <linux/mm.h>
  8. #include <linux/smp_lock.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/syscalls.h>
  11. #include <linux/time.h>
  12. #include <linux/posix-timers.h>
  13. #include <linux/hrtimer.h>
  14. #include <asm/uaccess.h>
  15. /**
  16. * itimer_get_remtime - get remaining time for the timer
  17. *
  18. * @timer: the timer to read
  19. *
  20. * Returns the delta between the expiry time and now, which can be
  21. * less than zero or 1usec for an pending expired timer
  22. */
  23. static struct timeval itimer_get_remtime(struct hrtimer *timer)
  24. {
  25. ktime_t rem = hrtimer_get_remaining(timer);
  26. /*
  27. * Racy but safe: if the itimer expires after the above
  28. * hrtimer_get_remtime() call but before this condition
  29. * then we return 0 - which is correct.
  30. */
  31. if (hrtimer_active(timer)) {
  32. if (rem.tv64 <= 0)
  33. rem.tv64 = NSEC_PER_USEC;
  34. } else
  35. rem.tv64 = 0;
  36. return ktime_to_timeval(rem);
  37. }
  38. int do_getitimer(int which, struct itimerval *value)
  39. {
  40. struct task_struct *tsk = current;
  41. cputime_t cinterval, cval;
  42. switch (which) {
  43. case ITIMER_REAL:
  44. spin_lock_irq(&tsk->sighand->siglock);
  45. value->it_value = itimer_get_remtime(&tsk->signal->real_timer);
  46. value->it_interval =
  47. ktime_to_timeval(tsk->signal->it_real_incr);
  48. spin_unlock_irq(&tsk->sighand->siglock);
  49. break;
  50. case ITIMER_VIRTUAL:
  51. read_lock(&tasklist_lock);
  52. spin_lock_irq(&tsk->sighand->siglock);
  53. cval = tsk->signal->it_virt_expires;
  54. cinterval = tsk->signal->it_virt_incr;
  55. if (!cputime_eq(cval, cputime_zero)) {
  56. struct task_struct *t = tsk;
  57. cputime_t utime = tsk->signal->utime;
  58. do {
  59. utime = cputime_add(utime, t->utime);
  60. t = next_thread(t);
  61. } while (t != tsk);
  62. if (cputime_le(cval, utime)) { /* about to fire */
  63. cval = jiffies_to_cputime(1);
  64. } else {
  65. cval = cputime_sub(cval, utime);
  66. }
  67. }
  68. spin_unlock_irq(&tsk->sighand->siglock);
  69. read_unlock(&tasklist_lock);
  70. cputime_to_timeval(cval, &value->it_value);
  71. cputime_to_timeval(cinterval, &value->it_interval);
  72. break;
  73. case ITIMER_PROF:
  74. read_lock(&tasklist_lock);
  75. spin_lock_irq(&tsk->sighand->siglock);
  76. cval = tsk->signal->it_prof_expires;
  77. cinterval = tsk->signal->it_prof_incr;
  78. if (!cputime_eq(cval, cputime_zero)) {
  79. struct task_struct *t = tsk;
  80. cputime_t ptime = cputime_add(tsk->signal->utime,
  81. tsk->signal->stime);
  82. do {
  83. ptime = cputime_add(ptime,
  84. cputime_add(t->utime,
  85. t->stime));
  86. t = next_thread(t);
  87. } while (t != tsk);
  88. if (cputime_le(cval, ptime)) { /* about to fire */
  89. cval = jiffies_to_cputime(1);
  90. } else {
  91. cval = cputime_sub(cval, ptime);
  92. }
  93. }
  94. spin_unlock_irq(&tsk->sighand->siglock);
  95. read_unlock(&tasklist_lock);
  96. cputime_to_timeval(cval, &value->it_value);
  97. cputime_to_timeval(cinterval, &value->it_interval);
  98. break;
  99. default:
  100. return(-EINVAL);
  101. }
  102. return 0;
  103. }
  104. asmlinkage long sys_getitimer(int which, struct itimerval __user *value)
  105. {
  106. int error = -EFAULT;
  107. struct itimerval get_buffer;
  108. if (value) {
  109. error = do_getitimer(which, &get_buffer);
  110. if (!error &&
  111. copy_to_user(value, &get_buffer, sizeof(get_buffer)))
  112. error = -EFAULT;
  113. }
  114. return error;
  115. }
  116. /*
  117. * The timer is automagically restarted, when interval != 0
  118. */
  119. int it_real_fn(void *data)
  120. {
  121. struct task_struct *tsk = (struct task_struct *) data;
  122. send_group_sig_info(SIGALRM, SEND_SIG_PRIV, tsk);
  123. if (tsk->signal->it_real_incr.tv64 != 0) {
  124. hrtimer_forward(&tsk->signal->real_timer,
  125. tsk->signal->it_real_incr);
  126. return HRTIMER_RESTART;
  127. }
  128. return HRTIMER_NORESTART;
  129. }
  130. int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue)
  131. {
  132. struct task_struct *tsk = current;
  133. struct hrtimer *timer;
  134. ktime_t expires;
  135. cputime_t cval, cinterval, nval, ninterval;
  136. switch (which) {
  137. case ITIMER_REAL:
  138. again:
  139. spin_lock_irq(&tsk->sighand->siglock);
  140. timer = &tsk->signal->real_timer;
  141. if (ovalue) {
  142. ovalue->it_value = itimer_get_remtime(timer);
  143. ovalue->it_interval
  144. = ktime_to_timeval(tsk->signal->it_real_incr);
  145. }
  146. /* We are sharing ->siglock with it_real_fn() */
  147. if (hrtimer_try_to_cancel(timer) < 0) {
  148. spin_unlock_irq(&tsk->sighand->siglock);
  149. goto again;
  150. }
  151. tsk->signal->it_real_incr =
  152. timeval_to_ktime(value->it_interval);
  153. expires = timeval_to_ktime(value->it_value);
  154. if (expires.tv64 != 0)
  155. hrtimer_start(timer, expires, HRTIMER_REL);
  156. spin_unlock_irq(&tsk->sighand->siglock);
  157. break;
  158. case ITIMER_VIRTUAL:
  159. nval = timeval_to_cputime(&value->it_value);
  160. ninterval = timeval_to_cputime(&value->it_interval);
  161. read_lock(&tasklist_lock);
  162. spin_lock_irq(&tsk->sighand->siglock);
  163. cval = tsk->signal->it_virt_expires;
  164. cinterval = tsk->signal->it_virt_incr;
  165. if (!cputime_eq(cval, cputime_zero) ||
  166. !cputime_eq(nval, cputime_zero)) {
  167. if (cputime_gt(nval, cputime_zero))
  168. nval = cputime_add(nval,
  169. jiffies_to_cputime(1));
  170. set_process_cpu_timer(tsk, CPUCLOCK_VIRT,
  171. &nval, &cval);
  172. }
  173. tsk->signal->it_virt_expires = nval;
  174. tsk->signal->it_virt_incr = ninterval;
  175. spin_unlock_irq(&tsk->sighand->siglock);
  176. read_unlock(&tasklist_lock);
  177. if (ovalue) {
  178. cputime_to_timeval(cval, &ovalue->it_value);
  179. cputime_to_timeval(cinterval, &ovalue->it_interval);
  180. }
  181. break;
  182. case ITIMER_PROF:
  183. nval = timeval_to_cputime(&value->it_value);
  184. ninterval = timeval_to_cputime(&value->it_interval);
  185. read_lock(&tasklist_lock);
  186. spin_lock_irq(&tsk->sighand->siglock);
  187. cval = tsk->signal->it_prof_expires;
  188. cinterval = tsk->signal->it_prof_incr;
  189. if (!cputime_eq(cval, cputime_zero) ||
  190. !cputime_eq(nval, cputime_zero)) {
  191. if (cputime_gt(nval, cputime_zero))
  192. nval = cputime_add(nval,
  193. jiffies_to_cputime(1));
  194. set_process_cpu_timer(tsk, CPUCLOCK_PROF,
  195. &nval, &cval);
  196. }
  197. tsk->signal->it_prof_expires = nval;
  198. tsk->signal->it_prof_incr = ninterval;
  199. spin_unlock_irq(&tsk->sighand->siglock);
  200. read_unlock(&tasklist_lock);
  201. if (ovalue) {
  202. cputime_to_timeval(cval, &ovalue->it_value);
  203. cputime_to_timeval(cinterval, &ovalue->it_interval);
  204. }
  205. break;
  206. default:
  207. return -EINVAL;
  208. }
  209. return 0;
  210. }
  211. asmlinkage long sys_setitimer(int which,
  212. struct itimerval __user *value,
  213. struct itimerval __user *ovalue)
  214. {
  215. struct itimerval set_buffer, get_buffer;
  216. int error;
  217. if (value) {
  218. if(copy_from_user(&set_buffer, value, sizeof(set_buffer)))
  219. return -EFAULT;
  220. } else
  221. memset((char *) &set_buffer, 0, sizeof(set_buffer));
  222. error = do_setitimer(which, &set_buffer, ovalue ? &get_buffer : NULL);
  223. if (error || !ovalue)
  224. return error;
  225. if (copy_to_user(ovalue, &get_buffer, sizeof(get_buffer)))
  226. return -EFAULT;
  227. return 0;
  228. }