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>,
Sashiko <sashiko-bot@kernel.org>
Subject: [PATCH bpf-next v3 0/8] bpf: Fix ring buffers and add a libbpf iterator
Date: Fri, 20 Nov 2026 15:33:13 -0500 [thread overview]
Message-ID: <20261120-bpf-ringbuf-fixes-v3-0-ff7ca15d6c37@kernel.org> (raw)
The ring buffer API exposes records only through callbacks. Callers that
need to stop at an operation-specific condition have no non-error way to
end consumption early.
Andrea Righi proposed using positive callback returns to request early
termination. Andrii Nakryiko noted that positive values are already
ignored and changing that behavior could break users, and suggested a
new API:
https://lore.kernel.org/bpf/20240310154726.734289-1-andrea.righi@canonical.com/
Discussion of overwrite-mode consumption identified an iterator as a
better zero-copy interface than callbacks:
https://lore.kernel.org/bpf/CAEf4Bzaq5drHWChXoRBnrmkb6reAsSVj8r=uByFSup31FMA7hw@mail.gmail.com/
Add a caller-owned iterator for consuming one ring buffer map directly.
It returns one borrowed record at a time, allowing ordinary loop
control. Each next call ends the previous borrow and lets the caller
choose whether to publish consumed records or defer publication for
batching. Existing callback-based consumption uses the same internal
iterator core while preserving callback return semantics and per-record
consumer position updates. Callback-based consumption and public
iteration of overwrite maps remain unsupported.
This v3 targets bpf-next/for-next. The following patches from v2 are
omitted after rebasing:
- 1/8, "libbpf: ringbuf: Honor zero consume bounds", was applied as
40edacd47993.
- 2/8, "libbpf: ringbuf: Prevent NULL callback crash", was applied as
c0628a0659b6.
- 3/8, "libbpf: ringbuf: Reject overwrite callback use", was applied as
f756eedb24d4.
- 4/8, "libbpf: ringbuf: Handle position counter wrap", is superseded by
Israel Téllez García's independently developed fix fdd4fad0bbbd.
- 5/8, "bpf: ringbuf: Handle pending position wrap", is superseded by
Israel's independently developed fix 6ff5b56a50c5.
Patches 1-3 resubmit v2 patches 6-8: "bpf: user_ringbuf: Handle position
wrap", "libbpf: ringbuf: Use compiler atomics", and "libbpf: ringbuf:
Prevent missed wakeups". They fix native-width wrap in the kernel's
user-ring consumer and a missed-notification race in libbpf. The atomic
conversion provides the portable full barrier needed by the wakeup fix.
Patches 4-5 add the caller-owned iterator and simplify its traversal.
The iterator preserves the wakeup handshake when destroyed before
exhaustion. Patches 6-8 contain the selftest refactoring, wakeup
regression coverage, callback-semantics checks, and iterator tests.
Selftests follow at the end, as requested during review.
Andrew Werner found the counter-wrap and missed-wakeup failures while
implementing Aya's ring buffer reader. Aya's original implementation
contains the equality reasoning and edge-triggered regression test:
https://github.com/aya-rs/aya/commit/e2cf734490bc188bcedb1eac92d23d81123e42cd
Aya later fixed the consumer ordering with an explicit fence:
https://github.com/aya-rs/aya/commit/7277a57ea8cdb74918d3096a4b22b6d814481973
This reroll continues the ring buffer fixes series and brings the
callback-free API into the same submission. The original callback API
proposal is a related submission:
https://lore.kernel.org/bpf/20260611-bpf-ringbuf-callback-v1-1-c32b6cb6ce40@kernel.org/
Review requesting that callback validation and the new API be discussed
together:
https://lore.kernel.org/bpf/CAADnVQLuNSE5AM6Cqzs4yDFrhTyD=j-y5dfya3dLwk3si9v26w@mail.gmail.com/
Earlier versions were developed with Codex (GPT-5.5). Codex (GPT-6)
assisted with combining the series, addressing review, and revising
the iterator, tests, and changelogs.
The libbpf build and BPF selftests have not been run.
Signed-off-by: Tamir Duberstein <tamird@kernel.org>
---
Changes in v3:
- Combine the remaining fixes with a new caller-owned iterator and its
tests, targeting bpf-next/for-next.
- Drop v2 patches 1-3, which were applied, and v2 patches 4-5, whose
issues were independently fixed, as listed above.
- Preserve -E2BIG for oversized user-ring samples when validating the
advertised window.
- Move the fixes' selftests to separate commits at the end.
- Simplify the wakeup fix's bound and error exits while retaining its
fenced busy retry.
- Keep the compiler atomic stores on single lines and follow the BPF
multiline comment style.
- Link to v2:
https://lore.kernel.org/bpf/20260618-bpf-ringbuf-fixes-v2-0-33fde039ddf3@kernel.org/
Changes in v2:
- Fix additional native 32-bit wrap paths found by Sashiko review.
- Explain modular position comparisons and leave mixed-bitness ABI work
separate.
- Preserve the wakeup handshake across bounded and callback returns.
- Follow BPF comment style and simplify wakeup test cleanup.
- Link to v1: https://patch.msgid.link/20260613-bpf-ringbuf-fixes-v1-0-e623481cb724@kernel.org
---
Tamir Duberstein (8):
bpf: user_ringbuf: Handle position wrap
libbpf: ringbuf: Use compiler atomics
libbpf: ringbuf: Prevent missed wakeups
libbpf: ringbuf: Add a caller-owned iterator
libbpf: ringbuf: Simplify iterator implementation
selftests/bpf: Refactor lifetimes and test wakeups
selftests/bpf: Verify ringbuf callback semantics
selftests/bpf: Test caller-owned ring iterators
kernel/bpf/ringbuf.c | 11 +-
tools/lib/bpf/libbpf.h | 90 +++
tools/lib/bpf/libbpf.map | 3 +
tools/lib/bpf/ringbuf.c | 187 ++++--
tools/testing/selftests/bpf/prog_tests/ringbuf.c | 660 ++++++++++++++++-----
tools/testing/selftests/bpf/progs/test_ringbuf_n.c | 9 +-
6 files changed, 757 insertions(+), 203 deletions(-)
---
base-commit: 82cc2b4383e3edfa1026c8612fb4e90a9329ba11
base-tree: git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
change-id: 20260613-bpf-ringbuf-fixes-e9a8b3c6125b
Best regards,
--
Tamir Duberstein <tamird@kernel.org>
next reply other threads:[~2026-11-20 20:33 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-20 20:33 Tamir Duberstein [this message]
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 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 20:33 ` [PATCH bpf-next v3 5/8] libbpf: ringbuf: Simplify iterator implementation Tamir Duberstein
2026-11-20 20:33 ` [PATCH bpf-next v3 6/8] selftests/bpf: Refactor lifetimes and test wakeups 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-0-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=sashiko-bot@kernel.org \
--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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®