Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * fs/eventfd.c |
| 3 | * |
| 4 | * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org> |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <linux/file.h> |
| 9 | #include <linux/poll.h> |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/fs.h> |
| 12 | #include <linux/sched.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/list.h> |
| 15 | #include <linux/spinlock.h> |
| 16 | #include <linux/anon_inodes.h> |
Adrian Bunk | 7747cdb | 2008-02-06 01:36:49 -0800 | [diff] [blame] | 17 | #include <linux/syscalls.h> |
Rusty Russell | 5718607 | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 18 | #include <linux/module.h> |
Davide Libenzi | 1338901 | 2009-06-30 11:41:11 -0700 | [diff] [blame] | 19 | #include <linux/kref.h> |
| 20 | #include <linux/eventfd.h> |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 21 | |
| 22 | struct eventfd_ctx { |
Davide Libenzi | 1338901 | 2009-06-30 11:41:11 -0700 | [diff] [blame] | 23 | struct kref kref; |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 24 | wait_queue_head_t wqh; |
| 25 | /* |
| 26 | * Every time that a write(2) is performed on an eventfd, the |
| 27 | * value of the __u64 being written is added to "count" and a |
| 28 | * wakeup is performed on "wqh". A read(2) will return the "count" |
| 29 | * value to userspace, and will reset "count" to zero. The kernel |
Davide Libenzi | 1338901 | 2009-06-30 11:41:11 -0700 | [diff] [blame] | 30 | * side eventfd_signal() also, adds to the "count" counter and |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 31 | * issue a wakeup. |
| 32 | */ |
| 33 | __u64 count; |
Davide Libenzi | bcd0b23 | 2009-03-31 15:24:18 -0700 | [diff] [blame] | 34 | unsigned int flags; |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 35 | }; |
| 36 | |
Davide Libenzi | 1338901 | 2009-06-30 11:41:11 -0700 | [diff] [blame] | 37 | /** |
| 38 | * eventfd_signal - Adds @n to the eventfd counter. |
| 39 | * @ctx: [in] Pointer to the eventfd context. |
| 40 | * @n: [in] Value of the counter to be added to the eventfd internal counter. |
| 41 | * The value cannot be negative. |
| 42 | * |
| 43 | * This function is supposed to be called by the kernel in paths that do not |
| 44 | * allow sleeping. In this function we allow the counter to reach the ULLONG_MAX |
| 45 | * value, and we signal this as overflow condition by returining a POLLERR |
| 46 | * to poll(2). |
| 47 | * |
| 48 | * Returns @n in case of success, a non-negative number lower than @n in case |
| 49 | * of overflow, or the following error codes: |
| 50 | * |
| 51 | * -EINVAL : The value of @n is negative. |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 52 | */ |
Davide Libenzi | 1338901 | 2009-06-30 11:41:11 -0700 | [diff] [blame] | 53 | int eventfd_signal(struct eventfd_ctx *ctx, int n) |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 54 | { |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 55 | unsigned long flags; |
| 56 | |
| 57 | if (n < 0) |
| 58 | return -EINVAL; |
Davide Libenzi | d48eb23 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 59 | spin_lock_irqsave(&ctx->wqh.lock, flags); |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 60 | if (ULLONG_MAX - ctx->count < n) |
| 61 | n = (int) (ULLONG_MAX - ctx->count); |
| 62 | ctx->count += n; |
| 63 | if (waitqueue_active(&ctx->wqh)) |
Davide Libenzi | 3951088 | 2009-03-31 15:24:23 -0700 | [diff] [blame] | 64 | wake_up_locked_poll(&ctx->wqh, POLLIN); |
Davide Libenzi | d48eb23 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 65 | spin_unlock_irqrestore(&ctx->wqh.lock, flags); |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 66 | |
| 67 | return n; |
| 68 | } |
Rusty Russell | 5718607 | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 69 | EXPORT_SYMBOL_GPL(eventfd_signal); |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 70 | |
Davide Libenzi | 1338901 | 2009-06-30 11:41:11 -0700 | [diff] [blame] | 71 | static void eventfd_free(struct kref *kref) |
| 72 | { |
| 73 | struct eventfd_ctx *ctx = container_of(kref, struct eventfd_ctx, kref); |
| 74 | |
| 75 | kfree(ctx); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * eventfd_ctx_get - Acquires a reference to the internal eventfd context. |
| 80 | * @ctx: [in] Pointer to the eventfd context. |
| 81 | * |
| 82 | * Returns: In case of success, returns a pointer to the eventfd context. |
| 83 | */ |
| 84 | struct eventfd_ctx *eventfd_ctx_get(struct eventfd_ctx *ctx) |
| 85 | { |
| 86 | kref_get(&ctx->kref); |
| 87 | return ctx; |
| 88 | } |
| 89 | EXPORT_SYMBOL_GPL(eventfd_ctx_get); |
| 90 | |
| 91 | /** |
| 92 | * eventfd_ctx_put - Releases a reference to the internal eventfd context. |
| 93 | * @ctx: [in] Pointer to eventfd context. |
| 94 | * |
| 95 | * The eventfd context reference must have been previously acquired either |
| 96 | * with eventfd_ctx_get() or eventfd_ctx_fdget()). |
| 97 | */ |
| 98 | void eventfd_ctx_put(struct eventfd_ctx *ctx) |
| 99 | { |
| 100 | kref_put(&ctx->kref, eventfd_free); |
| 101 | } |
| 102 | EXPORT_SYMBOL_GPL(eventfd_ctx_put); |
| 103 | |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 104 | static int eventfd_release(struct inode *inode, struct file *file) |
| 105 | { |
Davide Libenzi | 1338901 | 2009-06-30 11:41:11 -0700 | [diff] [blame] | 106 | struct eventfd_ctx *ctx = file->private_data; |
| 107 | |
| 108 | wake_up_poll(&ctx->wqh, POLLHUP); |
| 109 | eventfd_ctx_put(ctx); |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static unsigned int eventfd_poll(struct file *file, poll_table *wait) |
| 114 | { |
| 115 | struct eventfd_ctx *ctx = file->private_data; |
| 116 | unsigned int events = 0; |
| 117 | unsigned long flags; |
| 118 | |
| 119 | poll_wait(file, &ctx->wqh, wait); |
| 120 | |
Davide Libenzi | d48eb23 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 121 | spin_lock_irqsave(&ctx->wqh.lock, flags); |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 122 | if (ctx->count > 0) |
| 123 | events |= POLLIN; |
| 124 | if (ctx->count == ULLONG_MAX) |
| 125 | events |= POLLERR; |
| 126 | if (ULLONG_MAX - 1 > ctx->count) |
| 127 | events |= POLLOUT; |
Davide Libenzi | d48eb23 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 128 | spin_unlock_irqrestore(&ctx->wqh.lock, flags); |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 129 | |
| 130 | return events; |
| 131 | } |
| 132 | |
| 133 | static ssize_t eventfd_read(struct file *file, char __user *buf, size_t count, |
| 134 | loff_t *ppos) |
| 135 | { |
| 136 | struct eventfd_ctx *ctx = file->private_data; |
| 137 | ssize_t res; |
Davide Libenzi | bcd0b23 | 2009-03-31 15:24:18 -0700 | [diff] [blame] | 138 | __u64 ucnt = 0; |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 139 | DECLARE_WAITQUEUE(wait, current); |
| 140 | |
| 141 | if (count < sizeof(ucnt)) |
| 142 | return -EINVAL; |
Davide Libenzi | d48eb23 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 143 | spin_lock_irq(&ctx->wqh.lock); |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 144 | res = -EAGAIN; |
Davide Libenzi | bcd0b23 | 2009-03-31 15:24:18 -0700 | [diff] [blame] | 145 | if (ctx->count > 0) |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 146 | res = sizeof(ucnt); |
| 147 | else if (!(file->f_flags & O_NONBLOCK)) { |
| 148 | __add_wait_queue(&ctx->wqh, &wait); |
| 149 | for (res = 0;;) { |
| 150 | set_current_state(TASK_INTERRUPTIBLE); |
| 151 | if (ctx->count > 0) { |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 152 | res = sizeof(ucnt); |
| 153 | break; |
| 154 | } |
| 155 | if (signal_pending(current)) { |
| 156 | res = -ERESTARTSYS; |
| 157 | break; |
| 158 | } |
Davide Libenzi | d48eb23 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 159 | spin_unlock_irq(&ctx->wqh.lock); |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 160 | schedule(); |
Davide Libenzi | d48eb23 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 161 | spin_lock_irq(&ctx->wqh.lock); |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 162 | } |
| 163 | __remove_wait_queue(&ctx->wqh, &wait); |
| 164 | __set_current_state(TASK_RUNNING); |
| 165 | } |
Davide Libenzi | bcd0b23 | 2009-03-31 15:24:18 -0700 | [diff] [blame] | 166 | if (likely(res > 0)) { |
| 167 | ucnt = (ctx->flags & EFD_SEMAPHORE) ? 1 : ctx->count; |
| 168 | ctx->count -= ucnt; |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 169 | if (waitqueue_active(&ctx->wqh)) |
Davide Libenzi | 3951088 | 2009-03-31 15:24:23 -0700 | [diff] [blame] | 170 | wake_up_locked_poll(&ctx->wqh, POLLOUT); |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 171 | } |
Davide Libenzi | d48eb23 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 172 | spin_unlock_irq(&ctx->wqh.lock); |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 173 | if (res > 0 && put_user(ucnt, (__u64 __user *) buf)) |
| 174 | return -EFAULT; |
| 175 | |
| 176 | return res; |
| 177 | } |
| 178 | |
| 179 | static ssize_t eventfd_write(struct file *file, const char __user *buf, size_t count, |
| 180 | loff_t *ppos) |
| 181 | { |
| 182 | struct eventfd_ctx *ctx = file->private_data; |
| 183 | ssize_t res; |
| 184 | __u64 ucnt; |
| 185 | DECLARE_WAITQUEUE(wait, current); |
| 186 | |
| 187 | if (count < sizeof(ucnt)) |
| 188 | return -EINVAL; |
| 189 | if (copy_from_user(&ucnt, buf, sizeof(ucnt))) |
| 190 | return -EFAULT; |
| 191 | if (ucnt == ULLONG_MAX) |
| 192 | return -EINVAL; |
Davide Libenzi | d48eb23 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 193 | spin_lock_irq(&ctx->wqh.lock); |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 194 | res = -EAGAIN; |
| 195 | if (ULLONG_MAX - ctx->count > ucnt) |
| 196 | res = sizeof(ucnt); |
| 197 | else if (!(file->f_flags & O_NONBLOCK)) { |
| 198 | __add_wait_queue(&ctx->wqh, &wait); |
| 199 | for (res = 0;;) { |
| 200 | set_current_state(TASK_INTERRUPTIBLE); |
| 201 | if (ULLONG_MAX - ctx->count > ucnt) { |
| 202 | res = sizeof(ucnt); |
| 203 | break; |
| 204 | } |
| 205 | if (signal_pending(current)) { |
| 206 | res = -ERESTARTSYS; |
| 207 | break; |
| 208 | } |
Davide Libenzi | d48eb23 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 209 | spin_unlock_irq(&ctx->wqh.lock); |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 210 | schedule(); |
Davide Libenzi | d48eb23 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 211 | spin_lock_irq(&ctx->wqh.lock); |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 212 | } |
| 213 | __remove_wait_queue(&ctx->wqh, &wait); |
| 214 | __set_current_state(TASK_RUNNING); |
| 215 | } |
Davide Libenzi | bcd0b23 | 2009-03-31 15:24:18 -0700 | [diff] [blame] | 216 | if (likely(res > 0)) { |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 217 | ctx->count += ucnt; |
| 218 | if (waitqueue_active(&ctx->wqh)) |
Davide Libenzi | 3951088 | 2009-03-31 15:24:23 -0700 | [diff] [blame] | 219 | wake_up_locked_poll(&ctx->wqh, POLLIN); |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 220 | } |
Davide Libenzi | d48eb23 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 221 | spin_unlock_irq(&ctx->wqh.lock); |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 222 | |
| 223 | return res; |
| 224 | } |
| 225 | |
| 226 | static const struct file_operations eventfd_fops = { |
| 227 | .release = eventfd_release, |
| 228 | .poll = eventfd_poll, |
| 229 | .read = eventfd_read, |
| 230 | .write = eventfd_write, |
| 231 | }; |
| 232 | |
Davide Libenzi | 1338901 | 2009-06-30 11:41:11 -0700 | [diff] [blame] | 233 | /** |
| 234 | * eventfd_fget - Acquire a reference of an eventfd file descriptor. |
| 235 | * @fd: [in] Eventfd file descriptor. |
| 236 | * |
| 237 | * Returns a pointer to the eventfd file structure in case of success, or the |
| 238 | * following error pointer: |
| 239 | * |
| 240 | * -EBADF : Invalid @fd file descriptor. |
| 241 | * -EINVAL : The @fd file descriptor is not an eventfd file. |
| 242 | */ |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 243 | struct file *eventfd_fget(int fd) |
| 244 | { |
| 245 | struct file *file; |
| 246 | |
| 247 | file = fget(fd); |
| 248 | if (!file) |
| 249 | return ERR_PTR(-EBADF); |
| 250 | if (file->f_op != &eventfd_fops) { |
| 251 | fput(file); |
| 252 | return ERR_PTR(-EINVAL); |
| 253 | } |
| 254 | |
| 255 | return file; |
| 256 | } |
Rusty Russell | 5718607 | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 257 | EXPORT_SYMBOL_GPL(eventfd_fget); |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 258 | |
Davide Libenzi | 1338901 | 2009-06-30 11:41:11 -0700 | [diff] [blame] | 259 | /** |
| 260 | * eventfd_ctx_fdget - Acquires a reference to the internal eventfd context. |
| 261 | * @fd: [in] Eventfd file descriptor. |
| 262 | * |
| 263 | * Returns a pointer to the internal eventfd context, otherwise the error |
| 264 | * pointers returned by the following functions: |
| 265 | * |
| 266 | * eventfd_fget |
| 267 | */ |
| 268 | struct eventfd_ctx *eventfd_ctx_fdget(int fd) |
| 269 | { |
| 270 | struct file *file; |
| 271 | struct eventfd_ctx *ctx; |
| 272 | |
| 273 | file = eventfd_fget(fd); |
| 274 | if (IS_ERR(file)) |
| 275 | return (struct eventfd_ctx *) file; |
| 276 | ctx = eventfd_ctx_get(file->private_data); |
| 277 | fput(file); |
| 278 | |
| 279 | return ctx; |
| 280 | } |
| 281 | EXPORT_SYMBOL_GPL(eventfd_ctx_fdget); |
| 282 | |
| 283 | /** |
| 284 | * eventfd_ctx_fileget - Acquires a reference to the internal eventfd context. |
| 285 | * @file: [in] Eventfd file pointer. |
| 286 | * |
| 287 | * Returns a pointer to the internal eventfd context, otherwise the error |
| 288 | * pointer: |
| 289 | * |
| 290 | * -EINVAL : The @fd file descriptor is not an eventfd file. |
| 291 | */ |
| 292 | struct eventfd_ctx *eventfd_ctx_fileget(struct file *file) |
| 293 | { |
| 294 | if (file->f_op != &eventfd_fops) |
| 295 | return ERR_PTR(-EINVAL); |
| 296 | |
| 297 | return eventfd_ctx_get(file->private_data); |
| 298 | } |
| 299 | EXPORT_SYMBOL_GPL(eventfd_ctx_fileget); |
| 300 | |
Heiko Carstens | d4e8204 | 2009-01-14 14:14:34 +0100 | [diff] [blame] | 301 | SYSCALL_DEFINE2(eventfd2, unsigned int, count, int, flags) |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 302 | { |
Al Viro | 2030a42 | 2008-02-23 06:46:49 -0500 | [diff] [blame] | 303 | int fd; |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 304 | struct eventfd_ctx *ctx; |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 305 | |
Ulrich Drepper | e38b36f | 2008-07-23 21:29:42 -0700 | [diff] [blame] | 306 | /* Check the EFD_* constants for consistency. */ |
| 307 | BUILD_BUG_ON(EFD_CLOEXEC != O_CLOEXEC); |
| 308 | BUILD_BUG_ON(EFD_NONBLOCK != O_NONBLOCK); |
| 309 | |
Davide Libenzi | bcd0b23 | 2009-03-31 15:24:18 -0700 | [diff] [blame] | 310 | if (flags & ~EFD_FLAGS_SET) |
Ulrich Drepper | b087498e | 2008-07-23 21:29:25 -0700 | [diff] [blame] | 311 | return -EINVAL; |
| 312 | |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 313 | ctx = kmalloc(sizeof(*ctx), GFP_KERNEL); |
| 314 | if (!ctx) |
| 315 | return -ENOMEM; |
| 316 | |
Davide Libenzi | 1338901 | 2009-06-30 11:41:11 -0700 | [diff] [blame] | 317 | kref_init(&ctx->kref); |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 318 | init_waitqueue_head(&ctx->wqh); |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 319 | ctx->count = count; |
Davide Libenzi | bcd0b23 | 2009-03-31 15:24:18 -0700 | [diff] [blame] | 320 | ctx->flags = flags; |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 321 | |
| 322 | /* |
| 323 | * When we call this, the initialization must be complete, since |
| 324 | * anon_inode_getfd() will install the fd. |
| 325 | */ |
Ulrich Drepper | b087498e | 2008-07-23 21:29:25 -0700 | [diff] [blame] | 326 | fd = anon_inode_getfd("[eventfd]", &eventfd_fops, ctx, |
Davide Libenzi | bcd0b23 | 2009-03-31 15:24:18 -0700 | [diff] [blame] | 327 | flags & EFD_SHARED_FCNTL_FLAGS); |
Al Viro | 2030a42 | 2008-02-23 06:46:49 -0500 | [diff] [blame] | 328 | if (fd < 0) |
| 329 | kfree(ctx); |
| 330 | return fd; |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Heiko Carstens | d4e8204 | 2009-01-14 14:14:34 +0100 | [diff] [blame] | 333 | SYSCALL_DEFINE1(eventfd, unsigned int, count) |
Ulrich Drepper | b087498e | 2008-07-23 21:29:25 -0700 | [diff] [blame] | 334 | { |
| 335 | return sys_eventfd2(count, 0); |
| 336 | } |
Davide Libenzi | bcd0b23 | 2009-03-31 15:24:18 -0700 | [diff] [blame] | 337 | |