Skip to contentSkip to conversation

ovpn: avoid nesting TCP transport socket locks

PATCH Superseded Antonio Quartulli 1 participant 1 message Nov 25

Subsystems (2)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 ovpn: avoid nesting TCP transport socket locks

From: Antonio Quartulli <[email protected]>

A TCP transport socket may transmit while its spinlock is held, e.g. in ovpn_tcp_send_skb(), ovpn_tcp_release() or the TCP RX/timer paths. If the outer packet is routed into an ovpn interface, ovpn_tcp_send_skb() takes the spinlock of the selected peer's socket while the first one is still held. This deadlocks when it is the same socket or when two peers are routed through each other.

The originating socket cannot be reliably identified from the skb, so use spin_trylock() and defer transmission to the TX worker when the socket lock is busy, including on plain contention. out_queue becomes the generic deferred TX queue, protected by its own lock since packets are now added without the socket lock. New packets are queued behind deferred ones to keep ordering, and the queue is drained by whoever next holds the socket. When a packet stays pending, queued packets are no longer dropped but wait for write_space.

---

v4:
- use spin_trylock() instead of detecting transport skbs via skb->sk, which misses cloned/orphaned skbs, ICMP replies and detached sockets (Sashiko)
- drain deferred packets inline when the socket lock is taken
- skip TX in the worker once the socket has been detached
- drop OVPN_TCP_DEPTH_NESTING, no longer needed
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/

 drivers/net/ovpn/io.c   |   2 +-
 drivers/net/ovpn/peer.h |   2 +-
 drivers/net/ovpn/tcp.c  | 102 +++++++++++++++++++++++++++-------------
 drivers/net/ovpn/tcp.h  |   2 +-
 4 files changed, 73 insertions(+), 35 deletions(-)
drivers/net/ovpn/io.c +1 -1
index f14eb4e6c46a..3282c3b0bf58 100644
@@ -284,7 +284,7 @@ void ovpn_encrypt_post(void *data, int ret)
 		ovpn_udp_send_skb(peer, sock->sk, skb);
 		break;
 	case IPPROTO_TCP:
-		ovpn_tcp_send_skb(peer, sock->sk, skb);
+		ovpn_tcp_send_skb(peer, sock, skb);
 		break;
 	default:
 		/* no transport configured yet */
drivers/net/ovpn/peer.h +1 -1
index 1879bfb76992..8256c628f769 100644
@@ -45,7 +45,7 @@ struct ovpn_route_key {
  * @tcp: keeps track of TCP specific state
  * @tcp.strp: stream parser context (TCP only)
  * @tcp.user_queue: received packets that have to go to userspace (TCP only)
- * @tcp.out_queue: packets on hold while socket is taken by user (TCP only)
+ * @tcp.out_queue: packets deferred for later transmission (TCP only)
  * @tcp.tx_in_progress: true if TX is already ongoing (TCP only)
  * @tcp.out_msg.skb: packet scheduled for sending (TCP only)
  * @tcp.out_msg.offset: offset where next send should start (TCP only)
drivers/net/ovpn/tcp.c +70 -32

156 lines not on this page · raw message

drivers/net/ovpn/tcp.h +1 -1
index a3aa3570ae5e..9f2b5efcbd3a 100644
@@ -30,7 +30,7 @@ void ovpn_tcp_socket_wait_finish(struct ovpn_socket *sock);
  * Required by the OpenVPN protocol in order to extract packets from
  * the TCP stream on the receiver side.
  */
-void ovpn_tcp_send_skb(struct ovpn_peer *peer, struct sock *sk,
+void ovpn_tcp_send_skb(struct ovpn_peer *peer, struct ovpn_socket *sock,
 		       struct sk_buff *skb);
 void ovpn_tcp_tx_work(struct work_struct *work);
 
Signature · 8 lines
2.56.1



_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel