2 * fs/eventpoll.c (Efficent event polling implementation)
3 * Copyright (C) 2001,...,2007 Davide Libenzi
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.
10 * Davide Libenzi <davidel@xmailserver.org>
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/sched.h>
18 #include <linux/file.h>
19 #include <linux/signal.h>
20 #include <linux/errno.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/rbtree.h>
30 #include <linux/wait.h>
31 #include <linux/eventpoll.h>
32 #include <linux/mount.h>
33 #include <linux/bitops.h>
34 #include <linux/mutex.h>
35 #include <linux/anon_inodes.h>
36 #include <asm/uaccess.h>
37 #include <asm/system.h>
40 #include <asm/atomic.h>
44 * There are three level of locking required by epoll :
48 * 3) ep->lock (spinlock)
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
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()
62 * This mutex is acquired by ep_free() during the epoll file
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).
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.
69 * Events that require holding "epmutex" are very rare, while for
70 * normal operations the epoll private "ep->mtx" will guarantee
71 * a better scalability.
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 */
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 */
92 /* Epoll private bits inside the event mask */
93 #define EP_PRIVATE_BITS (EPOLLONESHOT | EPOLLET)
95 /* Maximum number of poll wake up nests we are allowing */
96 #define EP_MAX_POLLWAKE_NESTS 4
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)
101 #define EP_MAX_EVENTS (INT_MAX / sizeof(struct epoll_event))
103 #define EP_UNACTIVE_PTR ((void *) -1L)
105 struct epoll_filefd
{
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.
117 struct wake_task_node
{
118 struct list_head llink
;
119 struct task_struct
*task
;
120 wait_queue_head_t
*wq
;
124 * This is used to implement the safe poll wake up avoiding to reenter
125 * the poll callback from inside wake_up().
127 struct poll_safewake
{
128 struct list_head wake_task_list
;
133 * Each file descriptor added to the eventpoll interface will
134 * have an entry of this type linked to the "rbr" RB tree.
137 /* RB tree node used to link this structure to the eventpoll RB tree */
140 /* List header used to link this structure to the eventpoll ready list */
141 struct list_head rdllink
;
144 * Works together "struct eventpoll"->ovflist in keeping the
145 * single linked chain of items.
149 /* The file descriptor information this item refers to */
150 struct epoll_filefd ffd
;
152 /* Number of active wait queue attached to poll operations */
155 /* List containing poll wait queues */
156 struct list_head pwqlist
;
158 /* The "container" of this item */
159 struct eventpoll
*ep
;
161 /* List header used to link this item to the "struct file" items list */
162 struct list_head fllink
;
164 /* The structure that describe the interested events and the source fd */
165 struct epoll_event event
;
169 * This structure is stored inside the "private_data" member of the file
170 * structure and rapresent the main data sructure for the eventpoll
174 /* Protect the this structure access */
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.
185 /* Wait queue used by sys_epoll_wait() */
186 wait_queue_head_t wq
;
188 /* Wait queue used by file->poll() */
189 wait_queue_head_t poll_wait
;
191 /* List of ready file descriptors */
192 struct list_head rdllist
;
194 /* RB tree root used to store monitored fd structs */
198 * This is a single linked list that chains all the "struct epitem" that
199 * happened while transfering ready events to userspace w/out
202 struct epitem
*ovflist
;
205 /* Wait structure used by the poll hooks */
206 struct eppoll_entry
{
207 /* List header used to link this structure to the "struct epitem" */
208 struct list_head llink
;
210 /* The "base" pointer is set to the container "struct epitem" */
214 * Wait queue item that will be linked to the target file wait
219 /* The wait queue head that linked the "wait" wait queue item */
220 wait_queue_head_t
*whead
;
223 /* Wrapper struct used by poll queueing */
230 * This mutex is used to serialize ep_free() and eventpoll_release_file().
232 static struct mutex epmutex
;
234 /* Safe wake up implementation */
235 static struct poll_safewake psw
;
237 /* Slab cache used to allocate "struct epitem" */
238 static struct kmem_cache
*epi_cache __read_mostly
;
240 /* Slab cache used to allocate "struct eppoll_entry" */
241 static struct kmem_cache
*pwq_cache __read_mostly
;
244 /* Setup the structure that is used as key for the RB tree */
245 static inline void ep_set_ffd(struct epoll_filefd
*ffd
,
246 struct file
*file
, int fd
)
252 /* Compare RB tree keys */
253 static inline int ep_cmp_ffd(struct epoll_filefd
*p1
,
254 struct epoll_filefd
*p2
)
256 return (p1
->file
> p2
->file
? +1:
257 (p1
->file
< p2
->file
? -1 : p1
->fd
- p2
->fd
));
260 /* Special initialization for the RB tree node to detect linkage */
261 static inline void ep_rb_initnode(struct rb_node
*n
)
266 /* Removes a node from the RB tree and marks it for a fast is-linked check */
267 static inline void ep_rb_erase(struct rb_node
*n
, struct rb_root
*r
)
273 /* Fast check to verify that the item is linked to the main RB tree */
274 static inline int ep_rb_linked(struct rb_node
*n
)
276 return rb_parent(n
) != n
;
279 /* Tells us if the item is currently linked */
280 static inline int ep_is_linked(struct list_head
*p
)
282 return !list_empty(p
);
285 /* Get the "struct epitem" from a wait queue pointer */
286 static inline struct epitem
* ep_item_from_wait(wait_queue_t
*p
)
288 return container_of(p
, struct eppoll_entry
, wait
)->base
;
291 /* Get the "struct epitem" from an epoll queue wrapper */
292 static inline struct epitem
* ep_item_from_epqueue(poll_table
*p
)
294 return container_of(p
, struct ep_pqueue
, pt
)->epi
;
297 /* Tells if the epoll_ctl(2) operation needs an event copy from userspace */
298 static inline int ep_op_has_event(int op
)
300 return op
!= EPOLL_CTL_DEL
;
303 /* Initialize the poll safe wake up structure */
304 static void ep_poll_safewake_init(struct poll_safewake
*psw
)
307 INIT_LIST_HEAD(&psw
->wake_task_list
);
308 spin_lock_init(&psw
->lock
);
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.
323 static void ep_poll_safewake(struct poll_safewake
*psw
, wait_queue_head_t
*wq
)
327 struct task_struct
*this_task
= current
;
328 struct list_head
*lsthead
= &psw
->wake_task_list
, *lnk
;
329 struct wake_task_node
*tncur
;
330 struct wake_task_node tnode
;
332 spin_lock_irqsave(&psw
->lock
, flags
);
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
);
338 if (tncur
->wq
== wq
||
339 (tncur
->task
== this_task
&& ++wake_nests
> EP_MAX_POLLWAKE_NESTS
)) {
341 * Ops ... loop detected or maximum nest level reached.
342 * We abort this wake by breaking the cycle itself.
344 spin_unlock_irqrestore(&psw
->lock
, flags
);
349 /* Add the current task to the list */
350 tnode
.task
= this_task
;
352 list_add(&tnode
.llink
, lsthead
);
354 spin_unlock_irqrestore(&psw
->lock
, flags
);
356 /* Do really wake up now */
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
);
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.
370 static void ep_unregister_pollwait(struct eventpoll
*ep
, struct epitem
*epi
)
373 struct list_head
*lsthead
= &epi
->pwqlist
;
374 struct eppoll_entry
*pwq
;
376 /* This is called without locks, so we need the atomic exchange */
377 nwait
= xchg(&epi
->nwait
, 0);
380 while (!list_empty(lsthead
)) {
381 pwq
= list_first_entry(lsthead
, struct eppoll_entry
, llink
);
383 list_del_init(&pwq
->llink
);
384 remove_wait_queue(pwq
->whead
, &pwq
->wait
);
385 kmem_cache_free(pwq_cache
, pwq
);
391 * Removes a "struct epitem" from the eventpoll RB tree and deallocates
392 * all the associated resources. Must be called with "mtx" held.
394 static int ep_remove(struct eventpoll
*ep
, struct epitem
*epi
)
397 struct file
*file
= epi
->ffd
.file
;
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".
407 ep_unregister_pollwait(ep
, epi
);
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
);
415 if (ep_rb_linked(&epi
->rbn
))
416 ep_rb_erase(&epi
->rbn
, &ep
->rbr
);
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
);
423 /* At this point it is safe to free the eventpoll item */
424 kmem_cache_free(epi_cache
, epi
);
426 DNPRINTK(3, (KERN_INFO
"[%p] eventpoll: ep_remove(%p, %p)\n",
432 static void ep_free(struct eventpoll
*ep
)
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
);
442 * We need to lock this because we could be hit by
443 * eventpoll_release_file() while we're freeing the "struct eventpoll".
444 * We do not need to hold "ep->mtx" here because the epoll file
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.
449 mutex_lock(&epmutex
);
452 * Walks through the whole tree by unregistering poll callbacks.
454 for (rbp
= rb_first(&ep
->rbr
); rbp
; rbp
= rb_next(rbp
)) {
455 epi
= rb_entry(rbp
, struct epitem
, rbn
);
457 ep_unregister_pollwait(ep
, epi
);
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
463 * holding "epmutex" we can be sure that no file cleanup code will hit
464 * us during this operation. So we can avoid the lock on "ep->lock".
466 while ((rbp
= rb_first(&ep
->rbr
)) != 0) {
467 epi
= rb_entry(rbp
, struct epitem
, rbn
);
471 mutex_unlock(&epmutex
);
472 mutex_destroy(&ep
->mtx
);
476 static int ep_eventpoll_release(struct inode
*inode
, struct file
*file
)
478 struct eventpoll
*ep
= file
->private_data
;
483 DNPRINTK(3, (KERN_INFO
"[%p] eventpoll: close() ep=%p\n", current
, ep
));
487 static unsigned int ep_eventpoll_poll(struct file
*file
, poll_table
*wait
)
489 unsigned int pollflags
= 0;
491 struct eventpoll
*ep
= file
->private_data
;
493 /* Insert inside our poll wait queue */
494 poll_wait(file
, &ep
->poll_wait
, wait
);
496 /* Check our condition */
497 spin_lock_irqsave(&ep
->lock
, flags
);
498 if (!list_empty(&ep
->rdllist
))
499 pollflags
= POLLIN
| POLLRDNORM
;
500 spin_unlock_irqrestore(&ep
->lock
, flags
);
505 /* File callbacks that implement the eventpoll file behaviour */
506 static const struct file_operations eventpoll_fops
= {
507 .release
= ep_eventpoll_release
,
508 .poll
= ep_eventpoll_poll
511 /* Fast test to see if the file is an evenpoll file */
512 static inline int is_file_epoll(struct file
*f
)
514 return f
->f_op
== &eventpoll_fops
;
518 * This is called from eventpoll_release() to unlink files from the eventpoll
519 * interface. We need to have this facility to cleanup correctly files that are
520 * closed without being removed from the eventpoll interface.
522 void eventpoll_release_file(struct file
*file
)
524 struct list_head
*lsthead
= &file
->f_ep_links
;
525 struct eventpoll
*ep
;
529 * We don't want to get "file->f_ep_lock" because it is not
530 * necessary. It is not necessary because we're in the "struct file"
531 * cleanup path, and this means that noone is using this file anymore.
532 * So, for example, epoll_ctl() cannot hit here sicne if we reach this
533 * point, the file counter already went to zero and fget() would fail.
534 * The only hit might come from ep_free() but by holding the mutex
535 * will correctly serialize the operation. We do need to acquire
536 * "ep->mtx" after "epmutex" because ep_remove() requires it when called
537 * from anywhere but ep_free().
539 mutex_lock(&epmutex
);
541 while (!list_empty(lsthead
)) {
542 epi
= list_first_entry(lsthead
, struct epitem
, fllink
);
545 list_del_init(&epi
->fllink
);
546 mutex_lock(&ep
->mtx
);
548 mutex_unlock(&ep
->mtx
);
551 mutex_unlock(&epmutex
);
554 static int ep_alloc(struct eventpoll
**pep
)
556 struct eventpoll
*ep
= kzalloc(sizeof(*ep
), GFP_KERNEL
);
561 spin_lock_init(&ep
->lock
);
562 mutex_init(&ep
->mtx
);
563 init_waitqueue_head(&ep
->wq
);
564 init_waitqueue_head(&ep
->poll_wait
);
565 INIT_LIST_HEAD(&ep
->rdllist
);
567 ep
->ovflist
= EP_UNACTIVE_PTR
;
571 DNPRINTK(3, (KERN_INFO
"[%p] eventpoll: ep_alloc() ep=%p\n",
577 * Search the file inside the eventpoll tree. The RB tree operations
578 * are protected by the "mtx" mutex, and ep_find() must be called with
581 static struct epitem
*ep_find(struct eventpoll
*ep
, struct file
*file
, int fd
)
585 struct epitem
*epi
, *epir
= NULL
;
586 struct epoll_filefd ffd
;
588 ep_set_ffd(&ffd
, file
, fd
);
589 for (rbp
= ep
->rbr
.rb_node
; rbp
; ) {
590 epi
= rb_entry(rbp
, struct epitem
, rbn
);
591 kcmp
= ep_cmp_ffd(&ffd
, &epi
->ffd
);
602 DNPRINTK(3, (KERN_INFO
"[%p] eventpoll: ep_find(%p) -> %p\n",
603 current
, file
, epir
));
609 * This is the callback that is passed to the wait queue wakeup
610 * machanism. It is called by the stored file descriptors when they
611 * have events to report.
613 static int ep_poll_callback(wait_queue_t
*wait
, unsigned mode
, int sync
, void *key
)
617 struct epitem
*epi
= ep_item_from_wait(wait
);
618 struct eventpoll
*ep
= epi
->ep
;
620 DNPRINTK(3, (KERN_INFO
"[%p] eventpoll: poll_callback(%p) epi=%p ep=%p\n",
621 current
, epi
->ffd
.file
, epi
, ep
));
623 spin_lock_irqsave(&ep
->lock
, flags
);
626 * If the event mask does not contain any poll(2) event, we consider the
627 * descriptor to be disabled. This condition is likely the effect of the
628 * EPOLLONESHOT bit that disables the descriptor when an event is received,
629 * until the next EPOLL_CTL_MOD will be issued.
631 if (!(epi
->event
.events
& ~EP_PRIVATE_BITS
))
635 * If we are trasfering events to userspace, we can hold no locks
636 * (because we're accessing user memory, and because of linux f_op->poll()
637 * semantics). All the events that happens during that period of time are
638 * chained in ep->ovflist and requeued later on.
640 if (unlikely(ep
->ovflist
!= EP_UNACTIVE_PTR
)) {
641 if (epi
->next
== EP_UNACTIVE_PTR
) {
642 epi
->next
= ep
->ovflist
;
648 /* If this file is already in the ready list we exit soon */
649 if (ep_is_linked(&epi
->rdllink
))
652 list_add_tail(&epi
->rdllink
, &ep
->rdllist
);
656 * Wake up ( if active ) both the eventpoll wait list and the ->poll()
659 if (waitqueue_active(&ep
->wq
))
660 __wake_up_locked(&ep
->wq
, TASK_UNINTERRUPTIBLE
|
662 if (waitqueue_active(&ep
->poll_wait
))
666 spin_unlock_irqrestore(&ep
->lock
, flags
);
668 /* We have to call this outside the lock */
670 ep_poll_safewake(&psw
, &ep
->poll_wait
);
676 * This is the callback that is used to add our wait queue to the
677 * target file wakeup lists.
679 static void ep_ptable_queue_proc(struct file
*file
, wait_queue_head_t
*whead
,
682 struct epitem
*epi
= ep_item_from_epqueue(pt
);
683 struct eppoll_entry
*pwq
;
685 if (epi
->nwait
>= 0 && (pwq
= kmem_cache_alloc(pwq_cache
, GFP_KERNEL
))) {
686 init_waitqueue_func_entry(&pwq
->wait
, ep_poll_callback
);
689 add_wait_queue(whead
, &pwq
->wait
);
690 list_add_tail(&pwq
->llink
, &epi
->pwqlist
);
693 /* We have to signal that an error occurred */
698 static void ep_rbtree_insert(struct eventpoll
*ep
, struct epitem
*epi
)
701 struct rb_node
**p
= &ep
->rbr
.rb_node
, *parent
= NULL
;
706 epic
= rb_entry(parent
, struct epitem
, rbn
);
707 kcmp
= ep_cmp_ffd(&epi
->ffd
, &epic
->ffd
);
709 p
= &parent
->rb_right
;
711 p
= &parent
->rb_left
;
713 rb_link_node(&epi
->rbn
, parent
, p
);
714 rb_insert_color(&epi
->rbn
, &ep
->rbr
);
718 * Must be called with "mtx" held.
720 static int ep_insert(struct eventpoll
*ep
, struct epoll_event
*event
,
721 struct file
*tfile
, int fd
)
723 int error
, revents
, pwake
= 0;
726 struct ep_pqueue epq
;
729 if (!(epi
= kmem_cache_alloc(epi_cache
, GFP_KERNEL
)))
732 /* Item initialization follow here ... */
733 ep_rb_initnode(&epi
->rbn
);
734 INIT_LIST_HEAD(&epi
->rdllink
);
735 INIT_LIST_HEAD(&epi
->fllink
);
736 INIT_LIST_HEAD(&epi
->pwqlist
);
738 ep_set_ffd(&epi
->ffd
, tfile
, fd
);
741 epi
->next
= EP_UNACTIVE_PTR
;
743 /* Initialize the poll table using the queue callback */
745 init_poll_funcptr(&epq
.pt
, ep_ptable_queue_proc
);
748 * Attach the item to the poll hooks and get current event bits.
749 * We can safely use the file* here because its usage count has
750 * been increased by the caller of this function. Note that after
751 * this operation completes, the poll callback can start hitting
754 revents
= tfile
->f_op
->poll(tfile
, &epq
.pt
);
757 * We have to check if something went wrong during the poll wait queue
758 * install process. Namely an allocation for a wait queue failed due
759 * high memory pressure.
762 goto error_unregister
;
764 /* Add the current item to the list of active epoll hook for this file */
765 spin_lock(&tfile
->f_ep_lock
);
766 list_add_tail(&epi
->fllink
, &tfile
->f_ep_links
);
767 spin_unlock(&tfile
->f_ep_lock
);
770 * Add the current item to the RB tree. All RB tree operations are
771 * protected by "mtx", and ep_insert() is called with "mtx" held.
773 ep_rbtree_insert(ep
, epi
);
775 /* We have to drop the new item inside our item list to keep track of it */
776 spin_lock_irqsave(&ep
->lock
, flags
);
778 /* If the file is already "ready" we drop it inside the ready list */
779 if ((revents
& event
->events
) && !ep_is_linked(&epi
->rdllink
)) {
780 list_add_tail(&epi
->rdllink
, &ep
->rdllist
);
782 /* Notify waiting tasks that events are available */
783 if (waitqueue_active(&ep
->wq
))
784 __wake_up_locked(&ep
->wq
, TASK_UNINTERRUPTIBLE
| TASK_INTERRUPTIBLE
);
785 if (waitqueue_active(&ep
->poll_wait
))
789 spin_unlock_irqrestore(&ep
->lock
, flags
);
791 /* We have to call this outside the lock */
793 ep_poll_safewake(&psw
, &ep
->poll_wait
);
795 DNPRINTK(3, (KERN_INFO
"[%p] eventpoll: ep_insert(%p, %p, %d)\n",
796 current
, ep
, tfile
, fd
));
801 ep_unregister_pollwait(ep
, epi
);
804 * We need to do this because an event could have been arrived on some
805 * allocated wait queue. Note that we don't care about the ep->ovflist
806 * list, since that is used/cleaned only inside a section bound by "mtx".
807 * And ep_insert() is called with "mtx" held.
809 spin_lock_irqsave(&ep
->lock
, flags
);
810 if (ep_is_linked(&epi
->rdllink
))
811 list_del_init(&epi
->rdllink
);
812 spin_unlock_irqrestore(&ep
->lock
, flags
);
814 kmem_cache_free(epi_cache
, epi
);
820 * Modify the interest event mask by dropping an event if the new mask
821 * has a match in the current file status. Must be called with "mtx" held.
823 static int ep_modify(struct eventpoll
*ep
, struct epitem
*epi
, struct epoll_event
*event
)
826 unsigned int revents
;
830 * Set the new event interest mask before calling f_op->poll(), otherwise
831 * a potential race might occur. In fact if we do this operation inside
832 * the lock, an event might happen between the f_op->poll() call and the
833 * new event set registering.
835 epi
->event
.events
= event
->events
;
838 * Get current event bits. We can safely use the file* here because
839 * its usage count has been increased by the caller of this function.
841 revents
= epi
->ffd
.file
->f_op
->poll(epi
->ffd
.file
, NULL
);
843 spin_lock_irqsave(&ep
->lock
, flags
);
845 /* Copy the data member from inside the lock */
846 epi
->event
.data
= event
->data
;
849 * If the item is "hot" and it is not registered inside the ready
850 * list, push it inside.
852 if (revents
& event
->events
) {
853 if (!ep_is_linked(&epi
->rdllink
)) {
854 list_add_tail(&epi
->rdllink
, &ep
->rdllist
);
856 /* Notify waiting tasks that events are available */
857 if (waitqueue_active(&ep
->wq
))
858 __wake_up_locked(&ep
->wq
, TASK_UNINTERRUPTIBLE
|
860 if (waitqueue_active(&ep
->poll_wait
))
864 spin_unlock_irqrestore(&ep
->lock
, flags
);
866 /* We have to call this outside the lock */
868 ep_poll_safewake(&psw
, &ep
->poll_wait
);
873 static int ep_send_events(struct eventpoll
*ep
, struct epoll_event __user
*events
,
876 int eventcnt
, error
= -EFAULT
, pwake
= 0;
877 unsigned int revents
;
879 struct epitem
*epi
, *nepi
;
880 struct list_head txlist
;
882 INIT_LIST_HEAD(&txlist
);
885 * We need to lock this because we could be hit by
886 * eventpoll_release_file() and epoll_ctl(EPOLL_CTL_DEL).
888 mutex_lock(&ep
->mtx
);
891 * Steal the ready list, and re-init the original one to the
892 * empty list. Also, set ep->ovflist to NULL so that events
893 * happening while looping w/out locks, are not lost. We cannot
894 * have the poll callback to queue directly on ep->rdllist,
895 * because we are doing it in the loop below, in a lockless way.
897 spin_lock_irqsave(&ep
->lock
, flags
);
898 list_splice(&ep
->rdllist
, &txlist
);
899 INIT_LIST_HEAD(&ep
->rdllist
);
901 spin_unlock_irqrestore(&ep
->lock
, flags
);
904 * We can loop without lock because this is a task private list.
905 * We just splice'd out the ep->rdllist in ep_collect_ready_items().
906 * Items cannot vanish during the loop because we are holding "mtx".
908 for (eventcnt
= 0; !list_empty(&txlist
) && eventcnt
< maxevents
;) {
909 epi
= list_first_entry(&txlist
, struct epitem
, rdllink
);
911 list_del_init(&epi
->rdllink
);
914 * Get the ready file event set. We can safely use the file
915 * because we are holding the "mtx" and this will guarantee
916 * that both the file and the item will not vanish.
918 revents
= epi
->ffd
.file
->f_op
->poll(epi
->ffd
.file
, NULL
);
919 revents
&= epi
->event
.events
;
922 * Is the event mask intersect the caller-requested one,
923 * deliver the event to userspace. Again, we are holding
924 * "mtx", so no operations coming from userspace can change
928 if (__put_user(revents
,
929 &events
[eventcnt
].events
) ||
930 __put_user(epi
->event
.data
,
931 &events
[eventcnt
].data
))
933 if (epi
->event
.events
& EPOLLONESHOT
)
934 epi
->event
.events
&= EP_PRIVATE_BITS
;
938 * At this point, noone can insert into ep->rdllist besides
939 * us. The epoll_ctl() callers are locked out by us holding
940 * "mtx" and the poll callback will queue them in ep->ovflist.
942 if (!(epi
->event
.events
& EPOLLET
) &&
943 (revents
& epi
->event
.events
))
944 list_add_tail(&epi
->rdllink
, &ep
->rdllist
);
950 spin_lock_irqsave(&ep
->lock
, flags
);
952 * During the time we spent in the loop above, some other events
953 * might have been queued by the poll callback. We re-insert them
954 * here (in case they are not already queued, or they're one-shot).
956 for (nepi
= ep
->ovflist
; (epi
= nepi
) != NULL
;
957 nepi
= epi
->next
, epi
->next
= EP_UNACTIVE_PTR
) {
958 if (!ep_is_linked(&epi
->rdllink
) &&
959 (epi
->event
.events
& ~EP_PRIVATE_BITS
))
960 list_add_tail(&epi
->rdllink
, &ep
->rdllist
);
963 * We need to set back ep->ovflist to EP_UNACTIVE_PTR, so that after
964 * releasing the lock, events will be queued in the normal way inside
967 ep
->ovflist
= EP_UNACTIVE_PTR
;
970 * In case of error in the event-send loop, or in case the number of
971 * ready events exceeds the userspace limit, we need to splice the
972 * "txlist" back inside ep->rdllist.
974 list_splice(&txlist
, &ep
->rdllist
);
976 if (!list_empty(&ep
->rdllist
)) {
978 * Wake up (if active) both the eventpoll wait list and the ->poll()
979 * wait list (delayed after we release the lock).
981 if (waitqueue_active(&ep
->wq
))
982 __wake_up_locked(&ep
->wq
, TASK_UNINTERRUPTIBLE
|
984 if (waitqueue_active(&ep
->poll_wait
))
987 spin_unlock_irqrestore(&ep
->lock
, flags
);
989 mutex_unlock(&ep
->mtx
);
991 /* We have to call this outside the lock */
993 ep_poll_safewake(&psw
, &ep
->poll_wait
);
995 return eventcnt
== 0 ? error
: eventcnt
;
998 static int ep_poll(struct eventpoll
*ep
, struct epoll_event __user
*events
,
999 int maxevents
, long timeout
)
1002 unsigned long flags
;
1007 * Calculate the timeout by checking for the "infinite" value ( -1 )
1008 * and the overflow condition. The passed timeout is in milliseconds,
1009 * that why (t * HZ) / 1000.
1011 jtimeout
= (timeout
< 0 || timeout
>= EP_MAX_MSTIMEO
) ?
1012 MAX_SCHEDULE_TIMEOUT
: (timeout
* HZ
+ 999) / 1000;
1015 spin_lock_irqsave(&ep
->lock
, flags
);
1018 if (list_empty(&ep
->rdllist
)) {
1020 * We don't have any available event to return to the caller.
1021 * We need to sleep here, and we will be wake up by
1022 * ep_poll_callback() when events will become available.
1024 init_waitqueue_entry(&wait
, current
);
1025 wait
.flags
|= WQ_FLAG_EXCLUSIVE
;
1026 __add_wait_queue(&ep
->wq
, &wait
);
1030 * We don't want to sleep if the ep_poll_callback() sends us
1031 * a wakeup in between. That's why we set the task state
1032 * to TASK_INTERRUPTIBLE before doing the checks.
1034 set_current_state(TASK_INTERRUPTIBLE
);
1035 if (!list_empty(&ep
->rdllist
) || !jtimeout
)
1037 if (signal_pending(current
)) {
1042 spin_unlock_irqrestore(&ep
->lock
, flags
);
1043 jtimeout
= schedule_timeout(jtimeout
);
1044 spin_lock_irqsave(&ep
->lock
, flags
);
1046 __remove_wait_queue(&ep
->wq
, &wait
);
1048 set_current_state(TASK_RUNNING
);
1051 /* Is it worth to try to dig for events ? */
1052 eavail
= !list_empty(&ep
->rdllist
);
1054 spin_unlock_irqrestore(&ep
->lock
, flags
);
1057 * Try to transfer events to user space. In case we get 0 events and
1058 * there's still timeout left over, we go trying again in search of
1061 if (!res
&& eavail
&&
1062 !(res
= ep_send_events(ep
, events
, maxevents
)) && jtimeout
)
1069 * It opens an eventpoll file descriptor. The "size" parameter is there
1070 * for historical reasons, when epoll was using an hash instead of an
1071 * RB tree. With the current implementation, the "size" parameter is ignored
1072 * (besides sanity checks).
1074 asmlinkage
long sys_epoll_create(int size
)
1077 struct eventpoll
*ep
;
1078 struct inode
*inode
;
1081 DNPRINTK(3, (KERN_INFO
"[%p] eventpoll: sys_epoll_create(%d)\n",
1085 * Sanity check on the size parameter, and create the internal data
1086 * structure ( "struct eventpoll" ).
1089 if (size
<= 0 || (error
= ep_alloc(&ep
)) != 0)
1093 * Creates all the items needed to setup an eventpoll file. That is,
1094 * a file structure, and inode and a free file descriptor.
1096 error
= anon_inode_getfd(&fd
, &inode
, &file
, "[eventpoll]",
1097 &eventpoll_fops
, ep
);
1101 DNPRINTK(3, (KERN_INFO
"[%p] eventpoll: sys_epoll_create(%d) = %d\n",
1102 current
, size
, fd
));
1109 DNPRINTK(3, (KERN_INFO
"[%p] eventpoll: sys_epoll_create(%d) = %d\n",
1110 current
, size
, error
));
1115 * The following function implements the controller interface for
1116 * the eventpoll file that enables the insertion/removal/change of
1117 * file descriptors inside the interest set.
1119 asmlinkage
long sys_epoll_ctl(int epfd
, int op
, int fd
,
1120 struct epoll_event __user
*event
)
1123 struct file
*file
, *tfile
;
1124 struct eventpoll
*ep
;
1126 struct epoll_event epds
;
1128 DNPRINTK(3, (KERN_INFO
"[%p] eventpoll: sys_epoll_ctl(%d, %d, %d, %p)\n",
1129 current
, epfd
, op
, fd
, event
));
1132 if (ep_op_has_event(op
) &&
1133 copy_from_user(&epds
, event
, sizeof(struct epoll_event
)))
1136 /* Get the "struct file *" for the eventpoll file */
1142 /* Get the "struct file *" for the target file */
1147 /* The target file descriptor must support poll */
1149 if (!tfile
->f_op
|| !tfile
->f_op
->poll
)
1150 goto error_tgt_fput
;
1153 * We have to check that the file structure underneath the file descriptor
1154 * the user passed to us _is_ an eventpoll file. And also we do not permit
1155 * adding an epoll file descriptor inside itself.
1158 if (file
== tfile
|| !is_file_epoll(file
))
1159 goto error_tgt_fput
;
1162 * At this point it is safe to assume that the "private_data" contains
1163 * our own data structure.
1165 ep
= file
->private_data
;
1167 mutex_lock(&ep
->mtx
);
1170 * Try to lookup the file inside our RB tree, Since we grabbed "mtx"
1171 * above, we can be sure to be able to use the item looked up by
1172 * ep_find() till we release the mutex.
1174 epi
= ep_find(ep
, tfile
, fd
);
1180 epds
.events
|= POLLERR
| POLLHUP
;
1182 error
= ep_insert(ep
, &epds
, tfile
, fd
);
1188 error
= ep_remove(ep
, epi
);
1194 epds
.events
|= POLLERR
| POLLHUP
;
1195 error
= ep_modify(ep
, epi
, &epds
);
1200 mutex_unlock(&ep
->mtx
);
1207 DNPRINTK(3, (KERN_INFO
"[%p] eventpoll: sys_epoll_ctl(%d, %d, %d, %p) = %d\n",
1208 current
, epfd
, op
, fd
, event
, error
));
1214 * Implement the event wait interface for the eventpoll file. It is the kernel
1215 * part of the user space epoll_wait(2).
1217 asmlinkage
long sys_epoll_wait(int epfd
, struct epoll_event __user
*events
,
1218 int maxevents
, int timeout
)
1222 struct eventpoll
*ep
;
1224 DNPRINTK(3, (KERN_INFO
"[%p] eventpoll: sys_epoll_wait(%d, %p, %d, %d)\n",
1225 current
, epfd
, events
, maxevents
, timeout
));
1227 /* The maximum number of event must be greater than zero */
1228 if (maxevents
<= 0 || maxevents
> EP_MAX_EVENTS
)
1231 /* Verify that the area passed by the user is writeable */
1232 if (!access_ok(VERIFY_WRITE
, events
, maxevents
* sizeof(struct epoll_event
))) {
1237 /* Get the "struct file *" for the eventpoll file */
1244 * We have to check that the file structure underneath the fd
1245 * the user passed to us _is_ an eventpoll file.
1248 if (!is_file_epoll(file
))
1252 * At this point it is safe to assume that the "private_data" contains
1253 * our own data structure.
1255 ep
= file
->private_data
;
1257 /* Time to fish for events ... */
1258 error
= ep_poll(ep
, events
, maxevents
, timeout
);
1263 DNPRINTK(3, (KERN_INFO
"[%p] eventpoll: sys_epoll_wait(%d, %p, %d, %d) = %d\n",
1264 current
, epfd
, events
, maxevents
, timeout
, error
));
1269 #ifdef TIF_RESTORE_SIGMASK
1272 * Implement the event wait interface for the eventpoll file. It is the kernel
1273 * part of the user space epoll_pwait(2).
1275 asmlinkage
long sys_epoll_pwait(int epfd
, struct epoll_event __user
*events
,
1276 int maxevents
, int timeout
, const sigset_t __user
*sigmask
,
1280 sigset_t ksigmask
, sigsaved
;
1283 * If the caller wants a certain signal mask to be set during the wait,
1287 if (sigsetsize
!= sizeof(sigset_t
))
1289 if (copy_from_user(&ksigmask
, sigmask
, sizeof(ksigmask
)))
1291 sigdelsetmask(&ksigmask
, sigmask(SIGKILL
) | sigmask(SIGSTOP
));
1292 sigprocmask(SIG_SETMASK
, &ksigmask
, &sigsaved
);
1295 error
= sys_epoll_wait(epfd
, events
, maxevents
, timeout
);
1298 * If we changed the signal mask, we need to restore the original one.
1299 * In case we've got a signal while waiting, we do not restore the
1300 * signal mask yet, and we allow do_signal() to deliver the signal on
1301 * the way back to userspace, before the signal mask is restored.
1304 if (error
== -EINTR
) {
1305 memcpy(¤t
->saved_sigmask
, &sigsaved
,
1307 set_thread_flag(TIF_RESTORE_SIGMASK
);
1309 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
1315 #endif /* #ifdef TIF_RESTORE_SIGMASK */
1317 static int __init
eventpoll_init(void)
1319 mutex_init(&epmutex
);
1321 /* Initialize the structure used to perform safe poll wait head wake ups */
1322 ep_poll_safewake_init(&psw
);
1324 /* Allocates slab cache used to allocate "struct epitem" items */
1325 epi_cache
= kmem_cache_create("eventpoll_epi", sizeof(struct epitem
),
1326 0, SLAB_HWCACHE_ALIGN
|EPI_SLAB_DEBUG
|SLAB_PANIC
,
1329 /* Allocates slab cache used to allocate "struct eppoll_entry" */
1330 pwq_cache
= kmem_cache_create("eventpoll_pwq",
1331 sizeof(struct eppoll_entry
), 0,
1332 EPI_SLAB_DEBUG
|SLAB_PANIC
, NULL
, NULL
);
1336 fs_initcall(eventpoll_init
);