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 atomic_t instance_count = ATOMIC_INIT(0);
49 static LIST_HEAD(cancel_list);
50 static DEFINE_SPINLOCK(cancel_lock);
52 static inline bool isalarm(struct timerfd_ctx *ctx)
54 return ctx->clockid == CLOCK_REALTIME_ALARM ||
55 ctx->clockid == CLOCK_BOOTTIME_ALARM;
59 * This gets called when the timer event triggers. We set the "expired"
60 * flag, but we do not re-arm the timer (in case it's necessary,
61 * tintv.tv64 != 0) until the timer is accessed.
63 static void timerfd_triggered(struct timerfd_ctx *ctx)
67 spin_lock_irqsave(&ctx->wqh.lock, flags);
70 wake_up_locked(&ctx->wqh);
71 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
74 static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr)
76 struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx,
78 timerfd_triggered(ctx);
79 return HRTIMER_NORESTART;
82 static enum alarmtimer_restart timerfd_alarmproc(struct alarm *alarm,
85 struct timerfd_ctx *ctx = container_of(alarm, struct timerfd_ctx,
87 timerfd_triggered(ctx);
88 return ALARMTIMER_NORESTART;
92 * Called when the clock was set to cancel the timers in the cancel
93 * list. This will wake up processes waiting on these timers. The
94 * wake-up requires ctx->ticks to be non zero, therefore we increment
95 * it before calling wake_up_locked().
97 void timerfd_clock_was_set(void)
99 ktime_t moffs = ktime_mono_to_real((ktime_t){ .tv64 = 0 });
100 struct timerfd_ctx *ctx;
104 list_for_each_entry_rcu(ctx, &cancel_list, clist) {
105 if (!ctx->might_cancel)
107 spin_lock_irqsave(&ctx->wqh.lock, flags);
108 if (ctx->moffs.tv64 != moffs.tv64) {
109 ctx->moffs.tv64 = KTIME_MAX;
111 wake_up_locked(&ctx->wqh);
113 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
118 static void __timerfd_remove_cancel(struct timerfd_ctx *ctx)
120 if (ctx->might_cancel) {
121 ctx->might_cancel = false;
122 spin_lock(&cancel_lock);
123 list_del_rcu(&ctx->clist);
124 spin_unlock(&cancel_lock);
128 static void timerfd_remove_cancel(struct timerfd_ctx *ctx)
130 spin_lock(&ctx->cancel_lock);
131 __timerfd_remove_cancel(ctx);
132 spin_unlock(&ctx->cancel_lock);
135 static bool timerfd_canceled(struct timerfd_ctx *ctx)
137 if (!ctx->might_cancel || ctx->moffs.tv64 != KTIME_MAX)
139 ctx->moffs = ktime_mono_to_real((ktime_t){ .tv64 = 0 });
143 static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags)
145 spin_lock(&ctx->cancel_lock);
146 if ((ctx->clockid == CLOCK_REALTIME ||
147 ctx->clockid == CLOCK_REALTIME_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;
180 htmode = (flags & TFD_TIMER_ABSTIME) ?
181 HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
183 texp = timespec_to_ktime(ktmr->it_value);
186 ctx->tintv = timespec_to_ktime(ktmr->it_interval);
189 alarm_init(&ctx->t.alarm,
190 ctx->clockid == CLOCK_REALTIME_ALARM ?
191 ALARM_REALTIME : ALARM_BOOTTIME,
194 hrtimer_init(&ctx->t.tmr, clockid, htmode);
195 hrtimer_set_expires(&ctx->t.tmr, texp);
196 ctx->t.tmr.function = timerfd_tmrproc;
199 if (texp.tv64 != 0) {
201 if (flags & TFD_TIMER_ABSTIME)
202 alarm_start(&ctx->t.alarm, texp);
204 alarm_start_relative(&ctx->t.alarm, texp);
206 hrtimer_start(&ctx->t.tmr, texp, htmode);
209 if (timerfd_canceled(ctx))
213 ctx->settime_flags = flags & TFD_SETTIME_FLAGS;
217 static int timerfd_release(struct inode *inode, struct file *file)
219 struct timerfd_ctx *ctx = file->private_data;
221 timerfd_remove_cancel(ctx);
224 alarm_cancel(&ctx->t.alarm);
226 hrtimer_cancel(&ctx->t.tmr);
231 static unsigned int timerfd_poll(struct file *file, poll_table *wait)
233 struct timerfd_ctx *ctx = file->private_data;
234 unsigned int events = 0;
237 poll_wait(file, &ctx->wqh, wait);
239 spin_lock_irqsave(&ctx->wqh.lock, flags);
242 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
247 static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
250 struct timerfd_ctx *ctx = file->private_data;
254 if (count < sizeof(ticks))
256 spin_lock_irq(&ctx->wqh.lock);
257 if (file->f_flags & O_NONBLOCK)
260 res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
263 * If clock has changed, we do not care about the
264 * ticks and we do not rearm the timer. Userspace must
267 if (timerfd_canceled(ctx)) {
276 if (ctx->expired && ctx->tintv.tv64) {
278 * If tintv.tv64 != 0, this is a periodic timer that
279 * needs to be re-armed. We avoid doing it in the timer
280 * callback to avoid DoS attacks specifying a very
281 * short timer period.
284 ticks += alarm_forward_now(
285 &ctx->t.alarm, ctx->tintv) - 1;
286 alarm_restart(&ctx->t.alarm);
288 ticks += hrtimer_forward_now(&ctx->t.tmr,
290 hrtimer_restart(&ctx->t.tmr);
296 spin_unlock_irq(&ctx->wqh.lock);
298 res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
302 #ifdef CONFIG_PROC_FS
303 static void timerfd_show(struct seq_file *m, struct file *file)
305 struct timerfd_ctx *ctx = file->private_data;
308 spin_lock_irq(&ctx->wqh.lock);
309 t.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
310 t.it_interval = ktime_to_timespec(ctx->tintv);
311 spin_unlock_irq(&ctx->wqh.lock);
316 "settime flags: 0%o\n"
317 "it_value: (%llu, %llu)\n"
318 "it_interval: (%llu, %llu)\n",
320 (unsigned long long)ctx->ticks,
322 (unsigned long long)t.it_value.tv_sec,
323 (unsigned long long)t.it_value.tv_nsec,
324 (unsigned long long)t.it_interval.tv_sec,
325 (unsigned long long)t.it_interval.tv_nsec);
328 #define timerfd_show NULL
331 #ifdef CONFIG_CHECKPOINT_RESTORE
332 static long timerfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
334 struct timerfd_ctx *ctx = file->private_data;
338 case TFD_IOC_SET_TICKS: {
341 if (copy_from_user(&ticks, (u64 __user *)arg, sizeof(ticks)))
346 spin_lock_irq(&ctx->wqh.lock);
347 if (!timerfd_canceled(ctx)) {
349 wake_up_locked(&ctx->wqh);
352 spin_unlock_irq(&ctx->wqh.lock);
363 #define timerfd_ioctl NULL
366 static const struct file_operations timerfd_fops = {
367 .release = timerfd_release,
368 .poll = timerfd_poll,
369 .read = timerfd_read,
370 .llseek = noop_llseek,
371 .show_fdinfo = timerfd_show,
372 .unlocked_ioctl = timerfd_ioctl,
375 static int timerfd_fget(int fd, struct fd *p)
377 struct fd f = fdget(fd);
380 if (f.file->f_op != &timerfd_fops) {
388 SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
391 struct timerfd_ctx *ctx;
392 char task_comm_buf[TASK_COMM_LEN];
393 char file_name_buf[32];
396 /* Check the TFD_* constants for consistency. */
397 BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
398 BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
400 if ((flags & ~TFD_CREATE_FLAGS) ||
401 (clockid != CLOCK_MONOTONIC &&
402 clockid != CLOCK_REALTIME &&
403 clockid != CLOCK_REALTIME_ALARM &&
404 clockid != CLOCK_BOOTTIME &&
405 clockid != CLOCK_BOOTTIME_ALARM))
408 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
412 init_waitqueue_head(&ctx->wqh);
413 spin_lock_init(&ctx->cancel_lock);
414 ctx->clockid = clockid;
417 alarm_init(&ctx->t.alarm,
418 ctx->clockid == CLOCK_REALTIME_ALARM ?
419 ALARM_REALTIME : ALARM_BOOTTIME,
422 hrtimer_init(&ctx->t.tmr, clockid, HRTIMER_MODE_ABS);
424 ctx->moffs = ktime_mono_to_real((ktime_t){ .tv64 = 0 });
426 instance = atomic_inc_return(&instance_count);
427 get_task_comm(task_comm_buf, current);
428 snprintf(file_name_buf, sizeof(file_name_buf), "[timerfd%d_%.*s]",
429 instance, (int)sizeof(task_comm_buf), task_comm_buf);
431 ufd = anon_inode_getfd(file_name_buf, &timerfd_fops, ctx,
432 O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
439 static int do_timerfd_settime(int ufd, int flags,
440 const struct itimerspec *new,
441 struct itimerspec *old)
444 struct timerfd_ctx *ctx;
447 if ((flags & ~TFD_SETTIME_FLAGS) ||
448 !timespec_valid(&new->it_value) ||
449 !timespec_valid(&new->it_interval))
452 ret = timerfd_fget(ufd, &f);
455 ctx = f.file->private_data;
457 timerfd_setup_cancel(ctx, flags);
460 * We need to stop the existing timer before reprogramming
461 * it to the new values.
464 spin_lock_irq(&ctx->wqh.lock);
467 if (alarm_try_to_cancel(&ctx->t.alarm) >= 0)
470 if (hrtimer_try_to_cancel(&ctx->t.tmr) >= 0)
473 spin_unlock_irq(&ctx->wqh.lock);
478 * If the timer is expired and it's periodic, we need to advance it
479 * because the caller may want to know the previous expiration time.
480 * We do not update "ticks" and "expired" since the timer will be
481 * re-programmed again in the following timerfd_setup() call.
483 if (ctx->expired && ctx->tintv.tv64) {
485 alarm_forward_now(&ctx->t.alarm, ctx->tintv);
487 hrtimer_forward_now(&ctx->t.tmr, ctx->tintv);
490 old->it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
491 old->it_interval = ktime_to_timespec(ctx->tintv);
494 * Re-program the timer to the new value ...
496 ret = timerfd_setup(ctx, flags, new);
498 spin_unlock_irq(&ctx->wqh.lock);
503 static int do_timerfd_gettime(int ufd, struct itimerspec *t)
506 struct timerfd_ctx *ctx;
507 int ret = timerfd_fget(ufd, &f);
510 ctx = f.file->private_data;
512 spin_lock_irq(&ctx->wqh.lock);
513 if (ctx->expired && ctx->tintv.tv64) {
519 &ctx->t.alarm, ctx->tintv) - 1;
520 alarm_restart(&ctx->t.alarm);
523 hrtimer_forward_now(&ctx->t.tmr, ctx->tintv)
525 hrtimer_restart(&ctx->t.tmr);
528 t->it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
529 t->it_interval = ktime_to_timespec(ctx->tintv);
530 spin_unlock_irq(&ctx->wqh.lock);
535 SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
536 const struct itimerspec __user *, utmr,
537 struct itimerspec __user *, otmr)
539 struct itimerspec new, old;
542 if (copy_from_user(&new, utmr, sizeof(new)))
544 ret = do_timerfd_settime(ufd, flags, &new, &old);
547 if (otmr && copy_to_user(otmr, &old, sizeof(old)))
553 SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct itimerspec __user *, otmr)
555 struct itimerspec kotmr;
556 int ret = do_timerfd_gettime(ufd, &kotmr);
559 return copy_to_user(otmr, &kotmr, sizeof(kotmr)) ? -EFAULT: 0;
563 COMPAT_SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
564 const struct compat_itimerspec __user *, utmr,
565 struct compat_itimerspec __user *, otmr)
567 struct itimerspec new, old;
570 if (get_compat_itimerspec(&new, utmr))
572 ret = do_timerfd_settime(ufd, flags, &new, &old);
575 if (otmr && put_compat_itimerspec(otmr, &old))
580 COMPAT_SYSCALL_DEFINE2(timerfd_gettime, int, ufd,
581 struct compat_itimerspec __user *, otmr)
583 struct itimerspec kotmr;
584 int ret = do_timerfd_gettime(ufd, &kotmr);
587 return put_compat_itimerspec(otmr, &kotmr) ? -EFAULT: 0;