Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp1882iog; Tue, 28 Jun 2022 14:57:18 -0700 (PDT) X-Google-Smtp-Source: AGRyM1te14UlzFxx8KeL1CLZa4FBI7p7CnA4N2B722V+Feq9TxwaO5RRL6Ts/gWpg8odF0lv8+vW X-Received: by 2002:a17:902:e84a:b0:16a:5435:b5fc with SMTP id t10-20020a170902e84a00b0016a5435b5fcmr7240934plg.119.1656453438532; Tue, 28 Jun 2022 14:57:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656453438; cv=none; d=google.com; s=arc-20160816; b=HQHkoK3zUSzotXbwYFZgxjlM0d3w06vwssQviv9XdXZuHq2CaHEY7BeiGgOj04Ryu4 LJE9k+bGRq2Kq2IWvy19xgkWF4JvhoWmLu/TiH3YBxcqK+YzwoLxZW6lNXA6WYHU7Dnl oBoRCxofbMFUavjvqLprm8cFelv/mHfg/qQpR5jwOKcUMVAdTAp4XktOgX3MWE4jXscn KqrwuFZtKmoCCDLCFmkrpCqnrNPhhOuCi8kX2lGD28qlVtwo1OW9tBrvo3RHmXB4IJ7b OdOk0lFTa288APj18brm3rVzhPLjF2BSxpFrAyt4LN+4f0QuhRVpD6yRRYQ7wInSZqPV mD/A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:message-id:date:subject :cc:to:from:dkim-signature; bh=TeO0t0DJhH40WIl7Vbgx7DQrmpnWJwpLVnwQgvva4x4=; b=a619wxa6xdbgDYZEpiaUplawUcmotRUnx09rv0XOgQmBvkrT8lWXxTIpMjaOnYY6bG XcUq5B9AYoUYwgnVbC67axdCANjuzNib9vXLWGlmWOgOzYNZU5VqWACrvboXjx9+3Kmx qlZHnxUIRSW9Bb10J/Li3okdJcVfeOeW1w1RTV+9N3Zo8zHtX+cf2Yelj/Bu59cJ8Qpw 4rQXZVni0d4SmM/sZFTrNplBGZFt02SjT3CRTAmrtn/+YaYEW1PXUrEosCp0bAxDgS86 DRRFPmlBlNMzjKY3KAWS2SXCmPABOe+lVgkYr/mJFSUV41TtgjdaffKzC7PQYqpWA/mx t7GQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=KRkasxtH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ch16-20020a17090af41000b001e2f3831109si875003pjb.92.2022.06.28.14.56.58; Tue, 28 Jun 2022 14:57:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=KRkasxtH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229455AbiF1Vok (ORCPT + 99 others); Tue, 28 Jun 2022 17:44:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50038 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229436AbiF1Voi (ORCPT ); Tue, 28 Jun 2022 17:44:38 -0400 Received: from mail-pg1-x535.google.com (mail-pg1-x535.google.com [IPv6:2607:f8b0:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 002041B7A4 for ; Tue, 28 Jun 2022 14:44:37 -0700 (PDT) Received: by mail-pg1-x535.google.com with SMTP id g4so6519585pgc.1 for ; Tue, 28 Jun 2022 14:44:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=from:to:cc:subject:date:message-id:user-agent:mime-version; bh=TeO0t0DJhH40WIl7Vbgx7DQrmpnWJwpLVnwQgvva4x4=; b=KRkasxtHfuIGPlHfbWQTNG6foYVaYvPaKcOtI5HwDb2AOGP7QItQDDgNuopHUcga/H OLf3n7av5mB6vG9RPPSkTqynn5wLyvhrtQS9++zeNZuevyPFQD0RwmgxznqgJYXz333S jQot788JZnFFWnlMI++YSrPWVDDzrb+ohqODSWw6L7NVl6WNVfu6vMAYtbp6Abrdhpu8 9kjd8KOy0/0MlZPBJsAZffSgFNZ5fhnFu+W6TPIIJq2TXYo81qIcGwAvW/pLktIHUB2u lzXRGaYqB/abn1lCCnC1zz8aLBgvoom000fBXqmyXUxtTEVEyLr9vHshwXQ8zomi9ECx BZew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:user-agent :mime-version; bh=TeO0t0DJhH40WIl7Vbgx7DQrmpnWJwpLVnwQgvva4x4=; b=oJBvQV+E3AsHY9iB4g/kbdvFmG2ti6+vyuG55SnMmkF5oY7r+A0jiQ9DgAPLARgtdk R+TVIy7K/UyvUY+J1LO/5iwnVTn8f0799/NdZrNOySXu1Su5zYMBbbxgvRhH3WmCt4dy G0dGKnFrZy5NUG9sRwE4yWDnXpEFDvDejMy+kxNz3NGLBiMD5oTMmbDKVgx9ALC9mFhZ AmhMtZJFCcmYKWkQBW0Z8xbeC0/ezel8iD4h8pzLNoo+uZ0I9WdDBee9zBk30YHl4/to yddj7q+X5bZALkdo1ZJuRr9gWO9B1btmVQAgg4EIMrvZ1mNDh8/SBEjgqzk5g8FQ34aR Ml0Q== X-Gm-Message-State: AJIora/FlBYKtazE48OU/MQZYohObtF+6ydZ/3ZjfLk6qoYePF0fLM3m lha7Yjnel2f4cmEtShi99fSXSA== X-Received: by 2002:a05:6a00:218c:b0:525:5236:74c with SMTP id h12-20020a056a00218c00b005255236074cmr6938808pfi.44.1656452677380; Tue, 28 Jun 2022 14:44:37 -0700 (PDT) Received: from bsegall-glaptop.localhost (c-67-188-112-16.hsd1.ca.comcast.net. [67.188.112.16]) by smtp.gmail.com with ESMTPSA id g4-20020a655944000000b003fe4da67980sm9692585pgu.68.2022.06.28.14.44.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Jun 2022 14:44:35 -0700 (PDT) From: Benjamin Segall To: Alexander Viro , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Linus Torvalds , Andrew Morton , Shakeel Butt , Eric Dumazet Subject: [RESEND RFC PATCH] epoll: autoremove wakers even more aggressively Date: Wed, 15 Jun 2022 14:24:23 -0700 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-Spam-Status: No, score=-14.2 required=5.0 tests=BAYES_00,DATE_IN_PAST_96_XX, DKIMWL_WL_MED,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If a process is killed or otherwise exits while having active network connections and many threads waiting on epoll_wait, the threads will all be woken immediately, but not removed from ep->wq. Then when network traffic scans ep->wq in wake_up, every wakeup attempt will fail, and will not remove the entries from the list. This means that the cost of the wakeup attempt is far higher than usual, does not decrease, and this also competes with the dying threads trying to actually make progress and remove themselves from the wq. Handle this by removing visited epoll wq entries unconditionally, rather than only when the wakeup succeeds - the structure of ep_poll means that the only potential loss is the timed_out->eavail heuristic, which now can race and result in a redundant ep_send_events attempt. (But only when incoming data and a timeout actually race, not on every timeout) Signed-off-by: Ben Segall --- fs/eventpoll.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/fs/eventpoll.c b/fs/eventpoll.c index e2daa940ebce..8b56b94e2f56 100644 --- a/fs/eventpoll.c +++ b/fs/eventpoll.c @@ -1745,10 +1745,25 @@ static struct timespec64 *ep_timeout_to_timespec(struct timespec64 *to, long ms) ktime_get_ts64(&now); *to = timespec64_add_safe(now, *to); return to; } +/* + * autoremove_wake_function, but remove even on failure to wake up, because we + * know that default_wake_function/ttwu will only fail if the thread is already + * woken, and in that case the ep_poll loop will remove the entry anyways, not + * try to reuse it. + */ +static int ep_autoremove_wake_function(struct wait_queue_entry *wq_entry, + unsigned int mode, int sync, void *key) +{ + int ret = default_wake_function(wq_entry, mode, sync, key); + + list_del_init(&wq_entry->entry); + return ret; +} + /** * ep_poll - Retrieves ready events, and delivers them to the caller-supplied * event buffer. * * @ep: Pointer to the eventpoll context. @@ -1826,12 +1841,19 @@ static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events, * chance to harvest new event. Otherwise wakeup can be * lost. This is also good performance-wise, because on * normal wakeup path no need to call __remove_wait_queue() * explicitly, thus ep->lock is not taken, which halts the * event delivery. + * + * In fact, we now use an even more aggressive function that + * unconditionally removes, because we don't reuse the wait + * entry between loop iterations. This lets us also avoid the + * performance issue if a process is killed, causing all of its + * threads to wake up without being removed normally. */ init_wait(&wait); + wait.func = ep_autoremove_wake_function; write_lock_irq(&ep->lock); /* * Barrierless variant, waitqueue_active() is called under * the same lock on wakeup ep_poll_callback() side, so it -- 2.36.1.476.g0c4daa206d-goog