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/list.h>
18 #include <linux/spinlock.h>
19 #include <linux/time.h>
20 #include <linux/hrtimer.h>
21 #include <linux/anon_inodes.h>
22 #include <linux/timerfd.h>
23 #include <linux/syscalls.h>
28 wait_queue_head_t wqh
;
35 * This gets called when the timer event triggers. We set the "expired"
36 * flag, but we do not re-arm the timer (in case it's necessary,
37 * tintv.tv64 != 0) until the timer is accessed.
39 static enum hrtimer_restart
timerfd_tmrproc(struct hrtimer
*htmr
)
41 struct timerfd_ctx
*ctx
= container_of(htmr
, struct timerfd_ctx
, tmr
);
44 spin_lock_irqsave(&ctx
->wqh
.lock
, flags
);
47 wake_up_locked(&ctx
->wqh
);
48 spin_unlock_irqrestore(&ctx
->wqh
.lock
, flags
);
50 return HRTIMER_NORESTART
;
53 static ktime_t
timerfd_get_remaining(struct timerfd_ctx
*ctx
)
55 ktime_t now
, remaining
;
57 now
= ctx
->tmr
.base
->get_time();
58 remaining
= ktime_sub(ctx
->tmr
.expires
, now
);
60 return remaining
.tv64
< 0 ? ktime_set(0, 0): remaining
;
63 static void timerfd_setup(struct timerfd_ctx
*ctx
, int flags
,
64 const struct itimerspec
*ktmr
)
66 enum hrtimer_mode htmode
;
69 htmode
= (flags
& TFD_TIMER_ABSTIME
) ?
70 HRTIMER_MODE_ABS
: HRTIMER_MODE_REL
;
72 texp
= timespec_to_ktime(ktmr
->it_value
);
75 ctx
->tintv
= timespec_to_ktime(ktmr
->it_interval
);
76 hrtimer_init(&ctx
->tmr
, ctx
->clockid
, htmode
);
77 ctx
->tmr
.expires
= texp
;
78 ctx
->tmr
.function
= timerfd_tmrproc
;
80 hrtimer_start(&ctx
->tmr
, texp
, htmode
);
83 static int timerfd_release(struct inode
*inode
, struct file
*file
)
85 struct timerfd_ctx
*ctx
= file
->private_data
;
87 hrtimer_cancel(&ctx
->tmr
);
92 static unsigned int timerfd_poll(struct file
*file
, poll_table
*wait
)
94 struct timerfd_ctx
*ctx
= file
->private_data
;
95 unsigned int events
= 0;
98 poll_wait(file
, &ctx
->wqh
, wait
);
100 spin_lock_irqsave(&ctx
->wqh
.lock
, flags
);
103 spin_unlock_irqrestore(&ctx
->wqh
.lock
, flags
);
108 static ssize_t
timerfd_read(struct file
*file
, char __user
*buf
, size_t count
,
111 struct timerfd_ctx
*ctx
= file
->private_data
;
114 DECLARE_WAITQUEUE(wait
, current
);
116 if (count
< sizeof(ticks
))
118 spin_lock_irq(&ctx
->wqh
.lock
);
120 if (!ctx
->ticks
&& !(file
->f_flags
& O_NONBLOCK
)) {
121 __add_wait_queue(&ctx
->wqh
, &wait
);
123 set_current_state(TASK_INTERRUPTIBLE
);
128 if (signal_pending(current
)) {
132 spin_unlock_irq(&ctx
->wqh
.lock
);
134 spin_lock_irq(&ctx
->wqh
.lock
);
136 __remove_wait_queue(&ctx
->wqh
, &wait
);
137 __set_current_state(TASK_RUNNING
);
141 if (ctx
->expired
&& ctx
->tintv
.tv64
) {
143 * If tintv.tv64 != 0, this is a periodic timer that
144 * needs to be re-armed. We avoid doing it in the timer
145 * callback to avoid DoS attacks specifying a very
146 * short timer period.
148 ticks
+= hrtimer_forward_now(&ctx
->tmr
,
150 hrtimer_restart(&ctx
->tmr
);
155 spin_unlock_irq(&ctx
->wqh
.lock
);
157 res
= put_user(ticks
, (u64 __user
*) buf
) ? -EFAULT
: sizeof(ticks
);
161 static const struct file_operations timerfd_fops
= {
162 .release
= timerfd_release
,
163 .poll
= timerfd_poll
,
164 .read
= timerfd_read
,
167 static struct file
*timerfd_fget(int fd
)
173 return ERR_PTR(-EBADF
);
174 if (file
->f_op
!= &timerfd_fops
) {
176 return ERR_PTR(-EINVAL
);
182 SYSCALL_DEFINE2(timerfd_create
, int, clockid
, int, flags
)
185 struct timerfd_ctx
*ctx
;
187 /* Check the TFD_* constants for consistency. */
188 BUILD_BUG_ON(TFD_CLOEXEC
!= O_CLOEXEC
);
189 BUILD_BUG_ON(TFD_NONBLOCK
!= O_NONBLOCK
);
191 if ((flags
& ~TFD_CREATE_FLAGS
) ||
192 (clockid
!= CLOCK_MONOTONIC
&&
193 clockid
!= CLOCK_REALTIME
))
196 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
200 init_waitqueue_head(&ctx
->wqh
);
201 ctx
->clockid
= clockid
;
202 hrtimer_init(&ctx
->tmr
, clockid
, HRTIMER_MODE_ABS
);
204 ufd
= anon_inode_getfd("[timerfd]", &timerfd_fops
, ctx
,
205 flags
& TFD_SHARED_FCNTL_FLAGS
);
212 SYSCALL_DEFINE4(timerfd_settime
, int, ufd
, int, flags
,
213 const struct itimerspec __user
*, utmr
,
214 struct itimerspec __user
*, otmr
)
217 struct timerfd_ctx
*ctx
;
218 struct itimerspec ktmr
, kotmr
;
220 if (copy_from_user(&ktmr
, utmr
, sizeof(ktmr
)))
223 if ((flags
& ~TFD_SETTIME_FLAGS
) ||
224 !timespec_valid(&ktmr
.it_value
) ||
225 !timespec_valid(&ktmr
.it_interval
))
228 file
= timerfd_fget(ufd
);
230 return PTR_ERR(file
);
231 ctx
= file
->private_data
;
234 * We need to stop the existing timer before reprogramming
235 * it to the new values.
238 spin_lock_irq(&ctx
->wqh
.lock
);
239 if (hrtimer_try_to_cancel(&ctx
->tmr
) >= 0)
241 spin_unlock_irq(&ctx
->wqh
.lock
);
246 * If the timer is expired and it's periodic, we need to advance it
247 * because the caller may want to know the previous expiration time.
248 * We do not update "ticks" and "expired" since the timer will be
249 * re-programmed again in the following timerfd_setup() call.
251 if (ctx
->expired
&& ctx
->tintv
.tv64
)
252 hrtimer_forward_now(&ctx
->tmr
, ctx
->tintv
);
254 kotmr
.it_value
= ktime_to_timespec(timerfd_get_remaining(ctx
));
255 kotmr
.it_interval
= ktime_to_timespec(ctx
->tintv
);
258 * Re-program the timer to the new value ...
260 timerfd_setup(ctx
, flags
, &ktmr
);
262 spin_unlock_irq(&ctx
->wqh
.lock
);
264 if (otmr
&& copy_to_user(otmr
, &kotmr
, sizeof(kotmr
)))
270 SYSCALL_DEFINE2(timerfd_gettime
, int, ufd
, struct itimerspec __user
*, otmr
)
273 struct timerfd_ctx
*ctx
;
274 struct itimerspec kotmr
;
276 file
= timerfd_fget(ufd
);
278 return PTR_ERR(file
);
279 ctx
= file
->private_data
;
281 spin_lock_irq(&ctx
->wqh
.lock
);
282 if (ctx
->expired
&& ctx
->tintv
.tv64
) {
285 hrtimer_forward_now(&ctx
->tmr
, ctx
->tintv
) - 1;
286 hrtimer_restart(&ctx
->tmr
);
288 kotmr
.it_value
= ktime_to_timespec(timerfd_get_remaining(ctx
));
289 kotmr
.it_interval
= ktime_to_timespec(ctx
->tintv
);
290 spin_unlock_irq(&ctx
->wqh
.lock
);
293 return copy_to_user(otmr
, &kotmr
, sizeof(kotmr
)) ? -EFAULT
: 0;