itimer.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. nval = ktime_to_ns(timeval_to_ktime(value->it_value));
  132. ninterval = ktime_to_ns(timeval_to_ktime(value->it_interval));
  133. spin_lock_irq(&tsk->sighand->siglock);
  134. oval = it->expires;
  135. ointerval = it->incr;
  136. if (oval || nval) {
  137. if (nval > 0)
  138. nval += TICK_NSEC;
  139. set_process_cpu_timer(tsk, clock_id, &nval, &oval);
  140. }
  141. it->expires = nval;
  142. it->incr = ninterval;
  143. trace_itimer_state(clock_id == CPUCLOCK_VIRT ?
  144. ITIMER_VIRTUAL : ITIMER_PROF, value, nval);
  145. spin_unlock_irq(&tsk->sighand->siglock);
  146. if (ovalue) {
  147. ovalue->it_value = ns_to_timeval(oval);
  148. ovalue->it_interval = ns_to_timeval(ointerval);
  149. }
  150. }
  151. /*
  152. * Returns true if the timeval is in canonical form
  153. */
  154. #define timeval_valid(t) \
  155. (((t)->tv_sec >= 0) && (((unsigned long) (t)->tv_usec) < USEC_PER_SEC))
  156. int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue)
  157. {
  158. struct task_struct *tsk = current;
  159. struct hrtimer *timer;
  160. ktime_t expires;
  161. /*
  162. * Validate the timevals in value.
  163. */
  164. if (!timeval_valid(&value->it_value) ||
  165. !timeval_valid(&value->it_interval))
  166. return -EINVAL;
  167. switch (which) {
  168. case ITIMER_REAL:
  169. again:
  170. spin_lock_irq(&tsk->sighand->siglock);
  171. timer = &tsk->signal->real_timer;
  172. if (ovalue) {
  173. ovalue->it_value = itimer_get_remtime(timer);
  174. ovalue->it_interval
  175. = ktime_to_timeval(tsk->signal->it_real_incr);
  176. }
  177. /* We are sharing ->siglock with it_real_fn() */
  178. if (hrtimer_try_to_cancel(timer) < 0) {
  179. spin_unlock_irq(&tsk->sighand->siglock);
  180. goto again;
  181. }
  182. expires = timeval_to_ktime(value->it_value);
  183. if (expires != 0) {
  184. tsk->signal->it_real_incr =
  185. timeval_to_ktime(value->it_interval);
  186. hrtimer_start(timer, expires, HRTIMER_MODE_REL);
  187. } else
  188. tsk->signal->it_real_incr = 0;
  189. trace_itimer_state(ITIMER_REAL, value, 0);
  190. spin_unlock_irq(&tsk->sighand->siglock);
  191. break;
  192. case ITIMER_VIRTUAL:
  193. set_cpu_itimer(tsk, CPUCLOCK_VIRT, value, ovalue);
  194. break;
  195. case ITIMER_PROF:
  196. set_cpu_itimer(tsk, CPUCLOCK_PROF, value, ovalue);
  197. break;
  198. default:
  199. return -EINVAL;
  200. }
  201. return 0;
  202. }
  203. #ifdef __ARCH_WANT_SYS_ALARM
  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. static 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. /*
  237. * For backwards compatibility? This can be done in libc so Alpha
  238. * and all newer ports shouldn't need it.
  239. */
  240. SYSCALL_DEFINE1(alarm, unsigned int, seconds)
  241. {
  242. return alarm_setitimer(seconds);
  243. }
  244. #endif
  245. SYSCALL_DEFINE3(setitimer, int, which, struct itimerval __user *, value,
  246. struct itimerval __user *, ovalue)
  247. {
  248. struct itimerval set_buffer, get_buffer;
  249. int error;
  250. if (value) {
  251. if(copy_from_user(&set_buffer, value, sizeof(set_buffer)))
  252. return -EFAULT;
  253. } else {
  254. memset(&set_buffer, 0, sizeof(set_buffer));
  255. printk_once(KERN_WARNING "%s calls setitimer() with new_value NULL pointer."
  256. " Misfeature support will be removed\n",
  257. current->comm);
  258. }
  259. error = do_setitimer(which, &set_buffer, ovalue ? &get_buffer : NULL);
  260. if (error || !ovalue)
  261. return error;
  262. if (copy_to_user(ovalue, &get_buffer, sizeof(get_buffer)))
  263. return -EFAULT;
  264. return 0;
  265. }
  266. #ifdef CONFIG_COMPAT
  267. COMPAT_SYSCALL_DEFINE3(setitimer, int, which,
  268. struct compat_itimerval __user *, in,
  269. struct compat_itimerval __user *, out)
  270. {
  271. struct itimerval kin, kout;
  272. int error;
  273. if (in) {
  274. if (get_compat_itimerval(&kin, in))
  275. return -EFAULT;
  276. } else {
  277. memset(&kin, 0, sizeof(kin));
  278. }
  279. error = do_setitimer(which, &kin, out ? &kout : NULL);
  280. if (error || !out)
  281. return error;
  282. if (put_compat_itimerval(out, &kout))
  283. return -EFAULT;
  284. return 0;
  285. }
  286. #endif