4 * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org>
7 * Thanks to Thomas Gleixner for code reviews and useful comments.
11 #include <linux/file.h>
12 #include <linux/poll.h>
13 #include <linux/init.h>
15 #include <linux/sched.h>
16 #include <linux/kernel.h>
17 #include <linux/slab.h>
18 #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>
24 #include <linux/syscalls.h>
30 wait_queue_head_t wqh;
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,
40 * tintv.tv64 != 0) until the timer is accessed.
42 static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr)
44 struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx, tmr);
47 spin_lock_irqsave(&ctx->wqh.lock, flags);
50 wake_up_locked(&ctx->wqh);
51 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
53 return HRTIMER_NORESTART;
56 static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
60 remaining = hrtimer_expires_remaining(&ctx->tmr);
61 return remaining.tv64 < 0 ? ktime_set(0, 0): remaining;
64 static bool timerfd_canceled(struct timerfd_ctx *ctx)
68 if (!ctx->might_cancel)
71 moffs = ktime_get_monotonic_offset();
73 if (moffs.tv64 == ctx->moffs.tv64)
80 static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
81 const struct itimerspec *ktmr)
83 enum hrtimer_mode htmode;
85 int clockid = ctx->clockid;
87 htmode = (flags & TFD_TIMER_ABSTIME) ?
88 HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
90 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;
97 texp = timespec_to_ktime(ktmr->it_value);
100 ctx->tintv = timespec_to_ktime(ktmr->it_interval);
101 hrtimer_init(&ctx->tmr, clockid, htmode);
102 hrtimer_set_expires(&ctx->tmr, texp);
103 ctx->tmr.function = timerfd_tmrproc;
104 if (texp.tv64 != 0) {
105 hrtimer_start(&ctx->tmr, texp, htmode);
106 if (timerfd_canceled(ctx))
112 static int timerfd_release(struct inode *inode, struct file *file)
114 struct timerfd_ctx *ctx = file->private_data;
116 hrtimer_cancel(&ctx->tmr);
121 static unsigned int timerfd_poll(struct file *file, poll_table *wait)
123 struct timerfd_ctx *ctx = file->private_data;
124 unsigned int events = 0;
127 poll_wait(file, &ctx->wqh, wait);
129 spin_lock_irqsave(&ctx->wqh.lock, flags);
132 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
137 static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
140 struct timerfd_ctx *ctx = file->private_data;
144 if (count < sizeof(ticks))
146 spin_lock_irq(&ctx->wqh.lock);
147 if (file->f_flags & O_NONBLOCK)
150 res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
156 * If clock has changed, we do not care about the
157 * ticks and we do not rearm the timer. Userspace must
160 if (timerfd_canceled(ctx)) {
166 if (ctx->expired && ctx->tintv.tv64) {
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.
173 ticks += hrtimer_forward_now(&ctx->tmr,
175 hrtimer_restart(&ctx->tmr);
180 spin_unlock_irq(&ctx->wqh.lock);
182 res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
186 static const struct file_operations timerfd_fops = {
187 .release = timerfd_release,
188 .poll = timerfd_poll,
189 .read = timerfd_read,
190 .llseek = noop_llseek,
193 static struct file *timerfd_fget(int fd)
199 return ERR_PTR(-EBADF);
200 if (file->f_op != &timerfd_fops) {
202 return ERR_PTR(-EINVAL);
208 SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
211 struct timerfd_ctx *ctx;
213 /* Check the TFD_* constants for consistency. */
214 BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
215 BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
217 if ((flags & ~TFD_CREATE_FLAGS) ||
218 (clockid != CLOCK_MONOTONIC &&
219 clockid != CLOCK_REALTIME))
222 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
226 init_waitqueue_head(&ctx->wqh);
227 ctx->clockid = clockid;
228 hrtimer_init(&ctx->tmr, clockid, HRTIMER_MODE_ABS);
229 ctx->moffs = ktime_get_monotonic_offset();
231 ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx,
232 O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
239 SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
240 const struct itimerspec __user *, utmr,
241 struct itimerspec __user *, otmr)
244 struct timerfd_ctx *ctx;
245 struct itimerspec ktmr, kotmr;
248 if (copy_from_user(&ktmr, utmr, sizeof(ktmr)))
251 if ((flags & ~TFD_SETTIME_FLAGS) ||
252 !timespec_valid(&ktmr.it_value) ||
253 !timespec_valid(&ktmr.it_interval))
256 file = timerfd_fget(ufd);
258 return PTR_ERR(file);
259 ctx = file->private_data;
262 * We need to stop the existing timer before reprogramming
263 * it to the new values.
266 spin_lock_irq(&ctx->wqh.lock);
267 if (hrtimer_try_to_cancel(&ctx->tmr) >= 0)
269 spin_unlock_irq(&ctx->wqh.lock);
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.
279 if (ctx->expired && ctx->tintv.tv64)
280 hrtimer_forward_now(&ctx->tmr, ctx->tintv);
282 kotmr.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
283 kotmr.it_interval = ktime_to_timespec(ctx->tintv);
286 * Re-program the timer to the new value ...
288 ret = timerfd_setup(ctx, flags, &ktmr);
290 spin_unlock_irq(&ctx->wqh.lock);
292 if (otmr && copy_to_user(otmr, &kotmr, sizeof(kotmr)))
298 SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct itimerspec __user *, otmr)
301 struct timerfd_ctx *ctx;
302 struct itimerspec kotmr;
304 file = timerfd_fget(ufd);
306 return PTR_ERR(file);
307 ctx = file->private_data;
309 spin_lock_irq(&ctx->wqh.lock);
310 if (ctx->expired && ctx->tintv.tv64) {
313 hrtimer_forward_now(&ctx->tmr, ctx->tintv) - 1;
314 hrtimer_restart(&ctx->tmr);
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);
321 return copy_to_user(otmr, &kotmr, sizeof(kotmr)) ? -EFAULT: 0;