Lines Matching +full:no +full:- +full:poll +full:- +full:on +full:- +full:init
1 // SPDX-License-Identifier: GPL-2.0-or-later
9 #include <linux/init.h>
18 #include <linux/poll.h>
47 * 2) ep->mtx (mutex)
48 * 3) ep->lock (rwlock)
51 * We need a rwlock (ep->lock) because we manipulate objects
52 * from inside the poll callback, that might be triggered from
54 * So we can't sleep inside the poll callback and hence we need
58 * mutex (ep->mtx). It is acquired during the event transfer loop,
67 * It is necessary to acquire multiple "ep->mtx"es at once in the
70 * epoll_ctl(e1, EPOLL_CTL_ADD, e2), e1->mtx will always be acquired
71 * before e2->mtx). Since we disallow cycles of epoll file
72 * descriptors, this ensures that the mutexes are well-ordered. In
76 * It is possible to drop the "ep->mtx" and to use the global
77 * mutex "epnested_mutex" (together with "ep->lock") to have it working,
78 * but having "ep->mtx" will make the interface more scalable.
80 * normal operations the epoll private "ep->mtx" will guarantee
97 #define EP_UNACTIVE_PTR ((void *) -1L)
106 /* Wait structure used by the poll hooks */
128 * of these on a server and we do not want this to take another cache line.
142 * Works together "struct eventpoll"->ovflist in keeping the
151 * Protected by file->f_lock, true for to-be-released epitem already
153 * eventpoll->refcount orchestrates "struct eventpoll" disposal
157 /* List containing poll wait queues */
190 /* Wait queue used by file->poll() */
205 * holding ->lock.
222 * usage count, used together with epitem->dying to
228 /* used to track busy poll napi_id */
238 /* Wrapper struct used by poll queueing */
286 head = container_of(file->f_ep, struct epitems_head, epitems); in list_file()
287 if (!head->next) { in list_file()
288 head->next = tfile_check_list; in list_file()
296 struct hlist_node *p = rcu_dereference(hlist_first_rcu(&head->epitems)); in unlist_file()
299 spin_lock(&epi->ffd.file->f_lock); in unlist_file()
300 if (!hlist_empty(&head->epitems)) in unlist_file()
302 head->next = NULL; in unlist_file()
303 spin_unlock(&epi->ffd.file->f_lock); in unlist_file()
339 return f->f_op == &eventpoll_fops; in is_file_epoll()
346 ffd->file = file; in ep_set_ffd()
347 ffd->fd = fd; in ep_set_ffd()
354 return (p1->file > p2->file ? +1: in ep_cmp_ffd()
355 (p1->file < p2->file ? -1 : p1->fd - p2->fd)); in ep_cmp_ffd()
361 return !list_empty(&epi->rdllink); in ep_is_linked()
372 return container_of(p, struct eppoll_entry, wait)->base; in ep_item_from_wait()
376 * ep_events_available - Checks if ready events might be available.
385 return !list_empty_careful(&ep->rdllist) || in ep_events_available()
386 READ_ONCE(ep->ovflist) != EP_UNACTIVE_PTR; in ep_events_available()
398 * Busy poll if globally on and supporting sockets found && no events,
405 unsigned int napi_id = READ_ONCE(ep->napi_id); in ep_busy_loop()
413 * Busy poll timed out. Drop NAPI ID for now, we can add in ep_busy_loop()
417 ep->napi_id = 0; in ep_busy_loop()
424 * Set epoll busy poll NAPI ID from sk.
436 sock = sock_from_file(epi->ffd.file); in ep_set_busy_poll_napi_id()
440 sk = sock->sk; in ep_set_busy_poll_napi_id()
444 napi_id = READ_ONCE(sk->sk_napi_id); in ep_set_busy_poll_napi_id()
445 ep = epi->ep; in ep_set_busy_poll_napi_id()
447 /* Non-NAPI IDs can be rejected in ep_set_busy_poll_napi_id()
451 if (napi_id < MIN_NAPI_ID || napi_id == ep->napi_id) in ep_set_busy_poll_napi_id()
454 /* record NAPI ID for use in next busy poll */ in ep_set_busy_poll_napi_id()
455 ep->napi_id = napi_id; in ep_set_busy_poll_napi_id()
484 * issue a wake_up() on its poll wake list. Epoll (efd1) has installed a
485 * callback wakeup entry on that queue, and the wake_up() performed by the
488 * the waiters on its poll wait list (efd2). So it calls ep_poll_safewake()
490 * recursion constraints. That are, no more than EP_MAX_NESTS, to avoid
507 * it might be natural to create a per-cpu nest count. However, since in ep_poll_safewake()
508 * we can recurse on ep->poll_wait.lock, and a non-raw spinlock can in ep_poll_safewake()
509 * schedule() in the -rt kernel, the per-cpu variable are no longer in ep_poll_safewake()
521 if ((is_file_epoll(epi->ffd.file))) { in ep_poll_safewake()
522 ep_src = epi->ffd.file->private_data; in ep_poll_safewake()
523 nests = ep_src->nests; in ep_poll_safewake()
528 spin_lock_irqsave_nested(&ep->poll_wait.lock, flags, nests); in ep_poll_safewake()
529 ep->nests = nests + 1; in ep_poll_safewake()
530 wake_up_locked_poll(&ep->poll_wait, EPOLLIN | pollflags); in ep_poll_safewake()
531 ep->nests = 0; in ep_poll_safewake()
532 spin_unlock_irqrestore(&ep->poll_wait.lock, flags); in ep_poll_safewake()
540 wake_up_poll(&ep->poll_wait, EPOLLIN | pollflags); in ep_poll_safewake()
551 * If it is cleared by POLLFREE, it should be rcu-safe. in ep_remove_wait_queue()
554 * we rely on whead->lock. in ep_remove_wait_queue()
556 whead = smp_load_acquire(&pwq->whead); in ep_remove_wait_queue()
558 remove_wait_queue(whead, &pwq->wait); in ep_remove_wait_queue()
563 * This function unregisters poll callbacks from the associated file
568 struct eppoll_entry **p = &epi->pwqlist; in ep_unregister_pollwait()
572 *p = pwq->next; in ep_unregister_pollwait()
578 /* call only when ep->mtx is held */
581 return rcu_dereference_check(epi->ws, lockdep_is_held(&epi->ep->mtx)); in ep_wakeup_source()
584 /* call only when ep->mtx is held */
595 return rcu_access_pointer(epi->ws) ? true : false; in ep_has_wakeup_source()
598 /* call when ep->mtx cannot be held (ep_poll_callback) */
604 ws = rcu_dereference(epi->ws); in ep_pm_stay_awake_rcu()
612 * ep->mutex needs to be held because we could be hit by
618 * Steal the ready list, and re-init the original one to the in ep_start_scan()
619 * empty list. Also, set ep->ovflist to NULL so that events in ep_start_scan()
621 * have the poll callback to queue directly on ep->rdllist, in ep_start_scan()
626 write_lock_irq(&ep->lock); in ep_start_scan()
627 list_splice_init(&ep->rdllist, txlist); in ep_start_scan()
628 WRITE_ONCE(ep->ovflist, NULL); in ep_start_scan()
629 write_unlock_irq(&ep->lock); in ep_start_scan()
637 write_lock_irq(&ep->lock); in ep_done_scan()
640 * other events might have been queued by the poll callback. in ep_done_scan()
641 * We re-insert them inside the main ready-list here. in ep_done_scan()
643 for (nepi = READ_ONCE(ep->ovflist); (epi = nepi) != NULL; in ep_done_scan()
644 nepi = epi->next, epi->next = EP_UNACTIVE_PTR) { in ep_done_scan()
648 * queued into ->ovflist but the "txlist" might already in ep_done_scan()
653 * ->ovflist is LIFO, so we have to reverse it in order in ep_done_scan()
656 list_add(&epi->rdllink, &ep->rdllist); in ep_done_scan()
661 * We need to set back ep->ovflist to EP_UNACTIVE_PTR, so that after in ep_done_scan()
663 * ep->rdllist. in ep_done_scan()
665 WRITE_ONCE(ep->ovflist, EP_UNACTIVE_PTR); in ep_done_scan()
668 * Quickly re-inject items left on "txlist". in ep_done_scan()
670 list_splice(txlist, &ep->rdllist); in ep_done_scan()
671 __pm_relax(ep->ws); in ep_done_scan()
673 if (!list_empty(&ep->rdllist)) { in ep_done_scan()
674 if (waitqueue_active(&ep->wq)) in ep_done_scan()
675 wake_up(&ep->wq); in ep_done_scan()
678 write_unlock_irq(&ep->lock); in ep_done_scan()
689 refcount_inc(&ep->refcount); in ep_get()
693 * Returns true if the event poll can be disposed
697 if (!refcount_dec_and_test(&ep->refcount)) in ep_refcount_dec_and_test()
700 WARN_ON_ONCE(!RB_EMPTY_ROOT(&ep->rbr.rb_root)); in ep_refcount_dec_and_test()
706 mutex_destroy(&ep->mtx); in ep_free()
707 free_uid(ep->user); in ep_free()
708 wakeup_source_unregister(ep->ws); in ep_free()
722 struct file *file = epi->ffd.file; in __ep_remove()
729 * Removes poll wait queue hooks. in __ep_remove()
734 spin_lock(&file->f_lock); in __ep_remove()
735 if (epi->dying && !force) { in __ep_remove()
736 spin_unlock(&file->f_lock); in __ep_remove()
741 head = file->f_ep; in __ep_remove()
742 if (head->first == &epi->fllink && !epi->fllink.next) { in __ep_remove()
743 file->f_ep = NULL; in __ep_remove()
747 if (!smp_load_acquire(&v->next)) in __ep_remove()
751 hlist_del_rcu(&epi->fllink); in __ep_remove()
752 spin_unlock(&file->f_lock); in __ep_remove()
755 rb_erase_cached(&epi->rbn, &ep->rbr); in __ep_remove()
757 write_lock_irq(&ep->lock); in __ep_remove()
759 list_del_init(&epi->rdllink); in __ep_remove()
760 write_unlock_irq(&ep->lock); in __ep_remove()
765 * field epi->rcu, since we are trying to minimize the size of in __ep_remove()
766 * 'struct epitem'. The 'rbn' field is no longer in use. Protected by in __ep_remove()
767 * ep->mtx. The rcu read side, reverse_path_check_proc(), does not make in __ep_remove()
770 call_rcu(&epi->rcu, epi_rcu_free); in __ep_remove()
772 percpu_counter_dec(&ep->user->epoll_watches); in __ep_remove()
791 if (waitqueue_active(&ep->poll_wait)) in ep_clear_and_put()
794 mutex_lock(&ep->mtx); in ep_clear_and_put()
797 * Walks through the whole tree by unregistering poll callbacks. in ep_clear_and_put()
799 for (rbp = rb_first_cached(&ep->rbr); rbp; rbp = rb_next(rbp)) { in ep_clear_and_put()
810 * At this point we are sure no poll callbacks will be lingering around. in ep_clear_and_put()
814 for (rbp = rb_first_cached(&ep->rbr); rbp; rbp = next) { in ep_clear_and_put()
822 mutex_unlock(&ep->mtx); in ep_clear_and_put()
830 struct eventpoll *ep = file->private_data; in ep_eventpoll_release()
842 struct eventpoll *ep = file->private_data; in __ep_eventpoll_poll()
850 /* Insert inside our poll wait queue */ in __ep_eventpoll_poll()
851 poll_wait(file, &ep->poll_wait, wait); in __ep_eventpoll_poll()
857 mutex_lock_nested(&ep->mtx, depth); in __ep_eventpoll_poll()
865 * Item has been dropped into the ready list by the poll in __ep_eventpoll_poll()
870 list_del_init(&epi->rdllink); in __ep_eventpoll_poll()
874 mutex_unlock(&ep->mtx); in __ep_eventpoll_poll()
880 * the ep->mtx so we need to start from depth=1, such that mutex_lock_nested()
886 struct file *file = epi->ffd.file; in ep_item_poll()
889 pt->_key = epi->event.events; in ep_item_poll()
894 return res & epi->event.events; in ep_item_poll()
905 struct eventpoll *ep = f->private_data; in ep_show_fdinfo()
908 mutex_lock(&ep->mtx); in ep_show_fdinfo()
909 for (rbp = rb_first_cached(&ep->rbr); rbp; rbp = rb_next(rbp)) { in ep_show_fdinfo()
911 struct inode *inode = file_inode(epi->ffd.file); in ep_show_fdinfo()
915 epi->ffd.fd, epi->event.events, in ep_show_fdinfo()
916 (long long)epi->event.data, in ep_show_fdinfo()
917 (long long)epi->ffd.file->f_pos, in ep_show_fdinfo()
918 inode->i_ino, inode->i_sb->s_dev); in ep_show_fdinfo()
922 mutex_unlock(&ep->mtx); in ep_show_fdinfo()
932 .poll = ep_eventpoll_poll,
950 * the ep->mtx. in eventpoll_release_file()
953 spin_lock(&file->f_lock); in eventpoll_release_file()
954 if (file->f_ep && file->f_ep->first) { in eventpoll_release_file()
955 epi = hlist_entry(file->f_ep->first, struct epitem, fllink); in eventpoll_release_file()
956 epi->dying = true; in eventpoll_release_file()
957 spin_unlock(&file->f_lock); in eventpoll_release_file()
963 ep = epi->ep; in eventpoll_release_file()
964 mutex_lock(&ep->mtx); in eventpoll_release_file()
966 mutex_unlock(&ep->mtx); in eventpoll_release_file()
972 spin_unlock(&file->f_lock); in eventpoll_release_file()
981 return -ENOMEM; in ep_alloc()
983 mutex_init(&ep->mtx); in ep_alloc()
984 rwlock_init(&ep->lock); in ep_alloc()
985 init_waitqueue_head(&ep->wq); in ep_alloc()
986 init_waitqueue_head(&ep->poll_wait); in ep_alloc()
987 INIT_LIST_HEAD(&ep->rdllist); in ep_alloc()
988 ep->rbr = RB_ROOT_CACHED; in ep_alloc()
989 ep->ovflist = EP_UNACTIVE_PTR; in ep_alloc()
990 ep->user = get_current_user(); in ep_alloc()
991 refcount_set(&ep->refcount, 1); in ep_alloc()
1011 for (rbp = ep->rbr.rb_root.rb_node; rbp; ) { in ep_find()
1013 kcmp = ep_cmp_ffd(&ffd, &epi->ffd); in ep_find()
1015 rbp = rbp->rb_right; in ep_find()
1017 rbp = rbp->rb_left; in ep_find()
1033 for (rbp = rb_first_cached(&ep->rbr); rbp; rbp = rb_next(rbp)) { in ep_find_tfd()
1035 if (epi->ffd.fd == tfd) { in ep_find_tfd()
1039 toff--; in ep_find_tfd()
1055 return ERR_PTR(-EINVAL); in get_epoll_tfile_raw_ptr()
1057 ep = file->private_data; in get_epoll_tfile_raw_ptr()
1059 mutex_lock(&ep->mtx); in get_epoll_tfile_raw_ptr()
1062 file_raw = epi->ffd.file; in get_epoll_tfile_raw_ptr()
1064 file_raw = ERR_PTR(-ENOENT); in get_epoll_tfile_raw_ptr()
1065 mutex_unlock(&ep->mtx); in get_epoll_tfile_raw_ptr()
1095 * This is simple 'new->next = head' operation, but cmpxchg() in list_add_tail_lockless()
1098 * new->next == new. in list_add_tail_lockless()
1100 if (!try_cmpxchg(&new->next, &new, head)) in list_add_tail_lockless()
1104 * Initially ->next of a new element must be updated with the head in list_add_tail_lockless()
1106 * exchanged. XCHG guarantees memory ordering, thus ->next should be in list_add_tail_lockless()
1108 * swapped before prev->next is updated. in list_add_tail_lockless()
1111 prev = xchg(&head->prev, new); in list_add_tail_lockless()
1114 * It is safe to modify prev->next and new->prev, because a new element in list_add_tail_lockless()
1115 * is added only to the tail and new->next is updated before XCHG. in list_add_tail_lockless()
1118 prev->next = new; in list_add_tail_lockless()
1119 new->prev = prev; in list_add_tail_lockless()
1125 * Chains a new epi entry to the tail of the ep->ovflist in a lockless way,
1132 struct eventpoll *ep = epi->ep; in chain_epi_lockless()
1135 if (epi->next != EP_UNACTIVE_PTR) in chain_epi_lockless()
1139 if (cmpxchg(&epi->next, EP_UNACTIVE_PTR, NULL) != EP_UNACTIVE_PTR) in chain_epi_lockless()
1143 epi->next = xchg(&ep->ovflist, epi); in chain_epi_lockless()
1154 * events from another file descriptor, thus all modifications to ->rdllist
1155 * or ->ovflist are lockless. Read lock is paired with the write lock from
1160 * concurrently for the same @epi from different CPUs if poll table was inited
1170 struct eventpoll *ep = epi->ep; in ep_poll_callback()
1175 read_lock_irqsave(&ep->lock, flags); in ep_poll_callback()
1180 * If the event mask does not contain any poll(2) event, we consider the in ep_poll_callback()
1185 if (!(epi->event.events & ~EP_PRIVATE_BITS)) in ep_poll_callback()
1194 if (pollflags && !(pollflags & epi->event.events)) in ep_poll_callback()
1198 * If we are transferring events to userspace, we can hold no locks in ep_poll_callback()
1199 * (because we're accessing user memory, and because of linux f_op->poll() in ep_poll_callback()
1201 * chained in ep->ovflist and requeued later on. in ep_poll_callback()
1203 if (READ_ONCE(ep->ovflist) != EP_UNACTIVE_PTR) { in ep_poll_callback()
1208 if (list_add_tail_lockless(&epi->rdllink, &ep->rdllist)) in ep_poll_callback()
1213 * Wake up ( if active ) both the eventpoll wait list and the ->poll() in ep_poll_callback()
1216 if (waitqueue_active(&ep->wq)) { in ep_poll_callback()
1217 if ((epi->event.events & EPOLLEXCLUSIVE) && in ep_poll_callback()
1221 if (epi->event.events & EPOLLIN) in ep_poll_callback()
1225 if (epi->event.events & EPOLLOUT) in ep_poll_callback()
1233 wake_up(&ep->wq); in ep_poll_callback()
1235 if (waitqueue_active(&ep->poll_wait)) in ep_poll_callback()
1239 read_unlock_irqrestore(&ep->lock, flags); in ep_poll_callback()
1245 if (!(epi->event.events & EPOLLEXCLUSIVE)) in ep_poll_callback()
1251 * ->whead = NULL and do another remove_wait_queue() after in ep_poll_callback()
1254 list_del_init(&wait->entry); in ep_poll_callback()
1256 * ->whead != NULL protects us from the race with in ep_poll_callback()
1258 * takes whead->lock held by the caller. Once we nullify it, in ep_poll_callback()
1261 smp_store_release(&ep_pwq_from_wait(wait)->whead, NULL); in ep_poll_callback()
1275 struct epitem *epi = epq->epi; in ep_ptable_queue_proc()
1283 epq->epi = NULL; in ep_ptable_queue_proc()
1287 init_waitqueue_func_entry(&pwq->wait, ep_poll_callback); in ep_ptable_queue_proc()
1288 pwq->whead = whead; in ep_ptable_queue_proc()
1289 pwq->base = epi; in ep_ptable_queue_proc()
1290 if (epi->event.events & EPOLLEXCLUSIVE) in ep_ptable_queue_proc()
1291 add_wait_queue_exclusive(whead, &pwq->wait); in ep_ptable_queue_proc()
1293 add_wait_queue(whead, &pwq->wait); in ep_ptable_queue_proc()
1294 pwq->next = epi->pwqlist; in ep_ptable_queue_proc()
1295 epi->pwqlist = pwq; in ep_ptable_queue_proc()
1301 struct rb_node **p = &ep->rbr.rb_root.rb_node, *parent = NULL; in ep_rbtree_insert()
1308 kcmp = ep_cmp_ffd(&epi->ffd, &epic->ffd); in ep_rbtree_insert()
1310 p = &parent->rb_right; in ep_rbtree_insert()
1313 p = &parent->rb_left; in ep_rbtree_insert()
1315 rb_link_node(&epi->rbn, parent, p); in ep_rbtree_insert()
1316 rb_insert_color_cached(&epi->rbn, &ep->rbr, leftmost); in ep_rbtree_insert()
1343 return -1; in path_count_inc()
1361 return -1; in reverse_path_check_proc()
1365 struct hlist_head *refs = &epi->ep->refs; in reverse_path_check_proc()
1377 * reverse_path_check - The tfile_check_list is list of epitem_head, which have
1384 * %-1 otherwise.
1390 for (p = tfile_check_list; p != EP_UNACTIVE_PTR; p = p->next) { in reverse_path_check()
1394 error = reverse_path_check_proc(&p->epitems, 0); in reverse_path_check()
1407 if (!epi->ep->ws) { in ep_create_wakeup_source()
1408 epi->ep->ws = wakeup_source_register(NULL, "eventpoll"); in ep_create_wakeup_source()
1409 if (!epi->ep->ws) in ep_create_wakeup_source()
1410 return -ENOMEM; in ep_create_wakeup_source()
1413 take_dentry_name_snapshot(&n, epi->ffd.file->f_path.dentry); in ep_create_wakeup_source()
1418 return -ENOMEM; in ep_create_wakeup_source()
1419 rcu_assign_pointer(epi->ws, ws); in ep_create_wakeup_source()
1429 RCU_INIT_POINTER(epi->ws, NULL); in ep_destroy_wakeup_source()
1447 ep = file->private_data; in attach_epitem()
1450 head = &ep->refs; in attach_epitem()
1451 } else if (!READ_ONCE(file->f_ep)) { in attach_epitem()
1455 return -ENOMEM; in attach_epitem()
1456 head = &to_free->epitems; in attach_epitem()
1458 spin_lock(&file->f_lock); in attach_epitem()
1459 if (!file->f_ep) { in attach_epitem()
1461 spin_unlock(&file->f_lock); in attach_epitem()
1464 file->f_ep = head; in attach_epitem()
1467 hlist_add_head_rcu(&epi->fllink, file->f_ep); in attach_epitem()
1468 spin_unlock(&file->f_lock); in attach_epitem()
1486 tep = tfile->private_data; in ep_insert()
1490 if (unlikely(percpu_counter_compare(&ep->user->epoll_watches, in ep_insert()
1492 return -ENOSPC; in ep_insert()
1493 percpu_counter_inc(&ep->user->epoll_watches); in ep_insert()
1496 percpu_counter_dec(&ep->user->epoll_watches); in ep_insert()
1497 return -ENOMEM; in ep_insert()
1501 INIT_LIST_HEAD(&epi->rdllink); in ep_insert()
1502 epi->ep = ep; in ep_insert()
1503 ep_set_ffd(&epi->ffd, tfile, fd); in ep_insert()
1504 epi->event = *event; in ep_insert()
1505 epi->next = EP_UNACTIVE_PTR; in ep_insert()
1508 mutex_lock_nested(&tep->mtx, 1); in ep_insert()
1512 mutex_unlock(&tep->mtx); in ep_insert()
1514 percpu_counter_dec(&ep->user->epoll_watches); in ep_insert()
1515 return -ENOMEM; in ep_insert()
1527 mutex_unlock(&tep->mtx); in ep_insert()
1538 return -EINVAL; in ep_insert()
1541 if (epi->event.events & EPOLLWAKEUP) { in ep_insert()
1549 /* Initialize the poll table using the queue callback */ in ep_insert()
1554 * Attach the item to the poll hooks and get current event bits. in ep_insert()
1557 * this operation completes, the poll callback can start hitting in ep_insert()
1563 * We have to check if something went wrong during the poll wait queue in ep_insert()
1569 return -ENOMEM; in ep_insert()
1573 write_lock_irq(&ep->lock); in ep_insert()
1580 list_add_tail(&epi->rdllink, &ep->rdllist); in ep_insert()
1584 if (waitqueue_active(&ep->wq)) in ep_insert()
1585 wake_up(&ep->wq); in ep_insert()
1586 if (waitqueue_active(&ep->poll_wait)) in ep_insert()
1590 write_unlock_irq(&ep->lock); in ep_insert()
1614 * Set the new event interest mask before calling f_op->poll(); in ep_modify()
1616 * f_op->poll() call and the new event set registering. in ep_modify()
1618 epi->event.events = event->events; /* need barrier below */ in ep_modify()
1619 epi->event.data = event->data; /* protected by mtx */ in ep_modify()
1620 if (epi->event.events & EPOLLWAKEUP) { in ep_modify()
1632 * event occurs immediately after we call f_op->poll(). in ep_modify()
1633 * We need this because we did not take ep->lock while in ep_modify()
1635 * ep->lock). in ep_modify()
1638 * when calling f_op->poll(). This barrier also in ep_modify()
1642 * This barrier will now guarantee ep_poll_callback or f_op->poll in ep_modify()
1654 write_lock_irq(&ep->lock); in ep_modify()
1656 list_add_tail(&epi->rdllink, &ep->rdllist); in ep_modify()
1660 if (waitqueue_active(&ep->wq)) in ep_modify()
1661 wake_up(&ep->wq); in ep_modify()
1662 if (waitqueue_active(&ep->poll_wait)) in ep_modify()
1665 write_unlock_irq(&ep->lock); in ep_modify()
1684 * Always short-circuit for fatal signals to allow threads to make a in ep_send_events()
1689 return -EINTR; in ep_send_events()
1693 mutex_lock(&ep->mtx); in ep_send_events()
1698 * Items cannot vanish during the loop we are holding ep->mtx. in ep_send_events()
1708 * Activate ep->ws before deactivating epi->ws to prevent in ep_send_events()
1709 * triggering auto-suspend here (in case we reactive epi->ws in ep_send_events()
1712 * This could be rearranged to delay the deactivation of epi->ws in ep_send_events()
1713 * instead, but then epi->ws would temporarily be out of sync in ep_send_events()
1718 if (ws->active) in ep_send_events()
1719 __pm_stay_awake(ep->ws); in ep_send_events()
1723 list_del_init(&epi->rdllink); in ep_send_events()
1726 * If the event mask intersect the caller-requested one, in ep_send_events()
1727 * deliver the event to userspace. Again, we are holding ep->mtx, in ep_send_events()
1728 * so no operations coming from userspace can change the item. in ep_send_events()
1734 events = epoll_put_uevent(revents, epi->event.data, events); in ep_send_events()
1736 list_add(&epi->rdllink, &txlist); in ep_send_events()
1739 res = -EFAULT; in ep_send_events()
1743 if (epi->event.events & EPOLLONESHOT) in ep_send_events()
1744 epi->event.events &= EP_PRIVATE_BITS; in ep_send_events()
1745 else if (!(epi->event.events & EPOLLET)) { in ep_send_events()
1751 * availability. At this point, no one can insert in ep_send_events()
1752 * into ep->rdllist besides us. The epoll_ctl() in ep_send_events()
1755 * poll callback will queue them in ep->ovflist. in ep_send_events()
1757 list_add_tail(&epi->rdllink, &ep->rdllist); in ep_send_events()
1762 mutex_unlock(&ep->mtx); in ep_send_events()
1775 to->tv_sec = 0; in ep_timeout_to_timespec()
1776 to->tv_nsec = 0; in ep_timeout_to_timespec()
1780 to->tv_sec = ms / MSEC_PER_SEC; in ep_timeout_to_timespec()
1781 to->tv_nsec = NSEC_PER_MSEC * (ms % MSEC_PER_SEC); in ep_timeout_to_timespec()
1789 * autoremove_wake_function, but remove even on failure to wake up, because we
1803 list_del_init_careful(&wq_entry->entry); in ep_autoremove_wake_function()
1808 * ep_poll - Retrieves ready events, and delivers them to the caller-supplied
1834 if (timeout && (timeout->tv_sec | timeout->tv_nsec)) { in ep_poll()
1849 * with a non-zero timeout, this thread will check the ready list under in ep_poll()
1876 return -EINTR; in ep_poll()
1880 * thus wait entry is removed from the wait queue on each in ep_poll()
1884 * lost. This is also good performance-wise, because on in ep_poll()
1885 * normal wakeup path no need to call __remove_wait_queue() in ep_poll()
1886 * explicitly, thus ep->lock is not taken, which halts the in ep_poll()
1898 write_lock_irq(&ep->lock); in ep_poll()
1901 * the same lock on wakeup ep_poll_callback() side, so it in ep_poll()
1908 * plays with two lists (->rdllist and ->ovflist) and there in ep_poll()
1915 __add_wait_queue_exclusive(&ep->wq, &wait); in ep_poll()
1917 write_unlock_irq(&ep->lock); in ep_poll()
1926 * If timed out and still on the wait queue, recheck eavail in ep_poll()
1932 write_lock_irq(&ep->lock); in ep_poll()
1934 * If the thread timed out and is not on the wait queue, in ep_poll()
1942 __remove_wait_queue(&ep->wq, &wait); in ep_poll()
1943 write_unlock_irq(&ep->lock); in ep_poll()
1949 * ep_loop_check_proc - verify that adding an epoll file inside another
1958 * structure @ep does not violate the constraints, or %-1 otherwise.
1966 mutex_lock_nested(&ep->mtx, depth + 1); in ep_loop_check_proc()
1967 ep->gen = loop_check_gen; in ep_loop_check_proc()
1968 for (rbp = rb_first_cached(&ep->rbr); rbp; rbp = rb_next(rbp)) { in ep_loop_check_proc()
1970 if (unlikely(is_file_epoll(epi->ffd.file))) { in ep_loop_check_proc()
1972 ep_tovisit = epi->ffd.file->private_data; in ep_loop_check_proc()
1973 if (ep_tovisit->gen == loop_check_gen) in ep_loop_check_proc()
1976 error = -1; in ep_loop_check_proc()
1990 list_file(epi->ffd.file); in ep_loop_check_proc()
1993 mutex_unlock(&ep->mtx); in ep_loop_check_proc()
1999 * ep_loop_check - Performs a check to verify that adding an epoll file (@to)
2007 * does not violate the constraints, or %-1 otherwise.
2020 tfile_check_list = head->next; in clear_tfile_check_list()
2039 return -EINVAL; in do_epoll_create()
2061 ep->file = file; in do_epoll_create()
2080 return -EINVAL; in SYSCALL_DEFINE1()
2088 if ((epev->events & EPOLLWAKEUP) && !capable(CAP_BLOCK_SUSPEND)) in ep_take_care_of_epollwakeup()
2089 epev->events &= ~EPOLLWAKEUP; in ep_take_care_of_epollwakeup()
2094 epev->events &= ~EPOLLWAKEUP; in ep_take_care_of_epollwakeup()
2107 return -EAGAIN; in epoll_mutex_lock()
2120 error = -EBADF; in do_epoll_ctl()
2130 /* The target file descriptor must support poll */ in do_epoll_ctl()
2131 error = -EPERM; in do_epoll_ctl()
2144 error = -EINVAL; in do_epoll_ctl()
2153 if (ep_op_has_event(op) && (epds->events & EPOLLEXCLUSIVE)) { in do_epoll_ctl()
2157 (epds->events & ~EPOLLEXCLUSIVE_OK_BITS))) in do_epoll_ctl()
2165 ep = f.file->private_data; in do_epoll_ctl()
2172 * and hang them on the tfile_check_list, so we can check that we in do_epoll_ctl()
2175 * We do not need to take the global 'epumutex' on EPOLL_CTL_ADD when in do_epoll_ctl()
2178 * 'epnested_mutex' on add is to prevent complex toplogies such as loops and in do_epoll_ctl()
2182 error = epoll_mutex_lock(&ep->mtx, 0, nonblock); in do_epoll_ctl()
2186 if (READ_ONCE(f.file->f_ep) || ep->gen == loop_check_gen || in do_epoll_ctl()
2188 mutex_unlock(&ep->mtx); in do_epoll_ctl()
2195 tep = tf.file->private_data; in do_epoll_ctl()
2196 error = -ELOOP; in do_epoll_ctl()
2200 error = epoll_mutex_lock(&ep->mtx, 0, nonblock); in do_epoll_ctl()
2213 error = -EINVAL; in do_epoll_ctl()
2217 epds->events |= EPOLLERR | EPOLLHUP; in do_epoll_ctl()
2220 error = -EEXIST; in do_epoll_ctl()
2231 error = -ENOENT; in do_epoll_ctl()
2236 if (!(epi->event.events & EPOLLEXCLUSIVE)) { in do_epoll_ctl()
2237 epds->events |= EPOLLERR | EPOLLHUP; in do_epoll_ctl()
2241 error = -ENOENT; in do_epoll_ctl()
2244 mutex_unlock(&ep->mtx); in do_epoll_ctl()
2273 return -EFAULT; in SYSCALL_DEFINE4()
2291 return -EINVAL; in do_epoll_wait()
2295 return -EFAULT; in do_epoll_wait()
2300 return -EBADF; in do_epoll_wait()
2306 error = -EINVAL; in do_epoll_wait()
2314 ep = f.file->private_data; in do_epoll_wait()
2353 restore_saved_sigmask_unless(error == -EINTR); in do_epoll_pwait()
2377 return -EFAULT; in SYSCALL_DEFINE6()
2380 return -EINVAL; in SYSCALL_DEFINE6()
2405 restore_saved_sigmask_unless(err == -EINTR); in do_compat_epoll_pwait()
2434 return -EFAULT; in COMPAT_SYSCALL_DEFINE6()
2437 return -EINVAL; in COMPAT_SYSCALL_DEFINE6()
2454 max_user_watches = (((si.totalram - si.totalhigh) / 25) << PAGE_SHIFT) / in eventpoll_init()
2460 * using an extra cache line on 64-bit (and smaller) CPUs in eventpoll_init()