[Openvpn-devel,net-next] ovpn: avoid endpoint lock when the bind matches

Message ID dd65f48b91c7acc2ddf84f1dfdb55d84923a0ad2.1795022183.git.ralf@mandelbit.com
State New
Headers
Series [Openvpn-devel,net-next] ovpn: avoid endpoint lock when the bind matches |

Commit Message

Ralf Lici Nov. 18, 2026, 5:30 p.m. UTC
  ovpn_peer_endpoints_update runs for every authenticated UDP data packet
and takes peer->lock just to compare the packet addresses with the
peer's bind, although they almost always match.

Bind objects are immutable and replaced under RCU, so check for a match
under RCU first and take the lock only when the remote or local endpoint
has changed. The locked path rereads the current bind, so concurrent
updates are still handled there.

Signed-off-by: Ralf Lici <ralf@mandelbit.com>
---
 drivers/net/ovpn/peer.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)


base-commit: 7ccc5e05043b1bb84326b19a2b05938d8f7a2ae6
  

Patch

diff --git a/drivers/net/ovpn/peer.c b/drivers/net/ovpn/peer.c
index 2067825bb5b6..972ee4ba0aec 100644
--- a/drivers/net/ovpn/peer.c
+++ b/drivers/net/ovpn/peer.c
@@ -193,10 +193,29 @@  int ovpn_peer_reset_sockaddr(struct ovpn_peer *peer,
 static void __ovpn_peer_hash_transp_addr(struct ovpn_peer *peer,
 					 const struct ovpn_bind *bind);
 
+static bool ovpn_peer_endpoints_match(const struct ovpn_bind *bind,
+				      const struct sk_buff *skb)
+{
+	if (!bind || !ovpn_bind_skb_src_match(bind, skb))
+		return false;
+
+	switch (skb->protocol) {
+	case htons(ETH_P_IP):
+		return bind->local.ipv4.s_addr == ip_hdr(skb)->daddr;
+	case htons(ETH_P_IPV6):
+		return ipv6_addr_equal(&bind->local.ipv6,
+				       &ipv6_hdr(skb)->daddr);
+	default:
+		return true;
+	}
+}
+
 /**
  * ovpn_peer_endpoints_update - update remote or local endpoint for peer
  * @peer: peer to update the remote endpoint for
  * @skb: incoming packet to retrieve the source/destination address from
+ *
+ * Context: called with rcu_read_lock held.
  */
 void ovpn_peer_endpoints_update(struct ovpn_peer *peer, struct sk_buff *skb)
 {
@@ -207,6 +226,13 @@  void ovpn_peer_endpoints_update(struct ovpn_peer *peer, struct sk_buff *skb)
 	struct ovpn_bind *bind;
 	bool floated = false;
 
+	/* bind objects are immutable and replaced under RCU and the locked
+	 * path rechecks the current object before changing an endpoint
+	 */
+	bind = rcu_dereference(peer->bind);
+	if (likely(ovpn_peer_endpoints_match(bind, skb)))
+		return;
+
 	spin_lock_bh(&peer->lock);
 	bind = rcu_dereference_protected(peer->bind,
 					 lockdep_is_held(&peer->lock));