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