4 * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org>
7 * Thanks to Thomas Gleixner for code reviews and useful comments.
11 #include <linux/file.h>
12 #include <linux/poll.h>
13 #include <linux/init.h>
15 #include <linux/sched.h>
16 #include <linux/kernel.h>
17 #include <linux/list.h>
18 #include <linux/spinlock.h>
19 #include <linux/time.h>
20 #include <linux/hrtimer.h>
21 #include <linux/anon_inodes.h>
22 #include <linux/timerfd.h>
28 wait_queue_head_t wqh;
33 * This gets called when the timer event triggers. We set the "expired"
34 * flag, but we do not re-arm the timer (in case it's necessary,
35 * tintv.tv64 != 0) until the timer is read.
37 static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr)
39 struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx, tmr);
42 spin_lock_irqsave(&ctx->lock, flags);
44 wake_up_locked(&ctx->wqh);
45 spin_unlock_irqrestore(&ctx->lock, flags);
47 return HRTIMER_NORESTART;
50 static void timerfd_setup(struct timerfd_ctx *ctx, int clockid, int flags,
51 const struct itimerspec *ktmr)
53 enum hrtimer_mode htmode;
56 htmode = (flags & TFD_TIMER_ABSTIME) ?
57 HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
59 texp = timespec_to_ktime(ktmr->it_value);
61 ctx->tintv = timespec_to_ktime(ktmr->it_interval);
62 hrtimer_init(&ctx->tmr, clockid, htmode);
63 ctx->tmr.expires = texp;
64 ctx->tmr.function = timerfd_tmrproc;
66 hrtimer_start(&ctx->tmr, texp, htmode);
69 static int timerfd_release(struct inode *inode, struct file *file)
71 struct timerfd_ctx *ctx = file->private_data;
73 hrtimer_cancel(&ctx->tmr);
78 static unsigned int timerfd_poll(struct file *file, poll_table *wait)
80 struct timerfd_ctx *ctx = file->private_data;
81 unsigned int events = 0;
84 poll_wait(file, &ctx->wqh, wait);
86 spin_lock_irqsave(&ctx->lock, flags);
89 spin_unlock_irqrestore(&ctx->lock, flags);
94 static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
97 struct timerfd_ctx *ctx = file->private_data;
100 DECLARE_WAITQUEUE(wait, current);
102 if (count < sizeof(ticks))
104 spin_lock_irq(&ctx->lock);
106 if (!ctx->expired && !(file->f_flags & O_NONBLOCK)) {
107 __add_wait_queue(&ctx->wqh, &wait);
109 set_current_state(TASK_INTERRUPTIBLE);
114 if (signal_pending(current)) {
118 spin_unlock_irq(&ctx->lock);
120 spin_lock_irq(&ctx->lock);
122 __remove_wait_queue(&ctx->wqh, &wait);
123 __set_current_state(TASK_RUNNING);
127 if (ctx->tintv.tv64 != 0) {
129 * If tintv.tv64 != 0, this is a periodic timer that
130 * needs to be re-armed. We avoid doing it in the timer
131 * callback to avoid DoS attacks specifying a very
132 * short timer period.
135 hrtimer_forward(&ctx->tmr,
136 hrtimer_cb_get_time(&ctx->tmr),
138 hrtimer_restart(&ctx->tmr);
142 spin_unlock_irq(&ctx->lock);
144 res = put_user(ticks, buf) ? -EFAULT: sizeof(ticks);
148 static const struct file_operations timerfd_fops = {
149 .release = timerfd_release,
150 .poll = timerfd_poll,
151 .read = timerfd_read,
154 asmlinkage long sys_timerfd(int ufd, int clockid, int flags,
155 const struct itimerspec __user *utmr)
158 struct timerfd_ctx *ctx;
161 struct itimerspec ktmr;
163 if (copy_from_user(&ktmr, utmr, sizeof(ktmr)))
166 if (clockid != CLOCK_MONOTONIC &&
167 clockid != CLOCK_REALTIME)
169 if (!timespec_valid(&ktmr.it_value) ||
170 !timespec_valid(&ktmr.it_interval))
174 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
178 init_waitqueue_head(&ctx->wqh);
179 spin_lock_init(&ctx->lock);
181 timerfd_setup(ctx, clockid, flags, &ktmr);
184 * When we call this, the initialization must be complete, since
185 * anon_inode_getfd() will install the fd.
187 error = anon_inode_getfd(&ufd, &inode, &file, "[timerfd]",
195 ctx = file->private_data;
196 if (file->f_op != &timerfd_fops) {
201 * We need to stop the existing timer before reprogramming
202 * it to the new values.
205 spin_lock_irq(&ctx->lock);
206 if (hrtimer_try_to_cancel(&ctx->tmr) >= 0)
208 spin_unlock_irq(&ctx->lock);
212 * Re-program the timer to the new value ...
214 timerfd_setup(ctx, clockid, flags, &ktmr);
216 spin_unlock_irq(&ctx->lock);
223 hrtimer_cancel(&ctx->tmr);