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>
48 * 2) ep->mtx (mutex)
49 * 3) ep->lock (rwlock)
52 * We need a rwlock (ep->lock) because we manipulate objects
53 * from inside the poll callback, that might be triggered from
55 * So we can't sleep inside the poll callback and hence we need
59 * mutex (ep->mtx). It is acquired during the event transfer loop,
68 * It is necessary to acquire multiple "ep->mtx"es at once in the
71 * epoll_ctl(e1, EPOLL_CTL_ADD, e2), e1->mtx will always be acquired
72 * before e2->mtx). Since we disallow cycles of epoll file
73 * descriptors, this ensures that the mutexes are well-ordered. In
77 * It is possible to drop the "ep->mtx" and to use the global
78 * mutex "epnested_mutex" (together with "ep->lock") to have it working,
79 * but having "ep->mtx" will make the interface more scalable.
81 * normal operations the epoll private "ep->mtx" will guarantee
98 #define EP_UNACTIVE_PTR ((void *) -1L)
107 /* Wait structure used by the poll hooks */
129 * of these on a server and we do not want this to take another cache line.
143 * Works together "struct eventpoll"->ovflist in keeping the
152 * Protected by file->f_lock, true for to-be-released epitem already
154 * eventpoll->refcount orchestrates "struct eventpoll" disposal
158 /* List containing poll wait queues */
191 /* Wait queue used by file->poll() */
206 * holding ->lock.
223 * usage count, used together with epitem->dying to
229 /* used to track busy poll napi_id */
231 /* busy poll timeout */
233 /* busy poll packet budget */
244 /* Wrapper struct used by poll queueing */
292 head = container_of(file->f_ep, struct epitems_head, epitems); in list_file()
293 if (!head->next) { in list_file()
294 head->next = tfile_check_list; in list_file()
302 struct hlist_node *p = rcu_dereference(hlist_first_rcu(&head->epitems)); in unlist_file()
305 spin_lock(&epi->ffd.file->f_lock); in unlist_file()
306 if (!hlist_empty(&head->epitems)) in unlist_file()
308 head->next = NULL; in unlist_file()
309 spin_unlock(&epi->ffd.file->f_lock); in unlist_file()
345 return f->f_op == &eventpoll_fops; in is_file_epoll()
352 ffd->file = file; in ep_set_ffd()
353 ffd->fd = fd; in ep_set_ffd()
360 return (p1->file > p2->file ? +1: in ep_cmp_ffd()
361 (p1->file < p2->file ? -1 : p1->fd - p2->fd)); in ep_cmp_ffd()
367 return !list_empty(&epi->rdllink); in ep_is_linked()
378 return container_of(p, struct eppoll_entry, wait)->base; in ep_item_from_wait()
382 * ep_events_available - Checks if ready events might be available.
391 return !list_empty_careful(&ep->rdllist) || in ep_events_available()
392 READ_ONCE(ep->ovflist) != EP_UNACTIVE_PTR; in ep_events_available()
397 * busy_loop_ep_timeout - check if busy poll has timed out. The timeout value
399 * the system-wide global via busy_loop_timeout.
409 unsigned long bp_usec = READ_ONCE(ep->busy_poll_usecs); in busy_loop_ep_timeout()
423 return !!READ_ONCE(ep->busy_poll_usecs) || net_busy_loop_on(); in ep_busy_loop_on()
434 * Busy poll if globally on and supporting sockets found && no events,
441 unsigned int napi_id = READ_ONCE(ep->napi_id); in ep_busy_loop()
442 u16 budget = READ_ONCE(ep->busy_poll_budget); in ep_busy_loop()
443 bool prefer_busy_poll = READ_ONCE(ep->prefer_busy_poll); in ep_busy_loop()
454 * Busy poll timed out. Drop NAPI ID for now, we can add in ep_busy_loop()
458 ep->napi_id = 0; in ep_busy_loop()
465 * Set epoll busy poll NAPI ID from sk.
469 struct eventpoll *ep = epi->ep; in ep_set_busy_poll_napi_id()
477 sock = sock_from_file(epi->ffd.file); in ep_set_busy_poll_napi_id()
481 sk = sock->sk; in ep_set_busy_poll_napi_id()
485 napi_id = READ_ONCE(sk->sk_napi_id); in ep_set_busy_poll_napi_id()
487 /* Non-NAPI IDs can be rejected in ep_set_busy_poll_napi_id()
491 if (napi_id < MIN_NAPI_ID || napi_id == ep->napi_id) in ep_set_busy_poll_napi_id()
494 /* record NAPI ID for use in next busy poll */ in ep_set_busy_poll_napi_id()
495 ep->napi_id = napi_id; in ep_set_busy_poll_napi_id()
501 struct eventpoll *ep = file->private_data; in ep_eventpoll_bp_ioctl()
508 return -EFAULT; in ep_eventpoll_bp_ioctl()
512 return -EINVAL; in ep_eventpoll_bp_ioctl()
515 return -EINVAL; in ep_eventpoll_bp_ioctl()
518 return -EINVAL; in ep_eventpoll_bp_ioctl()
522 return -EPERM; in ep_eventpoll_bp_ioctl()
524 WRITE_ONCE(ep->busy_poll_usecs, epoll_params.busy_poll_usecs); in ep_eventpoll_bp_ioctl()
525 WRITE_ONCE(ep->busy_poll_budget, epoll_params.busy_poll_budget); in ep_eventpoll_bp_ioctl()
526 WRITE_ONCE(ep->prefer_busy_poll, epoll_params.prefer_busy_poll); in ep_eventpoll_bp_ioctl()
530 epoll_params.busy_poll_usecs = READ_ONCE(ep->busy_poll_usecs); in ep_eventpoll_bp_ioctl()
531 epoll_params.busy_poll_budget = READ_ONCE(ep->busy_poll_budget); in ep_eventpoll_bp_ioctl()
532 epoll_params.prefer_busy_poll = READ_ONCE(ep->prefer_busy_poll); in ep_eventpoll_bp_ioctl()
534 return -EFAULT; in ep_eventpoll_bp_ioctl()
537 return -ENOIOCTLCMD; in ep_eventpoll_bp_ioctl()
555 return -EOPNOTSUPP; in ep_eventpoll_bp_ioctl()
573 * issue a wake_up() on its poll wake list. Epoll (efd1) has installed a
574 * callback wakeup entry on that queue, and the wake_up() performed by the
577 * the waiters on its poll wait list (efd2). So it calls ep_poll_safewake()
579 * recursion constraints. That are, no more than EP_MAX_NESTS, to avoid
596 * it might be natural to create a per-cpu nest count. However, since in ep_poll_safewake()
597 * we can recurse on ep->poll_wait.lock, and a non-raw spinlock can in ep_poll_safewake()
598 * schedule() in the -rt kernel, the per-cpu variable are no longer in ep_poll_safewake()
610 if ((is_file_epoll(epi->ffd.file))) { in ep_poll_safewake()
611 ep_src = epi->ffd.file->private_data; in ep_poll_safewake()
612 nests = ep_src->nests; in ep_poll_safewake()
617 spin_lock_irqsave_nested(&ep->poll_wait.lock, flags, nests); in ep_poll_safewake()
618 ep->nests = nests + 1; in ep_poll_safewake()
619 wake_up_locked_poll(&ep->poll_wait, EPOLLIN | pollflags); in ep_poll_safewake()
620 ep->nests = 0; in ep_poll_safewake()
621 spin_unlock_irqrestore(&ep->poll_wait.lock, flags); in ep_poll_safewake()
629 wake_up_poll(&ep->poll_wait, EPOLLIN | pollflags); in ep_poll_safewake()
640 * If it is cleared by POLLFREE, it should be rcu-safe. in ep_remove_wait_queue()
643 * we rely on whead->lock. in ep_remove_wait_queue()
645 whead = smp_load_acquire(&pwq->whead); in ep_remove_wait_queue()
647 remove_wait_queue(whead, &pwq->wait); in ep_remove_wait_queue()
652 * This function unregisters poll callbacks from the associated file
657 struct eppoll_entry **p = &epi->pwqlist; in ep_unregister_pollwait()
661 *p = pwq->next; in ep_unregister_pollwait()
667 /* call only when ep->mtx is held */
670 return rcu_dereference_check(epi->ws, lockdep_is_held(&epi->ep->mtx)); in ep_wakeup_source()
673 /* call only when ep->mtx is held */
684 return rcu_access_pointer(epi->ws) ? true : false; in ep_has_wakeup_source()
687 /* call when ep->mtx cannot be held (ep_poll_callback) */
693 ws = rcu_dereference(epi->ws); in ep_pm_stay_awake_rcu()
701 * ep->mutex needs to be held because we could be hit by
707 * Steal the ready list, and re-init the original one to the in ep_start_scan()
708 * empty list. Also, set ep->ovflist to NULL so that events in ep_start_scan()
710 * have the poll callback to queue directly on ep->rdllist, in ep_start_scan()
715 write_lock_irq(&ep->lock); in ep_start_scan()
716 list_splice_init(&ep->rdllist, txlist); in ep_start_scan()
717 WRITE_ONCE(ep->ovflist, NULL); in ep_start_scan()
718 write_unlock_irq(&ep->lock); in ep_start_scan()
726 write_lock_irq(&ep->lock); in ep_done_scan()
729 * other events might have been queued by the poll callback. in ep_done_scan()
730 * We re-insert them inside the main ready-list here. in ep_done_scan()
732 for (nepi = READ_ONCE(ep->ovflist); (epi = nepi) != NULL; in ep_done_scan()
733 nepi = epi->next, epi->next = EP_UNACTIVE_PTR) { in ep_done_scan()
737 * queued into ->ovflist but the "txlist" might already in ep_done_scan()
742 * ->ovflist is LIFO, so we have to reverse it in order in ep_done_scan()
745 list_add(&epi->rdllink, &ep->rdllist); in ep_done_scan()
750 * We need to set back ep->ovflist to EP_UNACTIVE_PTR, so that after in ep_done_scan()
752 * ep->rdllist. in ep_done_scan()
754 WRITE_ONCE(ep->ovflist, EP_UNACTIVE_PTR); in ep_done_scan()
757 * Quickly re-inject items left on "txlist". in ep_done_scan()
759 list_splice(txlist, &ep->rdllist); in ep_done_scan()
760 __pm_relax(ep->ws); in ep_done_scan()
762 if (!list_empty(&ep->rdllist)) { in ep_done_scan()
763 if (waitqueue_active(&ep->wq)) in ep_done_scan()
764 wake_up(&ep->wq); in ep_done_scan()
767 write_unlock_irq(&ep->lock); in ep_done_scan()
772 refcount_inc(&ep->refcount); in ep_get()
776 * Returns true if the event poll can be disposed
780 if (!refcount_dec_and_test(&ep->refcount)) in ep_refcount_dec_and_test()
783 WARN_ON_ONCE(!RB_EMPTY_ROOT(&ep->rbr.rb_root)); in ep_refcount_dec_and_test()
789 mutex_destroy(&ep->mtx); in ep_free()
790 free_uid(ep->user); in ep_free()
791 wakeup_source_unregister(ep->ws); in ep_free()
805 struct file *file = epi->ffd.file; in __ep_remove()
812 * Removes poll wait queue hooks. in __ep_remove()
817 spin_lock(&file->f_lock); in __ep_remove()
818 if (epi->dying && !force) { in __ep_remove()
819 spin_unlock(&file->f_lock); in __ep_remove()
824 head = file->f_ep; in __ep_remove()
825 if (head->first == &epi->fllink && !epi->fllink.next) { in __ep_remove()
826 file->f_ep = NULL; in __ep_remove()
830 if (!smp_load_acquire(&v->next)) in __ep_remove()
834 hlist_del_rcu(&epi->fllink); in __ep_remove()
835 spin_unlock(&file->f_lock); in __ep_remove()
838 rb_erase_cached(&epi->rbn, &ep->rbr); in __ep_remove()
840 write_lock_irq(&ep->lock); in __ep_remove()
842 list_del_init(&epi->rdllink); in __ep_remove()
843 write_unlock_irq(&ep->lock); in __ep_remove()
848 * field epi->rcu, since we are trying to minimize the size of in __ep_remove()
849 * 'struct epitem'. The 'rbn' field is no longer in use. Protected by in __ep_remove()
850 * ep->mtx. The rcu read side, reverse_path_check_proc(), does not make in __ep_remove()
855 percpu_counter_dec(&ep->user->epoll_watches); in __ep_remove()
874 if (waitqueue_active(&ep->poll_wait)) in ep_clear_and_put()
877 mutex_lock(&ep->mtx); in ep_clear_and_put()
880 * Walks through the whole tree by unregistering poll callbacks. in ep_clear_and_put()
882 for (rbp = rb_first_cached(&ep->rbr); rbp; rbp = rb_next(rbp)) { in ep_clear_and_put()
893 * At this point we are sure no poll callbacks will be lingering around. in ep_clear_and_put()
897 for (rbp = rb_first_cached(&ep->rbr); rbp; rbp = next) { in ep_clear_and_put()
905 mutex_unlock(&ep->mtx); in ep_clear_and_put()
917 return -EINVAL; in ep_eventpoll_ioctl()
925 ret = -EINVAL; in ep_eventpoll_ioctl()
934 struct eventpoll *ep = file->private_data; in ep_eventpoll_release()
946 struct eventpoll *ep = file->private_data; in __ep_eventpoll_poll()
954 /* Insert inside our poll wait queue */ in __ep_eventpoll_poll()
955 poll_wait(file, &ep->poll_wait, wait); in __ep_eventpoll_poll()
961 mutex_lock_nested(&ep->mtx, depth); in __ep_eventpoll_poll()
969 * Item has been dropped into the ready list by the poll in __ep_eventpoll_poll()
974 list_del_init(&epi->rdllink); in __ep_eventpoll_poll()
978 mutex_unlock(&ep->mtx); in __ep_eventpoll_poll()
987 * been free'd and then gotten re-allocated to something else (since
988 * files are not RCU-delayed, they are SLAB_TYPESAFE_BY_RCU).
990 * But for epoll, users hold the ep->mtx mutex, and as such any file in
993 * file re-use cannot happen.
996 * operation - 'ffd.file' cannot go away even if the refcount has
997 * reached zero (but we must still not call out to ->poll() functions
1004 file = epi->ffd.file; in epi_fget()
1005 if (!atomic_long_inc_not_zero(&file->f_count)) in epi_fget()
1012 * the ep->mtx so we need to start from depth=1, such that mutex_lock_nested()
1023 * treat this more as "file doesn't exist, poll didn't happen". in ep_item_poll()
1028 pt->_key = epi->event.events; in ep_item_poll()
1034 return res & epi->event.events; in ep_item_poll()
1045 struct eventpoll *ep = f->private_data; in ep_show_fdinfo()
1048 mutex_lock(&ep->mtx); in ep_show_fdinfo()
1049 for (rbp = rb_first_cached(&ep->rbr); rbp; rbp = rb_next(rbp)) { in ep_show_fdinfo()
1051 struct inode *inode = file_inode(epi->ffd.file); in ep_show_fdinfo()
1055 epi->ffd.fd, epi->event.events, in ep_show_fdinfo()
1056 (long long)epi->event.data, in ep_show_fdinfo()
1057 (long long)epi->ffd.file->f_pos, in ep_show_fdinfo()
1058 inode->i_ino, inode->i_sb->s_dev); in ep_show_fdinfo()
1062 mutex_unlock(&ep->mtx); in ep_show_fdinfo()
1072 .poll = ep_eventpoll_poll,
1092 * the ep->mtx. in eventpoll_release_file()
1095 spin_lock(&file->f_lock); in eventpoll_release_file()
1096 if (file->f_ep && file->f_ep->first) { in eventpoll_release_file()
1097 epi = hlist_entry(file->f_ep->first, struct epitem, fllink); in eventpoll_release_file()
1098 epi->dying = true; in eventpoll_release_file()
1099 spin_unlock(&file->f_lock); in eventpoll_release_file()
1105 ep = epi->ep; in eventpoll_release_file()
1106 mutex_lock(&ep->mtx); in eventpoll_release_file()
1108 mutex_unlock(&ep->mtx); in eventpoll_release_file()
1114 spin_unlock(&file->f_lock); in eventpoll_release_file()
1123 return -ENOMEM; in ep_alloc()
1125 mutex_init(&ep->mtx); in ep_alloc()
1126 rwlock_init(&ep->lock); in ep_alloc()
1127 init_waitqueue_head(&ep->wq); in ep_alloc()
1128 init_waitqueue_head(&ep->poll_wait); in ep_alloc()
1129 INIT_LIST_HEAD(&ep->rdllist); in ep_alloc()
1130 ep->rbr = RB_ROOT_CACHED; in ep_alloc()
1131 ep->ovflist = EP_UNACTIVE_PTR; in ep_alloc()
1132 ep->user = get_current_user(); in ep_alloc()
1133 refcount_set(&ep->refcount, 1); in ep_alloc()
1153 for (rbp = ep->rbr.rb_root.rb_node; rbp; ) { in ep_find()
1155 kcmp = ep_cmp_ffd(&ffd, &epi->ffd); in ep_find()
1157 rbp = rbp->rb_right; in ep_find()
1159 rbp = rbp->rb_left; in ep_find()
1175 for (rbp = rb_first_cached(&ep->rbr); rbp; rbp = rb_next(rbp)) { in ep_find_tfd()
1177 if (epi->ffd.fd == tfd) { in ep_find_tfd()
1181 toff--; in ep_find_tfd()
1197 return ERR_PTR(-EINVAL); in get_epoll_tfile_raw_ptr()
1199 ep = file->private_data; in get_epoll_tfile_raw_ptr()
1201 mutex_lock(&ep->mtx); in get_epoll_tfile_raw_ptr()
1204 file_raw = epi->ffd.file; in get_epoll_tfile_raw_ptr()
1206 file_raw = ERR_PTR(-ENOENT); in get_epoll_tfile_raw_ptr()
1207 mutex_unlock(&ep->mtx); in get_epoll_tfile_raw_ptr()
1237 * This is simple 'new->next = head' operation, but cmpxchg() in list_add_tail_lockless()
1240 * new->next == new. in list_add_tail_lockless()
1242 if (!try_cmpxchg(&new->next, &new, head)) in list_add_tail_lockless()
1246 * Initially ->next of a new element must be updated with the head in list_add_tail_lockless()
1248 * exchanged. XCHG guarantees memory ordering, thus ->next should be in list_add_tail_lockless()
1250 * swapped before prev->next is updated. in list_add_tail_lockless()
1253 prev = xchg(&head->prev, new); in list_add_tail_lockless()
1256 * It is safe to modify prev->next and new->prev, because a new element in list_add_tail_lockless()
1257 * is added only to the tail and new->next is updated before XCHG. in list_add_tail_lockless()
1260 prev->next = new; in list_add_tail_lockless()
1261 new->prev = prev; in list_add_tail_lockless()
1267 * Chains a new epi entry to the tail of the ep->ovflist in a lockless way,
1274 struct eventpoll *ep = epi->ep; in chain_epi_lockless()
1277 if (epi->next != EP_UNACTIVE_PTR) in chain_epi_lockless()
1281 if (cmpxchg(&epi->next, EP_UNACTIVE_PTR, NULL) != EP_UNACTIVE_PTR) in chain_epi_lockless()
1285 epi->next = xchg(&ep->ovflist, epi); in chain_epi_lockless()
1296 * events from another file descriptor, thus all modifications to ->rdllist
1297 * or ->ovflist are lockless. Read lock is paired with the write lock from
1302 * concurrently for the same @epi from different CPUs if poll table was inited
1312 struct eventpoll *ep = epi->ep; in ep_poll_callback()
1317 read_lock_irqsave(&ep->lock, flags); in ep_poll_callback()
1322 * If the event mask does not contain any poll(2) event, we consider the in ep_poll_callback()
1327 if (!(epi->event.events & ~EP_PRIVATE_BITS)) in ep_poll_callback()
1336 if (pollflags && !(pollflags & epi->event.events)) in ep_poll_callback()
1340 * If we are transferring events to userspace, we can hold no locks in ep_poll_callback()
1341 * (because we're accessing user memory, and because of linux f_op->poll() in ep_poll_callback()
1343 * chained in ep->ovflist and requeued later on. in ep_poll_callback()
1345 if (READ_ONCE(ep->ovflist) != EP_UNACTIVE_PTR) { in ep_poll_callback()
1350 if (list_add_tail_lockless(&epi->rdllink, &ep->rdllist)) in ep_poll_callback()
1355 * Wake up ( if active ) both the eventpoll wait list and the ->poll() in ep_poll_callback()
1358 if (waitqueue_active(&ep->wq)) { in ep_poll_callback()
1359 if ((epi->event.events & EPOLLEXCLUSIVE) && in ep_poll_callback()
1363 if (epi->event.events & EPOLLIN) in ep_poll_callback()
1367 if (epi->event.events & EPOLLOUT) in ep_poll_callback()
1375 wake_up(&ep->wq); in ep_poll_callback()
1377 if (waitqueue_active(&ep->poll_wait)) in ep_poll_callback()
1381 read_unlock_irqrestore(&ep->lock, flags); in ep_poll_callback()
1387 if (!(epi->event.events & EPOLLEXCLUSIVE)) in ep_poll_callback()
1393 * ->whead = NULL and do another remove_wait_queue() after in ep_poll_callback()
1396 list_del_init(&wait->entry); in ep_poll_callback()
1398 * ->whead != NULL protects us from the race with in ep_poll_callback()
1400 * takes whead->lock held by the caller. Once we nullify it, in ep_poll_callback()
1403 smp_store_release(&ep_pwq_from_wait(wait)->whead, NULL); in ep_poll_callback()
1417 struct epitem *epi = epq->epi; in ep_ptable_queue_proc()
1425 epq->epi = NULL; in ep_ptable_queue_proc()
1429 init_waitqueue_func_entry(&pwq->wait, ep_poll_callback); in ep_ptable_queue_proc()
1430 pwq->whead = whead; in ep_ptable_queue_proc()
1431 pwq->base = epi; in ep_ptable_queue_proc()
1432 if (epi->event.events & EPOLLEXCLUSIVE) in ep_ptable_queue_proc()
1433 add_wait_queue_exclusive(whead, &pwq->wait); in ep_ptable_queue_proc()
1435 add_wait_queue(whead, &pwq->wait); in ep_ptable_queue_proc()
1436 pwq->next = epi->pwqlist; in ep_ptable_queue_proc()
1437 epi->pwqlist = pwq; in ep_ptable_queue_proc()
1443 struct rb_node **p = &ep->rbr.rb_root.rb_node, *parent = NULL; in ep_rbtree_insert()
1450 kcmp = ep_cmp_ffd(&epi->ffd, &epic->ffd); in ep_rbtree_insert()
1452 p = &parent->rb_right; in ep_rbtree_insert()
1455 p = &parent->rb_left; in ep_rbtree_insert()
1457 rb_link_node(&epi->rbn, parent, p); in ep_rbtree_insert()
1458 rb_insert_color_cached(&epi->rbn, &ep->rbr, leftmost); in ep_rbtree_insert()
1485 return -1; in path_count_inc()
1503 return -1; in reverse_path_check_proc()
1507 struct hlist_head *refs = &epi->ep->refs; in reverse_path_check_proc()
1519 * reverse_path_check - The tfile_check_list is list of epitem_head, which have
1526 * %-1 otherwise.
1532 for (p = tfile_check_list; p != EP_UNACTIVE_PTR; p = p->next) { in reverse_path_check()
1536 error = reverse_path_check_proc(&p->epitems, 0); in reverse_path_check()
1549 if (!epi->ep->ws) { in ep_create_wakeup_source()
1550 epi->ep->ws = wakeup_source_register(NULL, "eventpoll"); in ep_create_wakeup_source()
1551 if (!epi->ep->ws) in ep_create_wakeup_source()
1552 return -ENOMEM; in ep_create_wakeup_source()
1555 take_dentry_name_snapshot(&n, epi->ffd.file->f_path.dentry); in ep_create_wakeup_source()
1560 return -ENOMEM; in ep_create_wakeup_source()
1561 rcu_assign_pointer(epi->ws, ws); in ep_create_wakeup_source()
1571 RCU_INIT_POINTER(epi->ws, NULL); in ep_destroy_wakeup_source()
1589 ep = file->private_data; in attach_epitem()
1592 head = &ep->refs; in attach_epitem()
1593 } else if (!READ_ONCE(file->f_ep)) { in attach_epitem()
1597 return -ENOMEM; in attach_epitem()
1598 head = &to_free->epitems; in attach_epitem()
1600 spin_lock(&file->f_lock); in attach_epitem()
1601 if (!file->f_ep) { in attach_epitem()
1603 spin_unlock(&file->f_lock); in attach_epitem()
1606 file->f_ep = head; in attach_epitem()
1609 hlist_add_head_rcu(&epi->fllink, file->f_ep); in attach_epitem()
1610 spin_unlock(&file->f_lock); in attach_epitem()
1628 tep = tfile->private_data; in ep_insert()
1632 if (unlikely(percpu_counter_compare(&ep->user->epoll_watches, in ep_insert()
1634 return -ENOSPC; in ep_insert()
1635 percpu_counter_inc(&ep->user->epoll_watches); in ep_insert()
1638 percpu_counter_dec(&ep->user->epoll_watches); in ep_insert()
1639 return -ENOMEM; in ep_insert()
1643 INIT_LIST_HEAD(&epi->rdllink); in ep_insert()
1644 epi->ep = ep; in ep_insert()
1645 ep_set_ffd(&epi->ffd, tfile, fd); in ep_insert()
1646 epi->event = *event; in ep_insert()
1647 epi->next = EP_UNACTIVE_PTR; in ep_insert()
1650 mutex_lock_nested(&tep->mtx, 1); in ep_insert()
1654 mutex_unlock(&tep->mtx); in ep_insert()
1656 percpu_counter_dec(&ep->user->epoll_watches); in ep_insert()
1657 return -ENOMEM; in ep_insert()
1669 mutex_unlock(&tep->mtx); in ep_insert()
1680 return -EINVAL; in ep_insert()
1683 if (epi->event.events & EPOLLWAKEUP) { in ep_insert()
1691 /* Initialize the poll table using the queue callback */ in ep_insert()
1696 * Attach the item to the poll hooks and get current event bits. in ep_insert()
1699 * this operation completes, the poll callback can start hitting in ep_insert()
1705 * We have to check if something went wrong during the poll wait queue in ep_insert()
1711 return -ENOMEM; in ep_insert()
1715 write_lock_irq(&ep->lock); in ep_insert()
1722 list_add_tail(&epi->rdllink, &ep->rdllist); in ep_insert()
1726 if (waitqueue_active(&ep->wq)) in ep_insert()
1727 wake_up(&ep->wq); in ep_insert()
1728 if (waitqueue_active(&ep->poll_wait)) in ep_insert()
1732 write_unlock_irq(&ep->lock); in ep_insert()
1756 * Set the new event interest mask before calling f_op->poll(); in ep_modify()
1758 * f_op->poll() call and the new event set registering. in ep_modify()
1760 epi->event.events = event->events; /* need barrier below */ in ep_modify()
1761 epi->event.data = event->data; /* protected by mtx */ in ep_modify()
1762 if (epi->event.events & EPOLLWAKEUP) { in ep_modify()
1774 * event occurs immediately after we call f_op->poll(). in ep_modify()
1775 * We need this because we did not take ep->lock while in ep_modify()
1777 * ep->lock). in ep_modify()
1780 * when calling f_op->poll(). This barrier also in ep_modify()
1784 * This barrier will now guarantee ep_poll_callback or f_op->poll in ep_modify()
1796 write_lock_irq(&ep->lock); in ep_modify()
1798 list_add_tail(&epi->rdllink, &ep->rdllist); in ep_modify()
1802 if (waitqueue_active(&ep->wq)) in ep_modify()
1803 wake_up(&ep->wq); in ep_modify()
1804 if (waitqueue_active(&ep->poll_wait)) in ep_modify()
1807 write_unlock_irq(&ep->lock); in ep_modify()
1826 * Always short-circuit for fatal signals to allow threads to make a in ep_send_events()
1831 return -EINTR; in ep_send_events()
1835 mutex_lock(&ep->mtx); in ep_send_events()
1840 * Items cannot vanish during the loop we are holding ep->mtx. in ep_send_events()
1850 * Activate ep->ws before deactivating epi->ws to prevent in ep_send_events()
1851 * triggering auto-suspend here (in case we reactive epi->ws in ep_send_events()
1854 * This could be rearranged to delay the deactivation of epi->ws in ep_send_events()
1855 * instead, but then epi->ws would temporarily be out of sync in ep_send_events()
1860 if (ws->active) in ep_send_events()
1861 __pm_stay_awake(ep->ws); in ep_send_events()
1865 list_del_init(&epi->rdllink); in ep_send_events()
1868 * If the event mask intersect the caller-requested one, in ep_send_events()
1869 * deliver the event to userspace. Again, we are holding ep->mtx, in ep_send_events()
1870 * so no operations coming from userspace can change the item. in ep_send_events()
1876 events = epoll_put_uevent(revents, epi->event.data, events); in ep_send_events()
1878 list_add(&epi->rdllink, &txlist); in ep_send_events()
1881 res = -EFAULT; in ep_send_events()
1885 if (epi->event.events & EPOLLONESHOT) in ep_send_events()
1886 epi->event.events &= EP_PRIVATE_BITS; in ep_send_events()
1887 else if (!(epi->event.events & EPOLLET)) { in ep_send_events()
1893 * availability. At this point, no one can insert in ep_send_events()
1894 * into ep->rdllist besides us. The epoll_ctl() in ep_send_events()
1897 * poll callback will queue them in ep->ovflist. in ep_send_events()
1899 list_add_tail(&epi->rdllink, &ep->rdllist); in ep_send_events()
1904 mutex_unlock(&ep->mtx); in ep_send_events()
1917 to->tv_sec = 0; in ep_timeout_to_timespec()
1918 to->tv_nsec = 0; in ep_timeout_to_timespec()
1922 to->tv_sec = ms / MSEC_PER_SEC; in ep_timeout_to_timespec()
1923 to->tv_nsec = NSEC_PER_MSEC * (ms % MSEC_PER_SEC); in ep_timeout_to_timespec()
1931 * autoremove_wake_function, but remove even on failure to wake up, because we
1945 list_del_init_careful(&wq_entry->entry); in ep_autoremove_wake_function()
1950 * ep_poll - Retrieves ready events, and delivers them to the caller-supplied
1976 if (timeout && (timeout->tv_sec | timeout->tv_nsec)) { in ep_poll()
1991 * with a non-zero timeout, this thread will check the ready list under in ep_poll()
2018 return -EINTR; in ep_poll()
2022 * thus wait entry is removed from the wait queue on each in ep_poll()
2026 * lost. This is also good performance-wise, because on in ep_poll()
2027 * normal wakeup path no need to call __remove_wait_queue() in ep_poll()
2028 * explicitly, thus ep->lock is not taken, which halts the in ep_poll()
2040 write_lock_irq(&ep->lock); in ep_poll()
2043 * the same lock on wakeup ep_poll_callback() side, so it in ep_poll()
2050 * plays with two lists (->rdllist and ->ovflist) and there in ep_poll()
2057 __add_wait_queue_exclusive(&ep->wq, &wait); in ep_poll()
2059 write_unlock_irq(&ep->lock); in ep_poll()
2068 * If timed out and still on the wait queue, recheck eavail in ep_poll()
2074 write_lock_irq(&ep->lock); in ep_poll()
2076 * If the thread timed out and is not on the wait queue, in ep_poll()
2084 __remove_wait_queue(&ep->wq, &wait); in ep_poll()
2085 write_unlock_irq(&ep->lock); in ep_poll()
2091 * ep_loop_check_proc - verify that adding an epoll file inside another
2100 * structure @ep does not violate the constraints, or %-1 otherwise.
2108 mutex_lock_nested(&ep->mtx, depth + 1); in ep_loop_check_proc()
2109 ep->gen = loop_check_gen; in ep_loop_check_proc()
2110 for (rbp = rb_first_cached(&ep->rbr); rbp; rbp = rb_next(rbp)) { in ep_loop_check_proc()
2112 if (unlikely(is_file_epoll(epi->ffd.file))) { in ep_loop_check_proc()
2114 ep_tovisit = epi->ffd.file->private_data; in ep_loop_check_proc()
2115 if (ep_tovisit->gen == loop_check_gen) in ep_loop_check_proc()
2118 error = -1; in ep_loop_check_proc()
2132 list_file(epi->ffd.file); in ep_loop_check_proc()
2135 mutex_unlock(&ep->mtx); in ep_loop_check_proc()
2141 * ep_loop_check - Performs a check to verify that adding an epoll file (@to)
2149 * does not violate the constraints, or %-1 otherwise.
2162 tfile_check_list = head->next; in clear_tfile_check_list()
2181 return -EINVAL; in do_epoll_create()
2203 ep->file = file; in do_epoll_create()
2222 return -EINVAL; in SYSCALL_DEFINE1()
2230 if ((epev->events & EPOLLWAKEUP) && !capable(CAP_BLOCK_SUSPEND)) in ep_take_care_of_epollwakeup()
2231 epev->events &= ~EPOLLWAKEUP; in ep_take_care_of_epollwakeup()
2236 epev->events &= ~EPOLLWAKEUP; in ep_take_care_of_epollwakeup()
2249 return -EAGAIN; in epoll_mutex_lock()
2262 error = -EBADF; in do_epoll_ctl()
2272 /* The target file descriptor must support poll */ in do_epoll_ctl()
2273 error = -EPERM; in do_epoll_ctl()
2286 error = -EINVAL; in do_epoll_ctl()
2295 if (ep_op_has_event(op) && (epds->events & EPOLLEXCLUSIVE)) { in do_epoll_ctl()
2299 (epds->events & ~EPOLLEXCLUSIVE_OK_BITS))) in do_epoll_ctl()
2307 ep = fd_file(f)->private_data; in do_epoll_ctl()
2314 * and hang them on the tfile_check_list, so we can check that we in do_epoll_ctl()
2317 * We do not need to take the global 'epumutex' on EPOLL_CTL_ADD when in do_epoll_ctl()
2320 * 'epnested_mutex' on add is to prevent complex toplogies such as loops and in do_epoll_ctl()
2324 error = epoll_mutex_lock(&ep->mtx, 0, nonblock); in do_epoll_ctl()
2328 if (READ_ONCE(fd_file(f)->f_ep) || ep->gen == loop_check_gen || in do_epoll_ctl()
2330 mutex_unlock(&ep->mtx); in do_epoll_ctl()
2337 tep = fd_file(tf)->private_data; in do_epoll_ctl()
2338 error = -ELOOP; in do_epoll_ctl()
2342 error = epoll_mutex_lock(&ep->mtx, 0, nonblock); in do_epoll_ctl()
2355 error = -EINVAL; in do_epoll_ctl()
2359 epds->events |= EPOLLERR | EPOLLHUP; in do_epoll_ctl()
2362 error = -EEXIST; in do_epoll_ctl()
2373 error = -ENOENT; in do_epoll_ctl()
2378 if (!(epi->event.events & EPOLLEXCLUSIVE)) { in do_epoll_ctl()
2379 epds->events |= EPOLLERR | EPOLLHUP; in do_epoll_ctl()
2383 error = -ENOENT; in do_epoll_ctl()
2386 mutex_unlock(&ep->mtx); in do_epoll_ctl()
2415 return -EFAULT; in SYSCALL_DEFINE4()
2433 return -EINVAL; in do_epoll_wait()
2437 return -EFAULT; in do_epoll_wait()
2442 return -EBADF; in do_epoll_wait()
2448 error = -EINVAL; in do_epoll_wait()
2456 ep = fd_file(f)->private_data; in do_epoll_wait()
2495 restore_saved_sigmask_unless(error == -EINTR); in do_epoll_pwait()
2519 return -EFAULT; in SYSCALL_DEFINE6()
2522 return -EINVAL; in SYSCALL_DEFINE6()
2547 restore_saved_sigmask_unless(err == -EINTR); in do_compat_epoll_pwait()
2576 return -EFAULT; in COMPAT_SYSCALL_DEFINE6()
2579 return -EINVAL; in COMPAT_SYSCALL_DEFINE6()
2596 max_user_watches = (((si.totalram - si.totalhigh) / 25) << PAGE_SHIFT) / in eventpoll_init()
2602 * using an extra cache line on 64-bit (and smaller) CPUs in eventpoll_init()