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>
25 #include <linux/compat.h>
26 #include <linux/rcupdate.h>
32 wait_queue_head_t wqh;
37 struct list_head clist;
41 static LIST_HEAD(cancel_list);
42 static DEFINE_SPINLOCK(cancel_lock);
45 * This gets called when the timer event triggers. We set the "expired"
46 * flag, but we do not re-arm the timer (in case it's necessary,
47 * tintv.tv64 != 0) until the timer is accessed.
49 static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr)
51 struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx, tmr);
54 spin_lock_irqsave(&ctx->wqh.lock, flags);
57 wake_up_locked(&ctx->wqh);
58 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
60 return HRTIMER_NORESTART;
64 * Called when the clock was set to cancel the timers in the cancel
65 * list. This will wake up processes waiting on these timers. The
66 * wake-up requires ctx->ticks to be non zero, therefore we increment
67 * it before calling wake_up_locked().
69 void timerfd_clock_was_set(void)
71 ktime_t moffs = ktime_get_monotonic_offset();
72 struct timerfd_ctx *ctx;
76 list_for_each_entry_rcu(ctx, &cancel_list, clist) {
77 if (!ctx->might_cancel)
79 spin_lock_irqsave(&ctx->wqh.lock, flags);
80 if (ctx->moffs.tv64 != moffs.tv64) {
81 ctx->moffs.tv64 = KTIME_MAX;
83 wake_up_locked(&ctx->wqh);
85 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
90 static void timerfd_remove_cancel(struct timerfd_ctx *ctx)
92 if (ctx->might_cancel) {
93 ctx->might_cancel = false;
94 spin_lock(&cancel_lock);
95 list_del_rcu(&ctx->clist);
96 spin_unlock(&cancel_lock);
100 static bool timerfd_canceled(struct timerfd_ctx *ctx)
102 if (!ctx->might_cancel || ctx->moffs.tv64 != KTIME_MAX)
104 ctx->moffs = ktime_get_monotonic_offset();
108 static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags)
110 if (ctx->clockid == CLOCK_REALTIME && (flags & TFD_TIMER_ABSTIME) &&
111 (flags & TFD_TIMER_CANCEL_ON_SET)) {
112 if (!ctx->might_cancel) {
113 ctx->might_cancel = true;
114 spin_lock(&cancel_lock);
115 list_add_rcu(&ctx->clist, &cancel_list);
116 spin_unlock(&cancel_lock);
118 } else if (ctx->might_cancel) {
119 timerfd_remove_cancel(ctx);
123 static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
127 remaining = hrtimer_expires_remaining(&ctx->tmr);
128 return remaining.tv64 < 0 ? ktime_set(0, 0): remaining;
131 static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
132 const struct itimerspec *ktmr)
134 enum hrtimer_mode htmode;
136 int clockid = ctx->clockid;
138 htmode = (flags & TFD_TIMER_ABSTIME) ?
139 HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
141 texp = timespec_to_ktime(ktmr->it_value);
144 ctx->tintv = timespec_to_ktime(ktmr->it_interval);
145 hrtimer_init(&ctx->tmr, clockid, htmode);
146 hrtimer_set_expires(&ctx->tmr, texp);
147 ctx->tmr.function = timerfd_tmrproc;
148 if (texp.tv64 != 0) {
149 hrtimer_start(&ctx->tmr, texp, htmode);
150 if (timerfd_canceled(ctx))
156 static int timerfd_release(struct inode *inode, struct file *file)
158 struct timerfd_ctx *ctx = file->private_data;
160 timerfd_remove_cancel(ctx);
161 hrtimer_cancel(&ctx->tmr);
166 static unsigned int timerfd_poll(struct file *file, poll_table *wait)
168 struct timerfd_ctx *ctx = file->private_data;
169 unsigned int events = 0;
172 poll_wait(file, &ctx->wqh, wait);
174 spin_lock_irqsave(&ctx->wqh.lock, flags);
177 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
182 static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
185 struct timerfd_ctx *ctx = file->private_data;
189 if (count < sizeof(ticks))
191 spin_lock_irq(&ctx->wqh.lock);
192 if (file->f_flags & O_NONBLOCK)
195 res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
198 * If clock has changed, we do not care about the
199 * ticks and we do not rearm the timer. Userspace must
202 if (timerfd_canceled(ctx)) {
211 if (ctx->expired && ctx->tintv.tv64) {
213 * If tintv.tv64 != 0, this is a periodic timer that
214 * needs to be re-armed. We avoid doing it in the timer
215 * callback to avoid DoS attacks specifying a very
216 * short timer period.
218 ticks += hrtimer_forward_now(&ctx->tmr,
220 hrtimer_restart(&ctx->tmr);
225 spin_unlock_irq(&ctx->wqh.lock);
227 res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
231 static const struct file_operations timerfd_fops = {
232 .release = timerfd_release,
233 .poll = timerfd_poll,
234 .read = timerfd_read,
235 .llseek = noop_llseek,
238 static int timerfd_fget(int fd, struct fd *p)
240 struct fd f = fdget(fd);
243 if (f.file->f_op != &timerfd_fops) {
251 SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
254 struct timerfd_ctx *ctx;
256 /* Check the TFD_* constants for consistency. */
257 BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
258 BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
260 if ((flags & ~TFD_CREATE_FLAGS) ||
261 (clockid != CLOCK_MONOTONIC &&
262 clockid != CLOCK_REALTIME))
265 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
269 init_waitqueue_head(&ctx->wqh);
270 ctx->clockid = clockid;
271 hrtimer_init(&ctx->tmr, clockid, HRTIMER_MODE_ABS);
272 ctx->moffs = ktime_get_monotonic_offset();
274 ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx,
275 O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
282 static int do_timerfd_settime(int ufd, int flags,
283 const struct itimerspec *new,
284 struct itimerspec *old)
287 struct timerfd_ctx *ctx;
290 if ((flags & ~TFD_SETTIME_FLAGS) ||
291 !timespec_valid(&new->it_value) ||
292 !timespec_valid(&new->it_interval))
295 ret = timerfd_fget(ufd, &f);
298 ctx = f.file->private_data;
300 timerfd_setup_cancel(ctx, flags);
303 * We need to stop the existing timer before reprogramming
304 * it to the new values.
307 spin_lock_irq(&ctx->wqh.lock);
308 if (hrtimer_try_to_cancel(&ctx->tmr) >= 0)
310 spin_unlock_irq(&ctx->wqh.lock);
315 * If the timer is expired and it's periodic, we need to advance it
316 * because the caller may want to know the previous expiration time.
317 * We do not update "ticks" and "expired" since the timer will be
318 * re-programmed again in the following timerfd_setup() call.
320 if (ctx->expired && ctx->tintv.tv64)
321 hrtimer_forward_now(&ctx->tmr, ctx->tintv);
323 old->it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
324 old->it_interval = ktime_to_timespec(ctx->tintv);
327 * Re-program the timer to the new value ...
329 ret = timerfd_setup(ctx, flags, new);
331 spin_unlock_irq(&ctx->wqh.lock);
336 static int do_timerfd_gettime(int ufd, struct itimerspec *t)
339 struct timerfd_ctx *ctx;
340 int ret = timerfd_fget(ufd, &f);
343 ctx = f.file->private_data;
345 spin_lock_irq(&ctx->wqh.lock);
346 if (ctx->expired && ctx->tintv.tv64) {
349 hrtimer_forward_now(&ctx->tmr, ctx->tintv) - 1;
350 hrtimer_restart(&ctx->tmr);
352 t->it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
353 t->it_interval = ktime_to_timespec(ctx->tintv);
354 spin_unlock_irq(&ctx->wqh.lock);
359 SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
360 const struct itimerspec __user *, utmr,
361 struct itimerspec __user *, otmr)
363 struct itimerspec new, old;
366 if (copy_from_user(&new, utmr, sizeof(new)))
368 ret = do_timerfd_settime(ufd, flags, &new, &old);
371 if (otmr && copy_to_user(otmr, &old, sizeof(old)))
377 SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct itimerspec __user *, otmr)
379 struct itimerspec kotmr;
380 int ret = do_timerfd_gettime(ufd, &kotmr);
383 return copy_to_user(otmr, &kotmr, sizeof(kotmr)) ? -EFAULT: 0;
387 COMPAT_SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
388 const struct compat_itimerspec __user *, utmr,
389 struct compat_itimerspec __user *, otmr)
391 struct itimerspec new, old;
394 if (get_compat_itimerspec(&new, utmr))
396 ret = do_timerfd_settime(ufd, flags, &new, &old);
399 if (otmr && put_compat_itimerspec(otmr, &old))
404 COMPAT_SYSCALL_DEFINE2(timerfd_gettime, int, ufd,
405 struct compat_itimerspec __user *, otmr)
407 struct itimerspec kotmr;
408 int ret = do_timerfd_gettime(ufd, &kotmr);
411 return put_compat_itimerspec(otmr, &kotmr) ? -EFAULT: 0;