Skip to content

bpf: Fix ring buffers and add a libbpf iterator

Tamir Duberstein bpf linux-kselftest lkml 3 versions – In review

Get series with b4
b4 am [email protected] change-id 20260613-bpf-ringbuf-fixes-e9a8b3c6125b

Versions newest first

  1. v3 Latest8 patches 7 repliesIn review
  2. v28 patches 19 repliesSuperseded
  3. v16 patches 17 repliesSuperseded

Changes in v3 from the cover letter

- 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/[email protected]/

Patches in v3 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/8bpf: user_ringbuf: Handle position wrapkernel/bpf/ringbuf.c+8 -30 replies
2/8libbpf: ringbuf: Use compiler atomicstools/lib/bpf/ringbuf.c+8 -91 reply
3/8libbpf: ringbuf: Prevent missed wakeupstools/lib/bpf/libbpf.h tools/lib/bpf/ringbuf.c+37 -40 replies
4/8libbpf: ringbuf: Add a caller-owned iteratortools/lib/bpf/libbpf.h tools/lib/bpf/libbpf.map tools/lib/bpf/ringbuf.c+207 -355 replies
5/8libbpf: ringbuf: Simplify iterator implementationtools/lib/bpf/ringbuf.c+30 -380 replies
6/8selftests/bpf: Refactor lifetimes and test wakeupstools/testing/selftests/bpf/prog_tests/ringbuf.c+310 -1551 reply
7/8selftests/bpf: Verify ringbuf callback semanticstools/testing/selftests/bpf/prog_tests/ringbuf.c tools/testing/selftests/bpf/progs/test_ringbuf_n.c+31 -50 replies
8/8selftests/bpf: Test caller-owned ring iteratorstools/testing/selftests/bpf/prog_tests/ringbuf.c tools/testing/selftests/bpf/progs/test_ringbuf_n.c+173 -10 replies

Reviews trailers per patch

2 of 8 patches have a Reviewed-by or Acked-by.

PatchEmil TsalapatisEduard Zingerman
1/8
2/8R-b
3/8R-b
4/8
5/8
6/8
7/8
8/8

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v3 thread

Cover letter [PATCH bpf-next v3 0/8] bpf: Fix ring buffers and add a libbpf iterator

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/[email protected]/

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/[email protected]/

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.

---
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/[email protected]/

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/[email protected]

---
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,

Signature · 1 line
Tamir Duberstein <[email protected]>