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