4 * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org>
8 #include <linux/file.h>
9 #include <linux/poll.h>
10 #include <linux/init.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>
18 #include <linux/syscalls.h>
21 wait_queue_head_t wqh;
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
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).
42 int eventfd_signal(struct file *file, int n)
44 struct eventfd_ctx *ctx = file->private_data;
49 spin_lock_irqsave(&ctx->wqh.lock, flags);
50 if (ULLONG_MAX - ctx->count < n)
51 n = (int) (ULLONG_MAX - ctx->count);
53 if (waitqueue_active(&ctx->wqh))
54 wake_up_locked_poll(&ctx->wqh, POLLIN);
55 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
60 static int eventfd_release(struct inode *inode, struct file *file)
62 kfree(file->private_data);
66 static unsigned int eventfd_poll(struct file *file, poll_table *wait)
68 struct eventfd_ctx *ctx = file->private_data;
69 unsigned int events = 0;
72 poll_wait(file, &ctx->wqh, wait);
74 spin_lock_irqsave(&ctx->wqh.lock, flags);
77 if (ctx->count == ULLONG_MAX)
79 if (ULLONG_MAX - 1 > ctx->count)
81 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
86 static ssize_t eventfd_read(struct file *file, char __user *buf, size_t count,
89 struct eventfd_ctx *ctx = file->private_data;
92 DECLARE_WAITQUEUE(wait, current);
94 if (count < sizeof(ucnt))
96 spin_lock_irq(&ctx->wqh.lock);
100 else if (!(file->f_flags & O_NONBLOCK)) {
101 __add_wait_queue(&ctx->wqh, &wait);
103 set_current_state(TASK_INTERRUPTIBLE);
104 if (ctx->count > 0) {
108 if (signal_pending(current)) {
112 spin_unlock_irq(&ctx->wqh.lock);
114 spin_lock_irq(&ctx->wqh.lock);
116 __remove_wait_queue(&ctx->wqh, &wait);
117 __set_current_state(TASK_RUNNING);
119 if (likely(res > 0)) {
120 ucnt = (ctx->flags & EFD_SEMAPHORE) ? 1 : ctx->count;
122 if (waitqueue_active(&ctx->wqh))
123 wake_up_locked_poll(&ctx->wqh, POLLOUT);
125 spin_unlock_irq(&ctx->wqh.lock);
126 if (res > 0 && put_user(ucnt, (__u64 __user *) buf))
132 static ssize_t eventfd_write(struct file *file, const char __user *buf, size_t count,
135 struct eventfd_ctx *ctx = file->private_data;
138 DECLARE_WAITQUEUE(wait, current);
140 if (count < sizeof(ucnt))
142 if (copy_from_user(&ucnt, buf, sizeof(ucnt)))
144 if (ucnt == ULLONG_MAX)
146 spin_lock_irq(&ctx->wqh.lock);
148 if (ULLONG_MAX - ctx->count > ucnt)
150 else if (!(file->f_flags & O_NONBLOCK)) {
151 __add_wait_queue(&ctx->wqh, &wait);
153 set_current_state(TASK_INTERRUPTIBLE);
154 if (ULLONG_MAX - ctx->count > ucnt) {
158 if (signal_pending(current)) {
162 spin_unlock_irq(&ctx->wqh.lock);
164 spin_lock_irq(&ctx->wqh.lock);
166 __remove_wait_queue(&ctx->wqh, &wait);
167 __set_current_state(TASK_RUNNING);
169 if (likely(res > 0)) {
171 if (waitqueue_active(&ctx->wqh))
172 wake_up_locked_poll(&ctx->wqh, POLLIN);
174 spin_unlock_irq(&ctx->wqh.lock);
179 static const struct file_operations eventfd_fops = {
180 .release = eventfd_release,
181 .poll = eventfd_poll,
182 .read = eventfd_read,
183 .write = eventfd_write,
186 struct file *eventfd_fget(int fd)
192 return ERR_PTR(-EBADF);
193 if (file->f_op != &eventfd_fops) {
195 return ERR_PTR(-EINVAL);
201 SYSCALL_DEFINE2(eventfd2, unsigned int, count, int, flags)
204 struct eventfd_ctx *ctx;
206 /* Check the EFD_* constants for consistency. */
207 BUILD_BUG_ON(EFD_CLOEXEC != O_CLOEXEC);
208 BUILD_BUG_ON(EFD_NONBLOCK != O_NONBLOCK);
210 if (flags & ~EFD_FLAGS_SET)
213 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
217 init_waitqueue_head(&ctx->wqh);
222 * When we call this, the initialization must be complete, since
223 * anon_inode_getfd() will install the fd.
225 fd = anon_inode_getfd("[eventfd]", &eventfd_fops, ctx,
226 flags & EFD_SHARED_FCNTL_FLAGS);
232 SYSCALL_DEFINE1(eventfd, unsigned int, count)
234 return sys_eventfd2(count, 0);