itimer.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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, true);
  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 (cval) {
  47. struct task_cputime cputime;
  48. cputime_t t;
  49. thread_group_cputimer(tsk, &cputime);
  50. if (clock_id == CPUCLOCK_PROF)
  51. t = cputime.utime + cputime.stime;
  52. else
  53. /* CPUCLOCK_VIRT */
  54. t = cputime.utime;
  55. if (cval < t)
  56. /* about to fire */
  57. cval = cputime_one_jiffy;
  58. else
  59. cval = 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. u32 error, incr_error;
  125. struct cpu_itimer *it = &tsk->signal->it[clock_id];
  126. nval = timeval_to_cputime(&value->it_value);
  127. ns_nval = timeval_to_ns(&value->it_value);
  128. ninterval = timeval_to_cputime(&value->it_interval);
  129. ns_ninterval = timeval_to_ns(&value->it_interval);
  130. error = cputime_sub_ns(nval, ns_nval);
  131. incr_error = cputime_sub_ns(ninterval, ns_ninterval);
  132. spin_lock_irq(&tsk->sighand->siglock);
  133. cval = it->expires;
  134. cinterval = it->incr;
  135. if (cval || nval) {
  136. if (nval > 0)
  137. nval += cputime_one_jiffy;
  138. set_process_cpu_timer(tsk, clock_id, &nval, &cval);
  139. }
  140. it->expires = nval;
  141. it->incr = ninterval;
  142. it->error = error;
  143. it->incr_error = incr_error;
  144. trace_itimer_state(clock_id == CPUCLOCK_VIRT ?
  145. ITIMER_VIRTUAL : ITIMER_PROF, value, nval);
  146. spin_unlock_irq(&tsk->sighand->siglock);
  147. if (ovalue) {
  148. cputime_to_timeval(cval, &ovalue->it_value);
  149. cputime_to_timeval(cinterval, &ovalue->it_interval);
  150. }
  151. }
  152. /*
  153. * Returns true if the timeval is in canonical form
  154. */
  155. #define timeval_valid(t) \
  156. (((t)->tv_sec >= 0) && (((unsigned long) (t)->tv_usec) < USEC_PER_SEC))
  157. int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue)
  158. {
  159. struct task_struct *tsk = current;
  160. struct hrtimer *timer;
  161. ktime_t expires;
  162. /*
  163. * Validate the timevals in value.
  164. */
  165. if (!timeval_valid(&value->it_value) ||
  166. !timeval_valid(&value->it_interval))
  167. return -EINVAL;
  168. switch (which) {
  169. case ITIMER_REAL:
  170. again:
  171. spin_lock_irq(&tsk->sighand->siglock);
  172. timer = &tsk->signal->real_timer;
  173. if (ovalue) {
  174. ovalue->it_value = itimer_get_remtime(timer);
  175. ovalue->it_interval
  176. = ktime_to_timeval(tsk->signal->it_real_incr);
  177. }
  178. /* We are sharing ->siglock with it_real_fn() */
  179. if (hrtimer_try_to_cancel(timer) < 0) {
  180. spin_unlock_irq(&tsk->sighand->siglock);
  181. goto again;
  182. }
  183. expires = timeval_to_ktime(value->it_value);
  184. if (expires.tv64 != 0) {
  185. tsk->signal->it_real_incr =
  186. timeval_to_ktime(value->it_interval);
  187. hrtimer_start(timer, expires, HRTIMER_MODE_REL);
  188. } else
  189. tsk->signal->it_real_incr.tv64 = 0;
  190. trace_itimer_state(ITIMER_REAL, value, 0);
  191. spin_unlock_irq(&tsk->sighand->siglock);
  192. break;
  193. case ITIMER_VIRTUAL:
  194. set_cpu_itimer(tsk, CPUCLOCK_VIRT, value, ovalue);
  195. break;
  196. case ITIMER_PROF:
  197. set_cpu_itimer(tsk, CPUCLOCK_PROF, value, ovalue);
  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(&set_buffer, 0, sizeof(set_buffer));
  246. printk_once(KERN_WARNING "%s calls setitimer() with new_value NULL pointer."
  247. " Misfeature support will be removed\n",
  248. current->comm);
  249. }
  250. error = do_setitimer(which, &set_buffer, ovalue ? &get_buffer : NULL);
  251. if (error || !ovalue)
  252. return error;
  253. if (copy_to_user(ovalue, &get_buffer, sizeof(get_buffer)))
  254. return -EFAULT;
  255. return 0;
  256. }