itimer.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. static void get_cpu_itimer(struct task_struct *tsk, unsigned int clock_id,
  38. struct itimerval *value)
  39. {
  40. cputime_t cval, cinterval;
  41. struct cpu_itimer *it = &tsk->signal->it[clock_id];
  42. spin_lock_irq(&tsk->sighand->siglock);
  43. cval = it->expires;
  44. cinterval = it->incr;
  45. if (!cputime_eq(cval, cputime_zero)) {
  46. struct task_cputime cputime;
  47. cputime_t t;
  48. thread_group_cputimer(tsk, &cputime);
  49. if (clock_id == CPUCLOCK_PROF)
  50. t = cputime_add(cputime.utime, cputime.stime);
  51. else
  52. /* CPUCLOCK_VIRT */
  53. t = cputime.utime;
  54. if (cputime_le(cval, t))
  55. /* about to fire */
  56. cval = jiffies_to_cputime(1);
  57. else
  58. cval = cputime_sub(cval, t);
  59. }
  60. spin_unlock_irq(&tsk->sighand->siglock);
  61. cputime_to_timeval(cval, &value->it_value);
  62. cputime_to_timeval(cinterval, &value->it_interval);
  63. }
  64. int do_getitimer(int which, struct itimerval *value)
  65. {
  66. struct task_struct *tsk = current;
  67. switch (which) {
  68. case ITIMER_REAL:
  69. spin_lock_irq(&tsk->sighand->siglock);
  70. value->it_value = itimer_get_remtime(&tsk->signal->real_timer);
  71. value->it_interval =
  72. ktime_to_timeval(tsk->signal->it_real_incr);
  73. spin_unlock_irq(&tsk->sighand->siglock);
  74. break;
  75. case ITIMER_VIRTUAL:
  76. get_cpu_itimer(tsk, CPUCLOCK_VIRT, value);
  77. break;
  78. case ITIMER_PROF:
  79. get_cpu_itimer(tsk, CPUCLOCK_PROF, value);
  80. break;
  81. default:
  82. return(-EINVAL);
  83. }
  84. return 0;
  85. }
  86. SYSCALL_DEFINE2(getitimer, int, which, struct itimerval __user *, value)
  87. {
  88. int error = -EFAULT;
  89. struct itimerval get_buffer;
  90. if (value) {
  91. error = do_getitimer(which, &get_buffer);
  92. if (!error &&
  93. copy_to_user(value, &get_buffer, sizeof(get_buffer)))
  94. error = -EFAULT;
  95. }
  96. return error;
  97. }
  98. /*
  99. * The timer is automagically restarted, when interval != 0
  100. */
  101. enum hrtimer_restart it_real_fn(struct hrtimer *timer)
  102. {
  103. struct signal_struct *sig =
  104. container_of(timer, struct signal_struct, real_timer);
  105. kill_pid_info(SIGALRM, SEND_SIG_PRIV, sig->leader_pid);
  106. return HRTIMER_NORESTART;
  107. }
  108. static void set_cpu_itimer(struct task_struct *tsk, unsigned int clock_id,
  109. struct itimerval *value, struct itimerval *ovalue)
  110. {
  111. cputime_t cval, cinterval, nval, ninterval;
  112. struct cpu_itimer *it = &tsk->signal->it[clock_id];
  113. nval = timeval_to_cputime(&value->it_value);
  114. ninterval = timeval_to_cputime(&value->it_interval);
  115. spin_lock_irq(&tsk->sighand->siglock);
  116. cval = it->expires;
  117. cinterval = it->incr;
  118. if (!cputime_eq(cval, cputime_zero) ||
  119. !cputime_eq(nval, cputime_zero)) {
  120. if (cputime_gt(nval, cputime_zero))
  121. nval = cputime_add(nval, jiffies_to_cputime(1));
  122. set_process_cpu_timer(tsk, clock_id, &nval, &cval);
  123. }
  124. it->expires = nval;
  125. it->incr = ninterval;
  126. spin_unlock_irq(&tsk->sighand->siglock);
  127. if (ovalue) {
  128. cputime_to_timeval(cval, &ovalue->it_value);
  129. cputime_to_timeval(cinterval, &ovalue->it_interval);
  130. }
  131. }
  132. /*
  133. * Returns true if the timeval is in canonical form
  134. */
  135. #define timeval_valid(t) \
  136. (((t)->tv_sec >= 0) && (((unsigned long) (t)->tv_usec) < USEC_PER_SEC))
  137. int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue)
  138. {
  139. struct task_struct *tsk = current;
  140. struct hrtimer *timer;
  141. ktime_t expires;
  142. /*
  143. * Validate the timevals in value.
  144. */
  145. if (!timeval_valid(&value->it_value) ||
  146. !timeval_valid(&value->it_interval))
  147. return -EINVAL;
  148. switch (which) {
  149. case ITIMER_REAL:
  150. again:
  151. spin_lock_irq(&tsk->sighand->siglock);
  152. timer = &tsk->signal->real_timer;
  153. if (ovalue) {
  154. ovalue->it_value = itimer_get_remtime(timer);
  155. ovalue->it_interval
  156. = ktime_to_timeval(tsk->signal->it_real_incr);
  157. }
  158. /* We are sharing ->siglock with it_real_fn() */
  159. if (hrtimer_try_to_cancel(timer) < 0) {
  160. spin_unlock_irq(&tsk->sighand->siglock);
  161. goto again;
  162. }
  163. expires = timeval_to_ktime(value->it_value);
  164. if (expires.tv64 != 0) {
  165. tsk->signal->it_real_incr =
  166. timeval_to_ktime(value->it_interval);
  167. hrtimer_start(timer, expires, HRTIMER_MODE_REL);
  168. } else
  169. tsk->signal->it_real_incr.tv64 = 0;
  170. spin_unlock_irq(&tsk->sighand->siglock);
  171. break;
  172. case ITIMER_VIRTUAL:
  173. set_cpu_itimer(tsk, CPUCLOCK_VIRT, value, ovalue);
  174. break;
  175. case ITIMER_PROF:
  176. set_cpu_itimer(tsk, CPUCLOCK_PROF, value, ovalue);
  177. break;
  178. default:
  179. return -EINVAL;
  180. }
  181. return 0;
  182. }
  183. /**
  184. * alarm_setitimer - set alarm in seconds
  185. *
  186. * @seconds: number of seconds until alarm
  187. * 0 disables the alarm
  188. *
  189. * Returns the remaining time in seconds of a pending timer or 0 when
  190. * the timer is not active.
  191. *
  192. * On 32 bit machines the seconds value is limited to (INT_MAX/2) to avoid
  193. * negative timeval settings which would cause immediate expiry.
  194. */
  195. unsigned int alarm_setitimer(unsigned int seconds)
  196. {
  197. struct itimerval it_new, it_old;
  198. #if BITS_PER_LONG < 64
  199. if (seconds > INT_MAX)
  200. seconds = INT_MAX;
  201. #endif
  202. it_new.it_value.tv_sec = seconds;
  203. it_new.it_value.tv_usec = 0;
  204. it_new.it_interval.tv_sec = it_new.it_interval.tv_usec = 0;
  205. do_setitimer(ITIMER_REAL, &it_new, &it_old);
  206. /*
  207. * We can't return 0 if we have an alarm pending ... And we'd
  208. * better return too much than too little anyway
  209. */
  210. if ((!it_old.it_value.tv_sec && it_old.it_value.tv_usec) ||
  211. it_old.it_value.tv_usec >= 500000)
  212. it_old.it_value.tv_sec++;
  213. return it_old.it_value.tv_sec;
  214. }
  215. SYSCALL_DEFINE3(setitimer, int, which, struct itimerval __user *, value,
  216. struct itimerval __user *, ovalue)
  217. {
  218. struct itimerval set_buffer, get_buffer;
  219. int error;
  220. if (value) {
  221. if(copy_from_user(&set_buffer, value, sizeof(set_buffer)))
  222. return -EFAULT;
  223. } else
  224. memset((char *) &set_buffer, 0, sizeof(set_buffer));
  225. error = do_setitimer(which, &set_buffer, ovalue ? &get_buffer : NULL);
  226. if (error || !ovalue)
  227. return error;
  228. if (copy_to_user(ovalue, &get_buffer, sizeof(get_buffer)))
  229. return -EFAULT;
  230. return 0;
  231. }