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_ctx(struct eventfd_ctx
*ctx
)
76 static void eventfd_free(struct kref
*kref
)
78 struct eventfd_ctx
*ctx
= container_of(kref
, struct eventfd_ctx
, kref
);
80 eventfd_free_ctx(ctx
);
84 * eventfd_ctx_get - Acquires a reference to the internal eventfd context.
85 * @ctx: [in] Pointer to the eventfd context.
87 * Returns: In case of success, returns a pointer to the eventfd context.
89 struct eventfd_ctx
*eventfd_ctx_get(struct eventfd_ctx
*ctx
)
94 EXPORT_SYMBOL_GPL(eventfd_ctx_get
);
97 * eventfd_ctx_put - Releases a reference to the internal eventfd context.
98 * @ctx: [in] Pointer to eventfd context.
100 * The eventfd context reference must have been previously acquired either
101 * with eventfd_ctx_get() or eventfd_ctx_fdget()).
103 void eventfd_ctx_put(struct eventfd_ctx
*ctx
)
105 kref_put(&ctx
->kref
, eventfd_free
);
107 EXPORT_SYMBOL_GPL(eventfd_ctx_put
);
109 static int eventfd_release(struct inode
*inode
, struct file
*file
)
111 struct eventfd_ctx
*ctx
= file
->private_data
;
113 wake_up_poll(&ctx
->wqh
, POLLHUP
);
114 eventfd_ctx_put(ctx
);
118 static unsigned int eventfd_poll(struct file
*file
, poll_table
*wait
)
120 struct eventfd_ctx
*ctx
= file
->private_data
;
121 unsigned int events
= 0;
124 poll_wait(file
, &ctx
->wqh
, wait
);
126 spin_lock_irqsave(&ctx
->wqh
.lock
, flags
);
129 if (ctx
->count
== ULLONG_MAX
)
131 if (ULLONG_MAX
- 1 > ctx
->count
)
133 spin_unlock_irqrestore(&ctx
->wqh
.lock
, flags
);
138 static ssize_t
eventfd_read(struct file
*file
, char __user
*buf
, size_t count
,
141 struct eventfd_ctx
*ctx
= file
->private_data
;
144 DECLARE_WAITQUEUE(wait
, current
);
146 if (count
< sizeof(ucnt
))
148 spin_lock_irq(&ctx
->wqh
.lock
);
152 else if (!(file
->f_flags
& O_NONBLOCK
)) {
153 __add_wait_queue(&ctx
->wqh
, &wait
);
155 set_current_state(TASK_INTERRUPTIBLE
);
156 if (ctx
->count
> 0) {
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)) {
172 ucnt
= (ctx
->flags
& EFD_SEMAPHORE
) ? 1 : ctx
->count
;
174 if (waitqueue_active(&ctx
->wqh
))
175 wake_up_locked_poll(&ctx
->wqh
, POLLOUT
);
177 spin_unlock_irq(&ctx
->wqh
.lock
);
178 if (res
> 0 && put_user(ucnt
, (__u64 __user
*) buf
))
184 static ssize_t
eventfd_write(struct file
*file
, const char __user
*buf
, size_t count
,
187 struct eventfd_ctx
*ctx
= file
->private_data
;
190 DECLARE_WAITQUEUE(wait
, current
);
192 if (count
< sizeof(ucnt
))
194 if (copy_from_user(&ucnt
, buf
, sizeof(ucnt
)))
196 if (ucnt
== ULLONG_MAX
)
198 spin_lock_irq(&ctx
->wqh
.lock
);
200 if (ULLONG_MAX
- ctx
->count
> ucnt
)
202 else if (!(file
->f_flags
& O_NONBLOCK
)) {
203 __add_wait_queue(&ctx
->wqh
, &wait
);
205 set_current_state(TASK_INTERRUPTIBLE
);
206 if (ULLONG_MAX
- ctx
->count
> ucnt
) {
210 if (signal_pending(current
)) {
214 spin_unlock_irq(&ctx
->wqh
.lock
);
216 spin_lock_irq(&ctx
->wqh
.lock
);
218 __remove_wait_queue(&ctx
->wqh
, &wait
);
219 __set_current_state(TASK_RUNNING
);
221 if (likely(res
> 0)) {
223 if (waitqueue_active(&ctx
->wqh
))
224 wake_up_locked_poll(&ctx
->wqh
, POLLIN
);
226 spin_unlock_irq(&ctx
->wqh
.lock
);
231 static const struct file_operations eventfd_fops
= {
232 .release
= eventfd_release
,
233 .poll
= eventfd_poll
,
234 .read
= eventfd_read
,
235 .write
= eventfd_write
,
239 * eventfd_fget - Acquire a reference of an eventfd file descriptor.
240 * @fd: [in] Eventfd file descriptor.
242 * Returns a pointer to the eventfd file structure in case of success, or the
243 * following error pointer:
245 * -EBADF : Invalid @fd file descriptor.
246 * -EINVAL : The @fd file descriptor is not an eventfd file.
248 struct file
*eventfd_fget(int fd
)
254 return ERR_PTR(-EBADF
);
255 if (file
->f_op
!= &eventfd_fops
) {
257 return ERR_PTR(-EINVAL
);
262 EXPORT_SYMBOL_GPL(eventfd_fget
);
265 * eventfd_ctx_fdget - Acquires a reference to the internal eventfd context.
266 * @fd: [in] Eventfd file descriptor.
268 * Returns a pointer to the internal eventfd context, otherwise the error
269 * pointers returned by the following functions:
273 struct eventfd_ctx
*eventfd_ctx_fdget(int fd
)
276 struct eventfd_ctx
*ctx
;
278 file
= eventfd_fget(fd
);
280 return (struct eventfd_ctx
*) file
;
281 ctx
= eventfd_ctx_get(file
->private_data
);
286 EXPORT_SYMBOL_GPL(eventfd_ctx_fdget
);
289 * eventfd_ctx_fileget - Acquires a reference to the internal eventfd context.
290 * @file: [in] Eventfd file pointer.
292 * Returns a pointer to the internal eventfd context, otherwise the error
295 * -EINVAL : The @fd file descriptor is not an eventfd file.
297 struct eventfd_ctx
*eventfd_ctx_fileget(struct file
*file
)
299 if (file
->f_op
!= &eventfd_fops
)
300 return ERR_PTR(-EINVAL
);
302 return eventfd_ctx_get(file
->private_data
);
304 EXPORT_SYMBOL_GPL(eventfd_ctx_fileget
);
307 * eventfd_file_create - Creates an eventfd file pointer.
308 * @count: Initial eventfd counter value.
309 * @flags: Flags for the eventfd file.
311 * This function creates an eventfd file pointer, w/out installing it into
312 * the fd table. This is useful when the eventfd file is used during the
313 * initialization of data structures that require extra setup after the eventfd
314 * creation. So the eventfd creation is split into the file pointer creation
315 * phase, and the file descriptor installation phase.
316 * In this way races with userspace closing the newly installed file descriptor
318 * Returns an eventfd file pointer, or a proper error pointer.
320 struct file
*eventfd_file_create(unsigned int count
, int flags
)
323 struct eventfd_ctx
*ctx
;
325 /* Check the EFD_* constants for consistency. */
326 BUILD_BUG_ON(EFD_CLOEXEC
!= O_CLOEXEC
);
327 BUILD_BUG_ON(EFD_NONBLOCK
!= O_NONBLOCK
);
329 if (flags
& ~EFD_FLAGS_SET
)
330 return ERR_PTR(-EINVAL
);
332 ctx
= kmalloc(sizeof(*ctx
), GFP_KERNEL
);
334 return ERR_PTR(-ENOMEM
);
336 kref_init(&ctx
->kref
);
337 init_waitqueue_head(&ctx
->wqh
);
341 file
= anon_inode_getfile("[eventfd]", &eventfd_fops
, ctx
,
342 O_RDWR
| (flags
& EFD_SHARED_FCNTL_FLAGS
));
344 eventfd_free_ctx(ctx
);
349 SYSCALL_DEFINE2(eventfd2
, unsigned int, count
, int, flags
)
354 error
= get_unused_fd_flags(flags
& EFD_SHARED_FCNTL_FLAGS
);
359 file
= eventfd_file_create(count
, flags
);
361 error
= PTR_ERR(file
);
362 goto err_put_unused_fd
;
364 fd_install(fd
, file
);
374 SYSCALL_DEFINE1(eventfd
, unsigned int, count
)
376 return sys_eventfd2(count
, 0);