Merge master.kernel.org:/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6] / 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 enum hrtimer_restart it_real_fn(struct hrtimer *timer)
132 {
133         struct signal_struct *sig =
134             container_of(timer, struct signal_struct, real_timer);
135
136         send_group_sig_info(SIGALRM, SEND_SIG_PRIV, sig->tsk);
137
138         return HRTIMER_NORESTART;
139 }
140
141 /*
142  * We do not care about correctness. We just sanitize the values so
143  * the ktime_t operations which expect normalized values do not
144  * break. This converts negative values to long timeouts similar to
145  * the code in kernel versions < 2.6.16
146  *
147  * Print a limited number of warning messages when an invalid timeval
148  * is detected.
149  */
150 static void fixup_timeval(struct timeval *tv, int interval)
151 {
152         static int warnlimit = 10;
153         unsigned long tmp;
154
155         if (warnlimit > 0) {
156                 warnlimit--;
157                 printk(KERN_WARNING
158                        "setitimer: %s (pid = %d) provided "
159                        "invalid timeval %s: tv_sec = %ld tv_usec = %ld\n",
160                        current->comm, current->pid,
161                        interval ? "it_interval" : "it_value",
162                        tv->tv_sec, (long) tv->tv_usec);
163         }
164
165         tmp = tv->tv_usec;
166         if (tmp >= USEC_PER_SEC) {
167                 tv->tv_usec = tmp % USEC_PER_SEC;
168                 tv->tv_sec += tmp / USEC_PER_SEC;
169         }
170
171         tmp = tv->tv_sec;
172         if (tmp > LONG_MAX)
173                 tv->tv_sec = LONG_MAX;
174 }
175
176 /*
177  * Returns true if the timeval is in canonical form
178  */
179 #define timeval_valid(t) \
180         (((t)->tv_sec >= 0) && (((unsigned long) (t)->tv_usec) < USEC_PER_SEC))
181
182 /*
183  * Check for invalid timevals, sanitize them and print a limited
184  * number of warnings.
185  */
186 static void check_itimerval(struct itimerval *value) {
187
188         if (unlikely(!timeval_valid(&value->it_value)))
189                 fixup_timeval(&value->it_value, 0);
190
191         if (unlikely(!timeval_valid(&value->it_interval)))
192                 fixup_timeval(&value->it_interval, 1);
193 }
194
195 int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue)
196 {
197         struct task_struct *tsk = current;
198         struct hrtimer *timer;
199         ktime_t expires;
200         cputime_t cval, cinterval, nval, ninterval;
201
202         /*
203          * Validate the timevals in value.
204          *
205          * Note: Although the spec requires that invalid values shall
206          * return -EINVAL, we just fixup the value and print a limited
207          * number of warnings in order not to break users of this
208          * historical misfeature.
209          *
210          * Scheduled for replacement in March 2007
211          */
212         check_itimerval(value);
213
214         switch (which) {
215         case ITIMER_REAL:
216 again:
217                 spin_lock_irq(&tsk->sighand->siglock);
218                 timer = &tsk->signal->real_timer;
219                 if (ovalue) {
220                         ovalue->it_value = itimer_get_remtime(timer);
221                         ovalue->it_interval
222                                 = ktime_to_timeval(tsk->signal->it_real_incr);
223                 }
224                 /* We are sharing ->siglock with it_real_fn() */
225                 if (hrtimer_try_to_cancel(timer) < 0) {
226                         spin_unlock_irq(&tsk->sighand->siglock);
227                         goto again;
228                 }
229                 expires = timeval_to_ktime(value->it_value);
230                 if (expires.tv64 != 0) {
231                         tsk->signal->it_real_incr =
232                                 timeval_to_ktime(value->it_interval);
233                         hrtimer_start(timer, expires, HRTIMER_MODE_REL);
234                 } else
235                         tsk->signal->it_real_incr.tv64 = 0;
236
237                 spin_unlock_irq(&tsk->sighand->siglock);
238                 break;
239         case ITIMER_VIRTUAL:
240                 nval = timeval_to_cputime(&value->it_value);
241                 ninterval = timeval_to_cputime(&value->it_interval);
242                 read_lock(&tasklist_lock);
243                 spin_lock_irq(&tsk->sighand->siglock);
244                 cval = tsk->signal->it_virt_expires;
245                 cinterval = tsk->signal->it_virt_incr;
246                 if (!cputime_eq(cval, cputime_zero) ||
247                     !cputime_eq(nval, cputime_zero)) {
248                         if (cputime_gt(nval, cputime_zero))
249                                 nval = cputime_add(nval,
250                                                    jiffies_to_cputime(1));
251                         set_process_cpu_timer(tsk, CPUCLOCK_VIRT,
252                                               &nval, &cval);
253                 }
254                 tsk->signal->it_virt_expires = nval;
255                 tsk->signal->it_virt_incr = ninterval;
256                 spin_unlock_irq(&tsk->sighand->siglock);
257                 read_unlock(&tasklist_lock);
258                 if (ovalue) {
259                         cputime_to_timeval(cval, &ovalue->it_value);
260                         cputime_to_timeval(cinterval, &ovalue->it_interval);
261                 }
262                 break;
263         case ITIMER_PROF:
264                 nval = timeval_to_cputime(&value->it_value);
265                 ninterval = timeval_to_cputime(&value->it_interval);
266                 read_lock(&tasklist_lock);
267                 spin_lock_irq(&tsk->sighand->siglock);
268                 cval = tsk->signal->it_prof_expires;
269                 cinterval = tsk->signal->it_prof_incr;
270                 if (!cputime_eq(cval, cputime_zero) ||
271                     !cputime_eq(nval, cputime_zero)) {
272                         if (cputime_gt(nval, cputime_zero))
273                                 nval = cputime_add(nval,
274                                                    jiffies_to_cputime(1));
275                         set_process_cpu_timer(tsk, CPUCLOCK_PROF,
276                                               &nval, &cval);
277                 }
278                 tsk->signal->it_prof_expires = nval;
279                 tsk->signal->it_prof_incr = ninterval;
280                 spin_unlock_irq(&tsk->sighand->siglock);
281                 read_unlock(&tasklist_lock);
282                 if (ovalue) {
283                         cputime_to_timeval(cval, &ovalue->it_value);
284                         cputime_to_timeval(cinterval, &ovalue->it_interval);
285                 }
286                 break;
287         default:
288                 return -EINVAL;
289         }
290         return 0;
291 }
292
293 /**
294  * alarm_setitimer - set alarm in seconds
295  *
296  * @seconds:    number of seconds until alarm
297  *              0 disables the alarm
298  *
299  * Returns the remaining time in seconds of a pending timer or 0 when
300  * the timer is not active.
301  *
302  * On 32 bit machines the seconds value is limited to (INT_MAX/2) to avoid
303  * negative timeval settings which would cause immediate expiry.
304  */
305 unsigned int alarm_setitimer(unsigned int seconds)
306 {
307         struct itimerval it_new, it_old;
308
309 #if BITS_PER_LONG < 64
310         if (seconds > INT_MAX)
311                 seconds = INT_MAX;
312 #endif
313         it_new.it_value.tv_sec = seconds;
314         it_new.it_value.tv_usec = 0;
315         it_new.it_interval.tv_sec = it_new.it_interval.tv_usec = 0;
316
317         do_setitimer(ITIMER_REAL, &it_new, &it_old);
318
319         /*
320          * We can't return 0 if we have an alarm pending ...  And we'd
321          * better return too much than too little anyway
322          */
323         if ((!it_old.it_value.tv_sec && it_old.it_value.tv_usec) ||
324               it_old.it_value.tv_usec >= 500000)
325                 it_old.it_value.tv_sec++;
326
327         return it_old.it_value.tv_sec;
328 }
329
330 asmlinkage long sys_setitimer(int which,
331                               struct itimerval __user *value,
332                               struct itimerval __user *ovalue)
333 {
334         struct itimerval set_buffer, get_buffer;
335         int error;
336
337         if (value) {
338                 if(copy_from_user(&set_buffer, value, sizeof(set_buffer)))
339                         return -EFAULT;
340         } else
341                 memset((char *) &set_buffer, 0, sizeof(set_buffer));
342
343         error = do_setitimer(which, &set_buffer, ovalue ? &get_buffer : NULL);
344         if (error || !ovalue)
345                 return error;
346
347         if (copy_to_user(ovalue, &get_buffer, sizeof(get_buffer)))
348                 return -EFAULT; 
349         return 0;
350 }