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> |
| 17 | #include <linux/eventfd.h> |
Adrian Bunk | 7747cdb | 2008-02-06 01:36:49 -0800 | [diff] [blame] | 18 | #include <linux/syscalls.h> |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 19 | |
| 20 | struct eventfd_ctx { |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 21 | wait_queue_head_t wqh; |
| 22 | /* |
| 23 | * Every time that a write(2) is performed on an eventfd, the |
| 24 | * value of the __u64 being written is added to "count" and a |
| 25 | * wakeup is performed on "wqh". A read(2) will return the "count" |
| 26 | * value to userspace, and will reset "count" to zero. The kernel |
| 27 | * size eventfd_signal() also, adds to the "count" counter and |
| 28 | * issue a wakeup. |
| 29 | */ |
| 30 | __u64 count; |
Davide Libenzi | bcd0b23 | 2009-03-31 15:24:18 -0700 | [diff] [blame] | 31 | unsigned int flags; |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | /* |
| 35 | * Adds "n" to the eventfd counter "count". Returns "n" in case of |
| 36 | * success, or a value lower then "n" in case of coutner overflow. |
| 37 | * This function is supposed to be called by the kernel in paths |
| 38 | * that do not allow sleeping. In this function we allow the counter |
| 39 | * to reach the ULLONG_MAX value, and we signal this as overflow |
| 40 | * condition by returining a POLLERR to poll(2). |
| 41 | */ |
| 42 | int eventfd_signal(struct file *file, int n) |
| 43 | { |
| 44 | struct eventfd_ctx *ctx = file->private_data; |
| 45 | unsigned long flags; |
| 46 | |
| 47 | if (n < 0) |
| 48 | return -EINVAL; |
Davide Libenzi | d48eb23 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 49 | spin_lock_irqsave(&ctx->wqh.lock, flags); |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 50 | if (ULLONG_MAX - ctx->count < n) |
| 51 | n = (int) (ULLONG_MAX - ctx->count); |
| 52 | ctx->count += n; |
| 53 | if (waitqueue_active(&ctx->wqh)) |
Davide Libenzi | 3951088 | 2009-03-31 15:24:23 -0700 | [diff] [blame] | 54 | wake_up_locked_poll(&ctx->wqh, POLLIN); |
Davide Libenzi | d48eb23 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 55 | spin_unlock_irqrestore(&ctx->wqh.lock, flags); |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 56 | |
| 57 | return n; |
| 58 | } |
| 59 | |
| 60 | static int eventfd_release(struct inode *inode, struct file *file) |
| 61 | { |
| 62 | kfree(file->private_data); |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | static unsigned int eventfd_poll(struct file *file, poll_table *wait) |
| 67 | { |
| 68 | struct eventfd_ctx *ctx = file->private_data; |
| 69 | unsigned int events = 0; |
| 70 | unsigned long flags; |
| 71 | |
| 72 | poll_wait(file, &ctx->wqh, wait); |
| 73 | |
Davide Libenzi | d48eb23 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 74 | spin_lock_irqsave(&ctx->wqh.lock, flags); |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 75 | if (ctx->count > 0) |
| 76 | events |= POLLIN; |
| 77 | if (ctx->count == ULLONG_MAX) |
| 78 | events |= POLLERR; |
| 79 | if (ULLONG_MAX - 1 > ctx->count) |
| 80 | events |= POLLOUT; |
Davide Libenzi | d48eb23 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 81 | spin_unlock_irqrestore(&ctx->wqh.lock, flags); |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 82 | |
| 83 | return events; |
| 84 | } |
| 85 | |
| 86 | static ssize_t eventfd_read(struct file *file, char __user *buf, size_t count, |
| 87 | loff_t *ppos) |
| 88 | { |
| 89 | struct eventfd_ctx *ctx = file->private_data; |
| 90 | ssize_t res; |
Davide Libenzi | bcd0b23 | 2009-03-31 15:24:18 -0700 | [diff] [blame] | 91 | __u64 ucnt = 0; |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 92 | DECLARE_WAITQUEUE(wait, current); |
| 93 | |
| 94 | if (count < sizeof(ucnt)) |
| 95 | return -EINVAL; |
Davide Libenzi | d48eb23 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 96 | spin_lock_irq(&ctx->wqh.lock); |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 97 | res = -EAGAIN; |
Davide Libenzi | bcd0b23 | 2009-03-31 15:24:18 -0700 | [diff] [blame] | 98 | if (ctx->count > 0) |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 99 | res = sizeof(ucnt); |
| 100 | else if (!(file->f_flags & O_NONBLOCK)) { |
| 101 | __add_wait_queue(&ctx->wqh, &wait); |
| 102 | for (res = 0;;) { |
| 103 | set_current_state(TASK_INTERRUPTIBLE); |
| 104 | if (ctx->count > 0) { |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 105 | res = sizeof(ucnt); |
| 106 | break; |
| 107 | } |
| 108 | if (signal_pending(current)) { |
| 109 | res = -ERESTARTSYS; |
| 110 | break; |
| 111 | } |
Davide Libenzi | d48eb23 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 112 | spin_unlock_irq(&ctx->wqh.lock); |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 113 | schedule(); |
Davide Libenzi | d48eb23 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 114 | spin_lock_irq(&ctx->wqh.lock); |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 115 | } |
| 116 | __remove_wait_queue(&ctx->wqh, &wait); |
| 117 | __set_current_state(TASK_RUNNING); |
| 118 | } |
Davide Libenzi | bcd0b23 | 2009-03-31 15:24:18 -0700 | [diff] [blame] | 119 | if (likely(res > 0)) { |
| 120 | ucnt = (ctx->flags & EFD_SEMAPHORE) ? 1 : ctx->count; |
| 121 | ctx->count -= ucnt; |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 122 | if (waitqueue_active(&ctx->wqh)) |
Davide Libenzi | 3951088 | 2009-03-31 15:24:23 -0700 | [diff] [blame] | 123 | wake_up_locked_poll(&ctx->wqh, POLLOUT); |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 124 | } |
Davide Libenzi | d48eb23 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 125 | spin_unlock_irq(&ctx->wqh.lock); |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 126 | if (res > 0 && put_user(ucnt, (__u64 __user *) buf)) |
| 127 | return -EFAULT; |
| 128 | |
| 129 | return res; |
| 130 | } |
| 131 | |
| 132 | static ssize_t eventfd_write(struct file *file, const char __user *buf, size_t count, |
| 133 | loff_t *ppos) |
| 134 | { |
| 135 | struct eventfd_ctx *ctx = file->private_data; |
| 136 | ssize_t res; |
| 137 | __u64 ucnt; |
| 138 | DECLARE_WAITQUEUE(wait, current); |
| 139 | |
| 140 | if (count < sizeof(ucnt)) |
| 141 | return -EINVAL; |
| 142 | if (copy_from_user(&ucnt, buf, sizeof(ucnt))) |
| 143 | return -EFAULT; |
| 144 | if (ucnt == ULLONG_MAX) |
| 145 | return -EINVAL; |
Davide Libenzi | d48eb23 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 146 | spin_lock_irq(&ctx->wqh.lock); |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 147 | res = -EAGAIN; |
| 148 | if (ULLONG_MAX - ctx->count > ucnt) |
| 149 | res = sizeof(ucnt); |
| 150 | else if (!(file->f_flags & O_NONBLOCK)) { |
| 151 | __add_wait_queue(&ctx->wqh, &wait); |
| 152 | for (res = 0;;) { |
| 153 | set_current_state(TASK_INTERRUPTIBLE); |
| 154 | if (ULLONG_MAX - ctx->count > ucnt) { |
| 155 | res = sizeof(ucnt); |
| 156 | break; |
| 157 | } |
| 158 | if (signal_pending(current)) { |
| 159 | res = -ERESTARTSYS; |
| 160 | break; |
| 161 | } |
Davide Libenzi | d48eb23 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 162 | spin_unlock_irq(&ctx->wqh.lock); |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 163 | schedule(); |
Davide Libenzi | d48eb23 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 164 | spin_lock_irq(&ctx->wqh.lock); |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 165 | } |
| 166 | __remove_wait_queue(&ctx->wqh, &wait); |
| 167 | __set_current_state(TASK_RUNNING); |
| 168 | } |
Davide Libenzi | bcd0b23 | 2009-03-31 15:24:18 -0700 | [diff] [blame] | 169 | if (likely(res > 0)) { |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 170 | ctx->count += ucnt; |
| 171 | if (waitqueue_active(&ctx->wqh)) |
Davide Libenzi | 3951088 | 2009-03-31 15:24:23 -0700 | [diff] [blame] | 172 | wake_up_locked_poll(&ctx->wqh, POLLIN); |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 173 | } |
Davide Libenzi | d48eb23 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 174 | spin_unlock_irq(&ctx->wqh.lock); |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 175 | |
| 176 | return res; |
| 177 | } |
| 178 | |
| 179 | static const struct file_operations eventfd_fops = { |
| 180 | .release = eventfd_release, |
| 181 | .poll = eventfd_poll, |
| 182 | .read = eventfd_read, |
| 183 | .write = eventfd_write, |
| 184 | }; |
| 185 | |
| 186 | struct file *eventfd_fget(int fd) |
| 187 | { |
| 188 | struct file *file; |
| 189 | |
| 190 | file = fget(fd); |
| 191 | if (!file) |
| 192 | return ERR_PTR(-EBADF); |
| 193 | if (file->f_op != &eventfd_fops) { |
| 194 | fput(file); |
| 195 | return ERR_PTR(-EINVAL); |
| 196 | } |
| 197 | |
| 198 | return file; |
| 199 | } |
| 200 | |
Heiko Carstens | d4e8204 | 2009-01-14 14:14:34 +0100 | [diff] [blame] | 201 | SYSCALL_DEFINE2(eventfd2, unsigned int, count, int, flags) |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 202 | { |
Al Viro | 2030a42 | 2008-02-23 06:46:49 -0500 | [diff] [blame] | 203 | int fd; |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 204 | struct eventfd_ctx *ctx; |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 205 | |
Ulrich Drepper | e38b36f | 2008-07-23 21:29:42 -0700 | [diff] [blame] | 206 | /* Check the EFD_* constants for consistency. */ |
| 207 | BUILD_BUG_ON(EFD_CLOEXEC != O_CLOEXEC); |
| 208 | BUILD_BUG_ON(EFD_NONBLOCK != O_NONBLOCK); |
| 209 | |
Davide Libenzi | bcd0b23 | 2009-03-31 15:24:18 -0700 | [diff] [blame] | 210 | if (flags & ~EFD_FLAGS_SET) |
Ulrich Drepper | b087498e | 2008-07-23 21:29:25 -0700 | [diff] [blame] | 211 | return -EINVAL; |
| 212 | |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 213 | ctx = kmalloc(sizeof(*ctx), GFP_KERNEL); |
| 214 | if (!ctx) |
| 215 | return -ENOMEM; |
| 216 | |
| 217 | init_waitqueue_head(&ctx->wqh); |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 218 | ctx->count = count; |
Davide Libenzi | bcd0b23 | 2009-03-31 15:24:18 -0700 | [diff] [blame] | 219 | ctx->flags = flags; |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 220 | |
| 221 | /* |
| 222 | * When we call this, the initialization must be complete, since |
| 223 | * anon_inode_getfd() will install the fd. |
| 224 | */ |
Ulrich Drepper | b087498e | 2008-07-23 21:29:25 -0700 | [diff] [blame] | 225 | fd = anon_inode_getfd("[eventfd]", &eventfd_fops, ctx, |
Davide Libenzi | bcd0b23 | 2009-03-31 15:24:18 -0700 | [diff] [blame] | 226 | flags & EFD_SHARED_FCNTL_FLAGS); |
Al Viro | 2030a42 | 2008-02-23 06:46:49 -0500 | [diff] [blame] | 227 | if (fd < 0) |
| 228 | kfree(ctx); |
| 229 | return fd; |
Davide Libenzi | e1ad746 | 2007-05-10 22:23:19 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Heiko Carstens | d4e8204 | 2009-01-14 14:14:34 +0100 | [diff] [blame] | 232 | SYSCALL_DEFINE1(eventfd, unsigned int, count) |
Ulrich Drepper | b087498e | 2008-07-23 21:29:25 -0700 | [diff] [blame] | 233 | { |
| 234 | return sys_eventfd2(count, 0); |
| 235 | } |
Davide Libenzi | bcd0b23 | 2009-03-31 15:24:18 -0700 | [diff] [blame] | 236 | |