Merge tag 'acpi-4.18-rc1-2' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael...
[linux-2.6/btrfs-unstable.git] / fs / timerfd.c
blobd84a2bee4f82b2f8470b7f2fbd42b2f33beb2bce
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * fs/timerfd.c
5 * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org>
8 * Thanks to Thomas Gleixner for code reviews and useful comments.
12 #include <linux/alarmtimer.h>
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>
19 #include <linux/slab.h>
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>
26 #include <linux/syscalls.h>
27 #include <linux/compat.h>
28 #include <linux/rcupdate.h>
30 struct timerfd_ctx {
31 union {
32 struct hrtimer tmr;
33 struct alarm alarm;
34 } t;
35 ktime_t tintv;
36 ktime_t moffs;
37 wait_queue_head_t wqh;
38 u64 ticks;
39 int clockid;
40 short unsigned expired;
41 short unsigned settime_flags; /* to show in fdinfo */
42 struct rcu_head rcu;
43 struct list_head clist;
44 spinlock_t cancel_lock;
45 bool might_cancel;
48 static LIST_HEAD(cancel_list);
49 static DEFINE_SPINLOCK(cancel_lock);
51 static inline bool isalarm(struct timerfd_ctx *ctx)
53 return ctx->clockid == CLOCK_REALTIME_ALARM ||
54 ctx->clockid == CLOCK_BOOTTIME_ALARM;
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,
60 * tintv != 0) until the timer is accessed.
62 static void timerfd_triggered(struct timerfd_ctx *ctx)
64 unsigned long flags;
66 spin_lock_irqsave(&ctx->wqh.lock, flags);
67 ctx->expired = 1;
68 ctx->ticks++;
69 wake_up_locked(&ctx->wqh);
70 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
73 static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr)
75 struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx,
76 t.tmr);
77 timerfd_triggered(ctx);
78 return HRTIMER_NORESTART;
81 static enum alarmtimer_restart timerfd_alarmproc(struct alarm *alarm,
82 ktime_t now)
84 struct timerfd_ctx *ctx = container_of(alarm, struct timerfd_ctx,
85 t.alarm);
86 timerfd_triggered(ctx);
87 return ALARMTIMER_NORESTART;
91 * Called when the clock was set to cancel the timers in the cancel
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().
96 void timerfd_clock_was_set(void)
98 ktime_t moffs = ktime_mono_to_real(0);
99 struct timerfd_ctx *ctx;
100 unsigned long flags;
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);
107 if (ctx->moffs != moffs) {
108 ctx->moffs = KTIME_MAX;
109 ctx->ticks++;
110 wake_up_locked(&ctx->wqh);
112 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
114 rcu_read_unlock();
117 static void __timerfd_remove_cancel(struct timerfd_ctx *ctx)
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);
127 static void timerfd_remove_cancel(struct timerfd_ctx *ctx)
129 spin_lock(&ctx->cancel_lock);
130 __timerfd_remove_cancel(ctx);
131 spin_unlock(&ctx->cancel_lock);
134 static bool timerfd_canceled(struct timerfd_ctx *ctx)
136 if (!ctx->might_cancel || ctx->moffs != KTIME_MAX)
137 return false;
138 ctx->moffs = ktime_mono_to_real(0);
139 return true;
142 static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags)
144 spin_lock(&ctx->cancel_lock);
145 if ((ctx->clockid == CLOCK_REALTIME ||
146 ctx->clockid == CLOCK_REALTIME_ALARM) &&
147 (flags & TFD_TIMER_ABSTIME) && (flags & TFD_TIMER_CANCEL_ON_SET)) {
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);
154 } else {
155 __timerfd_remove_cancel(ctx);
157 spin_unlock(&ctx->cancel_lock);
160 static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
162 ktime_t remaining;
164 if (isalarm(ctx))
165 remaining = alarm_expires_remaining(&ctx->t.alarm);
166 else
167 remaining = hrtimer_expires_remaining_adjusted(&ctx->t.tmr);
169 return remaining < 0 ? 0: remaining;
172 static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
173 const struct itimerspec64 *ktmr)
175 enum hrtimer_mode htmode;
176 ktime_t texp;
177 int clockid = ctx->clockid;
179 htmode = (flags & TFD_TIMER_ABSTIME) ?
180 HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
182 texp = timespec64_to_ktime(ktmr->it_value);
183 ctx->expired = 0;
184 ctx->ticks = 0;
185 ctx->tintv = timespec64_to_ktime(ktmr->it_interval);
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;
198 if (texp != 0) {
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);
208 if (timerfd_canceled(ctx))
209 return -ECANCELED;
212 ctx->settime_flags = flags & TFD_SETTIME_FLAGS;
213 return 0;
216 static int timerfd_release(struct inode *inode, struct file *file)
218 struct timerfd_ctx *ctx = file->private_data;
220 timerfd_remove_cancel(ctx);
222 if (isalarm(ctx))
223 alarm_cancel(&ctx->t.alarm);
224 else
225 hrtimer_cancel(&ctx->t.tmr);
226 kfree_rcu(ctx, rcu);
227 return 0;
230 static struct wait_queue_head *timerfd_get_poll_head(struct file *file,
231 __poll_t eventmask)
233 struct timerfd_ctx *ctx = file->private_data;
235 return &ctx->wqh;
238 static __poll_t timerfd_poll_mask(struct file *file, __poll_t eventmask)
240 struct timerfd_ctx *ctx = file->private_data;
242 return ctx->ticks ? EPOLLIN : 0;
245 static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
246 loff_t *ppos)
248 struct timerfd_ctx *ctx = file->private_data;
249 ssize_t res;
250 u64 ticks = 0;
252 if (count < sizeof(ticks))
253 return -EINVAL;
254 spin_lock_irq(&ctx->wqh.lock);
255 if (file->f_flags & O_NONBLOCK)
256 res = -EAGAIN;
257 else
258 res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
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.
265 if (timerfd_canceled(ctx)) {
266 ctx->ticks = 0;
267 ctx->expired = 0;
268 res = -ECANCELED;
271 if (ctx->ticks) {
272 ticks = ctx->ticks;
274 if (ctx->expired && ctx->tintv) {
276 * If tintv != 0, this is a periodic timer that
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.
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);
291 ctx->expired = 0;
292 ctx->ticks = 0;
294 spin_unlock_irq(&ctx->wqh.lock);
295 if (ticks)
296 res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
297 return res;
300 #ifdef CONFIG_PROC_FS
301 static void timerfd_show(struct seq_file *m, struct file *file)
303 struct timerfd_ctx *ctx = file->private_data;
304 struct itimerspec t;
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);
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);
325 #else
326 #define timerfd_show NULL
327 #endif
329 #ifdef CONFIG_CHECKPOINT_RESTORE
330 static long timerfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
332 struct timerfd_ctx *ctx = file->private_data;
333 int ret = 0;
335 switch (cmd) {
336 case TFD_IOC_SET_TICKS: {
337 u64 ticks;
339 if (copy_from_user(&ticks, (u64 __user *)arg, sizeof(ticks)))
340 return -EFAULT;
341 if (!ticks)
342 return -EINVAL;
344 spin_lock_irq(&ctx->wqh.lock);
345 if (!timerfd_canceled(ctx)) {
346 ctx->ticks = ticks;
347 wake_up_locked(&ctx->wqh);
348 } else
349 ret = -ECANCELED;
350 spin_unlock_irq(&ctx->wqh.lock);
351 break;
353 default:
354 ret = -ENOTTY;
355 break;
358 return ret;
360 #else
361 #define timerfd_ioctl NULL
362 #endif
364 static const struct file_operations timerfd_fops = {
365 .release = timerfd_release,
366 .get_poll_head = timerfd_get_poll_head,
367 .poll_mask = timerfd_poll_mask,
368 .read = timerfd_read,
369 .llseek = noop_llseek,
370 .show_fdinfo = timerfd_show,
371 .unlocked_ioctl = timerfd_ioctl,
374 static int timerfd_fget(int fd, struct fd *p)
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;
383 *p = f;
384 return 0;
387 SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
389 int ufd;
390 struct timerfd_ctx *ctx;
392 /* Check the TFD_* constants for consistency. */
393 BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
394 BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
396 if ((flags & ~TFD_CREATE_FLAGS) ||
397 (clockid != CLOCK_MONOTONIC &&
398 clockid != CLOCK_REALTIME &&
399 clockid != CLOCK_REALTIME_ALARM &&
400 clockid != CLOCK_BOOTTIME &&
401 clockid != CLOCK_BOOTTIME_ALARM))
402 return -EINVAL;
404 if ((clockid == CLOCK_REALTIME_ALARM ||
405 clockid == CLOCK_BOOTTIME_ALARM) &&
406 !capable(CAP_WAKE_ALARM))
407 return -EPERM;
409 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
410 if (!ctx)
411 return -ENOMEM;
413 init_waitqueue_head(&ctx->wqh);
414 spin_lock_init(&ctx->cancel_lock);
415 ctx->clockid = clockid;
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);
425 ctx->moffs = ktime_mono_to_real(0);
427 ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx,
428 O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
429 if (ufd < 0)
430 kfree(ctx);
432 return ufd;
435 static int do_timerfd_settime(int ufd, int flags,
436 const struct itimerspec64 *new,
437 struct itimerspec64 *old)
439 struct fd f;
440 struct timerfd_ctx *ctx;
441 int ret;
443 if ((flags & ~TFD_SETTIME_FLAGS) ||
444 !itimerspec64_valid(new))
445 return -EINVAL;
447 ret = timerfd_fget(ufd, &f);
448 if (ret)
449 return ret;
450 ctx = f.file->private_data;
452 if (isalarm(ctx) && !capable(CAP_WAKE_ALARM)) {
453 fdput(f);
454 return -EPERM;
457 timerfd_setup_cancel(ctx, flags);
460 * We need to stop the existing timer before reprogramming
461 * it to the new values.
463 for (;;) {
464 spin_lock_irq(&ctx->wqh.lock);
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;
473 spin_unlock_irq(&ctx->wqh.lock);
474 cpu_relax();
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.
483 if (ctx->expired && ctx->tintv) {
484 if (isalarm(ctx))
485 alarm_forward_now(&ctx->t.alarm, ctx->tintv);
486 else
487 hrtimer_forward_now(&ctx->t.tmr, ctx->tintv);
490 old->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx));
491 old->it_interval = ktime_to_timespec64(ctx->tintv);
494 * Re-program the timer to the new value ...
496 ret = timerfd_setup(ctx, flags, new);
498 spin_unlock_irq(&ctx->wqh.lock);
499 fdput(f);
500 return ret;
503 static int do_timerfd_gettime(int ufd, struct itimerspec64 *t)
505 struct fd f;
506 struct timerfd_ctx *ctx;
507 int ret = timerfd_fget(ufd, &f);
508 if (ret)
509 return ret;
510 ctx = f.file->private_data;
512 spin_lock_irq(&ctx->wqh.lock);
513 if (ctx->expired && ctx->tintv) {
514 ctx->expired = 0;
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);
528 t->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx));
529 t->it_interval = ktime_to_timespec64(ctx->tintv);
530 spin_unlock_irq(&ctx->wqh.lock);
531 fdput(f);
532 return 0;
535 SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
536 const struct itimerspec __user *, utmr,
537 struct itimerspec __user *, otmr)
539 struct itimerspec64 new, old;
540 int ret;
542 if (get_itimerspec64(&new, utmr))
543 return -EFAULT;
544 ret = do_timerfd_settime(ufd, flags, &new, &old);
545 if (ret)
546 return ret;
547 if (otmr && put_itimerspec64(&old, otmr))
548 return -EFAULT;
550 return ret;
553 SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct itimerspec __user *, otmr)
555 struct itimerspec64 kotmr;
556 int ret = do_timerfd_gettime(ufd, &kotmr);
557 if (ret)
558 return ret;
559 return put_itimerspec64(&kotmr, otmr) ? -EFAULT : 0;
562 #ifdef CONFIG_COMPAT
563 COMPAT_SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
564 const struct compat_itimerspec __user *, utmr,
565 struct compat_itimerspec __user *, otmr)
567 struct itimerspec64 new, old;
568 int ret;
570 if (get_compat_itimerspec64(&new, utmr))
571 return -EFAULT;
572 ret = do_timerfd_settime(ufd, flags, &new, &old);
573 if (ret)
574 return ret;
575 if (otmr && put_compat_itimerspec64(&old, otmr))
576 return -EFAULT;
577 return ret;
580 COMPAT_SYSCALL_DEFINE2(timerfd_gettime, int, ufd,
581 struct compat_itimerspec __user *, otmr)
583 struct itimerspec64 kotmr;
584 int ret = do_timerfd_gettime(ufd, &kotmr);
585 if (ret)
586 return ret;
587 return put_compat_itimerspec64(&kotmr, otmr) ? -EFAULT : 0;
589 #endif