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
)
57 remaining
= hrtimer_expires_remaining(&ctx
->tmr
);
58 return remaining
.tv64
< 0 ? ktime_set(0, 0): remaining
;
61 static void timerfd_setup(struct timerfd_ctx
*ctx
, int flags
,
62 const struct itimerspec
*ktmr
)
64 enum hrtimer_mode htmode
;
67 htmode
= (flags
& TFD_TIMER_ABSTIME
) ?
68 HRTIMER_MODE_ABS
: HRTIMER_MODE_REL
;
70 texp
= timespec_to_ktime(ktmr
->it_value
);
73 ctx
->tintv
= timespec_to_ktime(ktmr
->it_interval
);
74 hrtimer_init(&ctx
->tmr
, ctx
->clockid
, htmode
);
75 hrtimer_set_expires(&ctx
->tmr
, texp
);
76 ctx
->tmr
.function
= timerfd_tmrproc
;
78 hrtimer_start(&ctx
->tmr
, texp
, htmode
);
81 static int timerfd_release(struct inode
*inode
, struct file
*file
)
83 struct timerfd_ctx
*ctx
= file
->private_data
;
85 hrtimer_cancel(&ctx
->tmr
);
90 static unsigned int timerfd_poll(struct file
*file
, poll_table
*wait
)
92 struct timerfd_ctx
*ctx
= file
->private_data
;
93 unsigned int events
= 0;
96 poll_wait(file
, &ctx
->wqh
, wait
);
98 spin_lock_irqsave(&ctx
->wqh
.lock
, flags
);
101 spin_unlock_irqrestore(&ctx
->wqh
.lock
, flags
);
106 static ssize_t
timerfd_read(struct file
*file
, char __user
*buf
, size_t count
,
109 struct timerfd_ctx
*ctx
= file
->private_data
;
112 DECLARE_WAITQUEUE(wait
, current
);
114 if (count
< sizeof(ticks
))
116 spin_lock_irq(&ctx
->wqh
.lock
);
118 if (!ctx
->ticks
&& !(file
->f_flags
& O_NONBLOCK
)) {
119 __add_wait_queue(&ctx
->wqh
, &wait
);
121 set_current_state(TASK_INTERRUPTIBLE
);
126 if (signal_pending(current
)) {
130 spin_unlock_irq(&ctx
->wqh
.lock
);
132 spin_lock_irq(&ctx
->wqh
.lock
);
134 __remove_wait_queue(&ctx
->wqh
, &wait
);
135 __set_current_state(TASK_RUNNING
);
139 if (ctx
->expired
&& ctx
->tintv
.tv64
) {
141 * If tintv.tv64 != 0, this is a periodic timer that
142 * needs to be re-armed. We avoid doing it in the timer
143 * callback to avoid DoS attacks specifying a very
144 * short timer period.
146 ticks
+= hrtimer_forward_now(&ctx
->tmr
,
148 hrtimer_restart(&ctx
->tmr
);
153 spin_unlock_irq(&ctx
->wqh
.lock
);
155 res
= put_user(ticks
, (u64 __user
*) buf
) ? -EFAULT
: sizeof(ticks
);
159 static const struct file_operations timerfd_fops
= {
160 .release
= timerfd_release
,
161 .poll
= timerfd_poll
,
162 .read
= timerfd_read
,
165 static struct file
*timerfd_fget(int fd
)
171 return ERR_PTR(-EBADF
);
172 if (file
->f_op
!= &timerfd_fops
) {
174 return ERR_PTR(-EINVAL
);
180 asmlinkage
long sys_timerfd_create(int clockid
, int flags
)
183 struct timerfd_ctx
*ctx
;
185 /* Check the TFD_* constants for consistency. */
186 BUILD_BUG_ON(TFD_CLOEXEC
!= O_CLOEXEC
);
187 BUILD_BUG_ON(TFD_NONBLOCK
!= O_NONBLOCK
);
189 if (flags
& ~(TFD_CLOEXEC
| TFD_NONBLOCK
))
191 if (clockid
!= CLOCK_MONOTONIC
&&
192 clockid
!= CLOCK_REALTIME
)
195 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
199 init_waitqueue_head(&ctx
->wqh
);
200 ctx
->clockid
= clockid
;
201 hrtimer_init(&ctx
->tmr
, clockid
, HRTIMER_MODE_ABS
);
203 ufd
= anon_inode_getfd("[timerfd]", &timerfd_fops
, ctx
,
204 flags
& (O_CLOEXEC
| O_NONBLOCK
));
211 asmlinkage
long sys_timerfd_settime(int ufd
, int flags
,
212 const struct itimerspec __user
*utmr
,
213 struct itimerspec __user
*otmr
)
216 struct timerfd_ctx
*ctx
;
217 struct itimerspec ktmr
, kotmr
;
219 if (copy_from_user(&ktmr
, utmr
, sizeof(ktmr
)))
222 if (!timespec_valid(&ktmr
.it_value
) ||
223 !timespec_valid(&ktmr
.it_interval
))
226 file
= timerfd_fget(ufd
);
228 return PTR_ERR(file
);
229 ctx
= file
->private_data
;
232 * We need to stop the existing timer before reprogramming
233 * it to the new values.
236 spin_lock_irq(&ctx
->wqh
.lock
);
237 if (hrtimer_try_to_cancel(&ctx
->tmr
) >= 0)
239 spin_unlock_irq(&ctx
->wqh
.lock
);
244 * If the timer is expired and it's periodic, we need to advance it
245 * because the caller may want to know the previous expiration time.
246 * We do not update "ticks" and "expired" since the timer will be
247 * re-programmed again in the following timerfd_setup() call.
249 if (ctx
->expired
&& ctx
->tintv
.tv64
)
250 hrtimer_forward_now(&ctx
->tmr
, ctx
->tintv
);
252 kotmr
.it_value
= ktime_to_timespec(timerfd_get_remaining(ctx
));
253 kotmr
.it_interval
= ktime_to_timespec(ctx
->tintv
);
256 * Re-program the timer to the new value ...
258 timerfd_setup(ctx
, flags
, &ktmr
);
260 spin_unlock_irq(&ctx
->wqh
.lock
);
262 if (otmr
&& copy_to_user(otmr
, &kotmr
, sizeof(kotmr
)))
268 asmlinkage
long sys_timerfd_gettime(int ufd
, struct itimerspec __user
*otmr
)
271 struct timerfd_ctx
*ctx
;
272 struct itimerspec kotmr
;
274 file
= timerfd_fget(ufd
);
276 return PTR_ERR(file
);
277 ctx
= file
->private_data
;
279 spin_lock_irq(&ctx
->wqh
.lock
);
280 if (ctx
->expired
&& ctx
->tintv
.tv64
) {
283 hrtimer_forward_now(&ctx
->tmr
, ctx
->tintv
) - 1;
284 hrtimer_restart(&ctx
->tmr
);
286 kotmr
.it_value
= ktime_to_timespec(timerfd_get_remaining(ctx
));
287 kotmr
.it_interval
= ktime_to_timespec(ctx
->tintv
);
288 spin_unlock_irq(&ctx
->wqh
.lock
);
291 return copy_to_user(otmr
, &kotmr
, sizeof(kotmr
)) ? -EFAULT
: 0;