Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp1882306rwb; Fri, 19 Aug 2022 11:01:15 -0700 (PDT) X-Google-Smtp-Source: AA6agR6jy1cuW02N1zLXoQ+Vi9jA6OB5TNoqIyKxs7pitHy5ravvI5xAklg+JfNnT11dqpLSUAOR X-Received: by 2002:a17:902:d509:b0:16f:1e1:2067 with SMTP id b9-20020a170902d50900b0016f01e12067mr8364571plg.140.1660932075047; Fri, 19 Aug 2022 11:01:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660932075; cv=none; d=google.com; s=arc-20160816; b=0e33fP28ZZZ3poJR0tjf1YaEK8TvJ2N15ndFg6eLQ+SzR28oOF0Tciq037lrfTE3G5 s76nAE/nNtyGbQaUzt2LT7ixl0jLyCF0ZUOSLdhQm0JB/73n1P+7lDeEI0T3fB8gokF+ Odd2ZnhijDUnwhlWtrQ3tdYpMVLkU+SYxa4DaNotNP5S+acYFPIueVYHgwMvEOg0MRA5 a2uQRmPxNKKVHgxA4E5lw1KxbDOT1ND+sb6sAAP3yPkgcFnBch6TzOB45DFArf5fmTmP RDBQYO9IFd7dpVE/HIGEd/OkRQq35xpotrOCH23NbwDd4zudjXr3Iv8EYBpe35fvzhsV lBDg== 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=65DlmUPE5TMiL/fX4Qiy7eaWQfWvv921FUatF0cxdU4=; b=bIqHvUhqNYpTOs1wfqlYo+46vhBEgOGpI/b60s0SdFWleJC014omEPp0HqsRKu371W tCgU5TwwamsD9Y0j3Ab2PvD93giXk4XaA969YQon19NnfQDTeonUFltgJ58c5ipcZVV1 z8E+LFvXd6zl86SGTFlcjhq+Y/+dzKod0Y91/lWNP3BheCQ2qXcVtCjq4sI4jJLhiyTg SHzv/4N0uy5ht8nxwfVhaoX2ykorB0c5ZsCnvBdLh+DcTynh9TOFa3+rHdID5/thXwXV EnY99b+iz9rsC+GnVIp+fvR/aXqLUouyg+uEAkcxjuVhF6RY07U5yXi/pnsgxf8kEOcE sqvg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=SSBbWpSl; 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 z9-20020a63d009000000b00429cf28b83dsi4644154pgf.396.2022.08.19.11.01.03; Fri, 19 Aug 2022 11:01:15 -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=SSBbWpSl; 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 S1350587AbiHSPy1 (ORCPT + 99 others); Fri, 19 Aug 2022 11:54:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40082 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350584AbiHSPxv (ORCPT ); Fri, 19 Aug 2022 11:53:51 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3EA04107761; Fri, 19 Aug 2022 08:49:24 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 11227616B3; Fri, 19 Aug 2022 15:49:22 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CD11BC433C1; Fri, 19 Aug 2022 15:49:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660924161; bh=1CxuOg8SWJNRYSad+XAcI0z8/fgiwfsl4ADjr8TjHVM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SSBbWpSlkC0M5OHsKezt0bL3hwFPNp4yEycZVquq9S/9HSGRaO/a6Wj1cJ3Zg9FdH xG5R6uvgUdE9v2TId5fTiEedVAR3oVAiNGzwE5VR8qnQzG+o43DueMN0penFqM3JtN pxjWvf1f/Z3RKE7DpsccaqGsr75EqnuFDXVy35cw= 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.10 076/545] epoll: autoremove wakers even more aggressively Date: Fri, 19 Aug 2022 17:37:26 +0200 Message-Id: <20220819153832.667365217@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220819153829.135562864@linuxfoundation.org> References: <20220819153829.135562864@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 @@ -1804,6 +1804,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. @@ -1881,8 +1896,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); /*