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