drm/i915: Fix calculation of backlight value in combined mode
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / timerfd.c
blob8c4fc1425b3eff171bd37b9e1bed290b93473e11
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/slab.h>
18 #include <linux/list.h>
19 #include <linux/spinlock.h>
20 #include <linux/time.h>
21 #include <linux/hrtimer.h>
22 #include <linux/anon_inodes.h>
23 #include <linux/timerfd.h>
24 #include <linux/syscalls.h>
26 struct timerfd_ctx {
27 struct hrtimer tmr;
28 ktime_t tintv;
29 wait_queue_head_t wqh;
30 u64 ticks;
31 int expired;
32 int clockid;
36 * This gets called when the timer event triggers. We set the "expired"
37 * flag, but we do not re-arm the timer (in case it's necessary,
38 * tintv.tv64 != 0) until the timer is accessed.
40 static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr)
42 struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx, tmr);
43 unsigned long flags;
45 spin_lock_irqsave(&ctx->wqh.lock, flags);
46 ctx->expired = 1;
47 ctx->ticks++;
48 wake_up_locked(&ctx->wqh);
49 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
51 return HRTIMER_NORESTART;
54 static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
56 ktime_t remaining;
58 remaining = hrtimer_expires_remaining(&ctx->tmr);
59 return remaining.tv64 < 0 ? ktime_set(0, 0): remaining;
62 static void timerfd_setup(struct timerfd_ctx *ctx, int flags,
63 const struct itimerspec *ktmr)
65 enum hrtimer_mode htmode;
66 ktime_t texp;
68 htmode = (flags & TFD_TIMER_ABSTIME) ?
69 HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
71 texp = timespec_to_ktime(ktmr->it_value);
72 ctx->expired = 0;
73 ctx->ticks = 0;
74 ctx->tintv = timespec_to_ktime(ktmr->it_interval);
75 hrtimer_init(&ctx->tmr, ctx->clockid, htmode);
76 hrtimer_set_expires(&ctx->tmr, texp);
77 ctx->tmr.function = timerfd_tmrproc;
78 if (texp.tv64 != 0)
79 hrtimer_start(&ctx->tmr, texp, htmode);
82 static int timerfd_release(struct inode *inode, struct file *file)
84 struct timerfd_ctx *ctx = file->private_data;
86 hrtimer_cancel(&ctx->tmr);
87 kfree(ctx);
88 return 0;
91 static unsigned int timerfd_poll(struct file *file, poll_table *wait)
93 struct timerfd_ctx *ctx = file->private_data;
94 unsigned int events = 0;
95 unsigned long flags;
97 poll_wait(file, &ctx->wqh, wait);
99 spin_lock_irqsave(&ctx->wqh.lock, flags);
100 if (ctx->ticks)
101 events |= POLLIN;
102 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
104 return events;
107 static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
108 loff_t *ppos)
110 struct timerfd_ctx *ctx = file->private_data;
111 ssize_t res;
112 u64 ticks = 0;
114 if (count < sizeof(ticks))
115 return -EINVAL;
116 spin_lock_irq(&ctx->wqh.lock);
117 if (file->f_flags & O_NONBLOCK)
118 res = -EAGAIN;
119 else
120 res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
121 if (ctx->ticks) {
122 ticks = ctx->ticks;
123 if (ctx->expired && ctx->tintv.tv64) {
125 * If tintv.tv64 != 0, this is a periodic timer that
126 * needs to be re-armed. We avoid doing it in the timer
127 * callback to avoid DoS attacks specifying a very
128 * short timer period.
130 ticks += hrtimer_forward_now(&ctx->tmr,
131 ctx->tintv) - 1;
132 hrtimer_restart(&ctx->tmr);
134 ctx->expired = 0;
135 ctx->ticks = 0;
137 spin_unlock_irq(&ctx->wqh.lock);
138 if (ticks)
139 res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
140 return res;
143 static const struct file_operations timerfd_fops = {
144 .release = timerfd_release,
145 .poll = timerfd_poll,
146 .read = timerfd_read,
147 .llseek = noop_llseek,
150 static struct file *timerfd_fget(int fd)
152 struct file *file;
154 file = fget(fd);
155 if (!file)
156 return ERR_PTR(-EBADF);
157 if (file->f_op != &timerfd_fops) {
158 fput(file);
159 return ERR_PTR(-EINVAL);
162 return file;
165 SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
167 int ufd;
168 struct timerfd_ctx *ctx;
170 /* Check the TFD_* constants for consistency. */
171 BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
172 BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
174 if ((flags & ~TFD_CREATE_FLAGS) ||
175 (clockid != CLOCK_MONOTONIC &&
176 clockid != CLOCK_REALTIME))
177 return -EINVAL;
179 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
180 if (!ctx)
181 return -ENOMEM;
183 init_waitqueue_head(&ctx->wqh);
184 ctx->clockid = clockid;
185 hrtimer_init(&ctx->tmr, clockid, HRTIMER_MODE_ABS);
187 ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx,
188 O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
189 if (ufd < 0)
190 kfree(ctx);
192 return ufd;
195 SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
196 const struct itimerspec __user *, utmr,
197 struct itimerspec __user *, otmr)
199 struct file *file;
200 struct timerfd_ctx *ctx;
201 struct itimerspec ktmr, kotmr;
203 if (copy_from_user(&ktmr, utmr, sizeof(ktmr)))
204 return -EFAULT;
206 if ((flags & ~TFD_SETTIME_FLAGS) ||
207 !timespec_valid(&ktmr.it_value) ||
208 !timespec_valid(&ktmr.it_interval))
209 return -EINVAL;
211 file = timerfd_fget(ufd);
212 if (IS_ERR(file))
213 return PTR_ERR(file);
214 ctx = file->private_data;
217 * We need to stop the existing timer before reprogramming
218 * it to the new values.
220 for (;;) {
221 spin_lock_irq(&ctx->wqh.lock);
222 if (hrtimer_try_to_cancel(&ctx->tmr) >= 0)
223 break;
224 spin_unlock_irq(&ctx->wqh.lock);
225 cpu_relax();
229 * If the timer is expired and it's periodic, we need to advance it
230 * because the caller may want to know the previous expiration time.
231 * We do not update "ticks" and "expired" since the timer will be
232 * re-programmed again in the following timerfd_setup() call.
234 if (ctx->expired && ctx->tintv.tv64)
235 hrtimer_forward_now(&ctx->tmr, ctx->tintv);
237 kotmr.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
238 kotmr.it_interval = ktime_to_timespec(ctx->tintv);
241 * Re-program the timer to the new value ...
243 timerfd_setup(ctx, flags, &ktmr);
245 spin_unlock_irq(&ctx->wqh.lock);
246 fput(file);
247 if (otmr && copy_to_user(otmr, &kotmr, sizeof(kotmr)))
248 return -EFAULT;
250 return 0;
253 SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct itimerspec __user *, otmr)
255 struct file *file;
256 struct timerfd_ctx *ctx;
257 struct itimerspec kotmr;
259 file = timerfd_fget(ufd);
260 if (IS_ERR(file))
261 return PTR_ERR(file);
262 ctx = file->private_data;
264 spin_lock_irq(&ctx->wqh.lock);
265 if (ctx->expired && ctx->tintv.tv64) {
266 ctx->expired = 0;
267 ctx->ticks +=
268 hrtimer_forward_now(&ctx->tmr, ctx->tintv) - 1;
269 hrtimer_restart(&ctx->tmr);
271 kotmr.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
272 kotmr.it_interval = ktime_to_timespec(ctx->tintv);
273 spin_unlock_irq(&ctx->wqh.lock);
274 fput(file);
276 return copy_to_user(otmr, &kotmr, sizeof(kotmr)) ? -EFAULT: 0;