linux/net/rxrpc
David Howells 540b1c48c3 rxrpc: Fix deadlock between call creation and sendmsg/recvmsg
All the routines by which rxrpc is accessed from the outside are serialised
by means of the socket lock (sendmsg, recvmsg, bind,
rxrpc_kernel_begin_call(), ...) and this presents a problem:

 (1) If a number of calls on the same socket are in the process of
     connection to the same peer, a maximum of four concurrent live calls
     are permitted before further calls need to wait for a slot.

 (2) If a call is waiting for a slot, it is deep inside sendmsg() or
     rxrpc_kernel_begin_call() and the entry function is holding the socket
     lock.

 (3) sendmsg() and recvmsg() or the in-kernel equivalents are prevented
     from servicing the other calls as they need to take the socket lock to
     do so.

 (4) The socket is stuck until a call is aborted and makes its slot
     available to the waiter.

Fix this by:

 (1) Provide each call with a mutex ('user_mutex') that arbitrates access
     by the users of rxrpc separately for each specific call.

 (2) Make rxrpc_sendmsg() and rxrpc_recvmsg() unlock the socket as soon as
     they've got a call and taken its mutex.

     Note that I'm returning EWOULDBLOCK from recvmsg() if MSG_DONTWAIT is
     set but someone else has the lock.  Should I instead only return
     EWOULDBLOCK if there's nothing currently to be done on a socket, and
     sleep in this particular instance because there is something to be
     done, but we appear to be blocked by the interrupt handler doing its
     ping?

 (3) Make rxrpc_new_client_call() unlock the socket after allocating a new
     call, locking its user mutex and adding it to the socket's call tree.
     The call is returned locked so that sendmsg() can add data to it
     immediately.

     From the moment the call is in the socket tree, it is subject to
     access by sendmsg() and recvmsg() - even if it isn't connected yet.

 (4) Lock new service calls in the UDP data_ready handler (in
     rxrpc_new_incoming_call()) because they may already be in the socket's
     tree and the data_ready handler makes them live immediately if a user
     ID has already been preassigned.

     Note that the new call is locked before any notifications are sent
     that it is live, so doing mutex_trylock() *ought* to always succeed.
     Userspace is prevented from doing sendmsg() on calls that are in a
     too-early state in rxrpc_do_sendmsg().

 (5) Make rxrpc_new_incoming_call() return the call with the user mutex
     held so that a ping can be scheduled immediately under it.

     Note that it might be worth moving the ping call into
     rxrpc_new_incoming_call() and then we can drop the mutex there.

 (6) Make rxrpc_accept_call() take the lock on the call it is accepting and
     release the socket after adding the call to the socket's tree.  This
     is slightly tricky as we've dequeued the call by that point and have
     to requeue it.

     Note that requeuing emits a trace event.

 (7) Make rxrpc_kernel_send_data() and rxrpc_kernel_recv_data() take the
     new mutex immediately and don't bother with the socket mutex at all.

This patch has the nice bonus that calls on the same socket are now to some
extent parallelisable.

Note that we might want to move rxrpc_service_prealloc() calls out from the
socket lock and give it its own lock, so that we don't hang progress in
other calls because we're waiting for the allocator.

We probably also want to avoid calling rxrpc_notify_socket() from within
the socket lock (rxrpc_accept_call()).

Signed-off-by: David Howells <dhowells@redhat.com>
Tested-by: Marc Dionne <marc.c.dionne@auristor.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-03-01 09:50:58 -08:00
..
af_rxrpc.c rxrpc: Fix deadlock between call creation and sendmsg/recvmsg 2017-03-01 09:50:58 -08:00
ar-internal.h rxrpc: Fix deadlock between call creation and sendmsg/recvmsg 2017-03-01 09:50:58 -08:00
call_accept.c rxrpc: Fix deadlock between call creation and sendmsg/recvmsg 2017-03-01 09:50:58 -08:00
call_event.c rxrpc: Need to produce an ACK for service op if op takes a long time 2016-10-06 08:11:50 +01:00
call_object.c rxrpc: Fix deadlock between call creation and sendmsg/recvmsg 2017-03-01 09:50:58 -08:00
conn_client.c rxrpc: Fix handling of enums-to-string translation in tracing 2017-01-05 10:38:33 +00:00
conn_event.c rxrpc: The offset field in struct rxrpc_skb_priv is unnecessary 2016-09-30 14:39:28 +01:00
conn_object.c rxrpc: Add some more tracing 2017-01-05 11:39:12 +00:00
conn_service.c rxrpc: Add connection tracepoint and client conn state tracepoint 2016-09-17 11:24:03 +01:00
input.c rxrpc: Fix deadlock between call creation and sendmsg/recvmsg 2017-03-01 09:50:58 -08:00
insecure.c rxrpc: Rewrite the data and ack handling code 2016-09-08 11:10:12 +01:00
Kconfig rxrpc: Add config to inject packet loss 2016-09-17 11:24:04 +01:00
key.c rxrpc: Fix an assertion in rxrpc_read() 2017-02-24 11:42:55 -05:00
local_event.c rxrpc: The offset field in struct rxrpc_skb_priv is unnecessary 2016-09-30 14:39:28 +01:00
local_object.c rxrpc: Reduce the rxrpc_local::services list to a pointer 2016-09-29 22:57:47 +01:00
Makefile rxrpc: Change module filename to rxrpc.ko 2017-02-17 15:09:19 -05:00
misc.c rxrpc: Fix handling of enums-to-string translation in tracing 2017-01-05 10:38:33 +00:00
output.c rxrpc: Don't request an ACK on the last DATA packet of a call's Tx phase 2016-10-06 08:11:51 +01:00
peer_event.c rxrpc: Add per-peer RTT tracker 2016-09-22 01:26:25 +01:00
peer_object.c rxrpc: Fix checking of error from ip6_route_output() 2016-10-13 08:43:17 +01:00
proc.c rxrpc: Show a call's hard-ACK cursors in /proc/net/rxrpc_calls 2017-01-05 11:39:44 +00:00
recvmsg.c rxrpc: Fix deadlock between call creation and sendmsg/recvmsg 2017-03-01 09:50:58 -08:00
rxkad.c rxrpc: Fix warning by splitting rxrpc_send_call_packet() 2016-10-06 08:11:49 +01:00
security.c rxrpc: Reduce the rxrpc_local::services list to a pointer 2016-09-29 22:57:47 +01:00
sendmsg.c rxrpc: Fix deadlock between call creation and sendmsg/recvmsg 2017-03-01 09:50:58 -08:00
skbuff.c rxrpc: Make Tx loss-injection go through normal return and adjust tracing 2016-09-29 22:37:15 +01:00
sysctl.c rxrpc: Keep the call timeouts as ktimes rather than jiffies 2016-09-30 14:40:11 +01:00
utils.c rxrpc: Make IPv6 support conditional on CONFIG_IPV6 2016-09-17 03:58:45 -04:00