itimer.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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/interrupt.h>
  9. #include <linux/syscalls.h>
  10. #include <linux/time.h>
  11. #include <linux/posix-timers.h>
  12. #include <linux/hrtimer.h>
  13. #include <asm/uaccess.h>
  14. /**
  15. * itimer_get_remtime - get remaining time for the timer
  16. *
  17. * @timer: the timer to read
  18. *
  19. * Returns the delta between the expiry time and now, which can be
  20. * less than zero or 1usec for an pending expired timer
  21. */
  22. static struct timeval itimer_get_remtime(struct hrtimer *timer)
  23. {
  24. ktime_t rem = hrtimer_get_remaining(timer);
  25. /*
  26. * Racy but safe: if the itimer expires after the above
  27. * hrtimer_get_remtime() call but before this condition
  28. * then we return 0 - which is correct.
  29. */
  30. if (hrtimer_active(timer)) {
  31. if (rem.tv64 <= 0)
  32. rem.tv64 = NSEC_PER_USEC;
  33. } else
  34. rem.tv64 = 0;
  35. return ktime_to_timeval(rem);
  36. }
  37. int do_getitimer(int which, struct itimerval *value)
  38. {
  39. struct task_struct *tsk = current;
  40. cputime_t cinterval, cval;
  41. switch (which) {
  42. case ITIMER_REAL:
  43. spin_lock_irq(&tsk->sighand->siglock);
  44. value->it_value = itimer_get_remtime(&tsk->signal->real_timer);
  45. value->it_interval =
  46. ktime_to_timeval(tsk->signal->it_real_incr);
  47. spin_unlock_irq(&tsk->sighand->siglock);
  48. break;
  49. case ITIMER_VIRTUAL:
  50. spin_lock_irq(&tsk->sighand->siglock);
  51. cval = tsk->signal->it_virt_expires;
  52. cinterval = tsk->signal->it_virt_incr;
  53. if (!cputime_eq(cval, cputime_zero)) {
  54. struct task_cputime cputime;
  55. cputime_t utime;
  56. thread_group_cputimer(tsk, &cputime);
  57. utime = cputime.utime;
  58. if (cputime_le(cval, utime)) { /* about to fire */
  59. cval = jiffies_to_cputime(1);
  60. } else {
  61. cval = cputime_sub(cval, utime);
  62. }
  63. }
  64. spin_unlock_irq(&tsk->sighand->siglock);
  65. cputime_to_timeval(cval, &value->it_value);
  66. cputime_to_timeval(cinterval, &value->it_interval);
  67. break;
  68. case ITIMER_PROF:
  69. spin_lock_irq(&tsk->sighand->siglock);
  70. cval = tsk->signal->it_prof_expires;
  71. cinterval = tsk->signal->it_prof_incr;
  72. if (!cputime_eq(cval, cputime_zero)) {
  73. struct task_cputime times;
  74. cputime_t ptime;
  75. thread_group_cputimer(tsk, &times);
  76. ptime = cputime_add(times.utime, times.stime);
  77. if (cputime_le(cval, ptime)) { /* about to fire */
  78. cval = jiffies_to_cputime(1);
  79. } else {
  80. cval = cputime_sub(cval, ptime);
  81. }
  82. }
  83. spin_unlock_irq(&tsk->sighand->siglock);
  84. cputime_to_timeval(cval, &value->it_value);
  85. cputime_to_timeval(cinterval, &value->it_interval);
  86. break;
  87. default:
  88. return(-EINVAL);
  89. }
  90. return 0;
  91. }
  92. SYSCALL_DEFINE2(getitimer, int, which, struct itimerval __user *, value)
  93. {
  94. int error = -EFAULT;
  95. struct itimerval get_buffer;
  96. if (value) {
  97. error = do_getitimer(which, &get_buffer);
  98. if (!error &&
  99. copy_to_user(value, &get_buffer, sizeof(get_buffer)))
  100. error = -EFAULT;
  101. }
  102. return error;
  103. }
  104. /*
  105. * The timer is automagically restarted, when interval != 0
  106. */
  107. enum hrtimer_restart it_real_fn(struct hrtimer *timer)
  108. {
  109. struct signal_struct *sig =
  110. container_of(timer, struct signal_struct, real_timer);
  111. kill_pid_info(SIGALRM, SEND_SIG_PRIV, sig->leader_pid);
  112. return HRTIMER_NORESTART;
  113. }
  114. /*
  115. * Returns true if the timeval is in canonical form
  116. */
  117. #define timeval_valid(t) \
  118. (((t)->tv_sec >= 0) && (((unsigned long) (t)->tv_usec) < USEC_PER_SEC))
  119. int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue)
  120. {
  121. struct task_struct *tsk = current;
  122. struct hrtimer *timer;
  123. ktime_t expires;
  124. cputime_t cval, cinterval, nval, ninterval;
  125. /*
  126. * Validate the timevals in value.
  127. */
  128. if (!timeval_valid(&value->it_value) ||
  129. !timeval_valid(&value->it_interval))
  130. return -EINVAL;
  131. switch (which) {
  132. case ITIMER_REAL:
  133. again:
  134. spin_lock_irq(&tsk->sighand->siglock);
  135. timer = &tsk->signal->real_timer;
  136. if (ovalue) {
  137. ovalue->it_value = itimer_get_remtime(timer);
  138. ovalue->it_interval
  139. = ktime_to_timeval(tsk->signal->it_real_incr);
  140. }
  141. /* We are sharing ->siglock with it_real_fn() */
  142. if (hrtimer_try_to_cancel(timer) < 0) {
  143. spin_unlock_irq(&tsk->sighand->siglock);
  144. goto again;
  145. }
  146. expires = timeval_to_ktime(value->it_value);
  147. if (expires.tv64 != 0) {
  148. tsk->signal->it_real_incr =
  149. timeval_to_ktime(value->it_interval);
  150. hrtimer_start(timer, expires, HRTIMER_MODE_REL);
  151. } else
  152. tsk->signal->it_real_incr.tv64 = 0;
  153. spin_unlock_irq(&tsk->sighand->siglock);
  154. break;
  155. case ITIMER_VIRTUAL:
  156. nval = timeval_to_cputime(&value->it_value);
  157. ninterval = timeval_to_cputime(&value->it_interval);
  158. spin_lock_irq(&tsk->sighand->siglock);
  159. cval = tsk->signal->it_virt_expires;
  160. cinterval = tsk->signal->it_virt_incr;
  161. if (!cputime_eq(cval, cputime_zero) ||
  162. !cputime_eq(nval, cputime_zero)) {
  163. if (cputime_gt(nval, cputime_zero))
  164. nval = cputime_add(nval,
  165. jiffies_to_cputime(1));
  166. set_process_cpu_timer(tsk, CPUCLOCK_VIRT,
  167. &nval, &cval);
  168. }
  169. tsk->signal->it_virt_expires = nval;
  170. tsk->signal->it_virt_incr = ninterval;
  171. spin_unlock_irq(&tsk->sighand->siglock);
  172. if (ovalue) {
  173. cputime_to_timeval(cval, &ovalue->it_value);
  174. cputime_to_timeval(cinterval, &ovalue->it_interval);
  175. }
  176. break;
  177. case ITIMER_PROF:
  178. nval = timeval_to_cputime(&value->it_value);
  179. ninterval = timeval_to_cputime(&value->it_interval);
  180. spin_lock_irq(&tsk->sighand->siglock);
  181. cval = tsk->signal->it_prof_expires;
  182. cinterval = tsk->signal->it_prof_incr;
  183. if (!cputime_eq(cval, cputime_zero) ||
  184. !cputime_eq(nval, cputime_zero)) {
  185. if (cputime_gt(nval, cputime_zero))
  186. nval = cputime_add(nval,
  187. jiffies_to_cputime(1));
  188. set_process_cpu_timer(tsk, CPUCLOCK_PROF,
  189. &nval, &cval);
  190. }
  191. tsk->signal->it_prof_expires = nval;
  192. tsk->signal->it_prof_incr = ninterval;
  193. spin_unlock_irq(&tsk->sighand->siglock);
  194. if (ovalue) {
  195. cputime_to_timeval(cval, &ovalue->it_value);
  196. cputime_to_timeval(cinterval, &ovalue->it_interval);
  197. }
  198. break;
  199. default:
  200. return -EINVAL;
  201. }
  202. return 0;
  203. }
  204. /**
  205. * alarm_setitimer - set alarm in seconds
  206. *
  207. * @seconds: number of seconds until alarm
  208. * 0 disables the alarm
  209. *
  210. * Returns the remaining time in seconds of a pending timer or 0 when
  211. * the timer is not active.
  212. *
  213. * On 32 bit machines the seconds value is limited to (INT_MAX/2) to avoid
  214. * negative timeval settings which would cause immediate expiry.
  215. */
  216. unsigned int alarm_setitimer(unsigned int seconds)
  217. {
  218. struct itimerval it_new, it_old;
  219. #if BITS_PER_LONG < 64
  220. if (seconds > INT_MAX)
  221. seconds = INT_MAX;
  222. #endif
  223. it_new.it_value.tv_sec = seconds;
  224. it_new.it_value.tv_usec = 0;
  225. it_new.it_interval.tv_sec = it_new.it_interval.tv_usec = 0;
  226. do_setitimer(ITIMER_REAL, &it_new, &it_old);
  227. /*
  228. * We can't return 0 if we have an alarm pending ... And we'd
  229. * better return too much than too little anyway
  230. */
  231. if ((!it_old.it_value.tv_sec && it_old.it_value.tv_usec) ||
  232. it_old.it_value.tv_usec >= 500000)
  233. it_old.it_value.tv_sec++;
  234. return it_old.it_value.tv_sec;
  235. }
  236. SYSCALL_DEFINE3(setitimer, int, which, struct itimerval __user *, value,
  237. struct itimerval __user *, ovalue)
  238. {
  239. struct itimerval set_buffer, get_buffer;
  240. int error;
  241. if (value) {
  242. if(copy_from_user(&set_buffer, value, sizeof(set_buffer)))
  243. return -EFAULT;
  244. } else
  245. memset((char *) &set_buffer, 0, sizeof(set_buffer));
  246. error = do_setitimer(which, &set_buffer, ovalue ? &get_buffer : NULL);
  247. if (error || !ovalue)
  248. return error;
  249. if (copy_to_user(ovalue, &get_buffer, sizeof(get_buffer)))
  250. return -EFAULT;
  251. return 0;
  252. }