itimer.c 7.9 KB

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