1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2e1ad7468SDavide Libenzi /* 3e1ad7468SDavide Libenzi * fs/eventfd.c 4e1ad7468SDavide Libenzi * 5e1ad7468SDavide Libenzi * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org> 6e1ad7468SDavide Libenzi * 7e1ad7468SDavide Libenzi */ 8e1ad7468SDavide Libenzi 9e1ad7468SDavide Libenzi #include <linux/file.h> 10e1ad7468SDavide Libenzi #include <linux/poll.h> 11e1ad7468SDavide Libenzi #include <linux/init.h> 12e1ad7468SDavide Libenzi #include <linux/fs.h> 13174cd4b1SIngo Molnar #include <linux/sched/signal.h> 14e1ad7468SDavide Libenzi #include <linux/kernel.h> 155a0e3ad6STejun Heo #include <linux/slab.h> 16e1ad7468SDavide Libenzi #include <linux/list.h> 17e1ad7468SDavide Libenzi #include <linux/spinlock.h> 18e1ad7468SDavide Libenzi #include <linux/anon_inodes.h> 197747cdb2SAdrian Bunk #include <linux/syscalls.h> 20630d9c47SPaul Gortmaker #include <linux/export.h> 2113389010SDavide Libenzi #include <linux/kref.h> 2213389010SDavide Libenzi #include <linux/eventfd.h> 23cbac5542SCyrill Gorcunov #include <linux/proc_fs.h> 24cbac5542SCyrill Gorcunov #include <linux/seq_file.h> 25b556db17SMasatake YAMATO #include <linux/idr.h> 2612aceb89SJens Axboe #include <linux/uio.h> 27b556db17SMasatake YAMATO 28ce528c4cSYueHaibing static DEFINE_IDA(eventfd_ida); 29e1ad7468SDavide Libenzi 30e1ad7468SDavide Libenzi struct eventfd_ctx { 3113389010SDavide Libenzi struct kref kref; 32e1ad7468SDavide Libenzi wait_queue_head_t wqh; 33e1ad7468SDavide Libenzi /* 34e1ad7468SDavide Libenzi * Every time that a write(2) is performed on an eventfd, the 35e1ad7468SDavide Libenzi * value of the __u64 being written is added to "count" and a 36e1ad7468SDavide Libenzi * wakeup is performed on "wqh". A read(2) will return the "count" 37e1ad7468SDavide Libenzi * value to userspace, and will reset "count" to zero. The kernel 3813389010SDavide Libenzi * side eventfd_signal() also, adds to the "count" counter and 39e1ad7468SDavide Libenzi * issue a wakeup. 40e1ad7468SDavide Libenzi */ 41e1ad7468SDavide Libenzi __u64 count; 42bcd0b235SDavide Libenzi unsigned int flags; 43b556db17SMasatake YAMATO int id; 44e1ad7468SDavide Libenzi }; 45e1ad7468SDavide Libenzi 4613389010SDavide Libenzi /** 4713389010SDavide Libenzi * eventfd_signal - Adds @n to the eventfd counter. 4813389010SDavide Libenzi * @ctx: [in] Pointer to the eventfd context. 4913389010SDavide Libenzi * @n: [in] Value of the counter to be added to the eventfd internal counter. 5013389010SDavide Libenzi * The value cannot be negative. 5113389010SDavide Libenzi * 5213389010SDavide Libenzi * This function is supposed to be called by the kernel in paths that do not 5313389010SDavide Libenzi * allow sleeping. In this function we allow the counter to reach the ULLONG_MAX 54a9a08845SLinus Torvalds * value, and we signal this as overflow condition by returning a EPOLLERR 5513389010SDavide Libenzi * to poll(2). 5613389010SDavide Libenzi * 5720d5a865SMasanari Iida * Returns the amount by which the counter was incremented. This will be less 58ee62c6b2SSha Zhengju * than @n if the counter has overflowed. 59e1ad7468SDavide Libenzi */ 60ee62c6b2SSha Zhengju __u64 eventfd_signal(struct eventfd_ctx *ctx, __u64 n) 61e1ad7468SDavide Libenzi { 62e1ad7468SDavide Libenzi unsigned long flags; 63e1ad7468SDavide Libenzi 64b5e683d5SJens Axboe /* 65b5e683d5SJens Axboe * Deadlock or stack overflow issues can happen if we recurse here 66b5e683d5SJens Axboe * through waitqueue wakeup handlers. If the caller users potentially 67b5e683d5SJens Axboe * nested waitqueues with custom wakeup handlers, then it should 68*b542e383SThomas Gleixner * check eventfd_signal_allowed() before calling this function. If 69*b542e383SThomas Gleixner * it returns false, the eventfd_signal() call should be deferred to a 70b5e683d5SJens Axboe * safe context. 71b5e683d5SJens Axboe */ 72*b542e383SThomas Gleixner if (WARN_ON_ONCE(current->in_eventfd_signal)) 73b5e683d5SJens Axboe return 0; 74b5e683d5SJens Axboe 75d48eb233SDavide Libenzi spin_lock_irqsave(&ctx->wqh.lock, flags); 76*b542e383SThomas Gleixner current->in_eventfd_signal = 1; 77e1ad7468SDavide Libenzi if (ULLONG_MAX - ctx->count < n) 78ee62c6b2SSha Zhengju n = ULLONG_MAX - ctx->count; 79e1ad7468SDavide Libenzi ctx->count += n; 80e1ad7468SDavide Libenzi if (waitqueue_active(&ctx->wqh)) 81a9a08845SLinus Torvalds wake_up_locked_poll(&ctx->wqh, EPOLLIN); 82*b542e383SThomas Gleixner current->in_eventfd_signal = 0; 83d48eb233SDavide Libenzi spin_unlock_irqrestore(&ctx->wqh.lock, flags); 84e1ad7468SDavide Libenzi 85e1ad7468SDavide Libenzi return n; 86e1ad7468SDavide Libenzi } 875718607bSRusty Russell EXPORT_SYMBOL_GPL(eventfd_signal); 88e1ad7468SDavide Libenzi 89562787a5SDavide Libenzi static void eventfd_free_ctx(struct eventfd_ctx *ctx) 90562787a5SDavide Libenzi { 91b556db17SMasatake YAMATO if (ctx->id >= 0) 92b556db17SMasatake YAMATO ida_simple_remove(&eventfd_ida, ctx->id); 93562787a5SDavide Libenzi kfree(ctx); 94562787a5SDavide Libenzi } 95562787a5SDavide Libenzi 9613389010SDavide Libenzi static void eventfd_free(struct kref *kref) 9713389010SDavide Libenzi { 9813389010SDavide Libenzi struct eventfd_ctx *ctx = container_of(kref, struct eventfd_ctx, kref); 9913389010SDavide Libenzi 100562787a5SDavide Libenzi eventfd_free_ctx(ctx); 10113389010SDavide Libenzi } 10213389010SDavide Libenzi 10313389010SDavide Libenzi /** 10413389010SDavide Libenzi * eventfd_ctx_put - Releases a reference to the internal eventfd context. 10513389010SDavide Libenzi * @ctx: [in] Pointer to eventfd context. 10613389010SDavide Libenzi * 10713389010SDavide Libenzi * The eventfd context reference must have been previously acquired either 108105f2b70SEric Biggers * with eventfd_ctx_fdget() or eventfd_ctx_fileget(). 10913389010SDavide Libenzi */ 11013389010SDavide Libenzi void eventfd_ctx_put(struct eventfd_ctx *ctx) 11113389010SDavide Libenzi { 11213389010SDavide Libenzi kref_put(&ctx->kref, eventfd_free); 11313389010SDavide Libenzi } 11413389010SDavide Libenzi EXPORT_SYMBOL_GPL(eventfd_ctx_put); 11513389010SDavide Libenzi 116e1ad7468SDavide Libenzi static int eventfd_release(struct inode *inode, struct file *file) 117e1ad7468SDavide Libenzi { 11813389010SDavide Libenzi struct eventfd_ctx *ctx = file->private_data; 11913389010SDavide Libenzi 120a9a08845SLinus Torvalds wake_up_poll(&ctx->wqh, EPOLLHUP); 12113389010SDavide Libenzi eventfd_ctx_put(ctx); 122e1ad7468SDavide Libenzi return 0; 123e1ad7468SDavide Libenzi } 124e1ad7468SDavide Libenzi 125a11e1d43SLinus Torvalds static __poll_t eventfd_poll(struct file *file, poll_table *wait) 126e1ad7468SDavide Libenzi { 127e1ad7468SDavide Libenzi struct eventfd_ctx *ctx = file->private_data; 128076ccb76SAl Viro __poll_t events = 0; 129e22553e2SChris Mason u64 count; 130e1ad7468SDavide Libenzi 131a11e1d43SLinus Torvalds poll_wait(file, &ctx->wqh, wait); 132a11e1d43SLinus Torvalds 133a484c3ddSPaolo Bonzini /* 134a484c3ddSPaolo Bonzini * All writes to ctx->count occur within ctx->wqh.lock. This read 135a484c3ddSPaolo Bonzini * can be done outside ctx->wqh.lock because we know that poll_wait 136a484c3ddSPaolo Bonzini * takes that lock (through add_wait_queue) if our caller will sleep. 137a484c3ddSPaolo Bonzini * 138a484c3ddSPaolo Bonzini * The read _can_ therefore seep into add_wait_queue's critical 139a484c3ddSPaolo Bonzini * section, but cannot move above it! add_wait_queue's spin_lock acts 140a484c3ddSPaolo Bonzini * as an acquire barrier and ensures that the read be ordered properly 141a484c3ddSPaolo Bonzini * against the writes. The following CAN happen and is safe: 142a484c3ddSPaolo Bonzini * 143a484c3ddSPaolo Bonzini * poll write 144a484c3ddSPaolo Bonzini * ----------------- ------------ 145a484c3ddSPaolo Bonzini * lock ctx->wqh.lock (in poll_wait) 146a484c3ddSPaolo Bonzini * count = ctx->count 147a484c3ddSPaolo Bonzini * __add_wait_queue 148a484c3ddSPaolo Bonzini * unlock ctx->wqh.lock 149a484c3ddSPaolo Bonzini * lock ctx->qwh.lock 150a484c3ddSPaolo Bonzini * ctx->count += n 151a484c3ddSPaolo Bonzini * if (waitqueue_active) 152a484c3ddSPaolo Bonzini * wake_up_locked_poll 153a484c3ddSPaolo Bonzini * unlock ctx->qwh.lock 154a484c3ddSPaolo Bonzini * eventfd_poll returns 0 155a484c3ddSPaolo Bonzini * 156a484c3ddSPaolo Bonzini * but the following, which would miss a wakeup, cannot happen: 157a484c3ddSPaolo Bonzini * 158a484c3ddSPaolo Bonzini * poll write 159a484c3ddSPaolo Bonzini * ----------------- ------------ 160a484c3ddSPaolo Bonzini * count = ctx->count (INVALID!) 161a484c3ddSPaolo Bonzini * lock ctx->qwh.lock 162a484c3ddSPaolo Bonzini * ctx->count += n 163a484c3ddSPaolo Bonzini * **waitqueue_active is false** 164a484c3ddSPaolo Bonzini * **no wake_up_locked_poll!** 165a484c3ddSPaolo Bonzini * unlock ctx->qwh.lock 166a484c3ddSPaolo Bonzini * lock ctx->wqh.lock (in poll_wait) 167a484c3ddSPaolo Bonzini * __add_wait_queue 168a484c3ddSPaolo Bonzini * unlock ctx->wqh.lock 169a484c3ddSPaolo Bonzini * eventfd_poll returns 0 170a484c3ddSPaolo Bonzini */ 171a484c3ddSPaolo Bonzini count = READ_ONCE(ctx->count); 172e1ad7468SDavide Libenzi 173e22553e2SChris Mason if (count > 0) 174a11e1d43SLinus Torvalds events |= EPOLLIN; 175e22553e2SChris Mason if (count == ULLONG_MAX) 176a9a08845SLinus Torvalds events |= EPOLLERR; 177e22553e2SChris Mason if (ULLONG_MAX - 1 > count) 178a11e1d43SLinus Torvalds events |= EPOLLOUT; 179e1ad7468SDavide Libenzi 180e1ad7468SDavide Libenzi return events; 181e1ad7468SDavide Libenzi } 182e1ad7468SDavide Libenzi 18328f13267SDavid Woodhouse void eventfd_ctx_do_read(struct eventfd_ctx *ctx, __u64 *cnt) 184e1ad7468SDavide Libenzi { 18528f13267SDavid Woodhouse lockdep_assert_held(&ctx->wqh.lock); 18628f13267SDavid Woodhouse 187cb289d62SDavide Libenzi *cnt = (ctx->flags & EFD_SEMAPHORE) ? 1 : ctx->count; 188cb289d62SDavide Libenzi ctx->count -= *cnt; 189cb289d62SDavide Libenzi } 19028f13267SDavid Woodhouse EXPORT_SYMBOL_GPL(eventfd_ctx_do_read); 191cb289d62SDavide Libenzi 192cb289d62SDavide Libenzi /** 193cb289d62SDavide Libenzi * eventfd_ctx_remove_wait_queue - Read the current counter and removes wait queue. 194cb289d62SDavide Libenzi * @ctx: [in] Pointer to eventfd context. 195cb289d62SDavide Libenzi * @wait: [in] Wait queue to be removed. 19636182185SRandy Dunlap * @cnt: [out] Pointer to the 64-bit counter value. 197cb289d62SDavide Libenzi * 19836182185SRandy Dunlap * Returns %0 if successful, or the following error codes: 199cb289d62SDavide Libenzi * 200cb289d62SDavide Libenzi * -EAGAIN : The operation would have blocked. 201cb289d62SDavide Libenzi * 202cb289d62SDavide Libenzi * This is used to atomically remove a wait queue entry from the eventfd wait 203cb289d62SDavide Libenzi * queue head, and read/reset the counter value. 204cb289d62SDavide Libenzi */ 205ac6424b9SIngo Molnar int eventfd_ctx_remove_wait_queue(struct eventfd_ctx *ctx, wait_queue_entry_t *wait, 206cb289d62SDavide Libenzi __u64 *cnt) 207cb289d62SDavide Libenzi { 208cb289d62SDavide Libenzi unsigned long flags; 209cb289d62SDavide Libenzi 210cb289d62SDavide Libenzi spin_lock_irqsave(&ctx->wqh.lock, flags); 211cb289d62SDavide Libenzi eventfd_ctx_do_read(ctx, cnt); 212cb289d62SDavide Libenzi __remove_wait_queue(&ctx->wqh, wait); 213cb289d62SDavide Libenzi if (*cnt != 0 && waitqueue_active(&ctx->wqh)) 214a9a08845SLinus Torvalds wake_up_locked_poll(&ctx->wqh, EPOLLOUT); 215cb289d62SDavide Libenzi spin_unlock_irqrestore(&ctx->wqh.lock, flags); 216cb289d62SDavide Libenzi 217cb289d62SDavide Libenzi return *cnt != 0 ? 0 : -EAGAIN; 218cb289d62SDavide Libenzi } 219cb289d62SDavide Libenzi EXPORT_SYMBOL_GPL(eventfd_ctx_remove_wait_queue); 220cb289d62SDavide Libenzi 22112aceb89SJens Axboe static ssize_t eventfd_read(struct kiocb *iocb, struct iov_iter *to) 222cb289d62SDavide Libenzi { 22312aceb89SJens Axboe struct file *file = iocb->ki_filp; 224b6364572SEric Biggers struct eventfd_ctx *ctx = file->private_data; 225b6364572SEric Biggers __u64 ucnt = 0; 226e1ad7468SDavide Libenzi DECLARE_WAITQUEUE(wait, current); 227e1ad7468SDavide Libenzi 22812aceb89SJens Axboe if (iov_iter_count(to) < sizeof(ucnt)) 229b6364572SEric Biggers return -EINVAL; 230d48eb233SDavide Libenzi spin_lock_irq(&ctx->wqh.lock); 23112aceb89SJens Axboe if (!ctx->count) { 23212aceb89SJens Axboe if ((file->f_flags & O_NONBLOCK) || 23312aceb89SJens Axboe (iocb->ki_flags & IOCB_NOWAIT)) { 23412aceb89SJens Axboe spin_unlock_irq(&ctx->wqh.lock); 23512aceb89SJens Axboe return -EAGAIN; 23612aceb89SJens Axboe } 237e1ad7468SDavide Libenzi __add_wait_queue(&ctx->wqh, &wait); 238cb289d62SDavide Libenzi for (;;) { 239e1ad7468SDavide Libenzi set_current_state(TASK_INTERRUPTIBLE); 24012aceb89SJens Axboe if (ctx->count) 241e1ad7468SDavide Libenzi break; 242e1ad7468SDavide Libenzi if (signal_pending(current)) { 24312aceb89SJens Axboe __remove_wait_queue(&ctx->wqh, &wait); 24412aceb89SJens Axboe __set_current_state(TASK_RUNNING); 24512aceb89SJens Axboe spin_unlock_irq(&ctx->wqh.lock); 24612aceb89SJens Axboe return -ERESTARTSYS; 247e1ad7468SDavide Libenzi } 248d48eb233SDavide Libenzi spin_unlock_irq(&ctx->wqh.lock); 249e1ad7468SDavide Libenzi schedule(); 250d48eb233SDavide Libenzi spin_lock_irq(&ctx->wqh.lock); 251e1ad7468SDavide Libenzi } 252e1ad7468SDavide Libenzi __remove_wait_queue(&ctx->wqh, &wait); 253e1ad7468SDavide Libenzi __set_current_state(TASK_RUNNING); 254e1ad7468SDavide Libenzi } 255b6364572SEric Biggers eventfd_ctx_do_read(ctx, &ucnt); 256e1ad7468SDavide Libenzi if (waitqueue_active(&ctx->wqh)) 257a9a08845SLinus Torvalds wake_up_locked_poll(&ctx->wqh, EPOLLOUT); 258d48eb233SDavide Libenzi spin_unlock_irq(&ctx->wqh.lock); 25912aceb89SJens Axboe if (unlikely(copy_to_iter(&ucnt, sizeof(ucnt), to) != sizeof(ucnt))) 260b6364572SEric Biggers return -EFAULT; 261b6364572SEric Biggers 26212aceb89SJens Axboe return sizeof(ucnt); 263e1ad7468SDavide Libenzi } 264e1ad7468SDavide Libenzi 265e1ad7468SDavide Libenzi static ssize_t eventfd_write(struct file *file, const char __user *buf, size_t count, 266e1ad7468SDavide Libenzi loff_t *ppos) 267e1ad7468SDavide Libenzi { 268e1ad7468SDavide Libenzi struct eventfd_ctx *ctx = file->private_data; 269e1ad7468SDavide Libenzi ssize_t res; 270e1ad7468SDavide Libenzi __u64 ucnt; 271e1ad7468SDavide Libenzi DECLARE_WAITQUEUE(wait, current); 272e1ad7468SDavide Libenzi 273e1ad7468SDavide Libenzi if (count < sizeof(ucnt)) 274e1ad7468SDavide Libenzi return -EINVAL; 275e1ad7468SDavide Libenzi if (copy_from_user(&ucnt, buf, sizeof(ucnt))) 276e1ad7468SDavide Libenzi return -EFAULT; 277e1ad7468SDavide Libenzi if (ucnt == ULLONG_MAX) 278e1ad7468SDavide Libenzi return -EINVAL; 279d48eb233SDavide Libenzi spin_lock_irq(&ctx->wqh.lock); 280e1ad7468SDavide Libenzi res = -EAGAIN; 281e1ad7468SDavide Libenzi if (ULLONG_MAX - ctx->count > ucnt) 282e1ad7468SDavide Libenzi res = sizeof(ucnt); 283e1ad7468SDavide Libenzi else if (!(file->f_flags & O_NONBLOCK)) { 284e1ad7468SDavide Libenzi __add_wait_queue(&ctx->wqh, &wait); 285e1ad7468SDavide Libenzi for (res = 0;;) { 286e1ad7468SDavide Libenzi set_current_state(TASK_INTERRUPTIBLE); 287e1ad7468SDavide Libenzi if (ULLONG_MAX - ctx->count > ucnt) { 288e1ad7468SDavide Libenzi res = sizeof(ucnt); 289e1ad7468SDavide Libenzi break; 290e1ad7468SDavide Libenzi } 291e1ad7468SDavide Libenzi if (signal_pending(current)) { 292e1ad7468SDavide Libenzi res = -ERESTARTSYS; 293e1ad7468SDavide Libenzi break; 294e1ad7468SDavide Libenzi } 295d48eb233SDavide Libenzi spin_unlock_irq(&ctx->wqh.lock); 296e1ad7468SDavide Libenzi schedule(); 297d48eb233SDavide Libenzi spin_lock_irq(&ctx->wqh.lock); 298e1ad7468SDavide Libenzi } 299e1ad7468SDavide Libenzi __remove_wait_queue(&ctx->wqh, &wait); 300e1ad7468SDavide Libenzi __set_current_state(TASK_RUNNING); 301e1ad7468SDavide Libenzi } 302bcd0b235SDavide Libenzi if (likely(res > 0)) { 303e1ad7468SDavide Libenzi ctx->count += ucnt; 304e1ad7468SDavide Libenzi if (waitqueue_active(&ctx->wqh)) 305a9a08845SLinus Torvalds wake_up_locked_poll(&ctx->wqh, EPOLLIN); 306e1ad7468SDavide Libenzi } 307d48eb233SDavide Libenzi spin_unlock_irq(&ctx->wqh.lock); 308e1ad7468SDavide Libenzi 309e1ad7468SDavide Libenzi return res; 310e1ad7468SDavide Libenzi } 311e1ad7468SDavide Libenzi 312cbac5542SCyrill Gorcunov #ifdef CONFIG_PROC_FS 313a3816ab0SJoe Perches static void eventfd_show_fdinfo(struct seq_file *m, struct file *f) 314cbac5542SCyrill Gorcunov { 315cbac5542SCyrill Gorcunov struct eventfd_ctx *ctx = f->private_data; 316cbac5542SCyrill Gorcunov 317cbac5542SCyrill Gorcunov spin_lock_irq(&ctx->wqh.lock); 318a3816ab0SJoe Perches seq_printf(m, "eventfd-count: %16llx\n", 319cbac5542SCyrill Gorcunov (unsigned long long)ctx->count); 320cbac5542SCyrill Gorcunov spin_unlock_irq(&ctx->wqh.lock); 321b556db17SMasatake YAMATO seq_printf(m, "eventfd-id: %d\n", ctx->id); 322cbac5542SCyrill Gorcunov } 323cbac5542SCyrill Gorcunov #endif 324cbac5542SCyrill Gorcunov 325e1ad7468SDavide Libenzi static const struct file_operations eventfd_fops = { 326cbac5542SCyrill Gorcunov #ifdef CONFIG_PROC_FS 327cbac5542SCyrill Gorcunov .show_fdinfo = eventfd_show_fdinfo, 328cbac5542SCyrill Gorcunov #endif 329e1ad7468SDavide Libenzi .release = eventfd_release, 330a11e1d43SLinus Torvalds .poll = eventfd_poll, 33112aceb89SJens Axboe .read_iter = eventfd_read, 332e1ad7468SDavide Libenzi .write = eventfd_write, 3336038f373SArnd Bergmann .llseek = noop_llseek, 334e1ad7468SDavide Libenzi }; 335e1ad7468SDavide Libenzi 33613389010SDavide Libenzi /** 33713389010SDavide Libenzi * eventfd_fget - Acquire a reference of an eventfd file descriptor. 33813389010SDavide Libenzi * @fd: [in] Eventfd file descriptor. 33913389010SDavide Libenzi * 34013389010SDavide Libenzi * Returns a pointer to the eventfd file structure in case of success, or the 34113389010SDavide Libenzi * following error pointer: 34213389010SDavide Libenzi * 34313389010SDavide Libenzi * -EBADF : Invalid @fd file descriptor. 34413389010SDavide Libenzi * -EINVAL : The @fd file descriptor is not an eventfd file. 34513389010SDavide Libenzi */ 346e1ad7468SDavide Libenzi struct file *eventfd_fget(int fd) 347e1ad7468SDavide Libenzi { 348e1ad7468SDavide Libenzi struct file *file; 349e1ad7468SDavide Libenzi 350e1ad7468SDavide Libenzi file = fget(fd); 351e1ad7468SDavide Libenzi if (!file) 352e1ad7468SDavide Libenzi return ERR_PTR(-EBADF); 353e1ad7468SDavide Libenzi if (file->f_op != &eventfd_fops) { 354e1ad7468SDavide Libenzi fput(file); 355e1ad7468SDavide Libenzi return ERR_PTR(-EINVAL); 356e1ad7468SDavide Libenzi } 357e1ad7468SDavide Libenzi 358e1ad7468SDavide Libenzi return file; 359e1ad7468SDavide Libenzi } 3605718607bSRusty Russell EXPORT_SYMBOL_GPL(eventfd_fget); 361e1ad7468SDavide Libenzi 36213389010SDavide Libenzi /** 36313389010SDavide Libenzi * eventfd_ctx_fdget - Acquires a reference to the internal eventfd context. 36413389010SDavide Libenzi * @fd: [in] Eventfd file descriptor. 36513389010SDavide Libenzi * 36613389010SDavide Libenzi * Returns a pointer to the internal eventfd context, otherwise the error 36713389010SDavide Libenzi * pointers returned by the following functions: 36813389010SDavide Libenzi * 36913389010SDavide Libenzi * eventfd_fget 37013389010SDavide Libenzi */ 37113389010SDavide Libenzi struct eventfd_ctx *eventfd_ctx_fdget(int fd) 37213389010SDavide Libenzi { 37313389010SDavide Libenzi struct eventfd_ctx *ctx; 37436a74117SAl Viro struct fd f = fdget(fd); 37536a74117SAl Viro if (!f.file) 37636a74117SAl Viro return ERR_PTR(-EBADF); 37736a74117SAl Viro ctx = eventfd_ctx_fileget(f.file); 37836a74117SAl Viro fdput(f); 37913389010SDavide Libenzi return ctx; 38013389010SDavide Libenzi } 38113389010SDavide Libenzi EXPORT_SYMBOL_GPL(eventfd_ctx_fdget); 38213389010SDavide Libenzi 38313389010SDavide Libenzi /** 38413389010SDavide Libenzi * eventfd_ctx_fileget - Acquires a reference to the internal eventfd context. 38513389010SDavide Libenzi * @file: [in] Eventfd file pointer. 38613389010SDavide Libenzi * 38713389010SDavide Libenzi * Returns a pointer to the internal eventfd context, otherwise the error 38813389010SDavide Libenzi * pointer: 38913389010SDavide Libenzi * 39013389010SDavide Libenzi * -EINVAL : The @fd file descriptor is not an eventfd file. 39113389010SDavide Libenzi */ 39213389010SDavide Libenzi struct eventfd_ctx *eventfd_ctx_fileget(struct file *file) 39313389010SDavide Libenzi { 394105f2b70SEric Biggers struct eventfd_ctx *ctx; 395105f2b70SEric Biggers 39613389010SDavide Libenzi if (file->f_op != &eventfd_fops) 39713389010SDavide Libenzi return ERR_PTR(-EINVAL); 39813389010SDavide Libenzi 399105f2b70SEric Biggers ctx = file->private_data; 400105f2b70SEric Biggers kref_get(&ctx->kref); 401105f2b70SEric Biggers return ctx; 40213389010SDavide Libenzi } 40313389010SDavide Libenzi EXPORT_SYMBOL_GPL(eventfd_ctx_fileget); 40413389010SDavide Libenzi 4052fc96f83SDominik Brodowski static int do_eventfd(unsigned int count, int flags) 406e1ad7468SDavide Libenzi { 407e1ad7468SDavide Libenzi struct eventfd_ctx *ctx; 40812aceb89SJens Axboe struct file *file; 4097d815165SEric Biggers int fd; 410e1ad7468SDavide Libenzi 411e38b36f3SUlrich Drepper /* Check the EFD_* constants for consistency. */ 412e38b36f3SUlrich Drepper BUILD_BUG_ON(EFD_CLOEXEC != O_CLOEXEC); 413e38b36f3SUlrich Drepper BUILD_BUG_ON(EFD_NONBLOCK != O_NONBLOCK); 414e38b36f3SUlrich Drepper 415bcd0b235SDavide Libenzi if (flags & ~EFD_FLAGS_SET) 4167d815165SEric Biggers return -EINVAL; 417b087498eSUlrich Drepper 418e1ad7468SDavide Libenzi ctx = kmalloc(sizeof(*ctx), GFP_KERNEL); 419e1ad7468SDavide Libenzi if (!ctx) 4207d815165SEric Biggers return -ENOMEM; 421e1ad7468SDavide Libenzi 42213389010SDavide Libenzi kref_init(&ctx->kref); 423e1ad7468SDavide Libenzi init_waitqueue_head(&ctx->wqh); 424e1ad7468SDavide Libenzi ctx->count = count; 425bcd0b235SDavide Libenzi ctx->flags = flags; 426b556db17SMasatake YAMATO ctx->id = ida_simple_get(&eventfd_ida, 0, 0, GFP_KERNEL); 427e1ad7468SDavide Libenzi 42812aceb89SJens Axboe flags &= EFD_SHARED_FCNTL_FLAGS; 42912aceb89SJens Axboe flags |= O_RDWR; 43012aceb89SJens Axboe fd = get_unused_fd_flags(flags); 4317d815165SEric Biggers if (fd < 0) 43212aceb89SJens Axboe goto err; 433562787a5SDavide Libenzi 43412aceb89SJens Axboe file = anon_inode_getfile("[eventfd]", &eventfd_fops, ctx, flags); 43512aceb89SJens Axboe if (IS_ERR(file)) { 43612aceb89SJens Axboe put_unused_fd(fd); 43712aceb89SJens Axboe fd = PTR_ERR(file); 43812aceb89SJens Axboe goto err; 43912aceb89SJens Axboe } 44012aceb89SJens Axboe 44112aceb89SJens Axboe file->f_mode |= FMODE_NOWAIT; 44212aceb89SJens Axboe fd_install(fd, file); 44312aceb89SJens Axboe return fd; 44412aceb89SJens Axboe err: 44512aceb89SJens Axboe eventfd_free_ctx(ctx); 4462030a42cSAl Viro return fd; 447e1ad7468SDavide Libenzi } 448e1ad7468SDavide Libenzi 4492fc96f83SDominik Brodowski SYSCALL_DEFINE2(eventfd2, unsigned int, count, int, flags) 4502fc96f83SDominik Brodowski { 4512fc96f83SDominik Brodowski return do_eventfd(count, flags); 4522fc96f83SDominik Brodowski } 4532fc96f83SDominik Brodowski 454d4e82042SHeiko Carstens SYSCALL_DEFINE1(eventfd, unsigned int, count) 455b087498eSUlrich Drepper { 4562fc96f83SDominik Brodowski return do_eventfd(count, 0); 457b087498eSUlrich Drepper } 458bcd0b235SDavide Libenzi 459