Skip to content

Commit 89f4249

Browse files
author
Trond Myklebust
committed
SUNRPC: Don't call connect() more than once on a TCP socket
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 <[email protected]> Fixes: 3be232f ("SUNRPC: Prevent immediate close+reconnect") Signed-off-by: Trond Myklebust <[email protected]>
1 parent e47a62d commit 89f4249

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

include/linux/sunrpc/xprtsock.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,6 @@ struct sock_xprt {
8989
#define XPRT_SOCK_WAKE_WRITE (5)
9090
#define XPRT_SOCK_WAKE_PENDING (6)
9191
#define XPRT_SOCK_WAKE_DISCONNECT (7)
92+
#define XPRT_SOCK_CONNECT_SENT (8)
9293

9394
#endif /* _LINUX_SUNRPC_XPRTSOCK_H */

net/sunrpc/xprtsock.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2235,10 +2235,15 @@ static void xs_tcp_setup_socket(struct work_struct *work)
22352235

22362236
if (atomic_read(&xprt->swapper))
22372237
current->flags |= PF_MEMALLOC;
2238-
if (!sock) {
2239-
sock = xs_create_sock(xprt, transport,
2240-
xs_addr(xprt)->sa_family, SOCK_STREAM,
2241-
IPPROTO_TCP, true);
2238+
2239+
if (xprt_connected(xprt))
2240+
goto out;
2241+
if (test_and_clear_bit(XPRT_SOCK_CONNECT_SENT,
2242+
&transport->sock_state) ||
2243+
!sock) {
2244+
xs_reset_transport(transport);
2245+
sock = xs_create_sock(xprt, transport, xs_addr(xprt)->sa_family,
2246+
SOCK_STREAM, IPPROTO_TCP, true);
22422247
if (IS_ERR(sock)) {
22432248
xprt_wake_pending_tasks(xprt, PTR_ERR(sock));
22442249
goto out;
@@ -2262,6 +2267,7 @@ static void xs_tcp_setup_socket(struct work_struct *work)
22622267
fallthrough;
22632268
case -EINPROGRESS:
22642269
/* SYN_SENT! */
2270+
set_bit(XPRT_SOCK_CONNECT_SENT, &transport->sock_state);
22652271
if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
22662272
xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
22672273
fallthrough;
@@ -2323,13 +2329,9 @@ static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
23232329

23242330
WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport));
23252331

2326-
if (transport->sock != NULL && !xprt_connecting(xprt)) {
2332+
if (transport->sock != NULL) {
23272333
dprintk("RPC: xs_connect delayed xprt %p for %lu "
2328-
"seconds\n",
2329-
xprt, xprt->reestablish_timeout / HZ);
2330-
2331-
/* Start by resetting any existing state */
2332-
xs_reset_transport(transport);
2334+
"seconds\n", xprt, xprt->reestablish_timeout / HZ);
23332335

23342336
delay = xprt_reconnect_delay(xprt);
23352337
xprt_reconnect_backoff(xprt, XS_TCP_INIT_REEST_TO);

0 commit comments

Comments
 (0)