git: d3d34d56bee4 - main - buf_ring: Support DEBUG_BUFRING in userspace

From: Andrew Turner <andrew_at_FreeBSD.org>
Date: Mon, 19 Aug 2024 11:03:25 UTC
The branch main has been updated by andrew:

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

commit d3d34d56bee4222b3bf3ec26d7877998405115a3
Author:     Andrew Turner <andrew@FreeBSD.org>
AuthorDate: 2024-07-29 10:28:24 +0000
Commit:     Andrew Turner <andrew@FreeBSD.org>
CommitDate: 2024-08-19 09:04:25 +0000

    buf_ring: Support DEBUG_BUFRING in userspace
    
    The only part of DEBUG_BUFRING we don't support in userspace is the
    mutex checks. Add _KERNEL checks around these so we can enable the
    extra debugging.
    
    Reviewed by:    alc, imp, kib, markj
    Sponsored by:   Arm Ltd
    Differential Revision:  https://reviews.freebsd.org/D46149
---
 sys/sys/buf_ring.h | 20 ++++++++------------
 1 file changed, 8 insertions(+), 12 deletions(-)

diff --git a/sys/sys/buf_ring.h b/sys/sys/buf_ring.h
index 6bd3b91dcd04..2d61f393712d 100644
--- a/sys/sys/buf_ring.h
+++ b/sys/sys/buf_ring.h
@@ -35,13 +35,9 @@
 #include <machine/atomic.h>
 #include <machine/cpu.h>
 
-#ifdef DEBUG_BUFRING
-#ifdef _KERNEL
+#if defined(DEBUG_BUFRING) && defined(_KERNEL)
 #include <sys/lock.h>
 #include <sys/mutex.h>
-#else
-#error "DEBUG_BUFRING is only supported in kernel"
-#endif
 #endif
 
 struct buf_ring {
@@ -54,7 +50,7 @@ struct buf_ring {
 	volatile uint32_t	br_cons_tail;
 	int		 	br_cons_size;
 	int              	br_cons_mask;
-#ifdef DEBUG_BUFRING
+#if defined(DEBUG_BUFRING) && defined(_KERNEL)
 	struct mtx		*br_lock;
 #endif	
 	void			*br_ring[0] __aligned(CACHE_LINE_SIZE);
@@ -210,8 +206,10 @@ buf_ring_dequeue_sc(struct buf_ring *br)
 
 #ifdef DEBUG_BUFRING
 	br->br_ring[cons_head] = NULL;
+#ifdef _KERNEL
 	if (!mtx_owned(br->br_lock))
 		panic("lock not held on single consumer dequeue");
+#endif
 	if (br->br_cons_tail != cons_head)
 		panic("inconsistent list cons_tail=%d cons_head=%d",
 		    br->br_cons_tail, cons_head);
@@ -277,7 +275,7 @@ static __inline void *
 buf_ring_peek(struct buf_ring *br)
 {
 
-#ifdef DEBUG_BUFRING
+#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	
@@ -296,9 +294,9 @@ buf_ring_peek(struct buf_ring *br)
 static __inline void *
 buf_ring_peek_clear_sc(struct buf_ring *br)
 {
-#ifdef DEBUG_BUFRING
 	void *ret;
 
+#if defined(DEBUG_BUFRING) && defined(_KERNEL)
 	if (!mtx_owned(br->br_lock))
 		panic("lock not held on single consumer dequeue");
 #endif	
@@ -320,17 +318,15 @@ buf_ring_peek_clear_sc(struct buf_ring *br)
 	atomic_thread_fence_acq();
 #endif
 
+	ret = br->br_ring[br->br_cons_head];
 #ifdef DEBUG_BUFRING
 	/*
 	 * Single consumer, i.e. cons_head will not move while we are
 	 * running, so atomic_swap_ptr() is not necessary here.
 	 */
-	ret = br->br_ring[br->br_cons_head];
 	br->br_ring[br->br_cons_head] = NULL;
-	return (ret);
-#else
-	return (br->br_ring[br->br_cons_head]);
 #endif
+	return (ret);
 }
 
 static __inline int