Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

fs/epoll: remove unnecessary wakeups of nested epoll

Take the case where we have:

t0
| (ew)
e0
| (et)
e1
| (lt)
s0

t0: thread 0
e0: epoll fd 0
e1: epoll fd 1
s0: socket fd 0
ew: epoll_wait
et: edge-trigger
lt: level-trigger

We remove unnecessary wakeups to prevent the nested epoll that working in edge-
triggered mode to waking up continuously.

Test code:
#include <unistd.h>
#include <sys/epoll.h>
#include <sys/socket.h>

int main(int argc, char *argv[])
{
int sfd[2];
int efd[2];
struct epoll_event e;

if (socketpair(AF_UNIX, SOCK_STREAM, 0, sfd) < 0)
goto out;

efd[0] = epoll_create(1);
if (efd[0] < 0)
goto out;

efd[1] = epoll_create(1);
if (efd[1] < 0)
goto out;

e.events = EPOLLIN;
if (epoll_ctl(efd[1], EPOLL_CTL_ADD, sfd[0], &e) < 0)
goto out;

e.events = EPOLLIN | EPOLLET;
if (epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[1], &e) < 0)
goto out;

if (write(sfd[1], "w", 1) != 1)
goto out;

if (epoll_wait(efd[0], &e, 1, 0) != 1)
goto out;

if (epoll_wait(efd[0], &e, 1, 0) != 0)
goto out;

close(efd[0]);
close(efd[1]);
close(sfd[0]);
close(sfd[1]);

return 0;

out:
return -1;
}

More tests:
https://github.com/heiher/epoll-wakeup

Link: http://lkml.kernel.org/r/20191009060516.3577-1-r@hev.cc
Signed-off-by: hev <r@hev.cc>
Reviewed-by: Roman Penyaev <rpenyaev@suse.de>
Cc: Al Viro <viro@ZenIV.linux.org.uk>
Cc: Davide Libenzi <davidel@xmailserver.org>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Dominik Brodowski <linux@dominikbrodowski.net>
Cc: Eric Wong <e@80x24.org>
Cc: Jason Baron <jbaron@akamai.com>
Cc: Sridhar Samudrala <sridhar.samudrala@intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Heiher and committed by
Linus Torvalds
339ddb53 f6520c52

-16
-16
fs/eventpoll.c
··· 666 666 void *priv, int depth, bool ep_locked) 667 667 { 668 668 __poll_t res; 669 - int pwake = 0; 670 669 struct epitem *epi, *nepi; 671 670 LIST_HEAD(txlist); 672 671 ··· 732 733 */ 733 734 list_splice(&txlist, &ep->rdllist); 734 735 __pm_relax(ep->ws); 735 - 736 - if (!list_empty(&ep->rdllist)) { 737 - /* 738 - * Wake up (if active) both the eventpoll wait list and 739 - * the ->poll() wait list (delayed after we release the lock). 740 - */ 741 - if (waitqueue_active(&ep->wq)) 742 - wake_up(&ep->wq); 743 - if (waitqueue_active(&ep->poll_wait)) 744 - pwake++; 745 - } 746 736 write_unlock_irq(&ep->lock); 747 737 748 738 if (!ep_locked) 749 739 mutex_unlock(&ep->mtx); 750 - 751 - /* We have to call this outside the lock */ 752 - if (pwake) 753 - ep_poll_safewake(&ep->poll_wait); 754 740 755 741 return res; 756 742 }