RDMA/ucma: Check that device exists prior to accessing it
[pandora-kernel.git] / fs / timerfd.c
1 /*
2  *  fs/timerfd.c
3  *
4  *  Copyright (C) 2007  Davide Libenzi <davidel@xmailserver.org>
5  *
6  *
7  *  Thanks to Thomas Gleixner for code reviews and useful comments.
8  *
9  */
10
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>
25 #include <linux/rcupdate.h>
26
27 struct timerfd_ctx {
28         struct hrtimer tmr;
29         ktime_t tintv;
30         ktime_t moffs;
31         wait_queue_head_t wqh;
32         u64 ticks;
33         int expired;
34         int clockid;
35         struct rcu_head rcu;
36         struct list_head clist;
37         spinlock_t cancel_lock;
38         bool might_cancel;
39 };
40
41 static LIST_HEAD(cancel_list);
42 static DEFINE_SPINLOCK(cancel_lock);
43
44 /*
45  * This gets called when the timer event triggers. We set the "expired"
46  * flag, but we do not re-arm the timer (in case it's necessary,
47  * tintv.tv64 != 0) until the timer is accessed.
48  */
49 static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr)
50 {
51         struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx, tmr);
52         unsigned long flags;
53
54         spin_lock_irqsave(&ctx->wqh.lock, flags);
55         ctx->expired = 1;
56         ctx->ticks++;
57         wake_up_locked(&ctx->wqh);
58         spin_unlock_irqrestore(&ctx->wqh.lock, flags);
59
60         return HRTIMER_NORESTART;
61 }
62
63 /*
64  * Called when the clock was set to cancel the timers in the cancel
65  * list. This will wake up processes waiting on these timers. The
66  * wake-up requires ctx->ticks to be non zero, therefore we increment
67  * it before calling wake_up_locked().
68  */
69 void timerfd_clock_was_set(void)
70 {
71         ktime_t moffs = ktime_get_monotonic_offset();
72         struct timerfd_ctx *ctx;
73         unsigned long flags;
74
75         rcu_read_lock();
76         list_for_each_entry_rcu(ctx, &cancel_list, clist) {
77                 if (!ctx->might_cancel)
78                         continue;
79                 spin_lock_irqsave(&ctx->wqh.lock, flags);
80                 if (ctx->moffs.tv64 != moffs.tv64) {
81                         ctx->moffs.tv64 = KTIME_MAX;
82                         ctx->ticks++;
83                         wake_up_locked(&ctx->wqh);
84                 }
85                 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
86         }
87         rcu_read_unlock();
88 }
89
90 static void __timerfd_remove_cancel(struct timerfd_ctx *ctx)
91 {
92         if (ctx->might_cancel) {
93                 ctx->might_cancel = false;
94                 spin_lock(&cancel_lock);
95                 list_del_rcu(&ctx->clist);
96                 spin_unlock(&cancel_lock);
97         }
98 }
99
100 static void timerfd_remove_cancel(struct timerfd_ctx *ctx)
101 {
102         spin_lock(&ctx->cancel_lock);
103         __timerfd_remove_cancel(ctx);
104         spin_unlock(&ctx->cancel_lock);
105 }
106
107 static bool timerfd_canceled(struct timerfd_ctx *ctx)
108 {
109         if (!ctx->might_cancel || ctx->moffs.tv64 != KTIME_MAX)
110                 return false;
111         ctx->moffs = ktime_get_monotonic_offset();
112         return true;
113 }
114
115 static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags)
116 {
117         spin_lock(&ctx->cancel_lock);
118         if (ctx->clockid == CLOCK_REALTIME && (flags & TFD_TIMER_ABSTIME) &&
119             (flags & TFD_TIMER_CANCEL_ON_SET)) {
120                 if (!ctx->might_cancel) {
121                         ctx->might_cancel = true;
122                         spin_lock(&cancel_lock);
123                         list_add_rcu(&ctx->clist, &cancel_list);
124                         spin_unlock(&cancel_lock);
125                 }
126         } else {
127                 __timerfd_remove_cancel(ctx);
128         }
129         spin_unlock(&ctx->cancel_lock);
130 }
131
132 static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
133 {
134         ktime_t remaining;
135
136         remaining = hrtimer_expires_remaining_adjusted(&ctx->tmr);
137         return remaining.tv64 < 0 ? ktime_set(0, 0): remaining;
138 }
139
140 static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
141                          const struct itimerspec *ktmr)
142 {
143         enum hrtimer_mode htmode;
144         ktime_t texp;
145         int clockid = ctx->clockid;
146
147         htmode = (flags & TFD_TIMER_ABSTIME) ?
148                 HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
149
150         texp = timespec_to_ktime(ktmr->it_value);
151         ctx->expired = 0;
152         ctx->ticks = 0;
153         ctx->tintv = timespec_to_ktime(ktmr->it_interval);
154         hrtimer_init(&ctx->tmr, clockid, htmode);
155         hrtimer_set_expires(&ctx->tmr, texp);
156         ctx->tmr.function = timerfd_tmrproc;
157         if (texp.tv64 != 0) {
158                 hrtimer_start(&ctx->tmr, texp, htmode);
159                 if (timerfd_canceled(ctx))
160                         return -ECANCELED;
161         }
162         return 0;
163 }
164
165 static int timerfd_release(struct inode *inode, struct file *file)
166 {
167         struct timerfd_ctx *ctx = file->private_data;
168
169         timerfd_remove_cancel(ctx);
170         hrtimer_cancel(&ctx->tmr);
171         kfree_rcu(ctx, rcu);
172         return 0;
173 }
174
175 static unsigned int timerfd_poll(struct file *file, poll_table *wait)
176 {
177         struct timerfd_ctx *ctx = file->private_data;
178         unsigned int events = 0;
179         unsigned long flags;
180
181         poll_wait(file, &ctx->wqh, wait);
182
183         spin_lock_irqsave(&ctx->wqh.lock, flags);
184         if (ctx->ticks)
185                 events |= POLLIN;
186         spin_unlock_irqrestore(&ctx->wqh.lock, flags);
187
188         return events;
189 }
190
191 static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
192                             loff_t *ppos)
193 {
194         struct timerfd_ctx *ctx = file->private_data;
195         ssize_t res;
196         u64 ticks = 0;
197
198         if (count < sizeof(ticks))
199                 return -EINVAL;
200         spin_lock_irq(&ctx->wqh.lock);
201         if (file->f_flags & O_NONBLOCK)
202                 res = -EAGAIN;
203         else
204                 res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
205
206         /*
207          * If clock has changed, we do not care about the
208          * ticks and we do not rearm the timer. Userspace must
209          * reevaluate anyway.
210          */
211         if (timerfd_canceled(ctx)) {
212                 ctx->ticks = 0;
213                 ctx->expired = 0;
214                 res = -ECANCELED;
215         }
216
217         if (ctx->ticks) {
218                 ticks = ctx->ticks;
219
220                 if (ctx->expired && ctx->tintv.tv64) {
221                         /*
222                          * If tintv.tv64 != 0, this is a periodic timer that
223                          * needs to be re-armed. We avoid doing it in the timer
224                          * callback to avoid DoS attacks specifying a very
225                          * short timer period.
226                          */
227                         ticks += hrtimer_forward_now(&ctx->tmr,
228                                                      ctx->tintv) - 1;
229                         hrtimer_restart(&ctx->tmr);
230                 }
231                 ctx->expired = 0;
232                 ctx->ticks = 0;
233         }
234         spin_unlock_irq(&ctx->wqh.lock);
235         if (ticks)
236                 res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
237         return res;
238 }
239
240 static const struct file_operations timerfd_fops = {
241         .release        = timerfd_release,
242         .poll           = timerfd_poll,
243         .read           = timerfd_read,
244         .llseek         = noop_llseek,
245 };
246
247 static struct file *timerfd_fget(int fd)
248 {
249         struct file *file;
250
251         file = fget(fd);
252         if (!file)
253                 return ERR_PTR(-EBADF);
254         if (file->f_op != &timerfd_fops) {
255                 fput(file);
256                 return ERR_PTR(-EINVAL);
257         }
258
259         return file;
260 }
261
262 SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
263 {
264         int ufd;
265         struct timerfd_ctx *ctx;
266
267         /* Check the TFD_* constants for consistency.  */
268         BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
269         BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
270
271         if ((flags & ~TFD_CREATE_FLAGS) ||
272             (clockid != CLOCK_MONOTONIC &&
273              clockid != CLOCK_REALTIME))
274                 return -EINVAL;
275
276         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
277         if (!ctx)
278                 return -ENOMEM;
279
280         init_waitqueue_head(&ctx->wqh);
281         spin_lock_init(&ctx->cancel_lock);
282         ctx->clockid = clockid;
283         hrtimer_init(&ctx->tmr, clockid, HRTIMER_MODE_ABS);
284         ctx->moffs = ktime_get_monotonic_offset();
285
286         ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx,
287                                O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
288         if (ufd < 0)
289                 kfree(ctx);
290
291         return ufd;
292 }
293
294 SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
295                 const struct itimerspec __user *, utmr,
296                 struct itimerspec __user *, otmr)
297 {
298         struct file *file;
299         struct timerfd_ctx *ctx;
300         struct itimerspec ktmr, kotmr;
301         int ret;
302
303         if (copy_from_user(&ktmr, utmr, sizeof(ktmr)))
304                 return -EFAULT;
305
306         if ((flags & ~TFD_SETTIME_FLAGS) ||
307             !timespec_valid(&ktmr.it_value) ||
308             !timespec_valid(&ktmr.it_interval))
309                 return -EINVAL;
310
311         file = timerfd_fget(ufd);
312         if (IS_ERR(file))
313                 return PTR_ERR(file);
314         ctx = file->private_data;
315
316         timerfd_setup_cancel(ctx, flags);
317
318         /*
319          * We need to stop the existing timer before reprogramming
320          * it to the new values.
321          */
322         for (;;) {
323                 spin_lock_irq(&ctx->wqh.lock);
324                 if (hrtimer_try_to_cancel(&ctx->tmr) >= 0)
325                         break;
326                 spin_unlock_irq(&ctx->wqh.lock);
327                 cpu_relax();
328         }
329
330         /*
331          * If the timer is expired and it's periodic, we need to advance it
332          * because the caller may want to know the previous expiration time.
333          * We do not update "ticks" and "expired" since the timer will be
334          * re-programmed again in the following timerfd_setup() call.
335          */
336         if (ctx->expired && ctx->tintv.tv64)
337                 hrtimer_forward_now(&ctx->tmr, ctx->tintv);
338
339         kotmr.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
340         kotmr.it_interval = ktime_to_timespec(ctx->tintv);
341
342         /*
343          * Re-program the timer to the new value ...
344          */
345         ret = timerfd_setup(ctx, flags, &ktmr);
346
347         spin_unlock_irq(&ctx->wqh.lock);
348         fput(file);
349         if (otmr && copy_to_user(otmr, &kotmr, sizeof(kotmr)))
350                 return -EFAULT;
351
352         return ret;
353 }
354
355 SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct itimerspec __user *, otmr)
356 {
357         struct file *file;
358         struct timerfd_ctx *ctx;
359         struct itimerspec kotmr;
360
361         file = timerfd_fget(ufd);
362         if (IS_ERR(file))
363                 return PTR_ERR(file);
364         ctx = file->private_data;
365
366         spin_lock_irq(&ctx->wqh.lock);
367         if (ctx->expired && ctx->tintv.tv64) {
368                 ctx->expired = 0;
369                 ctx->ticks +=
370                         hrtimer_forward_now(&ctx->tmr, ctx->tintv) - 1;
371                 hrtimer_restart(&ctx->tmr);
372         }
373         kotmr.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
374         kotmr.it_interval = ktime_to_timespec(ctx->tintv);
375         spin_unlock_irq(&ctx->wqh.lock);
376         fput(file);
377
378         return copy_to_user(otmr, &kotmr, sizeof(kotmr)) ? -EFAULT: 0;
379 }
380