Skip to content

Commit f004320

Browse files
author
Trond Myklebust
committed
SUNRPC: Ensure we flush any closed sockets before xs_xprt_free()
We must ensure that all sockets are closed before we call xprt_free() and release the reference to the net namespace. The problem is that calling fput() will defer closing the socket until delayed_fput() gets called. Let's fix the situation by allowing rpciod and the transport teardown code (which runs on the system wq) to call __fput_sync(), and directly close the socket. Reported-by: Felix Fu <[email protected]> Acked-by: Al Viro <[email protected]> Fixes: a73881c ("SUNRPC: Fix an Oops in udp_poll()") Cc: [email protected] # 5.1.x: 3be232f: SUNRPC: Prevent immediate close+reconnect Cc: [email protected] # 5.1.x: 89f4249: SUNRPC: Don't call connect() more than once on a TCP socket Cc: [email protected] # 5.1.x Signed-off-by: Trond Myklebust <[email protected]>
1 parent 830f111 commit f004320

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

fs/file_table.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ void __fput_sync(struct file *file)
412412
}
413413

414414
EXPORT_SYMBOL(fput);
415+
EXPORT_SYMBOL(__fput_sync);
415416

416417
void __init files_init(void)
417418
{

include/trace/events/sunrpc.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1004,7 +1004,6 @@ DEFINE_RPC_XPRT_LIFETIME_EVENT(connect);
10041004
DEFINE_RPC_XPRT_LIFETIME_EVENT(disconnect_auto);
10051005
DEFINE_RPC_XPRT_LIFETIME_EVENT(disconnect_done);
10061006
DEFINE_RPC_XPRT_LIFETIME_EVENT(disconnect_force);
1007-
DEFINE_RPC_XPRT_LIFETIME_EVENT(disconnect_cleanup);
10081007
DEFINE_RPC_XPRT_LIFETIME_EVENT(destroy);
10091008

10101009
DECLARE_EVENT_CLASS(rpc_xprt_event,

net/sunrpc/xprt.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -930,12 +930,7 @@ void xprt_connect(struct rpc_task *task)
930930
if (!xprt_lock_write(xprt, task))
931931
return;
932932

933-
if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state)) {
934-
trace_xprt_disconnect_cleanup(xprt);
935-
xprt->ops->close(xprt);
936-
}
937-
938-
if (!xprt_connected(xprt)) {
933+
if (!xprt_connected(xprt) && !test_bit(XPRT_CLOSE_WAIT, &xprt->state)) {
939934
task->tk_rqstp->rq_connect_cookie = xprt->connect_cookie;
940935
rpc_sleep_on_timeout(&xprt->pending, task, NULL,
941936
xprt_request_timeout(task->tk_rqstp));

net/sunrpc/xprtsock.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ static int xs_local_send_request(struct rpc_rqst *req)
879879

880880
/* Close the stream if the previous transmission was incomplete */
881881
if (xs_send_request_was_aborted(transport, req)) {
882-
xs_close(xprt);
882+
xprt_force_disconnect(xprt);
883883
return -ENOTCONN;
884884
}
885885

@@ -915,7 +915,7 @@ static int xs_local_send_request(struct rpc_rqst *req)
915915
-status);
916916
fallthrough;
917917
case -EPIPE:
918-
xs_close(xprt);
918+
xprt_force_disconnect(xprt);
919919
status = -ENOTCONN;
920920
}
921921

@@ -1185,6 +1185,16 @@ static void xs_reset_transport(struct sock_xprt *transport)
11851185

11861186
if (sk == NULL)
11871187
return;
1188+
/*
1189+
* Make sure we're calling this in a context from which it is safe
1190+
* to call __fput_sync(). In practice that means rpciod and the
1191+
* system workqueue.
1192+
*/
1193+
if (!(current->flags & PF_WQ_WORKER)) {
1194+
WARN_ON_ONCE(1);
1195+
set_bit(XPRT_CLOSE_WAIT, &xprt->state);
1196+
return;
1197+
}
11881198

11891199
if (atomic_read(&transport->xprt.swapper))
11901200
sk_clear_memalloc(sk);
@@ -1208,7 +1218,7 @@ static void xs_reset_transport(struct sock_xprt *transport)
12081218
mutex_unlock(&transport->recv_mutex);
12091219

12101220
trace_rpc_socket_close(xprt, sock);
1211-
fput(filp);
1221+
__fput_sync(filp);
12121222

12131223
xprt_disconnect_done(xprt);
12141224
}

0 commit comments

Comments
 (0)