Lines Matching +full:poll +full:- +full:timeout +full:- +full:ms
1 // SPDX-License-Identifier: GPL-2.0-or-later
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,
73 * It is necessary to acquire multiple "ep->mtx"es at once in the
76 * epoll_ctl(e1, EPOLL_CTL_ADD, e2), e1->mtx will always be acquired
77 * before e2->mtx). Since we disallow cycles of epoll file
78 * descriptors, this ensures that the mutexes are well-ordered. In
82 * It is possible to drop the "ep->mtx" and to use the global
83 * mutex "epmutex" (together with "ep->lock") to have it working,
84 * but having "ep->mtx" will make the interface more scalable.
86 * normal operations the epoll private "ep->mtx" will guarantee
103 #define EP_UNACTIVE_PTR ((void *) -1L)
112 /* Wait structure used by the poll hooks */
148 * Works together "struct eventpoll"->ovflist in keeping the
156 /* List containing poll wait queues */
189 /* Wait queue used by file->poll() */
204 * holding ->lock.
221 /* used to track busy poll napi_id */
231 /* Wrapper struct used by poll queueing */
281 head = container_of(file->f_ep, struct epitems_head, epitems); in list_file()
282 if (!head->next) { in list_file()
283 head->next = tfile_check_list; in list_file()
291 struct hlist_node *p = rcu_dereference(hlist_first_rcu(&head->epitems)); in unlist_file()
294 spin_lock(&epi->ffd.file->f_lock); in unlist_file()
295 if (!hlist_empty(&head->epitems)) in unlist_file()
297 head->next = NULL; in unlist_file()
298 spin_unlock(&epi->ffd.file->f_lock); in unlist_file()
328 return f->f_op == &eventpoll_fops; in is_file_epoll()
335 ffd->file = file; in ep_set_ffd()
336 ffd->fd = fd; in ep_set_ffd()
343 return (p1->file > p2->file ? +1: in ep_cmp_ffd()
344 (p1->file < p2->file ? -1 : p1->fd - p2->fd)); in ep_cmp_ffd()
350 return !list_empty(&epi->rdllink); in ep_is_linked()
361 return container_of(p, struct eppoll_entry, wait)->base; in ep_item_from_wait()
365 * ep_events_available - Checks if ready events might be available.
374 return !list_empty_careful(&ep->rdllist) || in ep_events_available()
375 READ_ONCE(ep->ovflist) != EP_UNACTIVE_PTR; in ep_events_available()
387 * Busy poll if globally on and supporting sockets found && no events,
394 unsigned int napi_id = READ_ONCE(ep->napi_id); in ep_busy_loop()
402 * Busy poll timed out. Drop NAPI ID for now, we can add in ep_busy_loop()
406 ep->napi_id = 0; in ep_busy_loop()
413 * Set epoll busy poll NAPI ID from sk.
425 sock = sock_from_file(epi->ffd.file); in ep_set_busy_poll_napi_id()
429 sk = sock->sk; in ep_set_busy_poll_napi_id()
433 napi_id = READ_ONCE(sk->sk_napi_id); in ep_set_busy_poll_napi_id()
434 ep = epi->ep; in ep_set_busy_poll_napi_id()
436 /* Non-NAPI IDs can be rejected in ep_set_busy_poll_napi_id()
440 if (napi_id < MIN_NAPI_ID || napi_id == ep->napi_id) in ep_set_busy_poll_napi_id()
443 /* record NAPI ID for use in next busy poll */ in ep_set_busy_poll_napi_id()
444 ep->napi_id = napi_id; in ep_set_busy_poll_napi_id()
473 * issue a wake_up() on its poll wake list. Epoll (efd1) has installed a
477 * the waiters on its poll wait list (efd2). So it calls ep_poll_safewake()
495 * it might be natural to create a per-cpu nest count. However, since in ep_poll_safewake()
496 * we can recurse on ep->poll_wait.lock, and a non-raw spinlock can in ep_poll_safewake()
497 * schedule() in the -rt kernel, the per-cpu variable are no longer in ep_poll_safewake()
509 if ((is_file_epoll(epi->ffd.file))) { in ep_poll_safewake()
510 ep_src = epi->ffd.file->private_data; in ep_poll_safewake()
511 nests = ep_src->nests; in ep_poll_safewake()
516 spin_lock_irqsave_nested(&ep->poll_wait.lock, flags, nests); in ep_poll_safewake()
517 ep->nests = nests + 1; in ep_poll_safewake()
518 wake_up_locked_poll(&ep->poll_wait, EPOLLIN); in ep_poll_safewake()
519 ep->nests = 0; in ep_poll_safewake()
520 spin_unlock_irqrestore(&ep->poll_wait.lock, flags); in ep_poll_safewake()
527 wake_up_poll(&ep->poll_wait, EPOLLIN); in ep_poll_safewake()
538 * If it is cleared by POLLFREE, it should be rcu-safe. in ep_remove_wait_queue()
541 * we rely on whead->lock. in ep_remove_wait_queue()
543 whead = smp_load_acquire(&pwq->whead); in ep_remove_wait_queue()
545 remove_wait_queue(whead, &pwq->wait); in ep_remove_wait_queue()
550 * This function unregisters poll callbacks from the associated file
556 struct eppoll_entry **p = &epi->pwqlist; in ep_unregister_pollwait()
560 *p = pwq->next; in ep_unregister_pollwait()
566 /* call only when ep->mtx is held */
569 return rcu_dereference_check(epi->ws, lockdep_is_held(&epi->ep->mtx)); in ep_wakeup_source()
572 /* call only when ep->mtx is held */
583 return rcu_access_pointer(epi->ws) ? true : false; in ep_has_wakeup_source()
586 /* call when ep->mtx cannot be held (ep_poll_callback) */
592 ws = rcu_dereference(epi->ws); in ep_pm_stay_awake_rcu()
600 * ep->mutex needs to be held because we could be hit by
606 * Steal the ready list, and re-init the original one to the in ep_start_scan()
607 * empty list. Also, set ep->ovflist to NULL so that events in ep_start_scan()
609 * have the poll callback to queue directly on ep->rdllist, in ep_start_scan()
614 write_lock_irq(&ep->lock); in ep_start_scan()
615 list_splice_init(&ep->rdllist, txlist); in ep_start_scan()
616 WRITE_ONCE(ep->ovflist, NULL); in ep_start_scan()
617 write_unlock_irq(&ep->lock); in ep_start_scan()
625 write_lock_irq(&ep->lock); in ep_done_scan()
628 * other events might have been queued by the poll callback. in ep_done_scan()
629 * We re-insert them inside the main ready-list here. in ep_done_scan()
631 for (nepi = READ_ONCE(ep->ovflist); (epi = nepi) != NULL; in ep_done_scan()
632 nepi = epi->next, epi->next = EP_UNACTIVE_PTR) { in ep_done_scan()
636 * queued into ->ovflist but the "txlist" might already in ep_done_scan()
641 * ->ovflist is LIFO, so we have to reverse it in order in ep_done_scan()
644 list_add(&epi->rdllink, &ep->rdllist); in ep_done_scan()
649 * We need to set back ep->ovflist to EP_UNACTIVE_PTR, so that after in ep_done_scan()
651 * ep->rdllist. in ep_done_scan()
653 WRITE_ONCE(ep->ovflist, EP_UNACTIVE_PTR); in ep_done_scan()
656 * Quickly re-inject items left on "txlist". in ep_done_scan()
658 list_splice(txlist, &ep->rdllist); in ep_done_scan()
659 __pm_relax(ep->ws); in ep_done_scan()
661 if (!list_empty(&ep->rdllist)) { in ep_done_scan()
662 if (waitqueue_active(&ep->wq)) in ep_done_scan()
663 wake_up(&ep->wq); in ep_done_scan()
666 write_unlock_irq(&ep->lock); in ep_done_scan()
681 struct file *file = epi->ffd.file; in ep_remove()
688 * Removes poll wait queue hooks. in ep_remove()
693 spin_lock(&file->f_lock); in ep_remove()
695 head = file->f_ep; in ep_remove()
696 if (head->first == &epi->fllink && !epi->fllink.next) { in ep_remove()
697 file->f_ep = NULL; in ep_remove()
701 if (!smp_load_acquire(&v->next)) in ep_remove()
705 hlist_del_rcu(&epi->fllink); in ep_remove()
706 spin_unlock(&file->f_lock); in ep_remove()
709 rb_erase_cached(&epi->rbn, &ep->rbr); in ep_remove()
711 write_lock_irq(&ep->lock); in ep_remove()
713 list_del_init(&epi->rdllink); in ep_remove()
714 write_unlock_irq(&ep->lock); in ep_remove()
719 * field epi->rcu, since we are trying to minimize the size of in ep_remove()
721 * ep->mtx. The rcu read side, reverse_path_check_proc(), does not make in ep_remove()
724 call_rcu(&epi->rcu, epi_rcu_free); in ep_remove()
726 percpu_counter_dec(&ep->user->epoll_watches); in ep_remove()
737 if (waitqueue_active(&ep->poll_wait)) in ep_free()
743 * We do not need to hold "ep->mtx" here because the epoll file in ep_free()
751 * Walks through the whole tree by unregistering poll callbacks. in ep_free()
753 for (rbp = rb_first_cached(&ep->rbr); rbp; rbp = rb_next(rbp)) { in ep_free()
762 * point we are sure no poll callbacks will be lingering around, and also by in ep_free()
764 * us during this operation. So we can avoid the lock on "ep->lock". in ep_free()
765 * We do not need to lock ep->mtx, either, we only do it to prevent in ep_free()
768 mutex_lock(&ep->mtx); in ep_free()
769 while ((rbp = rb_first_cached(&ep->rbr)) != NULL) { in ep_free()
774 mutex_unlock(&ep->mtx); in ep_free()
777 mutex_destroy(&ep->mtx); in ep_free()
778 free_uid(ep->user); in ep_free()
779 wakeup_source_unregister(ep->ws); in ep_free()
785 struct eventpoll *ep = file->private_data; in ep_eventpoll_release()
797 struct eventpoll *ep = file->private_data; in __ep_eventpoll_poll()
805 /* Insert inside our poll wait queue */ in __ep_eventpoll_poll()
806 poll_wait(file, &ep->poll_wait, wait); in __ep_eventpoll_poll()
812 mutex_lock_nested(&ep->mtx, depth); in __ep_eventpoll_poll()
820 * Item has been dropped into the ready list by the poll in __ep_eventpoll_poll()
825 list_del_init(&epi->rdllink); in __ep_eventpoll_poll()
829 mutex_unlock(&ep->mtx); in __ep_eventpoll_poll()
835 * the ep->mtx so we need to start from depth=1, such that mutex_lock_nested()
841 struct file *file = epi->ffd.file; in ep_item_poll()
844 pt->_key = epi->event.events; in ep_item_poll()
849 return res & epi->event.events; in ep_item_poll()
860 struct eventpoll *ep = f->private_data; in ep_show_fdinfo()
863 mutex_lock(&ep->mtx); in ep_show_fdinfo()
864 for (rbp = rb_first_cached(&ep->rbr); rbp; rbp = rb_next(rbp)) { in ep_show_fdinfo()
866 struct inode *inode = file_inode(epi->ffd.file); in ep_show_fdinfo()
870 epi->ffd.fd, epi->event.events, in ep_show_fdinfo()
871 (long long)epi->event.data, in ep_show_fdinfo()
872 (long long)epi->ffd.file->f_pos, in ep_show_fdinfo()
873 inode->i_ino, inode->i_sb->s_dev); in ep_show_fdinfo()
877 mutex_unlock(&ep->mtx); in ep_show_fdinfo()
887 .poll = ep_eventpoll_poll,
903 * We don't want to get "file->f_lock" because it is not in eventpoll_release_file()
910 * "ep->mtx" after "epmutex" because ep_remove() requires it when called in eventpoll_release_file()
916 if (unlikely(!file->f_ep)) { in eventpoll_release_file()
920 hlist_for_each_entry_safe(epi, next, file->f_ep, fllink) { in eventpoll_release_file()
921 ep = epi->ep; in eventpoll_release_file()
922 mutex_lock_nested(&ep->mtx, 0); in eventpoll_release_file()
924 mutex_unlock(&ep->mtx); in eventpoll_release_file()
936 error = -ENOMEM; in ep_alloc()
941 mutex_init(&ep->mtx); in ep_alloc()
942 rwlock_init(&ep->lock); in ep_alloc()
943 init_waitqueue_head(&ep->wq); in ep_alloc()
944 init_waitqueue_head(&ep->poll_wait); in ep_alloc()
945 INIT_LIST_HEAD(&ep->rdllist); in ep_alloc()
946 ep->rbr = RB_ROOT_CACHED; in ep_alloc()
947 ep->ovflist = EP_UNACTIVE_PTR; in ep_alloc()
948 ep->user = user; in ep_alloc()
972 for (rbp = ep->rbr.rb_root.rb_node; rbp; ) { in ep_find()
974 kcmp = ep_cmp_ffd(&ffd, &epi->ffd); in ep_find()
976 rbp = rbp->rb_right; in ep_find()
978 rbp = rbp->rb_left; in ep_find()
994 for (rbp = rb_first_cached(&ep->rbr); rbp; rbp = rb_next(rbp)) { in ep_find_tfd()
996 if (epi->ffd.fd == tfd) { in ep_find_tfd()
1000 toff--; in ep_find_tfd()
1016 return ERR_PTR(-EINVAL); in get_epoll_tfile_raw_ptr()
1018 ep = file->private_data; in get_epoll_tfile_raw_ptr()
1020 mutex_lock(&ep->mtx); in get_epoll_tfile_raw_ptr()
1023 file_raw = epi->ffd.file; in get_epoll_tfile_raw_ptr()
1025 file_raw = ERR_PTR(-ENOENT); in get_epoll_tfile_raw_ptr()
1026 mutex_unlock(&ep->mtx); in get_epoll_tfile_raw_ptr()
1056 * This is simple 'new->next = head' operation, but cmpxchg() in list_add_tail_lockless()
1059 * new->next == new. in list_add_tail_lockless()
1061 if (cmpxchg(&new->next, new, head) != new) in list_add_tail_lockless()
1065 * Initially ->next of a new element must be updated with the head in list_add_tail_lockless()
1067 * exchanged. XCHG guarantees memory ordering, thus ->next should be in list_add_tail_lockless()
1069 * swapped before prev->next is updated. in list_add_tail_lockless()
1072 prev = xchg(&head->prev, new); in list_add_tail_lockless()
1075 * It is safe to modify prev->next and new->prev, because a new element in list_add_tail_lockless()
1076 * is added only to the tail and new->next is updated before XCHG. in list_add_tail_lockless()
1079 prev->next = new; in list_add_tail_lockless()
1080 new->prev = prev; in list_add_tail_lockless()
1086 * Chains a new epi entry to the tail of the ep->ovflist in a lockless way,
1093 struct eventpoll *ep = epi->ep; in chain_epi_lockless()
1096 if (epi->next != EP_UNACTIVE_PTR) in chain_epi_lockless()
1100 if (cmpxchg(&epi->next, EP_UNACTIVE_PTR, NULL) != EP_UNACTIVE_PTR) in chain_epi_lockless()
1104 epi->next = xchg(&ep->ovflist, epi); in chain_epi_lockless()
1115 * events from another file descriptor, thus all modifications to ->rdllist
1116 * or ->ovflist are lockless. Read lock is paired with the write lock from
1121 * concurrently for the same @epi from different CPUs if poll table was inited
1131 struct eventpoll *ep = epi->ep; in ep_poll_callback()
1136 read_lock_irqsave(&ep->lock, flags); in ep_poll_callback()
1141 * If the event mask does not contain any poll(2) event, we consider the in ep_poll_callback()
1146 if (!(epi->event.events & ~EP_PRIVATE_BITS)) in ep_poll_callback()
1155 if (pollflags && !(pollflags & epi->event.events)) in ep_poll_callback()
1160 * (because we're accessing user memory, and because of linux f_op->poll() in ep_poll_callback()
1162 * chained in ep->ovflist and requeued later on. in ep_poll_callback()
1164 if (READ_ONCE(ep->ovflist) != EP_UNACTIVE_PTR) { in ep_poll_callback()
1169 if (list_add_tail_lockless(&epi->rdllink, &ep->rdllist)) in ep_poll_callback()
1174 * Wake up ( if active ) both the eventpoll wait list and the ->poll() in ep_poll_callback()
1177 if (waitqueue_active(&ep->wq)) { in ep_poll_callback()
1178 if ((epi->event.events & EPOLLEXCLUSIVE) && in ep_poll_callback()
1182 if (epi->event.events & EPOLLIN) in ep_poll_callback()
1186 if (epi->event.events & EPOLLOUT) in ep_poll_callback()
1194 wake_up(&ep->wq); in ep_poll_callback()
1196 if (waitqueue_active(&ep->poll_wait)) in ep_poll_callback()
1200 read_unlock_irqrestore(&ep->lock, flags); in ep_poll_callback()
1206 if (!(epi->event.events & EPOLLEXCLUSIVE)) in ep_poll_callback()
1212 * ->whead = NULL and do another remove_wait_queue() after in ep_poll_callback()
1215 list_del_init(&wait->entry); in ep_poll_callback()
1217 * ->whead != NULL protects us from the race with ep_free() in ep_poll_callback()
1218 * or ep_remove(), ep_remove_wait_queue() takes whead->lock in ep_poll_callback()
1222 smp_store_release(&ep_pwq_from_wait(wait)->whead, NULL); in ep_poll_callback()
1236 struct epitem *epi = epq->epi; in ep_ptable_queue_proc()
1244 epq->epi = NULL; in ep_ptable_queue_proc()
1248 init_waitqueue_func_entry(&pwq->wait, ep_poll_callback); in ep_ptable_queue_proc()
1249 pwq->whead = whead; in ep_ptable_queue_proc()
1250 pwq->base = epi; in ep_ptable_queue_proc()
1251 if (epi->event.events & EPOLLEXCLUSIVE) in ep_ptable_queue_proc()
1252 add_wait_queue_exclusive(whead, &pwq->wait); in ep_ptable_queue_proc()
1254 add_wait_queue(whead, &pwq->wait); in ep_ptable_queue_proc()
1255 pwq->next = epi->pwqlist; in ep_ptable_queue_proc()
1256 epi->pwqlist = pwq; in ep_ptable_queue_proc()
1262 struct rb_node **p = &ep->rbr.rb_root.rb_node, *parent = NULL; in ep_rbtree_insert()
1269 kcmp = ep_cmp_ffd(&epi->ffd, &epic->ffd); in ep_rbtree_insert()
1271 p = &parent->rb_right; in ep_rbtree_insert()
1274 p = &parent->rb_left; in ep_rbtree_insert()
1276 rb_link_node(&epi->rbn, parent, p); in ep_rbtree_insert()
1277 rb_insert_color_cached(&epi->rbn, &ep->rbr, leftmost); in ep_rbtree_insert()
1304 return -1; in path_count_inc()
1322 return -1; in reverse_path_check_proc()
1326 struct hlist_head *refs = &epi->ep->refs; in reverse_path_check_proc()
1338 * reverse_path_check - The tfile_check_list is list of epitem_head, which have
1345 * %-1 otherwise.
1351 for (p = tfile_check_list; p != EP_UNACTIVE_PTR; p = p->next) { in reverse_path_check()
1355 error = reverse_path_check_proc(&p->epitems, 0); in reverse_path_check()
1368 if (!epi->ep->ws) { in ep_create_wakeup_source()
1369 epi->ep->ws = wakeup_source_register(NULL, "eventpoll"); in ep_create_wakeup_source()
1370 if (!epi->ep->ws) in ep_create_wakeup_source()
1371 return -ENOMEM; in ep_create_wakeup_source()
1374 take_dentry_name_snapshot(&n, epi->ffd.file->f_path.dentry); in ep_create_wakeup_source()
1379 return -ENOMEM; in ep_create_wakeup_source()
1380 rcu_assign_pointer(epi->ws, ws); in ep_create_wakeup_source()
1390 RCU_INIT_POINTER(epi->ws, NULL); in ep_destroy_wakeup_source()
1408 ep = file->private_data; in attach_epitem()
1411 head = &ep->refs; in attach_epitem()
1412 } else if (!READ_ONCE(file->f_ep)) { in attach_epitem()
1416 return -ENOMEM; in attach_epitem()
1417 head = &to_free->epitems; in attach_epitem()
1419 spin_lock(&file->f_lock); in attach_epitem()
1420 if (!file->f_ep) { in attach_epitem()
1422 spin_unlock(&file->f_lock); in attach_epitem()
1425 file->f_ep = head; in attach_epitem()
1428 hlist_add_head_rcu(&epi->fllink, file->f_ep); in attach_epitem()
1429 spin_unlock(&file->f_lock); in attach_epitem()
1447 tep = tfile->private_data; in ep_insert()
1451 if (unlikely(percpu_counter_compare(&ep->user->epoll_watches, in ep_insert()
1453 return -ENOSPC; in ep_insert()
1454 percpu_counter_inc(&ep->user->epoll_watches); in ep_insert()
1457 percpu_counter_dec(&ep->user->epoll_watches); in ep_insert()
1458 return -ENOMEM; in ep_insert()
1462 INIT_LIST_HEAD(&epi->rdllink); in ep_insert()
1463 epi->ep = ep; in ep_insert()
1464 ep_set_ffd(&epi->ffd, tfile, fd); in ep_insert()
1465 epi->event = *event; in ep_insert()
1466 epi->next = EP_UNACTIVE_PTR; in ep_insert()
1469 mutex_lock_nested(&tep->mtx, 1); in ep_insert()
1473 mutex_unlock(&tep->mtx); in ep_insert()
1475 percpu_counter_dec(&ep->user->epoll_watches); in ep_insert()
1476 return -ENOMEM; in ep_insert()
1488 mutex_unlock(&tep->mtx); in ep_insert()
1493 return -EINVAL; in ep_insert()
1496 if (epi->event.events & EPOLLWAKEUP) { in ep_insert()
1504 /* Initialize the poll table using the queue callback */ in ep_insert()
1509 * Attach the item to the poll hooks and get current event bits. in ep_insert()
1512 * this operation completes, the poll callback can start hitting in ep_insert()
1518 * We have to check if something went wrong during the poll wait queue in ep_insert()
1524 return -ENOMEM; in ep_insert()
1528 write_lock_irq(&ep->lock); in ep_insert()
1535 list_add_tail(&epi->rdllink, &ep->rdllist); in ep_insert()
1539 if (waitqueue_active(&ep->wq)) in ep_insert()
1540 wake_up(&ep->wq); in ep_insert()
1541 if (waitqueue_active(&ep->poll_wait)) in ep_insert()
1545 write_unlock_irq(&ep->lock); in ep_insert()
1569 * Set the new event interest mask before calling f_op->poll(); in ep_modify()
1571 * f_op->poll() call and the new event set registering. in ep_modify()
1573 epi->event.events = event->events; /* need barrier below */ in ep_modify()
1574 epi->event.data = event->data; /* protected by mtx */ in ep_modify()
1575 if (epi->event.events & EPOLLWAKEUP) { in ep_modify()
1587 * event occurs immediately after we call f_op->poll(). in ep_modify()
1588 * We need this because we did not take ep->lock while in ep_modify()
1590 * ep->lock). in ep_modify()
1593 * when calling f_op->poll(). This barrier also in ep_modify()
1597 * This barrier will now guarantee ep_poll_callback or f_op->poll in ep_modify()
1609 write_lock_irq(&ep->lock); in ep_modify()
1611 list_add_tail(&epi->rdllink, &ep->rdllist); in ep_modify()
1615 if (waitqueue_active(&ep->wq)) in ep_modify()
1616 wake_up(&ep->wq); in ep_modify()
1617 if (waitqueue_active(&ep->poll_wait)) in ep_modify()
1620 write_unlock_irq(&ep->lock); in ep_modify()
1639 * Always short-circuit for fatal signals to allow threads to make a in ep_send_events()
1644 return -EINTR; in ep_send_events()
1648 mutex_lock(&ep->mtx); in ep_send_events()
1653 * Items cannot vanish during the loop we are holding ep->mtx. in ep_send_events()
1663 * Activate ep->ws before deactivating epi->ws to prevent in ep_send_events()
1664 * triggering auto-suspend here (in case we reactive epi->ws in ep_send_events()
1667 * This could be rearranged to delay the deactivation of epi->ws in ep_send_events()
1668 * instead, but then epi->ws would temporarily be out of sync in ep_send_events()
1673 if (ws->active) in ep_send_events()
1674 __pm_stay_awake(ep->ws); in ep_send_events()
1678 list_del_init(&epi->rdllink); in ep_send_events()
1681 * If the event mask intersect the caller-requested one, in ep_send_events()
1682 * deliver the event to userspace. Again, we are holding ep->mtx, in ep_send_events()
1689 events = epoll_put_uevent(revents, epi->event.data, events); in ep_send_events()
1691 list_add(&epi->rdllink, &txlist); in ep_send_events()
1694 res = -EFAULT; in ep_send_events()
1698 if (epi->event.events & EPOLLONESHOT) in ep_send_events()
1699 epi->event.events &= EP_PRIVATE_BITS; in ep_send_events()
1700 else if (!(epi->event.events & EPOLLET)) { in ep_send_events()
1707 * into ep->rdllist besides us. The epoll_ctl() in ep_send_events()
1710 * poll callback will queue them in ep->ovflist. in ep_send_events()
1712 list_add_tail(&epi->rdllink, &ep->rdllist); in ep_send_events()
1717 mutex_unlock(&ep->mtx); in ep_send_events()
1722 static struct timespec64 *ep_timeout_to_timespec(struct timespec64 *to, long ms) in ep_timeout_to_timespec() argument
1726 if (ms < 0) in ep_timeout_to_timespec()
1729 if (!ms) { in ep_timeout_to_timespec()
1730 to->tv_sec = 0; in ep_timeout_to_timespec()
1731 to->tv_nsec = 0; in ep_timeout_to_timespec()
1735 to->tv_sec = ms / MSEC_PER_SEC; in ep_timeout_to_timespec()
1736 to->tv_nsec = NSEC_PER_MSEC * (ms % MSEC_PER_SEC); in ep_timeout_to_timespec()
1744 * ep_poll - Retrieves ready events, and delivers them to the caller-supplied
1751 * @timeout: Maximum timeout for the ready events fetch operation, in
1752 * timespec. If the timeout is zero, the function will not block,
1753 * while if the @timeout ptr is NULL, the function will block
1761 int maxevents, struct timespec64 *timeout) in ep_poll() argument
1770 if (timeout && (timeout->tv_sec | timeout->tv_nsec)) { in ep_poll()
1771 slack = select_estimate_accuracy(timeout); in ep_poll()
1773 *to = timespec64_to_ktime(*timeout); in ep_poll()
1774 } else if (timeout) { in ep_poll()
1785 * with a non-zero timeout, this thread will check the ready list under in ep_poll()
1787 * timeout, the user by definition should not care and will have to in ep_poll()
1796 * 0 events and there's still timeout left over, we go in ep_poll()
1812 return -EINTR; in ep_poll()
1820 * lost. This is also good performance-wise, because on in ep_poll()
1822 * explicitly, thus ep->lock is not taken, which halts the in ep_poll()
1827 write_lock_irq(&ep->lock); in ep_poll()
1837 * plays with two lists (->rdllist and ->ovflist) and there in ep_poll()
1844 __add_wait_queue_exclusive(&ep->wq, &wait); in ep_poll()
1846 write_unlock_irq(&ep->lock); in ep_poll()
1861 write_lock_irq(&ep->lock); in ep_poll()
1865 * timeout expired before it could reacquire the lock. in ep_poll()
1871 __remove_wait_queue(&ep->wq, &wait); in ep_poll()
1872 write_unlock_irq(&ep->lock); in ep_poll()
1878 * ep_loop_check_proc - verify that adding an epoll file inside another
1887 * structure @ep does not violate the constraints, or %-1 otherwise.
1895 mutex_lock_nested(&ep->mtx, depth + 1); in ep_loop_check_proc()
1896 ep->gen = loop_check_gen; in ep_loop_check_proc()
1897 for (rbp = rb_first_cached(&ep->rbr); rbp; rbp = rb_next(rbp)) { in ep_loop_check_proc()
1899 if (unlikely(is_file_epoll(epi->ffd.file))) { in ep_loop_check_proc()
1901 ep_tovisit = epi->ffd.file->private_data; in ep_loop_check_proc()
1902 if (ep_tovisit->gen == loop_check_gen) in ep_loop_check_proc()
1905 error = -1; in ep_loop_check_proc()
1919 list_file(epi->ffd.file); in ep_loop_check_proc()
1922 mutex_unlock(&ep->mtx); in ep_loop_check_proc()
1928 * ep_loop_check - Performs a check to verify that adding an epoll file (@to)
1936 * does not violate the constraints, or %-1 otherwise.
1949 tfile_check_list = head->next; in clear_tfile_check_list()
1968 return -EINVAL; in do_epoll_create()
1990 ep->file = file; in do_epoll_create()
2009 return -EINVAL; in SYSCALL_DEFINE1()
2023 return -EAGAIN; in epoll_mutex_lock()
2036 error = -EBADF; in do_epoll_ctl()
2046 /* The target file descriptor must support poll */ in do_epoll_ctl()
2047 error = -EPERM; in do_epoll_ctl()
2060 error = -EINVAL; in do_epoll_ctl()
2069 if (ep_op_has_event(op) && (epds->events & EPOLLEXCLUSIVE)) { in do_epoll_ctl()
2073 (epds->events & ~EPOLLEXCLUSIVE_OK_BITS))) in do_epoll_ctl()
2081 ep = f.file->private_data; in do_epoll_ctl()
2098 error = epoll_mutex_lock(&ep->mtx, 0, nonblock); in do_epoll_ctl()
2102 if (READ_ONCE(f.file->f_ep) || ep->gen == loop_check_gen || in do_epoll_ctl()
2104 mutex_unlock(&ep->mtx); in do_epoll_ctl()
2111 tep = tf.file->private_data; in do_epoll_ctl()
2112 error = -ELOOP; in do_epoll_ctl()
2116 error = epoll_mutex_lock(&ep->mtx, 0, nonblock); in do_epoll_ctl()
2129 error = -EINVAL; in do_epoll_ctl()
2133 epds->events |= EPOLLERR | EPOLLHUP; in do_epoll_ctl()
2136 error = -EEXIST; in do_epoll_ctl()
2142 error = -ENOENT; in do_epoll_ctl()
2146 if (!(epi->event.events & EPOLLEXCLUSIVE)) { in do_epoll_ctl()
2147 epds->events |= EPOLLERR | EPOLLHUP; in do_epoll_ctl()
2151 error = -ENOENT; in do_epoll_ctl()
2154 mutex_unlock(&ep->mtx); in do_epoll_ctl()
2183 return -EFAULT; in SYSCALL_DEFINE4()
2201 return -EINVAL; in do_epoll_wait()
2205 return -EFAULT; in do_epoll_wait()
2210 return -EBADF; in do_epoll_wait()
2216 error = -EINVAL; in do_epoll_wait()
2224 ep = f.file->private_data; in do_epoll_wait()
2235 int, maxevents, int, timeout) in SYSCALL_DEFINE4() argument
2240 ep_timeout_to_timespec(&to, timeout)); in SYSCALL_DEFINE4()
2263 restore_saved_sigmask_unless(error == -EINTR); in do_epoll_pwait()
2269 int, maxevents, int, timeout, const sigset_t __user *, sigmask, in SYSCALL_DEFINE6() argument
2275 ep_timeout_to_timespec(&to, timeout), in SYSCALL_DEFINE6()
2280 int, maxevents, const struct __kernel_timespec __user *, timeout, in SYSCALL_DEFINE6() argument
2285 if (timeout) { in SYSCALL_DEFINE6()
2286 if (get_timespec64(&ts, timeout)) in SYSCALL_DEFINE6()
2287 return -EFAULT; in SYSCALL_DEFINE6()
2290 return -EINVAL; in SYSCALL_DEFINE6()
2299 int maxevents, struct timespec64 *timeout, in do_compat_epoll_pwait() argument
2313 err = do_epoll_wait(epfd, events, maxevents, timeout); in do_compat_epoll_pwait()
2315 restore_saved_sigmask_unless(err == -EINTR); in do_compat_epoll_pwait()
2322 int, maxevents, int, timeout, in COMPAT_SYSCALL_DEFINE6() argument
2329 ep_timeout_to_timespec(&to, timeout), in COMPAT_SYSCALL_DEFINE6()
2336 const struct __kernel_timespec __user *, timeout, in COMPAT_SYSCALL_DEFINE6() argument
2342 if (timeout) { in COMPAT_SYSCALL_DEFINE6()
2343 if (get_timespec64(&ts, timeout)) in COMPAT_SYSCALL_DEFINE6()
2344 return -EFAULT; in COMPAT_SYSCALL_DEFINE6()
2347 return -EINVAL; in COMPAT_SYSCALL_DEFINE6()
2364 max_user_watches = (((si.totalram - si.totalhigh) / 25) << PAGE_SHIFT) / in eventpoll_init()
2370 * using an extra cache line on 64-bit (and smaller) CPUs in eventpoll_init()