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