blob: d03822bbf1909080ff201acc372de185d10d50d5 [file] [log] [blame]
Davide Libenzib215e282007-05-10 22:23:16 -07001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Davide Libenzib215e282007-05-10 22:23:16 -070018#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>
Adrian Bunk45cc2b92008-04-29 00:58:59 -070024#include <linux/syscalls.h>
Thomas Gleixner9ec26902011-05-20 16:18:50 +020025#include <linux/rcupdate.h>
Davide Libenzib215e282007-05-10 22:23:16 -070026
27struct timerfd_ctx {
28 struct hrtimer tmr;
29 ktime_t tintv;
Thomas Gleixner99ee5312011-04-27 14:16:42 +020030 ktime_t moffs;
Davide Libenzib215e282007-05-10 22:23:16 -070031 wait_queue_head_t wqh;
Davide Libenzi4d672e72008-02-04 22:27:26 -080032 u64 ticks;
Davide Libenzib215e282007-05-10 22:23:16 -070033 int expired;
Davide Libenzi4d672e72008-02-04 22:27:26 -080034 int clockid;
Thomas Gleixner9ec26902011-05-20 16:18:50 +020035 struct rcu_head rcu;
36 struct list_head clist;
Thomas Gleixner99ee5312011-04-27 14:16:42 +020037 bool might_cancel;
Davide Libenzib215e282007-05-10 22:23:16 -070038};
39
Thomas Gleixner9ec26902011-05-20 16:18:50 +020040static LIST_HEAD(cancel_list);
41static DEFINE_SPINLOCK(cancel_lock);
42
Davide Libenzib215e282007-05-10 22:23:16 -070043/*
44 * This gets called when the timer event triggers. We set the "expired"
45 * flag, but we do not re-arm the timer (in case it's necessary,
Davide Libenzi4d672e72008-02-04 22:27:26 -080046 * tintv.tv64 != 0) until the timer is accessed.
Davide Libenzib215e282007-05-10 22:23:16 -070047 */
48static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr)
49{
50 struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx, tmr);
51 unsigned long flags;
52
Davide Libenzi18963c02007-05-18 12:02:33 -070053 spin_lock_irqsave(&ctx->wqh.lock, flags);
Davide Libenzib215e282007-05-10 22:23:16 -070054 ctx->expired = 1;
Davide Libenzi4d672e72008-02-04 22:27:26 -080055 ctx->ticks++;
Davide Libenzib215e282007-05-10 22:23:16 -070056 wake_up_locked(&ctx->wqh);
Davide Libenzi18963c02007-05-18 12:02:33 -070057 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
Davide Libenzib215e282007-05-10 22:23:16 -070058
59 return HRTIMER_NORESTART;
60}
61
Thomas Gleixner9ec26902011-05-20 16:18:50 +020062/*
63 * Called when the clock was set to cancel the timers in the cancel
Max Asbock1123d9392011-06-13 10:18:32 -070064 * list. This will wake up processes waiting on these timers. The
65 * wake-up requires ctx->ticks to be non zero, therefore we increment
66 * it before calling wake_up_locked().
Thomas Gleixner9ec26902011-05-20 16:18:50 +020067 */
68void timerfd_clock_was_set(void)
69{
70 ktime_t moffs = ktime_get_monotonic_offset();
71 struct timerfd_ctx *ctx;
72 unsigned long flags;
73
74 rcu_read_lock();
75 list_for_each_entry_rcu(ctx, &cancel_list, clist) {
76 if (!ctx->might_cancel)
77 continue;
78 spin_lock_irqsave(&ctx->wqh.lock, flags);
79 if (ctx->moffs.tv64 != moffs.tv64) {
80 ctx->moffs.tv64 = KTIME_MAX;
Max Asbock1123d9392011-06-13 10:18:32 -070081 ctx->ticks++;
Thomas Gleixner9ec26902011-05-20 16:18:50 +020082 wake_up_locked(&ctx->wqh);
83 }
84 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
85 }
86 rcu_read_unlock();
87}
88
89static void timerfd_remove_cancel(struct timerfd_ctx *ctx)
90{
91 if (ctx->might_cancel) {
92 ctx->might_cancel = false;
93 spin_lock(&cancel_lock);
94 list_del_rcu(&ctx->clist);
95 spin_unlock(&cancel_lock);
96 }
97}
98
99static bool timerfd_canceled(struct timerfd_ctx *ctx)
100{
101 if (!ctx->might_cancel || ctx->moffs.tv64 != KTIME_MAX)
102 return false;
103 ctx->moffs = ktime_get_monotonic_offset();
104 return true;
105}
106
107static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags)
108{
109 if (ctx->clockid == CLOCK_REALTIME && (flags & TFD_TIMER_ABSTIME) &&
110 (flags & TFD_TIMER_CANCEL_ON_SET)) {
111 if (!ctx->might_cancel) {
112 ctx->might_cancel = true;
113 spin_lock(&cancel_lock);
114 list_add_rcu(&ctx->clist, &cancel_list);
115 spin_unlock(&cancel_lock);
116 }
117 } else if (ctx->might_cancel) {
118 timerfd_remove_cancel(ctx);
119 }
120}
121
Davide Libenzi4d672e72008-02-04 22:27:26 -0800122static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
123{
Arjan van de Ven76369472008-09-01 15:00:14 -0700124 ktime_t remaining;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800125
Arjan van de Ven76369472008-09-01 15:00:14 -0700126 remaining = hrtimer_expires_remaining(&ctx->tmr);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800127 return remaining.tv64 < 0 ? ktime_set(0, 0): remaining;
128}
129
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200130static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
131 const struct itimerspec *ktmr)
Davide Libenzib215e282007-05-10 22:23:16 -0700132{
133 enum hrtimer_mode htmode;
134 ktime_t texp;
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200135 int clockid = ctx->clockid;
Davide Libenzib215e282007-05-10 22:23:16 -0700136
137 htmode = (flags & TFD_TIMER_ABSTIME) ?
138 HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
139
140 texp = timespec_to_ktime(ktmr->it_value);
141 ctx->expired = 0;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800142 ctx->ticks = 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700143 ctx->tintv = timespec_to_ktime(ktmr->it_interval);
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200144 hrtimer_init(&ctx->tmr, clockid, htmode);
Arjan van de Ven76369472008-09-01 15:00:14 -0700145 hrtimer_set_expires(&ctx->tmr, texp);
Davide Libenzib215e282007-05-10 22:23:16 -0700146 ctx->tmr.function = timerfd_tmrproc;
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200147 if (texp.tv64 != 0) {
Davide Libenzib215e282007-05-10 22:23:16 -0700148 hrtimer_start(&ctx->tmr, texp, htmode);
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200149 if (timerfd_canceled(ctx))
150 return -ECANCELED;
151 }
152 return 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700153}
154
155static int timerfd_release(struct inode *inode, struct file *file)
156{
157 struct timerfd_ctx *ctx = file->private_data;
158
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200159 timerfd_remove_cancel(ctx);
Davide Libenzib215e282007-05-10 22:23:16 -0700160 hrtimer_cancel(&ctx->tmr);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200161 kfree_rcu(ctx, rcu);
Davide Libenzib215e282007-05-10 22:23:16 -0700162 return 0;
163}
164
165static unsigned int timerfd_poll(struct file *file, poll_table *wait)
166{
167 struct timerfd_ctx *ctx = file->private_data;
168 unsigned int events = 0;
169 unsigned long flags;
170
171 poll_wait(file, &ctx->wqh, wait);
172
Davide Libenzi18963c02007-05-18 12:02:33 -0700173 spin_lock_irqsave(&ctx->wqh.lock, flags);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800174 if (ctx->ticks)
Davide Libenzib215e282007-05-10 22:23:16 -0700175 events |= POLLIN;
Davide Libenzi18963c02007-05-18 12:02:33 -0700176 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
Davide Libenzib215e282007-05-10 22:23:16 -0700177
178 return events;
179}
180
181static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
182 loff_t *ppos)
183{
184 struct timerfd_ctx *ctx = file->private_data;
185 ssize_t res;
Davide Libenzi09828402007-07-26 10:41:07 -0700186 u64 ticks = 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700187
188 if (count < sizeof(ticks))
189 return -EINVAL;
Davide Libenzi18963c02007-05-18 12:02:33 -0700190 spin_lock_irq(&ctx->wqh.lock);
Michal Nazarewicz8120a8a2010-05-05 12:53:12 +0200191 if (file->f_flags & O_NONBLOCK)
192 res = -EAGAIN;
193 else
194 res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200195
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200196 /*
197 * If clock has changed, we do not care about the
198 * ticks and we do not rearm the timer. Userspace must
199 * reevaluate anyway.
200 */
201 if (timerfd_canceled(ctx)) {
202 ctx->ticks = 0;
203 ctx->expired = 0;
204 res = -ECANCELED;
205 }
206
Davide Libenzi4d672e72008-02-04 22:27:26 -0800207 if (ctx->ticks) {
208 ticks = ctx->ticks;
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200209
Davide Libenzi4d672e72008-02-04 22:27:26 -0800210 if (ctx->expired && ctx->tintv.tv64) {
Davide Libenzib215e282007-05-10 22:23:16 -0700211 /*
212 * If tintv.tv64 != 0, this is a periodic timer that
213 * needs to be re-armed. We avoid doing it in the timer
214 * callback to avoid DoS attacks specifying a very
215 * short timer period.
216 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800217 ticks += hrtimer_forward_now(&ctx->tmr,
218 ctx->tintv) - 1;
Davide Libenzib215e282007-05-10 22:23:16 -0700219 hrtimer_restart(&ctx->tmr);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800220 }
221 ctx->expired = 0;
222 ctx->ticks = 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700223 }
Davide Libenzi18963c02007-05-18 12:02:33 -0700224 spin_unlock_irq(&ctx->wqh.lock);
Davide Libenzib215e282007-05-10 22:23:16 -0700225 if (ticks)
Davide Libenzi09828402007-07-26 10:41:07 -0700226 res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
Davide Libenzib215e282007-05-10 22:23:16 -0700227 return res;
228}
229
230static const struct file_operations timerfd_fops = {
231 .release = timerfd_release,
232 .poll = timerfd_poll,
233 .read = timerfd_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200234 .llseek = noop_llseek,
Davide Libenzib215e282007-05-10 22:23:16 -0700235};
236
Al Viro2903ff02012-08-28 12:52:22 -0400237static int timerfd_fget(int fd, struct fd *p)
Davide Libenzib215e282007-05-10 22:23:16 -0700238{
Al Viro2903ff02012-08-28 12:52:22 -0400239 struct fd f = fdget(fd);
240 if (!f.file)
241 return -EBADF;
242 if (f.file->f_op != &timerfd_fops) {
243 fdput(f);
244 return -EINVAL;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800245 }
Al Viro2903ff02012-08-28 12:52:22 -0400246 *p = f;
247 return 0;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800248}
249
Heiko Carstens836f92a2009-01-14 14:14:33 +0100250SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800251{
Al Viro2030a422008-02-23 06:46:49 -0500252 int ufd;
Davide Libenzib215e282007-05-10 22:23:16 -0700253 struct timerfd_ctx *ctx;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800254
Ulrich Dreppere38b36f2008-07-23 21:29:42 -0700255 /* Check the TFD_* constants for consistency. */
256 BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
257 BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
258
Davide Libenzi610d18f2009-02-18 14:48:18 -0800259 if ((flags & ~TFD_CREATE_FLAGS) ||
260 (clockid != CLOCK_MONOTONIC &&
261 clockid != CLOCK_REALTIME))
Davide Libenzi4d672e72008-02-04 22:27:26 -0800262 return -EINVAL;
263
264 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
265 if (!ctx)
266 return -ENOMEM;
267
268 init_waitqueue_head(&ctx->wqh);
269 ctx->clockid = clockid;
270 hrtimer_init(&ctx->tmr, clockid, HRTIMER_MODE_ABS);
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200271 ctx->moffs = ktime_get_monotonic_offset();
Davide Libenzi4d672e72008-02-04 22:27:26 -0800272
Ulrich Drepper11fcb6c2008-07-23 21:29:26 -0700273 ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx,
Roland Dreier628ff7c2009-12-18 09:41:24 -0800274 O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
Al Viro2030a422008-02-23 06:46:49 -0500275 if (ufd < 0)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800276 kfree(ctx);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800277
278 return ufd;
279}
280
Heiko Carstens836f92a2009-01-14 14:14:33 +0100281SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
282 const struct itimerspec __user *, utmr,
283 struct itimerspec __user *, otmr)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800284{
Al Viro2903ff02012-08-28 12:52:22 -0400285 struct fd f;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800286 struct timerfd_ctx *ctx;
287 struct itimerspec ktmr, kotmr;
Al Viro2903ff02012-08-28 12:52:22 -0400288 int ret;
Davide Libenzib215e282007-05-10 22:23:16 -0700289
290 if (copy_from_user(&ktmr, utmr, sizeof(ktmr)))
291 return -EFAULT;
292
Davide Libenzi610d18f2009-02-18 14:48:18 -0800293 if ((flags & ~TFD_SETTIME_FLAGS) ||
294 !timespec_valid(&ktmr.it_value) ||
Davide Libenzib215e282007-05-10 22:23:16 -0700295 !timespec_valid(&ktmr.it_interval))
296 return -EINVAL;
297
Al Viro2903ff02012-08-28 12:52:22 -0400298 ret = timerfd_fget(ufd, &f);
299 if (ret)
300 return ret;
301 ctx = f.file->private_data;
Davide Libenzib215e282007-05-10 22:23:16 -0700302
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200303 timerfd_setup_cancel(ctx, flags);
304
Davide Libenzi4d672e72008-02-04 22:27:26 -0800305 /*
306 * We need to stop the existing timer before reprogramming
307 * it to the new values.
308 */
309 for (;;) {
310 spin_lock_irq(&ctx->wqh.lock);
311 if (hrtimer_try_to_cancel(&ctx->tmr) >= 0)
312 break;
Davide Libenzi18963c02007-05-18 12:02:33 -0700313 spin_unlock_irq(&ctx->wqh.lock);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800314 cpu_relax();
Davide Libenzib215e282007-05-10 22:23:16 -0700315 }
316
Davide Libenzi4d672e72008-02-04 22:27:26 -0800317 /*
318 * If the timer is expired and it's periodic, we need to advance it
319 * because the caller may want to know the previous expiration time.
320 * We do not update "ticks" and "expired" since the timer will be
321 * re-programmed again in the following timerfd_setup() call.
322 */
323 if (ctx->expired && ctx->tintv.tv64)
324 hrtimer_forward_now(&ctx->tmr, ctx->tintv);
Davide Libenzib215e282007-05-10 22:23:16 -0700325
Davide Libenzi4d672e72008-02-04 22:27:26 -0800326 kotmr.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
327 kotmr.it_interval = ktime_to_timespec(ctx->tintv);
328
329 /*
330 * Re-program the timer to the new value ...
331 */
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200332 ret = timerfd_setup(ctx, flags, &ktmr);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800333
334 spin_unlock_irq(&ctx->wqh.lock);
Al Viro2903ff02012-08-28 12:52:22 -0400335 fdput(f);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800336 if (otmr && copy_to_user(otmr, &kotmr, sizeof(kotmr)))
337 return -EFAULT;
338
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200339 return ret;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800340}
341
Heiko Carstensd4e82042009-01-14 14:14:34 +0100342SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct itimerspec __user *, otmr)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800343{
Al Viro2903ff02012-08-28 12:52:22 -0400344 struct fd f;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800345 struct timerfd_ctx *ctx;
346 struct itimerspec kotmr;
Al Viro2903ff02012-08-28 12:52:22 -0400347 int ret = timerfd_fget(ufd, &f);
348 if (ret)
349 return ret;
350 ctx = f.file->private_data;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800351
352 spin_lock_irq(&ctx->wqh.lock);
353 if (ctx->expired && ctx->tintv.tv64) {
354 ctx->expired = 0;
355 ctx->ticks +=
356 hrtimer_forward_now(&ctx->tmr, ctx->tintv) - 1;
357 hrtimer_restart(&ctx->tmr);
358 }
359 kotmr.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
360 kotmr.it_interval = ktime_to_timespec(ctx->tintv);
361 spin_unlock_irq(&ctx->wqh.lock);
Al Viro2903ff02012-08-28 12:52:22 -0400362 fdput(f);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800363
364 return copy_to_user(otmr, &kotmr, sizeof(kotmr)) ? -EFAULT: 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700365}
366