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>
27 wait_queue_head_t wqh;
32 * This gets called when the timer event triggers. We set the "expired"
33 * flag, but we do not re-arm the timer (in case it's necessary,
34 * tintv.tv64 != 0) until the timer is read.
36 static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr)
38 struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx, tmr);
41 spin_lock_irqsave(&ctx->wqh.lock, flags);
43 wake_up_locked(&ctx->wqh);
44 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
46 return HRTIMER_NORESTART;
49 static void timerfd_setup(struct timerfd_ctx *ctx, int clockid, int flags,
50 const struct itimerspec *ktmr)
52 enum hrtimer_mode htmode;
55 htmode = (flags & TFD_TIMER_ABSTIME) ?
56 HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
58 texp = timespec_to_ktime(ktmr->it_value);
60 ctx->tintv = timespec_to_ktime(ktmr->it_interval);
61 hrtimer_init(&ctx->tmr, clockid, htmode);
62 ctx->tmr.expires = texp;
63 ctx->tmr.function = timerfd_tmrproc;
65 hrtimer_start(&ctx->tmr, texp, htmode);
68 static int timerfd_release(struct inode *inode, struct file *file)
70 struct timerfd_ctx *ctx = file->private_data;
72 hrtimer_cancel(&ctx->tmr);
77 static unsigned int timerfd_poll(struct file *file, poll_table *wait)
79 struct timerfd_ctx *ctx = file->private_data;
80 unsigned int events = 0;
83 poll_wait(file, &ctx->wqh, wait);
85 spin_lock_irqsave(&ctx->wqh.lock, flags);
88 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
93 static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
96 struct timerfd_ctx *ctx = file->private_data;
99 DECLARE_WAITQUEUE(wait, current);
101 if (count < sizeof(ticks))
103 spin_lock_irq(&ctx->wqh.lock);
105 if (!ctx->expired && !(file->f_flags & O_NONBLOCK)) {
106 __add_wait_queue(&ctx->wqh, &wait);
108 set_current_state(TASK_INTERRUPTIBLE);
113 if (signal_pending(current)) {
117 spin_unlock_irq(&ctx->wqh.lock);
119 spin_lock_irq(&ctx->wqh.lock);
121 __remove_wait_queue(&ctx->wqh, &wait);
122 __set_current_state(TASK_RUNNING);
126 if (ctx->tintv.tv64 != 0) {
128 * If tintv.tv64 != 0, this is a periodic timer that
129 * needs to be re-armed. We avoid doing it in the timer
130 * callback to avoid DoS attacks specifying a very
131 * short timer period.
134 hrtimer_forward(&ctx->tmr,
135 hrtimer_cb_get_time(&ctx->tmr),
137 hrtimer_restart(&ctx->tmr);
141 spin_unlock_irq(&ctx->wqh.lock);
143 res = put_user(ticks, buf) ? -EFAULT: sizeof(ticks);
147 static const struct file_operations timerfd_fops = {
148 .release = timerfd_release,
149 .poll = timerfd_poll,
150 .read = timerfd_read,
153 asmlinkage long sys_timerfd(int ufd, int clockid, int flags,
154 const struct itimerspec __user *utmr)
157 struct timerfd_ctx *ctx;
160 struct itimerspec ktmr;
162 if (copy_from_user(&ktmr, utmr, sizeof(ktmr)))
165 if (clockid != CLOCK_MONOTONIC &&
166 clockid != CLOCK_REALTIME)
168 if (!timespec_valid(&ktmr.it_value) ||
169 !timespec_valid(&ktmr.it_interval))
173 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
177 init_waitqueue_head(&ctx->wqh);
179 timerfd_setup(ctx, clockid, flags, &ktmr);
182 * When we call this, the initialization must be complete, since
183 * anon_inode_getfd() will install the fd.
185 error = anon_inode_getfd(&ufd, &inode, &file, "[timerfd]",
193 ctx = file->private_data;
194 if (file->f_op != &timerfd_fops) {
199 * We need to stop the existing timer before reprogramming
200 * it to the new values.
203 spin_lock_irq(&ctx->wqh.lock);
204 if (hrtimer_try_to_cancel(&ctx->tmr) >= 0)
206 spin_unlock_irq(&ctx->wqh.lock);
210 * Re-program the timer to the new value ...
212 timerfd_setup(ctx, clockid, flags, &ktmr);
214 spin_unlock_irq(&ctx->wqh.lock);
221 hrtimer_cancel(&ctx->tmr);