Skip to content

Commit 839fe96

Browse files
dhowellsgregkh
authored andcommitted
rxrpc: Fix recv-recv race of completed call
[ Upstream commit 962fb1f ] If a call receives an event (such as incoming data), the call gets placed on the socket's queue and a thread in recvmsg can be awakened to go and process it. Once the thread has picked up the call off of the queue, further events will cause it to be requeued, and once the socket lock is dropped (recvmsg uses call->user_mutex to allow the socket to be used in parallel), a second thread can come in and its recvmsg can pop the call off the socket queue again. In such a case, the first thread will be receiving stuff from the call and the second thread will be blocked on call->user_mutex. The first thread can, at this point, process both the event that it picked call for and the event that the second thread picked the call for and may see the call terminate - in which case the call will be "released", decoupling the call from the user call ID assigned to it (RXRPC_USER_CALL_ID in the control message). The first thread will return okay, but then the second thread will wake up holding the user_mutex and, if it sees that the call has been released by the first thread, it will BUG thusly: kernel BUG at net/rxrpc/recvmsg.c:474! Fix this by just dequeuing the call and ignoring it if it is seen to be already released. We can't tell userspace about it anyway as the user call ID has become stale. Fixes: 248f219 ("rxrpc: Rewrite the data and ack handling code") Reported-by: Junvyyang, Tencent Zhuque Lab <zhuque@tencent.com> Signed-off-by: David Howells <dhowells@redhat.com> Reviewed-by: Jeffrey Altman <jaltman@auristor.com> cc: LePremierHomme <kwqcheii@proton.me> cc: Marc Dionne <marc.dionne@auristor.com> cc: Simon Horman <horms@kernel.org> cc: linux-afs@lists.infradead.org Link: https://patch.msgid.link/20250717074350.3767366-3-dhowells@redhat.com Signed-off-by: Jakub Kicinski <kuba@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent e5c480d commit 839fe96

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

include/trace/events/rxrpc.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,12 +282,15 @@
282282
EM(rxrpc_call_put_userid, "PUT user-id ") \
283283
EM(rxrpc_call_see_accept, "SEE accept ") \
284284
EM(rxrpc_call_see_activate_client, "SEE act-clnt") \
285+
EM(rxrpc_call_see_already_released, "SEE alrdy-rl") \
285286
EM(rxrpc_call_see_connect_failed, "SEE con-fail") \
286287
EM(rxrpc_call_see_connected, "SEE connect ") \
287288
EM(rxrpc_call_see_conn_abort, "SEE conn-abt") \
289+
EM(rxrpc_call_see_discard, "SEE discard ") \
288290
EM(rxrpc_call_see_disconnected, "SEE disconn ") \
289291
EM(rxrpc_call_see_distribute_error, "SEE dist-err") \
290292
EM(rxrpc_call_see_input, "SEE input ") \
293+
EM(rxrpc_call_see_recvmsg, "SEE recvmsg ") \
291294
EM(rxrpc_call_see_release, "SEE release ") \
292295
EM(rxrpc_call_see_userid_exists, "SEE u-exists") \
293296
EM(rxrpc_call_see_waiting_call, "SEE q-conn ") \

net/rxrpc/call_accept.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ void rxrpc_discard_prealloc(struct rxrpc_sock *rx)
219219
tail = b->call_backlog_tail;
220220
while (CIRC_CNT(head, tail, size) > 0) {
221221
struct rxrpc_call *call = b->call_backlog[tail];
222+
rxrpc_see_call(call, rxrpc_call_see_discard);
222223
rcu_assign_pointer(call->socket, rx);
223224
if (rx->discard_new_call) {
224225
_debug("discard %lx", call->user_call_ID);

net/rxrpc/recvmsg.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,16 @@ int rxrpc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
351351
goto try_again;
352352
}
353353

354+
rxrpc_see_call(call, rxrpc_call_see_recvmsg);
355+
if (test_bit(RXRPC_CALL_RELEASED, &call->flags)) {
356+
rxrpc_see_call(call, rxrpc_call_see_already_released);
357+
list_del_init(&call->recvmsg_link);
358+
spin_unlock_irq(&rx->recvmsg_lock);
359+
release_sock(&rx->sk);
360+
trace_rxrpc_recvmsg(call->debug_id, rxrpc_recvmsg_unqueue, 0);
361+
rxrpc_put_call(call, rxrpc_call_put_recvmsg);
362+
goto try_again;
363+
}
354364
if (!(flags & MSG_PEEK))
355365
list_del_init(&call->recvmsg_link);
356366
else
@@ -374,8 +384,13 @@ int rxrpc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
374384

375385
release_sock(&rx->sk);
376386

377-
if (test_bit(RXRPC_CALL_RELEASED, &call->flags))
378-
BUG();
387+
if (test_bit(RXRPC_CALL_RELEASED, &call->flags)) {
388+
rxrpc_see_call(call, rxrpc_call_see_already_released);
389+
mutex_unlock(&call->user_mutex);
390+
if (!(flags & MSG_PEEK))
391+
rxrpc_put_call(call, rxrpc_call_put_recvmsg);
392+
goto try_again;
393+
}
379394

380395
if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
381396
if (flags & MSG_CMSG_COMPAT) {

0 commit comments

Comments
 (0)