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