From nobody Mon Aug 19 11:03:31 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 4WnV840l0Wz5SpT2; Mon, 19 Aug 2024 11:03:32 +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 4WnV833d3nz4SSM; Mon, 19 Aug 2024 11:03:31 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1724065411; 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=BmspvFo7jJJAlp6AufO6IxyZwSDyTVQ395wu2LyHAZY=; b=BKUF+PSuNWkTzA/P7NwCNbxeDNBAqQHP4i8bJX7pXjQ3nTrA6Wxxo6YQgfJL2dKIToXIX5 h513CuJHPhsHrbZZc1o5MjjtmxuilfJlCgMqWjQWOUfWHwb3sJC8cUr1ca5GFk4R1yqzpm VF2wfsUHBretiHoyeKjkP5iGMerL6fPjXcwLloczkwkDGmS344k6MXpz0gPnmim8Z2xmg5 Iod6H77T4u9t6smtPTde/RRMdlLA0Uf9P7zg822ecTJ9sWobNhlj5t+64DMFSnPx1M4F/v BF7wisjh3rBm72F9DWej65H/OjyKn7VKM8dl8csYhD+3E3n6HrLBChDvqlt7pA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1724065411; a=rsa-sha256; cv=none; b=eX/2itwRvTNA+zYdae5cN8FzhyAstcdExOGjJ+th6gPFCNVODAJS7tufcoflX2/rxSuRNO te3PdUDn2TRmXgJXz+Lic39C+Tfs2a7xCF3Do+osvH0gnzw4ZanZsaKf5JpaiTa8Xa9ZyB p+vv3gf7d2fmKZFPT3Yw36CB2zjilJTtHEhedYicuK6baJi5AJE6HNVV3YhJDtMXV+DCv/ oyimOaal03WMzB8uJ1uPEH+rE7ZOPVw4wupwq5FbSUAseOomoJume9+uzR69hMM4d2s1T+ bxR95BxvdbofGB+5KbpSk0WRMKhx7yvBiZpkeEJmyVFUuFr5hGOSXDFP3Ltrxw== 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=1724065411; 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=BmspvFo7jJJAlp6AufO6IxyZwSDyTVQ395wu2LyHAZY=; b=mHIcusfcVj4GgdeSyv9yXR+67qeVwSq8KLIuNpYoUG2H8al7mmnHx8i6OHodj8HixPluBW LPalTZvnR1NSlePWqZ8o6tgYu3VscecZwgsKtARK6yiP00MKYIRB30g8dX628Zyhhfs6hn Y9HcZW1XesTSARfM/IGEMqabjfNt1LztUSiH36sh21hkO7jTlSiERaX4fEY8PMGhQBNI4g 8Q4HSLa5OEh9BZ6TEQfIRCc+R2CKLUkOlCwPjXsIDhuPC+wMt96DKhJeCHQOTME7xAwOVt qEMWV+iDdhpis2cLM286V46lYGYmcQ2zcZQbj6miSkMosTolO8sgYST7HfVMeQ== 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 4WnV833Dtzz1Ctv; Mon, 19 Aug 2024 11:03:31 +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 47JB3VQE037741; Mon, 19 Aug 2024 11:03:31 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 47JB3V4Y037738; Mon, 19 Aug 2024 11:03:31 GMT (envelope-from git) Date: Mon, 19 Aug 2024 11:03:31 GMT Message-Id: <202408191103.47JB3V4Y037738@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Andrew Turner Subject: git: 947754af55ed - main - 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/main X-Git-Reftype: branch X-Git-Commit: 947754af55edc217c10e3456d97558c4eb6d0f99 Auto-Submitted: auto-generated The branch main has been updated by andrew: URL: https://cgit.FreeBSD.org/src/commit/?id=947754af55edc217c10e3456d97558c4eb6d0f99 commit 947754af55edc217c10e3456d97558c4eb6d0f99 Author: Andrew Turner AuthorDate: 2024-08-19 09:07:19 +0000 Commit: Andrew Turner CommitDate: 2024-08-19 10:53:11 +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 --- 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); }