]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/eventpoll.c
ep_scan_ready_list(): prepare to splitup
[mirror_ubuntu-jammy-kernel.git] / fs / eventpoll.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4 2/*
5071f97e
DL
3 * fs/eventpoll.c (Efficient event retrieval implementation)
4 * Copyright (C) 2001,...,2009 Davide Libenzi
1da177e4 5 *
1da177e4 6 * Davide Libenzi <davidel@xmailserver.org>
1da177e4
LT
7 */
8
1da177e4
LT
9#include <linux/init.h>
10#include <linux/kernel.h>
174cd4b1 11#include <linux/sched/signal.h>
1da177e4
LT
12#include <linux/fs.h>
13#include <linux/file.h>
14#include <linux/signal.h>
15#include <linux/errno.h>
16#include <linux/mm.h>
17#include <linux/slab.h>
18#include <linux/poll.h>
1da177e4
LT
19#include <linux/string.h>
20#include <linux/list.h>
21#include <linux/hash.h>
22#include <linux/spinlock.h>
23#include <linux/syscalls.h>
1da177e4
LT
24#include <linux/rbtree.h>
25#include <linux/wait.h>
26#include <linux/eventpoll.h>
27#include <linux/mount.h>
28#include <linux/bitops.h>
144efe3e 29#include <linux/mutex.h>
da66f7cb 30#include <linux/anon_inodes.h>
4d7e30d9 31#include <linux/device.h>
7c0f6ba6 32#include <linux/uaccess.h>
1da177e4
LT
33#include <asm/io.h>
34#include <asm/mman.h>
60063497 35#include <linux/atomic.h>
138d22b5
CG
36#include <linux/proc_fs.h>
37#include <linux/seq_file.h>
35280bd4 38#include <linux/compat.h>
ae10b2b4 39#include <linux/rculist.h>
bf3b9f63 40#include <net/busy_poll.h>
1da177e4 41
1da177e4
LT
42/*
43 * LOCKING:
44 * There are three level of locking required by epoll :
45 *
144efe3e 46 * 1) epmutex (mutex)
c7ea7630 47 * 2) ep->mtx (mutex)
a218cc49 48 * 3) ep->lock (rwlock)
1da177e4
LT
49 *
50 * The acquire order is the one listed above, from 1 to 3.
a218cc49 51 * We need a rwlock (ep->lock) because we manipulate objects
1da177e4
LT
52 * from inside the poll callback, that might be triggered from
53 * a wake_up() that in turn might be called from IRQ context.
54 * So we can't sleep inside the poll callback and hence we need
55 * a spinlock. During the event transfer loop (from kernel to
56 * user space) we could end up sleeping due a copy_to_user(), so
57 * we need a lock that will allow us to sleep. This lock is a
d47de16c
DL
58 * mutex (ep->mtx). It is acquired during the event transfer loop,
59 * during epoll_ctl(EPOLL_CTL_DEL) and during eventpoll_release_file().
60 * Then we also need a global mutex to serialize eventpoll_release_file()
61 * and ep_free().
62 * This mutex is acquired by ep_free() during the epoll file
1da177e4
LT
63 * cleanup path and it is also acquired by eventpoll_release_file()
64 * if a file has been pushed inside an epoll set and it is then
bf6a41db 65 * close()d without a previous call to epoll_ctl(EPOLL_CTL_DEL).
22bacca4
DL
66 * It is also acquired when inserting an epoll fd onto another epoll
67 * fd. We do this so that we walk the epoll tree and ensure that this
68 * insertion does not create a cycle of epoll file descriptors, which
69 * could lead to deadlock. We need a global mutex to prevent two
70 * simultaneous inserts (A into B and B into A) from racing and
71 * constructing a cycle without either insert observing that it is
72 * going to.
d8805e63
NE
73 * It is necessary to acquire multiple "ep->mtx"es at once in the
74 * case when one epoll fd is added to another. In this case, we
75 * always acquire the locks in the order of nesting (i.e. after
76 * epoll_ctl(e1, EPOLL_CTL_ADD, e2), e1->mtx will always be acquired
77 * before e2->mtx). Since we disallow cycles of epoll file
78 * descriptors, this ensures that the mutexes are well-ordered. In
79 * order to communicate this nesting to lockdep, when walking a tree
80 * of epoll file descriptors, we use the current recursion depth as
81 * the lockdep subkey.
d47de16c 82 * It is possible to drop the "ep->mtx" and to use the global
a218cc49 83 * mutex "epmutex" (together with "ep->lock") to have it working,
d47de16c 84 * but having "ep->mtx" will make the interface more scalable.
144efe3e 85 * Events that require holding "epmutex" are very rare, while for
d47de16c
DL
86 * normal operations the epoll private "ep->mtx" will guarantee
87 * a better scalability.
1da177e4
LT
88 */
89
1da177e4 90/* Epoll private bits inside the event mask */
df0108c5 91#define EP_PRIVATE_BITS (EPOLLWAKEUP | EPOLLONESHOT | EPOLLET | EPOLLEXCLUSIVE)
1da177e4 92
a9a08845 93#define EPOLLINOUT_BITS (EPOLLIN | EPOLLOUT)
b6a515c8 94
a9a08845 95#define EPOLLEXCLUSIVE_OK_BITS (EPOLLINOUT_BITS | EPOLLERR | EPOLLHUP | \
b6a515c8
JB
96 EPOLLWAKEUP | EPOLLET | EPOLLEXCLUSIVE)
97
5071f97e
DL
98/* Maximum number of nesting allowed inside epoll sets */
99#define EP_MAX_NESTS 4
1da177e4 100
b611967d
DL
101#define EP_MAX_EVENTS (INT_MAX / sizeof(struct epoll_event))
102
d47de16c
DL
103#define EP_UNACTIVE_PTR ((void *) -1L)
104
7ef9964e
DL
105#define EP_ITEM_COST (sizeof(struct epitem) + sizeof(struct eppoll_entry))
106
1da177e4
LT
107struct epoll_filefd {
108 struct file *file;
109 int fd;
39732ca5 110} __packed;
1da177e4 111
80285b75
AV
112/* Wait structure used by the poll hooks */
113struct eppoll_entry {
114 /* List header used to link this structure to the "struct epitem" */
115 struct eppoll_entry *next;
116
117 /* The "base" pointer is set to the container "struct epitem" */
118 struct epitem *base;
119
120 /*
121 * Wait queue item that will be linked to the target file wait
122 * queue head.
123 */
124 wait_queue_entry_t wait;
125
126 /* The wait queue head that linked the "wait" wait queue item */
127 wait_queue_head_t *whead;
128};
129
d47de16c
DL
130/*
131 * Each file descriptor added to the eventpoll interface will
132 * have an entry of this type linked to the "rbr" RB tree.
39732ca5
EW
133 * Avoid increasing the size of this struct, there can be many thousands
134 * of these on a server and we do not want this to take another cache line.
d47de16c
DL
135 */
136struct epitem {
ae10b2b4
JB
137 union {
138 /* RB tree node links this structure to the eventpoll RB tree */
139 struct rb_node rbn;
140 /* Used to free the struct epitem */
141 struct rcu_head rcu;
142 };
d47de16c
DL
143
144 /* List header used to link this structure to the eventpoll ready list */
145 struct list_head rdllink;
146
c7ea7630
DL
147 /*
148 * Works together "struct eventpoll"->ovflist in keeping the
149 * single linked chain of items.
150 */
151 struct epitem *next;
152
d47de16c
DL
153 /* The file descriptor information this item refers to */
154 struct epoll_filefd ffd;
155
d47de16c 156 /* List containing poll wait queues */
80285b75 157 struct eppoll_entry *pwqlist;
d47de16c
DL
158
159 /* The "container" of this item */
160 struct eventpoll *ep;
161
d47de16c
DL
162 /* List header used to link this item to the "struct file" items list */
163 struct list_head fllink;
164
4d7e30d9 165 /* wakeup_source used when EPOLLWAKEUP is set */
eea1d585 166 struct wakeup_source __rcu *ws;
4d7e30d9 167
c7ea7630
DL
168 /* The structure that describe the interested events and the source fd */
169 struct epoll_event event;
d47de16c
DL
170};
171
1da177e4
LT
172/*
173 * This structure is stored inside the "private_data" member of the file
bf6a41db 174 * structure and represents the main data structure for the eventpoll
1da177e4
LT
175 * interface.
176 */
177struct eventpoll {
1da177e4 178 /*
d47de16c
DL
179 * This mutex is used to ensure that files are not removed
180 * while epoll is using them. This is held during the event
181 * collection loop, the file cleanup path, the epoll file exit
182 * code and the ctl operations.
1da177e4 183 */
d47de16c 184 struct mutex mtx;
1da177e4
LT
185
186 /* Wait queue used by sys_epoll_wait() */
187 wait_queue_head_t wq;
188
189 /* Wait queue used by file->poll() */
190 wait_queue_head_t poll_wait;
191
192 /* List of ready file descriptors */
193 struct list_head rdllist;
194
a218cc49
RP
195 /* Lock which protects rdllist and ovflist */
196 rwlock_t lock;
197
67647d0f 198 /* RB tree root used to store monitored fd structs */
b2ac2ea6 199 struct rb_root_cached rbr;
d47de16c
DL
200
201 /*
202 * This is a single linked list that chains all the "struct epitem" that
25985edc 203 * happened while transferring ready events to userspace w/out
a218cc49 204 * holding ->lock.
d47de16c
DL
205 */
206 struct epitem *ovflist;
7ef9964e 207
4d7e30d9
AH
208 /* wakeup_source used when ep_scan_ready_list is running */
209 struct wakeup_source *ws;
210
7ef9964e
DL
211 /* The user that created the eventpoll descriptor */
212 struct user_struct *user;
28d82dc1
JB
213
214 struct file *file;
215
216 /* used to optimize loop detection check */
18306c40 217 u64 gen;
bf3b9f63
SS
218
219#ifdef CONFIG_NET_RX_BUSY_POLL
220 /* used to track busy poll napi_id */
221 unsigned int napi_id;
222#endif
efcdd350
JB
223
224#ifdef CONFIG_DEBUG_LOCK_ALLOC
225 /* tracks wakeup nests for lockdep validation */
226 u8 nests;
227#endif
1da177e4
LT
228};
229
1da177e4
LT
230/* Wrapper struct used by poll queueing */
231struct ep_pqueue {
232 poll_table pt;
233 struct epitem *epi;
234};
235
5071f97e
DL
236/* Used by the ep_send_events() function as callback private data */
237struct ep_send_events_data {
238 int maxevents;
239 struct epoll_event __user *events;
d7ebbe46 240 int res;
5071f97e
DL
241};
242
7ef9964e
DL
243/*
244 * Configuration options available inside /proc/sys/fs/epoll/
245 */
7ef9964e 246/* Maximum number of epoll watched descriptors, per user */
52bd19f7 247static long max_user_watches __read_mostly;
7ef9964e 248
1da177e4 249/*
d47de16c 250 * This mutex is used to serialize ep_free() and eventpoll_release_file().
1da177e4 251 */
7ef9964e 252static DEFINE_MUTEX(epmutex);
1da177e4 253
18306c40
AV
254static u64 loop_check_gen = 0;
255
22bacca4 256/* Used to check for epoll file descriptor inclusion loops */
6a3890c4 257static struct eventpoll *inserting_into;
22bacca4 258
1da177e4 259/* Slab cache used to allocate "struct epitem" */
e18b890b 260static struct kmem_cache *epi_cache __read_mostly;
1da177e4
LT
261
262/* Slab cache used to allocate "struct eppoll_entry" */
e18b890b 263static struct kmem_cache *pwq_cache __read_mostly;
1da177e4 264
28d82dc1
JB
265/*
266 * List of files with newly added links, where we may need to limit the number
267 * of emanating paths. Protected by the epmutex.
268 */
269static LIST_HEAD(tfile_check_list);
270
7ef9964e
DL
271#ifdef CONFIG_SYSCTL
272
273#include <linux/sysctl.h>
274
eec4844f 275static long long_zero;
52bd19f7 276static long long_max = LONG_MAX;
7ef9964e 277
1f7e0616 278struct ctl_table epoll_table[] = {
7ef9964e
DL
279 {
280 .procname = "max_user_watches",
281 .data = &max_user_watches,
52bd19f7 282 .maxlen = sizeof(max_user_watches),
7ef9964e 283 .mode = 0644,
52bd19f7 284 .proc_handler = proc_doulongvec_minmax,
eec4844f 285 .extra1 = &long_zero,
52bd19f7 286 .extra2 = &long_max,
7ef9964e 287 },
ab09203e 288 { }
7ef9964e
DL
289};
290#endif /* CONFIG_SYSCTL */
291
28d82dc1
JB
292static const struct file_operations eventpoll_fops;
293
294static inline int is_file_epoll(struct file *f)
295{
296 return f->f_op == &eventpoll_fops;
297}
b030a4dd 298
67647d0f 299/* Setup the structure that is used as key for the RB tree */
b030a4dd
PE
300static inline void ep_set_ffd(struct epoll_filefd *ffd,
301 struct file *file, int fd)
302{
303 ffd->file = file;
304 ffd->fd = fd;
305}
306
67647d0f 307/* Compare RB tree keys */
b030a4dd
PE
308static inline int ep_cmp_ffd(struct epoll_filefd *p1,
309 struct epoll_filefd *p2)
310{
311 return (p1->file > p2->file ? +1:
312 (p1->file < p2->file ? -1 : p1->fd - p2->fd));
313}
314
b030a4dd 315/* Tells us if the item is currently linked */
992991c0 316static inline int ep_is_linked(struct epitem *epi)
b030a4dd 317{
992991c0 318 return !list_empty(&epi->rdllink);
b030a4dd
PE
319}
320
ac6424b9 321static inline struct eppoll_entry *ep_pwq_from_wait(wait_queue_entry_t *p)
971316f0
ON
322{
323 return container_of(p, struct eppoll_entry, wait);
324}
325
b030a4dd 326/* Get the "struct epitem" from a wait queue pointer */
ac6424b9 327static inline struct epitem *ep_item_from_wait(wait_queue_entry_t *p)
b030a4dd
PE
328{
329 return container_of(p, struct eppoll_entry, wait)->base;
330}
331
3fb0e584
DL
332/**
333 * ep_events_available - Checks if ready events might be available.
334 *
335 * @ep: Pointer to the eventpoll context.
336 *
337 * Returns: Returns a value different than zero if ready events are available,
338 * or zero otherwise.
339 */
340static inline int ep_events_available(struct eventpoll *ep)
341{
c5a282e9
DB
342 return !list_empty_careful(&ep->rdllist) ||
343 READ_ONCE(ep->ovflist) != EP_UNACTIVE_PTR;
3fb0e584
DL
344}
345
bf3b9f63
SS
346#ifdef CONFIG_NET_RX_BUSY_POLL
347static bool ep_busy_loop_end(void *p, unsigned long start_time)
348{
349 struct eventpoll *ep = p;
350
351 return ep_events_available(ep) || busy_loop_timeout(start_time);
352}
bf3b9f63
SS
353
354/*
355 * Busy poll if globally on and supporting sockets found && no events,
356 * busy loop will return if need_resched or ep_events_available.
357 *
358 * we must do our busy polling with irqs enabled
359 */
360static void ep_busy_loop(struct eventpoll *ep, int nonblock)
361{
bf3b9f63
SS
362 unsigned int napi_id = READ_ONCE(ep->napi_id);
363
364 if ((napi_id >= MIN_NAPI_ID) && net_busy_loop_on())
365 napi_busy_loop(napi_id, nonblock ? NULL : ep_busy_loop_end, ep);
bf3b9f63
SS
366}
367
368static inline void ep_reset_busy_poll_napi_id(struct eventpoll *ep)
369{
bf3b9f63
SS
370 if (ep->napi_id)
371 ep->napi_id = 0;
bf3b9f63
SS
372}
373
374/*
375 * Set epoll busy poll NAPI ID from sk.
376 */
377static inline void ep_set_busy_poll_napi_id(struct epitem *epi)
378{
bf3b9f63
SS
379 struct eventpoll *ep;
380 unsigned int napi_id;
381 struct socket *sock;
382 struct sock *sk;
383 int err;
384
385 if (!net_busy_loop_on())
386 return;
387
388 sock = sock_from_file(epi->ffd.file, &err);
389 if (!sock)
390 return;
391
392 sk = sock->sk;
393 if (!sk)
394 return;
395
396 napi_id = READ_ONCE(sk->sk_napi_id);
397 ep = epi->ep;
398
399 /* Non-NAPI IDs can be rejected
400 * or
401 * Nothing to do if we already have this ID
402 */
403 if (napi_id < MIN_NAPI_ID || napi_id == ep->napi_id)
404 return;
405
406 /* record NAPI ID for use in next busy poll */
407 ep->napi_id = napi_id;
bf3b9f63
SS
408}
409
514056d5
DB
410#else
411
412static inline void ep_busy_loop(struct eventpoll *ep, int nonblock)
413{
414}
415
416static inline void ep_reset_busy_poll_napi_id(struct eventpoll *ep)
417{
418}
419
420static inline void ep_set_busy_poll_napi_id(struct epitem *epi)
421{
422}
423
424#endif /* CONFIG_NET_RX_BUSY_POLL */
425
02edc6fc
SR
426/*
427 * As described in commit 0ccf831cb lockdep: annotate epoll
428 * the use of wait queues used by epoll is done in a very controlled
429 * manner. Wake ups can nest inside each other, but are never done
430 * with the same locking. For example:
431 *
432 * dfd = socket(...);
433 * efd1 = epoll_create();
434 * efd2 = epoll_create();
435 * epoll_ctl(efd1, EPOLL_CTL_ADD, dfd, ...);
436 * epoll_ctl(efd2, EPOLL_CTL_ADD, efd1, ...);
437 *
438 * When a packet arrives to the device underneath "dfd", the net code will
439 * issue a wake_up() on its poll wake list. Epoll (efd1) has installed a
440 * callback wakeup entry on that queue, and the wake_up() performed by the
441 * "dfd" net code will end up in ep_poll_callback(). At this point epoll
442 * (efd1) notices that it may have some event ready, so it needs to wake up
443 * the waiters on its poll wait list (efd2). So it calls ep_poll_safewake()
444 * that ends up in another wake_up(), after having checked about the
445 * recursion constraints. That are, no more than EP_MAX_POLLWAKE_NESTS, to
446 * avoid stack blasting.
447 *
448 * When CONFIG_DEBUG_LOCK_ALLOC is enabled, make sure lockdep can handle
449 * this special case of epoll.
450 */
2dfa4eea 451#ifdef CONFIG_DEBUG_LOCK_ALLOC
57a173bd 452
efcdd350 453static void ep_poll_safewake(struct eventpoll *ep, struct epitem *epi)
5071f97e 454{
efcdd350 455 struct eventpoll *ep_src;
f6520c52 456 unsigned long flags;
efcdd350
JB
457 u8 nests = 0;
458
459 /*
460 * To set the subclass or nesting level for spin_lock_irqsave_nested()
461 * it might be natural to create a per-cpu nest count. However, since
462 * we can recurse on ep->poll_wait.lock, and a non-raw spinlock can
463 * schedule() in the -rt kernel, the per-cpu variable are no longer
464 * protected. Thus, we are introducing a per eventpoll nest field.
465 * If we are not being call from ep_poll_callback(), epi is NULL and
466 * we are at the first level of nesting, 0. Otherwise, we are being
467 * called from ep_poll_callback() and if a previous wakeup source is
468 * not an epoll file itself, we are at depth 1 since the wakeup source
469 * is depth 0. If the wakeup source is a previous epoll file in the
470 * wakeup chain then we use its nests value and record ours as
471 * nests + 1. The previous epoll file nests value is stable since its
472 * already holding its own poll_wait.lock.
473 */
474 if (epi) {
475 if ((is_file_epoll(epi->ffd.file))) {
476 ep_src = epi->ffd.file->private_data;
477 nests = ep_src->nests;
478 } else {
479 nests = 1;
480 }
481 }
482 spin_lock_irqsave_nested(&ep->poll_wait.lock, flags, nests);
483 ep->nests = nests + 1;
484 wake_up_locked_poll(&ep->poll_wait, EPOLLIN);
485 ep->nests = 0;
486 spin_unlock_irqrestore(&ep->poll_wait.lock, flags);
1da177e4
LT
487}
488
57a173bd
JB
489#else
490
efcdd350 491static void ep_poll_safewake(struct eventpoll *ep, struct epitem *epi)
57a173bd 492{
efcdd350 493 wake_up_poll(&ep->poll_wait, EPOLLIN);
57a173bd
JB
494}
495
496#endif
497
971316f0
ON
498static void ep_remove_wait_queue(struct eppoll_entry *pwq)
499{
500 wait_queue_head_t *whead;
501
502 rcu_read_lock();
138e4ad6
ON
503 /*
504 * If it is cleared by POLLFREE, it should be rcu-safe.
505 * If we read NULL we need a barrier paired with
506 * smp_store_release() in ep_poll_callback(), otherwise
507 * we rely on whead->lock.
508 */
509 whead = smp_load_acquire(&pwq->whead);
971316f0
ON
510 if (whead)
511 remove_wait_queue(whead, &pwq->wait);
512 rcu_read_unlock();
513}
514
1da177e4 515/*
d1bc90dd
TB
516 * This function unregisters poll callbacks from the associated file
517 * descriptor. Must be called with "mtx" held (or "epmutex" if called from
518 * ep_free).
1da177e4 519 */
7699acd1 520static void ep_unregister_pollwait(struct eventpoll *ep, struct epitem *epi)
1da177e4 521{
80285b75 522 struct eppoll_entry **p = &epi->pwqlist;
7699acd1 523 struct eppoll_entry *pwq;
1da177e4 524
80285b75
AV
525 while ((pwq = *p) != NULL) {
526 *p = pwq->next;
971316f0 527 ep_remove_wait_queue(pwq);
d1bc90dd 528 kmem_cache_free(pwq_cache, pwq);
1da177e4 529 }
1da177e4
LT
530}
531
eea1d585
EW
532/* call only when ep->mtx is held */
533static inline struct wakeup_source *ep_wakeup_source(struct epitem *epi)
534{
535 return rcu_dereference_check(epi->ws, lockdep_is_held(&epi->ep->mtx));
536}
537
538/* call only when ep->mtx is held */
539static inline void ep_pm_stay_awake(struct epitem *epi)
540{
541 struct wakeup_source *ws = ep_wakeup_source(epi);
542
543 if (ws)
544 __pm_stay_awake(ws);
545}
546
547static inline bool ep_has_wakeup_source(struct epitem *epi)
548{
549 return rcu_access_pointer(epi->ws) ? true : false;
550}
551
552/* call when ep->mtx cannot be held (ep_poll_callback) */
553static inline void ep_pm_stay_awake_rcu(struct epitem *epi)
554{
555 struct wakeup_source *ws;
556
557 rcu_read_lock();
558 ws = rcu_dereference(epi->ws);
559 if (ws)
560 __pm_stay_awake(ws);
561 rcu_read_unlock();
562}
563
db502f8a
AV
564static void ep_start_scan(struct eventpoll *ep,
565 int depth, bool ep_locked,
566 struct list_head *txlist)
5071f97e 567{
92e64178
DB
568 lockdep_assert_irqs_enabled();
569
5071f97e
DL
570 /*
571 * We need to lock this because we could be hit by
e057e15f 572 * eventpoll_release_file() and epoll_ctl().
5071f97e 573 */
67347fe4
JB
574
575 if (!ep_locked)
576 mutex_lock_nested(&ep->mtx, depth);
5071f97e
DL
577
578 /*
579 * Steal the ready list, and re-init the original one to the
580 * empty list. Also, set ep->ovflist to NULL so that events
581 * happening while looping w/out locks, are not lost. We cannot
582 * have the poll callback to queue directly on ep->rdllist,
583 * because we want the "sproc" callback to be able to do it
584 * in a lockless way.
585 */
a218cc49 586 write_lock_irq(&ep->lock);
db502f8a 587 list_splice_init(&ep->rdllist, txlist);
c5a282e9 588 WRITE_ONCE(ep->ovflist, NULL);
a218cc49 589 write_unlock_irq(&ep->lock);
db502f8a 590}
5071f97e 591
db502f8a
AV
592static void ep_done_scan(struct eventpoll *ep,
593 int depth, bool ep_locked,
594 struct list_head *txlist)
595{
596 struct epitem *epi, *nepi;
5071f97e 597
a218cc49 598 write_lock_irq(&ep->lock);
5071f97e
DL
599 /*
600 * During the time we spent inside the "sproc" callback, some
601 * other events might have been queued by the poll callback.
602 * We re-insert them inside the main ready-list here.
603 */
c5a282e9 604 for (nepi = READ_ONCE(ep->ovflist); (epi = nepi) != NULL;
5071f97e
DL
605 nepi = epi->next, epi->next = EP_UNACTIVE_PTR) {
606 /*
607 * We need to check if the item is already in the list.
608 * During the "sproc" callback execution time, items are
609 * queued into ->ovflist but the "txlist" might already
610 * contain them, and the list_splice() below takes care of them.
611 */
992991c0 612 if (!ep_is_linked(epi)) {
c141175d
RP
613 /*
614 * ->ovflist is LIFO, so we have to reverse it in order
615 * to keep in FIFO.
616 */
617 list_add(&epi->rdllink, &ep->rdllist);
eea1d585 618 ep_pm_stay_awake(epi);
4d7e30d9 619 }
5071f97e
DL
620 }
621 /*
622 * We need to set back ep->ovflist to EP_UNACTIVE_PTR, so that after
623 * releasing the lock, events will be queued in the normal way inside
624 * ep->rdllist.
625 */
c5a282e9 626 WRITE_ONCE(ep->ovflist, EP_UNACTIVE_PTR);
5071f97e
DL
627
628 /*
629 * Quickly re-inject items left on "txlist".
630 */
db502f8a 631 list_splice(txlist, &ep->rdllist);
4d7e30d9 632 __pm_relax(ep->ws);
a218cc49 633 write_unlock_irq(&ep->lock);
5071f97e 634
67347fe4
JB
635 if (!ep_locked)
636 mutex_unlock(&ep->mtx);
db502f8a 637}
5071f97e 638
db502f8a
AV
639/**
640 * ep_scan_ready_list - Scans the ready list in a way that makes possible for
641 * the scan code, to call f_op->poll(). Also allows for
642 * O(NumReady) performance.
643 *
644 * @ep: Pointer to the epoll private data structure.
645 * @sproc: Pointer to the scan callback.
646 * @priv: Private opaque data passed to the @sproc callback.
647 * @depth: The current depth of recursive f_op->poll calls.
648 * @ep_locked: caller already holds ep->mtx
649 *
650 * Returns: The same integer error code returned by the @sproc callback.
651 */
652static __poll_t ep_scan_ready_list(struct eventpoll *ep,
653 __poll_t (*sproc)(struct eventpoll *,
654 struct list_head *, void *),
655 void *priv, int depth, bool ep_locked)
656{
657 __poll_t res;
658 LIST_HEAD(txlist);
659
660 ep_start_scan(ep, depth, ep_locked, &txlist);
661 res = (*sproc)(ep, &txlist, priv);
662 ep_done_scan(ep, depth, ep_locked, &txlist);
d85e2aa2 663 return res;
5071f97e
DL
664}
665
ae10b2b4
JB
666static void epi_rcu_free(struct rcu_head *head)
667{
668 struct epitem *epi = container_of(head, struct epitem, rcu);
669 kmem_cache_free(epi_cache, epi);
670}
671
7699acd1
DL
672/*
673 * Removes a "struct epitem" from the eventpoll RB tree and deallocates
c7ea7630 674 * all the associated resources. Must be called with "mtx" held.
7699acd1
DL
675 */
676static int ep_remove(struct eventpoll *ep, struct epitem *epi)
677{
7699acd1 678 struct file *file = epi->ffd.file;
1da177e4 679
92e64178
DB
680 lockdep_assert_irqs_enabled();
681
1da177e4 682 /*
ee8ef0a4 683 * Removes poll wait queue hooks.
1da177e4 684 */
7699acd1 685 ep_unregister_pollwait(ep, epi);
1da177e4 686
7699acd1 687 /* Remove the current item from the list of epoll hooks */
68499914 688 spin_lock(&file->f_lock);
ae10b2b4 689 list_del_rcu(&epi->fllink);
68499914 690 spin_unlock(&file->f_lock);
1da177e4 691
b2ac2ea6 692 rb_erase_cached(&epi->rbn, &ep->rbr);
1da177e4 693
a218cc49 694 write_lock_irq(&ep->lock);
992991c0 695 if (ep_is_linked(epi))
c7ea7630 696 list_del_init(&epi->rdllink);
a218cc49 697 write_unlock_irq(&ep->lock);
1da177e4 698
eea1d585 699 wakeup_source_unregister(ep_wakeup_source(epi));
ae10b2b4
JB
700 /*
701 * At this point it is safe to free the eventpoll item. Use the union
702 * field epi->rcu, since we are trying to minimize the size of
703 * 'struct epitem'. The 'rbn' field is no longer in use. Protected by
704 * ep->mtx. The rcu read side, reverse_path_check_proc(), does not make
705 * use of the rbn field.
706 */
707 call_rcu(&epi->rcu, epi_rcu_free);
1da177e4 708
52bd19f7 709 atomic_long_dec(&ep->user->epoll_watches);
7ef9964e 710
c7ea7630 711 return 0;
1da177e4
LT
712}
713
7699acd1 714static void ep_free(struct eventpoll *ep)
1da177e4 715{
7699acd1
DL
716 struct rb_node *rbp;
717 struct epitem *epi;
1da177e4 718
7699acd1
DL
719 /* We need to release all tasks waiting for these file */
720 if (waitqueue_active(&ep->poll_wait))
efcdd350 721 ep_poll_safewake(ep, NULL);
1da177e4 722
7699acd1
DL
723 /*
724 * We need to lock this because we could be hit by
725 * eventpoll_release_file() while we're freeing the "struct eventpoll".
d47de16c 726 * We do not need to hold "ep->mtx" here because the epoll file
7699acd1
DL
727 * is on the way to be removed and no one has references to it
728 * anymore. The only hit might come from eventpoll_release_file() but
25985edc 729 * holding "epmutex" is sufficient here.
7699acd1
DL
730 */
731 mutex_lock(&epmutex);
1da177e4
LT
732
733 /*
7699acd1 734 * Walks through the whole tree by unregistering poll callbacks.
1da177e4 735 */
b2ac2ea6 736 for (rbp = rb_first_cached(&ep->rbr); rbp; rbp = rb_next(rbp)) {
7699acd1
DL
737 epi = rb_entry(rbp, struct epitem, rbn);
738
739 ep_unregister_pollwait(ep, epi);
91cf5ab6 740 cond_resched();
7699acd1 741 }
1da177e4
LT
742
743 /*
7699acd1
DL
744 * Walks through the whole tree by freeing each "struct epitem". At this
745 * point we are sure no poll callbacks will be lingering around, and also by
d47de16c 746 * holding "epmutex" we can be sure that no file cleanup code will hit
a218cc49 747 * us during this operation. So we can avoid the lock on "ep->lock".
ddf676c3
EW
748 * We do not need to lock ep->mtx, either, we only do it to prevent
749 * a lockdep warning.
1da177e4 750 */
ddf676c3 751 mutex_lock(&ep->mtx);
b2ac2ea6 752 while ((rbp = rb_first_cached(&ep->rbr)) != NULL) {
7699acd1
DL
753 epi = rb_entry(rbp, struct epitem, rbn);
754 ep_remove(ep, epi);
91cf5ab6 755 cond_resched();
7699acd1 756 }
ddf676c3 757 mutex_unlock(&ep->mtx);
1da177e4 758
7699acd1 759 mutex_unlock(&epmutex);
d47de16c 760 mutex_destroy(&ep->mtx);
7ef9964e 761 free_uid(ep->user);
4d7e30d9 762 wakeup_source_unregister(ep->ws);
f0ee9aab 763 kfree(ep);
7699acd1 764}
1da177e4 765
7699acd1
DL
766static int ep_eventpoll_release(struct inode *inode, struct file *file)
767{
768 struct eventpoll *ep = file->private_data;
1da177e4 769
f0ee9aab 770 if (ep)
7699acd1 771 ep_free(ep);
7699acd1 772
7699acd1 773 return 0;
1da177e4
LT
774}
775
d85e2aa2 776static __poll_t ep_read_events_proc(struct eventpoll *ep, struct list_head *head,
37b5e521
JB
777 void *priv);
778static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead,
779 poll_table *pt);
780
781/*
782 * Differs from ep_eventpoll_poll() in that internal callers already have
783 * the ep->mtx so we need to start from depth=1, such that mutex_lock_nested()
784 * is correctly annotated.
785 */
d85e2aa2 786static __poll_t ep_item_poll(const struct epitem *epi, poll_table *pt,
bec1a502 787 int depth)
450d89ec 788{
37b5e521
JB
789 struct eventpoll *ep;
790 bool locked;
791
450d89ec 792 pt->_key = epi->event.events;
37b5e521 793 if (!is_file_epoll(epi->ffd.file))
9965ed17 794 return vfs_poll(epi->ffd.file, pt) & epi->event.events;
450d89ec 795
37b5e521
JB
796 ep = epi->ffd.file->private_data;
797 poll_wait(epi->ffd.file, &ep->poll_wait, pt);
798 locked = pt && (pt->_qproc == ep_ptable_queue_proc);
450d89ec 799
37b5e521
JB
800 return ep_scan_ready_list(epi->ffd.file->private_data,
801 ep_read_events_proc, &depth, depth,
802 locked) & epi->event.events;
450d89ec
EW
803}
804
d85e2aa2 805static __poll_t ep_read_events_proc(struct eventpoll *ep, struct list_head *head,
296e236e 806 void *priv)
5071f97e
DL
807{
808 struct epitem *epi, *tmp;
626cf236 809 poll_table pt;
37b5e521 810 int depth = *(int *)priv;
5071f97e 811
626cf236 812 init_poll_funcptr(&pt, NULL);
37b5e521 813 depth++;
450d89ec 814
5071f97e 815 list_for_each_entry_safe(epi, tmp, head, rdllink) {
37b5e521 816 if (ep_item_poll(epi, &pt, depth)) {
a9a08845 817 return EPOLLIN | EPOLLRDNORM;
37b5e521 818 } else {
5071f97e
DL
819 /*
820 * Item has been dropped into the ready list by the poll
821 * callback, but it's not actually ready, as far as
822 * caller requested events goes. We can remove it here.
823 */
eea1d585 824 __pm_relax(ep_wakeup_source(epi));
5071f97e 825 list_del_init(&epi->rdllink);
296e236e 826 }
5071f97e
DL
827 }
828
829 return 0;
830}
831
a11e1d43 832static __poll_t ep_eventpoll_poll(struct file *file, poll_table *wait)
11c5ad0e
BN
833{
834 struct eventpoll *ep = file->private_data;
835 int depth = 0;
7699acd1 836
a11e1d43
LT
837 /* Insert inside our poll wait queue */
838 poll_wait(file, &ep->poll_wait, wait);
839
5071f97e
DL
840 /*
841 * Proceed to find out if wanted events are really available inside
37b5e521 842 * the ready list.
5071f97e 843 */
37b5e521
JB
844 return ep_scan_ready_list(ep, ep_read_events_proc,
845 &depth, depth, false);
7699acd1
DL
846}
847
138d22b5 848#ifdef CONFIG_PROC_FS
a3816ab0 849static void ep_show_fdinfo(struct seq_file *m, struct file *f)
138d22b5
CG
850{
851 struct eventpoll *ep = f->private_data;
852 struct rb_node *rbp;
138d22b5
CG
853
854 mutex_lock(&ep->mtx);
b2ac2ea6 855 for (rbp = rb_first_cached(&ep->rbr); rbp; rbp = rb_next(rbp)) {
138d22b5 856 struct epitem *epi = rb_entry(rbp, struct epitem, rbn);
77493f04 857 struct inode *inode = file_inode(epi->ffd.file);
138d22b5 858
77493f04
CG
859 seq_printf(m, "tfd: %8d events: %8x data: %16llx "
860 " pos:%lli ino:%lx sdev:%x\n",
a3816ab0 861 epi->ffd.fd, epi->event.events,
77493f04
CG
862 (long long)epi->event.data,
863 (long long)epi->ffd.file->f_pos,
864 inode->i_ino, inode->i_sb->s_dev);
a3816ab0 865 if (seq_has_overflowed(m))
138d22b5
CG
866 break;
867 }
868 mutex_unlock(&ep->mtx);
138d22b5
CG
869}
870#endif
871
7699acd1
DL
872/* File callbacks that implement the eventpoll file behaviour */
873static const struct file_operations eventpoll_fops = {
138d22b5
CG
874#ifdef CONFIG_PROC_FS
875 .show_fdinfo = ep_show_fdinfo,
876#endif
7699acd1 877 .release = ep_eventpoll_release,
a11e1d43 878 .poll = ep_eventpoll_poll,
6038f373 879 .llseek = noop_llseek,
7699acd1
DL
880};
881
b611967d 882/*
7699acd1
DL
883 * This is called from eventpoll_release() to unlink files from the eventpoll
884 * interface. We need to have this facility to cleanup correctly files that are
885 * closed without being removed from the eventpoll interface.
b611967d 886 */
7699acd1 887void eventpoll_release_file(struct file *file)
b611967d 888{
7699acd1 889 struct eventpoll *ep;
ebe06187 890 struct epitem *epi, *next;
b611967d
DL
891
892 /*
68499914 893 * We don't want to get "file->f_lock" because it is not
7699acd1 894 * necessary. It is not necessary because we're in the "struct file"
25985edc 895 * cleanup path, and this means that no one is using this file anymore.
5071f97e 896 * So, for example, epoll_ctl() cannot hit here since if we reach this
67647d0f 897 * point, the file counter already went to zero and fget() would fail.
d47de16c 898 * The only hit might come from ep_free() but by holding the mutex
7699acd1 899 * will correctly serialize the operation. We do need to acquire
d47de16c 900 * "ep->mtx" after "epmutex" because ep_remove() requires it when called
7699acd1 901 * from anywhere but ep_free().
68499914
JC
902 *
903 * Besides, ep_remove() acquires the lock, so we can't hold it here.
b611967d 904 */
7699acd1 905 mutex_lock(&epmutex);
ebe06187 906 list_for_each_entry_safe(epi, next, &file->f_ep_links, fllink) {
7699acd1 907 ep = epi->ep;
d8805e63 908 mutex_lock_nested(&ep->mtx, 0);
7699acd1 909 ep_remove(ep, epi);
d47de16c 910 mutex_unlock(&ep->mtx);
b611967d 911 }
7699acd1 912 mutex_unlock(&epmutex);
b611967d
DL
913}
914
53d2be79 915static int ep_alloc(struct eventpoll **pep)
1da177e4 916{
7ef9964e
DL
917 int error;
918 struct user_struct *user;
919 struct eventpoll *ep;
1da177e4 920
7ef9964e 921 user = get_current_user();
7ef9964e
DL
922 error = -ENOMEM;
923 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
924 if (unlikely(!ep))
925 goto free_uid;
1da177e4 926
d47de16c 927 mutex_init(&ep->mtx);
a218cc49 928 rwlock_init(&ep->lock);
1da177e4
LT
929 init_waitqueue_head(&ep->wq);
930 init_waitqueue_head(&ep->poll_wait);
931 INIT_LIST_HEAD(&ep->rdllist);
b2ac2ea6 932 ep->rbr = RB_ROOT_CACHED;
d47de16c 933 ep->ovflist = EP_UNACTIVE_PTR;
7ef9964e 934 ep->user = user;
1da177e4 935
53d2be79 936 *pep = ep;
1da177e4 937
1da177e4 938 return 0;
7ef9964e
DL
939
940free_uid:
941 free_uid(user);
942 return error;
1da177e4
LT
943}
944
1da177e4 945/*
c7ea7630
DL
946 * Search the file inside the eventpoll tree. The RB tree operations
947 * are protected by the "mtx" mutex, and ep_find() must be called with
948 * "mtx" held.
1da177e4
LT
949 */
950static struct epitem *ep_find(struct eventpoll *ep, struct file *file, int fd)
951{
952 int kcmp;
1da177e4
LT
953 struct rb_node *rbp;
954 struct epitem *epi, *epir = NULL;
955 struct epoll_filefd ffd;
956
b030a4dd 957 ep_set_ffd(&ffd, file, fd);
b2ac2ea6 958 for (rbp = ep->rbr.rb_root.rb_node; rbp; ) {
1da177e4 959 epi = rb_entry(rbp, struct epitem, rbn);
b030a4dd 960 kcmp = ep_cmp_ffd(&ffd, &epi->ffd);
1da177e4
LT
961 if (kcmp > 0)
962 rbp = rbp->rb_right;
963 else if (kcmp < 0)
964 rbp = rbp->rb_left;
965 else {
1da177e4
LT
966 epir = epi;
967 break;
968 }
969 }
1da177e4 970
1da177e4
LT
971 return epir;
972}
973
92ef6da3 974#ifdef CONFIG_CHECKPOINT_RESTORE
0791e364
CG
975static struct epitem *ep_find_tfd(struct eventpoll *ep, int tfd, unsigned long toff)
976{
977 struct rb_node *rbp;
978 struct epitem *epi;
979
b2ac2ea6 980 for (rbp = rb_first_cached(&ep->rbr); rbp; rbp = rb_next(rbp)) {
0791e364
CG
981 epi = rb_entry(rbp, struct epitem, rbn);
982 if (epi->ffd.fd == tfd) {
983 if (toff == 0)
984 return epi;
985 else
986 toff--;
987 }
988 cond_resched();
989 }
990
991 return NULL;
992}
993
994struct file *get_epoll_tfile_raw_ptr(struct file *file, int tfd,
995 unsigned long toff)
996{
997 struct file *file_raw;
998 struct eventpoll *ep;
999 struct epitem *epi;
1000
1001 if (!is_file_epoll(file))
1002 return ERR_PTR(-EINVAL);
1003
1004 ep = file->private_data;
1005
1006 mutex_lock(&ep->mtx);
1007 epi = ep_find_tfd(ep, tfd, toff);
1008 if (epi)
1009 file_raw = epi->ffd.file;
1010 else
1011 file_raw = ERR_PTR(-ENOENT);
1012 mutex_unlock(&ep->mtx);
1013
1014 return file_raw;
1015}
92ef6da3 1016#endif /* CONFIG_CHECKPOINT_RESTORE */
0791e364 1017
a218cc49
RP
1018/**
1019 * Adds a new entry to the tail of the list in a lockless way, i.e.
1020 * multiple CPUs are allowed to call this function concurrently.
1021 *
1022 * Beware: it is necessary to prevent any other modifications of the
1023 * existing list until all changes are completed, in other words
1024 * concurrent list_add_tail_lockless() calls should be protected
1025 * with a read lock, where write lock acts as a barrier which
1026 * makes sure all list_add_tail_lockless() calls are fully
1027 * completed.
1028 *
1029 * Also an element can be locklessly added to the list only in one
1030 * direction i.e. either to the tail either to the head, otherwise
1031 * concurrent access will corrupt the list.
1032 *
1033 * Returns %false if element has been already added to the list, %true
1034 * otherwise.
1035 */
1036static inline bool list_add_tail_lockless(struct list_head *new,
1037 struct list_head *head)
1038{
1039 struct list_head *prev;
1040
1041 /*
1042 * This is simple 'new->next = head' operation, but cmpxchg()
1043 * is used in order to detect that same element has been just
1044 * added to the list from another CPU: the winner observes
1045 * new->next == new.
1046 */
1047 if (cmpxchg(&new->next, new, head) != new)
1048 return false;
1049
1050 /*
1051 * Initially ->next of a new element must be updated with the head
1052 * (we are inserting to the tail) and only then pointers are atomically
1053 * exchanged. XCHG guarantees memory ordering, thus ->next should be
1054 * updated before pointers are actually swapped and pointers are
1055 * swapped before prev->next is updated.
1056 */
1057
1058 prev = xchg(&head->prev, new);
1059
1060 /*
1061 * It is safe to modify prev->next and new->prev, because a new element
1062 * is added only to the tail and new->next is updated before XCHG.
1063 */
1064
1065 prev->next = new;
1066 new->prev = prev;
1067
1068 return true;
1069}
1070
1071/**
1072 * Chains a new epi entry to the tail of the ep->ovflist in a lockless way,
1073 * i.e. multiple CPUs are allowed to call this function concurrently.
1074 *
1075 * Returns %false if epi element has been already chained, %true otherwise.
1076 */
1077static inline bool chain_epi_lockless(struct epitem *epi)
1078{
1079 struct eventpoll *ep = epi->ep;
1080
0c54a6a4
KK
1081 /* Fast preliminary check */
1082 if (epi->next != EP_UNACTIVE_PTR)
1083 return false;
1084
a218cc49
RP
1085 /* Check that the same epi has not been just chained from another CPU */
1086 if (cmpxchg(&epi->next, EP_UNACTIVE_PTR, NULL) != EP_UNACTIVE_PTR)
1087 return false;
1088
1089 /* Atomically exchange tail */
1090 epi->next = xchg(&ep->ovflist, epi);
1091
1092 return true;
1093}
1094
1da177e4 1095/*
7699acd1 1096 * This is the callback that is passed to the wait queue wakeup
bf6a41db 1097 * mechanism. It is called by the stored file descriptors when they
7699acd1 1098 * have events to report.
a218cc49
RP
1099 *
1100 * This callback takes a read lock in order not to content with concurrent
1101 * events from another file descriptors, thus all modifications to ->rdllist
1102 * or ->ovflist are lockless. Read lock is paired with the write lock from
1103 * ep_scan_ready_list(), which stops all list modifications and guarantees
1104 * that lists state is seen correctly.
1105 *
1106 * Another thing worth to mention is that ep_poll_callback() can be called
1107 * concurrently for the same @epi from different CPUs if poll table was inited
1108 * with several wait queues entries. Plural wakeup from different CPUs of a
1109 * single wait queue is serialized by wq.lock, but the case when multiple wait
1110 * queues are used should be detected accordingly. This is detected using
1111 * cmpxchg() operation.
1da177e4 1112 */
ac6424b9 1113static int ep_poll_callback(wait_queue_entry_t *wait, unsigned mode, int sync, void *key)
1da177e4 1114{
7699acd1 1115 int pwake = 0;
7699acd1
DL
1116 struct epitem *epi = ep_item_from_wait(wait);
1117 struct eventpoll *ep = epi->ep;
3ad6f93e 1118 __poll_t pollflags = key_to_poll(key);
a218cc49 1119 unsigned long flags;
df0108c5 1120 int ewake = 0;
1da177e4 1121
a218cc49 1122 read_lock_irqsave(&ep->lock, flags);
1da177e4 1123
bf3b9f63
SS
1124 ep_set_busy_poll_napi_id(epi);
1125
7699acd1
DL
1126 /*
1127 * If the event mask does not contain any poll(2) event, we consider the
1128 * descriptor to be disabled. This condition is likely the effect of the
1129 * EPOLLONESHOT bit that disables the descriptor when an event is received,
1130 * until the next EPOLL_CTL_MOD will be issued.
1131 */
1132 if (!(epi->event.events & ~EP_PRIVATE_BITS))
d47de16c
DL
1133 goto out_unlock;
1134
2dfa4eea
DL
1135 /*
1136 * Check the events coming with the callback. At this stage, not
1137 * every device reports the events in the "key" parameter of the
1138 * callback. We need to be able to handle both cases here, hence the
1139 * test for "key" != NULL before the event match test.
1140 */
3ad6f93e 1141 if (pollflags && !(pollflags & epi->event.events))
2dfa4eea
DL
1142 goto out_unlock;
1143
d47de16c 1144 /*
bf6a41db 1145 * If we are transferring events to userspace, we can hold no locks
d47de16c 1146 * (because we're accessing user memory, and because of linux f_op->poll()
bf6a41db 1147 * semantics). All the events that happen during that period of time are
d47de16c
DL
1148 * chained in ep->ovflist and requeued later on.
1149 */
c5a282e9 1150 if (READ_ONCE(ep->ovflist) != EP_UNACTIVE_PTR) {
0c54a6a4
KK
1151 if (chain_epi_lockless(epi))
1152 ep_pm_stay_awake_rcu(epi);
1153 } else if (!ep_is_linked(epi)) {
1154 /* In the usual case, add event to ready list. */
1155 if (list_add_tail_lockless(&epi->rdllink, &ep->rdllist))
c3e320b6 1156 ep_pm_stay_awake_rcu(epi);
4d7e30d9 1157 }
7699acd1 1158
7699acd1
DL
1159 /*
1160 * Wake up ( if active ) both the eventpoll wait list and the ->poll()
1161 * wait list.
1162 */
df0108c5 1163 if (waitqueue_active(&ep->wq)) {
b6a515c8 1164 if ((epi->event.events & EPOLLEXCLUSIVE) &&
3ad6f93e
AV
1165 !(pollflags & POLLFREE)) {
1166 switch (pollflags & EPOLLINOUT_BITS) {
a9a08845
LT
1167 case EPOLLIN:
1168 if (epi->event.events & EPOLLIN)
b6a515c8
JB
1169 ewake = 1;
1170 break;
a9a08845
LT
1171 case EPOLLOUT:
1172 if (epi->event.events & EPOLLOUT)
b6a515c8
JB
1173 ewake = 1;
1174 break;
1175 case 0:
1176 ewake = 1;
1177 break;
1178 }
1179 }
a218cc49 1180 wake_up(&ep->wq);
df0108c5 1181 }
7699acd1
DL
1182 if (waitqueue_active(&ep->poll_wait))
1183 pwake++;
1184
d47de16c 1185out_unlock:
a218cc49 1186 read_unlock_irqrestore(&ep->lock, flags);
1da177e4 1187
7699acd1
DL
1188 /* We have to call this outside the lock */
1189 if (pwake)
efcdd350 1190 ep_poll_safewake(ep, epi);
7699acd1 1191
138e4ad6
ON
1192 if (!(epi->event.events & EPOLLEXCLUSIVE))
1193 ewake = 1;
1194
3ad6f93e 1195 if (pollflags & POLLFREE) {
138e4ad6
ON
1196 /*
1197 * If we race with ep_remove_wait_queue() it can miss
1198 * ->whead = NULL and do another remove_wait_queue() after
1199 * us, so we can't use __remove_wait_queue().
1200 */
1201 list_del_init(&wait->entry);
1202 /*
1203 * ->whead != NULL protects us from the race with ep_free()
1204 * or ep_remove(), ep_remove_wait_queue() takes whead->lock
1205 * held by the caller. Once we nullify it, nothing protects
1206 * ep/epi or even wait.
1207 */
1208 smp_store_release(&ep_pwq_from_wait(wait)->whead, NULL);
1209 }
df0108c5 1210
138e4ad6 1211 return ewake;
7699acd1 1212}
1da177e4
LT
1213
1214/*
1215 * This is the callback that is used to add our wait queue to the
1216 * target file wakeup lists.
1217 */
1218static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead,
1219 poll_table *pt)
1220{
364f374f
AV
1221 struct ep_pqueue *epq = container_of(pt, struct ep_pqueue, pt);
1222 struct epitem *epi = epq->epi;
1da177e4
LT
1223 struct eppoll_entry *pwq;
1224
364f374f
AV
1225 if (unlikely(!epi)) // an earlier allocation has failed
1226 return;
1227
1228 pwq = kmem_cache_alloc(pwq_cache, GFP_KERNEL);
1229 if (unlikely(!pwq)) {
1230 epq->epi = NULL;
1231 return;
296e236e 1232 }
364f374f
AV
1233
1234 init_waitqueue_func_entry(&pwq->wait, ep_poll_callback);
1235 pwq->whead = whead;
1236 pwq->base = epi;
1237 if (epi->event.events & EPOLLEXCLUSIVE)
1238 add_wait_queue_exclusive(whead, &pwq->wait);
1239 else
1240 add_wait_queue(whead, &pwq->wait);
1241 pwq->next = epi->pwqlist;
1242 epi->pwqlist = pwq;
1da177e4
LT
1243}
1244
1da177e4
LT
1245static void ep_rbtree_insert(struct eventpoll *ep, struct epitem *epi)
1246{
1247 int kcmp;
b2ac2ea6 1248 struct rb_node **p = &ep->rbr.rb_root.rb_node, *parent = NULL;
1da177e4 1249 struct epitem *epic;
b2ac2ea6 1250 bool leftmost = true;
1da177e4
LT
1251
1252 while (*p) {
1253 parent = *p;
1254 epic = rb_entry(parent, struct epitem, rbn);
b030a4dd 1255 kcmp = ep_cmp_ffd(&epi->ffd, &epic->ffd);
b2ac2ea6 1256 if (kcmp > 0) {
1da177e4 1257 p = &parent->rb_right;
b2ac2ea6
DB
1258 leftmost = false;
1259 } else
1da177e4
LT
1260 p = &parent->rb_left;
1261 }
1262 rb_link_node(&epi->rbn, parent, p);
b2ac2ea6 1263 rb_insert_color_cached(&epi->rbn, &ep->rbr, leftmost);
1da177e4
LT
1264}
1265
a80a6b85
AM
1266
1267
28d82dc1
JB
1268#define PATH_ARR_SIZE 5
1269/*
1270 * These are the number paths of length 1 to 5, that we are allowing to emanate
1271 * from a single file of interest. For example, we allow 1000 paths of length
1272 * 1, to emanate from each file of interest. This essentially represents the
1273 * potential wakeup paths, which need to be limited in order to avoid massive
1274 * uncontrolled wakeup storms. The common use case should be a single ep which
1275 * is connected to n file sources. In this case each file source has 1 path
1276 * of length 1. Thus, the numbers below should be more than sufficient. These
1277 * path limits are enforced during an EPOLL_CTL_ADD operation, since a modify
1278 * and delete can't add additional paths. Protected by the epmutex.
1279 */
1280static const int path_limits[PATH_ARR_SIZE] = { 1000, 500, 100, 50, 10 };
1281static int path_count[PATH_ARR_SIZE];
1282
1283static int path_count_inc(int nests)
1284{
93dc6107
JB
1285 /* Allow an arbitrary number of depth 1 paths */
1286 if (nests == 0)
1287 return 0;
1288
28d82dc1
JB
1289 if (++path_count[nests] > path_limits[nests])
1290 return -1;
1291 return 0;
1292}
1293
1294static void path_count_init(void)
1295{
1296 int i;
1297
1298 for (i = 0; i < PATH_ARR_SIZE; i++)
1299 path_count[i] = 0;
1300}
1301
aebf15f0 1302static int reverse_path_check_proc(struct file *file, int depth)
28d82dc1
JB
1303{
1304 int error = 0;
28d82dc1
JB
1305 struct epitem *epi;
1306
0c320f77 1307 if (depth > EP_MAX_NESTS) /* too deep nesting */
99d84d43
AV
1308 return -1;
1309
ae10b2b4
JB
1310 /* CTL_DEL can remove links here, but that can't increase our count */
1311 rcu_read_lock();
1312 list_for_each_entry_rcu(epi, &file->f_ep_links, fllink) {
d16312a4
AV
1313 struct file *recepient = epi->ep->file;
1314 if (WARN_ON(!is_file_epoll(recepient)))
1315 continue;
1316 if (list_empty(&recepient->f_ep_links))
1317 error = path_count_inc(depth);
1318 else
1319 error = reverse_path_check_proc(recepient, depth + 1);
1320 if (error != 0)
1321 break;
28d82dc1 1322 }
ae10b2b4 1323 rcu_read_unlock();
28d82dc1
JB
1324 return error;
1325}
1326
1327/**
1328 * reverse_path_check - The tfile_check_list is list of file *, which have
1329 * links that are proposed to be newly added. We need to
1330 * make sure that those added links don't add too many
1331 * paths such that we will spend all our time waking up
1332 * eventpoll objects.
1333 *
1334 * Returns: Returns zero if the proposed links don't create too many paths,
1335 * -1 otherwise.
1336 */
1337static int reverse_path_check(void)
1338{
28d82dc1
JB
1339 int error = 0;
1340 struct file *current_file;
1341
1342 /* let's call this for all tfiles */
1343 list_for_each_entry(current_file, &tfile_check_list, f_tfile_llink) {
28d82dc1 1344 path_count_init();
aebf15f0 1345 error = reverse_path_check_proc(current_file, 0);
28d82dc1
JB
1346 if (error)
1347 break;
1348 }
1349 return error;
1350}
1351
4d7e30d9
AH
1352static int ep_create_wakeup_source(struct epitem *epi)
1353{
3701cb59 1354 struct name_snapshot n;
eea1d585 1355 struct wakeup_source *ws;
4d7e30d9
AH
1356
1357 if (!epi->ep->ws) {
c8377adf 1358 epi->ep->ws = wakeup_source_register(NULL, "eventpoll");
4d7e30d9
AH
1359 if (!epi->ep->ws)
1360 return -ENOMEM;
1361 }
1362
3701cb59
AV
1363 take_dentry_name_snapshot(&n, epi->ffd.file->f_path.dentry);
1364 ws = wakeup_source_register(NULL, n.name.name);
1365 release_dentry_name_snapshot(&n);
eea1d585
EW
1366
1367 if (!ws)
4d7e30d9 1368 return -ENOMEM;
eea1d585 1369 rcu_assign_pointer(epi->ws, ws);
4d7e30d9
AH
1370
1371 return 0;
1372}
1373
eea1d585
EW
1374/* rare code path, only used when EPOLL_CTL_MOD removes a wakeup source */
1375static noinline void ep_destroy_wakeup_source(struct epitem *epi)
4d7e30d9 1376{
eea1d585
EW
1377 struct wakeup_source *ws = ep_wakeup_source(epi);
1378
d6d67e72 1379 RCU_INIT_POINTER(epi->ws, NULL);
eea1d585
EW
1380
1381 /*
1382 * wait for ep_pm_stay_awake_rcu to finish, synchronize_rcu is
1383 * used internally by wakeup_source_remove, too (called by
1384 * wakeup_source_unregister), so we cannot use call_rcu
1385 */
1386 synchronize_rcu();
1387 wakeup_source_unregister(ws);
4d7e30d9
AH
1388}
1389
c7ea7630
DL
1390/*
1391 * Must be called with "mtx" held.
1392 */
bec1a502 1393static int ep_insert(struct eventpoll *ep, const struct epoll_event *event,
67347fe4 1394 struct file *tfile, int fd, int full_check)
1da177e4 1395{
d85e2aa2
AV
1396 int error, pwake = 0;
1397 __poll_t revents;
52bd19f7 1398 long user_watches;
1da177e4
LT
1399 struct epitem *epi;
1400 struct ep_pqueue epq;
1401
92e64178
DB
1402 lockdep_assert_irqs_enabled();
1403
52bd19f7
RH
1404 user_watches = atomic_long_read(&ep->user->epoll_watches);
1405 if (unlikely(user_watches >= max_user_watches))
7ef9964e 1406 return -ENOSPC;
e94b1766 1407 if (!(epi = kmem_cache_alloc(epi_cache, GFP_KERNEL)))
7ef9964e 1408 return -ENOMEM;
1da177e4
LT
1409
1410 /* Item initialization follow here ... */
1da177e4
LT
1411 INIT_LIST_HEAD(&epi->rdllink);
1412 INIT_LIST_HEAD(&epi->fllink);
80285b75 1413 epi->pwqlist = NULL;
1da177e4 1414 epi->ep = ep;
b030a4dd 1415 ep_set_ffd(&epi->ffd, tfile, fd);
1da177e4 1416 epi->event = *event;
d47de16c 1417 epi->next = EP_UNACTIVE_PTR;
4d7e30d9
AH
1418 if (epi->event.events & EPOLLWAKEUP) {
1419 error = ep_create_wakeup_source(epi);
1420 if (error)
1421 goto error_create_wakeup_source;
1422 } else {
eea1d585 1423 RCU_INIT_POINTER(epi->ws, NULL);
4d7e30d9 1424 }
1da177e4 1425
f8d4f44d
AV
1426 /* Add the current item to the list of active epoll hook for this file */
1427 spin_lock(&tfile->f_lock);
1428 list_add_tail_rcu(&epi->fllink, &tfile->f_ep_links);
1429 spin_unlock(&tfile->f_lock);
1430
1431 /*
1432 * Add the current item to the RB tree. All RB tree operations are
1433 * protected by "mtx", and ep_insert() is called with "mtx" held.
1434 */
1435 ep_rbtree_insert(ep, epi);
1436
1437 /* now check if we've created too many backpaths */
1438 error = -EINVAL;
1439 if (full_check && reverse_path_check())
1440 goto error_remove_epi;
1441
1da177e4
LT
1442 /* Initialize the poll table using the queue callback */
1443 epq.epi = epi;
1444 init_poll_funcptr(&epq.pt, ep_ptable_queue_proc);
1445
1446 /*
1447 * Attach the item to the poll hooks and get current event bits.
1448 * We can safely use the file* here because its usage count has
c7ea7630
DL
1449 * been increased by the caller of this function. Note that after
1450 * this operation completes, the poll callback can start hitting
1451 * the new item.
1da177e4 1452 */
37b5e521 1453 revents = ep_item_poll(epi, &epq.pt, 1);
1da177e4
LT
1454
1455 /*
1456 * We have to check if something went wrong during the poll wait queue
1457 * install process. Namely an allocation for a wait queue failed due
1458 * high memory pressure.
1459 */
7ef9964e 1460 error = -ENOMEM;
364f374f 1461 if (!epq.epi)
7699acd1 1462 goto error_unregister;
1da177e4 1463
c7ea7630 1464 /* We have to drop the new item inside our item list to keep track of it */
a218cc49 1465 write_lock_irq(&ep->lock);
c7ea7630 1466
bf3b9f63
SS
1467 /* record NAPI ID of new item if present */
1468 ep_set_busy_poll_napi_id(epi);
1469
1da177e4 1470 /* If the file is already "ready" we drop it inside the ready list */
992991c0 1471 if (revents && !ep_is_linked(epi)) {
1da177e4 1472 list_add_tail(&epi->rdllink, &ep->rdllist);
eea1d585 1473 ep_pm_stay_awake(epi);
1da177e4
LT
1474
1475 /* Notify waiting tasks that events are available */
1476 if (waitqueue_active(&ep->wq))
a218cc49 1477 wake_up(&ep->wq);
1da177e4
LT
1478 if (waitqueue_active(&ep->poll_wait))
1479 pwake++;
1480 }
1481
a218cc49 1482 write_unlock_irq(&ep->lock);
1da177e4 1483
52bd19f7 1484 atomic_long_inc(&ep->user->epoll_watches);
7ef9964e 1485
1da177e4
LT
1486 /* We have to call this outside the lock */
1487 if (pwake)
efcdd350 1488 ep_poll_safewake(ep, NULL);
1da177e4 1489
1da177e4
LT
1490 return 0;
1491
f8d4f44d
AV
1492error_unregister:
1493 ep_unregister_pollwait(ep, epi);
28d82dc1
JB
1494error_remove_epi:
1495 spin_lock(&tfile->f_lock);
ae10b2b4 1496 list_del_rcu(&epi->fllink);
28d82dc1
JB
1497 spin_unlock(&tfile->f_lock);
1498
b2ac2ea6 1499 rb_erase_cached(&epi->rbn, &ep->rbr);
28d82dc1 1500
1da177e4
LT
1501 /*
1502 * We need to do this because an event could have been arrived on some
67647d0f
DL
1503 * allocated wait queue. Note that we don't care about the ep->ovflist
1504 * list, since that is used/cleaned only inside a section bound by "mtx".
1505 * And ep_insert() is called with "mtx" held.
1da177e4 1506 */
a218cc49 1507 write_lock_irq(&ep->lock);
992991c0 1508 if (ep_is_linked(epi))
6192bd53 1509 list_del_init(&epi->rdllink);
a218cc49 1510 write_unlock_irq(&ep->lock);
1da177e4 1511
eea1d585 1512 wakeup_source_unregister(ep_wakeup_source(epi));
4d7e30d9
AH
1513
1514error_create_wakeup_source:
b030a4dd 1515 kmem_cache_free(epi_cache, epi);
7ef9964e 1516
1da177e4
LT
1517 return error;
1518}
1519
1da177e4
LT
1520/*
1521 * Modify the interest event mask by dropping an event if the new mask
c7ea7630 1522 * has a match in the current file status. Must be called with "mtx" held.
1da177e4 1523 */
bec1a502
AV
1524static int ep_modify(struct eventpoll *ep, struct epitem *epi,
1525 const struct epoll_event *event)
1da177e4
LT
1526{
1527 int pwake = 0;
626cf236
HV
1528 poll_table pt;
1529
92e64178
DB
1530 lockdep_assert_irqs_enabled();
1531
626cf236 1532 init_poll_funcptr(&pt, NULL);
1da177e4
LT
1533
1534 /*
e057e15f
TB
1535 * Set the new event interest mask before calling f_op->poll();
1536 * otherwise we might miss an event that happens between the
1537 * f_op->poll() call and the new event set registering.
1da177e4 1538 */
128dd175 1539 epi->event.events = event->events; /* need barrier below */
e057e15f 1540 epi->event.data = event->data; /* protected by mtx */
4d7e30d9 1541 if (epi->event.events & EPOLLWAKEUP) {
eea1d585 1542 if (!ep_has_wakeup_source(epi))
4d7e30d9 1543 ep_create_wakeup_source(epi);
eea1d585 1544 } else if (ep_has_wakeup_source(epi)) {
4d7e30d9
AH
1545 ep_destroy_wakeup_source(epi);
1546 }
1da177e4 1547
128dd175
EW
1548 /*
1549 * The following barrier has two effects:
1550 *
1551 * 1) Flush epi changes above to other CPUs. This ensures
1552 * we do not miss events from ep_poll_callback if an
1553 * event occurs immediately after we call f_op->poll().
a218cc49 1554 * We need this because we did not take ep->lock while
128dd175 1555 * changing epi above (but ep_poll_callback does take
a218cc49 1556 * ep->lock).
128dd175
EW
1557 *
1558 * 2) We also need to ensure we do not miss _past_ events
1559 * when calling f_op->poll(). This barrier also
1560 * pairs with the barrier in wq_has_sleeper (see
1561 * comments for wq_has_sleeper).
1562 *
1563 * This barrier will now guarantee ep_poll_callback or f_op->poll
1564 * (or both) will notice the readiness of an item.
1565 */
1566 smp_mb();
1567
1da177e4
LT
1568 /*
1569 * Get current event bits. We can safely use the file* here because
1570 * its usage count has been increased by the caller of this function.
c7ea7630 1571 * If the item is "hot" and it is not registered inside the ready
67647d0f 1572 * list, push it inside.
1da177e4 1573 */
69112736 1574 if (ep_item_poll(epi, &pt, 1)) {
a218cc49 1575 write_lock_irq(&ep->lock);
992991c0 1576 if (!ep_is_linked(epi)) {
c7ea7630 1577 list_add_tail(&epi->rdllink, &ep->rdllist);
eea1d585 1578 ep_pm_stay_awake(epi);
c7ea7630
DL
1579
1580 /* Notify waiting tasks that events are available */
1581 if (waitqueue_active(&ep->wq))
a218cc49 1582 wake_up(&ep->wq);
c7ea7630
DL
1583 if (waitqueue_active(&ep->poll_wait))
1584 pwake++;
7699acd1 1585 }
a218cc49 1586 write_unlock_irq(&ep->lock);
7699acd1 1587 }
1da177e4 1588
7699acd1
DL
1589 /* We have to call this outside the lock */
1590 if (pwake)
efcdd350 1591 ep_poll_safewake(ep, NULL);
1da177e4 1592
7699acd1 1593 return 0;
1da177e4
LT
1594}
1595
d85e2aa2 1596static __poll_t ep_send_events_proc(struct eventpoll *ep, struct list_head *head,
296e236e 1597 void *priv)
1da177e4 1598{
5071f97e 1599 struct ep_send_events_data *esed = priv;
d85e2aa2 1600 __poll_t revents;
4e0982a0
DB
1601 struct epitem *epi, *tmp;
1602 struct epoll_event __user *uevent = esed->events;
eea1d585 1603 struct wakeup_source *ws;
626cf236
HV
1604 poll_table pt;
1605
1606 init_poll_funcptr(&pt, NULL);
4e0982a0 1607 esed->res = 0;
1da177e4 1608
296e236e 1609 /*
5071f97e
DL
1610 * We can loop without lock because we are passed a task private list.
1611 * Items cannot vanish during the loop because ep_scan_ready_list() is
1612 * holding "mtx" during this call.
296e236e 1613 */
21877e1a
DB
1614 lockdep_assert_held(&ep->mtx);
1615
4e0982a0
DB
1616 list_for_each_entry_safe(epi, tmp, head, rdllink) {
1617 if (esed->res >= esed->maxevents)
1618 break;
d47de16c 1619
4d7e30d9
AH
1620 /*
1621 * Activate ep->ws before deactivating epi->ws to prevent
1622 * triggering auto-suspend here (in case we reactive epi->ws
1623 * below).
1624 *
1625 * This could be rearranged to delay the deactivation of epi->ws
1626 * instead, but then epi->ws would temporarily be out of sync
1627 * with ep_is_linked().
1628 */
eea1d585
EW
1629 ws = ep_wakeup_source(epi);
1630 if (ws) {
1631 if (ws->active)
1632 __pm_stay_awake(ep->ws);
1633 __pm_relax(ws);
1634 }
1635
d47de16c 1636 list_del_init(&epi->rdllink);
1da177e4 1637
296e236e 1638 /*
5071f97e
DL
1639 * If the event mask intersect the caller-requested one,
1640 * deliver the event to userspace. Again, ep_scan_ready_list()
4e0982a0 1641 * is holding ep->mtx, so no operations coming from userspace
5071f97e 1642 * can change the item.
296e236e 1643 */
4e0982a0
DB
1644 revents = ep_item_poll(epi, &pt, 1);
1645 if (!revents)
1646 continue;
1647
1648 if (__put_user(revents, &uevent->events) ||
1649 __put_user(epi->event.data, &uevent->data)) {
1650 list_add(&epi->rdllink, head);
1651 ep_pm_stay_awake(epi);
1652 if (!esed->res)
1653 esed->res = -EFAULT;
1654 return 0;
1655 }
1656 esed->res++;
1657 uevent++;
1658 if (epi->event.events & EPOLLONESHOT)
1659 epi->event.events &= EP_PRIVATE_BITS;
1660 else if (!(epi->event.events & EPOLLET)) {
1661 /*
1662 * If this file has been added with Level
1663 * Trigger mode, we need to insert back inside
1664 * the ready list, so that the next call to
1665 * epoll_wait() will check again the events
1666 * availability. At this point, no one can insert
1667 * into ep->rdllist besides us. The epoll_ctl()
1668 * callers are locked out by
1669 * ep_scan_ready_list() holding "mtx" and the
1670 * poll callback will queue them in ep->ovflist.
1671 */
1672 list_add_tail(&epi->rdllink, &ep->rdllist);
1673 ep_pm_stay_awake(epi);
296e236e
DL
1674 }
1675 }
5071f97e 1676
d7ebbe46 1677 return 0;
5071f97e 1678}
d47de16c 1679
296e236e
DL
1680static int ep_send_events(struct eventpoll *ep,
1681 struct epoll_event __user *events, int maxevents)
5071f97e
DL
1682{
1683 struct ep_send_events_data esed;
1da177e4 1684
5071f97e
DL
1685 esed.maxevents = maxevents;
1686 esed.events = events;
6192bd53 1687
d7ebbe46
AV
1688 ep_scan_ready_list(ep, ep_send_events_proc, &esed, 0, false);
1689 return esed.res;
1da177e4
LT
1690}
1691
766b9f92 1692static inline struct timespec64 ep_set_mstimeout(long ms)
0781b909 1693{
766b9f92 1694 struct timespec64 now, ts = {
0781b909
ED
1695 .tv_sec = ms / MSEC_PER_SEC,
1696 .tv_nsec = NSEC_PER_MSEC * (ms % MSEC_PER_SEC),
1697 };
1698
766b9f92
DD
1699 ktime_get_ts64(&now);
1700 return timespec64_add_safe(now, ts);
0781b909
ED
1701}
1702
f4d93ad7
SB
1703/**
1704 * ep_poll - Retrieves ready events, and delivers them to the caller supplied
1705 * event buffer.
1706 *
1707 * @ep: Pointer to the eventpoll context.
1708 * @events: Pointer to the userspace buffer where the ready events should be
1709 * stored.
1710 * @maxevents: Size (in terms of number of events) of the caller event buffer.
1711 * @timeout: Maximum timeout for the ready events fetch operation, in
1712 * milliseconds. If the @timeout is zero, the function will not block,
1713 * while if the @timeout is less than zero, the function will block
1714 * until at least one event has been retrieved (or an error
1715 * occurred).
1716 *
1717 * Returns: Returns the number of ready events which have been fetched, or an
1718 * error code, in case of error.
1719 */
1da177e4
LT
1720static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
1721 int maxevents, long timeout)
1722{
f4d93ad7 1723 int res = 0, eavail, timed_out = 0;
da8b44d5 1724 u64 slack = 0;
ac6424b9 1725 wait_queue_entry_t wait;
95aac7b1
SB
1726 ktime_t expires, *to = NULL;
1727
679abf38
DB
1728 lockdep_assert_irqs_enabled();
1729
95aac7b1 1730 if (timeout > 0) {
766b9f92 1731 struct timespec64 end_time = ep_set_mstimeout(timeout);
0781b909 1732
95aac7b1
SB
1733 slack = select_estimate_accuracy(&end_time);
1734 to = &expires;
766b9f92 1735 *to = timespec64_to_ktime(end_time);
95aac7b1 1736 } else if (timeout == 0) {
f4d93ad7
SB
1737 /*
1738 * Avoid the unnecessary trip to the wait queue loop, if the
c5a282e9
DB
1739 * caller specified a non blocking operation. We still need
1740 * lock because we could race and not see an epi being added
1741 * to the ready list while in irq callback. Thus incorrectly
1742 * returning 0 back to userspace.
f4d93ad7 1743 */
95aac7b1 1744 timed_out = 1;
c5a282e9 1745
a218cc49 1746 write_lock_irq(&ep->lock);
c5a282e9 1747 eavail = ep_events_available(ep);
a218cc49 1748 write_unlock_irq(&ep->lock);
c5a282e9 1749
35cff1a6 1750 goto send_events;
95aac7b1 1751 }
1da177e4 1752
f4d93ad7 1753fetch_events:
bf3b9f63
SS
1754
1755 if (!ep_events_available(ep))
1756 ep_busy_loop(ep, timed_out);
1757
c5a282e9
DB
1758 eavail = ep_events_available(ep);
1759 if (eavail)
35cff1a6 1760 goto send_events;
1da177e4 1761
c5a282e9
DB
1762 /*
1763 * Busy poll timed out. Drop NAPI ID for now, we can add
1764 * it back in when we have moved a socket with a valid NAPI
1765 * ID onto the ready list.
1766 */
1767 ep_reset_busy_poll_napi_id(ep);
bf3b9f63 1768
412895f0
RP
1769 do {
1770 /*
1771 * Internally init_wait() uses autoremove_wake_function(),
1772 * thus wait entry is removed from the wait queue on each
1773 * wakeup. Why it is important? In case of several waiters
1774 * each new wakeup will hit the next waiter, giving it the
1775 * chance to harvest new event. Otherwise wakeup can be
1776 * lost. This is also good performance-wise, because on
1777 * normal wakeup path no need to call __remove_wait_queue()
1778 * explicitly, thus ep->lock is not taken, which halts the
1779 * event delivery.
1780 */
1781 init_wait(&wait);
1da177e4 1782
65759097 1783 write_lock_irq(&ep->lock);
bf3b9f63 1784 /*
65759097
RP
1785 * Barrierless variant, waitqueue_active() is called under
1786 * the same lock on wakeup ep_poll_callback() side, so it
1787 * is safe to avoid an explicit barrier.
bf3b9f63 1788 */
65759097
RP
1789 __set_current_state(TASK_INTERRUPTIBLE);
1790
1da177e4 1791 /*
65759097
RP
1792 * Do the final check under the lock. ep_scan_ready_list()
1793 * plays with two lists (->rdllist and ->ovflist) and there
1794 * is always a race when both lists are empty for short
1795 * period of time although events are pending, so lock is
1796 * important.
1da177e4 1797 */
65759097
RP
1798 eavail = ep_events_available(ep);
1799 if (!eavail) {
1800 if (signal_pending(current))
1801 res = -EINTR;
1802 else
1803 __add_wait_queue_exclusive(&ep->wq, &wait);
c5a282e9 1804 }
65759097 1805 write_unlock_irq(&ep->lock);
95aac7b1 1806
65759097 1807 if (eavail || res)
c5a282e9 1808 break;
1da177e4 1809
abc610e0 1810 if (!schedule_hrtimeout_range(to, slack, HRTIMER_MODE_ABS)) {
c5a282e9 1811 timed_out = 1;
abc610e0
DB
1812 break;
1813 }
412895f0
RP
1814
1815 /* We were woken up, thus go and try to harvest some events */
1816 eavail = 1;
1817
1818 } while (0);
1da177e4 1819
c5a282e9 1820 __set_current_state(TASK_RUNNING);
1da177e4 1821
412895f0
RP
1822 if (!list_empty_careful(&wait.entry)) {
1823 write_lock_irq(&ep->lock);
1824 __remove_wait_queue(&ep->wq, &wait);
1825 write_unlock_irq(&ep->lock);
1826 }
1827
35cff1a6 1828send_events:
65759097
RP
1829 if (fatal_signal_pending(current)) {
1830 /*
1831 * Always short-circuit for fatal signals to allow
1832 * threads to make a timely exit without the chance of
1833 * finding more events available and fetching
1834 * repeatedly.
1835 */
1836 res = -EINTR;
1837 }
1da177e4
LT
1838 /*
1839 * Try to transfer events to user space. In case we get 0 events and
1840 * there's still timeout left over, we go trying again in search of
1841 * more luck.
1842 */
1843 if (!res && eavail &&
95aac7b1 1844 !(res = ep_send_events(ep, events, maxevents)) && !timed_out)
f4d93ad7 1845 goto fetch_events;
1da177e4
LT
1846
1847 return res;
1848}
1849
22bacca4 1850/**
773318ed 1851 * ep_loop_check_proc - verify that adding an epoll file inside another
22bacca4
DL
1852 * epoll structure, does not violate the constraints, in
1853 * terms of closed loops, or too deep chains (which can
1854 * result in excessive stack usage).
1855 *
1856 * @priv: Pointer to the epoll file to be currently checked.
bde03c4c 1857 * @depth: Current depth of the path being checked.
22bacca4
DL
1858 *
1859 * Returns: Returns zero if adding the epoll @file inside current epoll
1860 * structure @ep does not violate the constraints, or -1 otherwise.
1861 */
bde03c4c 1862static int ep_loop_check_proc(struct eventpoll *ep, int depth)
22bacca4
DL
1863{
1864 int error = 0;
22bacca4
DL
1865 struct rb_node *rbp;
1866 struct epitem *epi;
1867
773318ed 1868 mutex_lock_nested(&ep->mtx, depth + 1);
18306c40 1869 ep->gen = loop_check_gen;
b2ac2ea6 1870 for (rbp = rb_first_cached(&ep->rbr); rbp; rbp = rb_next(rbp)) {
22bacca4
DL
1871 epi = rb_entry(rbp, struct epitem, rbn);
1872 if (unlikely(is_file_epoll(epi->ffd.file))) {
bde03c4c 1873 struct eventpoll *ep_tovisit;
28d82dc1 1874 ep_tovisit = epi->ffd.file->private_data;
18306c40 1875 if (ep_tovisit->gen == loop_check_gen)
28d82dc1 1876 continue;
bde03c4c 1877 if (ep_tovisit == inserting_into || depth > EP_MAX_NESTS)
56c428ca 1878 error = -1;
bde03c4c
AV
1879 else
1880 error = ep_loop_check_proc(ep_tovisit, depth + 1);
22bacca4
DL
1881 if (error != 0)
1882 break;
28d82dc1
JB
1883 } else {
1884 /*
1885 * If we've reached a file that is not associated with
1886 * an ep, then we need to check if the newly added
1887 * links are going to add too many wakeup paths. We do
1888 * this by adding it to the tfile_check_list, if it's
1889 * not already there, and calling reverse_path_check()
1890 * during ep_insert().
1891 */
a9ed4a65 1892 if (list_empty(&epi->ffd.file->f_tfile_llink)) {
77f4689d
AV
1893 if (get_file_rcu(epi->ffd.file))
1894 list_add(&epi->ffd.file->f_tfile_llink,
1895 &tfile_check_list);
a9ed4a65 1896 }
22bacca4
DL
1897 }
1898 }
1899 mutex_unlock(&ep->mtx);
1900
1901 return error;
1902}
1903
1904/**
bde03c4c
AV
1905 * ep_loop_check - Performs a check to verify that adding an epoll file (@to)
1906 * into another epoll file (represented by @from) does not create
22bacca4
DL
1907 * closed loops or too deep chains.
1908 *
bde03c4c
AV
1909 * @from: Pointer to the epoll we are inserting into.
1910 * @to: Pointer to the epoll to be inserted.
22bacca4 1911 *
bde03c4c
AV
1912 * Returns: Returns zero if adding the epoll @to inside the epoll @from
1913 * does not violate the constraints, or -1 otherwise.
22bacca4 1914 */
bde03c4c 1915static int ep_loop_check(struct eventpoll *ep, struct eventpoll *to)
22bacca4 1916{
6a3890c4 1917 inserting_into = ep;
bde03c4c 1918 return ep_loop_check_proc(to, 0);
28d82dc1
JB
1919}
1920
1921static void clear_tfile_check_list(void)
1922{
1923 struct file *file;
1924
1925 /* first clear the tfile_check_list */
1926 while (!list_empty(&tfile_check_list)) {
1927 file = list_first_entry(&tfile_check_list, struct file,
1928 f_tfile_llink);
1929 list_del_init(&file->f_tfile_llink);
a9ed4a65 1930 fput(file);
28d82dc1
JB
1931 }
1932 INIT_LIST_HEAD(&tfile_check_list);
22bacca4
DL
1933}
1934
7699acd1 1935/*
523723bb 1936 * Open an eventpoll file descriptor.
7699acd1 1937 */
791eb22e 1938static int do_epoll_create(int flags)
7699acd1 1939{
28d82dc1 1940 int error, fd;
bb57c3ed 1941 struct eventpoll *ep = NULL;
28d82dc1 1942 struct file *file;
7699acd1 1943
e38b36f3
UD
1944 /* Check the EPOLL_* constant for consistency. */
1945 BUILD_BUG_ON(EPOLL_CLOEXEC != O_CLOEXEC);
1946
296e236e
DL
1947 if (flags & ~EPOLL_CLOEXEC)
1948 return -EINVAL;
7699acd1 1949 /*
bb57c3ed 1950 * Create the internal data structure ("struct eventpoll").
7699acd1 1951 */
9fe5ad9c 1952 error = ep_alloc(&ep);
bb57c3ed
DL
1953 if (error < 0)
1954 return error;
7699acd1
DL
1955 /*
1956 * Creates all the items needed to setup an eventpoll file. That is,
2030a42c 1957 * a file structure and a free file descriptor.
7699acd1 1958 */
28d82dc1
JB
1959 fd = get_unused_fd_flags(O_RDWR | (flags & O_CLOEXEC));
1960 if (fd < 0) {
1961 error = fd;
1962 goto out_free_ep;
1963 }
1964 file = anon_inode_getfile("[eventpoll]", &eventpoll_fops, ep,
628ff7c1 1965 O_RDWR | (flags & O_CLOEXEC));
28d82dc1
JB
1966 if (IS_ERR(file)) {
1967 error = PTR_ERR(file);
1968 goto out_free_fd;
1969 }
28d82dc1 1970 ep->file = file;
98022748 1971 fd_install(fd, file);
28d82dc1
JB
1972 return fd;
1973
1974out_free_fd:
1975 put_unused_fd(fd);
1976out_free_ep:
1977 ep_free(ep);
bb57c3ed 1978 return error;
7699acd1
DL
1979}
1980
791eb22e
DB
1981SYSCALL_DEFINE1(epoll_create1, int, flags)
1982{
1983 return do_epoll_create(flags);
1984}
1985
5a8a82b1 1986SYSCALL_DEFINE1(epoll_create, int, size)
a0998b50 1987{
bfe3891a 1988 if (size <= 0)
9fe5ad9c
UD
1989 return -EINVAL;
1990
791eb22e 1991 return do_epoll_create(0);
a0998b50
UD
1992}
1993
39220e8d
JA
1994static inline int epoll_mutex_lock(struct mutex *mutex, int depth,
1995 bool nonblock)
1996{
1997 if (!nonblock) {
1998 mutex_lock_nested(mutex, depth);
1999 return 0;
2000 }
2001 if (mutex_trylock(mutex))
2002 return 0;
2003 return -EAGAIN;
2004}
2005
2006int do_epoll_ctl(int epfd, int op, int fd, struct epoll_event *epds,
2007 bool nonblock)
7699acd1
DL
2008{
2009 int error;
67347fe4 2010 int full_check = 0;
7e3fb584 2011 struct fd f, tf;
7699acd1
DL
2012 struct eventpoll *ep;
2013 struct epitem *epi;
67347fe4 2014 struct eventpoll *tep = NULL;
7699acd1 2015
7699acd1 2016 error = -EBADF;
7e3fb584
AV
2017 f = fdget(epfd);
2018 if (!f.file)
7699acd1
DL
2019 goto error_return;
2020
2021 /* Get the "struct file *" for the target file */
7e3fb584
AV
2022 tf = fdget(fd);
2023 if (!tf.file)
7699acd1
DL
2024 goto error_fput;
2025
2026 /* The target file descriptor must support poll */
2027 error = -EPERM;
9965ed17 2028 if (!file_can_poll(tf.file))
7699acd1
DL
2029 goto error_tgt_fput;
2030
4d7e30d9 2031 /* Check if EPOLLWAKEUP is allowed */
c680e41b 2032 if (ep_op_has_event(op))
58e41a44 2033 ep_take_care_of_epollwakeup(epds);
4d7e30d9 2034
7699acd1
DL
2035 /*
2036 * We have to check that the file structure underneath the file descriptor
2037 * the user passed to us _is_ an eventpoll file. And also we do not permit
2038 * adding an epoll file descriptor inside itself.
2039 */
2040 error = -EINVAL;
7e3fb584 2041 if (f.file == tf.file || !is_file_epoll(f.file))
7699acd1
DL
2042 goto error_tgt_fput;
2043
df0108c5
JB
2044 /*
2045 * epoll adds to the wakeup queue at EPOLL_CTL_ADD time only,
2046 * so EPOLLEXCLUSIVE is not allowed for a EPOLL_CTL_MOD operation.
2047 * Also, we do not currently supported nested exclusive wakeups.
2048 */
58e41a44 2049 if (ep_op_has_event(op) && (epds->events & EPOLLEXCLUSIVE)) {
b6a515c8
JB
2050 if (op == EPOLL_CTL_MOD)
2051 goto error_tgt_fput;
2052 if (op == EPOLL_CTL_ADD && (is_file_epoll(tf.file) ||
58e41a44 2053 (epds->events & ~EPOLLEXCLUSIVE_OK_BITS)))
b6a515c8
JB
2054 goto error_tgt_fput;
2055 }
df0108c5 2056
7699acd1
DL
2057 /*
2058 * At this point it is safe to assume that the "private_data" contains
2059 * our own data structure.
2060 */
7e3fb584 2061 ep = f.file->private_data;
7699acd1 2062
22bacca4
DL
2063 /*
2064 * When we insert an epoll file descriptor, inside another epoll file
2065 * descriptor, there is the change of creating closed loops, which are
28d82dc1
JB
2066 * better be handled here, than in more critical paths. While we are
2067 * checking for loops we also determine the list of files reachable
2068 * and hang them on the tfile_check_list, so we can check that we
2069 * haven't created too many possible wakeup paths.
22bacca4 2070 *
67347fe4
JB
2071 * We do not need to take the global 'epumutex' on EPOLL_CTL_ADD when
2072 * the epoll file descriptor is attaching directly to a wakeup source,
2073 * unless the epoll file descriptor is nested. The purpose of taking the
2074 * 'epmutex' on add is to prevent complex toplogies such as loops and
2075 * deep wakeup paths from forming in parallel through multiple
2076 * EPOLL_CTL_ADD operations.
22bacca4 2077 */
39220e8d
JA
2078 error = epoll_mutex_lock(&ep->mtx, 0, nonblock);
2079 if (error)
2080 goto error_tgt_fput;
28d82dc1 2081 if (op == EPOLL_CTL_ADD) {
67347fe4 2082 if (!list_empty(&f.file->f_ep_links) ||
fe0a916c 2083 ep->gen == loop_check_gen ||
67347fe4 2084 is_file_epoll(tf.file)) {
67347fe4 2085 mutex_unlock(&ep->mtx);
39220e8d
JA
2086 error = epoll_mutex_lock(&epmutex, 0, nonblock);
2087 if (error)
2088 goto error_tgt_fput;
18306c40 2089 loop_check_gen++;
39220e8d 2090 full_check = 1;
67347fe4 2091 if (is_file_epoll(tf.file)) {
bde03c4c 2092 tep = tf.file->private_data;
67347fe4 2093 error = -ELOOP;
bde03c4c 2094 if (ep_loop_check(ep, tep) != 0)
67347fe4 2095 goto error_tgt_fput;
a9ed4a65
MZ
2096 } else {
2097 get_file(tf.file);
67347fe4
JB
2098 list_add(&tf.file->f_tfile_llink,
2099 &tfile_check_list);
a9ed4a65 2100 }
39220e8d 2101 error = epoll_mutex_lock(&ep->mtx, 0, nonblock);
52c47969 2102 if (error)
39220e8d 2103 goto error_tgt_fput;
67347fe4 2104 if (is_file_epoll(tf.file)) {
39220e8d
JA
2105 error = epoll_mutex_lock(&tep->mtx, 1, nonblock);
2106 if (error) {
2107 mutex_unlock(&ep->mtx);
52c47969 2108 goto error_tgt_fput;
39220e8d 2109 }
13d51807 2110 }
67347fe4
JB
2111 }
2112 }
7699acd1 2113
67647d0f
DL
2114 /*
2115 * Try to lookup the file inside our RB tree, Since we grabbed "mtx"
2116 * above, we can be sure to be able to use the item looked up by
2117 * ep_find() till we release the mutex.
2118 */
7e3fb584 2119 epi = ep_find(ep, tf.file, fd);
7699acd1
DL
2120
2121 error = -EINVAL;
2122 switch (op) {
2123 case EPOLL_CTL_ADD:
2124 if (!epi) {
58e41a44
JA
2125 epds->events |= EPOLLERR | EPOLLHUP;
2126 error = ep_insert(ep, epds, tf.file, fd, full_check);
7699acd1
DL
2127 } else
2128 error = -EEXIST;
2129 break;
2130 case EPOLL_CTL_DEL:
2131 if (epi)
2132 error = ep_remove(ep, epi);
2133 else
2134 error = -ENOENT;
2135 break;
2136 case EPOLL_CTL_MOD:
2137 if (epi) {
b6a515c8 2138 if (!(epi->event.events & EPOLLEXCLUSIVE)) {
58e41a44
JA
2139 epds->events |= EPOLLERR | EPOLLHUP;
2140 error = ep_modify(ep, epi, epds);
b6a515c8 2141 }
7699acd1
DL
2142 } else
2143 error = -ENOENT;
2144 break;
2145 }
67347fe4
JB
2146 if (tep != NULL)
2147 mutex_unlock(&tep->mtx);
d47de16c 2148 mutex_unlock(&ep->mtx);
7699acd1
DL
2149
2150error_tgt_fput:
52c47969
AV
2151 if (full_check) {
2152 clear_tfile_check_list();
18306c40 2153 loop_check_gen++;
22bacca4 2154 mutex_unlock(&epmutex);
52c47969 2155 }
22bacca4 2156
7e3fb584 2157 fdput(tf);
7699acd1 2158error_fput:
7e3fb584 2159 fdput(f);
7699acd1 2160error_return:
7699acd1
DL
2161
2162 return error;
2163}
2164
58e41a44
JA
2165/*
2166 * The following function implements the controller interface for
2167 * the eventpoll file that enables the insertion/removal/change of
2168 * file descriptors inside the interest set.
2169 */
2170SYSCALL_DEFINE4(epoll_ctl, int, epfd, int, op, int, fd,
2171 struct epoll_event __user *, event)
2172{
2173 struct epoll_event epds;
2174
2175 if (ep_op_has_event(op) &&
2176 copy_from_user(&epds, event, sizeof(struct epoll_event)))
2177 return -EFAULT;
2178
39220e8d 2179 return do_epoll_ctl(epfd, op, fd, &epds, false);
58e41a44
JA
2180}
2181
7699acd1
DL
2182/*
2183 * Implement the event wait interface for the eventpoll file. It is the kernel
2184 * part of the user space epoll_wait(2).
2185 */
791eb22e
DB
2186static int do_epoll_wait(int epfd, struct epoll_event __user *events,
2187 int maxevents, int timeout)
7699acd1 2188{
2903ff01
AV
2189 int error;
2190 struct fd f;
7699acd1
DL
2191 struct eventpoll *ep;
2192
7699acd1
DL
2193 /* The maximum number of event must be greater than zero */
2194 if (maxevents <= 0 || maxevents > EP_MAX_EVENTS)
2195 return -EINVAL;
2196
2197 /* Verify that the area passed by the user is writeable */
96d4f267 2198 if (!access_ok(events, maxevents * sizeof(struct epoll_event)))
2903ff01 2199 return -EFAULT;
7699acd1
DL
2200
2201 /* Get the "struct file *" for the eventpoll file */
2903ff01
AV
2202 f = fdget(epfd);
2203 if (!f.file)
2204 return -EBADF;
7699acd1
DL
2205
2206 /*
2207 * We have to check that the file structure underneath the fd
2208 * the user passed to us _is_ an eventpoll file.
2209 */
2210 error = -EINVAL;
2903ff01 2211 if (!is_file_epoll(f.file))
7699acd1
DL
2212 goto error_fput;
2213
2214 /*
2215 * At this point it is safe to assume that the "private_data" contains
2216 * our own data structure.
2217 */
2903ff01 2218 ep = f.file->private_data;
7699acd1
DL
2219
2220 /* Time to fish for events ... */
2221 error = ep_poll(ep, events, maxevents, timeout);
2222
2223error_fput:
2903ff01 2224 fdput(f);
7699acd1
DL
2225 return error;
2226}
2227
791eb22e
DB
2228SYSCALL_DEFINE4(epoll_wait, int, epfd, struct epoll_event __user *, events,
2229 int, maxevents, int, timeout)
2230{
2231 return do_epoll_wait(epfd, events, maxevents, timeout);
2232}
2233
7699acd1
DL
2234/*
2235 * Implement the event wait interface for the eventpoll file. It is the kernel
2236 * part of the user space epoll_pwait(2).
2237 */
5a8a82b1
HC
2238SYSCALL_DEFINE6(epoll_pwait, int, epfd, struct epoll_event __user *, events,
2239 int, maxevents, int, timeout, const sigset_t __user *, sigmask,
2240 size_t, sigsetsize)
7699acd1
DL
2241{
2242 int error;
7699acd1
DL
2243
2244 /*
2245 * If the caller wants a certain signal mask to be set during the wait,
2246 * we apply it here.
2247 */
b772434b 2248 error = set_user_sigmask(sigmask, sigsetsize);
ded653cc
DD
2249 if (error)
2250 return error;
7699acd1 2251
791eb22e 2252 error = do_epoll_wait(epfd, events, maxevents, timeout);
b772434b 2253 restore_saved_sigmask_unless(error == -EINTR);
7699acd1
DL
2254
2255 return error;
2256}
2257
35280bd4
AV
2258#ifdef CONFIG_COMPAT
2259COMPAT_SYSCALL_DEFINE6(epoll_pwait, int, epfd,
2260 struct epoll_event __user *, events,
2261 int, maxevents, int, timeout,
2262 const compat_sigset_t __user *, sigmask,
2263 compat_size_t, sigsetsize)
2264{
2265 long err;
35280bd4
AV
2266
2267 /*
2268 * If the caller wants a certain signal mask to be set during the wait,
2269 * we apply it here.
2270 */
b772434b 2271 err = set_compat_user_sigmask(sigmask, sigsetsize);
ded653cc
DD
2272 if (err)
2273 return err;
35280bd4 2274
791eb22e 2275 err = do_epoll_wait(epfd, events, maxevents, timeout);
b772434b 2276 restore_saved_sigmask_unless(err == -EINTR);
35280bd4
AV
2277
2278 return err;
2279}
2280#endif
2281
1da177e4
LT
2282static int __init eventpoll_init(void)
2283{
7ef9964e
DL
2284 struct sysinfo si;
2285
2286 si_meminfo(&si);
9df04e1f
DL
2287 /*
2288 * Allows top 4% of lomem to be allocated for epoll watches (per user).
2289 */
2290 max_user_watches = (((si.totalram - si.totalhigh) / 25) << PAGE_SHIFT) /
7ef9964e 2291 EP_ITEM_COST;
52bd19f7 2292 BUG_ON(max_user_watches < 0);
1da177e4 2293
39732ca5
EW
2294 /*
2295 * We can have many thousands of epitems, so prevent this from
2296 * using an extra cache line on 64-bit (and smaller) CPUs
2297 */
2298 BUILD_BUG_ON(sizeof(void *) <= 8 && sizeof(struct epitem) > 128);
2299
1da177e4
LT
2300 /* Allocates slab cache used to allocate "struct epitem" items */
2301 epi_cache = kmem_cache_create("eventpoll_epi", sizeof(struct epitem),
2ae928a9 2302 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, NULL);
1da177e4
LT
2303
2304 /* Allocates slab cache used to allocate "struct eppoll_entry" */
2305 pwq_cache = kmem_cache_create("eventpoll_pwq",
2ae928a9 2306 sizeof(struct eppoll_entry), 0, SLAB_PANIC|SLAB_ACCOUNT, NULL);
1da177e4 2307
1da177e4 2308 return 0;
1da177e4 2309}
cea69241 2310fs_initcall(eventpoll_init);