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> |
| 17 | #include <linux/list.h> |
| 18 | #include <linux/spinlock.h> |
| 19 | #include <linux/time.h> |
| 20 | #include <linux/hrtimer.h> |
| 21 | #include <linux/anon_inodes.h> |
| 22 | #include <linux/timerfd.h> |
Adrian Bunk | 45cc2b9 | 2008-04-29 00:58:59 -0700 | [diff] [blame] | 23 | #include <linux/syscalls.h> |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 24 | |
| 25 | struct timerfd_ctx { |
| 26 | struct hrtimer tmr; |
| 27 | ktime_t tintv; |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 28 | wait_queue_head_t wqh; |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 29 | u64 ticks; |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 30 | int expired; |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 31 | int clockid; |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | /* |
| 35 | * This gets called when the timer event triggers. We set the "expired" |
| 36 | * 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] | 37 | * tintv.tv64 != 0) until the timer is accessed. |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 38 | */ |
| 39 | static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr) |
| 40 | { |
| 41 | struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx, tmr); |
| 42 | unsigned long flags; |
| 43 | |
Davide Libenzi | 18963c0 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 44 | spin_lock_irqsave(&ctx->wqh.lock, flags); |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 45 | ctx->expired = 1; |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 46 | ctx->ticks++; |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 47 | wake_up_locked(&ctx->wqh); |
Davide Libenzi | 18963c0 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 48 | spin_unlock_irqrestore(&ctx->wqh.lock, flags); |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 49 | |
| 50 | return HRTIMER_NORESTART; |
| 51 | } |
| 52 | |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 53 | static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx) |
| 54 | { |
Arjan van de Ven | 7636947 | 2008-09-01 15:00:14 -0700 | [diff] [blame] | 55 | ktime_t remaining; |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 56 | |
Arjan van de Ven | 7636947 | 2008-09-01 15:00:14 -0700 | [diff] [blame] | 57 | remaining = hrtimer_expires_remaining(&ctx->tmr); |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 58 | return remaining.tv64 < 0 ? ktime_set(0, 0): remaining; |
| 59 | } |
| 60 | |
| 61 | static void timerfd_setup(struct timerfd_ctx *ctx, int flags, |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 62 | const struct itimerspec *ktmr) |
| 63 | { |
| 64 | enum hrtimer_mode htmode; |
| 65 | ktime_t texp; |
| 66 | |
| 67 | htmode = (flags & TFD_TIMER_ABSTIME) ? |
| 68 | HRTIMER_MODE_ABS: HRTIMER_MODE_REL; |
| 69 | |
| 70 | texp = timespec_to_ktime(ktmr->it_value); |
| 71 | ctx->expired = 0; |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 72 | ctx->ticks = 0; |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 73 | ctx->tintv = timespec_to_ktime(ktmr->it_interval); |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 74 | hrtimer_init(&ctx->tmr, ctx->clockid, htmode); |
Arjan van de Ven | 7636947 | 2008-09-01 15:00:14 -0700 | [diff] [blame] | 75 | hrtimer_set_expires(&ctx->tmr, texp); |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 76 | ctx->tmr.function = timerfd_tmrproc; |
| 77 | if (texp.tv64 != 0) |
| 78 | hrtimer_start(&ctx->tmr, texp, htmode); |
| 79 | } |
| 80 | |
| 81 | static int timerfd_release(struct inode *inode, struct file *file) |
| 82 | { |
| 83 | struct timerfd_ctx *ctx = file->private_data; |
| 84 | |
| 85 | hrtimer_cancel(&ctx->tmr); |
| 86 | kfree(ctx); |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | static unsigned int timerfd_poll(struct file *file, poll_table *wait) |
| 91 | { |
| 92 | struct timerfd_ctx *ctx = file->private_data; |
| 93 | unsigned int events = 0; |
| 94 | unsigned long flags; |
| 95 | |
| 96 | poll_wait(file, &ctx->wqh, wait); |
| 97 | |
Davide Libenzi | 18963c0 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 98 | spin_lock_irqsave(&ctx->wqh.lock, flags); |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 99 | if (ctx->ticks) |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 100 | events |= POLLIN; |
Davide Libenzi | 18963c0 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 101 | spin_unlock_irqrestore(&ctx->wqh.lock, flags); |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 102 | |
| 103 | return events; |
| 104 | } |
| 105 | |
| 106 | static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count, |
| 107 | loff_t *ppos) |
| 108 | { |
| 109 | struct timerfd_ctx *ctx = file->private_data; |
| 110 | ssize_t res; |
Davide Libenzi | 0982840 | 2007-07-26 10:41:07 -0700 | [diff] [blame] | 111 | u64 ticks = 0; |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 112 | DECLARE_WAITQUEUE(wait, current); |
| 113 | |
| 114 | if (count < sizeof(ticks)) |
| 115 | return -EINVAL; |
Davide Libenzi | 18963c0 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 116 | spin_lock_irq(&ctx->wqh.lock); |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 117 | res = -EAGAIN; |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 118 | if (!ctx->ticks && !(file->f_flags & O_NONBLOCK)) { |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 119 | __add_wait_queue(&ctx->wqh, &wait); |
| 120 | for (res = 0;;) { |
| 121 | set_current_state(TASK_INTERRUPTIBLE); |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 122 | if (ctx->ticks) { |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 123 | res = 0; |
| 124 | break; |
| 125 | } |
| 126 | if (signal_pending(current)) { |
| 127 | res = -ERESTARTSYS; |
| 128 | break; |
| 129 | } |
Davide Libenzi | 18963c0 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 130 | spin_unlock_irq(&ctx->wqh.lock); |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 131 | schedule(); |
Davide Libenzi | 18963c0 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 132 | spin_lock_irq(&ctx->wqh.lock); |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 133 | } |
| 134 | __remove_wait_queue(&ctx->wqh, &wait); |
| 135 | __set_current_state(TASK_RUNNING); |
| 136 | } |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 137 | if (ctx->ticks) { |
| 138 | ticks = ctx->ticks; |
| 139 | if (ctx->expired && ctx->tintv.tv64) { |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 140 | /* |
| 141 | * If tintv.tv64 != 0, this is a periodic timer that |
| 142 | * needs to be re-armed. We avoid doing it in the timer |
| 143 | * callback to avoid DoS attacks specifying a very |
| 144 | * short timer period. |
| 145 | */ |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 146 | ticks += hrtimer_forward_now(&ctx->tmr, |
| 147 | ctx->tintv) - 1; |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 148 | hrtimer_restart(&ctx->tmr); |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 149 | } |
| 150 | ctx->expired = 0; |
| 151 | ctx->ticks = 0; |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 152 | } |
Davide Libenzi | 18963c0 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 153 | spin_unlock_irq(&ctx->wqh.lock); |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 154 | if (ticks) |
Davide Libenzi | 0982840 | 2007-07-26 10:41:07 -0700 | [diff] [blame] | 155 | res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks); |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 156 | return res; |
| 157 | } |
| 158 | |
| 159 | static const struct file_operations timerfd_fops = { |
| 160 | .release = timerfd_release, |
| 161 | .poll = timerfd_poll, |
| 162 | .read = timerfd_read, |
| 163 | }; |
| 164 | |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 165 | static struct file *timerfd_fget(int fd) |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 166 | { |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 167 | struct file *file; |
| 168 | |
| 169 | file = fget(fd); |
| 170 | if (!file) |
| 171 | return ERR_PTR(-EBADF); |
| 172 | if (file->f_op != &timerfd_fops) { |
| 173 | fput(file); |
| 174 | return ERR_PTR(-EINVAL); |
| 175 | } |
| 176 | |
| 177 | return file; |
| 178 | } |
| 179 | |
Heiko Carstens | 836f92a | 2009-01-14 14:14:33 +0100 | [diff] [blame] | 180 | SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags) |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 181 | { |
Al Viro | 2030a42 | 2008-02-23 06:46:49 -0500 | [diff] [blame] | 182 | int ufd; |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 183 | struct timerfd_ctx *ctx; |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 184 | |
Ulrich Drepper | e38b36f | 2008-07-23 21:29:42 -0700 | [diff] [blame] | 185 | /* Check the TFD_* constants for consistency. */ |
| 186 | BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC); |
| 187 | BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK); |
| 188 | |
Davide Libenzi | 610d18f | 2009-02-18 14:48:18 -0800 | [diff] [blame] | 189 | if ((flags & ~TFD_CREATE_FLAGS) || |
| 190 | (clockid != CLOCK_MONOTONIC && |
| 191 | clockid != CLOCK_REALTIME)) |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 192 | return -EINVAL; |
| 193 | |
| 194 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 195 | if (!ctx) |
| 196 | return -ENOMEM; |
| 197 | |
| 198 | init_waitqueue_head(&ctx->wqh); |
| 199 | ctx->clockid = clockid; |
| 200 | hrtimer_init(&ctx->tmr, clockid, HRTIMER_MODE_ABS); |
| 201 | |
Ulrich Drepper | 11fcb6c | 2008-07-23 21:29:26 -0700 | [diff] [blame] | 202 | ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx, |
Davide Libenzi | 610d18f | 2009-02-18 14:48:18 -0800 | [diff] [blame] | 203 | flags & TFD_SHARED_FCNTL_FLAGS); |
Al Viro | 2030a42 | 2008-02-23 06:46:49 -0500 | [diff] [blame] | 204 | if (ufd < 0) |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 205 | kfree(ctx); |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 206 | |
| 207 | return ufd; |
| 208 | } |
| 209 | |
Heiko Carstens | 836f92a | 2009-01-14 14:14:33 +0100 | [diff] [blame] | 210 | SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags, |
| 211 | const struct itimerspec __user *, utmr, |
| 212 | struct itimerspec __user *, otmr) |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 213 | { |
| 214 | struct file *file; |
| 215 | struct timerfd_ctx *ctx; |
| 216 | struct itimerspec ktmr, kotmr; |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 217 | |
| 218 | if (copy_from_user(&ktmr, utmr, sizeof(ktmr))) |
| 219 | return -EFAULT; |
| 220 | |
Davide Libenzi | 610d18f | 2009-02-18 14:48:18 -0800 | [diff] [blame] | 221 | if ((flags & ~TFD_SETTIME_FLAGS) || |
| 222 | !timespec_valid(&ktmr.it_value) || |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 223 | !timespec_valid(&ktmr.it_interval)) |
| 224 | return -EINVAL; |
| 225 | |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 226 | file = timerfd_fget(ufd); |
| 227 | if (IS_ERR(file)) |
| 228 | return PTR_ERR(file); |
| 229 | ctx = file->private_data; |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 230 | |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 231 | /* |
| 232 | * We need to stop the existing timer before reprogramming |
| 233 | * it to the new values. |
| 234 | */ |
| 235 | for (;;) { |
| 236 | spin_lock_irq(&ctx->wqh.lock); |
| 237 | if (hrtimer_try_to_cancel(&ctx->tmr) >= 0) |
| 238 | break; |
Davide Libenzi | 18963c0 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 239 | spin_unlock_irq(&ctx->wqh.lock); |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 240 | cpu_relax(); |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 243 | /* |
| 244 | * If the timer is expired and it's periodic, we need to advance it |
| 245 | * because the caller may want to know the previous expiration time. |
| 246 | * We do not update "ticks" and "expired" since the timer will be |
| 247 | * re-programmed again in the following timerfd_setup() call. |
| 248 | */ |
| 249 | if (ctx->expired && ctx->tintv.tv64) |
| 250 | hrtimer_forward_now(&ctx->tmr, ctx->tintv); |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 251 | |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 252 | kotmr.it_value = ktime_to_timespec(timerfd_get_remaining(ctx)); |
| 253 | kotmr.it_interval = ktime_to_timespec(ctx->tintv); |
| 254 | |
| 255 | /* |
| 256 | * Re-program the timer to the new value ... |
| 257 | */ |
| 258 | timerfd_setup(ctx, flags, &ktmr); |
| 259 | |
| 260 | spin_unlock_irq(&ctx->wqh.lock); |
| 261 | fput(file); |
| 262 | if (otmr && copy_to_user(otmr, &kotmr, sizeof(kotmr))) |
| 263 | return -EFAULT; |
| 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
Heiko Carstens | d4e8204 | 2009-01-14 14:14:34 +0100 | [diff] [blame] | 268 | SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct itimerspec __user *, otmr) |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 269 | { |
| 270 | struct file *file; |
| 271 | struct timerfd_ctx *ctx; |
| 272 | struct itimerspec kotmr; |
| 273 | |
| 274 | file = timerfd_fget(ufd); |
| 275 | if (IS_ERR(file)) |
| 276 | return PTR_ERR(file); |
| 277 | ctx = file->private_data; |
| 278 | |
| 279 | spin_lock_irq(&ctx->wqh.lock); |
| 280 | if (ctx->expired && ctx->tintv.tv64) { |
| 281 | ctx->expired = 0; |
| 282 | ctx->ticks += |
| 283 | hrtimer_forward_now(&ctx->tmr, ctx->tintv) - 1; |
| 284 | hrtimer_restart(&ctx->tmr); |
| 285 | } |
| 286 | kotmr.it_value = ktime_to_timespec(timerfd_get_remaining(ctx)); |
| 287 | kotmr.it_interval = ktime_to_timespec(ctx->tintv); |
| 288 | spin_unlock_irq(&ctx->wqh.lock); |
| 289 | fput(file); |
| 290 | |
| 291 | return copy_to_user(otmr, &kotmr, sizeof(kotmr)) ? -EFAULT: 0; |
Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 292 | } |
| 293 | |