ovpn: reduce cacheline contention in the data path
Subsystems (2)
v3 cover ovpn: reduce cacheline contention in the data path
Packet processing updates shared peer and key-slot reference counters and per-peer statistics. These updates cause cacheline contention when traffic for one peer is spread across CPUs.
Use percpu_ref for peers and key slots, and per-CPU counters for peer statistics. Separate read-mostly fields from fields written per packet to reduce false sharing.
Tests used an EPYC 7443 encryptor and a workstation decryptor, with five flows pinned to separate cores across four CCDs and RPS enabled on the encryptor's tunnel interface. Results are averages of four interleaved runs per configuration.
The per-CPU conversions increased throughput from 12.095 to 12.856 Gbit/s (+6.29%). The cacheline layout changes included in this series added another 2.11%, bringing the total gain to approximately 8.5%.
With all cores on one CCD, changes ranged from -0.9% to +1.5%, within measurement variance. The benefit is therefore most visible when traffic spans CCDs.
Marco Baffo (1):
ovpn: use per-CPU peer statistics
Ralf Lici (2):
ovpn: use percpu references for peers
ovpn: use percpu references for key slots
drivers/net/ovpn/Makefile | 1 - drivers/net/ovpn/crypto.c | 16 ++++----- drivers/net/ovpn/crypto.h | 33 +++++++++++++++--- drivers/net/ovpn/crypto_aead.c | 20 ++++++++--- drivers/net/ovpn/io.c | 17 +++++---- drivers/net/ovpn/netlink.c | 26 ++++++++------ drivers/net/ovpn/peer.c | 59 +++++++++++++++++++++---------- drivers/net/ovpn/peer.h | 57 ++++++++++++++++++++++++------ drivers/net/ovpn/stats.c | 21 ------------ drivers/net/ovpn/stats.h | 63 ++++++++++++++++++++-------------- drivers/net/ovpn/tcp.c | 42 +++++++++++------------ 11 files changed, 220 insertions(+), 135 deletions(-) delete mode 100644 drivers/net/ovpn/stats.c
Signature · 8 lines
2.43.0 _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
PATCH v3 1/3 ovpn: use percpu references for peers
From: Ralf Lici <[email protected]>
Each packet holds its peer across packet processing and asynchronous crypto completion. When traffic for one peer is spread across several CPUs, the shared kref cache line therefore moves between those CPUs for every packet.
Replace the peer kref with percpu_ref and kill the initial reference exactly once after the peer has been removed from its lookup structures and detached from its socket. Keep the existing RCU-delayed destruction and release the percpu_ref storage in the final RCU callback.
Use an unconditional percpu_ref_get where the caller already owns a peer reference or otherwise excludes teardown. This includes TCP callbacks that observe an ovpn_socket under RCU: socket teardown clears sk_user_data and waits for RCU readers before killing the peer. RCU-protected peer lookup paths instead use percpu_ref_tryget_live_rcu, so they stop admitting new users as soon as teardown starts.
percpu_ref_init can fail, so propagate allocation failure from peer creation.
This trades a per-CPU counter allocation for a cheaper live data path on these long-lived, heavily shared objects. Specifically, in terms of memory, this costs: 8 bytes per possible CPU for the counters, a 56-byte control object, and 8 additional bytes in struct ovpn_peer.
In a 32-stream test using one peer and one key, perf c2c placed the peer kref cacheline second among system-wide shared cachelines, with 37 sampled HITM loads attributed to the locked reference update. After this change that shared reference line was no longer sampled, while the untouched key-slot kref addressed by the next commit remained visible. RX used a single receive queue, so only TX exercised CPU fan-out in this test.
---
No changes in v3.
Changes in v2:
- Use unconditional peer holds in TCP socket callbacks, as observing an ovpn_socket under RCU guarantees that peer teardown has not reached percpu_ref_kill.
- Document the socket-detachment and peer-kill ordering in the commit message and ovpn_tcp_close.
drivers/net/ovpn/io.c | 6 +----- drivers/net/ovpn/netlink.c | 6 +++--- drivers/net/ovpn/peer.c | 37 ++++++++++++++++++++++-------------- drivers/net/ovpn/peer.h | 37 ++++++++++++++++++++++++++++++------ drivers/net/ovpn/tcp.c | 39 +++++++++++++++++++------------------- 5 files changed, 77 insertions(+), 48 deletions(-)
drivers/net/ovpn/io.c +1 -5
@@ -321,11 +321,7 @@ static bool ovpn_encrypt_one(struct ovpn_peer *peer, struct sk_buff *skb)/* take a reference to the peer because the crypto code may run async. * ovpn_encrypt_post() will release it upon completion */ -if (unlikely (!ovpn_peer_hold (peer))) { -DEBUG_NET_WARN_ON_ONCE (1 ); -ovpn_crypto_key_slot_put (ks); -return false; - } +ovpn_peer_hold (peer);memset (ovpn_skb_cb (skb),0 ,sizeof (struct ovpn_cb ));ovpn_encrypt_post (skb,ovpn_aead_encrypt (peer, ks, skb));
drivers/net/ovpn/netlink.c +3 -3
@@ -463,11 +463,11 @@ int ovpn_nl_peer_new_doit(struct sk_buff *skb, struct genl_info *info)ovpn_socket_release (peer); peer_release:/* For UDP, the peer is unreachable until added to the hashtables, so -* dropping the initial reference is enough. For TCP, the peer may be +* killing the initial reference is enough. For TCP, the peer may be * concurrently reachable via sk_user_data->peer until -* ovpn_socket_release() detaches; rely on the refcount. +* ovpn_socket_release() detaches; rely on the percpu reference. */ -ovpn_peer_put (peer); +ovpn_peer_kill (peer);return ret; }
drivers/net/ovpn/peer.c +23 -14
@@ -22,6 +22,8 @@#include "peer.h" #include "socket.h" +static void ovpn_peer_release_ref (struct percpu_ref *ref); + static void unlock_ovpn(struct ovpn_priv *ovpn, struct llist_head *release_list) __releases(&ovpn->lock)
@@ -33,7 +35,7 @@ static void unlock_ovpn(struct ovpn_priv *ovpn,llist_for_each_entry_safe (peer, next, release_list->first, release_entry) {ovpn_socket_release (peer); -ovpn_peer_put (peer); +ovpn_peer_kill (peer); } }
@@ -113,7 +115,6 @@ struct ovpn_peer *ovpn_peer_new(struct ovpn_priv *ovpn, u32 id)RCU_INIT_POINTER (peer->bind,NULL );ovpn_crypto_state_init (&peer->crypto);spin_lock_init (&peer->lock); -kref_init (&peer->refcount);ovpn_peer_stats_init (&peer->vpn_stats);ovpn_peer_stats_init (&peer->link_stats);INIT_WORK (&peer->keepalive_work, ovpn_peer_keepalive_send);
@@ -127,6 +128,14 @@ struct ovpn_peer *ovpn_peer_new(struct ovpn_priv *ovpn, u32 id)return ERR_PTR (ret); } + ret =percpu_ref_init (&peer->refcount, ovpn_peer_release_ref,0 , +GFP_KERNEL ); +if (ret <0 ) { +dst_cache_destroy (&peer->dst_cache); +kfree (peer); +return ERR_PTR (ret); + } +netdev_hold (ovpn->dev, &peer->dev_tracker,GFP_KERNEL );return peer;
@@ -348,6 +357,7 @@ static void ovpn_peer_release_rcu(struct rcu_head *head)* perform it in the RCU callback, when all contexts are done */ dst_cache_destroy (&peer->dst_cache); +percpu_ref_exit (&peer->refcount);kfree (peer); }
@@ -366,12 +376,12 @@ static void ovpn_peer_release(struct ovpn_peer *peer) }/** -* ovpn_peer_release_kref - callback for kref_put -* @kref: the kref object belonging to the peer +* ovpn_peer_release_ref - callback for the peer percpu reference +* @ref: the percpu_ref object belonging to the peer */ -void ovpn_peer_release_kref (struct kref *kref) +static void ovpn_peer_release_ref (struct percpu_ref *ref) { - struct ovpn_peer *peer = container_of(kref, struct ovpn_peer, refcount); + struct ovpn_peer *peer = container_of(ref, struct ovpn_peer, refcount);ovpn_peer_release (peer); }
@@ -569,7 +579,7 @@ ovpn_peer_get_by_transp_addr_p2p(struct ovpn_priv *ovpn,rcu_read_lock (); tmp =rcu_dereference (ovpn->peer);if (likely (tmp &&ovpn_peer_transp_match (tmp, ss) && -ovpn_peer_hold (tmp))) +ovpn_peer_hold_rcu (tmp))) peer = tmp;rcu_read_unlock ();
@@ -610,7 +620,7 @@ struct ovpn_peer *ovpn_peer_get_by_transp_addr(struct ovpn_priv *ovpn,if (!ovpn_peer_transp_match (tmp, &ss))continue ; -if (!ovpn_peer_hold (tmp)) +if (!ovpn_peer_hold_rcu (tmp))continue ; peer = tmp;
@@ -641,7 +651,7 @@ static struct ovpn_peer *ovpn_peer_get_by_id_p2p(struct ovpn_priv *ovpn,rcu_read_lock (); tmp =rcu_dereference (ovpn->peer); -if (likely (tmp && tmp->id == peer_id &&ovpn_peer_hold (tmp))) +if (likely (tmp && tmp->id == peer_id &&ovpn_peer_hold_rcu (tmp))) peer = tmp;rcu_read_unlock ();
@@ -671,7 +681,7 @@ struct ovpn_peer *ovpn_peer_get_by_id(struct ovpn_priv *ovpn, u32 peer_id)if (tmp->id != peer_id)continue ; -if (!ovpn_peer_hold (tmp)) +if (!ovpn_peer_hold_rcu (tmp))continue ; peer = tmp;
@@ -745,7 +755,7 @@ struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_priv *ovpn,if (ovpn->mode ==OVPN_MODE_P2P ) {rcu_read_lock (); peer =rcu_dereference (ovpn->peer); -if (unlikely (peer && !ovpn_peer_hold (peer))) +if (unlikely (peer && !ovpn_peer_hold_rcu (peer))) peer =NULL ;rcu_read_unlock ();return peer;
@@ -763,7 +773,7 @@ struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_priv *ovpn,break ; } -if (unlikely (peer && !ovpn_peer_hold (peer))) +if (unlikely (peer && !ovpn_peer_hold_rcu (peer))) peer =NULL ;rcu_read_unlock ();
@@ -1369,8 +1379,7 @@ static time64_t ovpn_peer_keepalive_work_single(struct ovpn_peer *peer,netdev_dbg (peer->ovpn->dev,"sending keepalive to peer %u\n" , peer->id); -if (WARN_ON (!ovpn_peer_hold (peer))) -return 0 ; +ovpn_peer_hold (peer);if (!queue_work (ovpn_wq, &peer->keepalive_work))ovpn_peer_put (peer); }
drivers/net/ovpn/peer.h +31 -6
@@ -10,6 +10,7 @@#ifndef _NET_OVPN_OVPNPEER_H_#define _NET_OVPN_OVPNPEER_H_ +#include <linux/percpu-refcount.h> #include <net/dst_cache.h> #include <net/strparser.h>
@@ -110,7 +111,7 @@ struct ovpn_peer {struct ovpn_peer_stats link_stats;enum ovpn_del_peer_reason delete_reason;spinlock_t lock;/* protects bind and keepalive* */ -struct kref refcount; +struct percpu_ref refcount;struct rcu_head rcu;struct llist_node release_entry;struct work_struct keepalive_work;
@@ -120,14 +121,26 @@ struct ovpn_peer {* ovpn_peer_hold - increase reference counter * @peer: the peer whose counter should be increased * -* Return: true if the counter was increased or false if it was zero already +* The caller must already own a peer reference or otherwise guarantee that +* teardown cannot complete while the new reference is acquired. */ -static inline bool ovpn_peer_hold (struct ovpn_peer *peer) +static inline void ovpn_peer_hold (struct ovpn_peer *peer) { -return kref_get_unless_zero (&peer->refcount); +percpu_ref_get (&peer->refcount); } -void ovpn_peer_release_kref (struct kref *kref); +/** +* ovpn_peer_hold_rcu - acquire a live peer reference under RCU +* @peer: peer to acquire +* +* The caller must hold rcu_read_lock. +* +* Return: true if the reference was acquired, false if teardown has started +*/ +static inline bool ovpn_peer_hold_rcu (struct ovpn_peer *peer) +{ +return percpu_ref_tryget_live_rcu (&peer->refcount); +}/** * ovpn_peer_put - decrease reference counter
@@ -135,7 +148,19 @@ void ovpn_peer_release_kref(struct kref *kref);*/ static inline void ovpn_peer_put (struct ovpn_peer *peer) { -kref_put (&peer->refcount, ovpn_peer_release_kref); +percpu_ref_put (&peer->refcount); +} + +/** +* ovpn_peer_kill - drop the peer's initial reference +* @peer: peer which has been unpublished and is being destroyed +* +* This must be called exactly once, after the peer is no longer reachable +* through its owning ovpn instance. +*/ +static inline void ovpn_peer_kill (struct ovpn_peer *peer) +{ +percpu_ref_kill (&peer->refcount); }struct ovpn_peer *ovpn_peer_new (struct ovpn_priv *ovpn,u32 id);
drivers/net/ovpn/tcp.c +19 -20
@@ -140,21 +140,18 @@ static void ovpn_tcp_rcv(struct strparser *strp, struct sk_buff *skb)/* hold reference to peer as required by ovpn_recv(). * * NOTE: in this context we should already be holding a reference to -* this peer, therefore ovpn_peer_hold() is not expected to fail +* this peer */ -if (WARN_ON (!ovpn_peer_hold (peer))) - goto err_nopeer; +ovpn_peer_hold (peer);ovpn_recv (peer, skb);return ; err: -/* take reference for deferred peer deletion. should never fail */ -if (WARN_ON (!ovpn_peer_hold (peer))) - goto err_nopeer; +/* take reference for deferred peer deletion */ +ovpn_peer_hold (peer);if (!queue_work (ovpn_wq, &peer->tcp.defer_del_work))ovpn_peer_put (peer);ovpn_dev_dstats_rx_dropped (peer->ovpn->dev); -err_nopeer:kfree_skb (skb); }
@@ -168,11 +165,12 @@ static int ovpn_tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,rcu_read_lock (); sock =rcu_dereference_sk_user_data (sk); -if (unlikely (!sock || !sock->peer || !ovpn_peer_hold (sock->peer))) { +if (unlikely (!sock || !sock->peer)) {rcu_read_unlock ();return -EBADF ; } peer = sock->peer; +ovpn_peer_hold (peer);rcu_read_unlock (); skb =__skb_recv_datagram (sk, &peer->tcp.user_queue, flags, &off, &err);
@@ -385,10 +383,11 @@ static void ovpn_tcp_release(struct sock *sk)/* during initialization this function is called before * assigning sock->peer */ -if (unlikely (!peer || !ovpn_peer_hold (peer))) { +if (unlikely (!peer)) {rcu_read_unlock ();return ; } +ovpn_peer_hold (peer);rcu_read_unlock ();__skb_queue_head_init (&queue);
@@ -411,13 +410,14 @@ static int ovpn_tcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)lock_sock (sk);rcu_read_lock (); sock =rcu_dereference_sk_user_data (sk); -if (unlikely (!sock || !sock->peer || !ovpn_peer_hold (sock->peer))) { +if (unlikely (!sock || !sock->peer)) {rcu_read_unlock ();release_sock (sk);return -EIO ; } -rcu_read_unlock (); peer = sock->peer; +ovpn_peer_hold (peer); +rcu_read_unlock ();if (msg->msg_flags & ~(MSG_DONTWAIT |MSG_NOSIGNAL )) { ret = -EOPNOTSUPP ;
@@ -588,10 +588,12 @@ static void ovpn_tcp_close(struct sock *sk, long timeout) } peer = sock->peer; -if (!peer || !ovpn_peer_hold (peer)) { +if (!peer) {rcu_read_unlock ();return ; } +/* socket teardown waits for RCU readers before killing the peer */ +ovpn_peer_hold (peer);rcu_read_unlock ();ovpn_peer_del (peer,OVPN_DEL_PEER_REASON_TRANSPORT_DISCONNECT );
@@ -604,7 +606,7 @@ static __poll_t ovpn_tcp_poll(struct file *file, struct socket *sock, {struct sk_buff_head *queue = &sock->sk->sk_receive_queue;struct ovpn_socket *ovpn_sock; -struct ovpn_peer *peer =NULL ; +struct ovpn_peer *peer;__poll_t mask;rcu_read_lock ();
@@ -619,16 +621,13 @@ static __poll_t ovpn_tcp_poll(struct file *file, struct socket *sock,return 0 ; } -if (ovpn_peer_hold (ovpn_sock->peer)) { - peer = ovpn_sock->peer; - queue = &peer->tcp.user_queue; - } + peer = ovpn_sock->peer; +ovpn_peer_hold (peer); + queue = &peer->tcp.user_queue;rcu_read_unlock (); mask =datagram_poll_queue (file, sock, wait, queue); - -if (peer) -ovpn_peer_put (peer); +ovpn_peer_put (peer);return mask; }
Signature · 8 lines
2.43.0 _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
PATCH v3 2/3 ovpn: use percpu references for key slots
In reply to Marco BaffoFrom: Ralf Lici <[email protected]>
Packet processing holds a key slot until asynchronous crypto completion. Parallel traffic using one key consequently updates the same kref cache line for every packet, even though key slots are normally long-lived.
Replace the key-slot kref with percpu_ref. Kill the initial reference after atomically unpublishing a slot, while ordinary packet completions only put their live references.
Key destruction needs process context to release the crypto transforms and an RCU grace period for lockless configuration readers which do not hold a key reference. With percpu_ref, the release callback may run from the internal RCU callback which switches the reference to atomic mode. Queueing rcu_work from there would create a nested callback which the module cleanup rcu_barrier is not guaranteed to wait for.
Record the RCU state after unpublishing the slot and queue ordinary work from the release callback instead. The worker conditionally waits for the recorded grace period before freeing the key. This provides explicit RCU synchronization without depending on percpu_ref implementation details or unconditionally starting another grace period.
Peer teardown kills its keys before dropping the final netdevice reference, so rtnl_link_unregister cannot return before the key percpu-ref callbacks have been registered. The existing rcu_barrier drains those callbacks and destroy_workqueue subsequently drains the ordinary key cleanup work.
Initialize the reference only after the rest of the key slot is ready, allowing the existing destruction path to handle allocation failure.
The conversion adds an 8-byte counter per possible CPU and a 56-byte control allocation for each installed key slot; with at most two long-lived slots per peer, this is a bounded memory tradeoff for removing the shared reference cacheline from the packet path.
After the peer conversion, perf c2c still identified the key-slot kref cacheline, with 15 sampled HITM loads attributed to its locked reference update. This change removed that line from the shared-cacheline profile as well. In a set of interleaved 32-flow iperf3 runs, the combined peer and key conversion moved throughput to 8.812 Gbit/s, against 7.734 Gbit/s for the baseline, a +13.95% improvement. Single-stream control found no reproducible regression.
---
No changes in v3.
No changes in v2.
drivers/net/ovpn/crypto.c | 16 ++++++++-------- drivers/net/ovpn/crypto.h | 27 ++++++++++++++++++++++----- drivers/net/ovpn/crypto_aead.c | 17 +++++++++++++---- 3 files changed, 43 insertions(+), 17 deletions(-)
drivers/net/ovpn/crypto.c +8 -8
@@ -18,12 +18,12 @@#include "crypto_aead.h" #include "crypto.h" -void ovpn_crypto_key_slot_release (struct kref *kref) +void ovpn_crypto_key_slot_release (struct percpu_ref *ref) {struct ovpn_crypto_key_slot *ks; - ks = container_of(kref, struct ovpn_crypto_key_slot, refcount); -queue_rcu_work (ovpn_wq, &ks->free_work); + ks = container_of(ref, struct ovpn_crypto_key_slot, refcount); +queue_work (ovpn_wq, &ks->free_work); }/* can only be invoked when all peer references have been dropped (i.e. RCU
@@ -36,13 +36,13 @@ void ovpn_crypto_state_release(struct ovpn_crypto_state *cs) ks =rcu_access_pointer (cs->slots[0 ]);if (ks) {RCU_INIT_POINTER (cs->slots[0 ],NULL ); -ovpn_crypto_key_slot_put (ks); +ovpn_crypto_key_slot_kill (ks); } ks =rcu_access_pointer (cs->slots[1 ]);if (ks) {RCU_INIT_POINTER (cs->slots[1 ],NULL ); -ovpn_crypto_key_slot_put (ks); +ovpn_crypto_key_slot_kill (ks); } }
@@ -66,7 +66,7 @@ bool ovpn_crypto_kill_key(struct ovpn_crypto_state *cs, u8 key_id)spin_unlock_bh (&cs->lock);if (ks) -ovpn_crypto_key_slot_put (ks); +ovpn_crypto_key_slot_kill (ks);/* let the caller know if a key was actually killed */ return ks;
@@ -104,7 +104,7 @@ int ovpn_crypto_state_reset(struct ovpn_crypto_state *cs,spin_unlock_bh (&cs->lock);if (old) -ovpn_crypto_key_slot_put (old); +ovpn_crypto_key_slot_kill (old);return 0 ; }
@@ -141,7 +141,7 @@ void ovpn_crypto_key_slot_delete(struct ovpn_crypto_state *cs, }pr_debug ("deleting key slot %u, key_id=%u\n" , slot, ks->key_id); -ovpn_crypto_key_slot_put (ks); +ovpn_crypto_key_slot_kill (ks); }void ovpn_crypto_key_slots_swap (struct ovpn_crypto_state *cs)
drivers/net/ovpn/crypto.h +22 -5
@@ -10,6 +10,8 @@#ifndef _NET_OVPN_OVPNCRYPTO_H_#define _NET_OVPN_OVPNCRYPTO_H_ +#include <linux/percpu-refcount.h> +#include <linux/rcupdate.h> #include <linux/workqueue.h> #include "pktid.h"
@@ -47,8 +49,9 @@ struct ovpn_crypto_key_slot {struct ovpn_pktid_recv pid_recv____cacheline_aligned_in_smp ;struct ovpn_pktid_xmit pid_xmit____cacheline_aligned_in_smp ; -struct rcu_work free_work; -struct kref refcount; +struct work_struct free_work; +unsigned long rcu_state; +struct percpu_ref refcount; };struct ovpn_crypto_state {
@@ -61,7 +64,7 @@ struct ovpn_crypto_state {static inline bool ovpn_crypto_key_slot_hold (struct ovpn_crypto_key_slot *ks) { -return kref_get_unless_zero (&ks->refcount); +return percpu_ref_tryget_live_rcu (&ks->refcount); }static inline void ovpn_crypto_state_init (struct ovpn_crypto_state *cs)
@@ -121,11 +124,25 @@ ovpn_crypto_key_slot_primary(const struct ovpn_crypto_state *cs)return ks; } -void ovpn_crypto_key_slot_release (struct kref *kref); +void ovpn_crypto_key_slot_release (struct percpu_ref *ref);static inline void ovpn_crypto_key_slot_put (struct ovpn_crypto_key_slot *ks) { -kref_put (&ks->refcount, ovpn_crypto_key_slot_release); +percpu_ref_put (&ks->refcount); +} + +/** +* ovpn_crypto_key_slot_kill - stop new users and drop the initial reference +* @ks: key slot which has already been unpublished +* +* percpu_ref does not guarantee an RCU grace period before release. Record +* the RCU state after unpublishing the key so the final worker can synchronize +* with lockless readers without unconditionally starting another grace period. +*/ +static inline void ovpn_crypto_key_slot_kill (struct ovpn_crypto_key_slot *ks) +{ + ks->rcu_state =get_state_synchronize_rcu (); +percpu_ref_kill (&ks->refcount); } int ovpn_crypto_state_reset(struct ovpn_crypto_state *cs,
drivers/net/ovpn/crypto_aead.c +13 -4
@@ -391,8 +391,13 @@ static void ovpn_aead_crypto_key_slot_free_work(struct work_struct *work) {struct ovpn_crypto_key_slot *ks; - ks = container_of(to_rcu_work(work), struct ovpn_crypto_key_slot, - free_work); + ks = container_of(work, struct ovpn_crypto_key_slot, free_work); +/* Reaching this worker means every reference held by packet processing +* and asynchronous crypto has been returned. Separately wait for any +* RCU-only reader which observed the slot before it was unpublished. +*/ +cond_synchronize_rcu (ks->rcu_state); +percpu_ref_exit (&ks->refcount);ovpn_aead_crypto_key_slot_free (ks);kfree (ks); }
@@ -427,8 +432,7 @@ ovpn_aead_crypto_key_slot_new(const struct ovpn_key_config *kc) ks->encrypt =NULL ; ks->decrypt =NULL ; -INIT_RCU_WORK (&ks->free_work, ovpn_aead_crypto_key_slot_free_work); -kref_init (&ks->refcount); +INIT_WORK (&ks->free_work, ovpn_aead_crypto_key_slot_free_work); ks->key_id = kc->key_id; ks->encrypt = ovpn_aead_init("encrypt", alg_name,
@@ -458,6 +462,11 @@ ovpn_aead_crypto_key_slot_new(const struct ovpn_key_config *kc)ovpn_pktid_xmit_init (&ks->pid_xmit);ovpn_pktid_recv_init (&ks->pid_recv); + ret =percpu_ref_init (&ks->refcount, ovpn_crypto_key_slot_release,0 , +GFP_KERNEL ); +if (ret <0 ) + goto destroy_ks; +return ks; destroy_ks:
Signature · 8 lines
2.43.0 _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
PATCH v3 3/3 ovpn: use per-CPU peer statistics
In reply to Marco BaffoPer-peer VPN and transport counters use atomic64 operations on every packet, causing cacheline contention when CPUs share a peer.
Replace these counters with pcpu_sw_netstats and aggregate them when reporting peers over netlink. Keep the statistics pointers in a separate cacheline to avoid false sharing with frequently updated peer fields. Disable BHs around the link RX update to cover TCP strparser callbacks running in process context. Disable BHs around the link TX statistics update in ovpn_encrypt_post(), since some hardware crypto drivers, such as HiSilicon SEC2, invoke completion callbacks with BHs enabled.
Measurements on a laptop with a Ryzen 7 PRO 7840U, with four concurrent iperf3 streams on separate cores using the same ovpn peer, showed statistics HITM samples dropping from 8-12 to zero for UDP and 16-18 to zero for TCP. UDP cycles per packet fell from 26,389 to 26,339 (-0.19%) and TCP cycles per segment fell from 18,920 to 18,861 (-0.31%).
The peer and key-slot cacheline layout changes reduce total HITM samples by up to 75% for UDP and up to 63% for TCP.
---
Changes in v3:
- Disable BHs around the link TX statistics update in ovpn_encrypt_post() to handle crypto completions running with BHs enabled.
Changes in v2:
- Replace the cacheline static_assert() with DEBUG_NET_WARN_ON_ONCE() during key-slot creation, allowing builds with 16-byte or 32-byte cache lines. The warning is enabled only with CONFIG_DEBUG_NET.
- Update the layout comment to describe fitting within one cacheline as a goal rather than a guarantee.
drivers/net/ovpn/Makefile | 1 - drivers/net/ovpn/crypto.h | 8 ++++- drivers/net/ovpn/crypto_aead.c | 3 ++ drivers/net/ovpn/io.c | 11 +++--- drivers/net/ovpn/netlink.c | 20 ++++++----- drivers/net/ovpn/peer.c | 26 ++++++++++---- drivers/net/ovpn/peer.h | 22 ++++++++---- drivers/net/ovpn/stats.c | 21 ------------ drivers/net/ovpn/stats.h | 63 ++++++++++++++++++++-------------- drivers/net/ovpn/tcp.c | 3 +- 10 files changed, 104 insertions(+), 74 deletions(-) delete mode 100644 drivers/net/ovpn/stats.c
drivers/net/ovpn/Makefile +0 -1
@@ -17,6 +17,5 @@ ovpn-y += netlink-gen.o ovpn-y += peer.o ovpn-y += pktid.o ovpn-y += socket.o -ovpn-y += stats.o ovpn-y += tcp.o ovpn-y += udp.o
drivers/net/ovpn/crypto.h +7 -1
@@ -46,12 +46,18 @@ struct ovpn_crypto_key_slot {struct crypto_aead *decrypt;u8 nonce_tail_xmit[OVPN_NONCE_TAIL_SIZE ];u8 nonce_tail_recv[OVPN_NONCE_TAIL_SIZE ]; +struct percpu_ref refcount; + +/* The fields up to refcount are read on every packet but written only when +* the key is installed or torn down, so ideally they fit in one cacheline. +* pid_recv and pid_xmit are written on every packet and get cachelines of +* their own. Keep per-packet writes out of the first block. +*/ struct ovpn_pktid_recv pid_recv____cacheline_aligned_in_smp ;struct ovpn_pktid_xmit pid_xmit____cacheline_aligned_in_smp ;struct work_struct free_work;unsigned long rcu_state; -struct percpu_ref refcount; };struct ovpn_crypto_state {
drivers/net/ovpn/crypto_aead.c +3 -0
@@ -409,6 +409,9 @@ ovpn_aead_crypto_key_slot_new(const struct ovpn_key_config *kc)const char *alg_name;int ret; + DEBUG_NET_WARN_ON_ONCE(offsetofend(struct ovpn_crypto_key_slot, refcount) > +SMP_CACHE_BYTES ); +/* validate crypto alg */ switch (kc->cipher_alg) {case OVPN_CIPHER_ALG_AES_GCM :
drivers/net/ovpn/io.c +7 -4
@@ -99,7 +99,7 @@ static void ovpn_netdev_write(struct ovpn_peer *peer, struct sk_buff *skb) ret =gro_cells_receive (&peer->ovpn->gro_cells, skb);if (likely (ret ==NET_RX_SUCCESS )) {/* update RX stats with the size of decrypted packet */ -ovpn_peer_stats_increment_rx (&peer->vpn_stats, pkt_len); +ovpn_peer_stats_increment_rx (peer->vpn_stats, pkt_len);dev_dstats_rx_add (peer->ovpn->dev, pkt_len); }local_bh_enable ();
@@ -216,7 +216,7 @@ void ovpn_recv(struct ovpn_peer *peer, struct sk_buff *skb)struct ovpn_crypto_key_slot *ks;u8 key_id; -ovpn_peer_stats_increment_rx (&peer->link_stats, skb->len); +ovpn_peer_stats_increment_rx (peer->link_stats, skb->len);/* get the key slot matching the key ID in the received packet */ key_id =ovpn_key_id_from_skb (skb);
@@ -292,7 +292,10 @@ void ovpn_encrypt_post(void *data, int ret) goto err_unlock; } -ovpn_peer_stats_increment_tx (&peer->link_stats, orig_len); +/* some crypto backends invoke completion with BHs enabled */ +local_bh_disable (); +ovpn_peer_stats_increment_tx (peer->link_stats, orig_len); +local_bh_enable ();/* keep track of last sent packet for keepalive */ WRITE_ONCE (peer->last_sent,ktime_get_boottime_seconds ());/* skb passed down the stack - don't free it */
@@ -428,7 +431,7 @@ netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev) } skb_list.prev->next =NULL ; -ovpn_peer_stats_increment_tx (&peer->vpn_stats, tx_bytes); +ovpn_peer_stats_increment_tx (peer->vpn_stats, tx_bytes);ovpn_send (ovpn, skb_list.next, peer);return NETDEV_TX_OK ;
drivers/net/ovpn/netlink.c +12 -8
@@ -550,6 +550,7 @@ static int ovpn_nl_send_peer(struct sk_buff *skb, const struct genl_info *info, const struct ovpn_peer *peer, u32 portid, u32 seq, int flags) { + struct rtnl_link_stats64 vpn_stats = {}, link_stats = {};const struct ovpn_bind *bind;struct ovpn_socket *sock;int ret = -EMSGSIZE ;
@@ -631,27 +632,30 @@ static int ovpn_nl_send_peer(struct sk_buff *skb, const struct genl_info *info, }rcu_read_unlock (); +dev_fetch_sw_netstats (&vpn_stats, peer->vpn_stats); +dev_fetch_sw_netstats (&link_stats, peer->link_stats); +if (nla_put_net16 (skb,OVPN_A_PEER_LOCAL_PORT , local_port) ||/* VPN RX stats */ nla_put_uint (skb,OVPN_A_PEER_VPN_RX_BYTES , -atomic64_read (&peer->vpn_stats.rx.bytes)) || + vpn_stats.rx_bytes) ||nla_put_uint (skb,OVPN_A_PEER_VPN_RX_PACKETS , -atomic64_read (&peer->vpn_stats.rx.packets)) || + vpn_stats.rx_packets) ||/* VPN TX stats */ nla_put_uint (skb,OVPN_A_PEER_VPN_TX_BYTES , -atomic64_read (&peer->vpn_stats.tx.bytes)) || + vpn_stats.tx_bytes) ||nla_put_uint (skb,OVPN_A_PEER_VPN_TX_PACKETS , -atomic64_read (&peer->vpn_stats.tx.packets)) || + vpn_stats.tx_packets) ||/* link RX stats */ nla_put_uint (skb,OVPN_A_PEER_LINK_RX_BYTES , -atomic64_read (&peer->link_stats.rx.bytes)) || + link_stats.rx_bytes) ||nla_put_uint (skb,OVPN_A_PEER_LINK_RX_PACKETS , -atomic64_read (&peer->link_stats.rx.packets)) || + link_stats.rx_packets) ||/* link TX stats */ nla_put_uint (skb,OVPN_A_PEER_LINK_TX_BYTES , -atomic64_read (&peer->link_stats.tx.bytes)) || + link_stats.tx_bytes) ||nla_put_uint (skb,OVPN_A_PEER_LINK_TX_PACKETS , -atomic64_read (&peer->link_stats.tx.packets))) + link_stats.tx_packets)) goto err;nla_nest_end (skb, attr);
drivers/net/ovpn/peer.c +20 -6
@@ -115,30 +115,42 @@ struct ovpn_peer *ovpn_peer_new(struct ovpn_priv *ovpn, u32 id)RCU_INIT_POINTER (peer->bind,NULL );ovpn_crypto_state_init (&peer->crypto);spin_lock_init (&peer->lock); -ovpn_peer_stats_init (&peer->vpn_stats); -ovpn_peer_stats_init (&peer->link_stats);INIT_WORK (&peer->keepalive_work, ovpn_peer_keepalive_send); + ret = -ENOMEM ; + peer->vpn_stats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); +if (!peer->vpn_stats) + goto err_free_peer; + + peer->link_stats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); +if (!peer->link_stats) + goto err_free_stats; + ret =dst_cache_init (&peer->dst_cache,GFP_KERNEL );if (ret <0 ) {netdev_err (ovpn->dev,"cannot initialize dst cache for peer %u\n" , peer->id); -kfree (peer); -return ERR_PTR (ret); + goto err_free_stats; } ret =percpu_ref_init (&peer->refcount, ovpn_peer_release_ref,0 ,GFP_KERNEL );if (ret <0 ) {dst_cache_destroy (&peer->dst_cache); -kfree (peer); -return ERR_PTR (ret); + goto err_free_stats; }netdev_hold (ovpn->dev, &peer->dev_tracker,GFP_KERNEL );return peer; + +err_free_stats: +free_percpu (peer->link_stats); +free_percpu (peer->vpn_stats); +err_free_peer: +kfree (peer); +return ERR_PTR (ret); }/**
@@ -357,6 +369,8 @@ static void ovpn_peer_release_rcu(struct rcu_head *head)* perform it in the RCU callback, when all contexts are done */ dst_cache_destroy (&peer->dst_cache); +free_percpu (peer->link_stats); +free_percpu (peer->vpn_stats);percpu_ref_exit (&peer->refcount);kfree (peer); }
drivers/net/ovpn/peer.h +16 -6
@@ -10,6 +10,7 @@#ifndef _NET_OVPN_OVPNPEER_H_#define _NET_OVPN_OVPNPEER_H_ +#include <linux/cache.h> #include <linux/percpu-refcount.h> #include <net/dst_cache.h> #include <net/strparser.h>
@@ -53,14 +54,14 @@* @keepalive_timeout: seconds after which an inactive peer is considered dead * @keepalive_recv_exp: future timestamp when the peer should expire * @last_recv: timestamp of the last authenticated received packet -* @vpn_stats: per-peer in-VPN TX/RX stats -* @link_stats: per-peer link/transport TX/RX stats * @delete_reason: why peer was deleted (i.e. timeout, transport error, ..) * @lock: protects binding to peer (bind) and keepalive* fields -* @refcount: reference counter * @rcu: used to free peer in an RCU safe way * @release_entry: entry for the socket release list * @keepalive_work: used to schedule keepalive sending +* @refcount: reference counter +* @vpn_stats: per-CPU in-VPN TX/RX stats +* @link_stats: per-CPU link/transport TX/RX stats */ struct ovpn_peer {struct ovpn_priv *ovpn;
@@ -107,14 +108,23 @@ struct ovpn_peer {unsigned long keepalive_timeout;unsigned long keepalive_recv_exp;time64_t last_recv; -struct ovpn_peer_stats vpn_stats; -struct ovpn_peer_stats link_stats;enum ovpn_del_peer_reason delete_reason;spinlock_t lock;/* protects bind and keepalive* */ -struct percpu_ref refcount;struct rcu_head rcu;struct llist_node release_entry;struct work_struct keepalive_work; + +/* Read on the packet path but written only at setup +* and teardown: keep away from per-packet writes. +*/ +/* private: */ +__cacheline_group_begin_aligned (read_mostly); +/* public: */ +struct pcpu_sw_netstats __percpu *vpn_stats; +struct pcpu_sw_netstats __percpu *link_stats; +struct percpu_ref refcount; +/* private: */ +__cacheline_group_end_aligned (read_mostly); };/**
drivers/net/ovpn/stats.c deleted +0 -21
@@ -1,21 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 -/* OpenVPN data channel offload -* -* Copyright (C) 2020-2025 OpenVPN, Inc. -* -* Author: James Yonan <[email protected]> -* Antonio Quartulli <[email protected]> -*/ - -#include <linux/atomic.h> - -#include "stats.h" - -void ovpn_peer_stats_init (struct ovpn_peer_stats *ps) -{ -atomic64_set (&ps->rx.bytes,0 ); -atomic64_set (&ps->rx.packets,0 ); - -atomic64_set (&ps->tx.bytes,0 ); -atomic64_set (&ps->tx.packets,0 ); -}
drivers/net/ovpn/stats.h +37 -26
74 lines not on this page · raw message
drivers/net/ovpn/tcp.c +2 -1
@@ -143,8 +143,9 @@ static void ovpn_tcp_rcv(struct strparser *strp, struct sk_buff *skb)* this peer */ ovpn_peer_hold (peer); - +local_bh_disable ();ovpn_recv (peer, skb); +local_bh_enable ();return ; err:/* take reference for deferred peer deletion */
Signature · 8 lines
2.43.0 _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel