Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 28 Mar 2024 05:07:54 GMT
From:      Gordon Tetlow <gordon@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: 8f1f4e60ceb9 - releng/13.2 - if_wg: use proper barriers around pkt->p_state
Message-ID:  <202403280507.42S57s3J099783@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch releng/13.2 has been updated by gordon:

URL: https://cgit.FreeBSD.org/src/commit/?id=8f1f4e60ceb9b8e5eddd54cf1fde62944f56eaa4

commit 8f1f4e60ceb9b8e5eddd54cf1fde62944f56eaa4
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2024-03-15 01:19:18 +0000
Commit:     Gordon Tetlow <gordon@FreeBSD.org>
CommitDate: 2024-03-28 03:05:58 +0000

    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
    Approved by:    so
    Security:       FreeBSD-EN-24:06.wireguard
    
    (cherry picked from commit 3705d679a6344c957cae7a1b6372a8bfb8c44f0e)
    (cherry picked from commit 806e51f81dbae21feb6e7ddd95d2ed2a28b04f8f)
---
 sys/dev/wg/if_wg.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/sys/dev/wg/if_wg.c b/sys/dev/wg/if_wg.c
index 9c4ebe5dd393..99df8a6f0afc 100644
--- a/sys/dev/wg/if_wg.c
+++ b/sys/dev/wg/if_wg.c
@@ -1515,8 +1515,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);
 }
@@ -1588,8 +1587,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);
 }
@@ -1645,7 +1643,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;
@@ -1687,7 +1685,7 @@ wg_deliver_in(struct wg_peer *peer)
 	struct epoch_tracker	 et;
 
 	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;



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202403280507.42S57s3J099783>