blob: 61983f3b107c5ee5d12521d3f38255a77bc6f781 [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>
23
24struct timerfd_ctx {
25 struct hrtimer tmr;
26 ktime_t tintv;
Davide Libenzib215e282007-05-10 22:23:16 -070027 wait_queue_head_t wqh;
28 int expired;
29};
30
31/*
32 * This gets called when the timer event triggers. We set the "expired"
33 * flag, but we do not re-arm the timer (in case it's necessary,
34 * tintv.tv64 != 0) until the timer is read.
35 */
36static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr)
37{
38 struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx, tmr);
39 unsigned long flags;
40
Davide Libenzi18963c02007-05-18 12:02:33 -070041 spin_lock_irqsave(&ctx->wqh.lock, flags);
Davide Libenzib215e282007-05-10 22:23:16 -070042 ctx->expired = 1;
43 wake_up_locked(&ctx->wqh);
Davide Libenzi18963c02007-05-18 12:02:33 -070044 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
Davide Libenzib215e282007-05-10 22:23:16 -070045
46 return HRTIMER_NORESTART;
47}
48
49static void timerfd_setup(struct timerfd_ctx *ctx, int clockid, int flags,
50 const struct itimerspec *ktmr)
51{
52 enum hrtimer_mode htmode;
53 ktime_t texp;
54
55 htmode = (flags & TFD_TIMER_ABSTIME) ?
56 HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
57
58 texp = timespec_to_ktime(ktmr->it_value);
59 ctx->expired = 0;
60 ctx->tintv = timespec_to_ktime(ktmr->it_interval);
61 hrtimer_init(&ctx->tmr, clockid, htmode);
62 ctx->tmr.expires = texp;
63 ctx->tmr.function = timerfd_tmrproc;
64 if (texp.tv64 != 0)
65 hrtimer_start(&ctx->tmr, texp, htmode);
66}
67
68static int timerfd_release(struct inode *inode, struct file *file)
69{
70 struct timerfd_ctx *ctx = file->private_data;
71
72 hrtimer_cancel(&ctx->tmr);
73 kfree(ctx);
74 return 0;
75}
76
77static unsigned int timerfd_poll(struct file *file, poll_table *wait)
78{
79 struct timerfd_ctx *ctx = file->private_data;
80 unsigned int events = 0;
81 unsigned long flags;
82
83 poll_wait(file, &ctx->wqh, wait);
84
Davide Libenzi18963c02007-05-18 12:02:33 -070085 spin_lock_irqsave(&ctx->wqh.lock, flags);
Davide Libenzib215e282007-05-10 22:23:16 -070086 if (ctx->expired)
87 events |= POLLIN;
Davide Libenzi18963c02007-05-18 12:02:33 -070088 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
Davide Libenzib215e282007-05-10 22:23:16 -070089
90 return events;
91}
92
93static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
94 loff_t *ppos)
95{
96 struct timerfd_ctx *ctx = file->private_data;
97 ssize_t res;
Davide Libenzi09828402007-07-26 10:41:07 -070098 u64 ticks = 0;
Davide Libenzib215e282007-05-10 22:23:16 -070099 DECLARE_WAITQUEUE(wait, current);
100
101 if (count < sizeof(ticks))
102 return -EINVAL;
Davide Libenzi18963c02007-05-18 12:02:33 -0700103 spin_lock_irq(&ctx->wqh.lock);
Davide Libenzib215e282007-05-10 22:23:16 -0700104 res = -EAGAIN;
105 if (!ctx->expired && !(file->f_flags & O_NONBLOCK)) {
106 __add_wait_queue(&ctx->wqh, &wait);
107 for (res = 0;;) {
108 set_current_state(TASK_INTERRUPTIBLE);
109 if (ctx->expired) {
110 res = 0;
111 break;
112 }
113 if (signal_pending(current)) {
114 res = -ERESTARTSYS;
115 break;
116 }
Davide Libenzi18963c02007-05-18 12:02:33 -0700117 spin_unlock_irq(&ctx->wqh.lock);
Davide Libenzib215e282007-05-10 22:23:16 -0700118 schedule();
Davide Libenzi18963c02007-05-18 12:02:33 -0700119 spin_lock_irq(&ctx->wqh.lock);
Davide Libenzib215e282007-05-10 22:23:16 -0700120 }
121 __remove_wait_queue(&ctx->wqh, &wait);
122 __set_current_state(TASK_RUNNING);
123 }
124 if (ctx->expired) {
125 ctx->expired = 0;
126 if (ctx->tintv.tv64 != 0) {
127 /*
128 * If tintv.tv64 != 0, this is a periodic timer that
129 * needs to be re-armed. We avoid doing it in the timer
130 * callback to avoid DoS attacks specifying a very
131 * short timer period.
132 */
Davide Libenzi09828402007-07-26 10:41:07 -0700133 ticks = (u64)
Davide Libenzib215e282007-05-10 22:23:16 -0700134 hrtimer_forward(&ctx->tmr,
135 hrtimer_cb_get_time(&ctx->tmr),
136 ctx->tintv);
137 hrtimer_restart(&ctx->tmr);
138 } else
139 ticks = 1;
140 }
Davide Libenzi18963c02007-05-18 12:02:33 -0700141 spin_unlock_irq(&ctx->wqh.lock);
Davide Libenzib215e282007-05-10 22:23:16 -0700142 if (ticks)
Davide Libenzi09828402007-07-26 10:41:07 -0700143 res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
Davide Libenzib215e282007-05-10 22:23:16 -0700144 return res;
145}
146
147static const struct file_operations timerfd_fops = {
148 .release = timerfd_release,
149 .poll = timerfd_poll,
150 .read = timerfd_read,
151};
152
153asmlinkage long sys_timerfd(int ufd, int clockid, int flags,
154 const struct itimerspec __user *utmr)
155{
156 int error;
157 struct timerfd_ctx *ctx;
158 struct file *file;
159 struct inode *inode;
160 struct itimerspec ktmr;
161
162 if (copy_from_user(&ktmr, utmr, sizeof(ktmr)))
163 return -EFAULT;
164
165 if (clockid != CLOCK_MONOTONIC &&
166 clockid != CLOCK_REALTIME)
167 return -EINVAL;
168 if (!timespec_valid(&ktmr.it_value) ||
169 !timespec_valid(&ktmr.it_interval))
170 return -EINVAL;
171
172 if (ufd == -1) {
173 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
174 if (!ctx)
175 return -ENOMEM;
176
177 init_waitqueue_head(&ctx->wqh);
Davide Libenzib215e282007-05-10 22:23:16 -0700178
179 timerfd_setup(ctx, clockid, flags, &ktmr);
180
181 /*
182 * When we call this, the initialization must be complete, since
183 * anon_inode_getfd() will install the fd.
184 */
185 error = anon_inode_getfd(&ufd, &inode, &file, "[timerfd]",
186 &timerfd_fops, ctx);
187 if (error)
188 goto err_tmrcancel;
189 } else {
190 file = fget(ufd);
191 if (!file)
192 return -EBADF;
193 ctx = file->private_data;
194 if (file->f_op != &timerfd_fops) {
195 fput(file);
196 return -EINVAL;
197 }
198 /*
199 * We need to stop the existing timer before reprogramming
200 * it to the new values.
201 */
202 for (;;) {
Davide Libenzi18963c02007-05-18 12:02:33 -0700203 spin_lock_irq(&ctx->wqh.lock);
Davide Libenzib215e282007-05-10 22:23:16 -0700204 if (hrtimer_try_to_cancel(&ctx->tmr) >= 0)
205 break;
Davide Libenzi18963c02007-05-18 12:02:33 -0700206 spin_unlock_irq(&ctx->wqh.lock);
Davide Libenzib215e282007-05-10 22:23:16 -0700207 cpu_relax();
208 }
209 /*
210 * Re-program the timer to the new value ...
211 */
212 timerfd_setup(ctx, clockid, flags, &ktmr);
213
Davide Libenzi18963c02007-05-18 12:02:33 -0700214 spin_unlock_irq(&ctx->wqh.lock);
Davide Libenzib215e282007-05-10 22:23:16 -0700215 fput(file);
216 }
217
218 return ufd;
219
220err_tmrcancel:
221 hrtimer_cancel(&ctx->tmr);
222 kfree(ctx);
223 return error;
224}
225