blob: 087d6a1279b833124d6359017687c4fff0cd88ca [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/itimer.c
3 *
4 * Copyright (C) 1992 Darren Senn
5 */
6
7/* These are all the functions necessary to implement itimers */
8
9#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/interrupt.h>
11#include <linux/syscalls.h>
12#include <linux/time.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010013#include <linux/sched/signal.h>
Ingo Molnar32ef5512017-02-05 11:48:36 +010014#include <linux/sched/cputime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/posix-timers.h>
Thomas Gleixner2ff678b2006-01-09 20:52:34 -080016#include <linux/hrtimer.h>
Xiao Guangrong3f0a5252009-08-10 10:52:30 +080017#include <trace/events/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080019#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Thomas Gleixner2ff678b2006-01-09 20:52:34 -080021/**
22 * itimer_get_remtime - get remaining time for the timer
23 *
24 * @timer: the timer to read
25 *
26 * Returns the delta between the expiry time and now, which can be
27 * less than zero or 1usec for an pending expired timer
28 */
29static struct timeval itimer_get_remtime(struct hrtimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
Thomas Gleixner51cbb522016-01-14 16:54:48 +000031 ktime_t rem = __hrtimer_get_remaining(timer, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Thomas Gleixner2ff678b2006-01-09 20:52:34 -080033 /*
34 * Racy but safe: if the itimer expires after the above
35 * hrtimer_get_remtime() call but before this condition
36 * then we return 0 - which is correct.
37 */
38 if (hrtimer_active(timer)) {
Thomas Gleixner2456e852016-12-25 11:38:40 +010039 if (rem <= 0)
40 rem = NSEC_PER_USEC;
Thomas Gleixner2ff678b2006-01-09 20:52:34 -080041 } else
Thomas Gleixner2456e852016-12-25 11:38:40 +010042 rem = 0;
Thomas Gleixner2ff678b2006-01-09 20:52:34 -080043
44 return ktime_to_timeval(rem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045}
46
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +020047static void get_cpu_itimer(struct task_struct *tsk, unsigned int clock_id,
Stanislaw Gruszka8356b5f2009-07-29 12:15:27 +020048 struct itimerval *const value)
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +020049{
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +010050 u64 val, interval;
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +020051 struct cpu_itimer *it = &tsk->signal->it[clock_id];
52
53 spin_lock_irq(&tsk->sighand->siglock);
54
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +010055 val = it->expires;
56 interval = it->incr;
57 if (val) {
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +010058 struct task_cputime cputime;
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +010059 u64 t;
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +020060
61 thread_group_cputimer(tsk, &cputime);
62 if (clock_id == CPUCLOCK_PROF)
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +010063 t = cputime.utime + cputime.stime;
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +020064 else
65 /* CPUCLOCK_VIRT */
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +010066 t = cputime.utime;
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +020067
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +010068 if (val < t)
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +020069 /* about to fire */
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +010070 val = TICK_NSEC;
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +020071 else
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +010072 val -= t;
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +020073 }
74
75 spin_unlock_irq(&tsk->sighand->siglock);
76
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +010077 value->it_value = ns_to_timeval(val);
78 value->it_interval = ns_to_timeval(interval);
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +020079}
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081int do_getitimer(int which, struct itimerval *value)
82{
83 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 switch (which) {
86 case ITIMER_REAL:
Thomas Gleixnerbc1978d2006-02-01 03:05:08 -080087 spin_lock_irq(&tsk->sighand->siglock);
Thomas Gleixner2ff678b2006-01-09 20:52:34 -080088 value->it_value = itimer_get_remtime(&tsk->signal->real_timer);
89 value->it_interval =
90 ktime_to_timeval(tsk->signal->it_real_incr);
Thomas Gleixnerbc1978d2006-02-01 03:05:08 -080091 spin_unlock_irq(&tsk->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 break;
93 case ITIMER_VIRTUAL:
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +020094 get_cpu_itimer(tsk, CPUCLOCK_VIRT, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 break;
96 case ITIMER_PROF:
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +020097 get_cpu_itimer(tsk, CPUCLOCK_PROF, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 break;
99 default:
100 return(-EINVAL);
101 }
102 return 0;
103}
104
Heiko Carstensb290ebe2009-01-14 14:14:06 +0100105SYSCALL_DEFINE2(getitimer, int, which, struct itimerval __user *, value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
107 int error = -EFAULT;
108 struct itimerval get_buffer;
109
110 if (value) {
111 error = do_getitimer(which, &get_buffer);
112 if (!error &&
113 copy_to_user(value, &get_buffer, sizeof(get_buffer)))
114 error = -EFAULT;
115 }
116 return error;
117}
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Thomas Gleixner2ff678b2006-01-09 20:52:34 -0800120/*
121 * The timer is automagically restarted, when interval != 0
122 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800123enum hrtimer_restart it_real_fn(struct hrtimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Roman Zippel05cfb612006-03-26 01:38:12 -0800125 struct signal_struct *sig =
Daniel Walker0719e372007-10-18 03:06:11 -0700126 container_of(timer, struct signal_struct, real_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Xiao Guangrong3f0a5252009-08-10 10:52:30 +0800128 trace_itimer_expire(ITIMER_REAL, sig->leader_pid, 0);
Oleg Nesterovfea9d172008-02-08 04:19:19 -0800129 kill_pid_info(SIGALRM, SEND_SIG_PRIV, sig->leader_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Thomas Gleixner2ff678b2006-01-09 20:52:34 -0800131 return HRTIMER_NORESTART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
Stanislaw Gruszka8356b5f2009-07-29 12:15:27 +0200134static void set_cpu_itimer(struct task_struct *tsk, unsigned int clock_id,
135 const struct itimerval *const value,
136 struct itimerval *const ovalue)
137{
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +0100138 u64 oval, nval, ointerval, ninterval;
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200139 struct cpu_itimer *it = &tsk->signal->it[clock_id];
140
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +0100141 nval = timeval_to_ns(&value->it_value);
142 ninterval = timeval_to_ns(&value->it_interval);
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200143
144 spin_lock_irq(&tsk->sighand->siglock);
145
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +0100146 oval = it->expires;
147 ointerval = it->incr;
148 if (oval || nval) {
Martin Schwidefsky64861632011-12-15 14:56:09 +0100149 if (nval > 0)
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +0100150 nval += TICK_NSEC;
151 set_process_cpu_timer(tsk, clock_id, &nval, &oval);
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200152 }
153 it->expires = nval;
154 it->incr = ninterval;
Xiao Guangrong3f0a5252009-08-10 10:52:30 +0800155 trace_itimer_state(clock_id == CPUCLOCK_VIRT ?
156 ITIMER_VIRTUAL : ITIMER_PROF, value, nval);
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200157
158 spin_unlock_irq(&tsk->sighand->siglock);
159
160 if (ovalue) {
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +0100161 ovalue->it_value = ns_to_timeval(oval);
162 ovalue->it_interval = ns_to_timeval(ointerval);
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200163 }
164}
165
Thomas Gleixner7d99b7d2006-03-25 03:06:35 -0800166/*
Thomas Gleixner7d99b7d2006-03-25 03:06:35 -0800167 * Returns true if the timeval is in canonical form
168 */
169#define timeval_valid(t) \
170 (((t)->tv_sec >= 0) && (((unsigned long) (t)->tv_usec) < USEC_PER_SEC))
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue)
173{
174 struct task_struct *tsk = current;
Thomas Gleixner2ff678b2006-01-09 20:52:34 -0800175 struct hrtimer *timer;
176 ktime_t expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Thomas Gleixner7d99b7d2006-03-25 03:06:35 -0800178 /*
179 * Validate the timevals in value.
Thomas Gleixner7d99b7d2006-03-25 03:06:35 -0800180 */
Adrian Bunk35bab752007-05-08 00:30:49 -0700181 if (!timeval_valid(&value->it_value) ||
182 !timeval_valid(&value->it_interval))
183 return -EINVAL;
Thomas Gleixner7d99b7d2006-03-25 03:06:35 -0800184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 switch (which) {
186 case ITIMER_REAL:
Thomas Gleixnerbc1978d2006-02-01 03:05:08 -0800187again:
188 spin_lock_irq(&tsk->sighand->siglock);
Thomas Gleixner2ff678b2006-01-09 20:52:34 -0800189 timer = &tsk->signal->real_timer;
Thomas Gleixner2ff678b2006-01-09 20:52:34 -0800190 if (ovalue) {
191 ovalue->it_value = itimer_get_remtime(timer);
192 ovalue->it_interval
193 = ktime_to_timeval(tsk->signal->it_real_incr);
Oleg Nesterovf01b1b02005-06-28 20:44:47 -0700194 }
Thomas Gleixnera16a1c02006-02-01 03:05:09 -0800195 /* We are sharing ->siglock with it_real_fn() */
196 if (hrtimer_try_to_cancel(timer) < 0) {
197 spin_unlock_irq(&tsk->sighand->siglock);
198 goto again;
199 }
Thomas Gleixner2ff678b2006-01-09 20:52:34 -0800200 expires = timeval_to_ktime(value->it_value);
Thomas Gleixner2456e852016-12-25 11:38:40 +0100201 if (expires != 0) {
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800202 tsk->signal->it_real_incr =
203 timeval_to_ktime(value->it_interval);
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800204 hrtimer_start(timer, expires, HRTIMER_MODE_REL);
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800205 } else
Thomas Gleixner2456e852016-12-25 11:38:40 +0100206 tsk->signal->it_real_incr = 0;
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800207
Xiao Guangrong3f0a5252009-08-10 10:52:30 +0800208 trace_itimer_state(ITIMER_REAL, value, 0);
Thomas Gleixnerbc1978d2006-02-01 03:05:08 -0800209 spin_unlock_irq(&tsk->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 break;
211 case ITIMER_VIRTUAL:
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200212 set_cpu_itimer(tsk, CPUCLOCK_VIRT, value, ovalue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 break;
214 case ITIMER_PROF:
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200215 set_cpu_itimer(tsk, CPUCLOCK_PROF, value, ovalue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 break;
217 default:
218 return -EINVAL;
219 }
220 return 0;
221}
222
Nicolas Pitre74ba1812016-11-11 00:10:08 -0500223#ifdef __ARCH_WANT_SYS_ALARM
224
Thomas Gleixnerc08b8a42006-03-25 03:06:33 -0800225/**
226 * alarm_setitimer - set alarm in seconds
227 *
228 * @seconds: number of seconds until alarm
229 * 0 disables the alarm
230 *
231 * Returns the remaining time in seconds of a pending timer or 0 when
232 * the timer is not active.
233 *
234 * On 32 bit machines the seconds value is limited to (INT_MAX/2) to avoid
235 * negative timeval settings which would cause immediate expiry.
236 */
Nicolas Pitre74ba1812016-11-11 00:10:08 -0500237static unsigned int alarm_setitimer(unsigned int seconds)
Thomas Gleixnerc08b8a42006-03-25 03:06:33 -0800238{
239 struct itimerval it_new, it_old;
240
241#if BITS_PER_LONG < 64
242 if (seconds > INT_MAX)
243 seconds = INT_MAX;
244#endif
245 it_new.it_value.tv_sec = seconds;
246 it_new.it_value.tv_usec = 0;
247 it_new.it_interval.tv_sec = it_new.it_interval.tv_usec = 0;
248
249 do_setitimer(ITIMER_REAL, &it_new, &it_old);
250
251 /*
252 * We can't return 0 if we have an alarm pending ... And we'd
253 * better return too much than too little anyway
254 */
255 if ((!it_old.it_value.tv_sec && it_old.it_value.tv_usec) ||
256 it_old.it_value.tv_usec >= 500000)
257 it_old.it_value.tv_sec++;
258
259 return it_old.it_value.tv_sec;
260}
261
Nicolas Pitre74ba1812016-11-11 00:10:08 -0500262/*
263 * For backwards compatibility? This can be done in libc so Alpha
264 * and all newer ports shouldn't need it.
265 */
266SYSCALL_DEFINE1(alarm, unsigned int, seconds)
267{
268 return alarm_setitimer(seconds);
269}
270
271#endif
272
Heiko Carstens362e9c02009-01-14 14:14:07 +0100273SYSCALL_DEFINE3(setitimer, int, which, struct itimerval __user *, value,
274 struct itimerval __user *, ovalue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 struct itimerval set_buffer, get_buffer;
277 int error;
278
279 if (value) {
280 if(copy_from_user(&set_buffer, value, sizeof(set_buffer)))
281 return -EFAULT;
Sasikantha babuaa2bf9b2012-03-21 20:10:54 +0530282 } else {
Thomas Gleixner9886f442012-04-10 10:50:55 +0200283 memset(&set_buffer, 0, sizeof(set_buffer));
284 printk_once(KERN_WARNING "%s calls setitimer() with new_value NULL pointer."
285 " Misfeature support will be removed\n",
286 current->comm);
Sasikantha babuaa2bf9b2012-03-21 20:10:54 +0530287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 error = do_setitimer(which, &set_buffer, ovalue ? &get_buffer : NULL);
290 if (error || !ovalue)
291 return error;
292
293 if (copy_to_user(ovalue, &get_buffer, sizeof(get_buffer)))
Daniel Walker0719e372007-10-18 03:06:11 -0700294 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return 0;
296}