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/syscalls.h>
18 #include <linux/module.h>
19 #include <linux/kref.h>
20 #include <linux/eventfd.h>
24 wait_queue_head_t wqh
;
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
30 * side eventfd_signal() also, adds to the "count" counter and
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.
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
48 * Returns @n in case of success, a non-negative number lower than @n in case
49 * of overflow, or the following error codes:
51 * -EINVAL : The value of @n is negative.
53 int eventfd_signal(struct eventfd_ctx
*ctx
, int n
)
59 spin_lock_irqsave(&ctx
->wqh
.lock
, flags
);
60 if (ULLONG_MAX
- ctx
->count
< n
)
61 n
= (int) (ULLONG_MAX
- ctx
->count
);
63 if (waitqueue_active(&ctx
->wqh
))
64 wake_up_locked_poll(&ctx
->wqh
, POLLIN
);
65 spin_unlock_irqrestore(&ctx
->wqh
.lock
, flags
);
69 EXPORT_SYMBOL_GPL(eventfd_signal
);
71 static void eventfd_free(struct kref
*kref
)
73 struct eventfd_ctx
*ctx
= container_of(kref
, struct eventfd_ctx
, kref
);
79 * eventfd_ctx_get - Acquires a reference to the internal eventfd context.
80 * @ctx: [in] Pointer to the eventfd context.
82 * Returns: In case of success, returns a pointer to the eventfd context.
84 struct eventfd_ctx
*eventfd_ctx_get(struct eventfd_ctx
*ctx
)
89 EXPORT_SYMBOL_GPL(eventfd_ctx_get
);
92 * eventfd_ctx_put - Releases a reference to the internal eventfd context.
93 * @ctx: [in] Pointer to eventfd context.
95 * The eventfd context reference must have been previously acquired either
96 * with eventfd_ctx_get() or eventfd_ctx_fdget()).
98 void eventfd_ctx_put(struct eventfd_ctx
*ctx
)
100 kref_put(&ctx
->kref
, eventfd_free
);
102 EXPORT_SYMBOL_GPL(eventfd_ctx_put
);
104 static int eventfd_release(struct inode
*inode
, struct file
*file
)
106 struct eventfd_ctx
*ctx
= file
->private_data
;
108 wake_up_poll(&ctx
->wqh
, POLLHUP
);
109 eventfd_ctx_put(ctx
);
113 static unsigned int eventfd_poll(struct file
*file
, poll_table
*wait
)
115 struct eventfd_ctx
*ctx
= file
->private_data
;
116 unsigned int events
= 0;
119 poll_wait(file
, &ctx
->wqh
, wait
);
121 spin_lock_irqsave(&ctx
->wqh
.lock
, flags
);
124 if (ctx
->count
== ULLONG_MAX
)
126 if (ULLONG_MAX
- 1 > ctx
->count
)
128 spin_unlock_irqrestore(&ctx
->wqh
.lock
, flags
);
133 static ssize_t
eventfd_read(struct file
*file
, char __user
*buf
, size_t count
,
136 struct eventfd_ctx
*ctx
= file
->private_data
;
139 DECLARE_WAITQUEUE(wait
, current
);
141 if (count
< sizeof(ucnt
))
143 spin_lock_irq(&ctx
->wqh
.lock
);
147 else if (!(file
->f_flags
& O_NONBLOCK
)) {
148 __add_wait_queue(&ctx
->wqh
, &wait
);
150 set_current_state(TASK_INTERRUPTIBLE
);
151 if (ctx
->count
> 0) {
155 if (signal_pending(current
)) {
159 spin_unlock_irq(&ctx
->wqh
.lock
);
161 spin_lock_irq(&ctx
->wqh
.lock
);
163 __remove_wait_queue(&ctx
->wqh
, &wait
);
164 __set_current_state(TASK_RUNNING
);
166 if (likely(res
> 0)) {
167 ucnt
= (ctx
->flags
& EFD_SEMAPHORE
) ? 1 : ctx
->count
;
169 if (waitqueue_active(&ctx
->wqh
))
170 wake_up_locked_poll(&ctx
->wqh
, POLLOUT
);
172 spin_unlock_irq(&ctx
->wqh
.lock
);
173 if (res
> 0 && put_user(ucnt
, (__u64 __user
*) buf
))
179 static ssize_t
eventfd_write(struct file
*file
, const char __user
*buf
, size_t count
,
182 struct eventfd_ctx
*ctx
= file
->private_data
;
185 DECLARE_WAITQUEUE(wait
, current
);
187 if (count
< sizeof(ucnt
))
189 if (copy_from_user(&ucnt
, buf
, sizeof(ucnt
)))
191 if (ucnt
== ULLONG_MAX
)
193 spin_lock_irq(&ctx
->wqh
.lock
);
195 if (ULLONG_MAX
- ctx
->count
> ucnt
)
197 else if (!(file
->f_flags
& O_NONBLOCK
)) {
198 __add_wait_queue(&ctx
->wqh
, &wait
);
200 set_current_state(TASK_INTERRUPTIBLE
);
201 if (ULLONG_MAX
- ctx
->count
> ucnt
) {
205 if (signal_pending(current
)) {
209 spin_unlock_irq(&ctx
->wqh
.lock
);
211 spin_lock_irq(&ctx
->wqh
.lock
);
213 __remove_wait_queue(&ctx
->wqh
, &wait
);
214 __set_current_state(TASK_RUNNING
);
216 if (likely(res
> 0)) {
218 if (waitqueue_active(&ctx
->wqh
))
219 wake_up_locked_poll(&ctx
->wqh
, POLLIN
);
221 spin_unlock_irq(&ctx
->wqh
.lock
);
226 static const struct file_operations eventfd_fops
= {
227 .release
= eventfd_release
,
228 .poll
= eventfd_poll
,
229 .read
= eventfd_read
,
230 .write
= eventfd_write
,
234 * eventfd_fget - Acquire a reference of an eventfd file descriptor.
235 * @fd: [in] Eventfd file descriptor.
237 * Returns a pointer to the eventfd file structure in case of success, or the
238 * following error pointer:
240 * -EBADF : Invalid @fd file descriptor.
241 * -EINVAL : The @fd file descriptor is not an eventfd file.
243 struct file
*eventfd_fget(int fd
)
249 return ERR_PTR(-EBADF
);
250 if (file
->f_op
!= &eventfd_fops
) {
252 return ERR_PTR(-EINVAL
);
257 EXPORT_SYMBOL_GPL(eventfd_fget
);
260 * eventfd_ctx_fdget - Acquires a reference to the internal eventfd context.
261 * @fd: [in] Eventfd file descriptor.
263 * Returns a pointer to the internal eventfd context, otherwise the error
264 * pointers returned by the following functions:
268 struct eventfd_ctx
*eventfd_ctx_fdget(int fd
)
271 struct eventfd_ctx
*ctx
;
273 file
= eventfd_fget(fd
);
275 return (struct eventfd_ctx
*) file
;
276 ctx
= eventfd_ctx_get(file
->private_data
);
281 EXPORT_SYMBOL_GPL(eventfd_ctx_fdget
);
284 * eventfd_ctx_fileget - Acquires a reference to the internal eventfd context.
285 * @file: [in] Eventfd file pointer.
287 * Returns a pointer to the internal eventfd context, otherwise the error
290 * -EINVAL : The @fd file descriptor is not an eventfd file.
292 struct eventfd_ctx
*eventfd_ctx_fileget(struct file
*file
)
294 if (file
->f_op
!= &eventfd_fops
)
295 return ERR_PTR(-EINVAL
);
297 return eventfd_ctx_get(file
->private_data
);
299 EXPORT_SYMBOL_GPL(eventfd_ctx_fileget
);
301 SYSCALL_DEFINE2(eventfd2
, unsigned int, count
, int, flags
)
304 struct eventfd_ctx
*ctx
;
306 /* Check the EFD_* constants for consistency. */
307 BUILD_BUG_ON(EFD_CLOEXEC
!= O_CLOEXEC
);
308 BUILD_BUG_ON(EFD_NONBLOCK
!= O_NONBLOCK
);
310 if (flags
& ~EFD_FLAGS_SET
)
313 ctx
= kmalloc(sizeof(*ctx
), GFP_KERNEL
);
317 kref_init(&ctx
->kref
);
318 init_waitqueue_head(&ctx
->wqh
);
323 * When we call this, the initialization must be complete, since
324 * anon_inode_getfd() will install the fd.
326 fd
= anon_inode_getfd("[eventfd]", &eventfd_fops
, ctx
,
327 flags
& EFD_SHARED_FCNTL_FLAGS
);
333 SYSCALL_DEFINE1(eventfd
, unsigned int, count
)
335 return sys_eventfd2(count
, 0);