blob: 0013142c04759b527485f2abde83c084e17a7f8f [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 Libenzib215e282007-05-10 22:23:16 -070038 int expired;
Davide Libenzi4d672e72008-02-04 22:27:26 -080039 int clockid;
Thomas Gleixner9ec26902011-05-20 16:18:50 +020040 struct rcu_head rcu;
41 struct list_head clist;
Thomas Gleixner99ee5312011-04-27 14:16:42 +020042 bool might_cancel;
Davide Libenzib215e282007-05-10 22:23:16 -070043};
44
Thomas Gleixner9ec26902011-05-20 16:18:50 +020045static LIST_HEAD(cancel_list);
46static DEFINE_SPINLOCK(cancel_lock);
47
Todd Poynor11ffa9d2013-05-15 14:38:12 -070048static inline bool isalarm(struct timerfd_ctx *ctx)
49{
50 return ctx->clockid == CLOCK_REALTIME_ALARM ||
51 ctx->clockid == CLOCK_BOOTTIME_ALARM;
52}
53
Davide Libenzib215e282007-05-10 22:23:16 -070054/*
55 * This gets called when the timer event triggers. We set the "expired"
56 * flag, but we do not re-arm the timer (in case it's necessary,
Davide Libenzi4d672e72008-02-04 22:27:26 -080057 * tintv.tv64 != 0) until the timer is accessed.
Davide Libenzib215e282007-05-10 22:23:16 -070058 */
Todd Poynor11ffa9d2013-05-15 14:38:12 -070059static void timerfd_triggered(struct timerfd_ctx *ctx)
Davide Libenzib215e282007-05-10 22:23:16 -070060{
Davide Libenzib215e282007-05-10 22:23:16 -070061 unsigned long flags;
62
Davide Libenzi18963c02007-05-18 12:02:33 -070063 spin_lock_irqsave(&ctx->wqh.lock, flags);
Davide Libenzib215e282007-05-10 22:23:16 -070064 ctx->expired = 1;
Davide Libenzi4d672e72008-02-04 22:27:26 -080065 ctx->ticks++;
Davide Libenzib215e282007-05-10 22:23:16 -070066 wake_up_locked(&ctx->wqh);
Davide Libenzi18963c02007-05-18 12:02:33 -070067 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
Todd Poynor11ffa9d2013-05-15 14:38:12 -070068}
Davide Libenzib215e282007-05-10 22:23:16 -070069
Todd Poynor11ffa9d2013-05-15 14:38:12 -070070static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr)
71{
72 struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx,
73 t.tmr);
74 timerfd_triggered(ctx);
Davide Libenzib215e282007-05-10 22:23:16 -070075 return HRTIMER_NORESTART;
76}
77
Todd Poynor11ffa9d2013-05-15 14:38:12 -070078static enum alarmtimer_restart timerfd_alarmproc(struct alarm *alarm,
79 ktime_t now)
80{
81 struct timerfd_ctx *ctx = container_of(alarm, struct timerfd_ctx,
82 t.alarm);
83 timerfd_triggered(ctx);
84 return ALARMTIMER_NORESTART;
85}
86
Thomas Gleixner9ec26902011-05-20 16:18:50 +020087/*
88 * Called when the clock was set to cancel the timers in the cancel
Max Asbock1123d9392011-06-13 10:18:32 -070089 * list. This will wake up processes waiting on these timers. The
90 * wake-up requires ctx->ticks to be non zero, therefore we increment
91 * it before calling wake_up_locked().
Thomas Gleixner9ec26902011-05-20 16:18:50 +020092 */
93void timerfd_clock_was_set(void)
94{
95 ktime_t moffs = ktime_get_monotonic_offset();
96 struct timerfd_ctx *ctx;
97 unsigned long flags;
98
99 rcu_read_lock();
100 list_for_each_entry_rcu(ctx, &cancel_list, clist) {
101 if (!ctx->might_cancel)
102 continue;
103 spin_lock_irqsave(&ctx->wqh.lock, flags);
104 if (ctx->moffs.tv64 != moffs.tv64) {
105 ctx->moffs.tv64 = KTIME_MAX;
Max Asbock1123d9392011-06-13 10:18:32 -0700106 ctx->ticks++;
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200107 wake_up_locked(&ctx->wqh);
108 }
109 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
110 }
111 rcu_read_unlock();
112}
113
114static void timerfd_remove_cancel(struct timerfd_ctx *ctx)
115{
116 if (ctx->might_cancel) {
117 ctx->might_cancel = false;
118 spin_lock(&cancel_lock);
119 list_del_rcu(&ctx->clist);
120 spin_unlock(&cancel_lock);
121 }
122}
123
124static bool timerfd_canceled(struct timerfd_ctx *ctx)
125{
126 if (!ctx->might_cancel || ctx->moffs.tv64 != KTIME_MAX)
127 return false;
128 ctx->moffs = ktime_get_monotonic_offset();
129 return true;
130}
131
132static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags)
133{
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700134 if ((ctx->clockid == CLOCK_REALTIME ||
135 ctx->clockid == CLOCK_REALTIME_ALARM) &&
136 (flags & TFD_TIMER_ABSTIME) && (flags & TFD_TIMER_CANCEL_ON_SET)) {
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200137 if (!ctx->might_cancel) {
138 ctx->might_cancel = true;
139 spin_lock(&cancel_lock);
140 list_add_rcu(&ctx->clist, &cancel_list);
141 spin_unlock(&cancel_lock);
142 }
143 } else if (ctx->might_cancel) {
144 timerfd_remove_cancel(ctx);
145 }
146}
147
Davide Libenzi4d672e72008-02-04 22:27:26 -0800148static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
149{
Arjan van de Ven76369472008-09-01 15:00:14 -0700150 ktime_t remaining;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800151
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700152 if (isalarm(ctx))
153 remaining = alarm_expires_remaining(&ctx->t.alarm);
154 else
155 remaining = hrtimer_expires_remaining(&ctx->t.tmr);
156
Davide Libenzi4d672e72008-02-04 22:27:26 -0800157 return remaining.tv64 < 0 ? ktime_set(0, 0): remaining;
158}
159
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200160static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
161 const struct itimerspec *ktmr)
Davide Libenzib215e282007-05-10 22:23:16 -0700162{
163 enum hrtimer_mode htmode;
164 ktime_t texp;
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200165 int clockid = ctx->clockid;
Davide Libenzib215e282007-05-10 22:23:16 -0700166
167 htmode = (flags & TFD_TIMER_ABSTIME) ?
168 HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
169
170 texp = timespec_to_ktime(ktmr->it_value);
171 ctx->expired = 0;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800172 ctx->ticks = 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700173 ctx->tintv = timespec_to_ktime(ktmr->it_interval);
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700174
175 if (isalarm(ctx)) {
176 alarm_init(&ctx->t.alarm,
177 ctx->clockid == CLOCK_REALTIME_ALARM ?
178 ALARM_REALTIME : ALARM_BOOTTIME,
179 timerfd_alarmproc);
180 } else {
181 hrtimer_init(&ctx->t.tmr, clockid, htmode);
182 hrtimer_set_expires(&ctx->t.tmr, texp);
183 ctx->t.tmr.function = timerfd_tmrproc;
184 }
185
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200186 if (texp.tv64 != 0) {
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700187 if (isalarm(ctx)) {
188 if (flags & TFD_TIMER_ABSTIME)
189 alarm_start(&ctx->t.alarm, texp);
190 else
191 alarm_start_relative(&ctx->t.alarm, texp);
192 } else {
193 hrtimer_start(&ctx->t.tmr, texp, htmode);
194 }
195
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200196 if (timerfd_canceled(ctx))
197 return -ECANCELED;
198 }
199 return 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700200}
201
202static int timerfd_release(struct inode *inode, struct file *file)
203{
204 struct timerfd_ctx *ctx = file->private_data;
205
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200206 timerfd_remove_cancel(ctx);
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700207
208 if (isalarm(ctx))
209 alarm_cancel(&ctx->t.alarm);
210 else
211 hrtimer_cancel(&ctx->t.tmr);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200212 kfree_rcu(ctx, rcu);
Davide Libenzib215e282007-05-10 22:23:16 -0700213 return 0;
214}
215
216static unsigned int timerfd_poll(struct file *file, poll_table *wait)
217{
218 struct timerfd_ctx *ctx = file->private_data;
219 unsigned int events = 0;
220 unsigned long flags;
221
222 poll_wait(file, &ctx->wqh, wait);
223
Davide Libenzi18963c02007-05-18 12:02:33 -0700224 spin_lock_irqsave(&ctx->wqh.lock, flags);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800225 if (ctx->ticks)
Davide Libenzib215e282007-05-10 22:23:16 -0700226 events |= POLLIN;
Davide Libenzi18963c02007-05-18 12:02:33 -0700227 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
Davide Libenzib215e282007-05-10 22:23:16 -0700228
229 return events;
230}
231
232static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
233 loff_t *ppos)
234{
235 struct timerfd_ctx *ctx = file->private_data;
236 ssize_t res;
Davide Libenzi09828402007-07-26 10:41:07 -0700237 u64 ticks = 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700238
239 if (count < sizeof(ticks))
240 return -EINVAL;
Davide Libenzi18963c02007-05-18 12:02:33 -0700241 spin_lock_irq(&ctx->wqh.lock);
Michal Nazarewicz8120a8a2010-05-05 12:53:12 +0200242 if (file->f_flags & O_NONBLOCK)
243 res = -EAGAIN;
244 else
245 res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200246
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200247 /*
248 * If clock has changed, we do not care about the
249 * ticks and we do not rearm the timer. Userspace must
250 * reevaluate anyway.
251 */
252 if (timerfd_canceled(ctx)) {
253 ctx->ticks = 0;
254 ctx->expired = 0;
255 res = -ECANCELED;
256 }
257
Davide Libenzi4d672e72008-02-04 22:27:26 -0800258 if (ctx->ticks) {
259 ticks = ctx->ticks;
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200260
Davide Libenzi4d672e72008-02-04 22:27:26 -0800261 if (ctx->expired && ctx->tintv.tv64) {
Davide Libenzib215e282007-05-10 22:23:16 -0700262 /*
263 * If tintv.tv64 != 0, this is a periodic timer that
264 * needs to be re-armed. We avoid doing it in the timer
265 * callback to avoid DoS attacks specifying a very
266 * short timer period.
267 */
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700268 if (isalarm(ctx)) {
269 ticks += alarm_forward_now(
270 &ctx->t.alarm, ctx->tintv) - 1;
271 alarm_restart(&ctx->t.alarm);
272 } else {
273 ticks += hrtimer_forward_now(&ctx->t.tmr,
274 ctx->tintv) - 1;
275 hrtimer_restart(&ctx->t.tmr);
276 }
Davide Libenzi4d672e72008-02-04 22:27:26 -0800277 }
278 ctx->expired = 0;
279 ctx->ticks = 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700280 }
Davide Libenzi18963c02007-05-18 12:02:33 -0700281 spin_unlock_irq(&ctx->wqh.lock);
Davide Libenzib215e282007-05-10 22:23:16 -0700282 if (ticks)
Davide Libenzi09828402007-07-26 10:41:07 -0700283 res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
Davide Libenzib215e282007-05-10 22:23:16 -0700284 return res;
285}
286
287static const struct file_operations timerfd_fops = {
288 .release = timerfd_release,
289 .poll = timerfd_poll,
290 .read = timerfd_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200291 .llseek = noop_llseek,
Davide Libenzib215e282007-05-10 22:23:16 -0700292};
293
Al Viro2903ff02012-08-28 12:52:22 -0400294static int timerfd_fget(int fd, struct fd *p)
Davide Libenzib215e282007-05-10 22:23:16 -0700295{
Al Viro2903ff02012-08-28 12:52:22 -0400296 struct fd f = fdget(fd);
297 if (!f.file)
298 return -EBADF;
299 if (f.file->f_op != &timerfd_fops) {
300 fdput(f);
301 return -EINVAL;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800302 }
Al Viro2903ff02012-08-28 12:52:22 -0400303 *p = f;
304 return 0;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800305}
306
Heiko Carstens836f92a2009-01-14 14:14:33 +0100307SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800308{
Al Viro2030a422008-02-23 06:46:49 -0500309 int ufd;
Davide Libenzib215e282007-05-10 22:23:16 -0700310 struct timerfd_ctx *ctx;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800311
Ulrich Dreppere38b36f2008-07-23 21:29:42 -0700312 /* Check the TFD_* constants for consistency. */
313 BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
314 BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
315
Davide Libenzi610d18f2009-02-18 14:48:18 -0800316 if ((flags & ~TFD_CREATE_FLAGS) ||
317 (clockid != CLOCK_MONOTONIC &&
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700318 clockid != CLOCK_REALTIME &&
319 clockid != CLOCK_REALTIME_ALARM &&
Greg Hackmann4a2378a2014-01-08 10:57:03 -0800320 clockid != CLOCK_BOOTTIME &&
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700321 clockid != CLOCK_BOOTTIME_ALARM))
Davide Libenzi4d672e72008-02-04 22:27:26 -0800322 return -EINVAL;
323
324 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
325 if (!ctx)
326 return -ENOMEM;
327
328 init_waitqueue_head(&ctx->wqh);
329 ctx->clockid = clockid;
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700330
331 if (isalarm(ctx))
332 alarm_init(&ctx->t.alarm,
333 ctx->clockid == CLOCK_REALTIME_ALARM ?
334 ALARM_REALTIME : ALARM_BOOTTIME,
335 timerfd_alarmproc);
336 else
337 hrtimer_init(&ctx->t.tmr, clockid, HRTIMER_MODE_ABS);
338
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200339 ctx->moffs = ktime_get_monotonic_offset();
Davide Libenzi4d672e72008-02-04 22:27:26 -0800340
Ulrich Drepper11fcb6c2008-07-23 21:29:26 -0700341 ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx,
Roland Dreier628ff7c2009-12-18 09:41:24 -0800342 O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
Al Viro2030a422008-02-23 06:46:49 -0500343 if (ufd < 0)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800344 kfree(ctx);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800345
346 return ufd;
347}
348
Al Viro9d94b9e2012-12-27 16:52:33 -0500349static int do_timerfd_settime(int ufd, int flags,
350 const struct itimerspec *new,
351 struct itimerspec *old)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800352{
Al Viro2903ff02012-08-28 12:52:22 -0400353 struct fd f;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800354 struct timerfd_ctx *ctx;
Al Viro2903ff02012-08-28 12:52:22 -0400355 int ret;
Davide Libenzib215e282007-05-10 22:23:16 -0700356
Davide Libenzi610d18f2009-02-18 14:48:18 -0800357 if ((flags & ~TFD_SETTIME_FLAGS) ||
Al Viro9d94b9e2012-12-27 16:52:33 -0500358 !timespec_valid(&new->it_value) ||
359 !timespec_valid(&new->it_interval))
Davide Libenzib215e282007-05-10 22:23:16 -0700360 return -EINVAL;
361
Al Viro2903ff02012-08-28 12:52:22 -0400362 ret = timerfd_fget(ufd, &f);
363 if (ret)
364 return ret;
365 ctx = f.file->private_data;
Davide Libenzib215e282007-05-10 22:23:16 -0700366
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200367 timerfd_setup_cancel(ctx, flags);
368
Davide Libenzi4d672e72008-02-04 22:27:26 -0800369 /*
370 * We need to stop the existing timer before reprogramming
371 * it to the new values.
372 */
373 for (;;) {
374 spin_lock_irq(&ctx->wqh.lock);
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700375
376 if (isalarm(ctx)) {
377 if (alarm_try_to_cancel(&ctx->t.alarm) >= 0)
378 break;
379 } else {
380 if (hrtimer_try_to_cancel(&ctx->t.tmr) >= 0)
381 break;
382 }
Davide Libenzi18963c02007-05-18 12:02:33 -0700383 spin_unlock_irq(&ctx->wqh.lock);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800384 cpu_relax();
Davide Libenzib215e282007-05-10 22:23:16 -0700385 }
386
Davide Libenzi4d672e72008-02-04 22:27:26 -0800387 /*
388 * If the timer is expired and it's periodic, we need to advance it
389 * because the caller may want to know the previous expiration time.
390 * We do not update "ticks" and "expired" since the timer will be
391 * re-programmed again in the following timerfd_setup() call.
392 */
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700393 if (ctx->expired && ctx->tintv.tv64) {
394 if (isalarm(ctx))
395 alarm_forward_now(&ctx->t.alarm, ctx->tintv);
396 else
397 hrtimer_forward_now(&ctx->t.tmr, ctx->tintv);
398 }
Davide Libenzib215e282007-05-10 22:23:16 -0700399
Al Viro9d94b9e2012-12-27 16:52:33 -0500400 old->it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
401 old->it_interval = ktime_to_timespec(ctx->tintv);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800402
403 /*
404 * Re-program the timer to the new value ...
405 */
Al Viro9d94b9e2012-12-27 16:52:33 -0500406 ret = timerfd_setup(ctx, flags, new);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800407
408 spin_unlock_irq(&ctx->wqh.lock);
Al Viro2903ff02012-08-28 12:52:22 -0400409 fdput(f);
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200410 return ret;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800411}
412
Al Viro9d94b9e2012-12-27 16:52:33 -0500413static int do_timerfd_gettime(int ufd, struct itimerspec *t)
Davide Libenzi4d672e72008-02-04 22:27:26 -0800414{
Al Viro2903ff02012-08-28 12:52:22 -0400415 struct fd f;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800416 struct timerfd_ctx *ctx;
Al Viro2903ff02012-08-28 12:52:22 -0400417 int ret = timerfd_fget(ufd, &f);
418 if (ret)
419 return ret;
420 ctx = f.file->private_data;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800421
422 spin_lock_irq(&ctx->wqh.lock);
423 if (ctx->expired && ctx->tintv.tv64) {
424 ctx->expired = 0;
Todd Poynor11ffa9d2013-05-15 14:38:12 -0700425
426 if (isalarm(ctx)) {
427 ctx->ticks +=
428 alarm_forward_now(
429 &ctx->t.alarm, ctx->tintv) - 1;
430 alarm_restart(&ctx->t.alarm);
431 } else {
432 ctx->ticks +=
433 hrtimer_forward_now(&ctx->t.tmr, ctx->tintv)
434 - 1;
435 hrtimer_restart(&ctx->t.tmr);
436 }
Davide Libenzi4d672e72008-02-04 22:27:26 -0800437 }
Al Viro9d94b9e2012-12-27 16:52:33 -0500438 t->it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
439 t->it_interval = ktime_to_timespec(ctx->tintv);
Davide Libenzi4d672e72008-02-04 22:27:26 -0800440 spin_unlock_irq(&ctx->wqh.lock);
Al Viro2903ff02012-08-28 12:52:22 -0400441 fdput(f);
Al Viro9d94b9e2012-12-27 16:52:33 -0500442 return 0;
443}
Davide Libenzi4d672e72008-02-04 22:27:26 -0800444
Al Viro9d94b9e2012-12-27 16:52:33 -0500445SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
446 const struct itimerspec __user *, utmr,
447 struct itimerspec __user *, otmr)
448{
449 struct itimerspec new, old;
450 int ret;
451
452 if (copy_from_user(&new, utmr, sizeof(new)))
453 return -EFAULT;
454 ret = do_timerfd_settime(ufd, flags, &new, &old);
455 if (ret)
456 return ret;
457 if (otmr && copy_to_user(otmr, &old, sizeof(old)))
458 return -EFAULT;
459
460 return ret;
461}
462
463SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct itimerspec __user *, otmr)
464{
465 struct itimerspec kotmr;
466 int ret = do_timerfd_gettime(ufd, &kotmr);
467 if (ret)
468 return ret;
Davide Libenzi4d672e72008-02-04 22:27:26 -0800469 return copy_to_user(otmr, &kotmr, sizeof(kotmr)) ? -EFAULT: 0;
Davide Libenzib215e282007-05-10 22:23:16 -0700470}
471
Heiko Carstens0e803ba2013-03-02 12:26:30 +0100472#ifdef CONFIG_COMPAT
Al Viro9d94b9e2012-12-27 16:52:33 -0500473COMPAT_SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
Heiko Carstens0e803ba2013-03-02 12:26:30 +0100474 const struct compat_itimerspec __user *, utmr,
475 struct compat_itimerspec __user *, otmr)
Al Viro9d94b9e2012-12-27 16:52:33 -0500476{
477 struct itimerspec new, old;
478 int ret;
479
480 if (get_compat_itimerspec(&new, utmr))
481 return -EFAULT;
482 ret = do_timerfd_settime(ufd, flags, &new, &old);
483 if (ret)
484 return ret;
485 if (otmr && put_compat_itimerspec(otmr, &old))
486 return -EFAULT;
487 return ret;
488}
489
490COMPAT_SYSCALL_DEFINE2(timerfd_gettime, int, ufd,
Heiko Carstens0e803ba2013-03-02 12:26:30 +0100491 struct compat_itimerspec __user *, otmr)
Al Viro9d94b9e2012-12-27 16:52:33 -0500492{
493 struct itimerspec kotmr;
494 int ret = do_timerfd_gettime(ufd, &kotmr);
495 if (ret)
496 return ret;
Heiko Carstens0e803ba2013-03-02 12:26:30 +0100497 return put_compat_itimerspec(otmr, &kotmr) ? -EFAULT: 0;
Al Viro9d94b9e2012-12-27 16:52:33 -0500498}
499#endif