Received: by 2002:a25:824b:0:0:0:0:0 with SMTP id d11csp1782983ybn; Thu, 26 Sep 2019 02:05:33 -0700 (PDT) X-Google-Smtp-Source: APXvYqzWjRqnLVY2EtfAqT9uG3LXhxmR9y36fW+PCAQ12Z6ZQ4Q3dDvfGpc5oTaKr5D6X1e5iDfw X-Received: by 2002:a50:d903:: with SMTP id t3mr2296762edj.117.1569488733794; Thu, 26 Sep 2019 02:05:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1569488733; cv=none; d=google.com; s=arc-20160816; b=Hb3ry43mRl6aKXzSq1HNCjDKzBwV70uJ7pm8HUe7EOYzsnJr+pZSXcvmd+ZyvWfdIW TS+99MA9SCj5w2uyxTzssDQ+DEkQqdvU68h0diNG85jXrD8plfWPp1TkRFU5KGna5nvA 50ePmurzoLfklgvvhvqSW8wEOePsY9g27cBZpmDjiqev5VvzK78Cnbr9lTn0/Rwo4bX5 h5sPyvi6MabX2h2XNTvAv3AXUNnk7ziZ92gsjm0JbN0pFQzytsNtJlumzw7af+sg/Bqd dtMXb2biTXejnLcGK9cBCYOPI2siQ7mQdEPOlnqPiceb2Z4tVAXeRs3lzj18DuKoL++1 3rdQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=XF6jmOvtyrBMaqdt7hPfORfmiOBGH0e252B9Q5r9KVc=; b=HTME+7ybVWlxsqzFIlZQ06nHaU19EpbBiNC6/l75aPdpcvDUYdsq208dYWuNT5Utzp XIz/PoUGrDbrzNRi0oGE+fR7nveSNlA6XaAiztWr+mytr5Cr9+8TwWfpnOMa84qRToIt wG30diLB+g2g5AbgOJvvGsjHe3qtvmo36OsIIj1I2pf9jPuJTOuHDi2OR/xhrN3CrWB6 t7eA1g7LUzCbHgiaJJXbRjeixdw021AKTynjtlln0gK1l46VUu7+oftbdHr+LIsmJ0yQ aTkkuyBnGugrDruKSFZXR6M006ifzsLPmQruY/yd9s9vDBD6Iso/cm9+ahwkR2LvhQZU a6lQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@hev-cc.20150623.gappssmtp.com header.s=20150623 header.b=sn99UHj6; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id bt13si902786edb.245.2019.09.26.02.05.09; Thu, 26 Sep 2019 02:05:33 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@hev-cc.20150623.gappssmtp.com header.s=20150623 header.b=sn99UHj6; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2633698AbfIYB4Y (ORCPT + 99 others); Tue, 24 Sep 2019 21:56:24 -0400 Received: from mail-oi1-f194.google.com ([209.85.167.194]:39229 "EHLO mail-oi1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2633689AbfIYB4X (ORCPT ); Tue, 24 Sep 2019 21:56:23 -0400 Received: by mail-oi1-f194.google.com with SMTP id w144so3496415oia.6 for ; Tue, 24 Sep 2019 18:56:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hev-cc.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=XF6jmOvtyrBMaqdt7hPfORfmiOBGH0e252B9Q5r9KVc=; b=sn99UHj6vmUiCfF9Qt2YuozEa4TDgMGJD/uVFjfTtYh4ZBdfL+L3vsmeWsIyb0s/Zk Fol14MrBW5JV9dZoUnG9YTyoj336NdpOjNiZ7wHC7B1QUWn++yotMppCp6AWKoE916/W mmJHjzktF6EdtXLr/Mlm8iBJ4rnweuQcgISHw6UfuucYKLhwF2KQAi/6e4qDOVnWLW5y 6iyyVyot/AsDAl0rNPuMXe395vmBETZnnMu0dsM0DvDI2wnlg84IlRLkLvje45q8FA5X TpcM2ecwMpX/fE3CLVZUTGw8miu8l+zU6PcIoa4jikL9XMmSmMCE2QdzQJR8jpnQl+rl 3eeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=XF6jmOvtyrBMaqdt7hPfORfmiOBGH0e252B9Q5r9KVc=; b=bTzviv9rq8WJ276IVM4eXG3n6wJPKWgaPvzR8XJkFPJYbNJNv+b6kHtL0vJCy/bJLJ 47d+Aac55blO6dKTu0wWMVitQR+8vzhZ3dnyNUpD14Qv3tvo8LAmbpQASXVnOfnKADfz ruy0ezD2JPqOPvrJFPsIG6vNL/H+XGkGr+t7VOsDNqBvMTBZnWOFq9Wyec4VjDWMRJj7 +F3DoYdkniGKQIoOdthyI2p+LGGNQokqhMK3EG7SMTDa+IVR1CCu9EOgxzxH1VobOdb5 nXCODW+l7Sp7z8IP5PJrryU5jiQ0zOIlFR+G5sj8Gxd4adtxFpvLw1ysk+BVEzPS2X7U QzQw== X-Gm-Message-State: APjAAAVjfMWfSfSbxla3KFbx8f0+WPq95z5ZHO+CBHmFGtTn2hs1bQTU vT/gwtYT9Tt+8gviQNBJR7RLpQ== X-Received: by 2002:a05:6808:302:: with SMTP id i2mr2804825oie.176.1569376581500; Tue, 24 Sep 2019 18:56:21 -0700 (PDT) Received: from hev-sbc.hz.ali.com ([47.89.83.40]) by smtp.gmail.com with ESMTPSA id d69sm1190658oig.32.2019.09.24.18.56.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Sep 2019 18:56:20 -0700 (PDT) From: hev To: linux-fsdevel@vger.kernel.org Cc: Heiher , Al Viro , Andrew Morton , Davide Libenzi , Davidlohr Bueso , Dominik Brodowski , Eric Wong , Jason Baron , Linus Torvalds , Roman Penyaev , Sridhar Samudrala , linux-kernel@vger.kernel.org Subject: [PATCH RESEND v4] fs/epoll: Remove unnecessary wakeups of nested epoll that in ET mode Date: Wed, 25 Sep 2019 09:56:03 +0800 Message-Id: <20190925015603.10939-1-r@hev.cc> X-Mailer: git-send-email 2.23.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Heiher 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 only need to wakeup nested epoll fds if something has been queued to the overflow list, since the ep_poll() traverses the rdllist during recursive poll and thus events on the overflow list may not be visible yet. Test code: #include #include #include 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 Cc: Andrew Morton Cc: Davide Libenzi Cc: Davidlohr Bueso Cc: Dominik Brodowski Cc: Eric Wong Cc: Jason Baron Cc: Linus Torvalds Cc: Roman Penyaev Cc: Sridhar Samudrala Cc: linux-kernel@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org Signed-off-by: hev --- fs/eventpoll.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/fs/eventpoll.c b/fs/eventpoll.c index c4159bcc05d9..a0c07f6653c6 100644 --- a/fs/eventpoll.c +++ b/fs/eventpoll.c @@ -704,12 +704,21 @@ static __poll_t ep_scan_ready_list(struct eventpoll *ep, res = (*sproc)(ep, &txlist, priv); write_lock_irq(&ep->lock); + nepi = READ_ONCE(ep->ovflist); + /* + * We only need to wakeup nested epoll fds if something has been queued + * to the overflow list, since the ep_poll() traverses the rdllist + * during recursive poll and thus events on the overflow list may not be + * visible yet. + */ + if (nepi != NULL) + pwake++; /* * During the time we spent inside the "sproc" callback, some * other events might have been queued by the poll callback. * We re-insert them inside the main ready-list here. */ - for (nepi = READ_ONCE(ep->ovflist); (epi = nepi) != NULL; + for (; (epi = nepi) != NULL; nepi = epi->next, epi->next = EP_UNACTIVE_PTR) { /* * We need to check if the item is already in the list. @@ -755,7 +764,7 @@ static __poll_t ep_scan_ready_list(struct eventpoll *ep, mutex_unlock(&ep->mtx); /* We have to call this outside the lock */ - if (pwake) + if (pwake == 2) ep_poll_safewake(&ep->poll_wait); return res; -- 2.23.0