From: Kumar Kartikeya Dwivedi <memxor@gmail.com>
To: bpf@vger.kernel.org
Cc: Xu Kuohai <xukuohai@huawei.com>,
Tamir Duberstein <tamird@kernel.org>,
Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Eduard Zingerman <eddyz87@gmail.com>,
Emil Tsalapatis <emil@etsalapatis.com>,
Ihor Solodrai <ihor.solodrai@linux.dev>,
kkd@meta.com, kernel-team@meta.com
Subject: [PATCH bpf-next v1 00/10] Ring Buffer Iterator API
Date: Mon, 23 Nov 2026 22:13:23 +0100 [thread overview]
Message-ID: <cover.1795467315.git.memxor@gmail.com> (raw)
libbpf can only consume a BPF ring buffer through a sample callback, e.g.
from ring_buffer__poll() or ring__consume(). This series adds an API that
returns records to the caller one by one instead.
It is needed for overwritable ring buffers (BPF_F_RB_OVERWRITE). There, the
kernel doesn't wait for the consumer and overwrites the oldest records when
the ring buffer is full, so a record can change while it is being read.
libbpf rejects callbacks for such ring buffers since commit f756eedb24d4
("libbpf: Reject overwrite callback use"). v1 of the overwrite mode series
copied the readable data before calling the callbacks [1], and Andrii
asked for an iterator-style API instead [2].
The API works for regular ring buffers too, e.g. to process a number of
records before consuming them, or to consume a ring buffer from the
application's own event loop without a ring_buffer manager.
API
===
struct ring_rec {
const void *data;
unsigned long pos;
__u32 len;
};
const struct ring_rec *ring__next(struct ring *r);
int ring__release(struct ring *r);
void ring__rewind(struct ring *r);
bool ring__invalidated(const struct ring *r);
struct ring *ring__new(int map_fd, const struct ring_opts *opts);
void ring__free(struct ring *r);
int ring_buffer__wait(struct ring_buffer *rb, int timeout_ms);
ring__next() returns the next record without consuming it, or NULL if
there is nothing to consume. ring__release() consumes all records returned
since the last release or rewind. If it returns 1, the caller has to call
ring__next() again instead of waiting. ring__rewind() goes back to the
first record that was not released, so that ring__next() returns the same
records again.
ring__invalidated() is for overwritable ring buffers. It returns true if
any of the records returned since the last release or rewind may have been
overwritten. The caller has to copy record data first, and use the copy
only if ring__invalidated() returns false afterwards.
ring__new() and ring__free() create and free a ring without a ring_buffer
manager. ring_buffer__wait() waits for data in the rings of a manager
without consuming it, for rings with and without callbacks.
ring__next(), ring__release(), ring__rewind() and ring__invalidated() can
only be used on a ring without a sample callback.
Usage
=====
Wait with the ring_buffer manager:
rb = ring_buffer__new(map_fd, NULL, NULL, NULL);
r = ring_buffer__ring(rb, 0);
while (ring_buffer__wait(rb, -1) >= 0) {
do {
while ((rec = ring__next(r)))
handle(rec->data, rec->len);
} while (ring__release(r) > 0);
}
Wait in the application's event loop:
r = ring__new(map_fd, NULL);
epoll_ctl(epfd, EPOLL_CTL_ADD, ring__map_fd(r), &ev);
for (;;) {
epoll_wait(epfd, &ev, 1, -1);
do {
while ((rec = ring__next(r)))
handle(rec->data, rec->len);
} while (ring__release(r) > 0);
}
Consume an overwritable ring buffer:
do {
while ((rec = ring__next(r))) {
memcpy(copy, rec->data, rec->len);
if (ring__invalidated(r))
break;
handle(copy, rec->len);
}
} while (ring__release(r) > 0);
Dump an overwritable ring buffer without consuming it:
n = 0;
while ((rec = ring__next(r))) {
memcpy(copies[n], rec->data, rec->len);
lens[n++] = rec->len;
}
if (!ring__invalidated(r))
dump(copies, lens, n);
ring__rewind(r);
Patches
=======
Patches 1-2 are two kernel fixes for overwrite mode; they don't depend on
the rest of the series. Patch 3 documents overwrite mode. Patches 4-9 are
the libbpf changes and patch 10 adds the selftests.
[1] https://lore.kernel.org/bpf/20250804022101.2171981-3-xukuohai@huaweicloud.com/
[2] https://lore.kernel.org/bpf/CAEf4Bzaq5drHWChXoRBnrmkb6reAsSVj8r=uByFSup31FMA7hw@mail.gmail.com/
Kumar Kartikeya Dwivedi (10):
bpf: Order record data after the producer position in overwrite mode
bpf: Keep an overwritable ring buffer readable while it is overwritten
bpf, docs: Document the overwrite mode of BPF ring buffer
libbpf: Account for the overwrite position in ring__avail_data_size()
libbpf: Add a record API for consuming BPF ring buffers
libbpf: Add ring_buffer__wait()
libbpf: Support overwritable ring buffers in the record API
libbpf: Split ring setup out of ring_buffer__add()
libbpf: Add ring__new() and ring__free()
selftests/bpf: Add tests for the ring buffer record API
Documentation/bpf/ringbuf.rst | 44 +-
kernel/bpf/ringbuf.c | 24 +-
tools/lib/bpf/libbpf.h | 139 ++++-
tools/lib/bpf/libbpf.map | 7 +
tools/lib/bpf/ringbuf.c | 336 ++++++++--
.../selftests/bpf/prog_tests/ringbuf_iter.c | 586 ++++++++++++++++++
.../selftests/bpf/progs/test_ringbuf_iter.c | 66 ++
7 files changed, 1155 insertions(+), 47 deletions(-)
create mode 100644 tools/testing/selftests/bpf/prog_tests/ringbuf_iter.c
create mode 100644 tools/testing/selftests/bpf/progs/test_ringbuf_iter.c
base-commit: 2bf3351c5f894697e8443eeef5333d4c4ce90cab
--
2.53.1-Meta
next reply other threads:[~2026-11-23 21:13 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-23 21:13 Kumar Kartikeya Dwivedi [this message]
2026-11-23 21:13 ` [PATCH bpf-next v1 01/10] bpf: Order record data after the producer position in overwrite mode Kumar Kartikeya Dwivedi
2026-11-23 21:13 ` [PATCH bpf-next v1 02/10] bpf: Keep an overwritable ring buffer readable while it is overwritten Kumar Kartikeya Dwivedi
2026-11-23 21:13 ` [PATCH bpf-next v1 03/10] bpf, docs: Document the overwrite mode of BPF ring buffer Kumar Kartikeya Dwivedi
2026-11-23 21:13 ` [PATCH bpf-next v1 04/10] libbpf: Account for the overwrite position in ring__avail_data_size() Kumar Kartikeya Dwivedi
2026-11-23 21:13 ` [PATCH bpf-next v1 05/10] libbpf: Add a record API for consuming BPF ring buffers Kumar Kartikeya Dwivedi
2026-11-23 21:13 ` [PATCH bpf-next v1 06/10] libbpf: Add ring_buffer__wait() Kumar Kartikeya Dwivedi
2026-11-23 21:13 ` [PATCH bpf-next v1 07/10] libbpf: Support overwritable ring buffers in the record API Kumar Kartikeya Dwivedi
2026-11-23 21:13 ` [PATCH bpf-next v1 08/10] libbpf: Split ring setup out of ring_buffer__add() Kumar Kartikeya Dwivedi
2026-11-23 21:13 ` [PATCH bpf-next v1 09/10] libbpf: Add ring__new() and ring__free() Kumar Kartikeya Dwivedi
2026-11-23 21:13 ` [PATCH bpf-next v1 10/10] selftests/bpf: Add tests for the ring buffer record API Kumar Kartikeya Dwivedi
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=cover.1795467315.git.memxor@gmail.com \
--to=memxor@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=ihor.solodrai@linux.dev \
--cc=kernel-team@meta.com \
--cc=kkd@meta.com \
--cc=tamird@kernel.org \
--cc=xukuohai@huawei.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox