Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp2119679ioo; Mon, 23 May 2022 10:29:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwgTIvZD/bWlS9fE71u6KlGzAHhf8ni7l0zyCc3SDuycwYrCn4oXIYdt0VJAHi/3OKLTUBO X-Received: by 2002:a17:903:208:b0:162:284:3396 with SMTP id r8-20020a170903020800b0016202843396mr13402282plh.121.1653326942934; Mon, 23 May 2022 10:29:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653326942; cv=none; d=google.com; s=arc-20160816; b=PjTUk9PVKK9xQ43LAEYhyUGjDTd9g+zD8czQ9D313yH8l/sdHb57dT1eJKgeYw9JRQ WnO1t8cF9qYzrBrRgBZIyOa0PT0RN39Ryi3Uqcwtted9RCCbBxacGXjCoqnqBFD4dVAU TuTkvgVvlWem5gICHTFqhqNE3nZ17H2CSMRcWNW73WP5YNLTLLMqLRS+4VfICQjIOlxt 3K0eYkfA2qGyxVIJDlnq8YP7S16y5nu/pLpeHnRzJQqdXTHcN3l+zqN7jnf2kF++dsiU 5vfWb7wfy4bBsT8ZnXGTdBT+44sVzkUcx0mdzwlrDNulCMtfv4tE/31UikrMLNQnyYym ND7Q== 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=ZZ5xXpYjxK3H3beRto5e6MeGko3XjxHM60ce6vhYo/w=; b=G/xabKIH6+VT83aTQFwVlU5SoA0Bd9gUapjUnfaTMEvT6SWvHaHg8JhPVWE+viAD01 aHg48lTnfHU8kv1ae5gI/z3vhLgcS/2ZMVeaxcT9cbN4iT4VWFqdoQ6PZaoujwPoS41e xJpIJ513IyAzNLYBYyGYkad9fFKMfHhl1C3TEz3CoYiTq2Wr2Uqe6X3Dwp3FfHPecsTc ZKl+hvzffQSK28lDfSq1VKo8RY+vHL7OTLJPg1qlwLPKHEJUokWmGBBXkY4w8SJoN08B l7bYlDqcKbGd+S96UFVfFdLYFxxaYhjzCHDdluKIhFJPdkUYh5dzDatTnByyKnKvI5J/ 7azA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=FZXNlYqt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id q145-20020a632a97000000b003ab9d94aaccsi10384732pgq.328.2022.05.23.10.29.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 May 2022 10:29:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=FZXNlYqt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 5444087217; Mon, 23 May 2022 10:28:52 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240510AbiEWRZj (ORCPT + 99 others); Mon, 23 May 2022 13:25:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34330 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240666AbiEWRQj (ORCPT ); Mon, 23 May 2022 13:16:39 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0F6B013DE2; Mon, 23 May 2022 10:16:18 -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 32C3D61538; Mon, 23 May 2022 17:15:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 41361C385A9; Mon, 23 May 2022 17:15:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1653326133; bh=hfN64kEhFU/FzzVPdDp9ONjzRmOgpNHI4qtV1X1y8BE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FZXNlYqt4rhlrrRAzVPUy+va1l3B4KKhl5TJZ3+j5IFIXacltEdeXskG28sKBvJ2s mSlLG6KeiduVhfmP01CEV13nUHQMwVJ9NJ+5oqIWkXh5CodGcezyOH/ecO/S6cArOL SzQdQ+pwNLRMUBC9M/3996IakM5cIEYzKO192UhY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Greg Kroah-Hartman , Enrico Scholz , Trond Myklebust , Meena Shanmugam Subject: [PATCH 5.4 24/68] SUNRPC: Dont call connect() more than once on a TCP socket Date: Mon, 23 May 2022 19:04:51 +0200 Message-Id: <20220523165806.630090168@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220523165802.500642349@linuxfoundation.org> References: <20220523165802.500642349@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=-2.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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: Meena Shanmugam From: Trond Myklebust commit 89f42494f92f448747bd8a7ab1ae8b5d5520577d upstream. 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 [meenashanmugam: Fix merge conflict in xs_tcp_setup_socket] Signed-off-by: Meena Shanmugam Signed-off-by: Greg Kroah-Hartman --- Added fallthrough which I missed in 5.10 patch. include/linux/sunrpc/xprtsock.h | 1 + net/sunrpc/xprtsock.c | 22 ++++++++++++---------- 2 files changed, 13 insertions(+), 10 deletions(-) --- a/include/linux/sunrpc/xprtsock.h +++ b/include/linux/sunrpc/xprtsock.h @@ -90,6 +90,7 @@ 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 /* __KERNEL__ */ --- a/net/sunrpc/xprtsock.c +++ b/net/sunrpc/xprtsock.c @@ -2384,10 +2384,14 @@ static void xs_tcp_setup_socket(struct w struct rpc_xprt *xprt = &transport->xprt; int status = -EIO; - 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)) { status = PTR_ERR(sock); goto out; @@ -2418,6 +2422,8 @@ static void xs_tcp_setup_socket(struct w break; case 0: case -EINPROGRESS: + set_bit(XPRT_SOCK_CONNECT_SENT, &transport->sock_state); + fallthrough; case -EALREADY: xprt_unlock_connect(xprt, transport); return; @@ -2469,13 +2475,9 @@ static void xs_connect(struct rpc_xprt * 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);