• epoll源码分析


    epoll_create

    epoll的第一个系统调用epoll_create,生成一个文件描述符,创建eventpoll结构,保存到文件的私有数据结构中。
    当创建好epoll句柄后,它也会占用一个fd值,所以在使用完epoll后,必须调用close()关闭,否则可能导致fd被耗尽。

    注意:size参数只是告诉内核这个 epoll对象会处理的事件大致数目,而不是能够处理的事件的最大个数。在 Linux最新的一些内核版本的实现中,这个 size参数没有任何意义。

    1. /*
    2. * This structure is stored inside the "private_data" member of the file
    3. * structure and represents the main data structure for the eventpoll
    4. * interface.
    5. */
    6. struct eventpoll {
    7. /* Protect the access to this structure */
    8. spinlock_t lock;
    9. /*
    10. * This mutex is used to ensure that files are not removed
    11. * while epoll is using them. This is held during the event
    12. * collection loop, the file cleanup path, the epoll file exit
    13. * code and the ctl operations.
    14. */
    15. struct mutex mtx;
    16. /* Wait queue used by sys_epoll_wait() */
    17. //调用epoll_wait时,需要从ready链表上获取已准备好事件。
    18. //如果没有准备好的事件,则需要堵塞等待,将进程堵塞在wq队列上,
    19. //目标文件有事件发生了,则调用ep_poll_callback将epi(目标文件)挂到ready链表上,
    20. //同时唤醒调用epoll_wait的进程
    21. wait_queue_head_t wq;
    22. /* Wait queue used by file->poll() */
    23. //epoll也提供了poll函数,即可以将epoll添加到poll/select/epoll进行监听,如果有进程调用它的
    24. //poll,会将调用进程加到它的等待队列poll_wait中,以便唤醒调用进程
    25. wait_queue_head_t poll_wait;
    26. /* List of ready file descriptors */
    27. //存放已有事件发生的监听文件
    28. struct list_head rdllist;
    29. /* RB tree root used to store monitored fd structs */
    30. //树根,将监听文件存储在epitem结构中,将epitem插入此树
    31. struct rb_root rbr;
    32. /*
    33. * This is a single linked list that chains all the "struct epitem" that
    34. * happened while transferring ready events to userspace w/out
    35. * holding ->lock.
    36. */
    37. //将发生事件的目标文件传递到用户空间时,发生事件的其他文件暂时被挂载ovflist上
    38. struct epitem *ovflist;
    39. /* wakeup_source used when ep_scan_ready_list is running */
    40. struct wakeup_source *ws;
    41. /* The user that created the eventpoll descriptor */
    42. struct user_struct *user;
    43. struct file *file;
    44. /* used to optimize loop detection check */
    45. int visited;
    46. struct list_head visited_list_link;
    47. };

    1. epoll_create的参数size is ignored, but must be greater than zero;
    2. In the initial epoll_create() implementation, the size argument
    3. informed the kernel of the number of file descriptors that the caller
    4. expected to add to the epoll instance. The kernel used this
    5. information as a hint for the amount of space to initially
    6. allocate in internal data structures describing events. (If
    7. necessary, the kernel would allocate more space if the caller's
    8. usage exceeded the hint given in size.) Nowadays, this hint is
    9. no longer required (the kernel dynamically sizes the required
    10. data structures without needing the hint), but size must still be
    11. greater than zero, in order to ensure backward compatibility when
    12. new epoll applications are run on older kernels.
    13. 从下面的代码看,参数size虽然用不到,但是必须大于0,这是为了向后兼容,以前的kernel版本是需要这个参数的。
    14. linux/fs/eventpoll.c:
    15. SYSCALL_DEFINE1(epoll_create, int, size)
    16. if (size <= 0)
    17. return -EINVAL;
    18. return sys_epoll_create1(0);

    1. /* File callbacks that implement the eventpoll file behaviour */
    2. static const struct file_operations eventpoll_fops = {
    3. #ifdef CONFIG_PROC_FS
    4. .show_fdinfo = ep_show_fdinfo,
    5. #endif
    6. .release = ep_eventpoll_release,
    7. .poll = ep_eventpoll_poll,
    8. .llseek = noop_llseek,
    9. };
    10. SYSCALL_DEFINE1(epoll_create1, int, flags)
    11. //创建epoll结构体并初始化相关字段
    12. error = ep_alloc(&ep);
    13. user = get_current_user();
    14. ep = kzalloc(sizeof(*ep), GFP_KERNEL);
    15. spin_lock_init(&ep->lock);
    16. mutex_init(&ep->mtx);
    17. init_waitqueue_head(&ep->wq);
    18. init_waitqueue_head(&ep->poll_wait);
    19. INIT_LIST_HEAD(&ep->rdllist);
    20. ep->rbr = RB_ROOT;
    21. ep->ovflist = EP_UNACTIVE_PTR;
    22. ep->user = user;
    23. *pep = ep;
    24. fd = get_unused_fd_flags(O_RDWR | (flags & O_CLOEXEC));
    25. file = anon_inode_getfile("[eventpoll]", &eventpoll_fops, ep, O_RDWR | (flags & O_CLOEXEC));
    26. //将ep赋值给file的私有数据字段
    27. file->private_data = priv;
    28. ep->file = file;
    29. fd_install(fd, file);
    30. return fd;

    epoll_ctl

    epoll的第二个系统调用epoll_ctl,向 epoll对象中添加、修改或者删除感兴趣的事件,返回0表示成功,否则返回–1,此时需要根据errno错误码判断错误类型。
    epoll_wait方法返回的事件必然是通过 epoll_ctl添加到 epoll中的。
    第一个参数是epoll_create()返回的描述符,
    第二个参数表示动作,用三个宏来表示:

    1. EPOLL_CTL_ADD:注册新的fd到epfd中;
    2. EPOLL_CTL_MOD:修改已经注册的fd的监听事件;
    3. EPOLL_CTL_DEL:从epfd中删除一个fd

    第三个参数是需要监听的fd,
    第四个参数是告诉内核需要监听什么事。

    1. SYSCALL_DEFINE4(epoll_ctl, int, epfd, int, op, int, fd, struct epoll_event __user *, event)
    2. {
    3. //如果不是删除操作,即添加或者修改操作,需要从用户态拷贝事件到内核态
    4. if (ep_op_has_event(op) &&
    5. copy_from_user(&epds, event, sizeof(struct epoll_event)))
    6. goto error_return;
    7. //获取epoll文件
    8. f = fdget(epfd);
    9. //获取目标文件
    10. tf = fdget(fd);
    11. //目标文件必须支持poll操作
    12. if (!tf.file->f_op->poll)
    13. goto error_tgt_fput;
    14. //不允许epoll监听自己
    15. error = -EINVAL;
    16. if (f.file == tf.file || !is_file_epoll(f.file))
    17. goto error_tgt_fput;
    18. //在红黑树中查找目标文件是否已经存在
    19. epi = ep_find(ep, tf.file, fd);
    20. error = -EINVAL;
    21. switch (op) {
    22. case EPOLL_CTL_ADD:
    23. //epi为空,说明目标文件不存在,则将目标文件插入到红黑树
    24. if (!epi) {
    25. epds.events |= POLLERR | POLLHUP;
    26. error = ep_insert(ep, &epds, tf.file, fd, full_check);
    27. } else
    28. //如果已经存在,则返回error
    29. error = -EEXIST;
    30. if (full_check)
    31. clear_tfile_check_list();
    32. break;
    33. }

    ep_insert用来创建新的epi(代表目标文件),插入红黑树。初始化wait结构,加入目标文件的等待队列,如果有事件发生,则调用ep_poll_callback将发生事件的epi挂载ready链表上。
    注意的是,调用init_waitqueue_func_entry初始化wait ,调用此函数的一般不会是打算唤醒进程,而是事件发生时,调用func来做其他事情。

    1. static inline void
    2. init_waitqueue_func_entry(wait_queue_t *q, wait_queue_func_t func)
    3. {
    4. q->flags = 0;
    5. q->private = NULL;
    6. q->func = func;
    7. }

    1. static int ep_insert(struct eventpoll *ep, struct epoll_event *event,
    2. struct file *tfile, int fd, int full_check)
    3. {
    4. //创建epoll的用户最多可以监视max_user_watches个fd
    5. //max_user_watches可以通过/proc/sys/fs/epoll修改
    6. user_watches = atomic_long_read(&ep->user->epoll_watches);
    7. if (unlikely(user_watches >= max_user_watches))
    8. return -ENOSPC;
    9. //分配epitem结构体,存放监视fd相关信息
    10. epi = kmem_cache_alloc(epi_cache, GFP_KERNEL))
    11. INIT_LIST_HEAD(&epi->rdllink);
    12. INIT_LIST_HEAD(&epi->fllink);
    13. INIT_LIST_HEAD(&epi->pwqlist);
    14. epi->ep = ep;
    15. ep_set_ffd(&epi->ffd, tfile, fd);
    16. epi->event = *event;
    17. epi->nwait = 0;
    18. epi->next = EP_UNACTIVE_PTR;
    19. /* Wrapper struct used by poll queueing */
    20. struct ep_pqueue {
    21. poll_table pt;
    22. struct epitem *epi;
    23. };
    24. struct ep_pqueue epq;
    25. epq.epi = epi;
    26. //调用目标文件poll函数时,在poll_wait中调用 ep_ptable_queue_proc,
    27. init_poll_funcptr(&epq.pt, ep_ptable_queue_proc);
    28. pt->_qproc = qproc;
    29. pt->_key = ~0UL; /* all events enabled */
    30. //获取目标文件事件,同时将调用进程添加到目标文件等待队列中
    31. revents = ep_item_poll(epi, &epq.pt);
    32. pt->_key = epi->event.events;
    33. //调用被监视fd的poll函数。
    34. //对于tcp socket来说,就是调用tcp_poll(见下文解释)
    35. return epi->ffd.file->f_op->poll(epi->ffd.file, pt) & epi->event.events;
    36. //将epi插入到红黑树
    37. ep_rbtree_insert(ep, epi);
    38. //如果当前目标文件有事件发生,并且包含目标文件的epi还没加入到ready链表,则将epi添加到ready链表中
    39. if ((revents & event->events) && !ep_is_linked(&epi->rdllink))
    40. {
    41. list_add_tail(&epi->rdllink, &ep->rdllist);
    42. /* Notify waiting tasks that events are available */
    43. if (waitqueue_active(&ep->wq))
    44. wake_up_locked(&ep->wq);
    45. if (waitqueue_active(&ep->poll_wait))
    46. pwake++;
    47. }
    48. spin_unlock_irqrestore(&ep->lock, flags);
    49. //增加监听个数
    50. atomic_long_inc(&ep->user->epoll_watches);
    51. /* We have to call this outside the lock */
    52. if (pwake)
    53. ep_poll_safewake(&ep->poll_wait);
    54. return 0;
    55. }

    1. unsigned int tcp_poll(struct file *file, struct socket *sock, poll_table *wait)
    2. {
    3. //调用ep_ptable_queue_proc将等待队列添加到sk的等待队列头上
    4. sock_poll_wait(file, sk_sleep(sk), wait);
    5. //返回当前发生的事件(判断全连接队列是否为空),如果为空则返回0
    6. //如果不为空,则返回(POLLIN | POLLRDNORM)
    7. if (sk->sk_state == TCP_LISTEN)
    8. return inet_csk_listen_poll(sk);
    9. }
    10. static inline void sock_poll_wait(struct file *filp,wait_queue_head_t *wait_address, poll_table *p)
    11. {
    12. if (!poll_does_not_wait(p) && wait_address) {
    13. poll_wait(filp, wait_address, p);
    14. //调用 ep_ptable_queue_proc
    15. if (p && p->_qproc && wait_address)
    16. p->_qproc(filp, wait_address, p);
    17. /* We need to be sure we are in sync with the
    18. * socket flags modification.
    19. *
    20. * This memory barrier is paired in the wq_has_sleeper.
    21. */
    22. smp_mb();
    23. }
    24. }

    1. /* Wait structure used by the poll hooks */
    2. //连接目标文件等待队列和epitem
    3. struct eppoll_entry {
    4. /* List header used to link this structure to the "struct epitem" */
    5. //链接到epi->pwqlist
    6. struct list_head llink;
    7. /* The "base" pointer is set to the container "struct epitem" */
    8. //指向epitem
    9. struct epitem *base;
    10. /*
    11. * Wait queue item that will be linked to the target file wait
    12. * queue head.
    13. */
    14. //等待队列节点,会被添加到whead中
    15. wait_queue_t wait;
    16. /* The wait queue head that linked the "wait" wait queue item */
    17. //存放目标文件的等待队列
    18. wait_queue_head_t *whead;
    19. };
    20. static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead, poll_table *pt)
    21. {
    22. struct epitem *epi = ep_item_from_epqueue(pt);
    23. struct eppoll_entry *pwq;
    24. if (epi->nwait >= 0 && (pwq = kmem_cache_alloc(pwq_cache, GFP_KERNEL))) {
    25. //目标文件有事件发生时,会调用函数ep_poll_callback,此函数会唤醒调用epoll_wait而
    26. //堵塞的进程,而至于发生了什么事件,则会在调用进程中再次调用目标文件的poll来获取。
    27. init_waitqueue_func_entry(&pwq->wait, ep_poll_callback);
    28. q->flags = 0;
    29. q->private = NULL;
    30. q->func = func;
    31. pwq->whead = whead;
    32. pwq->base = epi;
    33. //将pwq->wait添加到目标文件的等待队列whead上,有事件发生时调用ep_poll_callback
    34. add_wait_queue(whead, &pwq->wait);
    35. unsigned long flags;
    36. wait->flags &= ~WQ_FLAG_EXCLUSIVE;
    37. spin_lock_irqsave(&q->lock, flags);
    38. __add_wait_queue(q, wait);
    39. spin_unlock_irqrestore(&q->lock, flags);
    40. list_add_tail(&pwq->llink, &epi->pwqlist);
    41. epi->nwait++;
    42. } else {
    43. /* We have to signal that an error occurred */
    44. epi->nwait = -1;
    45. }
    46. }

    目标文件有事件发生时,调用函数ep_poll_callback,将发生事件的epi添加到readly链表中,并且唤醒调用epoll_wait堵塞的进程来获取事件,通知用户空间程序去处理目标文件的事件。

    1. /*
    2. * This is the callback that is passed to the wait queue wakeup
    3. * mechanism. It is called by the stored file descriptors when they
    4. * have events to report.
    5. */
    6. static int ep_poll_callback(wait_queue_t *wait, unsigned mode, int sync, void *key)
    7. {
    8. int pwake = 0;
    9. unsigned long flags;
    10. struct epitem *epi = ep_item_from_wait(wait);
    11. struct eventpoll *ep = epi->ep;
    12. spin_lock_irqsave(&ep->lock, flags);
    13. /*
    14. * If the event mask does not contain any poll(2) event, we consider the
    15. * descriptor to be disabled. This condition is likely the effect of the
    16. * EPOLLONESHOT bit that disables the descriptor when an event is received,
    17. * until the next EPOLL_CTL_MOD will be issued.
    18. */
    19. //如果用户设置了EPOLLONESHOT ,则会忽略所有发生的事件,直到下一次调用EPOLL_CTL_MOD
    20. if (!(epi->event.events & ~EP_PRIVATE_BITS))
    21. goto out_unlock;
    22. /*
    23. * Check the events coming with the callback. At this stage, not
    24. * every device reports the events in the "key" parameter of the
    25. * callback. We need to be able to handle both cases here, hence the
    26. * test for "key" != NULL before the event match test.
    27. */
    28. //如果没有用户感兴趣的事件发生,则直接返回
    29. if (key && !((unsigned long) key & epi->event.events))
    30. goto out_unlock;
    31. /*
    32. * If we are transferring events to userspace, we can hold no locks
    33. * (because we're accessing user memory, and because of linux f_op->poll()
    34. * semantics). All the events that happen during that period of time are
    35. * chained in ep->ovflist and requeued later on.
    36. */
    37. //如果正在将之前发生的事件传递到用户程序,则将新事件添加到ovflist链表中。
    38. //然后goto跳转出去,不会再添加到rdlist上
    39. if (unlikely(ep->ovflist != EP_UNACTIVE_PTR)) {
    40. if (epi->next == EP_UNACTIVE_PTR) {
    41. epi->next = ep->ovflist;
    42. ep->ovflist = epi;
    43. if (epi->ws) {
    44. /*
    45. * Activate ep->ws since epi->ws may get
    46. * deactivated at any time.
    47. */
    48. __pm_stay_awake(ep->ws);
    49. }
    50. }
    51. goto out_unlock;
    52. }
    53. //将epi挂在ready链表
    54. /* If this file is already in the ready list we exit soon */
    55. if (!ep_is_linked(&epi->rdllink)) {
    56. list_add_tail(&epi->rdllink, &ep->rdllist);
    57. ep_pm_stay_awake_rcu(epi);
    58. }
    59. /*
    60. * Wake up ( if active ) both the eventpoll wait list and the ->poll()
    61. * wait list.
    62. */
    63. //如果ep句柄上有调用epoll_wait而堵塞等待进程,则唤醒。
    64. //因为调用epoll_wait,添加到等待队列时,调用了exclusive,并且添加到等待队列头部,
    65. //所以只会唤醒一个进程(类似多进程下accept的处理)
    66. if (waitqueue_active(&ep->wq))
    67. wake_up_locked(&ep->wq);
    68. out_unlock:
    69. spin_unlock_irqrestore(&ep->lock, flags);
    70. }

    epoll_wait

    epoll的第三个系统调用,用来获取发生事件的fd。
    a. 参数epfd是epoll_create返回的描述符,
    b. 参数events是分配好的 epoll_event结构体数组,epoll将会把发生的事件复制到 events数组中(events不可以是空指针,内核只负责把数据复制到这个 events数组中,不会去帮助我们在用户态中分配内存。内核这种做法效率很高),
    c. 参数maxevents表示本次可以返回的最大事件数目,通常 maxevents参数与预分配的events数组的大小是相等的,
    d. 参数timeout表示在没有检测到事件发生时最多等待的时间(单位为毫秒),如果 timeout为0,则表示 epoll_wait在 rdllist链表中为空,立刻返回,不会等待。

    1. SYSCALL_DEFINE4(epoll_wait, int, epfd, struct epoll_event __user *, events, int, maxevents, int, timeout)
    2. /* The maximum number of event must be greater than zero */
    3. if (maxevents <= 0 || maxevents > EP_MAX_EVENTS)
    4. return -EINVAL;
    5. /* Get the "struct file *" for the eventpoll file */
    6. f = fdget(epfd);
    7. if (!f.file)
    8. return -EBADF;
    9. error = -EINVAL;
    10. //必须是epoll文件
    11. if (!is_file_epoll(f.file))
    12. goto error_fput;
    13. //获取epoll结构体
    14. ep = f.file->private_data;
    15. /* Time to fish for events ... */
    16. //获取准备好的事件,传递到用户空间
    17. error = ep_poll(ep, events, maxevents, timeout);

    将readly链表上已就绪的fd传递到用户空间,如果没有就绪事件,则根据timeout决定堵塞或者返回

    1. static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events, int maxevents, long timeout)
    2. if (timeout > 0) {
    3. struct timespec end_time = ep_set_mstimeout(timeout);
    4. slack = select_estimate_accuracy(&end_time);
    5. to = &expires;
    6. *to = timespec_to_ktime(end_time);
    7. } else if (timeout == 0) {
    8. //如果timeout为0,则跳转到check_events检查当前是否有事件发生,没有则直接返回
    9. //如果有事件发生,则将事件发送到用户空间
    10. /*
    11. * Avoid the unnecessary trip to the wait queue loop, if the
    12. * caller specified a non blocking operation.
    13. */
    14. timed_out = 1;
    15. spin_lock_irqsave(&ep->lock, flags);
    16. goto check_events;
    17. }
    18. //如果没有就绪事件,则堵塞等待事件发生
    19. if (!ep_events_available(ep)) {
    20. /*
    21. * We don't have any available event to return to the caller.
    22. * We need to sleep here, and we will be wake up by
    23. * ep_poll_callback() when events will become available.
    24. */
    25. //初始化等待节点,将当前进程加入等待节点
    26. wait_queue_t wait;
    27. init_waitqueue_entry(&wait, current);
    28. q->flags = 0;
    29. q->private = p;
    30. q->func = default_wake_function;
    31. //将等待节点加入ep->wq等待队列头部。而在accept流程中是将等待节点加入到等待队列尾部。
    32. //在早期linux版本中,内核对于堵塞在epoll_wait的进程,也是全部唤醒的机制,所以存在和
    33. //accept相似的“惊群”问题。新版本的解决方案也是只会唤醒等待队列上的第一个进程。所以
    34. //新版本linux已经部分解决了epoll的“惊群”问题。所谓部分解决只是在LT模式下存在,ET模式下是不存在的。
    35. __add_wait_queue_exclusive(&ep->wq, &wait);
    36. wait->flags |= WQ_FLAG_EXCLUSIVE;
    37. __add_wait_queue(q, wait);
    38. for (;;) {
    39. /*
    40. * We don't want to sleep if the ep_poll_callback() sends us
    41. * a wakeup in between. That's why we set the task state
    42. * to TASK_INTERRUPTIBLE before doing the checks.
    43. */
    44. set_current_state(TASK_INTERRUPTIBLE);
    45. if (ep_events_available(ep) || timed_out)
    46. break;
    47. if (signal_pending(current)) {
    48. res = -EINTR;
    49. break;
    50. }
    51. spin_unlock_irqrestore(&ep->lock, flags);
    52. if (!schedule_hrtimeout_range(to, slack, HRTIMER_MODE_ABS))
    53. timed_out = 1;
    54. spin_lock_irqsave(&ep->lock, flags);
    55. }
    56. __remove_wait_queue(&ep->wq, &wait);
    57. set_current_state(TASK_RUNNING);
    58. }
    59. check_events:
    60. /* Is it worth to try to dig for events ? */
    61. eavail = ep_events_available(ep);
    62. !list_empty(&ep->rdllist) || ep->ovflist != EP_UNACTIVE_PTR;
    63. spin_unlock_irqrestore(&ep->lock, flags);
    64. /*
    65. * Try to transfer events to user space. In case we get 0 events and
    66. * there's still timeout left over, we go trying again in search of
    67. * more luck.
    68. */
    69. //如果readly链表不为空,说明有事件发生,则调用ep_send_events传递到用户空间
    70. if (!res && eavail &&
    71. !(res = ep_send_events(ep, events, maxevents)) && !timed_out)
    72. goto fetch_events;
    73. return res;

    将ready链表上的已就绪事件传递到用户空间

    1. static int ep_send_events(struct eventpoll *ep,
    2. struct epoll_event __user *events, int maxevents)
    3. {
    4. struct ep_send_events_data esed;
    5. esed.maxevents = maxevents;
    6. esed.events = events;
    7. return ep_scan_ready_list(ep, ep_send_events_proc, &esed, 0, false);
    8. }
    9. static int ep_scan_ready_list(struct eventpoll *ep, int (*sproc)(struct eventpoll *,
    10. struct list_head *, void *), void *priv, int depth, bool ep_locked)
    11. {
    12. LIST_HEAD(txlist);
    13. spin_lock_irqsave(&ep->lock, flags);
    14. //将rdlist赋值到txlist
    15. list_splice_init(&ep->rdllist, &txlist);
    16. //将ovslist置为空,以便在ep_poll_callback将又发生的事件临时存放在ovflist中
    17. ep->ovflist = NULL;
    18. spin_unlock_irqrestore(&ep->lock, flags);
    19. /*
    20. * Now call the callback function.
    21. */
    22. //调用ep_send_events_proc将发生的事件传递到用户程序
    23. error = (*sproc)(ep, &txlist, priv);
    24. spin_lock_irqsave(&ep->lock, flags);
    25. /*
    26. * During the time we spent inside the "sproc" callback, some
    27. * other events might have been queued by the poll callback.
    28. * We re-insert them inside the main ready-list here.
    29. */
    30. //在调用ep_send_events_proc将发生的事件传送到用户空间过程时,如果目标文件有事件发生,
    31. //则在ep_poll_callback函数中将epi暂时存放在ep->ovflist链表中。
    32. //此处将它们从ovflist取下,放入rdlink链表
    33. for (nepi = ep->ovflist; (epi = nepi) != NULL;
    34. nepi = epi->next, epi->next = EP_UNACTIVE_PTR) {
    35. /*
    36. * We need to check if the item is already in the list.
    37. * During the "sproc" callback execution time, items are
    38. * queued into ->ovflist but the "txlist" might already
    39. * contain them, and the list_splice() below takes care of them.
    40. */
    41. if (!ep_is_linked(&epi->rdllink)) {
    42. list_add_tail(&epi->rdllink, &ep->rdllist);
    43. ep_pm_stay_awake(epi);
    44. }
    45. }
    46. /*
    47. * We need to set back ep->ovflist to EP_UNACTIVE_PTR, so that after
    48. * releasing the lock, events will be queued in the normal way inside
    49. * ep->rdllist.
    50. */
    51. ep->ovflist = EP_UNACTIVE_PTR;
    52. /*
    53. * Quickly re-inject items left on "txlist".
    54. */
    55. list_splice(&txlist, &ep->rdllist);
    56. __pm_relax(ep->ws);
    57. if (!list_empty(&ep->rdllist)) {
    58. /*
    59. * Wake up (if active) both the eventpoll wait list and
    60. * the ->poll() wait list (delayed after we release the lock).
    61. */
    62. if (waitqueue_active(&ep->wq))
    63. wake_up_locked(&ep->wq);
    64. if (waitqueue_active(&ep->poll_wait))
    65. pwake++;
    66. }
    67. spin_unlock_irqrestore(&ep->lock, flags);
    68. if (!ep_locked)
    69. mutex_unlock(&ep->mtx);
    70. /* We have to call this outside the lock */
    71. if (pwake)
    72. ep_poll_safewake(&ep->poll_wait);
    73. return error;
    74. }

    调用函数ep_send_events_proc将发生事件的fd传递到用户空间,
    同时根据设置的不同模式进行不同的处理,比如如果是LT模式,则无论如何都会将epi重新加回到“就绪链表”,等待下次重新再poll以
    确认是否仍然有未处理的事件。

    1. static int ep_send_events_proc(struct eventpoll *ep, struct list_head *head, void *priv)
    2. {
    3. struct ep_send_events_data *esed = priv;
    4. //最多传递esed->maxevents个fd事件
    5. for (eventcnt = 0, uevent = esed->events;
    6. !list_empty(head) && eventcnt < esed->maxevents;) {
    7. epi = list_first_entry(head, struct epitem, rdllink);
    8. //将epi从ready链表上删除
    9. list_del_init(&epi->rdllink);
    10. //初始化空的pt,只是为了作为参数而已
    11. init_poll_funcptr(&pt, NULL);
    12. //调用目标文件的poll函数,获取发生的事件
    13. revents = ep_item_poll(epi, &pt);
    14. if (revents) {
    15. //将发生的事件放入用户空间
    16. //如果没有完成,则将epi重新加到“就绪链表”等待下次继续传送。
    17. if (__put_user(revents, &uevent->events) ||
    18. __put_user(epi->event.data, &uevent->data)) {
    19. list_add(&epi->rdllink, head);
    20. ep_pm_stay_awake(epi);
    21. return eventcnt ? eventcnt : -EFAULT;
    22. }
    23. eventcnt++;
    24. uevent++;
    25. //epi->event.events如果设置了EPOLLONESHOT,则将EP_PRIVATE_BITS赋值给
    26. //epi->event.events,表示只调用一次,即使有了新事件也不会再次通知到用户程序(在ep_poll_callback中进行判断)
    27. if (epi->event.events & EPOLLONESHOT)
    28. epi->event.events &= EP_PRIVATE_BITS;
    29. // 如果是LT模式,则无论如何都会将epi重新加回到“就绪链表”,等待下次重新再poll以
    30. //确认是否仍然有未处理的事件。这也符合“水平触发”的逻辑,即“只要你不处理,我就会一直通知你”。
    31. else if (!(epi->event.events & EPOLLET)) {
    32. /*
    33. * If this file has been added with Level
    34. * Trigger mode, we need to insert back inside
    35. * the ready list, so that the next call to
    36. * epoll_wait() will check again the events
    37. * availability. At this point, no one can insert
    38. * into ep->rdllist besides us. The epoll_ctl()
    39. * callers are locked out by
    40. * ep_scan_ready_list() holding "mtx" and the
    41. * poll callback will queue them in ep->ovflist.
    42. */
    43. list_add_tail(&epi->rdllink, &ep->rdllist);
    44. ep_pm_stay_awake(epi);
    45. }
    46. }
    47. }
    48. }

    参考

    https://www.cnblogs.com/xuewangkai/p/11158576.html

    也可参考;https://www.jianshu.com/p/f22a66f5949e 

  • 相关阅读:
    Spark 增量抽取 Mysql To Hive
    pytorch初学笔记(一):如何加载数据和Dataset实战
    springMVC下载文件
    SpringBoot【SpringBoot介绍、SpringBoot入门、SpringBoot原理分析、SpringBoot原理分析】(一)-全面详解(学习总结---从入门到深化)
    【python深度学习】——torch.min()
    数字三角形加强版,费马小定理求逆元,快速幂
    zookeeper节点数据类型介绍及集群搭建
    经典算法之快速排序法(附B站最细讲解视频)
    【算法题】2874. 有序三元组中的最大值 II
    【论文阅读】【yolo系列】YOLO-Pose的论文阅读
  • 原文地址:https://blog.csdn.net/fengcai_ke/article/details/126564001