1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org>
8 * Thanks to Thomas Gleixner for code reviews and useful comments.
12 #include <linux/alarmtimer.h>
13 #include <linux/file.h>
14 #include <linux/poll.h>
15 #include <linux/init.h>
17 #include <linux/sched.h>
18 #include <linux/kernel.h>
19 #include <linux/slab.h>
20 #include <linux/list.h>
21 #include <linux/spinlock.h>
22 #include <linux/time.h>
23 #include <linux/hrtimer.h>
24 #include <linux/anon_inodes.h>
25 #include <linux/timerfd.h>
26 #include <linux/syscalls.h>
27 #include <linux/compat.h>
28 #include <linux/rcupdate.h>
29 #include <linux/time_namespace.h>
38 wait_queue_head_t wqh;
41 short unsigned expired;
42 short unsigned settime_flags; /* to show in fdinfo */
44 struct list_head clist;
45 spinlock_t cancel_lock;
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 != 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_poll(&ctx->wqh, EPOLLIN);
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(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 != moffs) {
109 ctx->moffs = KTIME_MAX;
111 wake_up_locked_poll(&ctx->wqh, EPOLLIN);
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 != KTIME_MAX)
139 ctx->moffs = ktime_mono_to_real(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 < 0 ? 0: remaining;
173 static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
174 const struct itimerspec64 *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 = timespec64_to_ktime(ktmr->it_value);
186 ctx->tintv = timespec64_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;
200 if (flags & TFD_TIMER_ABSTIME)
201 texp = timens_ktime_to_host(clockid, texp);
203 if (flags & TFD_TIMER_ABSTIME)
204 alarm_start(&ctx->t.alarm, texp);
206 alarm_start_relative(&ctx->t.alarm, texp);
208 hrtimer_start(&ctx->t.tmr, texp, htmode);
211 if (timerfd_canceled(ctx))
215 ctx->settime_flags = flags & TFD_SETTIME_FLAGS;
219 static int timerfd_release(struct inode *inode, struct file *file)
221 struct timerfd_ctx *ctx = file->private_data;
223 timerfd_remove_cancel(ctx);
226 alarm_cancel(&ctx->t.alarm);
228 hrtimer_cancel(&ctx->t.tmr);
233 static __poll_t timerfd_poll(struct file *file, poll_table *wait)
235 struct timerfd_ctx *ctx = file->private_data;
239 poll_wait(file, &ctx->wqh, wait);
241 spin_lock_irqsave(&ctx->wqh.lock, flags);
244 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
249 static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
252 struct timerfd_ctx *ctx = file->private_data;
256 if (count < sizeof(ticks))
258 spin_lock_irq(&ctx->wqh.lock);
259 if (file->f_flags & O_NONBLOCK)
262 res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
265 * If clock has changed, we do not care about the
266 * ticks and we do not rearm the timer. Userspace must
269 if (timerfd_canceled(ctx)) {
278 if (ctx->expired && ctx->tintv) {
280 * If tintv != 0, this is a periodic timer that
281 * needs to be re-armed. We avoid doing it in the timer
282 * callback to avoid DoS attacks specifying a very
283 * short timer period.
286 ticks += alarm_forward_now(
287 &ctx->t.alarm, ctx->tintv) - 1;
288 alarm_restart(&ctx->t.alarm);
290 ticks += hrtimer_forward_now(&ctx->t.tmr,
292 hrtimer_restart(&ctx->t.tmr);
298 spin_unlock_irq(&ctx->wqh.lock);
300 res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
304 #ifdef CONFIG_PROC_FS
305 static void timerfd_show(struct seq_file *m, struct file *file)
307 struct timerfd_ctx *ctx = file->private_data;
308 struct timespec64 value, interval;
310 spin_lock_irq(&ctx->wqh.lock);
311 value = ktime_to_timespec64(timerfd_get_remaining(ctx));
312 interval = ktime_to_timespec64(ctx->tintv);
313 spin_unlock_irq(&ctx->wqh.lock);
318 "settime flags: 0%o\n"
319 "it_value: (%llu, %llu)\n"
320 "it_interval: (%llu, %llu)\n",
322 (unsigned long long)ctx->ticks,
324 (unsigned long long)value.tv_sec,
325 (unsigned long long)value.tv_nsec,
326 (unsigned long long)interval.tv_sec,
327 (unsigned long long)interval.tv_nsec);
330 #define timerfd_show NULL
333 #ifdef CONFIG_CHECKPOINT_RESTORE
334 static long timerfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
336 struct timerfd_ctx *ctx = file->private_data;
340 case TFD_IOC_SET_TICKS: {
343 if (copy_from_user(&ticks, (u64 __user *)arg, sizeof(ticks)))
348 spin_lock_irq(&ctx->wqh.lock);
349 if (!timerfd_canceled(ctx)) {
351 wake_up_locked_poll(&ctx->wqh, EPOLLIN);
354 spin_unlock_irq(&ctx->wqh.lock);
365 #define timerfd_ioctl NULL
368 static const struct file_operations timerfd_fops = {
369 .release = timerfd_release,
370 .poll = timerfd_poll,
371 .read = timerfd_read,
372 .llseek = noop_llseek,
373 .show_fdinfo = timerfd_show,
374 .unlocked_ioctl = timerfd_ioctl,
377 static int timerfd_fget(int fd, struct fd *p)
379 struct fd f = fdget(fd);
382 if (f.file->f_op != &timerfd_fops) {
390 SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
393 struct timerfd_ctx *ctx;
395 /* Check the TFD_* constants for consistency. */
396 BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
397 BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
399 if ((flags & ~TFD_CREATE_FLAGS) ||
400 (clockid != CLOCK_MONOTONIC &&
401 clockid != CLOCK_REALTIME &&
402 clockid != CLOCK_REALTIME_ALARM &&
403 clockid != CLOCK_BOOTTIME &&
404 clockid != CLOCK_BOOTTIME_ALARM))
407 if ((clockid == CLOCK_REALTIME_ALARM ||
408 clockid == CLOCK_BOOTTIME_ALARM) &&
409 !capable(CAP_WAKE_ALARM))
412 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
416 init_waitqueue_head(&ctx->wqh);
417 spin_lock_init(&ctx->cancel_lock);
418 ctx->clockid = clockid;
421 alarm_init(&ctx->t.alarm,
422 ctx->clockid == CLOCK_REALTIME_ALARM ?
423 ALARM_REALTIME : ALARM_BOOTTIME,
426 hrtimer_init(&ctx->t.tmr, clockid, HRTIMER_MODE_ABS);
428 ctx->moffs = ktime_mono_to_real(0);
430 ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx,
431 O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
438 static int do_timerfd_settime(int ufd, int flags,
439 const struct itimerspec64 *new,
440 struct itimerspec64 *old)
443 struct timerfd_ctx *ctx;
446 if ((flags & ~TFD_SETTIME_FLAGS) ||
447 !itimerspec64_valid(new))
450 ret = timerfd_fget(ufd, &f);
453 ctx = f.file->private_data;
455 if (isalarm(ctx) && !capable(CAP_WAKE_ALARM)) {
460 timerfd_setup_cancel(ctx, flags);
463 * We need to stop the existing timer before reprogramming
464 * it to the new values.
467 spin_lock_irq(&ctx->wqh.lock);
470 if (alarm_try_to_cancel(&ctx->t.alarm) >= 0)
473 if (hrtimer_try_to_cancel(&ctx->t.tmr) >= 0)
476 spin_unlock_irq(&ctx->wqh.lock);
479 hrtimer_cancel_wait_running(&ctx->t.alarm.timer);
481 hrtimer_cancel_wait_running(&ctx->t.tmr);
485 * If the timer is expired and it's periodic, we need to advance it
486 * because the caller may want to know the previous expiration time.
487 * We do not update "ticks" and "expired" since the timer will be
488 * re-programmed again in the following timerfd_setup() call.
490 if (ctx->expired && ctx->tintv) {
492 alarm_forward_now(&ctx->t.alarm, ctx->tintv);
494 hrtimer_forward_now(&ctx->t.tmr, ctx->tintv);
497 old->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx));
498 old->it_interval = ktime_to_timespec64(ctx->tintv);
501 * Re-program the timer to the new value ...
503 ret = timerfd_setup(ctx, flags, new);
505 spin_unlock_irq(&ctx->wqh.lock);
510 static int do_timerfd_gettime(int ufd, struct itimerspec64 *t)
513 struct timerfd_ctx *ctx;
514 int ret = timerfd_fget(ufd, &f);
517 ctx = f.file->private_data;
519 spin_lock_irq(&ctx->wqh.lock);
520 if (ctx->expired && ctx->tintv) {
526 &ctx->t.alarm, ctx->tintv) - 1;
527 alarm_restart(&ctx->t.alarm);
530 hrtimer_forward_now(&ctx->t.tmr, ctx->tintv)
532 hrtimer_restart(&ctx->t.tmr);
535 t->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx));
536 t->it_interval = ktime_to_timespec64(ctx->tintv);
537 spin_unlock_irq(&ctx->wqh.lock);
542 SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
543 const struct __kernel_itimerspec __user *, utmr,
544 struct __kernel_itimerspec __user *, otmr)
546 struct itimerspec64 new, old;
549 if (get_itimerspec64(&new, utmr))
551 ret = do_timerfd_settime(ufd, flags, &new, &old);
554 if (otmr && put_itimerspec64(&old, otmr))
560 SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct __kernel_itimerspec __user *, otmr)
562 struct itimerspec64 kotmr;
563 int ret = do_timerfd_gettime(ufd, &kotmr);
566 return put_itimerspec64(&kotmr, otmr) ? -EFAULT : 0;
569 #ifdef CONFIG_COMPAT_32BIT_TIME
570 SYSCALL_DEFINE4(timerfd_settime32, int, ufd, int, flags,
571 const struct old_itimerspec32 __user *, utmr,
572 struct old_itimerspec32 __user *, otmr)
574 struct itimerspec64 new, old;
577 if (get_old_itimerspec32(&new, utmr))
579 ret = do_timerfd_settime(ufd, flags, &new, &old);
582 if (otmr && put_old_itimerspec32(&old, otmr))
587 SYSCALL_DEFINE2(timerfd_gettime32, int, ufd,
588 struct old_itimerspec32 __user *, otmr)
590 struct itimerspec64 kotmr;
591 int ret = do_timerfd_gettime(ufd, &kotmr);
594 return put_old_itimerspec32(&kotmr, otmr) ? -EFAULT : 0;