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>
19 #include <linux/module.h>
22 wait_queue_head_t wqh;
24 * Every time that a write(2) is performed on an eventfd, the
25 * value of the __u64 being written is added to "count" and a
26 * wakeup is performed on "wqh". A read(2) will return the "count"
27 * value to userspace, and will reset "count" to zero. The kernel
28 * size eventfd_signal() also, adds to the "count" counter and
36 * Adds "n" to the eventfd counter "count". Returns "n" in case of
37 * success, or a value lower then "n" in case of coutner overflow.
38 * This function is supposed to be called by the kernel in paths
39 * that do not allow sleeping. In this function we allow the counter
40 * to reach the ULLONG_MAX value, and we signal this as overflow
41 * condition by returining a POLLERR to poll(2).
43 int eventfd_signal(struct file *file, int n)
45 struct eventfd_ctx *ctx = file->private_data;
50 spin_lock_irqsave(&ctx->wqh.lock, flags);
51 if (ULLONG_MAX - ctx->count < n)
52 n = (int) (ULLONG_MAX - ctx->count);
54 if (waitqueue_active(&ctx->wqh))
55 wake_up_locked_poll(&ctx->wqh, POLLIN);
56 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
60 EXPORT_SYMBOL_GPL(eventfd_signal);
62 static int eventfd_release(struct inode *inode, struct file *file)
64 kfree(file->private_data);
68 static unsigned int eventfd_poll(struct file *file, poll_table *wait)
70 struct eventfd_ctx *ctx = file->private_data;
71 unsigned int events = 0;
74 poll_wait(file, &ctx->wqh, wait);
76 spin_lock_irqsave(&ctx->wqh.lock, flags);
79 if (ctx->count == ULLONG_MAX)
81 if (ULLONG_MAX - 1 > ctx->count)
83 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
88 static ssize_t eventfd_read(struct file *file, char __user *buf, size_t count,
91 struct eventfd_ctx *ctx = file->private_data;
94 DECLARE_WAITQUEUE(wait, current);
96 if (count < sizeof(ucnt))
98 spin_lock_irq(&ctx->wqh.lock);
102 else if (!(file->f_flags & O_NONBLOCK)) {
103 __add_wait_queue(&ctx->wqh, &wait);
105 set_current_state(TASK_INTERRUPTIBLE);
106 if (ctx->count > 0) {
110 if (signal_pending(current)) {
114 spin_unlock_irq(&ctx->wqh.lock);
116 spin_lock_irq(&ctx->wqh.lock);
118 __remove_wait_queue(&ctx->wqh, &wait);
119 __set_current_state(TASK_RUNNING);
121 if (likely(res > 0)) {
122 ucnt = (ctx->flags & EFD_SEMAPHORE) ? 1 : ctx->count;
124 if (waitqueue_active(&ctx->wqh))
125 wake_up_locked_poll(&ctx->wqh, POLLOUT);
127 spin_unlock_irq(&ctx->wqh.lock);
128 if (res > 0 && put_user(ucnt, (__u64 __user *) buf))
134 static ssize_t eventfd_write(struct file *file, const char __user *buf, size_t count,
137 struct eventfd_ctx *ctx = file->private_data;
140 DECLARE_WAITQUEUE(wait, current);
142 if (count < sizeof(ucnt))
144 if (copy_from_user(&ucnt, buf, sizeof(ucnt)))
146 if (ucnt == ULLONG_MAX)
148 spin_lock_irq(&ctx->wqh.lock);
150 if (ULLONG_MAX - ctx->count > ucnt)
152 else if (!(file->f_flags & O_NONBLOCK)) {
153 __add_wait_queue(&ctx->wqh, &wait);
155 set_current_state(TASK_INTERRUPTIBLE);
156 if (ULLONG_MAX - ctx->count > ucnt) {
160 if (signal_pending(current)) {
164 spin_unlock_irq(&ctx->wqh.lock);
166 spin_lock_irq(&ctx->wqh.lock);
168 __remove_wait_queue(&ctx->wqh, &wait);
169 __set_current_state(TASK_RUNNING);
171 if (likely(res > 0)) {
173 if (waitqueue_active(&ctx->wqh))
174 wake_up_locked_poll(&ctx->wqh, POLLIN);
176 spin_unlock_irq(&ctx->wqh.lock);
181 static const struct file_operations eventfd_fops = {
182 .release = eventfd_release,
183 .poll = eventfd_poll,
184 .read = eventfd_read,
185 .write = eventfd_write,
188 struct file *eventfd_fget(int fd)
194 return ERR_PTR(-EBADF);
195 if (file->f_op != &eventfd_fops) {
197 return ERR_PTR(-EINVAL);
202 EXPORT_SYMBOL_GPL(eventfd_fget);
204 SYSCALL_DEFINE2(eventfd2, unsigned int, count, int, flags)
207 struct eventfd_ctx *ctx;
209 /* Check the EFD_* constants for consistency. */
210 BUILD_BUG_ON(EFD_CLOEXEC != O_CLOEXEC);
211 BUILD_BUG_ON(EFD_NONBLOCK != O_NONBLOCK);
213 if (flags & ~EFD_FLAGS_SET)
216 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
220 init_waitqueue_head(&ctx->wqh);
225 * When we call this, the initialization must be complete, since
226 * anon_inode_getfd() will install the fd.
228 fd = anon_inode_getfd("[eventfd]", &eventfd_fops, ctx,
229 flags & EFD_SHARED_FCNTL_FLAGS);
235 SYSCALL_DEFINE1(eventfd, unsigned int, count)
237 return sys_eventfd2(count, 0);