]>
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 | ||
66f7b0c8 TG |
118 | static void timerfd_resume_work(struct work_struct *work) |
119 | { | |
120 | timerfd_clock_was_set(); | |
121 | } | |
122 | ||
123 | static DECLARE_WORK(timerfd_work, timerfd_resume_work); | |
124 | ||
125 | /* | |
126 | * Invoked from timekeeping_resume(). Defer the actual update to work so | |
127 | * timerfd_clock_was_set() runs in task context. | |
128 | */ | |
129 | void timerfd_resume(void) | |
130 | { | |
131 | schedule_work(&timerfd_work); | |
132 | } | |
133 | ||
1e38da30 | 134 | static void __timerfd_remove_cancel(struct timerfd_ctx *ctx) |
99ee5315 | 135 | { |
9ec26907 TG |
136 | if (ctx->might_cancel) { |
137 | ctx->might_cancel = false; | |
138 | spin_lock(&cancel_lock); | |
139 | list_del_rcu(&ctx->clist); | |
140 | spin_unlock(&cancel_lock); | |
141 | } | |
142 | } | |
99ee5315 | 143 | |
1e38da30 TG |
144 | static void timerfd_remove_cancel(struct timerfd_ctx *ctx) |
145 | { | |
146 | spin_lock(&ctx->cancel_lock); | |
147 | __timerfd_remove_cancel(ctx); | |
148 | spin_unlock(&ctx->cancel_lock); | |
149 | } | |
150 | ||
9ec26907 TG |
151 | static bool timerfd_canceled(struct timerfd_ctx *ctx) |
152 | { | |
2456e855 | 153 | if (!ctx->might_cancel || ctx->moffs != KTIME_MAX) |
99ee5315 | 154 | return false; |
2456e855 | 155 | ctx->moffs = ktime_mono_to_real(0); |
9ec26907 TG |
156 | return true; |
157 | } | |
99ee5315 | 158 | |
9ec26907 TG |
159 | static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags) |
160 | { | |
1e38da30 | 161 | spin_lock(&ctx->cancel_lock); |
11ffa9d6 TP |
162 | if ((ctx->clockid == CLOCK_REALTIME || |
163 | ctx->clockid == CLOCK_REALTIME_ALARM) && | |
164 | (flags & TFD_TIMER_ABSTIME) && (flags & TFD_TIMER_CANCEL_ON_SET)) { | |
9ec26907 TG |
165 | if (!ctx->might_cancel) { |
166 | ctx->might_cancel = true; | |
167 | spin_lock(&cancel_lock); | |
168 | list_add_rcu(&ctx->clist, &cancel_list); | |
169 | spin_unlock(&cancel_lock); | |
170 | } | |
1e38da30 TG |
171 | } else { |
172 | __timerfd_remove_cancel(ctx); | |
9ec26907 | 173 | } |
1e38da30 | 174 | spin_unlock(&ctx->cancel_lock); |
9ec26907 | 175 | } |
99ee5315 | 176 | |
9ec26907 TG |
177 | static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx) |
178 | { | |
179 | ktime_t remaining; | |
99ee5315 | 180 | |
11ffa9d6 TP |
181 | if (isalarm(ctx)) |
182 | remaining = alarm_expires_remaining(&ctx->t.alarm); | |
183 | else | |
b62526ed | 184 | remaining = hrtimer_expires_remaining_adjusted(&ctx->t.tmr); |
11ffa9d6 | 185 | |
8b0e1953 | 186 | return remaining < 0 ? 0: remaining; |
99ee5315 TG |
187 | } |
188 | ||
189 | static int timerfd_setup(struct timerfd_ctx *ctx, int flags, | |
bff41203 | 190 | const struct itimerspec64 *ktmr) |
b215e283 DL |
191 | { |
192 | enum hrtimer_mode htmode; | |
193 | ktime_t texp; | |
99ee5315 | 194 | int clockid = ctx->clockid; |
b215e283 DL |
195 | |
196 | htmode = (flags & TFD_TIMER_ABSTIME) ? | |
197 | HRTIMER_MODE_ABS: HRTIMER_MODE_REL; | |
198 | ||
bff41203 | 199 | texp = timespec64_to_ktime(ktmr->it_value); |
b215e283 | 200 | ctx->expired = 0; |
4d672e7a | 201 | ctx->ticks = 0; |
bff41203 | 202 | ctx->tintv = timespec64_to_ktime(ktmr->it_interval); |
11ffa9d6 TP |
203 | |
204 | if (isalarm(ctx)) { | |
205 | alarm_init(&ctx->t.alarm, | |
206 | ctx->clockid == CLOCK_REALTIME_ALARM ? | |
207 | ALARM_REALTIME : ALARM_BOOTTIME, | |
208 | timerfd_alarmproc); | |
209 | } else { | |
210 | hrtimer_init(&ctx->t.tmr, clockid, htmode); | |
211 | hrtimer_set_expires(&ctx->t.tmr, texp); | |
212 | ctx->t.tmr.function = timerfd_tmrproc; | |
213 | } | |
214 | ||
2456e855 | 215 | if (texp != 0) { |
6cd889d4 AV |
216 | if (flags & TFD_TIMER_ABSTIME) |
217 | texp = timens_ktime_to_host(clockid, texp); | |
11ffa9d6 TP |
218 | if (isalarm(ctx)) { |
219 | if (flags & TFD_TIMER_ABSTIME) | |
220 | alarm_start(&ctx->t.alarm, texp); | |
221 | else | |
222 | alarm_start_relative(&ctx->t.alarm, texp); | |
223 | } else { | |
224 | hrtimer_start(&ctx->t.tmr, texp, htmode); | |
225 | } | |
226 | ||
99ee5315 TG |
227 | if (timerfd_canceled(ctx)) |
228 | return -ECANCELED; | |
229 | } | |
af9c4957 CG |
230 | |
231 | ctx->settime_flags = flags & TFD_SETTIME_FLAGS; | |
99ee5315 | 232 | return 0; |
b215e283 DL |
233 | } |
234 | ||
235 | static int timerfd_release(struct inode *inode, struct file *file) | |
236 | { | |
237 | struct timerfd_ctx *ctx = file->private_data; | |
238 | ||
9ec26907 | 239 | timerfd_remove_cancel(ctx); |
11ffa9d6 TP |
240 | |
241 | if (isalarm(ctx)) | |
242 | alarm_cancel(&ctx->t.alarm); | |
243 | else | |
244 | hrtimer_cancel(&ctx->t.tmr); | |
9ec26907 | 245 | kfree_rcu(ctx, rcu); |
b215e283 DL |
246 | return 0; |
247 | } | |
a11e1d43 LT |
248 | |
249 | static __poll_t timerfd_poll(struct file *file, poll_table *wait) | |
b215e283 DL |
250 | { |
251 | struct timerfd_ctx *ctx = file->private_data; | |
a11e1d43 LT |
252 | __poll_t events = 0; |
253 | unsigned long flags; | |
b215e283 | 254 | |
a11e1d43 | 255 | poll_wait(file, &ctx->wqh, wait); |
b215e283 | 256 | |
a11e1d43 LT |
257 | spin_lock_irqsave(&ctx->wqh.lock, flags); |
258 | if (ctx->ticks) | |
259 | events |= EPOLLIN; | |
260 | spin_unlock_irqrestore(&ctx->wqh.lock, flags); | |
b215e283 | 261 | |
a11e1d43 | 262 | return events; |
b215e283 DL |
263 | } |
264 | ||
d9497990 | 265 | static ssize_t timerfd_read_iter(struct kiocb *iocb, struct iov_iter *to) |
b215e283 | 266 | { |
d9497990 | 267 | struct file *file = iocb->ki_filp; |
b215e283 DL |
268 | struct timerfd_ctx *ctx = file->private_data; |
269 | ssize_t res; | |
09828402 | 270 | u64 ticks = 0; |
b215e283 | 271 | |
d9497990 | 272 | if (iov_iter_count(to) < sizeof(ticks)) |
b215e283 | 273 | return -EINVAL; |
d9497990 | 274 | |
18963c01 | 275 | spin_lock_irq(&ctx->wqh.lock); |
d9497990 | 276 | if (file->f_flags & O_NONBLOCK || iocb->ki_flags & IOCB_NOWAIT) |
8120a8aa MN |
277 | res = -EAGAIN; |
278 | else | |
279 | res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks); | |
99ee5315 | 280 | |
9ec26907 TG |
281 | /* |
282 | * If clock has changed, we do not care about the | |
283 | * ticks and we do not rearm the timer. Userspace must | |
284 | * reevaluate anyway. | |
285 | */ | |
286 | if (timerfd_canceled(ctx)) { | |
287 | ctx->ticks = 0; | |
288 | ctx->expired = 0; | |
289 | res = -ECANCELED; | |
290 | } | |
291 | ||
4d672e7a DL |
292 | if (ctx->ticks) { |
293 | ticks = ctx->ticks; | |
99ee5315 | 294 | |
2456e855 | 295 | if (ctx->expired && ctx->tintv) { |
b215e283 | 296 | /* |
2456e855 | 297 | * If tintv != 0, this is a periodic timer that |
b215e283 DL |
298 | * needs to be re-armed. We avoid doing it in the timer |
299 | * callback to avoid DoS attacks specifying a very | |
300 | * short timer period. | |
301 | */ | |
11ffa9d6 TP |
302 | if (isalarm(ctx)) { |
303 | ticks += alarm_forward_now( | |
304 | &ctx->t.alarm, ctx->tintv) - 1; | |
305 | alarm_restart(&ctx->t.alarm); | |
306 | } else { | |
307 | ticks += hrtimer_forward_now(&ctx->t.tmr, | |
308 | ctx->tintv) - 1; | |
309 | hrtimer_restart(&ctx->t.tmr); | |
310 | } | |
4d672e7a DL |
311 | } |
312 | ctx->expired = 0; | |
313 | ctx->ticks = 0; | |
b215e283 | 314 | } |
18963c01 | 315 | spin_unlock_irq(&ctx->wqh.lock); |
d9497990 JA |
316 | if (ticks) { |
317 | res = copy_to_iter(&ticks, sizeof(ticks), to); | |
318 | if (!res) | |
319 | res = -EFAULT; | |
320 | } | |
b215e283 DL |
321 | return res; |
322 | } | |
323 | ||
af9c4957 | 324 | #ifdef CONFIG_PROC_FS |
a3816ab0 | 325 | static void timerfd_show(struct seq_file *m, struct file *file) |
af9c4957 CG |
326 | { |
327 | struct timerfd_ctx *ctx = file->private_data; | |
bde9e963 | 328 | struct timespec64 value, interval; |
af9c4957 CG |
329 | |
330 | spin_lock_irq(&ctx->wqh.lock); | |
bde9e963 AB |
331 | value = ktime_to_timespec64(timerfd_get_remaining(ctx)); |
332 | interval = ktime_to_timespec64(ctx->tintv); | |
af9c4957 CG |
333 | spin_unlock_irq(&ctx->wqh.lock); |
334 | ||
a3816ab0 JP |
335 | seq_printf(m, |
336 | "clockid: %d\n" | |
337 | "ticks: %llu\n" | |
338 | "settime flags: 0%o\n" | |
339 | "it_value: (%llu, %llu)\n" | |
340 | "it_interval: (%llu, %llu)\n", | |
341 | ctx->clockid, | |
342 | (unsigned long long)ctx->ticks, | |
343 | ctx->settime_flags, | |
bde9e963 AB |
344 | (unsigned long long)value.tv_sec, |
345 | (unsigned long long)value.tv_nsec, | |
346 | (unsigned long long)interval.tv_sec, | |
347 | (unsigned long long)interval.tv_nsec); | |
af9c4957 CG |
348 | } |
349 | #else | |
350 | #define timerfd_show NULL | |
351 | #endif | |
352 | ||
5442e9fb CG |
353 | #ifdef CONFIG_CHECKPOINT_RESTORE |
354 | static long timerfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
355 | { | |
356 | struct timerfd_ctx *ctx = file->private_data; | |
357 | int ret = 0; | |
358 | ||
359 | switch (cmd) { | |
360 | case TFD_IOC_SET_TICKS: { | |
361 | u64 ticks; | |
362 | ||
363 | if (copy_from_user(&ticks, (u64 __user *)arg, sizeof(ticks))) | |
364 | return -EFAULT; | |
365 | if (!ticks) | |
366 | return -EINVAL; | |
367 | ||
368 | spin_lock_irq(&ctx->wqh.lock); | |
369 | if (!timerfd_canceled(ctx)) { | |
370 | ctx->ticks = ticks; | |
7dda7128 | 371 | wake_up_locked_poll(&ctx->wqh, EPOLLIN); |
5442e9fb CG |
372 | } else |
373 | ret = -ECANCELED; | |
374 | spin_unlock_irq(&ctx->wqh.lock); | |
375 | break; | |
376 | } | |
377 | default: | |
378 | ret = -ENOTTY; | |
379 | break; | |
380 | } | |
381 | ||
382 | return ret; | |
383 | } | |
384 | #else | |
385 | #define timerfd_ioctl NULL | |
386 | #endif | |
387 | ||
b215e283 DL |
388 | static const struct file_operations timerfd_fops = { |
389 | .release = timerfd_release, | |
a11e1d43 | 390 | .poll = timerfd_poll, |
d9497990 | 391 | .read_iter = timerfd_read_iter, |
6038f373 | 392 | .llseek = noop_llseek, |
af9c4957 | 393 | .show_fdinfo = timerfd_show, |
5442e9fb | 394 | .unlocked_ioctl = timerfd_ioctl, |
b215e283 DL |
395 | }; |
396 | ||
2903ff01 | 397 | static int timerfd_fget(int fd, struct fd *p) |
4d672e7a | 398 | { |
2903ff01 AV |
399 | struct fd f = fdget(fd); |
400 | if (!f.file) | |
401 | return -EBADF; | |
402 | if (f.file->f_op != &timerfd_fops) { | |
403 | fdput(f); | |
404 | return -EINVAL; | |
4d672e7a | 405 | } |
2903ff01 AV |
406 | *p = f; |
407 | return 0; | |
4d672e7a DL |
408 | } |
409 | ||
836f92ad | 410 | SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags) |
b215e283 | 411 | { |
2030a42c | 412 | int ufd; |
b215e283 | 413 | struct timerfd_ctx *ctx; |
d9497990 | 414 | struct file *file; |
b215e283 | 415 | |
e38b36f3 UD |
416 | /* Check the TFD_* constants for consistency. */ |
417 | BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC); | |
418 | BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK); | |
419 | ||
610d18f4 DL |
420 | if ((flags & ~TFD_CREATE_FLAGS) || |
421 | (clockid != CLOCK_MONOTONIC && | |
11ffa9d6 TP |
422 | clockid != CLOCK_REALTIME && |
423 | clockid != CLOCK_REALTIME_ALARM && | |
4a2378a9 | 424 | clockid != CLOCK_BOOTTIME && |
11ffa9d6 | 425 | clockid != CLOCK_BOOTTIME_ALARM)) |
b215e283 | 426 | return -EINVAL; |
4d672e7a | 427 | |
25b68a8f SS |
428 | if ((clockid == CLOCK_REALTIME_ALARM || |
429 | clockid == CLOCK_BOOTTIME_ALARM) && | |
430 | !capable(CAP_WAKE_ALARM)) | |
2895a5e5 EC |
431 | return -EPERM; |
432 | ||
4d672e7a DL |
433 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
434 | if (!ctx) | |
435 | return -ENOMEM; | |
436 | ||
437 | init_waitqueue_head(&ctx->wqh); | |
1e38da30 | 438 | spin_lock_init(&ctx->cancel_lock); |
4d672e7a | 439 | ctx->clockid = clockid; |
11ffa9d6 TP |
440 | |
441 | if (isalarm(ctx)) | |
442 | alarm_init(&ctx->t.alarm, | |
443 | ctx->clockid == CLOCK_REALTIME_ALARM ? | |
444 | ALARM_REALTIME : ALARM_BOOTTIME, | |
445 | timerfd_alarmproc); | |
446 | else | |
447 | hrtimer_init(&ctx->t.tmr, clockid, HRTIMER_MODE_ABS); | |
448 | ||
2456e855 | 449 | ctx->moffs = ktime_mono_to_real(0); |
4d672e7a | 450 | |
d9497990 JA |
451 | ufd = get_unused_fd_flags(flags & TFD_SHARED_FCNTL_FLAGS); |
452 | if (ufd < 0) { | |
453 | kfree(ctx); | |
454 | return ufd; | |
455 | } | |
456 | ||
457 | file = anon_inode_getfile("[timerfd]", &timerfd_fops, ctx, | |
458 | O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS)); | |
459 | if (IS_ERR(file)) { | |
460 | put_unused_fd(ufd); | |
4d672e7a | 461 | kfree(ctx); |
d9497990 JA |
462 | return PTR_ERR(file); |
463 | } | |
4d672e7a | 464 | |
d9497990 JA |
465 | file->f_mode |= FMODE_NOWAIT; |
466 | fd_install(ufd, file); | |
4d672e7a DL |
467 | return ufd; |
468 | } | |
469 | ||
9d94b9e2 | 470 | static int do_timerfd_settime(int ufd, int flags, |
bff41203 DD |
471 | const struct itimerspec64 *new, |
472 | struct itimerspec64 *old) | |
4d672e7a | 473 | { |
2903ff01 | 474 | struct fd f; |
4d672e7a | 475 | struct timerfd_ctx *ctx; |
2903ff01 | 476 | int ret; |
4d672e7a | 477 | |
610d18f4 | 478 | if ((flags & ~TFD_SETTIME_FLAGS) || |
bff41203 | 479 | !itimerspec64_valid(new)) |
b215e283 DL |
480 | return -EINVAL; |
481 | ||
2903ff01 AV |
482 | ret = timerfd_fget(ufd, &f); |
483 | if (ret) | |
484 | return ret; | |
485 | ctx = f.file->private_data; | |
b215e283 | 486 | |
25b68a8f | 487 | if (isalarm(ctx) && !capable(CAP_WAKE_ALARM)) { |
2895a5e5 EC |
488 | fdput(f); |
489 | return -EPERM; | |
490 | } | |
491 | ||
9ec26907 TG |
492 | timerfd_setup_cancel(ctx, flags); |
493 | ||
4d672e7a DL |
494 | /* |
495 | * We need to stop the existing timer before reprogramming | |
496 | * it to the new values. | |
497 | */ | |
498 | for (;;) { | |
499 | spin_lock_irq(&ctx->wqh.lock); | |
11ffa9d6 TP |
500 | |
501 | if (isalarm(ctx)) { | |
502 | if (alarm_try_to_cancel(&ctx->t.alarm) >= 0) | |
503 | break; | |
504 | } else { | |
505 | if (hrtimer_try_to_cancel(&ctx->t.tmr) >= 0) | |
506 | break; | |
507 | } | |
18963c01 | 508 | spin_unlock_irq(&ctx->wqh.lock); |
a125ecc1 AMG |
509 | |
510 | if (isalarm(ctx)) | |
511 | hrtimer_cancel_wait_running(&ctx->t.alarm.timer); | |
512 | else | |
513 | hrtimer_cancel_wait_running(&ctx->t.tmr); | |
b215e283 DL |
514 | } |
515 | ||
4d672e7a DL |
516 | /* |
517 | * If the timer is expired and it's periodic, we need to advance it | |
518 | * because the caller may want to know the previous expiration time. | |
519 | * We do not update "ticks" and "expired" since the timer will be | |
520 | * re-programmed again in the following timerfd_setup() call. | |
521 | */ | |
2456e855 | 522 | if (ctx->expired && ctx->tintv) { |
11ffa9d6 TP |
523 | if (isalarm(ctx)) |
524 | alarm_forward_now(&ctx->t.alarm, ctx->tintv); | |
525 | else | |
526 | hrtimer_forward_now(&ctx->t.tmr, ctx->tintv); | |
527 | } | |
b215e283 | 528 | |
bff41203 DD |
529 | old->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx)); |
530 | old->it_interval = ktime_to_timespec64(ctx->tintv); | |
4d672e7a DL |
531 | |
532 | /* | |
533 | * Re-program the timer to the new value ... | |
534 | */ | |
9d94b9e2 | 535 | ret = timerfd_setup(ctx, flags, new); |
4d672e7a DL |
536 | |
537 | spin_unlock_irq(&ctx->wqh.lock); | |
2903ff01 | 538 | fdput(f); |
99ee5315 | 539 | return ret; |
4d672e7a DL |
540 | } |
541 | ||
bff41203 | 542 | static int do_timerfd_gettime(int ufd, struct itimerspec64 *t) |
4d672e7a | 543 | { |
2903ff01 | 544 | struct fd f; |
4d672e7a | 545 | struct timerfd_ctx *ctx; |
2903ff01 AV |
546 | int ret = timerfd_fget(ufd, &f); |
547 | if (ret) | |
548 | return ret; | |
549 | ctx = f.file->private_data; | |
4d672e7a DL |
550 | |
551 | spin_lock_irq(&ctx->wqh.lock); | |
2456e855 | 552 | if (ctx->expired && ctx->tintv) { |
4d672e7a | 553 | ctx->expired = 0; |
11ffa9d6 TP |
554 | |
555 | if (isalarm(ctx)) { | |
556 | ctx->ticks += | |
557 | alarm_forward_now( | |
558 | &ctx->t.alarm, ctx->tintv) - 1; | |
559 | alarm_restart(&ctx->t.alarm); | |
560 | } else { | |
561 | ctx->ticks += | |
562 | hrtimer_forward_now(&ctx->t.tmr, ctx->tintv) | |
563 | - 1; | |
564 | hrtimer_restart(&ctx->t.tmr); | |
565 | } | |
4d672e7a | 566 | } |
bff41203 DD |
567 | t->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx)); |
568 | t->it_interval = ktime_to_timespec64(ctx->tintv); | |
4d672e7a | 569 | spin_unlock_irq(&ctx->wqh.lock); |
2903ff01 | 570 | fdput(f); |
9d94b9e2 AV |
571 | return 0; |
572 | } | |
573 | ||
574 | SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags, | |
6ff84735 DD |
575 | const struct __kernel_itimerspec __user *, utmr, |
576 | struct __kernel_itimerspec __user *, otmr) | |
9d94b9e2 | 577 | { |
bff41203 | 578 | struct itimerspec64 new, old; |
9d94b9e2 AV |
579 | int ret; |
580 | ||
bff41203 | 581 | if (get_itimerspec64(&new, utmr)) |
9d94b9e2 AV |
582 | return -EFAULT; |
583 | ret = do_timerfd_settime(ufd, flags, &new, &old); | |
584 | if (ret) | |
585 | return ret; | |
bff41203 | 586 | if (otmr && put_itimerspec64(&old, otmr)) |
9d94b9e2 AV |
587 | return -EFAULT; |
588 | ||
589 | return ret; | |
590 | } | |
4d672e7a | 591 | |
6ff84735 | 592 | SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct __kernel_itimerspec __user *, otmr) |
9d94b9e2 | 593 | { |
bff41203 | 594 | struct itimerspec64 kotmr; |
9d94b9e2 AV |
595 | int ret = do_timerfd_gettime(ufd, &kotmr); |
596 | if (ret) | |
597 | return ret; | |
bff41203 | 598 | return put_itimerspec64(&kotmr, otmr) ? -EFAULT : 0; |
b215e283 DL |
599 | } |
600 | ||
6ff84735 | 601 | #ifdef CONFIG_COMPAT_32BIT_TIME |
8dabe724 | 602 | SYSCALL_DEFINE4(timerfd_settime32, int, ufd, int, flags, |
9afc5eee AB |
603 | const struct old_itimerspec32 __user *, utmr, |
604 | struct old_itimerspec32 __user *, otmr) | |
9d94b9e2 | 605 | { |
bff41203 | 606 | struct itimerspec64 new, old; |
9d94b9e2 AV |
607 | int ret; |
608 | ||
9afc5eee | 609 | if (get_old_itimerspec32(&new, utmr)) |
9d94b9e2 AV |
610 | return -EFAULT; |
611 | ret = do_timerfd_settime(ufd, flags, &new, &old); | |
612 | if (ret) | |
613 | return ret; | |
9afc5eee | 614 | if (otmr && put_old_itimerspec32(&old, otmr)) |
9d94b9e2 AV |
615 | return -EFAULT; |
616 | return ret; | |
617 | } | |
618 | ||
8dabe724 | 619 | SYSCALL_DEFINE2(timerfd_gettime32, int, ufd, |
9afc5eee | 620 | struct old_itimerspec32 __user *, otmr) |
9d94b9e2 | 621 | { |
bff41203 | 622 | struct itimerspec64 kotmr; |
9d94b9e2 AV |
623 | int ret = do_timerfd_gettime(ufd, &kotmr); |
624 | if (ret) | |
625 | return ret; | |
9afc5eee | 626 | return put_old_itimerspec32(&kotmr, otmr) ? -EFAULT : 0; |
9d94b9e2 AV |
627 | } |
628 | #endif |