Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp2886823rwb; Mon, 15 Aug 2022 13:19:37 -0700 (PDT) X-Google-Smtp-Source: AA6agR4PCXSRzB3BNgQrosFZRPgnkWqQ52qPTp9Y+mh6DOI++134T2RV/Mv+38UrbPWxMTXrLLrv X-Received: by 2002:a17:907:9606:b0:72f:826d:21b4 with SMTP id gb6-20020a170907960600b0072f826d21b4mr11384691ejc.510.1660594777334; Mon, 15 Aug 2022 13:19:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660594777; cv=none; d=google.com; s=arc-20160816; b=EnbHqws9IniJEUvLPyOjiXXzROb2a+pW/H4IwSOXSp0pz2YV+YkzX09gjM3ezFDZ45 rsMXfQAPp/b8PEZ4ktPY/HiYeDHnsS9+pJqfdYUdMJQITdqC/6P5pLiWR8FGqCBCnrap GjDEAmCUfjV5M11GNDaz/W8e4g0QX+b31EPGFis6bLdqFcyF9oi3nH5qLHl76JeEvgaC iMXFejCMFTkPpZKkdnISQn3G9U6kB7XzUKLISiHPPjuqhvNxyoPxU4sK7GzKWKjBfal3 4DuyjwZW8YBQjfG02hSK3fMrKF7Cvs3+8pEPjMKMRlkTDeT5RPLB7JX4ePYkFRJv3Pny pVgQ== 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=ynTT58QeAvOb8OWTSEbEt7qJ9Jhk6ut3Y/juz0aI3Xs=; b=aJ3wslwm0Ea6Iq0SY9QbhrKhFioC4dHomXsLw9Gkvw6H+gZAOemlBXxclrB1/BGSy3 /ENUiHtuNyT7uDLVUdCMRv25cIebgeTHwnSoQdj+mWjOArYL76J+5zGFJWSJSXfQAzgO Nhcs3Y+c1Mp30zfYeRp9a/5pIwIAWeJYwsUL9VZMPZm1itU+9DFY9n7LR0bV8WPQqJLk 5ExZz3qUo4MidKmp+rh8/8lCDAPKf5VvE9WYI0oDnWyCaIlzb0L3XCpVI3y1XTMOeRBv s0LntrSwTaujAzXeoEMRvGKY+pUPw1T4MWAJtvfI9cdoOVVh/84j5lAPs22dpDOh7seK m0ew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=RAYr4bso; 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 p17-20020a056402075100b004381b6af19csi8042441edy.155.2022.08.15.13.19.10; Mon, 15 Aug 2022 13:19:37 -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=RAYr4bso; 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 S241910AbiHOS2B (ORCPT + 99 others); Mon, 15 Aug 2022 14:28:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55394 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242318AbiHOS0C (ORCPT ); Mon, 15 Aug 2022 14:26:02 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7F8902F67F; Mon, 15 Aug 2022 11:19:05 -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 67252B80F99; Mon, 15 Aug 2022 18:18:42 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5D80DC433C1; Mon, 15 Aug 2022 18:18:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660587521; bh=ktZ+YUfLo8CeU345XT9G1M7j/YCZnhlTFnxaB4h9gXE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RAYr4bsoBwILyJNoD/k3wk4EMnxa9EuVw9OKrYXkJkKvAQBJfp+z4YqqHUtbAH1YF yfUvnlm1/gan7Kypa6iqj2B9Qt87F9Z33/F5sKMZIXknh/j4oCyXBI/zZohc4evKpd 6Sf8TXcM+v/XzNfc99OtOH4IafBroV6/crJ6fEQc= 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.15 110/779] epoll: autoremove wakers even more aggressively Date: Mon, 15 Aug 2022 19:55:54 +0200 Message-Id: <20220815180342.032766610@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180337.130757997@linuxfoundation.org> References: <20220815180337.130757997@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 @@ -1740,6 +1740,21 @@ static struct timespec64 *ep_timeout_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. @@ -1821,8 +1836,15 @@ static int ep_poll(struct eventpoll *ep, * 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); /*