itimer.c 7.0 KB

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