blob: 06c0623cc87e972aa93bc1884b978740211671ec [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Davide Libenzib215e282007-05-10 22:23:16 -07002/*
3 * fs/timerfd.c
4 *
5 * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org>
6 *
7 *
8 * Thanks to Thomas Gleixner for code reviews and useful comments.
9 *
10 */
11
Todd Poynor11ffa9d2013-05-15 14:38:12 -070012#include <linux/alarmtimer.h>
Davide Libenzib215e282007-05-10 22:23:16 -070013#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Davide Libenzib215e282007-05-10 22:23:16 -070020#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>
Adrian Bunk45cc2b92008-04-29 00:58:59 -070026#include <linux/syscalls.h>
Al Viro9d94b9e2012-12-27 16:52:33 -050027#include <linux/compat.h>
Thomas Gleixner9ec26902011-05-20 16:18:50 +020028#include <linux/rcupdate.h>
Andrei Vagin6cd889d2019-11-12 01:27:02 +000029#include <linux/time_namespace.h>
Davide Libenzib215e282007-05-10 22:23:16 -070030
Manish Varma0ff110f2021-03-15 21:40:24 -070031#include <trace/hooks/fs.h>
32
Davide Libenzib215e282007-05-10 22:23:16 -070033struct timerfd_ctx {
Todd Poynor11ffa9d2013-05-15 14:38:12 -070034 union {
35 struct hrtimer tmr;
36 struct alarm alarm;
37 } t;
Davide Libenzib215e282007-05-10 22:23:16 -070038 ktime_t tintv;
Thomas Gleixner99ee5312011-04-27 14:16:42 +020039 ktime_t moffs;
Davide Libenzib215e282007-05-10 22:23:16 -070040 wait_queue_head_t wqh;
Davide Libenzi4d672e72008-02-04 22:27:26 -080041 u64 ticks;
Davide Libenzi4d672e72008-02-04 22:27:26 -080042 int clockid;
Cyrill Gorcunovaf9c4952014-07-16 01:54:52 +040043 short unsigned expired;
44 short unsigned settime_flags; /* to show in fdinfo */
Thomas Gleixner9ec26902011-05-20 16:18:50 +020045 struct rcu_head rcu;
46 struct list_head clist;
Thomas Gleixner1e38da32017-01-31 15:24:03 +010047 spinlock_t cancel_lock;
Thomas Gleixner99ee5312011-04-27 14:16:42 +020048 bool might_cancel;
Davide Libenzib215e282007-05-10 22:23:16 -070049};
50
Thomas Gleixner9ec26902011-05-20 16:18:50 +020051static LIST_HEAD(cancel_list);
52static DEFINE_SPINLOCK(cancel_lock);
53
Todd Poynor11ffa9d2013-05-15 14:38:12 -070054static inline bool isalarm(struct timerfd_ctx *ctx)
55{
56 return ctx->clockid == CLOCK_REALTIME_ALARM ||
57 ctx->clockid == CLOCK_BOOTTIME_ALARM;
58}
59
Davide Libenzib215e282007-05-10 22:23:16 -070060/*
61 * This gets called when the timer event triggers. We set the "expired"
62 * flag, but we do not re-arm the timer (in case it's necessary,
Thomas Gleixner2456e852016-12-25 11:38:40 +010063 * tintv != 0) until the timer is accessed.
Davide Libenzib215e282007-05-10 22:23:16 -070064 */
Todd Poynor11ffa9d2013-05-15 14:38:12 -070065static void timerfd_triggered(struct timerfd_ctx *ctx)
Davide Libenzib215e282007-05-10 22:23:16 -070066{
Davide Libenzib215e282007-05-10 22:23:16 -070067 unsigned long flags;
68
Davide Libenzi18963c02007-05-18 12:02:33 -070069 spin_lock_irqsave(&ctx->wqh.lock, flags);
Davide Libenzib215e282007-05-10 22:23:16 -070070 ctx->expired = 1;
Davide Libenzi4d672e72008-02-04 22:27:26 -080071 ctx->ticks++;
Christoph Hellwig7dda7122018-07-16 12:25:50 +020072 wake_up_locked_poll(&ctx->wqh, EPOLLIN);
Davide Libenzi18963c02007-05-18 12:02:33 -070073 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
Todd Poynor11ffa9d2013-05-15 14:38:12 -070074}
Davide Libenzib215e282007-05-10 22:23:16 -070075
Todd Poynor11ffa9d2013-05-15 14:38:12 -070076static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr)
77{
78 struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx,
79 t.tmr);
80 timerfd_triggered(ctx);
Davide Libenzib215e282007-05-10 22:23:16 -070081 return HRTIMER_NORESTART;
82}
83
Todd Poynor11ffa9d2013-05-15 14:38:12 -070084static enum alarmtimer_restart timerfd_alarmproc(struct alarm *alarm,
85 ktime_t now)
86{
87 struct timerfd_ctx *ctx = container_of(alarm, struct timerfd_ctx,
88 t.alarm);
89 timerfd_triggered(ctx);
90 return ALARMTIMER_NORESTART;
91}
92
Thomas Gleixner9ec26902011-05-20 16:18:50 +020093/*
94 * Called when the clock was set to cancel the timers in the cancel
Max Asbock1123d9392011-06-13 10:18:32 -070095 * list. This will wake up processes waiting on these timers. The
96 * wake-up requires ctx->ticks to be non zero, therefore we increment
97 * it before calling wake_up_locked().
Thomas Gleixner9ec26902011-05-20 16:18:50 +020098 */
99void timerfd_clock_was_set(void)
100{
Thomas Gleixner2456e852016-12-25 11:38:40 +0100101 ktime_t moffs = ktime_mono_to_real(0);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200102 struct timerfd_ctx *ctx;
103 unsigned long flags;
104
105 rcu_read_lock();
106 list_for_each_entry_rcu(ctx, &cancel_list, clist) {
107 if (!ctx->might_cancel)
108 continue;
109 spin_lock_irqsave(&ctx->wqh.lock, flags);
Thomas Gleixner2456e852016-12-25 11:38:40 +0100110 if (ctx->moffs != moffs) {
111 ctx->moffs = KTIME_MAX;
Max Asbock1123d9392011-06-13 10:18:32 -0700112 ctx->ticks++;
Christoph Hellwig7dda7122018-07-16 12:25:50 +0200113 wake_up_locked_poll(&ctx->wqh, EPOLLIN);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200114 }
115 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
116 }
117 rcu_read_unlock();
118}
119
Thomas Gleixner1e38da32017-01-31 15:24:03 +0100120static void __timerfd_remove_cancel(struct timerfd_ctx *ctx)
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200121{
122 if (ctx->might_cancel) {
123 ctx->might_cancel = false;
124 spin_lock(&cancel_lock);
125 list_del_rcu(&ctx->clist);
126 spin_unlock(&cancel_lock);
127 }
128}
129
Thomas Gleixner1e38da32017-01-31 15:24:03 +0100130static void timerfd_remove_cancel(struct timerfd_ctx *ctx)
131{
132 spin_lock(&ctx->cancel_lock);
133 __timerfd_remove_cancel(ctx);
134 spin_unlock(&ctx->cancel_lock);
135}
136
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200137static bool timerfd_canceled(struct timerfd_ctx *ctx)
138{
Thomas Gleixner2456e852016-12-25 11:38:40 +0100139 if (!ctx->might_cancel || ctx->moffs != KTIME_MAX)
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200140 return false;
Thomas Gleixner2456e852016-12-25 11:38:40 +0100141 ctx->moffs = ktime_mono_to_real(0);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200142 return true;
143}
144
145static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags)
146{
Thomas Gleixner1e38da32017-01-31 15:24:03 +0100147 spin_lock(&ctx->cancel_lock);
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700148 if ((ctx->clockid == CLOCK_REALTIME ||
149 ctx->clockid == CLOCK_REALTIME_ALARM) &&
150 (flags & TFD_TIMER_ABSTIME) && (flags & TFD_TIMER_CANCEL_ON_SET)) {
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200151 if (!ctx->might_cancel) {
152 ctx->might_cancel = true;
153 spin_lock(&cancel_lock);
154 list_add_rcu(&ctx->clist, &cancel_list);
155 spin_unlock(&cancel_lock);
156 }
Thomas Gleixner1e38da32017-01-31 15:24:03 +0100157 } else {
158 __timerfd_remove_cancel(ctx);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200159 }
Thomas Gleixner1e38da32017-01-31 15:24:03 +0100160 spin_unlock(&ctx->cancel_lock);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200161}
162
Davide Libenzi4d672e72008-02-04 22:27:26 -0800163static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
164{
Arjan van de Ven76369472008-09-01 15:00:14 -0700165 ktime_t remaining;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800166
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700167 if (isalarm(ctx))
168 remaining = alarm_expires_remaining(&ctx->t.alarm);
169 else
Thomas Gleixnerb62526e2016-01-14 16:54:46 +0000170 remaining = hrtimer_expires_remaining_adjusted(&ctx->t.tmr);
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700171
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100172 return remaining < 0 ? 0: remaining;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800173}
174
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200175static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
Deepa Dinamanibff41202017-06-24 11:45:07 -0700176 const struct itimerspec64 *ktmr)
Davide Libenzib215e282007-05-10 22:23:16 -0700177{
178 enum hrtimer_mode htmode;
179 ktime_t texp;
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200180 int clockid = ctx->clockid;
Davide Libenzib215e282007-05-10 22:23:16 -0700181
182 htmode = (flags & TFD_TIMER_ABSTIME) ?
183 HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
184
Deepa Dinamanibff41202017-06-24 11:45:07 -0700185 texp = timespec64_to_ktime(ktmr->it_value);
Davide Libenzib215e282007-05-10 22:23:16 -0700186 ctx->expired = 0;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800187 ctx->ticks = 0;
Deepa Dinamanibff41202017-06-24 11:45:07 -0700188 ctx->tintv = timespec64_to_ktime(ktmr->it_interval);
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700189
190 if (isalarm(ctx)) {
191 alarm_init(&ctx->t.alarm,
192 ctx->clockid == CLOCK_REALTIME_ALARM ?
193 ALARM_REALTIME : ALARM_BOOTTIME,
194 timerfd_alarmproc);
195 } else {
196 hrtimer_init(&ctx->t.tmr, clockid, htmode);
197 hrtimer_set_expires(&ctx->t.tmr, texp);
198 ctx->t.tmr.function = timerfd_tmrproc;
199 }
200
Thomas Gleixner2456e852016-12-25 11:38:40 +0100201 if (texp != 0) {
Andrei Vagin6cd889d2019-11-12 01:27:02 +0000202 if (flags & TFD_TIMER_ABSTIME)
203 texp = timens_ktime_to_host(clockid, texp);
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700204 if (isalarm(ctx)) {
205 if (flags & TFD_TIMER_ABSTIME)
206 alarm_start(&ctx->t.alarm, texp);
207 else
208 alarm_start_relative(&ctx->t.alarm, texp);
209 } else {
210 hrtimer_start(&ctx->t.tmr, texp, htmode);
211 }
212
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200213 if (timerfd_canceled(ctx))
214 return -ECANCELED;
215 }
Cyrill Gorcunovaf9c4952014-07-16 01:54:52 +0400216
217 ctx->settime_flags = flags & TFD_SETTIME_FLAGS;
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200218 return 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700219}
220
221static int timerfd_release(struct inode *inode, struct file *file)
222{
223 struct timerfd_ctx *ctx = file->private_data;
224
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200225 timerfd_remove_cancel(ctx);
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700226
227 if (isalarm(ctx))
228 alarm_cancel(&ctx->t.alarm);
229 else
230 hrtimer_cancel(&ctx->t.tmr);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200231 kfree_rcu(ctx, rcu);
Davide Libenzib215e282007-05-10 22:23:16 -0700232 return 0;
233}
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700234
235static __poll_t timerfd_poll(struct file *file, poll_table *wait)
Davide Libenzib215e282007-05-10 22:23:16 -0700236{
237 struct timerfd_ctx *ctx = file->private_data;
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700238 __poll_t events = 0;
239 unsigned long flags;
Davide Libenzib215e282007-05-10 22:23:16 -0700240
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700241 poll_wait(file, &ctx->wqh, wait);
Davide Libenzib215e282007-05-10 22:23:16 -0700242
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700243 spin_lock_irqsave(&ctx->wqh.lock, flags);
244 if (ctx->ticks)
245 events |= EPOLLIN;
246 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
Davide Libenzib215e282007-05-10 22:23:16 -0700247
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700248 return events;
Davide Libenzib215e282007-05-10 22:23:16 -0700249}
250
251static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
252 loff_t *ppos)
253{
254 struct timerfd_ctx *ctx = file->private_data;
255 ssize_t res;
Davide Libenzi09828402007-07-26 10:41:07 -0700256 u64 ticks = 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700257
258 if (count < sizeof(ticks))
259 return -EINVAL;
Davide Libenzi18963c02007-05-18 12:02:33 -0700260 spin_lock_irq(&ctx->wqh.lock);
Michal Nazarewicz8120a8a2010-05-05 12:53:12 +0200261 if (file->f_flags & O_NONBLOCK)
262 res = -EAGAIN;
263 else
264 res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200265
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200266 /*
267 * If clock has changed, we do not care about the
268 * ticks and we do not rearm the timer. Userspace must
269 * reevaluate anyway.
270 */
271 if (timerfd_canceled(ctx)) {
272 ctx->ticks = 0;
273 ctx->expired = 0;
274 res = -ECANCELED;
275 }
276
Davide Libenzi4d672e72008-02-04 22:27:26 -0800277 if (ctx->ticks) {
278 ticks = ctx->ticks;
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200279
Thomas Gleixner2456e852016-12-25 11:38:40 +0100280 if (ctx->expired && ctx->tintv) {
Davide Libenzib215e282007-05-10 22:23:16 -0700281 /*
Thomas Gleixner2456e852016-12-25 11:38:40 +0100282 * If tintv != 0, this is a periodic timer that
Davide Libenzib215e282007-05-10 22:23:16 -0700283 * needs to be re-armed. We avoid doing it in the timer
284 * callback to avoid DoS attacks specifying a very
285 * short timer period.
286 */
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700287 if (isalarm(ctx)) {
288 ticks += alarm_forward_now(
289 &ctx->t.alarm, ctx->tintv) - 1;
290 alarm_restart(&ctx->t.alarm);
291 } else {
292 ticks += hrtimer_forward_now(&ctx->t.tmr,
293 ctx->tintv) - 1;
294 hrtimer_restart(&ctx->t.tmr);
295 }
Davide Libenzi4d672e72008-02-04 22:27:26 -0800296 }
297 ctx->expired = 0;
298 ctx->ticks = 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700299 }
Davide Libenzi18963c02007-05-18 12:02:33 -0700300 spin_unlock_irq(&ctx->wqh.lock);
Davide Libenzib215e282007-05-10 22:23:16 -0700301 if (ticks)
Davide Libenzi09828402007-07-26 10:41:07 -0700302 res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
Davide Libenzib215e282007-05-10 22:23:16 -0700303 return res;
304}
305
Cyrill Gorcunovaf9c4952014-07-16 01:54:52 +0400306#ifdef CONFIG_PROC_FS
Joe Perchesa3816ab2014-09-29 16:08:25 -0700307static void timerfd_show(struct seq_file *m, struct file *file)
Cyrill Gorcunovaf9c4952014-07-16 01:54:52 +0400308{
309 struct timerfd_ctx *ctx = file->private_data;
Arnd Bergmannbde9e962018-04-20 22:03:38 +0200310 struct timespec64 value, interval;
Cyrill Gorcunovaf9c4952014-07-16 01:54:52 +0400311
312 spin_lock_irq(&ctx->wqh.lock);
Arnd Bergmannbde9e962018-04-20 22:03:38 +0200313 value = ktime_to_timespec64(timerfd_get_remaining(ctx));
314 interval = ktime_to_timespec64(ctx->tintv);
Cyrill Gorcunovaf9c4952014-07-16 01:54:52 +0400315 spin_unlock_irq(&ctx->wqh.lock);
316
Joe Perchesa3816ab2014-09-29 16:08:25 -0700317 seq_printf(m,
318 "clockid: %d\n"
319 "ticks: %llu\n"
320 "settime flags: 0%o\n"
321 "it_value: (%llu, %llu)\n"
322 "it_interval: (%llu, %llu)\n",
323 ctx->clockid,
324 (unsigned long long)ctx->ticks,
325 ctx->settime_flags,
Arnd Bergmannbde9e962018-04-20 22:03:38 +0200326 (unsigned long long)value.tv_sec,
327 (unsigned long long)value.tv_nsec,
328 (unsigned long long)interval.tv_sec,
329 (unsigned long long)interval.tv_nsec);
Cyrill Gorcunovaf9c4952014-07-16 01:54:52 +0400330}
331#else
332#define timerfd_show NULL
333#endif
334
Cyrill Gorcunov5442e9f2014-07-16 01:54:54 +0400335#ifdef CONFIG_CHECKPOINT_RESTORE
336static long timerfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
337{
338 struct timerfd_ctx *ctx = file->private_data;
339 int ret = 0;
340
341 switch (cmd) {
342 case TFD_IOC_SET_TICKS: {
343 u64 ticks;
344
345 if (copy_from_user(&ticks, (u64 __user *)arg, sizeof(ticks)))
346 return -EFAULT;
347 if (!ticks)
348 return -EINVAL;
349
350 spin_lock_irq(&ctx->wqh.lock);
351 if (!timerfd_canceled(ctx)) {
352 ctx->ticks = ticks;
Christoph Hellwig7dda7122018-07-16 12:25:50 +0200353 wake_up_locked_poll(&ctx->wqh, EPOLLIN);
Cyrill Gorcunov5442e9f2014-07-16 01:54:54 +0400354 } else
355 ret = -ECANCELED;
356 spin_unlock_irq(&ctx->wqh.lock);
357 break;
358 }
359 default:
360 ret = -ENOTTY;
361 break;
362 }
363
364 return ret;
365}
366#else
367#define timerfd_ioctl NULL
368#endif
369
Davide Libenzib215e282007-05-10 22:23:16 -0700370static const struct file_operations timerfd_fops = {
371 .release = timerfd_release,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700372 .poll = timerfd_poll,
Davide Libenzib215e282007-05-10 22:23:16 -0700373 .read = timerfd_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200374 .llseek = noop_llseek,
Cyrill Gorcunovaf9c4952014-07-16 01:54:52 +0400375 .show_fdinfo = timerfd_show,
Cyrill Gorcunov5442e9f2014-07-16 01:54:54 +0400376 .unlocked_ioctl = timerfd_ioctl,
Davide Libenzib215e282007-05-10 22:23:16 -0700377};
378
Al Viro2903ff02012-08-28 12:52:22 -0400379static int timerfd_fget(int fd, struct fd *p)
Davide Libenzib215e282007-05-10 22:23:16 -0700380{
Al Viro2903ff02012-08-28 12:52:22 -0400381 struct fd f = fdget(fd);
382 if (!f.file)
383 return -EBADF;
384 if (f.file->f_op != &timerfd_fops) {
385 fdput(f);
386 return -EINVAL;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800387 }
Al Viro2903ff02012-08-28 12:52:22 -0400388 *p = f;
389 return 0;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800390}
391
Heiko Carstens836f92a2009-01-14 14:14:33 +0100392SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800393{
Al Viro2030a422008-02-23 06:46:49 -0500394 int ufd;
Davide Libenzib215e282007-05-10 22:23:16 -0700395 struct timerfd_ctx *ctx;
Manish Varma0ff110f2021-03-15 21:40:24 -0700396 char file_name_buf[32];
Davide Libenzi4d672e72008-02-04 22:27:26 -0800397
Ulrich Dreppere38b36f2008-07-23 21:29:42 -0700398 /* Check the TFD_* constants for consistency. */
399 BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
400 BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
401
Davide Libenzi610d18f2009-02-18 14:48:18 -0800402 if ((flags & ~TFD_CREATE_FLAGS) ||
403 (clockid != CLOCK_MONOTONIC &&
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700404 clockid != CLOCK_REALTIME &&
405 clockid != CLOCK_REALTIME_ALARM &&
Greg Hackmann4a2378a2014-01-08 10:57:03 -0800406 clockid != CLOCK_BOOTTIME &&
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700407 clockid != CLOCK_BOOTTIME_ALARM))
Davide Libenzi4d672e72008-02-04 22:27:26 -0800408 return -EINVAL;
409
Stephen Smalley25b68a82017-02-17 10:13:59 -0500410 if ((clockid == CLOCK_REALTIME_ALARM ||
411 clockid == CLOCK_BOOTTIME_ALARM) &&
412 !capable(CAP_WAKE_ALARM))
Eric Caruso2895a5e2016-06-08 16:08:59 -0700413 return -EPERM;
414
Davide Libenzi4d672e72008-02-04 22:27:26 -0800415 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
416 if (!ctx)
417 return -ENOMEM;
418
419 init_waitqueue_head(&ctx->wqh);
Thomas Gleixner1e38da32017-01-31 15:24:03 +0100420 spin_lock_init(&ctx->cancel_lock);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800421 ctx->clockid = clockid;
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700422
423 if (isalarm(ctx))
424 alarm_init(&ctx->t.alarm,
425 ctx->clockid == CLOCK_REALTIME_ALARM ?
426 ALARM_REALTIME : ALARM_BOOTTIME,
427 timerfd_alarmproc);
428 else
429 hrtimer_init(&ctx->t.tmr, clockid, HRTIMER_MODE_ABS);
430
Thomas Gleixner2456e852016-12-25 11:38:40 +0100431 ctx->moffs = ktime_mono_to_real(0);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800432
Manish Varma0ff110f2021-03-15 21:40:24 -0700433 strlcpy(file_name_buf, "[timerfd]", sizeof(file_name_buf));
434 trace_android_vh_timerfd_create(file_name_buf, sizeof(file_name_buf));
435 ufd = anon_inode_getfd(file_name_buf, &timerfd_fops, ctx,
Roland Dreier628ff7c2009-12-18 09:41:24 -0800436 O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
Al Viro2030a422008-02-23 06:46:49 -0500437 if (ufd < 0)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800438 kfree(ctx);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800439
440 return ufd;
441}
442
Manish Varma0ff110f2021-03-15 21:40:24 -0700443static int do_timerfd_settime(int ufd, int flags,
Deepa Dinamanibff41202017-06-24 11:45:07 -0700444 const struct itimerspec64 *new,
445 struct itimerspec64 *old)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800446{
Al Viro2903ff02012-08-28 12:52:22 -0400447 struct fd f;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800448 struct timerfd_ctx *ctx;
Al Viro2903ff02012-08-28 12:52:22 -0400449 int ret;
Davide Libenzib215e282007-05-10 22:23:16 -0700450
Davide Libenzi610d18f2009-02-18 14:48:18 -0800451 if ((flags & ~TFD_SETTIME_FLAGS) ||
Deepa Dinamanibff41202017-06-24 11:45:07 -0700452 !itimerspec64_valid(new))
Davide Libenzib215e282007-05-10 22:23:16 -0700453 return -EINVAL;
454
Al Viro2903ff02012-08-28 12:52:22 -0400455 ret = timerfd_fget(ufd, &f);
456 if (ret)
457 return ret;
458 ctx = f.file->private_data;
Davide Libenzib215e282007-05-10 22:23:16 -0700459
Stephen Smalley25b68a82017-02-17 10:13:59 -0500460 if (isalarm(ctx) && !capable(CAP_WAKE_ALARM)) {
Eric Caruso2895a5e2016-06-08 16:08:59 -0700461 fdput(f);
462 return -EPERM;
463 }
464
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200465 timerfd_setup_cancel(ctx, flags);
466
Davide Libenzi4d672e72008-02-04 22:27:26 -0800467 /*
468 * We need to stop the existing timer before reprogramming
469 * it to the new values.
470 */
471 for (;;) {
472 spin_lock_irq(&ctx->wqh.lock);
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700473
474 if (isalarm(ctx)) {
475 if (alarm_try_to_cancel(&ctx->t.alarm) >= 0)
476 break;
477 } else {
478 if (hrtimer_try_to_cancel(&ctx->t.tmr) >= 0)
479 break;
480 }
Davide Libenzi18963c02007-05-18 12:02:33 -0700481 spin_unlock_irq(&ctx->wqh.lock);
Anna-Maria Gleixnera125ecc2019-07-31 00:33:50 +0200482
483 if (isalarm(ctx))
484 hrtimer_cancel_wait_running(&ctx->t.alarm.timer);
485 else
486 hrtimer_cancel_wait_running(&ctx->t.tmr);
Davide Libenzib215e282007-05-10 22:23:16 -0700487 }
488
Davide Libenzi4d672e72008-02-04 22:27:26 -0800489 /*
490 * If the timer is expired and it's periodic, we need to advance it
491 * because the caller may want to know the previous expiration time.
492 * We do not update "ticks" and "expired" since the timer will be
493 * re-programmed again in the following timerfd_setup() call.
494 */
Thomas Gleixner2456e852016-12-25 11:38:40 +0100495 if (ctx->expired && ctx->tintv) {
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700496 if (isalarm(ctx))
497 alarm_forward_now(&ctx->t.alarm, ctx->tintv);
498 else
499 hrtimer_forward_now(&ctx->t.tmr, ctx->tintv);
500 }
Davide Libenzib215e282007-05-10 22:23:16 -0700501
Deepa Dinamanibff41202017-06-24 11:45:07 -0700502 old->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx));
503 old->it_interval = ktime_to_timespec64(ctx->tintv);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800504
505 /*
506 * Re-program the timer to the new value ...
507 */
Al Viro9d94b9e2012-12-27 16:52:33 -0500508 ret = timerfd_setup(ctx, flags, new);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800509
510 spin_unlock_irq(&ctx->wqh.lock);
Al Viro2903ff02012-08-28 12:52:22 -0400511 fdput(f);
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200512 return ret;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800513}
514
Deepa Dinamanibff41202017-06-24 11:45:07 -0700515static int do_timerfd_gettime(int ufd, struct itimerspec64 *t)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800516{
Al Viro2903ff02012-08-28 12:52:22 -0400517 struct fd f;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800518 struct timerfd_ctx *ctx;
Al Viro2903ff02012-08-28 12:52:22 -0400519 int ret = timerfd_fget(ufd, &f);
520 if (ret)
521 return ret;
522 ctx = f.file->private_data;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800523
524 spin_lock_irq(&ctx->wqh.lock);
Thomas Gleixner2456e852016-12-25 11:38:40 +0100525 if (ctx->expired && ctx->tintv) {
Davide Libenzi4d672e72008-02-04 22:27:26 -0800526 ctx->expired = 0;
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700527
528 if (isalarm(ctx)) {
529 ctx->ticks +=
530 alarm_forward_now(
531 &ctx->t.alarm, ctx->tintv) - 1;
532 alarm_restart(&ctx->t.alarm);
533 } else {
534 ctx->ticks +=
535 hrtimer_forward_now(&ctx->t.tmr, ctx->tintv)
536 - 1;
537 hrtimer_restart(&ctx->t.tmr);
538 }
Davide Libenzi4d672e72008-02-04 22:27:26 -0800539 }
Deepa Dinamanibff41202017-06-24 11:45:07 -0700540 t->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx));
541 t->it_interval = ktime_to_timespec64(ctx->tintv);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800542 spin_unlock_irq(&ctx->wqh.lock);
Al Viro2903ff02012-08-28 12:52:22 -0400543 fdput(f);
Al Viro9d94b9e2012-12-27 16:52:33 -0500544 return 0;
545}
Davide Libenzi4d672e72008-02-04 22:27:26 -0800546
Al Viro9d94b9e2012-12-27 16:52:33 -0500547SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
Deepa Dinamani6ff84732018-06-16 22:11:44 -0700548 const struct __kernel_itimerspec __user *, utmr,
549 struct __kernel_itimerspec __user *, otmr)
Al Viro9d94b9e2012-12-27 16:52:33 -0500550{
Deepa Dinamanibff41202017-06-24 11:45:07 -0700551 struct itimerspec64 new, old;
Al Viro9d94b9e2012-12-27 16:52:33 -0500552 int ret;
553
Deepa Dinamanibff41202017-06-24 11:45:07 -0700554 if (get_itimerspec64(&new, utmr))
Al Viro9d94b9e2012-12-27 16:52:33 -0500555 return -EFAULT;
556 ret = do_timerfd_settime(ufd, flags, &new, &old);
557 if (ret)
558 return ret;
Deepa Dinamanibff41202017-06-24 11:45:07 -0700559 if (otmr && put_itimerspec64(&old, otmr))
Al Viro9d94b9e2012-12-27 16:52:33 -0500560 return -EFAULT;
561
562 return ret;
563}
564
Deepa Dinamani6ff84732018-06-16 22:11:44 -0700565SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct __kernel_itimerspec __user *, otmr)
Al Viro9d94b9e2012-12-27 16:52:33 -0500566{
Deepa Dinamanibff41202017-06-24 11:45:07 -0700567 struct itimerspec64 kotmr;
Al Viro9d94b9e2012-12-27 16:52:33 -0500568 int ret = do_timerfd_gettime(ufd, &kotmr);
569 if (ret)
570 return ret;
Deepa Dinamanibff41202017-06-24 11:45:07 -0700571 return put_itimerspec64(&kotmr, otmr) ? -EFAULT : 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700572}
573
Deepa Dinamani6ff84732018-06-16 22:11:44 -0700574#ifdef CONFIG_COMPAT_32BIT_TIME
Arnd Bergmann8dabe722019-01-07 00:33:08 +0100575SYSCALL_DEFINE4(timerfd_settime32, int, ufd, int, flags,
Arnd Bergmann9afc5ee2018-07-13 12:52:28 +0200576 const struct old_itimerspec32 __user *, utmr,
577 struct old_itimerspec32 __user *, otmr)
Al Viro9d94b9e2012-12-27 16:52:33 -0500578{
Deepa Dinamanibff41202017-06-24 11:45:07 -0700579 struct itimerspec64 new, old;
Al Viro9d94b9e2012-12-27 16:52:33 -0500580 int ret;
581
Arnd Bergmann9afc5ee2018-07-13 12:52:28 +0200582 if (get_old_itimerspec32(&new, utmr))
Al Viro9d94b9e2012-12-27 16:52:33 -0500583 return -EFAULT;
584 ret = do_timerfd_settime(ufd, flags, &new, &old);
585 if (ret)
586 return ret;
Arnd Bergmann9afc5ee2018-07-13 12:52:28 +0200587 if (otmr && put_old_itimerspec32(&old, otmr))
Al Viro9d94b9e2012-12-27 16:52:33 -0500588 return -EFAULT;
589 return ret;
590}
591
Arnd Bergmann8dabe722019-01-07 00:33:08 +0100592SYSCALL_DEFINE2(timerfd_gettime32, int, ufd,
Arnd Bergmann9afc5ee2018-07-13 12:52:28 +0200593 struct old_itimerspec32 __user *, otmr)
Al Viro9d94b9e2012-12-27 16:52:33 -0500594{
Deepa Dinamanibff41202017-06-24 11:45:07 -0700595 struct itimerspec64 kotmr;
Al Viro9d94b9e2012-12-27 16:52:33 -0500596 int ret = do_timerfd_gettime(ufd, &kotmr);
597 if (ret)
598 return ret;
Arnd Bergmann9afc5ee2018-07-13 12:52:28 +0200599 return put_old_itimerspec32(&kotmr, otmr) ? -EFAULT : 0;
Al Viro9d94b9e2012-12-27 16:52:33 -0500600}
601#endif