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