]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * fs/eventpoll.c ( Efficent event polling implementation ) | |
3419b23a | 3 | * Copyright (C) 2001,...,2006 Davide Libenzi |
1da177e4 LT |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * Davide Libenzi <davidel@xmailserver.org> | |
11 | * | |
12 | */ | |
13 | ||
14 | #include <linux/module.h> | |
15 | #include <linux/init.h> | |
16 | #include <linux/kernel.h> | |
17 | #include <linux/sched.h> | |
18 | #include <linux/fs.h> | |
19 | #include <linux/file.h> | |
20 | #include <linux/signal.h> | |
21 | #include <linux/errno.h> | |
22 | #include <linux/mm.h> | |
23 | #include <linux/slab.h> | |
24 | #include <linux/poll.h> | |
1da177e4 LT |
25 | #include <linux/string.h> |
26 | #include <linux/list.h> | |
27 | #include <linux/hash.h> | |
28 | #include <linux/spinlock.h> | |
29 | #include <linux/syscalls.h> | |
30 | #include <linux/rwsem.h> | |
31 | #include <linux/rbtree.h> | |
32 | #include <linux/wait.h> | |
33 | #include <linux/eventpoll.h> | |
34 | #include <linux/mount.h> | |
35 | #include <linux/bitops.h> | |
144efe3e | 36 | #include <linux/mutex.h> |
da66f7cb | 37 | #include <linux/anon_inodes.h> |
1da177e4 LT |
38 | #include <asm/uaccess.h> |
39 | #include <asm/system.h> | |
40 | #include <asm/io.h> | |
41 | #include <asm/mman.h> | |
42 | #include <asm/atomic.h> | |
43 | #include <asm/semaphore.h> | |
44 | ||
45 | ||
46 | /* | |
47 | * LOCKING: | |
48 | * There are three level of locking required by epoll : | |
49 | * | |
144efe3e | 50 | * 1) epmutex (mutex) |
1da177e4 LT |
51 | * 2) ep->sem (rw_semaphore) |
52 | * 3) ep->lock (rw_lock) | |
53 | * | |
54 | * The acquire order is the one listed above, from 1 to 3. | |
55 | * We need a spinlock (ep->lock) because we manipulate objects | |
56 | * from inside the poll callback, that might be triggered from | |
57 | * a wake_up() that in turn might be called from IRQ context. | |
58 | * So we can't sleep inside the poll callback and hence we need | |
59 | * a spinlock. During the event transfer loop (from kernel to | |
60 | * user space) we could end up sleeping due a copy_to_user(), so | |
61 | * we need a lock that will allow us to sleep. This lock is a | |
62 | * read-write semaphore (ep->sem). It is acquired on read during | |
63 | * the event transfer loop and in write during epoll_ctl(EPOLL_CTL_DEL) | |
64 | * and during eventpoll_release_file(). Then we also need a global | |
65 | * semaphore to serialize eventpoll_release_file() and ep_free(). | |
66 | * This semaphore is acquired by ep_free() during the epoll file | |
67 | * cleanup path and it is also acquired by eventpoll_release_file() | |
68 | * if a file has been pushed inside an epoll set and it is then | |
69 | * close()d without a previous call toepoll_ctl(EPOLL_CTL_DEL). | |
70 | * It is possible to drop the "ep->sem" and to use the global | |
144efe3e | 71 | * semaphore "epmutex" (together with "ep->lock") to have it working, |
1da177e4 | 72 | * but having "ep->sem" will make the interface more scalable. |
144efe3e | 73 | * Events that require holding "epmutex" are very rare, while for |
1da177e4 LT |
74 | * normal operations the epoll private "ep->sem" will guarantee |
75 | * a greater scalability. | |
76 | */ | |
77 | ||
78 | ||
1da177e4 LT |
79 | #define DEBUG_EPOLL 0 |
80 | ||
81 | #if DEBUG_EPOLL > 0 | |
82 | #define DPRINTK(x) printk x | |
83 | #define DNPRINTK(n, x) do { if ((n) <= DEBUG_EPOLL) printk x; } while (0) | |
84 | #else /* #if DEBUG_EPOLL > 0 */ | |
85 | #define DPRINTK(x) (void) 0 | |
86 | #define DNPRINTK(n, x) (void) 0 | |
87 | #endif /* #if DEBUG_EPOLL > 0 */ | |
88 | ||
89 | #define DEBUG_EPI 0 | |
90 | ||
91 | #if DEBUG_EPI != 0 | |
92 | #define EPI_SLAB_DEBUG (SLAB_DEBUG_FREE | SLAB_RED_ZONE /* | SLAB_POISON */) | |
93 | #else /* #if DEBUG_EPI != 0 */ | |
94 | #define EPI_SLAB_DEBUG 0 | |
95 | #endif /* #if DEBUG_EPI != 0 */ | |
96 | ||
97 | /* Epoll private bits inside the event mask */ | |
98 | #define EP_PRIVATE_BITS (EPOLLONESHOT | EPOLLET) | |
99 | ||
100 | /* Maximum number of poll wake up nests we are allowing */ | |
101 | #define EP_MAX_POLLWAKE_NESTS 4 | |
102 | ||
e3306dd5 DL |
103 | /* Maximum msec timeout value storeable in a long int */ |
104 | #define EP_MAX_MSTIMEO min(1000ULL * MAX_SCHEDULE_TIMEOUT / HZ, (LONG_MAX - 999ULL) / HZ) | |
105 | ||
b611967d DL |
106 | #define EP_MAX_EVENTS (INT_MAX / sizeof(struct epoll_event)) |
107 | ||
e3306dd5 | 108 | |
1da177e4 LT |
109 | struct epoll_filefd { |
110 | struct file *file; | |
111 | int fd; | |
112 | }; | |
113 | ||
114 | /* | |
115 | * Node that is linked into the "wake_task_list" member of the "struct poll_safewake". | |
116 | * It is used to keep track on all tasks that are currently inside the wake_up() code | |
117 | * to 1) short-circuit the one coming from the same task and same wait queue head | |
118 | * ( loop ) 2) allow a maximum number of epoll descriptors inclusion nesting | |
119 | * 3) let go the ones coming from other tasks. | |
120 | */ | |
121 | struct wake_task_node { | |
122 | struct list_head llink; | |
36c8b586 | 123 | struct task_struct *task; |
1da177e4 LT |
124 | wait_queue_head_t *wq; |
125 | }; | |
126 | ||
127 | /* | |
128 | * This is used to implement the safe poll wake up avoiding to reenter | |
129 | * the poll callback from inside wake_up(). | |
130 | */ | |
131 | struct poll_safewake { | |
132 | struct list_head wake_task_list; | |
133 | spinlock_t lock; | |
134 | }; | |
135 | ||
136 | /* | |
137 | * This structure is stored inside the "private_data" member of the file | |
138 | * structure and rapresent the main data sructure for the eventpoll | |
139 | * interface. | |
140 | */ | |
141 | struct eventpoll { | |
142 | /* Protect the this structure access */ | |
143 | rwlock_t lock; | |
144 | ||
145 | /* | |
146 | * This semaphore is used to ensure that files are not removed | |
147 | * while epoll is using them. This is read-held during the event | |
148 | * collection loop and it is write-held during the file cleanup | |
149 | * path, the epoll file exit code and the ctl operations. | |
150 | */ | |
151 | struct rw_semaphore sem; | |
152 | ||
153 | /* Wait queue used by sys_epoll_wait() */ | |
154 | wait_queue_head_t wq; | |
155 | ||
156 | /* Wait queue used by file->poll() */ | |
157 | wait_queue_head_t poll_wait; | |
158 | ||
159 | /* List of ready file descriptors */ | |
160 | struct list_head rdllist; | |
161 | ||
162 | /* RB-Tree root used to store monitored fd structs */ | |
163 | struct rb_root rbr; | |
164 | }; | |
165 | ||
166 | /* Wait structure used by the poll hooks */ | |
167 | struct eppoll_entry { | |
168 | /* List header used to link this structure to the "struct epitem" */ | |
169 | struct list_head llink; | |
170 | ||
171 | /* The "base" pointer is set to the container "struct epitem" */ | |
172 | void *base; | |
173 | ||
174 | /* | |
175 | * Wait queue item that will be linked to the target file wait | |
176 | * queue head. | |
177 | */ | |
178 | wait_queue_t wait; | |
179 | ||
180 | /* The wait queue head that linked the "wait" wait queue item */ | |
181 | wait_queue_head_t *whead; | |
182 | }; | |
183 | ||
184 | /* | |
185 | * Each file descriptor added to the eventpoll interface will | |
6192bd53 | 186 | * have an entry of this type linked to the "rbr" RB tree. |
1da177e4 LT |
187 | */ |
188 | struct epitem { | |
189 | /* RB-Tree node used to link this structure to the eventpoll rb-tree */ | |
190 | struct rb_node rbn; | |
191 | ||
192 | /* List header used to link this structure to the eventpoll ready list */ | |
193 | struct list_head rdllink; | |
194 | ||
195 | /* The file descriptor information this item refers to */ | |
196 | struct epoll_filefd ffd; | |
197 | ||
198 | /* Number of active wait queue attached to poll operations */ | |
199 | int nwait; | |
200 | ||
201 | /* List containing poll wait queues */ | |
202 | struct list_head pwqlist; | |
203 | ||
204 | /* The "container" of this item */ | |
205 | struct eventpoll *ep; | |
206 | ||
207 | /* The structure that describe the interested events and the source fd */ | |
208 | struct epoll_event event; | |
209 | ||
210 | /* | |
211 | * Used to keep track of the usage count of the structure. This avoids | |
212 | * that the structure will desappear from underneath our processing. | |
213 | */ | |
214 | atomic_t usecnt; | |
215 | ||
216 | /* List header used to link this item to the "struct file" items list */ | |
217 | struct list_head fllink; | |
1da177e4 LT |
218 | }; |
219 | ||
220 | /* Wrapper struct used by poll queueing */ | |
221 | struct ep_pqueue { | |
222 | poll_table pt; | |
223 | struct epitem *epi; | |
224 | }; | |
225 | ||
226 | ||
227 | ||
228 | static void ep_poll_safewake_init(struct poll_safewake *psw); | |
229 | static void ep_poll_safewake(struct poll_safewake *psw, wait_queue_head_t *wq); | |
53d2be79 | 230 | static int ep_alloc(struct eventpoll **pep); |
1da177e4 LT |
231 | static void ep_free(struct eventpoll *ep); |
232 | static struct epitem *ep_find(struct eventpoll *ep, struct file *file, int fd); | |
233 | static void ep_use_epitem(struct epitem *epi); | |
234 | static void ep_release_epitem(struct epitem *epi); | |
235 | static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead, | |
236 | poll_table *pt); | |
237 | static void ep_rbtree_insert(struct eventpoll *ep, struct epitem *epi); | |
238 | static int ep_insert(struct eventpoll *ep, struct epoll_event *event, | |
239 | struct file *tfile, int fd); | |
240 | static int ep_modify(struct eventpoll *ep, struct epitem *epi, | |
241 | struct epoll_event *event); | |
242 | static void ep_unregister_pollwait(struct eventpoll *ep, struct epitem *epi); | |
243 | static int ep_unlink(struct eventpoll *ep, struct epitem *epi); | |
244 | static int ep_remove(struct eventpoll *ep, struct epitem *epi); | |
245 | static int ep_poll_callback(wait_queue_t *wait, unsigned mode, int sync, void *key); | |
246 | static int ep_eventpoll_close(struct inode *inode, struct file *file); | |
247 | static unsigned int ep_eventpoll_poll(struct file *file, poll_table *wait); | |
1da177e4 | 248 | static int ep_send_events(struct eventpoll *ep, struct list_head *txlist, |
6192bd53 | 249 | struct epoll_event __user *events, int maxevents); |
1da177e4 LT |
250 | static int ep_events_transfer(struct eventpoll *ep, |
251 | struct epoll_event __user *events, | |
252 | int maxevents); | |
253 | static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events, | |
254 | int maxevents, long timeout); | |
1da177e4 LT |
255 | |
256 | /* | |
257 | * This semaphore is used to serialize ep_free() and eventpoll_release_file(). | |
258 | */ | |
144efe3e | 259 | static struct mutex epmutex; |
1da177e4 LT |
260 | |
261 | /* Safe wake up implementation */ | |
262 | static struct poll_safewake psw; | |
263 | ||
264 | /* Slab cache used to allocate "struct epitem" */ | |
e18b890b | 265 | static struct kmem_cache *epi_cache __read_mostly; |
1da177e4 LT |
266 | |
267 | /* Slab cache used to allocate "struct eppoll_entry" */ | |
e18b890b | 268 | static struct kmem_cache *pwq_cache __read_mostly; |
1da177e4 | 269 | |
1da177e4 | 270 | /* File callbacks that implement the eventpoll file behaviour */ |
4b6f5d20 | 271 | static const struct file_operations eventpoll_fops = { |
1da177e4 LT |
272 | .release = ep_eventpoll_close, |
273 | .poll = ep_eventpoll_poll | |
274 | }; | |
275 | ||
1da177e4 LT |
276 | |
277 | ||
b030a4dd PE |
278 | /* Fast test to see if the file is an evenpoll file */ |
279 | static inline int is_file_epoll(struct file *f) | |
280 | { | |
281 | return f->f_op == &eventpoll_fops; | |
282 | } | |
283 | ||
284 | /* Setup the structure that is used as key for the rb-tree */ | |
285 | static inline void ep_set_ffd(struct epoll_filefd *ffd, | |
286 | struct file *file, int fd) | |
287 | { | |
288 | ffd->file = file; | |
289 | ffd->fd = fd; | |
290 | } | |
291 | ||
292 | /* Compare rb-tree keys */ | |
293 | static inline int ep_cmp_ffd(struct epoll_filefd *p1, | |
294 | struct epoll_filefd *p2) | |
295 | { | |
296 | return (p1->file > p2->file ? +1: | |
297 | (p1->file < p2->file ? -1 : p1->fd - p2->fd)); | |
298 | } | |
299 | ||
300 | /* Special initialization for the rb-tree node to detect linkage */ | |
301 | static inline void ep_rb_initnode(struct rb_node *n) | |
302 | { | |
c569882b | 303 | rb_set_parent(n, n); |
b030a4dd PE |
304 | } |
305 | ||
306 | /* Removes a node from the rb-tree and marks it for a fast is-linked check */ | |
307 | static inline void ep_rb_erase(struct rb_node *n, struct rb_root *r) | |
308 | { | |
309 | rb_erase(n, r); | |
c569882b | 310 | rb_set_parent(n, n); |
b030a4dd PE |
311 | } |
312 | ||
313 | /* Fast check to verify that the item is linked to the main rb-tree */ | |
314 | static inline int ep_rb_linked(struct rb_node *n) | |
315 | { | |
c569882b | 316 | return rb_parent(n) != n; |
b030a4dd PE |
317 | } |
318 | ||
b030a4dd PE |
319 | /* Tells us if the item is currently linked */ |
320 | static inline int ep_is_linked(struct list_head *p) | |
321 | { | |
322 | return !list_empty(p); | |
323 | } | |
324 | ||
325 | /* Get the "struct epitem" from a wait queue pointer */ | |
326 | static inline struct epitem * ep_item_from_wait(wait_queue_t *p) | |
327 | { | |
328 | return container_of(p, struct eppoll_entry, wait)->base; | |
329 | } | |
330 | ||
331 | /* Get the "struct epitem" from an epoll queue wrapper */ | |
332 | static inline struct epitem * ep_item_from_epqueue(poll_table *p) | |
333 | { | |
334 | return container_of(p, struct ep_pqueue, pt)->epi; | |
335 | } | |
336 | ||
337 | /* Tells if the epoll_ctl(2) operation needs an event copy from userspace */ | |
6192bd53 | 338 | static inline int ep_op_has_event(int op) |
b030a4dd PE |
339 | { |
340 | return op != EPOLL_CTL_DEL; | |
341 | } | |
342 | ||
1da177e4 LT |
343 | /* Initialize the poll safe wake up structure */ |
344 | static void ep_poll_safewake_init(struct poll_safewake *psw) | |
345 | { | |
346 | ||
347 | INIT_LIST_HEAD(&psw->wake_task_list); | |
348 | spin_lock_init(&psw->lock); | |
349 | } | |
350 | ||
351 | ||
352 | /* | |
353 | * Perform a safe wake up of the poll wait list. The problem is that | |
354 | * with the new callback'd wake up system, it is possible that the | |
355 | * poll callback is reentered from inside the call to wake_up() done | |
356 | * on the poll wait queue head. The rule is that we cannot reenter the | |
357 | * wake up code from the same task more than EP_MAX_POLLWAKE_NESTS times, | |
358 | * and we cannot reenter the same wait queue head at all. This will | |
359 | * enable to have a hierarchy of epoll file descriptor of no more than | |
360 | * EP_MAX_POLLWAKE_NESTS deep. We need the irq version of the spin lock | |
361 | * because this one gets called by the poll callback, that in turn is called | |
362 | * from inside a wake_up(), that might be called from irq context. | |
363 | */ | |
364 | static void ep_poll_safewake(struct poll_safewake *psw, wait_queue_head_t *wq) | |
365 | { | |
366 | int wake_nests = 0; | |
367 | unsigned long flags; | |
36c8b586 | 368 | struct task_struct *this_task = current; |
1da177e4 LT |
369 | struct list_head *lsthead = &psw->wake_task_list, *lnk; |
370 | struct wake_task_node *tncur; | |
371 | struct wake_task_node tnode; | |
372 | ||
373 | spin_lock_irqsave(&psw->lock, flags); | |
374 | ||
375 | /* Try to see if the current task is already inside this wakeup call */ | |
376 | list_for_each(lnk, lsthead) { | |
377 | tncur = list_entry(lnk, struct wake_task_node, llink); | |
378 | ||
379 | if (tncur->wq == wq || | |
380 | (tncur->task == this_task && ++wake_nests > EP_MAX_POLLWAKE_NESTS)) { | |
381 | /* | |
382 | * Ops ... loop detected or maximum nest level reached. | |
383 | * We abort this wake by breaking the cycle itself. | |
384 | */ | |
385 | spin_unlock_irqrestore(&psw->lock, flags); | |
386 | return; | |
387 | } | |
388 | } | |
389 | ||
390 | /* Add the current task to the list */ | |
391 | tnode.task = this_task; | |
392 | tnode.wq = wq; | |
393 | list_add(&tnode.llink, lsthead); | |
394 | ||
395 | spin_unlock_irqrestore(&psw->lock, flags); | |
396 | ||
397 | /* Do really wake up now */ | |
398 | wake_up(wq); | |
399 | ||
400 | /* Remove the current task from the list */ | |
401 | spin_lock_irqsave(&psw->lock, flags); | |
402 | list_del(&tnode.llink); | |
403 | spin_unlock_irqrestore(&psw->lock, flags); | |
404 | } | |
405 | ||
406 | ||
1da177e4 LT |
407 | /* |
408 | * This is called from eventpoll_release() to unlink files from the eventpoll | |
409 | * interface. We need to have this facility to cleanup correctly files that are | |
410 | * closed without being removed from the eventpoll interface. | |
411 | */ | |
412 | void eventpoll_release_file(struct file *file) | |
413 | { | |
414 | struct list_head *lsthead = &file->f_ep_links; | |
415 | struct eventpoll *ep; | |
416 | struct epitem *epi; | |
417 | ||
418 | /* | |
419 | * We don't want to get "file->f_ep_lock" because it is not | |
420 | * necessary. It is not necessary because we're in the "struct file" | |
421 | * cleanup path, and this means that noone is using this file anymore. | |
422 | * The only hit might come from ep_free() but by holding the semaphore | |
423 | * will correctly serialize the operation. We do need to acquire | |
144efe3e | 424 | * "ep->sem" after "epmutex" because ep_remove() requires it when called |
1da177e4 LT |
425 | * from anywhere but ep_free(). |
426 | */ | |
144efe3e | 427 | mutex_lock(&epmutex); |
1da177e4 LT |
428 | |
429 | while (!list_empty(lsthead)) { | |
b5e61818 | 430 | epi = list_first_entry(lsthead, struct epitem, fllink); |
1da177e4 LT |
431 | |
432 | ep = epi->ep; | |
6192bd53 | 433 | list_del_init(&epi->fllink); |
1da177e4 LT |
434 | down_write(&ep->sem); |
435 | ep_remove(ep, epi); | |
436 | up_write(&ep->sem); | |
437 | } | |
438 | ||
144efe3e | 439 | mutex_unlock(&epmutex); |
1da177e4 LT |
440 | } |
441 | ||
442 | ||
443 | /* | |
444 | * It opens an eventpoll file descriptor by suggesting a storage of "size" | |
445 | * file descriptors. The size parameter is just an hint about how to size | |
446 | * data structures. It won't prevent the user to store more than "size" | |
447 | * file descriptors inside the epoll interface. It is the kernel part of | |
448 | * the userspace epoll_create(2). | |
449 | */ | |
450 | asmlinkage long sys_epoll_create(int size) | |
451 | { | |
b611967d | 452 | int error, fd = -1; |
53d2be79 | 453 | struct eventpoll *ep; |
1da177e4 LT |
454 | struct inode *inode; |
455 | struct file *file; | |
456 | ||
457 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d)\n", | |
458 | current, size)); | |
459 | ||
53d2be79 DL |
460 | /* |
461 | * Sanity check on the size parameter, and create the internal data | |
462 | * structure ( "struct eventpoll" ). | |
463 | */ | |
1da177e4 | 464 | error = -EINVAL; |
53d2be79 | 465 | if (size <= 0 || (error = ep_alloc(&ep)) != 0) |
1da177e4 LT |
466 | goto eexit_1; |
467 | ||
468 | /* | |
469 | * Creates all the items needed to setup an eventpoll file. That is, | |
470 | * a file structure, and inode and a free file descriptor. | |
471 | */ | |
da66f7cb DL |
472 | error = anon_inode_getfd(&fd, &inode, &file, "[eventpoll]", |
473 | &eventpoll_fops, ep); | |
1da177e4 LT |
474 | if (error) |
475 | goto eexit_2; | |
476 | ||
1da177e4 LT |
477 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d) = %d\n", |
478 | current, size, fd)); | |
479 | ||
480 | return fd; | |
481 | ||
482 | eexit_2: | |
53d2be79 DL |
483 | ep_free(ep); |
484 | kfree(ep); | |
1da177e4 LT |
485 | eexit_1: |
486 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d) = %d\n", | |
487 | current, size, error)); | |
488 | return error; | |
489 | } | |
490 | ||
491 | ||
492 | /* | |
493 | * The following function implements the controller interface for | |
494 | * the eventpoll file that enables the insertion/removal/change of | |
495 | * file descriptors inside the interest set. It represents | |
496 | * the kernel part of the user space epoll_ctl(2). | |
497 | */ | |
498 | asmlinkage long | |
499 | sys_epoll_ctl(int epfd, int op, int fd, struct epoll_event __user *event) | |
500 | { | |
501 | int error; | |
502 | struct file *file, *tfile; | |
503 | struct eventpoll *ep; | |
504 | struct epitem *epi; | |
505 | struct epoll_event epds; | |
506 | ||
507 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_ctl(%d, %d, %d, %p)\n", | |
508 | current, epfd, op, fd, event)); | |
509 | ||
510 | error = -EFAULT; | |
6192bd53 | 511 | if (ep_op_has_event(op) && |
1da177e4 LT |
512 | copy_from_user(&epds, event, sizeof(struct epoll_event))) |
513 | goto eexit_1; | |
514 | ||
515 | /* Get the "struct file *" for the eventpoll file */ | |
516 | error = -EBADF; | |
517 | file = fget(epfd); | |
518 | if (!file) | |
519 | goto eexit_1; | |
520 | ||
521 | /* Get the "struct file *" for the target file */ | |
522 | tfile = fget(fd); | |
523 | if (!tfile) | |
524 | goto eexit_2; | |
525 | ||
526 | /* The target file descriptor must support poll */ | |
527 | error = -EPERM; | |
528 | if (!tfile->f_op || !tfile->f_op->poll) | |
529 | goto eexit_3; | |
530 | ||
531 | /* | |
532 | * We have to check that the file structure underneath the file descriptor | |
533 | * the user passed to us _is_ an eventpoll file. And also we do not permit | |
534 | * adding an epoll file descriptor inside itself. | |
535 | */ | |
536 | error = -EINVAL; | |
b030a4dd | 537 | if (file == tfile || !is_file_epoll(file)) |
1da177e4 LT |
538 | goto eexit_3; |
539 | ||
540 | /* | |
541 | * At this point it is safe to assume that the "private_data" contains | |
542 | * our own data structure. | |
543 | */ | |
544 | ep = file->private_data; | |
545 | ||
546 | down_write(&ep->sem); | |
547 | ||
6192bd53 | 548 | /* Try to lookup the file inside our RB tree */ |
1da177e4 LT |
549 | epi = ep_find(ep, tfile, fd); |
550 | ||
551 | error = -EINVAL; | |
552 | switch (op) { | |
553 | case EPOLL_CTL_ADD: | |
554 | if (!epi) { | |
2395140e | 555 | epds.events |= POLLERR | POLLHUP; |
1da177e4 LT |
556 | |
557 | error = ep_insert(ep, &epds, tfile, fd); | |
558 | } else | |
559 | error = -EEXIST; | |
560 | break; | |
561 | case EPOLL_CTL_DEL: | |
562 | if (epi) | |
563 | error = ep_remove(ep, epi); | |
564 | else | |
565 | error = -ENOENT; | |
566 | break; | |
567 | case EPOLL_CTL_MOD: | |
568 | if (epi) { | |
2395140e | 569 | epds.events |= POLLERR | POLLHUP; |
1da177e4 LT |
570 | error = ep_modify(ep, epi, &epds); |
571 | } else | |
572 | error = -ENOENT; | |
573 | break; | |
574 | } | |
575 | ||
576 | /* | |
577 | * The function ep_find() increments the usage count of the structure | |
578 | * so, if this is not NULL, we need to release it. | |
579 | */ | |
580 | if (epi) | |
581 | ep_release_epitem(epi); | |
582 | ||
583 | up_write(&ep->sem); | |
584 | ||
585 | eexit_3: | |
586 | fput(tfile); | |
587 | eexit_2: | |
588 | fput(file); | |
589 | eexit_1: | |
590 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_ctl(%d, %d, %d, %p) = %d\n", | |
591 | current, epfd, op, fd, event, error)); | |
592 | ||
593 | return error; | |
594 | } | |
595 | ||
1da177e4 LT |
596 | |
597 | /* | |
598 | * Implement the event wait interface for the eventpoll file. It is the kernel | |
599 | * part of the user space epoll_wait(2). | |
600 | */ | |
601 | asmlinkage long sys_epoll_wait(int epfd, struct epoll_event __user *events, | |
602 | int maxevents, int timeout) | |
603 | { | |
604 | int error; | |
605 | struct file *file; | |
606 | struct eventpoll *ep; | |
607 | ||
608 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_wait(%d, %p, %d, %d)\n", | |
609 | current, epfd, events, maxevents, timeout)); | |
610 | ||
611 | /* The maximum number of event must be greater than zero */ | |
b611967d | 612 | if (maxevents <= 0 || maxevents > EP_MAX_EVENTS) |
1da177e4 LT |
613 | return -EINVAL; |
614 | ||
615 | /* Verify that the area passed by the user is writeable */ | |
616 | if (!access_ok(VERIFY_WRITE, events, maxevents * sizeof(struct epoll_event))) { | |
617 | error = -EFAULT; | |
618 | goto eexit_1; | |
619 | } | |
620 | ||
621 | /* Get the "struct file *" for the eventpoll file */ | |
622 | error = -EBADF; | |
623 | file = fget(epfd); | |
624 | if (!file) | |
625 | goto eexit_1; | |
626 | ||
627 | /* | |
628 | * We have to check that the file structure underneath the fd | |
629 | * the user passed to us _is_ an eventpoll file. | |
630 | */ | |
631 | error = -EINVAL; | |
b030a4dd | 632 | if (!is_file_epoll(file)) |
1da177e4 LT |
633 | goto eexit_2; |
634 | ||
635 | /* | |
636 | * At this point it is safe to assume that the "private_data" contains | |
637 | * our own data structure. | |
638 | */ | |
639 | ep = file->private_data; | |
640 | ||
641 | /* Time to fish for events ... */ | |
642 | error = ep_poll(ep, events, maxevents, timeout); | |
643 | ||
644 | eexit_2: | |
645 | fput(file); | |
646 | eexit_1: | |
647 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_wait(%d, %p, %d, %d) = %d\n", | |
648 | current, epfd, events, maxevents, timeout, error)); | |
649 | ||
650 | return error; | |
651 | } | |
652 | ||
653 | ||
b611967d DL |
654 | #ifdef TIF_RESTORE_SIGMASK |
655 | ||
656 | /* | |
657 | * Implement the event wait interface for the eventpoll file. It is the kernel | |
658 | * part of the user space epoll_pwait(2). | |
659 | */ | |
660 | asmlinkage long sys_epoll_pwait(int epfd, struct epoll_event __user *events, | |
661 | int maxevents, int timeout, const sigset_t __user *sigmask, | |
662 | size_t sigsetsize) | |
663 | { | |
664 | int error; | |
665 | sigset_t ksigmask, sigsaved; | |
666 | ||
667 | /* | |
668 | * If the caller wants a certain signal mask to be set during the wait, | |
669 | * we apply it here. | |
670 | */ | |
671 | if (sigmask) { | |
672 | if (sigsetsize != sizeof(sigset_t)) | |
673 | return -EINVAL; | |
674 | if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask))) | |
675 | return -EFAULT; | |
676 | sigdelsetmask(&ksigmask, sigmask(SIGKILL) | sigmask(SIGSTOP)); | |
677 | sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved); | |
678 | } | |
679 | ||
680 | error = sys_epoll_wait(epfd, events, maxevents, timeout); | |
681 | ||
682 | /* | |
683 | * If we changed the signal mask, we need to restore the original one. | |
684 | * In case we've got a signal while waiting, we do not restore the | |
685 | * signal mask yet, and we allow do_signal() to deliver the signal on | |
686 | * the way back to userspace, before the signal mask is restored. | |
687 | */ | |
688 | if (sigmask) { | |
689 | if (error == -EINTR) { | |
690 | memcpy(¤t->saved_sigmask, &sigsaved, | |
691 | sizeof(sigsaved)); | |
692 | set_thread_flag(TIF_RESTORE_SIGMASK); | |
693 | } else | |
694 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); | |
695 | } | |
696 | ||
697 | return error; | |
698 | } | |
699 | ||
700 | #endif /* #ifdef TIF_RESTORE_SIGMASK */ | |
701 | ||
702 | ||
53d2be79 | 703 | static int ep_alloc(struct eventpoll **pep) |
1da177e4 | 704 | { |
53d2be79 | 705 | struct eventpoll *ep = kzalloc(sizeof(*ep), GFP_KERNEL); |
1da177e4 | 706 | |
53d2be79 | 707 | if (!ep) |
1da177e4 LT |
708 | return -ENOMEM; |
709 | ||
1da177e4 LT |
710 | rwlock_init(&ep->lock); |
711 | init_rwsem(&ep->sem); | |
712 | init_waitqueue_head(&ep->wq); | |
713 | init_waitqueue_head(&ep->poll_wait); | |
714 | INIT_LIST_HEAD(&ep->rdllist); | |
715 | ep->rbr = RB_ROOT; | |
716 | ||
53d2be79 | 717 | *pep = ep; |
1da177e4 | 718 | |
53d2be79 | 719 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_alloc() ep=%p\n", |
1da177e4 LT |
720 | current, ep)); |
721 | return 0; | |
722 | } | |
723 | ||
724 | ||
725 | static void ep_free(struct eventpoll *ep) | |
726 | { | |
727 | struct rb_node *rbp; | |
728 | struct epitem *epi; | |
729 | ||
730 | /* We need to release all tasks waiting for these file */ | |
731 | if (waitqueue_active(&ep->poll_wait)) | |
732 | ep_poll_safewake(&psw, &ep->poll_wait); | |
733 | ||
734 | /* | |
735 | * We need to lock this because we could be hit by | |
736 | * eventpoll_release_file() while we're freeing the "struct eventpoll". | |
737 | * We do not need to hold "ep->sem" here because the epoll file | |
738 | * is on the way to be removed and no one has references to it | |
739 | * anymore. The only hit might come from eventpoll_release_file() but | |
144efe3e | 740 | * holding "epmutex" is sufficent here. |
1da177e4 | 741 | */ |
144efe3e | 742 | mutex_lock(&epmutex); |
1da177e4 LT |
743 | |
744 | /* | |
745 | * Walks through the whole tree by unregistering poll callbacks. | |
746 | */ | |
747 | for (rbp = rb_first(&ep->rbr); rbp; rbp = rb_next(rbp)) { | |
748 | epi = rb_entry(rbp, struct epitem, rbn); | |
749 | ||
750 | ep_unregister_pollwait(ep, epi); | |
751 | } | |
752 | ||
753 | /* | |
6192bd53 | 754 | * Walks through the whole tree by freeing each "struct epitem". At this |
1da177e4 LT |
755 | * point we are sure no poll callbacks will be lingering around, and also by |
756 | * write-holding "sem" we can be sure that no file cleanup code will hit | |
757 | * us during this operation. So we can avoid the lock on "ep->lock". | |
758 | */ | |
759 | while ((rbp = rb_first(&ep->rbr)) != 0) { | |
760 | epi = rb_entry(rbp, struct epitem, rbn); | |
761 | ep_remove(ep, epi); | |
762 | } | |
763 | ||
144efe3e | 764 | mutex_unlock(&epmutex); |
1da177e4 LT |
765 | } |
766 | ||
767 | ||
768 | /* | |
6192bd53 | 769 | * Search the file inside the eventpoll tree. It add usage count to |
1da177e4 LT |
770 | * the returned item, so the caller must call ep_release_epitem() |
771 | * after finished using the "struct epitem". | |
772 | */ | |
773 | static struct epitem *ep_find(struct eventpoll *ep, struct file *file, int fd) | |
774 | { | |
775 | int kcmp; | |
776 | unsigned long flags; | |
777 | struct rb_node *rbp; | |
778 | struct epitem *epi, *epir = NULL; | |
779 | struct epoll_filefd ffd; | |
780 | ||
b030a4dd | 781 | ep_set_ffd(&ffd, file, fd); |
1da177e4 LT |
782 | read_lock_irqsave(&ep->lock, flags); |
783 | for (rbp = ep->rbr.rb_node; rbp; ) { | |
784 | epi = rb_entry(rbp, struct epitem, rbn); | |
b030a4dd | 785 | kcmp = ep_cmp_ffd(&ffd, &epi->ffd); |
1da177e4 LT |
786 | if (kcmp > 0) |
787 | rbp = rbp->rb_right; | |
788 | else if (kcmp < 0) | |
789 | rbp = rbp->rb_left; | |
790 | else { | |
791 | ep_use_epitem(epi); | |
792 | epir = epi; | |
793 | break; | |
794 | } | |
795 | } | |
796 | read_unlock_irqrestore(&ep->lock, flags); | |
797 | ||
798 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_find(%p) -> %p\n", | |
799 | current, file, epir)); | |
800 | ||
801 | return epir; | |
802 | } | |
803 | ||
804 | ||
805 | /* | |
806 | * Increment the usage count of the "struct epitem" making it sure | |
807 | * that the user will have a valid pointer to reference. | |
808 | */ | |
809 | static void ep_use_epitem(struct epitem *epi) | |
810 | { | |
811 | ||
812 | atomic_inc(&epi->usecnt); | |
813 | } | |
814 | ||
815 | ||
816 | /* | |
817 | * Decrement ( release ) the usage count by signaling that the user | |
818 | * has finished using the structure. It might lead to freeing the | |
819 | * structure itself if the count goes to zero. | |
820 | */ | |
821 | static void ep_release_epitem(struct epitem *epi) | |
822 | { | |
823 | ||
824 | if (atomic_dec_and_test(&epi->usecnt)) | |
b030a4dd | 825 | kmem_cache_free(epi_cache, epi); |
1da177e4 LT |
826 | } |
827 | ||
828 | ||
829 | /* | |
830 | * This is the callback that is used to add our wait queue to the | |
831 | * target file wakeup lists. | |
832 | */ | |
833 | static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead, | |
834 | poll_table *pt) | |
835 | { | |
b030a4dd | 836 | struct epitem *epi = ep_item_from_epqueue(pt); |
1da177e4 LT |
837 | struct eppoll_entry *pwq; |
838 | ||
e94b1766 | 839 | if (epi->nwait >= 0 && (pwq = kmem_cache_alloc(pwq_cache, GFP_KERNEL))) { |
1da177e4 LT |
840 | init_waitqueue_func_entry(&pwq->wait, ep_poll_callback); |
841 | pwq->whead = whead; | |
842 | pwq->base = epi; | |
843 | add_wait_queue(whead, &pwq->wait); | |
844 | list_add_tail(&pwq->llink, &epi->pwqlist); | |
845 | epi->nwait++; | |
846 | } else { | |
847 | /* We have to signal that an error occurred */ | |
848 | epi->nwait = -1; | |
849 | } | |
850 | } | |
851 | ||
852 | ||
853 | static void ep_rbtree_insert(struct eventpoll *ep, struct epitem *epi) | |
854 | { | |
855 | int kcmp; | |
856 | struct rb_node **p = &ep->rbr.rb_node, *parent = NULL; | |
857 | struct epitem *epic; | |
858 | ||
859 | while (*p) { | |
860 | parent = *p; | |
861 | epic = rb_entry(parent, struct epitem, rbn); | |
b030a4dd | 862 | kcmp = ep_cmp_ffd(&epi->ffd, &epic->ffd); |
1da177e4 LT |
863 | if (kcmp > 0) |
864 | p = &parent->rb_right; | |
865 | else | |
866 | p = &parent->rb_left; | |
867 | } | |
868 | rb_link_node(&epi->rbn, parent, p); | |
869 | rb_insert_color(&epi->rbn, &ep->rbr); | |
870 | } | |
871 | ||
872 | ||
873 | static int ep_insert(struct eventpoll *ep, struct epoll_event *event, | |
874 | struct file *tfile, int fd) | |
875 | { | |
876 | int error, revents, pwake = 0; | |
877 | unsigned long flags; | |
878 | struct epitem *epi; | |
879 | struct ep_pqueue epq; | |
880 | ||
881 | error = -ENOMEM; | |
e94b1766 | 882 | if (!(epi = kmem_cache_alloc(epi_cache, GFP_KERNEL))) |
1da177e4 LT |
883 | goto eexit_1; |
884 | ||
885 | /* Item initialization follow here ... */ | |
b030a4dd | 886 | ep_rb_initnode(&epi->rbn); |
1da177e4 LT |
887 | INIT_LIST_HEAD(&epi->rdllink); |
888 | INIT_LIST_HEAD(&epi->fllink); | |
1da177e4 LT |
889 | INIT_LIST_HEAD(&epi->pwqlist); |
890 | epi->ep = ep; | |
b030a4dd | 891 | ep_set_ffd(&epi->ffd, tfile, fd); |
1da177e4 LT |
892 | epi->event = *event; |
893 | atomic_set(&epi->usecnt, 1); | |
894 | epi->nwait = 0; | |
895 | ||
896 | /* Initialize the poll table using the queue callback */ | |
897 | epq.epi = epi; | |
898 | init_poll_funcptr(&epq.pt, ep_ptable_queue_proc); | |
899 | ||
900 | /* | |
901 | * Attach the item to the poll hooks and get current event bits. | |
902 | * We can safely use the file* here because its usage count has | |
903 | * been increased by the caller of this function. | |
904 | */ | |
905 | revents = tfile->f_op->poll(tfile, &epq.pt); | |
906 | ||
907 | /* | |
908 | * We have to check if something went wrong during the poll wait queue | |
909 | * install process. Namely an allocation for a wait queue failed due | |
910 | * high memory pressure. | |
911 | */ | |
912 | if (epi->nwait < 0) | |
913 | goto eexit_2; | |
914 | ||
915 | /* Add the current item to the list of active epoll hook for this file */ | |
916 | spin_lock(&tfile->f_ep_lock); | |
917 | list_add_tail(&epi->fllink, &tfile->f_ep_links); | |
918 | spin_unlock(&tfile->f_ep_lock); | |
919 | ||
920 | /* We have to drop the new item inside our item list to keep track of it */ | |
921 | write_lock_irqsave(&ep->lock, flags); | |
922 | ||
923 | /* Add the current item to the rb-tree */ | |
924 | ep_rbtree_insert(ep, epi); | |
925 | ||
926 | /* If the file is already "ready" we drop it inside the ready list */ | |
b030a4dd | 927 | if ((revents & event->events) && !ep_is_linked(&epi->rdllink)) { |
1da177e4 LT |
928 | list_add_tail(&epi->rdllink, &ep->rdllist); |
929 | ||
930 | /* Notify waiting tasks that events are available */ | |
931 | if (waitqueue_active(&ep->wq)) | |
3419b23a | 932 | __wake_up_locked(&ep->wq, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE); |
1da177e4 LT |
933 | if (waitqueue_active(&ep->poll_wait)) |
934 | pwake++; | |
935 | } | |
936 | ||
937 | write_unlock_irqrestore(&ep->lock, flags); | |
938 | ||
939 | /* We have to call this outside the lock */ | |
940 | if (pwake) | |
941 | ep_poll_safewake(&psw, &ep->poll_wait); | |
942 | ||
943 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_insert(%p, %p, %d)\n", | |
944 | current, ep, tfile, fd)); | |
945 | ||
946 | return 0; | |
947 | ||
948 | eexit_2: | |
949 | ep_unregister_pollwait(ep, epi); | |
950 | ||
951 | /* | |
952 | * We need to do this because an event could have been arrived on some | |
953 | * allocated wait queue. | |
954 | */ | |
955 | write_lock_irqsave(&ep->lock, flags); | |
b030a4dd | 956 | if (ep_is_linked(&epi->rdllink)) |
6192bd53 | 957 | list_del_init(&epi->rdllink); |
1da177e4 LT |
958 | write_unlock_irqrestore(&ep->lock, flags); |
959 | ||
b030a4dd | 960 | kmem_cache_free(epi_cache, epi); |
1da177e4 LT |
961 | eexit_1: |
962 | return error; | |
963 | } | |
964 | ||
965 | ||
966 | /* | |
967 | * Modify the interest event mask by dropping an event if the new mask | |
968 | * has a match in the current file status. | |
969 | */ | |
970 | static int ep_modify(struct eventpoll *ep, struct epitem *epi, struct epoll_event *event) | |
971 | { | |
972 | int pwake = 0; | |
973 | unsigned int revents; | |
974 | unsigned long flags; | |
975 | ||
976 | /* | |
977 | * Set the new event interest mask before calling f_op->poll(), otherwise | |
978 | * a potential race might occur. In fact if we do this operation inside | |
979 | * the lock, an event might happen between the f_op->poll() call and the | |
980 | * new event set registering. | |
981 | */ | |
982 | epi->event.events = event->events; | |
983 | ||
984 | /* | |
985 | * Get current event bits. We can safely use the file* here because | |
986 | * its usage count has been increased by the caller of this function. | |
987 | */ | |
988 | revents = epi->ffd.file->f_op->poll(epi->ffd.file, NULL); | |
989 | ||
990 | write_lock_irqsave(&ep->lock, flags); | |
991 | ||
992 | /* Copy the data member from inside the lock */ | |
993 | epi->event.data = event->data; | |
994 | ||
995 | /* | |
6192bd53 | 996 | * If the item is not linked to the RB tree it means that it's on its |
1da177e4 LT |
997 | * way toward the removal. Do nothing in this case. |
998 | */ | |
b030a4dd | 999 | if (ep_rb_linked(&epi->rbn)) { |
1da177e4 LT |
1000 | /* |
1001 | * If the item is "hot" and it is not registered inside the ready | |
1002 | * list, push it inside. If the item is not "hot" and it is currently | |
1003 | * registered inside the ready list, unlink it. | |
1004 | */ | |
1005 | if (revents & event->events) { | |
b030a4dd | 1006 | if (!ep_is_linked(&epi->rdllink)) { |
1da177e4 LT |
1007 | list_add_tail(&epi->rdllink, &ep->rdllist); |
1008 | ||
1009 | /* Notify waiting tasks that events are available */ | |
1010 | if (waitqueue_active(&ep->wq)) | |
3419b23a DL |
1011 | __wake_up_locked(&ep->wq, TASK_UNINTERRUPTIBLE | |
1012 | TASK_INTERRUPTIBLE); | |
1da177e4 LT |
1013 | if (waitqueue_active(&ep->poll_wait)) |
1014 | pwake++; | |
1015 | } | |
1016 | } | |
1017 | } | |
1018 | ||
1019 | write_unlock_irqrestore(&ep->lock, flags); | |
1020 | ||
1021 | /* We have to call this outside the lock */ | |
1022 | if (pwake) | |
1023 | ep_poll_safewake(&psw, &ep->poll_wait); | |
1024 | ||
1025 | return 0; | |
1026 | } | |
1027 | ||
1028 | ||
1029 | /* | |
1030 | * This function unregister poll callbacks from the associated file descriptor. | |
1031 | * Since this must be called without holding "ep->lock" the atomic exchange trick | |
1032 | * will protect us from multiple unregister. | |
1033 | */ | |
1034 | static void ep_unregister_pollwait(struct eventpoll *ep, struct epitem *epi) | |
1035 | { | |
1036 | int nwait; | |
1037 | struct list_head *lsthead = &epi->pwqlist; | |
1038 | struct eppoll_entry *pwq; | |
1039 | ||
1040 | /* This is called without locks, so we need the atomic exchange */ | |
1041 | nwait = xchg(&epi->nwait, 0); | |
1042 | ||
1043 | if (nwait) { | |
1044 | while (!list_empty(lsthead)) { | |
b5e61818 | 1045 | pwq = list_first_entry(lsthead, struct eppoll_entry, llink); |
1da177e4 | 1046 | |
6192bd53 | 1047 | list_del_init(&pwq->llink); |
1da177e4 | 1048 | remove_wait_queue(pwq->whead, &pwq->wait); |
b030a4dd | 1049 | kmem_cache_free(pwq_cache, pwq); |
1da177e4 LT |
1050 | } |
1051 | } | |
1052 | } | |
1053 | ||
1054 | ||
1055 | /* | |
1056 | * Unlink the "struct epitem" from all places it might have been hooked up. | |
1057 | * This function must be called with write IRQ lock on "ep->lock". | |
1058 | */ | |
1059 | static int ep_unlink(struct eventpoll *ep, struct epitem *epi) | |
1060 | { | |
1061 | int error; | |
1062 | ||
1063 | /* | |
1064 | * It can happen that this one is called for an item already unlinked. | |
1065 | * The check protect us from doing a double unlink ( crash ). | |
1066 | */ | |
1067 | error = -ENOENT; | |
b030a4dd | 1068 | if (!ep_rb_linked(&epi->rbn)) |
1da177e4 LT |
1069 | goto eexit_1; |
1070 | ||
1071 | /* | |
1072 | * Clear the event mask for the unlinked item. This will avoid item | |
1073 | * notifications to be sent after the unlink operation from inside | |
1074 | * the kernel->userspace event transfer loop. | |
1075 | */ | |
1076 | epi->event.events = 0; | |
1077 | ||
1078 | /* | |
1079 | * At this point is safe to do the job, unlink the item from our rb-tree. | |
1080 | * This operation togheter with the above check closes the door to | |
1081 | * double unlinks. | |
1082 | */ | |
b030a4dd | 1083 | ep_rb_erase(&epi->rbn, &ep->rbr); |
1da177e4 LT |
1084 | |
1085 | /* | |
1086 | * If the item we are going to remove is inside the ready file descriptors | |
1087 | * we want to remove it from this list to avoid stale events. | |
1088 | */ | |
b030a4dd | 1089 | if (ep_is_linked(&epi->rdllink)) |
6192bd53 | 1090 | list_del_init(&epi->rdllink); |
1da177e4 LT |
1091 | |
1092 | error = 0; | |
1093 | eexit_1: | |
1094 | ||
1095 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_unlink(%p, %p) = %d\n", | |
45f17e0c | 1096 | current, ep, epi->ffd.file, error)); |
1da177e4 LT |
1097 | |
1098 | return error; | |
1099 | } | |
1100 | ||
1101 | ||
1102 | /* | |
6192bd53 | 1103 | * Removes a "struct epitem" from the eventpoll RB tree and deallocates |
1da177e4 LT |
1104 | * all the associated resources. |
1105 | */ | |
1106 | static int ep_remove(struct eventpoll *ep, struct epitem *epi) | |
1107 | { | |
1108 | int error; | |
1109 | unsigned long flags; | |
1110 | struct file *file = epi->ffd.file; | |
1111 | ||
1112 | /* | |
1113 | * Removes poll wait queue hooks. We _have_ to do this without holding | |
1114 | * the "ep->lock" otherwise a deadlock might occur. This because of the | |
1115 | * sequence of the lock acquisition. Here we do "ep->lock" then the wait | |
1116 | * queue head lock when unregistering the wait queue. The wakeup callback | |
1117 | * will run by holding the wait queue head lock and will call our callback | |
1118 | * that will try to get "ep->lock". | |
1119 | */ | |
1120 | ep_unregister_pollwait(ep, epi); | |
1121 | ||
1122 | /* Remove the current item from the list of epoll hooks */ | |
1123 | spin_lock(&file->f_ep_lock); | |
b030a4dd | 1124 | if (ep_is_linked(&epi->fllink)) |
6192bd53 | 1125 | list_del_init(&epi->fllink); |
1da177e4 LT |
1126 | spin_unlock(&file->f_ep_lock); |
1127 | ||
1128 | /* We need to acquire the write IRQ lock before calling ep_unlink() */ | |
1129 | write_lock_irqsave(&ep->lock, flags); | |
1130 | ||
6192bd53 | 1131 | /* Really unlink the item from the RB tree */ |
1da177e4 LT |
1132 | error = ep_unlink(ep, epi); |
1133 | ||
1134 | write_unlock_irqrestore(&ep->lock, flags); | |
1135 | ||
1136 | if (error) | |
1137 | goto eexit_1; | |
1138 | ||
1139 | /* At this point it is safe to free the eventpoll item */ | |
1140 | ep_release_epitem(epi); | |
1141 | ||
1142 | error = 0; | |
1143 | eexit_1: | |
1144 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_remove(%p, %p) = %d\n", | |
1145 | current, ep, file, error)); | |
1146 | ||
1147 | return error; | |
1148 | } | |
1149 | ||
1150 | ||
1151 | /* | |
1152 | * This is the callback that is passed to the wait queue wakeup | |
1153 | * machanism. It is called by the stored file descriptors when they | |
1154 | * have events to report. | |
1155 | */ | |
1156 | static int ep_poll_callback(wait_queue_t *wait, unsigned mode, int sync, void *key) | |
1157 | { | |
1158 | int pwake = 0; | |
1159 | unsigned long flags; | |
b030a4dd | 1160 | struct epitem *epi = ep_item_from_wait(wait); |
1da177e4 LT |
1161 | struct eventpoll *ep = epi->ep; |
1162 | ||
1163 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: poll_callback(%p) epi=%p ep=%p\n", | |
45f17e0c | 1164 | current, epi->ffd.file, epi, ep)); |
1da177e4 LT |
1165 | |
1166 | write_lock_irqsave(&ep->lock, flags); | |
1167 | ||
1168 | /* | |
1169 | * If the event mask does not contain any poll(2) event, we consider the | |
1170 | * descriptor to be disabled. This condition is likely the effect of the | |
1171 | * EPOLLONESHOT bit that disables the descriptor when an event is received, | |
1172 | * until the next EPOLL_CTL_MOD will be issued. | |
1173 | */ | |
1174 | if (!(epi->event.events & ~EP_PRIVATE_BITS)) | |
1175 | goto is_disabled; | |
1176 | ||
1177 | /* If this file is already in the ready list we exit soon */ | |
b030a4dd | 1178 | if (ep_is_linked(&epi->rdllink)) |
1da177e4 LT |
1179 | goto is_linked; |
1180 | ||
1181 | list_add_tail(&epi->rdllink, &ep->rdllist); | |
1182 | ||
1183 | is_linked: | |
1184 | /* | |
1185 | * Wake up ( if active ) both the eventpoll wait list and the ->poll() | |
1186 | * wait list. | |
1187 | */ | |
1188 | if (waitqueue_active(&ep->wq)) | |
3419b23a DL |
1189 | __wake_up_locked(&ep->wq, TASK_UNINTERRUPTIBLE | |
1190 | TASK_INTERRUPTIBLE); | |
1da177e4 LT |
1191 | if (waitqueue_active(&ep->poll_wait)) |
1192 | pwake++; | |
1193 | ||
1194 | is_disabled: | |
1195 | write_unlock_irqrestore(&ep->lock, flags); | |
1196 | ||
1197 | /* We have to call this outside the lock */ | |
1198 | if (pwake) | |
1199 | ep_poll_safewake(&psw, &ep->poll_wait); | |
1200 | ||
1201 | return 1; | |
1202 | } | |
1203 | ||
1204 | ||
1205 | static int ep_eventpoll_close(struct inode *inode, struct file *file) | |
1206 | { | |
1207 | struct eventpoll *ep = file->private_data; | |
1208 | ||
1209 | if (ep) { | |
1210 | ep_free(ep); | |
1211 | kfree(ep); | |
1212 | } | |
1213 | ||
1214 | DNPRINTK(3, (KERN_INFO "[%p] eventpoll: close() ep=%p\n", current, ep)); | |
1215 | return 0; | |
1216 | } | |
1217 | ||
1218 | ||
1219 | static unsigned int ep_eventpoll_poll(struct file *file, poll_table *wait) | |
1220 | { | |
1221 | unsigned int pollflags = 0; | |
1222 | unsigned long flags; | |
1223 | struct eventpoll *ep = file->private_data; | |
1224 | ||
1225 | /* Insert inside our poll wait queue */ | |
1226 | poll_wait(file, &ep->poll_wait, wait); | |
1227 | ||
1228 | /* Check our condition */ | |
1229 | read_lock_irqsave(&ep->lock, flags); | |
1230 | if (!list_empty(&ep->rdllist)) | |
1231 | pollflags = POLLIN | POLLRDNORM; | |
1232 | read_unlock_irqrestore(&ep->lock, flags); | |
1233 | ||
1234 | return pollflags; | |
1235 | } | |
1236 | ||
1237 | ||
1da177e4 LT |
1238 | /* |
1239 | * This function is called without holding the "ep->lock" since the call to | |
1240 | * __copy_to_user() might sleep, and also f_op->poll() might reenable the IRQ | |
1241 | * because of the way poll() is traditionally implemented in Linux. | |
1242 | */ | |
1243 | static int ep_send_events(struct eventpoll *ep, struct list_head *txlist, | |
6192bd53 | 1244 | struct epoll_event __user *events, int maxevents) |
1da177e4 | 1245 | { |
6192bd53 | 1246 | int eventcnt, error = -EFAULT, pwake = 0; |
1da177e4 | 1247 | unsigned int revents; |
6192bd53 | 1248 | unsigned long flags; |
1da177e4 | 1249 | struct epitem *epi; |
6192bd53 DL |
1250 | struct list_head injlist; |
1251 | ||
1252 | INIT_LIST_HEAD(&injlist); | |
1da177e4 LT |
1253 | |
1254 | /* | |
1255 | * We can loop without lock because this is a task private list. | |
6192bd53 DL |
1256 | * We just splice'd out the ep->rdllist in ep_collect_ready_items(). |
1257 | * Items cannot vanish during the loop because we are holding "sem" in | |
1258 | * read. | |
1da177e4 | 1259 | */ |
6192bd53 | 1260 | for (eventcnt = 0; !list_empty(txlist) && eventcnt < maxevents;) { |
b5e61818 | 1261 | epi = list_first_entry(txlist, struct epitem, rdllink); |
6192bd53 | 1262 | prefetch(epi->rdllink.next); |
1da177e4 LT |
1263 | |
1264 | /* | |
1265 | * Get the ready file event set. We can safely use the file | |
1266 | * because we are holding the "sem" in read and this will | |
1267 | * guarantee that both the file and the item will not vanish. | |
1268 | */ | |
1269 | revents = epi->ffd.file->f_op->poll(epi->ffd.file, NULL); | |
6192bd53 | 1270 | revents &= epi->event.events; |
1da177e4 LT |
1271 | |
1272 | /* | |
6192bd53 DL |
1273 | * Is the event mask intersect the caller-requested one, |
1274 | * deliver the event to userspace. Again, we are holding | |
1275 | * "sem" in read, so no operations coming from userspace | |
1276 | * can change the item. | |
1da177e4 | 1277 | */ |
6192bd53 DL |
1278 | if (revents) { |
1279 | if (__put_user(revents, | |
1da177e4 LT |
1280 | &events[eventcnt].events) || |
1281 | __put_user(epi->event.data, | |
1282 | &events[eventcnt].data)) | |
6192bd53 | 1283 | goto errxit; |
1da177e4 LT |
1284 | if (epi->event.events & EPOLLONESHOT) |
1285 | epi->event.events &= EP_PRIVATE_BITS; | |
1286 | eventcnt++; | |
1287 | } | |
1da177e4 LT |
1288 | |
1289 | /* | |
6192bd53 DL |
1290 | * This is tricky. We are holding the "sem" in read, and this |
1291 | * means that the operations that can change the "linked" status | |
1292 | * of the epoll item (epi->rbn and epi->rdllink), cannot touch | |
1293 | * them. Also, since we are "linked" from a epi->rdllink POV | |
1294 | * (the item is linked to our transmission list we just | |
1295 | * spliced), the ep_poll_callback() cannot touch us either, | |
1296 | * because of the check present in there. Another parallel | |
1297 | * epoll_wait() will not get the same result set, since we | |
1298 | * spliced the ready list before. Note that list_del() still | |
1299 | * shows the item as linked to the test in ep_poll_callback(). | |
1da177e4 | 1300 | */ |
6192bd53 DL |
1301 | list_del(&epi->rdllink); |
1302 | if (!(epi->event.events & EPOLLET) && | |
1303 | (revents & epi->event.events)) | |
1304 | list_add_tail(&epi->rdllink, &injlist); | |
1305 | else { | |
1306 | /* | |
1307 | * Be sure the item is totally detached before re-init | |
1308 | * the list_head. After INIT_LIST_HEAD() is committed, | |
1309 | * the ep_poll_callback() can requeue the item again, | |
1310 | * but we don't care since we are already past it. | |
1311 | */ | |
1312 | smp_mb(); | |
1313 | INIT_LIST_HEAD(&epi->rdllink); | |
1da177e4 LT |
1314 | } |
1315 | } | |
6192bd53 | 1316 | error = 0; |
1da177e4 | 1317 | |
6192bd53 DL |
1318 | errxit: |
1319 | ||
1320 | /* | |
1321 | * If the re-injection list or the txlist are not empty, re-splice | |
1322 | * them to the ready list and do proper wakeups. | |
1323 | */ | |
1324 | if (!list_empty(&injlist) || !list_empty(txlist)) { | |
1325 | write_lock_irqsave(&ep->lock, flags); | |
1326 | ||
1327 | list_splice(txlist, &ep->rdllist); | |
1328 | list_splice(&injlist, &ep->rdllist); | |
1da177e4 LT |
1329 | /* |
1330 | * Wake up ( if active ) both the eventpoll wait list and the ->poll() | |
1331 | * wait list. | |
1332 | */ | |
1333 | if (waitqueue_active(&ep->wq)) | |
3419b23a DL |
1334 | __wake_up_locked(&ep->wq, TASK_UNINTERRUPTIBLE | |
1335 | TASK_INTERRUPTIBLE); | |
1da177e4 LT |
1336 | if (waitqueue_active(&ep->poll_wait)) |
1337 | pwake++; | |
1da177e4 | 1338 | |
6192bd53 DL |
1339 | write_unlock_irqrestore(&ep->lock, flags); |
1340 | } | |
1da177e4 LT |
1341 | |
1342 | /* We have to call this outside the lock */ | |
1343 | if (pwake) | |
1344 | ep_poll_safewake(&psw, &ep->poll_wait); | |
6192bd53 DL |
1345 | |
1346 | return eventcnt == 0 ? error: eventcnt; | |
1da177e4 LT |
1347 | } |
1348 | ||
1349 | ||
1350 | /* | |
1351 | * Perform the transfer of events to user space. | |
1352 | */ | |
1353 | static int ep_events_transfer(struct eventpoll *ep, | |
1354 | struct epoll_event __user *events, int maxevents) | |
1355 | { | |
6192bd53 DL |
1356 | int eventcnt; |
1357 | unsigned long flags; | |
1da177e4 LT |
1358 | struct list_head txlist; |
1359 | ||
1360 | INIT_LIST_HEAD(&txlist); | |
1361 | ||
1362 | /* | |
1363 | * We need to lock this because we could be hit by | |
1364 | * eventpoll_release_file() and epoll_ctl(EPOLL_CTL_DEL). | |
1365 | */ | |
1366 | down_read(&ep->sem); | |
1367 | ||
6192bd53 DL |
1368 | /* |
1369 | * Steal the ready list, and re-init the original one to the | |
1370 | * empty list. | |
1371 | */ | |
1372 | write_lock_irqsave(&ep->lock, flags); | |
1373 | list_splice(&ep->rdllist, &txlist); | |
1374 | INIT_LIST_HEAD(&ep->rdllist); | |
1375 | write_unlock_irqrestore(&ep->lock, flags); | |
1da177e4 | 1376 | |
6192bd53 DL |
1377 | /* Build result set in userspace */ |
1378 | eventcnt = ep_send_events(ep, &txlist, events, maxevents); | |
1da177e4 LT |
1379 | |
1380 | up_read(&ep->sem); | |
1381 | ||
1382 | return eventcnt; | |
1383 | } | |
1384 | ||
1385 | ||
1386 | static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events, | |
1387 | int maxevents, long timeout) | |
1388 | { | |
1389 | int res, eavail; | |
1390 | unsigned long flags; | |
1391 | long jtimeout; | |
1392 | wait_queue_t wait; | |
1393 | ||
1394 | /* | |
1395 | * Calculate the timeout by checking for the "infinite" value ( -1 ) | |
1396 | * and the overflow condition. The passed timeout is in milliseconds, | |
1397 | * that why (t * HZ) / 1000. | |
1398 | */ | |
e3306dd5 DL |
1399 | jtimeout = (timeout < 0 || timeout >= EP_MAX_MSTIMEO) ? |
1400 | MAX_SCHEDULE_TIMEOUT : (timeout * HZ + 999) / 1000; | |
1da177e4 LT |
1401 | |
1402 | retry: | |
1403 | write_lock_irqsave(&ep->lock, flags); | |
1404 | ||
1405 | res = 0; | |
1406 | if (list_empty(&ep->rdllist)) { | |
1407 | /* | |
1408 | * We don't have any available event to return to the caller. | |
1409 | * We need to sleep here, and we will be wake up by | |
1410 | * ep_poll_callback() when events will become available. | |
1411 | */ | |
1412 | init_waitqueue_entry(&wait, current); | |
3419b23a | 1413 | __add_wait_queue(&ep->wq, &wait); |
1da177e4 LT |
1414 | |
1415 | for (;;) { | |
1416 | /* | |
1417 | * We don't want to sleep if the ep_poll_callback() sends us | |
1418 | * a wakeup in between. That's why we set the task state | |
1419 | * to TASK_INTERRUPTIBLE before doing the checks. | |
1420 | */ | |
1421 | set_current_state(TASK_INTERRUPTIBLE); | |
1422 | if (!list_empty(&ep->rdllist) || !jtimeout) | |
1423 | break; | |
1424 | if (signal_pending(current)) { | |
1425 | res = -EINTR; | |
1426 | break; | |
1427 | } | |
1428 | ||
1429 | write_unlock_irqrestore(&ep->lock, flags); | |
1430 | jtimeout = schedule_timeout(jtimeout); | |
1431 | write_lock_irqsave(&ep->lock, flags); | |
1432 | } | |
3419b23a | 1433 | __remove_wait_queue(&ep->wq, &wait); |
1da177e4 LT |
1434 | |
1435 | set_current_state(TASK_RUNNING); | |
1436 | } | |
1437 | ||
1438 | /* Is it worth to try to dig for events ? */ | |
1439 | eavail = !list_empty(&ep->rdllist); | |
1440 | ||
1441 | write_unlock_irqrestore(&ep->lock, flags); | |
1442 | ||
1443 | /* | |
1444 | * Try to transfer events to user space. In case we get 0 events and | |
1445 | * there's still timeout left over, we go trying again in search of | |
1446 | * more luck. | |
1447 | */ | |
1448 | if (!res && eavail && | |
1449 | !(res = ep_events_transfer(ep, events, maxevents)) && jtimeout) | |
1450 | goto retry; | |
1451 | ||
1452 | return res; | |
1453 | } | |
1454 | ||
1da177e4 LT |
1455 | static int __init eventpoll_init(void) |
1456 | { | |
144efe3e | 1457 | mutex_init(&epmutex); |
1da177e4 LT |
1458 | |
1459 | /* Initialize the structure used to perform safe poll wait head wake ups */ | |
1460 | ep_poll_safewake_init(&psw); | |
1461 | ||
1462 | /* Allocates slab cache used to allocate "struct epitem" items */ | |
1463 | epi_cache = kmem_cache_create("eventpoll_epi", sizeof(struct epitem), | |
1464 | 0, SLAB_HWCACHE_ALIGN|EPI_SLAB_DEBUG|SLAB_PANIC, | |
1465 | NULL, NULL); | |
1466 | ||
1467 | /* Allocates slab cache used to allocate "struct eppoll_entry" */ | |
1468 | pwq_cache = kmem_cache_create("eventpoll_pwq", | |
1469 | sizeof(struct eppoll_entry), 0, | |
1470 | EPI_SLAB_DEBUG|SLAB_PANIC, NULL, NULL); | |
1471 | ||
1da177e4 | 1472 | return 0; |
1da177e4 LT |
1473 | } |
1474 | ||
1475 | ||
1476 | static void __exit eventpoll_exit(void) | |
1477 | { | |
1478 | /* Undo all operations done inside eventpoll_init() */ | |
1da177e4 LT |
1479 | kmem_cache_destroy(pwq_cache); |
1480 | kmem_cache_destroy(epi_cache); | |
1481 | } | |
1482 | ||
1483 | module_init(eventpoll_init); | |
1484 | module_exit(eventpoll_exit); | |
1485 | ||
1486 | MODULE_LICENSE("GPL"); |