[PATCH] hrtimers: pass current time to hrtimer_forward()
[linux-3.10.git] / kernel / itimer.c
1 /*
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>
10 #include <linux/smp_lock.h>
11 #include <linux/interrupt.h>
12 #include <linux/syscalls.h>
13 #include <linux/time.h>
14 #include <linux/posix-timers.h>
15 #include <linux/hrtimer.h>
16
17 #include <asm/uaccess.h>
18
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);
30
31         /*
32          * Racy but safe: if the itimer expires after the above
33          * hrtimer_get_remtime() call but before this condition
34          * then we return 0 - which is correct.
35          */
36         if (hrtimer_active(timer)) {
37                 if (rem.tv64 <= 0)
38                         rem.tv64 = NSEC_PER_USEC;
39         } else
40                 rem.tv64 = 0;
41
42         return ktime_to_timeval(rem);
43 }
44
45 int do_getitimer(int which, struct itimerval *value)
46 {
47         struct task_struct *tsk = current;
48         cputime_t cinterval, cval;
49
50         switch (which) {
51         case ITIMER_REAL:
52                 spin_lock_irq(&tsk->sighand->siglock);
53                 value->it_value = itimer_get_remtime(&tsk->signal->real_timer);
54                 value->it_interval =
55                         ktime_to_timeval(tsk->signal->it_real_incr);
56                 spin_unlock_irq(&tsk->sighand->siglock);
57                 break;
58         case ITIMER_VIRTUAL:
59                 read_lock(&tasklist_lock);
60                 spin_lock_irq(&tsk->sighand->siglock);
61                 cval = tsk->signal->it_virt_expires;
62                 cinterval = tsk->signal->it_virt_incr;
63                 if (!cputime_eq(cval, cputime_zero)) {
64                         struct task_struct *t = tsk;
65                         cputime_t utime = tsk->signal->utime;
66                         do {
67                                 utime = cputime_add(utime, t->utime);
68                                 t = next_thread(t);
69                         } while (t != tsk);
70                         if (cputime_le(cval, utime)) { /* about to fire */
71                                 cval = jiffies_to_cputime(1);
72                         } else {
73                                 cval = cputime_sub(cval, utime);
74                         }
75                 }
76                 spin_unlock_irq(&tsk->sighand->siglock);
77                 read_unlock(&tasklist_lock);
78                 cputime_to_timeval(cval, &value->it_value);
79                 cputime_to_timeval(cinterval, &value->it_interval);
80                 break;
81         case ITIMER_PROF:
82                 read_lock(&tasklist_lock);
83                 spin_lock_irq(&tsk->sighand->siglock);
84                 cval = tsk->signal->it_prof_expires;
85                 cinterval = tsk->signal->it_prof_incr;
86                 if (!cputime_eq(cval, cputime_zero)) {
87                         struct task_struct *t = tsk;
88                         cputime_t ptime = cputime_add(tsk->signal->utime,
89                                                       tsk->signal->stime);
90                         do {
91                                 ptime = cputime_add(ptime,
92                                                     cputime_add(t->utime,
93                                                                 t->stime));
94                                 t = next_thread(t);
95                         } while (t != tsk);
96                         if (cputime_le(cval, ptime)) { /* about to fire */
97                                 cval = jiffies_to_cputime(1);
98                         } else {
99                                 cval = cputime_sub(cval, ptime);
100                         }
101                 }
102                 spin_unlock_irq(&tsk->sighand->siglock);
103                 read_unlock(&tasklist_lock);
104                 cputime_to_timeval(cval, &value->it_value);
105                 cputime_to_timeval(cinterval, &value->it_interval);
106                 break;
107         default:
108                 return(-EINVAL);
109         }
110         return 0;
111 }
112
113 asmlinkage long sys_getitimer(int which, struct itimerval __user *value)
114 {
115         int error = -EFAULT;
116         struct itimerval get_buffer;
117
118         if (value) {
119                 error = do_getitimer(which, &get_buffer);
120                 if (!error &&
121                     copy_to_user(value, &get_buffer, sizeof(get_buffer)))
122                         error = -EFAULT;
123         }
124         return error;
125 }
126
127
128 /*
129  * The timer is automagically restarted, when interval != 0
130  */
131 int it_real_fn(void *data)
132 {
133         struct task_struct *tsk = (struct task_struct *) data;
134
135         send_group_sig_info(SIGALRM, SEND_SIG_PRIV, tsk);
136
137         if (tsk->signal->it_real_incr.tv64 != 0) {
138                 hrtimer_forward(&tsk->signal->real_timer,
139                                 tsk->signal->real_timer.base->softirq_time,
140                                 tsk->signal->it_real_incr);
141
142                 return HRTIMER_RESTART;
143         }
144         return HRTIMER_NORESTART;
145 }
146
147 /*
148  * We do not care about correctness. We just sanitize the values so
149  * the ktime_t operations which expect normalized values do not
150  * break. This converts negative values to long timeouts similar to
151  * the code in kernel versions < 2.6.16
152  *
153  * Print a limited number of warning messages when an invalid timeval
154  * is detected.
155  */
156 static void fixup_timeval(struct timeval *tv, int interval)
157 {
158         static int warnlimit = 10;
159         unsigned long tmp;
160
161         if (warnlimit > 0) {
162                 warnlimit--;
163                 printk(KERN_WARNING
164                        "setitimer: %s (pid = %d) provided "
165                        "invalid timeval %s: tv_sec = %ld tv_usec = %ld\n",
166                        current->comm, current->pid,
167                        interval ? "it_interval" : "it_value",
168                        tv->tv_sec, (long) tv->tv_usec);
169         }
170
171         tmp = tv->tv_usec;
172         if (tmp >= USEC_PER_SEC) {
173                 tv->tv_usec = tmp % USEC_PER_SEC;
174                 tv->tv_sec += tmp / USEC_PER_SEC;
175         }
176
177         tmp = tv->tv_sec;
178         if (tmp > LONG_MAX)
179                 tv->tv_sec = LONG_MAX;
180 }
181
182 /*
183  * Returns true if the timeval is in canonical form
184  */
185 #define timeval_valid(t) \
186         (((t)->tv_sec >= 0) && (((unsigned long) (t)->tv_usec) < USEC_PER_SEC))
187
188 /*
189  * Check for invalid timevals, sanitize them and print a limited
190  * number of warnings.
191  */
192 static void check_itimerval(struct itimerval *value) {
193
194         if (unlikely(!timeval_valid(&value->it_value)))
195                 fixup_timeval(&value->it_value, 0);
196
197         if (unlikely(!timeval_valid(&value->it_interval)))
198                 fixup_timeval(&value->it_interval, 1);
199 }
200
201 int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue)
202 {
203         struct task_struct *tsk = current;
204         struct hrtimer *timer;
205         ktime_t expires;
206         cputime_t cval, cinterval, nval, ninterval;
207
208         /*
209          * Validate the timevals in value.
210          *
211          * Note: Although the spec requires that invalid values shall
212          * return -EINVAL, we just fixup the value and print a limited
213          * number of warnings in order not to break users of this
214          * historical misfeature.
215          *
216          * Scheduled for replacement in March 2007
217          */
218         check_itimerval(value);
219
220         switch (which) {
221         case ITIMER_REAL:
222 again:
223                 spin_lock_irq(&tsk->sighand->siglock);
224                 timer = &tsk->signal->real_timer;
225                 if (ovalue) {
226                         ovalue->it_value = itimer_get_remtime(timer);
227                         ovalue->it_interval
228                                 = ktime_to_timeval(tsk->signal->it_real_incr);
229                 }
230                 /* We are sharing ->siglock with it_real_fn() */
231                 if (hrtimer_try_to_cancel(timer) < 0) {
232                         spin_unlock_irq(&tsk->sighand->siglock);
233                         goto again;
234                 }
235                 tsk->signal->it_real_incr =
236                         timeval_to_ktime(value->it_interval);
237                 expires = timeval_to_ktime(value->it_value);
238                 if (expires.tv64 != 0)
239                         hrtimer_start(timer, expires, HRTIMER_REL);
240                 spin_unlock_irq(&tsk->sighand->siglock);
241                 break;
242         case ITIMER_VIRTUAL:
243                 nval = timeval_to_cputime(&value->it_value);
244                 ninterval = timeval_to_cputime(&value->it_interval);
245                 read_lock(&tasklist_lock);
246                 spin_lock_irq(&tsk->sighand->siglock);
247                 cval = tsk->signal->it_virt_expires;
248                 cinterval = tsk->signal->it_virt_incr;
249                 if (!cputime_eq(cval, cputime_zero) ||
250                     !cputime_eq(nval, cputime_zero)) {
251                         if (cputime_gt(nval, cputime_zero))
252                                 nval = cputime_add(nval,
253                                                    jiffies_to_cputime(1));
254                         set_process_cpu_timer(tsk, CPUCLOCK_VIRT,
255                                               &nval, &cval);
256                 }
257                 tsk->signal->it_virt_expires = nval;
258                 tsk->signal->it_virt_incr = ninterval;
259                 spin_unlock_irq(&tsk->sighand->siglock);
260                 read_unlock(&tasklist_lock);
261                 if (ovalue) {
262                         cputime_to_timeval(cval, &ovalue->it_value);
263                         cputime_to_timeval(cinterval, &ovalue->it_interval);
264                 }
265                 break;
266         case ITIMER_PROF:
267                 nval = timeval_to_cputime(&value->it_value);
268                 ninterval = timeval_to_cputime(&value->it_interval);
269                 read_lock(&tasklist_lock);
270                 spin_lock_irq(&tsk->sighand->siglock);
271                 cval = tsk->signal->it_prof_expires;
272                 cinterval = tsk->signal->it_prof_incr;
273                 if (!cputime_eq(cval, cputime_zero) ||
274                     !cputime_eq(nval, cputime_zero)) {
275                         if (cputime_gt(nval, cputime_zero))
276                                 nval = cputime_add(nval,
277                                                    jiffies_to_cputime(1));
278                         set_process_cpu_timer(tsk, CPUCLOCK_PROF,
279                                               &nval, &cval);
280                 }
281                 tsk->signal->it_prof_expires = nval;
282                 tsk->signal->it_prof_incr = ninterval;
283                 spin_unlock_irq(&tsk->sighand->siglock);
284                 read_unlock(&tasklist_lock);
285                 if (ovalue) {
286                         cputime_to_timeval(cval, &ovalue->it_value);
287                         cputime_to_timeval(cinterval, &ovalue->it_interval);
288                 }
289                 break;
290         default:
291                 return -EINVAL;
292         }
293         return 0;
294 }
295
296 /**
297  * alarm_setitimer - set alarm in seconds
298  *
299  * @seconds:    number of seconds until alarm
300  *              0 disables the alarm
301  *
302  * Returns the remaining time in seconds of a pending timer or 0 when
303  * the timer is not active.
304  *
305  * On 32 bit machines the seconds value is limited to (INT_MAX/2) to avoid
306  * negative timeval settings which would cause immediate expiry.
307  */
308 unsigned int alarm_setitimer(unsigned int seconds)
309 {
310         struct itimerval it_new, it_old;
311
312 #if BITS_PER_LONG < 64
313         if (seconds > INT_MAX)
314                 seconds = INT_MAX;
315 #endif
316         it_new.it_value.tv_sec = seconds;
317         it_new.it_value.tv_usec = 0;
318         it_new.it_interval.tv_sec = it_new.it_interval.tv_usec = 0;
319
320         do_setitimer(ITIMER_REAL, &it_new, &it_old);
321
322         /*
323          * We can't return 0 if we have an alarm pending ...  And we'd
324          * better return too much than too little anyway
325          */
326         if ((!it_old.it_value.tv_sec && it_old.it_value.tv_usec) ||
327               it_old.it_value.tv_usec >= 500000)
328                 it_old.it_value.tv_sec++;
329
330         return it_old.it_value.tv_sec;
331 }
332
333 asmlinkage long sys_setitimer(int which,
334                               struct itimerval __user *value,
335                               struct itimerval __user *ovalue)
336 {
337         struct itimerval set_buffer, get_buffer;
338         int error;
339
340         if (value) {
341                 if(copy_from_user(&set_buffer, value, sizeof(set_buffer)))
342                         return -EFAULT;
343         } else
344                 memset((char *) &set_buffer, 0, sizeof(set_buffer));
345
346         error = do_setitimer(which, &set_buffer, ovalue ? &get_buffer : NULL);
347         if (error || !ovalue)
348                 return error;
349
350         if (copy_to_user(ovalue, &get_buffer, sizeof(get_buffer)))
351                 return -EFAULT; 
352         return 0;
353 }