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