In the above code, ep_insert() first check to make sure that the total number of files watched by the current user does not exceed value setted in /proc/sys/fs/epoll/max_user_watches. Otherwise, ep_insert() returns immediately with errno set to ENOSPC.

Next, ep_insert() allocates memory from the kernel slab allocator:

if (!(epi = kmem_cache_alloc(epi_cache, GFP_KERNEL)))
return -ENOMEM;

If ep_insert() was able to get enough memory for struct epitem, the following initialization process will happen:

Next, ep_insert() will be trying to register the callback into the file descriptor. But before we can talk about it, we need to get familiar with a few important data structures first:

poll_table is an important struct used by the VFS poll() implementation. (I know this is confusing, but I want to make it clear that the poll() I mentioned here is the implementation of the poll() file operation, not the poll() system call.) It is defined in include/linux/poll.h:

_key member of poll_table is confusing. Despite it's name, _key actually stores the interested event masks. In epoll implementation, _key is being set to ~0 (the complement of 0), which means epoll would like to receive callbacks for any kinds of event. This makes sense because user space application could change the event mask via epoll_ctl() at anytime, accepting all events from the VFS and then filter inside epoll implementation would make things much easier.

In order to make recovering of the original struct epitem easier in poll_queue_proc, epoll uses a simple struct called struct ep_pqueue to wrap a poll_table with a pointer to the corresponding struct epitem. (fs/eventpoll.c, line 243):

ep_insert() then initializes struct ep_pqueue. The following code first sets the epi member of struct ep_pqueue to be pointing to the struct epitem corresponding to the file trying to add, then it sets _qproc member of struct ep_pqueue to ep_ptable_queue_proc() and _key to ~0.

Next, ep_insert() will call ep_item_poll(epi, &epq.pt);, which will call the corresponding file's poll() implementation. Just for the sake of an example, let's use the Linux TCP stack's poll() implementation to see what it actually does with the poll_table:

tcp_poll() is the actual implementation of poll() for TCP sockets. It could be found at net/ipv4/tcp.c, line 436. Here is a snippet of it:

So what is sock_poll_wait() going to do with the event wait queue? You guessed right! It will do some simple checking and then invoke poll_wait() with the same parameter passed to it. poll_wait() will then invoke our specified callback and pass the event wait queue to it. (include/linux/poll.h, line 42):

First, ep_ptable_queue_proc() will try to recover the struct epitem that corresponds to the file of the wait queue we were dealing with. Since epoll used a wrapper struct struct ep_pqueue, recovering struct epitem from the poll_table pointer were just some simple pointer arithmetic.

After that, ep_ptable_queue_proc() simply allocates enough space for a struct eppoll_entry. This struct acts as a "glue" between the monitored file's wait queue and the corresponding struct epitem for that file. It is vital that epoll keeps track the head of the wait queue for the monitored file. Otherwise epoll would not be able to unregister from the wait queue later. struct eppoll_entry also contains the wait queue (pwq->wait) with wake-up function set to ep_poll_callback(). pwq->wait might be the most important thing in the whole epoll implementation because it will be used for:

monitoring events happening on that particular monitored file

wake up other processes as necessary

After that, ep_ptable_queue_proc() will attach pwq->wait into target file's wait queue (whead). And also add struct eppoll_entry into a linked list inside struct epitem (epi->pwqlist) and increment epi->nwait, which is the length of epi->pwqlist.

Ok, here is the question. Why does epoll need to use a linked list to store struct eppoll_entry inside a single file's struct epitem? Isn't struct epitem only need one struct eppoll_entry entry ever?

The answer is, I don't know either :) As far as I can tell, unless you are trying to create some crazy loops around epoll instances, epi->pwqlist will only contain one struct eppoll_entry and epi->nwait will most likely be 1 for most files.

Good news, this confusion about epi->pwqlist wouldn't cause any problems about what I will be talking next, which is how Linux notifies epoll instance about events happening on monitored files.

Remember what we talked about in the previous section? epoll attaches a wait_queue_t into the target file's wait list (which is a wait_queue_head_t). Despite wait_queue_t are most commonly used as wake up mechanism, it is really just a struct that stores a function pointer that will be called whenever Linux want to wake up the wait_queue_ts attached on a wait_queue_head_t. In the function, epoll could choose what to do with the wake up signal, and it doesn't have to wake up any process! As we can see later, most time when ep_poll_callback() is being called, nothing is being waked up.

Another thing I believe worth emphasizing is that, the wake-up mechanism of poll() is completely implementation dependent. For TCP socket files, the wait queue head is member sk_wq stored inside the struct sock struct. This also explains why we need the ep_ptable_queue_proc() callback to add ourself into the wait queue. Since different file implementation will put the wait queue head in completely different locations, there is no way we could locate the correct wait_queue_head_t without the use of the callback.

So when exactly is the sk_wq inside a struct sock being waked up? Well, turns out, the Linux socket system are also following the "OO" design as VFS is doing. struct sock defined the following hooks in net/core/sock.c, line 2312:

Inside sock_def_readable() and sock_def_write_space(), wake_up_interruptible_sync_poll()
is called on (struct sock)->sk_wq to execute the wake up callback function.

So when will sk->sk_data_ready() and sk->sk_write_space() be called? Well, it depends on the implementation. Using TCP socket as an example. sk->sk_data_ready() will be called from the bottom half interruption handler whenever a TCP connection finished the three-way handshake, or a buffer has been received for a particular TCP socket. sk->sk_write_space() will be called whenever a buffer state transition from full->available occurs for that socket. Remember this behavior, it will make the later topic, specifically the topic of Edge-Triggered mode interesting.

This ends the second article about epoll implementation. In the next article, I will be talking about what epoll does in the registered callback inside the socket wake up queue. If you like this article, or have any questions, fell free to leave them in the comment area below.