if_wg: use proper barriers around pkt->p_state

Without appropriate load-synchronization to pair with store barriers in
wg_encrypt() and wg_decrypt(), the compiler and hardware are often
allowed to reorder these loads in wg_deliver_out() and wg_deliver_in()
such that we end up with a garbage or intermediate mbuf that we try to
pass on.  The issue is particularly prevalent with the weaker
memory models of !x86 platforms.

Switch from the big-hammer wmb() to more explicit acq/rel atomics to
both make it obvious what we're syncing up with, and to avoid somewhat
hefty fences on platforms that don't necessarily need this.

With this patch, my dual-iperf3 reproducer is dramatically more stable
than it is without on aarch64.

PR:		264115
Reviewed by:	andrew, zlei

(cherry picked from commit 3705d679a6344c957cae7a1b6372a8bfb8c44f0e)
This commit is contained in:
Kyle Evans 2024-03-14 20:19:18 -05:00 committed by Franco Fichtner
parent 358e20e613
commit 7efda340ff
1 changed files with 4 additions and 6 deletions

View File

@ -1519,8 +1519,7 @@ wg_encrypt(struct wg_softc *sc, struct wg_packet *pkt)
state = WG_PACKET_CRYPTED;
out:
pkt->p_mbuf = m;
wmb();
pkt->p_state = state;
atomic_store_rel_int(&pkt->p_state, state);
GROUPTASK_ENQUEUE(&peer->p_send);
noise_remote_put(remote);
}
@ -1592,8 +1591,7 @@ wg_decrypt(struct wg_softc *sc, struct wg_packet *pkt)
state = WG_PACKET_CRYPTED;
out:
pkt->p_mbuf = m;
wmb();
pkt->p_state = state;
atomic_store_rel_int(&pkt->p_state, state);
GROUPTASK_ENQUEUE(&peer->p_recv);
noise_remote_put(remote);
}
@ -1649,7 +1647,7 @@ wg_deliver_out(struct wg_peer *peer)
wg_peer_get_endpoint(peer, &endpoint);
while ((pkt = wg_queue_dequeue_serial(&peer->p_encrypt_serial)) != NULL) {
if (pkt->p_state != WG_PACKET_CRYPTED)
if (atomic_load_acq_int(&pkt->p_state) != WG_PACKET_CRYPTED)
goto error;
m = pkt->p_mbuf;
@ -1713,7 +1711,7 @@ wg_deliver_in(struct wg_peer *peer)
int af;
while ((pkt = wg_queue_dequeue_serial(&peer->p_decrypt_serial)) != NULL) {
if (pkt->p_state != WG_PACKET_CRYPTED)
if (atomic_load_acq_int(&pkt->p_state) != WG_PACKET_CRYPTED)
goto error;
m = pkt->p_mbuf;