public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] libbpf: ringbuf: allow to partially consume items
@ 2024-03-10 15:47 Andrea Righi
  2024-03-11 17:55 ` Andrii Nakryiko
  0 siblings, 1 reply; 4+ messages in thread
From: Andrea Righi @ 2024-03-10 15:47 UTC (permalink / raw)
  To: Andrii Nakryiko, Eduard Zingerman
  Cc: Alexei Starovoitov, Daniel Borkmann, Martin KaFai Lau, Song Liu,
	Yonghong Song, John Fastabend, KP Singh, Stanislav Fomichev,
	Hao Luo, Jiri Olsa, David Vernet, Tejun Heo, bpf, linux-kernel,
	Andrea Righi

Instead of always consuming all items from a ring buffer in a greedy
way, allow to stop when the callback returns a value > 0.

This allows to distinguish between an error condition and an intentional
stop condition by returning a non-negative non-zero value from the ring
buffer callback.

This can be useful, for example, to consume just a single item from the
ring buffer.

Signed-off-by: Andrea Righi <andrea.righi@canonical.com>
---
 tools/lib/bpf/ringbuf.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/tools/lib/bpf/ringbuf.c b/tools/lib/bpf/ringbuf.c
index aacb64278a01..dd8908eb3204 100644
--- a/tools/lib/bpf/ringbuf.c
+++ b/tools/lib/bpf/ringbuf.c
@@ -265,6 +265,14 @@ static int64_t ringbuf_process_ring(struct ring *r)
 					return err;
 				}
 				cnt++;
+				if (err > 0) {
+					/* update consumer pos and return the
+					 * total amount of items consumed.
+					 */
+					smp_store_release(r->consumer_pos,
+							  cons_pos);
+					goto done;
+				}
 			}
 
 			smp_store_release(r->consumer_pos, cons_pos);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-03-12 22:42 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-03-10 15:47 [PATCH] libbpf: ringbuf: allow to partially consume items Andrea Righi
2024-03-11 17:55 ` Andrii Nakryiko
2024-03-11 20:25   ` Andrea Righi
2024-03-12 22:42     ` Andrii Nakryiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox