From nobody Thu Mar 28 07:14:19 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4V4vt33mbMz5GNJd; Thu, 28 Mar 2024 07:14:19 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4V4vt33Dlmz4hXR; Thu, 28 Mar 2024 07:14:19 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1711610059; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=WRdAvMj7NaRtrY/6MYJ/YGDwzYUqTsRfHl3ZWKNBoVY=; b=CCHVuRA97J0lJJhetVJ2eoEbyvh2MBW2KlG8pPWZpzwsZyipeE2VlEsyDo34KVYiuoN6Ic 78ZeO2JNhNelp7OV4p9zDmWpks5CUttAFh4UVmKFdrapsQI7/cPXmud0aaDgNupJ1jmvqV HpBZ9N4Lq4mCNC2EPfgwFrBxIJSk/ec/gNXEMYe871CoMp06Heww5q0OoqPr6GzNdx272G nAB8Rr4CZypwNfJ8ru71lPBRPVtWlft874JvP9C1T5SKt3H+s5HTO7rwaFOqS+m5WUBEAp MpPJJTyrsqcQrLoKv08Etl2w9+3GNL+FDNaDeUrvFYDtcwj5GUJTKXRuSh/8HA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1711610059; a=rsa-sha256; cv=none; b=GJ2mVmEDtuAdOIHaFVH15aHWhc2frMehxzXLa4ShOtX7u1ew3k+ZNLno0l1zksNiKCqEzF U/e+XYbDSh7tp3dWotAePQyuBqN2A2LM02Q+6vaVO0XfNJZ/tp1HN70g5jxADMEcy7Zl0/ MOcdFffxeNGRyaampxTr+z0/lq5vYgI77FY0vmhYpcqPeqrNJRYVWZyMKBFjiVMtWpVr5d K3Bu0EACnpgqjj0c1aN8QnVYlMm48QIYFnuKa9aIVxk5Vzva7hpHMu/K1f7za5+e8VhAu5 gs3qRezloYnoBYWxGISXSSNQU/ZeFtJRUAK5fgPbFRAKXP97WKJMxi1k2qdmlQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1711610059; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=WRdAvMj7NaRtrY/6MYJ/YGDwzYUqTsRfHl3ZWKNBoVY=; b=HlmCj8BQT9QeM0fZ6rC6q5VIhhlMcOGQ4GjjYV+6fJi0iveo+PdsD+AO4vdtkw+R+zws2v Fv8WL9vCkUfAF9J4wUF2b0Fx1Pm0YuuomxnNltLH56tsrk0Xg5y4KS0Vc8r9F01gBFEdNc 8gE3S2PAfLjVLR9F84FRrLt+znAz8bO1GCv5IBcFaOoC7gjpFsAb/TQ9CZS60srCjbjDN0 RwtGsoJPjzu/WeS4gu14V8sV1yptOq8ddoqctzdYUZCrnCqgkiJPyEzK+2guWvdLHokZmM WHdfvFmS20WTU1TmY7UxPbygZQGxoLtyxR5MtCDvKpxK+8ESAdJi2W28UtTkWA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4V4vt32r31zJB7; Thu, 28 Mar 2024 07:14:19 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 42S7EJm1018806; Thu, 28 Mar 2024 07:14:19 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 42S7EJQg018803; Thu, 28 Mar 2024 07:14:19 GMT (envelope-from git) Date: Thu, 28 Mar 2024 07:14:19 GMT Message-Id: <202403280714.42S7EJQg018803@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Gordon Tetlow Subject: git: f07351f90aa3 - releng/13.3 - if_wg: use proper barriers around pkt->p_state List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: gordon X-Git-Repository: src X-Git-Refname: refs/heads/releng/13.3 X-Git-Reftype: branch X-Git-Commit: f07351f90aa37d8fc1b86e96d76447eec884d237 Auto-Submitted: auto-generated The branch releng/13.3 has been updated by gordon: URL: https://cgit.FreeBSD.org/src/commit/?id=f07351f90aa37d8fc1b86e96d76447eec884d237 commit f07351f90aa37d8fc1b86e96d76447eec884d237 Author: Kyle Evans AuthorDate: 2024-03-15 01:19:18 +0000 Commit: Gordon Tetlow CommitDate: 2024-03-28 07:13:08 +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 Approved by: re (so, implicit, appease the commit-hook) 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 89b5e52b7b2b..6638524301b8 100644 --- a/sys/dev/wg/if_wg.c +++ b/sys/dev/wg/if_wg.c @@ -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; @@ -1691,7 +1689,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;