Skip to contentSkip to conversation

fs/epoll: Remove unnecessary wakeups of nested epoll

PATCH RESEND In review hev 2 participants 3 messages Oct 9, 2019 – Oct 28, 2019

Subsystems (2)
lore
Share & tools
lore

hev

author
#0 lore raw

PATCH RESEND v5 1/1 fs/epoll: Remove unnecessary wakeups of nested epoll

From: Heiher <[email protected]>

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

Cc Al Viro <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Davide Libenzi <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Dominik Brodowski <[email protected]>
Cc Eric Wong <[email protected]>
Cc Jason Baron <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Roman Penyaev <[email protected]>
Cc Sridhar Samudrala <[email protected]>
---
 fs/eventpoll.c | 16 ----------------
 1 file changed, 16 deletions(-)
fs/eventpoll.c +0 -16
index c4159bcc05d9..75fccae100b5 100644
@@ -671,7 +671,6 @@ static __poll_t ep_scan_ready_list(struct eventpoll *ep,
 			      void *priv, int depth, bool ep_locked)
 {
 	__poll_t res;
-	int pwake = 0;
 	struct epitem *epi, *nepi;
 	LIST_HEAD(txlist);
 
@@ -738,26 +737,11 @@ static __poll_t ep_scan_ready_list(struct eventpoll *ep,
 	 */
 	list_splice(&txlist, &ep->rdllist);
 	__pm_relax(ep->ws);
-
-	if (!list_empty(&ep->rdllist)) {
-		/*
-		 * Wake up (if active) both the eventpoll wait list and
-		 * the ->poll() wait list (delayed after we release the lock).
-		 */
-		if (waitqueue_active(&ep->wq))
-			wake_up(&ep->wq);
-		if (waitqueue_active(&ep->poll_wait))
-			pwake++;
-	}
 	write_unlock_irq(&ep->lock);
 
 	if (!ep_locked)
 		mutex_unlock(&ep->mtx);
 
-	/* We have to call this outside the lock */
-	if (pwake)
-		ep_poll_safewake(&ep->poll_wait);
-
 	return res;
 }
 
Signature · 1 line
2.23.0
Reviewed-by on v5 1/1
#1 lore raw
Quoted 123 lines · hev

From: Heiher <[email protected]>

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[])
{

(quoted diff) +0 -0
 	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

Cc Al Viro <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Davide Libenzi <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Dominik Brodowski <[email protected]>
Cc Eric Wong <[email protected]>
Cc Jason Baron <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Roman Penyaev <[email protected]>
Cc Sridhar Samudrala <[email protected]>
---
 fs/eventpoll.c | 16 ----------------
 1 file changed, 16 deletions(-)
fs/eventpoll.c +0 -12
index c4159bcc05d9..75fccae100b5 100644
@@ -671,7 +671,6 @@ static __poll_t ep_scan_ready_list(struct eventpoll 
 *ep,
 			      void *priv, int depth, bool ep_locked)
 {
 	__poll_t res;
-	int pwake = 0;
 	struct epitem *epi, *nepi;
 	LIST_HEAD(txlist);
@@ -738,26 +737,11 @@ static __poll_t ep_scan_ready_list(struct 
 eventpoll *ep,
 	 */
 	list_splice(&txlist, &ep->rdllist);
 	__pm_relax(ep->ws);
-
-	if (!list_empty(&ep->rdllist)) {
-		/*
-		 * Wake up (if active) both the eventpoll wait list and
-		 * the ->poll() wait list (delayed after we release the lock).
-		 */
-		if (waitqueue_active(&ep->wq))
-			wake_up(&ep->wq);
-		if (waitqueue_active(&ep->poll_wait))
-			pwake++;
-	}
 	write_unlock_irq(&ep->lock);

fs/eventpoll.c static __poll_t ep_scan_ready_list(struct · line 747

Quoted 10 lines
fs/eventpoll.c +0 -4
 
 	if (!ep_locked)
 		mutex_unlock(&ep->mtx);
 
-	/* We have to call this outside the lock */
-	if (pwake)
-		ep_poll_safewake(&ep->poll_wait);
-
 	return res;
 }

This looks good to me. Heiher, mind to make kselftest out of your test suite?

--
Roman

author
#2 lore raw

Hi,

Quoted 140 lines · Roman Penyaev
Quoted 124 lines · hev

From: Heiher <[email protected]>

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

Cc Al Viro <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Davide Libenzi <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Dominik Brodowski <[email protected]>
Cc Eric Wong <[email protected]>
Cc Jason Baron <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Roman Penyaev <[email protected]>
Cc Sridhar Samudrala <[email protected]>
---
 fs/eventpoll.c | 16 ----------------
 1 file changed, 16 deletions(-)
fs/eventpoll.c +0 -12
index c4159bcc05d9..75fccae100b5 100644
@@ -671,7 +671,6 @@ static __poll_t ep_scan_ready_list(struct eventpoll
 *ep,
                            void *priv, int depth, bool ep_locked)
 {
      __poll_t res;
-     int pwake = 0;
      struct epitem *epi, *nepi;
      LIST_HEAD(txlist);
@@ -738,26 +737,11 @@ static __poll_t ep_scan_ready_list(struct
 eventpoll *ep,
       */
      list_splice(&txlist, &ep->rdllist);
      __pm_relax(ep->ws);
-
-     if (!list_empty(&ep->rdllist)) {
-             /*
-              * Wake up (if active) both the eventpoll wait list and
-              * the ->poll() wait list (delayed after we release the lock).
-              */
-             if (waitqueue_active(&ep->wq))
-                     wake_up(&ep->wq);
-             if (waitqueue_active(&ep->poll_wait))
-                     pwake++;
-     }
      write_unlock_irq(&ep->lock);

fs/eventpoll.c static __poll_t ep_scan_ready_list(struct · line 747

Quoted 10 lines
fs/eventpoll.c +0 -4
 
      if (!ep_locked)
              mutex_unlock(&ep->mtx);
 
-     /* We have to call this outside the lock */
-     if (pwake)
-             ep_poll_safewake(&ep->poll_wait);
-
      return res;
 }

This looks good to me. Heiher, mind to make kselftest out of your test suite?

Quoted 7 lines

--
Roman

Need to back port this patch to stable branches?

Signature · 3 lines
Best regards!
Hev
https://hev.cc