From: sashiko-bot@kernel.org
To: "Kumar Kartikeya Dwivedi" <memxor@gmail.com>, bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next v1 07/10] libbpf: Support overwritable ring buffers in the record API
Date: Mon, 23 Nov 2026 21:28:28 +0000 [thread overview]
Message-ID: <sashiko-outbox-230084@kernel.org> (raw)
In-Reply-To: <8115836d8bcbb9d59e2a350f9fb999f5ab497348.1795467315.git.memxor@gmail.com>
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] libbpf: silent loss of valid records via batch_pos in
ring__invalidated()
--
commit d2233d2ea9e5d957fed51c54cbd2073eb0c4fc69
Author: Kumar Kartikeya Dwivedi <memxor@gmail.com>
libbpf: Support overwritable ring buffers in the record API
This commit adds support for overwritable BPF ring buffers
(BPF_F_RB_OVERWRITE) to the libbpf record API and introduces
ring__invalidated() to detect whether records returned since the last
release or rewind were overwritten.
> diff --git a/tools/lib/bpf/ringbuf.c b/tools/lib/bpf/ringbuf.c
> index 12d586afed1dd..b4e9e27951eb8 100644
> --- a/tools/lib/bpf/ringbuf.c
> +++ b/tools/lib/bpf/ringbuf.c
[ ... ]
> @@ -497,11 +500,72 @@ static int ringbuf_validate_rec_api(const struct ring *r)
> {
> if (unlikely(r->sample_cb))
> return -EINVAL;
> - if (unlikely(r->overwrite))
> + /* 32-bit: no overwrite_pos, and positions wrap after 4 GiB of records */
> + if (unlikely(r->overwrite && !r->overwrite_pos))
> return -EOPNOTSUPP;
> return 0;
> }
>
> +/*
> + * Overwrite mode: check that whatever was read at pos, and at later positions,
> + * was not overwritten while we were reading it. The producer can overwrite pos
> + * once it is a ring size ahead of pos.
> + */
> +static bool ringbuf_pos_valid(const struct ring *r, unsigned long pos, unsigned long *prod_pos)
> +{
> + /* pairs with smp_wmb() after the producer_pos update in the kernel */
> + smp_rmb();
> + *prod_pos = smp_load_acquire(r->producer_pos);
> + return *prod_pos - pos <= r->mask;
> +}
> +
> +/* ring__next() for overwritable ring buffers */
> +static const struct ring_rec *ringbuf_overwrite_next(struct ring *r)
> +{
> + unsigned long pos = r->next_pos, prod_pos;
> + __u32 *len_ptr, len;
> +
> + prod_pos = smp_load_acquire(r->producer_pos);
> + for (;;) {
> + if (prod_pos - pos > r->mask) {
> + /*
> + * The producer lapped us, continue from the oldest record.
> + * overwrite_pos is never ahead of a producer_pos that is
> + * loaded after it.
> + */
> + pos = smp_load_acquire(r->overwrite_pos);
> + prod_pos = smp_load_acquire(r->producer_pos);
> + continue;
> + }
> + r->next_pos = pos;
> + if (pos == prod_pos)
> + return NULL;
> +
> + len_ptr = r->data + (pos & r->mask);
> + len = smp_load_acquire(len_ptr);
> + /* if the producer lapped us, this is not a record header */
> + if (!ringbuf_pos_valid(r, pos, &prod_pos))
> + continue;
> + /* record is not committed yet */
> + if (len & BPF_RINGBUF_BUSY_BIT)
> + return NULL;
> +
> + r->next_pos = pos + roundup_len(len);
> + if (len & BPF_RINGBUF_DISCARD_BIT) {
> + pos = r->next_pos;
> + continue;
> + }
> + if (!r->has_batch) {
> + r->batch_pos = pos;
> + r->has_batch = true;
> + }
> + r->rec.data = (void *)len_ptr + BPF_RINGBUF_HDR_SZ;
> + r->rec.pos = pos;
> + r->rec.len = len;
> + return &r->rec;
> + }
> +}
[ ... ]
> @@ -540,9 +607,20 @@ const struct ring_rec *ring__next(struct ring *r)
> }
> }
>
> +bool ring__invalidated(const struct ring *r)
> +{
> + unsigned long prod_pos;
> +
> + if (!r->has_batch)
> + return false;
> +
> + /* records are overwritten in order, so it is enough to check the first one */
> + return !ringbuf_pos_valid(r, r->batch_pos, &prod_pos);
> +}
> +
> int ring__release(struct ring *r)
> {
> - unsigned long pos = r->next_pos;
> + unsigned long pos = r->next_pos, prod_pos;
> __u32 len;
> int err;
>
> err = ringbuf_validate_rec_api(r);
> if (err)
> return libbpf_err(err);
>
> + r->has_batch = false;
> smp_store_release(r->consumer_pos, pos);
[Severity: Medium]
Can checking r->batch_pos in ring__invalidated() cause the documented
consumption loop to drop valid records that were never overwritten?
In the per-record loop:
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);
r->batch_pos is only set on the first record returned after
ring__release() or ring__rewind(), while r->next_pos advances past every
record returned by ring__next().
If the inner loop processes record A at r->batch_pos and then reads a
later record B, the producer can overwrite record A without reaching
record B. ring__invalidated() still checks r->batch_pos, so it returns
true for record B even though B's data copy is valid. The caller then
breaks out of the inner loop without handling B, and ring__release()
advances r->consumer_pos to r->next_pos (past B), permanently skipping
record B.
Similarly, if the producer laps pos on a later iteration of the same
batch (when r->has_batch is already true), ringbuf_overwrite_next() jumps
to r->overwrite_pos, advances r->next_pos past the oldest valid record
there, and returns it with r->batch_pos still pointing to the lapped start
of the batch. ring__invalidated() is then guaranteed to return true, so
ring__release() drops that record too (and in the ring__rewind() dump
pattern, jumping to r->overwrite_pos mid-batch keeps returning records for
a batch that is already invalidated).
--
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1795467315.git.memxor@gmail.com?part=7
next prev parent reply other threads:[~2026-11-23 21:28 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-23 21:13 [PATCH bpf-next v1 00/10] Ring Buffer Iterator API Kumar Kartikeya Dwivedi
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-24 7:38 ` Xu Kuohai
2026-11-24 17:44 ` Tamir Duberstein
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-24 7:55 ` Xu Kuohai
2026-11-24 17:44 ` Tamir Duberstein
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:46 ` bot+bpf-ci
2026-11-24 17:44 ` Tamir Duberstein
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:22 ` sashiko-bot
2026-11-24 17:44 ` Tamir Duberstein
2026-11-25 20:49 ` Andrii Nakryiko
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-24 17:44 ` Tamir Duberstein
2026-11-23 21:13 ` [PATCH bpf-next v1 06/10] libbpf: Add ring_buffer__wait() Kumar Kartikeya Dwivedi
2026-11-24 17:44 ` Tamir Duberstein
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:28 ` sashiko-bot [this message]
2026-11-25 20:49 ` Andrii Nakryiko
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:20 ` sashiko-bot
2026-11-23 21:13 ` [PATCH bpf-next v1 10/10] selftests/bpf: Add tests for the ring buffer record API Kumar Kartikeya Dwivedi
2026-11-24 17:44 ` Tamir Duberstein
2026-11-25 20:51 ` [PATCH bpf-next v1 00/10] Ring Buffer Iterator API Andrii Nakryiko
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=sashiko-outbox-230084@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=memxor@gmail.com \
--cc=sashiko-reviews@lists.linux.dev \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.