blob: f67acbdda5e8c13fce54e51f72fe0882f2fa94da [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
64 * list.
65 */
66void timerfd_clock_was_set(void)
67{
68 ktime_t moffs = ktime_get_monotonic_offset();
69 struct timerfd_ctx *ctx;
70 unsigned long flags;
71
72 rcu_read_lock();
73 list_for_each_entry_rcu(ctx, &cancel_list, clist) {
74 if (!ctx->might_cancel)
75 continue;
76 spin_lock_irqsave(&ctx->wqh.lock, flags);
77 if (ctx->moffs.tv64 != moffs.tv64) {
78 ctx->moffs.tv64 = KTIME_MAX;
79 wake_up_locked(&ctx->wqh);
80 }
81 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
82 }
83 rcu_read_unlock();
84}
85
86static void timerfd_remove_cancel(struct timerfd_ctx *ctx)
87{
88 if (ctx->might_cancel) {
89 ctx->might_cancel = false;
90 spin_lock(&cancel_lock);
91 list_del_rcu(&ctx->clist);
92 spin_unlock(&cancel_lock);
93 }
94}
95
96static bool timerfd_canceled(struct timerfd_ctx *ctx)
97{
98 if (!ctx->might_cancel || ctx->moffs.tv64 != KTIME_MAX)
99 return false;
100 ctx->moffs = ktime_get_monotonic_offset();
101 return true;
102}
103
104static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags)
105{
106 if (ctx->clockid == CLOCK_REALTIME && (flags & TFD_TIMER_ABSTIME) &&
107 (flags & TFD_TIMER_CANCEL_ON_SET)) {
108 if (!ctx->might_cancel) {
109 ctx->might_cancel = true;
110 spin_lock(&cancel_lock);
111 list_add_rcu(&ctx->clist, &cancel_list);
112 spin_unlock(&cancel_lock);
113 }
114 } else if (ctx->might_cancel) {
115 timerfd_remove_cancel(ctx);
116 }
117}
118
Davide Libenzi4d672e72008-02-04 22:27:26 -0800119static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
120{
Arjan van de Ven76369472008-09-01 15:00:14 -0700121 ktime_t remaining;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800122
Arjan van de Ven76369472008-09-01 15:00:14 -0700123 remaining = hrtimer_expires_remaining(&ctx->tmr);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800124 return remaining.tv64 < 0 ? ktime_set(0, 0): remaining;
125}
126
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200127static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
128 const struct itimerspec *ktmr)
Davide Libenzib215e282007-05-10 22:23:16 -0700129{
130 enum hrtimer_mode htmode;
131 ktime_t texp;
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200132 int clockid = ctx->clockid;
Davide Libenzib215e282007-05-10 22:23:16 -0700133
134 htmode = (flags & TFD_TIMER_ABSTIME) ?
135 HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
136
137 texp = timespec_to_ktime(ktmr->it_value);
138 ctx->expired = 0;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800139 ctx->ticks = 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700140 ctx->tintv = timespec_to_ktime(ktmr->it_interval);
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200141 hrtimer_init(&ctx->tmr, clockid, htmode);
Arjan van de Ven76369472008-09-01 15:00:14 -0700142 hrtimer_set_expires(&ctx->tmr, texp);
Davide Libenzib215e282007-05-10 22:23:16 -0700143 ctx->tmr.function = timerfd_tmrproc;
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200144 if (texp.tv64 != 0) {
Davide Libenzib215e282007-05-10 22:23:16 -0700145 hrtimer_start(&ctx->tmr, texp, htmode);
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200146 if (timerfd_canceled(ctx))
147 return -ECANCELED;
148 }
149 return 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700150}
151
152static int timerfd_release(struct inode *inode, struct file *file)
153{
154 struct timerfd_ctx *ctx = file->private_data;
155
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200156 timerfd_remove_cancel(ctx);
Davide Libenzib215e282007-05-10 22:23:16 -0700157 hrtimer_cancel(&ctx->tmr);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200158 kfree_rcu(ctx, rcu);
Davide Libenzib215e282007-05-10 22:23:16 -0700159 return 0;
160}
161
162static unsigned int timerfd_poll(struct file *file, poll_table *wait)
163{
164 struct timerfd_ctx *ctx = file->private_data;
165 unsigned int events = 0;
166 unsigned long flags;
167
168 poll_wait(file, &ctx->wqh, wait);
169
Davide Libenzi18963c02007-05-18 12:02:33 -0700170 spin_lock_irqsave(&ctx->wqh.lock, flags);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800171 if (ctx->ticks)
Davide Libenzib215e282007-05-10 22:23:16 -0700172 events |= POLLIN;
Davide Libenzi18963c02007-05-18 12:02:33 -0700173 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
Davide Libenzib215e282007-05-10 22:23:16 -0700174
175 return events;
176}
177
178static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
179 loff_t *ppos)
180{
181 struct timerfd_ctx *ctx = file->private_data;
182 ssize_t res;
Davide Libenzi09828402007-07-26 10:41:07 -0700183 u64 ticks = 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700184
185 if (count < sizeof(ticks))
186 return -EINVAL;
Davide Libenzi18963c02007-05-18 12:02:33 -0700187 spin_lock_irq(&ctx->wqh.lock);
Michal Nazarewicz8120a8a2010-05-05 12:53:12 +0200188 if (file->f_flags & O_NONBLOCK)
189 res = -EAGAIN;
190 else
191 res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200192
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200193 /*
194 * If clock has changed, we do not care about the
195 * ticks and we do not rearm the timer. Userspace must
196 * reevaluate anyway.
197 */
198 if (timerfd_canceled(ctx)) {
199 ctx->ticks = 0;
200 ctx->expired = 0;
201 res = -ECANCELED;
202 }
203
Davide Libenzi4d672e72008-02-04 22:27:26 -0800204 if (ctx->ticks) {
205 ticks = ctx->ticks;
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200206
Davide Libenzi4d672e72008-02-04 22:27:26 -0800207 if (ctx->expired && ctx->tintv.tv64) {
Davide Libenzib215e282007-05-10 22:23:16 -0700208 /*
209 * If tintv.tv64 != 0, this is a periodic timer that
210 * needs to be re-armed. We avoid doing it in the timer
211 * callback to avoid DoS attacks specifying a very
212 * short timer period.
213 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800214 ticks += hrtimer_forward_now(&ctx->tmr,
215 ctx->tintv) - 1;
Davide Libenzib215e282007-05-10 22:23:16 -0700216 hrtimer_restart(&ctx->tmr);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800217 }
218 ctx->expired = 0;
219 ctx->ticks = 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700220 }
Davide Libenzi18963c02007-05-18 12:02:33 -0700221 spin_unlock_irq(&ctx->wqh.lock);
Davide Libenzib215e282007-05-10 22:23:16 -0700222 if (ticks)
Davide Libenzi09828402007-07-26 10:41:07 -0700223 res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
Davide Libenzib215e282007-05-10 22:23:16 -0700224 return res;
225}
226
227static const struct file_operations timerfd_fops = {
228 .release = timerfd_release,
229 .poll = timerfd_poll,
230 .read = timerfd_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200231 .llseek = noop_llseek,
Davide Libenzib215e282007-05-10 22:23:16 -0700232};
233
Davide Libenzi4d672e72008-02-04 22:27:26 -0800234static struct file *timerfd_fget(int fd)
Davide Libenzib215e282007-05-10 22:23:16 -0700235{
Davide Libenzi4d672e72008-02-04 22:27:26 -0800236 struct file *file;
237
238 file = fget(fd);
239 if (!file)
240 return ERR_PTR(-EBADF);
241 if (file->f_op != &timerfd_fops) {
242 fput(file);
243 return ERR_PTR(-EINVAL);
244 }
245
246 return file;
247}
248
Heiko Carstens836f92a2009-01-14 14:14:33 +0100249SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800250{
Al Viro2030a422008-02-23 06:46:49 -0500251 int ufd;
Davide Libenzib215e282007-05-10 22:23:16 -0700252 struct timerfd_ctx *ctx;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800253
Ulrich Dreppere38b36f2008-07-23 21:29:42 -0700254 /* Check the TFD_* constants for consistency. */
255 BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
256 BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
257
Davide Libenzi610d18f2009-02-18 14:48:18 -0800258 if ((flags & ~TFD_CREATE_FLAGS) ||
259 (clockid != CLOCK_MONOTONIC &&
260 clockid != CLOCK_REALTIME))
Davide Libenzi4d672e72008-02-04 22:27:26 -0800261 return -EINVAL;
262
263 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
264 if (!ctx)
265 return -ENOMEM;
266
267 init_waitqueue_head(&ctx->wqh);
268 ctx->clockid = clockid;
269 hrtimer_init(&ctx->tmr, clockid, HRTIMER_MODE_ABS);
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200270 ctx->moffs = ktime_get_monotonic_offset();
Davide Libenzi4d672e72008-02-04 22:27:26 -0800271
Ulrich Drepper11fcb6c2008-07-23 21:29:26 -0700272 ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx,
Roland Dreier628ff7c2009-12-18 09:41:24 -0800273 O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
Al Viro2030a422008-02-23 06:46:49 -0500274 if (ufd < 0)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800275 kfree(ctx);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800276
277 return ufd;
278}
279
Heiko Carstens836f92a2009-01-14 14:14:33 +0100280SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
281 const struct itimerspec __user *, utmr,
282 struct itimerspec __user *, otmr)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800283{
284 struct file *file;
285 struct timerfd_ctx *ctx;
286 struct itimerspec ktmr, kotmr;
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200287 int ret;
Davide Libenzib215e282007-05-10 22:23:16 -0700288
289 if (copy_from_user(&ktmr, utmr, sizeof(ktmr)))
290 return -EFAULT;
291
Davide Libenzi610d18f2009-02-18 14:48:18 -0800292 if ((flags & ~TFD_SETTIME_FLAGS) ||
293 !timespec_valid(&ktmr.it_value) ||
Davide Libenzib215e282007-05-10 22:23:16 -0700294 !timespec_valid(&ktmr.it_interval))
295 return -EINVAL;
296
Davide Libenzi4d672e72008-02-04 22:27:26 -0800297 file = timerfd_fget(ufd);
298 if (IS_ERR(file))
299 return PTR_ERR(file);
300 ctx = file->private_data;
Davide Libenzib215e282007-05-10 22:23:16 -0700301
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200302 timerfd_setup_cancel(ctx, flags);
303
Davide Libenzi4d672e72008-02-04 22:27:26 -0800304 /*
305 * We need to stop the existing timer before reprogramming
306 * it to the new values.
307 */
308 for (;;) {
309 spin_lock_irq(&ctx->wqh.lock);
310 if (hrtimer_try_to_cancel(&ctx->tmr) >= 0)
311 break;
Davide Libenzi18963c02007-05-18 12:02:33 -0700312 spin_unlock_irq(&ctx->wqh.lock);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800313 cpu_relax();
Davide Libenzib215e282007-05-10 22:23:16 -0700314 }
315
Davide Libenzi4d672e72008-02-04 22:27:26 -0800316 /*
317 * If the timer is expired and it's periodic, we need to advance it
318 * because the caller may want to know the previous expiration time.
319 * We do not update "ticks" and "expired" since the timer will be
320 * re-programmed again in the following timerfd_setup() call.
321 */
322 if (ctx->expired && ctx->tintv.tv64)
323 hrtimer_forward_now(&ctx->tmr, ctx->tintv);
Davide Libenzib215e282007-05-10 22:23:16 -0700324
Davide Libenzi4d672e72008-02-04 22:27:26 -0800325 kotmr.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
326 kotmr.it_interval = ktime_to_timespec(ctx->tintv);
327
328 /*
329 * Re-program the timer to the new value ...
330 */
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200331 ret = timerfd_setup(ctx, flags, &ktmr);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800332
333 spin_unlock_irq(&ctx->wqh.lock);
334 fput(file);
335 if (otmr && copy_to_user(otmr, &kotmr, sizeof(kotmr)))
336 return -EFAULT;
337
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200338 return ret;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800339}
340
Heiko Carstensd4e82042009-01-14 14:14:34 +0100341SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct itimerspec __user *, otmr)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800342{
343 struct file *file;
344 struct timerfd_ctx *ctx;
345 struct itimerspec kotmr;
346
347 file = timerfd_fget(ufd);
348 if (IS_ERR(file))
349 return PTR_ERR(file);
350 ctx = file->private_data;
351
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);
362 fput(file);
363
364 return copy_to_user(otmr, &kotmr, sizeof(kotmr)) ? -EFAULT: 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700365}
366