2 * linux/kernel/itimer.c
4 * Copyright (C) 1992 Darren Senn
7 /* These are all the functions necessary to implement itimers */
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>
17 #include <asm/uaccess.h>
20 * itimer_get_remtime - get remaining time for the timer
22 * @timer: the timer to read
24 * Returns the delta between the expiry time and now, which can be
25 * less than zero or 1usec for an pending expired timer
27 static struct timeval
itimer_get_remtime(struct hrtimer
*timer
)
29 ktime_t rem
= hrtimer_get_remaining(timer
);
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.
36 if (hrtimer_active(timer
)) {
38 rem
.tv64
= NSEC_PER_USEC
;
42 return ktime_to_timeval(rem
);
45 int do_getitimer(int which
, struct itimerval
*value
)
47 struct task_struct
*tsk
= current
;
48 cputime_t cinterval
, cval
;
52 spin_lock_irq(&tsk
->sighand
->siglock
);
53 value
->it_value
= itimer_get_remtime(&tsk
->signal
->real_timer
);
55 ktime_to_timeval(tsk
->signal
->it_real_incr
);
56 spin_unlock_irq(&tsk
->sighand
->siglock
);
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
;
67 utime
= cputime_add(utime
, t
->utime
);
70 if (cputime_le(cval
, utime
)) { /* about to fire */
71 cval
= jiffies_to_cputime(1);
73 cval
= cputime_sub(cval
, utime
);
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
);
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
,
91 ptime
= cputime_add(ptime
,
96 if (cputime_le(cval
, ptime
)) { /* about to fire */
97 cval
= jiffies_to_cputime(1);
99 cval
= cputime_sub(cval
, ptime
);
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
);
113 asmlinkage
long sys_getitimer(int which
, struct itimerval __user
*value
)
116 struct itimerval get_buffer
;
119 error
= do_getitimer(which
, &get_buffer
);
121 copy_to_user(value
, &get_buffer
, sizeof(get_buffer
)))
129 * The timer is automagically restarted, when interval != 0
131 int it_real_fn(void *data
)
133 struct task_struct
*tsk
= (struct task_struct
*) data
;
135 send_group_sig_info(SIGALRM
, SEND_SIG_PRIV
, tsk
);
137 if (tsk
->signal
->it_real_incr
.tv64
!= 0) {
138 hrtimer_forward(&tsk
->signal
->real_timer
,
139 tsk
->signal
->it_real_incr
);
141 return HRTIMER_RESTART
;
143 return HRTIMER_NORESTART
;
147 * We do not care about correctness. We just sanitize the values so
148 * the ktime_t operations which expect normalized values do not
149 * break. This converts negative values to long timeouts similar to
150 * the code in kernel versions < 2.6.16
152 * Print a limited number of warning messages when an invalid timeval
155 static void fixup_timeval(struct timeval
*tv
, int interval
)
157 static int warnlimit
= 10;
163 "setitimer: %s (pid = %d) provided "
164 "invalid timeval %s: tv_sec = %ld tv_usec = %ld\n",
165 current
->comm
, current
->pid
,
166 interval
? "it_interval" : "it_value",
167 tv
->tv_sec
, (long) tv
->tv_usec
);
171 if (tmp
>= USEC_PER_SEC
) {
172 tv
->tv_usec
= tmp
% USEC_PER_SEC
;
173 tv
->tv_sec
+= tmp
/ USEC_PER_SEC
;
178 tv
->tv_sec
= LONG_MAX
;
182 * Returns true if the timeval is in canonical form
184 #define timeval_valid(t) \
185 (((t)->tv_sec >= 0) && (((unsigned long) (t)->tv_usec) < USEC_PER_SEC))
188 * Check for invalid timevals, sanitize them and print a limited
189 * number of warnings.
191 static void check_itimerval(struct itimerval
*value
) {
193 if (unlikely(!timeval_valid(&value
->it_value
)))
194 fixup_timeval(&value
->it_value
, 0);
196 if (unlikely(!timeval_valid(&value
->it_interval
)))
197 fixup_timeval(&value
->it_interval
, 1);
200 int do_setitimer(int which
, struct itimerval
*value
, struct itimerval
*ovalue
)
202 struct task_struct
*tsk
= current
;
203 struct hrtimer
*timer
;
205 cputime_t cval
, cinterval
, nval
, ninterval
;
208 * Validate the timevals in value.
210 * Note: Although the spec requires that invalid values shall
211 * return -EINVAL, we just fixup the value and print a limited
212 * number of warnings in order not to break users of this
213 * historical misfeature.
215 * Scheduled for replacement in March 2007
217 check_itimerval(value
);
222 spin_lock_irq(&tsk
->sighand
->siglock
);
223 timer
= &tsk
->signal
->real_timer
;
225 ovalue
->it_value
= itimer_get_remtime(timer
);
227 = ktime_to_timeval(tsk
->signal
->it_real_incr
);
229 /* We are sharing ->siglock with it_real_fn() */
230 if (hrtimer_try_to_cancel(timer
) < 0) {
231 spin_unlock_irq(&tsk
->sighand
->siglock
);
234 tsk
->signal
->it_real_incr
=
235 timeval_to_ktime(value
->it_interval
);
236 expires
= timeval_to_ktime(value
->it_value
);
237 if (expires
.tv64
!= 0)
238 hrtimer_start(timer
, expires
, HRTIMER_REL
);
239 spin_unlock_irq(&tsk
->sighand
->siglock
);
242 nval
= timeval_to_cputime(&value
->it_value
);
243 ninterval
= timeval_to_cputime(&value
->it_interval
);
244 read_lock(&tasklist_lock
);
245 spin_lock_irq(&tsk
->sighand
->siglock
);
246 cval
= tsk
->signal
->it_virt_expires
;
247 cinterval
= tsk
->signal
->it_virt_incr
;
248 if (!cputime_eq(cval
, cputime_zero
) ||
249 !cputime_eq(nval
, cputime_zero
)) {
250 if (cputime_gt(nval
, cputime_zero
))
251 nval
= cputime_add(nval
,
252 jiffies_to_cputime(1));
253 set_process_cpu_timer(tsk
, CPUCLOCK_VIRT
,
256 tsk
->signal
->it_virt_expires
= nval
;
257 tsk
->signal
->it_virt_incr
= ninterval
;
258 spin_unlock_irq(&tsk
->sighand
->siglock
);
259 read_unlock(&tasklist_lock
);
261 cputime_to_timeval(cval
, &ovalue
->it_value
);
262 cputime_to_timeval(cinterval
, &ovalue
->it_interval
);
266 nval
= timeval_to_cputime(&value
->it_value
);
267 ninterval
= timeval_to_cputime(&value
->it_interval
);
268 read_lock(&tasklist_lock
);
269 spin_lock_irq(&tsk
->sighand
->siglock
);
270 cval
= tsk
->signal
->it_prof_expires
;
271 cinterval
= tsk
->signal
->it_prof_incr
;
272 if (!cputime_eq(cval
, cputime_zero
) ||
273 !cputime_eq(nval
, cputime_zero
)) {
274 if (cputime_gt(nval
, cputime_zero
))
275 nval
= cputime_add(nval
,
276 jiffies_to_cputime(1));
277 set_process_cpu_timer(tsk
, CPUCLOCK_PROF
,
280 tsk
->signal
->it_prof_expires
= nval
;
281 tsk
->signal
->it_prof_incr
= ninterval
;
282 spin_unlock_irq(&tsk
->sighand
->siglock
);
283 read_unlock(&tasklist_lock
);
285 cputime_to_timeval(cval
, &ovalue
->it_value
);
286 cputime_to_timeval(cinterval
, &ovalue
->it_interval
);
296 * alarm_setitimer - set alarm in seconds
298 * @seconds: number of seconds until alarm
299 * 0 disables the alarm
301 * Returns the remaining time in seconds of a pending timer or 0 when
302 * the timer is not active.
304 * On 32 bit machines the seconds value is limited to (INT_MAX/2) to avoid
305 * negative timeval settings which would cause immediate expiry.
307 unsigned int alarm_setitimer(unsigned int seconds
)
309 struct itimerval it_new
, it_old
;
311 #if BITS_PER_LONG < 64
312 if (seconds
> INT_MAX
)
315 it_new
.it_value
.tv_sec
= seconds
;
316 it_new
.it_value
.tv_usec
= 0;
317 it_new
.it_interval
.tv_sec
= it_new
.it_interval
.tv_usec
= 0;
319 do_setitimer(ITIMER_REAL
, &it_new
, &it_old
);
322 * We can't return 0 if we have an alarm pending ... And we'd
323 * better return too much than too little anyway
325 if ((!it_old
.it_value
.tv_sec
&& it_old
.it_value
.tv_usec
) ||
326 it_old
.it_value
.tv_usec
>= 500000)
327 it_old
.it_value
.tv_sec
++;
329 return it_old
.it_value
.tv_sec
;
332 asmlinkage
long sys_setitimer(int which
,
333 struct itimerval __user
*value
,
334 struct itimerval __user
*ovalue
)
336 struct itimerval set_buffer
, get_buffer
;
340 if(copy_from_user(&set_buffer
, value
, sizeof(set_buffer
)))
343 memset((char *) &set_buffer
, 0, sizeof(set_buffer
));
345 error
= do_setitimer(which
, &set_buffer
, ovalue
? &get_buffer
: NULL
);
346 if (error
|| !ovalue
)
349 if (copy_to_user(ovalue
, &get_buffer
, sizeof(get_buffer
)))