[PATCH net-next v5 1/8] __ptr_ring_full_next: Returns if ring will be full after next insertion

Simon Schippers posted 8 patches 1 week, 2 days ago
There is a newer version of this series
[PATCH net-next v5 1/8] __ptr_ring_full_next: Returns if ring will be full after next insertion
Posted by Simon Schippers 1 week, 2 days ago
Useful if the caller would like to act before the ptr_ring gets full after
the next __ptr_ring_produce call. Because __ptr_ring_produce has a
smp_wmb(), taking action before ensures memory ordering.

Co-developed-by: Tim Gebauer <tim.gebauer@tu-dortmund.de>
Signed-off-by: Tim Gebauer <tim.gebauer@tu-dortmund.de>
Signed-off-by: Simon Schippers <simon.schippers@tu-dortmund.de>
---
 include/linux/ptr_ring.h | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/include/linux/ptr_ring.h b/include/linux/ptr_ring.h
index 551329220e4f..c45e95071d7e 100644
--- a/include/linux/ptr_ring.h
+++ b/include/linux/ptr_ring.h
@@ -96,6 +96,28 @@ static inline bool ptr_ring_full_bh(struct ptr_ring *r)
 	return ret;
 }
 
+/* Returns if the ptr_ring will be full after inserting the next ptr.
+ */
+static inline bool __ptr_ring_full_next(struct ptr_ring *r)
+{
+	int p;
+
+	/* Since __ptr_ring_discard_one invalidates in reverse order, the
+	 * next producer entry might be NULL even though the current one
+	 * is not. Therefore, also check the current producer entry with
+	 * __ptr_ring_full.
+	 */
+	if (unlikely(r->size <= 1 || __ptr_ring_full(r)))
+		return true;
+
+	p = r->producer + 1;
+
+	if (unlikely(p >= r->size))
+		p = 0;
+
+	return r->queue[p];
+}
+
 /* Note: callers invoking this in a loop must use a compiler barrier,
  * for example cpu_relax(). Callers must hold producer_lock.
  * Callers are responsible for making sure pointer that is being queued
-- 
2.43.0