blob: b46ffa94372a58b600660a106a2a92545252f5f1 [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 ||
52 ctx->clockid == CLOCK_BOOTTIME_ALARM;
53}
54
Davide Libenzib215e282007-05-10 22:23:16 -070055/*
56 * This gets called when the timer event triggers. We set the "expired"
57 * flag, but we do not re-arm the timer (in case it's necessary,
Davide Libenzi4d672e72008-02-04 22:27:26 -080058 * tintv.tv64 != 0) until the timer is accessed.
Davide Libenzib215e282007-05-10 22:23:16 -070059 */
Todd Poynor11ffa9d2013-05-15 14:38:12 -070060static void timerfd_triggered(struct timerfd_ctx *ctx)
Davide Libenzib215e282007-05-10 22:23:16 -070061{
Davide Libenzib215e282007-05-10 22:23:16 -070062 unsigned long flags;
63
Davide Libenzi18963c02007-05-18 12:02:33 -070064 spin_lock_irqsave(&ctx->wqh.lock, flags);
Davide Libenzib215e282007-05-10 22:23:16 -070065 ctx->expired = 1;
Davide Libenzi4d672e72008-02-04 22:27:26 -080066 ctx->ticks++;
Davide Libenzib215e282007-05-10 22:23:16 -070067 wake_up_locked(&ctx->wqh);
Davide Libenzi18963c02007-05-18 12:02:33 -070068 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
Todd Poynor11ffa9d2013-05-15 14:38:12 -070069}
Davide Libenzib215e282007-05-10 22:23:16 -070070
Todd Poynor11ffa9d2013-05-15 14:38:12 -070071static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr)
72{
73 struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx,
74 t.tmr);
75 timerfd_triggered(ctx);
Davide Libenzib215e282007-05-10 22:23:16 -070076 return HRTIMER_NORESTART;
77}
78
Todd Poynor11ffa9d2013-05-15 14:38:12 -070079static enum alarmtimer_restart timerfd_alarmproc(struct alarm *alarm,
80 ktime_t now)
81{
82 struct timerfd_ctx *ctx = container_of(alarm, struct timerfd_ctx,
83 t.alarm);
84 timerfd_triggered(ctx);
85 return ALARMTIMER_NORESTART;
86}
87
Thomas Gleixner9ec26902011-05-20 16:18:50 +020088/*
89 * Called when the clock was set to cancel the timers in the cancel
Max Asbock1123d9392011-06-13 10:18:32 -070090 * list. This will wake up processes waiting on these timers. The
91 * wake-up requires ctx->ticks to be non zero, therefore we increment
92 * it before calling wake_up_locked().
Thomas Gleixner9ec26902011-05-20 16:18:50 +020093 */
94void timerfd_clock_was_set(void)
95{
Thomas Gleixner53cc7ba2014-07-16 21:04:23 +000096 ktime_t moffs = ktime_mono_to_real((ktime_t){ .tv64 = 0 });
Thomas Gleixner9ec26902011-05-20 16:18:50 +020097 struct timerfd_ctx *ctx;
98 unsigned long flags;
99
100 rcu_read_lock();
101 list_for_each_entry_rcu(ctx, &cancel_list, clist) {
102 if (!ctx->might_cancel)
103 continue;
104 spin_lock_irqsave(&ctx->wqh.lock, flags);
105 if (ctx->moffs.tv64 != moffs.tv64) {
106 ctx->moffs.tv64 = KTIME_MAX;
Max Asbock1123d9392011-06-13 10:18:32 -0700107 ctx->ticks++;
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200108 wake_up_locked(&ctx->wqh);
109 }
110 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
111 }
112 rcu_read_unlock();
113}
114
115static void timerfd_remove_cancel(struct timerfd_ctx *ctx)
116{
117 if (ctx->might_cancel) {
118 ctx->might_cancel = false;
119 spin_lock(&cancel_lock);
120 list_del_rcu(&ctx->clist);
121 spin_unlock(&cancel_lock);
122 }
123}
124
125static bool timerfd_canceled(struct timerfd_ctx *ctx)
126{
127 if (!ctx->might_cancel || ctx->moffs.tv64 != KTIME_MAX)
128 return false;
Thomas Gleixner53cc7ba2014-07-16 21:04:23 +0000129 ctx->moffs = ktime_mono_to_real((ktime_t){ .tv64 = 0 });
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200130 return true;
131}
132
133static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags)
134{
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700135 if ((ctx->clockid == CLOCK_REALTIME ||
136 ctx->clockid == CLOCK_REALTIME_ALARM) &&
137 (flags & TFD_TIMER_ABSTIME) && (flags & TFD_TIMER_CANCEL_ON_SET)) {
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200138 if (!ctx->might_cancel) {
139 ctx->might_cancel = true;
140 spin_lock(&cancel_lock);
141 list_add_rcu(&ctx->clist, &cancel_list);
142 spin_unlock(&cancel_lock);
143 }
144 } else if (ctx->might_cancel) {
145 timerfd_remove_cancel(ctx);
146 }
147}
148
Davide Libenzi4d672e72008-02-04 22:27:26 -0800149static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
150{
Arjan van de Ven76369472008-09-01 15:00:14 -0700151 ktime_t remaining;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800152
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700153 if (isalarm(ctx))
154 remaining = alarm_expires_remaining(&ctx->t.alarm);
155 else
156 remaining = hrtimer_expires_remaining(&ctx->t.tmr);
157
Davide Libenzi4d672e72008-02-04 22:27:26 -0800158 return remaining.tv64 < 0 ? ktime_set(0, 0): remaining;
159}
160
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200161static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
162 const struct itimerspec *ktmr)
Davide Libenzib215e282007-05-10 22:23:16 -0700163{
164 enum hrtimer_mode htmode;
165 ktime_t texp;
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200166 int clockid = ctx->clockid;
Davide Libenzib215e282007-05-10 22:23:16 -0700167
168 htmode = (flags & TFD_TIMER_ABSTIME) ?
169 HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
170
171 texp = timespec_to_ktime(ktmr->it_value);
172 ctx->expired = 0;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800173 ctx->ticks = 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700174 ctx->tintv = timespec_to_ktime(ktmr->it_interval);
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700175
176 if (isalarm(ctx)) {
177 alarm_init(&ctx->t.alarm,
178 ctx->clockid == CLOCK_REALTIME_ALARM ?
179 ALARM_REALTIME : ALARM_BOOTTIME,
180 timerfd_alarmproc);
181 } else {
182 hrtimer_init(&ctx->t.tmr, clockid, htmode);
183 hrtimer_set_expires(&ctx->t.tmr, texp);
184 ctx->t.tmr.function = timerfd_tmrproc;
185 }
186
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200187 if (texp.tv64 != 0) {
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700188 if (isalarm(ctx)) {
189 if (flags & TFD_TIMER_ABSTIME)
190 alarm_start(&ctx->t.alarm, texp);
191 else
192 alarm_start_relative(&ctx->t.alarm, texp);
193 } else {
194 hrtimer_start(&ctx->t.tmr, texp, htmode);
195 }
196
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200197 if (timerfd_canceled(ctx))
198 return -ECANCELED;
199 }
Cyrill Gorcunovaf9c4952014-07-16 01:54:52 +0400200
201 ctx->settime_flags = flags & TFD_SETTIME_FLAGS;
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200202 return 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700203}
204
205static int timerfd_release(struct inode *inode, struct file *file)
206{
207 struct timerfd_ctx *ctx = file->private_data;
208
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200209 timerfd_remove_cancel(ctx);
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700210
211 if (isalarm(ctx))
212 alarm_cancel(&ctx->t.alarm);
213 else
214 hrtimer_cancel(&ctx->t.tmr);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200215 kfree_rcu(ctx, rcu);
Davide Libenzib215e282007-05-10 22:23:16 -0700216 return 0;
217}
218
219static unsigned int timerfd_poll(struct file *file, poll_table *wait)
220{
221 struct timerfd_ctx *ctx = file->private_data;
222 unsigned int events = 0;
223 unsigned long flags;
224
225 poll_wait(file, &ctx->wqh, wait);
226
Davide Libenzi18963c02007-05-18 12:02:33 -0700227 spin_lock_irqsave(&ctx->wqh.lock, flags);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800228 if (ctx->ticks)
Davide Libenzib215e282007-05-10 22:23:16 -0700229 events |= POLLIN;
Davide Libenzi18963c02007-05-18 12:02:33 -0700230 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
Davide Libenzib215e282007-05-10 22:23:16 -0700231
232 return events;
233}
234
235static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
236 loff_t *ppos)
237{
238 struct timerfd_ctx *ctx = file->private_data;
239 ssize_t res;
Davide Libenzi09828402007-07-26 10:41:07 -0700240 u64 ticks = 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700241
242 if (count < sizeof(ticks))
243 return -EINVAL;
Davide Libenzi18963c02007-05-18 12:02:33 -0700244 spin_lock_irq(&ctx->wqh.lock);
Michal Nazarewicz8120a8a2010-05-05 12:53:12 +0200245 if (file->f_flags & O_NONBLOCK)
246 res = -EAGAIN;
247 else
248 res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200249
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200250 /*
251 * If clock has changed, we do not care about the
252 * ticks and we do not rearm the timer. Userspace must
253 * reevaluate anyway.
254 */
255 if (timerfd_canceled(ctx)) {
256 ctx->ticks = 0;
257 ctx->expired = 0;
258 res = -ECANCELED;
259 }
260
Davide Libenzi4d672e72008-02-04 22:27:26 -0800261 if (ctx->ticks) {
262 ticks = ctx->ticks;
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200263
Davide Libenzi4d672e72008-02-04 22:27:26 -0800264 if (ctx->expired && ctx->tintv.tv64) {
Davide Libenzib215e282007-05-10 22:23:16 -0700265 /*
266 * If tintv.tv64 != 0, this is a periodic timer that
267 * needs to be re-armed. We avoid doing it in the timer
268 * callback to avoid DoS attacks specifying a very
269 * short timer period.
270 */
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700271 if (isalarm(ctx)) {
272 ticks += alarm_forward_now(
273 &ctx->t.alarm, ctx->tintv) - 1;
274 alarm_restart(&ctx->t.alarm);
275 } else {
276 ticks += hrtimer_forward_now(&ctx->t.tmr,
277 ctx->tintv) - 1;
278 hrtimer_restart(&ctx->t.tmr);
279 }
Davide Libenzi4d672e72008-02-04 22:27:26 -0800280 }
281 ctx->expired = 0;
282 ctx->ticks = 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700283 }
Davide Libenzi18963c02007-05-18 12:02:33 -0700284 spin_unlock_irq(&ctx->wqh.lock);
Davide Libenzib215e282007-05-10 22:23:16 -0700285 if (ticks)
Davide Libenzi09828402007-07-26 10:41:07 -0700286 res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
Davide Libenzib215e282007-05-10 22:23:16 -0700287 return res;
288}
289
Cyrill Gorcunovaf9c4952014-07-16 01:54:52 +0400290#ifdef CONFIG_PROC_FS
291static int timerfd_show(struct seq_file *m, struct file *file)
292{
293 struct timerfd_ctx *ctx = file->private_data;
294 struct itimerspec t;
295
296 spin_lock_irq(&ctx->wqh.lock);
297 t.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
298 t.it_interval = ktime_to_timespec(ctx->tintv);
299 spin_unlock_irq(&ctx->wqh.lock);
300
301 return seq_printf(m,
302 "clockid: %d\n"
303 "ticks: %llu\n"
304 "settime flags: 0%o\n"
305 "it_value: (%llu, %llu)\n"
306 "it_interval: (%llu, %llu)\n",
307 ctx->clockid, (unsigned long long)ctx->ticks,
308 ctx->settime_flags,
309 (unsigned long long)t.it_value.tv_sec,
310 (unsigned long long)t.it_value.tv_nsec,
311 (unsigned long long)t.it_interval.tv_sec,
312 (unsigned long long)t.it_interval.tv_nsec);
313}
314#else
315#define timerfd_show NULL
316#endif
317
Cyrill Gorcunov5442e9f2014-07-16 01:54:54 +0400318#ifdef CONFIG_CHECKPOINT_RESTORE
319static long timerfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
320{
321 struct timerfd_ctx *ctx = file->private_data;
322 int ret = 0;
323
324 switch (cmd) {
325 case TFD_IOC_SET_TICKS: {
326 u64 ticks;
327
328 if (copy_from_user(&ticks, (u64 __user *)arg, sizeof(ticks)))
329 return -EFAULT;
330 if (!ticks)
331 return -EINVAL;
332
333 spin_lock_irq(&ctx->wqh.lock);
334 if (!timerfd_canceled(ctx)) {
335 ctx->ticks = ticks;
Dan Carpenter88299c92014-08-01 11:28:48 +0300336 wake_up_locked(&ctx->wqh);
Cyrill Gorcunov5442e9f2014-07-16 01:54:54 +0400337 } else
338 ret = -ECANCELED;
339 spin_unlock_irq(&ctx->wqh.lock);
340 break;
341 }
342 default:
343 ret = -ENOTTY;
344 break;
345 }
346
347 return ret;
348}
349#else
350#define timerfd_ioctl NULL
351#endif
352
Davide Libenzib215e282007-05-10 22:23:16 -0700353static const struct file_operations timerfd_fops = {
354 .release = timerfd_release,
355 .poll = timerfd_poll,
356 .read = timerfd_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200357 .llseek = noop_llseek,
Cyrill Gorcunovaf9c4952014-07-16 01:54:52 +0400358 .show_fdinfo = timerfd_show,
Cyrill Gorcunov5442e9f2014-07-16 01:54:54 +0400359 .unlocked_ioctl = timerfd_ioctl,
Davide Libenzib215e282007-05-10 22:23:16 -0700360};
361
Al Viro2903ff02012-08-28 12:52:22 -0400362static int timerfd_fget(int fd, struct fd *p)
Davide Libenzib215e282007-05-10 22:23:16 -0700363{
Al Viro2903ff02012-08-28 12:52:22 -0400364 struct fd f = fdget(fd);
365 if (!f.file)
366 return -EBADF;
367 if (f.file->f_op != &timerfd_fops) {
368 fdput(f);
369 return -EINVAL;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800370 }
Al Viro2903ff02012-08-28 12:52:22 -0400371 *p = f;
372 return 0;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800373}
374
Heiko Carstens836f92a2009-01-14 14:14:33 +0100375SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800376{
Al Viro2030a422008-02-23 06:46:49 -0500377 int ufd;
Davide Libenzib215e282007-05-10 22:23:16 -0700378 struct timerfd_ctx *ctx;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800379
Ulrich Dreppere38b36f2008-07-23 21:29:42 -0700380 /* Check the TFD_* constants for consistency. */
381 BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
382 BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
383
Davide Libenzi610d18f2009-02-18 14:48:18 -0800384 if ((flags & ~TFD_CREATE_FLAGS) ||
385 (clockid != CLOCK_MONOTONIC &&
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700386 clockid != CLOCK_REALTIME &&
387 clockid != CLOCK_REALTIME_ALARM &&
Greg Hackmann4a2378a2014-01-08 10:57:03 -0800388 clockid != CLOCK_BOOTTIME &&
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700389 clockid != CLOCK_BOOTTIME_ALARM))
Davide Libenzi4d672e72008-02-04 22:27:26 -0800390 return -EINVAL;
391
392 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
393 if (!ctx)
394 return -ENOMEM;
395
396 init_waitqueue_head(&ctx->wqh);
397 ctx->clockid = clockid;
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700398
399 if (isalarm(ctx))
400 alarm_init(&ctx->t.alarm,
401 ctx->clockid == CLOCK_REALTIME_ALARM ?
402 ALARM_REALTIME : ALARM_BOOTTIME,
403 timerfd_alarmproc);
404 else
405 hrtimer_init(&ctx->t.tmr, clockid, HRTIMER_MODE_ABS);
406
Thomas Gleixner53cc7ba2014-07-16 21:04:23 +0000407 ctx->moffs = ktime_mono_to_real((ktime_t){ .tv64 = 0 });
Davide Libenzi4d672e72008-02-04 22:27:26 -0800408
Ulrich Drepper11fcb6c2008-07-23 21:29:26 -0700409 ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx,
Roland Dreier628ff7c2009-12-18 09:41:24 -0800410 O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
Al Viro2030a422008-02-23 06:46:49 -0500411 if (ufd < 0)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800412 kfree(ctx);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800413
414 return ufd;
415}
416
Al Viro9d94b9e2012-12-27 16:52:33 -0500417static int do_timerfd_settime(int ufd, int flags,
418 const struct itimerspec *new,
419 struct itimerspec *old)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800420{
Al Viro2903ff02012-08-28 12:52:22 -0400421 struct fd f;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800422 struct timerfd_ctx *ctx;
Al Viro2903ff02012-08-28 12:52:22 -0400423 int ret;
Davide Libenzib215e282007-05-10 22:23:16 -0700424
Davide Libenzi610d18f2009-02-18 14:48:18 -0800425 if ((flags & ~TFD_SETTIME_FLAGS) ||
Al Viro9d94b9e2012-12-27 16:52:33 -0500426 !timespec_valid(&new->it_value) ||
427 !timespec_valid(&new->it_interval))
Davide Libenzib215e282007-05-10 22:23:16 -0700428 return -EINVAL;
429
Al Viro2903ff02012-08-28 12:52:22 -0400430 ret = timerfd_fget(ufd, &f);
431 if (ret)
432 return ret;
433 ctx = f.file->private_data;
Davide Libenzib215e282007-05-10 22:23:16 -0700434
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200435 timerfd_setup_cancel(ctx, flags);
436
Davide Libenzi4d672e72008-02-04 22:27:26 -0800437 /*
438 * We need to stop the existing timer before reprogramming
439 * it to the new values.
440 */
441 for (;;) {
442 spin_lock_irq(&ctx->wqh.lock);
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700443
444 if (isalarm(ctx)) {
445 if (alarm_try_to_cancel(&ctx->t.alarm) >= 0)
446 break;
447 } else {
448 if (hrtimer_try_to_cancel(&ctx->t.tmr) >= 0)
449 break;
450 }
Davide Libenzi18963c02007-05-18 12:02:33 -0700451 spin_unlock_irq(&ctx->wqh.lock);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800452 cpu_relax();
Davide Libenzib215e282007-05-10 22:23:16 -0700453 }
454
Davide Libenzi4d672e72008-02-04 22:27:26 -0800455 /*
456 * If the timer is expired and it's periodic, we need to advance it
457 * because the caller may want to know the previous expiration time.
458 * We do not update "ticks" and "expired" since the timer will be
459 * re-programmed again in the following timerfd_setup() call.
460 */
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700461 if (ctx->expired && ctx->tintv.tv64) {
462 if (isalarm(ctx))
463 alarm_forward_now(&ctx->t.alarm, ctx->tintv);
464 else
465 hrtimer_forward_now(&ctx->t.tmr, ctx->tintv);
466 }
Davide Libenzib215e282007-05-10 22:23:16 -0700467
Al Viro9d94b9e2012-12-27 16:52:33 -0500468 old->it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
469 old->it_interval = ktime_to_timespec(ctx->tintv);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800470
471 /*
472 * Re-program the timer to the new value ...
473 */
Al Viro9d94b9e2012-12-27 16:52:33 -0500474 ret = timerfd_setup(ctx, flags, new);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800475
476 spin_unlock_irq(&ctx->wqh.lock);
Al Viro2903ff02012-08-28 12:52:22 -0400477 fdput(f);
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200478 return ret;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800479}
480
Al Viro9d94b9e2012-12-27 16:52:33 -0500481static int do_timerfd_gettime(int ufd, struct itimerspec *t)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800482{
Al Viro2903ff02012-08-28 12:52:22 -0400483 struct fd f;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800484 struct timerfd_ctx *ctx;
Al Viro2903ff02012-08-28 12:52:22 -0400485 int ret = timerfd_fget(ufd, &f);
486 if (ret)
487 return ret;
488 ctx = f.file->private_data;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800489
490 spin_lock_irq(&ctx->wqh.lock);
491 if (ctx->expired && ctx->tintv.tv64) {
492 ctx->expired = 0;
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700493
494 if (isalarm(ctx)) {
495 ctx->ticks +=
496 alarm_forward_now(
497 &ctx->t.alarm, ctx->tintv) - 1;
498 alarm_restart(&ctx->t.alarm);
499 } else {
500 ctx->ticks +=
501 hrtimer_forward_now(&ctx->t.tmr, ctx->tintv)
502 - 1;
503 hrtimer_restart(&ctx->t.tmr);
504 }
Davide Libenzi4d672e72008-02-04 22:27:26 -0800505 }
Al Viro9d94b9e2012-12-27 16:52:33 -0500506 t->it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
507 t->it_interval = ktime_to_timespec(ctx->tintv);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800508 spin_unlock_irq(&ctx->wqh.lock);
Al Viro2903ff02012-08-28 12:52:22 -0400509 fdput(f);
Al Viro9d94b9e2012-12-27 16:52:33 -0500510 return 0;
511}
Davide Libenzi4d672e72008-02-04 22:27:26 -0800512
Al Viro9d94b9e2012-12-27 16:52:33 -0500513SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
514 const struct itimerspec __user *, utmr,
515 struct itimerspec __user *, otmr)
516{
517 struct itimerspec new, old;
518 int ret;
519
520 if (copy_from_user(&new, utmr, sizeof(new)))
521 return -EFAULT;
522 ret = do_timerfd_settime(ufd, flags, &new, &old);
523 if (ret)
524 return ret;
525 if (otmr && copy_to_user(otmr, &old, sizeof(old)))
526 return -EFAULT;
527
528 return ret;
529}
530
531SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct itimerspec __user *, otmr)
532{
533 struct itimerspec kotmr;
534 int ret = do_timerfd_gettime(ufd, &kotmr);
535 if (ret)
536 return ret;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800537 return copy_to_user(otmr, &kotmr, sizeof(kotmr)) ? -EFAULT: 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700538}
539
Heiko Carstens0e803ba2013-03-02 12:26:30 +0100540#ifdef CONFIG_COMPAT
Al Viro9d94b9e2012-12-27 16:52:33 -0500541COMPAT_SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
Heiko Carstens0e803ba2013-03-02 12:26:30 +0100542 const struct compat_itimerspec __user *, utmr,
543 struct compat_itimerspec __user *, otmr)
Al Viro9d94b9e2012-12-27 16:52:33 -0500544{
545 struct itimerspec new, old;
546 int ret;
547
548 if (get_compat_itimerspec(&new, utmr))
549 return -EFAULT;
550 ret = do_timerfd_settime(ufd, flags, &new, &old);
551 if (ret)
552 return ret;
553 if (otmr && put_compat_itimerspec(otmr, &old))
554 return -EFAULT;
555 return ret;
556}
557
558COMPAT_SYSCALL_DEFINE2(timerfd_gettime, int, ufd,
Heiko Carstens0e803ba2013-03-02 12:26:30 +0100559 struct compat_itimerspec __user *, otmr)
Al Viro9d94b9e2012-12-27 16:52:33 -0500560{
561 struct itimerspec kotmr;
562 int ret = do_timerfd_gettime(ufd, &kotmr);
563 if (ret)
564 return ret;
Heiko Carstens0e803ba2013-03-02 12:26:30 +0100565 return put_compat_itimerspec(otmr, &kotmr) ? -EFAULT: 0;
Al Viro9d94b9e2012-12-27 16:52:33 -0500566}
567#endif