xref: /openbmc/linux/fs/eventfd.c (revision 758b4920)
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
3633d8b5d7SWen Yang 	 * wakeup is performed on "wqh". If EFD_SEMAPHORE flag was not
3733d8b5d7SWen Yang 	 * specified, a read(2) will return the "count" value to userspace,
3833d8b5d7SWen Yang 	 * and will reset "count" to zero. The kernel side eventfd_signal()
3933d8b5d7SWen Yang 	 * also, adds to the "count" counter and issue a wakeup.
40e1ad7468SDavide Libenzi 	 */
41e1ad7468SDavide Libenzi 	__u64 count;
42bcd0b235SDavide Libenzi 	unsigned int flags;
43b556db17SMasatake YAMATO 	int id;
44e1ad7468SDavide Libenzi };
45e1ad7468SDavide Libenzi 
eventfd_signal_mask(struct eventfd_ctx * ctx,__u64 n,__poll_t mask)4638f1755aSMin-Hua Chen __u64 eventfd_signal_mask(struct eventfd_ctx *ctx, __u64 n, __poll_t mask)
47e1ad7468SDavide Libenzi {
48e1ad7468SDavide Libenzi 	unsigned long flags;
49e1ad7468SDavide Libenzi 
50b5e683d5SJens Axboe 	/*
51b5e683d5SJens Axboe 	 * Deadlock or stack overflow issues can happen if we recurse here
52b5e683d5SJens Axboe 	 * through waitqueue wakeup handlers. If the caller users potentially
53b5e683d5SJens Axboe 	 * nested waitqueues with custom wakeup handlers, then it should
54b542e383SThomas Gleixner 	 * check eventfd_signal_allowed() before calling this function. If
55b542e383SThomas Gleixner 	 * it returns false, the eventfd_signal() call should be deferred to a
56b5e683d5SJens Axboe 	 * safe context.
57b5e683d5SJens Axboe 	 */
589f0deaa1SDylan Yudaken 	if (WARN_ON_ONCE(current->in_eventfd))
59b5e683d5SJens Axboe 		return 0;
60b5e683d5SJens Axboe 
61d48eb233SDavide Libenzi 	spin_lock_irqsave(&ctx->wqh.lock, flags);
629f0deaa1SDylan Yudaken 	current->in_eventfd = 1;
63e1ad7468SDavide Libenzi 	if (ULLONG_MAX - ctx->count < n)
64ee62c6b2SSha Zhengju 		n = ULLONG_MAX - ctx->count;
65e1ad7468SDavide Libenzi 	ctx->count += n;
66e1ad7468SDavide Libenzi 	if (waitqueue_active(&ctx->wqh))
6703e02acdSJens Axboe 		wake_up_locked_poll(&ctx->wqh, EPOLLIN | mask);
689f0deaa1SDylan Yudaken 	current->in_eventfd = 0;
69d48eb233SDavide Libenzi 	spin_unlock_irqrestore(&ctx->wqh.lock, flags);
70e1ad7468SDavide Libenzi 
71e1ad7468SDavide Libenzi 	return n;
72e1ad7468SDavide Libenzi }
7303e02acdSJens Axboe 
7403e02acdSJens Axboe /**
7503e02acdSJens Axboe  * eventfd_signal - Adds @n to the eventfd counter.
7603e02acdSJens Axboe  * @ctx: [in] Pointer to the eventfd context.
7703e02acdSJens Axboe  * @n: [in] Value of the counter to be added to the eventfd internal counter.
7803e02acdSJens Axboe  *          The value cannot be negative.
7903e02acdSJens Axboe  *
8003e02acdSJens Axboe  * This function is supposed to be called by the kernel in paths that do not
8103e02acdSJens Axboe  * allow sleeping. In this function we allow the counter to reach the ULLONG_MAX
8203e02acdSJens Axboe  * value, and we signal this as overflow condition by returning a EPOLLERR
8303e02acdSJens Axboe  * to poll(2).
8403e02acdSJens Axboe  *
8503e02acdSJens Axboe  * Returns the amount by which the counter was incremented.  This will be less
8603e02acdSJens Axboe  * than @n if the counter has overflowed.
8703e02acdSJens Axboe  */
eventfd_signal(struct eventfd_ctx * ctx,__u64 n)8803e02acdSJens Axboe __u64 eventfd_signal(struct eventfd_ctx *ctx, __u64 n)
8903e02acdSJens Axboe {
9003e02acdSJens Axboe 	return eventfd_signal_mask(ctx, n, 0);
9103e02acdSJens Axboe }
925718607bSRusty Russell EXPORT_SYMBOL_GPL(eventfd_signal);
93e1ad7468SDavide Libenzi 
eventfd_free_ctx(struct eventfd_ctx * ctx)94562787a5SDavide Libenzi static void eventfd_free_ctx(struct eventfd_ctx *ctx)
95562787a5SDavide Libenzi {
96b556db17SMasatake YAMATO 	if (ctx->id >= 0)
97b556db17SMasatake YAMATO 		ida_simple_remove(&eventfd_ida, ctx->id);
98562787a5SDavide Libenzi 	kfree(ctx);
99562787a5SDavide Libenzi }
100562787a5SDavide Libenzi 
eventfd_free(struct kref * kref)10113389010SDavide Libenzi static void eventfd_free(struct kref *kref)
10213389010SDavide Libenzi {
10313389010SDavide Libenzi 	struct eventfd_ctx *ctx = container_of(kref, struct eventfd_ctx, kref);
10413389010SDavide Libenzi 
105562787a5SDavide Libenzi 	eventfd_free_ctx(ctx);
10613389010SDavide Libenzi }
10713389010SDavide Libenzi 
10813389010SDavide Libenzi /**
10913389010SDavide Libenzi  * eventfd_ctx_put - Releases a reference to the internal eventfd context.
11013389010SDavide Libenzi  * @ctx: [in] Pointer to eventfd context.
11113389010SDavide Libenzi  *
11213389010SDavide Libenzi  * The eventfd context reference must have been previously acquired either
113105f2b70SEric Biggers  * with eventfd_ctx_fdget() or eventfd_ctx_fileget().
11413389010SDavide Libenzi  */
eventfd_ctx_put(struct eventfd_ctx * ctx)11513389010SDavide Libenzi void eventfd_ctx_put(struct eventfd_ctx *ctx)
11613389010SDavide Libenzi {
11713389010SDavide Libenzi 	kref_put(&ctx->kref, eventfd_free);
11813389010SDavide Libenzi }
11913389010SDavide Libenzi EXPORT_SYMBOL_GPL(eventfd_ctx_put);
12013389010SDavide Libenzi 
eventfd_release(struct inode * inode,struct file * file)121e1ad7468SDavide Libenzi static int eventfd_release(struct inode *inode, struct file *file)
122e1ad7468SDavide Libenzi {
12313389010SDavide Libenzi 	struct eventfd_ctx *ctx = file->private_data;
12413389010SDavide Libenzi 
125a9a08845SLinus Torvalds 	wake_up_poll(&ctx->wqh, EPOLLHUP);
12613389010SDavide Libenzi 	eventfd_ctx_put(ctx);
127e1ad7468SDavide Libenzi 	return 0;
128e1ad7468SDavide Libenzi }
129e1ad7468SDavide Libenzi 
eventfd_poll(struct file * file,poll_table * wait)130a11e1d43SLinus Torvalds static __poll_t eventfd_poll(struct file *file, poll_table *wait)
131e1ad7468SDavide Libenzi {
132e1ad7468SDavide Libenzi 	struct eventfd_ctx *ctx = file->private_data;
133076ccb76SAl Viro 	__poll_t events = 0;
134e22553e2SChris Mason 	u64 count;
135e1ad7468SDavide Libenzi 
136a11e1d43SLinus Torvalds 	poll_wait(file, &ctx->wqh, wait);
137a11e1d43SLinus Torvalds 
138a484c3ddSPaolo Bonzini 	/*
139a484c3ddSPaolo Bonzini 	 * All writes to ctx->count occur within ctx->wqh.lock.  This read
140a484c3ddSPaolo Bonzini 	 * can be done outside ctx->wqh.lock because we know that poll_wait
141a484c3ddSPaolo Bonzini 	 * takes that lock (through add_wait_queue) if our caller will sleep.
142a484c3ddSPaolo Bonzini 	 *
143a484c3ddSPaolo Bonzini 	 * The read _can_ therefore seep into add_wait_queue's critical
144a484c3ddSPaolo Bonzini 	 * section, but cannot move above it!  add_wait_queue's spin_lock acts
145a484c3ddSPaolo Bonzini 	 * as an acquire barrier and ensures that the read be ordered properly
146a484c3ddSPaolo Bonzini 	 * against the writes.  The following CAN happen and is safe:
147a484c3ddSPaolo Bonzini 	 *
148a484c3ddSPaolo Bonzini 	 *     poll                               write
149a484c3ddSPaolo Bonzini 	 *     -----------------                  ------------
150a484c3ddSPaolo Bonzini 	 *     lock ctx->wqh.lock (in poll_wait)
151a484c3ddSPaolo Bonzini 	 *     count = ctx->count
152a484c3ddSPaolo Bonzini 	 *     __add_wait_queue
153a484c3ddSPaolo Bonzini 	 *     unlock ctx->wqh.lock
154a484c3ddSPaolo Bonzini 	 *                                        lock ctx->qwh.lock
155a484c3ddSPaolo Bonzini 	 *                                        ctx->count += n
156a484c3ddSPaolo Bonzini 	 *                                        if (waitqueue_active)
157a484c3ddSPaolo Bonzini 	 *                                          wake_up_locked_poll
158a484c3ddSPaolo Bonzini 	 *                                        unlock ctx->qwh.lock
159a484c3ddSPaolo Bonzini 	 *     eventfd_poll returns 0
160a484c3ddSPaolo Bonzini 	 *
161a484c3ddSPaolo Bonzini 	 * but the following, which would miss a wakeup, cannot happen:
162a484c3ddSPaolo Bonzini 	 *
163a484c3ddSPaolo Bonzini 	 *     poll                               write
164a484c3ddSPaolo Bonzini 	 *     -----------------                  ------------
165a484c3ddSPaolo Bonzini 	 *     count = ctx->count (INVALID!)
166a484c3ddSPaolo Bonzini 	 *                                        lock ctx->qwh.lock
167a484c3ddSPaolo Bonzini 	 *                                        ctx->count += n
168a484c3ddSPaolo Bonzini 	 *                                        **waitqueue_active is false**
169a484c3ddSPaolo Bonzini 	 *                                        **no wake_up_locked_poll!**
170a484c3ddSPaolo Bonzini 	 *                                        unlock ctx->qwh.lock
171a484c3ddSPaolo Bonzini 	 *     lock ctx->wqh.lock (in poll_wait)
172a484c3ddSPaolo Bonzini 	 *     __add_wait_queue
173a484c3ddSPaolo Bonzini 	 *     unlock ctx->wqh.lock
174a484c3ddSPaolo Bonzini 	 *     eventfd_poll returns 0
175a484c3ddSPaolo Bonzini 	 */
176a484c3ddSPaolo Bonzini 	count = READ_ONCE(ctx->count);
177e1ad7468SDavide Libenzi 
178e22553e2SChris Mason 	if (count > 0)
179a11e1d43SLinus Torvalds 		events |= EPOLLIN;
180e22553e2SChris Mason 	if (count == ULLONG_MAX)
181a9a08845SLinus Torvalds 		events |= EPOLLERR;
182e22553e2SChris Mason 	if (ULLONG_MAX - 1 > count)
183a11e1d43SLinus Torvalds 		events |= EPOLLOUT;
184e1ad7468SDavide Libenzi 
185e1ad7468SDavide Libenzi 	return events;
186e1ad7468SDavide Libenzi }
187e1ad7468SDavide Libenzi 
eventfd_ctx_do_read(struct eventfd_ctx * ctx,__u64 * cnt)18828f13267SDavid Woodhouse void eventfd_ctx_do_read(struct eventfd_ctx *ctx, __u64 *cnt)
189e1ad7468SDavide Libenzi {
19028f13267SDavid Woodhouse 	lockdep_assert_held(&ctx->wqh.lock);
19128f13267SDavid Woodhouse 
192*758b4920SWen Yang 	*cnt = ((ctx->flags & EFD_SEMAPHORE) && ctx->count) ? 1 : ctx->count;
193cb289d62SDavide Libenzi 	ctx->count -= *cnt;
194cb289d62SDavide Libenzi }
19528f13267SDavid Woodhouse EXPORT_SYMBOL_GPL(eventfd_ctx_do_read);
196cb289d62SDavide Libenzi 
197cb289d62SDavide Libenzi /**
198cb289d62SDavide Libenzi  * eventfd_ctx_remove_wait_queue - Read the current counter and removes wait queue.
199cb289d62SDavide Libenzi  * @ctx: [in] Pointer to eventfd context.
200cb289d62SDavide Libenzi  * @wait: [in] Wait queue to be removed.
20136182185SRandy Dunlap  * @cnt: [out] Pointer to the 64-bit counter value.
202cb289d62SDavide Libenzi  *
20336182185SRandy Dunlap  * Returns %0 if successful, or the following error codes:
204cb289d62SDavide Libenzi  *
205cb289d62SDavide Libenzi  * -EAGAIN      : The operation would have blocked.
206cb289d62SDavide Libenzi  *
207cb289d62SDavide Libenzi  * This is used to atomically remove a wait queue entry from the eventfd wait
208cb289d62SDavide Libenzi  * queue head, and read/reset the counter value.
209cb289d62SDavide Libenzi  */
eventfd_ctx_remove_wait_queue(struct eventfd_ctx * ctx,wait_queue_entry_t * wait,__u64 * cnt)210ac6424b9SIngo Molnar int eventfd_ctx_remove_wait_queue(struct eventfd_ctx *ctx, wait_queue_entry_t *wait,
211cb289d62SDavide Libenzi 				  __u64 *cnt)
212cb289d62SDavide Libenzi {
213cb289d62SDavide Libenzi 	unsigned long flags;
214cb289d62SDavide Libenzi 
215cb289d62SDavide Libenzi 	spin_lock_irqsave(&ctx->wqh.lock, flags);
216cb289d62SDavide Libenzi 	eventfd_ctx_do_read(ctx, cnt);
217cb289d62SDavide Libenzi 	__remove_wait_queue(&ctx->wqh, wait);
218cb289d62SDavide Libenzi 	if (*cnt != 0 && waitqueue_active(&ctx->wqh))
219a9a08845SLinus Torvalds 		wake_up_locked_poll(&ctx->wqh, EPOLLOUT);
220cb289d62SDavide Libenzi 	spin_unlock_irqrestore(&ctx->wqh.lock, flags);
221cb289d62SDavide Libenzi 
222cb289d62SDavide Libenzi 	return *cnt != 0 ? 0 : -EAGAIN;
223cb289d62SDavide Libenzi }
224cb289d62SDavide Libenzi EXPORT_SYMBOL_GPL(eventfd_ctx_remove_wait_queue);
225cb289d62SDavide Libenzi 
eventfd_read(struct kiocb * iocb,struct iov_iter * to)22612aceb89SJens Axboe static ssize_t eventfd_read(struct kiocb *iocb, struct iov_iter *to)
227cb289d62SDavide Libenzi {
22812aceb89SJens Axboe 	struct file *file = iocb->ki_filp;
229b6364572SEric Biggers 	struct eventfd_ctx *ctx = file->private_data;
230b6364572SEric Biggers 	__u64 ucnt = 0;
231e1ad7468SDavide Libenzi 
23212aceb89SJens Axboe 	if (iov_iter_count(to) < sizeof(ucnt))
233b6364572SEric Biggers 		return -EINVAL;
234d48eb233SDavide Libenzi 	spin_lock_irq(&ctx->wqh.lock);
23512aceb89SJens Axboe 	if (!ctx->count) {
23612aceb89SJens Axboe 		if ((file->f_flags & O_NONBLOCK) ||
23712aceb89SJens Axboe 		    (iocb->ki_flags & IOCB_NOWAIT)) {
23812aceb89SJens Axboe 			spin_unlock_irq(&ctx->wqh.lock);
23912aceb89SJens Axboe 			return -EAGAIN;
24012aceb89SJens Axboe 		}
241113348a4SWen Yang 
242113348a4SWen Yang 		if (wait_event_interruptible_locked_irq(ctx->wqh, ctx->count)) {
24312aceb89SJens Axboe 			spin_unlock_irq(&ctx->wqh.lock);
24412aceb89SJens Axboe 			return -ERESTARTSYS;
245e1ad7468SDavide Libenzi 		}
246e1ad7468SDavide Libenzi 	}
247b6364572SEric Biggers 	eventfd_ctx_do_read(ctx, &ucnt);
2489f0deaa1SDylan Yudaken 	current->in_eventfd = 1;
249e1ad7468SDavide Libenzi 	if (waitqueue_active(&ctx->wqh))
250a9a08845SLinus Torvalds 		wake_up_locked_poll(&ctx->wqh, EPOLLOUT);
2519f0deaa1SDylan Yudaken 	current->in_eventfd = 0;
252d48eb233SDavide Libenzi 	spin_unlock_irq(&ctx->wqh.lock);
25312aceb89SJens Axboe 	if (unlikely(copy_to_iter(&ucnt, sizeof(ucnt), to) != sizeof(ucnt)))
254b6364572SEric Biggers 		return -EFAULT;
255b6364572SEric Biggers 
25612aceb89SJens Axboe 	return sizeof(ucnt);
257e1ad7468SDavide Libenzi }
258e1ad7468SDavide Libenzi 
eventfd_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)259e1ad7468SDavide Libenzi static ssize_t eventfd_write(struct file *file, const char __user *buf, size_t count,
260e1ad7468SDavide Libenzi 			     loff_t *ppos)
261e1ad7468SDavide Libenzi {
262e1ad7468SDavide Libenzi 	struct eventfd_ctx *ctx = file->private_data;
263e1ad7468SDavide Libenzi 	ssize_t res;
264e1ad7468SDavide Libenzi 	__u64 ucnt;
265e1ad7468SDavide Libenzi 
266e1ad7468SDavide Libenzi 	if (count < sizeof(ucnt))
267e1ad7468SDavide Libenzi 		return -EINVAL;
268e1ad7468SDavide Libenzi 	if (copy_from_user(&ucnt, buf, sizeof(ucnt)))
269e1ad7468SDavide Libenzi 		return -EFAULT;
270e1ad7468SDavide Libenzi 	if (ucnt == ULLONG_MAX)
271e1ad7468SDavide Libenzi 		return -EINVAL;
272d48eb233SDavide Libenzi 	spin_lock_irq(&ctx->wqh.lock);
273e1ad7468SDavide Libenzi 	res = -EAGAIN;
274e1ad7468SDavide Libenzi 	if (ULLONG_MAX - ctx->count > ucnt)
275e1ad7468SDavide Libenzi 		res = sizeof(ucnt);
276e1ad7468SDavide Libenzi 	else if (!(file->f_flags & O_NONBLOCK)) {
277113348a4SWen Yang 		res = wait_event_interruptible_locked_irq(ctx->wqh,
278113348a4SWen Yang 				ULLONG_MAX - ctx->count > ucnt);
279113348a4SWen Yang 		if (!res)
280e1ad7468SDavide Libenzi 			res = sizeof(ucnt);
281e1ad7468SDavide Libenzi 	}
282bcd0b235SDavide Libenzi 	if (likely(res > 0)) {
283e1ad7468SDavide Libenzi 		ctx->count += ucnt;
2849f0deaa1SDylan Yudaken 		current->in_eventfd = 1;
285e1ad7468SDavide Libenzi 		if (waitqueue_active(&ctx->wqh))
286a9a08845SLinus Torvalds 			wake_up_locked_poll(&ctx->wqh, EPOLLIN);
2879f0deaa1SDylan Yudaken 		current->in_eventfd = 0;
288e1ad7468SDavide Libenzi 	}
289d48eb233SDavide Libenzi 	spin_unlock_irq(&ctx->wqh.lock);
290e1ad7468SDavide Libenzi 
291e1ad7468SDavide Libenzi 	return res;
292e1ad7468SDavide Libenzi }
293e1ad7468SDavide Libenzi 
294cbac5542SCyrill Gorcunov #ifdef CONFIG_PROC_FS
eventfd_show_fdinfo(struct seq_file * m,struct file * f)295a3816ab0SJoe Perches static void eventfd_show_fdinfo(struct seq_file *m, struct file *f)
296cbac5542SCyrill Gorcunov {
297cbac5542SCyrill Gorcunov 	struct eventfd_ctx *ctx = f->private_data;
298cbac5542SCyrill Gorcunov 
299cbac5542SCyrill Gorcunov 	spin_lock_irq(&ctx->wqh.lock);
300a3816ab0SJoe Perches 	seq_printf(m, "eventfd-count: %16llx\n",
301cbac5542SCyrill Gorcunov 		   (unsigned long long)ctx->count);
302cbac5542SCyrill Gorcunov 	spin_unlock_irq(&ctx->wqh.lock);
303b556db17SMasatake YAMATO 	seq_printf(m, "eventfd-id: %d\n", ctx->id);
30433d8b5d7SWen Yang 	seq_printf(m, "eventfd-semaphore: %d\n",
30533d8b5d7SWen Yang 		   !!(ctx->flags & EFD_SEMAPHORE));
306cbac5542SCyrill Gorcunov }
307cbac5542SCyrill Gorcunov #endif
308cbac5542SCyrill Gorcunov 
309e1ad7468SDavide Libenzi static const struct file_operations eventfd_fops = {
310cbac5542SCyrill Gorcunov #ifdef CONFIG_PROC_FS
311cbac5542SCyrill Gorcunov 	.show_fdinfo	= eventfd_show_fdinfo,
312cbac5542SCyrill Gorcunov #endif
313e1ad7468SDavide Libenzi 	.release	= eventfd_release,
314a11e1d43SLinus Torvalds 	.poll		= eventfd_poll,
31512aceb89SJens Axboe 	.read_iter	= eventfd_read,
316e1ad7468SDavide Libenzi 	.write		= eventfd_write,
3176038f373SArnd Bergmann 	.llseek		= noop_llseek,
318e1ad7468SDavide Libenzi };
319e1ad7468SDavide Libenzi 
32013389010SDavide Libenzi /**
32113389010SDavide Libenzi  * eventfd_fget - Acquire a reference of an eventfd file descriptor.
32213389010SDavide Libenzi  * @fd: [in] Eventfd file descriptor.
32313389010SDavide Libenzi  *
32413389010SDavide Libenzi  * Returns a pointer to the eventfd file structure in case of success, or the
32513389010SDavide Libenzi  * following error pointer:
32613389010SDavide Libenzi  *
32713389010SDavide Libenzi  * -EBADF    : Invalid @fd file descriptor.
32813389010SDavide Libenzi  * -EINVAL   : The @fd file descriptor is not an eventfd file.
32913389010SDavide Libenzi  */
eventfd_fget(int fd)330e1ad7468SDavide Libenzi struct file *eventfd_fget(int fd)
331e1ad7468SDavide Libenzi {
332e1ad7468SDavide Libenzi 	struct file *file;
333e1ad7468SDavide Libenzi 
334e1ad7468SDavide Libenzi 	file = fget(fd);
335e1ad7468SDavide Libenzi 	if (!file)
336e1ad7468SDavide Libenzi 		return ERR_PTR(-EBADF);
337e1ad7468SDavide Libenzi 	if (file->f_op != &eventfd_fops) {
338e1ad7468SDavide Libenzi 		fput(file);
339e1ad7468SDavide Libenzi 		return ERR_PTR(-EINVAL);
340e1ad7468SDavide Libenzi 	}
341e1ad7468SDavide Libenzi 
342e1ad7468SDavide Libenzi 	return file;
343e1ad7468SDavide Libenzi }
3445718607bSRusty Russell EXPORT_SYMBOL_GPL(eventfd_fget);
345e1ad7468SDavide Libenzi 
34613389010SDavide Libenzi /**
34713389010SDavide Libenzi  * eventfd_ctx_fdget - Acquires a reference to the internal eventfd context.
34813389010SDavide Libenzi  * @fd: [in] Eventfd file descriptor.
34913389010SDavide Libenzi  *
35013389010SDavide Libenzi  * Returns a pointer to the internal eventfd context, otherwise the error
35113389010SDavide Libenzi  * pointers returned by the following functions:
35213389010SDavide Libenzi  *
35313389010SDavide Libenzi  * eventfd_fget
35413389010SDavide Libenzi  */
eventfd_ctx_fdget(int fd)35513389010SDavide Libenzi struct eventfd_ctx *eventfd_ctx_fdget(int fd)
35613389010SDavide Libenzi {
35713389010SDavide Libenzi 	struct eventfd_ctx *ctx;
35836a74117SAl Viro 	struct fd f = fdget(fd);
35936a74117SAl Viro 	if (!f.file)
36036a74117SAl Viro 		return ERR_PTR(-EBADF);
36136a74117SAl Viro 	ctx = eventfd_ctx_fileget(f.file);
36236a74117SAl Viro 	fdput(f);
36313389010SDavide Libenzi 	return ctx;
36413389010SDavide Libenzi }
36513389010SDavide Libenzi EXPORT_SYMBOL_GPL(eventfd_ctx_fdget);
36613389010SDavide Libenzi 
36713389010SDavide Libenzi /**
36813389010SDavide Libenzi  * eventfd_ctx_fileget - Acquires a reference to the internal eventfd context.
36913389010SDavide Libenzi  * @file: [in] Eventfd file pointer.
37013389010SDavide Libenzi  *
37113389010SDavide Libenzi  * Returns a pointer to the internal eventfd context, otherwise the error
37213389010SDavide Libenzi  * pointer:
37313389010SDavide Libenzi  *
37413389010SDavide Libenzi  * -EINVAL   : The @fd file descriptor is not an eventfd file.
37513389010SDavide Libenzi  */
eventfd_ctx_fileget(struct file * file)37613389010SDavide Libenzi struct eventfd_ctx *eventfd_ctx_fileget(struct file *file)
37713389010SDavide Libenzi {
378105f2b70SEric Biggers 	struct eventfd_ctx *ctx;
379105f2b70SEric Biggers 
38013389010SDavide Libenzi 	if (file->f_op != &eventfd_fops)
38113389010SDavide Libenzi 		return ERR_PTR(-EINVAL);
38213389010SDavide Libenzi 
383105f2b70SEric Biggers 	ctx = file->private_data;
384105f2b70SEric Biggers 	kref_get(&ctx->kref);
385105f2b70SEric Biggers 	return ctx;
38613389010SDavide Libenzi }
38713389010SDavide Libenzi EXPORT_SYMBOL_GPL(eventfd_ctx_fileget);
38813389010SDavide Libenzi 
do_eventfd(unsigned int count,int flags)3892fc96f83SDominik Brodowski static int do_eventfd(unsigned int count, int flags)
390e1ad7468SDavide Libenzi {
391e1ad7468SDavide Libenzi 	struct eventfd_ctx *ctx;
39212aceb89SJens Axboe 	struct file *file;
3937d815165SEric Biggers 	int fd;
394e1ad7468SDavide Libenzi 
395e38b36f3SUlrich Drepper 	/* Check the EFD_* constants for consistency.  */
396e38b36f3SUlrich Drepper 	BUILD_BUG_ON(EFD_CLOEXEC != O_CLOEXEC);
397e38b36f3SUlrich Drepper 	BUILD_BUG_ON(EFD_NONBLOCK != O_NONBLOCK);
398e38b36f3SUlrich Drepper 
399bcd0b235SDavide Libenzi 	if (flags & ~EFD_FLAGS_SET)
4007d815165SEric Biggers 		return -EINVAL;
401b087498eSUlrich Drepper 
402e1ad7468SDavide Libenzi 	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
403e1ad7468SDavide Libenzi 	if (!ctx)
4047d815165SEric Biggers 		return -ENOMEM;
405e1ad7468SDavide Libenzi 
40613389010SDavide Libenzi 	kref_init(&ctx->kref);
407e1ad7468SDavide Libenzi 	init_waitqueue_head(&ctx->wqh);
408e1ad7468SDavide Libenzi 	ctx->count = count;
409bcd0b235SDavide Libenzi 	ctx->flags = flags;
410b556db17SMasatake YAMATO 	ctx->id = ida_simple_get(&eventfd_ida, 0, 0, GFP_KERNEL);
411e1ad7468SDavide Libenzi 
41212aceb89SJens Axboe 	flags &= EFD_SHARED_FCNTL_FLAGS;
41312aceb89SJens Axboe 	flags |= O_RDWR;
41412aceb89SJens Axboe 	fd = get_unused_fd_flags(flags);
4157d815165SEric Biggers 	if (fd < 0)
41612aceb89SJens Axboe 		goto err;
417562787a5SDavide Libenzi 
41812aceb89SJens Axboe 	file = anon_inode_getfile("[eventfd]", &eventfd_fops, ctx, flags);
41912aceb89SJens Axboe 	if (IS_ERR(file)) {
42012aceb89SJens Axboe 		put_unused_fd(fd);
42112aceb89SJens Axboe 		fd = PTR_ERR(file);
42212aceb89SJens Axboe 		goto err;
42312aceb89SJens Axboe 	}
42412aceb89SJens Axboe 
42512aceb89SJens Axboe 	file->f_mode |= FMODE_NOWAIT;
42612aceb89SJens Axboe 	fd_install(fd, file);
42712aceb89SJens Axboe 	return fd;
42812aceb89SJens Axboe err:
42912aceb89SJens Axboe 	eventfd_free_ctx(ctx);
4302030a42cSAl Viro 	return fd;
431e1ad7468SDavide Libenzi }
432e1ad7468SDavide Libenzi 
SYSCALL_DEFINE2(eventfd2,unsigned int,count,int,flags)4332fc96f83SDominik Brodowski SYSCALL_DEFINE2(eventfd2, unsigned int, count, int, flags)
4342fc96f83SDominik Brodowski {
4352fc96f83SDominik Brodowski 	return do_eventfd(count, flags);
4362fc96f83SDominik Brodowski }
4372fc96f83SDominik Brodowski 
SYSCALL_DEFINE1(eventfd,unsigned int,count)438d4e82042SHeiko Carstens SYSCALL_DEFINE1(eventfd, unsigned int, count)
439b087498eSUlrich Drepper {
4402fc96f83SDominik Brodowski 	return do_eventfd(count, 0);
441b087498eSUlrich Drepper }
442bcd0b235SDavide Libenzi 
443