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