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;
39 short unsigned expired;
40 short unsigned settime_flags; /* to show in fdinfo */
42 struct list_head clist;
43 spinlock_t cancel_lock;
47 static LIST_HEAD(cancel_list);
48 static DEFINE_SPINLOCK(cancel_lock);
50 static inline bool isalarm(struct timerfd_ctx *ctx)
52 return ctx->clockid == CLOCK_REALTIME_ALARM ||
53 ctx->clockid == CLOCK_BOOTTIME_ALARM ||
54 ctx->clockid == CLOCK_POWEROFF_ALARM;
58 * This gets called when the timer event triggers. We set the "expired"
59 * flag, but we do not re-arm the timer (in case it's necessary,
60 * tintv.tv64 != 0) until the timer is accessed.
62 static void timerfd_triggered(struct timerfd_ctx *ctx)
66 spin_lock_irqsave(&ctx->wqh.lock, flags);
69 wake_up_locked(&ctx->wqh);
70 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
73 static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr)
75 struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx,
77 timerfd_triggered(ctx);
78 return HRTIMER_NORESTART;
81 static enum alarmtimer_restart timerfd_alarmproc(struct alarm *alarm,
84 struct timerfd_ctx *ctx = container_of(alarm, struct timerfd_ctx,
86 timerfd_triggered(ctx);
87 return ALARMTIMER_NORESTART;
91 * Called when the clock was set to cancel the timers in the cancel
92 * list. This will wake up processes waiting on these timers. The
93 * wake-up requires ctx->ticks to be non zero, therefore we increment
94 * it before calling wake_up_locked().
96 void timerfd_clock_was_set(void)
98 ktime_t moffs = ktime_mono_to_real((ktime_t){ .tv64 = 0 });
99 struct timerfd_ctx *ctx;
103 list_for_each_entry_rcu(ctx, &cancel_list, clist) {
104 if (!ctx->might_cancel)
106 spin_lock_irqsave(&ctx->wqh.lock, flags);
107 if (ctx->moffs.tv64 != moffs.tv64) {
108 ctx->moffs.tv64 = KTIME_MAX;
110 wake_up_locked(&ctx->wqh);
112 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
117 static void __timerfd_remove_cancel(struct timerfd_ctx *ctx)
119 if (ctx->might_cancel) {
120 ctx->might_cancel = false;
121 spin_lock(&cancel_lock);
122 list_del_rcu(&ctx->clist);
123 spin_unlock(&cancel_lock);
127 static void timerfd_remove_cancel(struct timerfd_ctx *ctx)
129 spin_lock(&ctx->cancel_lock);
130 __timerfd_remove_cancel(ctx);
131 spin_unlock(&ctx->cancel_lock);
134 static bool timerfd_canceled(struct timerfd_ctx *ctx)
136 if (!ctx->might_cancel || ctx->moffs.tv64 != KTIME_MAX)
138 ctx->moffs = ktime_mono_to_real((ktime_t){ .tv64 = 0 });
142 static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags)
144 spin_lock(&ctx->cancel_lock);
145 if ((ctx->clockid == CLOCK_REALTIME ||
146 ctx->clockid == CLOCK_REALTIME_ALARM ||
147 ctx->clockid == CLOCK_POWEROFF_ALARM) &&
148 (flags & TFD_TIMER_ABSTIME) && (flags & TFD_TIMER_CANCEL_ON_SET)) {
149 if (!ctx->might_cancel) {
150 ctx->might_cancel = true;
151 spin_lock(&cancel_lock);
152 list_add_rcu(&ctx->clist, &cancel_list);
153 spin_unlock(&cancel_lock);
156 __timerfd_remove_cancel(ctx);
158 spin_unlock(&ctx->cancel_lock);
161 static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
166 remaining = alarm_expires_remaining(&ctx->t.alarm);
168 remaining = hrtimer_expires_remaining_adjusted(&ctx->t.tmr);
170 return remaining.tv64 < 0 ? ktime_set(0, 0): remaining;
173 static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
174 const struct itimerspec *ktmr)
176 enum hrtimer_mode htmode;
178 int clockid = ctx->clockid;
179 enum alarmtimer_type type;
181 htmode = (flags & TFD_TIMER_ABSTIME) ?
182 HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
184 texp = timespec_to_ktime(ktmr->it_value);
187 ctx->tintv = timespec_to_ktime(ktmr->it_interval);
190 type = clock2alarm(ctx->clockid);
191 alarm_init(&ctx->t.alarm, type, timerfd_alarmproc);
193 hrtimer_init(&ctx->t.tmr, clockid, htmode);
194 hrtimer_set_expires(&ctx->t.tmr, texp);
195 ctx->t.tmr.function = timerfd_tmrproc;
198 if (texp.tv64 != 0) {
200 if (flags & TFD_TIMER_ABSTIME)
201 alarm_start(&ctx->t.alarm, texp);
203 alarm_start_relative(&ctx->t.alarm, texp);
205 hrtimer_start(&ctx->t.tmr, texp, htmode);
208 if (timerfd_canceled(ctx))
212 ctx->settime_flags = flags & TFD_SETTIME_FLAGS;
216 static int timerfd_release(struct inode *inode, struct file *file)
218 struct timerfd_ctx *ctx = file->private_data;
220 timerfd_remove_cancel(ctx);
223 alarm_cancel(&ctx->t.alarm);
225 hrtimer_cancel(&ctx->t.tmr);
230 static unsigned int timerfd_poll(struct file *file, poll_table *wait)
232 struct timerfd_ctx *ctx = file->private_data;
233 unsigned int events = 0;
236 poll_wait(file, &ctx->wqh, wait);
238 spin_lock_irqsave(&ctx->wqh.lock, flags);
241 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
246 static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
249 struct timerfd_ctx *ctx = file->private_data;
253 if (count < sizeof(ticks))
255 spin_lock_irq(&ctx->wqh.lock);
256 if (file->f_flags & O_NONBLOCK)
259 res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
262 * If clock has changed, we do not care about the
263 * ticks and we do not rearm the timer. Userspace must
266 if (timerfd_canceled(ctx)) {
275 if (ctx->expired && ctx->tintv.tv64) {
277 * If tintv.tv64 != 0, this is a periodic timer that
278 * needs to be re-armed. We avoid doing it in the timer
279 * callback to avoid DoS attacks specifying a very
280 * short timer period.
283 ticks += alarm_forward_now(
284 &ctx->t.alarm, ctx->tintv) - 1;
285 alarm_restart(&ctx->t.alarm);
287 ticks += hrtimer_forward_now(&ctx->t.tmr,
289 hrtimer_restart(&ctx->t.tmr);
295 spin_unlock_irq(&ctx->wqh.lock);
297 res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
301 #ifdef CONFIG_PROC_FS
302 static void timerfd_show(struct seq_file *m, struct file *file)
304 struct timerfd_ctx *ctx = file->private_data;
307 spin_lock_irq(&ctx->wqh.lock);
308 t.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
309 t.it_interval = ktime_to_timespec(ctx->tintv);
310 spin_unlock_irq(&ctx->wqh.lock);
315 "settime flags: 0%o\n"
316 "it_value: (%llu, %llu)\n"
317 "it_interval: (%llu, %llu)\n",
319 (unsigned long long)ctx->ticks,
321 (unsigned long long)t.it_value.tv_sec,
322 (unsigned long long)t.it_value.tv_nsec,
323 (unsigned long long)t.it_interval.tv_sec,
324 (unsigned long long)t.it_interval.tv_nsec);
327 #define timerfd_show NULL
330 #ifdef CONFIG_CHECKPOINT_RESTORE
331 static long timerfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
333 struct timerfd_ctx *ctx = file->private_data;
337 case TFD_IOC_SET_TICKS: {
340 if (copy_from_user(&ticks, (u64 __user *)arg, sizeof(ticks)))
345 spin_lock_irq(&ctx->wqh.lock);
346 if (!timerfd_canceled(ctx)) {
348 wake_up_locked(&ctx->wqh);
351 spin_unlock_irq(&ctx->wqh.lock);
362 #define timerfd_ioctl NULL
365 static const struct file_operations timerfd_fops = {
366 .release = timerfd_release,
367 .poll = timerfd_poll,
368 .read = timerfd_read,
369 .llseek = noop_llseek,
370 .show_fdinfo = timerfd_show,
371 .unlocked_ioctl = timerfd_ioctl,
374 static int timerfd_fget(int fd, struct fd *p)
376 struct fd f = fdget(fd);
379 if (f.file->f_op != &timerfd_fops) {
387 SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
390 struct timerfd_ctx *ctx;
391 enum alarmtimer_type type;
393 /* Check the TFD_* constants for consistency. */
394 BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
395 BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
397 if ((flags & ~TFD_CREATE_FLAGS) ||
398 (clockid != CLOCK_MONOTONIC &&
399 clockid != CLOCK_REALTIME &&
400 clockid != CLOCK_REALTIME_ALARM &&
401 clockid != CLOCK_BOOTTIME &&
402 clockid != CLOCK_BOOTTIME_ALARM &&
403 clockid != CLOCK_POWEROFF_ALARM))
406 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
410 init_waitqueue_head(&ctx->wqh);
411 spin_lock_init(&ctx->cancel_lock);
412 ctx->clockid = clockid;
415 type = clock2alarm(ctx->clockid);
416 alarm_init(&ctx->t.alarm, type, timerfd_alarmproc);
418 hrtimer_init(&ctx->t.tmr, clockid, HRTIMER_MODE_ABS);
421 ctx->moffs = ktime_mono_to_real((ktime_t){ .tv64 = 0 });
423 ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx,
424 O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
431 static int do_timerfd_settime(int ufd, int flags,
432 const struct itimerspec *new,
433 struct itimerspec *old)
436 struct timerfd_ctx *ctx;
439 if ((flags & ~TFD_SETTIME_FLAGS) ||
440 !timespec_valid(&new->it_value) ||
441 !timespec_valid(&new->it_interval))
444 ret = timerfd_fget(ufd, &f);
447 ctx = f.file->private_data;
449 timerfd_setup_cancel(ctx, flags);
452 * We need to stop the existing timer before reprogramming
453 * it to the new values.
456 spin_lock_irq(&ctx->wqh.lock);
459 if (alarm_try_to_cancel(&ctx->t.alarm) >= 0)
462 if (hrtimer_try_to_cancel(&ctx->t.tmr) >= 0)
465 spin_unlock_irq(&ctx->wqh.lock);
470 * If the timer is expired and it's periodic, we need to advance it
471 * because the caller may want to know the previous expiration time.
472 * We do not update "ticks" and "expired" since the timer will be
473 * re-programmed again in the following timerfd_setup() call.
475 if (ctx->expired && ctx->tintv.tv64) {
477 alarm_forward_now(&ctx->t.alarm, ctx->tintv);
479 hrtimer_forward_now(&ctx->t.tmr, ctx->tintv);
482 old->it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
483 old->it_interval = ktime_to_timespec(ctx->tintv);
486 * Re-program the timer to the new value ...
488 ret = timerfd_setup(ctx, flags, new);
490 spin_unlock_irq(&ctx->wqh.lock);
492 if (ctx->clockid == CLOCK_POWEROFF_ALARM)
493 set_power_on_alarm();
499 static int do_timerfd_gettime(int ufd, struct itimerspec *t)
502 struct timerfd_ctx *ctx;
503 int ret = timerfd_fget(ufd, &f);
506 ctx = f.file->private_data;
508 spin_lock_irq(&ctx->wqh.lock);
509 if (ctx->expired && ctx->tintv.tv64) {
515 &ctx->t.alarm, ctx->tintv) - 1;
516 alarm_restart(&ctx->t.alarm);
519 hrtimer_forward_now(&ctx->t.tmr, ctx->tintv)
521 hrtimer_restart(&ctx->t.tmr);
524 t->it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
525 t->it_interval = ktime_to_timespec(ctx->tintv);
526 spin_unlock_irq(&ctx->wqh.lock);
531 SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
532 const struct itimerspec __user *, utmr,
533 struct itimerspec __user *, otmr)
535 struct itimerspec new, old;
538 if (copy_from_user(&new, utmr, sizeof(new)))
540 ret = do_timerfd_settime(ufd, flags, &new, &old);
543 if (otmr && copy_to_user(otmr, &old, sizeof(old)))
549 SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct itimerspec __user *, otmr)
551 struct itimerspec kotmr;
552 int ret = do_timerfd_gettime(ufd, &kotmr);
555 return copy_to_user(otmr, &kotmr, sizeof(kotmr)) ? -EFAULT: 0;
559 COMPAT_SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
560 const struct compat_itimerspec __user *, utmr,
561 struct compat_itimerspec __user *, otmr)
563 struct itimerspec new, old;
566 if (get_compat_itimerspec(&new, utmr))
568 ret = do_timerfd_settime(ufd, flags, &new, &old);
571 if (otmr && put_compat_itimerspec(otmr, &old))
576 COMPAT_SYSCALL_DEFINE2(timerfd_gettime, int, ufd,
577 struct compat_itimerspec __user *, otmr)
579 struct itimerspec kotmr;
580 int ret = do_timerfd_gettime(ufd, &kotmr);
583 return put_compat_itimerspec(otmr, &kotmr) ? -EFAULT: 0;