blob: ece0c02d7e63a434cffbc16de66321fc0d43f3b6 [file] [log] [blame]
Davide Libenzib215e282007-05-10 22:23:16 -07001/*
2 * fs/timerfd.c
3 *
4 * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org>
5 *
6 *
7 * Thanks to Thomas Gleixner for code reviews and useful comments.
8 *
9 */
10
Todd Poynor11ffa9d2013-05-15 14:38:12 -070011#include <linux/alarmtimer.h>
Davide Libenzib215e282007-05-10 22:23:16 -070012#include <linux/file.h>
13#include <linux/poll.h>
14#include <linux/init.h>
15#include <linux/fs.h>
16#include <linux/sched.h>
17#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Davide Libenzib215e282007-05-10 22:23:16 -070019#include <linux/list.h>
20#include <linux/spinlock.h>
21#include <linux/time.h>
22#include <linux/hrtimer.h>
23#include <linux/anon_inodes.h>
24#include <linux/timerfd.h>
Adrian Bunk45cc2b92008-04-29 00:58:59 -070025#include <linux/syscalls.h>
Al Viro9d94b9e2012-12-27 16:52:33 -050026#include <linux/compat.h>
Thomas Gleixner9ec26902011-05-20 16:18:50 +020027#include <linux/rcupdate.h>
Davide Libenzib215e282007-05-10 22:23:16 -070028
29struct timerfd_ctx {
Todd Poynor11ffa9d2013-05-15 14:38:12 -070030 union {
31 struct hrtimer tmr;
32 struct alarm alarm;
33 } t;
Davide Libenzib215e282007-05-10 22:23:16 -070034 ktime_t tintv;
Thomas Gleixner99ee5312011-04-27 14:16:42 +020035 ktime_t moffs;
Davide Libenzib215e282007-05-10 22:23:16 -070036 wait_queue_head_t wqh;
Davide Libenzi4d672e72008-02-04 22:27:26 -080037 u64 ticks;
Davide Libenzi4d672e72008-02-04 22:27:26 -080038 int clockid;
Cyrill Gorcunovaf9c4952014-07-16 01:54:52 +040039 short unsigned expired;
40 short unsigned settime_flags; /* to show in fdinfo */
Thomas Gleixner9ec26902011-05-20 16:18:50 +020041 struct rcu_head rcu;
42 struct list_head clist;
Thomas Gleixner1e38da32017-01-31 15:24:03 +010043 spinlock_t cancel_lock;
Thomas Gleixner99ee5312011-04-27 14:16:42 +020044 bool might_cancel;
Davide Libenzib215e282007-05-10 22:23:16 -070045};
46
Thomas Gleixner9ec26902011-05-20 16:18:50 +020047static LIST_HEAD(cancel_list);
48static DEFINE_SPINLOCK(cancel_lock);
49
Todd Poynor11ffa9d2013-05-15 14:38:12 -070050static inline bool isalarm(struct timerfd_ctx *ctx)
51{
52 return ctx->clockid == CLOCK_REALTIME_ALARM ||
53 ctx->clockid == CLOCK_BOOTTIME_ALARM;
54}
55
Davide Libenzib215e282007-05-10 22:23:16 -070056/*
57 * This gets called when the timer event triggers. We set the "expired"
58 * flag, but we do not re-arm the timer (in case it's necessary,
Thomas Gleixner2456e852016-12-25 11:38:40 +010059 * tintv != 0) until the timer is accessed.
Davide Libenzib215e282007-05-10 22:23:16 -070060 */
Todd Poynor11ffa9d2013-05-15 14:38:12 -070061static void timerfd_triggered(struct timerfd_ctx *ctx)
Davide Libenzib215e282007-05-10 22:23:16 -070062{
Davide Libenzib215e282007-05-10 22:23:16 -070063 unsigned long flags;
64
Davide Libenzi18963c02007-05-18 12:02:33 -070065 spin_lock_irqsave(&ctx->wqh.lock, flags);
Davide Libenzib215e282007-05-10 22:23:16 -070066 ctx->expired = 1;
Davide Libenzi4d672e72008-02-04 22:27:26 -080067 ctx->ticks++;
Davide Libenzib215e282007-05-10 22:23:16 -070068 wake_up_locked(&ctx->wqh);
Davide Libenzi18963c02007-05-18 12:02:33 -070069 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
Todd Poynor11ffa9d2013-05-15 14:38:12 -070070}
Davide Libenzib215e282007-05-10 22:23:16 -070071
Todd Poynor11ffa9d2013-05-15 14:38:12 -070072static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr)
73{
74 struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx,
75 t.tmr);
76 timerfd_triggered(ctx);
Davide Libenzib215e282007-05-10 22:23:16 -070077 return HRTIMER_NORESTART;
78}
79
Todd Poynor11ffa9d2013-05-15 14:38:12 -070080static enum alarmtimer_restart timerfd_alarmproc(struct alarm *alarm,
81 ktime_t now)
82{
83 struct timerfd_ctx *ctx = container_of(alarm, struct timerfd_ctx,
84 t.alarm);
85 timerfd_triggered(ctx);
86 return ALARMTIMER_NORESTART;
87}
88
Thomas Gleixner9ec26902011-05-20 16:18:50 +020089/*
90 * Called when the clock was set to cancel the timers in the cancel
Max Asbock1123d9392011-06-13 10:18:32 -070091 * list. This will wake up processes waiting on these timers. The
92 * wake-up requires ctx->ticks to be non zero, therefore we increment
93 * it before calling wake_up_locked().
Thomas Gleixner9ec26902011-05-20 16:18:50 +020094 */
95void timerfd_clock_was_set(void)
96{
Thomas Gleixner2456e852016-12-25 11:38:40 +010097 ktime_t moffs = ktime_mono_to_real(0);
Thomas Gleixner9ec26902011-05-20 16:18:50 +020098 struct timerfd_ctx *ctx;
99 unsigned long flags;
100
101 rcu_read_lock();
102 list_for_each_entry_rcu(ctx, &cancel_list, clist) {
103 if (!ctx->might_cancel)
104 continue;
105 spin_lock_irqsave(&ctx->wqh.lock, flags);
Thomas Gleixner2456e852016-12-25 11:38:40 +0100106 if (ctx->moffs != moffs) {
107 ctx->moffs = KTIME_MAX;
Max Asbock1123d9392011-06-13 10:18:32 -0700108 ctx->ticks++;
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200109 wake_up_locked(&ctx->wqh);
110 }
111 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
112 }
113 rcu_read_unlock();
114}
115
Thomas Gleixner1e38da32017-01-31 15:24:03 +0100116static void __timerfd_remove_cancel(struct timerfd_ctx *ctx)
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200117{
118 if (ctx->might_cancel) {
119 ctx->might_cancel = false;
120 spin_lock(&cancel_lock);
121 list_del_rcu(&ctx->clist);
122 spin_unlock(&cancel_lock);
123 }
124}
125
Thomas Gleixner1e38da32017-01-31 15:24:03 +0100126static void timerfd_remove_cancel(struct timerfd_ctx *ctx)
127{
128 spin_lock(&ctx->cancel_lock);
129 __timerfd_remove_cancel(ctx);
130 spin_unlock(&ctx->cancel_lock);
131}
132
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200133static bool timerfd_canceled(struct timerfd_ctx *ctx)
134{
Thomas Gleixner2456e852016-12-25 11:38:40 +0100135 if (!ctx->might_cancel || ctx->moffs != KTIME_MAX)
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200136 return false;
Thomas Gleixner2456e852016-12-25 11:38:40 +0100137 ctx->moffs = ktime_mono_to_real(0);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200138 return true;
139}
140
141static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags)
142{
Thomas Gleixner1e38da32017-01-31 15:24:03 +0100143 spin_lock(&ctx->cancel_lock);
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700144 if ((ctx->clockid == CLOCK_REALTIME ||
145 ctx->clockid == CLOCK_REALTIME_ALARM) &&
146 (flags & TFD_TIMER_ABSTIME) && (flags & TFD_TIMER_CANCEL_ON_SET)) {
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200147 if (!ctx->might_cancel) {
148 ctx->might_cancel = true;
149 spin_lock(&cancel_lock);
150 list_add_rcu(&ctx->clist, &cancel_list);
151 spin_unlock(&cancel_lock);
152 }
Thomas Gleixner1e38da32017-01-31 15:24:03 +0100153 } else {
154 __timerfd_remove_cancel(ctx);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200155 }
Thomas Gleixner1e38da32017-01-31 15:24:03 +0100156 spin_unlock(&ctx->cancel_lock);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200157}
158
Davide Libenzi4d672e72008-02-04 22:27:26 -0800159static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
160{
Arjan van de Ven76369472008-09-01 15:00:14 -0700161 ktime_t remaining;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800162
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700163 if (isalarm(ctx))
164 remaining = alarm_expires_remaining(&ctx->t.alarm);
165 else
Thomas Gleixnerb62526e2016-01-14 16:54:46 +0000166 remaining = hrtimer_expires_remaining_adjusted(&ctx->t.tmr);
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700167
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100168 return remaining < 0 ? 0: remaining;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800169}
170
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200171static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
Deepa Dinamanibff41202017-06-24 11:45:07 -0700172 const struct itimerspec64 *ktmr)
Davide Libenzib215e282007-05-10 22:23:16 -0700173{
174 enum hrtimer_mode htmode;
175 ktime_t texp;
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200176 int clockid = ctx->clockid;
Davide Libenzib215e282007-05-10 22:23:16 -0700177
178 htmode = (flags & TFD_TIMER_ABSTIME) ?
179 HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
180
Deepa Dinamanibff41202017-06-24 11:45:07 -0700181 texp = timespec64_to_ktime(ktmr->it_value);
Davide Libenzib215e282007-05-10 22:23:16 -0700182 ctx->expired = 0;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800183 ctx->ticks = 0;
Deepa Dinamanibff41202017-06-24 11:45:07 -0700184 ctx->tintv = timespec64_to_ktime(ktmr->it_interval);
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700185
186 if (isalarm(ctx)) {
187 alarm_init(&ctx->t.alarm,
188 ctx->clockid == CLOCK_REALTIME_ALARM ?
189 ALARM_REALTIME : ALARM_BOOTTIME,
190 timerfd_alarmproc);
191 } else {
192 hrtimer_init(&ctx->t.tmr, clockid, htmode);
193 hrtimer_set_expires(&ctx->t.tmr, texp);
194 ctx->t.tmr.function = timerfd_tmrproc;
195 }
196
Thomas Gleixner2456e852016-12-25 11:38:40 +0100197 if (texp != 0) {
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700198 if (isalarm(ctx)) {
199 if (flags & TFD_TIMER_ABSTIME)
200 alarm_start(&ctx->t.alarm, texp);
201 else
202 alarm_start_relative(&ctx->t.alarm, texp);
203 } else {
204 hrtimer_start(&ctx->t.tmr, texp, htmode);
205 }
206
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200207 if (timerfd_canceled(ctx))
208 return -ECANCELED;
209 }
Cyrill Gorcunovaf9c4952014-07-16 01:54:52 +0400210
211 ctx->settime_flags = flags & TFD_SETTIME_FLAGS;
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200212 return 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700213}
214
215static int timerfd_release(struct inode *inode, struct file *file)
216{
217 struct timerfd_ctx *ctx = file->private_data;
218
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200219 timerfd_remove_cancel(ctx);
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700220
221 if (isalarm(ctx))
222 alarm_cancel(&ctx->t.alarm);
223 else
224 hrtimer_cancel(&ctx->t.tmr);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200225 kfree_rcu(ctx, rcu);
Davide Libenzib215e282007-05-10 22:23:16 -0700226 return 0;
227}
228
229static unsigned int timerfd_poll(struct file *file, poll_table *wait)
230{
231 struct timerfd_ctx *ctx = file->private_data;
232 unsigned int events = 0;
233 unsigned long flags;
234
235 poll_wait(file, &ctx->wqh, wait);
236
Davide Libenzi18963c02007-05-18 12:02:33 -0700237 spin_lock_irqsave(&ctx->wqh.lock, flags);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800238 if (ctx->ticks)
Davide Libenzib215e282007-05-10 22:23:16 -0700239 events |= POLLIN;
Davide Libenzi18963c02007-05-18 12:02:33 -0700240 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
Davide Libenzib215e282007-05-10 22:23:16 -0700241
242 return events;
243}
244
245static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
246 loff_t *ppos)
247{
248 struct timerfd_ctx *ctx = file->private_data;
249 ssize_t res;
Davide Libenzi09828402007-07-26 10:41:07 -0700250 u64 ticks = 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700251
252 if (count < sizeof(ticks))
253 return -EINVAL;
Davide Libenzi18963c02007-05-18 12:02:33 -0700254 spin_lock_irq(&ctx->wqh.lock);
Michal Nazarewicz8120a8a2010-05-05 12:53:12 +0200255 if (file->f_flags & O_NONBLOCK)
256 res = -EAGAIN;
257 else
258 res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200259
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200260 /*
261 * If clock has changed, we do not care about the
262 * ticks and we do not rearm the timer. Userspace must
263 * reevaluate anyway.
264 */
265 if (timerfd_canceled(ctx)) {
266 ctx->ticks = 0;
267 ctx->expired = 0;
268 res = -ECANCELED;
269 }
270
Davide Libenzi4d672e72008-02-04 22:27:26 -0800271 if (ctx->ticks) {
272 ticks = ctx->ticks;
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200273
Thomas Gleixner2456e852016-12-25 11:38:40 +0100274 if (ctx->expired && ctx->tintv) {
Davide Libenzib215e282007-05-10 22:23:16 -0700275 /*
Thomas Gleixner2456e852016-12-25 11:38:40 +0100276 * If tintv != 0, this is a periodic timer that
Davide Libenzib215e282007-05-10 22:23:16 -0700277 * needs to be re-armed. We avoid doing it in the timer
278 * callback to avoid DoS attacks specifying a very
279 * short timer period.
280 */
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700281 if (isalarm(ctx)) {
282 ticks += alarm_forward_now(
283 &ctx->t.alarm, ctx->tintv) - 1;
284 alarm_restart(&ctx->t.alarm);
285 } else {
286 ticks += hrtimer_forward_now(&ctx->t.tmr,
287 ctx->tintv) - 1;
288 hrtimer_restart(&ctx->t.tmr);
289 }
Davide Libenzi4d672e72008-02-04 22:27:26 -0800290 }
291 ctx->expired = 0;
292 ctx->ticks = 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700293 }
Davide Libenzi18963c02007-05-18 12:02:33 -0700294 spin_unlock_irq(&ctx->wqh.lock);
Davide Libenzib215e282007-05-10 22:23:16 -0700295 if (ticks)
Davide Libenzi09828402007-07-26 10:41:07 -0700296 res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
Davide Libenzib215e282007-05-10 22:23:16 -0700297 return res;
298}
299
Cyrill Gorcunovaf9c4952014-07-16 01:54:52 +0400300#ifdef CONFIG_PROC_FS
Joe Perchesa3816ab2014-09-29 16:08:25 -0700301static void timerfd_show(struct seq_file *m, struct file *file)
Cyrill Gorcunovaf9c4952014-07-16 01:54:52 +0400302{
303 struct timerfd_ctx *ctx = file->private_data;
304 struct itimerspec t;
305
306 spin_lock_irq(&ctx->wqh.lock);
307 t.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
308 t.it_interval = ktime_to_timespec(ctx->tintv);
309 spin_unlock_irq(&ctx->wqh.lock);
310
Joe Perchesa3816ab2014-09-29 16:08:25 -0700311 seq_printf(m,
312 "clockid: %d\n"
313 "ticks: %llu\n"
314 "settime flags: 0%o\n"
315 "it_value: (%llu, %llu)\n"
316 "it_interval: (%llu, %llu)\n",
317 ctx->clockid,
318 (unsigned long long)ctx->ticks,
319 ctx->settime_flags,
320 (unsigned long long)t.it_value.tv_sec,
321 (unsigned long long)t.it_value.tv_nsec,
322 (unsigned long long)t.it_interval.tv_sec,
323 (unsigned long long)t.it_interval.tv_nsec);
Cyrill Gorcunovaf9c4952014-07-16 01:54:52 +0400324}
325#else
326#define timerfd_show NULL
327#endif
328
Cyrill Gorcunov5442e9f2014-07-16 01:54:54 +0400329#ifdef CONFIG_CHECKPOINT_RESTORE
330static long timerfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
331{
332 struct timerfd_ctx *ctx = file->private_data;
333 int ret = 0;
334
335 switch (cmd) {
336 case TFD_IOC_SET_TICKS: {
337 u64 ticks;
338
339 if (copy_from_user(&ticks, (u64 __user *)arg, sizeof(ticks)))
340 return -EFAULT;
341 if (!ticks)
342 return -EINVAL;
343
344 spin_lock_irq(&ctx->wqh.lock);
345 if (!timerfd_canceled(ctx)) {
346 ctx->ticks = ticks;
Dan Carpenter88299c92014-08-01 11:28:48 +0300347 wake_up_locked(&ctx->wqh);
Cyrill Gorcunov5442e9f2014-07-16 01:54:54 +0400348 } else
349 ret = -ECANCELED;
350 spin_unlock_irq(&ctx->wqh.lock);
351 break;
352 }
353 default:
354 ret = -ENOTTY;
355 break;
356 }
357
358 return ret;
359}
360#else
361#define timerfd_ioctl NULL
362#endif
363
Davide Libenzib215e282007-05-10 22:23:16 -0700364static const struct file_operations timerfd_fops = {
365 .release = timerfd_release,
366 .poll = timerfd_poll,
367 .read = timerfd_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200368 .llseek = noop_llseek,
Cyrill Gorcunovaf9c4952014-07-16 01:54:52 +0400369 .show_fdinfo = timerfd_show,
Cyrill Gorcunov5442e9f2014-07-16 01:54:54 +0400370 .unlocked_ioctl = timerfd_ioctl,
Davide Libenzib215e282007-05-10 22:23:16 -0700371};
372
Al Viro2903ff02012-08-28 12:52:22 -0400373static int timerfd_fget(int fd, struct fd *p)
Davide Libenzib215e282007-05-10 22:23:16 -0700374{
Al Viro2903ff02012-08-28 12:52:22 -0400375 struct fd f = fdget(fd);
376 if (!f.file)
377 return -EBADF;
378 if (f.file->f_op != &timerfd_fops) {
379 fdput(f);
380 return -EINVAL;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800381 }
Al Viro2903ff02012-08-28 12:52:22 -0400382 *p = f;
383 return 0;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800384}
385
Heiko Carstens836f92a2009-01-14 14:14:33 +0100386SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800387{
Al Viro2030a422008-02-23 06:46:49 -0500388 int ufd;
Davide Libenzib215e282007-05-10 22:23:16 -0700389 struct timerfd_ctx *ctx;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800390
Ulrich Dreppere38b36f2008-07-23 21:29:42 -0700391 /* Check the TFD_* constants for consistency. */
392 BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
393 BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
394
Davide Libenzi610d18f2009-02-18 14:48:18 -0800395 if ((flags & ~TFD_CREATE_FLAGS) ||
396 (clockid != CLOCK_MONOTONIC &&
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700397 clockid != CLOCK_REALTIME &&
398 clockid != CLOCK_REALTIME_ALARM &&
Greg Hackmann4a2378a2014-01-08 10:57:03 -0800399 clockid != CLOCK_BOOTTIME &&
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700400 clockid != CLOCK_BOOTTIME_ALARM))
Davide Libenzi4d672e72008-02-04 22:27:26 -0800401 return -EINVAL;
402
Stephen Smalley25b68a82017-02-17 10:13:59 -0500403 if ((clockid == CLOCK_REALTIME_ALARM ||
404 clockid == CLOCK_BOOTTIME_ALARM) &&
405 !capable(CAP_WAKE_ALARM))
Eric Caruso2895a5e2016-06-08 16:08:59 -0700406 return -EPERM;
407
Davide Libenzi4d672e72008-02-04 22:27:26 -0800408 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
409 if (!ctx)
410 return -ENOMEM;
411
412 init_waitqueue_head(&ctx->wqh);
Thomas Gleixner1e38da32017-01-31 15:24:03 +0100413 spin_lock_init(&ctx->cancel_lock);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800414 ctx->clockid = clockid;
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700415
416 if (isalarm(ctx))
417 alarm_init(&ctx->t.alarm,
418 ctx->clockid == CLOCK_REALTIME_ALARM ?
419 ALARM_REALTIME : ALARM_BOOTTIME,
420 timerfd_alarmproc);
421 else
422 hrtimer_init(&ctx->t.tmr, clockid, HRTIMER_MODE_ABS);
423
Thomas Gleixner2456e852016-12-25 11:38:40 +0100424 ctx->moffs = ktime_mono_to_real(0);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800425
Ulrich Drepper11fcb6c2008-07-23 21:29:26 -0700426 ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx,
Roland Dreier628ff7c2009-12-18 09:41:24 -0800427 O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
Al Viro2030a422008-02-23 06:46:49 -0500428 if (ufd < 0)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800429 kfree(ctx);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800430
431 return ufd;
432}
433
Al Viro9d94b9e2012-12-27 16:52:33 -0500434static int do_timerfd_settime(int ufd, int flags,
Deepa Dinamanibff41202017-06-24 11:45:07 -0700435 const struct itimerspec64 *new,
436 struct itimerspec64 *old)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800437{
Al Viro2903ff02012-08-28 12:52:22 -0400438 struct fd f;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800439 struct timerfd_ctx *ctx;
Al Viro2903ff02012-08-28 12:52:22 -0400440 int ret;
Davide Libenzib215e282007-05-10 22:23:16 -0700441
Davide Libenzi610d18f2009-02-18 14:48:18 -0800442 if ((flags & ~TFD_SETTIME_FLAGS) ||
Deepa Dinamanibff41202017-06-24 11:45:07 -0700443 !itimerspec64_valid(new))
Davide Libenzib215e282007-05-10 22:23:16 -0700444 return -EINVAL;
445
Al Viro2903ff02012-08-28 12:52:22 -0400446 ret = timerfd_fget(ufd, &f);
447 if (ret)
448 return ret;
449 ctx = f.file->private_data;
Davide Libenzib215e282007-05-10 22:23:16 -0700450
Stephen Smalley25b68a82017-02-17 10:13:59 -0500451 if (isalarm(ctx) && !capable(CAP_WAKE_ALARM)) {
Eric Caruso2895a5e2016-06-08 16:08:59 -0700452 fdput(f);
453 return -EPERM;
454 }
455
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200456 timerfd_setup_cancel(ctx, flags);
457
Davide Libenzi4d672e72008-02-04 22:27:26 -0800458 /*
459 * We need to stop the existing timer before reprogramming
460 * it to the new values.
461 */
462 for (;;) {
463 spin_lock_irq(&ctx->wqh.lock);
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700464
465 if (isalarm(ctx)) {
466 if (alarm_try_to_cancel(&ctx->t.alarm) >= 0)
467 break;
468 } else {
469 if (hrtimer_try_to_cancel(&ctx->t.tmr) >= 0)
470 break;
471 }
Davide Libenzi18963c02007-05-18 12:02:33 -0700472 spin_unlock_irq(&ctx->wqh.lock);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800473 cpu_relax();
Davide Libenzib215e282007-05-10 22:23:16 -0700474 }
475
Davide Libenzi4d672e72008-02-04 22:27:26 -0800476 /*
477 * If the timer is expired and it's periodic, we need to advance it
478 * because the caller may want to know the previous expiration time.
479 * We do not update "ticks" and "expired" since the timer will be
480 * re-programmed again in the following timerfd_setup() call.
481 */
Thomas Gleixner2456e852016-12-25 11:38:40 +0100482 if (ctx->expired && ctx->tintv) {
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700483 if (isalarm(ctx))
484 alarm_forward_now(&ctx->t.alarm, ctx->tintv);
485 else
486 hrtimer_forward_now(&ctx->t.tmr, ctx->tintv);
487 }
Davide Libenzib215e282007-05-10 22:23:16 -0700488
Deepa Dinamanibff41202017-06-24 11:45:07 -0700489 old->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx));
490 old->it_interval = ktime_to_timespec64(ctx->tintv);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800491
492 /*
493 * Re-program the timer to the new value ...
494 */
Al Viro9d94b9e2012-12-27 16:52:33 -0500495 ret = timerfd_setup(ctx, flags, new);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800496
497 spin_unlock_irq(&ctx->wqh.lock);
Al Viro2903ff02012-08-28 12:52:22 -0400498 fdput(f);
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200499 return ret;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800500}
501
Deepa Dinamanibff41202017-06-24 11:45:07 -0700502static int do_timerfd_gettime(int ufd, struct itimerspec64 *t)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800503{
Al Viro2903ff02012-08-28 12:52:22 -0400504 struct fd f;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800505 struct timerfd_ctx *ctx;
Al Viro2903ff02012-08-28 12:52:22 -0400506 int ret = timerfd_fget(ufd, &f);
507 if (ret)
508 return ret;
509 ctx = f.file->private_data;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800510
511 spin_lock_irq(&ctx->wqh.lock);
Thomas Gleixner2456e852016-12-25 11:38:40 +0100512 if (ctx->expired && ctx->tintv) {
Davide Libenzi4d672e72008-02-04 22:27:26 -0800513 ctx->expired = 0;
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700514
515 if (isalarm(ctx)) {
516 ctx->ticks +=
517 alarm_forward_now(
518 &ctx->t.alarm, ctx->tintv) - 1;
519 alarm_restart(&ctx->t.alarm);
520 } else {
521 ctx->ticks +=
522 hrtimer_forward_now(&ctx->t.tmr, ctx->tintv)
523 - 1;
524 hrtimer_restart(&ctx->t.tmr);
525 }
Davide Libenzi4d672e72008-02-04 22:27:26 -0800526 }
Deepa Dinamanibff41202017-06-24 11:45:07 -0700527 t->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx));
528 t->it_interval = ktime_to_timespec64(ctx->tintv);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800529 spin_unlock_irq(&ctx->wqh.lock);
Al Viro2903ff02012-08-28 12:52:22 -0400530 fdput(f);
Al Viro9d94b9e2012-12-27 16:52:33 -0500531 return 0;
532}
Davide Libenzi4d672e72008-02-04 22:27:26 -0800533
Al Viro9d94b9e2012-12-27 16:52:33 -0500534SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
535 const struct itimerspec __user *, utmr,
536 struct itimerspec __user *, otmr)
537{
Deepa Dinamanibff41202017-06-24 11:45:07 -0700538 struct itimerspec64 new, old;
Al Viro9d94b9e2012-12-27 16:52:33 -0500539 int ret;
540
Deepa Dinamanibff41202017-06-24 11:45:07 -0700541 if (get_itimerspec64(&new, utmr))
Al Viro9d94b9e2012-12-27 16:52:33 -0500542 return -EFAULT;
543 ret = do_timerfd_settime(ufd, flags, &new, &old);
544 if (ret)
545 return ret;
Deepa Dinamanibff41202017-06-24 11:45:07 -0700546 if (otmr && put_itimerspec64(&old, otmr))
Al Viro9d94b9e2012-12-27 16:52:33 -0500547 return -EFAULT;
548
549 return ret;
550}
551
552SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct itimerspec __user *, otmr)
553{
Deepa Dinamanibff41202017-06-24 11:45:07 -0700554 struct itimerspec64 kotmr;
Al Viro9d94b9e2012-12-27 16:52:33 -0500555 int ret = do_timerfd_gettime(ufd, &kotmr);
556 if (ret)
557 return ret;
Deepa Dinamanibff41202017-06-24 11:45:07 -0700558 return put_itimerspec64(&kotmr, otmr) ? -EFAULT : 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700559}
560
Heiko Carstens0e803ba2013-03-02 12:26:30 +0100561#ifdef CONFIG_COMPAT
Al Viro9d94b9e2012-12-27 16:52:33 -0500562COMPAT_SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
Heiko Carstens0e803ba2013-03-02 12:26:30 +0100563 const struct compat_itimerspec __user *, utmr,
564 struct compat_itimerspec __user *, otmr)
Al Viro9d94b9e2012-12-27 16:52:33 -0500565{
Deepa Dinamanibff41202017-06-24 11:45:07 -0700566 struct itimerspec64 new, old;
Al Viro9d94b9e2012-12-27 16:52:33 -0500567 int ret;
568
Deepa Dinamanibff41202017-06-24 11:45:07 -0700569 if (get_compat_itimerspec64(&new, utmr))
Al Viro9d94b9e2012-12-27 16:52:33 -0500570 return -EFAULT;
571 ret = do_timerfd_settime(ufd, flags, &new, &old);
572 if (ret)
573 return ret;
Deepa Dinamanibff41202017-06-24 11:45:07 -0700574 if (otmr && put_compat_itimerspec64(&old, otmr))
Al Viro9d94b9e2012-12-27 16:52:33 -0500575 return -EFAULT;
576 return ret;
577}
578
579COMPAT_SYSCALL_DEFINE2(timerfd_gettime, int, ufd,
Heiko Carstens0e803ba2013-03-02 12:26:30 +0100580 struct compat_itimerspec __user *, otmr)
Al Viro9d94b9e2012-12-27 16:52:33 -0500581{
Deepa Dinamanibff41202017-06-24 11:45:07 -0700582 struct itimerspec64 kotmr;
Al Viro9d94b9e2012-12-27 16:52:33 -0500583 int ret = do_timerfd_gettime(ufd, &kotmr);
584 if (ret)
585 return ret;
Deepa Dinamanibff41202017-06-24 11:45:07 -0700586 return put_compat_itimerspec64(&kotmr, otmr) ? -EFAULT : 0;
Al Viro9d94b9e2012-12-27 16:52:33 -0500587}
588#endif