All of lore.kernel.org
 help / color / mirror / Atom feed
From: Tamir Duberstein <tamird@kernel.org>
To: Alexei Starovoitov <ast@kernel.org>,
	 Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	 Eduard Zingerman <eddyz87@gmail.com>,
	 Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	 Martin KaFai Lau <martin.lau@linux.dev>,
	Song Liu <song@kernel.org>,
	 Yonghong Song <yonghong.song@linux.dev>,
	Jiri Olsa <jolsa@kernel.org>,
	 Emil Tsalapatis <emil@etsalapatis.com>,
	Shuah Khan <shuah@kernel.org>,  Andrea Righi <arighi@nvidia.com>,
	Xu Kuohai <xukuohai@huawei.com>,
	 Andrea Righi <andrea.righi@canonical.com>,
	 Bing-Jhong Billy Jheng <billy@starlabs.sg>,
	 David Vernet <void@manifault.com>,
	Ihor Solodrai <ihor.solodrai@linux.dev>
Cc: bpf@vger.kernel.org, linux-kselftest@vger.kernel.org,
	 linux-kernel@vger.kernel.org,
	Andrew Werner <awerner32@gmail.com>,
	 Zvi Effron <zeffron@riotgames.com>,
	Andrii Nakryiko <andriin@fb.com>,
	 Tamir Duberstein <tamird@kernel.org>
Subject: [PATCH bpf-next v3 5/8] libbpf: ringbuf: Simplify iterator implementation
Date: Fri, 20 Nov 2026 15:33:18 -0500	[thread overview]
Message-ID: <20261120-bpf-ringbuf-fixes-v3-5-ff7ca15d6c37@kernel.org> (raw)
In-Reply-To: <20261120-bpf-ringbuf-fixes-v3-0-ff7ca15d6c37@kernel.org>

The iterator core retained ringbuf_process_ring()'s nested loop shape to
make the functional conversion easy to compare. This leaves mirrored
position variables and a got_new_data flag to coordinate the two loops.

Express the same protocol as a single loop over the iterator state. Keep
consumer publication, producer rechecks, and the single busy-record
retry unchanged.

Signed-off-by: Tamir Duberstein <tamird@kernel.org>
---
 tools/lib/bpf/ringbuf.c | 68 ++++++++++++++++++++++---------------------------
 1 file changed, 30 insertions(+), 38 deletions(-)

diff --git a/tools/lib/bpf/ringbuf.c b/tools/lib/bpf/ringbuf.c
index 2e281e2fa573..9da54e6b5acd 100644
--- a/tools/lib/bpf/ringbuf.c
+++ b/tools/lib/bpf/ringbuf.c
@@ -311,20 +311,14 @@ static const void *ringbuf_iter_next(struct ring_buffer_iter *it, size_t *size,
 	struct ring_buffer_iter_state *state = (void *)it;
 	struct ring *r = state->ring;
 	int *len_ptr, len;
-	unsigned long cons_pos, prod_pos;
-	bool got_new_data;
-	void *sample;
 
 	if (!r)
 		return NULL;
 	if (sync)
 		ringbuf_iter_publish(state);
-	cons_pos = state->cons_pos;
-	prod_pos = state->prod_pos;
 
-	do {
-		got_new_data = false;
-		if (cons_pos == prod_pos) {
+	for (;;) {
+		if (state->cons_pos == state->prod_pos) {
 			ringbuf_iter_publish(state);
 			if (state->needs_wakeup) {
 				/*
@@ -334,46 +328,44 @@ static const void *ringbuf_iter_next(struct ring_buffer_iter *it, size_t *size,
 				__atomic_thread_fence(__ATOMIC_SEQ_CST);
 				state->needs_wakeup = false;
 			}
-			prod_pos = __atomic_load_n(r->producer_pos,
-						   __ATOMIC_ACQUIRE);
-			state->prod_pos = prod_pos;
+			state->prod_pos = __atomic_load_n(r->producer_pos,
+							  __ATOMIC_ACQUIRE);
 		}
 
-		while (cons_pos != prod_pos) {
-			len_ptr = r->data + (cons_pos & r->mask);
-			len = __atomic_load_n(len_ptr, __ATOMIC_ACQUIRE);
-
-			/* Retry a busy record once after publishing prior records. */
-			if (len & BPF_RINGBUF_BUSY_BIT) {
-				ringbuf_iter_publish(state);
-				got_new_data = state->needs_wakeup;
-				if (got_new_data) {
-					/* Order the consumer update before retrying the header. */
-					__atomic_thread_fence(__ATOMIC_SEQ_CST);
-					state->needs_wakeup = false;
-				}
-				break;
-			}
+		if (state->cons_pos == state->prod_pos) {
+			state->ring = NULL;
+			return NULL;
+		}
 
-			got_new_data = true;
-			cons_pos += roundup_len(len);
-			state->cons_pos = cons_pos;
-			state->consumer_dirty = true;
+		len_ptr = r->data + (state->cons_pos & r->mask);
+		len = __atomic_load_n(len_ptr, __ATOMIC_ACQUIRE);
 
-			if ((len & BPF_RINGBUF_DISCARD_BIT) == 0) {
-				sample = (void *)len_ptr + BPF_RINGBUF_HDR_SZ;
-				if (size)
-					*size = len;
-				return sample;
+		/* Publish prior records and retry a record that is still busy. */
+		if (len & BPF_RINGBUF_BUSY_BIT) {
+			ringbuf_iter_publish(state);
+			if (!state->needs_wakeup) {
+				state->ring = NULL;
+				return NULL;
 			}
+			/* Order the consumer update before retrying the header. */
+			__atomic_thread_fence(__ATOMIC_SEQ_CST);
+			state->needs_wakeup = false;
+			continue;
+		}
 
+		state->cons_pos += roundup_len(len);
+		state->consumer_dirty = true;
+
+		if (len & BPF_RINGBUF_DISCARD_BIT) {
 			if (sync)
 				ringbuf_iter_publish(state);
+			continue;
 		}
-	} while (got_new_data);
 
-	state->ring = NULL;
-	return NULL;
+		if (size)
+			*size = len;
+		return (void *)len_ptr + BPF_RINGBUF_HDR_SZ;
+	}
 }
 
 const void *ring_buffer_iter_next(struct ring_buffer_iter *it, size_t *size,

-- 
2.56.1


  parent reply	other threads:[~2026-11-20 20:33 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 20:33 [PATCH bpf-next v3 0/8] bpf: Fix ring buffers and add a libbpf iterator Tamir Duberstein
2026-11-20 20:33 ` [PATCH bpf-next v3 1/8] bpf: user_ringbuf: Handle position wrap Tamir Duberstein
2026-11-20 20:33 ` [PATCH bpf-next v3 2/8] libbpf: ringbuf: Use compiler atomics Tamir Duberstein
2026-11-20 21:34   ` bot+bpf-ci
2026-11-20 22:38     ` Tamir Duberstein
2026-11-20 20:33 ` [PATCH bpf-next v3 3/8] libbpf: ringbuf: Prevent missed wakeups Tamir Duberstein
2026-11-20 20:33 ` [PATCH bpf-next v3 4/8] libbpf: ringbuf: Add a caller-owned iterator Tamir Duberstein
2026-11-20 21:34   ` bot+bpf-ci
2026-11-20 22:38     ` Tamir Duberstein
2026-11-21 10:01   ` Andrea Righi
2026-11-20 20:33 ` Tamir Duberstein [this message]
2026-11-20 20:33 ` [PATCH bpf-next v3 6/8] selftests/bpf: Refactor lifetimes and test wakeups Tamir Duberstein
2026-11-20 21:34   ` bot+bpf-ci
2026-11-20 22:38     ` Tamir Duberstein
2026-11-20 20:33 ` [PATCH bpf-next v3 7/8] selftests/bpf: Verify ringbuf callback semantics Tamir Duberstein
2026-11-20 20:33 ` [PATCH bpf-next v3 8/8] selftests/bpf: Test caller-owned ring iterators Tamir Duberstein

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261120-bpf-ringbuf-fixes-v3-5-ff7ca15d6c37@kernel.org \
    --to=tamird@kernel.org \
    --cc=andrea.righi@canonical.com \
    --cc=andrii@kernel.org \
    --cc=andriin@fb.com \
    --cc=arighi@nvidia.com \
    --cc=ast@kernel.org \
    --cc=awerner32@gmail.com \
    --cc=billy@starlabs.sg \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=shuah@kernel.org \
    --cc=song@kernel.org \
    --cc=void@manifault.com \
    --cc=xukuohai@huawei.com \
    --cc=yonghong.song@linux.dev \
    --cc=zeffron@riotgames.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.