]> Pileus Git - ~andy/linux/blob - fs/timerfd.c
timerfd: Allow timers to be cancelled when clock was set
[~andy/linux] / 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
26 struct timerfd_ctx {
27         struct hrtimer tmr;
28         ktime_t tintv;
29         ktime_t moffs;
30         wait_queue_head_t wqh;
31         u64 ticks;
32         int expired;
33         int clockid;
34         bool might_cancel;
35 };
36
37 /*
38  * This gets called when the timer event triggers. We set the "expired"
39  * flag, but we do not re-arm the timer (in case it's necessary,
40  * tintv.tv64 != 0) until the timer is accessed.
41  */
42 static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr)
43 {
44         struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx, tmr);
45         unsigned long flags;
46
47         spin_lock_irqsave(&ctx->wqh.lock, flags);
48         ctx->expired = 1;
49         ctx->ticks++;
50         wake_up_locked(&ctx->wqh);
51         spin_unlock_irqrestore(&ctx->wqh.lock, flags);
52
53         return HRTIMER_NORESTART;
54 }
55
56 static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
57 {
58         ktime_t remaining;
59
60         remaining = hrtimer_expires_remaining(&ctx->tmr);
61         return remaining.tv64 < 0 ? ktime_set(0, 0): remaining;
62 }
63
64 static bool timerfd_canceled(struct timerfd_ctx *ctx)
65 {
66         ktime_t moffs;
67
68         if (!ctx->might_cancel)
69                 return false;
70
71         moffs = ktime_get_monotonic_offset();
72
73         if (moffs.tv64 == ctx->moffs.tv64)
74                 return false;
75
76         ctx->moffs = moffs;
77         return true;
78 }
79
80 static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
81                          const struct itimerspec *ktmr)
82 {
83         enum hrtimer_mode htmode;
84         ktime_t texp;
85         int clockid = ctx->clockid;
86
87         htmode = (flags & TFD_TIMER_ABSTIME) ?
88                 HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
89
90         ctx->might_cancel = false;
91         if (htmode == HRTIMER_MODE_ABS && ctx->clockid == CLOCK_REALTIME &&
92             (flags & TFD_TIMER_CANCELON_SET)) {
93                 clockid = CLOCK_REALTIME_COS;
94                 ctx->might_cancel = true;
95         }
96
97         texp = timespec_to_ktime(ktmr->it_value);
98         ctx->expired = 0;
99         ctx->ticks = 0;
100         ctx->tintv = timespec_to_ktime(ktmr->it_interval);
101         hrtimer_init(&ctx->tmr, clockid, htmode);
102         hrtimer_set_expires(&ctx->tmr, texp);
103         ctx->tmr.function = timerfd_tmrproc;
104         if (texp.tv64 != 0) {
105                 hrtimer_start(&ctx->tmr, texp, htmode);
106                 if (timerfd_canceled(ctx))
107                         return -ECANCELED;
108         }
109         return 0;
110 }
111
112 static int timerfd_release(struct inode *inode, struct file *file)
113 {
114         struct timerfd_ctx *ctx = file->private_data;
115
116         hrtimer_cancel(&ctx->tmr);
117         kfree(ctx);
118         return 0;
119 }
120
121 static unsigned int timerfd_poll(struct file *file, poll_table *wait)
122 {
123         struct timerfd_ctx *ctx = file->private_data;
124         unsigned int events = 0;
125         unsigned long flags;
126
127         poll_wait(file, &ctx->wqh, wait);
128
129         spin_lock_irqsave(&ctx->wqh.lock, flags);
130         if (ctx->ticks)
131                 events |= POLLIN;
132         spin_unlock_irqrestore(&ctx->wqh.lock, flags);
133
134         return events;
135 }
136
137 static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
138                             loff_t *ppos)
139 {
140         struct timerfd_ctx *ctx = file->private_data;
141         ssize_t res;
142         u64 ticks = 0;
143
144         if (count < sizeof(ticks))
145                 return -EINVAL;
146         spin_lock_irq(&ctx->wqh.lock);
147         if (file->f_flags & O_NONBLOCK)
148                 res = -EAGAIN;
149         else
150                 res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
151
152         if (ctx->ticks) {
153                 ticks = ctx->ticks;
154
155                 /*
156                  * If clock has changed, we do not care about the
157                  * ticks and we do not rearm the timer. Userspace must
158                  * reevaluate anyway.
159                  */
160                 if (timerfd_canceled(ctx)) {
161                         ticks = 0;
162                         ctx->expired = 0;
163                         res = -ECANCELED;
164                 }
165
166                 if (ctx->expired && ctx->tintv.tv64) {
167                         /*
168                          * If tintv.tv64 != 0, this is a periodic timer that
169                          * needs to be re-armed. We avoid doing it in the timer
170                          * callback to avoid DoS attacks specifying a very
171                          * short timer period.
172                          */
173                         ticks += hrtimer_forward_now(&ctx->tmr,
174                                                      ctx->tintv) - 1;
175                         hrtimer_restart(&ctx->tmr);
176                 }
177                 ctx->expired = 0;
178                 ctx->ticks = 0;
179         }
180         spin_unlock_irq(&ctx->wqh.lock);
181         if (ticks)
182                 res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
183         return res;
184 }
185
186 static const struct file_operations timerfd_fops = {
187         .release        = timerfd_release,
188         .poll           = timerfd_poll,
189         .read           = timerfd_read,
190         .llseek         = noop_llseek,
191 };
192
193 static struct file *timerfd_fget(int fd)
194 {
195         struct file *file;
196
197         file = fget(fd);
198         if (!file)
199                 return ERR_PTR(-EBADF);
200         if (file->f_op != &timerfd_fops) {
201                 fput(file);
202                 return ERR_PTR(-EINVAL);
203         }
204
205         return file;
206 }
207
208 SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
209 {
210         int ufd;
211         struct timerfd_ctx *ctx;
212
213         /* Check the TFD_* constants for consistency.  */
214         BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
215         BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
216
217         if ((flags & ~TFD_CREATE_FLAGS) ||
218             (clockid != CLOCK_MONOTONIC &&
219              clockid != CLOCK_REALTIME))
220                 return -EINVAL;
221
222         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
223         if (!ctx)
224                 return -ENOMEM;
225
226         init_waitqueue_head(&ctx->wqh);
227         ctx->clockid = clockid;
228         hrtimer_init(&ctx->tmr, clockid, HRTIMER_MODE_ABS);
229         ctx->moffs = ktime_get_monotonic_offset();
230
231         ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx,
232                                O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
233         if (ufd < 0)
234                 kfree(ctx);
235
236         return ufd;
237 }
238
239 SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
240                 const struct itimerspec __user *, utmr,
241                 struct itimerspec __user *, otmr)
242 {
243         struct file *file;
244         struct timerfd_ctx *ctx;
245         struct itimerspec ktmr, kotmr;
246         int ret;
247
248         if (copy_from_user(&ktmr, utmr, sizeof(ktmr)))
249                 return -EFAULT;
250
251         if ((flags & ~TFD_SETTIME_FLAGS) ||
252             !timespec_valid(&ktmr.it_value) ||
253             !timespec_valid(&ktmr.it_interval))
254                 return -EINVAL;
255
256         file = timerfd_fget(ufd);
257         if (IS_ERR(file))
258                 return PTR_ERR(file);
259         ctx = file->private_data;
260
261         /*
262          * We need to stop the existing timer before reprogramming
263          * it to the new values.
264          */
265         for (;;) {
266                 spin_lock_irq(&ctx->wqh.lock);
267                 if (hrtimer_try_to_cancel(&ctx->tmr) >= 0)
268                         break;
269                 spin_unlock_irq(&ctx->wqh.lock);
270                 cpu_relax();
271         }
272
273         /*
274          * If the timer is expired and it's periodic, we need to advance it
275          * because the caller may want to know the previous expiration time.
276          * We do not update "ticks" and "expired" since the timer will be
277          * re-programmed again in the following timerfd_setup() call.
278          */
279         if (ctx->expired && ctx->tintv.tv64)
280                 hrtimer_forward_now(&ctx->tmr, ctx->tintv);
281
282         kotmr.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
283         kotmr.it_interval = ktime_to_timespec(ctx->tintv);
284
285         /*
286          * Re-program the timer to the new value ...
287          */
288         ret = timerfd_setup(ctx, flags, &ktmr);
289
290         spin_unlock_irq(&ctx->wqh.lock);
291         fput(file);
292         if (otmr && copy_to_user(otmr, &kotmr, sizeof(kotmr)))
293                 return -EFAULT;
294
295         return ret;
296 }
297
298 SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct itimerspec __user *, otmr)
299 {
300         struct file *file;
301         struct timerfd_ctx *ctx;
302         struct itimerspec kotmr;
303
304         file = timerfd_fget(ufd);
305         if (IS_ERR(file))
306                 return PTR_ERR(file);
307         ctx = file->private_data;
308
309         spin_lock_irq(&ctx->wqh.lock);
310         if (ctx->expired && ctx->tintv.tv64) {
311                 ctx->expired = 0;
312                 ctx->ticks +=
313                         hrtimer_forward_now(&ctx->tmr, ctx->tintv) - 1;
314                 hrtimer_restart(&ctx->tmr);
315         }
316         kotmr.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
317         kotmr.it_interval = ktime_to_timespec(ctx->tintv);
318         spin_unlock_irq(&ctx->wqh.lock);
319         fput(file);
320
321         return copy_to_user(otmr, &kotmr, sizeof(kotmr)) ? -EFAULT: 0;
322 }
323