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