Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp754522ybe; Mon, 2 Sep 2019 08:38:19 -0700 (PDT) X-Google-Smtp-Source: APXvYqx+J2/jWRG6oY6Pofv0/Pk0EMKjZkX96ynaxSixW13XzLWQ7Squdd/q5/Sb4aztUaZHkQFB X-Received: by 2002:a17:902:1c7:: with SMTP id b65mr29865377plb.313.1567438699468; Mon, 02 Sep 2019 08:38:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1567438699; cv=none; d=google.com; s=arc-20160816; b=HQ4rgokmbPBWmplLq/aV88IBOk47DZSZR/1XLJ1yozZsqwoj4oncgz6kzSUoDW18lo jKbZwxQ3eWebxOMkWNv7wadKVNz0tc9QTHunfks0dQZDj3MnTG9rlP4kDpRpNgcubpYW op+BKn9K/zlkuTlkD4wMnM8Ky3h5Jg7KFzuteu7u1rjTYA2YzaFZNVeFSrga0BZn0lkr ZV/sGs164PaWMioYL6F6I7olssKU8vGvSzFfyLbp6P9o7B6EzoA1E9CBHzsUJQH5kwO0 LFP2V/iQdGD7fSyd8HL/r68ZnUzg28FxxTcTcOvtUnYYH7OmZlyMVbmEjGm3cWE7ALYv dHQw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:message-id:references :in-reply-to:subject:cc:to:from:date:content-transfer-encoding :mime-version; bh=1MsFViCji6ELJt5vgEfWjHQ38VOWd/xCZ7l/soYmgco=; b=xDtuW7kD/sv0umE9qkuf3ZdjsO8RFKvMeSqZMA62Ea8XRBgN7ToCIFFwaLIHkEibXJ 2U3WPhfAAHnjf+AjdSa956hv8EB1ZLGLsUKtc1F6BK6x+7Hxj5SaDLN+ZCxagVxCZGpE fVmycmcA7ZIaqVkCYReHCjY9jqBrpJh8ZMupwfc/8rMGEt0AF9qDO06UQk8l/Jukf58h 27UaR/3CAtgvdDkywUzt6vt56OoRBgmXKfKm7Tqppv5shS9szwcth9mBf/N80DQjziiL bbEVpyAx5J+h1MeN+cHO7WMkm+bFlmP7eq4gZKfsDnXh4xYmGciga/wvrPwNI4g8DwhR KFCg== ARC-Authentication-Results: i=1; mx.google.com; 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 j6si11733736pgb.206.2019.09.02.08.38.02; Mon, 02 Sep 2019 08:38:19 -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; 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 S1725867AbfIBPhA (ORCPT + 99 others); Mon, 2 Sep 2019 11:37:00 -0400 Received: from mx2.suse.de ([195.135.220.15]:48684 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725806AbfIBPhA (ORCPT ); Mon, 2 Sep 2019 11:37:00 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id CD57AACC9; Mon, 2 Sep 2019 15:36:57 +0000 (UTC) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit Date: Mon, 02 Sep 2019 17:36:54 +0200 From: Roman Penyaev To: hev Cc: linux-fsdevel@vger.kernel.org, e@80x24.org, Al Viro , Andrew Morton , Davide Libenzi , Davidlohr Bueso , Dominik Brodowski , Jason Baron , Linus Torvalds , Sridhar Samudrala , linux-kernel@vger.kernel.org Subject: Re: [PATCH RESEND] fs/epoll: fix the edge-triggered mode for nested epoll In-Reply-To: <20190902052034.16423-1-r@hev.cc> References: <20190902052034.16423-1-r@hev.cc> Message-ID: <0cdc9905efb9b77b159e09bee17d3ad4@suse.de> X-Sender: rpenyaev@suse.de User-Agent: Roundcube Webmail Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi, This is indeed a bug. (quick side note: could you please remove efd[1] from your test, because it is not related to the reproduction of a current bug). Your patch lacks a good description, what exactly you've fixed. Let me speak out loud and please correct me if I'm wrong, my understanding of epoll internals has become a bit rusty: when epoll fds are nested an attempt to harvest events (ep_scan_ready_list() call) produces a second (repeated) event from an internal fd up to an external fd: epoll_wait(efd[0], ...): ep_send_events(): ep_scan_ready_list(depth=0): ep_send_events_proc(): ep_item_poll(): ep_scan_ready_list(depth=1): ep_poll_safewake(): ep_poll_callback() list_add_tail(&epi, &epi->rdllist); ^^^^^^ repeated event In your patch you forbid wakeup for the cases, where depth != 0, i.e. for all nested cases. That seems clear. But what if we can go further and remove the whole chunk, which seems excessive: @@ -885,26 +886,11 @@ static __poll_t ep_scan_ready_list(struct eventpoll *ep, - - 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); I reason like that: by the time we've reached the point of scanning events for readiness all wakeups from ep_poll_callback have been already fired and new events have been already accounted in ready list (ep_poll_callback() calls the same ep_poll_safewake()). Here, frankly, I'm not 100% sure and probably missing some corner cases. Thoughts? PS. You call list_empty(&ep->rdllist) without ep->lock taken, that is fine, but you should be _careful_, so list_empty_careful(&ep->rdllist) call instead. -- Roman On 2019-09-02 07:20, hev wrote: > From: Heiher > > 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 > #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[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 > 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 | 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).