1 // SPDX-License-Identifier: GPL-2.0
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_resume_work(struct work_struct *work)
120 timerfd_clock_was_set();
123 static DECLARE_WORK(timerfd_work, timerfd_resume_work);
126 * Invoked from timekeeping_resume(). Defer the actual update to work so
127 * timerfd_clock_was_set() runs in task context.
129 void timerfd_resume(void)
131 schedule_work(&timerfd_work);
134 static void __timerfd_remove_cancel(struct timerfd_ctx *ctx)
136 if (ctx->might_cancel) {
137 ctx->might_cancel = false;
138 spin_lock(&cancel_lock);
139 list_del_rcu(&ctx->clist);
140 spin_unlock(&cancel_lock);
144 static void timerfd_remove_cancel(struct timerfd_ctx *ctx)
146 spin_lock(&ctx->cancel_lock);
147 __timerfd_remove_cancel(ctx);
148 spin_unlock(&ctx->cancel_lock);
151 static bool timerfd_canceled(struct timerfd_ctx *ctx)
153 if (!ctx->might_cancel || ctx->moffs != KTIME_MAX)
155 ctx->moffs = ktime_mono_to_real(0);
159 static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags)
161 spin_lock(&ctx->cancel_lock);
162 if ((ctx->clockid == CLOCK_REALTIME ||
163 ctx->clockid == CLOCK_REALTIME_ALARM) &&
164 (flags & TFD_TIMER_ABSTIME) && (flags & TFD_TIMER_CANCEL_ON_SET)) {
165 if (!ctx->might_cancel) {
166 ctx->might_cancel = true;
167 spin_lock(&cancel_lock);
168 list_add_rcu(&ctx->clist, &cancel_list);
169 spin_unlock(&cancel_lock);
172 __timerfd_remove_cancel(ctx);
174 spin_unlock(&ctx->cancel_lock);
177 static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
182 remaining = alarm_expires_remaining(&ctx->t.alarm);
184 remaining = hrtimer_expires_remaining_adjusted(&ctx->t.tmr);
186 return remaining < 0 ? 0: remaining;
189 static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
190 const struct itimerspec64 *ktmr)
192 enum hrtimer_mode htmode;
194 int clockid = ctx->clockid;
196 htmode = (flags & TFD_TIMER_ABSTIME) ?
197 HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
199 texp = timespec64_to_ktime(ktmr->it_value);
202 ctx->tintv = timespec64_to_ktime(ktmr->it_interval);
205 alarm_init(&ctx->t.alarm,
206 ctx->clockid == CLOCK_REALTIME_ALARM ?
207 ALARM_REALTIME : ALARM_BOOTTIME,
210 hrtimer_init(&ctx->t.tmr, clockid, htmode);
211 hrtimer_set_expires(&ctx->t.tmr, texp);
212 ctx->t.tmr.function = timerfd_tmrproc;
216 if (flags & TFD_TIMER_ABSTIME)
217 texp = timens_ktime_to_host(clockid, texp);
219 if (flags & TFD_TIMER_ABSTIME)
220 alarm_start(&ctx->t.alarm, texp);
222 alarm_start_relative(&ctx->t.alarm, texp);
224 hrtimer_start(&ctx->t.tmr, texp, htmode);
227 if (timerfd_canceled(ctx))
231 ctx->settime_flags = flags & TFD_SETTIME_FLAGS;
235 static int timerfd_release(struct inode *inode, struct file *file)
237 struct timerfd_ctx *ctx = file->private_data;
239 timerfd_remove_cancel(ctx);
242 alarm_cancel(&ctx->t.alarm);
244 hrtimer_cancel(&ctx->t.tmr);
249 static __poll_t timerfd_poll(struct file *file, poll_table *wait)
251 struct timerfd_ctx *ctx = file->private_data;
255 poll_wait(file, &ctx->wqh, wait);
257 spin_lock_irqsave(&ctx->wqh.lock, flags);
260 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
265 static ssize_t timerfd_read_iter(struct kiocb *iocb, struct iov_iter *to)
267 struct file *file = iocb->ki_filp;
268 struct timerfd_ctx *ctx = file->private_data;
272 if (iov_iter_count(to) < sizeof(ticks))
275 spin_lock_irq(&ctx->wqh.lock);
276 if (file->f_flags & O_NONBLOCK || iocb->ki_flags & IOCB_NOWAIT)
279 res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
282 * If clock has changed, we do not care about the
283 * ticks and we do not rearm the timer. Userspace must
286 if (timerfd_canceled(ctx)) {
295 if (ctx->expired && ctx->tintv) {
297 * If tintv != 0, this is a periodic timer that
298 * needs to be re-armed. We avoid doing it in the timer
299 * callback to avoid DoS attacks specifying a very
300 * short timer period.
303 ticks += alarm_forward_now(
304 &ctx->t.alarm, ctx->tintv) - 1;
305 alarm_restart(&ctx->t.alarm);
307 ticks += hrtimer_forward_now(&ctx->t.tmr,
309 hrtimer_restart(&ctx->t.tmr);
315 spin_unlock_irq(&ctx->wqh.lock);
317 res = copy_to_iter(&ticks, sizeof(ticks), to);
324 #ifdef CONFIG_PROC_FS
325 static void timerfd_show(struct seq_file *m, struct file *file)
327 struct timerfd_ctx *ctx = file->private_data;
328 struct timespec64 value, interval;
330 spin_lock_irq(&ctx->wqh.lock);
331 value = ktime_to_timespec64(timerfd_get_remaining(ctx));
332 interval = ktime_to_timespec64(ctx->tintv);
333 spin_unlock_irq(&ctx->wqh.lock);
338 "settime flags: 0%o\n"
339 "it_value: (%llu, %llu)\n"
340 "it_interval: (%llu, %llu)\n",
342 (unsigned long long)ctx->ticks,
344 (unsigned long long)value.tv_sec,
345 (unsigned long long)value.tv_nsec,
346 (unsigned long long)interval.tv_sec,
347 (unsigned long long)interval.tv_nsec);
350 #define timerfd_show NULL
353 #ifdef CONFIG_CHECKPOINT_RESTORE
354 static long timerfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
356 struct timerfd_ctx *ctx = file->private_data;
360 case TFD_IOC_SET_TICKS: {
363 if (copy_from_user(&ticks, (u64 __user *)arg, sizeof(ticks)))
368 spin_lock_irq(&ctx->wqh.lock);
369 if (!timerfd_canceled(ctx)) {
371 wake_up_locked_poll(&ctx->wqh, EPOLLIN);
374 spin_unlock_irq(&ctx->wqh.lock);
385 #define timerfd_ioctl NULL
388 static const struct file_operations timerfd_fops = {
389 .release = timerfd_release,
390 .poll = timerfd_poll,
391 .read_iter = timerfd_read_iter,
392 .llseek = noop_llseek,
393 .show_fdinfo = timerfd_show,
394 .unlocked_ioctl = timerfd_ioctl,
397 static int timerfd_fget(int fd, struct fd *p)
399 struct fd f = fdget(fd);
402 if (f.file->f_op != &timerfd_fops) {
410 SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
413 struct timerfd_ctx *ctx;
416 /* Check the TFD_* constants for consistency. */
417 BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
418 BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
420 if ((flags & ~TFD_CREATE_FLAGS) ||
421 (clockid != CLOCK_MONOTONIC &&
422 clockid != CLOCK_REALTIME &&
423 clockid != CLOCK_REALTIME_ALARM &&
424 clockid != CLOCK_BOOTTIME &&
425 clockid != CLOCK_BOOTTIME_ALARM))
428 if ((clockid == CLOCK_REALTIME_ALARM ||
429 clockid == CLOCK_BOOTTIME_ALARM) &&
430 !capable(CAP_WAKE_ALARM))
433 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
437 init_waitqueue_head(&ctx->wqh);
438 spin_lock_init(&ctx->cancel_lock);
439 ctx->clockid = clockid;
442 alarm_init(&ctx->t.alarm,
443 ctx->clockid == CLOCK_REALTIME_ALARM ?
444 ALARM_REALTIME : ALARM_BOOTTIME,
447 hrtimer_init(&ctx->t.tmr, clockid, HRTIMER_MODE_ABS);
449 ctx->moffs = ktime_mono_to_real(0);
451 ufd = get_unused_fd_flags(flags & TFD_SHARED_FCNTL_FLAGS);
457 file = anon_inode_getfile("[timerfd]", &timerfd_fops, ctx,
458 O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
462 return PTR_ERR(file);
465 file->f_mode |= FMODE_NOWAIT;
466 fd_install(ufd, file);
470 static int do_timerfd_settime(int ufd, int flags,
471 const struct itimerspec64 *new,
472 struct itimerspec64 *old)
475 struct timerfd_ctx *ctx;
478 if ((flags & ~TFD_SETTIME_FLAGS) ||
479 !itimerspec64_valid(new))
482 ret = timerfd_fget(ufd, &f);
485 ctx = f.file->private_data;
487 if (isalarm(ctx) && !capable(CAP_WAKE_ALARM)) {
492 timerfd_setup_cancel(ctx, flags);
495 * We need to stop the existing timer before reprogramming
496 * it to the new values.
499 spin_lock_irq(&ctx->wqh.lock);
502 if (alarm_try_to_cancel(&ctx->t.alarm) >= 0)
505 if (hrtimer_try_to_cancel(&ctx->t.tmr) >= 0)
508 spin_unlock_irq(&ctx->wqh.lock);
511 hrtimer_cancel_wait_running(&ctx->t.alarm.timer);
513 hrtimer_cancel_wait_running(&ctx->t.tmr);
517 * If the timer is expired and it's periodic, we need to advance it
518 * because the caller may want to know the previous expiration time.
519 * We do not update "ticks" and "expired" since the timer will be
520 * re-programmed again in the following timerfd_setup() call.
522 if (ctx->expired && ctx->tintv) {
524 alarm_forward_now(&ctx->t.alarm, ctx->tintv);
526 hrtimer_forward_now(&ctx->t.tmr, ctx->tintv);
529 old->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx));
530 old->it_interval = ktime_to_timespec64(ctx->tintv);
533 * Re-program the timer to the new value ...
535 ret = timerfd_setup(ctx, flags, new);
537 spin_unlock_irq(&ctx->wqh.lock);
542 static int do_timerfd_gettime(int ufd, struct itimerspec64 *t)
545 struct timerfd_ctx *ctx;
546 int ret = timerfd_fget(ufd, &f);
549 ctx = f.file->private_data;
551 spin_lock_irq(&ctx->wqh.lock);
552 if (ctx->expired && ctx->tintv) {
558 &ctx->t.alarm, ctx->tintv) - 1;
559 alarm_restart(&ctx->t.alarm);
562 hrtimer_forward_now(&ctx->t.tmr, ctx->tintv)
564 hrtimer_restart(&ctx->t.tmr);
567 t->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx));
568 t->it_interval = ktime_to_timespec64(ctx->tintv);
569 spin_unlock_irq(&ctx->wqh.lock);
574 SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
575 const struct __kernel_itimerspec __user *, utmr,
576 struct __kernel_itimerspec __user *, otmr)
578 struct itimerspec64 new, old;
581 if (get_itimerspec64(&new, utmr))
583 ret = do_timerfd_settime(ufd, flags, &new, &old);
586 if (otmr && put_itimerspec64(&old, otmr))
592 SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct __kernel_itimerspec __user *, otmr)
594 struct itimerspec64 kotmr;
595 int ret = do_timerfd_gettime(ufd, &kotmr);
598 return put_itimerspec64(&kotmr, otmr) ? -EFAULT : 0;
601 #ifdef CONFIG_COMPAT_32BIT_TIME
602 SYSCALL_DEFINE4(timerfd_settime32, int, ufd, int, flags,
603 const struct old_itimerspec32 __user *, utmr,
604 struct old_itimerspec32 __user *, otmr)
606 struct itimerspec64 new, old;
609 if (get_old_itimerspec32(&new, utmr))
611 ret = do_timerfd_settime(ufd, flags, &new, &old);
614 if (otmr && put_old_itimerspec32(&old, otmr))
619 SYSCALL_DEFINE2(timerfd_gettime32, int, ufd,
620 struct old_itimerspec32 __user *, otmr)
622 struct itimerspec64 kotmr;
623 int ret = do_timerfd_gettime(ufd, &kotmr);
626 return put_old_itimerspec32(&kotmr, otmr) ? -EFAULT : 0;