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