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>
29 wait_queue_head_t wqh
;
36 * This gets called when the timer event triggers. We set the "expired"
37 * flag, but we do not re-arm the timer (in case it's necessary,
38 * tintv.tv64 != 0) until the timer is accessed.
40 static enum hrtimer_restart
timerfd_tmrproc(struct hrtimer
*htmr
)
42 struct timerfd_ctx
*ctx
= container_of(htmr
, struct timerfd_ctx
, tmr
);
45 spin_lock_irqsave(&ctx
->wqh
.lock
, flags
);
48 wake_up_locked(&ctx
->wqh
);
49 spin_unlock_irqrestore(&ctx
->wqh
.lock
, flags
);
51 return HRTIMER_NORESTART
;
54 static ktime_t
timerfd_get_remaining(struct timerfd_ctx
*ctx
)
58 remaining
= hrtimer_expires_remaining(&ctx
->tmr
);
59 return remaining
.tv64
< 0 ? ktime_set(0, 0): remaining
;
62 static void timerfd_setup(struct timerfd_ctx
*ctx
, int flags
,
63 const struct itimerspec
*ktmr
)
65 enum hrtimer_mode htmode
;
68 htmode
= (flags
& TFD_TIMER_ABSTIME
) ?
69 HRTIMER_MODE_ABS
: HRTIMER_MODE_REL
;
71 texp
= timespec_to_ktime(ktmr
->it_value
);
74 ctx
->tintv
= timespec_to_ktime(ktmr
->it_interval
);
75 hrtimer_init(&ctx
->tmr
, ctx
->clockid
, htmode
);
76 hrtimer_set_expires(&ctx
->tmr
, texp
);
77 ctx
->tmr
.function
= timerfd_tmrproc
;
79 hrtimer_start(&ctx
->tmr
, texp
, htmode
);
82 static int timerfd_release(struct inode
*inode
, struct file
*file
)
84 struct timerfd_ctx
*ctx
= file
->private_data
;
86 hrtimer_cancel(&ctx
->tmr
);
91 static unsigned int timerfd_poll(struct file
*file
, poll_table
*wait
)
93 struct timerfd_ctx
*ctx
= file
->private_data
;
94 unsigned int events
= 0;
97 poll_wait(file
, &ctx
->wqh
, wait
);
99 spin_lock_irqsave(&ctx
->wqh
.lock
, flags
);
102 spin_unlock_irqrestore(&ctx
->wqh
.lock
, flags
);
107 static ssize_t
timerfd_read(struct file
*file
, char __user
*buf
, size_t count
,
110 struct timerfd_ctx
*ctx
= file
->private_data
;
113 DECLARE_WAITQUEUE(wait
, current
);
115 if (count
< sizeof(ticks
))
117 spin_lock_irq(&ctx
->wqh
.lock
);
119 if (!ctx
->ticks
&& !(file
->f_flags
& O_NONBLOCK
)) {
120 __add_wait_queue(&ctx
->wqh
, &wait
);
122 set_current_state(TASK_INTERRUPTIBLE
);
127 if (signal_pending(current
)) {
131 spin_unlock_irq(&ctx
->wqh
.lock
);
133 spin_lock_irq(&ctx
->wqh
.lock
);
135 __remove_wait_queue(&ctx
->wqh
, &wait
);
136 __set_current_state(TASK_RUNNING
);
140 if (ctx
->expired
&& ctx
->tintv
.tv64
) {
142 * If tintv.tv64 != 0, this is a periodic timer that
143 * needs to be re-armed. We avoid doing it in the timer
144 * callback to avoid DoS attacks specifying a very
145 * short timer period.
147 ticks
+= hrtimer_forward_now(&ctx
->tmr
,
149 hrtimer_restart(&ctx
->tmr
);
154 spin_unlock_irq(&ctx
->wqh
.lock
);
156 res
= put_user(ticks
, (u64 __user
*) buf
) ? -EFAULT
: sizeof(ticks
);
160 static const struct file_operations timerfd_fops
= {
161 .release
= timerfd_release
,
162 .poll
= timerfd_poll
,
163 .read
= timerfd_read
,
166 static struct file
*timerfd_fget(int fd
)
172 return ERR_PTR(-EBADF
);
173 if (file
->f_op
!= &timerfd_fops
) {
175 return ERR_PTR(-EINVAL
);
181 SYSCALL_DEFINE2(timerfd_create
, int, clockid
, int, flags
)
184 struct timerfd_ctx
*ctx
;
186 /* Check the TFD_* constants for consistency. */
187 BUILD_BUG_ON(TFD_CLOEXEC
!= O_CLOEXEC
);
188 BUILD_BUG_ON(TFD_NONBLOCK
!= O_NONBLOCK
);
190 if ((flags
& ~TFD_CREATE_FLAGS
) ||
191 (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 O_RDWR
| (flags
& TFD_SHARED_FCNTL_FLAGS
));
211 SYSCALL_DEFINE4(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 ((flags
& ~TFD_SETTIME_FLAGS
) ||
223 !timespec_valid(&ktmr
.it_value
) ||
224 !timespec_valid(&ktmr
.it_interval
))
227 file
= timerfd_fget(ufd
);
229 return PTR_ERR(file
);
230 ctx
= file
->private_data
;
233 * We need to stop the existing timer before reprogramming
234 * it to the new values.
237 spin_lock_irq(&ctx
->wqh
.lock
);
238 if (hrtimer_try_to_cancel(&ctx
->tmr
) >= 0)
240 spin_unlock_irq(&ctx
->wqh
.lock
);
245 * If the timer is expired and it's periodic, we need to advance it
246 * because the caller may want to know the previous expiration time.
247 * We do not update "ticks" and "expired" since the timer will be
248 * re-programmed again in the following timerfd_setup() call.
250 if (ctx
->expired
&& ctx
->tintv
.tv64
)
251 hrtimer_forward_now(&ctx
->tmr
, ctx
->tintv
);
253 kotmr
.it_value
= ktime_to_timespec(timerfd_get_remaining(ctx
));
254 kotmr
.it_interval
= ktime_to_timespec(ctx
->tintv
);
257 * Re-program the timer to the new value ...
259 timerfd_setup(ctx
, flags
, &ktmr
);
261 spin_unlock_irq(&ctx
->wqh
.lock
);
263 if (otmr
&& copy_to_user(otmr
, &kotmr
, sizeof(kotmr
)))
269 SYSCALL_DEFINE2(timerfd_gettime
, int, ufd
, struct itimerspec __user
*, otmr
)
272 struct timerfd_ctx
*ctx
;
273 struct itimerspec kotmr
;
275 file
= timerfd_fget(ufd
);
277 return PTR_ERR(file
);
278 ctx
= file
->private_data
;
280 spin_lock_irq(&ctx
->wqh
.lock
);
281 if (ctx
->expired
&& ctx
->tintv
.tv64
) {
284 hrtimer_forward_now(&ctx
->tmr
, ctx
->tintv
) - 1;
285 hrtimer_restart(&ctx
->tmr
);
287 kotmr
.it_value
= ktime_to_timespec(timerfd_get_remaining(ctx
));
288 kotmr
.it_interval
= ktime_to_timespec(ctx
->tintv
);
289 spin_unlock_irq(&ctx
->wqh
.lock
);
292 return copy_to_user(otmr
, &kotmr
, sizeof(kotmr
)) ? -EFAULT
: 0;