Skip to content

Commit 90d91b0

Browse files
trondmypdamschuma-ntap
authored andcommitted
SUNRPC: Fix a race in the receive code path
We must ensure that the call to rpc_sleep_on() in xprt_transmit() cannot race with the call to xprt_complete_rqst(). Reported-by: Chuck Lever <[email protected]> Link: https://bugzilla.linux-nfs.org/show_bug.cgi?id=317 Fixes: ce7c252 ("SUNRPC: Add a separate spinlock to protect..") Cc: [email protected] # 4.14+ Reviewed-by: Chuck Lever <[email protected]> Signed-off-by: Trond Myklebust <[email protected]> Signed-off-by: Anna Schumaker <[email protected]>
1 parent dc4fd9a commit 90d91b0

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

net/sunrpc/xprt.c

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,7 @@ void xprt_transmit(struct rpc_task *task)
10011001
{
10021002
struct rpc_rqst *req = task->tk_rqstp;
10031003
struct rpc_xprt *xprt = req->rq_xprt;
1004+
unsigned int connect_cookie;
10041005
int status, numreqs;
10051006

10061007
dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen);
@@ -1024,6 +1025,7 @@ void xprt_transmit(struct rpc_task *task)
10241025
} else if (!req->rq_bytes_sent)
10251026
return;
10261027

1028+
connect_cookie = xprt->connect_cookie;
10271029
req->rq_xtime = ktime_get();
10281030
status = xprt->ops->send_request(task);
10291031
trace_xprt_transmit(xprt, req->rq_xid, status);
@@ -1047,20 +1049,28 @@ void xprt_transmit(struct rpc_task *task)
10471049
xprt->stat.bklog_u += xprt->backlog.qlen;
10481050
xprt->stat.sending_u += xprt->sending.qlen;
10491051
xprt->stat.pending_u += xprt->pending.qlen;
1052+
spin_unlock_bh(&xprt->transport_lock);
10501053

1051-
/* Don't race with disconnect */
1052-
if (!xprt_connected(xprt))
1053-
task->tk_status = -ENOTCONN;
1054-
else {
1054+
req->rq_connect_cookie = connect_cookie;
1055+
if (rpc_reply_expected(task) && !READ_ONCE(req->rq_reply_bytes_recvd)) {
10551056
/*
1056-
* Sleep on the pending queue since
1057-
* we're expecting a reply.
1057+
* Sleep on the pending queue if we're expecting a reply.
1058+
* The spinlock ensures atomicity between the test of
1059+
* req->rq_reply_bytes_recvd, and the call to rpc_sleep_on().
10581060
*/
1059-
if (!req->rq_reply_bytes_recvd && rpc_reply_expected(task))
1061+
spin_lock(&xprt->recv_lock);
1062+
if (!req->rq_reply_bytes_recvd) {
10601063
rpc_sleep_on(&xprt->pending, task, xprt_timer);
1061-
req->rq_connect_cookie = xprt->connect_cookie;
1064+
/*
1065+
* Send an extra queue wakeup call if the
1066+
* connection was dropped in case the call to
1067+
* rpc_sleep_on() raced.
1068+
*/
1069+
if (!xprt_connected(xprt))
1070+
xprt_wake_pending_tasks(xprt, -ENOTCONN);
1071+
}
1072+
spin_unlock(&xprt->recv_lock);
10621073
}
1063-
spin_unlock_bh(&xprt->transport_lock);
10641074
}
10651075

10661076
static void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task)

0 commit comments

Comments
 (0)