blob: 7e14c9e7c4ee97b8a15700d463846777a9592ac8 [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>
Davide Libenzib215e282007-05-10 22:23:16 -070025
26struct timerfd_ctx {
27 struct hrtimer tmr;
28 ktime_t tintv;
Thomas Gleixner99ee5312011-04-27 14:16:42 +020029 ktime_t moffs;
Davide Libenzib215e282007-05-10 22:23:16 -070030 wait_queue_head_t wqh;
Davide Libenzi4d672e72008-02-04 22:27:26 -080031 u64 ticks;
Davide Libenzib215e282007-05-10 22:23:16 -070032 int expired;
Davide Libenzi4d672e72008-02-04 22:27:26 -080033 int clockid;
Thomas Gleixner99ee5312011-04-27 14:16:42 +020034 bool might_cancel;
Davide Libenzib215e282007-05-10 22:23:16 -070035};
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,
Davide Libenzi4d672e72008-02-04 22:27:26 -080040 * tintv.tv64 != 0) until the timer is accessed.
Davide Libenzib215e282007-05-10 22:23:16 -070041 */
42static 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
Davide Libenzi18963c02007-05-18 12:02:33 -070047 spin_lock_irqsave(&ctx->wqh.lock, flags);
Davide Libenzib215e282007-05-10 22:23:16 -070048 ctx->expired = 1;
Davide Libenzi4d672e72008-02-04 22:27:26 -080049 ctx->ticks++;
Davide Libenzib215e282007-05-10 22:23:16 -070050 wake_up_locked(&ctx->wqh);
Davide Libenzi18963c02007-05-18 12:02:33 -070051 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
Davide Libenzib215e282007-05-10 22:23:16 -070052
53 return HRTIMER_NORESTART;
54}
55
Davide Libenzi4d672e72008-02-04 22:27:26 -080056static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
57{
Arjan van de Ven76369472008-09-01 15:00:14 -070058 ktime_t remaining;
Davide Libenzi4d672e72008-02-04 22:27:26 -080059
Arjan van de Ven76369472008-09-01 15:00:14 -070060 remaining = hrtimer_expires_remaining(&ctx->tmr);
Davide Libenzi4d672e72008-02-04 22:27:26 -080061 return remaining.tv64 < 0 ? ktime_set(0, 0): remaining;
62}
63
Thomas Gleixner99ee5312011-04-27 14:16:42 +020064static 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
80static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
81 const struct itimerspec *ktmr)
Davide Libenzib215e282007-05-10 22:23:16 -070082{
83 enum hrtimer_mode htmode;
84 ktime_t texp;
Thomas Gleixner99ee5312011-04-27 14:16:42 +020085 int clockid = ctx->clockid;
Davide Libenzib215e282007-05-10 22:23:16 -070086
87 htmode = (flags & TFD_TIMER_ABSTIME) ?
88 HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
89
Thomas Gleixner99ee5312011-04-27 14:16:42 +020090 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
Davide Libenzib215e282007-05-10 22:23:16 -070097 texp = timespec_to_ktime(ktmr->it_value);
98 ctx->expired = 0;
Davide Libenzi4d672e72008-02-04 22:27:26 -080099 ctx->ticks = 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700100 ctx->tintv = timespec_to_ktime(ktmr->it_interval);
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200101 hrtimer_init(&ctx->tmr, clockid, htmode);
Arjan van de Ven76369472008-09-01 15:00:14 -0700102 hrtimer_set_expires(&ctx->tmr, texp);
Davide Libenzib215e282007-05-10 22:23:16 -0700103 ctx->tmr.function = timerfd_tmrproc;
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200104 if (texp.tv64 != 0) {
Davide Libenzib215e282007-05-10 22:23:16 -0700105 hrtimer_start(&ctx->tmr, texp, htmode);
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200106 if (timerfd_canceled(ctx))
107 return -ECANCELED;
108 }
109 return 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700110}
111
112static 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
121static 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
Davide Libenzi18963c02007-05-18 12:02:33 -0700129 spin_lock_irqsave(&ctx->wqh.lock, flags);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800130 if (ctx->ticks)
Davide Libenzib215e282007-05-10 22:23:16 -0700131 events |= POLLIN;
Davide Libenzi18963c02007-05-18 12:02:33 -0700132 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
Davide Libenzib215e282007-05-10 22:23:16 -0700133
134 return events;
135}
136
137static 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;
Davide Libenzi09828402007-07-26 10:41:07 -0700142 u64 ticks = 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700143
144 if (count < sizeof(ticks))
145 return -EINVAL;
Davide Libenzi18963c02007-05-18 12:02:33 -0700146 spin_lock_irq(&ctx->wqh.lock);
Michal Nazarewicz8120a8a2010-05-05 12:53:12 +0200147 if (file->f_flags & O_NONBLOCK)
148 res = -EAGAIN;
149 else
150 res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200151
Davide Libenzi4d672e72008-02-04 22:27:26 -0800152 if (ctx->ticks) {
153 ticks = ctx->ticks;
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200154
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
Davide Libenzi4d672e72008-02-04 22:27:26 -0800166 if (ctx->expired && ctx->tintv.tv64) {
Davide Libenzib215e282007-05-10 22:23:16 -0700167 /*
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 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800173 ticks += hrtimer_forward_now(&ctx->tmr,
174 ctx->tintv) - 1;
Davide Libenzib215e282007-05-10 22:23:16 -0700175 hrtimer_restart(&ctx->tmr);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800176 }
177 ctx->expired = 0;
178 ctx->ticks = 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700179 }
Davide Libenzi18963c02007-05-18 12:02:33 -0700180 spin_unlock_irq(&ctx->wqh.lock);
Davide Libenzib215e282007-05-10 22:23:16 -0700181 if (ticks)
Davide Libenzi09828402007-07-26 10:41:07 -0700182 res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
Davide Libenzib215e282007-05-10 22:23:16 -0700183 return res;
184}
185
186static const struct file_operations timerfd_fops = {
187 .release = timerfd_release,
188 .poll = timerfd_poll,
189 .read = timerfd_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200190 .llseek = noop_llseek,
Davide Libenzib215e282007-05-10 22:23:16 -0700191};
192
Davide Libenzi4d672e72008-02-04 22:27:26 -0800193static struct file *timerfd_fget(int fd)
Davide Libenzib215e282007-05-10 22:23:16 -0700194{
Davide Libenzi4d672e72008-02-04 22:27:26 -0800195 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
Heiko Carstens836f92a2009-01-14 14:14:33 +0100208SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800209{
Al Viro2030a422008-02-23 06:46:49 -0500210 int ufd;
Davide Libenzib215e282007-05-10 22:23:16 -0700211 struct timerfd_ctx *ctx;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800212
Ulrich Dreppere38b36f2008-07-23 21:29:42 -0700213 /* Check the TFD_* constants for consistency. */
214 BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
215 BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
216
Davide Libenzi610d18f2009-02-18 14:48:18 -0800217 if ((flags & ~TFD_CREATE_FLAGS) ||
218 (clockid != CLOCK_MONOTONIC &&
219 clockid != CLOCK_REALTIME))
Davide Libenzi4d672e72008-02-04 22:27:26 -0800220 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);
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200229 ctx->moffs = ktime_get_monotonic_offset();
Davide Libenzi4d672e72008-02-04 22:27:26 -0800230
Ulrich Drepper11fcb6c2008-07-23 21:29:26 -0700231 ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx,
Roland Dreier628ff7c2009-12-18 09:41:24 -0800232 O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
Al Viro2030a422008-02-23 06:46:49 -0500233 if (ufd < 0)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800234 kfree(ctx);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800235
236 return ufd;
237}
238
Heiko Carstens836f92a2009-01-14 14:14:33 +0100239SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
240 const struct itimerspec __user *, utmr,
241 struct itimerspec __user *, otmr)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800242{
243 struct file *file;
244 struct timerfd_ctx *ctx;
245 struct itimerspec ktmr, kotmr;
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200246 int ret;
Davide Libenzib215e282007-05-10 22:23:16 -0700247
248 if (copy_from_user(&ktmr, utmr, sizeof(ktmr)))
249 return -EFAULT;
250
Davide Libenzi610d18f2009-02-18 14:48:18 -0800251 if ((flags & ~TFD_SETTIME_FLAGS) ||
252 !timespec_valid(&ktmr.it_value) ||
Davide Libenzib215e282007-05-10 22:23:16 -0700253 !timespec_valid(&ktmr.it_interval))
254 return -EINVAL;
255
Davide Libenzi4d672e72008-02-04 22:27:26 -0800256 file = timerfd_fget(ufd);
257 if (IS_ERR(file))
258 return PTR_ERR(file);
259 ctx = file->private_data;
Davide Libenzib215e282007-05-10 22:23:16 -0700260
Davide Libenzi4d672e72008-02-04 22:27:26 -0800261 /*
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;
Davide Libenzi18963c02007-05-18 12:02:33 -0700269 spin_unlock_irq(&ctx->wqh.lock);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800270 cpu_relax();
Davide Libenzib215e282007-05-10 22:23:16 -0700271 }
272
Davide Libenzi4d672e72008-02-04 22:27:26 -0800273 /*
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);
Davide Libenzib215e282007-05-10 22:23:16 -0700281
Davide Libenzi4d672e72008-02-04 22:27:26 -0800282 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 */
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200288 ret = timerfd_setup(ctx, flags, &ktmr);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800289
290 spin_unlock_irq(&ctx->wqh.lock);
291 fput(file);
292 if (otmr && copy_to_user(otmr, &kotmr, sizeof(kotmr)))
293 return -EFAULT;
294
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200295 return ret;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800296}
297
Heiko Carstensd4e82042009-01-14 14:14:34 +0100298SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct itimerspec __user *, otmr)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800299{
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;
Davide Libenzib215e282007-05-10 22:23:16 -0700322}
323