Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp7041117imu; Wed, 14 Nov 2018 10:45:05 -0800 (PST) X-Google-Smtp-Source: AJdET5eRRSmSW+JVsNPDVlx1RpSVAsxUsCzydKQlbQh1hVoIQWDT0tGZMpPI5pHJHS5vbE0YfvXN X-Received: by 2002:a63:cd4c:: with SMTP id a12mr2880045pgj.252.1542221105064; Wed, 14 Nov 2018 10:45:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542221104; cv=none; d=google.com; s=arc-20160816; b=BOTHwTXfDxewJ0kxxhiLz4vBJWIaBQmclETeR1MkpeqGWR9/excA8GBLUEOMb+Oruz s1Ot7h5+KlPeeswhT5dZ1ricG2rsVZIfzIQm76/F2JH0cF71aggabKNFbBfsbQwPP2Id l+h5GuMYKNXRzSNHep96jYCNVNFasXD2kSIjORIg+4j8gzKkt/bx11816+eKYTd2FNgk V7RhynQA166ADtznNxRolcZdQkSyO37j711v9XzwW4UZW8s3r0PR2fzJLvhL/m8wMzVE tMiKe05EBuR4StvwReMSK2A27sAnyH1AUPPZVFgC9UHkbvKRnzI6Ta222yV5+MceauLN z3mg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=YFiSfSDZ3soSv8JOfMqkq25TtWqcvFX2dsdNtC6LSWc=; b=rLIHWzPt8fKCgraYHiQgsAQXvLfX7GE80RNlQNrA0l9vsmQN1Ghs5gSl0mNYeUG0AU EHh4UJaHgQVIpQVIZkAWZ1+fAvIxkoC+IVyYyVhGkb78uYa8QJLVoRdLvcaGRSWuWHnx uVLLNEF5tdKBVP6/Km8IU3SIZoHVFc4NDINyiZv8SBQezt1sP4VI32RRf1+wM4ZBz4Du Yhmos3e+PeUIZSK7XVkMGp8q7xneU58RvWDnLZ8tOO4hy4Y/DxEz5QBWhY5cAvHuR2rr fskyoBz5p5DMQbcxLDh4aO4tAzYYWmzNjzgz0ha/0wGBiNRk3tIoCtEt0djt+EPefB4y GWyg== 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 d2-v6si24887525plo.210.2018.11.14.10.44.47; Wed, 14 Nov 2018 10:45:04 -0800 (PST) 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 S1731710AbeKOEaL (ORCPT + 99 others); Wed, 14 Nov 2018 23:30:11 -0500 Received: from smtp2.provo.novell.com ([137.65.250.81]:59757 "EHLO smtp2.provo.novell.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727576AbeKOEaL (ORCPT ); Wed, 14 Nov 2018 23:30:11 -0500 Received: from localhost.localdomain (prv-ext-foundry1int.gns.novell.com [137.65.251.240]) by smtp2.provo.novell.com with ESMTP (TLS encrypted); Wed, 14 Nov 2018 11:25:43 -0700 From: Davidlohr Bueso To: akpm@linux-foundation.org Cc: jbaron@akamai.com, viro@zeniv.linux.org.uk, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, dave@stgolabs.net, Davidlohr Bueso Subject: [PATCH 2/2] fs/epoll: deal with wait_queue only once Date: Wed, 14 Nov 2018 10:25:32 -0800 Message-Id: <20181114182532.27981-2-dave@stgolabs.net> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20181114182532.27981-1-dave@stgolabs.net> References: <20181114182532.27981-1-dave@stgolabs.net> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org There is no reason why we rearm the waitiqueue upon every fetch_events retry (for when events are found yet send_events() fails). If nothing else, this saves four lock operations per retry, and furthermore reduces the scope of the lock even further. Signed-off-by: Davidlohr Bueso --- fs/eventpoll.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/fs/eventpoll.c b/fs/eventpoll.c index 7e2b5f3d6b3e..25b0c94cc091 100644 --- a/fs/eventpoll.c +++ b/fs/eventpoll.c @@ -1749,6 +1749,7 @@ static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events, { int res = 0, eavail, timed_out = 0; u64 slack = 0; + bool waiter = false; wait_queue_entry_t wait; ktime_t expires, *to = NULL; @@ -1786,6 +1787,15 @@ static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events, if (eavail) goto send_events; + if (!waiter) { + waiter = true; + init_waitqueue_entry(&wait, current); + + spin_lock_irq(&ep->wq.lock); + __add_wait_queue_exclusive(&ep->wq, &wait); + spin_unlock_irq(&ep->wq.lock); + } + /* * Busy poll timed out. Drop NAPI ID for now, we can add * it back in when we have moved a socket with a valid NAPI @@ -1798,10 +1808,6 @@ static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events, * We need to sleep here, and we will be wake up by * ep_poll_callback() when events will become available. */ - init_waitqueue_entry(&wait, current); - spin_lock_irq(&ep->wq.lock); - __add_wait_queue_exclusive(&ep->wq, &wait); - spin_unlock_irq(&ep->wq.lock); for (;;) { /* @@ -1836,10 +1842,6 @@ static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events, __set_current_state(TASK_RUNNING); - spin_lock_irq(&ep->wq.lock); - __remove_wait_queue(&ep->wq, &wait); - spin_unlock_irq(&ep->wq.lock); - send_events: /* * Try to transfer events to user space. In case we get 0 events and @@ -1850,6 +1852,12 @@ static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events, !(res = ep_send_events(ep, events, maxevents)) && !timed_out) goto fetch_events; + if (waiter) { + spin_lock_irq(&ep->wq.lock); + __remove_wait_queue(&ep->wq, &wait); + spin_unlock_irq(&ep->wq.lock); + } + return res; } -- 2.16.4