Re: [PATCH 1/4] ptr_ring_spare: Helper to check if spare capacity of size cnt is available

From
Jason Wang <hidden>
Date
2025-09-03 03:13:23
Also in
kvm, lkml, virtualization
Reply to
Willem de Bruijn, 2025-09-02
On Wed, Sep 3, 2025 at 5:13 AM Willem de Bruijn
[off-list ref] wrote:
Simon Schippers wrote:
quoted
The implementation is inspired by ptr_ring_empty.

Co-developed-by: Tim Gebauer <redacted>
Signed-off-by: Tim Gebauer <redacted>
Signed-off-by: Simon Schippers <redacted>
---
 include/linux/ptr_ring.h | 71 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 71 insertions(+)
diff --git a/include/linux/ptr_ring.h b/include/linux/ptr_ring.h
index 551329220e4f..6b8cfaecf478 100644
--- a/include/linux/ptr_ring.h
+++ b/include/linux/ptr_ring.h
@@ -243,6 +243,77 @@ static inline bool ptr_ring_empty_bh(struct ptr_ring *r)
      return ret;
 }

+/*
+ * Check if a spare capacity of cnt is available without taking any locks.
+ *
+ * If cnt==0 or cnt > r->size it acts the same as __ptr_ring_empty.
cnt >= r->size?
quoted
+ *
+ * The same requirements apply as described for __ptr_ring_empty.
+ */
+static inline bool __ptr_ring_spare(struct ptr_ring *r, int cnt)
+{
+     int size = r->size;
+     int to_check;
+
+     if (unlikely(!size || cnt < 0))
+             return true;
Does !size ever happen.
Yes, see 982fb490c298 ("ptr_ring: support zero length ring"). The
reason is tun reuse dev->tx_queue_len for ptr_ring size.
Also no need for preconditions for trivial
errors that never happen, like passing negative values. Or prefer
an unsigned type.
+1.

Thanks
Thread (25 messages) 25 messages, 4 authors, last reply 2025-09-10
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help