]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
b215e283 DL |
2 | /* |
3 | * fs/timerfd.c | |
4 | * | |
5 | * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org> | |
6 | * | |
7 | * | |
8 | * Thanks to Thomas Gleixner for code reviews and useful comments. | |
9 | * | |
10 | */ | |
11 | ||
11ffa9d6 | 12 | #include <linux/alarmtimer.h> |
b215e283 DL |
13 | #include <linux/file.h> |
14 | #include <linux/poll.h> | |
15 | #include <linux/init.h> | |
16 | #include <linux/fs.h> | |
17 | #include <linux/sched.h> | |
18 | #include <linux/kernel.h> | |
5a0e3ad6 | 19 | #include <linux/slab.h> |
b215e283 DL |
20 | #include <linux/list.h> |
21 | #include <linux/spinlock.h> | |
22 | #include <linux/time.h> | |
23 | #include <linux/hrtimer.h> | |
24 | #include <linux/anon_inodes.h> | |
25 | #include <linux/timerfd.h> | |
45cc2b96 | 26 | #include <linux/syscalls.h> |
9d94b9e2 | 27 | #include <linux/compat.h> |
9ec26907 | 28 | #include <linux/rcupdate.h> |
b215e283 DL |
29 | |
30 | struct timerfd_ctx { | |
11ffa9d6 TP |
31 | union { |
32 | struct hrtimer tmr; | |
33 | struct alarm alarm; | |
34 | } t; | |
b215e283 | 35 | ktime_t tintv; |
99ee5315 | 36 | ktime_t moffs; |
b215e283 | 37 | wait_queue_head_t wqh; |
4d672e7a | 38 | u64 ticks; |
4d672e7a | 39 | int clockid; |
af9c4957 CG |
40 | short unsigned expired; |
41 | short unsigned settime_flags; /* to show in fdinfo */ | |
9ec26907 TG |
42 | struct rcu_head rcu; |
43 | struct list_head clist; | |
1e38da30 | 44 | spinlock_t cancel_lock; |
99ee5315 | 45 | bool might_cancel; |
b215e283 DL |
46 | }; |
47 | ||
9ec26907 TG |
48 | static LIST_HEAD(cancel_list); |
49 | static DEFINE_SPINLOCK(cancel_lock); | |
50 | ||
11ffa9d6 TP |
51 | static inline bool isalarm(struct timerfd_ctx *ctx) |
52 | { | |
53 | return ctx->clockid == CLOCK_REALTIME_ALARM || | |
54 | ctx->clockid == CLOCK_BOOTTIME_ALARM; | |
55 | } | |
56 | ||
b215e283 DL |
57 | /* |
58 | * This gets called when the timer event triggers. We set the "expired" | |
59 | * flag, but we do not re-arm the timer (in case it's necessary, | |
2456e855 | 60 | * tintv != 0) until the timer is accessed. |
b215e283 | 61 | */ |
11ffa9d6 | 62 | static void timerfd_triggered(struct timerfd_ctx *ctx) |
b215e283 | 63 | { |
b215e283 DL |
64 | unsigned long flags; |
65 | ||
18963c01 | 66 | spin_lock_irqsave(&ctx->wqh.lock, flags); |
b215e283 | 67 | ctx->expired = 1; |
4d672e7a | 68 | ctx->ticks++; |
7dda7128 | 69 | wake_up_locked_poll(&ctx->wqh, EPOLLIN); |
18963c01 | 70 | spin_unlock_irqrestore(&ctx->wqh.lock, flags); |
11ffa9d6 | 71 | } |
b215e283 | 72 | |
11ffa9d6 TP |
73 | static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr) |
74 | { | |
75 | struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx, | |
76 | t.tmr); | |
77 | timerfd_triggered(ctx); | |
b215e283 DL |
78 | return HRTIMER_NORESTART; |
79 | } | |
80 | ||
11ffa9d6 TP |
81 | static enum alarmtimer_restart timerfd_alarmproc(struct alarm *alarm, |
82 | ktime_t now) | |
83 | { | |
84 | struct timerfd_ctx *ctx = container_of(alarm, struct timerfd_ctx, | |
85 | t.alarm); | |
86 | timerfd_triggered(ctx); | |
87 | return ALARMTIMER_NORESTART; | |
88 | } | |
89 | ||
9ec26907 TG |
90 | /* |
91 | * Called when the clock was set to cancel the timers in the cancel | |
1123d939 MA |
92 | * list. This will wake up processes waiting on these timers. The |
93 | * wake-up requires ctx->ticks to be non zero, therefore we increment | |
94 | * it before calling wake_up_locked(). | |
9ec26907 TG |
95 | */ |
96 | void timerfd_clock_was_set(void) | |
4d672e7a | 97 | { |
2456e855 | 98 | ktime_t moffs = ktime_mono_to_real(0); |
9ec26907 TG |
99 | struct timerfd_ctx *ctx; |
100 | unsigned long flags; | |
4d672e7a | 101 | |
9ec26907 TG |
102 | rcu_read_lock(); |
103 | list_for_each_entry_rcu(ctx, &cancel_list, clist) { | |
104 | if (!ctx->might_cancel) | |
105 | continue; | |
106 | spin_lock_irqsave(&ctx->wqh.lock, flags); | |
2456e855 TG |
107 | if (ctx->moffs != moffs) { |
108 | ctx->moffs = KTIME_MAX; | |
1123d939 | 109 | ctx->ticks++; |
7dda7128 | 110 | wake_up_locked_poll(&ctx->wqh, EPOLLIN); |
9ec26907 TG |
111 | } |
112 | spin_unlock_irqrestore(&ctx->wqh.lock, flags); | |
113 | } | |
114 | rcu_read_unlock(); | |
4d672e7a DL |
115 | } |
116 | ||
1e38da30 | 117 | static void __timerfd_remove_cancel(struct timerfd_ctx *ctx) |
99ee5315 | 118 | { |
9ec26907 TG |
119 | if (ctx->might_cancel) { |
120 | ctx->might_cancel = false; | |
121 | spin_lock(&cancel_lock); | |
122 | list_del_rcu(&ctx->clist); | |
123 | spin_unlock(&cancel_lock); | |
124 | } | |
125 | } | |
99ee5315 | 126 | |
1e38da30 TG |
127 | static void timerfd_remove_cancel(struct timerfd_ctx *ctx) |
128 | { | |
129 | spin_lock(&ctx->cancel_lock); | |
130 | __timerfd_remove_cancel(ctx); | |
131 | spin_unlock(&ctx->cancel_lock); | |
132 | } | |
133 | ||
9ec26907 TG |
134 | static bool timerfd_canceled(struct timerfd_ctx *ctx) |
135 | { | |
2456e855 | 136 | if (!ctx->might_cancel || ctx->moffs != KTIME_MAX) |
99ee5315 | 137 | return false; |
2456e855 | 138 | ctx->moffs = ktime_mono_to_real(0); |
9ec26907 TG |
139 | return true; |
140 | } | |
99ee5315 | 141 | |
9ec26907 TG |
142 | static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags) |
143 | { | |
1e38da30 | 144 | spin_lock(&ctx->cancel_lock); |
11ffa9d6 TP |
145 | if ((ctx->clockid == CLOCK_REALTIME || |
146 | ctx->clockid == CLOCK_REALTIME_ALARM) && | |
147 | (flags & TFD_TIMER_ABSTIME) && (flags & TFD_TIMER_CANCEL_ON_SET)) { | |
9ec26907 TG |
148 | if (!ctx->might_cancel) { |
149 | ctx->might_cancel = true; | |
150 | spin_lock(&cancel_lock); | |
151 | list_add_rcu(&ctx->clist, &cancel_list); | |
152 | spin_unlock(&cancel_lock); | |
153 | } | |
1e38da30 TG |
154 | } else { |
155 | __timerfd_remove_cancel(ctx); | |
9ec26907 | 156 | } |
1e38da30 | 157 | spin_unlock(&ctx->cancel_lock); |
9ec26907 | 158 | } |
99ee5315 | 159 | |
9ec26907 TG |
160 | static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx) |
161 | { | |
162 | ktime_t remaining; | |
99ee5315 | 163 | |
11ffa9d6 TP |
164 | if (isalarm(ctx)) |
165 | remaining = alarm_expires_remaining(&ctx->t.alarm); | |
166 | else | |
b62526ed | 167 | remaining = hrtimer_expires_remaining_adjusted(&ctx->t.tmr); |
11ffa9d6 | 168 | |
8b0e1953 | 169 | return remaining < 0 ? 0: remaining; |
99ee5315 TG |
170 | } |
171 | ||
172 | static int timerfd_setup(struct timerfd_ctx *ctx, int flags, | |
bff41203 | 173 | const struct itimerspec64 *ktmr) |
b215e283 DL |
174 | { |
175 | enum hrtimer_mode htmode; | |
176 | ktime_t texp; | |
99ee5315 | 177 | int clockid = ctx->clockid; |
b215e283 DL |
178 | |
179 | htmode = (flags & TFD_TIMER_ABSTIME) ? | |
180 | HRTIMER_MODE_ABS: HRTIMER_MODE_REL; | |
181 | ||
bff41203 | 182 | texp = timespec64_to_ktime(ktmr->it_value); |
b215e283 | 183 | ctx->expired = 0; |
4d672e7a | 184 | ctx->ticks = 0; |
bff41203 | 185 | ctx->tintv = timespec64_to_ktime(ktmr->it_interval); |
11ffa9d6 TP |
186 | |
187 | if (isalarm(ctx)) { | |
188 | alarm_init(&ctx->t.alarm, | |
189 | ctx->clockid == CLOCK_REALTIME_ALARM ? | |
190 | ALARM_REALTIME : ALARM_BOOTTIME, | |
191 | timerfd_alarmproc); | |
192 | } else { | |
193 | hrtimer_init(&ctx->t.tmr, clockid, htmode); | |
194 | hrtimer_set_expires(&ctx->t.tmr, texp); | |
195 | ctx->t.tmr.function = timerfd_tmrproc; | |
196 | } | |
197 | ||
2456e855 | 198 | if (texp != 0) { |
11ffa9d6 TP |
199 | if (isalarm(ctx)) { |
200 | if (flags & TFD_TIMER_ABSTIME) | |
201 | alarm_start(&ctx->t.alarm, texp); | |
202 | else | |
203 | alarm_start_relative(&ctx->t.alarm, texp); | |
204 | } else { | |
205 | hrtimer_start(&ctx->t.tmr, texp, htmode); | |
206 | } | |
207 | ||
99ee5315 TG |
208 | if (timerfd_canceled(ctx)) |
209 | return -ECANCELED; | |
210 | } | |
af9c4957 CG |
211 | |
212 | ctx->settime_flags = flags & TFD_SETTIME_FLAGS; | |
99ee5315 | 213 | return 0; |
b215e283 DL |
214 | } |
215 | ||
216 | static int timerfd_release(struct inode *inode, struct file *file) | |
217 | { | |
218 | struct timerfd_ctx *ctx = file->private_data; | |
219 | ||
9ec26907 | 220 | timerfd_remove_cancel(ctx); |
11ffa9d6 TP |
221 | |
222 | if (isalarm(ctx)) | |
223 | alarm_cancel(&ctx->t.alarm); | |
224 | else | |
225 | hrtimer_cancel(&ctx->t.tmr); | |
9ec26907 | 226 | kfree_rcu(ctx, rcu); |
b215e283 DL |
227 | return 0; |
228 | } | |
a11e1d43 LT |
229 | |
230 | static __poll_t timerfd_poll(struct file *file, poll_table *wait) | |
b215e283 DL |
231 | { |
232 | struct timerfd_ctx *ctx = file->private_data; | |
a11e1d43 LT |
233 | __poll_t events = 0; |
234 | unsigned long flags; | |
b215e283 | 235 | |
a11e1d43 | 236 | poll_wait(file, &ctx->wqh, wait); |
b215e283 | 237 | |
a11e1d43 LT |
238 | spin_lock_irqsave(&ctx->wqh.lock, flags); |
239 | if (ctx->ticks) | |
240 | events |= EPOLLIN; | |
241 | spin_unlock_irqrestore(&ctx->wqh.lock, flags); | |
b215e283 | 242 | |
a11e1d43 | 243 | return events; |
b215e283 DL |
244 | } |
245 | ||
246 | static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count, | |
247 | loff_t *ppos) | |
248 | { | |
249 | struct timerfd_ctx *ctx = file->private_data; | |
250 | ssize_t res; | |
09828402 | 251 | u64 ticks = 0; |
b215e283 DL |
252 | |
253 | if (count < sizeof(ticks)) | |
254 | return -EINVAL; | |
18963c01 | 255 | spin_lock_irq(&ctx->wqh.lock); |
8120a8aa MN |
256 | if (file->f_flags & O_NONBLOCK) |
257 | res = -EAGAIN; | |
258 | else | |
259 | res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks); | |
99ee5315 | 260 | |
9ec26907 TG |
261 | /* |
262 | * If clock has changed, we do not care about the | |
263 | * ticks and we do not rearm the timer. Userspace must | |
264 | * reevaluate anyway. | |
265 | */ | |
266 | if (timerfd_canceled(ctx)) { | |
267 | ctx->ticks = 0; | |
268 | ctx->expired = 0; | |
269 | res = -ECANCELED; | |
270 | } | |
271 | ||
4d672e7a DL |
272 | if (ctx->ticks) { |
273 | ticks = ctx->ticks; | |
99ee5315 | 274 | |
2456e855 | 275 | if (ctx->expired && ctx->tintv) { |
b215e283 | 276 | /* |
2456e855 | 277 | * If tintv != 0, this is a periodic timer that |
b215e283 DL |
278 | * needs to be re-armed. We avoid doing it in the timer |
279 | * callback to avoid DoS attacks specifying a very | |
280 | * short timer period. | |
281 | */ | |
11ffa9d6 TP |
282 | if (isalarm(ctx)) { |
283 | ticks += alarm_forward_now( | |
284 | &ctx->t.alarm, ctx->tintv) - 1; | |
285 | alarm_restart(&ctx->t.alarm); | |
286 | } else { | |
287 | ticks += hrtimer_forward_now(&ctx->t.tmr, | |
288 | ctx->tintv) - 1; | |
289 | hrtimer_restart(&ctx->t.tmr); | |
290 | } | |
4d672e7a DL |
291 | } |
292 | ctx->expired = 0; | |
293 | ctx->ticks = 0; | |
b215e283 | 294 | } |
18963c01 | 295 | spin_unlock_irq(&ctx->wqh.lock); |
b215e283 | 296 | if (ticks) |
09828402 | 297 | res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks); |
b215e283 DL |
298 | return res; |
299 | } | |
300 | ||
af9c4957 | 301 | #ifdef CONFIG_PROC_FS |
a3816ab0 | 302 | static void timerfd_show(struct seq_file *m, struct file *file) |
af9c4957 CG |
303 | { |
304 | struct timerfd_ctx *ctx = file->private_data; | |
305 | struct itimerspec t; | |
306 | ||
307 | spin_lock_irq(&ctx->wqh.lock); | |
308 | t.it_value = ktime_to_timespec(timerfd_get_remaining(ctx)); | |
309 | t.it_interval = ktime_to_timespec(ctx->tintv); | |
310 | spin_unlock_irq(&ctx->wqh.lock); | |
311 | ||
a3816ab0 JP |
312 | seq_printf(m, |
313 | "clockid: %d\n" | |
314 | "ticks: %llu\n" | |
315 | "settime flags: 0%o\n" | |
316 | "it_value: (%llu, %llu)\n" | |
317 | "it_interval: (%llu, %llu)\n", | |
318 | ctx->clockid, | |
319 | (unsigned long long)ctx->ticks, | |
320 | ctx->settime_flags, | |
321 | (unsigned long long)t.it_value.tv_sec, | |
322 | (unsigned long long)t.it_value.tv_nsec, | |
323 | (unsigned long long)t.it_interval.tv_sec, | |
324 | (unsigned long long)t.it_interval.tv_nsec); | |
af9c4957 CG |
325 | } |
326 | #else | |
327 | #define timerfd_show NULL | |
328 | #endif | |
329 | ||
5442e9fb CG |
330 | #ifdef CONFIG_CHECKPOINT_RESTORE |
331 | static long timerfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
332 | { | |
333 | struct timerfd_ctx *ctx = file->private_data; | |
334 | int ret = 0; | |
335 | ||
336 | switch (cmd) { | |
337 | case TFD_IOC_SET_TICKS: { | |
338 | u64 ticks; | |
339 | ||
340 | if (copy_from_user(&ticks, (u64 __user *)arg, sizeof(ticks))) | |
341 | return -EFAULT; | |
342 | if (!ticks) | |
343 | return -EINVAL; | |
344 | ||
345 | spin_lock_irq(&ctx->wqh.lock); | |
346 | if (!timerfd_canceled(ctx)) { | |
347 | ctx->ticks = ticks; | |
7dda7128 | 348 | wake_up_locked_poll(&ctx->wqh, EPOLLIN); |
5442e9fb CG |
349 | } else |
350 | ret = -ECANCELED; | |
351 | spin_unlock_irq(&ctx->wqh.lock); | |
352 | break; | |
353 | } | |
354 | default: | |
355 | ret = -ENOTTY; | |
356 | break; | |
357 | } | |
358 | ||
359 | return ret; | |
360 | } | |
361 | #else | |
362 | #define timerfd_ioctl NULL | |
363 | #endif | |
364 | ||
b215e283 DL |
365 | static const struct file_operations timerfd_fops = { |
366 | .release = timerfd_release, | |
a11e1d43 | 367 | .poll = timerfd_poll, |
b215e283 | 368 | .read = timerfd_read, |
6038f373 | 369 | .llseek = noop_llseek, |
af9c4957 | 370 | .show_fdinfo = timerfd_show, |
5442e9fb | 371 | .unlocked_ioctl = timerfd_ioctl, |
b215e283 DL |
372 | }; |
373 | ||
2903ff01 | 374 | static int timerfd_fget(int fd, struct fd *p) |
4d672e7a | 375 | { |
2903ff01 AV |
376 | struct fd f = fdget(fd); |
377 | if (!f.file) | |
378 | return -EBADF; | |
379 | if (f.file->f_op != &timerfd_fops) { | |
380 | fdput(f); | |
381 | return -EINVAL; | |
4d672e7a | 382 | } |
2903ff01 AV |
383 | *p = f; |
384 | return 0; | |
4d672e7a DL |
385 | } |
386 | ||
836f92ad | 387 | SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags) |
b215e283 | 388 | { |
2030a42c | 389 | int ufd; |
b215e283 | 390 | struct timerfd_ctx *ctx; |
b215e283 | 391 | |
e38b36f3 UD |
392 | /* Check the TFD_* constants for consistency. */ |
393 | BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC); | |
394 | BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK); | |
395 | ||
610d18f4 DL |
396 | if ((flags & ~TFD_CREATE_FLAGS) || |
397 | (clockid != CLOCK_MONOTONIC && | |
11ffa9d6 TP |
398 | clockid != CLOCK_REALTIME && |
399 | clockid != CLOCK_REALTIME_ALARM && | |
4a2378a9 | 400 | clockid != CLOCK_BOOTTIME && |
11ffa9d6 | 401 | clockid != CLOCK_BOOTTIME_ALARM)) |
b215e283 | 402 | return -EINVAL; |
4d672e7a | 403 | |
25b68a8f SS |
404 | if ((clockid == CLOCK_REALTIME_ALARM || |
405 | clockid == CLOCK_BOOTTIME_ALARM) && | |
406 | !capable(CAP_WAKE_ALARM)) | |
2895a5e5 EC |
407 | return -EPERM; |
408 | ||
4d672e7a DL |
409 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
410 | if (!ctx) | |
411 | return -ENOMEM; | |
412 | ||
413 | init_waitqueue_head(&ctx->wqh); | |
1e38da30 | 414 | spin_lock_init(&ctx->cancel_lock); |
4d672e7a | 415 | ctx->clockid = clockid; |
11ffa9d6 TP |
416 | |
417 | if (isalarm(ctx)) | |
418 | alarm_init(&ctx->t.alarm, | |
419 | ctx->clockid == CLOCK_REALTIME_ALARM ? | |
420 | ALARM_REALTIME : ALARM_BOOTTIME, | |
421 | timerfd_alarmproc); | |
422 | else | |
423 | hrtimer_init(&ctx->t.tmr, clockid, HRTIMER_MODE_ABS); | |
424 | ||
2456e855 | 425 | ctx->moffs = ktime_mono_to_real(0); |
4d672e7a | 426 | |
11fcb6c1 | 427 | ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx, |
628ff7c1 | 428 | O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS)); |
2030a42c | 429 | if (ufd < 0) |
4d672e7a | 430 | kfree(ctx); |
4d672e7a DL |
431 | |
432 | return ufd; | |
433 | } | |
434 | ||
9d94b9e2 | 435 | static int do_timerfd_settime(int ufd, int flags, |
bff41203 DD |
436 | const struct itimerspec64 *new, |
437 | struct itimerspec64 *old) | |
4d672e7a | 438 | { |
2903ff01 | 439 | struct fd f; |
4d672e7a | 440 | struct timerfd_ctx *ctx; |
2903ff01 | 441 | int ret; |
4d672e7a | 442 | |
610d18f4 | 443 | if ((flags & ~TFD_SETTIME_FLAGS) || |
bff41203 | 444 | !itimerspec64_valid(new)) |
b215e283 DL |
445 | return -EINVAL; |
446 | ||
2903ff01 AV |
447 | ret = timerfd_fget(ufd, &f); |
448 | if (ret) | |
449 | return ret; | |
450 | ctx = f.file->private_data; | |
b215e283 | 451 | |
25b68a8f | 452 | if (isalarm(ctx) && !capable(CAP_WAKE_ALARM)) { |
2895a5e5 EC |
453 | fdput(f); |
454 | return -EPERM; | |
455 | } | |
456 | ||
9ec26907 TG |
457 | timerfd_setup_cancel(ctx, flags); |
458 | ||
4d672e7a DL |
459 | /* |
460 | * We need to stop the existing timer before reprogramming | |
461 | * it to the new values. | |
462 | */ | |
463 | for (;;) { | |
464 | spin_lock_irq(&ctx->wqh.lock); | |
11ffa9d6 TP |
465 | |
466 | if (isalarm(ctx)) { | |
467 | if (alarm_try_to_cancel(&ctx->t.alarm) >= 0) | |
468 | break; | |
469 | } else { | |
470 | if (hrtimer_try_to_cancel(&ctx->t.tmr) >= 0) | |
471 | break; | |
472 | } | |
18963c01 | 473 | spin_unlock_irq(&ctx->wqh.lock); |
4d672e7a | 474 | cpu_relax(); |
b215e283 DL |
475 | } |
476 | ||
4d672e7a DL |
477 | /* |
478 | * If the timer is expired and it's periodic, we need to advance it | |
479 | * because the caller may want to know the previous expiration time. | |
480 | * We do not update "ticks" and "expired" since the timer will be | |
481 | * re-programmed again in the following timerfd_setup() call. | |
482 | */ | |
2456e855 | 483 | if (ctx->expired && ctx->tintv) { |
11ffa9d6 TP |
484 | if (isalarm(ctx)) |
485 | alarm_forward_now(&ctx->t.alarm, ctx->tintv); | |
486 | else | |
487 | hrtimer_forward_now(&ctx->t.tmr, ctx->tintv); | |
488 | } | |
b215e283 | 489 | |
bff41203 DD |
490 | old->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx)); |
491 | old->it_interval = ktime_to_timespec64(ctx->tintv); | |
4d672e7a DL |
492 | |
493 | /* | |
494 | * Re-program the timer to the new value ... | |
495 | */ | |
9d94b9e2 | 496 | ret = timerfd_setup(ctx, flags, new); |
4d672e7a DL |
497 | |
498 | spin_unlock_irq(&ctx->wqh.lock); | |
2903ff01 | 499 | fdput(f); |
99ee5315 | 500 | return ret; |
4d672e7a DL |
501 | } |
502 | ||
bff41203 | 503 | static int do_timerfd_gettime(int ufd, struct itimerspec64 *t) |
4d672e7a | 504 | { |
2903ff01 | 505 | struct fd f; |
4d672e7a | 506 | struct timerfd_ctx *ctx; |
2903ff01 AV |
507 | int ret = timerfd_fget(ufd, &f); |
508 | if (ret) | |
509 | return ret; | |
510 | ctx = f.file->private_data; | |
4d672e7a DL |
511 | |
512 | spin_lock_irq(&ctx->wqh.lock); | |
2456e855 | 513 | if (ctx->expired && ctx->tintv) { |
4d672e7a | 514 | ctx->expired = 0; |
11ffa9d6 TP |
515 | |
516 | if (isalarm(ctx)) { | |
517 | ctx->ticks += | |
518 | alarm_forward_now( | |
519 | &ctx->t.alarm, ctx->tintv) - 1; | |
520 | alarm_restart(&ctx->t.alarm); | |
521 | } else { | |
522 | ctx->ticks += | |
523 | hrtimer_forward_now(&ctx->t.tmr, ctx->tintv) | |
524 | - 1; | |
525 | hrtimer_restart(&ctx->t.tmr); | |
526 | } | |
4d672e7a | 527 | } |
bff41203 DD |
528 | t->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx)); |
529 | t->it_interval = ktime_to_timespec64(ctx->tintv); | |
4d672e7a | 530 | spin_unlock_irq(&ctx->wqh.lock); |
2903ff01 | 531 | fdput(f); |
9d94b9e2 AV |
532 | return 0; |
533 | } | |
534 | ||
535 | SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags, | |
6ff84735 DD |
536 | const struct __kernel_itimerspec __user *, utmr, |
537 | struct __kernel_itimerspec __user *, otmr) | |
9d94b9e2 | 538 | { |
bff41203 | 539 | struct itimerspec64 new, old; |
9d94b9e2 AV |
540 | int ret; |
541 | ||
bff41203 | 542 | if (get_itimerspec64(&new, utmr)) |
9d94b9e2 AV |
543 | return -EFAULT; |
544 | ret = do_timerfd_settime(ufd, flags, &new, &old); | |
545 | if (ret) | |
546 | return ret; | |
bff41203 | 547 | if (otmr && put_itimerspec64(&old, otmr)) |
9d94b9e2 AV |
548 | return -EFAULT; |
549 | ||
550 | return ret; | |
551 | } | |
4d672e7a | 552 | |
6ff84735 | 553 | SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct __kernel_itimerspec __user *, otmr) |
9d94b9e2 | 554 | { |
bff41203 | 555 | struct itimerspec64 kotmr; |
9d94b9e2 AV |
556 | int ret = do_timerfd_gettime(ufd, &kotmr); |
557 | if (ret) | |
558 | return ret; | |
bff41203 | 559 | return put_itimerspec64(&kotmr, otmr) ? -EFAULT : 0; |
b215e283 DL |
560 | } |
561 | ||
6ff84735 | 562 | #ifdef CONFIG_COMPAT_32BIT_TIME |
9d94b9e2 | 563 | COMPAT_SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags, |
0e803baf HC |
564 | const struct compat_itimerspec __user *, utmr, |
565 | struct compat_itimerspec __user *, otmr) | |
9d94b9e2 | 566 | { |
bff41203 | 567 | struct itimerspec64 new, old; |
9d94b9e2 AV |
568 | int ret; |
569 | ||
bff41203 | 570 | if (get_compat_itimerspec64(&new, utmr)) |
9d94b9e2 AV |
571 | return -EFAULT; |
572 | ret = do_timerfd_settime(ufd, flags, &new, &old); | |
573 | if (ret) | |
574 | return ret; | |
bff41203 | 575 | if (otmr && put_compat_itimerspec64(&old, otmr)) |
9d94b9e2 AV |
576 | return -EFAULT; |
577 | return ret; | |
578 | } | |
579 | ||
580 | COMPAT_SYSCALL_DEFINE2(timerfd_gettime, int, ufd, | |
0e803baf | 581 | struct compat_itimerspec __user *, otmr) |
9d94b9e2 | 582 | { |
bff41203 | 583 | struct itimerspec64 kotmr; |
9d94b9e2 AV |
584 | int ret = do_timerfd_gettime(ufd, &kotmr); |
585 | if (ret) | |
586 | return ret; | |
bff41203 | 587 | return put_compat_itimerspec64(&kotmr, otmr) ? -EFAULT : 0; |
9d94b9e2 AV |
588 | } |
589 | #endif |