git: b547900689ec - stable/13 - netmap: Tell the compiler to avoid reloading ring indices

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Wed, 01 Feb 2023 17:32:01 UTC
The branch stable/13 has been updated by markj:

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

commit b547900689ec6b500fefbf76585e7e4650a49dc2
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2023-01-23 19:41:05 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2023-02-01 17:22:31 +0000

    netmap: Tell the compiler to avoid reloading ring indices
    
    Per the removed comments these fields should be loaded only once, since
    they can in principle be modified concurrently, though this would be a
    violation of the userspace contract with netmap.
    
    No functional change intended.
    
    Reviewed by:    vmaffione
    MFC after:      1 week
    Sponsored by:   Zenarmor
    Sponsored by:   OPNsense
    Sponsored by:   Klara, Inc.
    Differential Revision:  https://reviews.freebsd.org/D38061
    
    (cherry picked from commit 56c438fcd4a755f0abe8254779c39a0c4b530c75)
---
 sys/dev/netmap/netmap.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/sys/dev/netmap/netmap.c b/sys/dev/netmap/netmap.c
index 1b8a4ff1d5d4..8c4481c2e6c3 100644
--- a/sys/dev/netmap/netmap.c
+++ b/sys/dev/netmap/netmap.c
@@ -1671,8 +1671,8 @@ netmap_unget_na(struct netmap_adapter *na, struct ifnet *ifp)
 u_int
 nm_txsync_prologue(struct netmap_kring *kring, struct netmap_ring *ring)
 {
-	u_int head = ring->head; /* read only once */
-	u_int cur = ring->cur; /* read only once */
+	u_int head = NM_ACCESS_ONCE(ring->head);
+	u_int cur = NM_ACCESS_ONCE(ring->cur);
 	u_int n = kring->nkr_num_slots;
 
 	nm_prdis(5, "%s kcur %d ktail %d head %d cur %d tail %d",
@@ -1749,8 +1749,8 @@ nm_rxsync_prologue(struct netmap_kring *kring, struct netmap_ring *ring)
 	 * - cur could in principle go back, however it does not matter
 	 *   because we are processing a brand new rxsync()
 	 */
-	cur = kring->rcur = ring->cur;	/* read only once */
-	head = kring->rhead = ring->head;	/* read only once */
+	cur = kring->rcur = NM_ACCESS_ONCE(ring->cur);
+	head = kring->rhead = NM_ACCESS_ONCE(ring->head);
 #if 1 /* kernel sanity checks */
 	NM_FAIL_ON(kring->nr_hwcur >= n || kring->nr_hwtail >= n);
 #endif /* kernel sanity checks */