itimer.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. struct pid *leader_pid = sig->pids[PIDTYPE_TGID];
  122. trace_itimer_expire(ITIMER_REAL, leader_pid, 0);
  123. kill_pid_info(SIGALRM, SEND_SIG_PRIV, leader_pid);
  124. return HRTIMER_NORESTART;
  125. }
  126. static void set_cpu_itimer(struct task_struct *tsk, unsigned int clock_id,
  127. const struct itimerval *const value,
  128. struct itimerval *const ovalue)
  129. {
  130. u64 oval, nval, ointerval, ninterval;
  131. struct cpu_itimer *it = &tsk->signal->it[clock_id];
  132. /*
  133. * Use the to_ktime conversion because that clamps the maximum
  134. * value to KTIME_MAX and avoid multiplication overflows.
  135. */
  136. nval = ktime_to_ns(timeval_to_ktime(value->it_value));
  137. ninterval = ktime_to_ns(timeval_to_ktime(value->it_interval));
  138. spin_lock_irq(&tsk->sighand->siglock);
  139. oval = it->expires;
  140. ointerval = it->incr;
  141. if (oval || nval) {
  142. if (nval > 0)
  143. nval += TICK_NSEC;
  144. set_process_cpu_timer(tsk, clock_id, &nval, &oval);
  145. }
  146. it->expires = nval;
  147. it->incr = ninterval;
  148. trace_itimer_state(clock_id == CPUCLOCK_VIRT ?
  149. ITIMER_VIRTUAL : ITIMER_PROF, value, nval);
  150. spin_unlock_irq(&tsk->sighand->siglock);
  151. if (ovalue) {
  152. ovalue->it_value = ns_to_timeval(oval);
  153. ovalue->it_interval = ns_to_timeval(ointerval);
  154. }
  155. }
  156. /*
  157. * Returns true if the timeval is in canonical form
  158. */
  159. #define timeval_valid(t) \
  160. (((t)->tv_sec >= 0) && (((unsigned long) (t)->tv_usec) < USEC_PER_SEC))
  161. int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue)
  162. {
  163. struct task_struct *tsk = current;
  164. struct hrtimer *timer;
  165. ktime_t expires;
  166. /*
  167. * Validate the timevals in value.
  168. */
  169. if (!timeval_valid(&value->it_value) ||
  170. !timeval_valid(&value->it_interval))
  171. return -EINVAL;
  172. switch (which) {
  173. case ITIMER_REAL:
  174. again:
  175. spin_lock_irq(&tsk->sighand->siglock);
  176. timer = &tsk->signal->real_timer;
  177. if (ovalue) {
  178. ovalue->it_value = itimer_get_remtime(timer);
  179. ovalue->it_interval
  180. = ktime_to_timeval(tsk->signal->it_real_incr);
  181. }
  182. /* We are sharing ->siglock with it_real_fn() */
  183. if (hrtimer_try_to_cancel(timer) < 0) {
  184. spin_unlock_irq(&tsk->sighand->siglock);
  185. goto again;
  186. }
  187. expires = timeval_to_ktime(value->it_value);
  188. if (expires != 0) {
  189. tsk->signal->it_real_incr =
  190. timeval_to_ktime(value->it_interval);
  191. hrtimer_start(timer, expires, HRTIMER_MODE_REL);
  192. } else
  193. tsk->signal->it_real_incr = 0;
  194. trace_itimer_state(ITIMER_REAL, value, 0);
  195. spin_unlock_irq(&tsk->sighand->siglock);
  196. break;
  197. case ITIMER_VIRTUAL:
  198. set_cpu_itimer(tsk, CPUCLOCK_VIRT, value, ovalue);
  199. break;
  200. case ITIMER_PROF:
  201. set_cpu_itimer(tsk, CPUCLOCK_PROF, value, ovalue);
  202. break;
  203. default:
  204. return -EINVAL;
  205. }
  206. return 0;
  207. }
  208. #ifdef __ARCH_WANT_SYS_ALARM
  209. /**
  210. * alarm_setitimer - set alarm in seconds
  211. *
  212. * @seconds: number of seconds until alarm
  213. * 0 disables the alarm
  214. *
  215. * Returns the remaining time in seconds of a pending timer or 0 when
  216. * the timer is not active.
  217. *
  218. * On 32 bit machines the seconds value is limited to (INT_MAX/2) to avoid
  219. * negative timeval settings which would cause immediate expiry.
  220. */
  221. static unsigned int alarm_setitimer(unsigned int seconds)
  222. {
  223. struct itimerval it_new, it_old;
  224. #if BITS_PER_LONG < 64
  225. if (seconds > INT_MAX)
  226. seconds = INT_MAX;
  227. #endif
  228. it_new.it_value.tv_sec = seconds;
  229. it_new.it_value.tv_usec = 0;
  230. it_new.it_interval.tv_sec = it_new.it_interval.tv_usec = 0;
  231. do_setitimer(ITIMER_REAL, &it_new, &it_old);
  232. /*
  233. * We can't return 0 if we have an alarm pending ... And we'd
  234. * better return too much than too little anyway
  235. */
  236. if ((!it_old.it_value.tv_sec && it_old.it_value.tv_usec) ||
  237. it_old.it_value.tv_usec >= 500000)
  238. it_old.it_value.tv_sec++;
  239. return it_old.it_value.tv_sec;
  240. }
  241. /*
  242. * For backwards compatibility? This can be done in libc so Alpha
  243. * and all newer ports shouldn't need it.
  244. */
  245. SYSCALL_DEFINE1(alarm, unsigned int, seconds)
  246. {
  247. return alarm_setitimer(seconds);
  248. }
  249. #endif
  250. SYSCALL_DEFINE3(setitimer, int, which, struct itimerval __user *, value,
  251. struct itimerval __user *, ovalue)
  252. {
  253. struct itimerval set_buffer, get_buffer;
  254. int error;
  255. if (value) {
  256. if(copy_from_user(&set_buffer, value, sizeof(set_buffer)))
  257. return -EFAULT;
  258. } else {
  259. memset(&set_buffer, 0, sizeof(set_buffer));
  260. printk_once(KERN_WARNING "%s calls setitimer() with new_value NULL pointer."
  261. " Misfeature support will be removed\n",
  262. current->comm);
  263. }
  264. error = do_setitimer(which, &set_buffer, ovalue ? &get_buffer : NULL);
  265. if (error || !ovalue)
  266. return error;
  267. if (copy_to_user(ovalue, &get_buffer, sizeof(get_buffer)))
  268. return -EFAULT;
  269. return 0;
  270. }
  271. #ifdef CONFIG_COMPAT
  272. COMPAT_SYSCALL_DEFINE3(setitimer, int, which,
  273. struct compat_itimerval __user *, in,
  274. struct compat_itimerval __user *, out)
  275. {
  276. struct itimerval kin, kout;
  277. int error;
  278. if (in) {
  279. if (get_compat_itimerval(&kin, in))
  280. return -EFAULT;
  281. } else {
  282. memset(&kin, 0, sizeof(kin));
  283. }
  284. error = do_setitimer(which, &kin, out ? &kout : NULL);
  285. if (error || !out)
  286. return error;
  287. if (put_compat_itimerval(out, &kout))
  288. return -EFAULT;
  289. return 0;
  290. }
  291. #endif