From: hev <r@hev.cc>
To: linux-fsdevel@vger.kernel.org
Cc: e@80x24.org, Heiher <r@hev.cc>, Al Viro <viro@ZenIV.linux.org.uk>,
Andrew Morton <akpm@linux-foundation.org>,
Davide Libenzi <davidel@xmailserver.org>,
Davidlohr Bueso <dave@stgolabs.net>,
Dominik Brodowski <linux@dominikbrodowski.net>,
Jason Baron <jbaron@akamai.com>,
Linus Torvalds <torvalds@linux-foundation.org>,
Roman Penyaev <rpenyaev@suse.de>,
Sridhar Samudrala <sridhar.samudrala@intel.com>,
linux-kernel@vger.kernel.org
Subject: [PATCH RESEND] fs/epoll: fix the edge-triggered mode for nested epoll
Date: Mon, 2 Sep 2019 13:20:34 +0800 [thread overview]
Message-ID: <20190902052034.16423-1-r@hev.cc> (raw)
From: Heiher <r@hev.cc>
The structure of event pools:
efd[1]: { efd[2] (EPOLLIN) } efd[0]: { efd[2] (EPOLLIN | EPOLLET) }
| |
+-----------------+-----------------+
|
v
efd[2]: { sfd[0] (EPOLLIN) }
When sfd[0] to be readable:
* the epoll_wait(efd[0], ..., 0) should return efd[2]'s events on first call,
and returns 0 on next calls, because efd[2] is added in edge-triggered mode.
* the epoll_wait(efd[1], ..., 0) should returns efd[2]'s events on every calls
until efd[2] is not readable (epoll_wait(efd[2], ...) => 0), because efd[1]
is added in level-triggered mode.
* the epoll_wait(efd[2], ..., 0) should returns sfd[0]'s events on every calls
until sfd[0] is not readable (read(sfd[0], ...) => EAGAIN), because sfd[0]
is added in level-triggered mode.
Test code:
#include <stdio.h>
#include <unistd.h>
#include <sys/epoll.h>
#include <sys/socket.h>
int main(int argc, char *argv[])
{
int sfd[2];
int efd[3];
int nfds;
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;
efd[2] = epoll_create(1);
if (efd[2] < 0)
goto out;
e.events = EPOLLIN;
if (epoll_ctl(efd[2], EPOLL_CTL_ADD, sfd[0], &e) < 0)
goto out;
e.events = EPOLLIN;
if (epoll_ctl(efd[1], EPOLL_CTL_ADD, efd[2], &e) < 0)
goto out;
e.events = EPOLLIN | EPOLLET;
if (epoll_ctl(efd[0], EPOLL_CTL_ADD, efd[2], &e) < 0)
goto out;
if (write(sfd[1], "w", 1) != 1)
goto out;
nfds = epoll_wait(efd[0], &e, 1, 0);
if (nfds != 1)
goto out;
nfds = epoll_wait(efd[0], &e, 1, 0);
if (nfds != 0)
goto out;
nfds = epoll_wait(efd[1], &e, 1, 0);
if (nfds != 1)
goto out;
nfds = epoll_wait(efd[1], &e, 1, 0);
if (nfds != 1)
goto out;
nfds = epoll_wait(efd[2], &e, 1, 0);
if (nfds != 1)
goto out;
nfds = epoll_wait(efd[2], &e, 1, 0);
if (nfds != 1)
goto out;
close(efd[2]);
close(efd[1]);
close(efd[0]);
close(sfd[0]);
close(sfd[1]);
printf("PASS\n");
return 0;
out:
printf("FAIL\n");
return -1;
}
Cc: Al Viro <viro@ZenIV.linux.org.uk>
Cc: Andrew Morton <akpm@linux-foundation.org>
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: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Roman Penyaev <rpenyaev@suse.de>
Cc: Sridhar Samudrala <sridhar.samudrala@intel.com>
Cc: linux-kernel@vger.kernel.org
Cc: linux-fsdevel@vger.kernel.org
Signed-off-by: hev <r@hev.cc>
---
fs/eventpoll.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/fs/eventpoll.c b/fs/eventpoll.c
index d7f1f5011fac..a44cb27c636c 100644
--- a/fs/eventpoll.c
+++ b/fs/eventpoll.c
@@ -672,6 +672,7 @@ static __poll_t ep_scan_ready_list(struct eventpoll *ep,
{
__poll_t res;
int pwake = 0;
+ int nwake = 0;
struct epitem *epi, *nepi;
LIST_HEAD(txlist);
@@ -685,6 +686,9 @@ static __poll_t ep_scan_ready_list(struct eventpoll *ep,
if (!ep_locked)
mutex_lock_nested(&ep->mtx, depth);
+ if (!depth || list_empty(&ep->rdllist))
+ nwake = 1;
+
/*
* Steal the ready list, and re-init the original one to the
* empty list. Also, set ep->ovflist to NULL so that events
@@ -739,7 +743,7 @@ static __poll_t ep_scan_ready_list(struct eventpoll *ep,
list_splice(&txlist, &ep->rdllist);
__pm_relax(ep->ws);
- if (!list_empty(&ep->rdllist)) {
+ if (nwake && !list_empty(&ep->rdllist)) {
/*
* Wake up (if active) both the eventpoll wait list and
* the ->poll() wait list (delayed after we release the lock).
--
2.23.0
next reply other threads:[~2019-09-02 5:21 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-09-02 5:20 hev [this message]
2019-09-02 15:36 ` [PATCH RESEND] fs/epoll: fix the edge-triggered mode for nested epoll Roman Penyaev
2019-09-03 21:08 ` Jason Baron
2019-09-04 9:57 ` Roman Penyaev
2019-09-04 12:02 ` Jason Baron
2019-09-04 14:02 ` Heiher
2019-09-05 2:53 ` Heiher
2019-09-05 9:56 ` Heiher
2019-09-05 17:27 ` Roman Penyaev
2019-09-05 17:44 ` Jason Baron
2019-09-11 8:19 ` Heiher
2019-09-12 21:36 ` Jason Baron
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190902052034.16423-1-r@hev.cc \
--to=r@hev.cc \
--cc=akpm@linux-foundation.org \
--cc=dave@stgolabs.net \
--cc=davidel@xmailserver.org \
--cc=e@80x24.org \
--cc=jbaron@akamai.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@dominikbrodowski.net \
--cc=rpenyaev@suse.de \
--cc=sridhar.samudrala@intel.com \
--cc=torvalds@linux-foundation.org \
--cc=viro@ZenIV.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).