blob: 8c4fc1425b3eff171bd37b9e1bed290b93473e11 [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;
Davide Libenzib215e282007-05-10 22:23:16 -070029 wait_queue_head_t wqh;
Davide Libenzi4d672e72008-02-04 22:27:26 -080030 u64 ticks;
Davide Libenzib215e282007-05-10 22:23:16 -070031 int expired;
Davide Libenzi4d672e72008-02-04 22:27:26 -080032 int clockid;
Davide Libenzib215e282007-05-10 22:23:16 -070033};
34
35/*
36 * This gets called when the timer event triggers. We set the "expired"
37 * flag, but we do not re-arm the timer (in case it's necessary,
Davide Libenzi4d672e72008-02-04 22:27:26 -080038 * tintv.tv64 != 0) until the timer is accessed.
Davide Libenzib215e282007-05-10 22:23:16 -070039 */
40static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr)
41{
42 struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx, tmr);
43 unsigned long flags;
44
Davide Libenzi18963c02007-05-18 12:02:33 -070045 spin_lock_irqsave(&ctx->wqh.lock, flags);
Davide Libenzib215e282007-05-10 22:23:16 -070046 ctx->expired = 1;
Davide Libenzi4d672e72008-02-04 22:27:26 -080047 ctx->ticks++;
Davide Libenzib215e282007-05-10 22:23:16 -070048 wake_up_locked(&ctx->wqh);
Davide Libenzi18963c02007-05-18 12:02:33 -070049 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
Davide Libenzib215e282007-05-10 22:23:16 -070050
51 return HRTIMER_NORESTART;
52}
53
Davide Libenzi4d672e72008-02-04 22:27:26 -080054static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
55{
Arjan van de Ven76369472008-09-01 15:00:14 -070056 ktime_t remaining;
Davide Libenzi4d672e72008-02-04 22:27:26 -080057
Arjan van de Ven76369472008-09-01 15:00:14 -070058 remaining = hrtimer_expires_remaining(&ctx->tmr);
Davide Libenzi4d672e72008-02-04 22:27:26 -080059 return remaining.tv64 < 0 ? ktime_set(0, 0): remaining;
60}
61
62static void timerfd_setup(struct timerfd_ctx *ctx, int flags,
Davide Libenzib215e282007-05-10 22:23:16 -070063 const struct itimerspec *ktmr)
64{
65 enum hrtimer_mode htmode;
66 ktime_t texp;
67
68 htmode = (flags & TFD_TIMER_ABSTIME) ?
69 HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
70
71 texp = timespec_to_ktime(ktmr->it_value);
72 ctx->expired = 0;
Davide Libenzi4d672e72008-02-04 22:27:26 -080073 ctx->ticks = 0;
Davide Libenzib215e282007-05-10 22:23:16 -070074 ctx->tintv = timespec_to_ktime(ktmr->it_interval);
Davide Libenzi4d672e72008-02-04 22:27:26 -080075 hrtimer_init(&ctx->tmr, ctx->clockid, htmode);
Arjan van de Ven76369472008-09-01 15:00:14 -070076 hrtimer_set_expires(&ctx->tmr, texp);
Davide Libenzib215e282007-05-10 22:23:16 -070077 ctx->tmr.function = timerfd_tmrproc;
78 if (texp.tv64 != 0)
79 hrtimer_start(&ctx->tmr, texp, htmode);
80}
81
82static int timerfd_release(struct inode *inode, struct file *file)
83{
84 struct timerfd_ctx *ctx = file->private_data;
85
86 hrtimer_cancel(&ctx->tmr);
87 kfree(ctx);
88 return 0;
89}
90
91static unsigned int timerfd_poll(struct file *file, poll_table *wait)
92{
93 struct timerfd_ctx *ctx = file->private_data;
94 unsigned int events = 0;
95 unsigned long flags;
96
97 poll_wait(file, &ctx->wqh, wait);
98
Davide Libenzi18963c02007-05-18 12:02:33 -070099 spin_lock_irqsave(&ctx->wqh.lock, flags);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800100 if (ctx->ticks)
Davide Libenzib215e282007-05-10 22:23:16 -0700101 events |= POLLIN;
Davide Libenzi18963c02007-05-18 12:02:33 -0700102 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
Davide Libenzib215e282007-05-10 22:23:16 -0700103
104 return events;
105}
106
107static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
108 loff_t *ppos)
109{
110 struct timerfd_ctx *ctx = file->private_data;
111 ssize_t res;
Davide Libenzi09828402007-07-26 10:41:07 -0700112 u64 ticks = 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700113
114 if (count < sizeof(ticks))
115 return -EINVAL;
Davide Libenzi18963c02007-05-18 12:02:33 -0700116 spin_lock_irq(&ctx->wqh.lock);
Michal Nazarewicz8120a8a2010-05-05 12:53:12 +0200117 if (file->f_flags & O_NONBLOCK)
118 res = -EAGAIN;
119 else
120 res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800121 if (ctx->ticks) {
122 ticks = ctx->ticks;
123 if (ctx->expired && ctx->tintv.tv64) {
Davide Libenzib215e282007-05-10 22:23:16 -0700124 /*
125 * If tintv.tv64 != 0, this is a periodic timer that
126 * needs to be re-armed. We avoid doing it in the timer
127 * callback to avoid DoS attacks specifying a very
128 * short timer period.
129 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800130 ticks += hrtimer_forward_now(&ctx->tmr,
131 ctx->tintv) - 1;
Davide Libenzib215e282007-05-10 22:23:16 -0700132 hrtimer_restart(&ctx->tmr);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800133 }
134 ctx->expired = 0;
135 ctx->ticks = 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700136 }
Davide Libenzi18963c02007-05-18 12:02:33 -0700137 spin_unlock_irq(&ctx->wqh.lock);
Davide Libenzib215e282007-05-10 22:23:16 -0700138 if (ticks)
Davide Libenzi09828402007-07-26 10:41:07 -0700139 res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
Davide Libenzib215e282007-05-10 22:23:16 -0700140 return res;
141}
142
143static const struct file_operations timerfd_fops = {
144 .release = timerfd_release,
145 .poll = timerfd_poll,
146 .read = timerfd_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200147 .llseek = noop_llseek,
Davide Libenzib215e282007-05-10 22:23:16 -0700148};
149
Davide Libenzi4d672e72008-02-04 22:27:26 -0800150static struct file *timerfd_fget(int fd)
Davide Libenzib215e282007-05-10 22:23:16 -0700151{
Davide Libenzi4d672e72008-02-04 22:27:26 -0800152 struct file *file;
153
154 file = fget(fd);
155 if (!file)
156 return ERR_PTR(-EBADF);
157 if (file->f_op != &timerfd_fops) {
158 fput(file);
159 return ERR_PTR(-EINVAL);
160 }
161
162 return file;
163}
164
Heiko Carstens836f92a2009-01-14 14:14:33 +0100165SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800166{
Al Viro2030a422008-02-23 06:46:49 -0500167 int ufd;
Davide Libenzib215e282007-05-10 22:23:16 -0700168 struct timerfd_ctx *ctx;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800169
Ulrich Dreppere38b36f2008-07-23 21:29:42 -0700170 /* Check the TFD_* constants for consistency. */
171 BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
172 BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
173
Davide Libenzi610d18f2009-02-18 14:48:18 -0800174 if ((flags & ~TFD_CREATE_FLAGS) ||
175 (clockid != CLOCK_MONOTONIC &&
176 clockid != CLOCK_REALTIME))
Davide Libenzi4d672e72008-02-04 22:27:26 -0800177 return -EINVAL;
178
179 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
180 if (!ctx)
181 return -ENOMEM;
182
183 init_waitqueue_head(&ctx->wqh);
184 ctx->clockid = clockid;
185 hrtimer_init(&ctx->tmr, clockid, HRTIMER_MODE_ABS);
186
Ulrich Drepper11fcb6c2008-07-23 21:29:26 -0700187 ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx,
Roland Dreier628ff7c2009-12-18 09:41:24 -0800188 O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
Al Viro2030a422008-02-23 06:46:49 -0500189 if (ufd < 0)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800190 kfree(ctx);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800191
192 return ufd;
193}
194
Heiko Carstens836f92a2009-01-14 14:14:33 +0100195SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
196 const struct itimerspec __user *, utmr,
197 struct itimerspec __user *, otmr)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800198{
199 struct file *file;
200 struct timerfd_ctx *ctx;
201 struct itimerspec ktmr, kotmr;
Davide Libenzib215e282007-05-10 22:23:16 -0700202
203 if (copy_from_user(&ktmr, utmr, sizeof(ktmr)))
204 return -EFAULT;
205
Davide Libenzi610d18f2009-02-18 14:48:18 -0800206 if ((flags & ~TFD_SETTIME_FLAGS) ||
207 !timespec_valid(&ktmr.it_value) ||
Davide Libenzib215e282007-05-10 22:23:16 -0700208 !timespec_valid(&ktmr.it_interval))
209 return -EINVAL;
210
Davide Libenzi4d672e72008-02-04 22:27:26 -0800211 file = timerfd_fget(ufd);
212 if (IS_ERR(file))
213 return PTR_ERR(file);
214 ctx = file->private_data;
Davide Libenzib215e282007-05-10 22:23:16 -0700215
Davide Libenzi4d672e72008-02-04 22:27:26 -0800216 /*
217 * We need to stop the existing timer before reprogramming
218 * it to the new values.
219 */
220 for (;;) {
221 spin_lock_irq(&ctx->wqh.lock);
222 if (hrtimer_try_to_cancel(&ctx->tmr) >= 0)
223 break;
Davide Libenzi18963c02007-05-18 12:02:33 -0700224 spin_unlock_irq(&ctx->wqh.lock);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800225 cpu_relax();
Davide Libenzib215e282007-05-10 22:23:16 -0700226 }
227
Davide Libenzi4d672e72008-02-04 22:27:26 -0800228 /*
229 * If the timer is expired and it's periodic, we need to advance it
230 * because the caller may want to know the previous expiration time.
231 * We do not update "ticks" and "expired" since the timer will be
232 * re-programmed again in the following timerfd_setup() call.
233 */
234 if (ctx->expired && ctx->tintv.tv64)
235 hrtimer_forward_now(&ctx->tmr, ctx->tintv);
Davide Libenzib215e282007-05-10 22:23:16 -0700236
Davide Libenzi4d672e72008-02-04 22:27:26 -0800237 kotmr.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
238 kotmr.it_interval = ktime_to_timespec(ctx->tintv);
239
240 /*
241 * Re-program the timer to the new value ...
242 */
243 timerfd_setup(ctx, flags, &ktmr);
244
245 spin_unlock_irq(&ctx->wqh.lock);
246 fput(file);
247 if (otmr && copy_to_user(otmr, &kotmr, sizeof(kotmr)))
248 return -EFAULT;
249
250 return 0;
251}
252
Heiko Carstensd4e82042009-01-14 14:14:34 +0100253SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct itimerspec __user *, otmr)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800254{
255 struct file *file;
256 struct timerfd_ctx *ctx;
257 struct itimerspec kotmr;
258
259 file = timerfd_fget(ufd);
260 if (IS_ERR(file))
261 return PTR_ERR(file);
262 ctx = file->private_data;
263
264 spin_lock_irq(&ctx->wqh.lock);
265 if (ctx->expired && ctx->tintv.tv64) {
266 ctx->expired = 0;
267 ctx->ticks +=
268 hrtimer_forward_now(&ctx->tmr, ctx->tintv) - 1;
269 hrtimer_restart(&ctx->tmr);
270 }
271 kotmr.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
272 kotmr.it_interval = ktime_to_timespec(ctx->tintv);
273 spin_unlock_irq(&ctx->wqh.lock);
274 fput(file);
275
276 return copy_to_user(otmr, &kotmr, sizeof(kotmr)) ? -EFAULT: 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700277}
278