Skip to contentSkip to conversation

ovpn: avoid nesting TCP transport socket locks

PATCH New Antonio Quartulli 1 participant 1 message Nov 25

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v5 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 the next ovpn sender, the TX worker or release_cb. Nothing is sent on a socket that was meanwhile detached or attached to another peer.

Under contention, e.g. while the RX path holds the socket lock, some TX now goes through the worker, at the cost of some latency and CPU.

---

v5:
- don't send on a socket that was detached or attached to another peer meanwhile; check it in the TX worker too (Sashiko)
- keep dropping packets while TX is blocked instead of queueing them
- mention the cost under contention in the commit message
v4: https://lore.kernel.org/all/[email protected]/
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  | 117 +++++++++++++++++++++++++++++-----------
 drivers/net/ovpn/tcp.h  |   2 +-
 4 files changed, 88 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 +85 -32

171 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