Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp682058pxb; Tue, 5 Apr 2022 18:35:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxR99H73mCKEkZjtIT1R/NkAYQI/RynSQwxJX3TTELs+2/n96dcxWUpDRDPRW3kSalXPM0i X-Received: by 2002:a17:90a:b384:b0:1ca:88a5:2c59 with SMTP id e4-20020a17090ab38400b001ca88a52c59mr7212166pjr.62.1649208853269; Tue, 05 Apr 2022 18:34:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649208853; cv=none; d=google.com; s=arc-20160816; b=f0OZfvqXhae2HuBncYBwYke8ozL/dcMhg66F7qERxH9XT5Df6GzTUAv/BSFjGWeLqG 7cvZ0AwHw5KaINfSQ0ISY1mzk5onnhR+TsUo9eQpbqaDOJBwocbFFYE9mn3Qv1doc9ye 7XtO9oDcC+svenRhQTG/4h38IgPnaoJU/j/DsjspjF6eAPJeTHdzuUzCfIlEHSOpptsA slC4tRr/SaVxTDYhzaYmkliN4aKhwPtg5d6nAQFu8l7a64Ixd1pNzTFxqJhpQp7xdXAM f8Nku8YiY7PDGbsBL+iIG5RE4v269UQ1CKN7Z4KkslRxUCLH94I4ld0AqdjP3BHeYVH4 FlBw== 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=V2wg5wb2azhsZe71K1B+c9JLlTZlesDlaMQzeqLICu0=; b=N+lH9si4Om0t4mMCWlz0JX/Dfb17HJTmknigSst/yE2NHto1qr3IpOAWXWeW+KVv8a ksERTAPijkJIKi5D3Ghy4CaL4uDb1myoIRYNoivD1nxIw9L5ZU2O8n62cQpeTZlvAXzf jat+qzT/Wdl3pCRrr8Sa+b8tuEHH22mYEqsp5IRbFl6SZBgaC0QuASNvJiBSQtLC9jDN 4HA2UcMUPbj3Web/I9rYfFK0HZMN2UaNbDUHv0idziinla2esdvzxGvWTC44S5agYVCf 4xIWI19q3McIlGLNmSOzRQ+nhFPrYEixbubqzuD5cfwNmJouHoclvAszpWnmKO/xlXN9 SO+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=iu8ywZT5; 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 o20-20020a656a54000000b0039924bcaf8bsi9272321pgu.713.2022.04.05.18.33.58; Tue, 05 Apr 2022 18:34:13 -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=iu8ywZT5; 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 S244408AbiDEJJ5 (ORCPT + 99 others); Tue, 5 Apr 2022 05:09:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45578 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239527AbiDEIUM (ORCPT ); Tue, 5 Apr 2022 04:20:12 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1CE03F22; Tue, 5 Apr 2022 01:15:17 -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 B1CBE609AD; Tue, 5 Apr 2022 08:15:16 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C8F61C385A1; Tue, 5 Apr 2022 08:15:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649146516; bh=GEZrR1AaIsfF8Burel1HSK4o2bOQp7VB+6HmqiyFV2s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iu8ywZT5dKzaMby38F71O9jS/j1cZW5WODJzPSneDgmvuZkGA7jzQ+5UIPOL8z+Pm LU5hRtb8GTvkxuspAYKoPbPDpwGdWPZ9oU3qqhleuWpkMJy9+wjLnO+fGvb5EGNPiL OwAiST1GAFF0Nv6P6hDcX5Eg5gryogI93aqIi/mg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Enrico Scholz , Trond Myklebust , Sasha Levin Subject: [PATCH 5.17 0791/1126] SUNRPC: Dont call connect() more than once on a TCP socket Date: Tue, 5 Apr 2022 09:25:38 +0200 Message-Id: <20220405070430.789950324@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070407.513532867@linuxfoundation.org> References: <20220405070407.513532867@linuxfoundation.org> User-Agent: quilt/0.66 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: Trond Myklebust [ Upstream commit 89f42494f92f448747bd8a7ab1ae8b5d5520577d ] Avoid socket state races due to repeated calls to ->connect() using the same socket. If connect() returns 0 due to the connection having completed, but we are in fact in a closing state, then we may leave the XPRT_CONNECTING flag set on the transport. Reported-by: Enrico Scholz Fixes: 3be232f11a3c ("SUNRPC: Prevent immediate close+reconnect") Signed-off-by: Trond Myklebust Signed-off-by: Sasha Levin --- include/linux/sunrpc/xprtsock.h | 1 + net/sunrpc/xprtsock.c | 22 ++++++++++++---------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/include/linux/sunrpc/xprtsock.h b/include/linux/sunrpc/xprtsock.h index 3c1423ee74b4..fed813ffe7db 100644 --- a/include/linux/sunrpc/xprtsock.h +++ b/include/linux/sunrpc/xprtsock.h @@ -88,5 +88,6 @@ struct sock_xprt { #define XPRT_SOCK_WAKE_WRITE (5) #define XPRT_SOCK_WAKE_PENDING (6) #define XPRT_SOCK_WAKE_DISCONNECT (7) +#define XPRT_SOCK_CONNECT_SENT (8) #endif /* _LINUX_SUNRPC_XPRTSOCK_H */ diff --git a/net/sunrpc/xprtsock.c b/net/sunrpc/xprtsock.c index 5f22671b8abd..11eab0f0333b 100644 --- a/net/sunrpc/xprtsock.c +++ b/net/sunrpc/xprtsock.c @@ -2257,10 +2257,15 @@ static void xs_tcp_setup_socket(struct work_struct *work) if (atomic_read(&xprt->swapper)) current->flags |= PF_MEMALLOC; - if (!sock) { - sock = xs_create_sock(xprt, transport, - xs_addr(xprt)->sa_family, SOCK_STREAM, - IPPROTO_TCP, true); + + if (xprt_connected(xprt)) + goto out; + if (test_and_clear_bit(XPRT_SOCK_CONNECT_SENT, + &transport->sock_state) || + !sock) { + xs_reset_transport(transport); + sock = xs_create_sock(xprt, transport, xs_addr(xprt)->sa_family, + SOCK_STREAM, IPPROTO_TCP, true); if (IS_ERR(sock)) { xprt_wake_pending_tasks(xprt, PTR_ERR(sock)); goto out; @@ -2284,6 +2289,7 @@ static void xs_tcp_setup_socket(struct work_struct *work) fallthrough; case -EINPROGRESS: /* SYN_SENT! */ + set_bit(XPRT_SOCK_CONNECT_SENT, &transport->sock_state); if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; fallthrough; @@ -2345,13 +2351,9 @@ static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task) WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport)); - if (transport->sock != NULL && !xprt_connecting(xprt)) { + if (transport->sock != NULL) { dprintk("RPC: xs_connect delayed xprt %p for %lu " - "seconds\n", - xprt, xprt->reestablish_timeout / HZ); - - /* Start by resetting any existing state */ - xs_reset_transport(transport); + "seconds\n", xprt, xprt->reestablish_timeout / HZ); delay = xprt_reconnect_delay(xprt); xprt_reconnect_backoff(xprt, XS_TCP_INIT_REEST_TO); -- 2.34.1