> libbpf: Allow callbacks at consumption time
>
> Ring buffer callbacks and contexts are fixed when each ring is added to
> a manager. Callers that drain records into per-operation state, such as
> a bounded destination buffer, must therefore keep mutable dispatch state
> alive for the lifetime of the manager or rebuild it for each operation.
>
> Add bounded manager and per-ring consumption APIs that take a callback
> and context for one operation. The supplied callback overrides the
> configured callback without changing it. This also allows managers used
> only through the new APIs to be created without a setup-time callback.
>
> Make zero-record requests return without invoking callbacks so all
> bounded consumption APIs preserve their bound.
>
> Signed-off-by: Tamir Duberstein <[email protected]>
Part of this change looks like a bug fix. The early return added to
ringbuf_process_ring():
if (n == 0)
return 0;
makes a request for zero records return without consuming or dispatching
anything. Before this, the cnt >= n limit was only checked after the
sample callback ran, so an n == 0 request still consumed and dispatched
one record and violated the requested bound.
That bound-violation behavior came in with the bounded consumption APIs
ring_buffer__consume_n() / ring__consume_n() and the n parameter on
ringbuf_process_ring(). Should this carry:
Fixes: 4d22ea94ea33 ("libbpf: Add ring__consume_n / ring_buffer__consume_n")
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/27394962126