Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 11 | #include <linux/file.h> |
| 12 | #include <linux/poll.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/fs.h> |
| 15 | #include <linux/sched.h> |
| 16 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 18 | #include <linux/list.h> |
| 19 | #include <linux/spinlock.h> |
| 20 | #include <linux/time.h> |
| 21 | #include <linux/hrtimer.h> |
| 22 | #include <linux/anon_inodes.h> |
| 23 | #include <linux/timerfd.h> |
Adrian Bunk | 45cc2b9 | 2008-04-29 00:58:59 -0700 | [diff] [blame] | 24 | #include <linux/syscalls.h> |
Al Viro | 9d94b9e | 2012-12-27 16:52:33 -0500 | [diff] [blame] | 25 | #include <linux/compat.h> |
Thomas Gleixner | 9ec2690 | 2011-05-20 16:18:50 +0200 | [diff] [blame] | 26 | #include <linux/rcupdate.h> |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 27 | |
| 28 | struct timerfd_ctx { |
| 29 | struct hrtimer tmr; |
| 30 | ktime_t tintv; |
Thomas Gleixner | 99ee531 | 2011-04-27 14:16:42 +0200 | [diff] [blame] | 31 | ktime_t moffs; |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 32 | wait_queue_head_t wqh; |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 33 | u64 ticks; |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 34 | int expired; |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 35 | int clockid; |
Thomas Gleixner | 9ec2690 | 2011-05-20 16:18:50 +0200 | [diff] [blame] | 36 | struct rcu_head rcu; |
| 37 | struct list_head clist; |
Thomas Gleixner | 99ee531 | 2011-04-27 14:16:42 +0200 | [diff] [blame] | 38 | bool might_cancel; |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
Thomas Gleixner | 9ec2690 | 2011-05-20 16:18:50 +0200 | [diff] [blame] | 41 | static LIST_HEAD(cancel_list); |
| 42 | static DEFINE_SPINLOCK(cancel_lock); |
| 43 | |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 44 | /* |
| 45 | * This gets called when the timer event triggers. We set the "expired" |
| 46 | * flag, but we do not re-arm the timer (in case it's necessary, |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 47 | * tintv.tv64 != 0) until the timer is accessed. |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 48 | */ |
| 49 | static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr) |
| 50 | { |
| 51 | struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx, tmr); |
| 52 | unsigned long flags; |
| 53 | |
Davide Libenzi | 18963c0 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 54 | spin_lock_irqsave(&ctx->wqh.lock, flags); |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 55 | ctx->expired = 1; |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 56 | ctx->ticks++; |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 57 | wake_up_locked(&ctx->wqh); |
Davide Libenzi | 18963c0 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 58 | spin_unlock_irqrestore(&ctx->wqh.lock, flags); |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 59 | |
| 60 | return HRTIMER_NORESTART; |
| 61 | } |
| 62 | |
Thomas Gleixner | 9ec2690 | 2011-05-20 16:18:50 +0200 | [diff] [blame] | 63 | /* |
| 64 | * Called when the clock was set to cancel the timers in the cancel |
Max Asbock | 1123d939 | 2011-06-13 10:18:32 -0700 | [diff] [blame] | 65 | * list. This will wake up processes waiting on these timers. The |
| 66 | * wake-up requires ctx->ticks to be non zero, therefore we increment |
| 67 | * it before calling wake_up_locked(). |
Thomas Gleixner | 9ec2690 | 2011-05-20 16:18:50 +0200 | [diff] [blame] | 68 | */ |
| 69 | void timerfd_clock_was_set(void) |
| 70 | { |
| 71 | ktime_t moffs = ktime_get_monotonic_offset(); |
| 72 | struct timerfd_ctx *ctx; |
| 73 | unsigned long flags; |
| 74 | |
| 75 | rcu_read_lock(); |
| 76 | list_for_each_entry_rcu(ctx, &cancel_list, clist) { |
| 77 | if (!ctx->might_cancel) |
| 78 | continue; |
| 79 | spin_lock_irqsave(&ctx->wqh.lock, flags); |
| 80 | if (ctx->moffs.tv64 != moffs.tv64) { |
| 81 | ctx->moffs.tv64 = KTIME_MAX; |
Max Asbock | 1123d939 | 2011-06-13 10:18:32 -0700 | [diff] [blame] | 82 | ctx->ticks++; |
Thomas Gleixner | 9ec2690 | 2011-05-20 16:18:50 +0200 | [diff] [blame] | 83 | wake_up_locked(&ctx->wqh); |
| 84 | } |
| 85 | spin_unlock_irqrestore(&ctx->wqh.lock, flags); |
| 86 | } |
| 87 | rcu_read_unlock(); |
| 88 | } |
| 89 | |
| 90 | static void timerfd_remove_cancel(struct timerfd_ctx *ctx) |
| 91 | { |
| 92 | if (ctx->might_cancel) { |
| 93 | ctx->might_cancel = false; |
| 94 | spin_lock(&cancel_lock); |
| 95 | list_del_rcu(&ctx->clist); |
| 96 | spin_unlock(&cancel_lock); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | static bool timerfd_canceled(struct timerfd_ctx *ctx) |
| 101 | { |
| 102 | if (!ctx->might_cancel || ctx->moffs.tv64 != KTIME_MAX) |
| 103 | return false; |
| 104 | ctx->moffs = ktime_get_monotonic_offset(); |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags) |
| 109 | { |
| 110 | if (ctx->clockid == CLOCK_REALTIME && (flags & TFD_TIMER_ABSTIME) && |
| 111 | (flags & TFD_TIMER_CANCEL_ON_SET)) { |
| 112 | if (!ctx->might_cancel) { |
| 113 | ctx->might_cancel = true; |
| 114 | spin_lock(&cancel_lock); |
| 115 | list_add_rcu(&ctx->clist, &cancel_list); |
| 116 | spin_unlock(&cancel_lock); |
| 117 | } |
| 118 | } else if (ctx->might_cancel) { |
| 119 | timerfd_remove_cancel(ctx); |
| 120 | } |
| 121 | } |
| 122 | |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 123 | static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx) |
| 124 | { |
Arjan van de Ven | 7636947 | 2008-09-01 15:00:14 -0700 | [diff] [blame] | 125 | ktime_t remaining; |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 126 | |
Arjan van de Ven | 7636947 | 2008-09-01 15:00:14 -0700 | [diff] [blame] | 127 | remaining = hrtimer_expires_remaining(&ctx->tmr); |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 128 | return remaining.tv64 < 0 ? ktime_set(0, 0): remaining; |
| 129 | } |
| 130 | |
Thomas Gleixner | 99ee531 | 2011-04-27 14:16:42 +0200 | [diff] [blame] | 131 | static int timerfd_setup(struct timerfd_ctx *ctx, int flags, |
| 132 | const struct itimerspec *ktmr) |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 133 | { |
| 134 | enum hrtimer_mode htmode; |
| 135 | ktime_t texp; |
Thomas Gleixner | 99ee531 | 2011-04-27 14:16:42 +0200 | [diff] [blame] | 136 | int clockid = ctx->clockid; |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 137 | |
| 138 | htmode = (flags & TFD_TIMER_ABSTIME) ? |
| 139 | HRTIMER_MODE_ABS: HRTIMER_MODE_REL; |
| 140 | |
| 141 | texp = timespec_to_ktime(ktmr->it_value); |
| 142 | ctx->expired = 0; |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 143 | ctx->ticks = 0; |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 144 | ctx->tintv = timespec_to_ktime(ktmr->it_interval); |
Thomas Gleixner | 99ee531 | 2011-04-27 14:16:42 +0200 | [diff] [blame] | 145 | hrtimer_init(&ctx->tmr, clockid, htmode); |
Arjan van de Ven | 7636947 | 2008-09-01 15:00:14 -0700 | [diff] [blame] | 146 | hrtimer_set_expires(&ctx->tmr, texp); |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 147 | ctx->tmr.function = timerfd_tmrproc; |
Thomas Gleixner | 99ee531 | 2011-04-27 14:16:42 +0200 | [diff] [blame] | 148 | if (texp.tv64 != 0) { |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 149 | hrtimer_start(&ctx->tmr, texp, htmode); |
Thomas Gleixner | 99ee531 | 2011-04-27 14:16:42 +0200 | [diff] [blame] | 150 | if (timerfd_canceled(ctx)) |
| 151 | return -ECANCELED; |
| 152 | } |
| 153 | return 0; |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | static int timerfd_release(struct inode *inode, struct file *file) |
| 157 | { |
| 158 | struct timerfd_ctx *ctx = file->private_data; |
| 159 | |
Thomas Gleixner | 9ec2690 | 2011-05-20 16:18:50 +0200 | [diff] [blame] | 160 | timerfd_remove_cancel(ctx); |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 161 | hrtimer_cancel(&ctx->tmr); |
Thomas Gleixner | 9ec2690 | 2011-05-20 16:18:50 +0200 | [diff] [blame] | 162 | kfree_rcu(ctx, rcu); |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static unsigned int timerfd_poll(struct file *file, poll_table *wait) |
| 167 | { |
| 168 | struct timerfd_ctx *ctx = file->private_data; |
| 169 | unsigned int events = 0; |
| 170 | unsigned long flags; |
| 171 | |
| 172 | poll_wait(file, &ctx->wqh, wait); |
| 173 | |
Davide Libenzi | 18963c0 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 174 | spin_lock_irqsave(&ctx->wqh.lock, flags); |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 175 | if (ctx->ticks) |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 176 | events |= POLLIN; |
Davide Libenzi | 18963c0 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 177 | spin_unlock_irqrestore(&ctx->wqh.lock, flags); |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 178 | |
| 179 | return events; |
| 180 | } |
| 181 | |
| 182 | static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count, |
| 183 | loff_t *ppos) |
| 184 | { |
| 185 | struct timerfd_ctx *ctx = file->private_data; |
| 186 | ssize_t res; |
Davide Libenzi | 0982840 | 2007-07-26 10:41:07 -0700 | [diff] [blame] | 187 | u64 ticks = 0; |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 188 | |
| 189 | if (count < sizeof(ticks)) |
| 190 | return -EINVAL; |
Davide Libenzi | 18963c0 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 191 | spin_lock_irq(&ctx->wqh.lock); |
Michal Nazarewicz | 8120a8a | 2010-05-05 12:53:12 +0200 | [diff] [blame] | 192 | if (file->f_flags & O_NONBLOCK) |
| 193 | res = -EAGAIN; |
| 194 | else |
| 195 | res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks); |
Thomas Gleixner | 99ee531 | 2011-04-27 14:16:42 +0200 | [diff] [blame] | 196 | |
Thomas Gleixner | 9ec2690 | 2011-05-20 16:18:50 +0200 | [diff] [blame] | 197 | /* |
| 198 | * If clock has changed, we do not care about the |
| 199 | * ticks and we do not rearm the timer. Userspace must |
| 200 | * reevaluate anyway. |
| 201 | */ |
| 202 | if (timerfd_canceled(ctx)) { |
| 203 | ctx->ticks = 0; |
| 204 | ctx->expired = 0; |
| 205 | res = -ECANCELED; |
| 206 | } |
| 207 | |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 208 | if (ctx->ticks) { |
| 209 | ticks = ctx->ticks; |
Thomas Gleixner | 99ee531 | 2011-04-27 14:16:42 +0200 | [diff] [blame] | 210 | |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 211 | if (ctx->expired && ctx->tintv.tv64) { |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 212 | /* |
| 213 | * If tintv.tv64 != 0, this is a periodic timer that |
| 214 | * needs to be re-armed. We avoid doing it in the timer |
| 215 | * callback to avoid DoS attacks specifying a very |
| 216 | * short timer period. |
| 217 | */ |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 218 | ticks += hrtimer_forward_now(&ctx->tmr, |
| 219 | ctx->tintv) - 1; |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 220 | hrtimer_restart(&ctx->tmr); |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 221 | } |
| 222 | ctx->expired = 0; |
| 223 | ctx->ticks = 0; |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 224 | } |
Davide Libenzi | 18963c0 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 225 | spin_unlock_irq(&ctx->wqh.lock); |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 226 | if (ticks) |
Davide Libenzi | 0982840 | 2007-07-26 10:41:07 -0700 | [diff] [blame] | 227 | res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks); |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 228 | return res; |
| 229 | } |
| 230 | |
| 231 | static const struct file_operations timerfd_fops = { |
| 232 | .release = timerfd_release, |
| 233 | .poll = timerfd_poll, |
| 234 | .read = timerfd_read, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 235 | .llseek = noop_llseek, |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 236 | }; |
| 237 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 238 | static int timerfd_fget(int fd, struct fd *p) |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 239 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 240 | struct fd f = fdget(fd); |
| 241 | if (!f.file) |
| 242 | return -EBADF; |
| 243 | if (f.file->f_op != &timerfd_fops) { |
| 244 | fdput(f); |
| 245 | return -EINVAL; |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 246 | } |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 247 | *p = f; |
| 248 | return 0; |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 249 | } |
| 250 | |
Heiko Carstens | 836f92a | 2009-01-14 14:14:33 +0100 | [diff] [blame] | 251 | SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags) |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 252 | { |
Al Viro | 2030a42 | 2008-02-23 06:46:49 -0500 | [diff] [blame] | 253 | int ufd; |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 254 | struct timerfd_ctx *ctx; |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 255 | |
Ulrich Drepper | e38b36f | 2008-07-23 21:29:42 -0700 | [diff] [blame] | 256 | /* Check the TFD_* constants for consistency. */ |
| 257 | BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC); |
| 258 | BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK); |
| 259 | |
Davide Libenzi | 610d18f | 2009-02-18 14:48:18 -0800 | [diff] [blame] | 260 | if ((flags & ~TFD_CREATE_FLAGS) || |
| 261 | (clockid != CLOCK_MONOTONIC && |
| 262 | clockid != CLOCK_REALTIME)) |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 263 | return -EINVAL; |
| 264 | |
| 265 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 266 | if (!ctx) |
| 267 | return -ENOMEM; |
| 268 | |
| 269 | init_waitqueue_head(&ctx->wqh); |
| 270 | ctx->clockid = clockid; |
| 271 | hrtimer_init(&ctx->tmr, clockid, HRTIMER_MODE_ABS); |
Thomas Gleixner | 99ee531 | 2011-04-27 14:16:42 +0200 | [diff] [blame] | 272 | ctx->moffs = ktime_get_monotonic_offset(); |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 273 | |
Ulrich Drepper | 11fcb6c | 2008-07-23 21:29:26 -0700 | [diff] [blame] | 274 | ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx, |
Roland Dreier | 628ff7c | 2009-12-18 09:41:24 -0800 | [diff] [blame] | 275 | O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS)); |
Al Viro | 2030a42 | 2008-02-23 06:46:49 -0500 | [diff] [blame] | 276 | if (ufd < 0) |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 277 | kfree(ctx); |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 278 | |
| 279 | return ufd; |
| 280 | } |
| 281 | |
Al Viro | 9d94b9e | 2012-12-27 16:52:33 -0500 | [diff] [blame] | 282 | static int do_timerfd_settime(int ufd, int flags, |
| 283 | const struct itimerspec *new, |
| 284 | struct itimerspec *old) |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 285 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 286 | struct fd f; |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 287 | struct timerfd_ctx *ctx; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 288 | int ret; |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 289 | |
Davide Libenzi | 610d18f | 2009-02-18 14:48:18 -0800 | [diff] [blame] | 290 | if ((flags & ~TFD_SETTIME_FLAGS) || |
Al Viro | 9d94b9e | 2012-12-27 16:52:33 -0500 | [diff] [blame] | 291 | !timespec_valid(&new->it_value) || |
| 292 | !timespec_valid(&new->it_interval)) |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 293 | return -EINVAL; |
| 294 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 295 | ret = timerfd_fget(ufd, &f); |
| 296 | if (ret) |
| 297 | return ret; |
| 298 | ctx = f.file->private_data; |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 299 | |
Thomas Gleixner | 9ec2690 | 2011-05-20 16:18:50 +0200 | [diff] [blame] | 300 | timerfd_setup_cancel(ctx, flags); |
| 301 | |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 302 | /* |
| 303 | * We need to stop the existing timer before reprogramming |
| 304 | * it to the new values. |
| 305 | */ |
| 306 | for (;;) { |
| 307 | spin_lock_irq(&ctx->wqh.lock); |
| 308 | if (hrtimer_try_to_cancel(&ctx->tmr) >= 0) |
| 309 | break; |
Davide Libenzi | 18963c0 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 310 | spin_unlock_irq(&ctx->wqh.lock); |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 311 | cpu_relax(); |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 314 | /* |
| 315 | * If the timer is expired and it's periodic, we need to advance it |
| 316 | * because the caller may want to know the previous expiration time. |
| 317 | * We do not update "ticks" and "expired" since the timer will be |
| 318 | * re-programmed again in the following timerfd_setup() call. |
| 319 | */ |
| 320 | if (ctx->expired && ctx->tintv.tv64) |
| 321 | hrtimer_forward_now(&ctx->tmr, ctx->tintv); |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 322 | |
Al Viro | 9d94b9e | 2012-12-27 16:52:33 -0500 | [diff] [blame] | 323 | old->it_value = ktime_to_timespec(timerfd_get_remaining(ctx)); |
| 324 | old->it_interval = ktime_to_timespec(ctx->tintv); |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 325 | |
| 326 | /* |
| 327 | * Re-program the timer to the new value ... |
| 328 | */ |
Al Viro | 9d94b9e | 2012-12-27 16:52:33 -0500 | [diff] [blame] | 329 | ret = timerfd_setup(ctx, flags, new); |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 330 | |
| 331 | spin_unlock_irq(&ctx->wqh.lock); |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 332 | fdput(f); |
Thomas Gleixner | 99ee531 | 2011-04-27 14:16:42 +0200 | [diff] [blame] | 333 | return ret; |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 334 | } |
| 335 | |
Al Viro | 9d94b9e | 2012-12-27 16:52:33 -0500 | [diff] [blame] | 336 | static int do_timerfd_gettime(int ufd, struct itimerspec *t) |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 337 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 338 | struct fd f; |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 339 | struct timerfd_ctx *ctx; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 340 | int ret = timerfd_fget(ufd, &f); |
| 341 | if (ret) |
| 342 | return ret; |
| 343 | ctx = f.file->private_data; |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 344 | |
| 345 | spin_lock_irq(&ctx->wqh.lock); |
| 346 | if (ctx->expired && ctx->tintv.tv64) { |
| 347 | ctx->expired = 0; |
| 348 | ctx->ticks += |
| 349 | hrtimer_forward_now(&ctx->tmr, ctx->tintv) - 1; |
| 350 | hrtimer_restart(&ctx->tmr); |
| 351 | } |
Al Viro | 9d94b9e | 2012-12-27 16:52:33 -0500 | [diff] [blame] | 352 | t->it_value = ktime_to_timespec(timerfd_get_remaining(ctx)); |
| 353 | t->it_interval = ktime_to_timespec(ctx->tintv); |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 354 | spin_unlock_irq(&ctx->wqh.lock); |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 355 | fdput(f); |
Al Viro | 9d94b9e | 2012-12-27 16:52:33 -0500 | [diff] [blame] | 356 | return 0; |
| 357 | } |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 358 | |
Al Viro | 9d94b9e | 2012-12-27 16:52:33 -0500 | [diff] [blame] | 359 | SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags, |
| 360 | const struct itimerspec __user *, utmr, |
| 361 | struct itimerspec __user *, otmr) |
| 362 | { |
| 363 | struct itimerspec new, old; |
| 364 | int ret; |
| 365 | |
| 366 | if (copy_from_user(&new, utmr, sizeof(new))) |
| 367 | return -EFAULT; |
| 368 | ret = do_timerfd_settime(ufd, flags, &new, &old); |
| 369 | if (ret) |
| 370 | return ret; |
| 371 | if (otmr && copy_to_user(otmr, &old, sizeof(old))) |
| 372 | return -EFAULT; |
| 373 | |
| 374 | return ret; |
| 375 | } |
| 376 | |
| 377 | SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct itimerspec __user *, otmr) |
| 378 | { |
| 379 | struct itimerspec kotmr; |
| 380 | int ret = do_timerfd_gettime(ufd, &kotmr); |
| 381 | if (ret) |
| 382 | return ret; |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 383 | return copy_to_user(otmr, &kotmr, sizeof(kotmr)) ? -EFAULT: 0; |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Heiko Carstens | 0e803ba | 2013-03-02 12:26:30 +0100 | [diff] [blame] | 386 | #ifdef CONFIG_COMPAT |
Al Viro | 9d94b9e | 2012-12-27 16:52:33 -0500 | [diff] [blame] | 387 | COMPAT_SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags, |
Heiko Carstens | 0e803ba | 2013-03-02 12:26:30 +0100 | [diff] [blame] | 388 | const struct compat_itimerspec __user *, utmr, |
| 389 | struct compat_itimerspec __user *, otmr) |
Al Viro | 9d94b9e | 2012-12-27 16:52:33 -0500 | [diff] [blame] | 390 | { |
| 391 | struct itimerspec new, old; |
| 392 | int ret; |
| 393 | |
| 394 | if (get_compat_itimerspec(&new, utmr)) |
| 395 | return -EFAULT; |
| 396 | ret = do_timerfd_settime(ufd, flags, &new, &old); |
| 397 | if (ret) |
| 398 | return ret; |
| 399 | if (otmr && put_compat_itimerspec(otmr, &old)) |
| 400 | return -EFAULT; |
| 401 | return ret; |
| 402 | } |
| 403 | |
| 404 | COMPAT_SYSCALL_DEFINE2(timerfd_gettime, int, ufd, |
Heiko Carstens | 0e803ba | 2013-03-02 12:26:30 +0100 | [diff] [blame] | 405 | struct compat_itimerspec __user *, otmr) |
Al Viro | 9d94b9e | 2012-12-27 16:52:33 -0500 | [diff] [blame] | 406 | { |
| 407 | struct itimerspec kotmr; |
| 408 | int ret = do_timerfd_gettime(ufd, &kotmr); |
| 409 | if (ret) |
| 410 | return ret; |
Heiko Carstens | 0e803ba | 2013-03-02 12:26:30 +0100 | [diff] [blame] | 411 | return put_compat_itimerspec(otmr, &kotmr) ? -EFAULT: 0; |
Al Viro | 9d94b9e | 2012-12-27 16:52:33 -0500 | [diff] [blame] | 412 | } |
| 413 | #endif |