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
;
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 != 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_mono_to_real(0);
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
!= moffs
) {
106 ctx
->moffs
= 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 bool timerfd_canceled(struct timerfd_ctx
*ctx
)
127 if (!ctx
->might_cancel
|| ctx
->moffs
!= KTIME_MAX
)
129 ctx
->moffs
= ktime_mono_to_real(0);
133 static void timerfd_setup_cancel(struct timerfd_ctx
*ctx
, int flags
)
135 if ((ctx
->clockid
== CLOCK_REALTIME
||
136 ctx
->clockid
== CLOCK_REALTIME_ALARM
) &&
137 (flags
& TFD_TIMER_ABSTIME
) && (flags
& TFD_TIMER_CANCEL_ON_SET
)) {
138 if (!ctx
->might_cancel
) {
139 ctx
->might_cancel
= true;
140 spin_lock(&cancel_lock
);
141 list_add_rcu(&ctx
->clist
, &cancel_list
);
142 spin_unlock(&cancel_lock
);
144 } else if (ctx
->might_cancel
) {
145 timerfd_remove_cancel(ctx
);
149 static ktime_t
timerfd_get_remaining(struct timerfd_ctx
*ctx
)
154 remaining
= alarm_expires_remaining(&ctx
->t
.alarm
);
156 remaining
= hrtimer_expires_remaining_adjusted(&ctx
->t
.tmr
);
158 return remaining
< 0 ? 0: remaining
;
161 static int timerfd_setup(struct timerfd_ctx
*ctx
, int flags
,
162 const struct itimerspec
*ktmr
)
164 enum hrtimer_mode htmode
;
166 int clockid
= ctx
->clockid
;
168 htmode
= (flags
& TFD_TIMER_ABSTIME
) ?
169 HRTIMER_MODE_ABS
: HRTIMER_MODE_REL
;
171 texp
= timespec_to_ktime(ktmr
->it_value
);
174 ctx
->tintv
= timespec_to_ktime(ktmr
->it_interval
);
177 alarm_init(&ctx
->t
.alarm
,
178 ctx
->clockid
== CLOCK_REALTIME_ALARM
?
179 ALARM_REALTIME
: ALARM_BOOTTIME
,
182 hrtimer_init(&ctx
->t
.tmr
, clockid
, htmode
);
183 hrtimer_set_expires(&ctx
->t
.tmr
, texp
);
184 ctx
->t
.tmr
.function
= timerfd_tmrproc
;
189 if (flags
& TFD_TIMER_ABSTIME
)
190 alarm_start(&ctx
->t
.alarm
, texp
);
192 alarm_start_relative(&ctx
->t
.alarm
, texp
);
194 hrtimer_start(&ctx
->t
.tmr
, texp
, htmode
);
197 if (timerfd_canceled(ctx
))
201 ctx
->settime_flags
= flags
& TFD_SETTIME_FLAGS
;
205 static int timerfd_release(struct inode
*inode
, struct file
*file
)
207 struct timerfd_ctx
*ctx
= file
->private_data
;
209 timerfd_remove_cancel(ctx
);
212 alarm_cancel(&ctx
->t
.alarm
);
214 hrtimer_cancel(&ctx
->t
.tmr
);
219 static unsigned int timerfd_poll(struct file
*file
, poll_table
*wait
)
221 struct timerfd_ctx
*ctx
= file
->private_data
;
222 unsigned int events
= 0;
225 poll_wait(file
, &ctx
->wqh
, wait
);
227 spin_lock_irqsave(&ctx
->wqh
.lock
, flags
);
230 spin_unlock_irqrestore(&ctx
->wqh
.lock
, flags
);
235 static ssize_t
timerfd_read(struct file
*file
, char __user
*buf
, size_t count
,
238 struct timerfd_ctx
*ctx
= file
->private_data
;
242 if (count
< sizeof(ticks
))
244 spin_lock_irq(&ctx
->wqh
.lock
);
245 if (file
->f_flags
& O_NONBLOCK
)
248 res
= wait_event_interruptible_locked_irq(ctx
->wqh
, ctx
->ticks
);
251 * If clock has changed, we do not care about the
252 * ticks and we do not rearm the timer. Userspace must
255 if (timerfd_canceled(ctx
)) {
264 if (ctx
->expired
&& ctx
->tintv
) {
266 * If tintv != 0, this is a periodic timer that
267 * needs to be re-armed. We avoid doing it in the timer
268 * callback to avoid DoS attacks specifying a very
269 * short timer period.
272 ticks
+= alarm_forward_now(
273 &ctx
->t
.alarm
, ctx
->tintv
) - 1;
274 alarm_restart(&ctx
->t
.alarm
);
276 ticks
+= hrtimer_forward_now(&ctx
->t
.tmr
,
278 hrtimer_restart(&ctx
->t
.tmr
);
284 spin_unlock_irq(&ctx
->wqh
.lock
);
286 res
= put_user(ticks
, (u64 __user
*) buf
) ? -EFAULT
: sizeof(ticks
);
290 #ifdef CONFIG_PROC_FS
291 static void timerfd_show(struct seq_file
*m
, struct file
*file
)
293 struct timerfd_ctx
*ctx
= file
->private_data
;
296 spin_lock_irq(&ctx
->wqh
.lock
);
297 t
.it_value
= ktime_to_timespec(timerfd_get_remaining(ctx
));
298 t
.it_interval
= ktime_to_timespec(ctx
->tintv
);
299 spin_unlock_irq(&ctx
->wqh
.lock
);
304 "settime flags: 0%o\n"
305 "it_value: (%llu, %llu)\n"
306 "it_interval: (%llu, %llu)\n",
308 (unsigned long long)ctx
->ticks
,
310 (unsigned long long)t
.it_value
.tv_sec
,
311 (unsigned long long)t
.it_value
.tv_nsec
,
312 (unsigned long long)t
.it_interval
.tv_sec
,
313 (unsigned long long)t
.it_interval
.tv_nsec
);
316 #define timerfd_show NULL
319 #ifdef CONFIG_CHECKPOINT_RESTORE
320 static long timerfd_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
322 struct timerfd_ctx
*ctx
= file
->private_data
;
326 case TFD_IOC_SET_TICKS
: {
329 if (copy_from_user(&ticks
, (u64 __user
*)arg
, sizeof(ticks
)))
334 spin_lock_irq(&ctx
->wqh
.lock
);
335 if (!timerfd_canceled(ctx
)) {
337 wake_up_locked(&ctx
->wqh
);
340 spin_unlock_irq(&ctx
->wqh
.lock
);
351 #define timerfd_ioctl NULL
354 static const struct file_operations timerfd_fops
= {
355 .release
= timerfd_release
,
356 .poll
= timerfd_poll
,
357 .read
= timerfd_read
,
358 .llseek
= noop_llseek
,
359 .show_fdinfo
= timerfd_show
,
360 .unlocked_ioctl
= timerfd_ioctl
,
363 static int timerfd_fget(int fd
, struct fd
*p
)
365 struct fd f
= fdget(fd
);
368 if (f
.file
->f_op
!= &timerfd_fops
) {
376 SYSCALL_DEFINE2(timerfd_create
, int, clockid
, int, flags
)
379 struct timerfd_ctx
*ctx
;
381 /* Check the TFD_* constants for consistency. */
382 BUILD_BUG_ON(TFD_CLOEXEC
!= O_CLOEXEC
);
383 BUILD_BUG_ON(TFD_NONBLOCK
!= O_NONBLOCK
);
385 if ((flags
& ~TFD_CREATE_FLAGS
) ||
386 (clockid
!= CLOCK_MONOTONIC
&&
387 clockid
!= CLOCK_REALTIME
&&
388 clockid
!= CLOCK_REALTIME_ALARM
&&
389 clockid
!= CLOCK_BOOTTIME
&&
390 clockid
!= CLOCK_BOOTTIME_ALARM
))
393 if (!capable(CAP_WAKE_ALARM
) &&
394 (clockid
== CLOCK_REALTIME_ALARM
||
395 clockid
== CLOCK_BOOTTIME_ALARM
))
398 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
402 init_waitqueue_head(&ctx
->wqh
);
403 ctx
->clockid
= clockid
;
406 alarm_init(&ctx
->t
.alarm
,
407 ctx
->clockid
== CLOCK_REALTIME_ALARM
?
408 ALARM_REALTIME
: ALARM_BOOTTIME
,
411 hrtimer_init(&ctx
->t
.tmr
, clockid
, HRTIMER_MODE_ABS
);
413 ctx
->moffs
= ktime_mono_to_real(0);
415 ufd
= anon_inode_getfd("[timerfd]", &timerfd_fops
, ctx
,
416 O_RDWR
| (flags
& TFD_SHARED_FCNTL_FLAGS
));
423 static int do_timerfd_settime(int ufd
, int flags
,
424 const struct itimerspec
*new,
425 struct itimerspec
*old
)
428 struct timerfd_ctx
*ctx
;
431 if ((flags
& ~TFD_SETTIME_FLAGS
) ||
432 !timespec_valid(&new->it_value
) ||
433 !timespec_valid(&new->it_interval
))
436 ret
= timerfd_fget(ufd
, &f
);
439 ctx
= f
.file
->private_data
;
441 if (!capable(CAP_WAKE_ALARM
) && isalarm(ctx
)) {
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
) {
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
) {
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;