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 void timerfd_alarmproc(struct alarm
*alarm
, ktime_t now
)
84 struct timerfd_ctx
*ctx
= container_of(alarm
, struct timerfd_ctx
,
86 timerfd_triggered(ctx
);
90 * Called when the clock was set to cancel the timers in the cancel
91 * list. This will wake up processes waiting on these timers. The
92 * wake-up requires ctx->ticks to be non zero, therefore we increment
93 * it before calling wake_up_locked().
95 void timerfd_clock_was_set(void)
97 ktime_t moffs
= ktime_mono_to_real(0);
98 struct timerfd_ctx
*ctx
;
102 list_for_each_entry_rcu(ctx
, &cancel_list
, clist
) {
103 if (!ctx
->might_cancel
)
105 spin_lock_irqsave(&ctx
->wqh
.lock
, flags
);
106 if (ctx
->moffs
!= moffs
) {
107 ctx
->moffs
= KTIME_MAX
;
109 wake_up_locked_poll(&ctx
->wqh
, EPOLLIN
);
111 spin_unlock_irqrestore(&ctx
->wqh
.lock
, flags
);
116 static void timerfd_resume_work(struct work_struct
*work
)
118 timerfd_clock_was_set();
121 static DECLARE_WORK(timerfd_work
, timerfd_resume_work
);
124 * Invoked from timekeeping_resume(). Defer the actual update to work so
125 * timerfd_clock_was_set() runs in task context.
127 void timerfd_resume(void)
129 schedule_work(&timerfd_work
);
132 static void __timerfd_remove_cancel(struct timerfd_ctx
*ctx
)
134 if (ctx
->might_cancel
) {
135 ctx
->might_cancel
= false;
136 spin_lock(&cancel_lock
);
137 list_del_rcu(&ctx
->clist
);
138 spin_unlock(&cancel_lock
);
142 static void timerfd_remove_cancel(struct timerfd_ctx
*ctx
)
144 spin_lock(&ctx
->cancel_lock
);
145 __timerfd_remove_cancel(ctx
);
146 spin_unlock(&ctx
->cancel_lock
);
149 static bool timerfd_canceled(struct timerfd_ctx
*ctx
)
151 if (!ctx
->might_cancel
|| ctx
->moffs
!= KTIME_MAX
)
153 ctx
->moffs
= ktime_mono_to_real(0);
157 static void timerfd_setup_cancel(struct timerfd_ctx
*ctx
, int flags
)
159 spin_lock(&ctx
->cancel_lock
);
160 if ((ctx
->clockid
== CLOCK_REALTIME
||
161 ctx
->clockid
== CLOCK_REALTIME_ALARM
) &&
162 (flags
& TFD_TIMER_ABSTIME
) && (flags
& TFD_TIMER_CANCEL_ON_SET
)) {
163 if (!ctx
->might_cancel
) {
164 ctx
->might_cancel
= true;
165 spin_lock(&cancel_lock
);
166 list_add_rcu(&ctx
->clist
, &cancel_list
);
167 spin_unlock(&cancel_lock
);
170 __timerfd_remove_cancel(ctx
);
172 spin_unlock(&ctx
->cancel_lock
);
175 static ktime_t
timerfd_get_remaining(struct timerfd_ctx
*ctx
)
180 remaining
= alarm_expires_remaining(&ctx
->t
.alarm
);
182 remaining
= hrtimer_expires_remaining_adjusted(&ctx
->t
.tmr
);
184 return remaining
< 0 ? 0: remaining
;
187 static int timerfd_setup(struct timerfd_ctx
*ctx
, int flags
,
188 const struct itimerspec64
*ktmr
)
190 enum hrtimer_mode htmode
;
192 int clockid
= ctx
->clockid
;
194 htmode
= (flags
& TFD_TIMER_ABSTIME
) ?
195 HRTIMER_MODE_ABS
: HRTIMER_MODE_REL
;
197 texp
= timespec64_to_ktime(ktmr
->it_value
);
200 ctx
->tintv
= timespec64_to_ktime(ktmr
->it_interval
);
203 alarm_init(&ctx
->t
.alarm
,
204 ctx
->clockid
== CLOCK_REALTIME_ALARM
?
205 ALARM_REALTIME
: ALARM_BOOTTIME
,
208 hrtimer_init(&ctx
->t
.tmr
, clockid
, htmode
);
209 hrtimer_set_expires(&ctx
->t
.tmr
, texp
);
210 ctx
->t
.tmr
.function
= timerfd_tmrproc
;
214 if (flags
& TFD_TIMER_ABSTIME
)
215 texp
= timens_ktime_to_host(clockid
, texp
);
217 if (flags
& TFD_TIMER_ABSTIME
)
218 alarm_start(&ctx
->t
.alarm
, texp
);
220 alarm_start_relative(&ctx
->t
.alarm
, texp
);
222 hrtimer_start(&ctx
->t
.tmr
, texp
, htmode
);
225 if (timerfd_canceled(ctx
))
229 ctx
->settime_flags
= flags
& TFD_SETTIME_FLAGS
;
233 static int timerfd_release(struct inode
*inode
, struct file
*file
)
235 struct timerfd_ctx
*ctx
= file
->private_data
;
237 timerfd_remove_cancel(ctx
);
240 alarm_cancel(&ctx
->t
.alarm
);
242 hrtimer_cancel(&ctx
->t
.tmr
);
247 static __poll_t
timerfd_poll(struct file
*file
, poll_table
*wait
)
249 struct timerfd_ctx
*ctx
= file
->private_data
;
253 poll_wait(file
, &ctx
->wqh
, wait
);
255 spin_lock_irqsave(&ctx
->wqh
.lock
, flags
);
258 spin_unlock_irqrestore(&ctx
->wqh
.lock
, flags
);
263 static ssize_t
timerfd_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
265 struct file
*file
= iocb
->ki_filp
;
266 struct timerfd_ctx
*ctx
= file
->private_data
;
270 if (iov_iter_count(to
) < sizeof(ticks
))
273 spin_lock_irq(&ctx
->wqh
.lock
);
274 if (file
->f_flags
& O_NONBLOCK
|| iocb
->ki_flags
& IOCB_NOWAIT
)
277 res
= wait_event_interruptible_locked_irq(ctx
->wqh
, ctx
->ticks
);
280 * If clock has changed, we do not care about the
281 * ticks and we do not rearm the timer. Userspace must
284 if (timerfd_canceled(ctx
)) {
293 if (ctx
->expired
&& ctx
->tintv
) {
295 * If tintv != 0, this is a periodic timer that
296 * needs to be re-armed. We avoid doing it in the timer
297 * callback to avoid DoS attacks specifying a very
298 * short timer period.
301 ticks
+= alarm_forward_now(
302 &ctx
->t
.alarm
, ctx
->tintv
) - 1;
303 alarm_restart(&ctx
->t
.alarm
);
305 ticks
+= hrtimer_forward_now(&ctx
->t
.tmr
,
307 hrtimer_restart(&ctx
->t
.tmr
);
313 spin_unlock_irq(&ctx
->wqh
.lock
);
315 res
= copy_to_iter(&ticks
, sizeof(ticks
), to
);
322 #ifdef CONFIG_PROC_FS
323 static void timerfd_show(struct seq_file
*m
, struct file
*file
)
325 struct timerfd_ctx
*ctx
= file
->private_data
;
326 struct timespec64 value
, interval
;
328 spin_lock_irq(&ctx
->wqh
.lock
);
329 value
= ktime_to_timespec64(timerfd_get_remaining(ctx
));
330 interval
= ktime_to_timespec64(ctx
->tintv
);
331 spin_unlock_irq(&ctx
->wqh
.lock
);
336 "settime flags: 0%o\n"
337 "it_value: (%llu, %llu)\n"
338 "it_interval: (%llu, %llu)\n",
340 (unsigned long long)ctx
->ticks
,
342 (unsigned long long)value
.tv_sec
,
343 (unsigned long long)value
.tv_nsec
,
344 (unsigned long long)interval
.tv_sec
,
345 (unsigned long long)interval
.tv_nsec
);
348 #define timerfd_show NULL
351 #ifdef CONFIG_CHECKPOINT_RESTORE
352 static long timerfd_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
354 struct timerfd_ctx
*ctx
= file
->private_data
;
358 case TFD_IOC_SET_TICKS
: {
361 if (copy_from_user(&ticks
, (u64 __user
*)arg
, sizeof(ticks
)))
366 spin_lock_irq(&ctx
->wqh
.lock
);
367 if (!timerfd_canceled(ctx
)) {
369 wake_up_locked_poll(&ctx
->wqh
, EPOLLIN
);
372 spin_unlock_irq(&ctx
->wqh
.lock
);
383 #define timerfd_ioctl NULL
386 static const struct file_operations timerfd_fops
= {
387 .release
= timerfd_release
,
388 .poll
= timerfd_poll
,
389 .read_iter
= timerfd_read_iter
,
390 .llseek
= noop_llseek
,
391 .show_fdinfo
= timerfd_show
,
392 .unlocked_ioctl
= timerfd_ioctl
,
395 SYSCALL_DEFINE2(timerfd_create
, int, clockid
, int, flags
)
398 struct timerfd_ctx
*ctx
;
401 /* Check the TFD_* constants for consistency. */
402 BUILD_BUG_ON(TFD_CLOEXEC
!= O_CLOEXEC
);
403 BUILD_BUG_ON(TFD_NONBLOCK
!= O_NONBLOCK
);
405 if ((flags
& ~TFD_CREATE_FLAGS
) ||
406 (clockid
!= CLOCK_MONOTONIC
&&
407 clockid
!= CLOCK_REALTIME
&&
408 clockid
!= CLOCK_REALTIME_ALARM
&&
409 clockid
!= CLOCK_BOOTTIME
&&
410 clockid
!= CLOCK_BOOTTIME_ALARM
))
413 if ((clockid
== CLOCK_REALTIME_ALARM
||
414 clockid
== CLOCK_BOOTTIME_ALARM
) &&
415 !capable(CAP_WAKE_ALARM
))
418 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
422 init_waitqueue_head(&ctx
->wqh
);
423 spin_lock_init(&ctx
->cancel_lock
);
424 ctx
->clockid
= clockid
;
427 alarm_init(&ctx
->t
.alarm
,
428 ctx
->clockid
== CLOCK_REALTIME_ALARM
?
429 ALARM_REALTIME
: ALARM_BOOTTIME
,
432 hrtimer_init(&ctx
->t
.tmr
, clockid
, HRTIMER_MODE_ABS
);
434 ctx
->moffs
= ktime_mono_to_real(0);
436 ufd
= get_unused_fd_flags(flags
& TFD_SHARED_FCNTL_FLAGS
);
442 file
= anon_inode_getfile("[timerfd]", &timerfd_fops
, ctx
,
443 O_RDWR
| (flags
& TFD_SHARED_FCNTL_FLAGS
));
447 return PTR_ERR(file
);
450 file
->f_mode
|= FMODE_NOWAIT
;
451 fd_install(ufd
, file
);
455 static int do_timerfd_settime(int ufd
, int flags
,
456 const struct itimerspec64
*new,
457 struct itimerspec64
*old
)
459 struct timerfd_ctx
*ctx
;
462 if ((flags
& ~TFD_SETTIME_FLAGS
) ||
463 !itimerspec64_valid(new))
470 if (fd_file(f
)->f_op
!= &timerfd_fops
)
473 ctx
= fd_file(f
)->private_data
;
475 if (isalarm(ctx
) && !capable(CAP_WAKE_ALARM
))
478 timerfd_setup_cancel(ctx
, flags
);
481 * We need to stop the existing timer before reprogramming
482 * it to the new values.
485 spin_lock_irq(&ctx
->wqh
.lock
);
488 if (alarm_try_to_cancel(&ctx
->t
.alarm
) >= 0)
491 if (hrtimer_try_to_cancel(&ctx
->t
.tmr
) >= 0)
494 spin_unlock_irq(&ctx
->wqh
.lock
);
497 hrtimer_cancel_wait_running(&ctx
->t
.alarm
.timer
);
499 hrtimer_cancel_wait_running(&ctx
->t
.tmr
);
503 * If the timer is expired and it's periodic, we need to advance it
504 * because the caller may want to know the previous expiration time.
505 * We do not update "ticks" and "expired" since the timer will be
506 * re-programmed again in the following timerfd_setup() call.
508 if (ctx
->expired
&& ctx
->tintv
) {
510 alarm_forward_now(&ctx
->t
.alarm
, ctx
->tintv
);
512 hrtimer_forward_now(&ctx
->t
.tmr
, ctx
->tintv
);
515 old
->it_value
= ktime_to_timespec64(timerfd_get_remaining(ctx
));
516 old
->it_interval
= ktime_to_timespec64(ctx
->tintv
);
519 * Re-program the timer to the new value ...
521 ret
= timerfd_setup(ctx
, flags
, new);
523 spin_unlock_irq(&ctx
->wqh
.lock
);
527 static int do_timerfd_gettime(int ufd
, struct itimerspec64
*t
)
529 struct timerfd_ctx
*ctx
;
534 if (fd_file(f
)->f_op
!= &timerfd_fops
)
536 ctx
= fd_file(f
)->private_data
;
538 spin_lock_irq(&ctx
->wqh
.lock
);
539 if (ctx
->expired
&& ctx
->tintv
) {
545 &ctx
->t
.alarm
, ctx
->tintv
) - 1;
546 alarm_restart(&ctx
->t
.alarm
);
549 hrtimer_forward_now(&ctx
->t
.tmr
, ctx
->tintv
)
551 hrtimer_restart(&ctx
->t
.tmr
);
554 t
->it_value
= ktime_to_timespec64(timerfd_get_remaining(ctx
));
555 t
->it_interval
= ktime_to_timespec64(ctx
->tintv
);
556 spin_unlock_irq(&ctx
->wqh
.lock
);
560 SYSCALL_DEFINE4(timerfd_settime
, int, ufd
, int, flags
,
561 const struct __kernel_itimerspec __user
*, utmr
,
562 struct __kernel_itimerspec __user
*, otmr
)
564 struct itimerspec64
new, old
;
567 if (get_itimerspec64(&new, utmr
))
569 ret
= do_timerfd_settime(ufd
, flags
, &new, &old
);
572 if (otmr
&& put_itimerspec64(&old
, otmr
))
578 SYSCALL_DEFINE2(timerfd_gettime
, int, ufd
, struct __kernel_itimerspec __user
*, otmr
)
580 struct itimerspec64 kotmr
;
581 int ret
= do_timerfd_gettime(ufd
, &kotmr
);
584 return put_itimerspec64(&kotmr
, otmr
) ? -EFAULT
: 0;
587 #ifdef CONFIG_COMPAT_32BIT_TIME
588 SYSCALL_DEFINE4(timerfd_settime32
, int, ufd
, int, flags
,
589 const struct old_itimerspec32 __user
*, utmr
,
590 struct old_itimerspec32 __user
*, otmr
)
592 struct itimerspec64
new, old
;
595 if (get_old_itimerspec32(&new, utmr
))
597 ret
= do_timerfd_settime(ufd
, flags
, &new, &old
);
600 if (otmr
&& put_old_itimerspec32(&old
, otmr
))
605 SYSCALL_DEFINE2(timerfd_gettime32
, int, ufd
,
606 struct old_itimerspec32 __user
*, otmr
)
608 struct itimerspec64 kotmr
;
609 int ret
= do_timerfd_gettime(ufd
, &kotmr
);
612 return put_old_itimerspec32(&kotmr
, otmr
) ? -EFAULT
: 0;