blob: b938fa7776673bc7f6c09114aafa5e2c2b7aa98f [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 Gleixner99ee5312011-04-27 14:16:42 +020043 bool might_cancel;
Davide Libenzib215e282007-05-10 22:23:16 -070044};
45
Thomas Gleixner9ec26902011-05-20 16:18:50 +020046static LIST_HEAD(cancel_list);
47static DEFINE_SPINLOCK(cancel_lock);
48
Todd Poynor11ffa9d2013-05-15 14:38:12 -070049static inline bool isalarm(struct timerfd_ctx *ctx)
50{
51 return ctx->clockid == CLOCK_REALTIME_ALARM ||
Mao Jinlong2e1a4ae2016-02-17 15:21:49 +080052 ctx->clockid == CLOCK_BOOTTIME_ALARM ||
53 ctx->clockid == CLOCK_POWEROFF_ALARM;
Todd Poynor11ffa9d2013-05-15 14:38:12 -070054}
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,
Davide Libenzi4d672e72008-02-04 22:27:26 -080059 * tintv.tv64 != 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 Gleixner53cc7ba2014-07-16 21:04:23 +000097 ktime_t moffs = ktime_mono_to_real((ktime_t){ .tv64 = 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);
106 if (ctx->moffs.tv64 != moffs.tv64) {
107 ctx->moffs.tv64 = 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
116static void timerfd_remove_cancel(struct timerfd_ctx *ctx)
117{
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
126static bool timerfd_canceled(struct timerfd_ctx *ctx)
127{
128 if (!ctx->might_cancel || ctx->moffs.tv64 != KTIME_MAX)
129 return false;
Thomas Gleixner53cc7ba2014-07-16 21:04:23 +0000130 ctx->moffs = ktime_mono_to_real((ktime_t){ .tv64 = 0 });
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200131 return true;
132}
133
134static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags)
135{
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700136 if ((ctx->clockid == CLOCK_REALTIME ||
Mao Jinlong2e1a4ae2016-02-17 15:21:49 +0800137 ctx->clockid == CLOCK_REALTIME_ALARM ||
138 ctx->clockid == CLOCK_POWEROFF_ALARM) &&
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700139 (flags & TFD_TIMER_ABSTIME) && (flags & TFD_TIMER_CANCEL_ON_SET)) {
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200140 if (!ctx->might_cancel) {
141 ctx->might_cancel = true;
142 spin_lock(&cancel_lock);
143 list_add_rcu(&ctx->clist, &cancel_list);
144 spin_unlock(&cancel_lock);
145 }
146 } else if (ctx->might_cancel) {
147 timerfd_remove_cancel(ctx);
148 }
149}
150
Davide Libenzi4d672e72008-02-04 22:27:26 -0800151static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
152{
Arjan van de Ven76369472008-09-01 15:00:14 -0700153 ktime_t remaining;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800154
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700155 if (isalarm(ctx))
156 remaining = alarm_expires_remaining(&ctx->t.alarm);
157 else
Thomas Gleixnerb62526e2016-01-14 16:54:46 +0000158 remaining = hrtimer_expires_remaining_adjusted(&ctx->t.tmr);
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700159
Davide Libenzi4d672e72008-02-04 22:27:26 -0800160 return remaining.tv64 < 0 ? ktime_set(0, 0): remaining;
161}
162
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200163static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
164 const struct itimerspec *ktmr)
Davide Libenzib215e282007-05-10 22:23:16 -0700165{
166 enum hrtimer_mode htmode;
167 ktime_t texp;
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200168 int clockid = ctx->clockid;
Mao Jinlong2e1a4ae2016-02-17 15:21:49 +0800169 enum alarmtimer_type type;
Davide Libenzib215e282007-05-10 22:23:16 -0700170
171 htmode = (flags & TFD_TIMER_ABSTIME) ?
172 HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
173
174 texp = timespec_to_ktime(ktmr->it_value);
175 ctx->expired = 0;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800176 ctx->ticks = 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700177 ctx->tintv = timespec_to_ktime(ktmr->it_interval);
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700178
179 if (isalarm(ctx)) {
Mao Jinlong2e1a4ae2016-02-17 15:21:49 +0800180 type = clock2alarm(ctx->clockid);
181 alarm_init(&ctx->t.alarm, type, timerfd_alarmproc);
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700182 } else {
183 hrtimer_init(&ctx->t.tmr, clockid, htmode);
184 hrtimer_set_expires(&ctx->t.tmr, texp);
185 ctx->t.tmr.function = timerfd_tmrproc;
186 }
187
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200188 if (texp.tv64 != 0) {
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700189 if (isalarm(ctx)) {
190 if (flags & TFD_TIMER_ABSTIME)
191 alarm_start(&ctx->t.alarm, texp);
192 else
193 alarm_start_relative(&ctx->t.alarm, texp);
194 } else {
195 hrtimer_start(&ctx->t.tmr, texp, htmode);
196 }
197
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200198 if (timerfd_canceled(ctx))
199 return -ECANCELED;
200 }
Cyrill Gorcunovaf9c4952014-07-16 01:54:52 +0400201
202 ctx->settime_flags = flags & TFD_SETTIME_FLAGS;
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200203 return 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700204}
205
206static int timerfd_release(struct inode *inode, struct file *file)
207{
208 struct timerfd_ctx *ctx = file->private_data;
209
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200210 timerfd_remove_cancel(ctx);
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700211
212 if (isalarm(ctx))
213 alarm_cancel(&ctx->t.alarm);
214 else
215 hrtimer_cancel(&ctx->t.tmr);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200216 kfree_rcu(ctx, rcu);
Davide Libenzib215e282007-05-10 22:23:16 -0700217 return 0;
218}
219
220static unsigned int timerfd_poll(struct file *file, poll_table *wait)
221{
222 struct timerfd_ctx *ctx = file->private_data;
223 unsigned int events = 0;
224 unsigned long flags;
225
226 poll_wait(file, &ctx->wqh, wait);
227
Davide Libenzi18963c02007-05-18 12:02:33 -0700228 spin_lock_irqsave(&ctx->wqh.lock, flags);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800229 if (ctx->ticks)
Davide Libenzib215e282007-05-10 22:23:16 -0700230 events |= POLLIN;
Davide Libenzi18963c02007-05-18 12:02:33 -0700231 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
Davide Libenzib215e282007-05-10 22:23:16 -0700232
233 return events;
234}
235
236static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
237 loff_t *ppos)
238{
239 struct timerfd_ctx *ctx = file->private_data;
240 ssize_t res;
Davide Libenzi09828402007-07-26 10:41:07 -0700241 u64 ticks = 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700242
243 if (count < sizeof(ticks))
244 return -EINVAL;
Davide Libenzi18963c02007-05-18 12:02:33 -0700245 spin_lock_irq(&ctx->wqh.lock);
Michal Nazarewicz8120a8a2010-05-05 12:53:12 +0200246 if (file->f_flags & O_NONBLOCK)
247 res = -EAGAIN;
248 else
249 res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200250
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200251 /*
252 * If clock has changed, we do not care about the
253 * ticks and we do not rearm the timer. Userspace must
254 * reevaluate anyway.
255 */
256 if (timerfd_canceled(ctx)) {
257 ctx->ticks = 0;
258 ctx->expired = 0;
259 res = -ECANCELED;
260 }
261
Davide Libenzi4d672e72008-02-04 22:27:26 -0800262 if (ctx->ticks) {
263 ticks = ctx->ticks;
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200264
Davide Libenzi4d672e72008-02-04 22:27:26 -0800265 if (ctx->expired && ctx->tintv.tv64) {
Davide Libenzib215e282007-05-10 22:23:16 -0700266 /*
267 * If tintv.tv64 != 0, this is a periodic timer that
268 * needs to be re-armed. We avoid doing it in the timer
269 * callback to avoid DoS attacks specifying a very
270 * short timer period.
271 */
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700272 if (isalarm(ctx)) {
273 ticks += alarm_forward_now(
274 &ctx->t.alarm, ctx->tintv) - 1;
275 alarm_restart(&ctx->t.alarm);
276 } else {
277 ticks += hrtimer_forward_now(&ctx->t.tmr,
278 ctx->tintv) - 1;
279 hrtimer_restart(&ctx->t.tmr);
280 }
Davide Libenzi4d672e72008-02-04 22:27:26 -0800281 }
282 ctx->expired = 0;
283 ctx->ticks = 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700284 }
Davide Libenzi18963c02007-05-18 12:02:33 -0700285 spin_unlock_irq(&ctx->wqh.lock);
Davide Libenzib215e282007-05-10 22:23:16 -0700286 if (ticks)
Davide Libenzi09828402007-07-26 10:41:07 -0700287 res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
Davide Libenzib215e282007-05-10 22:23:16 -0700288 return res;
289}
290
Cyrill Gorcunovaf9c4952014-07-16 01:54:52 +0400291#ifdef CONFIG_PROC_FS
Joe Perchesa3816ab2014-09-29 16:08:25 -0700292static void timerfd_show(struct seq_file *m, struct file *file)
Cyrill Gorcunovaf9c4952014-07-16 01:54:52 +0400293{
294 struct timerfd_ctx *ctx = file->private_data;
295 struct itimerspec t;
296
297 spin_lock_irq(&ctx->wqh.lock);
298 t.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
299 t.it_interval = ktime_to_timespec(ctx->tintv);
300 spin_unlock_irq(&ctx->wqh.lock);
301
Joe Perchesa3816ab2014-09-29 16:08:25 -0700302 seq_printf(m,
303 "clockid: %d\n"
304 "ticks: %llu\n"
305 "settime flags: 0%o\n"
306 "it_value: (%llu, %llu)\n"
307 "it_interval: (%llu, %llu)\n",
308 ctx->clockid,
309 (unsigned long long)ctx->ticks,
310 ctx->settime_flags,
311 (unsigned long long)t.it_value.tv_sec,
312 (unsigned long long)t.it_value.tv_nsec,
313 (unsigned long long)t.it_interval.tv_sec,
314 (unsigned long long)t.it_interval.tv_nsec);
Cyrill Gorcunovaf9c4952014-07-16 01:54:52 +0400315}
316#else
317#define timerfd_show NULL
318#endif
319
Cyrill Gorcunov5442e9f2014-07-16 01:54:54 +0400320#ifdef CONFIG_CHECKPOINT_RESTORE
321static long timerfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
322{
323 struct timerfd_ctx *ctx = file->private_data;
324 int ret = 0;
325
326 switch (cmd) {
327 case TFD_IOC_SET_TICKS: {
328 u64 ticks;
329
330 if (copy_from_user(&ticks, (u64 __user *)arg, sizeof(ticks)))
331 return -EFAULT;
332 if (!ticks)
333 return -EINVAL;
334
335 spin_lock_irq(&ctx->wqh.lock);
336 if (!timerfd_canceled(ctx)) {
337 ctx->ticks = ticks;
Dan Carpenter88299c92014-08-01 11:28:48 +0300338 wake_up_locked(&ctx->wqh);
Cyrill Gorcunov5442e9f2014-07-16 01:54:54 +0400339 } else
340 ret = -ECANCELED;
341 spin_unlock_irq(&ctx->wqh.lock);
342 break;
343 }
344 default:
345 ret = -ENOTTY;
346 break;
347 }
348
349 return ret;
350}
351#else
352#define timerfd_ioctl NULL
353#endif
354
Davide Libenzib215e282007-05-10 22:23:16 -0700355static const struct file_operations timerfd_fops = {
356 .release = timerfd_release,
357 .poll = timerfd_poll,
358 .read = timerfd_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200359 .llseek = noop_llseek,
Cyrill Gorcunovaf9c4952014-07-16 01:54:52 +0400360 .show_fdinfo = timerfd_show,
Cyrill Gorcunov5442e9f2014-07-16 01:54:54 +0400361 .unlocked_ioctl = timerfd_ioctl,
Davide Libenzib215e282007-05-10 22:23:16 -0700362};
363
Al Viro2903ff02012-08-28 12:52:22 -0400364static int timerfd_fget(int fd, struct fd *p)
Davide Libenzib215e282007-05-10 22:23:16 -0700365{
Al Viro2903ff02012-08-28 12:52:22 -0400366 struct fd f = fdget(fd);
367 if (!f.file)
368 return -EBADF;
369 if (f.file->f_op != &timerfd_fops) {
370 fdput(f);
371 return -EINVAL;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800372 }
Al Viro2903ff02012-08-28 12:52:22 -0400373 *p = f;
374 return 0;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800375}
376
Heiko Carstens836f92a2009-01-14 14:14:33 +0100377SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800378{
Al Viro2030a422008-02-23 06:46:49 -0500379 int ufd;
Davide Libenzib215e282007-05-10 22:23:16 -0700380 struct timerfd_ctx *ctx;
Mao Jinlong2e1a4ae2016-02-17 15:21:49 +0800381 enum alarmtimer_type type;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800382
Ulrich Dreppere38b36f2008-07-23 21:29:42 -0700383 /* Check the TFD_* constants for consistency. */
384 BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
385 BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
386
Davide Libenzi610d18f2009-02-18 14:48:18 -0800387 if ((flags & ~TFD_CREATE_FLAGS) ||
388 (clockid != CLOCK_MONOTONIC &&
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700389 clockid != CLOCK_REALTIME &&
390 clockid != CLOCK_REALTIME_ALARM &&
Greg Hackmann4a2378a2014-01-08 10:57:03 -0800391 clockid != CLOCK_BOOTTIME &&
Mao Jinlong2e1a4ae2016-02-17 15:21:49 +0800392 clockid != CLOCK_BOOTTIME_ALARM &&
393 clockid != CLOCK_POWEROFF_ALARM))
Davide Libenzi4d672e72008-02-04 22:27:26 -0800394 return -EINVAL;
395
Eric Caruso2895a5e2016-06-08 16:08:59 -0700396 if (!capable(CAP_WAKE_ALARM) &&
397 (clockid == CLOCK_REALTIME_ALARM ||
398 clockid == CLOCK_BOOTTIME_ALARM))
399 return -EPERM;
400
Davide Libenzi4d672e72008-02-04 22:27:26 -0800401 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
402 if (!ctx)
403 return -ENOMEM;
404
405 init_waitqueue_head(&ctx->wqh);
406 ctx->clockid = clockid;
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700407
Mao Jinlong2e1a4ae2016-02-17 15:21:49 +0800408 if (isalarm(ctx)) {
409 type = clock2alarm(ctx->clockid);
410 alarm_init(&ctx->t.alarm, type, timerfd_alarmproc);
411 } else {
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700412 hrtimer_init(&ctx->t.tmr, clockid, HRTIMER_MODE_ABS);
Mao Jinlong2e1a4ae2016-02-17 15:21:49 +0800413 }
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700414
Thomas Gleixner53cc7ba2014-07-16 21:04:23 +0000415 ctx->moffs = ktime_mono_to_real((ktime_t){ .tv64 = 0 });
Davide Libenzi4d672e72008-02-04 22:27:26 -0800416
Ulrich Drepper11fcb6c2008-07-23 21:29:26 -0700417 ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx,
Roland Dreier628ff7c2009-12-18 09:41:24 -0800418 O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
Al Viro2030a422008-02-23 06:46:49 -0500419 if (ufd < 0)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800420 kfree(ctx);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800421
422 return ufd;
423}
424
Al Viro9d94b9e2012-12-27 16:52:33 -0500425static int do_timerfd_settime(int ufd, int flags,
426 const struct itimerspec *new,
427 struct itimerspec *old)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800428{
Al Viro2903ff02012-08-28 12:52:22 -0400429 struct fd f;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800430 struct timerfd_ctx *ctx;
Al Viro2903ff02012-08-28 12:52:22 -0400431 int ret;
Davide Libenzib215e282007-05-10 22:23:16 -0700432
Davide Libenzi610d18f2009-02-18 14:48:18 -0800433 if ((flags & ~TFD_SETTIME_FLAGS) ||
Al Viro9d94b9e2012-12-27 16:52:33 -0500434 !timespec_valid(&new->it_value) ||
435 !timespec_valid(&new->it_interval))
Davide Libenzib215e282007-05-10 22:23:16 -0700436 return -EINVAL;
437
Al Viro2903ff02012-08-28 12:52:22 -0400438 ret = timerfd_fget(ufd, &f);
439 if (ret)
440 return ret;
441 ctx = f.file->private_data;
Davide Libenzib215e282007-05-10 22:23:16 -0700442
Eric Caruso2895a5e2016-06-08 16:08:59 -0700443 if (!capable(CAP_WAKE_ALARM) && isalarm(ctx)) {
444 fdput(f);
445 return -EPERM;
446 }
447
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200448 timerfd_setup_cancel(ctx, flags);
449
Davide Libenzi4d672e72008-02-04 22:27:26 -0800450 /*
451 * We need to stop the existing timer before reprogramming
452 * it to the new values.
453 */
454 for (;;) {
455 spin_lock_irq(&ctx->wqh.lock);
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700456
457 if (isalarm(ctx)) {
458 if (alarm_try_to_cancel(&ctx->t.alarm) >= 0)
459 break;
460 } else {
461 if (hrtimer_try_to_cancel(&ctx->t.tmr) >= 0)
462 break;
463 }
Davide Libenzi18963c02007-05-18 12:02:33 -0700464 spin_unlock_irq(&ctx->wqh.lock);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800465 cpu_relax();
Davide Libenzib215e282007-05-10 22:23:16 -0700466 }
467
Davide Libenzi4d672e72008-02-04 22:27:26 -0800468 /*
469 * If the timer is expired and it's periodic, we need to advance it
470 * because the caller may want to know the previous expiration time.
471 * We do not update "ticks" and "expired" since the timer will be
472 * re-programmed again in the following timerfd_setup() call.
473 */
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700474 if (ctx->expired && ctx->tintv.tv64) {
475 if (isalarm(ctx))
476 alarm_forward_now(&ctx->t.alarm, ctx->tintv);
477 else
478 hrtimer_forward_now(&ctx->t.tmr, ctx->tintv);
479 }
Davide Libenzib215e282007-05-10 22:23:16 -0700480
Al Viro9d94b9e2012-12-27 16:52:33 -0500481 old->it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
482 old->it_interval = ktime_to_timespec(ctx->tintv);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800483
484 /*
485 * Re-program the timer to the new value ...
486 */
Al Viro9d94b9e2012-12-27 16:52:33 -0500487 ret = timerfd_setup(ctx, flags, new);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800488
489 spin_unlock_irq(&ctx->wqh.lock);
Mao Jinlong2e1a4ae2016-02-17 15:21:49 +0800490
491 if (ctx->clockid == CLOCK_POWEROFF_ALARM)
492 set_power_on_alarm();
493
Al Viro2903ff02012-08-28 12:52:22 -0400494 fdput(f);
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200495 return ret;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800496}
497
Al Viro9d94b9e2012-12-27 16:52:33 -0500498static int do_timerfd_gettime(int ufd, struct itimerspec *t)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800499{
Al Viro2903ff02012-08-28 12:52:22 -0400500 struct fd f;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800501 struct timerfd_ctx *ctx;
Al Viro2903ff02012-08-28 12:52:22 -0400502 int ret = timerfd_fget(ufd, &f);
503 if (ret)
504 return ret;
505 ctx = f.file->private_data;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800506
507 spin_lock_irq(&ctx->wqh.lock);
508 if (ctx->expired && ctx->tintv.tv64) {
509 ctx->expired = 0;
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700510
511 if (isalarm(ctx)) {
512 ctx->ticks +=
513 alarm_forward_now(
514 &ctx->t.alarm, ctx->tintv) - 1;
515 alarm_restart(&ctx->t.alarm);
516 } else {
517 ctx->ticks +=
518 hrtimer_forward_now(&ctx->t.tmr, ctx->tintv)
519 - 1;
520 hrtimer_restart(&ctx->t.tmr);
521 }
Davide Libenzi4d672e72008-02-04 22:27:26 -0800522 }
Al Viro9d94b9e2012-12-27 16:52:33 -0500523 t->it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
524 t->it_interval = ktime_to_timespec(ctx->tintv);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800525 spin_unlock_irq(&ctx->wqh.lock);
Al Viro2903ff02012-08-28 12:52:22 -0400526 fdput(f);
Al Viro9d94b9e2012-12-27 16:52:33 -0500527 return 0;
528}
Davide Libenzi4d672e72008-02-04 22:27:26 -0800529
Al Viro9d94b9e2012-12-27 16:52:33 -0500530SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
531 const struct itimerspec __user *, utmr,
532 struct itimerspec __user *, otmr)
533{
534 struct itimerspec new, old;
535 int ret;
536
537 if (copy_from_user(&new, utmr, sizeof(new)))
538 return -EFAULT;
539 ret = do_timerfd_settime(ufd, flags, &new, &old);
540 if (ret)
541 return ret;
542 if (otmr && copy_to_user(otmr, &old, sizeof(old)))
543 return -EFAULT;
544
545 return ret;
546}
547
548SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct itimerspec __user *, otmr)
549{
550 struct itimerspec kotmr;
551 int ret = do_timerfd_gettime(ufd, &kotmr);
552 if (ret)
553 return ret;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800554 return copy_to_user(otmr, &kotmr, sizeof(kotmr)) ? -EFAULT: 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700555}
556
Heiko Carstens0e803ba2013-03-02 12:26:30 +0100557#ifdef CONFIG_COMPAT
Al Viro9d94b9e2012-12-27 16:52:33 -0500558COMPAT_SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
Heiko Carstens0e803ba2013-03-02 12:26:30 +0100559 const struct compat_itimerspec __user *, utmr,
560 struct compat_itimerspec __user *, otmr)
Al Viro9d94b9e2012-12-27 16:52:33 -0500561{
562 struct itimerspec new, old;
563 int ret;
564
565 if (get_compat_itimerspec(&new, utmr))
566 return -EFAULT;
567 ret = do_timerfd_settime(ufd, flags, &new, &old);
568 if (ret)
569 return ret;
570 if (otmr && put_compat_itimerspec(otmr, &old))
571 return -EFAULT;
572 return ret;
573}
574
575COMPAT_SYSCALL_DEFINE2(timerfd_gettime, int, ufd,
Heiko Carstens0e803ba2013-03-02 12:26:30 +0100576 struct compat_itimerspec __user *, otmr)
Al Viro9d94b9e2012-12-27 16:52:33 -0500577{
578 struct itimerspec kotmr;
579 int ret = do_timerfd_gettime(ufd, &kotmr);
580 if (ret)
581 return ret;
Heiko Carstens0e803ba2013-03-02 12:26:30 +0100582 return put_compat_itimerspec(otmr, &kotmr) ? -EFAULT: 0;
Al Viro9d94b9e2012-12-27 16:52:33 -0500583}
584#endif