* [PATCH bpf] bpf: Fix pending_pos walk on 32-bit ring position wrap
@ 2026-08-06 13:02 Israel Téllez García
2026-08-06 13:20 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Israel Téllez García @ 2026-08-06 13:02 UTC (permalink / raw)
To: Andrii Nakryiko, Alexei Starovoitov, Daniel Borkmann
Cc: Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau,
Song Liu, Yonghong Song, Jiri Olsa, bpf, linux-kernel,
Israel Téllez García
The reservation path caches the position of the oldest not-yet-committed
record in rb->pending_pos and advances it past already committed records
on every reservation:
while (pend_pos < prod_pos) {
consumer_pos, producer_pos and pending_pos are unsigned long, i.e.
32-bit on 32-bit architectures, and Documentation/bpf/ringbuf.rst states
that these counters may wrap around there. Every other comparison in the
file is written as a difference, so modular arithmetic keeps them
correct across the wrap. This one is an ordering comparison, and it is
not wrap-safe.
Once producer_pos wraps past 2^32, prod_pos is small while pend_pos
still holds its pre-wrap value, so the loop condition is false and
pending_pos is never advanced again. Reservations keep succeeding for a
while, because bpf_ringbuf_has_space() uses differences, but
new_prod_pos - pend_pos grows as the producer advances, and once it
exceeds rb->mask every subsequent __bpf_ringbuf_reserve() call fails:
the kernel believes a pending record spans the whole buffer. The ring
never recovers, bpf_ringbuf_output() drops every event from then on, and
nothing is logged.
Observed on four armv7 devices (i.MX7 Dual, 6.6.52) running a
tracepoint-based collector with a 512 KiB ring and 160-byte records.
Every one of them stopped delivering after exactly 26846821 records and
4295491360 bytes had passed through the ring, at event rates between 441
and 862 records/s, that is after 8 h to 17 h of uptime: the trigger is
the byte count, not time or load. That figure is 2^32 plus 524064 bytes,
and the excess is one ring's worth of grace period, as expected while
new_prod_pos - pend_pos is still below rb->mask. The last reservation
that fits is the largest record boundary X with X + 160 <= 524287, and
since 2^32 mod 160 = 96 the boundaries after the wrap sit at
X = 64 (mod 160), giving X = 524064. Userspace kept consuming normally
until the producer stopped, then read zero records for good. With this
patch applied, one of the four devices took 10 GiB through the same ring
with no stall, while the three unpatched ones kept wedging at the same
byte count.
64-bit hosts are unaffected in practice: their counters would need
16 EiB to wrap.
Compare the two positions as a signed difference, which is wrap-safe
here: the real distance between pend_pos and prod_pos is bounded by the
ring size, far below the point where the sign would become ambiguous.
Fixes: cfa1a2329a69 ("bpf: Fix overrunning reservations in ringbuf")
Signed-off-by: Israel Téllez García <i.tellez@btesa.com>
---
kernel/bpf/ringbuf.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/kernel/bpf/ringbuf.c b/kernel/bpf/ringbuf.c
index 35ae64ade36b..2c08a98370d5 100644
--- a/kernel/bpf/ringbuf.c
+++ b/kernel/bpf/ringbuf.c
@@ -482,7 +482,7 @@ static void *__bpf_ringbuf_reserve(struct bpf_ringbuf *rb, u64 size)
prod_pos = rb->producer_pos;
new_prod_pos = prod_pos + len;
- while (pend_pos < prod_pos) {
+ while ((long)(prod_pos - pend_pos) > 0) {
hdr = (void *)rb->data + (pend_pos & rb->mask);
hdr_len = READ_ONCE(hdr->len);
if (hdr_len & BPF_RINGBUF_BUSY_BIT)
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH bpf] bpf: Fix pending_pos walk on 32-bit ring position wrap
2026-08-06 13:02 [PATCH bpf] bpf: Fix pending_pos walk on 32-bit ring position wrap Israel Téllez García
@ 2026-08-06 13:20 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-08-06 13:20 UTC (permalink / raw)
To: Israel Téllez García; +Cc: bpf
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
Pre-existing issues:
- [High] `ringbuf_avail_data_sz` incorrectly handles `overwrite_mode` on 32-bit architectures because `max(cons_pos, over_pos)` fails on wrapped sequence numbers.
- [High] `__bpf_user_ringbuf_peek` permanently stalls on 32-bit architectures (and for 32-bit userspace on 64-bit kernels) when the ringbuffer hits 4GB of traffic.
- [High] The user-space ringbuffer consumer loop in `libbpf` uses `cons_pos < prod_pos`, which permanently hangs processing after 4GB of data on 32-bit architectures.
- [Medium] `ringbuf_avail_data_sz()` loads `overwrite_pos` before `producer_pos`, violating the explicit memory barrier contract and risking reads of stale state.
--
--- Patch [1]: [PATCH bpf] bpf: Fix pending_pos walk on 32-bit ring position wrap ---
commit 617dac82211278dfe2a6441e138c7025d95f169b
Author: Israel Téllez García <i.tellez@btesa.com>
bpf: Fix pending_pos walk on 32-bit ring position wrap
This patch fixes a 32-bit wrap-around bug in __bpf_ringbuf_reserve() by
changing an absolute ordering comparison to a signed difference. This ensures
the pending position is correctly advanced even when the counters wrap past 4GB.
> diff --git a/kernel/bpf/ringbuf.c b/kernel/bpf/ringbuf.c
> index 35ae64ade36b8..2c08a98370d5e 100644
> --- a/kernel/bpf/ringbuf.c
> +++ b/kernel/bpf/ringbuf.c
> @@ -482,7 +482,7 @@ static void *__bpf_ringbuf_reserve(struct bpf_ringbuf *rb, u64 size)
> prod_pos = rb->producer_pos;
> new_prod_pos = prod_pos + len;
>
> - while (pend_pos < prod_pos) {
> + while ((long)(prod_pos - pend_pos) > 0) {
> hdr = (void *)rb->data + (pend_pos & rb->mask);
> hdr_len = READ_ONCE(hdr->len);
> if (hdr_len & BPF_RINGBUF_BUSY_BIT)
While reviewing this fix for the 32-bit wrap-around in __bpf_ringbuf_reserve(),
I noticed a few other similar pre-existing 32-bit wrap-around and concurrency
issues in the ringbuf code.
This is a pre-existing issue, but there's a similar wrap issue in
ringbuf_avail_data_sz():
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 prod_pos - max(cons_pos, over_pos);
...
}
[Severity: High]
Will this code incorrectly handle overwrite_mode on 32-bit architectures?
The max() macro performs an absolute comparison. If over_pos wraps while
cons_pos has not, max() will incorrectly select cons_pos.
This calculation then evaluates modularly to a value larger than the ring size,
returning an artificially inflated available data size that permanently starves
pollers.
[Severity: Medium]
Also in ringbuf_avail_data_sz(), could this load order violate the producer's
barrier semantics?
The producer updates overwrite_pos and then executes
smp_store_release(&rb->producer_pos, ...). The consumer must read producer_pos
with smp_load_acquire() first to guarantee it observes the new overwrite_pos.
By reading overwrite_pos first, a race window allows observing a stale, smaller
overwrite_pos alongside the new producer_pos, throwing off the size calculation.
This isn't a bug introduced by this patch, but __bpf_user_ringbuf_peek() also
permanently stalls on 32-bit architectures:
kernel/bpf/ringbuf.c:__bpf_user_ringbuf_peek() {
...
u64 cons_pos, prod_pos;
/* Synchronizes with smp_store_release() in user-space producer. */
prod_pos = smp_load_acquire(&rb->producer_pos);
if (prod_pos % 8)
return -EINVAL;
/* Synchronizes with smp_store_release() in __bpf_user_ringbuf_sample_release() */
cons_pos = smp_load_acquire(&rb->consumer_pos);
if (cons_pos >= prod_pos)
return -ENODATA;
...
}
[Severity: High]
Can the zero-extension of 32-bit wrapping counters to 64-bit variables break
modular arithmetic here?
When producer_pos wraps past 4GB to a small value while consumer_pos is large,
the condition evaluates as true (e.g., 4294967200ULL >= 10ULL).
The consumer stops, preventing consumer_pos from ever wrapping to recover,
which causes a permanent stall.
This is a pre-existing issue, but I found a similar wrap issue in the libbpf
user-space consumer:
tools/lib/bpf/ringbuf.c:ringbuf_process_ring() {
...
unsigned long cons_pos, prod_pos;
...
prod_pos = smp_load_acquire(r->producer_pos);
while (cons_pos < prod_pos) {
len_ptr = r->data + (cons_pos & r->mask);
...
}
[Severity: High]
Would this absolute comparison permanently hang event consumption after 4GB of
data on 32-bit architectures?
When prod_pos wraps past 4GB, it becomes smaller than the non-wrapped cons_pos.
The absolute comparison fails, the loop terminates prematurely, and since no
data is consumed, cons_pos never wraps to recover.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260806130214.66028-1-i.tellez@btesa.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-08-06 13:20 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-06 13:02 [PATCH bpf] bpf: Fix pending_pos walk on 32-bit ring position wrap Israel Téllez García
2026-08-06 13:20 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox