itimer.c 10 KB

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