blob: ac7f59a58f947d13df41d8cb1c577c8c6c4ce4f2 [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>
Davide Libenzib215e282007-05-10 22:23:16 -070029
30struct timerfd_ctx {
Todd Poynor11ffa9d2013-05-15 14:38:12 -070031 union {
32 struct hrtimer tmr;
33 struct alarm alarm;
34 } t;
Davide Libenzib215e282007-05-10 22:23:16 -070035 ktime_t tintv;
Thomas Gleixner99ee5312011-04-27 14:16:42 +020036 ktime_t moffs;
Davide Libenzib215e282007-05-10 22:23:16 -070037 wait_queue_head_t wqh;
Davide Libenzi4d672e72008-02-04 22:27:26 -080038 u64 ticks;
Davide Libenzi4d672e72008-02-04 22:27:26 -080039 int clockid;
Cyrill Gorcunovaf9c4952014-07-16 01:54:52 +040040 short unsigned expired;
41 short unsigned settime_flags; /* to show in fdinfo */
Thomas Gleixner9ec26902011-05-20 16:18:50 +020042 struct rcu_head rcu;
43 struct list_head clist;
Thomas Gleixner1e38da32017-01-31 15:24:03 +010044 spinlock_t cancel_lock;
Thomas Gleixner99ee5312011-04-27 14:16:42 +020045 bool might_cancel;
Davide Libenzib215e282007-05-10 22:23:16 -070046};
47
Thomas Gleixner9ec26902011-05-20 16:18:50 +020048static LIST_HEAD(cancel_list);
49static DEFINE_SPINLOCK(cancel_lock);
50
Todd Poynor11ffa9d2013-05-15 14:38:12 -070051static inline bool isalarm(struct timerfd_ctx *ctx)
52{
53 return ctx->clockid == CLOCK_REALTIME_ALARM ||
54 ctx->clockid == CLOCK_BOOTTIME_ALARM;
55}
56
Davide Libenzib215e282007-05-10 22:23:16 -070057/*
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,
Thomas Gleixner2456e852016-12-25 11:38:40 +010060 * tintv != 0) until the timer is accessed.
Davide Libenzib215e282007-05-10 22:23:16 -070061 */
Todd Poynor11ffa9d2013-05-15 14:38:12 -070062static void timerfd_triggered(struct timerfd_ctx *ctx)
Davide Libenzib215e282007-05-10 22:23:16 -070063{
Davide Libenzib215e282007-05-10 22:23:16 -070064 unsigned long flags;
65
Davide Libenzi18963c02007-05-18 12:02:33 -070066 spin_lock_irqsave(&ctx->wqh.lock, flags);
Davide Libenzib215e282007-05-10 22:23:16 -070067 ctx->expired = 1;
Davide Libenzi4d672e72008-02-04 22:27:26 -080068 ctx->ticks++;
Christoph Hellwig7dda7122018-07-16 12:25:50 +020069 wake_up_locked_poll(&ctx->wqh, EPOLLIN);
Davide Libenzi18963c02007-05-18 12:02:33 -070070 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
Todd Poynor11ffa9d2013-05-15 14:38:12 -070071}
Davide Libenzib215e282007-05-10 22:23:16 -070072
Todd Poynor11ffa9d2013-05-15 14:38:12 -070073static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr)
74{
75 struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx,
76 t.tmr);
77 timerfd_triggered(ctx);
Davide Libenzib215e282007-05-10 22:23:16 -070078 return HRTIMER_NORESTART;
79}
80
Todd Poynor11ffa9d2013-05-15 14:38:12 -070081static enum alarmtimer_restart timerfd_alarmproc(struct alarm *alarm,
82 ktime_t now)
83{
84 struct timerfd_ctx *ctx = container_of(alarm, struct timerfd_ctx,
85 t.alarm);
86 timerfd_triggered(ctx);
87 return ALARMTIMER_NORESTART;
88}
89
Thomas Gleixner9ec26902011-05-20 16:18:50 +020090/*
91 * Called when the clock was set to cancel the timers in the cancel
Max Asbock1123d9392011-06-13 10:18:32 -070092 * 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().
Thomas Gleixner9ec26902011-05-20 16:18:50 +020095 */
96void timerfd_clock_was_set(void)
97{
Thomas Gleixner2456e852016-12-25 11:38:40 +010098 ktime_t moffs = ktime_mono_to_real(0);
Thomas Gleixner9ec26902011-05-20 16:18:50 +020099 struct timerfd_ctx *ctx;
100 unsigned long flags;
101
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);
Thomas Gleixner2456e852016-12-25 11:38:40 +0100107 if (ctx->moffs != moffs) {
108 ctx->moffs = KTIME_MAX;
Max Asbock1123d9392011-06-13 10:18:32 -0700109 ctx->ticks++;
Christoph Hellwig7dda7122018-07-16 12:25:50 +0200110 wake_up_locked_poll(&ctx->wqh, EPOLLIN);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200111 }
112 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
113 }
114 rcu_read_unlock();
115}
116
Thomas Gleixner1e38da32017-01-31 15:24:03 +0100117static void __timerfd_remove_cancel(struct timerfd_ctx *ctx)
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200118{
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);
124 }
125}
126
Thomas Gleixner1e38da32017-01-31 15:24:03 +0100127static void timerfd_remove_cancel(struct timerfd_ctx *ctx)
128{
129 spin_lock(&ctx->cancel_lock);
130 __timerfd_remove_cancel(ctx);
131 spin_unlock(&ctx->cancel_lock);
132}
133
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200134static bool timerfd_canceled(struct timerfd_ctx *ctx)
135{
Thomas Gleixner2456e852016-12-25 11:38:40 +0100136 if (!ctx->might_cancel || ctx->moffs != KTIME_MAX)
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200137 return false;
Thomas Gleixner2456e852016-12-25 11:38:40 +0100138 ctx->moffs = ktime_mono_to_real(0);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200139 return true;
140}
141
142static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags)
143{
Thomas Gleixner1e38da32017-01-31 15:24:03 +0100144 spin_lock(&ctx->cancel_lock);
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700145 if ((ctx->clockid == CLOCK_REALTIME ||
146 ctx->clockid == CLOCK_REALTIME_ALARM) &&
147 (flags & TFD_TIMER_ABSTIME) && (flags & TFD_TIMER_CANCEL_ON_SET)) {
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200148 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);
153 }
Thomas Gleixner1e38da32017-01-31 15:24:03 +0100154 } else {
155 __timerfd_remove_cancel(ctx);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200156 }
Thomas Gleixner1e38da32017-01-31 15:24:03 +0100157 spin_unlock(&ctx->cancel_lock);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200158}
159
Davide Libenzi4d672e72008-02-04 22:27:26 -0800160static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
161{
Arjan van de Ven76369472008-09-01 15:00:14 -0700162 ktime_t remaining;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800163
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700164 if (isalarm(ctx))
165 remaining = alarm_expires_remaining(&ctx->t.alarm);
166 else
Thomas Gleixnerb62526e2016-01-14 16:54:46 +0000167 remaining = hrtimer_expires_remaining_adjusted(&ctx->t.tmr);
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700168
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100169 return remaining < 0 ? 0: remaining;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800170}
171
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200172static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
Deepa Dinamanibff41202017-06-24 11:45:07 -0700173 const struct itimerspec64 *ktmr)
Davide Libenzib215e282007-05-10 22:23:16 -0700174{
175 enum hrtimer_mode htmode;
176 ktime_t texp;
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200177 int clockid = ctx->clockid;
Davide Libenzib215e282007-05-10 22:23:16 -0700178
179 htmode = (flags & TFD_TIMER_ABSTIME) ?
180 HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
181
Deepa Dinamanibff41202017-06-24 11:45:07 -0700182 texp = timespec64_to_ktime(ktmr->it_value);
Davide Libenzib215e282007-05-10 22:23:16 -0700183 ctx->expired = 0;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800184 ctx->ticks = 0;
Deepa Dinamanibff41202017-06-24 11:45:07 -0700185 ctx->tintv = timespec64_to_ktime(ktmr->it_interval);
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700186
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;
196 }
197
Thomas Gleixner2456e852016-12-25 11:38:40 +0100198 if (texp != 0) {
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700199 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);
206 }
207
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200208 if (timerfd_canceled(ctx))
209 return -ECANCELED;
210 }
Cyrill Gorcunovaf9c4952014-07-16 01:54:52 +0400211
212 ctx->settime_flags = flags & TFD_SETTIME_FLAGS;
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200213 return 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700214}
215
216static int timerfd_release(struct inode *inode, struct file *file)
217{
218 struct timerfd_ctx *ctx = file->private_data;
219
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200220 timerfd_remove_cancel(ctx);
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700221
222 if (isalarm(ctx))
223 alarm_cancel(&ctx->t.alarm);
224 else
225 hrtimer_cancel(&ctx->t.tmr);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200226 kfree_rcu(ctx, rcu);
Davide Libenzib215e282007-05-10 22:23:16 -0700227 return 0;
228}
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700229
230static __poll_t timerfd_poll(struct file *file, poll_table *wait)
Davide Libenzib215e282007-05-10 22:23:16 -0700231{
232 struct timerfd_ctx *ctx = file->private_data;
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700233 __poll_t events = 0;
234 unsigned long flags;
Davide Libenzib215e282007-05-10 22:23:16 -0700235
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700236 poll_wait(file, &ctx->wqh, wait);
Davide Libenzib215e282007-05-10 22:23:16 -0700237
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700238 spin_lock_irqsave(&ctx->wqh.lock, flags);
239 if (ctx->ticks)
240 events |= EPOLLIN;
241 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
Davide Libenzib215e282007-05-10 22:23:16 -0700242
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700243 return events;
Davide Libenzib215e282007-05-10 22:23:16 -0700244}
245
246static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
247 loff_t *ppos)
248{
249 struct timerfd_ctx *ctx = file->private_data;
250 ssize_t res;
Davide Libenzi09828402007-07-26 10:41:07 -0700251 u64 ticks = 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700252
253 if (count < sizeof(ticks))
254 return -EINVAL;
Davide Libenzi18963c02007-05-18 12:02:33 -0700255 spin_lock_irq(&ctx->wqh.lock);
Michal Nazarewicz8120a8a2010-05-05 12:53:12 +0200256 if (file->f_flags & O_NONBLOCK)
257 res = -EAGAIN;
258 else
259 res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200260
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200261 /*
262 * If clock has changed, we do not care about the
263 * ticks and we do not rearm the timer. Userspace must
264 * reevaluate anyway.
265 */
266 if (timerfd_canceled(ctx)) {
267 ctx->ticks = 0;
268 ctx->expired = 0;
269 res = -ECANCELED;
270 }
271
Davide Libenzi4d672e72008-02-04 22:27:26 -0800272 if (ctx->ticks) {
273 ticks = ctx->ticks;
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200274
Thomas Gleixner2456e852016-12-25 11:38:40 +0100275 if (ctx->expired && ctx->tintv) {
Davide Libenzib215e282007-05-10 22:23:16 -0700276 /*
Thomas Gleixner2456e852016-12-25 11:38:40 +0100277 * If tintv != 0, this is a periodic timer that
Davide Libenzib215e282007-05-10 22:23:16 -0700278 * needs to be re-armed. We avoid doing it in the timer
279 * callback to avoid DoS attacks specifying a very
280 * short timer period.
281 */
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700282 if (isalarm(ctx)) {
283 ticks += alarm_forward_now(
284 &ctx->t.alarm, ctx->tintv) - 1;
285 alarm_restart(&ctx->t.alarm);
286 } else {
287 ticks += hrtimer_forward_now(&ctx->t.tmr,
288 ctx->tintv) - 1;
289 hrtimer_restart(&ctx->t.tmr);
290 }
Davide Libenzi4d672e72008-02-04 22:27:26 -0800291 }
292 ctx->expired = 0;
293 ctx->ticks = 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700294 }
Davide Libenzi18963c02007-05-18 12:02:33 -0700295 spin_unlock_irq(&ctx->wqh.lock);
Davide Libenzib215e282007-05-10 22:23:16 -0700296 if (ticks)
Davide Libenzi09828402007-07-26 10:41:07 -0700297 res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
Davide Libenzib215e282007-05-10 22:23:16 -0700298 return res;
299}
300
Cyrill Gorcunovaf9c4952014-07-16 01:54:52 +0400301#ifdef CONFIG_PROC_FS
Joe Perchesa3816ab2014-09-29 16:08:25 -0700302static void timerfd_show(struct seq_file *m, struct file *file)
Cyrill Gorcunovaf9c4952014-07-16 01:54:52 +0400303{
304 struct timerfd_ctx *ctx = file->private_data;
Arnd Bergmannbde9e962018-04-20 22:03:38 +0200305 struct timespec64 value, interval;
Cyrill Gorcunovaf9c4952014-07-16 01:54:52 +0400306
307 spin_lock_irq(&ctx->wqh.lock);
Arnd Bergmannbde9e962018-04-20 22:03:38 +0200308 value = ktime_to_timespec64(timerfd_get_remaining(ctx));
309 interval = ktime_to_timespec64(ctx->tintv);
Cyrill Gorcunovaf9c4952014-07-16 01:54:52 +0400310 spin_unlock_irq(&ctx->wqh.lock);
311
Joe Perchesa3816ab2014-09-29 16:08:25 -0700312 seq_printf(m,
313 "clockid: %d\n"
314 "ticks: %llu\n"
315 "settime flags: 0%o\n"
316 "it_value: (%llu, %llu)\n"
317 "it_interval: (%llu, %llu)\n",
318 ctx->clockid,
319 (unsigned long long)ctx->ticks,
320 ctx->settime_flags,
Arnd Bergmannbde9e962018-04-20 22:03:38 +0200321 (unsigned long long)value.tv_sec,
322 (unsigned long long)value.tv_nsec,
323 (unsigned long long)interval.tv_sec,
324 (unsigned long long)interval.tv_nsec);
Cyrill Gorcunovaf9c4952014-07-16 01:54:52 +0400325}
326#else
327#define timerfd_show NULL
328#endif
329
Cyrill Gorcunov5442e9f2014-07-16 01:54:54 +0400330#ifdef CONFIG_CHECKPOINT_RESTORE
331static long timerfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
332{
333 struct timerfd_ctx *ctx = file->private_data;
334 int ret = 0;
335
336 switch (cmd) {
337 case TFD_IOC_SET_TICKS: {
338 u64 ticks;
339
340 if (copy_from_user(&ticks, (u64 __user *)arg, sizeof(ticks)))
341 return -EFAULT;
342 if (!ticks)
343 return -EINVAL;
344
345 spin_lock_irq(&ctx->wqh.lock);
346 if (!timerfd_canceled(ctx)) {
347 ctx->ticks = ticks;
Christoph Hellwig7dda7122018-07-16 12:25:50 +0200348 wake_up_locked_poll(&ctx->wqh, EPOLLIN);
Cyrill Gorcunov5442e9f2014-07-16 01:54:54 +0400349 } else
350 ret = -ECANCELED;
351 spin_unlock_irq(&ctx->wqh.lock);
352 break;
353 }
354 default:
355 ret = -ENOTTY;
356 break;
357 }
358
359 return ret;
360}
361#else
362#define timerfd_ioctl NULL
363#endif
364
Davide Libenzib215e282007-05-10 22:23:16 -0700365static const struct file_operations timerfd_fops = {
366 .release = timerfd_release,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700367 .poll = timerfd_poll,
Davide Libenzib215e282007-05-10 22:23:16 -0700368 .read = timerfd_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200369 .llseek = noop_llseek,
Cyrill Gorcunovaf9c4952014-07-16 01:54:52 +0400370 .show_fdinfo = timerfd_show,
Cyrill Gorcunov5442e9f2014-07-16 01:54:54 +0400371 .unlocked_ioctl = timerfd_ioctl,
Davide Libenzib215e282007-05-10 22:23:16 -0700372};
373
Al Viro2903ff02012-08-28 12:52:22 -0400374static int timerfd_fget(int fd, struct fd *p)
Davide Libenzib215e282007-05-10 22:23:16 -0700375{
Al Viro2903ff02012-08-28 12:52:22 -0400376 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;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800382 }
Al Viro2903ff02012-08-28 12:52:22 -0400383 *p = f;
384 return 0;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800385}
386
Heiko Carstens836f92a2009-01-14 14:14:33 +0100387SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800388{
Al Viro2030a422008-02-23 06:46:49 -0500389 int ufd;
Davide Libenzib215e282007-05-10 22:23:16 -0700390 struct timerfd_ctx *ctx;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800391
Ulrich Dreppere38b36f2008-07-23 21:29:42 -0700392 /* Check the TFD_* constants for consistency. */
393 BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
394 BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
395
Davide Libenzi610d18f2009-02-18 14:48:18 -0800396 if ((flags & ~TFD_CREATE_FLAGS) ||
397 (clockid != CLOCK_MONOTONIC &&
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700398 clockid != CLOCK_REALTIME &&
399 clockid != CLOCK_REALTIME_ALARM &&
Greg Hackmann4a2378a2014-01-08 10:57:03 -0800400 clockid != CLOCK_BOOTTIME &&
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700401 clockid != CLOCK_BOOTTIME_ALARM))
Davide Libenzi4d672e72008-02-04 22:27:26 -0800402 return -EINVAL;
403
Stephen Smalley25b68a82017-02-17 10:13:59 -0500404 if ((clockid == CLOCK_REALTIME_ALARM ||
405 clockid == CLOCK_BOOTTIME_ALARM) &&
406 !capable(CAP_WAKE_ALARM))
Eric Caruso2895a5e2016-06-08 16:08:59 -0700407 return -EPERM;
408
Davide Libenzi4d672e72008-02-04 22:27:26 -0800409 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
410 if (!ctx)
411 return -ENOMEM;
412
413 init_waitqueue_head(&ctx->wqh);
Thomas Gleixner1e38da32017-01-31 15:24:03 +0100414 spin_lock_init(&ctx->cancel_lock);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800415 ctx->clockid = clockid;
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700416
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);
424
Thomas Gleixner2456e852016-12-25 11:38:40 +0100425 ctx->moffs = ktime_mono_to_real(0);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800426
Ulrich Drepper11fcb6c2008-07-23 21:29:26 -0700427 ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx,
Roland Dreier628ff7c2009-12-18 09:41:24 -0800428 O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
Al Viro2030a422008-02-23 06:46:49 -0500429 if (ufd < 0)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800430 kfree(ctx);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800431
432 return ufd;
433}
434
Al Viro9d94b9e2012-12-27 16:52:33 -0500435static int do_timerfd_settime(int ufd, int flags,
Deepa Dinamanibff41202017-06-24 11:45:07 -0700436 const struct itimerspec64 *new,
437 struct itimerspec64 *old)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800438{
Al Viro2903ff02012-08-28 12:52:22 -0400439 struct fd f;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800440 struct timerfd_ctx *ctx;
Al Viro2903ff02012-08-28 12:52:22 -0400441 int ret;
Davide Libenzib215e282007-05-10 22:23:16 -0700442
Davide Libenzi610d18f2009-02-18 14:48:18 -0800443 if ((flags & ~TFD_SETTIME_FLAGS) ||
Deepa Dinamanibff41202017-06-24 11:45:07 -0700444 !itimerspec64_valid(new))
Davide Libenzib215e282007-05-10 22:23:16 -0700445 return -EINVAL;
446
Al Viro2903ff02012-08-28 12:52:22 -0400447 ret = timerfd_fget(ufd, &f);
448 if (ret)
449 return ret;
450 ctx = f.file->private_data;
Davide Libenzib215e282007-05-10 22:23:16 -0700451
Stephen Smalley25b68a82017-02-17 10:13:59 -0500452 if (isalarm(ctx) && !capable(CAP_WAKE_ALARM)) {
Eric Caruso2895a5e2016-06-08 16:08:59 -0700453 fdput(f);
454 return -EPERM;
455 }
456
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200457 timerfd_setup_cancel(ctx, flags);
458
Davide Libenzi4d672e72008-02-04 22:27:26 -0800459 /*
460 * We need to stop the existing timer before reprogramming
461 * it to the new values.
462 */
463 for (;;) {
464 spin_lock_irq(&ctx->wqh.lock);
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700465
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;
472 }
Davide Libenzi18963c02007-05-18 12:02:33 -0700473 spin_unlock_irq(&ctx->wqh.lock);
Anna-Maria Gleixnera125ecc2019-07-31 00:33:50 +0200474
475 if (isalarm(ctx))
476 hrtimer_cancel_wait_running(&ctx->t.alarm.timer);
477 else
478 hrtimer_cancel_wait_running(&ctx->t.tmr);
Davide Libenzib215e282007-05-10 22:23:16 -0700479 }
480
Davide Libenzi4d672e72008-02-04 22:27:26 -0800481 /*
482 * If the timer is expired and it's periodic, we need to advance it
483 * because the caller may want to know the previous expiration time.
484 * We do not update "ticks" and "expired" since the timer will be
485 * re-programmed again in the following timerfd_setup() call.
486 */
Thomas Gleixner2456e852016-12-25 11:38:40 +0100487 if (ctx->expired && ctx->tintv) {
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700488 if (isalarm(ctx))
489 alarm_forward_now(&ctx->t.alarm, ctx->tintv);
490 else
491 hrtimer_forward_now(&ctx->t.tmr, ctx->tintv);
492 }
Davide Libenzib215e282007-05-10 22:23:16 -0700493
Deepa Dinamanibff41202017-06-24 11:45:07 -0700494 old->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx));
495 old->it_interval = ktime_to_timespec64(ctx->tintv);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800496
497 /*
498 * Re-program the timer to the new value ...
499 */
Al Viro9d94b9e2012-12-27 16:52:33 -0500500 ret = timerfd_setup(ctx, flags, new);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800501
502 spin_unlock_irq(&ctx->wqh.lock);
Al Viro2903ff02012-08-28 12:52:22 -0400503 fdput(f);
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200504 return ret;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800505}
506
Deepa Dinamanibff41202017-06-24 11:45:07 -0700507static int do_timerfd_gettime(int ufd, struct itimerspec64 *t)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800508{
Al Viro2903ff02012-08-28 12:52:22 -0400509 struct fd f;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800510 struct timerfd_ctx *ctx;
Al Viro2903ff02012-08-28 12:52:22 -0400511 int ret = timerfd_fget(ufd, &f);
512 if (ret)
513 return ret;
514 ctx = f.file->private_data;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800515
516 spin_lock_irq(&ctx->wqh.lock);
Thomas Gleixner2456e852016-12-25 11:38:40 +0100517 if (ctx->expired && ctx->tintv) {
Davide Libenzi4d672e72008-02-04 22:27:26 -0800518 ctx->expired = 0;
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700519
520 if (isalarm(ctx)) {
521 ctx->ticks +=
522 alarm_forward_now(
523 &ctx->t.alarm, ctx->tintv) - 1;
524 alarm_restart(&ctx->t.alarm);
525 } else {
526 ctx->ticks +=
527 hrtimer_forward_now(&ctx->t.tmr, ctx->tintv)
528 - 1;
529 hrtimer_restart(&ctx->t.tmr);
530 }
Davide Libenzi4d672e72008-02-04 22:27:26 -0800531 }
Deepa Dinamanibff41202017-06-24 11:45:07 -0700532 t->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx));
533 t->it_interval = ktime_to_timespec64(ctx->tintv);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800534 spin_unlock_irq(&ctx->wqh.lock);
Al Viro2903ff02012-08-28 12:52:22 -0400535 fdput(f);
Al Viro9d94b9e2012-12-27 16:52:33 -0500536 return 0;
537}
Davide Libenzi4d672e72008-02-04 22:27:26 -0800538
Al Viro9d94b9e2012-12-27 16:52:33 -0500539SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
Deepa Dinamani6ff84732018-06-16 22:11:44 -0700540 const struct __kernel_itimerspec __user *, utmr,
541 struct __kernel_itimerspec __user *, otmr)
Al Viro9d94b9e2012-12-27 16:52:33 -0500542{
Deepa Dinamanibff41202017-06-24 11:45:07 -0700543 struct itimerspec64 new, old;
Al Viro9d94b9e2012-12-27 16:52:33 -0500544 int ret;
545
Deepa Dinamanibff41202017-06-24 11:45:07 -0700546 if (get_itimerspec64(&new, utmr))
Al Viro9d94b9e2012-12-27 16:52:33 -0500547 return -EFAULT;
548 ret = do_timerfd_settime(ufd, flags, &new, &old);
549 if (ret)
550 return ret;
Deepa Dinamanibff41202017-06-24 11:45:07 -0700551 if (otmr && put_itimerspec64(&old, otmr))
Al Viro9d94b9e2012-12-27 16:52:33 -0500552 return -EFAULT;
553
554 return ret;
555}
556
Deepa Dinamani6ff84732018-06-16 22:11:44 -0700557SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct __kernel_itimerspec __user *, otmr)
Al Viro9d94b9e2012-12-27 16:52:33 -0500558{
Deepa Dinamanibff41202017-06-24 11:45:07 -0700559 struct itimerspec64 kotmr;
Al Viro9d94b9e2012-12-27 16:52:33 -0500560 int ret = do_timerfd_gettime(ufd, &kotmr);
561 if (ret)
562 return ret;
Deepa Dinamanibff41202017-06-24 11:45:07 -0700563 return put_itimerspec64(&kotmr, otmr) ? -EFAULT : 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700564}
565
Deepa Dinamani6ff84732018-06-16 22:11:44 -0700566#ifdef CONFIG_COMPAT_32BIT_TIME
Arnd Bergmann8dabe722019-01-07 00:33:08 +0100567SYSCALL_DEFINE4(timerfd_settime32, int, ufd, int, flags,
Arnd Bergmann9afc5ee2018-07-13 12:52:28 +0200568 const struct old_itimerspec32 __user *, utmr,
569 struct old_itimerspec32 __user *, otmr)
Al Viro9d94b9e2012-12-27 16:52:33 -0500570{
Deepa Dinamanibff41202017-06-24 11:45:07 -0700571 struct itimerspec64 new, old;
Al Viro9d94b9e2012-12-27 16:52:33 -0500572 int ret;
573
Arnd Bergmann9afc5ee2018-07-13 12:52:28 +0200574 if (get_old_itimerspec32(&new, utmr))
Al Viro9d94b9e2012-12-27 16:52:33 -0500575 return -EFAULT;
576 ret = do_timerfd_settime(ufd, flags, &new, &old);
577 if (ret)
578 return ret;
Arnd Bergmann9afc5ee2018-07-13 12:52:28 +0200579 if (otmr && put_old_itimerspec32(&old, otmr))
Al Viro9d94b9e2012-12-27 16:52:33 -0500580 return -EFAULT;
581 return ret;
582}
583
Arnd Bergmann8dabe722019-01-07 00:33:08 +0100584SYSCALL_DEFINE2(timerfd_gettime32, int, ufd,
Arnd Bergmann9afc5ee2018-07-13 12:52:28 +0200585 struct old_itimerspec32 __user *, otmr)
Al Viro9d94b9e2012-12-27 16:52:33 -0500586{
Deepa Dinamanibff41202017-06-24 11:45:07 -0700587 struct itimerspec64 kotmr;
Al Viro9d94b9e2012-12-27 16:52:33 -0500588 int ret = do_timerfd_gettime(ufd, &kotmr);
589 if (ret)
590 return ret;
Arnd Bergmann9afc5ee2018-07-13 12:52:28 +0200591 return put_old_itimerspec32(&kotmr, otmr) ? -EFAULT : 0;
Al Viro9d94b9e2012-12-27 16:52:33 -0500592}
593#endif