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