Received: by 2002:a05:6358:5282:b0:b5:90e7:25cb with SMTP id g2csp3464407rwa; Tue, 23 Aug 2022 05:19:14 -0700 (PDT) X-Google-Smtp-Source: AA6agR6f5c4nmE1uRdcaokzJvfRk4rAwYuf7RGQPKJTCQ90pPr4MyTiz+XE00VxCxAWUEWvLRfHJ X-Received: by 2002:a17:90b:3ec3:b0:1fa:a293:c511 with SMTP id rm3-20020a17090b3ec300b001faa293c511mr3127175pjb.156.1661257154237; Tue, 23 Aug 2022 05:19:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661257154; cv=none; d=google.com; s=arc-20160816; b=VcAqcQ++rXbMrnU2PdaUokLRv06yXefNCYan1j6XegHxFmf+suxk6/je2QAeOUQi4p KH0PZaa0j83PZyUlB2bu78drfpts/Hk6wxfDEnNYofaXyV02de9/mVuOWKCsEfWgaG3D OKBKsVOf4zFX9RLEJAWyRQyyCCtG0D1zPvoBldwTxwRGCb0a89vhyjvNBZoEprxGVJOD kUnV72n9+LlHhwWSCOaS/DGtlkteb+1J5dVRl0W7/AtU8z/NTvFLU4TZZfqh4G19RBfX /nXT6QlZSWIoeNihnnT21y3sdL13ywMaHBrFNgLAKOZxaBurNS0s2NT+r+dJfydFRLNx ZHxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=oWXTpt3eRhzG/BQFmviU20AT/ZjDPwXNlX/YkvMyRco=; b=ynLGrq1gU8R+nkCXs/RAxHbry7qeAfvJelwgeHcQInGjnqgBAgFoDBQurltuMPtUhp zkuZqirNoLbhtRy4JrYZx79yIBzYHmYxuUlBXsj0+DE0ewETWlHN+dDZPjqW8+/80m3S qcxwK0JxxXJSwmAw7t9ty7WCcaKabJ/lFHgIC+IN4y7ZDVKnmUd21+a1OyzwvcKs2ab0 TjAqx7aX06uPic4LAQOFR+nchIJCc+lDUuHILzKD434rmICMHWLYxY524gVnvpJG6gdd DL/1ryk1WKdc6lwkMHnrV1c8f2LabbjNI8J0Woj0JQNs2pJsuln1bazqcDCfvfiCPCR0 /MOA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=z344QA2W; 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=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c1-20020a170902aa4100b00172a54835b7si12741250plr.612.2022.08.23.05.19.03; Tue, 23 Aug 2022 05:19:14 -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=@linuxfoundation.org header.s=korg header.b=z344QA2W; 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=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349692AbiHWLNM (ORCPT + 99 others); Tue, 23 Aug 2022 07:13:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60054 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1357558AbiHWLLP (ORCPT ); Tue, 23 Aug 2022 07:11:15 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EC274B7750; Tue, 23 Aug 2022 02:17:41 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 75733B81B1F; Tue, 23 Aug 2022 09:17:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 94BEFC433D6; Tue, 23 Aug 2022 09:17:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1661246257; bh=y43SoCtlZ8AuT7tYECJwTI0ihJ0YaFxSqTqZepbZkYo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=z344QA2WUSSj1gzo7DD+iSfRHZw2XuXKc+VRQLE1ujNvY4bmbYzvKh3Y7BeqzkL1b aCVg2HiBWUreTO8YnPiPpx3dPnbZPsJwq9faxfzDHoGVAkA1Rnp4mEDgx/K6q7AuL1 AGYOfHJtxRD6uaYcMRf42eWBW6uibNnsAQpqYTL4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ben Segall , Shakeel Butt , Alexander Viro , Linus Torvalds , Eric Dumazet , Roman Penyaev , Jason Baron , Khazhismel Kumykov , Heiher , stable@kernel.org, Andrew Morton Subject: [PATCH 5.4 051/389] epoll: autoremove wakers even more aggressively Date: Tue, 23 Aug 2022 10:22:09 +0200 Message-Id: <20220823080117.738248512@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220823080115.331990024@linuxfoundation.org> References: <20220823080115.331990024@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 From: Benjamin Segall commit a16ceb13961068f7209e34d7984f8e42d2c06159 upstream. 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) Shakeel added: : We are seeing this issue in production with real workloads and it has : caused hard lockups. Particularly network heavy workloads with a lot : of threads in epoll_wait() can easily trigger this issue if they get : killed (oom-killed in our case). Link: https://lkml.kernel.org/r/xm26fsjotqda.fsf@google.com Signed-off-by: Ben Segall Tested-by: Shakeel Butt Cc: Alexander Viro Cc: Linus Torvalds Cc: Shakeel Butt Cc: Eric Dumazet Cc: Roman Penyaev Cc: Jason Baron Cc: Khazhismel Kumykov Cc: Heiher Cc: Signed-off-by: Andrew Morton Signed-off-by: Greg Kroah-Hartman --- fs/eventpoll.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) --- a/fs/eventpoll.c +++ b/fs/eventpoll.c @@ -1803,6 +1803,21 @@ static inline struct timespec64 ep_set_m return timespec64_add_safe(now, ts); } +/* + * 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. @@ -1880,8 +1895,15 @@ fetch_events: * 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); __add_wait_queue_exclusive(&ep->wq, &wait); write_unlock_irq(&ep->lock);