From nobody Thu Mar 28 05:06:22 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 4V4s2Q5jLPz5GCj5; Thu, 28 Mar 2024 05:06:22 +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 4V4s2Q3mcMz4TtQ; Thu, 28 Mar 2024 05:06:22 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1711602382; 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=1WNByDuwO571aG/lLu5ZYJJZ3HC9MeDOFFChb+ZPz58=; b=qlb+aBLaGliCxgzW21Yb1+4WPVreZjOWCUfMk3bowNTQTBC/qf1Gi1Tt7veyRDoUIdK6gi GcpCO0I72eJuHIky4eeTtNE1I77da0WKG16yd4sc9vvz0n0SXBtMZlOfBf8q3U3CdhJ0Mv Tf+a11WjJtSH0I6zmOP/ucMxyXfuEeFT/PkVoZx27CSm6dK2EyJm1q6bQfUgQnvkvEiGni hAmyhdhzg15+RpDYxtP/weF+V/BbqaWw2HkGkK20dCZ+HXiEW+3u12ctlHEhn/u/Nwf8LS 7OEE7YVBM6O9M3h2Mwg5bsbLGJEJ1ueGATurbNudGtJl8FBKmTVCw3+EmCG0vQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1711602382; a=rsa-sha256; cv=none; b=b4f/FaeT8Jnfe/AUAdQ5WtiQKuOCfGiK1IcRxetrFTVzj3lwaD6CSkWa+ldXGVQGlKoHtV r3lgb+ByGZ/1PzWc/Dh9SnRPtioj8YGZ3RXwelTMJJrCqZYGesW8aXeOVk63QLe1YJydBZ fY2dHwvPUNUHZxnbOeANVmUw6ZOaJlD95msuekHacNBYUZ/Wyd6ih54npGt6+bGScikFfE 3V5jQf3D/Gwy67HxYhmhV1NZz8BeMM+GRPru1HIoSE5zIrKTYufCZzHguu+eKAjKooCSGS yY6c35Q8uXQlaLPjZQ8mVbcUJlkMIBJlifkQrc7Jiy6CgKr2AG2qgRy69L/1dg== 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=1711602382; 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=1WNByDuwO571aG/lLu5ZYJJZ3HC9MeDOFFChb+ZPz58=; b=v+pxaLutP19DUkfJ2S67DcgqcN3/17pzSMSOX9i7OKapXcfea5VlGMdx8A4+e/dcnUzx93 ZYRzYYqx46d/rodGTrkLerWgXYDg8CdvPoOp93ALdY+KlZBfpZyMQSp6S85PHKSy2bikK1 Yq8euI2aRj2OTSGs3DjjN+J7bQ2A/jyZiAmwIzGeIO8Fw0YfX1f5cw7qe375py8rWICdhj xdLmlg7Xxih0aU+R58PkWQ7t+DR+V70/Q1d0rdM4lapZNDJf2Dj5Sg6OdBQej/to6tcjib y/6Te/MfwGkvNCmRngmZNJq3p1OciaDxjZxY0apv2vKc+9K2Fe1vgyknz/HSgA== 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 4V4s2Q3NJPzDt2; Thu, 28 Mar 2024 05:06:22 +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 42S56M08099029; Thu, 28 Mar 2024 05:06:22 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 42S56MQh099027; Thu, 28 Mar 2024 05:06:22 GMT (envelope-from git) Date: Thu, 28 Mar 2024 05:06:22 GMT Message-Id: <202403280506.42S56MQh099027@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: 56be7cd84447 - releng/14.0 - 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/14.0 X-Git-Reftype: branch X-Git-Commit: 56be7cd84447d9bc18b5b9e467eb976044194bbe Auto-Submitted: auto-generated The branch releng/14.0 has been updated by gordon: URL: https://cgit.FreeBSD.org/src/commit/?id=56be7cd84447d9bc18b5b9e467eb976044194bbe commit 56be7cd84447d9bc18b5b9e467eb976044194bbe Author: Kyle Evans AuthorDate: 2024-03-15 01:19:18 +0000 Commit: Gordon Tetlow CommitDate: 2024-03-28 03:12:41 +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 590e02d3c088b220c19d53ce40a5aecc6fa099e4) --- 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 4a21afe82eb5..6748539bff3e 100644 --- a/sys/dev/wg/if_wg.c +++ b/sys/dev/wg/if_wg.c @@ -1511,8 +1511,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); } @@ -1584,8 +1583,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); } @@ -1641,7 +1639,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; @@ -1683,7 +1681,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;