blob: d87d354ec42438a4a0794cf964999eb376053c73 [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>
17#include <linux/list.h>
18#include <linux/spinlock.h>
19#include <linux/time.h>
20#include <linux/hrtimer.h>
21#include <linux/anon_inodes.h>
22#include <linux/timerfd.h>
Adrian Bunk45cc2b92008-04-29 00:58:59 -070023#include <linux/syscalls.h>
Davide Libenzib215e282007-05-10 22:23:16 -070024
25struct timerfd_ctx {
26 struct hrtimer tmr;
27 ktime_t tintv;
Davide Libenzib215e282007-05-10 22:23:16 -070028 wait_queue_head_t wqh;
Davide Libenzi4d672e72008-02-04 22:27:26 -080029 u64 ticks;
Davide Libenzib215e282007-05-10 22:23:16 -070030 int expired;
Davide Libenzi4d672e72008-02-04 22:27:26 -080031 int clockid;
Davide Libenzib215e282007-05-10 22:23:16 -070032};
33
34/*
35 * This gets called when the timer event triggers. We set the "expired"
36 * flag, but we do not re-arm the timer (in case it's necessary,
Davide Libenzi4d672e72008-02-04 22:27:26 -080037 * tintv.tv64 != 0) until the timer is accessed.
Davide Libenzib215e282007-05-10 22:23:16 -070038 */
39static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr)
40{
41 struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx, tmr);
42 unsigned long flags;
43
Davide Libenzi18963c02007-05-18 12:02:33 -070044 spin_lock_irqsave(&ctx->wqh.lock, flags);
Davide Libenzib215e282007-05-10 22:23:16 -070045 ctx->expired = 1;
Davide Libenzi4d672e72008-02-04 22:27:26 -080046 ctx->ticks++;
Davide Libenzib215e282007-05-10 22:23:16 -070047 wake_up_locked(&ctx->wqh);
Davide Libenzi18963c02007-05-18 12:02:33 -070048 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
Davide Libenzib215e282007-05-10 22:23:16 -070049
50 return HRTIMER_NORESTART;
51}
52
Davide Libenzi4d672e72008-02-04 22:27:26 -080053static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
54{
55 ktime_t now, remaining;
56
57 now = ctx->tmr.base->get_time();
58 remaining = ktime_sub(ctx->tmr.expires, now);
59
60 return remaining.tv64 < 0 ? ktime_set(0, 0): remaining;
61}
62
63static void timerfd_setup(struct timerfd_ctx *ctx, int flags,
Davide Libenzib215e282007-05-10 22:23:16 -070064 const struct itimerspec *ktmr)
65{
66 enum hrtimer_mode htmode;
67 ktime_t texp;
68
69 htmode = (flags & TFD_TIMER_ABSTIME) ?
70 HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
71
72 texp = timespec_to_ktime(ktmr->it_value);
73 ctx->expired = 0;
Davide Libenzi4d672e72008-02-04 22:27:26 -080074 ctx->ticks = 0;
Davide Libenzib215e282007-05-10 22:23:16 -070075 ctx->tintv = timespec_to_ktime(ktmr->it_interval);
Davide Libenzi4d672e72008-02-04 22:27:26 -080076 hrtimer_init(&ctx->tmr, ctx->clockid, htmode);
Davide Libenzib215e282007-05-10 22:23:16 -070077 ctx->tmr.expires = texp;
78 ctx->tmr.function = timerfd_tmrproc;
79 if (texp.tv64 != 0)
80 hrtimer_start(&ctx->tmr, texp, htmode);
81}
82
83static int timerfd_release(struct inode *inode, struct file *file)
84{
85 struct timerfd_ctx *ctx = file->private_data;
86
87 hrtimer_cancel(&ctx->tmr);
88 kfree(ctx);
89 return 0;
90}
91
92static unsigned int timerfd_poll(struct file *file, poll_table *wait)
93{
94 struct timerfd_ctx *ctx = file->private_data;
95 unsigned int events = 0;
96 unsigned long flags;
97
98 poll_wait(file, &ctx->wqh, wait);
99
Davide Libenzi18963c02007-05-18 12:02:33 -0700100 spin_lock_irqsave(&ctx->wqh.lock, flags);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800101 if (ctx->ticks)
Davide Libenzib215e282007-05-10 22:23:16 -0700102 events |= POLLIN;
Davide Libenzi18963c02007-05-18 12:02:33 -0700103 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
Davide Libenzib215e282007-05-10 22:23:16 -0700104
105 return events;
106}
107
108static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
109 loff_t *ppos)
110{
111 struct timerfd_ctx *ctx = file->private_data;
112 ssize_t res;
Davide Libenzi09828402007-07-26 10:41:07 -0700113 u64 ticks = 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700114 DECLARE_WAITQUEUE(wait, current);
115
116 if (count < sizeof(ticks))
117 return -EINVAL;
Davide Libenzi18963c02007-05-18 12:02:33 -0700118 spin_lock_irq(&ctx->wqh.lock);
Davide Libenzib215e282007-05-10 22:23:16 -0700119 res = -EAGAIN;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800120 if (!ctx->ticks && !(file->f_flags & O_NONBLOCK)) {
Davide Libenzib215e282007-05-10 22:23:16 -0700121 __add_wait_queue(&ctx->wqh, &wait);
122 for (res = 0;;) {
123 set_current_state(TASK_INTERRUPTIBLE);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800124 if (ctx->ticks) {
Davide Libenzib215e282007-05-10 22:23:16 -0700125 res = 0;
126 break;
127 }
128 if (signal_pending(current)) {
129 res = -ERESTARTSYS;
130 break;
131 }
Davide Libenzi18963c02007-05-18 12:02:33 -0700132 spin_unlock_irq(&ctx->wqh.lock);
Davide Libenzib215e282007-05-10 22:23:16 -0700133 schedule();
Davide Libenzi18963c02007-05-18 12:02:33 -0700134 spin_lock_irq(&ctx->wqh.lock);
Davide Libenzib215e282007-05-10 22:23:16 -0700135 }
136 __remove_wait_queue(&ctx->wqh, &wait);
137 __set_current_state(TASK_RUNNING);
138 }
Davide Libenzi4d672e72008-02-04 22:27:26 -0800139 if (ctx->ticks) {
140 ticks = ctx->ticks;
141 if (ctx->expired && ctx->tintv.tv64) {
Davide Libenzib215e282007-05-10 22:23:16 -0700142 /*
143 * If tintv.tv64 != 0, this is a periodic timer that
144 * needs to be re-armed. We avoid doing it in the timer
145 * callback to avoid DoS attacks specifying a very
146 * short timer period.
147 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800148 ticks += hrtimer_forward_now(&ctx->tmr,
149 ctx->tintv) - 1;
Davide Libenzib215e282007-05-10 22:23:16 -0700150 hrtimer_restart(&ctx->tmr);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800151 }
152 ctx->expired = 0;
153 ctx->ticks = 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700154 }
Davide Libenzi18963c02007-05-18 12:02:33 -0700155 spin_unlock_irq(&ctx->wqh.lock);
Davide Libenzib215e282007-05-10 22:23:16 -0700156 if (ticks)
Davide Libenzi09828402007-07-26 10:41:07 -0700157 res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
Davide Libenzib215e282007-05-10 22:23:16 -0700158 return res;
159}
160
161static const struct file_operations timerfd_fops = {
162 .release = timerfd_release,
163 .poll = timerfd_poll,
164 .read = timerfd_read,
165};
166
Davide Libenzi4d672e72008-02-04 22:27:26 -0800167static struct file *timerfd_fget(int fd)
Davide Libenzib215e282007-05-10 22:23:16 -0700168{
Davide Libenzi4d672e72008-02-04 22:27:26 -0800169 struct file *file;
170
171 file = fget(fd);
172 if (!file)
173 return ERR_PTR(-EBADF);
174 if (file->f_op != &timerfd_fops) {
175 fput(file);
176 return ERR_PTR(-EINVAL);
177 }
178
179 return file;
180}
181
182asmlinkage long sys_timerfd_create(int clockid, int flags)
183{
Al Viro2030a422008-02-23 06:46:49 -0500184 int ufd;
Davide Libenzib215e282007-05-10 22:23:16 -0700185 struct timerfd_ctx *ctx;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800186
187 if (flags)
188 return -EINVAL;
189 if (clockid != CLOCK_MONOTONIC &&
190 clockid != CLOCK_REALTIME)
191 return -EINVAL;
192
193 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
194 if (!ctx)
195 return -ENOMEM;
196
197 init_waitqueue_head(&ctx->wqh);
198 ctx->clockid = clockid;
199 hrtimer_init(&ctx->tmr, clockid, HRTIMER_MODE_ABS);
200
Al Viro2030a422008-02-23 06:46:49 -0500201 ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx);
202 if (ufd < 0)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800203 kfree(ctx);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800204
205 return ufd;
206}
207
208asmlinkage long sys_timerfd_settime(int ufd, int flags,
209 const struct itimerspec __user *utmr,
210 struct itimerspec __user *otmr)
211{
212 struct file *file;
213 struct timerfd_ctx *ctx;
214 struct itimerspec ktmr, kotmr;
Davide Libenzib215e282007-05-10 22:23:16 -0700215
216 if (copy_from_user(&ktmr, utmr, sizeof(ktmr)))
217 return -EFAULT;
218
Davide Libenzib215e282007-05-10 22:23:16 -0700219 if (!timespec_valid(&ktmr.it_value) ||
220 !timespec_valid(&ktmr.it_interval))
221 return -EINVAL;
222
Davide Libenzi4d672e72008-02-04 22:27:26 -0800223 file = timerfd_fget(ufd);
224 if (IS_ERR(file))
225 return PTR_ERR(file);
226 ctx = file->private_data;
Davide Libenzib215e282007-05-10 22:23:16 -0700227
Davide Libenzi4d672e72008-02-04 22:27:26 -0800228 /*
229 * We need to stop the existing timer before reprogramming
230 * it to the new values.
231 */
232 for (;;) {
233 spin_lock_irq(&ctx->wqh.lock);
234 if (hrtimer_try_to_cancel(&ctx->tmr) >= 0)
235 break;
Davide Libenzi18963c02007-05-18 12:02:33 -0700236 spin_unlock_irq(&ctx->wqh.lock);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800237 cpu_relax();
Davide Libenzib215e282007-05-10 22:23:16 -0700238 }
239
Davide Libenzi4d672e72008-02-04 22:27:26 -0800240 /*
241 * If the timer is expired and it's periodic, we need to advance it
242 * because the caller may want to know the previous expiration time.
243 * We do not update "ticks" and "expired" since the timer will be
244 * re-programmed again in the following timerfd_setup() call.
245 */
246 if (ctx->expired && ctx->tintv.tv64)
247 hrtimer_forward_now(&ctx->tmr, ctx->tintv);
Davide Libenzib215e282007-05-10 22:23:16 -0700248
Davide Libenzi4d672e72008-02-04 22:27:26 -0800249 kotmr.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
250 kotmr.it_interval = ktime_to_timespec(ctx->tintv);
251
252 /*
253 * Re-program the timer to the new value ...
254 */
255 timerfd_setup(ctx, flags, &ktmr);
256
257 spin_unlock_irq(&ctx->wqh.lock);
258 fput(file);
259 if (otmr && copy_to_user(otmr, &kotmr, sizeof(kotmr)))
260 return -EFAULT;
261
262 return 0;
263}
264
265asmlinkage long sys_timerfd_gettime(int ufd, struct itimerspec __user *otmr)
266{
267 struct file *file;
268 struct timerfd_ctx *ctx;
269 struct itimerspec kotmr;
270
271 file = timerfd_fget(ufd);
272 if (IS_ERR(file))
273 return PTR_ERR(file);
274 ctx = file->private_data;
275
276 spin_lock_irq(&ctx->wqh.lock);
277 if (ctx->expired && ctx->tintv.tv64) {
278 ctx->expired = 0;
279 ctx->ticks +=
280 hrtimer_forward_now(&ctx->tmr, ctx->tintv) - 1;
281 hrtimer_restart(&ctx->tmr);
282 }
283 kotmr.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
284 kotmr.it_interval = ktime_to_timespec(ctx->tintv);
285 spin_unlock_irq(&ctx->wqh.lock);
286 fput(file);
287
288 return copy_to_user(otmr, &kotmr, sizeof(kotmr)) ? -EFAULT: 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700289}
290