From nobody Mon Sep 02 09:12:48 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 4Wy31s6m9Sz5Mr5p; Mon, 02 Sep 2024 09:12:49 +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 "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Wy31s0RTsz4Cj5; Mon, 2 Sep 2024 09:12:49 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1725268369; 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=223n5vHzBTjjuiGZTBrtQPgxJipI82R6hQSVnZJx1uw=; b=e5SEBFUfLXnqzE9KHyakSuhdlhPlKnLEAtBDm0RkuizbtHF7N+zu3JWgiC2LDZr/cFoX/D JgrbJzJLADmPZns/NtS5I4ZNEH5lMpaDhO1Ad7cUD5I+Q4tLpFKQ3iypYlLo1Pzb3wVFxj YS431XMeL38YX6T058xkfMMXUrAxKY6yshQSTnDF7upebuDyYcSh7s9hyhOF3RmhUtsa1r 6KCzCNDSZBpsdOz0b9BiZTUL2p3ozQ96Mx5nUJ/FaUDYjkww+xUa6A1m3+DtbE3AqSaZfu OVfs+M5giOu0At5py1ztwQS0jm4iiMW2JcECDJpgDuvdaWhy65FNYfj599a0jA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1725268369; a=rsa-sha256; cv=none; b=USR7I6ahww8Y3Q8hddQxgTH3Ppq8O/O4+wEzdmf2JvvG+JBwD6LCAo8ILsvrmWuSyNm83Z k82YZNd5Bf2M66xxV64WynHO9kCb6qL7M2XyTXqln0FbUCTWjICKfvxBwlreVXjtipMwdQ wNusjc484nPvXzHyKdD77kDjukMd6KHiel2E98QERoG//t2xB4YYu426uio9OQu81rD0U7 TkW5zsQclmusX5WqSPprTNRUZv3iWl5GjfpT7kiEvXTy5dhnK18+bpoHO3WSWyHUKGL+qh yDY6b81nObX3oIHuOyXAHZ0kDN1WFQL++j1GLsiGnpjVFNiqBNFbbVRXyS2H1Q== 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=1725268369; 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=223n5vHzBTjjuiGZTBrtQPgxJipI82R6hQSVnZJx1uw=; b=p3rYlasFt3Ys6hE1qxi+vpgi+JPCVJ1iaB7vF4W3MIRvWn7NlO79LzMwv6/GdEWfeLgHlJ F1otUPDm77A62ljjcf/ue0Qrav6y6NUngZAcEXzkd+ssLLlx0rgHifST9D104+Ofim3Ahe FT+QKcWrDysqR442IAJn7+JNA4SgXZiy4fr1VkSdum2ByAvsc43PHuaTIS2+P13I2BrQC1 jgK44L/PdzSAqAsYGIKE/FRzxr/g1T3GbF8D4sIcv+DNuyOlwu+RuCMeGaZ8kyW0Q0mhRy Lk4jO7aj2//5aMGXl7tzLNFVIq6YbP3F859N7VmcT95H02Nq0JqIF70MIwp97g== 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 4Wy31r75kZz16Gb; Mon, 2 Sep 2024 09:12:48 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 4829Cmpg050033; Mon, 2 Sep 2024 09:12:48 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 4829CmYm050030; Mon, 2 Sep 2024 09:12:48 GMT (envelope-from git) Date: Mon, 2 Sep 2024 09:12:48 GMT Message-Id: <202409020912.4829CmYm050030@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Andrew Turner Subject: git: 74538cec0cfb - stable/13 - buf_ring: Use atomic operations with br_prod_tail 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: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: andrew X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 74538cec0cfba5d336f294dcdf7357db6aa5a850 Auto-Submitted: auto-generated The branch stable/13 has been updated by andrew: URL: https://cgit.FreeBSD.org/src/commit/?id=74538cec0cfba5d336f294dcdf7357db6aa5a850 commit 74538cec0cfba5d336f294dcdf7357db6aa5a850 Author: Andrew Turner AuthorDate: 2024-08-19 09:07:19 +0000 Commit: Andrew Turner CommitDate: 2024-09-02 09:11:57 +0000 buf_ring: Use atomic operations with br_prod_tail As with br_cons_tail use an atomic load acquire to read br_prod_tail in buf_ring_dequeue_mc and buf_ring_peek*. On dequeue we need to ensure we don't read the entry from the buf_ring until it is available and prod_tail has updated. There is already an appropriate store in the enqueue path and an appropriate load in the single consumer dequeue, we just need one in the other functions that read from the buf_ring. Reviewed by: imp, markj Sponsored by: Arm Ltd Differential Revision: https://reviews.freebsd.org/D46154 (cherry picked from commit 947754af55edc217c10e3456d97558c4eb6d0f99) --- sys/sys/buf_ring.h | 45 ++++++++++++++++----------------------------- 1 file changed, 16 insertions(+), 29 deletions(-) diff --git a/sys/sys/buf_ring.h b/sys/sys/buf_ring.h index ad463fba27cb..66e1e55bc5e9 100644 --- a/sys/sys/buf_ring.h +++ b/sys/sys/buf_ring.h @@ -131,7 +131,7 @@ static __inline void * buf_ring_dequeue_mc(struct buf_ring *br) { uint32_t cons_head, cons_next, cons_idx; - uint32_t mask; + uint32_t prod_tail, mask; void *buf; critical_enter(); @@ -139,8 +139,9 @@ buf_ring_dequeue_mc(struct buf_ring *br) do { cons_head = br->br_cons_head; cons_next = cons_head + 1; + prod_tail = atomic_load_acq_32(&br->br_prod_tail); - if (cons_head == br->br_prod_tail) { + if (cons_head == prod_tail) { critical_exit(); return (NULL); } @@ -266,29 +267,26 @@ buf_ring_putback_sc(struct buf_ring *br, void *new) static __inline void * buf_ring_peek(struct buf_ring *br) { - uint32_t mask; + uint32_t cons_head, prod_tail, mask; #if defined(DEBUG_BUFRING) && defined(_KERNEL) if ((br->br_lock != NULL) && !mtx_owned(br->br_lock)) panic("lock not held on single consumer dequeue"); #endif mask = br->br_cons_mask; - /* - * I believe it is safe to not have a memory barrier - * here because we control cons and tail is worst case - * a lagging indicator so we worst case we might - * return NULL immediately after a buffer has been enqueued - */ - if (br->br_cons_head == br->br_prod_tail) + prod_tail = atomic_load_acq_32(&br->br_prod_tail); + cons_head = br->br_cons_head; + + if (cons_head == prod_tail) return (NULL); - return (br->br_ring[br->br_cons_head & mask]); + return (br->br_ring[cons_head & mask]); } static __inline void * buf_ring_peek_clear_sc(struct buf_ring *br) { - uint32_t mask; + uint32_t cons_head, prod_tail, mask; void *ret; #if defined(DEBUG_BUFRING) && defined(_KERNEL) @@ -297,30 +295,19 @@ buf_ring_peek_clear_sc(struct buf_ring *br) #endif mask = br->br_cons_mask; - if (br->br_cons_head == br->br_prod_tail) - return (NULL); + prod_tail = atomic_load_acq_32(&br->br_prod_tail); + cons_head = br->br_cons_head; -#if defined(__arm__) || defined(__aarch64__) - /* - * The barrier is required there on ARM and ARM64 to ensure, that - * br->br_ring[br->br_cons_head] will not be fetched before the above - * condition is checked. - * Without the barrier, it is possible, that buffer will be fetched - * before the enqueue will put mbuf into br, then, in the meantime, the - * enqueue will update the array and the br_prod_tail, and the - * conditional check will be true, so we will return previously fetched - * (and invalid) buffer. - */ - atomic_thread_fence_acq(); -#endif + if (cons_head == prod_tail) + return (NULL); - ret = br->br_ring[br->br_cons_head & mask]; + ret = br->br_ring[cons_head & mask]; #ifdef DEBUG_BUFRING /* * Single consumer, i.e. cons_head will not move while we are * running, so atomic_swap_ptr() is not necessary here. */ - br->br_ring[br->br_cons_head & mask] = NULL; + br->br_ring[cons_head & mask] = NULL; #endif return (ret); }