Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6/trivial-mods.git] / fs / timerfd.c
blob61983f3b107c5ee5d12521d3f38255a77bc6f781
1 /*
2 * fs/timerfd.c
4 * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org>
7 * Thanks to Thomas Gleixner for code reviews and useful comments.
9 */
11 #include <linux/file.h>
12 #include <linux/poll.h>
13 #include <linux/init.h>
14 #include <linux/fs.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>
24 struct timerfd_ctx {
25 struct hrtimer tmr;
26 ktime_t tintv;
27 wait_queue_head_t wqh;
28 int expired;
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);
39 unsigned long flags;
41 spin_lock_irqsave(&ctx->wqh.lock, flags);
42 ctx->expired = 1;
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;
53 ktime_t texp;
55 htmode = (flags & TFD_TIMER_ABSTIME) ?
56 HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
58 texp = timespec_to_ktime(ktmr->it_value);
59 ctx->expired = 0;
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;
64 if (texp.tv64 != 0)
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);
73 kfree(ctx);
74 return 0;
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;
81 unsigned long flags;
83 poll_wait(file, &ctx->wqh, wait);
85 spin_lock_irqsave(&ctx->wqh.lock, flags);
86 if (ctx->expired)
87 events |= POLLIN;
88 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
90 return events;
93 static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
94 loff_t *ppos)
96 struct timerfd_ctx *ctx = file->private_data;
97 ssize_t res;
98 u64 ticks = 0;
99 DECLARE_WAITQUEUE(wait, current);
101 if (count < sizeof(ticks))
102 return -EINVAL;
103 spin_lock_irq(&ctx->wqh.lock);
104 res = -EAGAIN;
105 if (!ctx->expired && !(file->f_flags & O_NONBLOCK)) {
106 __add_wait_queue(&ctx->wqh, &wait);
107 for (res = 0;;) {
108 set_current_state(TASK_INTERRUPTIBLE);
109 if (ctx->expired) {
110 res = 0;
111 break;
113 if (signal_pending(current)) {
114 res = -ERESTARTSYS;
115 break;
117 spin_unlock_irq(&ctx->wqh.lock);
118 schedule();
119 spin_lock_irq(&ctx->wqh.lock);
121 __remove_wait_queue(&ctx->wqh, &wait);
122 __set_current_state(TASK_RUNNING);
124 if (ctx->expired) {
125 ctx->expired = 0;
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.
133 ticks = (u64)
134 hrtimer_forward(&ctx->tmr,
135 hrtimer_cb_get_time(&ctx->tmr),
136 ctx->tintv);
137 hrtimer_restart(&ctx->tmr);
138 } else
139 ticks = 1;
141 spin_unlock_irq(&ctx->wqh.lock);
142 if (ticks)
143 res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
144 return res;
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)
156 int error;
157 struct timerfd_ctx *ctx;
158 struct file *file;
159 struct inode *inode;
160 struct itimerspec ktmr;
162 if (copy_from_user(&ktmr, utmr, sizeof(ktmr)))
163 return -EFAULT;
165 if (clockid != CLOCK_MONOTONIC &&
166 clockid != CLOCK_REALTIME)
167 return -EINVAL;
168 if (!timespec_valid(&ktmr.it_value) ||
169 !timespec_valid(&ktmr.it_interval))
170 return -EINVAL;
172 if (ufd == -1) {
173 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
174 if (!ctx)
175 return -ENOMEM;
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]",
186 &timerfd_fops, ctx);
187 if (error)
188 goto err_tmrcancel;
189 } else {
190 file = fget(ufd);
191 if (!file)
192 return -EBADF;
193 ctx = file->private_data;
194 if (file->f_op != &timerfd_fops) {
195 fput(file);
196 return -EINVAL;
199 * We need to stop the existing timer before reprogramming
200 * it to the new values.
202 for (;;) {
203 spin_lock_irq(&ctx->wqh.lock);
204 if (hrtimer_try_to_cancel(&ctx->tmr) >= 0)
205 break;
206 spin_unlock_irq(&ctx->wqh.lock);
207 cpu_relax();
210 * Re-program the timer to the new value ...
212 timerfd_setup(ctx, clockid, flags, &ktmr);
214 spin_unlock_irq(&ctx->wqh.lock);
215 fput(file);
218 return ufd;
220 err_tmrcancel:
221 hrtimer_cancel(&ctx->tmr);
222 kfree(ctx);
223 return error;