1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org>
9 #include <linux/file.h>
10 #include <linux/poll.h>
11 #include <linux/init.h>
13 #include <linux/sched/signal.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/list.h>
17 #include <linux/spinlock.h>
18 #include <linux/anon_inodes.h>
19 #include <linux/syscalls.h>
20 #include <linux/export.h>
21 #include <linux/kref.h>
22 #include <linux/eventfd.h>
23 #include <linux/proc_fs.h>
24 #include <linux/seq_file.h>
25 #include <linux/idr.h>
27 static DEFINE_IDA(eventfd_ida
);
31 wait_queue_head_t wqh
;
33 * Every time that a write(2) is performed on an eventfd, the
34 * value of the __u64 being written is added to "count" and a
35 * wakeup is performed on "wqh". A read(2) will return the "count"
36 * value to userspace, and will reset "count" to zero. The kernel
37 * side eventfd_signal() also, adds to the "count" counter and
46 * eventfd_signal - Adds @n to the eventfd counter.
47 * @ctx: [in] Pointer to the eventfd context.
48 * @n: [in] Value of the counter to be added to the eventfd internal counter.
49 * The value cannot be negative.
51 * This function is supposed to be called by the kernel in paths that do not
52 * allow sleeping. In this function we allow the counter to reach the ULLONG_MAX
53 * value, and we signal this as overflow condition by returning a EPOLLERR
56 * Returns the amount by which the counter was incremented. This will be less
57 * than @n if the counter has overflowed.
59 __u64
eventfd_signal(struct eventfd_ctx
*ctx
, __u64 n
)
63 spin_lock_irqsave(&ctx
->wqh
.lock
, flags
);
64 if (ULLONG_MAX
- ctx
->count
< n
)
65 n
= ULLONG_MAX
- ctx
->count
;
67 if (waitqueue_active(&ctx
->wqh
))
68 wake_up_locked_poll(&ctx
->wqh
, EPOLLIN
);
69 spin_unlock_irqrestore(&ctx
->wqh
.lock
, flags
);
73 EXPORT_SYMBOL_GPL(eventfd_signal
);
75 static void eventfd_free_ctx(struct eventfd_ctx
*ctx
)
78 ida_simple_remove(&eventfd_ida
, ctx
->id
);
82 static void eventfd_free(struct kref
*kref
)
84 struct eventfd_ctx
*ctx
= container_of(kref
, struct eventfd_ctx
, kref
);
86 eventfd_free_ctx(ctx
);
90 * eventfd_ctx_put - Releases a reference to the internal eventfd context.
91 * @ctx: [in] Pointer to eventfd context.
93 * The eventfd context reference must have been previously acquired either
94 * with eventfd_ctx_fdget() or eventfd_ctx_fileget().
96 void eventfd_ctx_put(struct eventfd_ctx
*ctx
)
98 kref_put(&ctx
->kref
, eventfd_free
);
100 EXPORT_SYMBOL_GPL(eventfd_ctx_put
);
102 static int eventfd_release(struct inode
*inode
, struct file
*file
)
104 struct eventfd_ctx
*ctx
= file
->private_data
;
106 wake_up_poll(&ctx
->wqh
, EPOLLHUP
);
107 eventfd_ctx_put(ctx
);
111 static __poll_t
eventfd_poll(struct file
*file
, poll_table
*wait
)
113 struct eventfd_ctx
*ctx
= file
->private_data
;
117 poll_wait(file
, &ctx
->wqh
, wait
);
120 * All writes to ctx->count occur within ctx->wqh.lock. This read
121 * can be done outside ctx->wqh.lock because we know that poll_wait
122 * takes that lock (through add_wait_queue) if our caller will sleep.
124 * The read _can_ therefore seep into add_wait_queue's critical
125 * section, but cannot move above it! add_wait_queue's spin_lock acts
126 * as an acquire barrier and ensures that the read be ordered properly
127 * against the writes. The following CAN happen and is safe:
130 * ----------------- ------------
131 * lock ctx->wqh.lock (in poll_wait)
134 * unlock ctx->wqh.lock
137 * if (waitqueue_active)
138 * wake_up_locked_poll
139 * unlock ctx->qwh.lock
140 * eventfd_poll returns 0
142 * but the following, which would miss a wakeup, cannot happen:
145 * ----------------- ------------
146 * count = ctx->count (INVALID!)
149 * **waitqueue_active is false**
150 * **no wake_up_locked_poll!**
151 * unlock ctx->qwh.lock
152 * lock ctx->wqh.lock (in poll_wait)
154 * unlock ctx->wqh.lock
155 * eventfd_poll returns 0
157 count
= READ_ONCE(ctx
->count
);
161 if (count
== ULLONG_MAX
)
163 if (ULLONG_MAX
- 1 > count
)
169 static void eventfd_ctx_do_read(struct eventfd_ctx
*ctx
, __u64
*cnt
)
171 *cnt
= (ctx
->flags
& EFD_SEMAPHORE
) ? 1 : ctx
->count
;
176 * eventfd_ctx_remove_wait_queue - Read the current counter and removes wait queue.
177 * @ctx: [in] Pointer to eventfd context.
178 * @wait: [in] Wait queue to be removed.
179 * @cnt: [out] Pointer to the 64-bit counter value.
181 * Returns %0 if successful, or the following error codes:
183 * -EAGAIN : The operation would have blocked.
185 * This is used to atomically remove a wait queue entry from the eventfd wait
186 * queue head, and read/reset the counter value.
188 int eventfd_ctx_remove_wait_queue(struct eventfd_ctx
*ctx
, wait_queue_entry_t
*wait
,
193 spin_lock_irqsave(&ctx
->wqh
.lock
, flags
);
194 eventfd_ctx_do_read(ctx
, cnt
);
195 __remove_wait_queue(&ctx
->wqh
, wait
);
196 if (*cnt
!= 0 && waitqueue_active(&ctx
->wqh
))
197 wake_up_locked_poll(&ctx
->wqh
, EPOLLOUT
);
198 spin_unlock_irqrestore(&ctx
->wqh
.lock
, flags
);
200 return *cnt
!= 0 ? 0 : -EAGAIN
;
202 EXPORT_SYMBOL_GPL(eventfd_ctx_remove_wait_queue
);
204 static ssize_t
eventfd_read(struct file
*file
, char __user
*buf
, size_t count
,
207 struct eventfd_ctx
*ctx
= file
->private_data
;
210 DECLARE_WAITQUEUE(wait
, current
);
212 if (count
< sizeof(ucnt
))
215 spin_lock_irq(&ctx
->wqh
.lock
);
219 else if (!(file
->f_flags
& O_NONBLOCK
)) {
220 __add_wait_queue(&ctx
->wqh
, &wait
);
222 set_current_state(TASK_INTERRUPTIBLE
);
223 if (ctx
->count
> 0) {
227 if (signal_pending(current
)) {
231 spin_unlock_irq(&ctx
->wqh
.lock
);
233 spin_lock_irq(&ctx
->wqh
.lock
);
235 __remove_wait_queue(&ctx
->wqh
, &wait
);
236 __set_current_state(TASK_RUNNING
);
238 if (likely(res
> 0)) {
239 eventfd_ctx_do_read(ctx
, &ucnt
);
240 if (waitqueue_active(&ctx
->wqh
))
241 wake_up_locked_poll(&ctx
->wqh
, EPOLLOUT
);
243 spin_unlock_irq(&ctx
->wqh
.lock
);
245 if (res
> 0 && put_user(ucnt
, (__u64 __user
*)buf
))
251 static ssize_t
eventfd_write(struct file
*file
, const char __user
*buf
, size_t count
,
254 struct eventfd_ctx
*ctx
= file
->private_data
;
257 DECLARE_WAITQUEUE(wait
, current
);
259 if (count
< sizeof(ucnt
))
261 if (copy_from_user(&ucnt
, buf
, sizeof(ucnt
)))
263 if (ucnt
== ULLONG_MAX
)
265 spin_lock_irq(&ctx
->wqh
.lock
);
267 if (ULLONG_MAX
- ctx
->count
> ucnt
)
269 else if (!(file
->f_flags
& O_NONBLOCK
)) {
270 __add_wait_queue(&ctx
->wqh
, &wait
);
272 set_current_state(TASK_INTERRUPTIBLE
);
273 if (ULLONG_MAX
- ctx
->count
> ucnt
) {
277 if (signal_pending(current
)) {
281 spin_unlock_irq(&ctx
->wqh
.lock
);
283 spin_lock_irq(&ctx
->wqh
.lock
);
285 __remove_wait_queue(&ctx
->wqh
, &wait
);
286 __set_current_state(TASK_RUNNING
);
288 if (likely(res
> 0)) {
290 if (waitqueue_active(&ctx
->wqh
))
291 wake_up_locked_poll(&ctx
->wqh
, EPOLLIN
);
293 spin_unlock_irq(&ctx
->wqh
.lock
);
298 #ifdef CONFIG_PROC_FS
299 static void eventfd_show_fdinfo(struct seq_file
*m
, struct file
*f
)
301 struct eventfd_ctx
*ctx
= f
->private_data
;
303 spin_lock_irq(&ctx
->wqh
.lock
);
304 seq_printf(m
, "eventfd-count: %16llx\n",
305 (unsigned long long)ctx
->count
);
306 spin_unlock_irq(&ctx
->wqh
.lock
);
307 seq_printf(m
, "eventfd-id: %d\n", ctx
->id
);
311 static const struct file_operations eventfd_fops
= {
312 #ifdef CONFIG_PROC_FS
313 .show_fdinfo
= eventfd_show_fdinfo
,
315 .release
= eventfd_release
,
316 .poll
= eventfd_poll
,
317 .read
= eventfd_read
,
318 .write
= eventfd_write
,
319 .llseek
= noop_llseek
,
323 * eventfd_fget - Acquire a reference of an eventfd file descriptor.
324 * @fd: [in] Eventfd file descriptor.
326 * Returns a pointer to the eventfd file structure in case of success, or the
327 * following error pointer:
329 * -EBADF : Invalid @fd file descriptor.
330 * -EINVAL : The @fd file descriptor is not an eventfd file.
332 struct file
*eventfd_fget(int fd
)
338 return ERR_PTR(-EBADF
);
339 if (file
->f_op
!= &eventfd_fops
) {
341 return ERR_PTR(-EINVAL
);
346 EXPORT_SYMBOL_GPL(eventfd_fget
);
349 * eventfd_ctx_fdget - Acquires a reference to the internal eventfd context.
350 * @fd: [in] Eventfd file descriptor.
352 * Returns a pointer to the internal eventfd context, otherwise the error
353 * pointers returned by the following functions:
357 struct eventfd_ctx
*eventfd_ctx_fdget(int fd
)
359 struct eventfd_ctx
*ctx
;
360 struct fd f
= fdget(fd
);
362 return ERR_PTR(-EBADF
);
363 ctx
= eventfd_ctx_fileget(f
.file
);
367 EXPORT_SYMBOL_GPL(eventfd_ctx_fdget
);
370 * eventfd_ctx_fileget - Acquires a reference to the internal eventfd context.
371 * @file: [in] Eventfd file pointer.
373 * Returns a pointer to the internal eventfd context, otherwise the error
376 * -EINVAL : The @fd file descriptor is not an eventfd file.
378 struct eventfd_ctx
*eventfd_ctx_fileget(struct file
*file
)
380 struct eventfd_ctx
*ctx
;
382 if (file
->f_op
!= &eventfd_fops
)
383 return ERR_PTR(-EINVAL
);
385 ctx
= file
->private_data
;
386 kref_get(&ctx
->kref
);
389 EXPORT_SYMBOL_GPL(eventfd_ctx_fileget
);
391 static int do_eventfd(unsigned int count
, int flags
)
393 struct eventfd_ctx
*ctx
;
396 /* Check the EFD_* constants for consistency. */
397 BUILD_BUG_ON(EFD_CLOEXEC
!= O_CLOEXEC
);
398 BUILD_BUG_ON(EFD_NONBLOCK
!= O_NONBLOCK
);
400 if (flags
& ~EFD_FLAGS_SET
)
403 ctx
= kmalloc(sizeof(*ctx
), GFP_KERNEL
);
407 kref_init(&ctx
->kref
);
408 init_waitqueue_head(&ctx
->wqh
);
411 ctx
->id
= ida_simple_get(&eventfd_ida
, 0, 0, GFP_KERNEL
);
413 fd
= anon_inode_getfd("[eventfd]", &eventfd_fops
, ctx
,
414 O_RDWR
| (flags
& EFD_SHARED_FCNTL_FLAGS
));
416 eventfd_free_ctx(ctx
);
421 SYSCALL_DEFINE2(eventfd2
, unsigned int, count
, int, flags
)
423 return do_eventfd(count
, flags
);
426 SYSCALL_DEFINE1(eventfd
, unsigned int, count
)
428 return do_eventfd(count
, 0);