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