Skip to content

Commit 0ffe86f

Browse files
author
Trond Myklebust
committed
SUNRPC: Use poll() to fix up the socket requeue races
Because we clear XPRT_SOCK_DATA_READY before reading, we can end up with a situation where new data arrives, causing xs_data_ready() to queue up a second receive worker job for the same socket, which then immediately gets stuck waiting on the transport receive mutex. The fix is to only clear XPRT_SOCK_DATA_READY once we're done reading, and then to use poll() to check if we might need to queue up a new job in order to deal with any new data. Signed-off-by: Trond Myklebust <[email protected]>
1 parent a1231fd commit 0ffe86f

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

net/sunrpc/xprtsock.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -656,13 +656,34 @@ xs_read_stream(struct sock_xprt *transport, int flags)
656656
return ret != 0 ? ret : -ESHUTDOWN;
657657
}
658658

659+
static __poll_t xs_poll_socket(struct sock_xprt *transport)
660+
{
661+
return transport->sock->ops->poll(NULL, transport->sock, NULL);
662+
}
663+
664+
static bool xs_poll_socket_readable(struct sock_xprt *transport)
665+
{
666+
__poll_t events = xs_poll_socket(transport);
667+
668+
return (events & (EPOLLIN | EPOLLRDNORM)) && !(events & EPOLLRDHUP);
669+
}
670+
671+
static void xs_poll_check_readable(struct sock_xprt *transport)
672+
{
673+
674+
clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
675+
if (!xs_poll_socket_readable(transport))
676+
return;
677+
if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
678+
queue_work(xprtiod_workqueue, &transport->recv_worker);
679+
}
680+
659681
static void xs_stream_data_receive(struct sock_xprt *transport)
660682
{
661683
size_t read = 0;
662684
ssize_t ret = 0;
663685

664686
mutex_lock(&transport->recv_mutex);
665-
clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
666687
if (transport->sock == NULL)
667688
goto out;
668689
for (;;) {
@@ -672,6 +693,7 @@ static void xs_stream_data_receive(struct sock_xprt *transport)
672693
read += ret;
673694
cond_resched();
674695
}
696+
xs_poll_check_readable(transport);
675697
out:
676698
mutex_unlock(&transport->recv_mutex);
677699
trace_xs_stream_read_data(&transport->xprt, ret, read);
@@ -1362,7 +1384,6 @@ static void xs_udp_data_receive(struct sock_xprt *transport)
13621384
int err;
13631385

13641386
mutex_lock(&transport->recv_mutex);
1365-
clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
13661387
sk = transport->inet;
13671388
if (sk == NULL)
13681389
goto out;
@@ -1374,6 +1395,7 @@ static void xs_udp_data_receive(struct sock_xprt *transport)
13741395
consume_skb(skb);
13751396
cond_resched();
13761397
}
1398+
xs_poll_check_readable(transport);
13771399
out:
13781400
mutex_unlock(&transport->recv_mutex);
13791401
}

0 commit comments

Comments
 (0)