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,
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 */
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()
340 return f->f_op == &eventpoll_fops; in is_file_epoll()
347 ffd->file = file; in ep_set_ffd()
348 ffd->fd = fd; in ep_set_ffd()
355 return (p1->file > p2->file ? +1: in ep_cmp_ffd()
356 (p1->file < p2->file ? -1 : p1->fd - p2->fd)); in ep_cmp_ffd()
362 return !list_empty(&epi->rdllink); in ep_is_linked()
373 return container_of(p, struct eppoll_entry, wait)->base; in ep_item_from_wait()
377 * ep_events_available - Checks if ready events might be available.
386 return !list_empty_careful(&ep->rdllist) || in ep_events_available()
387 READ_ONCE(ep->ovflist) != EP_UNACTIVE_PTR; in ep_events_available()
399 * Busy poll if globally on and supporting sockets found && no events,
406 unsigned int napi_id = READ_ONCE(ep->napi_id); in ep_busy_loop()
414 * Busy poll timed out. Drop NAPI ID for now, we can add in ep_busy_loop()
418 ep->napi_id = 0; in ep_busy_loop()
425 * Set epoll busy poll NAPI ID from sk.
437 sock = sock_from_file(epi->ffd.file); in ep_set_busy_poll_napi_id()
441 sk = sock->sk; in ep_set_busy_poll_napi_id()
445 napi_id = READ_ONCE(sk->sk_napi_id); in ep_set_busy_poll_napi_id()
446 ep = epi->ep; in ep_set_busy_poll_napi_id()
448 /* Non-NAPI IDs can be rejected in ep_set_busy_poll_napi_id()
452 if (napi_id < MIN_NAPI_ID || napi_id == ep->napi_id) in ep_set_busy_poll_napi_id()
455 /* record NAPI ID for use in next busy poll */ in ep_set_busy_poll_napi_id()
456 ep->napi_id = napi_id; in ep_set_busy_poll_napi_id()
485 * issue a wake_up() on its poll wake list. Epoll (efd1) has installed a
489 * the waiters on its poll wait list (efd2). So it calls ep_poll_safewake()
508 * it might be natural to create a per-cpu nest count. However, since in ep_poll_safewake()
509 * we can recurse on ep->poll_wait.lock, and a non-raw spinlock can in ep_poll_safewake()
510 * schedule() in the -rt kernel, the per-cpu variable are no longer in ep_poll_safewake()
522 if ((is_file_epoll(epi->ffd.file))) { in ep_poll_safewake()
523 ep_src = epi->ffd.file->private_data; in ep_poll_safewake()
524 nests = ep_src->nests; in ep_poll_safewake()
529 spin_lock_irqsave_nested(&ep->poll_wait.lock, flags, nests); in ep_poll_safewake()
530 ep->nests = nests + 1; in ep_poll_safewake()
531 wake_up_locked_poll(&ep->poll_wait, EPOLLIN | pollflags); in ep_poll_safewake()
532 ep->nests = 0; in ep_poll_safewake()
533 spin_unlock_irqrestore(&ep->poll_wait.lock, flags); in ep_poll_safewake()
541 wake_up_poll(&ep->poll_wait, EPOLLIN | pollflags); in ep_poll_safewake()
552 * If it is cleared by POLLFREE, it should be rcu-safe. in ep_remove_wait_queue()
555 * we rely on whead->lock. in ep_remove_wait_queue()
557 whead = smp_load_acquire(&pwq->whead); in ep_remove_wait_queue()
559 remove_wait_queue(whead, &pwq->wait); in ep_remove_wait_queue()
564 * This function unregisters poll callbacks from the associated file
569 struct eppoll_entry **p = &epi->pwqlist; in ep_unregister_pollwait()
573 *p = pwq->next; in ep_unregister_pollwait()
579 /* call only when ep->mtx is held */
582 return rcu_dereference_check(epi->ws, lockdep_is_held(&epi->ep->mtx)); in ep_wakeup_source()
585 /* call only when ep->mtx is held */
596 return rcu_access_pointer(epi->ws) ? true : false; in ep_has_wakeup_source()
599 /* call when ep->mtx cannot be held (ep_poll_callback) */
605 ws = rcu_dereference(epi->ws); in ep_pm_stay_awake_rcu()
613 * ep->mutex needs to be held because we could be hit by
619 * Steal the ready list, and re-init the original one to the in ep_start_scan()
620 * empty list. Also, set ep->ovflist to NULL so that events in ep_start_scan()
622 * have the poll callback to queue directly on ep->rdllist, in ep_start_scan()
627 write_lock_irq(&ep->lock); in ep_start_scan()
628 list_splice_init(&ep->rdllist, txlist); in ep_start_scan()
629 WRITE_ONCE(ep->ovflist, NULL); in ep_start_scan()
630 write_unlock_irq(&ep->lock); in ep_start_scan()
638 write_lock_irq(&ep->lock); in ep_done_scan()
641 * other events might have been queued by the poll callback. in ep_done_scan()
642 * We re-insert them inside the main ready-list here. in ep_done_scan()
644 for (nepi = READ_ONCE(ep->ovflist); (epi = nepi) != NULL; in ep_done_scan()
645 nepi = epi->next, epi->next = EP_UNACTIVE_PTR) { in ep_done_scan()
649 * queued into ->ovflist but the "txlist" might already in ep_done_scan()
654 * ->ovflist is LIFO, so we have to reverse it in order in ep_done_scan()
657 list_add(&epi->rdllink, &ep->rdllist); in ep_done_scan()
662 * We need to set back ep->ovflist to EP_UNACTIVE_PTR, so that after in ep_done_scan()
664 * ep->rdllist. in ep_done_scan()
666 WRITE_ONCE(ep->ovflist, EP_UNACTIVE_PTR); in ep_done_scan()
669 * Quickly re-inject items left on "txlist". in ep_done_scan()
671 list_splice(txlist, &ep->rdllist); in ep_done_scan()
672 __pm_relax(ep->ws); in ep_done_scan()
674 if (!list_empty(&ep->rdllist)) { in ep_done_scan()
675 if (waitqueue_active(&ep->wq)) in ep_done_scan()
676 wake_up(&ep->wq); in ep_done_scan()
679 write_unlock_irq(&ep->lock); in ep_done_scan()
690 refcount_inc(&ep->refcount); in ep_get()
694 * Returns true if the event poll can be disposed
698 if (!refcount_dec_and_test(&ep->refcount)) in ep_refcount_dec_and_test()
701 WARN_ON_ONCE(!RB_EMPTY_ROOT(&ep->rbr.rb_root)); in ep_refcount_dec_and_test()
707 mutex_destroy(&ep->mtx); in ep_free()
708 free_uid(ep->user); in ep_free()
709 wakeup_source_unregister(ep->ws); in ep_free()
723 struct file *file = epi->ffd.file; in __ep_remove()
730 * Removes poll wait queue hooks. in __ep_remove()
735 spin_lock(&file->f_lock); in __ep_remove()
736 if (epi->dying && !force) { in __ep_remove()
737 spin_unlock(&file->f_lock); in __ep_remove()
742 head = file->f_ep; in __ep_remove()
743 if (head->first == &epi->fllink && !epi->fllink.next) { in __ep_remove()
745 WRITE_ONCE(file->f_ep, NULL); in __ep_remove()
749 if (!smp_load_acquire(&v->next)) in __ep_remove()
753 hlist_del_rcu(&epi->fllink); in __ep_remove()
754 spin_unlock(&file->f_lock); in __ep_remove()
757 rb_erase_cached(&epi->rbn, &ep->rbr); in __ep_remove()
759 write_lock_irq(&ep->lock); in __ep_remove()
761 list_del_init(&epi->rdllink); in __ep_remove()
762 write_unlock_irq(&ep->lock); in __ep_remove()
767 * field epi->rcu, since we are trying to minimize the size of in __ep_remove()
769 * ep->mtx. The rcu read side, reverse_path_check_proc(), does not make in __ep_remove()
772 call_rcu(&epi->rcu, epi_rcu_free); in __ep_remove()
774 percpu_counter_dec(&ep->user->epoll_watches); in __ep_remove()
793 if (waitqueue_active(&ep->poll_wait)) in ep_clear_and_put()
796 mutex_lock(&ep->mtx); in ep_clear_and_put()
799 * Walks through the whole tree by unregistering poll callbacks. in ep_clear_and_put()
801 for (rbp = rb_first_cached(&ep->rbr); rbp; rbp = rb_next(rbp)) { in ep_clear_and_put()
812 * At this point we are sure no poll callbacks will be lingering around. in ep_clear_and_put()
816 for (rbp = rb_first_cached(&ep->rbr); rbp; rbp = next) { in ep_clear_and_put()
823 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()
883 * been free'd and then gotten re-allocated to something else (since
884 * files are not RCU-delayed, they are SLAB_TYPESAFE_BY_RCU).
886 * But for epoll, users hold the ep->mtx mutex, and as such any file in
889 * file re-use cannot happen.
892 * operation - 'ffd.file' cannot go away even if the refcount has
893 * reached zero (but we must still not call out to ->poll() functions
900 file = epi->ffd.file; in epi_fget()
901 if (!atomic_long_inc_not_zero(&file->f_count)) in epi_fget()
908 * the ep->mtx so we need to start from depth=1, such that mutex_lock_nested()
919 * treat this more as "file doesn't exist, poll didn't happen". in ep_item_poll()
924 pt->_key = epi->event.events; in ep_item_poll()
930 return res & epi->event.events; in ep_item_poll()
941 struct eventpoll *ep = f->private_data; in ep_show_fdinfo()
944 mutex_lock(&ep->mtx); in ep_show_fdinfo()
945 for (rbp = rb_first_cached(&ep->rbr); rbp; rbp = rb_next(rbp)) { in ep_show_fdinfo()
947 struct inode *inode = file_inode(epi->ffd.file); in ep_show_fdinfo()
951 epi->ffd.fd, epi->event.events, in ep_show_fdinfo()
952 (long long)epi->event.data, in ep_show_fdinfo()
953 (long long)epi->ffd.file->f_pos, in ep_show_fdinfo()
954 inode->i_ino, inode->i_sb->s_dev); in ep_show_fdinfo()
958 mutex_unlock(&ep->mtx); in ep_show_fdinfo()
968 .poll = ep_eventpoll_poll,
986 * the ep->mtx. in eventpoll_release_file()
989 spin_lock(&file->f_lock); in eventpoll_release_file()
990 if (file->f_ep && file->f_ep->first) { in eventpoll_release_file()
991 epi = hlist_entry(file->f_ep->first, struct epitem, fllink); in eventpoll_release_file()
992 epi->dying = true; in eventpoll_release_file()
993 spin_unlock(&file->f_lock); in eventpoll_release_file()
999 ep = epi->ep; in eventpoll_release_file()
1000 mutex_lock(&ep->mtx); in eventpoll_release_file()
1002 mutex_unlock(&ep->mtx); in eventpoll_release_file()
1008 spin_unlock(&file->f_lock); in eventpoll_release_file()
1017 return -ENOMEM; in ep_alloc()
1019 mutex_init(&ep->mtx); in ep_alloc()
1020 rwlock_init(&ep->lock); in ep_alloc()
1021 init_waitqueue_head(&ep->wq); in ep_alloc()
1022 init_waitqueue_head(&ep->poll_wait); in ep_alloc()
1023 INIT_LIST_HEAD(&ep->rdllist); in ep_alloc()
1024 ep->rbr = RB_ROOT_CACHED; in ep_alloc()
1025 ep->ovflist = EP_UNACTIVE_PTR; in ep_alloc()
1026 ep->user = get_current_user(); in ep_alloc()
1027 refcount_set(&ep->refcount, 1); in ep_alloc()
1047 for (rbp = ep->rbr.rb_root.rb_node; rbp; ) { in ep_find()
1049 kcmp = ep_cmp_ffd(&ffd, &epi->ffd); in ep_find()
1051 rbp = rbp->rb_right; in ep_find()
1053 rbp = rbp->rb_left; in ep_find()
1069 for (rbp = rb_first_cached(&ep->rbr); rbp; rbp = rb_next(rbp)) { in ep_find_tfd()
1071 if (epi->ffd.fd == tfd) { in ep_find_tfd()
1075 toff--; in ep_find_tfd()
1091 return ERR_PTR(-EINVAL); in get_epoll_tfile_raw_ptr()
1093 ep = file->private_data; in get_epoll_tfile_raw_ptr()
1095 mutex_lock(&ep->mtx); in get_epoll_tfile_raw_ptr()
1098 file_raw = epi->ffd.file; in get_epoll_tfile_raw_ptr()
1100 file_raw = ERR_PTR(-ENOENT); in get_epoll_tfile_raw_ptr()
1101 mutex_unlock(&ep->mtx); in get_epoll_tfile_raw_ptr()
1131 * This is simple 'new->next = head' operation, but cmpxchg() in list_add_tail_lockless()
1134 * new->next == new. in list_add_tail_lockless()
1136 if (!try_cmpxchg(&new->next, &new, head)) in list_add_tail_lockless()
1140 * Initially ->next of a new element must be updated with the head in list_add_tail_lockless()
1142 * exchanged. XCHG guarantees memory ordering, thus ->next should be in list_add_tail_lockless()
1144 * swapped before prev->next is updated. in list_add_tail_lockless()
1147 prev = xchg(&head->prev, new); in list_add_tail_lockless()
1150 * It is safe to modify prev->next and new->prev, because a new element in list_add_tail_lockless()
1151 * is added only to the tail and new->next is updated before XCHG. in list_add_tail_lockless()
1154 prev->next = new; in list_add_tail_lockless()
1155 new->prev = prev; in list_add_tail_lockless()
1161 * Chains a new epi entry to the tail of the ep->ovflist in a lockless way,
1168 struct eventpoll *ep = epi->ep; in chain_epi_lockless()
1171 if (epi->next != EP_UNACTIVE_PTR) in chain_epi_lockless()
1175 if (cmpxchg(&epi->next, EP_UNACTIVE_PTR, NULL) != EP_UNACTIVE_PTR) in chain_epi_lockless()
1179 epi->next = xchg(&ep->ovflist, epi); in chain_epi_lockless()
1190 * events from another file descriptor, thus all modifications to ->rdllist
1191 * or ->ovflist are lockless. Read lock is paired with the write lock from
1196 * concurrently for the same @epi from different CPUs if poll table was inited
1206 struct eventpoll *ep = epi->ep; in ep_poll_callback()
1211 read_lock_irqsave(&ep->lock, flags); in ep_poll_callback()
1216 * If the event mask does not contain any poll(2) event, we consider the in ep_poll_callback()
1221 if (!(epi->event.events & ~EP_PRIVATE_BITS)) in ep_poll_callback()
1230 if (pollflags && !(pollflags & epi->event.events)) in ep_poll_callback()
1235 * (because we're accessing user memory, and because of linux f_op->poll() in ep_poll_callback()
1237 * chained in ep->ovflist and requeued later on. in ep_poll_callback()
1239 if (READ_ONCE(ep->ovflist) != EP_UNACTIVE_PTR) { in ep_poll_callback()
1244 if (list_add_tail_lockless(&epi->rdllink, &ep->rdllist)) in ep_poll_callback()
1249 * Wake up ( if active ) both the eventpoll wait list and the ->poll() in ep_poll_callback()
1252 if (waitqueue_active(&ep->wq)) { in ep_poll_callback()
1253 if ((epi->event.events & EPOLLEXCLUSIVE) && in ep_poll_callback()
1257 if (epi->event.events & EPOLLIN) in ep_poll_callback()
1261 if (epi->event.events & EPOLLOUT) in ep_poll_callback()
1270 wake_up_sync(&ep->wq); in ep_poll_callback()
1272 wake_up(&ep->wq); in ep_poll_callback()
1274 if (waitqueue_active(&ep->poll_wait)) in ep_poll_callback()
1278 read_unlock_irqrestore(&ep->lock, flags); in ep_poll_callback()
1284 if (!(epi->event.events & EPOLLEXCLUSIVE)) in ep_poll_callback()
1290 * ->whead = NULL and do another remove_wait_queue() after in ep_poll_callback()
1293 list_del_init(&wait->entry); in ep_poll_callback()
1295 * ->whead != NULL protects us from the race with in ep_poll_callback()
1297 * takes whead->lock held by the caller. Once we nullify it, in ep_poll_callback()
1300 smp_store_release(&ep_pwq_from_wait(wait)->whead, NULL); in ep_poll_callback()
1314 struct epitem *epi = epq->epi; in ep_ptable_queue_proc()
1322 epq->epi = NULL; in ep_ptable_queue_proc()
1326 init_waitqueue_func_entry(&pwq->wait, ep_poll_callback); in ep_ptable_queue_proc()
1327 pwq->whead = whead; in ep_ptable_queue_proc()
1328 pwq->base = epi; in ep_ptable_queue_proc()
1329 if (epi->event.events & EPOLLEXCLUSIVE) in ep_ptable_queue_proc()
1330 add_wait_queue_exclusive(whead, &pwq->wait); in ep_ptable_queue_proc()
1332 add_wait_queue(whead, &pwq->wait); in ep_ptable_queue_proc()
1333 pwq->next = epi->pwqlist; in ep_ptable_queue_proc()
1334 epi->pwqlist = pwq; in ep_ptable_queue_proc()
1340 struct rb_node **p = &ep->rbr.rb_root.rb_node, *parent = NULL; in ep_rbtree_insert()
1347 kcmp = ep_cmp_ffd(&epi->ffd, &epic->ffd); in ep_rbtree_insert()
1349 p = &parent->rb_right; in ep_rbtree_insert()
1352 p = &parent->rb_left; in ep_rbtree_insert()
1354 rb_link_node(&epi->rbn, parent, p); in ep_rbtree_insert()
1355 rb_insert_color_cached(&epi->rbn, &ep->rbr, leftmost); in ep_rbtree_insert()
1382 return -1; in path_count_inc()
1400 return -1; in reverse_path_check_proc()
1404 struct hlist_head *refs = &epi->ep->refs; in reverse_path_check_proc()
1416 * reverse_path_check - The tfile_check_list is list of epitem_head, which have
1423 * %-1 otherwise.
1429 for (p = tfile_check_list; p != EP_UNACTIVE_PTR; p = p->next) { in reverse_path_check()
1433 error = reverse_path_check_proc(&p->epitems, 0); in reverse_path_check()
1446 if (!epi->ep->ws) { in ep_create_wakeup_source()
1447 epi->ep->ws = wakeup_source_register(NULL, "eventpoll"); in ep_create_wakeup_source()
1448 if (!epi->ep->ws) in ep_create_wakeup_source()
1449 return -ENOMEM; in ep_create_wakeup_source()
1452 take_dentry_name_snapshot(&n, epi->ffd.file->f_path.dentry); in ep_create_wakeup_source()
1457 return -ENOMEM; in ep_create_wakeup_source()
1458 rcu_assign_pointer(epi->ws, ws); in ep_create_wakeup_source()
1468 RCU_INIT_POINTER(epi->ws, NULL); in ep_destroy_wakeup_source()
1486 ep = file->private_data; in attach_epitem()
1489 head = &ep->refs; in attach_epitem()
1490 } else if (!READ_ONCE(file->f_ep)) { in attach_epitem()
1494 return -ENOMEM; in attach_epitem()
1495 head = &to_free->epitems; in attach_epitem()
1497 spin_lock(&file->f_lock); in attach_epitem()
1498 if (!file->f_ep) { in attach_epitem()
1500 spin_unlock(&file->f_lock); in attach_epitem()
1504 WRITE_ONCE(file->f_ep, head); in attach_epitem()
1507 hlist_add_head_rcu(&epi->fllink, file->f_ep); in attach_epitem()
1508 spin_unlock(&file->f_lock); in attach_epitem()
1526 tep = tfile->private_data; in ep_insert()
1530 if (unlikely(percpu_counter_compare(&ep->user->epoll_watches, in ep_insert()
1532 return -ENOSPC; in ep_insert()
1533 percpu_counter_inc(&ep->user->epoll_watches); in ep_insert()
1536 percpu_counter_dec(&ep->user->epoll_watches); in ep_insert()
1537 return -ENOMEM; in ep_insert()
1541 INIT_LIST_HEAD(&epi->rdllink); in ep_insert()
1542 epi->ep = ep; in ep_insert()
1543 ep_set_ffd(&epi->ffd, tfile, fd); in ep_insert()
1544 epi->event = *event; in ep_insert()
1545 epi->next = EP_UNACTIVE_PTR; in ep_insert()
1548 mutex_lock_nested(&tep->mtx, 1); in ep_insert()
1552 mutex_unlock(&tep->mtx); in ep_insert()
1554 percpu_counter_dec(&ep->user->epoll_watches); in ep_insert()
1555 return -ENOMEM; in ep_insert()
1567 mutex_unlock(&tep->mtx); in ep_insert()
1578 return -EINVAL; in ep_insert()
1581 if (epi->event.events & EPOLLWAKEUP) { in ep_insert()
1589 /* Initialize the poll table using the queue callback */ in ep_insert()
1594 * Attach the item to the poll hooks and get current event bits. in ep_insert()
1597 * this operation completes, the poll callback can start hitting in ep_insert()
1603 * We have to check if something went wrong during the poll wait queue in ep_insert()
1609 return -ENOMEM; in ep_insert()
1613 write_lock_irq(&ep->lock); in ep_insert()
1620 list_add_tail(&epi->rdllink, &ep->rdllist); in ep_insert()
1624 if (waitqueue_active(&ep->wq)) in ep_insert()
1625 wake_up(&ep->wq); in ep_insert()
1626 if (waitqueue_active(&ep->poll_wait)) in ep_insert()
1630 write_unlock_irq(&ep->lock); in ep_insert()
1654 * Set the new event interest mask before calling f_op->poll(); in ep_modify()
1656 * f_op->poll() call and the new event set registering. in ep_modify()
1658 epi->event.events = event->events; /* need barrier below */ in ep_modify()
1659 epi->event.data = event->data; /* protected by mtx */ in ep_modify()
1660 if (epi->event.events & EPOLLWAKEUP) { in ep_modify()
1672 * event occurs immediately after we call f_op->poll(). in ep_modify()
1673 * We need this because we did not take ep->lock while in ep_modify()
1675 * ep->lock). in ep_modify()
1678 * when calling f_op->poll(). This barrier also in ep_modify()
1682 * This barrier will now guarantee ep_poll_callback or f_op->poll in ep_modify()
1694 write_lock_irq(&ep->lock); in ep_modify()
1696 list_add_tail(&epi->rdllink, &ep->rdllist); in ep_modify()
1700 if (waitqueue_active(&ep->wq)) in ep_modify()
1701 wake_up(&ep->wq); in ep_modify()
1702 if (waitqueue_active(&ep->poll_wait)) in ep_modify()
1705 write_unlock_irq(&ep->lock); in ep_modify()
1724 * Always short-circuit for fatal signals to allow threads to make a in ep_send_events()
1729 return -EINTR; in ep_send_events()
1733 mutex_lock(&ep->mtx); in ep_send_events()
1738 * Items cannot vanish during the loop we are holding ep->mtx. in ep_send_events()
1748 * Activate ep->ws before deactivating epi->ws to prevent in ep_send_events()
1749 * triggering auto-suspend here (in case we reactive epi->ws in ep_send_events()
1752 * This could be rearranged to delay the deactivation of epi->ws in ep_send_events()
1753 * instead, but then epi->ws would temporarily be out of sync in ep_send_events()
1758 if (ws->active) in ep_send_events()
1759 __pm_stay_awake(ep->ws); in ep_send_events()
1763 list_del_init(&epi->rdllink); in ep_send_events()
1766 * If the event mask intersect the caller-requested one, in ep_send_events()
1767 * deliver the event to userspace. Again, we are holding ep->mtx, in ep_send_events()
1774 events = epoll_put_uevent(revents, epi->event.data, events); in ep_send_events()
1776 list_add(&epi->rdllink, &txlist); in ep_send_events()
1779 res = -EFAULT; in ep_send_events()
1783 if (epi->event.events & EPOLLONESHOT) in ep_send_events()
1784 epi->event.events &= EP_PRIVATE_BITS; in ep_send_events()
1785 else if (!(epi->event.events & EPOLLET)) { in ep_send_events()
1792 * into ep->rdllist besides us. The epoll_ctl() in ep_send_events()
1795 * poll callback will queue them in ep->ovflist. in ep_send_events()
1797 list_add_tail(&epi->rdllink, &ep->rdllist); in ep_send_events()
1802 mutex_unlock(&ep->mtx); in ep_send_events()
1807 static struct timespec64 *ep_timeout_to_timespec(struct timespec64 *to, long ms) in ep_timeout_to_timespec() argument
1811 if (ms < 0) in ep_timeout_to_timespec()
1814 if (!ms) { in ep_timeout_to_timespec()
1815 to->tv_sec = 0; in ep_timeout_to_timespec()
1816 to->tv_nsec = 0; in ep_timeout_to_timespec()
1820 to->tv_sec = ms / MSEC_PER_SEC; in ep_timeout_to_timespec()
1821 to->tv_nsec = NSEC_PER_MSEC * (ms % MSEC_PER_SEC); in ep_timeout_to_timespec()
1843 list_del_init_careful(&wq_entry->entry); in ep_autoremove_wake_function()
1848 * ep_poll - Retrieves ready events, and delivers them to the caller-supplied
1855 * @timeout: Maximum timeout for the ready events fetch operation, in
1856 * timespec. If the timeout is zero, the function will not block,
1857 * while if the @timeout ptr is NULL, the function will block
1865 int maxevents, struct timespec64 *timeout) in ep_poll() argument
1874 if (timeout && (timeout->tv_sec | timeout->tv_nsec)) { in ep_poll()
1875 slack = select_estimate_accuracy(timeout); in ep_poll()
1877 *to = timespec64_to_ktime(*timeout); in ep_poll()
1878 } else if (timeout) { in ep_poll()
1889 * with a non-zero timeout, this thread will check the ready list under in ep_poll()
1891 * timeout, the user by definition should not care and will have to in ep_poll()
1900 * 0 events and there's still timeout left over, we go in ep_poll()
1916 return -EINTR; in ep_poll()
1924 * lost. This is also good performance-wise, because on in ep_poll()
1926 * explicitly, thus ep->lock is not taken, which halts the in ep_poll()
1938 write_lock_irq(&ep->lock); in ep_poll()
1948 * plays with two lists (->rdllist and ->ovflist) and there in ep_poll()
1955 __add_wait_queue_exclusive(&ep->wq, &wait); in ep_poll()
1957 write_unlock_irq(&ep->lock); in ep_poll()
1972 write_lock_irq(&ep->lock); in ep_poll()
1976 * timeout expired before it could reacquire the lock. in ep_poll()
1982 __remove_wait_queue(&ep->wq, &wait); in ep_poll()
1983 write_unlock_irq(&ep->lock); in ep_poll()
1989 * ep_loop_check_proc - verify that adding an epoll file inside another
1998 * structure @ep does not violate the constraints, or %-1 otherwise.
2006 mutex_lock_nested(&ep->mtx, depth + 1); in ep_loop_check_proc()
2007 ep->gen = loop_check_gen; in ep_loop_check_proc()
2008 for (rbp = rb_first_cached(&ep->rbr); rbp; rbp = rb_next(rbp)) { in ep_loop_check_proc()
2010 if (unlikely(is_file_epoll(epi->ffd.file))) { in ep_loop_check_proc()
2012 ep_tovisit = epi->ffd.file->private_data; in ep_loop_check_proc()
2013 if (ep_tovisit->gen == loop_check_gen) in ep_loop_check_proc()
2016 error = -1; in ep_loop_check_proc()
2030 list_file(epi->ffd.file); in ep_loop_check_proc()
2033 mutex_unlock(&ep->mtx); in ep_loop_check_proc()
2039 * ep_loop_check - Performs a check to verify that adding an epoll file (@to)
2047 * does not violate the constraints, or %-1 otherwise.
2060 tfile_check_list = head->next; in clear_tfile_check_list()
2079 return -EINVAL; in do_epoll_create()
2101 ep->file = file; in do_epoll_create()
2120 return -EINVAL; in SYSCALL_DEFINE1()
2128 if ((epev->events & EPOLLWAKEUP) && !capable(CAP_BLOCK_SUSPEND)) in ep_take_care_of_epollwakeup()
2129 epev->events &= ~EPOLLWAKEUP; in ep_take_care_of_epollwakeup()
2134 epev->events &= ~EPOLLWAKEUP; in ep_take_care_of_epollwakeup()
2147 return -EAGAIN; in epoll_mutex_lock()
2160 error = -EBADF; in do_epoll_ctl()
2170 /* The target file descriptor must support poll */ in do_epoll_ctl()
2171 error = -EPERM; in do_epoll_ctl()
2184 error = -EINVAL; in do_epoll_ctl()
2193 if (ep_op_has_event(op) && (epds->events & EPOLLEXCLUSIVE)) { in do_epoll_ctl()
2197 (epds->events & ~EPOLLEXCLUSIVE_OK_BITS))) in do_epoll_ctl()
2205 ep = f.file->private_data; in do_epoll_ctl()
2222 error = epoll_mutex_lock(&ep->mtx, 0, nonblock); in do_epoll_ctl()
2226 if (READ_ONCE(f.file->f_ep) || ep->gen == loop_check_gen || in do_epoll_ctl()
2228 mutex_unlock(&ep->mtx); in do_epoll_ctl()
2235 tep = tf.file->private_data; in do_epoll_ctl()
2236 error = -ELOOP; in do_epoll_ctl()
2240 error = epoll_mutex_lock(&ep->mtx, 0, nonblock); in do_epoll_ctl()
2253 error = -EINVAL; in do_epoll_ctl()
2257 epds->events |= EPOLLERR | EPOLLHUP; in do_epoll_ctl()
2260 error = -EEXIST; in do_epoll_ctl()
2271 error = -ENOENT; in do_epoll_ctl()
2276 if (!(epi->event.events & EPOLLEXCLUSIVE)) { in do_epoll_ctl()
2277 epds->events |= EPOLLERR | EPOLLHUP; in do_epoll_ctl()
2281 error = -ENOENT; in do_epoll_ctl()
2284 mutex_unlock(&ep->mtx); in do_epoll_ctl()
2313 return -EFAULT; in SYSCALL_DEFINE4()
2331 return -EINVAL; in do_epoll_wait()
2335 return -EFAULT; in do_epoll_wait()
2340 return -EBADF; in do_epoll_wait()
2346 error = -EINVAL; in do_epoll_wait()
2354 ep = f.file->private_data; in do_epoll_wait()
2365 int, maxevents, int, timeout) in SYSCALL_DEFINE4() argument
2370 ep_timeout_to_timespec(&to, timeout)); in SYSCALL_DEFINE4()
2393 restore_saved_sigmask_unless(error == -EINTR); in do_epoll_pwait()
2399 int, maxevents, int, timeout, const sigset_t __user *, sigmask, in SYSCALL_DEFINE6() argument
2405 ep_timeout_to_timespec(&to, timeout), in SYSCALL_DEFINE6()
2410 int, maxevents, const struct __kernel_timespec __user *, timeout, in SYSCALL_DEFINE6() argument
2415 if (timeout) { in SYSCALL_DEFINE6()
2416 if (get_timespec64(&ts, timeout)) in SYSCALL_DEFINE6()
2417 return -EFAULT; in SYSCALL_DEFINE6()
2420 return -EINVAL; in SYSCALL_DEFINE6()
2429 int maxevents, struct timespec64 *timeout, in do_compat_epoll_pwait() argument
2443 err = do_epoll_wait(epfd, events, maxevents, timeout); in do_compat_epoll_pwait()
2445 restore_saved_sigmask_unless(err == -EINTR); in do_compat_epoll_pwait()
2452 int, maxevents, int, timeout, in COMPAT_SYSCALL_DEFINE6() argument
2459 ep_timeout_to_timespec(&to, timeout), in COMPAT_SYSCALL_DEFINE6()
2466 const struct __kernel_timespec __user *, timeout, in COMPAT_SYSCALL_DEFINE6() argument
2472 if (timeout) { in COMPAT_SYSCALL_DEFINE6()
2473 if (get_timespec64(&ts, timeout)) in COMPAT_SYSCALL_DEFINE6()
2474 return -EFAULT; in COMPAT_SYSCALL_DEFINE6()
2477 return -EINVAL; in COMPAT_SYSCALL_DEFINE6()
2494 max_user_watches = (((si.totalram - si.totalhigh) / 25) << PAGE_SHIFT) / in eventpoll_init()
2500 * using an extra cache line on 64-bit (and smaller) CPUs in eventpoll_init()