BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Kumar Kartikeya Dwivedi" <memxor@gmail.com>, bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next v1 04/10] libbpf: Account for the overwrite position in ring__avail_data_size()
Date: Mon, 23 Nov 2026 21:22:42 +0000	[thread overview]
Message-ID: <sashiko-outbox-230081@kernel.org> (raw)
In-Reply-To: <853ef5deb34f53b2007ab47699c889b730d87805.1795467315.git.memxor@gmail.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] libbpf: load ordering race and underflow in
  ring__avail_data_size()
--

commit 2b238949efda4fe4f9b112e9f915c9221770464d
Author: Kumar Kartikeya Dwivedi <memxor@gmail.com>

libbpf: Account for the overwrite position in ring__avail_data_size()

This commit updates ring__avail_data_size() in 64-bit libbpf to read
overwrite_pos from the producer page on overwrite-mode ring buffers and
cap the available data size at producer_pos - overwrite_pos.

> diff --git a/tools/lib/bpf/ringbuf.c b/tools/lib/bpf/ringbuf.c
> index 78fcb3df243f9..271c6b9b130de 100644
> --- a/tools/lib/bpf/ringbuf.c
> +++ b/tools/lib/bpf/ringbuf.c
[ ... ]
> @@ -426,11 +434,16 @@ unsigned long ring__producer_pos(const struct ring *r)
>  
>  size_t ring__avail_data_size(const struct ring *r)
>  {
> -	unsigned long cons_pos, prod_pos;
> +	unsigned long cons_pos, prod_pos, over_pos;
>  
>  	cons_pos = ring__consumer_pos(r);
>  	prod_pos = ring__producer_pos(r);
> -	return prod_pos - cons_pos;
> +	if (!r->overwrite_pos)
> +		return prod_pos - cons_pos;
> +
> +	/* records before overwrite_pos are gone */
> +	over_pos = smp_load_acquire(r->overwrite_pos);
> +	return min(prod_pos - cons_pos, prod_pos - over_pos);
>  }

[Severity: Medium]
Can prod_pos - over_pos underflow when producer_pos is loaded before
overwrite_pos?

CPU0 (userspace consumer)         CPU1 (BPF producer)
ring__avail_data_size()
  prod_pos = ring__producer_pos(r)
                                  __bpf_ringbuf_reserve()
                                    /* advances rb->overwrite_pos
                                     * and rb->producer_pos
                                     */
  over_pos = smp_load_acquire(r->overwrite_pos)
  prod_pos - over_pos             <-- underflows if over_pos > prod_pos

When multiple reservations advance overwrite_pos past the stale prod_pos
snapshot, prod_pos - over_pos wraps around unsigned long, causing min()
to fall back to prod_pos - cons_pos and include already overwritten
records.  If a single reservation advances overwrite_pos up to the
sampled prod_pos, ring__avail_data_size() returns 0 for a full buffer.

In the kernel, ringbuf_avail_data_sz() loads overwrite_pos before
producer_pos so that prod_pos is at least as recent as over_pos:

kernel/bpf/ringbuf.c:ringbuf_avail_data_sz() {
    ...
	if (unlikely(rb->overwrite_mode)) {
		over_pos = smp_load_acquire(&rb->overwrite_pos);
		prod_pos = smp_load_acquire(&rb->producer_pos);
		return min(prod_pos - cons_pos, prod_pos - over_pos);
	}
    ...
}

Loading r->overwrite_pos with smp_load_acquire() before calling
ring__producer_pos(r) avoids the underflow.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1795467315.git.memxor@gmail.com?part=4

  reply	other threads:[~2026-11-23 21:22 UTC|newest]

Thread overview: 15+ 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-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-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-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 [this message]
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-23 21:13 ` [PATCH bpf-next v1 06/10] libbpf: Add ring_buffer__wait() Kumar Kartikeya Dwivedi
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
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

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-230081@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox