From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C8E11C3A5A3 for ; Fri, 30 Aug 2019 05:45:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 8CF0421721 for ; Fri, 30 Aug 2019 05:45:08 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=hev-cc.20150623.gappssmtp.com header.i=@hev-cc.20150623.gappssmtp.com header.b="GI1KfcM4" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727738AbfH3FpH (ORCPT ); Fri, 30 Aug 2019 01:45:07 -0400 Received: from mail-ot1-f65.google.com ([209.85.210.65]:34593 "EHLO mail-ot1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726023AbfH3FpH (ORCPT ); Fri, 30 Aug 2019 01:45:07 -0400 Received: by mail-ot1-f65.google.com with SMTP id c7so5872375otp.1 for ; Thu, 29 Aug 2019 22:45:06 -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=5xuuSwgJWCqcDc1AbsIjp7yN/mRhhcSgBhiJdsYZVCA=; b=GI1KfcM4GYbh05OZLb000/qx2D4gy8snqMb4Nhu0mPHzjXiIQGXvxjYUu8geuOgkEB W+7KNsA2w9g1tN5YyxSNCuuG/SgM0+hEqdr7pO0ovTrxikZ55cXaGkJLeiAX9ICEef0S MkaShiZ1rT5cIDvil50XfiJD2diLLxqwk+So/ITlpV4VnZpykUPf78Qk/8IghjPUNAYx s7NMYkKy2G7zYAM5ZRu/zmDYl3w2FlqcK3Vu6ES8cPh9nOPsb7sS3nqZVr7jcXJbcOtz OrTpNN/50fQWFlzosGaObZK0aVCbv7wFfdFYqtHdIiRcjVPagMNnLydCj5TMZLfIsa0L PiqA== 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=5xuuSwgJWCqcDc1AbsIjp7yN/mRhhcSgBhiJdsYZVCA=; b=Q7HLjsqfrdKYth10g1R+I/ipChZSA3XD8LxMjWLD3I1HDcX7bPP+RSY6RHdWQPS3PN umWBpC9MLc8h3VBEHowk5kPjHbPmTUDYcDB/FS9D6gIuzzxTsb3A4lQGiaGQGZGBaw99 aT36iRg4IvaHy3PBTgoKL0IDbMU7RqBnoZvOSyyyLIntxKfSjQ1TLVfPcxGu6vJB44i0 bRg5wO9EhIWJfAK8T9qhNaDpQ1LPO9XylTp3xk2Ek4a7ZPV/jDaEZW3/ZH+WG+fLHW3/ g5XD9GLInYhyNHrMtqsMoWlqV+plUpa9Ugdzfddn3LyA78FSQfdEAENma9UFNOuvWBx7 rhew== X-Gm-Message-State: APjAAAXKjLyjbhhazTRZsO+F/sMJsJKQfamkSmEGcgmLAfaRmCjAiNwe zFcMhzpDGURl8+742D5B1coZSg== X-Google-Smtp-Source: APXvYqzGUyuAkVpBq01B1WkMHWncGpb/6RmallypNCRKvbAeHOKYuZlwyLICrAHdHcpk3ucJyNP5ug== X-Received: by 2002:a9d:6852:: with SMTP id c18mr11051720oto.218.1567143905919; Thu, 29 Aug 2019 22:45:05 -0700 (PDT) Received: from hev-sbc.hz.ali.com ([47.89.83.40]) by smtp.gmail.com with ESMTPSA id l14sm1310424oii.27.2019.08.29.22.45.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Aug 2019 22:45:04 -0700 (PDT) From: hev To: linux-fsdevel@vger.kernel.org Cc: e@80x24.org, Heiher , linux-kernel@vger.kernel.org Subject: [PATCH] fs/epoll: fix the edge-triggered mode for epoll itself Date: Fri, 30 Aug 2019 13:44:57 +0800 Message-Id: <20190830054457.5445-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 The structure of event pools: efd[2]: { sfd[0] (EPOLLIN) } efd[1]: { efd[2] (EPOLLIN) } efd[0]: { efd[2] (EPOLLIN | EPOLLET) } 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. #include #include #include #include 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[1]); close(efd[0]); close(sfd[0]); close(sfd[1]); printf("SUCC\n"); return 0; out: printf("FAIL\n"); return -1; } Signed-off-by: hev Cc: Eric Wong Cc: linux-kernel@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org --- 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