mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH bpf-next v3 0/8] bpf: Fix ring buffers and add a libbpf iterator
@ 2026-11-20 20:33 Tamir Duberstein
  2026-11-20 20:33 ` [PATCH bpf-next v3 1/8] bpf: user_ringbuf: Handle position wrap Tamir Duberstein
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: Tamir Duberstein @ 2026-11-20 20:33 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau,
	Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis, Shuah Khan,
	Andrea Righi, Xu Kuohai, Andrea Righi, Bing-Jhong Billy Jheng,
	David Vernet, Ihor Solodrai
  Cc: bpf, linux-kselftest, linux-kernel, Andrew Werner, Zvi Effron,
	Andrii Nakryiko, Tamir Duberstein, Sashiko

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>


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

end of thread, other threads:[~2026-11-20 20:34 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 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

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®