itimer.c 7.9 KB

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