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
;
39 short unsigned expired
;
40 short unsigned settime_flags
; /* to show in fdinfo */
42 struct list_head clist
;
43 spinlock_t cancel_lock
;
47 static LIST_HEAD(cancel_list
);
48 static DEFINE_SPINLOCK(cancel_lock
);
50 static inline bool isalarm(struct timerfd_ctx
*ctx
)
52 return ctx
->clockid
== CLOCK_REALTIME_ALARM
||
53 ctx
->clockid
== CLOCK_BOOTTIME_ALARM
;
57 * This gets called when the timer event triggers. We set the "expired"
58 * flag, but we do not re-arm the timer (in case it's necessary,
59 * tintv.tv64 != 0) until the timer is accessed.
61 static void timerfd_triggered(struct timerfd_ctx
*ctx
)
65 spin_lock_irqsave(&ctx
->wqh
.lock
, flags
);
68 wake_up_locked(&ctx
->wqh
);
69 spin_unlock_irqrestore(&ctx
->wqh
.lock
, flags
);
72 static enum hrtimer_restart
timerfd_tmrproc(struct hrtimer
*htmr
)
74 struct timerfd_ctx
*ctx
= container_of(htmr
, struct timerfd_ctx
,
76 timerfd_triggered(ctx
);
77 return HRTIMER_NORESTART
;
80 static enum alarmtimer_restart
timerfd_alarmproc(struct alarm
*alarm
,
83 struct timerfd_ctx
*ctx
= container_of(alarm
, struct timerfd_ctx
,
85 timerfd_triggered(ctx
);
86 return ALARMTIMER_NORESTART
;
90 * Called when the clock was set to cancel the timers in the cancel
91 * list. This will wake up processes waiting on these timers. The
92 * wake-up requires ctx->ticks to be non zero, therefore we increment
93 * it before calling wake_up_locked().
95 void timerfd_clock_was_set(void)
97 ktime_t moffs
= ktime_mono_to_real((ktime_t
){ .tv64
= 0 });
98 struct timerfd_ctx
*ctx
;
102 list_for_each_entry_rcu(ctx
, &cancel_list
, clist
) {
103 if (!ctx
->might_cancel
)
105 spin_lock_irqsave(&ctx
->wqh
.lock
, flags
);
106 if (ctx
->moffs
.tv64
!= moffs
.tv64
) {
107 ctx
->moffs
.tv64
= KTIME_MAX
;
109 wake_up_locked(&ctx
->wqh
);
111 spin_unlock_irqrestore(&ctx
->wqh
.lock
, flags
);
116 static void __timerfd_remove_cancel(struct timerfd_ctx
*ctx
)
118 if (ctx
->might_cancel
) {
119 ctx
->might_cancel
= false;
120 spin_lock(&cancel_lock
);
121 list_del_rcu(&ctx
->clist
);
122 spin_unlock(&cancel_lock
);
126 static void timerfd_remove_cancel(struct timerfd_ctx
*ctx
)
128 spin_lock(&ctx
->cancel_lock
);
129 __timerfd_remove_cancel(ctx
);
130 spin_unlock(&ctx
->cancel_lock
);
133 static bool timerfd_canceled(struct timerfd_ctx
*ctx
)
135 if (!ctx
->might_cancel
|| ctx
->moffs
.tv64
!= KTIME_MAX
)
137 ctx
->moffs
= ktime_mono_to_real((ktime_t
){ .tv64
= 0 });
141 static void timerfd_setup_cancel(struct timerfd_ctx
*ctx
, int flags
)
143 spin_lock(&ctx
->cancel_lock
);
144 if ((ctx
->clockid
== CLOCK_REALTIME
||
145 ctx
->clockid
== CLOCK_REALTIME_ALARM
) &&
146 (flags
& TFD_TIMER_ABSTIME
) && (flags
& TFD_TIMER_CANCEL_ON_SET
)) {
147 if (!ctx
->might_cancel
) {
148 ctx
->might_cancel
= true;
149 spin_lock(&cancel_lock
);
150 list_add_rcu(&ctx
->clist
, &cancel_list
);
151 spin_unlock(&cancel_lock
);
154 __timerfd_remove_cancel(ctx
);
156 spin_unlock(&ctx
->cancel_lock
);
159 static ktime_t
timerfd_get_remaining(struct timerfd_ctx
*ctx
)
164 remaining
= alarm_expires_remaining(&ctx
->t
.alarm
);
166 remaining
= hrtimer_expires_remaining(&ctx
->t
.tmr
);
168 return remaining
.tv64
< 0 ? ktime_set(0, 0): remaining
;
171 static int timerfd_setup(struct timerfd_ctx
*ctx
, int flags
,
172 const struct itimerspec
*ktmr
)
174 enum hrtimer_mode htmode
;
176 int clockid
= ctx
->clockid
;
178 htmode
= (flags
& TFD_TIMER_ABSTIME
) ?
179 HRTIMER_MODE_ABS
: HRTIMER_MODE_REL
;
181 texp
= timespec_to_ktime(ktmr
->it_value
);
184 ctx
->tintv
= timespec_to_ktime(ktmr
->it_interval
);
187 alarm_init(&ctx
->t
.alarm
,
188 ctx
->clockid
== CLOCK_REALTIME_ALARM
?
189 ALARM_REALTIME
: ALARM_BOOTTIME
,
192 hrtimer_init(&ctx
->t
.tmr
, clockid
, htmode
);
193 hrtimer_set_expires(&ctx
->t
.tmr
, texp
);
194 ctx
->t
.tmr
.function
= timerfd_tmrproc
;
197 if (texp
.tv64
!= 0) {
199 if (flags
& TFD_TIMER_ABSTIME
)
200 alarm_start(&ctx
->t
.alarm
, texp
);
202 alarm_start_relative(&ctx
->t
.alarm
, texp
);
204 hrtimer_start(&ctx
->t
.tmr
, texp
, htmode
);
207 if (timerfd_canceled(ctx
))
211 ctx
->settime_flags
= flags
& TFD_SETTIME_FLAGS
;
215 static int timerfd_release(struct inode
*inode
, struct file
*file
)
217 struct timerfd_ctx
*ctx
= file
->private_data
;
219 timerfd_remove_cancel(ctx
);
222 alarm_cancel(&ctx
->t
.alarm
);
224 hrtimer_cancel(&ctx
->t
.tmr
);
229 static unsigned int timerfd_poll(struct file
*file
, poll_table
*wait
)
231 struct timerfd_ctx
*ctx
= file
->private_data
;
232 unsigned int events
= 0;
235 poll_wait(file
, &ctx
->wqh
, wait
);
237 spin_lock_irqsave(&ctx
->wqh
.lock
, flags
);
240 spin_unlock_irqrestore(&ctx
->wqh
.lock
, flags
);
245 static ssize_t
timerfd_read(struct file
*file
, char __user
*buf
, size_t count
,
248 struct timerfd_ctx
*ctx
= file
->private_data
;
252 if (count
< sizeof(ticks
))
254 spin_lock_irq(&ctx
->wqh
.lock
);
255 if (file
->f_flags
& O_NONBLOCK
)
258 res
= wait_event_interruptible_locked_irq(ctx
->wqh
, ctx
->ticks
);
261 * If clock has changed, we do not care about the
262 * ticks and we do not rearm the timer. Userspace must
265 if (timerfd_canceled(ctx
)) {
274 if (ctx
->expired
&& ctx
->tintv
.tv64
) {
276 * If tintv.tv64 != 0, this is a periodic timer that
277 * needs to be re-armed. We avoid doing it in the timer
278 * callback to avoid DoS attacks specifying a very
279 * short timer period.
282 ticks
+= alarm_forward_now(
283 &ctx
->t
.alarm
, ctx
->tintv
) - 1;
284 alarm_restart(&ctx
->t
.alarm
);
286 ticks
+= hrtimer_forward_now(&ctx
->t
.tmr
,
288 hrtimer_restart(&ctx
->t
.tmr
);
294 spin_unlock_irq(&ctx
->wqh
.lock
);
296 res
= put_user(ticks
, (u64 __user
*) buf
) ? -EFAULT
: sizeof(ticks
);
300 #ifdef CONFIG_PROC_FS
301 static int timerfd_show(struct seq_file
*m
, struct file
*file
)
303 struct timerfd_ctx
*ctx
= file
->private_data
;
306 spin_lock_irq(&ctx
->wqh
.lock
);
307 t
.it_value
= ktime_to_timespec(timerfd_get_remaining(ctx
));
308 t
.it_interval
= ktime_to_timespec(ctx
->tintv
);
309 spin_unlock_irq(&ctx
->wqh
.lock
);
314 "settime flags: 0%o\n"
315 "it_value: (%llu, %llu)\n"
316 "it_interval: (%llu, %llu)\n",
317 ctx
->clockid
, (unsigned long long)ctx
->ticks
,
319 (unsigned long long)t
.it_value
.tv_sec
,
320 (unsigned long long)t
.it_value
.tv_nsec
,
321 (unsigned long long)t
.it_interval
.tv_sec
,
322 (unsigned long long)t
.it_interval
.tv_nsec
);
325 #define timerfd_show NULL
328 #ifdef CONFIG_CHECKPOINT_RESTORE
329 static long timerfd_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
331 struct timerfd_ctx
*ctx
= file
->private_data
;
335 case TFD_IOC_SET_TICKS
: {
338 if (copy_from_user(&ticks
, (u64 __user
*)arg
, sizeof(ticks
)))
343 spin_lock_irq(&ctx
->wqh
.lock
);
344 if (!timerfd_canceled(ctx
)) {
346 wake_up_locked(&ctx
->wqh
);
349 spin_unlock_irq(&ctx
->wqh
.lock
);
360 #define timerfd_ioctl NULL
363 static const struct file_operations timerfd_fops
= {
364 .release
= timerfd_release
,
365 .poll
= timerfd_poll
,
366 .read
= timerfd_read
,
367 .llseek
= noop_llseek
,
368 .show_fdinfo
= timerfd_show
,
369 .unlocked_ioctl
= timerfd_ioctl
,
372 static int timerfd_fget(int fd
, struct fd
*p
)
374 struct fd f
= fdget(fd
);
377 if (f
.file
->f_op
!= &timerfd_fops
) {
385 SYSCALL_DEFINE2(timerfd_create
, int, clockid
, int, flags
)
388 struct timerfd_ctx
*ctx
;
390 /* Check the TFD_* constants for consistency. */
391 BUILD_BUG_ON(TFD_CLOEXEC
!= O_CLOEXEC
);
392 BUILD_BUG_ON(TFD_NONBLOCK
!= O_NONBLOCK
);
394 if ((flags
& ~TFD_CREATE_FLAGS
) ||
395 (clockid
!= CLOCK_MONOTONIC
&&
396 clockid
!= CLOCK_REALTIME
&&
397 clockid
!= CLOCK_REALTIME_ALARM
&&
398 clockid
!= CLOCK_BOOTTIME
&&
399 clockid
!= CLOCK_BOOTTIME_ALARM
))
402 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
406 init_waitqueue_head(&ctx
->wqh
);
407 spin_lock_init(&ctx
->cancel_lock
);
408 ctx
->clockid
= clockid
;
411 alarm_init(&ctx
->t
.alarm
,
412 ctx
->clockid
== CLOCK_REALTIME_ALARM
?
413 ALARM_REALTIME
: ALARM_BOOTTIME
,
416 hrtimer_init(&ctx
->t
.tmr
, clockid
, HRTIMER_MODE_ABS
);
418 ctx
->moffs
= ktime_mono_to_real((ktime_t
){ .tv64
= 0 });
420 ufd
= anon_inode_getfd("[timerfd]", &timerfd_fops
, ctx
,
421 O_RDWR
| (flags
& TFD_SHARED_FCNTL_FLAGS
));
428 static int do_timerfd_settime(int ufd
, int flags
,
429 const struct itimerspec
*new,
430 struct itimerspec
*old
)
433 struct timerfd_ctx
*ctx
;
436 if ((flags
& ~TFD_SETTIME_FLAGS
) ||
437 !timespec_valid(&new->it_value
) ||
438 !timespec_valid(&new->it_interval
))
441 ret
= timerfd_fget(ufd
, &f
);
444 ctx
= f
.file
->private_data
;
446 timerfd_setup_cancel(ctx
, flags
);
449 * We need to stop the existing timer before reprogramming
450 * it to the new values.
453 spin_lock_irq(&ctx
->wqh
.lock
);
456 if (alarm_try_to_cancel(&ctx
->t
.alarm
) >= 0)
459 if (hrtimer_try_to_cancel(&ctx
->t
.tmr
) >= 0)
462 spin_unlock_irq(&ctx
->wqh
.lock
);
467 * If the timer is expired and it's periodic, we need to advance it
468 * because the caller may want to know the previous expiration time.
469 * We do not update "ticks" and "expired" since the timer will be
470 * re-programmed again in the following timerfd_setup() call.
472 if (ctx
->expired
&& ctx
->tintv
.tv64
) {
474 alarm_forward_now(&ctx
->t
.alarm
, ctx
->tintv
);
476 hrtimer_forward_now(&ctx
->t
.tmr
, ctx
->tintv
);
479 old
->it_value
= ktime_to_timespec(timerfd_get_remaining(ctx
));
480 old
->it_interval
= ktime_to_timespec(ctx
->tintv
);
483 * Re-program the timer to the new value ...
485 ret
= timerfd_setup(ctx
, flags
, new);
487 spin_unlock_irq(&ctx
->wqh
.lock
);
492 static int do_timerfd_gettime(int ufd
, struct itimerspec
*t
)
495 struct timerfd_ctx
*ctx
;
496 int ret
= timerfd_fget(ufd
, &f
);
499 ctx
= f
.file
->private_data
;
501 spin_lock_irq(&ctx
->wqh
.lock
);
502 if (ctx
->expired
&& ctx
->tintv
.tv64
) {
508 &ctx
->t
.alarm
, ctx
->tintv
) - 1;
509 alarm_restart(&ctx
->t
.alarm
);
512 hrtimer_forward_now(&ctx
->t
.tmr
, ctx
->tintv
)
514 hrtimer_restart(&ctx
->t
.tmr
);
517 t
->it_value
= ktime_to_timespec(timerfd_get_remaining(ctx
));
518 t
->it_interval
= ktime_to_timespec(ctx
->tintv
);
519 spin_unlock_irq(&ctx
->wqh
.lock
);
524 SYSCALL_DEFINE4(timerfd_settime
, int, ufd
, int, flags
,
525 const struct itimerspec __user
*, utmr
,
526 struct itimerspec __user
*, otmr
)
528 struct itimerspec
new, old
;
531 if (copy_from_user(&new, utmr
, sizeof(new)))
533 ret
= do_timerfd_settime(ufd
, flags
, &new, &old
);
536 if (otmr
&& copy_to_user(otmr
, &old
, sizeof(old
)))
542 SYSCALL_DEFINE2(timerfd_gettime
, int, ufd
, struct itimerspec __user
*, otmr
)
544 struct itimerspec kotmr
;
545 int ret
= do_timerfd_gettime(ufd
, &kotmr
);
548 return copy_to_user(otmr
, &kotmr
, sizeof(kotmr
)) ? -EFAULT
: 0;
552 COMPAT_SYSCALL_DEFINE4(timerfd_settime
, int, ufd
, int, flags
,
553 const struct compat_itimerspec __user
*, utmr
,
554 struct compat_itimerspec __user
*, otmr
)
556 struct itimerspec
new, old
;
559 if (get_compat_itimerspec(&new, utmr
))
561 ret
= do_timerfd_settime(ufd
, flags
, &new, &old
);
564 if (otmr
&& put_compat_itimerspec(otmr
, &old
))
569 COMPAT_SYSCALL_DEFINE2(timerfd_gettime
, int, ufd
,
570 struct compat_itimerspec __user
*, otmr
)
572 struct itimerspec kotmr
;
573 int ret
= do_timerfd_gettime(ufd
, &kotmr
);
576 return put_compat_itimerspec(otmr
, &kotmr
) ? -EFAULT
: 0;