itimer.c 7.2 KB

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