4 * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org>
7 * Thanks to Thomas Gleixner for code reviews and useful comments.
11 #include <linux/alarmtimer.h>
12 #include <linux/file.h>
13 #include <linux/poll.h>
14 #include <linux/init.h>
16 #include <linux/sched.h>
17 #include <linux/kernel.h>
18 #include <linux/slab.h>
19 #include <linux/list.h>
20 #include <linux/spinlock.h>
21 #include <linux/time.h>
22 #include <linux/hrtimer.h>
23 #include <linux/anon_inodes.h>
24 #include <linux/timerfd.h>
25 #include <linux/syscalls.h>
26 #include <linux/compat.h>
27 #include <linux/rcupdate.h>
36 wait_queue_head_t wqh;
41 struct list_head clist;
45 static LIST_HEAD(cancel_list);
46 static DEFINE_SPINLOCK(cancel_lock);
48 static inline bool isalarm(struct timerfd_ctx *ctx)
50 return ctx->clockid == CLOCK_REALTIME_ALARM ||
51 ctx->clockid == CLOCK_BOOTTIME_ALARM;
55 * This gets called when the timer event triggers. We set the "expired"
56 * flag, but we do not re-arm the timer (in case it's necessary,
57 * tintv.tv64 != 0) until the timer is accessed.
59 static void timerfd_triggered(struct timerfd_ctx *ctx)
63 spin_lock_irqsave(&ctx->wqh.lock, flags);
66 wake_up_locked(&ctx->wqh);
67 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
70 static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr)
72 struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx,
74 timerfd_triggered(ctx);
75 return HRTIMER_NORESTART;
78 static enum alarmtimer_restart timerfd_alarmproc(struct alarm *alarm,
81 struct timerfd_ctx *ctx = container_of(alarm, struct timerfd_ctx,
83 timerfd_triggered(ctx);
84 return ALARMTIMER_NORESTART;
88 * Called when the clock was set to cancel the timers in the cancel
89 * list. This will wake up processes waiting on these timers. The
90 * wake-up requires ctx->ticks to be non zero, therefore we increment
91 * it before calling wake_up_locked().
93 void timerfd_clock_was_set(void)
95 ktime_t moffs = ktime_get_monotonic_offset();
96 struct timerfd_ctx *ctx;
100 list_for_each_entry_rcu(ctx, &cancel_list, clist) {
101 if (!ctx->might_cancel)
103 spin_lock_irqsave(&ctx->wqh.lock, flags);
104 if (ctx->moffs.tv64 != moffs.tv64) {
105 ctx->moffs.tv64 = KTIME_MAX;
107 wake_up_locked(&ctx->wqh);
109 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
114 static void timerfd_remove_cancel(struct timerfd_ctx *ctx)
116 if (ctx->might_cancel) {
117 ctx->might_cancel = false;
118 spin_lock(&cancel_lock);
119 list_del_rcu(&ctx->clist);
120 spin_unlock(&cancel_lock);
124 static bool timerfd_canceled(struct timerfd_ctx *ctx)
126 if (!ctx->might_cancel || ctx->moffs.tv64 != KTIME_MAX)
128 ctx->moffs = ktime_get_monotonic_offset();
132 static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags)
134 if ((ctx->clockid == CLOCK_REALTIME ||
135 ctx->clockid == CLOCK_REALTIME_ALARM) &&
136 (flags & TFD_TIMER_ABSTIME) && (flags & TFD_TIMER_CANCEL_ON_SET)) {
137 if (!ctx->might_cancel) {
138 ctx->might_cancel = true;
139 spin_lock(&cancel_lock);
140 list_add_rcu(&ctx->clist, &cancel_list);
141 spin_unlock(&cancel_lock);
143 } else if (ctx->might_cancel) {
144 timerfd_remove_cancel(ctx);
148 static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
153 remaining = alarm_expires_remaining(&ctx->t.alarm);
155 remaining = hrtimer_expires_remaining(&ctx->t.tmr);
157 return remaining.tv64 < 0 ? ktime_set(0, 0): remaining;
160 static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
161 const struct itimerspec *ktmr)
163 enum hrtimer_mode htmode;
165 int clockid = ctx->clockid;
167 htmode = (flags & TFD_TIMER_ABSTIME) ?
168 HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
170 texp = timespec_to_ktime(ktmr->it_value);
173 ctx->tintv = timespec_to_ktime(ktmr->it_interval);
176 alarm_init(&ctx->t.alarm,
177 ctx->clockid == CLOCK_REALTIME_ALARM ?
178 ALARM_REALTIME : ALARM_BOOTTIME,
181 hrtimer_init(&ctx->t.tmr, clockid, htmode);
182 hrtimer_set_expires(&ctx->t.tmr, texp);
183 ctx->t.tmr.function = timerfd_tmrproc;
186 if (texp.tv64 != 0) {
188 if (flags & TFD_TIMER_ABSTIME)
189 alarm_start(&ctx->t.alarm, texp);
191 alarm_start_relative(&ctx->t.alarm, texp);
193 hrtimer_start(&ctx->t.tmr, texp, htmode);
196 if (timerfd_canceled(ctx))
202 static int timerfd_release(struct inode *inode, struct file *file)
204 struct timerfd_ctx *ctx = file->private_data;
206 timerfd_remove_cancel(ctx);
209 alarm_cancel(&ctx->t.alarm);
211 hrtimer_cancel(&ctx->t.tmr);
216 static unsigned int timerfd_poll(struct file *file, poll_table *wait)
218 struct timerfd_ctx *ctx = file->private_data;
219 unsigned int events = 0;
222 poll_wait(file, &ctx->wqh, wait);
224 spin_lock_irqsave(&ctx->wqh.lock, flags);
227 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
232 static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
235 struct timerfd_ctx *ctx = file->private_data;
239 if (count < sizeof(ticks))
241 spin_lock_irq(&ctx->wqh.lock);
242 if (file->f_flags & O_NONBLOCK)
245 res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
248 * If clock has changed, we do not care about the
249 * ticks and we do not rearm the timer. Userspace must
252 if (timerfd_canceled(ctx)) {
261 if (ctx->expired && ctx->tintv.tv64) {
263 * If tintv.tv64 != 0, this is a periodic timer that
264 * needs to be re-armed. We avoid doing it in the timer
265 * callback to avoid DoS attacks specifying a very
266 * short timer period.
269 ticks += alarm_forward_now(
270 &ctx->t.alarm, ctx->tintv) - 1;
271 alarm_restart(&ctx->t.alarm);
273 ticks += hrtimer_forward_now(&ctx->t.tmr,
275 hrtimer_restart(&ctx->t.tmr);
281 spin_unlock_irq(&ctx->wqh.lock);
283 res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
287 static const struct file_operations timerfd_fops = {
288 .release = timerfd_release,
289 .poll = timerfd_poll,
290 .read = timerfd_read,
291 .llseek = noop_llseek,
294 static int timerfd_fget(int fd, struct fd *p)
296 struct fd f = fdget(fd);
299 if (f.file->f_op != &timerfd_fops) {
307 SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
310 struct timerfd_ctx *ctx;
312 /* Check the TFD_* constants for consistency. */
313 BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
314 BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
316 if ((flags & ~TFD_CREATE_FLAGS) ||
317 (clockid != CLOCK_MONOTONIC &&
318 clockid != CLOCK_REALTIME &&
319 clockid != CLOCK_REALTIME_ALARM &&
320 clockid != CLOCK_BOOTTIME &&
321 clockid != CLOCK_BOOTTIME_ALARM))
324 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
328 init_waitqueue_head(&ctx->wqh);
329 ctx->clockid = clockid;
332 alarm_init(&ctx->t.alarm,
333 ctx->clockid == CLOCK_REALTIME_ALARM ?
334 ALARM_REALTIME : ALARM_BOOTTIME,
337 hrtimer_init(&ctx->t.tmr, clockid, HRTIMER_MODE_ABS);
339 ctx->moffs = ktime_get_monotonic_offset();
341 ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx,
342 O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
349 static int do_timerfd_settime(int ufd, int flags,
350 const struct itimerspec *new,
351 struct itimerspec *old)
354 struct timerfd_ctx *ctx;
357 if ((flags & ~TFD_SETTIME_FLAGS) ||
358 !timespec_valid(&new->it_value) ||
359 !timespec_valid(&new->it_interval))
362 ret = timerfd_fget(ufd, &f);
365 ctx = f.file->private_data;
367 timerfd_setup_cancel(ctx, flags);
370 * We need to stop the existing timer before reprogramming
371 * it to the new values.
374 spin_lock_irq(&ctx->wqh.lock);
377 if (alarm_try_to_cancel(&ctx->t.alarm) >= 0)
380 if (hrtimer_try_to_cancel(&ctx->t.tmr) >= 0)
383 spin_unlock_irq(&ctx->wqh.lock);
388 * If the timer is expired and it's periodic, we need to advance it
389 * because the caller may want to know the previous expiration time.
390 * We do not update "ticks" and "expired" since the timer will be
391 * re-programmed again in the following timerfd_setup() call.
393 if (ctx->expired && ctx->tintv.tv64) {
395 alarm_forward_now(&ctx->t.alarm, ctx->tintv);
397 hrtimer_forward_now(&ctx->t.tmr, ctx->tintv);
400 old->it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
401 old->it_interval = ktime_to_timespec(ctx->tintv);
404 * Re-program the timer to the new value ...
406 ret = timerfd_setup(ctx, flags, new);
408 spin_unlock_irq(&ctx->wqh.lock);
413 static int do_timerfd_gettime(int ufd, struct itimerspec *t)
416 struct timerfd_ctx *ctx;
417 int ret = timerfd_fget(ufd, &f);
420 ctx = f.file->private_data;
422 spin_lock_irq(&ctx->wqh.lock);
423 if (ctx->expired && ctx->tintv.tv64) {
429 &ctx->t.alarm, ctx->tintv) - 1;
430 alarm_restart(&ctx->t.alarm);
433 hrtimer_forward_now(&ctx->t.tmr, ctx->tintv)
435 hrtimer_restart(&ctx->t.tmr);
438 t->it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
439 t->it_interval = ktime_to_timespec(ctx->tintv);
440 spin_unlock_irq(&ctx->wqh.lock);
445 SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
446 const struct itimerspec __user *, utmr,
447 struct itimerspec __user *, otmr)
449 struct itimerspec new, old;
452 if (copy_from_user(&new, utmr, sizeof(new)))
454 ret = do_timerfd_settime(ufd, flags, &new, &old);
457 if (otmr && copy_to_user(otmr, &old, sizeof(old)))
463 SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct itimerspec __user *, otmr)
465 struct itimerspec kotmr;
466 int ret = do_timerfd_gettime(ufd, &kotmr);
469 return copy_to_user(otmr, &kotmr, sizeof(kotmr)) ? -EFAULT: 0;
473 COMPAT_SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
474 const struct compat_itimerspec __user *, utmr,
475 struct compat_itimerspec __user *, otmr)
477 struct itimerspec new, old;
480 if (get_compat_itimerspec(&new, utmr))
482 ret = do_timerfd_settime(ufd, flags, &new, &old);
485 if (otmr && put_compat_itimerspec(otmr, &old))
490 COMPAT_SYSCALL_DEFINE2(timerfd_gettime, int, ufd,
491 struct compat_itimerspec __user *, otmr)
493 struct itimerspec kotmr;
494 int ret = do_timerfd_gettime(ufd, &kotmr);
497 return put_compat_itimerspec(otmr, &kotmr) ? -EFAULT: 0;