]>
Commit | Line | Data |
---|---|---|
b215e283 DL |
1 | /* |
2 | * fs/timerfd.c | |
3 | * | |
4 | * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org> | |
5 | * | |
6 | * | |
7 | * Thanks to Thomas Gleixner for code reviews and useful comments. | |
8 | * | |
9 | */ | |
10 | ||
11 | #include <linux/file.h> | |
12 | #include <linux/poll.h> | |
13 | #include <linux/init.h> | |
14 | #include <linux/fs.h> | |
15 | #include <linux/sched.h> | |
16 | #include <linux/kernel.h> | |
5a0e3ad6 | 17 | #include <linux/slab.h> |
b215e283 DL |
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> | |
45cc2b96 | 24 | #include <linux/syscalls.h> |
b215e283 DL |
25 | |
26 | struct timerfd_ctx { | |
27 | struct hrtimer tmr; | |
28 | ktime_t tintv; | |
99ee5315 | 29 | ktime_t moffs; |
b215e283 | 30 | wait_queue_head_t wqh; |
4d672e7a | 31 | u64 ticks; |
b215e283 | 32 | int expired; |
4d672e7a | 33 | int clockid; |
99ee5315 | 34 | bool might_cancel; |
b215e283 DL |
35 | }; |
36 | ||
37 | /* | |
38 | * This gets called when the timer event triggers. We set the "expired" | |
39 | * flag, but we do not re-arm the timer (in case it's necessary, | |
4d672e7a | 40 | * tintv.tv64 != 0) until the timer is accessed. |
b215e283 DL |
41 | */ |
42 | static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr) | |
43 | { | |
44 | struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx, tmr); | |
45 | unsigned long flags; | |
46 | ||
18963c01 | 47 | spin_lock_irqsave(&ctx->wqh.lock, flags); |
b215e283 | 48 | ctx->expired = 1; |
4d672e7a | 49 | ctx->ticks++; |
b215e283 | 50 | wake_up_locked(&ctx->wqh); |
18963c01 | 51 | spin_unlock_irqrestore(&ctx->wqh.lock, flags); |
b215e283 DL |
52 | |
53 | return HRTIMER_NORESTART; | |
54 | } | |
55 | ||
4d672e7a DL |
56 | static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx) |
57 | { | |
76369470 | 58 | ktime_t remaining; |
4d672e7a | 59 | |
76369470 | 60 | remaining = hrtimer_expires_remaining(&ctx->tmr); |
4d672e7a DL |
61 | return remaining.tv64 < 0 ? ktime_set(0, 0): remaining; |
62 | } | |
63 | ||
99ee5315 TG |
64 | static bool timerfd_canceled(struct timerfd_ctx *ctx) |
65 | { | |
66 | ktime_t moffs; | |
67 | ||
68 | if (!ctx->might_cancel) | |
69 | return false; | |
70 | ||
71 | moffs = ktime_get_monotonic_offset(); | |
72 | ||
73 | if (moffs.tv64 == ctx->moffs.tv64) | |
74 | return false; | |
75 | ||
76 | ctx->moffs = moffs; | |
77 | return true; | |
78 | } | |
79 | ||
80 | static int timerfd_setup(struct timerfd_ctx *ctx, int flags, | |
81 | const struct itimerspec *ktmr) | |
b215e283 DL |
82 | { |
83 | enum hrtimer_mode htmode; | |
84 | ktime_t texp; | |
99ee5315 | 85 | int clockid = ctx->clockid; |
b215e283 DL |
86 | |
87 | htmode = (flags & TFD_TIMER_ABSTIME) ? | |
88 | HRTIMER_MODE_ABS: HRTIMER_MODE_REL; | |
89 | ||
99ee5315 TG |
90 | ctx->might_cancel = false; |
91 | if (htmode == HRTIMER_MODE_ABS && ctx->clockid == CLOCK_REALTIME && | |
92 | (flags & TFD_TIMER_CANCELON_SET)) { | |
93 | clockid = CLOCK_REALTIME_COS; | |
94 | ctx->might_cancel = true; | |
95 | } | |
96 | ||
b215e283 DL |
97 | texp = timespec_to_ktime(ktmr->it_value); |
98 | ctx->expired = 0; | |
4d672e7a | 99 | ctx->ticks = 0; |
b215e283 | 100 | ctx->tintv = timespec_to_ktime(ktmr->it_interval); |
99ee5315 | 101 | hrtimer_init(&ctx->tmr, clockid, htmode); |
76369470 | 102 | hrtimer_set_expires(&ctx->tmr, texp); |
b215e283 | 103 | ctx->tmr.function = timerfd_tmrproc; |
99ee5315 | 104 | if (texp.tv64 != 0) { |
b215e283 | 105 | hrtimer_start(&ctx->tmr, texp, htmode); |
99ee5315 TG |
106 | if (timerfd_canceled(ctx)) |
107 | return -ECANCELED; | |
108 | } | |
109 | return 0; | |
b215e283 DL |
110 | } |
111 | ||
112 | static int timerfd_release(struct inode *inode, struct file *file) | |
113 | { | |
114 | struct timerfd_ctx *ctx = file->private_data; | |
115 | ||
116 | hrtimer_cancel(&ctx->tmr); | |
117 | kfree(ctx); | |
118 | return 0; | |
119 | } | |
120 | ||
121 | static unsigned int timerfd_poll(struct file *file, poll_table *wait) | |
122 | { | |
123 | struct timerfd_ctx *ctx = file->private_data; | |
124 | unsigned int events = 0; | |
125 | unsigned long flags; | |
126 | ||
127 | poll_wait(file, &ctx->wqh, wait); | |
128 | ||
18963c01 | 129 | spin_lock_irqsave(&ctx->wqh.lock, flags); |
4d672e7a | 130 | if (ctx->ticks) |
b215e283 | 131 | events |= POLLIN; |
18963c01 | 132 | spin_unlock_irqrestore(&ctx->wqh.lock, flags); |
b215e283 DL |
133 | |
134 | return events; | |
135 | } | |
136 | ||
137 | static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count, | |
138 | loff_t *ppos) | |
139 | { | |
140 | struct timerfd_ctx *ctx = file->private_data; | |
141 | ssize_t res; | |
09828402 | 142 | u64 ticks = 0; |
b215e283 DL |
143 | |
144 | if (count < sizeof(ticks)) | |
145 | return -EINVAL; | |
18963c01 | 146 | spin_lock_irq(&ctx->wqh.lock); |
8120a8aa MN |
147 | if (file->f_flags & O_NONBLOCK) |
148 | res = -EAGAIN; | |
149 | else | |
150 | res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks); | |
99ee5315 | 151 | |
4d672e7a DL |
152 | if (ctx->ticks) { |
153 | ticks = ctx->ticks; | |
99ee5315 TG |
154 | |
155 | /* | |
156 | * If clock has changed, we do not care about the | |
157 | * ticks and we do not rearm the timer. Userspace must | |
158 | * reevaluate anyway. | |
159 | */ | |
160 | if (timerfd_canceled(ctx)) { | |
161 | ticks = 0; | |
162 | ctx->expired = 0; | |
163 | res = -ECANCELED; | |
164 | } | |
165 | ||
4d672e7a | 166 | if (ctx->expired && ctx->tintv.tv64) { |
b215e283 DL |
167 | /* |
168 | * If tintv.tv64 != 0, this is a periodic timer that | |
169 | * needs to be re-armed. We avoid doing it in the timer | |
170 | * callback to avoid DoS attacks specifying a very | |
171 | * short timer period. | |
172 | */ | |
4d672e7a DL |
173 | ticks += hrtimer_forward_now(&ctx->tmr, |
174 | ctx->tintv) - 1; | |
b215e283 | 175 | hrtimer_restart(&ctx->tmr); |
4d672e7a DL |
176 | } |
177 | ctx->expired = 0; | |
178 | ctx->ticks = 0; | |
b215e283 | 179 | } |
18963c01 | 180 | spin_unlock_irq(&ctx->wqh.lock); |
b215e283 | 181 | if (ticks) |
09828402 | 182 | res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks); |
b215e283 DL |
183 | return res; |
184 | } | |
185 | ||
186 | static const struct file_operations timerfd_fops = { | |
187 | .release = timerfd_release, | |
188 | .poll = timerfd_poll, | |
189 | .read = timerfd_read, | |
6038f373 | 190 | .llseek = noop_llseek, |
b215e283 DL |
191 | }; |
192 | ||
4d672e7a DL |
193 | static struct file *timerfd_fget(int fd) |
194 | { | |
195 | struct file *file; | |
196 | ||
197 | file = fget(fd); | |
198 | if (!file) | |
199 | return ERR_PTR(-EBADF); | |
200 | if (file->f_op != &timerfd_fops) { | |
201 | fput(file); | |
202 | return ERR_PTR(-EINVAL); | |
203 | } | |
204 | ||
205 | return file; | |
206 | } | |
207 | ||
836f92ad | 208 | SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags) |
b215e283 | 209 | { |
2030a42c | 210 | int ufd; |
b215e283 | 211 | struct timerfd_ctx *ctx; |
b215e283 | 212 | |
e38b36f3 UD |
213 | /* Check the TFD_* constants for consistency. */ |
214 | BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC); | |
215 | BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK); | |
216 | ||
610d18f4 DL |
217 | if ((flags & ~TFD_CREATE_FLAGS) || |
218 | (clockid != CLOCK_MONOTONIC && | |
219 | clockid != CLOCK_REALTIME)) | |
b215e283 | 220 | return -EINVAL; |
4d672e7a DL |
221 | |
222 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
223 | if (!ctx) | |
224 | return -ENOMEM; | |
225 | ||
226 | init_waitqueue_head(&ctx->wqh); | |
227 | ctx->clockid = clockid; | |
228 | hrtimer_init(&ctx->tmr, clockid, HRTIMER_MODE_ABS); | |
99ee5315 | 229 | ctx->moffs = ktime_get_monotonic_offset(); |
4d672e7a | 230 | |
11fcb6c1 | 231 | ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx, |
628ff7c1 | 232 | O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS)); |
2030a42c | 233 | if (ufd < 0) |
4d672e7a | 234 | kfree(ctx); |
4d672e7a DL |
235 | |
236 | return ufd; | |
237 | } | |
238 | ||
836f92ad HC |
239 | SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags, |
240 | const struct itimerspec __user *, utmr, | |
241 | struct itimerspec __user *, otmr) | |
4d672e7a DL |
242 | { |
243 | struct file *file; | |
244 | struct timerfd_ctx *ctx; | |
245 | struct itimerspec ktmr, kotmr; | |
99ee5315 | 246 | int ret; |
4d672e7a DL |
247 | |
248 | if (copy_from_user(&ktmr, utmr, sizeof(ktmr))) | |
249 | return -EFAULT; | |
250 | ||
610d18f4 DL |
251 | if ((flags & ~TFD_SETTIME_FLAGS) || |
252 | !timespec_valid(&ktmr.it_value) || | |
b215e283 DL |
253 | !timespec_valid(&ktmr.it_interval)) |
254 | return -EINVAL; | |
255 | ||
4d672e7a DL |
256 | file = timerfd_fget(ufd); |
257 | if (IS_ERR(file)) | |
258 | return PTR_ERR(file); | |
259 | ctx = file->private_data; | |
b215e283 | 260 | |
4d672e7a DL |
261 | /* |
262 | * We need to stop the existing timer before reprogramming | |
263 | * it to the new values. | |
264 | */ | |
265 | for (;;) { | |
266 | spin_lock_irq(&ctx->wqh.lock); | |
267 | if (hrtimer_try_to_cancel(&ctx->tmr) >= 0) | |
268 | break; | |
18963c01 | 269 | spin_unlock_irq(&ctx->wqh.lock); |
4d672e7a | 270 | cpu_relax(); |
b215e283 DL |
271 | } |
272 | ||
4d672e7a DL |
273 | /* |
274 | * If the timer is expired and it's periodic, we need to advance it | |
275 | * because the caller may want to know the previous expiration time. | |
276 | * We do not update "ticks" and "expired" since the timer will be | |
277 | * re-programmed again in the following timerfd_setup() call. | |
278 | */ | |
279 | if (ctx->expired && ctx->tintv.tv64) | |
280 | hrtimer_forward_now(&ctx->tmr, ctx->tintv); | |
b215e283 | 281 | |
4d672e7a DL |
282 | kotmr.it_value = ktime_to_timespec(timerfd_get_remaining(ctx)); |
283 | kotmr.it_interval = ktime_to_timespec(ctx->tintv); | |
284 | ||
285 | /* | |
286 | * Re-program the timer to the new value ... | |
287 | */ | |
99ee5315 | 288 | ret = timerfd_setup(ctx, flags, &ktmr); |
4d672e7a DL |
289 | |
290 | spin_unlock_irq(&ctx->wqh.lock); | |
291 | fput(file); | |
292 | if (otmr && copy_to_user(otmr, &kotmr, sizeof(kotmr))) | |
293 | return -EFAULT; | |
294 | ||
99ee5315 | 295 | return ret; |
4d672e7a DL |
296 | } |
297 | ||
d4e82042 | 298 | SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct itimerspec __user *, otmr) |
4d672e7a DL |
299 | { |
300 | struct file *file; | |
301 | struct timerfd_ctx *ctx; | |
302 | struct itimerspec kotmr; | |
303 | ||
304 | file = timerfd_fget(ufd); | |
305 | if (IS_ERR(file)) | |
306 | return PTR_ERR(file); | |
307 | ctx = file->private_data; | |
308 | ||
309 | spin_lock_irq(&ctx->wqh.lock); | |
310 | if (ctx->expired && ctx->tintv.tv64) { | |
311 | ctx->expired = 0; | |
312 | ctx->ticks += | |
313 | hrtimer_forward_now(&ctx->tmr, ctx->tintv) - 1; | |
314 | hrtimer_restart(&ctx->tmr); | |
315 | } | |
316 | kotmr.it_value = ktime_to_timespec(timerfd_get_remaining(ctx)); | |
317 | kotmr.it_interval = ktime_to_timespec(ctx->tintv); | |
318 | spin_unlock_irq(&ctx->wqh.lock); | |
319 | fput(file); | |
320 | ||
321 | return copy_to_user(otmr, &kotmr, sizeof(kotmr)) ? -EFAULT: 0; | |
b215e283 DL |
322 | } |
323 |