ibm_emac: fix link speed detection change
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / timerfd.c
blobe329e37f15a846b4898132a2b7b91b408ee2c03c
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 spinlock_t lock;
28 wait_queue_head_t wqh;
29 int expired;
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);
40 unsigned long flags;
42 spin_lock_irqsave(&ctx->lock, flags);
43 ctx->expired = 1;
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;
54 ktime_t texp;
56 htmode = (flags & TFD_TIMER_ABSTIME) ?
57 HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
59 texp = timespec_to_ktime(ktmr->it_value);
60 ctx->expired = 0;
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;
65 if (texp.tv64 != 0)
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);
74 kfree(ctx);
75 return 0;
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;
82 unsigned long flags;
84 poll_wait(file, &ctx->wqh, wait);
86 spin_lock_irqsave(&ctx->lock, flags);
87 if (ctx->expired)
88 events |= POLLIN;
89 spin_unlock_irqrestore(&ctx->lock, flags);
91 return events;
94 static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
95 loff_t *ppos)
97 struct timerfd_ctx *ctx = file->private_data;
98 ssize_t res;
99 u32 ticks = 0;
100 DECLARE_WAITQUEUE(wait, current);
102 if (count < sizeof(ticks))
103 return -EINVAL;
104 spin_lock_irq(&ctx->lock);
105 res = -EAGAIN;
106 if (!ctx->expired && !(file->f_flags & O_NONBLOCK)) {
107 __add_wait_queue(&ctx->wqh, &wait);
108 for (res = 0;;) {
109 set_current_state(TASK_INTERRUPTIBLE);
110 if (ctx->expired) {
111 res = 0;
112 break;
114 if (signal_pending(current)) {
115 res = -ERESTARTSYS;
116 break;
118 spin_unlock_irq(&ctx->lock);
119 schedule();
120 spin_lock_irq(&ctx->lock);
122 __remove_wait_queue(&ctx->wqh, &wait);
123 __set_current_state(TASK_RUNNING);
125 if (ctx->expired) {
126 ctx->expired = 0;
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.
134 ticks = (u32)
135 hrtimer_forward(&ctx->tmr,
136 hrtimer_cb_get_time(&ctx->tmr),
137 ctx->tintv);
138 hrtimer_restart(&ctx->tmr);
139 } else
140 ticks = 1;
142 spin_unlock_irq(&ctx->lock);
143 if (ticks)
144 res = put_user(ticks, buf) ? -EFAULT: sizeof(ticks);
145 return res;
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)
157 int error;
158 struct timerfd_ctx *ctx;
159 struct file *file;
160 struct inode *inode;
161 struct itimerspec ktmr;
163 if (copy_from_user(&ktmr, utmr, sizeof(ktmr)))
164 return -EFAULT;
166 if (clockid != CLOCK_MONOTONIC &&
167 clockid != CLOCK_REALTIME)
168 return -EINVAL;
169 if (!timespec_valid(&ktmr.it_value) ||
170 !timespec_valid(&ktmr.it_interval))
171 return -EINVAL;
173 if (ufd == -1) {
174 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
175 if (!ctx)
176 return -ENOMEM;
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]",
188 &timerfd_fops, ctx);
189 if (error)
190 goto err_tmrcancel;
191 } else {
192 file = fget(ufd);
193 if (!file)
194 return -EBADF;
195 ctx = file->private_data;
196 if (file->f_op != &timerfd_fops) {
197 fput(file);
198 return -EINVAL;
201 * We need to stop the existing timer before reprogramming
202 * it to the new values.
204 for (;;) {
205 spin_lock_irq(&ctx->lock);
206 if (hrtimer_try_to_cancel(&ctx->tmr) >= 0)
207 break;
208 spin_unlock_irq(&ctx->lock);
209 cpu_relax();
212 * Re-program the timer to the new value ...
214 timerfd_setup(ctx, clockid, flags, &ktmr);
216 spin_unlock_irq(&ctx->lock);
217 fput(file);
220 return ufd;
222 err_tmrcancel:
223 hrtimer_cancel(&ctx->tmr);
224 kfree(ctx);
225 return error;