blob: 8aa0ea8c55e873544aab2bc5940475c84608c086 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Davide Libenzie1ad7462007-05-10 22:23:19 -07002/*
3 * fs/eventfd.c
4 *
5 * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org>
6 *
7 */
8
9#include <linux/file.h>
10#include <linux/poll.h>
11#include <linux/init.h>
12#include <linux/fs.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010013#include <linux/sched/signal.h>
Davide Libenzie1ad7462007-05-10 22:23:19 -070014#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Davide Libenzie1ad7462007-05-10 22:23:19 -070016#include <linux/list.h>
17#include <linux/spinlock.h>
18#include <linux/anon_inodes.h>
Adrian Bunk7747cdb2008-02-06 01:36:49 -080019#include <linux/syscalls.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050020#include <linux/export.h>
Davide Libenzi13389012009-06-30 11:41:11 -070021#include <linux/kref.h>
22#include <linux/eventfd.h>
Cyrill Gorcunovcbac5542012-12-17 16:04:57 -080023#include <linux/proc_fs.h>
24#include <linux/seq_file.h>
Masatake YAMATOb556db12019-05-14 15:45:19 -070025#include <linux/idr.h>
26
YueHaibingce528c42019-05-14 15:45:22 -070027static DEFINE_IDA(eventfd_ida);
Davide Libenzie1ad7462007-05-10 22:23:19 -070028
29struct eventfd_ctx {
Davide Libenzi13389012009-06-30 11:41:11 -070030 struct kref kref;
Davide Libenzie1ad7462007-05-10 22:23:19 -070031 wait_queue_head_t wqh;
32 /*
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
Davide Libenzi13389012009-06-30 11:41:11 -070037 * side eventfd_signal() also, adds to the "count" counter and
Davide Libenzie1ad7462007-05-10 22:23:19 -070038 * issue a wakeup.
39 */
40 __u64 count;
Davide Libenzibcd0b232009-03-31 15:24:18 -070041 unsigned int flags;
Masatake YAMATOb556db12019-05-14 15:45:19 -070042 int id;
Davide Libenzie1ad7462007-05-10 22:23:19 -070043};
44
Davide Libenzi13389012009-06-30 11:41:11 -070045/**
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.
50 *
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
Linus Torvaldsa9a08842018-02-11 14:34:03 -080053 * value, and we signal this as overflow condition by returning a EPOLLERR
Davide Libenzi13389012009-06-30 11:41:11 -070054 * to poll(2).
55 *
Masanari Iida20d5a862015-09-22 12:04:17 +090056 * Returns the amount by which the counter was incremented. This will be less
Sha Zhengjuee62c6b2012-05-31 16:26:41 -070057 * than @n if the counter has overflowed.
Davide Libenzie1ad7462007-05-10 22:23:19 -070058 */
Sha Zhengjuee62c6b2012-05-31 16:26:41 -070059__u64 eventfd_signal(struct eventfd_ctx *ctx, __u64 n)
Davide Libenzie1ad7462007-05-10 22:23:19 -070060{
Davide Libenzie1ad7462007-05-10 22:23:19 -070061 unsigned long flags;
62
Davide Libenzid48eb232007-05-18 12:02:33 -070063 spin_lock_irqsave(&ctx->wqh.lock, flags);
Davide Libenzie1ad7462007-05-10 22:23:19 -070064 if (ULLONG_MAX - ctx->count < n)
Sha Zhengjuee62c6b2012-05-31 16:26:41 -070065 n = ULLONG_MAX - ctx->count;
Davide Libenzie1ad7462007-05-10 22:23:19 -070066 ctx->count += n;
67 if (waitqueue_active(&ctx->wqh))
Linus Torvaldsa9a08842018-02-11 14:34:03 -080068 wake_up_locked_poll(&ctx->wqh, EPOLLIN);
Davide Libenzid48eb232007-05-18 12:02:33 -070069 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
Davide Libenzie1ad7462007-05-10 22:23:19 -070070
71 return n;
72}
Rusty Russell57186072009-06-12 22:27:09 -060073EXPORT_SYMBOL_GPL(eventfd_signal);
Davide Libenzie1ad7462007-05-10 22:23:19 -070074
Davide Libenzi562787a2009-09-22 16:43:57 -070075static void eventfd_free_ctx(struct eventfd_ctx *ctx)
76{
Masatake YAMATOb556db12019-05-14 15:45:19 -070077 if (ctx->id >= 0)
78 ida_simple_remove(&eventfd_ida, ctx->id);
Davide Libenzi562787a2009-09-22 16:43:57 -070079 kfree(ctx);
80}
81
Davide Libenzi13389012009-06-30 11:41:11 -070082static void eventfd_free(struct kref *kref)
83{
84 struct eventfd_ctx *ctx = container_of(kref, struct eventfd_ctx, kref);
85
Davide Libenzi562787a2009-09-22 16:43:57 -070086 eventfd_free_ctx(ctx);
Davide Libenzi13389012009-06-30 11:41:11 -070087}
88
89/**
Davide Libenzi13389012009-06-30 11:41:11 -070090 * eventfd_ctx_put - Releases a reference to the internal eventfd context.
91 * @ctx: [in] Pointer to eventfd context.
92 *
93 * The eventfd context reference must have been previously acquired either
Eric Biggers105f2b72018-01-06 09:45:44 -080094 * with eventfd_ctx_fdget() or eventfd_ctx_fileget().
Davide Libenzi13389012009-06-30 11:41:11 -070095 */
96void eventfd_ctx_put(struct eventfd_ctx *ctx)
97{
98 kref_put(&ctx->kref, eventfd_free);
99}
100EXPORT_SYMBOL_GPL(eventfd_ctx_put);
101
Davide Libenzie1ad7462007-05-10 22:23:19 -0700102static int eventfd_release(struct inode *inode, struct file *file)
103{
Davide Libenzi13389012009-06-30 11:41:11 -0700104 struct eventfd_ctx *ctx = file->private_data;
105
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800106 wake_up_poll(&ctx->wqh, EPOLLHUP);
Davide Libenzi13389012009-06-30 11:41:11 -0700107 eventfd_ctx_put(ctx);
Davide Libenzie1ad7462007-05-10 22:23:19 -0700108 return 0;
109}
110
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700111static __poll_t eventfd_poll(struct file *file, poll_table *wait)
Davide Libenzie1ad7462007-05-10 22:23:19 -0700112{
113 struct eventfd_ctx *ctx = file->private_data;
Al Viro076ccb72017-07-03 01:02:18 -0400114 __poll_t events = 0;
Chris Masone22553e2015-02-17 13:46:07 -0800115 u64 count;
Davide Libenzie1ad7462007-05-10 22:23:19 -0700116
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700117 poll_wait(file, &ctx->wqh, wait);
118
Paolo Bonzinia484c3d2016-03-22 14:27:14 -0700119 /*
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.
123 *
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:
128 *
129 * poll write
130 * ----------------- ------------
131 * lock ctx->wqh.lock (in poll_wait)
132 * count = ctx->count
133 * __add_wait_queue
134 * unlock ctx->wqh.lock
135 * lock ctx->qwh.lock
136 * ctx->count += n
137 * if (waitqueue_active)
138 * wake_up_locked_poll
139 * unlock ctx->qwh.lock
140 * eventfd_poll returns 0
141 *
142 * but the following, which would miss a wakeup, cannot happen:
143 *
144 * poll write
145 * ----------------- ------------
146 * count = ctx->count (INVALID!)
147 * lock ctx->qwh.lock
148 * ctx->count += n
149 * **waitqueue_active is false**
150 * **no wake_up_locked_poll!**
151 * unlock ctx->qwh.lock
152 * lock ctx->wqh.lock (in poll_wait)
153 * __add_wait_queue
154 * unlock ctx->wqh.lock
155 * eventfd_poll returns 0
156 */
157 count = READ_ONCE(ctx->count);
Davide Libenzie1ad7462007-05-10 22:23:19 -0700158
Chris Masone22553e2015-02-17 13:46:07 -0800159 if (count > 0)
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700160 events |= EPOLLIN;
Chris Masone22553e2015-02-17 13:46:07 -0800161 if (count == ULLONG_MAX)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800162 events |= EPOLLERR;
Chris Masone22553e2015-02-17 13:46:07 -0800163 if (ULLONG_MAX - 1 > count)
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700164 events |= EPOLLOUT;
Davide Libenzie1ad7462007-05-10 22:23:19 -0700165
166 return events;
167}
168
Davide Libenzicb289d62010-01-13 09:34:36 -0800169static void eventfd_ctx_do_read(struct eventfd_ctx *ctx, __u64 *cnt)
Davide Libenzie1ad7462007-05-10 22:23:19 -0700170{
Davide Libenzicb289d62010-01-13 09:34:36 -0800171 *cnt = (ctx->flags & EFD_SEMAPHORE) ? 1 : ctx->count;
172 ctx->count -= *cnt;
173}
174
175/**
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.
Randy Dunlap36182182011-02-20 20:08:35 -0800179 * @cnt: [out] Pointer to the 64-bit counter value.
Davide Libenzicb289d62010-01-13 09:34:36 -0800180 *
Randy Dunlap36182182011-02-20 20:08:35 -0800181 * Returns %0 if successful, or the following error codes:
Davide Libenzicb289d62010-01-13 09:34:36 -0800182 *
183 * -EAGAIN : The operation would have blocked.
184 *
185 * This is used to atomically remove a wait queue entry from the eventfd wait
186 * queue head, and read/reset the counter value.
187 */
Ingo Molnarac6424b2017-06-20 12:06:13 +0200188int eventfd_ctx_remove_wait_queue(struct eventfd_ctx *ctx, wait_queue_entry_t *wait,
Davide Libenzicb289d62010-01-13 09:34:36 -0800189 __u64 *cnt)
190{
191 unsigned long flags;
192
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))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800197 wake_up_locked_poll(&ctx->wqh, EPOLLOUT);
Davide Libenzicb289d62010-01-13 09:34:36 -0800198 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
199
200 return *cnt != 0 ? 0 : -EAGAIN;
201}
202EXPORT_SYMBOL_GPL(eventfd_ctx_remove_wait_queue);
203
Eric Biggersb6364572018-01-06 09:45:43 -0800204static ssize_t eventfd_read(struct file *file, char __user *buf, size_t count,
205 loff_t *ppos)
Davide Libenzicb289d62010-01-13 09:34:36 -0800206{
Eric Biggersb6364572018-01-06 09:45:43 -0800207 struct eventfd_ctx *ctx = file->private_data;
Davide Libenzie1ad7462007-05-10 22:23:19 -0700208 ssize_t res;
Eric Biggersb6364572018-01-06 09:45:43 -0800209 __u64 ucnt = 0;
Davide Libenzie1ad7462007-05-10 22:23:19 -0700210 DECLARE_WAITQUEUE(wait, current);
211
Eric Biggersb6364572018-01-06 09:45:43 -0800212 if (count < sizeof(ucnt))
213 return -EINVAL;
214
Davide Libenzid48eb232007-05-18 12:02:33 -0700215 spin_lock_irq(&ctx->wqh.lock);
Davide Libenzie1ad7462007-05-10 22:23:19 -0700216 res = -EAGAIN;
Davide Libenzibcd0b232009-03-31 15:24:18 -0700217 if (ctx->count > 0)
Eric Biggersb6364572018-01-06 09:45:43 -0800218 res = sizeof(ucnt);
219 else if (!(file->f_flags & O_NONBLOCK)) {
Davide Libenzie1ad7462007-05-10 22:23:19 -0700220 __add_wait_queue(&ctx->wqh, &wait);
Davide Libenzicb289d62010-01-13 09:34:36 -0800221 for (;;) {
Davide Libenzie1ad7462007-05-10 22:23:19 -0700222 set_current_state(TASK_INTERRUPTIBLE);
223 if (ctx->count > 0) {
Eric Biggersb6364572018-01-06 09:45:43 -0800224 res = sizeof(ucnt);
Davide Libenzie1ad7462007-05-10 22:23:19 -0700225 break;
226 }
227 if (signal_pending(current)) {
228 res = -ERESTARTSYS;
229 break;
230 }
Davide Libenzid48eb232007-05-18 12:02:33 -0700231 spin_unlock_irq(&ctx->wqh.lock);
Davide Libenzie1ad7462007-05-10 22:23:19 -0700232 schedule();
Davide Libenzid48eb232007-05-18 12:02:33 -0700233 spin_lock_irq(&ctx->wqh.lock);
Davide Libenzie1ad7462007-05-10 22:23:19 -0700234 }
235 __remove_wait_queue(&ctx->wqh, &wait);
236 __set_current_state(TASK_RUNNING);
237 }
Eric Biggersb6364572018-01-06 09:45:43 -0800238 if (likely(res > 0)) {
239 eventfd_ctx_do_read(ctx, &ucnt);
Davide Libenzie1ad7462007-05-10 22:23:19 -0700240 if (waitqueue_active(&ctx->wqh))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800241 wake_up_locked_poll(&ctx->wqh, EPOLLOUT);
Davide Libenzie1ad7462007-05-10 22:23:19 -0700242 }
Davide Libenzid48eb232007-05-18 12:02:33 -0700243 spin_unlock_irq(&ctx->wqh.lock);
Davide Libenzie1ad7462007-05-10 22:23:19 -0700244
Eric Biggersb6364572018-01-06 09:45:43 -0800245 if (res > 0 && put_user(ucnt, (__u64 __user *)buf))
246 return -EFAULT;
247
Davide Libenzie1ad7462007-05-10 22:23:19 -0700248 return res;
249}
250
251static ssize_t eventfd_write(struct file *file, const char __user *buf, size_t count,
252 loff_t *ppos)
253{
254 struct eventfd_ctx *ctx = file->private_data;
255 ssize_t res;
256 __u64 ucnt;
257 DECLARE_WAITQUEUE(wait, current);
258
259 if (count < sizeof(ucnt))
260 return -EINVAL;
261 if (copy_from_user(&ucnt, buf, sizeof(ucnt)))
262 return -EFAULT;
263 if (ucnt == ULLONG_MAX)
264 return -EINVAL;
Davide Libenzid48eb232007-05-18 12:02:33 -0700265 spin_lock_irq(&ctx->wqh.lock);
Davide Libenzie1ad7462007-05-10 22:23:19 -0700266 res = -EAGAIN;
267 if (ULLONG_MAX - ctx->count > ucnt)
268 res = sizeof(ucnt);
269 else if (!(file->f_flags & O_NONBLOCK)) {
270 __add_wait_queue(&ctx->wqh, &wait);
271 for (res = 0;;) {
272 set_current_state(TASK_INTERRUPTIBLE);
273 if (ULLONG_MAX - ctx->count > ucnt) {
274 res = sizeof(ucnt);
275 break;
276 }
277 if (signal_pending(current)) {
278 res = -ERESTARTSYS;
279 break;
280 }
Davide Libenzid48eb232007-05-18 12:02:33 -0700281 spin_unlock_irq(&ctx->wqh.lock);
Davide Libenzie1ad7462007-05-10 22:23:19 -0700282 schedule();
Davide Libenzid48eb232007-05-18 12:02:33 -0700283 spin_lock_irq(&ctx->wqh.lock);
Davide Libenzie1ad7462007-05-10 22:23:19 -0700284 }
285 __remove_wait_queue(&ctx->wqh, &wait);
286 __set_current_state(TASK_RUNNING);
287 }
Davide Libenzibcd0b232009-03-31 15:24:18 -0700288 if (likely(res > 0)) {
Davide Libenzie1ad7462007-05-10 22:23:19 -0700289 ctx->count += ucnt;
290 if (waitqueue_active(&ctx->wqh))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800291 wake_up_locked_poll(&ctx->wqh, EPOLLIN);
Davide Libenzie1ad7462007-05-10 22:23:19 -0700292 }
Davide Libenzid48eb232007-05-18 12:02:33 -0700293 spin_unlock_irq(&ctx->wqh.lock);
Davide Libenzie1ad7462007-05-10 22:23:19 -0700294
295 return res;
296}
297
Cyrill Gorcunovcbac5542012-12-17 16:04:57 -0800298#ifdef CONFIG_PROC_FS
Joe Perchesa3816ab2014-09-29 16:08:25 -0700299static void eventfd_show_fdinfo(struct seq_file *m, struct file *f)
Cyrill Gorcunovcbac5542012-12-17 16:04:57 -0800300{
301 struct eventfd_ctx *ctx = f->private_data;
Cyrill Gorcunovcbac5542012-12-17 16:04:57 -0800302
303 spin_lock_irq(&ctx->wqh.lock);
Joe Perchesa3816ab2014-09-29 16:08:25 -0700304 seq_printf(m, "eventfd-count: %16llx\n",
305 (unsigned long long)ctx->count);
Cyrill Gorcunovcbac5542012-12-17 16:04:57 -0800306 spin_unlock_irq(&ctx->wqh.lock);
Masatake YAMATOb556db12019-05-14 15:45:19 -0700307 seq_printf(m, "eventfd-id: %d\n", ctx->id);
Cyrill Gorcunovcbac5542012-12-17 16:04:57 -0800308}
309#endif
310
Davide Libenzie1ad7462007-05-10 22:23:19 -0700311static const struct file_operations eventfd_fops = {
Cyrill Gorcunovcbac5542012-12-17 16:04:57 -0800312#ifdef CONFIG_PROC_FS
313 .show_fdinfo = eventfd_show_fdinfo,
314#endif
Davide Libenzie1ad7462007-05-10 22:23:19 -0700315 .release = eventfd_release,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700316 .poll = eventfd_poll,
Davide Libenzie1ad7462007-05-10 22:23:19 -0700317 .read = eventfd_read,
318 .write = eventfd_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200319 .llseek = noop_llseek,
Davide Libenzie1ad7462007-05-10 22:23:19 -0700320};
321
Davide Libenzi13389012009-06-30 11:41:11 -0700322/**
323 * eventfd_fget - Acquire a reference of an eventfd file descriptor.
324 * @fd: [in] Eventfd file descriptor.
325 *
326 * Returns a pointer to the eventfd file structure in case of success, or the
327 * following error pointer:
328 *
329 * -EBADF : Invalid @fd file descriptor.
330 * -EINVAL : The @fd file descriptor is not an eventfd file.
331 */
Davide Libenzie1ad7462007-05-10 22:23:19 -0700332struct file *eventfd_fget(int fd)
333{
334 struct file *file;
335
336 file = fget(fd);
337 if (!file)
338 return ERR_PTR(-EBADF);
339 if (file->f_op != &eventfd_fops) {
340 fput(file);
341 return ERR_PTR(-EINVAL);
342 }
343
344 return file;
345}
Rusty Russell57186072009-06-12 22:27:09 -0600346EXPORT_SYMBOL_GPL(eventfd_fget);
Davide Libenzie1ad7462007-05-10 22:23:19 -0700347
Davide Libenzi13389012009-06-30 11:41:11 -0700348/**
349 * eventfd_ctx_fdget - Acquires a reference to the internal eventfd context.
350 * @fd: [in] Eventfd file descriptor.
351 *
352 * Returns a pointer to the internal eventfd context, otherwise the error
353 * pointers returned by the following functions:
354 *
355 * eventfd_fget
356 */
357struct eventfd_ctx *eventfd_ctx_fdget(int fd)
358{
Davide Libenzi13389012009-06-30 11:41:11 -0700359 struct eventfd_ctx *ctx;
Al Viro36a74112013-12-23 16:51:33 -0500360 struct fd f = fdget(fd);
361 if (!f.file)
362 return ERR_PTR(-EBADF);
363 ctx = eventfd_ctx_fileget(f.file);
364 fdput(f);
Davide Libenzi13389012009-06-30 11:41:11 -0700365 return ctx;
366}
367EXPORT_SYMBOL_GPL(eventfd_ctx_fdget);
368
369/**
370 * eventfd_ctx_fileget - Acquires a reference to the internal eventfd context.
371 * @file: [in] Eventfd file pointer.
372 *
373 * Returns a pointer to the internal eventfd context, otherwise the error
374 * pointer:
375 *
376 * -EINVAL : The @fd file descriptor is not an eventfd file.
377 */
378struct eventfd_ctx *eventfd_ctx_fileget(struct file *file)
379{
Eric Biggers105f2b72018-01-06 09:45:44 -0800380 struct eventfd_ctx *ctx;
381
Davide Libenzi13389012009-06-30 11:41:11 -0700382 if (file->f_op != &eventfd_fops)
383 return ERR_PTR(-EINVAL);
384
Eric Biggers105f2b72018-01-06 09:45:44 -0800385 ctx = file->private_data;
386 kref_get(&ctx->kref);
387 return ctx;
Davide Libenzi13389012009-06-30 11:41:11 -0700388}
389EXPORT_SYMBOL_GPL(eventfd_ctx_fileget);
390
Dominik Brodowski2fc96f82018-03-11 11:34:37 +0100391static int do_eventfd(unsigned int count, int flags)
Davide Libenzie1ad7462007-05-10 22:23:19 -0700392{
Davide Libenzie1ad7462007-05-10 22:23:19 -0700393 struct eventfd_ctx *ctx;
Eric Biggers7d815162018-01-06 09:45:42 -0800394 int fd;
Davide Libenzie1ad7462007-05-10 22:23:19 -0700395
Ulrich Dreppere38b36f2008-07-23 21:29:42 -0700396 /* Check the EFD_* constants for consistency. */
397 BUILD_BUG_ON(EFD_CLOEXEC != O_CLOEXEC);
398 BUILD_BUG_ON(EFD_NONBLOCK != O_NONBLOCK);
399
Davide Libenzibcd0b232009-03-31 15:24:18 -0700400 if (flags & ~EFD_FLAGS_SET)
Eric Biggers7d815162018-01-06 09:45:42 -0800401 return -EINVAL;
Ulrich Drepperb087498e2008-07-23 21:29:25 -0700402
Davide Libenzie1ad7462007-05-10 22:23:19 -0700403 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
404 if (!ctx)
Eric Biggers7d815162018-01-06 09:45:42 -0800405 return -ENOMEM;
Davide Libenzie1ad7462007-05-10 22:23:19 -0700406
Davide Libenzi13389012009-06-30 11:41:11 -0700407 kref_init(&ctx->kref);
Davide Libenzie1ad7462007-05-10 22:23:19 -0700408 init_waitqueue_head(&ctx->wqh);
Davide Libenzie1ad7462007-05-10 22:23:19 -0700409 ctx->count = count;
Davide Libenzibcd0b232009-03-31 15:24:18 -0700410 ctx->flags = flags;
Masatake YAMATOb556db12019-05-14 15:45:19 -0700411 ctx->id = ida_simple_get(&eventfd_ida, 0, 0, GFP_KERNEL);
Davide Libenzie1ad7462007-05-10 22:23:19 -0700412
Eric Biggers7d815162018-01-06 09:45:42 -0800413 fd = anon_inode_getfd("[eventfd]", &eventfd_fops, ctx,
414 O_RDWR | (flags & EFD_SHARED_FCNTL_FLAGS));
415 if (fd < 0)
Davide Libenzi562787a2009-09-22 16:43:57 -0700416 eventfd_free_ctx(ctx);
417
Al Viro2030a422008-02-23 06:46:49 -0500418 return fd;
Davide Libenzie1ad7462007-05-10 22:23:19 -0700419}
420
Dominik Brodowski2fc96f82018-03-11 11:34:37 +0100421SYSCALL_DEFINE2(eventfd2, unsigned int, count, int, flags)
422{
423 return do_eventfd(count, flags);
424}
425
Heiko Carstensd4e82042009-01-14 14:14:34 +0100426SYSCALL_DEFINE1(eventfd, unsigned int, count)
Ulrich Drepperb087498e2008-07-23 21:29:25 -0700427{
Dominik Brodowski2fc96f82018-03-11 11:34:37 +0100428 return do_eventfd(count, 0);
Ulrich Drepperb087498e2008-07-23 21:29:25 -0700429}
Davide Libenzibcd0b232009-03-31 15:24:18 -0700430