4 * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org>
7 * Thanks to Thomas Gleixner for code reviews and useful comments.
11 #include <linux/alarmtimer.h>
12 #include <linux/file.h>
13 #include <linux/poll.h>
14 #include <linux/init.h>
16 #include <linux/sched.h>
17 #include <linux/kernel.h>
18 #include <linux/slab.h>
19 #include <linux/list.h>
20 #include <linux/spinlock.h>
21 #include <linux/time.h>
22 #include <linux/hrtimer.h>
23 #include <linux/anon_inodes.h>
24 #include <linux/timerfd.h>
25 #include <linux/syscalls.h>
26 #include <linux/compat.h>
27 #include <linux/rcupdate.h>
36 wait_queue_head_t wqh
;
41 struct list_head clist
;
42 spinlock_t cancel_lock
;
46 static LIST_HEAD(cancel_list
);
47 static DEFINE_SPINLOCK(cancel_lock
);
49 static inline bool isalarm(struct timerfd_ctx
*ctx
)
51 return ctx
->clockid
== CLOCK_REALTIME_ALARM
||
52 ctx
->clockid
== CLOCK_BOOTTIME_ALARM
;
56 * This gets called when the timer event triggers. We set the "expired"
57 * flag, but we do not re-arm the timer (in case it's necessary,
58 * tintv.tv64 != 0) until the timer is accessed.
60 static void timerfd_triggered(struct timerfd_ctx
*ctx
)
64 spin_lock_irqsave(&ctx
->wqh
.lock
, flags
);
67 wake_up_locked(&ctx
->wqh
);
68 spin_unlock_irqrestore(&ctx
->wqh
.lock
, flags
);
71 static enum hrtimer_restart
timerfd_tmrproc(struct hrtimer
*htmr
)
73 struct timerfd_ctx
*ctx
= container_of(htmr
, struct timerfd_ctx
,
75 timerfd_triggered(ctx
);
76 return HRTIMER_NORESTART
;
79 static enum alarmtimer_restart
timerfd_alarmproc(struct alarm
*alarm
,
82 struct timerfd_ctx
*ctx
= container_of(alarm
, struct timerfd_ctx
,
84 timerfd_triggered(ctx
);
85 return ALARMTIMER_NORESTART
;
89 * Called when the clock was set to cancel the timers in the cancel
90 * list. This will wake up processes waiting on these timers. The
91 * wake-up requires ctx->ticks to be non zero, therefore we increment
92 * it before calling wake_up_locked().
94 void timerfd_clock_was_set(void)
96 ktime_t moffs
= ktime_get_monotonic_offset();
97 struct timerfd_ctx
*ctx
;
101 list_for_each_entry_rcu(ctx
, &cancel_list
, clist
) {
102 if (!ctx
->might_cancel
)
104 spin_lock_irqsave(&ctx
->wqh
.lock
, flags
);
105 if (ctx
->moffs
.tv64
!= moffs
.tv64
) {
106 ctx
->moffs
.tv64
= KTIME_MAX
;
108 wake_up_locked(&ctx
->wqh
);
110 spin_unlock_irqrestore(&ctx
->wqh
.lock
, flags
);
115 static void __timerfd_remove_cancel(struct timerfd_ctx
*ctx
)
117 if (ctx
->might_cancel
) {
118 ctx
->might_cancel
= false;
119 spin_lock(&cancel_lock
);
120 list_del_rcu(&ctx
->clist
);
121 spin_unlock(&cancel_lock
);
125 static void timerfd_remove_cancel(struct timerfd_ctx
*ctx
)
127 spin_lock(&ctx
->cancel_lock
);
128 __timerfd_remove_cancel(ctx
);
129 spin_unlock(&ctx
->cancel_lock
);
132 static bool timerfd_canceled(struct timerfd_ctx
*ctx
)
134 if (!ctx
->might_cancel
|| ctx
->moffs
.tv64
!= KTIME_MAX
)
136 ctx
->moffs
= ktime_get_monotonic_offset();
140 static void timerfd_setup_cancel(struct timerfd_ctx
*ctx
, int flags
)
142 spin_lock(&ctx
->cancel_lock
);
143 if ((ctx
->clockid
== CLOCK_REALTIME
||
144 ctx
->clockid
== CLOCK_REALTIME_ALARM
) &&
145 (flags
& TFD_TIMER_ABSTIME
) && (flags
& TFD_TIMER_CANCEL_ON_SET
)) {
146 if (!ctx
->might_cancel
) {
147 ctx
->might_cancel
= true;
148 spin_lock(&cancel_lock
);
149 list_add_rcu(&ctx
->clist
, &cancel_list
);
150 spin_unlock(&cancel_lock
);
153 __timerfd_remove_cancel(ctx
);
155 spin_unlock(&ctx
->cancel_lock
);
158 static ktime_t
timerfd_get_remaining(struct timerfd_ctx
*ctx
)
163 remaining
= alarm_expires_remaining(&ctx
->t
.alarm
);
165 remaining
= hrtimer_expires_remaining(&ctx
->t
.tmr
);
167 return remaining
.tv64
< 0 ? ktime_set(0, 0): remaining
;
170 static int timerfd_setup(struct timerfd_ctx
*ctx
, int flags
,
171 const struct itimerspec
*ktmr
)
173 enum hrtimer_mode htmode
;
175 int clockid
= ctx
->clockid
;
177 htmode
= (flags
& TFD_TIMER_ABSTIME
) ?
178 HRTIMER_MODE_ABS
: HRTIMER_MODE_REL
;
180 texp
= timespec_to_ktime(ktmr
->it_value
);
183 ctx
->tintv
= timespec_to_ktime(ktmr
->it_interval
);
186 alarm_init(&ctx
->t
.alarm
,
187 ctx
->clockid
== CLOCK_REALTIME_ALARM
?
188 ALARM_REALTIME
: ALARM_BOOTTIME
,
191 hrtimer_init(&ctx
->t
.tmr
, clockid
, htmode
);
192 hrtimer_set_expires(&ctx
->t
.tmr
, texp
);
193 ctx
->t
.tmr
.function
= timerfd_tmrproc
;
196 if (texp
.tv64
!= 0) {
198 if (flags
& TFD_TIMER_ABSTIME
)
199 alarm_start(&ctx
->t
.alarm
, texp
);
201 alarm_start_relative(&ctx
->t
.alarm
, texp
);
203 hrtimer_start(&ctx
->t
.tmr
, texp
, htmode
);
206 if (timerfd_canceled(ctx
))
212 static int timerfd_release(struct inode
*inode
, struct file
*file
)
214 struct timerfd_ctx
*ctx
= file
->private_data
;
216 timerfd_remove_cancel(ctx
);
219 alarm_cancel(&ctx
->t
.alarm
);
221 hrtimer_cancel(&ctx
->t
.tmr
);
226 static unsigned int timerfd_poll(struct file
*file
, poll_table
*wait
)
228 struct timerfd_ctx
*ctx
= file
->private_data
;
229 unsigned int events
= 0;
232 poll_wait(file
, &ctx
->wqh
, wait
);
234 spin_lock_irqsave(&ctx
->wqh
.lock
, flags
);
237 spin_unlock_irqrestore(&ctx
->wqh
.lock
, flags
);
242 static ssize_t
timerfd_read(struct file
*file
, char __user
*buf
, size_t count
,
245 struct timerfd_ctx
*ctx
= file
->private_data
;
249 if (count
< sizeof(ticks
))
251 spin_lock_irq(&ctx
->wqh
.lock
);
252 if (file
->f_flags
& O_NONBLOCK
)
255 res
= wait_event_interruptible_locked_irq(ctx
->wqh
, ctx
->ticks
);
258 * If clock has changed, we do not care about the
259 * ticks and we do not rearm the timer. Userspace must
262 if (timerfd_canceled(ctx
)) {
271 if (ctx
->expired
&& ctx
->tintv
.tv64
) {
273 * If tintv.tv64 != 0, this is a periodic timer that
274 * needs to be re-armed. We avoid doing it in the timer
275 * callback to avoid DoS attacks specifying a very
276 * short timer period.
279 ticks
+= alarm_forward_now(
280 &ctx
->t
.alarm
, ctx
->tintv
) - 1;
281 alarm_restart(&ctx
->t
.alarm
);
283 ticks
+= hrtimer_forward_now(&ctx
->t
.tmr
,
285 hrtimer_restart(&ctx
->t
.tmr
);
291 spin_unlock_irq(&ctx
->wqh
.lock
);
293 res
= put_user(ticks
, (u64 __user
*) buf
) ? -EFAULT
: sizeof(ticks
);
297 static const struct file_operations timerfd_fops
= {
298 .release
= timerfd_release
,
299 .poll
= timerfd_poll
,
300 .read
= timerfd_read
,
301 .llseek
= noop_llseek
,
304 static int timerfd_fget(int fd
, struct fd
*p
)
306 struct fd f
= fdget(fd
);
309 if (f
.file
->f_op
!= &timerfd_fops
) {
317 SYSCALL_DEFINE2(timerfd_create
, int, clockid
, int, flags
)
320 struct timerfd_ctx
*ctx
;
322 /* Check the TFD_* constants for consistency. */
323 BUILD_BUG_ON(TFD_CLOEXEC
!= O_CLOEXEC
);
324 BUILD_BUG_ON(TFD_NONBLOCK
!= O_NONBLOCK
);
326 if ((flags
& ~TFD_CREATE_FLAGS
) ||
327 (clockid
!= CLOCK_MONOTONIC
&&
328 clockid
!= CLOCK_REALTIME
&&
329 clockid
!= CLOCK_REALTIME_ALARM
&&
330 clockid
!= CLOCK_BOOTTIME
&&
331 clockid
!= CLOCK_BOOTTIME_ALARM
))
334 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
338 init_waitqueue_head(&ctx
->wqh
);
339 spin_lock_init(&ctx
->cancel_lock
);
340 ctx
->clockid
= clockid
;
343 alarm_init(&ctx
->t
.alarm
,
344 ctx
->clockid
== CLOCK_REALTIME_ALARM
?
345 ALARM_REALTIME
: ALARM_BOOTTIME
,
348 hrtimer_init(&ctx
->t
.tmr
, clockid
, HRTIMER_MODE_ABS
);
350 ctx
->moffs
= ktime_get_monotonic_offset();
352 ufd
= anon_inode_getfd("[timerfd]", &timerfd_fops
, ctx
,
353 O_RDWR
| (flags
& TFD_SHARED_FCNTL_FLAGS
));
360 static int do_timerfd_settime(int ufd
, int flags
,
361 const struct itimerspec
*new,
362 struct itimerspec
*old
)
365 struct timerfd_ctx
*ctx
;
368 if ((flags
& ~TFD_SETTIME_FLAGS
) ||
369 !timespec_valid(&new->it_value
) ||
370 !timespec_valid(&new->it_interval
))
373 ret
= timerfd_fget(ufd
, &f
);
376 ctx
= f
.file
->private_data
;
378 timerfd_setup_cancel(ctx
, flags
);
381 * We need to stop the existing timer before reprogramming
382 * it to the new values.
385 spin_lock_irq(&ctx
->wqh
.lock
);
388 if (alarm_try_to_cancel(&ctx
->t
.alarm
) >= 0)
391 if (hrtimer_try_to_cancel(&ctx
->t
.tmr
) >= 0)
394 spin_unlock_irq(&ctx
->wqh
.lock
);
399 * If the timer is expired and it's periodic, we need to advance it
400 * because the caller may want to know the previous expiration time.
401 * We do not update "ticks" and "expired" since the timer will be
402 * re-programmed again in the following timerfd_setup() call.
404 if (ctx
->expired
&& ctx
->tintv
.tv64
) {
406 alarm_forward_now(&ctx
->t
.alarm
, ctx
->tintv
);
408 hrtimer_forward_now(&ctx
->t
.tmr
, ctx
->tintv
);
411 old
->it_value
= ktime_to_timespec(timerfd_get_remaining(ctx
));
412 old
->it_interval
= ktime_to_timespec(ctx
->tintv
);
415 * Re-program the timer to the new value ...
417 ret
= timerfd_setup(ctx
, flags
, new);
419 spin_unlock_irq(&ctx
->wqh
.lock
);
424 static int do_timerfd_gettime(int ufd
, struct itimerspec
*t
)
427 struct timerfd_ctx
*ctx
;
428 int ret
= timerfd_fget(ufd
, &f
);
431 ctx
= f
.file
->private_data
;
433 spin_lock_irq(&ctx
->wqh
.lock
);
434 if (ctx
->expired
&& ctx
->tintv
.tv64
) {
440 &ctx
->t
.alarm
, ctx
->tintv
) - 1;
441 alarm_restart(&ctx
->t
.alarm
);
444 hrtimer_forward_now(&ctx
->t
.tmr
, ctx
->tintv
)
446 hrtimer_restart(&ctx
->t
.tmr
);
449 t
->it_value
= ktime_to_timespec(timerfd_get_remaining(ctx
));
450 t
->it_interval
= ktime_to_timespec(ctx
->tintv
);
451 spin_unlock_irq(&ctx
->wqh
.lock
);
456 SYSCALL_DEFINE4(timerfd_settime
, int, ufd
, int, flags
,
457 const struct itimerspec __user
*, utmr
,
458 struct itimerspec __user
*, otmr
)
460 struct itimerspec
new, old
;
463 if (copy_from_user(&new, utmr
, sizeof(new)))
465 ret
= do_timerfd_settime(ufd
, flags
, &new, &old
);
468 if (otmr
&& copy_to_user(otmr
, &old
, sizeof(old
)))
474 SYSCALL_DEFINE2(timerfd_gettime
, int, ufd
, struct itimerspec __user
*, otmr
)
476 struct itimerspec kotmr
;
477 int ret
= do_timerfd_gettime(ufd
, &kotmr
);
480 return copy_to_user(otmr
, &kotmr
, sizeof(kotmr
)) ? -EFAULT
: 0;
484 COMPAT_SYSCALL_DEFINE4(timerfd_settime
, int, ufd
, int, flags
,
485 const struct compat_itimerspec __user
*, utmr
,
486 struct compat_itimerspec __user
*, otmr
)
488 struct itimerspec
new, old
;
491 if (get_compat_itimerspec(&new, utmr
))
493 ret
= do_timerfd_settime(ufd
, flags
, &new, &old
);
496 if (otmr
&& put_compat_itimerspec(otmr
, &old
))
501 COMPAT_SYSCALL_DEFINE2(timerfd_gettime
, int, ufd
,
502 struct compat_itimerspec __user
*, otmr
)
504 struct itimerspec kotmr
;
505 int ret
= do_timerfd_gettime(ufd
, &kotmr
);
508 return put_compat_itimerspec(otmr
, &kotmr
) ? -EFAULT
: 0;