4 * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org>
7 * Thanks to Thomas Gleixner for code reviews and useful comments.
11 #include <linux/file.h>
12 #include <linux/poll.h>
13 #include <linux/init.h>
15 #include <linux/sched.h>
16 #include <linux/kernel.h>
17 #include <linux/slab.h>
18 #include <linux/list.h>
19 #include <linux/spinlock.h>
20 #include <linux/time.h>
21 #include <linux/hrtimer.h>
22 #include <linux/anon_inodes.h>
23 #include <linux/timerfd.h>
24 #include <linux/syscalls.h>
25 #include <linux/rcupdate.h>
31 wait_queue_head_t wqh
;
36 struct list_head clist
;
40 static LIST_HEAD(cancel_list
);
41 static DEFINE_SPINLOCK(cancel_lock
);
44 * This gets called when the timer event triggers. We set the "expired"
45 * flag, but we do not re-arm the timer (in case it's necessary,
46 * tintv.tv64 != 0) until the timer is accessed.
48 static enum hrtimer_restart
timerfd_tmrproc(struct hrtimer
*htmr
)
50 struct timerfd_ctx
*ctx
= container_of(htmr
, struct timerfd_ctx
, tmr
);
53 spin_lock_irqsave(&ctx
->wqh
.lock
, flags
);
56 wake_up_locked(&ctx
->wqh
);
57 spin_unlock_irqrestore(&ctx
->wqh
.lock
, flags
);
59 return HRTIMER_NORESTART
;
63 * Called when the clock was set to cancel the timers in the cancel
66 void timerfd_clock_was_set(void)
68 ktime_t moffs
= ktime_get_monotonic_offset();
69 struct timerfd_ctx
*ctx
;
73 list_for_each_entry_rcu(ctx
, &cancel_list
, clist
) {
74 if (!ctx
->might_cancel
)
76 spin_lock_irqsave(&ctx
->wqh
.lock
, flags
);
77 if (ctx
->moffs
.tv64
!= moffs
.tv64
) {
78 ctx
->moffs
.tv64
= KTIME_MAX
;
79 wake_up_locked(&ctx
->wqh
);
81 spin_unlock_irqrestore(&ctx
->wqh
.lock
, flags
);
86 static void timerfd_remove_cancel(struct timerfd_ctx
*ctx
)
88 if (ctx
->might_cancel
) {
89 ctx
->might_cancel
= false;
90 spin_lock(&cancel_lock
);
91 list_del_rcu(&ctx
->clist
);
92 spin_unlock(&cancel_lock
);
96 static bool timerfd_canceled(struct timerfd_ctx
*ctx
)
98 if (!ctx
->might_cancel
|| ctx
->moffs
.tv64
!= KTIME_MAX
)
100 ctx
->moffs
= ktime_get_monotonic_offset();
104 static void timerfd_setup_cancel(struct timerfd_ctx
*ctx
, int flags
)
106 if (ctx
->clockid
== CLOCK_REALTIME
&& (flags
& TFD_TIMER_ABSTIME
) &&
107 (flags
& TFD_TIMER_CANCEL_ON_SET
)) {
108 if (!ctx
->might_cancel
) {
109 ctx
->might_cancel
= true;
110 spin_lock(&cancel_lock
);
111 list_add_rcu(&ctx
->clist
, &cancel_list
);
112 spin_unlock(&cancel_lock
);
114 } else if (ctx
->might_cancel
) {
115 timerfd_remove_cancel(ctx
);
119 static ktime_t
timerfd_get_remaining(struct timerfd_ctx
*ctx
)
123 remaining
= hrtimer_expires_remaining(&ctx
->tmr
);
124 return remaining
.tv64
< 0 ? ktime_set(0, 0): remaining
;
127 static int timerfd_setup(struct timerfd_ctx
*ctx
, int flags
,
128 const struct itimerspec
*ktmr
)
130 enum hrtimer_mode htmode
;
132 int clockid
= ctx
->clockid
;
134 htmode
= (flags
& TFD_TIMER_ABSTIME
) ?
135 HRTIMER_MODE_ABS
: HRTIMER_MODE_REL
;
137 texp
= timespec_to_ktime(ktmr
->it_value
);
140 ctx
->tintv
= timespec_to_ktime(ktmr
->it_interval
);
141 hrtimer_init(&ctx
->tmr
, clockid
, htmode
);
142 hrtimer_set_expires(&ctx
->tmr
, texp
);
143 ctx
->tmr
.function
= timerfd_tmrproc
;
144 if (texp
.tv64
!= 0) {
145 hrtimer_start(&ctx
->tmr
, texp
, htmode
);
146 if (timerfd_canceled(ctx
))
152 static int timerfd_release(struct inode
*inode
, struct file
*file
)
154 struct timerfd_ctx
*ctx
= file
->private_data
;
156 timerfd_remove_cancel(ctx
);
157 hrtimer_cancel(&ctx
->tmr
);
162 static unsigned int timerfd_poll(struct file
*file
, poll_table
*wait
)
164 struct timerfd_ctx
*ctx
= file
->private_data
;
165 unsigned int events
= 0;
168 poll_wait(file
, &ctx
->wqh
, wait
);
170 spin_lock_irqsave(&ctx
->wqh
.lock
, flags
);
173 spin_unlock_irqrestore(&ctx
->wqh
.lock
, flags
);
178 static ssize_t
timerfd_read(struct file
*file
, char __user
*buf
, size_t count
,
181 struct timerfd_ctx
*ctx
= file
->private_data
;
185 if (count
< sizeof(ticks
))
187 spin_lock_irq(&ctx
->wqh
.lock
);
188 if (file
->f_flags
& O_NONBLOCK
)
191 res
= wait_event_interruptible_locked_irq(ctx
->wqh
, ctx
->ticks
);
194 * If clock has changed, we do not care about the
195 * ticks and we do not rearm the timer. Userspace must
198 if (timerfd_canceled(ctx
)) {
207 if (ctx
->expired
&& ctx
->tintv
.tv64
) {
209 * If tintv.tv64 != 0, this is a periodic timer that
210 * needs to be re-armed. We avoid doing it in the timer
211 * callback to avoid DoS attacks specifying a very
212 * short timer period.
214 ticks
+= hrtimer_forward_now(&ctx
->tmr
,
216 hrtimer_restart(&ctx
->tmr
);
221 spin_unlock_irq(&ctx
->wqh
.lock
);
223 res
= put_user(ticks
, (u64 __user
*) buf
) ? -EFAULT
: sizeof(ticks
);
227 static const struct file_operations timerfd_fops
= {
228 .release
= timerfd_release
,
229 .poll
= timerfd_poll
,
230 .read
= timerfd_read
,
231 .llseek
= noop_llseek
,
234 static struct file
*timerfd_fget(int fd
)
240 return ERR_PTR(-EBADF
);
241 if (file
->f_op
!= &timerfd_fops
) {
243 return ERR_PTR(-EINVAL
);
249 SYSCALL_DEFINE2(timerfd_create
, int, clockid
, int, flags
)
252 struct timerfd_ctx
*ctx
;
254 /* Check the TFD_* constants for consistency. */
255 BUILD_BUG_ON(TFD_CLOEXEC
!= O_CLOEXEC
);
256 BUILD_BUG_ON(TFD_NONBLOCK
!= O_NONBLOCK
);
258 if ((flags
& ~TFD_CREATE_FLAGS
) ||
259 (clockid
!= CLOCK_MONOTONIC
&&
260 clockid
!= CLOCK_REALTIME
))
263 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
267 init_waitqueue_head(&ctx
->wqh
);
268 ctx
->clockid
= clockid
;
269 hrtimer_init(&ctx
->tmr
, clockid
, HRTIMER_MODE_ABS
);
270 ctx
->moffs
= ktime_get_monotonic_offset();
272 ufd
= anon_inode_getfd("[timerfd]", &timerfd_fops
, ctx
,
273 O_RDWR
| (flags
& TFD_SHARED_FCNTL_FLAGS
));
280 SYSCALL_DEFINE4(timerfd_settime
, int, ufd
, int, flags
,
281 const struct itimerspec __user
*, utmr
,
282 struct itimerspec __user
*, otmr
)
285 struct timerfd_ctx
*ctx
;
286 struct itimerspec ktmr
, kotmr
;
289 if (copy_from_user(&ktmr
, utmr
, sizeof(ktmr
)))
292 if ((flags
& ~TFD_SETTIME_FLAGS
) ||
293 !timespec_valid(&ktmr
.it_value
) ||
294 !timespec_valid(&ktmr
.it_interval
))
297 file
= timerfd_fget(ufd
);
299 return PTR_ERR(file
);
300 ctx
= file
->private_data
;
302 timerfd_setup_cancel(ctx
, flags
);
305 * We need to stop the existing timer before reprogramming
306 * it to the new values.
309 spin_lock_irq(&ctx
->wqh
.lock
);
310 if (hrtimer_try_to_cancel(&ctx
->tmr
) >= 0)
312 spin_unlock_irq(&ctx
->wqh
.lock
);
317 * If the timer is expired and it's periodic, we need to advance it
318 * because the caller may want to know the previous expiration time.
319 * We do not update "ticks" and "expired" since the timer will be
320 * re-programmed again in the following timerfd_setup() call.
322 if (ctx
->expired
&& ctx
->tintv
.tv64
)
323 hrtimer_forward_now(&ctx
->tmr
, ctx
->tintv
);
325 kotmr
.it_value
= ktime_to_timespec(timerfd_get_remaining(ctx
));
326 kotmr
.it_interval
= ktime_to_timespec(ctx
->tintv
);
329 * Re-program the timer to the new value ...
331 ret
= timerfd_setup(ctx
, flags
, &ktmr
);
333 spin_unlock_irq(&ctx
->wqh
.lock
);
335 if (otmr
&& copy_to_user(otmr
, &kotmr
, sizeof(kotmr
)))
341 SYSCALL_DEFINE2(timerfd_gettime
, int, ufd
, struct itimerspec __user
*, otmr
)
344 struct timerfd_ctx
*ctx
;
345 struct itimerspec kotmr
;
347 file
= timerfd_fget(ufd
);
349 return PTR_ERR(file
);
350 ctx
= file
->private_data
;
352 spin_lock_irq(&ctx
->wqh
.lock
);
353 if (ctx
->expired
&& ctx
->tintv
.tv64
) {
356 hrtimer_forward_now(&ctx
->tmr
, ctx
->tintv
) - 1;
357 hrtimer_restart(&ctx
->tmr
);
359 kotmr
.it_value
= ktime_to_timespec(timerfd_get_remaining(ctx
));
360 kotmr
.it_interval
= ktime_to_timespec(ctx
->tintv
);
361 spin_unlock_irq(&ctx
->wqh
.lock
);
364 return copy_to_user(otmr
, &kotmr
, sizeof(kotmr
)) ? -EFAULT
: 0;