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